FFmpeg
 All Data Structures Namespaces Files Functions Variables Typedefs Enumerations Enumerator Macros Groups Pages
dnxhdenc.c
Go to the documentation of this file.
1 /*
2  * VC3/DNxHD encoder
3  * Copyright (c) 2007 Baptiste Coudurier <baptiste dot coudurier at smartjog dot com>
4  * Copyright (c) 2011 MirriAd Ltd
5  *
6  * VC-3 encoder funded by the British Broadcasting Corporation
7  * 10 bit support added by MirriAd Ltd, Joseph Artsimovich <joseph@mirriad.com>
8  *
9  * This file is part of FFmpeg.
10  *
11  * FFmpeg is free software; you can redistribute it and/or
12  * modify it under the terms of the GNU Lesser General Public
13  * License as published by the Free Software Foundation; either
14  * version 2.1 of the License, or (at your option) any later version.
15  *
16  * FFmpeg is distributed in the hope that it will be useful,
17  * but WITHOUT ANY WARRANTY; without even the implied warranty of
18  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
19  * Lesser General Public License for more details.
20  *
21  * You should have received a copy of the GNU Lesser General Public
22  * License along with FFmpeg; if not, write to the Free Software
23  * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
24  */
25 
26 #include "libavutil/attributes.h"
27 #include "libavutil/internal.h"
28 #include "libavutil/opt.h"
29 #include "libavutil/timer.h"
30 
31 #include "avcodec.h"
32 #include "blockdsp.h"
33 #include "fdctdsp.h"
34 #include "internal.h"
35 #include "mpegvideo.h"
36 #include "pixblockdsp.h"
37 #include "dnxhdenc.h"
38 
39 
40 // The largest value that will not lead to overflow for 10bit samples.
41 #define DNX10BIT_QMAT_SHIFT 18
42 #define RC_VARIANCE 1 // use variance or ssd for fast rc
43 #define LAMBDA_FRAC_BITS 10
44 
45 #define VE AV_OPT_FLAG_VIDEO_PARAM | AV_OPT_FLAG_ENCODING_PARAM
46 static const AVOption options[] = {
47  { "nitris_compat", "encode with Avid Nitris compatibility",
48  offsetof(DNXHDEncContext, nitris_compat), AV_OPT_TYPE_BOOL, { .i64 = 0 }, 0, 1, VE },
49  { "ibias", "intra quant bias",
50  offsetof(DNXHDEncContext, intra_quant_bias), AV_OPT_TYPE_INT,
51  { .i64 = 0 }, INT_MIN, INT_MAX, VE },
52  { NULL }
53 };
54 
55 static const AVClass dnxhd_class = {
56  .class_name = "dnxhd",
57  .item_name = av_default_item_name,
58  .option = options,
59  .version = LIBAVUTIL_VERSION_INT,
60 };
61 
62 static void dnxhd_8bit_get_pixels_8x4_sym(int16_t *av_restrict block,
63  const uint8_t *pixels,
64  ptrdiff_t line_size)
65 {
66  int i;
67  for (i = 0; i < 4; i++) {
68  block[0] = pixels[0];
69  block[1] = pixels[1];
70  block[2] = pixels[2];
71  block[3] = pixels[3];
72  block[4] = pixels[4];
73  block[5] = pixels[5];
74  block[6] = pixels[6];
75  block[7] = pixels[7];
76  pixels += line_size;
77  block += 8;
78  }
79  memcpy(block, block - 8, sizeof(*block) * 8);
80  memcpy(block + 8, block - 16, sizeof(*block) * 8);
81  memcpy(block + 16, block - 24, sizeof(*block) * 8);
82  memcpy(block + 24, block - 32, sizeof(*block) * 8);
83 }
84 
85 static av_always_inline
86 void dnxhd_10bit_get_pixels_8x4_sym(int16_t *av_restrict block,
87  const uint8_t *pixels,
88  ptrdiff_t line_size)
89 {
90  memcpy(block + 0 * 8, pixels + 0 * line_size, 8 * sizeof(*block));
91  memcpy(block + 7 * 8, pixels + 0 * line_size, 8 * sizeof(*block));
92  memcpy(block + 1 * 8, pixels + 1 * line_size, 8 * sizeof(*block));
93  memcpy(block + 6 * 8, pixels + 1 * line_size, 8 * sizeof(*block));
94  memcpy(block + 2 * 8, pixels + 2 * line_size, 8 * sizeof(*block));
95  memcpy(block + 5 * 8, pixels + 2 * line_size, 8 * sizeof(*block));
96  memcpy(block + 3 * 8, pixels + 3 * line_size, 8 * sizeof(*block));
97  memcpy(block + 4 * 8, pixels + 3 * line_size, 8 * sizeof(*block));
98 }
99 
101  int n, int qscale, int *overflow)
102 {
104  const int *qmat = n<4 ? ctx->q_intra_matrix[qscale] : ctx->q_chroma_intra_matrix[qscale];
105  int last_non_zero = 0;
106  int i;
107 
108  ctx->fdsp.fdct(block);
109 
110  // Divide by 4 with rounding, to compensate scaling of DCT coefficients
111  block[0] = (block[0] + 2) >> 2;
112 
113  for (i = 1; i < 64; ++i) {
114  int j = scantable[i];
115  int sign = FF_SIGNBIT(block[j]);
116  int level = (block[j] ^ sign) - sign;
117  level = level * qmat[j] >> DNX10BIT_QMAT_SHIFT;
118  block[j] = (level ^ sign) - sign;
119  if (level)
120  last_non_zero = i;
121  }
122 
123  /* we need this permutation so that we correct the IDCT, we only permute the !=0 elements */
124  if (ctx->idsp.perm_type != FF_IDCT_PERM_NONE)
126  scantable, last_non_zero);
127 
128  return last_non_zero;
129 }
130 
132 {
133  int i, j, level, run;
134  int max_level = 1 << (ctx->cid_table->bit_depth + 2);
135 
137  max_level, 4 * sizeof(*ctx->vlc_codes), fail);
139  max_level, 4 * sizeof(*ctx->vlc_bits), fail);
140  FF_ALLOCZ_OR_GOTO(ctx->m.avctx, ctx->run_codes,
141  63 * 2, fail);
142  FF_ALLOCZ_OR_GOTO(ctx->m.avctx, ctx->run_bits,
143  63, fail);
144 
145  ctx->vlc_codes += max_level * 2;
146  ctx->vlc_bits += max_level * 2;
147  for (level = -max_level; level < max_level; level++) {
148  for (run = 0; run < 2; run++) {
149  int index = (level << 1) | run;
150  int sign, offset = 0, alevel = level;
151 
152  MASK_ABS(sign, alevel);
153  if (alevel > 64) {
154  offset = (alevel - 1) >> 6;
155  alevel -= offset << 6;
156  }
157  for (j = 0; j < 257; j++) {
158  if (ctx->cid_table->ac_info[2*j+0] >> 1 == alevel &&
159  (!offset || (ctx->cid_table->ac_info[2*j+1] & 1) && offset) &&
160  (!run || (ctx->cid_table->ac_info[2*j+1] & 2) && run)) {
161  av_assert1(!ctx->vlc_codes[index]);
162  if (alevel) {
163  ctx->vlc_codes[index] =
164  (ctx->cid_table->ac_codes[j] << 1) | (sign & 1);
165  ctx->vlc_bits[index] = ctx->cid_table->ac_bits[j] + 1;
166  } else {
167  ctx->vlc_codes[index] = ctx->cid_table->ac_codes[j];
168  ctx->vlc_bits[index] = ctx->cid_table->ac_bits[j];
169  }
170  break;
171  }
172  }
173  av_assert0(!alevel || j < 257);
174  if (offset) {
175  ctx->vlc_codes[index] =
176  (ctx->vlc_codes[index] << ctx->cid_table->index_bits) | offset;
177  ctx->vlc_bits[index] += ctx->cid_table->index_bits;
178  }
179  }
180  }
181  for (i = 0; i < 62; i++) {
182  int run = ctx->cid_table->run[i];
183  av_assert0(run < 63);
184  ctx->run_codes[run] = ctx->cid_table->run_codes[i];
185  ctx->run_bits[run] = ctx->cid_table->run_bits[i];
186  }
187  return 0;
188 fail:
189  return AVERROR(ENOMEM);
190 }
191 
192 static av_cold int dnxhd_init_qmat(DNXHDEncContext *ctx, int lbias, int cbias)
193 {
194  // init first elem to 1 to avoid div by 0 in convert_matrix
195  uint16_t weight_matrix[64] = { 1, }; // convert_matrix needs uint16_t*
196  int qscale, i;
197  const uint8_t *luma_weight_table = ctx->cid_table->luma_weight;
198  const uint8_t *chroma_weight_table = ctx->cid_table->chroma_weight;
199 
201  (ctx->m.avctx->qmax + 1), 64 * sizeof(int), fail);
203  (ctx->m.avctx->qmax + 1), 64 * sizeof(int), fail);
205  (ctx->m.avctx->qmax + 1), 64 * 2 * sizeof(uint16_t),
206  fail);
208  (ctx->m.avctx->qmax + 1), 64 * 2 * sizeof(uint16_t),
209  fail);
210 
211  if (ctx->cid_table->bit_depth == 8) {
212  for (i = 1; i < 64; i++) {
213  int j = ctx->m.idsp.idct_permutation[ff_zigzag_direct[i]];
214  weight_matrix[j] = ctx->cid_table->luma_weight[i];
215  }
216  ff_convert_matrix(&ctx->m, ctx->qmatrix_l, ctx->qmatrix_l16,
217  weight_matrix, ctx->intra_quant_bias, 1,
218  ctx->m.avctx->qmax, 1);
219  for (i = 1; i < 64; i++) {
220  int j = ctx->m.idsp.idct_permutation[ff_zigzag_direct[i]];
221  weight_matrix[j] = ctx->cid_table->chroma_weight[i];
222  }
223  ff_convert_matrix(&ctx->m, ctx->qmatrix_c, ctx->qmatrix_c16,
224  weight_matrix, ctx->intra_quant_bias, 1,
225  ctx->m.avctx->qmax, 1);
226 
227  for (qscale = 1; qscale <= ctx->m.avctx->qmax; qscale++) {
228  for (i = 0; i < 64; i++) {
229  ctx->qmatrix_l[qscale][i] <<= 2;
230  ctx->qmatrix_c[qscale][i] <<= 2;
231  ctx->qmatrix_l16[qscale][0][i] <<= 2;
232  ctx->qmatrix_l16[qscale][1][i] <<= 2;
233  ctx->qmatrix_c16[qscale][0][i] <<= 2;
234  ctx->qmatrix_c16[qscale][1][i] <<= 2;
235  }
236  }
237  } else {
238  // 10-bit
239  for (qscale = 1; qscale <= ctx->m.avctx->qmax; qscale++) {
240  for (i = 1; i < 64; i++) {
241  int j = ff_zigzag_direct[i];
242 
243  /* The quantization formula from the VC-3 standard is:
244  * quantized = sign(block[i]) * floor(abs(block[i]/s) * p /
245  * (qscale * weight_table[i]))
246  * Where p is 32 for 8-bit samples and 8 for 10-bit ones.
247  * The s factor compensates scaling of DCT coefficients done by
248  * the DCT routines, and therefore is not present in standard.
249  * It's 8 for 8-bit samples and 4 for 10-bit ones.
250  * We want values of ctx->qtmatrix_l and ctx->qtmatrix_r to be:
251  * ((1 << DNX10BIT_QMAT_SHIFT) * (p / s)) /
252  * (qscale * weight_table[i])
253  * For 10-bit samples, p / s == 2 */
254  ctx->qmatrix_l[qscale][j] = (1 << (DNX10BIT_QMAT_SHIFT + 1)) /
255  (qscale * luma_weight_table[i]);
256  ctx->qmatrix_c[qscale][j] = (1 << (DNX10BIT_QMAT_SHIFT + 1)) /
257  (qscale * chroma_weight_table[i]);
258  }
259  }
260  }
261 
263  ctx->m.q_chroma_intra_matrix = ctx->qmatrix_c;
264  ctx->m.q_intra_matrix16 = ctx->qmatrix_l16;
265  ctx->m.q_intra_matrix = ctx->qmatrix_l;
266 
267  return 0;
268 fail:
269  return AVERROR(ENOMEM);
270 }
271 
273 {
274  FF_ALLOCZ_ARRAY_OR_GOTO(ctx->m.avctx, ctx->mb_rc, (ctx->m.avctx->qmax + 1), 8160 * sizeof(RCEntry), fail);
275  if (ctx->m.avctx->mb_decision != FF_MB_DECISION_RD)
277  ctx->m.mb_num, sizeof(RCCMPEntry), fail);
278 
279  ctx->frame_bits = (ctx->cid_table->coding_unit_size -
280  640 - 4 - ctx->min_padding) * 8;
281  ctx->qscale = 1;
282  ctx->lambda = 2 << LAMBDA_FRAC_BITS; // qscale 2
283  return 0;
284 fail:
285  return AVERROR(ENOMEM);
286 }
287 
289 {
290  DNXHDEncContext *ctx = avctx->priv_data;
291  int i, index, bit_depth, ret;
292 
293  switch (avctx->pix_fmt) {
294  case AV_PIX_FMT_YUV422P:
295  bit_depth = 8;
296  break;
298  bit_depth = 10;
299  break;
300  default:
301  av_log(avctx, AV_LOG_ERROR,
302  "pixel format is incompatible with DNxHD\n");
303  return AVERROR(EINVAL);
304  }
305 
306  ctx->cid = ff_dnxhd_find_cid(avctx, bit_depth);
307  if (!ctx->cid) {
308  av_log(avctx, AV_LOG_ERROR,
309  "video parameters incompatible with DNxHD. Valid DNxHD profiles:\n");
311  return AVERROR(EINVAL);
312  }
313  av_log(avctx, AV_LOG_DEBUG, "cid %d\n", ctx->cid);
314 
315  index = ff_dnxhd_get_cid_table(ctx->cid);
316  av_assert0(index >= 0);
317 
319 
320  ctx->m.avctx = avctx;
321  ctx->m.mb_intra = 1;
322  ctx->m.h263_aic = 1;
323 
324  avctx->bits_per_raw_sample = ctx->cid_table->bit_depth;
325 
326  ff_blockdsp_init(&ctx->bdsp, avctx);
327  ff_fdctdsp_init(&ctx->m.fdsp, avctx);
328  ff_mpv_idct_init(&ctx->m);
329  ff_mpegvideoencdsp_init(&ctx->m.mpvencdsp, avctx);
330  ff_pixblockdsp_init(&ctx->m.pdsp, avctx);
331  ff_dct_encode_init(&ctx->m);
332 
333  if (!ctx->m.dct_quantize)
335 
336  if (ctx->cid_table->bit_depth == 10) {
339  ctx->block_width_l2 = 4;
340  } else {
342  ctx->block_width_l2 = 3;
343  }
344 
345  if (ARCH_X86)
347 
348  ctx->m.mb_height = (avctx->height + 15) / 16;
349  ctx->m.mb_width = (avctx->width + 15) / 16;
350 
351  if (avctx->flags & AV_CODEC_FLAG_INTERLACED_DCT) {
352  ctx->interlaced = 1;
353  ctx->m.mb_height /= 2;
354  }
355 
356  ctx->m.mb_num = ctx->m.mb_height * ctx->m.mb_width;
357 
358 #if FF_API_QUANT_BIAS
361  ctx->intra_quant_bias = avctx->intra_quant_bias;
363 #endif
364  // XXX tune lbias/cbias
365  if ((ret = dnxhd_init_qmat(ctx, ctx->intra_quant_bias, 0)) < 0)
366  return ret;
367 
368  /* Avid Nitris hardware decoder requires a minimum amount of padding
369  * in the coding unit payload */
370  if (ctx->nitris_compat)
371  ctx->min_padding = 1600;
372 
373  if ((ret = dnxhd_init_vlc(ctx)) < 0)
374  return ret;
375  if ((ret = dnxhd_init_rc(ctx)) < 0)
376  return ret;
377 
378  FF_ALLOCZ_OR_GOTO(ctx->m.avctx, ctx->slice_size,
379  ctx->m.mb_height * sizeof(uint32_t), fail);
380  FF_ALLOCZ_OR_GOTO(ctx->m.avctx, ctx->slice_offs,
381  ctx->m.mb_height * sizeof(uint32_t), fail);
382  FF_ALLOCZ_OR_GOTO(ctx->m.avctx, ctx->mb_bits,
383  ctx->m.mb_num * sizeof(uint16_t), fail);
384  FF_ALLOCZ_OR_GOTO(ctx->m.avctx, ctx->mb_qscale,
385  ctx->m.mb_num * sizeof(uint8_t), fail);
386 
387 #if FF_API_CODED_FRAME
389  avctx->coded_frame->key_frame = 1;
392 #endif
393 
394  if (avctx->thread_count > MAX_THREADS) {
395  av_log(avctx, AV_LOG_ERROR, "too many threads\n");
396  return AVERROR(EINVAL);
397  }
398 
399  if (avctx->qmax <= 1) {
400  av_log(avctx, AV_LOG_ERROR, "qmax must be at least 2\n");
401  return AVERROR(EINVAL);
402  }
403 
404  ctx->thread[0] = ctx;
405  for (i = 1; i < avctx->thread_count; i++) {
406  ctx->thread[i] = av_malloc(sizeof(DNXHDEncContext));
407  memcpy(ctx->thread[i], ctx, sizeof(DNXHDEncContext));
408  }
409 
410  return 0;
411 fail: // for FF_ALLOCZ_OR_GOTO
412  return AVERROR(ENOMEM);
413 }
414 
416 {
417  DNXHDEncContext *ctx = avctx->priv_data;
418  static const uint8_t header_prefix[5] = { 0x00, 0x00, 0x02, 0x80, 0x01 };
419 
420  memset(buf, 0, 640);
421 
422  memcpy(buf, header_prefix, 5);
423  buf[5] = ctx->interlaced ? ctx->cur_field + 2 : 0x01;
424  buf[6] = 0x80; // crc flag off
425  buf[7] = 0xa0; // reserved
426  AV_WB16(buf + 0x18, avctx->height >> ctx->interlaced); // ALPF
427  AV_WB16(buf + 0x1a, avctx->width); // SPL
428  AV_WB16(buf + 0x1d, avctx->height >> ctx->interlaced); // NAL
429 
430  buf[0x21] = ctx->cid_table->bit_depth == 10 ? 0x58 : 0x38;
431  buf[0x22] = 0x88 + (ctx->interlaced << 2);
432  AV_WB32(buf + 0x28, ctx->cid); // CID
433  buf[0x2c] = ctx->interlaced ? 0 : 0x80;
434 
435  buf[0x5f] = 0x01; // UDL
436 
437  buf[0x167] = 0x02; // reserved
438  AV_WB16(buf + 0x16a, ctx->m.mb_height * 4 + 4); // MSIPS
439  buf[0x16d] = ctx->m.mb_height; // Ns
440  buf[0x16f] = 0x10; // reserved
441 
442  ctx->msip = buf + 0x170;
443  return 0;
444 }
445 
447 {
448  int nbits;
449  if (diff < 0) {
450  nbits = av_log2_16bit(-2 * diff);
451  diff--;
452  } else {
453  nbits = av_log2_16bit(2 * diff);
454  }
455  put_bits(&ctx->m.pb, ctx->cid_table->dc_bits[nbits] + nbits,
456  (ctx->cid_table->dc_codes[nbits] << nbits) +
457  av_mod_uintp2(diff, nbits));
458 }
459 
460 static av_always_inline
462  int last_index, int n)
463 {
464  int last_non_zero = 0;
465  int slevel, i, j;
466 
467  dnxhd_encode_dc(ctx, block[0] - ctx->m.last_dc[n]);
468  ctx->m.last_dc[n] = block[0];
469 
470  for (i = 1; i <= last_index; i++) {
471  j = ctx->m.intra_scantable.permutated[i];
472  slevel = block[j];
473  if (slevel) {
474  int run_level = i - last_non_zero - 1;
475  int rlevel = (slevel << 1) | !!run_level;
476  put_bits(&ctx->m.pb, ctx->vlc_bits[rlevel], ctx->vlc_codes[rlevel]);
477  if (run_level)
478  put_bits(&ctx->m.pb, ctx->run_bits[run_level],
479  ctx->run_codes[run_level]);
480  last_non_zero = i;
481  }
482  }
483  put_bits(&ctx->m.pb, ctx->vlc_bits[0], ctx->vlc_codes[0]); // EOB
484 }
485 
486 static av_always_inline
488  int qscale, int last_index)
489 {
490  const uint8_t *weight_matrix;
491  int level;
492  int i;
493 
494  weight_matrix = (n & 2) ? ctx->cid_table->chroma_weight
495  : ctx->cid_table->luma_weight;
496 
497  for (i = 1; i <= last_index; i++) {
498  int j = ctx->m.intra_scantable.permutated[i];
499  level = block[j];
500  if (level) {
501  if (level < 0) {
502  level = (1 - 2 * level) * qscale * weight_matrix[i];
503  if (ctx->cid_table->bit_depth == 10) {
504  if (weight_matrix[i] != 8)
505  level += 8;
506  level >>= 4;
507  } else {
508  if (weight_matrix[i] != 32)
509  level += 32;
510  level >>= 6;
511  }
512  level = -level;
513  } else {
514  level = (2 * level + 1) * qscale * weight_matrix[i];
515  if (ctx->cid_table->bit_depth == 10) {
516  if (weight_matrix[i] != 8)
517  level += 8;
518  level >>= 4;
519  } else {
520  if (weight_matrix[i] != 32)
521  level += 32;
522  level >>= 6;
523  }
524  }
525  block[j] = level;
526  }
527  }
528 }
529 
530 static av_always_inline int dnxhd_ssd_block(int16_t *qblock, int16_t *block)
531 {
532  int score = 0;
533  int i;
534  for (i = 0; i < 64; i++)
535  score += (block[i] - qblock[i]) * (block[i] - qblock[i]);
536  return score;
537 }
538 
539 static av_always_inline
540 int dnxhd_calc_ac_bits(DNXHDEncContext *ctx, int16_t *block, int last_index)
541 {
542  int last_non_zero = 0;
543  int bits = 0;
544  int i, j, level;
545  for (i = 1; i <= last_index; i++) {
546  j = ctx->m.intra_scantable.permutated[i];
547  level = block[j];
548  if (level) {
549  int run_level = i - last_non_zero - 1;
550  bits += ctx->vlc_bits[(level << 1) |
551  !!run_level] + ctx->run_bits[run_level];
552  last_non_zero = i;
553  }
554  }
555  return bits;
556 }
557 
558 static av_always_inline
559 void dnxhd_get_blocks(DNXHDEncContext *ctx, int mb_x, int mb_y)
560 {
561  const int bs = ctx->block_width_l2;
562  const int bw = 1 << bs;
563  const uint8_t *ptr_y = ctx->thread[0]->src[0] +
564  ((mb_y << 4) * ctx->m.linesize) + (mb_x << bs + 1);
565  const uint8_t *ptr_u = ctx->thread[0]->src[1] +
566  ((mb_y << 4) * ctx->m.uvlinesize) + (mb_x << bs);
567  const uint8_t *ptr_v = ctx->thread[0]->src[2] +
568  ((mb_y << 4) * ctx->m.uvlinesize) + (mb_x << bs);
569  PixblockDSPContext *pdsp = &ctx->m.pdsp;
570 
571  pdsp->get_pixels(ctx->blocks[0], ptr_y, ctx->m.linesize);
572  pdsp->get_pixels(ctx->blocks[1], ptr_y + bw, ctx->m.linesize);
573  pdsp->get_pixels(ctx->blocks[2], ptr_u, ctx->m.uvlinesize);
574  pdsp->get_pixels(ctx->blocks[3], ptr_v, ctx->m.uvlinesize);
575 
576  if (mb_y + 1 == ctx->m.mb_height && ctx->m.avctx->height == 1080) {
577  if (ctx->interlaced) {
578  ctx->get_pixels_8x4_sym(ctx->blocks[4],
579  ptr_y + ctx->dct_y_offset,
580  ctx->m.linesize);
581  ctx->get_pixels_8x4_sym(ctx->blocks[5],
582  ptr_y + ctx->dct_y_offset + bw,
583  ctx->m.linesize);
584  ctx->get_pixels_8x4_sym(ctx->blocks[6],
585  ptr_u + ctx->dct_uv_offset,
586  ctx->m.uvlinesize);
587  ctx->get_pixels_8x4_sym(ctx->blocks[7],
588  ptr_v + ctx->dct_uv_offset,
589  ctx->m.uvlinesize);
590  } else {
591  ctx->bdsp.clear_block(ctx->blocks[4]);
592  ctx->bdsp.clear_block(ctx->blocks[5]);
593  ctx->bdsp.clear_block(ctx->blocks[6]);
594  ctx->bdsp.clear_block(ctx->blocks[7]);
595  }
596  } else {
597  pdsp->get_pixels(ctx->blocks[4],
598  ptr_y + ctx->dct_y_offset, ctx->m.linesize);
599  pdsp->get_pixels(ctx->blocks[5],
600  ptr_y + ctx->dct_y_offset + bw, ctx->m.linesize);
601  pdsp->get_pixels(ctx->blocks[6],
602  ptr_u + ctx->dct_uv_offset, ctx->m.uvlinesize);
603  pdsp->get_pixels(ctx->blocks[7],
604  ptr_v + ctx->dct_uv_offset, ctx->m.uvlinesize);
605  }
606 }
607 
608 static av_always_inline
610 {
611  const static uint8_t component[8]={0,0,1,2,0,0,1,2};
612  return component[i];
613 }
614 
616  int jobnr, int threadnr)
617 {
618  DNXHDEncContext *ctx = avctx->priv_data;
619  int mb_y = jobnr, mb_x;
620  int qscale = ctx->qscale;
621  LOCAL_ALIGNED_16(int16_t, block, [64]);
622  ctx = ctx->thread[threadnr];
623 
624  ctx->m.last_dc[0] =
625  ctx->m.last_dc[1] =
626  ctx->m.last_dc[2] = 1 << (ctx->cid_table->bit_depth + 2);
627 
628  for (mb_x = 0; mb_x < ctx->m.mb_width; mb_x++) {
629  unsigned mb = mb_y * ctx->m.mb_width + mb_x;
630  int ssd = 0;
631  int ac_bits = 0;
632  int dc_bits = 0;
633  int i;
634 
635  dnxhd_get_blocks(ctx, mb_x, mb_y);
636 
637  for (i = 0; i < 8; i++) {
638  int16_t *src_block = ctx->blocks[i];
639  int overflow, nbits, diff, last_index;
640  int n = dnxhd_switch_matrix(ctx, i);
641 
642  memcpy(block, src_block, 64 * sizeof(*block));
643  last_index = ctx->m.dct_quantize(&ctx->m, block, 4 & (2*i),
644  qscale, &overflow);
645  ac_bits += dnxhd_calc_ac_bits(ctx, block, last_index);
646 
647  diff = block[0] - ctx->m.last_dc[n];
648  if (diff < 0)
649  nbits = av_log2_16bit(-2 * diff);
650  else
651  nbits = av_log2_16bit(2 * diff);
652 
653  av_assert1(nbits < ctx->cid_table->bit_depth + 4);
654  dc_bits += ctx->cid_table->dc_bits[nbits] + nbits;
655 
656  ctx->m.last_dc[n] = block[0];
657 
658  if (avctx->mb_decision == FF_MB_DECISION_RD || !RC_VARIANCE) {
659  dnxhd_unquantize_c(ctx, block, i, qscale, last_index);
660  ctx->m.idsp.idct(block);
661  ssd += dnxhd_ssd_block(block, src_block);
662  }
663  }
664  ctx->mb_rc[qscale][mb].ssd = ssd;
665  ctx->mb_rc[qscale][mb].bits = ac_bits + dc_bits + 12 +
666  8 * ctx->vlc_bits[0];
667  }
668  return 0;
669 }
670 
672  int jobnr, int threadnr)
673 {
674  DNXHDEncContext *ctx = avctx->priv_data;
675  int mb_y = jobnr, mb_x;
676  ctx = ctx->thread[threadnr];
677  init_put_bits(&ctx->m.pb, (uint8_t *)arg + 640 + ctx->slice_offs[jobnr],
678  ctx->slice_size[jobnr]);
679 
680  ctx->m.last_dc[0] =
681  ctx->m.last_dc[1] =
682  ctx->m.last_dc[2] = 1 << (ctx->cid_table->bit_depth + 2);
683  for (mb_x = 0; mb_x < ctx->m.mb_width; mb_x++) {
684  unsigned mb = mb_y * ctx->m.mb_width + mb_x;
685  int qscale = ctx->mb_qscale[mb];
686  int i;
687 
688  put_bits(&ctx->m.pb, 12, qscale << 1);
689 
690  dnxhd_get_blocks(ctx, mb_x, mb_y);
691 
692  for (i = 0; i < 8; i++) {
693  int16_t *block = ctx->blocks[i];
694  int overflow, n = dnxhd_switch_matrix(ctx, i);
695  int last_index = ctx->m.dct_quantize(&ctx->m, block, 4 & (2*i),
696  qscale, &overflow);
697  // START_TIMER;
698  dnxhd_encode_block(ctx, block, last_index, n);
699  // STOP_TIMER("encode_block");
700  }
701  }
702  if (put_bits_count(&ctx->m.pb) & 31)
703  put_bits(&ctx->m.pb, 32 - (put_bits_count(&ctx->m.pb) & 31), 0);
704  flush_put_bits(&ctx->m.pb);
705  return 0;
706 }
707 
709 {
710  int mb_y, mb_x;
711  int offset = 0;
712  for (mb_y = 0; mb_y < ctx->m.mb_height; mb_y++) {
713  int thread_size;
714  ctx->slice_offs[mb_y] = offset;
715  ctx->slice_size[mb_y] = 0;
716  for (mb_x = 0; mb_x < ctx->m.mb_width; mb_x++) {
717  unsigned mb = mb_y * ctx->m.mb_width + mb_x;
718  ctx->slice_size[mb_y] += ctx->mb_bits[mb];
719  }
720  ctx->slice_size[mb_y] = (ctx->slice_size[mb_y] + 31) & ~31;
721  ctx->slice_size[mb_y] >>= 3;
722  thread_size = ctx->slice_size[mb_y];
723  offset += thread_size;
724  }
725 }
726 
728  int jobnr, int threadnr)
729 {
730  DNXHDEncContext *ctx = avctx->priv_data;
731  int mb_y = jobnr, mb_x, x, y;
732  int partial_last_row = (mb_y == ctx->m.mb_height - 1) &&
733  ((avctx->height >> ctx->interlaced) & 0xF);
734 
735  ctx = ctx->thread[threadnr];
736  if (ctx->cid_table->bit_depth == 8) {
737  uint8_t *pix = ctx->thread[0]->src[0] + ((mb_y << 4) * ctx->m.linesize);
738  for (mb_x = 0; mb_x < ctx->m.mb_width; ++mb_x, pix += 16) {
739  unsigned mb = mb_y * ctx->m.mb_width + mb_x;
740  int sum;
741  int varc;
742 
743  if (!partial_last_row && mb_x * 16 <= avctx->width - 16) {
744  sum = ctx->m.mpvencdsp.pix_sum(pix, ctx->m.linesize);
745  varc = ctx->m.mpvencdsp.pix_norm1(pix, ctx->m.linesize);
746  } else {
747  int bw = FFMIN(avctx->width - 16 * mb_x, 16);
748  int bh = FFMIN((avctx->height >> ctx->interlaced) - 16 * mb_y, 16);
749  sum = varc = 0;
750  for (y = 0; y < bh; y++) {
751  for (x = 0; x < bw; x++) {
752  uint8_t val = pix[x + y * ctx->m.linesize];
753  sum += val;
754  varc += val * val;
755  }
756  }
757  }
758  varc = (varc - (((unsigned) sum * sum) >> 8) + 128) >> 8;
759 
760  ctx->mb_cmp[mb].value = varc;
761  ctx->mb_cmp[mb].mb = mb;
762  }
763  } else { // 10-bit
764  int const linesize = ctx->m.linesize >> 1;
765  for (mb_x = 0; mb_x < ctx->m.mb_width; ++mb_x) {
766  uint16_t *pix = (uint16_t *)ctx->thread[0]->src[0] +
767  ((mb_y << 4) * linesize) + (mb_x << 4);
768  unsigned mb = mb_y * ctx->m.mb_width + mb_x;
769  int sum = 0;
770  int sqsum = 0;
771  int bw = FFMIN(avctx->width - 16 * mb_x, 16);
772  int bh = FFMIN((avctx->height >> ctx->interlaced) - 16 * mb_y, 16);
773  int mean, sqmean;
774  int i, j;
775  // Macroblocks are 16x16 pixels, unlike DCT blocks which are 8x8.
776  for (i = 0; i < bh; ++i) {
777  for (j = 0; j < bw; ++j) {
778  // Turn 16-bit pixels into 10-bit ones.
779  int const sample = (unsigned) pix[j] >> 6;
780  sum += sample;
781  sqsum += sample * sample;
782  // 2^10 * 2^10 * 16 * 16 = 2^28, which is less than INT_MAX
783  }
784  pix += linesize;
785  }
786  mean = sum >> 8; // 16*16 == 2^8
787  sqmean = sqsum >> 8;
788  ctx->mb_cmp[mb].value = sqmean - mean * mean;
789  ctx->mb_cmp[mb].mb = mb;
790  }
791  }
792  return 0;
793 }
794 
796 {
797  int lambda, up_step, down_step;
798  int last_lower = INT_MAX, last_higher = 0;
799  int x, y, q;
800 
801  for (q = 1; q < avctx->qmax; q++) {
802  ctx->qscale = q;
803  avctx->execute2(avctx, dnxhd_calc_bits_thread,
804  NULL, NULL, ctx->m.mb_height);
805  }
806  up_step = down_step = 2 << LAMBDA_FRAC_BITS;
807  lambda = ctx->lambda;
808 
809  for (;;) {
810  int bits = 0;
811  int end = 0;
812  if (lambda == last_higher) {
813  lambda++;
814  end = 1; // need to set final qscales/bits
815  }
816  for (y = 0; y < ctx->m.mb_height; y++) {
817  for (x = 0; x < ctx->m.mb_width; x++) {
818  unsigned min = UINT_MAX;
819  int qscale = 1;
820  int mb = y * ctx->m.mb_width + x;
821  for (q = 1; q < avctx->qmax; q++) {
822  unsigned score = ctx->mb_rc[q][mb].bits * lambda +
823  ((unsigned) ctx->mb_rc[q][mb].ssd << LAMBDA_FRAC_BITS);
824  if (score < min) {
825  min = score;
826  qscale = q;
827  }
828  }
829  bits += ctx->mb_rc[qscale][mb].bits;
830  ctx->mb_qscale[mb] = qscale;
831  ctx->mb_bits[mb] = ctx->mb_rc[qscale][mb].bits;
832  }
833  bits = (bits + 31) & ~31; // padding
834  if (bits > ctx->frame_bits)
835  break;
836  }
837  // ff_dlog(ctx->m.avctx,
838  // "lambda %d, up %u, down %u, bits %d, frame %d\n",
839  // lambda, last_higher, last_lower, bits, ctx->frame_bits);
840  if (end) {
841  if (bits > ctx->frame_bits)
842  return AVERROR(EINVAL);
843  break;
844  }
845  if (bits < ctx->frame_bits) {
846  last_lower = FFMIN(lambda, last_lower);
847  if (last_higher != 0)
848  lambda = (lambda+last_higher)>>1;
849  else
850  lambda -= down_step;
851  down_step = FFMIN((int64_t)down_step*5, INT_MAX);
852  up_step = 1<<LAMBDA_FRAC_BITS;
853  lambda = FFMAX(1, lambda);
854  if (lambda == last_lower)
855  break;
856  } else {
857  last_higher = FFMAX(lambda, last_higher);
858  if (last_lower != INT_MAX)
859  lambda = (lambda+last_lower)>>1;
860  else if ((int64_t)lambda + up_step > INT_MAX)
861  return AVERROR(EINVAL);
862  else
863  lambda += up_step;
864  up_step = FFMIN((int64_t)up_step*5, INT_MAX);
865  down_step = 1<<LAMBDA_FRAC_BITS;
866  }
867  }
868  //ff_dlog(ctx->m.avctx, "out lambda %d\n", lambda);
869  ctx->lambda = lambda;
870  return 0;
871 }
872 
874 {
875  int bits = 0;
876  int up_step = 1;
877  int down_step = 1;
878  int last_higher = 0;
879  int last_lower = INT_MAX;
880  int qscale;
881  int x, y;
882 
883  qscale = ctx->qscale;
884  for (;;) {
885  bits = 0;
886  ctx->qscale = qscale;
887  // XXX avoid recalculating bits
889  NULL, NULL, ctx->m.mb_height);
890  for (y = 0; y < ctx->m.mb_height; y++) {
891  for (x = 0; x < ctx->m.mb_width; x++)
892  bits += ctx->mb_rc[qscale][y*ctx->m.mb_width+x].bits;
893  bits = (bits+31)&~31; // padding
894  if (bits > ctx->frame_bits)
895  break;
896  }
897  // ff_dlog(ctx->m.avctx,
898  // "%d, qscale %d, bits %d, frame %d, higher %d, lower %d\n",
899  // ctx->m.avctx->frame_number, qscale, bits, ctx->frame_bits,
900  // last_higher, last_lower);
901  if (bits < ctx->frame_bits) {
902  if (qscale == 1)
903  return 1;
904  if (last_higher == qscale - 1) {
905  qscale = last_higher;
906  break;
907  }
908  last_lower = FFMIN(qscale, last_lower);
909  if (last_higher != 0)
910  qscale = (qscale + last_higher) >> 1;
911  else
912  qscale -= down_step++;
913  if (qscale < 1)
914  qscale = 1;
915  up_step = 1;
916  } else {
917  if (last_lower == qscale + 1)
918  break;
919  last_higher = FFMAX(qscale, last_higher);
920  if (last_lower != INT_MAX)
921  qscale = (qscale + last_lower) >> 1;
922  else
923  qscale += up_step++;
924  down_step = 1;
925  if (qscale >= ctx->m.avctx->qmax)
926  return AVERROR(EINVAL);
927  }
928  }
929  //ff_dlog(ctx->m.avctx, "out qscale %d\n", qscale);
930  ctx->qscale = qscale;
931  return 0;
932 }
933 
934 #define BUCKET_BITS 8
935 #define RADIX_PASSES 4
936 #define NBUCKETS (1 << BUCKET_BITS)
937 
938 static inline int get_bucket(int value, int shift)
939 {
940  value >>= shift;
941  value &= NBUCKETS - 1;
942  return NBUCKETS - 1 - value;
943 }
944 
945 static void radix_count(const RCCMPEntry *data, int size,
946  int buckets[RADIX_PASSES][NBUCKETS])
947 {
948  int i, j;
949  memset(buckets, 0, sizeof(buckets[0][0]) * RADIX_PASSES * NBUCKETS);
950  for (i = 0; i < size; i++) {
951  int v = data[i].value;
952  for (j = 0; j < RADIX_PASSES; j++) {
953  buckets[j][get_bucket(v, 0)]++;
954  v >>= BUCKET_BITS;
955  }
956  av_assert1(!v);
957  }
958  for (j = 0; j < RADIX_PASSES; j++) {
959  int offset = size;
960  for (i = NBUCKETS - 1; i >= 0; i--)
961  buckets[j][i] = offset -= buckets[j][i];
962  av_assert1(!buckets[j][0]);
963  }
964 }
965 
966 static void radix_sort_pass(RCCMPEntry *dst, const RCCMPEntry *data,
967  int size, int buckets[NBUCKETS], int pass)
968 {
969  int shift = pass * BUCKET_BITS;
970  int i;
971  for (i = 0; i < size; i++) {
972  int v = get_bucket(data[i].value, shift);
973  int pos = buckets[v]++;
974  dst[pos] = data[i];
975  }
976 }
977 
978 static void radix_sort(RCCMPEntry *data, int size)
979 {
980  int buckets[RADIX_PASSES][NBUCKETS];
981  RCCMPEntry *tmp = av_malloc_array(size, sizeof(*tmp));
982  radix_count(data, size, buckets);
983  radix_sort_pass(tmp, data, size, buckets[0], 0);
984  radix_sort_pass(data, tmp, size, buckets[1], 1);
985  if (buckets[2][NBUCKETS - 1] || buckets[3][NBUCKETS - 1]) {
986  radix_sort_pass(tmp, data, size, buckets[2], 2);
987  radix_sort_pass(data, tmp, size, buckets[3], 3);
988  }
989  av_free(tmp);
990 }
991 
993 {
994  int max_bits = 0;
995  int ret, x, y;
996  if ((ret = dnxhd_find_qscale(ctx)) < 0)
997  return ret;
998  for (y = 0; y < ctx->m.mb_height; y++) {
999  for (x = 0; x < ctx->m.mb_width; x++) {
1000  int mb = y * ctx->m.mb_width + x;
1001  int delta_bits;
1002  ctx->mb_qscale[mb] = ctx->qscale;
1003  ctx->mb_bits[mb] = ctx->mb_rc[ctx->qscale][mb].bits;
1004  max_bits += ctx->mb_rc[ctx->qscale][mb].bits;
1005  if (!RC_VARIANCE) {
1006  delta_bits = ctx->mb_rc[ctx->qscale][mb].bits -
1007  ctx->mb_rc[ctx->qscale + 1][mb].bits;
1008  ctx->mb_cmp[mb].mb = mb;
1009  ctx->mb_cmp[mb].value =
1010  delta_bits ? ((ctx->mb_rc[ctx->qscale][mb].ssd -
1011  ctx->mb_rc[ctx->qscale + 1][mb].ssd) * 100) /
1012  delta_bits
1013  : INT_MIN; // avoid increasing qscale
1014  }
1015  }
1016  max_bits += 31; // worst padding
1017  }
1018  if (!ret) {
1019  if (RC_VARIANCE)
1020  avctx->execute2(avctx, dnxhd_mb_var_thread,
1021  NULL, NULL, ctx->m.mb_height);
1022  radix_sort(ctx->mb_cmp, ctx->m.mb_num);
1023  for (x = 0; x < ctx->m.mb_num && max_bits > ctx->frame_bits; x++) {
1024  int mb = ctx->mb_cmp[x].mb;
1025  max_bits -= ctx->mb_rc[ctx->qscale][mb].bits -
1026  ctx->mb_rc[ctx->qscale + 1][mb].bits;
1027  ctx->mb_qscale[mb] = ctx->qscale + 1;
1028  ctx->mb_bits[mb] = ctx->mb_rc[ctx->qscale + 1][mb].bits;
1029  }
1030  }
1031  return 0;
1032 }
1033 
1035 {
1036  int i;
1037 
1038  for (i = 0; i < ctx->m.avctx->thread_count; i++) {
1039  ctx->thread[i]->m.linesize = frame->linesize[0] << ctx->interlaced;
1040  ctx->thread[i]->m.uvlinesize = frame->linesize[1] << ctx->interlaced;
1041  ctx->thread[i]->dct_y_offset = ctx->m.linesize *8;
1042  ctx->thread[i]->dct_uv_offset = ctx->m.uvlinesize*8;
1043  }
1044 
1045 #if FF_API_CODED_FRAME
1049 #endif
1050  ctx->cur_field = frame->interlaced_frame && !frame->top_field_first;
1051 }
1052 
1054  const AVFrame *frame, int *got_packet)
1055 {
1056  DNXHDEncContext *ctx = avctx->priv_data;
1057  int first_field = 1;
1058  int offset, i, ret;
1059  uint8_t *buf;
1060 
1061  if ((ret = ff_alloc_packet2(avctx, pkt, ctx->cid_table->frame_size, 0)) < 0)
1062  return ret;
1063  buf = pkt->data;
1064 
1065  dnxhd_load_picture(ctx, frame);
1066 
1067 encode_coding_unit:
1068  for (i = 0; i < 3; i++) {
1069  ctx->src[i] = frame->data[i];
1070  if (ctx->interlaced && ctx->cur_field)
1071  ctx->src[i] += frame->linesize[i];
1072  }
1073 
1074  dnxhd_write_header(avctx, buf);
1075 
1076  if (avctx->mb_decision == FF_MB_DECISION_RD)
1077  ret = dnxhd_encode_rdo(avctx, ctx);
1078  else
1079  ret = dnxhd_encode_fast(avctx, ctx);
1080  if (ret < 0) {
1081  av_log(avctx, AV_LOG_ERROR,
1082  "picture could not fit ratecontrol constraints, increase qmax\n");
1083  return ret;
1084  }
1085 
1087 
1088  offset = 0;
1089  for (i = 0; i < ctx->m.mb_height; i++) {
1090  AV_WB32(ctx->msip + i * 4, offset);
1091  offset += ctx->slice_size[i];
1092  av_assert1(!(ctx->slice_size[i] & 3));
1093  }
1094 
1095  avctx->execute2(avctx, dnxhd_encode_thread, buf, NULL, ctx->m.mb_height);
1096 
1097  av_assert1(640 + offset + 4 <= ctx->cid_table->coding_unit_size);
1098  memset(buf + 640 + offset, 0,
1099  ctx->cid_table->coding_unit_size - 4 - offset - 640);
1100 
1101  AV_WB32(buf + ctx->cid_table->coding_unit_size - 4, 0x600DC0DE); // EOF
1102 
1103  if (ctx->interlaced && first_field) {
1104  first_field = 0;
1105  ctx->cur_field ^= 1;
1106  buf += ctx->cid_table->coding_unit_size;
1107  goto encode_coding_unit;
1108  }
1109 
1110 #if FF_API_CODED_FRAME
1112  avctx->coded_frame->quality = ctx->qscale * FF_QP2LAMBDA;
1114 #endif
1115 
1117 
1118  pkt->flags |= AV_PKT_FLAG_KEY;
1119  *got_packet = 1;
1120  return 0;
1121 }
1122 
1124 {
1125  DNXHDEncContext *ctx = avctx->priv_data;
1126  int max_level = 1 << (ctx->cid_table->bit_depth + 2);
1127  int i;
1128 
1129  av_free(ctx->vlc_codes - max_level * 2);
1130  av_free(ctx->vlc_bits - max_level * 2);
1131  av_freep(&ctx->run_codes);
1132  av_freep(&ctx->run_bits);
1133 
1134  av_freep(&ctx->mb_bits);
1135  av_freep(&ctx->mb_qscale);
1136  av_freep(&ctx->mb_rc);
1137  av_freep(&ctx->mb_cmp);
1138  av_freep(&ctx->slice_size);
1139  av_freep(&ctx->slice_offs);
1140 
1141  av_freep(&ctx->qmatrix_c);
1142  av_freep(&ctx->qmatrix_l);
1143  av_freep(&ctx->qmatrix_c16);
1144  av_freep(&ctx->qmatrix_l16);
1145 
1146  for (i = 1; i < avctx->thread_count; i++)
1147  av_freep(&ctx->thread[i]);
1148 
1149  return 0;
1150 }
1151 
1152 static const AVCodecDefault dnxhd_defaults[] = {
1153  { "qmax", "1024" }, /* Maximum quantization scale factor allowed for VC-3 */
1154  { NULL },
1155 };
1156 
1158  .name = "dnxhd",
1159  .long_name = NULL_IF_CONFIG_SMALL("VC3/DNxHD"),
1160  .type = AVMEDIA_TYPE_VIDEO,
1161  .id = AV_CODEC_ID_DNXHD,
1162  .priv_data_size = sizeof(DNXHDEncContext),
1164  .encode2 = dnxhd_encode_picture,
1165  .close = dnxhd_encode_end,
1166  .capabilities = AV_CODEC_CAP_SLICE_THREADS,
1167  .pix_fmts = (const enum AVPixelFormat[]) {
1171  },
1172  .priv_class = &dnxhd_class,
1173  .defaults = dnxhd_defaults,
1174 };
static av_always_inline int dnxhd_ssd_block(int16_t *qblock, int16_t *block)
Definition: dnxhdenc.c:530
#define MASK_ABS(mask, level)
Definition: mathops.h:163
IDCTDSPContext idsp
Definition: mpegvideo.h:227
static void radix_count(const RCCMPEntry *data, int size, int buckets[RADIX_PASSES][NBUCKETS])
Definition: dnxhdenc.c:945
#define NULL
Definition: coverity.c:32
const char const char void * val
Definition: avisynth_c.h:634
attribute_deprecated int intra_quant_bias
Definition: avcodec.h:2052
static av_always_inline void dnxhd_get_blocks(DNXHDEncContext *ctx, int mb_x, int mb_y)
Definition: dnxhdenc.c:559
static int shift(int a, int b)
Definition: sonic.c:82
This structure describes decoded (raw) audio or video data.
Definition: frame.h:181
#define FF_ALLOCZ_ARRAY_OR_GOTO(ctx, p, nelem, elsize, label)
Definition: internal.h:157
const uint8_t * dc_bits
Definition: dnxhddata.h:53
AVOption.
Definition: opt.h:245
int ff_side_data_set_encoder_stats(AVPacket *pkt, int quality, int64_t *error, int error_count, int pict_type)
Definition: avpacket.c:618
ptrdiff_t const GLvoid * data
Definition: opengl_enc.c:101
void(* clear_block)(int16_t *block)
Definition: blockdsp.h:35
#define AV_CODEC_FLAG_INTERLACED_DCT
Use interlaced DCT.
Definition: avcodec.h:775
#define LAMBDA_FRAC_BITS
Definition: dnxhdenc.c:43
AVFormatContext * ctx
Definition: movenc-test.c:48
static void put_bits(Jpeg2000EncoderContext *s, int val, int n)
put n times val bit
Definition: j2kenc.c:168
const uint8_t * luma_weight
Definition: dnxhddata.h:52
#define LIBAVUTIL_VERSION_INT
Definition: version.h:70
static av_cold int init(AVCodecContext *avctx)
Definition: avrndec.c:35
uint16_t(* q_chroma_intra_matrix16)[2][64]
Definition: mpegvideo.h:328
int(* qmatrix_l)[64]
Definition: dnxhdenc.h:71
const CIDEntry ff_dnxhd_cid_table[]
Definition: dnxhddata.c:934
const uint16_t * run_codes
Definition: dnxhddata.h:56
static const AVClass dnxhd_class
Definition: dnxhdenc.c:55
void ff_convert_matrix(MpegEncContext *s, int(*qmat)[64], uint16_t(*qmat16)[2][64], const uint16_t *quant_matrix, int bias, int qmin, int qmax, int intra)
Definition: mpegvideo_enc.c:89
enum AVPixelFormat pix_fmt
Pixel format, see AV_PIX_FMT_xxx.
Definition: avcodec.h:1752
unsigned dct_uv_offset
Definition: dnxhdenc.h:58
static av_always_inline void dnxhd_encode_dc(DNXHDEncContext *ctx, int diff)
Definition: dnxhdenc.c:446
static av_cold int dnxhd_init_vlc(DNXHDEncContext *ctx)
Definition: dnxhdenc.c:131
mpegvideo header.
uint8_t permutated[64]
Definition: idctdsp.h:31
int ff_dnxhd_find_cid(AVCodecContext *avctx, int bit_depth)
Definition: dnxhddata.c:1113
uint8_t run
Definition: svq3.c:149
static AVPacket pkt
int bits_per_raw_sample
Bits per sample/pixel of internal libavcodec pixel/sample format.
Definition: avcodec.h:2924
int mb_num
number of MBs of a picture
Definition: mpegvideo.h:130
av_cold void ff_fdctdsp_init(FDCTDSPContext *c, AVCodecContext *avctx)
Definition: fdctdsp.c:26
struct DNXHDEncContext * thread[MAX_THREADS]
Definition: dnxhdenc.h:53
#define sample
int av_log2_16bit(unsigned v)
Definition: intmath.c:31
AVCodec.
Definition: avcodec.h:3392
static av_always_inline int dnxhd_calc_ac_bits(DNXHDEncContext *ctx, int16_t *block, int last_index)
Definition: dnxhdenc.c:540
int h263_aic
Advanded INTRA Coding (AIC)
Definition: mpegvideo.h:84
Macro definitions for various function/variable attributes.
int intra_quant_bias
Definition: dnxhdenc.h:66
const char * class_name
The name of the class; usually it is the same name as the context structure type to which the AVClass...
Definition: log.h:72
int16_t blocks[8][64]
Definition: dnxhdenc.h:68
#define av_assert0(cond)
assert() equivalent, that is always enabled.
Definition: avassert.h:37
void ff_block_permute(int16_t *block, uint8_t *permutation, const uint8_t *scantable, int last)
Permute an 8x8 block according to permuatation.
av_cold void ff_mpegvideoencdsp_init(MpegvideoEncDSPContext *c, AVCodecContext *avctx)
uint8_t bits
Definition: crc.c:295
uint8_t
#define av_cold
Definition: attributes.h:82
#define av_malloc(s)
void(* get_pixels_8x4_sym)(int16_t *, const uint8_t *, ptrdiff_t)
Definition: dnxhdenc.h:94
#define mb
void(* get_pixels)(int16_t *block, const uint8_t *pixels, ptrdiff_t line_size)
Definition: pixblockdsp.h:27
AVOptions.
static int get_bucket(int value, int shift)
Definition: dnxhdenc.c:938
static av_cold int end(AVCodecContext *avctx)
Definition: avrndec.c:90
static int dnxhd_encode_fast(AVCodecContext *avctx, DNXHDEncContext *ctx)
Definition: dnxhdenc.c:992
uint32_t * slice_size
Definition: dnxhdenc.h:50
int(* qmatrix_c)[64]
Definition: dnxhdenc.h:70
#define RADIX_PASSES
Definition: dnxhdenc.c:935
uint32_t * slice_offs
Definition: dnxhdenc.h:51
int(* q_chroma_intra_matrix)[64]
Definition: mpegvideo.h:324
unsigned qscale
Definition: dnxhdenc.h:85
static AVFrame * frame
uint8_t * data
Definition: avcodec.h:1467
static void radix_sort_pass(RCCMPEntry *dst, const RCCMPEntry *data, int size, int buckets[NBUCKETS], int pass)
Definition: dnxhdenc.c:966
const uint8_t * run_bits
Definition: dnxhddata.h:57
#define BUCKET_BITS
Definition: dnxhdenc.c:934
const uint8_t * scantable
Definition: idctdsp.h:30
int interlaced_frame
The content of the picture is interlaced.
Definition: frame.h:312
av_cold void ff_mpv_idct_init(MpegEncContext *s)
Definition: mpegvideo.c:330
int mb_height
number of MBs horizontally & vertically
Definition: mpegvideo.h:126
static av_cold int dnxhd_init_qmat(DNXHDEncContext *ctx, int lbias, int cbias)
Definition: dnxhdenc.c:192
unsigned int coding_unit_size
Definition: dnxhddata.h:47
ptrdiff_t size
Definition: opengl_enc.c:101
high precision timer, useful to profile code
#define AV_WB16(p, v)
Definition: intreadwrite.h:405
BlockDSPContext bdsp
Definition: dnxhdenc.h:44
#define av_log(a,...)
static int first_field(const struct video_data *s)
Definition: v4l2.c:228
static av_cold int dnxhd_encode_end(AVCodecContext *avctx)
Definition: dnxhdenc.c:1123
#define AV_PKT_FLAG_KEY
The packet contains a keyframe.
Definition: avcodec.h:1499
const uint8_t * ac_bits
Definition: dnxhddata.h:55
const uint8_t * ac_info
Definition: dnxhddata.h:55
int(* q_intra_matrix)[64]
precomputed matrix (combine qscale and DCT renorm)
Definition: mpegvideo.h:323
static av_always_inline int dnxhd_switch_matrix(DNXHDEncContext *ctx, int i)
Definition: dnxhdenc.c:609
const uint16_t * ac_codes
Definition: dnxhddata.h:54
static int dnxhd_calc_bits_thread(AVCodecContext *avctx, void *arg, int jobnr, int threadnr)
Definition: dnxhdenc.c:615
#define AV_LOG_ERROR
Something went wrong and cannot losslessly be recovered.
Definition: log.h:176
int ff_dnxhd_get_cid_table(int cid)
Definition: dnxhddata.c:1081
int last_dc[3]
last DC values for MPEG1
Definition: mpegvideo.h:182
static int dnxhd_find_qscale(DNXHDEncContext *ctx)
Definition: dnxhdenc.c:873
#define FF_SIGNBIT(x)
Definition: internal.h:75
av_default_item_name
#define AVERROR(e)
Definition: error.h:43
#define RC_VARIANCE
Definition: dnxhdenc.c:42
int qmax
maximum quantizer
Definition: avcodec.h:2473
#define NULL_IF_CONFIG_SMALL(x)
Return NULL if CONFIG_SMALL is true, otherwise the argument without modification. ...
Definition: internal.h:176
AVCodec ff_dnxhd_encoder
Definition: dnxhdenc.c:1157
static av_always_inline void dnxhd_unquantize_c(DNXHDEncContext *ctx, int16_t *block, int n, int qscale, int last_index)
Definition: dnxhdenc.c:487
#define AV_LOG_DEBUG
Stuff which is only useful for libav* developers.
Definition: log.h:197
PixblockDSPContext pdsp
Definition: mpegvideo.h:231
const char * arg
Definition: jacosubdec.c:66
int flags
AV_CODEC_FLAG_*.
Definition: avcodec.h:1627
static int dnxhd_write_header(AVCodecContext *avctx, uint8_t *buf)
Definition: dnxhdenc.c:415
const uint8_t * dc_codes
Definition: dnxhddata.h:53
MpegvideoEncDSPContext mpvencdsp
Definition: mpegvideo.h:230
const char * name
Name of the codec implementation.
Definition: avcodec.h:3399
static const uint8_t offset[127][2]
Definition: vf_spp.c:92
#define FFMAX(a, b)
Definition: common.h:94
av_cold void ff_pixblockdsp_init(PixblockDSPContext *c, AVCodecContext *avctx)
Definition: pixblockdsp.c:81
#define fail()
Definition: checkasm.h:80
int flags
A combination of AV_PKT_FLAG values.
Definition: avcodec.h:1473
static int put_bits_count(PutBitContext *s)
Definition: put_bits.h:85
planar YUV 4:2:2, 16bpp, (1 Cr & Cb sample per 2x1 Y samples)
Definition: pixfmt.h:67
#define pass
Definition: fft_template.c:509
const uint8_t * chroma_weight
Definition: dnxhddata.h:52
uint16_t * run_codes
Definition: dnxhdenc.h:80
common internal API header
#define MAX_THREADS
static const AVOption options[]
Definition: dnxhdenc.c:46
static void dnxhd_8bit_get_pixels_8x4_sym(int16_t *av_restrict block, const uint8_t *pixels, ptrdiff_t line_size)
Definition: dnxhdenc.c:62
enum AVPictureType pict_type
Picture type of the frame.
Definition: frame.h:252
#define av_assert1(cond)
assert() equivalent, that does not lie in speed critical code.
Definition: avassert.h:53
static const AVCodecDefault dnxhd_defaults[]
Definition: dnxhdenc.c:1152
MpegEncContext m
Used for quantization dsp functions.
Definition: dnxhdenc.h:45
#define FFMIN(a, b)
Definition: common.h:96
static av_always_inline void dnxhd_encode_block(DNXHDEncContext *ctx, int16_t *block, int last_index, int n)
Definition: dnxhdenc.c:461
uint8_t * run_bits
Definition: dnxhdenc.h:81
int width
picture width / height.
Definition: avcodec.h:1711
const uint8_t * run
Definition: dnxhddata.h:57
int(* pix_sum)(uint8_t *pix, int line_size)
GLsizei GLboolean const GLfloat * value
Definition: opengl_enc.c:109
unsigned frame_bits
Definition: dnxhdenc.h:75
static av_always_inline void dnxhd_10bit_get_pixels_8x4_sym(int16_t *av_restrict block, const uint8_t *pixels, ptrdiff_t line_size)
Definition: dnxhdenc.c:86
uint16_t(* q_intra_matrix16)[2][64]
identical to the above but for MMX & these are not permutated, second 64 entries are bias ...
Definition: mpegvideo.h:327
uint16_t(* qmatrix_l16)[2][64]
Definition: dnxhdenc.h:72
int quality
quality (between 1 (good) and FF_LAMBDA_MAX (bad))
Definition: frame.h:288
ScanTable scantable
Definition: dnxhddec.c:65
uint8_t * msip
Macroblock Scan Indexes Payload.
Definition: dnxhdenc.h:49
int n
Definition: avisynth_c.h:547
uint8_t idct_permutation[64]
IDCT input permutation.
Definition: idctdsp.h:94
int value
Definition: dnxhdenc.h:34
int mb_decision
macroblock decision mode
Definition: avcodec.h:2086
void ff_dnxhd_print_profiles(AVCodecContext *avctx, int loglevel)
Definition: dnxhddata.c:1139
uint8_t * vlc_bits
Definition: dnxhdenc.h:79
int thread_count
thread count is used to decide how many independent tasks should be passed to execute() ...
Definition: avcodec.h:2954
int ff_dct_quantize_c(MpegEncContext *s, int16_t *block, int n, int qscale, int *overflow)
static void radix_sort(RCCMPEntry *data, int size)
Definition: dnxhdenc.c:978
static int dnxhd_encode_rdo(AVCodecContext *avctx, DNXHDEncContext *ctx)
Definition: dnxhdenc.c:795
#define AV_CODEC_CAP_SLICE_THREADS
Codec supports slice-based (or partition-based) multithreading.
Definition: avcodec.h:923
int bit_depth
Definition: dnxhddata.h:50
int index_bits
Definition: dnxhddata.h:49
Libavcodec external API header.
static int dnxhd_10bit_dct_quantize(MpegEncContext *ctx, int16_t *block, int n, int qscale, int *overflow)
Definition: dnxhdenc.c:100
ptrdiff_t linesize
line size, in bytes, may be different from width
Definition: mpegvideo.h:131
av_cold void ff_blockdsp_init(BlockDSPContext *c, AVCodecContext *avctx)
Definition: blockdsp.c:58
int linesize[AV_NUM_DATA_POINTERS]
For video, size in bytes of each picture line.
Definition: frame.h:209
static int dnxhd_encode_picture(AVCodecContext *avctx, AVPacket *pkt, const AVFrame *frame, int *got_packet)
Definition: dnxhdenc.c:1053
void(* fdct)(int16_t *block)
Definition: fdctdsp.h:27
main external API structure.
Definition: avcodec.h:1532
unsigned block_width_l2
Definition: dnxhdenc.h:59
ScanTable intra_scantable
Definition: mpegvideo.h:88
#define FF_DEFAULT_QUANT_BIAS
Definition: avcodec.h:2053
int ff_dct_encode_init(MpegEncContext *s)
static av_cold int dnxhd_init_rc(DNXHDEncContext *ctx)
Definition: dnxhdenc.c:272
unsigned lambda
Definition: dnxhdenc.h:86
FDCTDSPContext fdsp
Definition: mpegvideo.h:224
void * buf
Definition: avisynth_c.h:553
void ff_dnxhdenc_init_x86(DNXHDEncContext *ctx)
Definition: dnxhdenc_init.c:31
Describe the class of an AVClass context structure.
Definition: log.h:67
int(* pix_norm1)(uint8_t *pix, int line_size)
#define AV_WB32(p, v)
Definition: intreadwrite.h:419
int index
Definition: gxfenc.c:89
#define FF_MB_DECISION_RD
rate distortion
Definition: avcodec.h:2089
#define NBUCKETS
Definition: dnxhdenc.c:936
const uint8_t ff_zigzag_direct[64]
Definition: mathtables.c:98
ptrdiff_t uvlinesize
line size, for chroma in bytes, may be different from width
Definition: mpegvideo.h:132
int ssd
Definition: dnxhdenc.h:38
static void dnxhd_setup_threads_slices(DNXHDEncContext *ctx)
Definition: dnxhdenc.c:708
int ff_alloc_packet2(AVCodecContext *avctx, AVPacket *avpkt, int64_t size, int64_t min_size)
Check AVPacket size and/or allocate data.
Definition: utils.c:1621
const CIDEntry * cid_table
Definition: dnxhdenc.h:48
uint16_t(* qmatrix_c16)[2][64]
Definition: dnxhdenc.h:73
unsigned dct_y_offset
Definition: dnxhdenc.h:57
static enum AVPixelFormat pix_fmts[]
Definition: libkvazaar.c:262
unsigned min_padding
Definition: dnxhdenc.h:65
static void dnxhd_load_picture(DNXHDEncContext *ctx, const AVFrame *frame)
Definition: dnxhdenc.c:1034
#define AV_PIX_FMT_YUV422P10
Definition: pixfmt.h:331
#define DNX10BIT_QMAT_SHIFT
Definition: dnxhdenc.c:41
uint8_t * data[AV_NUM_DATA_POINTERS]
pointer to the picture/channel planes.
Definition: frame.h:192
unsigned int frame_size
Definition: dnxhddata.h:46
uint8_t level
Definition: svq3.c:150
uint16_t * mb_bits
Definition: dnxhdenc.h:88
MpegEncContext.
Definition: mpegvideo.h:78
struct AVCodecContext * avctx
Definition: mpegvideo.h:95
PutBitContext pb
bit output
Definition: mpegvideo.h:148
static int dnxhd_mb_var_thread(AVCodecContext *avctx, void *arg, int jobnr, int threadnr)
Definition: dnxhdenc.c:727
static int dnxhd_encode_thread(AVCodecContext *avctx, void *arg, int jobnr, int threadnr)
Definition: dnxhdenc.c:671
static av_cold int dnxhd_encode_init(AVCodecContext *avctx)
Definition: dnxhdenc.c:288
#define VE
Definition: dnxhdenc.c:45
#define FF_DISABLE_DEPRECATION_WARNINGS
Definition: internal.h:80
common internal api header.
static void flush_put_bits(PutBitContext *s)
Pad the end of the output stream with zeros.
Definition: put_bits.h:101
static unsigned bit_depth(uint64_t mask)
Definition: af_astats.c:128
attribute_deprecated AVFrame * coded_frame
the picture in the bitstream
Definition: avcodec.h:2945
static void init_put_bits(PutBitContext *s, uint8_t *buffer, int buffer_size)
Initialize the PutBitContext s.
Definition: put_bits.h:48
void * priv_data
Definition: avcodec.h:1574
static av_always_inline int diff(const uint32_t a, const uint32_t b)
#define av_free(p)
RCCMPEntry * mb_cmp
Definition: dnxhdenc.h:91
int pixels
Definition: avisynth_c.h:298
#define FF_ENABLE_DEPRECATION_WARNINGS
Definition: internal.h:81
int top_field_first
If the content is interlaced, is top field displayed first.
Definition: frame.h:317
int nitris_compat
Definition: dnxhdenc.h:64
int(* execute2)(struct AVCodecContext *c, int(*func)(struct AVCodecContext *c2, void *arg, int jobnr, int threadnr), void *arg2, int *ret, int count)
The codec may call this to execute several independent things.
Definition: avcodec.h:3014
int bits
Definition: dnxhdenc.h:39
AVCodecContext * avctx
Definition: dnxhddec.c:51
int key_frame
1 -> keyframe, 0-> not
Definition: frame.h:247
#define FF_QP2LAMBDA
factor to convert from H.263 QP to lambda
Definition: avutil.h:219
const CIDEntry * cid_table
Definition: dnxhddec.c:66
#define LOCAL_ALIGNED_16(t, v,...)
Definition: internal.h:121
#define av_freep(p)
#define av_always_inline
Definition: attributes.h:39
#define av_malloc_array(a, b)
int(* dct_quantize)(struct MpegEncContext *s, int16_t *block, int n, int qscale, int *overflow)
Definition: mpegvideo.h:521
uint16_t mb
Definition: dnxhdenc.h:33
float min
AVPixelFormat
Pixel format.
Definition: pixfmt.h:61
This structure stores compressed data.
Definition: avcodec.h:1444
RCEntry(* mb_rc)[8160]
Definition: dnxhdenc.h:92
uint32_t * vlc_codes
Definition: dnxhdenc.h:78
for(j=16;j >0;--j)
#define FF_ALLOCZ_OR_GOTO(ctx, p, size, label)
Definition: internal.h:139
uint8_t * src[3]
Definition: dnxhdenc.h:76
uint8_t * mb_qscale
Definition: dnxhdenc.h:89
enum idct_permutation_type perm_type
Definition: idctdsp.h:95
void(* idct)(int16_t *block)
Definition: idctdsp.h:63
static int16_t block[64]
Definition: dct-test.c:112