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 "profiles.h"
38 #include "dnxhdenc.h"
39 
40 // The largest value that will not lead to overflow for 10-bit 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  { "profile", NULL, offsetof(DNXHDEncContext, profile), AV_OPT_TYPE_INT,
53  { .i64 = FF_PROFILE_DNXHD },
55  { "dnxhd", NULL, 0, AV_OPT_TYPE_CONST, { .i64 = FF_PROFILE_DNXHD },
56  0, 0, VE, "profile" },
57  { "dnxhr_444", NULL, 0, AV_OPT_TYPE_CONST, { .i64 = FF_PROFILE_DNXHR_444 },
58  0, 0, VE, "profile" },
59  { "dnxhr_hqx", NULL, 0, AV_OPT_TYPE_CONST, { .i64 = FF_PROFILE_DNXHR_HQX },
60  0, 0, VE, "profile" },
61  { "dnxhr_hq", NULL, 0, AV_OPT_TYPE_CONST, { .i64 = FF_PROFILE_DNXHR_HQ },
62  0, 0, VE, "profile" },
63  { "dnxhr_sq", NULL, 0, AV_OPT_TYPE_CONST, { .i64 = FF_PROFILE_DNXHR_SQ },
64  0, 0, VE, "profile" },
65  { "dnxhr_lb", NULL, 0, AV_OPT_TYPE_CONST, { .i64 = FF_PROFILE_DNXHR_LB },
66  0, 0, VE, "profile" },
67  { NULL }
68 };
69 
70 static const AVClass dnxhd_class = {
71  .class_name = "dnxhd",
72  .item_name = av_default_item_name,
73  .option = options,
74  .version = LIBAVUTIL_VERSION_INT,
75 };
76 
77 static void dnxhd_8bit_get_pixels_8x4_sym(int16_t *av_restrict block,
78  const uint8_t *pixels,
79  ptrdiff_t line_size)
80 {
81  int i;
82  for (i = 0; i < 4; i++) {
83  block[0] = pixels[0];
84  block[1] = pixels[1];
85  block[2] = pixels[2];
86  block[3] = pixels[3];
87  block[4] = pixels[4];
88  block[5] = pixels[5];
89  block[6] = pixels[6];
90  block[7] = pixels[7];
91  pixels += line_size;
92  block += 8;
93  }
94  memcpy(block, block - 8, sizeof(*block) * 8);
95  memcpy(block + 8, block - 16, sizeof(*block) * 8);
96  memcpy(block + 16, block - 24, sizeof(*block) * 8);
97  memcpy(block + 24, block - 32, sizeof(*block) * 8);
98 }
99 
100 static av_always_inline
101 void dnxhd_10bit_get_pixels_8x4_sym(int16_t *av_restrict block,
102  const uint8_t *pixels,
103  ptrdiff_t line_size)
104 {
105  memcpy(block + 0 * 8, pixels + 0 * line_size, 8 * sizeof(*block));
106  memcpy(block + 7 * 8, pixels + 0 * line_size, 8 * sizeof(*block));
107  memcpy(block + 1 * 8, pixels + 1 * line_size, 8 * sizeof(*block));
108  memcpy(block + 6 * 8, pixels + 1 * line_size, 8 * sizeof(*block));
109  memcpy(block + 2 * 8, pixels + 2 * line_size, 8 * sizeof(*block));
110  memcpy(block + 5 * 8, pixels + 2 * line_size, 8 * sizeof(*block));
111  memcpy(block + 3 * 8, pixels + 3 * line_size, 8 * sizeof(*block));
112  memcpy(block + 4 * 8, pixels + 3 * line_size, 8 * sizeof(*block));
113 }
114 
116  int n, int qscale, int *overflow)
117 {
119  const int *qmat = n<4 ? ctx->q_intra_matrix[qscale] : ctx->q_chroma_intra_matrix[qscale];
120  int last_non_zero = 0;
121  int i;
122 
123  ctx->fdsp.fdct(block);
124 
125  // Divide by 4 with rounding, to compensate scaling of DCT coefficients
126  block[0] = (block[0] + 2) >> 2;
127 
128  for (i = 1; i < 64; ++i) {
129  int j = scantable[i];
130  int sign = FF_SIGNBIT(block[j]);
131  int level = (block[j] ^ sign) - sign;
132  level = level * qmat[j] >> DNX10BIT_QMAT_SHIFT;
133  block[j] = (level ^ sign) - sign;
134  if (level)
135  last_non_zero = i;
136  }
137 
138  /* we need this permutation so that we correct the IDCT, we only permute the !=0 elements */
139  if (ctx->idsp.perm_type != FF_IDCT_PERM_NONE)
141  scantable, last_non_zero);
142 
143  return last_non_zero;
144 }
145 
147 {
148  int i, j, level, run;
149  int max_level = 1 << (ctx->cid_table->bit_depth + 2);
150 
152  max_level, 4 * sizeof(*ctx->vlc_codes), fail);
154  max_level, 4 * sizeof(*ctx->vlc_bits), fail);
155  FF_ALLOCZ_OR_GOTO(ctx->m.avctx, ctx->run_codes,
156  63 * 2, fail);
157  FF_ALLOCZ_OR_GOTO(ctx->m.avctx, ctx->run_bits,
158  63, fail);
159 
160  ctx->vlc_codes += max_level * 2;
161  ctx->vlc_bits += max_level * 2;
162  for (level = -max_level; level < max_level; level++) {
163  for (run = 0; run < 2; run++) {
164  int index = (level << 1) | run;
165  int sign, offset = 0, alevel = level;
166 
167  MASK_ABS(sign, alevel);
168  if (alevel > 64) {
169  offset = (alevel - 1) >> 6;
170  alevel -= offset << 6;
171  }
172  for (j = 0; j < 257; j++) {
173  if (ctx->cid_table->ac_info[2*j+0] >> 1 == alevel &&
174  (!offset || (ctx->cid_table->ac_info[2*j+1] & 1) && offset) &&
175  (!run || (ctx->cid_table->ac_info[2*j+1] & 2) && run)) {
176  av_assert1(!ctx->vlc_codes[index]);
177  if (alevel) {
178  ctx->vlc_codes[index] =
179  (ctx->cid_table->ac_codes[j] << 1) | (sign & 1);
180  ctx->vlc_bits[index] = ctx->cid_table->ac_bits[j] + 1;
181  } else {
182  ctx->vlc_codes[index] = ctx->cid_table->ac_codes[j];
183  ctx->vlc_bits[index] = ctx->cid_table->ac_bits[j];
184  }
185  break;
186  }
187  }
188  av_assert0(!alevel || j < 257);
189  if (offset) {
190  ctx->vlc_codes[index] =
191  (ctx->vlc_codes[index] << ctx->cid_table->index_bits) | offset;
192  ctx->vlc_bits[index] += ctx->cid_table->index_bits;
193  }
194  }
195  }
196  for (i = 0; i < 62; i++) {
197  int run = ctx->cid_table->run[i];
198  av_assert0(run < 63);
199  ctx->run_codes[run] = ctx->cid_table->run_codes[i];
200  ctx->run_bits[run] = ctx->cid_table->run_bits[i];
201  }
202  return 0;
203 fail:
204  return AVERROR(ENOMEM);
205 }
206 
207 static av_cold int dnxhd_init_qmat(DNXHDEncContext *ctx, int lbias, int cbias)
208 {
209  // init first elem to 1 to avoid div by 0 in convert_matrix
210  uint16_t weight_matrix[64] = { 1, }; // convert_matrix needs uint16_t*
211  int qscale, i;
212  const uint8_t *luma_weight_table = ctx->cid_table->luma_weight;
213  const uint8_t *chroma_weight_table = ctx->cid_table->chroma_weight;
214 
216  (ctx->m.avctx->qmax + 1), 64 * sizeof(int), fail);
218  (ctx->m.avctx->qmax + 1), 64 * sizeof(int), fail);
220  (ctx->m.avctx->qmax + 1), 64 * 2 * sizeof(uint16_t),
221  fail);
223  (ctx->m.avctx->qmax + 1), 64 * 2 * sizeof(uint16_t),
224  fail);
225 
226  if (ctx->cid_table->bit_depth == 8) {
227  for (i = 1; i < 64; i++) {
228  int j = ctx->m.idsp.idct_permutation[ff_zigzag_direct[i]];
229  weight_matrix[j] = ctx->cid_table->luma_weight[i];
230  }
231  ff_convert_matrix(&ctx->m, ctx->qmatrix_l, ctx->qmatrix_l16,
232  weight_matrix, ctx->intra_quant_bias, 1,
233  ctx->m.avctx->qmax, 1);
234  for (i = 1; i < 64; i++) {
235  int j = ctx->m.idsp.idct_permutation[ff_zigzag_direct[i]];
236  weight_matrix[j] = ctx->cid_table->chroma_weight[i];
237  }
238  ff_convert_matrix(&ctx->m, ctx->qmatrix_c, ctx->qmatrix_c16,
239  weight_matrix, ctx->intra_quant_bias, 1,
240  ctx->m.avctx->qmax, 1);
241 
242  for (qscale = 1; qscale <= ctx->m.avctx->qmax; qscale++) {
243  for (i = 0; i < 64; i++) {
244  ctx->qmatrix_l[qscale][i] <<= 2;
245  ctx->qmatrix_c[qscale][i] <<= 2;
246  ctx->qmatrix_l16[qscale][0][i] <<= 2;
247  ctx->qmatrix_l16[qscale][1][i] <<= 2;
248  ctx->qmatrix_c16[qscale][0][i] <<= 2;
249  ctx->qmatrix_c16[qscale][1][i] <<= 2;
250  }
251  }
252  } else {
253  // 10-bit
254  for (qscale = 1; qscale <= ctx->m.avctx->qmax; qscale++) {
255  for (i = 1; i < 64; i++) {
256  int j = ff_zigzag_direct[i];
257 
258  /* The quantization formula from the VC-3 standard is:
259  * quantized = sign(block[i]) * floor(abs(block[i]/s) * p /
260  * (qscale * weight_table[i]))
261  * Where p is 32 for 8-bit samples and 8 for 10-bit ones.
262  * The s factor compensates scaling of DCT coefficients done by
263  * the DCT routines, and therefore is not present in standard.
264  * It's 8 for 8-bit samples and 4 for 10-bit ones.
265  * We want values of ctx->qtmatrix_l and ctx->qtmatrix_r to be:
266  * ((1 << DNX10BIT_QMAT_SHIFT) * (p / s)) /
267  * (qscale * weight_table[i])
268  * For 10-bit samples, p / s == 2 */
269  ctx->qmatrix_l[qscale][j] = (1 << (DNX10BIT_QMAT_SHIFT + 1)) /
270  (qscale * luma_weight_table[i]);
271  ctx->qmatrix_c[qscale][j] = (1 << (DNX10BIT_QMAT_SHIFT + 1)) /
272  (qscale * chroma_weight_table[i]);
273  }
274  }
275  }
276 
278  ctx->m.q_chroma_intra_matrix = ctx->qmatrix_c;
279  ctx->m.q_intra_matrix16 = ctx->qmatrix_l16;
280  ctx->m.q_intra_matrix = ctx->qmatrix_l;
281 
282  return 0;
283 fail:
284  return AVERROR(ENOMEM);
285 }
286 
288 {
289  FF_ALLOCZ_ARRAY_OR_GOTO(ctx->m.avctx, ctx->mb_rc, (ctx->m.avctx->qmax + 1),
290  ctx->m.mb_num * sizeof(RCEntry), fail);
291  if (ctx->m.avctx->mb_decision != FF_MB_DECISION_RD)
293  ctx->m.mb_num, sizeof(RCCMPEntry), fail);
294  ctx->frame_bits = (ctx->coding_unit_size -
295  ctx->data_offset - 4 - ctx->min_padding) * 8;
296  ctx->qscale = 1;
297  ctx->lambda = 2 << LAMBDA_FRAC_BITS; // qscale 2
298  return 0;
299 fail:
300  return AVERROR(ENOMEM);
301 }
302 
303 static int dnxhd_get_hr_frame_size(const CIDEntry* profile, int mb_num)
304 {
305  int result = mb_num * profile->packet_scale.num / profile->packet_scale.den;
306  result = (result + 2048) / 4096 * 4096;
307  return FFMAX(result, 8192);
308 }
310 {
311  DNXHDEncContext *ctx = avctx->priv_data;
312  int i, index, bit_depth, ret;
313 
314  switch (avctx->pix_fmt) {
315  case AV_PIX_FMT_YUV422P:
316  bit_depth = 8;
317  break;
319  bit_depth = 10;
320  break;
321  default:
322  av_log(avctx, AV_LOG_ERROR,
323  "pixel format is incompatible with DNxHD\n");
324  return AVERROR(EINVAL);
325  }
326 
327  if (ctx->profile == FF_PROFILE_DNXHR_HQX ||
328  ctx->profile == FF_PROFILE_DNXHR_HQX) {
330  "dnxhr_444 or dnxhr_hqx profile");
331  return AVERROR_PATCHWELCOME;
332  }
333 
334  avctx->profile = ctx->profile;
335  ctx->cid = ff_dnxhd_find_cid(avctx, bit_depth);
336  if (!ctx->cid) {
337  av_log(avctx, AV_LOG_ERROR,
338  "video parameters incompatible with DNxHD. Valid DNxHD profiles:\n");
340  return AVERROR(EINVAL);
341  }
342  av_log(avctx, AV_LOG_DEBUG, "cid %d\n", ctx->cid);
343 
344  if (ctx->cid >= 1270 && ctx->cid <= 1274)
345  avctx->codec_tag = MKTAG('A','V','d','h');
346 
347  if (avctx->width < 256 || avctx->height < 120) {
348  av_log(avctx, AV_LOG_ERROR,
349  "Input dimensions too small, input must be at least 256x120\n");
350  return AVERROR(EINVAL);
351  }
352 
353  index = ff_dnxhd_get_cid_table(ctx->cid);
354  av_assert0(index >= 0);
355 
357 
358  ctx->m.avctx = avctx;
359  ctx->m.mb_intra = 1;
360  ctx->m.h263_aic = 1;
361 
362  avctx->bits_per_raw_sample = ctx->cid_table->bit_depth;
363 
364  ff_blockdsp_init(&ctx->bdsp, avctx);
365  ff_fdctdsp_init(&ctx->m.fdsp, avctx);
366  ff_mpv_idct_init(&ctx->m);
367  ff_mpegvideoencdsp_init(&ctx->m.mpvencdsp, avctx);
368  ff_pixblockdsp_init(&ctx->m.pdsp, avctx);
369  ff_dct_encode_init(&ctx->m);
370 
371  if (!ctx->m.dct_quantize)
373 
374  if (ctx->cid_table->bit_depth == 10) {
377  ctx->block_width_l2 = 4;
378  } else {
380  ctx->block_width_l2 = 3;
381  }
382 
383  if (ARCH_X86)
385 
386  ctx->m.mb_height = (avctx->height + 15) / 16;
387  ctx->m.mb_width = (avctx->width + 15) / 16;
388 
389  if (avctx->flags & AV_CODEC_FLAG_INTERLACED_DCT) {
390  ctx->interlaced = 1;
391  ctx->m.mb_height /= 2;
392  }
393 
394  ctx->m.mb_num = ctx->m.mb_height * ctx->m.mb_width;
395 
396  if (ctx->cid_table->frame_size == DNXHD_VARIABLE) {
398  ctx->m.mb_num);
399  ctx->coding_unit_size = ctx->frame_size;
400  } else {
401  ctx->frame_size = ctx->cid_table->frame_size;
403  }
404 
405  if (ctx->m.mb_height > 68)
406  ctx->data_offset = 0x170 + (ctx->m.mb_height << 2);
407  else
408  ctx->data_offset = 0x280;
409 
410 #if FF_API_QUANT_BIAS
413  ctx->intra_quant_bias = avctx->intra_quant_bias;
415 #endif
416  // XXX tune lbias/cbias
417  if ((ret = dnxhd_init_qmat(ctx, ctx->intra_quant_bias, 0)) < 0)
418  return ret;
419 
420  /* Avid Nitris hardware decoder requires a minimum amount of padding
421  * in the coding unit payload */
422  if (ctx->nitris_compat)
423  ctx->min_padding = 1600;
424 
425  if ((ret = dnxhd_init_vlc(ctx)) < 0)
426  return ret;
427  if ((ret = dnxhd_init_rc(ctx)) < 0)
428  return ret;
429 
430  FF_ALLOCZ_OR_GOTO(ctx->m.avctx, ctx->slice_size,
431  ctx->m.mb_height * sizeof(uint32_t), fail);
432  FF_ALLOCZ_OR_GOTO(ctx->m.avctx, ctx->slice_offs,
433  ctx->m.mb_height * sizeof(uint32_t), fail);
434  FF_ALLOCZ_OR_GOTO(ctx->m.avctx, ctx->mb_bits,
435  ctx->m.mb_num * sizeof(uint16_t), fail);
436  FF_ALLOCZ_OR_GOTO(ctx->m.avctx, ctx->mb_qscale,
437  ctx->m.mb_num * sizeof(uint8_t), fail);
438 
439 #if FF_API_CODED_FRAME
441  avctx->coded_frame->key_frame = 1;
444 #endif
445 
446  if (avctx->thread_count > MAX_THREADS) {
447  av_log(avctx, AV_LOG_ERROR, "too many threads\n");
448  return AVERROR(EINVAL);
449  }
450 
451  if (avctx->qmax <= 1) {
452  av_log(avctx, AV_LOG_ERROR, "qmax must be at least 2\n");
453  return AVERROR(EINVAL);
454  }
455 
456  ctx->thread[0] = ctx;
457  for (i = 1; i < avctx->thread_count; i++) {
458  ctx->thread[i] = av_malloc(sizeof(DNXHDEncContext));
459  memcpy(ctx->thread[i], ctx, sizeof(DNXHDEncContext));
460  }
461 
462  return 0;
463 fail: // for FF_ALLOCZ_OR_GOTO
464  return AVERROR(ENOMEM);
465 }
466 
468 {
469  DNXHDEncContext *ctx = avctx->priv_data;
470 
471  memset(buf, 0, ctx->data_offset);
472 
473  // * write prefix */
474  AV_WB16(buf + 0x02, ctx->data_offset);
475  if (ctx->cid >= 1270 && ctx->cid <= 1274)
476  buf[4] = 0x03;
477  else
478  buf[4] = 0x01;
479 
480  buf[5] = ctx->interlaced ? ctx->cur_field + 2 : 0x01;
481  buf[6] = 0x80; // crc flag off
482  buf[7] = 0xa0; // reserved
483  AV_WB16(buf + 0x18, avctx->height >> ctx->interlaced); // ALPF
484  AV_WB16(buf + 0x1a, avctx->width); // SPL
485  AV_WB16(buf + 0x1d, avctx->height >> ctx->interlaced); // NAL
486 
487  buf[0x21] = ctx->cid_table->bit_depth == 10 ? 0x58 : 0x38;
488  buf[0x22] = 0x88 + (ctx->interlaced << 2);
489  AV_WB32(buf + 0x28, ctx->cid); // CID
490  buf[0x2c] = ctx->interlaced ? 0 : 0x80;
491 
492  buf[0x5f] = 0x01; // UDL
493 
494  buf[0x167] = 0x02; // reserved
495  AV_WB16(buf + 0x16a, ctx->m.mb_height * 4 + 4); // MSIPS
496  buf[0x16d] = ctx->m.mb_height; // Ns
497  buf[0x16f] = 0x10; // reserved
498 
499  ctx->msip = buf + 0x170;
500  return 0;
501 }
502 
504 {
505  int nbits;
506  if (diff < 0) {
507  nbits = av_log2_16bit(-2 * diff);
508  diff--;
509  } else {
510  nbits = av_log2_16bit(2 * diff);
511  }
512  put_bits(&ctx->m.pb, ctx->cid_table->dc_bits[nbits] + nbits,
513  (ctx->cid_table->dc_codes[nbits] << nbits) +
514  av_mod_uintp2(diff, nbits));
515 }
516 
517 static av_always_inline
519  int last_index, int n)
520 {
521  int last_non_zero = 0;
522  int slevel, i, j;
523 
524  dnxhd_encode_dc(ctx, block[0] - ctx->m.last_dc[n]);
525  ctx->m.last_dc[n] = block[0];
526 
527  for (i = 1; i <= last_index; i++) {
528  j = ctx->m.intra_scantable.permutated[i];
529  slevel = block[j];
530  if (slevel) {
531  int run_level = i - last_non_zero - 1;
532  int rlevel = (slevel << 1) | !!run_level;
533  put_bits(&ctx->m.pb, ctx->vlc_bits[rlevel], ctx->vlc_codes[rlevel]);
534  if (run_level)
535  put_bits(&ctx->m.pb, ctx->run_bits[run_level],
536  ctx->run_codes[run_level]);
537  last_non_zero = i;
538  }
539  }
540  put_bits(&ctx->m.pb, ctx->vlc_bits[0], ctx->vlc_codes[0]); // EOB
541 }
542 
543 static av_always_inline
545  int qscale, int last_index)
546 {
547  const uint8_t *weight_matrix;
548  int level;
549  int i;
550 
551  weight_matrix = (n & 2) ? ctx->cid_table->chroma_weight
552  : ctx->cid_table->luma_weight;
553 
554  for (i = 1; i <= last_index; i++) {
555  int j = ctx->m.intra_scantable.permutated[i];
556  level = block[j];
557  if (level) {
558  if (level < 0) {
559  level = (1 - 2 * level) * qscale * weight_matrix[i];
560  if (ctx->cid_table->bit_depth == 10) {
561  if (weight_matrix[i] != 8)
562  level += 8;
563  level >>= 4;
564  } else {
565  if (weight_matrix[i] != 32)
566  level += 32;
567  level >>= 6;
568  }
569  level = -level;
570  } else {
571  level = (2 * level + 1) * qscale * weight_matrix[i];
572  if (ctx->cid_table->bit_depth == 10) {
573  if (weight_matrix[i] != 8)
574  level += 8;
575  level >>= 4;
576  } else {
577  if (weight_matrix[i] != 32)
578  level += 32;
579  level >>= 6;
580  }
581  }
582  block[j] = level;
583  }
584  }
585 }
586 
587 static av_always_inline int dnxhd_ssd_block(int16_t *qblock, int16_t *block)
588 {
589  int score = 0;
590  int i;
591  for (i = 0; i < 64; i++)
592  score += (block[i] - qblock[i]) * (block[i] - qblock[i]);
593  return score;
594 }
595 
596 static av_always_inline
597 int dnxhd_calc_ac_bits(DNXHDEncContext *ctx, int16_t *block, int last_index)
598 {
599  int last_non_zero = 0;
600  int bits = 0;
601  int i, j, level;
602  for (i = 1; i <= last_index; i++) {
603  j = ctx->m.intra_scantable.permutated[i];
604  level = block[j];
605  if (level) {
606  int run_level = i - last_non_zero - 1;
607  bits += ctx->vlc_bits[(level << 1) |
608  !!run_level] + ctx->run_bits[run_level];
609  last_non_zero = i;
610  }
611  }
612  return bits;
613 }
614 
615 static av_always_inline
616 void dnxhd_get_blocks(DNXHDEncContext *ctx, int mb_x, int mb_y)
617 {
618  const int bs = ctx->block_width_l2;
619  const int bw = 1 << bs;
620  const uint8_t *ptr_y = ctx->thread[0]->src[0] +
621  ((mb_y << 4) * ctx->m.linesize) + (mb_x << bs + 1);
622  const uint8_t *ptr_u = ctx->thread[0]->src[1] +
623  ((mb_y << 4) * ctx->m.uvlinesize) + (mb_x << bs);
624  const uint8_t *ptr_v = ctx->thread[0]->src[2] +
625  ((mb_y << 4) * ctx->m.uvlinesize) + (mb_x << bs);
626  PixblockDSPContext *pdsp = &ctx->m.pdsp;
627 
628  pdsp->get_pixels(ctx->blocks[0], ptr_y, ctx->m.linesize);
629  pdsp->get_pixels(ctx->blocks[1], ptr_y + bw, ctx->m.linesize);
630  pdsp->get_pixels(ctx->blocks[2], ptr_u, ctx->m.uvlinesize);
631  pdsp->get_pixels(ctx->blocks[3], ptr_v, ctx->m.uvlinesize);
632 
633  if (mb_y + 1 == ctx->m.mb_height && (ctx->m.avctx->height % 16) != 0) {
634  if (ctx->interlaced) {
635  ctx->get_pixels_8x4_sym(ctx->blocks[4],
636  ptr_y + ctx->dct_y_offset,
637  ctx->m.linesize);
638  ctx->get_pixels_8x4_sym(ctx->blocks[5],
639  ptr_y + ctx->dct_y_offset + bw,
640  ctx->m.linesize);
641  ctx->get_pixels_8x4_sym(ctx->blocks[6],
642  ptr_u + ctx->dct_uv_offset,
643  ctx->m.uvlinesize);
644  ctx->get_pixels_8x4_sym(ctx->blocks[7],
645  ptr_v + ctx->dct_uv_offset,
646  ctx->m.uvlinesize);
647  } else {
648  ctx->bdsp.clear_block(ctx->blocks[4]);
649  ctx->bdsp.clear_block(ctx->blocks[5]);
650  ctx->bdsp.clear_block(ctx->blocks[6]);
651  ctx->bdsp.clear_block(ctx->blocks[7]);
652  }
653  } else {
654  pdsp->get_pixels(ctx->blocks[4],
655  ptr_y + ctx->dct_y_offset, ctx->m.linesize);
656  pdsp->get_pixels(ctx->blocks[5],
657  ptr_y + ctx->dct_y_offset + bw, ctx->m.linesize);
658  pdsp->get_pixels(ctx->blocks[6],
659  ptr_u + ctx->dct_uv_offset, ctx->m.uvlinesize);
660  pdsp->get_pixels(ctx->blocks[7],
661  ptr_v + ctx->dct_uv_offset, ctx->m.uvlinesize);
662  }
663 }
664 
665 static av_always_inline
667 {
668  const static uint8_t component[8]={0,0,1,2,0,0,1,2};
669  return component[i];
670 }
671 
673  int jobnr, int threadnr)
674 {
675  DNXHDEncContext *ctx = avctx->priv_data;
676  int mb_y = jobnr, mb_x;
677  int qscale = ctx->qscale;
678  LOCAL_ALIGNED_16(int16_t, block, [64]);
679  ctx = ctx->thread[threadnr];
680 
681  ctx->m.last_dc[0] =
682  ctx->m.last_dc[1] =
683  ctx->m.last_dc[2] = 1 << (ctx->cid_table->bit_depth + 2);
684 
685  for (mb_x = 0; mb_x < ctx->m.mb_width; mb_x++) {
686  unsigned mb = mb_y * ctx->m.mb_width + mb_x;
687  int ssd = 0;
688  int ac_bits = 0;
689  int dc_bits = 0;
690  int i;
691 
692  dnxhd_get_blocks(ctx, mb_x, mb_y);
693 
694  for (i = 0; i < 8; i++) {
695  int16_t *src_block = ctx->blocks[i];
696  int overflow, nbits, diff, last_index;
697  int n = dnxhd_switch_matrix(ctx, i);
698 
699  memcpy(block, src_block, 64 * sizeof(*block));
700  last_index = ctx->m.dct_quantize(&ctx->m, block, 4 & (2*i),
701  qscale, &overflow);
702  ac_bits += dnxhd_calc_ac_bits(ctx, block, last_index);
703 
704  diff = block[0] - ctx->m.last_dc[n];
705  if (diff < 0)
706  nbits = av_log2_16bit(-2 * diff);
707  else
708  nbits = av_log2_16bit(2 * diff);
709 
710  av_assert1(nbits < ctx->cid_table->bit_depth + 4);
711  dc_bits += ctx->cid_table->dc_bits[nbits] + nbits;
712 
713  ctx->m.last_dc[n] = block[0];
714 
715  if (avctx->mb_decision == FF_MB_DECISION_RD || !RC_VARIANCE) {
716  dnxhd_unquantize_c(ctx, block, i, qscale, last_index);
717  ctx->m.idsp.idct(block);
718  ssd += dnxhd_ssd_block(block, src_block);
719  }
720  }
721  ctx->mb_rc[(qscale * ctx->m.mb_num) + mb].ssd = ssd;
722  ctx->mb_rc[(qscale * ctx->m.mb_num) + mb].bits = ac_bits + dc_bits + 12 +
723  8 * ctx->vlc_bits[0];
724  }
725  return 0;
726 }
727 
729  int jobnr, int threadnr)
730 {
731  DNXHDEncContext *ctx = avctx->priv_data;
732  int mb_y = jobnr, mb_x;
733  ctx = ctx->thread[threadnr];
734  init_put_bits(&ctx->m.pb, (uint8_t *)arg + ctx->data_offset + ctx->slice_offs[jobnr],
735  ctx->slice_size[jobnr]);
736 
737  ctx->m.last_dc[0] =
738  ctx->m.last_dc[1] =
739  ctx->m.last_dc[2] = 1 << (ctx->cid_table->bit_depth + 2);
740  for (mb_x = 0; mb_x < ctx->m.mb_width; mb_x++) {
741  unsigned mb = mb_y * ctx->m.mb_width + mb_x;
742  int qscale = ctx->mb_qscale[mb];
743  int i;
744 
745  put_bits(&ctx->m.pb, 12, qscale << 1);
746 
747  dnxhd_get_blocks(ctx, mb_x, mb_y);
748 
749  for (i = 0; i < 8; i++) {
750  int16_t *block = ctx->blocks[i];
751  int overflow, n = dnxhd_switch_matrix(ctx, i);
752  int last_index = ctx->m.dct_quantize(&ctx->m, block, 4 & (2*i),
753  qscale, &overflow);
754  // START_TIMER;
755  dnxhd_encode_block(ctx, block, last_index, n);
756  // STOP_TIMER("encode_block");
757  }
758  }
759  if (put_bits_count(&ctx->m.pb) & 31)
760  put_bits(&ctx->m.pb, 32 - (put_bits_count(&ctx->m.pb) & 31), 0);
761  flush_put_bits(&ctx->m.pb);
762  return 0;
763 }
764 
766 {
767  int mb_y, mb_x;
768  int offset = 0;
769  for (mb_y = 0; mb_y < ctx->m.mb_height; mb_y++) {
770  int thread_size;
771  ctx->slice_offs[mb_y] = offset;
772  ctx->slice_size[mb_y] = 0;
773  for (mb_x = 0; mb_x < ctx->m.mb_width; mb_x++) {
774  unsigned mb = mb_y * ctx->m.mb_width + mb_x;
775  ctx->slice_size[mb_y] += ctx->mb_bits[mb];
776  }
777  ctx->slice_size[mb_y] = (ctx->slice_size[mb_y] + 31) & ~31;
778  ctx->slice_size[mb_y] >>= 3;
779  thread_size = ctx->slice_size[mb_y];
780  offset += thread_size;
781  }
782 }
783 
785  int jobnr, int threadnr)
786 {
787  DNXHDEncContext *ctx = avctx->priv_data;
788  int mb_y = jobnr, mb_x, x, y;
789  int partial_last_row = (mb_y == ctx->m.mb_height - 1) &&
790  ((avctx->height >> ctx->interlaced) & 0xF);
791 
792  ctx = ctx->thread[threadnr];
793  if (ctx->cid_table->bit_depth == 8) {
794  uint8_t *pix = ctx->thread[0]->src[0] + ((mb_y << 4) * ctx->m.linesize);
795  for (mb_x = 0; mb_x < ctx->m.mb_width; ++mb_x, pix += 16) {
796  unsigned mb = mb_y * ctx->m.mb_width + mb_x;
797  int sum;
798  int varc;
799 
800  if (!partial_last_row && mb_x * 16 <= avctx->width - 16 && (avctx->width % 16) == 0) {
801  sum = ctx->m.mpvencdsp.pix_sum(pix, ctx->m.linesize);
802  varc = ctx->m.mpvencdsp.pix_norm1(pix, ctx->m.linesize);
803  } else {
804  int bw = FFMIN(avctx->width - 16 * mb_x, 16);
805  int bh = FFMIN((avctx->height >> ctx->interlaced) - 16 * mb_y, 16);
806  sum = varc = 0;
807  for (y = 0; y < bh; y++) {
808  for (x = 0; x < bw; x++) {
809  uint8_t val = pix[x + y * ctx->m.linesize];
810  sum += val;
811  varc += val * val;
812  }
813  }
814  }
815  varc = (varc - (((unsigned) sum * sum) >> 8) + 128) >> 8;
816 
817  ctx->mb_cmp[mb].value = varc;
818  ctx->mb_cmp[mb].mb = mb;
819  }
820  } else { // 10-bit
821  const int linesize = ctx->m.linesize >> 1;
822  for (mb_x = 0; mb_x < ctx->m.mb_width; ++mb_x) {
823  uint16_t *pix = (uint16_t *)ctx->thread[0]->src[0] +
824  ((mb_y << 4) * linesize) + (mb_x << 4);
825  unsigned mb = mb_y * ctx->m.mb_width + mb_x;
826  int sum = 0;
827  int sqsum = 0;
828  int bw = FFMIN(avctx->width - 16 * mb_x, 16);
829  int bh = FFMIN((avctx->height >> ctx->interlaced) - 16 * mb_y, 16);
830  int mean, sqmean;
831  int i, j;
832  // Macroblocks are 16x16 pixels, unlike DCT blocks which are 8x8.
833  for (i = 0; i < bh; ++i) {
834  for (j = 0; j < bw; ++j) {
835  // Turn 16-bit pixels into 10-bit ones.
836  const int sample = (unsigned) pix[j] >> 6;
837  sum += sample;
838  sqsum += sample * sample;
839  // 2^10 * 2^10 * 16 * 16 = 2^28, which is less than INT_MAX
840  }
841  pix += linesize;
842  }
843  mean = sum >> 8; // 16*16 == 2^8
844  sqmean = sqsum >> 8;
845  ctx->mb_cmp[mb].value = sqmean - mean * mean;
846  ctx->mb_cmp[mb].mb = mb;
847  }
848  }
849  return 0;
850 }
851 
853 {
854  int lambda, up_step, down_step;
855  int last_lower = INT_MAX, last_higher = 0;
856  int x, y, q;
857 
858  for (q = 1; q < avctx->qmax; q++) {
859  ctx->qscale = q;
860  avctx->execute2(avctx, dnxhd_calc_bits_thread,
861  NULL, NULL, ctx->m.mb_height);
862  }
863  up_step = down_step = 2 << LAMBDA_FRAC_BITS;
864  lambda = ctx->lambda;
865 
866  for (;;) {
867  int bits = 0;
868  int end = 0;
869  if (lambda == last_higher) {
870  lambda++;
871  end = 1; // need to set final qscales/bits
872  }
873  for (y = 0; y < ctx->m.mb_height; y++) {
874  for (x = 0; x < ctx->m.mb_width; x++) {
875  unsigned min = UINT_MAX;
876  int qscale = 1;
877  int mb = y * ctx->m.mb_width + x;
878  int rc = 0;
879  for (q = 1; q < avctx->qmax; q++) {
880  int i = (q*ctx->m.mb_num) + mb;
881  unsigned score = ctx->mb_rc[i].bits * lambda +
882  ((unsigned) ctx->mb_rc[i].ssd << LAMBDA_FRAC_BITS);
883  if (score < min) {
884  min = score;
885  qscale = q;
886  rc = i;
887  }
888  }
889  bits += ctx->mb_rc[rc].bits;
890  ctx->mb_qscale[mb] = qscale;
891  ctx->mb_bits[mb] = ctx->mb_rc[rc].bits;
892  }
893  bits = (bits + 31) & ~31; // padding
894  if (bits > ctx->frame_bits)
895  break;
896  }
897  // ff_dlog(ctx->m.avctx,
898  // "lambda %d, up %u, down %u, bits %d, frame %d\n",
899  // lambda, last_higher, last_lower, bits, ctx->frame_bits);
900  if (end) {
901  if (bits > ctx->frame_bits)
902  return AVERROR(EINVAL);
903  break;
904  }
905  if (bits < ctx->frame_bits) {
906  last_lower = FFMIN(lambda, last_lower);
907  if (last_higher != 0)
908  lambda = (lambda+last_higher)>>1;
909  else
910  lambda -= down_step;
911  down_step = FFMIN((int64_t)down_step*5, INT_MAX);
912  up_step = 1<<LAMBDA_FRAC_BITS;
913  lambda = FFMAX(1, lambda);
914  if (lambda == last_lower)
915  break;
916  } else {
917  last_higher = FFMAX(lambda, last_higher);
918  if (last_lower != INT_MAX)
919  lambda = (lambda+last_lower)>>1;
920  else if ((int64_t)lambda + up_step > INT_MAX)
921  return AVERROR(EINVAL);
922  else
923  lambda += up_step;
924  up_step = FFMIN((int64_t)up_step*5, INT_MAX);
925  down_step = 1<<LAMBDA_FRAC_BITS;
926  }
927  }
928  //ff_dlog(ctx->m.avctx, "out lambda %d\n", lambda);
929  ctx->lambda = lambda;
930  return 0;
931 }
932 
934 {
935  int bits = 0;
936  int up_step = 1;
937  int down_step = 1;
938  int last_higher = 0;
939  int last_lower = INT_MAX;
940  int qscale;
941  int x, y;
942 
943  qscale = ctx->qscale;
944  for (;;) {
945  bits = 0;
946  ctx->qscale = qscale;
947  // XXX avoid recalculating bits
949  NULL, NULL, ctx->m.mb_height);
950  for (y = 0; y < ctx->m.mb_height; y++) {
951  for (x = 0; x < ctx->m.mb_width; x++)
952  bits += ctx->mb_rc[(qscale*ctx->m.mb_num) + (y*ctx->m.mb_width+x)].bits;
953  bits = (bits+31)&~31; // padding
954  if (bits > ctx->frame_bits)
955  break;
956  }
957  // ff_dlog(ctx->m.avctx,
958  // "%d, qscale %d, bits %d, frame %d, higher %d, lower %d\n",
959  // ctx->m.avctx->frame_number, qscale, bits, ctx->frame_bits,
960  // last_higher, last_lower);
961  if (bits < ctx->frame_bits) {
962  if (qscale == 1)
963  return 1;
964  if (last_higher == qscale - 1) {
965  qscale = last_higher;
966  break;
967  }
968  last_lower = FFMIN(qscale, last_lower);
969  if (last_higher != 0)
970  qscale = (qscale + last_higher) >> 1;
971  else
972  qscale -= down_step++;
973  if (qscale < 1)
974  qscale = 1;
975  up_step = 1;
976  } else {
977  if (last_lower == qscale + 1)
978  break;
979  last_higher = FFMAX(qscale, last_higher);
980  if (last_lower != INT_MAX)
981  qscale = (qscale + last_lower) >> 1;
982  else
983  qscale += up_step++;
984  down_step = 1;
985  if (qscale >= ctx->m.avctx->qmax)
986  return AVERROR(EINVAL);
987  }
988  }
989  //ff_dlog(ctx->m.avctx, "out qscale %d\n", qscale);
990  ctx->qscale = qscale;
991  return 0;
992 }
993 
994 #define BUCKET_BITS 8
995 #define RADIX_PASSES 4
996 #define NBUCKETS (1 << BUCKET_BITS)
997 
998 static inline int get_bucket(int value, int shift)
999 {
1000  value >>= shift;
1001  value &= NBUCKETS - 1;
1002  return NBUCKETS - 1 - value;
1003 }
1004 
1005 static void radix_count(const RCCMPEntry *data, int size,
1006  int buckets[RADIX_PASSES][NBUCKETS])
1007 {
1008  int i, j;
1009  memset(buckets, 0, sizeof(buckets[0][0]) * RADIX_PASSES * NBUCKETS);
1010  for (i = 0; i < size; i++) {
1011  int v = data[i].value;
1012  for (j = 0; j < RADIX_PASSES; j++) {
1013  buckets[j][get_bucket(v, 0)]++;
1014  v >>= BUCKET_BITS;
1015  }
1016  av_assert1(!v);
1017  }
1018  for (j = 0; j < RADIX_PASSES; j++) {
1019  int offset = size;
1020  for (i = NBUCKETS - 1; i >= 0; i--)
1021  buckets[j][i] = offset -= buckets[j][i];
1022  av_assert1(!buckets[j][0]);
1023  }
1024 }
1025 
1026 static void radix_sort_pass(RCCMPEntry *dst, const RCCMPEntry *data,
1027  int size, int buckets[NBUCKETS], int pass)
1028 {
1029  int shift = pass * BUCKET_BITS;
1030  int i;
1031  for (i = 0; i < size; i++) {
1032  int v = get_bucket(data[i].value, shift);
1033  int pos = buckets[v]++;
1034  dst[pos] = data[i];
1035  }
1036 }
1037 
1038 static void radix_sort(RCCMPEntry *data, int size)
1039 {
1040  int buckets[RADIX_PASSES][NBUCKETS];
1041  RCCMPEntry *tmp = av_malloc_array(size, sizeof(*tmp));
1042  radix_count(data, size, buckets);
1043  radix_sort_pass(tmp, data, size, buckets[0], 0);
1044  radix_sort_pass(data, tmp, size, buckets[1], 1);
1045  if (buckets[2][NBUCKETS - 1] || buckets[3][NBUCKETS - 1]) {
1046  radix_sort_pass(tmp, data, size, buckets[2], 2);
1047  radix_sort_pass(data, tmp, size, buckets[3], 3);
1048  }
1049  av_free(tmp);
1050 }
1051 
1053 {
1054  int max_bits = 0;
1055  int ret, x, y;
1056  if ((ret = dnxhd_find_qscale(ctx)) < 0)
1057  return ret;
1058  for (y = 0; y < ctx->m.mb_height; y++) {
1059  for (x = 0; x < ctx->m.mb_width; x++) {
1060  int mb = y * ctx->m.mb_width + x;
1061  int rc = (ctx->qscale * ctx->m.mb_num ) + mb;
1062  int delta_bits;
1063  ctx->mb_qscale[mb] = ctx->qscale;
1064  ctx->mb_bits[mb] = ctx->mb_rc[rc].bits;
1065  max_bits += ctx->mb_rc[rc].bits;
1066  if (!RC_VARIANCE) {
1067  delta_bits = ctx->mb_rc[rc].bits -
1068  ctx->mb_rc[rc + ctx->m.mb_num].bits;
1069  ctx->mb_cmp[mb].mb = mb;
1070  ctx->mb_cmp[mb].value =
1071  delta_bits ? ((ctx->mb_rc[rc].ssd -
1072  ctx->mb_rc[rc + ctx->m.mb_num].ssd) * 100) /
1073  delta_bits
1074  : INT_MIN; // avoid increasing qscale
1075  }
1076  }
1077  max_bits += 31; // worst padding
1078  }
1079  if (!ret) {
1080  if (RC_VARIANCE)
1081  avctx->execute2(avctx, dnxhd_mb_var_thread,
1082  NULL, NULL, ctx->m.mb_height);
1083  radix_sort(ctx->mb_cmp, ctx->m.mb_num);
1084  for (x = 0; x < ctx->m.mb_num && max_bits > ctx->frame_bits; x++) {
1085  int mb = ctx->mb_cmp[x].mb;
1086  int rc = (ctx->qscale * ctx->m.mb_num ) + mb;
1087  max_bits -= ctx->mb_rc[rc].bits -
1088  ctx->mb_rc[rc + ctx->m.mb_num].bits;
1089  ctx->mb_qscale[mb] = ctx->qscale + 1;
1090  ctx->mb_bits[mb] = ctx->mb_rc[rc + ctx->m.mb_num].bits;
1091  }
1092  }
1093  return 0;
1094 }
1095 
1097 {
1098  int i;
1099 
1100  for (i = 0; i < ctx->m.avctx->thread_count; i++) {
1101  ctx->thread[i]->m.linesize = frame->linesize[0] << ctx->interlaced;
1102  ctx->thread[i]->m.uvlinesize = frame->linesize[1] << ctx->interlaced;
1103  ctx->thread[i]->dct_y_offset = ctx->m.linesize *8;
1104  ctx->thread[i]->dct_uv_offset = ctx->m.uvlinesize*8;
1105  }
1106 
1107 #if FF_API_CODED_FRAME
1111 #endif
1112  ctx->cur_field = frame->interlaced_frame && !frame->top_field_first;
1113 }
1114 
1116  const AVFrame *frame, int *got_packet)
1117 {
1118  DNXHDEncContext *ctx = avctx->priv_data;
1119  int first_field = 1;
1120  int offset, i, ret;
1121  uint8_t *buf;
1122 
1123  if ((ret = ff_alloc_packet2(avctx, pkt, ctx->frame_size, 0)) < 0)
1124  return ret;
1125  buf = pkt->data;
1126 
1127  dnxhd_load_picture(ctx, frame);
1128 
1129 encode_coding_unit:
1130  for (i = 0; i < 3; i++) {
1131  ctx->src[i] = frame->data[i];
1132  if (ctx->interlaced && ctx->cur_field)
1133  ctx->src[i] += frame->linesize[i];
1134  }
1135 
1136  dnxhd_write_header(avctx, buf);
1137 
1138  if (avctx->mb_decision == FF_MB_DECISION_RD)
1139  ret = dnxhd_encode_rdo(avctx, ctx);
1140  else
1141  ret = dnxhd_encode_fast(avctx, ctx);
1142  if (ret < 0) {
1143  av_log(avctx, AV_LOG_ERROR,
1144  "picture could not fit ratecontrol constraints, increase qmax\n");
1145  return ret;
1146  }
1147 
1149 
1150  offset = 0;
1151  for (i = 0; i < ctx->m.mb_height; i++) {
1152  AV_WB32(ctx->msip + i * 4, offset);
1153  offset += ctx->slice_size[i];
1154  av_assert1(!(ctx->slice_size[i] & 3));
1155  }
1156 
1157  avctx->execute2(avctx, dnxhd_encode_thread, buf, NULL, ctx->m.mb_height);
1158 
1159  av_assert1(ctx->data_offset + offset + 4 <= ctx->coding_unit_size);
1160  memset(buf + ctx->data_offset + offset, 0,
1161  ctx->coding_unit_size - 4 - offset - ctx->data_offset);
1162 
1163  AV_WB32(buf + ctx->coding_unit_size - 4, 0x600DC0DE); // EOF
1164 
1165  if (ctx->interlaced && first_field) {
1166  first_field = 0;
1167  ctx->cur_field ^= 1;
1168  buf += ctx->coding_unit_size;
1169  goto encode_coding_unit;
1170  }
1171 
1172 #if FF_API_CODED_FRAME
1174  avctx->coded_frame->quality = ctx->qscale * FF_QP2LAMBDA;
1176 #endif
1177 
1179 
1180  pkt->flags |= AV_PKT_FLAG_KEY;
1181  *got_packet = 1;
1182  return 0;
1183 }
1184 
1186 {
1187  DNXHDEncContext *ctx = avctx->priv_data;
1188  int max_level = 1 << (ctx->cid_table->bit_depth + 2);
1189  int i;
1190 
1191  av_free(ctx->vlc_codes - max_level * 2);
1192  av_free(ctx->vlc_bits - max_level * 2);
1193  av_freep(&ctx->run_codes);
1194  av_freep(&ctx->run_bits);
1195 
1196  av_freep(&ctx->mb_bits);
1197  av_freep(&ctx->mb_qscale);
1198  av_freep(&ctx->mb_rc);
1199  av_freep(&ctx->mb_cmp);
1200  av_freep(&ctx->slice_size);
1201  av_freep(&ctx->slice_offs);
1202 
1203  av_freep(&ctx->qmatrix_c);
1204  av_freep(&ctx->qmatrix_l);
1205  av_freep(&ctx->qmatrix_c16);
1206  av_freep(&ctx->qmatrix_l16);
1207 
1208  for (i = 1; i < avctx->thread_count; i++)
1209  av_freep(&ctx->thread[i]);
1210 
1211  return 0;
1212 }
1213 
1214 static const AVCodecDefault dnxhd_defaults[] = {
1215  { "qmax", "1024" }, /* Maximum quantization scale factor allowed for VC-3 */
1216  { NULL },
1217 };
1218 
1220  .name = "dnxhd",
1221  .long_name = NULL_IF_CONFIG_SMALL("VC3/DNxHD"),
1222  .type = AVMEDIA_TYPE_VIDEO,
1223  .id = AV_CODEC_ID_DNXHD,
1224  .priv_data_size = sizeof(DNXHDEncContext),
1226  .encode2 = dnxhd_encode_picture,
1227  .close = dnxhd_encode_end,
1228  .capabilities = AV_CODEC_CAP_SLICE_THREADS,
1229  .pix_fmts = (const enum AVPixelFormat[]) {
1233  },
1234  .priv_class = &dnxhd_class,
1235  .defaults = dnxhd_defaults,
1237 };
#define FF_PROFILE_DNXHD
Definition: avcodec.h:3176
static av_always_inline int dnxhd_ssd_block(int16_t *qblock, int16_t *block)
Definition: dnxhdenc.c:587
#define MASK_ABS(mask, level)
Definition: mathops.h:164
IDCTDSPContext idsp
Definition: mpegvideo.h:227
static void radix_count(const RCCMPEntry *data, int size, int buckets[RADIX_PASSES][NBUCKETS])
Definition: dnxhdenc.c:1005
#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:2185
static av_always_inline void dnxhd_get_blocks(DNXHDEncContext *ctx, int mb_x, int mb_y)
Definition: dnxhdenc.c:616
static int shift(int a, int b)
Definition: sonic.c:82
RCEntry * mb_rc
Definition: dnxhdenc.h:97
This structure describes decoded (raw) audio or video data.
Definition: frame.h:184
#define FF_ALLOCZ_ARRAY_OR_GOTO(ctx, p, nelem, elsize, label)
Definition: internal.h:157
const uint8_t * dc_bits
Definition: dnxhddata.h:52
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:641
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:871
#define LAMBDA_FRAC_BITS
Definition: dnxhdenc.c:43
static void put_bits(Jpeg2000EncoderContext *s, int val, int n)
put n times val bit
Definition: j2kenc.c:206
const uint8_t * luma_weight
Definition: dnxhddata.h:51
#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:76
int num
numerator
Definition: rational.h:44
const CIDEntry ff_dnxhd_cid_table[]
Definition: dnxhddata.c:933
const uint16_t * run_codes
Definition: dnxhddata.h:55
static const AVClass dnxhd_class
Definition: dnxhdenc.c:70
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:1885
unsigned dct_uv_offset
Definition: dnxhdenc.h:59
static av_always_inline void dnxhd_encode_dc(DNXHDEncContext *ctx, int diff)
Definition: dnxhdenc.c:503
static av_cold int dnxhd_init_vlc(DNXHDEncContext *ctx)
Definition: dnxhdenc.c:146
mpegvideo header.
uint8_t permutated[64]
Definition: idctdsp.h:31
int ff_dnxhd_find_cid(AVCodecContext *avctx, int bit_depth)
Definition: dnxhddata.c:1129
uint8_t run
Definition: svq3.c:192
static AVPacket pkt
int bits_per_raw_sample
Bits per sample/pixel of internal libavcodec pixel/sample format.
Definition: avcodec.h:3057
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:54
int profile
profile
Definition: avcodec.h:3161
#define sample
int av_log2_16bit(unsigned v)
Definition: intmath.c:31
AVCodec.
Definition: avcodec.h:3559
static av_always_inline int dnxhd_calc_ac_bits(DNXHDEncContext *ctx, int16_t *block, int last_index)
Definition: dnxhdenc.c:597
int h263_aic
Advanced INTRA Coding (AIC)
Definition: mpegvideo.h:84
Macro definitions for various function/variable attributes.
int coding_unit_size
Definition: dnxhdenc.h:63
int intra_quant_bias
Definition: dnxhdenc.h:71
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:73
#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 permutation.
av_cold void ff_mpegvideoencdsp_init(MpegvideoEncDSPContext *c, AVCodecContext *avctx)
uint8_t bits
Definition: crc.c:296
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:99
#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:998
const AVProfile ff_dnxhd_profiles[]
Definition: profiles.c:49
static av_cold int end(AVCodecContext *avctx)
Definition: avrndec.c:90
static int dnxhd_encode_fast(AVCodecContext *avctx, DNXHDEncContext *ctx)
Definition: dnxhdenc.c:1052
uint32_t * slice_size
Definition: dnxhdenc.h:51
int(* qmatrix_c)[64]
Definition: dnxhdenc.h:75
#define RADIX_PASSES
Definition: dnxhdenc.c:995
uint32_t * slice_offs
Definition: dnxhdenc.h:52
int(* q_chroma_intra_matrix)[64]
Definition: mpegvideo.h:324
unsigned qscale
Definition: dnxhdenc.h:90
static AVFrame * frame
static int dnxhd_get_hr_frame_size(const CIDEntry *profile, int mb_num)
Definition: dnxhdenc.c:303
uint8_t * data
Definition: avcodec.h:1588
static void radix_sort_pass(RCCMPEntry *dst, const RCCMPEntry *data, int size, int buckets[NBUCKETS], int pass)
Definition: dnxhdenc.c:1026
const uint8_t * run_bits
Definition: dnxhddata.h:56
#define BUCKET_BITS
Definition: dnxhdenc.c:994
const uint8_t * scantable
Definition: idctdsp.h:30
int interlaced_frame
The content of the picture is interlaced.
Definition: frame.h:318
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:207
unsigned int coding_unit_size
Definition: dnxhddata.h:46
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:1185
#define AV_PKT_FLAG_KEY
The packet contains a keyframe.
Definition: avcodec.h:1620
const uint8_t * ac_bits
Definition: dnxhddata.h:54
const uint8_t * ac_info
Definition: dnxhddata.h:54
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:666
const uint16_t * ac_codes
Definition: dnxhddata.h:53
static int dnxhd_calc_bits_thread(AVCodecContext *avctx, void *arg, int jobnr, int threadnr)
Definition: dnxhdenc.c:672
#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:1080
int last_dc[3]
last DC values for MPEG-1
Definition: mpegvideo.h:182
static int dnxhd_find_qscale(DNXHDEncContext *ctx)
Definition: dnxhdenc.c:933
AVRational packet_scale
Definition: dnxhddata.h:59
#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:2606
#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:1219
static av_always_inline void dnxhd_unquantize_c(DNXHDEncContext *ctx, int16_t *block, int n, int qscale, int last_index)
Definition: dnxhdenc.c:544
#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:1752
static int dnxhd_write_header(AVCodecContext *avctx, uint8_t *buf)
Definition: dnxhdenc.c:467
const uint8_t * dc_codes
Definition: dnxhddata.h:52
MpegvideoEncDSPContext mpvencdsp
Definition: mpegvideo.h:230
const char * name
Name of the codec implementation.
Definition: avcodec.h:3566
#define FF_PROFILE_DNXHR_LB
Definition: avcodec.h:3177
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:82
int flags
A combination of AV_PKT_FLAG values.
Definition: avcodec.h:1594
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:66
#define pass
Definition: fft_template.c:532
const uint8_t * chroma_weight
Definition: dnxhddata.h:51
uint16_t * run_codes
Definition: dnxhdenc.h:85
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:77
enum AVPictureType pict_type
Picture type of the frame.
Definition: frame.h:258
#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:1214
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:518
uint8_t * run_bits
Definition: dnxhdenc.h:86
int width
picture width / height.
Definition: avcodec.h:1844
const uint8_t * run
Definition: dnxhddata.h:56
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:80
AVFormatContext * ctx
Definition: movenc.c:48
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:101
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:77
int quality
quality (between 1 (good) and FF_LAMBDA_MAX (bad))
Definition: frame.h:294
ScanTable scantable
Definition: dnxhddec.c:66
uint8_t * msip
Macroblock Scan Indexes Payload.
Definition: dnxhdenc.h:50
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:2219
void ff_dnxhd_print_profiles(AVCodecContext *avctx, int loglevel)
Definition: dnxhddata.c:1159
#define FF_PROFILE_DNXHR_HQ
Definition: avcodec.h:3179
uint8_t * vlc_bits
Definition: dnxhdenc.h:84
int thread_count
thread count is used to decide how many independent tasks should be passed to execute() ...
Definition: avcodec.h:3087
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:1038
static int dnxhd_encode_rdo(AVCodecContext *avctx, DNXHDEncContext *ctx)
Definition: dnxhdenc.c:852
#define AVERROR_PATCHWELCOME
Not yet implemented in FFmpeg, patches welcome.
Definition: error.h:62
#define AV_CODEC_CAP_SLICE_THREADS
Codec supports slice-based (or partition-based) multithreading.
Definition: avcodec.h:1023
int bit_depth
Definition: dnxhddata.h:49
int index_bits
Definition: dnxhddata.h:48
Libavcodec external API header.
static int dnxhd_10bit_dct_quantize(MpegEncContext *ctx, int16_t *block, int n, int qscale, int *overflow)
Definition: dnxhdenc.c:115
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:215
static int dnxhd_encode_picture(AVCodecContext *avctx, AVPacket *pkt, const AVFrame *frame, int *got_packet)
Definition: dnxhdenc.c:1115
void(* fdct)(int16_t *block)
Definition: fdctdsp.h:27
main external API structure.
Definition: avcodec.h:1657
unsigned block_width_l2
Definition: dnxhdenc.h:60
ScanTable intra_scantable
Definition: mpegvideo.h:88
#define FF_DEFAULT_QUANT_BIAS
Definition: avcodec.h:2186
int ff_dct_encode_init(MpegEncContext *s)
static av_cold int dnxhd_init_rc(DNXHDEncContext *ctx)
Definition: dnxhdenc.c:287
unsigned int codec_tag
fourcc (LSB first, so "ABCD" -> ('D'<<24) + ('C'<<16) + ('B'<<8) + 'A').
Definition: avcodec.h:1689
unsigned lambda
Definition: dnxhdenc.h:91
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:2222
#define NBUCKETS
Definition: dnxhdenc.c:996
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:765
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:1690
#define FF_PROFILE_DNXHR_SQ
Definition: avcodec.h:3178
const CIDEntry * cid_table
Definition: dnxhdenc.h:49
uint16_t(* qmatrix_c16)[2][64]
Definition: dnxhdenc.h:78
unsigned dct_y_offset
Definition: dnxhdenc.h:58
static enum AVPixelFormat pix_fmts[]
Definition: libkvazaar.c:262
unsigned min_padding
Definition: dnxhdenc.h:70
mfxU16 profile
Definition: qsvenc.c:42
void avpriv_report_missing_feature(void *avc, const char *msg,...) av_printf_format(2
Log a generic warning message about a missing feature.
static void dnxhd_load_picture(DNXHDEncContext *ctx, const AVFrame *frame)
Definition: dnxhdenc.c:1096
#define AV_PIX_FMT_YUV422P10
Definition: pixfmt.h:344
#define DNX10BIT_QMAT_SHIFT
Definition: dnxhdenc.c:41
uint8_t * data[AV_NUM_DATA_POINTERS]
pointer to the picture/channel planes.
Definition: frame.h:198
unsigned int frame_size
Definition: dnxhddata.h:45
uint8_t level
Definition: svq3.c:193
uint16_t * mb_bits
Definition: dnxhdenc.h:93
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:784
#define DNXHD_VARIABLE
Indicate that a CIDEntry value must be read in the bitstream.
Definition: dnxhddata.h:40
#define FF_PROFILE_DNXHR_HQX
Definition: avcodec.h:3180
static int dnxhd_encode_thread(AVCodecContext *avctx, void *arg, int jobnr, int threadnr)
Definition: dnxhdenc.c:728
static av_cold int dnxhd_encode_init(AVCodecContext *avctx)
Definition: dnxhdenc.c:309
#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:142
attribute_deprecated AVFrame * coded_frame
the picture in the bitstream
Definition: avcodec.h:3078
static void init_put_bits(PutBitContext *s, uint8_t *buffer, int buffer_size)
Initialize the PutBitContext s.
Definition: put_bits.h:48
int den
denominator
Definition: rational.h:45
#define FF_PROFILE_DNXHR_444
Definition: avcodec.h:3181
void * priv_data
Definition: avcodec.h:1699
static av_always_inline int diff(const uint32_t a, const uint32_t b)
#define av_free(p)
RCCMPEntry * mb_cmp
Definition: dnxhdenc.h:96
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:323
int nitris_compat
Definition: dnxhdenc.h:69
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:3147
int bits
Definition: dnxhdenc.h:39
static uint8_t tmp[8]
Definition: des.c:38
AVCodecContext * avctx
Definition: dnxhddec.c:52
int key_frame
1 -> keyframe, 0-> not
Definition: frame.h:253
#define FF_QP2LAMBDA
factor to convert from H.263 QP to lambda
Definition: avutil.h:219
const CIDEntry * cid_table
Definition: dnxhddec.c:67
#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
#define MKTAG(a, b, c, d)
Definition: common.h:342
float min
AVPixelFormat
Pixel format.
Definition: pixfmt.h:60
This structure stores compressed data.
Definition: avcodec.h:1565
uint32_t * vlc_codes
Definition: dnxhdenc.h:83
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:81
uint8_t * mb_qscale
Definition: dnxhdenc.h:94
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