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_INT, { .i64 = 0 }, 0, 1, VE },
49  { NULL }
50 };
51 
52 static const AVClass dnxhd_class = {
53  .class_name = "dnxhd",
54  .item_name = av_default_item_name,
55  .option = options,
56  .version = LIBAVUTIL_VERSION_INT,
57 };
58 
59 static void dnxhd_8bit_get_pixels_8x4_sym(int16_t *av_restrict block,
60  const uint8_t *pixels,
61  ptrdiff_t line_size)
62 {
63  int i;
64  for (i = 0; i < 4; i++) {
65  block[0] = pixels[0];
66  block[1] = pixels[1];
67  block[2] = pixels[2];
68  block[3] = pixels[3];
69  block[4] = pixels[4];
70  block[5] = pixels[5];
71  block[6] = pixels[6];
72  block[7] = pixels[7];
73  pixels += line_size;
74  block += 8;
75  }
76  memcpy(block, block - 8, sizeof(*block) * 8);
77  memcpy(block + 8, block - 16, sizeof(*block) * 8);
78  memcpy(block + 16, block - 24, sizeof(*block) * 8);
79  memcpy(block + 24, block - 32, sizeof(*block) * 8);
80 }
81 
82 static av_always_inline
83 void dnxhd_10bit_get_pixels_8x4_sym(int16_t *av_restrict block,
84  const uint8_t *pixels,
85  ptrdiff_t line_size)
86 {
87  int i;
88  const uint16_t* pixels16 = (const uint16_t*)pixels;
89  line_size >>= 1;
90 
91  for (i = 0; i < 4; i++) {
92  block[0] = pixels16[0]; block[1] = pixels16[1];
93  block[2] = pixels16[2]; block[3] = pixels16[3];
94  block[4] = pixels16[4]; block[5] = pixels16[5];
95  block[6] = pixels16[6]; block[7] = pixels16[7];
96  pixels16 += line_size;
97  block += 8;
98  }
99  memcpy(block, block - 8, sizeof(*block) * 8);
100  memcpy(block + 8, block - 16, sizeof(*block) * 8);
101  memcpy(block + 16, block - 24, sizeof(*block) * 8);
102  memcpy(block + 24, block - 32, sizeof(*block) * 8);
103 }
104 
106  int n, int qscale, int *overflow)
107 {
109  const int *qmat = n<4 ? ctx->q_intra_matrix[qscale] : ctx->q_chroma_intra_matrix[qscale];
110  int last_non_zero = 0;
111  int i;
112 
113  ctx->fdsp.fdct(block);
114 
115  // Divide by 4 with rounding, to compensate scaling of DCT coefficients
116  block[0] = (block[0] + 2) >> 2;
117 
118  for (i = 1; i < 64; ++i) {
119  int j = scantable[i];
120  int sign = block[j] >> 31;
121  int level = (block[j] ^ sign) - sign;
122  level = level * qmat[j] >> DNX10BIT_QMAT_SHIFT;
123  block[j] = (level ^ sign) - sign;
124  if (level)
125  last_non_zero = i;
126  }
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_level[j] >> 1 == alevel &&
159  (!offset || (ctx->cid_table->ac_flags[j] & 1) && offset) &&
160  (!run || (ctx->cid_table->ac_flags[j] & 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->m.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->m.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 = ctx->m.idsp.idct_permutation[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);
318 
319  ctx->m.avctx = avctx;
320  ctx->m.mb_intra = 1;
321  ctx->m.h263_aic = 1;
322 
323  avctx->bits_per_raw_sample = ctx->cid_table->bit_depth;
324 
325  ff_blockdsp_init(&ctx->bdsp, avctx);
326  ff_fdctdsp_init(&ctx->m.fdsp, avctx);
327  ff_mpv_idct_init(&ctx->m);
328  ff_mpegvideoencdsp_init(&ctx->m.mpvencdsp, avctx);
329  ff_pixblockdsp_init(&ctx->m.pdsp, avctx);
330  ff_dct_encode_init(&ctx->m);
331 
332  if (!ctx->m.dct_quantize)
334 
335  if (ctx->cid_table->bit_depth == 10) {
338  ctx->block_width_l2 = 4;
339  } else {
341  ctx->block_width_l2 = 3;
342  }
343 
344  if (ARCH_X86)
346 
347  ctx->m.mb_height = (avctx->height + 15) / 16;
348  ctx->m.mb_width = (avctx->width + 15) / 16;
349 
350  if (avctx->flags & CODEC_FLAG_INTERLACED_DCT) {
351  ctx->interlaced = 1;
352  ctx->m.mb_height /= 2;
353  }
354 
355  ctx->m.mb_num = ctx->m.mb_height * ctx->m.mb_width;
356 
358  ctx->m.intra_quant_bias = avctx->intra_quant_bias;
359  // XXX tune lbias/cbias
360  if ((ret = dnxhd_init_qmat(ctx, ctx->m.intra_quant_bias, 0)) < 0)
361  return ret;
362 
363  /* Avid Nitris hardware decoder requires a minimum amount of padding
364  * in the coding unit payload */
365  if (ctx->nitris_compat)
366  ctx->min_padding = 1600;
367 
368  if ((ret = dnxhd_init_vlc(ctx)) < 0)
369  return ret;
370  if ((ret = dnxhd_init_rc(ctx)) < 0)
371  return ret;
372 
373  FF_ALLOCZ_OR_GOTO(ctx->m.avctx, ctx->slice_size,
374  ctx->m.mb_height * sizeof(uint32_t), fail);
375  FF_ALLOCZ_OR_GOTO(ctx->m.avctx, ctx->slice_offs,
376  ctx->m.mb_height * sizeof(uint32_t), fail);
377  FF_ALLOCZ_OR_GOTO(ctx->m.avctx, ctx->mb_bits,
378  ctx->m.mb_num * sizeof(uint16_t), fail);
379  FF_ALLOCZ_OR_GOTO(ctx->m.avctx, ctx->mb_qscale,
380  ctx->m.mb_num * sizeof(uint8_t), fail);
381 
382  avctx->coded_frame = av_frame_alloc();
383  if (!avctx->coded_frame)
384  return AVERROR(ENOMEM);
385 
386  avctx->coded_frame->key_frame = 1;
388 
389  if (avctx->thread_count > MAX_THREADS) {
390  av_log(avctx, AV_LOG_ERROR, "too many threads\n");
391  return AVERROR(EINVAL);
392  }
393 
394  if (avctx->qmax <= 1) {
395  av_log(avctx, AV_LOG_ERROR, "qmax must be at least 2\n");
396  return AVERROR(EINVAL);
397  }
398 
399  ctx->thread[0] = ctx;
400  for (i = 1; i < avctx->thread_count; i++) {
401  ctx->thread[i] = av_malloc(sizeof(DNXHDEncContext));
402  memcpy(ctx->thread[i], ctx, sizeof(DNXHDEncContext));
403  }
404 
405  return 0;
406 fail: // for FF_ALLOCZ_OR_GOTO
407  return AVERROR(ENOMEM);
408 }
409 
411 {
412  DNXHDEncContext *ctx = avctx->priv_data;
413  static const uint8_t header_prefix[5] = { 0x00, 0x00, 0x02, 0x80, 0x01 };
414 
415  memset(buf, 0, 640);
416 
417  memcpy(buf, header_prefix, 5);
418  buf[5] = ctx->interlaced ? ctx->cur_field + 2 : 0x01;
419  buf[6] = 0x80; // crc flag off
420  buf[7] = 0xa0; // reserved
421  AV_WB16(buf + 0x18, avctx->height >> ctx->interlaced); // ALPF
422  AV_WB16(buf + 0x1a, avctx->width); // SPL
423  AV_WB16(buf + 0x1d, avctx->height >> ctx->interlaced); // NAL
424 
425  buf[0x21] = ctx->cid_table->bit_depth == 10 ? 0x58 : 0x38;
426  buf[0x22] = 0x88 + (ctx->interlaced << 2);
427  AV_WB32(buf + 0x28, ctx->cid); // CID
428  buf[0x2c] = ctx->interlaced ? 0 : 0x80;
429 
430  buf[0x5f] = 0x01; // UDL
431 
432  buf[0x167] = 0x02; // reserved
433  AV_WB16(buf + 0x16a, ctx->m.mb_height * 4 + 4); // MSIPS
434  buf[0x16d] = ctx->m.mb_height; // Ns
435  buf[0x16f] = 0x10; // reserved
436 
437  ctx->msip = buf + 0x170;
438  return 0;
439 }
440 
442 {
443  int nbits;
444  if (diff < 0) {
445  nbits = av_log2_16bit(-2 * diff);
446  diff--;
447  } else {
448  nbits = av_log2_16bit(2 * diff);
449  }
450  put_bits(&ctx->m.pb, ctx->cid_table->dc_bits[nbits] + nbits,
451  (ctx->cid_table->dc_codes[nbits] << nbits) +
452  (diff & ((1 << nbits) - 1)));
453 }
454 
455 static av_always_inline
457  int last_index, int n)
458 {
459  int last_non_zero = 0;
460  int slevel, i, j;
461 
462  dnxhd_encode_dc(ctx, block[0] - ctx->m.last_dc[n]);
463  ctx->m.last_dc[n] = block[0];
464 
465  for (i = 1; i <= last_index; i++) {
466  j = ctx->m.intra_scantable.permutated[i];
467  slevel = block[j];
468  if (slevel) {
469  int run_level = i - last_non_zero - 1;
470  int rlevel = (slevel << 1) | !!run_level;
471  put_bits(&ctx->m.pb, ctx->vlc_bits[rlevel], ctx->vlc_codes[rlevel]);
472  if (run_level)
473  put_bits(&ctx->m.pb, ctx->run_bits[run_level],
474  ctx->run_codes[run_level]);
475  last_non_zero = i;
476  }
477  }
478  put_bits(&ctx->m.pb, ctx->vlc_bits[0], ctx->vlc_codes[0]); // EOB
479 }
480 
481 static av_always_inline
482 void dnxhd_unquantize_c(DNXHDEncContext *ctx, int16_t *block, int n,
483  int qscale, int last_index)
484 {
485  const uint8_t *weight_matrix;
486  int level;
487  int i;
488 
489  weight_matrix = (n & 2) ? ctx->cid_table->chroma_weight
490  : ctx->cid_table->luma_weight;
491 
492  for (i = 1; i <= last_index; i++) {
493  int j = ctx->m.intra_scantable.permutated[i];
494  level = block[j];
495  if (level) {
496  if (level < 0) {
497  level = (1 - 2 * level) * qscale * weight_matrix[i];
498  if (ctx->cid_table->bit_depth == 10) {
499  if (weight_matrix[i] != 8)
500  level += 8;
501  level >>= 4;
502  } else {
503  if (weight_matrix[i] != 32)
504  level += 32;
505  level >>= 6;
506  }
507  level = -level;
508  } else {
509  level = (2 * level + 1) * qscale * weight_matrix[i];
510  if (ctx->cid_table->bit_depth == 10) {
511  if (weight_matrix[i] != 8)
512  level += 8;
513  level >>= 4;
514  } else {
515  if (weight_matrix[i] != 32)
516  level += 32;
517  level >>= 6;
518  }
519  }
520  block[j] = level;
521  }
522  }
523 }
524 
525 static av_always_inline int dnxhd_ssd_block(int16_t *qblock, int16_t *block)
526 {
527  int score = 0;
528  int i;
529  for (i = 0; i < 64; i++)
530  score += (block[i] - qblock[i]) * (block[i] - qblock[i]);
531  return score;
532 }
533 
534 static av_always_inline
535 int dnxhd_calc_ac_bits(DNXHDEncContext *ctx, int16_t *block, int last_index)
536 {
537  int last_non_zero = 0;
538  int bits = 0;
539  int i, j, level;
540  for (i = 1; i <= last_index; i++) {
541  j = ctx->m.intra_scantable.permutated[i];
542  level = block[j];
543  if (level) {
544  int run_level = i - last_non_zero - 1;
545  bits += ctx->vlc_bits[(level << 1) |
546  !!run_level] + ctx->run_bits[run_level];
547  last_non_zero = i;
548  }
549  }
550  return bits;
551 }
552 
553 static av_always_inline
554 void dnxhd_get_blocks(DNXHDEncContext *ctx, int mb_x, int mb_y)
555 {
556  const int bs = ctx->block_width_l2;
557  const int bw = 1 << bs;
558  const uint8_t *ptr_y = ctx->thread[0]->src[0] +
559  ((mb_y << 4) * ctx->m.linesize) + (mb_x << bs + 1);
560  const uint8_t *ptr_u = ctx->thread[0]->src[1] +
561  ((mb_y << 4) * ctx->m.uvlinesize) + (mb_x << bs);
562  const uint8_t *ptr_v = ctx->thread[0]->src[2] +
563  ((mb_y << 4) * ctx->m.uvlinesize) + (mb_x << bs);
564  PixblockDSPContext *pdsp = &ctx->m.pdsp;
565 
566  pdsp->get_pixels(ctx->blocks[0], ptr_y, ctx->m.linesize);
567  pdsp->get_pixels(ctx->blocks[1], ptr_y + bw, ctx->m.linesize);
568  pdsp->get_pixels(ctx->blocks[2], ptr_u, ctx->m.uvlinesize);
569  pdsp->get_pixels(ctx->blocks[3], ptr_v, ctx->m.uvlinesize);
570 
571  if (mb_y + 1 == ctx->m.mb_height && ctx->m.avctx->height == 1080) {
572  if (ctx->interlaced) {
573  ctx->get_pixels_8x4_sym(ctx->blocks[4],
574  ptr_y + ctx->dct_y_offset,
575  ctx->m.linesize);
576  ctx->get_pixels_8x4_sym(ctx->blocks[5],
577  ptr_y + ctx->dct_y_offset + bw,
578  ctx->m.linesize);
579  ctx->get_pixels_8x4_sym(ctx->blocks[6],
580  ptr_u + ctx->dct_uv_offset,
581  ctx->m.uvlinesize);
582  ctx->get_pixels_8x4_sym(ctx->blocks[7],
583  ptr_v + ctx->dct_uv_offset,
584  ctx->m.uvlinesize);
585  } else {
586  ctx->bdsp.clear_block(ctx->blocks[4]);
587  ctx->bdsp.clear_block(ctx->blocks[5]);
588  ctx->bdsp.clear_block(ctx->blocks[6]);
589  ctx->bdsp.clear_block(ctx->blocks[7]);
590  }
591  } else {
592  pdsp->get_pixels(ctx->blocks[4],
593  ptr_y + ctx->dct_y_offset, ctx->m.linesize);
594  pdsp->get_pixels(ctx->blocks[5],
595  ptr_y + ctx->dct_y_offset + bw, ctx->m.linesize);
596  pdsp->get_pixels(ctx->blocks[6],
597  ptr_u + ctx->dct_uv_offset, ctx->m.uvlinesize);
598  pdsp->get_pixels(ctx->blocks[7],
599  ptr_v + ctx->dct_uv_offset, ctx->m.uvlinesize);
600  }
601 }
602 
603 static av_always_inline
605 {
606  const static uint8_t component[8]={0,0,1,2,0,0,1,2};
607  return component[i];
608 }
609 
611  int jobnr, int threadnr)
612 {
613  DNXHDEncContext *ctx = avctx->priv_data;
614  int mb_y = jobnr, mb_x;
615  int qscale = ctx->qscale;
616  LOCAL_ALIGNED_16(int16_t, block, [64]);
617  ctx = ctx->thread[threadnr];
618 
619  ctx->m.last_dc[0] =
620  ctx->m.last_dc[1] =
621  ctx->m.last_dc[2] = 1 << (ctx->cid_table->bit_depth + 2);
622 
623  for (mb_x = 0; mb_x < ctx->m.mb_width; mb_x++) {
624  unsigned mb = mb_y * ctx->m.mb_width + mb_x;
625  int ssd = 0;
626  int ac_bits = 0;
627  int dc_bits = 0;
628  int i;
629 
630  dnxhd_get_blocks(ctx, mb_x, mb_y);
631 
632  for (i = 0; i < 8; i++) {
633  int16_t *src_block = ctx->blocks[i];
634  int overflow, nbits, diff, last_index;
635  int n = dnxhd_switch_matrix(ctx, i);
636 
637  memcpy(block, src_block, 64 * sizeof(*block));
638  last_index = ctx->m.dct_quantize(&ctx->m, block, 4 & (2*i),
639  qscale, &overflow);
640  ac_bits += dnxhd_calc_ac_bits(ctx, block, last_index);
641 
642  diff = block[0] - ctx->m.last_dc[n];
643  if (diff < 0)
644  nbits = av_log2_16bit(-2 * diff);
645  else
646  nbits = av_log2_16bit(2 * diff);
647 
648  av_assert1(nbits < ctx->cid_table->bit_depth + 4);
649  dc_bits += ctx->cid_table->dc_bits[nbits] + nbits;
650 
651  ctx->m.last_dc[n] = block[0];
652 
653  if (avctx->mb_decision == FF_MB_DECISION_RD || !RC_VARIANCE) {
654  dnxhd_unquantize_c(ctx, block, i, qscale, last_index);
655  ctx->m.idsp.idct(block);
656  ssd += dnxhd_ssd_block(block, src_block);
657  }
658  }
659  ctx->mb_rc[qscale][mb].ssd = ssd;
660  ctx->mb_rc[qscale][mb].bits = ac_bits + dc_bits + 12 +
661  8 * ctx->vlc_bits[0];
662  }
663  return 0;
664 }
665 
667  int jobnr, int threadnr)
668 {
669  DNXHDEncContext *ctx = avctx->priv_data;
670  int mb_y = jobnr, mb_x;
671  ctx = ctx->thread[threadnr];
672  init_put_bits(&ctx->m.pb, (uint8_t *)arg + 640 + ctx->slice_offs[jobnr],
673  ctx->slice_size[jobnr]);
674 
675  ctx->m.last_dc[0] =
676  ctx->m.last_dc[1] =
677  ctx->m.last_dc[2] = 1 << (ctx->cid_table->bit_depth + 2);
678  for (mb_x = 0; mb_x < ctx->m.mb_width; mb_x++) {
679  unsigned mb = mb_y * ctx->m.mb_width + mb_x;
680  int qscale = ctx->mb_qscale[mb];
681  int i;
682 
683  put_bits(&ctx->m.pb, 12, qscale << 1);
684 
685  dnxhd_get_blocks(ctx, mb_x, mb_y);
686 
687  for (i = 0; i < 8; i++) {
688  int16_t *block = ctx->blocks[i];
689  int overflow, n = dnxhd_switch_matrix(ctx, i);
690  int last_index = ctx->m.dct_quantize(&ctx->m, block, 4 & (2*i),
691  qscale, &overflow);
692  // START_TIMER;
693  dnxhd_encode_block(ctx, block, last_index, n);
694  // STOP_TIMER("encode_block");
695  }
696  }
697  if (put_bits_count(&ctx->m.pb) & 31)
698  put_bits(&ctx->m.pb, 32 - (put_bits_count(&ctx->m.pb) & 31), 0);
699  flush_put_bits(&ctx->m.pb);
700  return 0;
701 }
702 
704 {
705  int mb_y, mb_x;
706  int offset = 0;
707  for (mb_y = 0; mb_y < ctx->m.mb_height; mb_y++) {
708  int thread_size;
709  ctx->slice_offs[mb_y] = offset;
710  ctx->slice_size[mb_y] = 0;
711  for (mb_x = 0; mb_x < ctx->m.mb_width; mb_x++) {
712  unsigned mb = mb_y * ctx->m.mb_width + mb_x;
713  ctx->slice_size[mb_y] += ctx->mb_bits[mb];
714  }
715  ctx->slice_size[mb_y] = (ctx->slice_size[mb_y] + 31) & ~31;
716  ctx->slice_size[mb_y] >>= 3;
717  thread_size = ctx->slice_size[mb_y];
718  offset += thread_size;
719  }
720 }
721 
723  int jobnr, int threadnr)
724 {
725  DNXHDEncContext *ctx = avctx->priv_data;
726  int mb_y = jobnr, mb_x, x, y;
727  int partial_last_row = (mb_y == ctx->m.mb_height - 1) &&
728  ((avctx->height >> ctx->interlaced) & 0xF);
729 
730  ctx = ctx->thread[threadnr];
731  if (ctx->cid_table->bit_depth == 8) {
732  uint8_t *pix = ctx->thread[0]->src[0] + ((mb_y << 4) * ctx->m.linesize);
733  for (mb_x = 0; mb_x < ctx->m.mb_width; ++mb_x, pix += 16) {
734  unsigned mb = mb_y * ctx->m.mb_width + mb_x;
735  int sum;
736  int varc;
737 
738  if (!partial_last_row && mb_x * 16 <= avctx->width - 16) {
739  sum = ctx->m.mpvencdsp.pix_sum(pix, ctx->m.linesize);
740  varc = ctx->m.mpvencdsp.pix_norm1(pix, ctx->m.linesize);
741  } else {
742  int bw = FFMIN(avctx->width - 16 * mb_x, 16);
743  int bh = FFMIN((avctx->height >> ctx->interlaced) - 16 * mb_y, 16);
744  sum = varc = 0;
745  for (y = 0; y < bh; y++) {
746  for (x = 0; x < bw; x++) {
747  uint8_t val = pix[x + y * ctx->m.linesize];
748  sum += val;
749  varc += val * val;
750  }
751  }
752  }
753  varc = (varc - (((unsigned) sum * sum) >> 8) + 128) >> 8;
754 
755  ctx->mb_cmp[mb].value = varc;
756  ctx->mb_cmp[mb].mb = mb;
757  }
758  } else { // 10-bit
759  int const linesize = ctx->m.linesize >> 1;
760  for (mb_x = 0; mb_x < ctx->m.mb_width; ++mb_x) {
761  uint16_t *pix = (uint16_t *)ctx->thread[0]->src[0] +
762  ((mb_y << 4) * linesize) + (mb_x << 4);
763  unsigned mb = mb_y * ctx->m.mb_width + mb_x;
764  int sum = 0;
765  int sqsum = 0;
766  int mean, sqmean;
767  int i, j;
768  // Macroblocks are 16x16 pixels, unlike DCT blocks which are 8x8.
769  for (i = 0; i < 16; ++i) {
770  for (j = 0; j < 16; ++j) {
771  // Turn 16-bit pixels into 10-bit ones.
772  int const sample = (unsigned) pix[j] >> 6;
773  sum += sample;
774  sqsum += sample * sample;
775  // 2^10 * 2^10 * 16 * 16 = 2^28, which is less than INT_MAX
776  }
777  pix += linesize;
778  }
779  mean = sum >> 8; // 16*16 == 2^8
780  sqmean = sqsum >> 8;
781  ctx->mb_cmp[mb].value = sqmean - mean * mean;
782  ctx->mb_cmp[mb].mb = mb;
783  }
784  }
785  return 0;
786 }
787 
789 {
790  int lambda, up_step, down_step;
791  int last_lower = INT_MAX, last_higher = 0;
792  int x, y, q;
793 
794  for (q = 1; q < avctx->qmax; q++) {
795  ctx->qscale = q;
796  avctx->execute2(avctx, dnxhd_calc_bits_thread,
797  NULL, NULL, ctx->m.mb_height);
798  }
799  up_step = down_step = 2 << LAMBDA_FRAC_BITS;
800  lambda = ctx->lambda;
801 
802  for (;;) {
803  int bits = 0;
804  int end = 0;
805  if (lambda == last_higher) {
806  lambda++;
807  end = 1; // need to set final qscales/bits
808  }
809  for (y = 0; y < ctx->m.mb_height; y++) {
810  for (x = 0; x < ctx->m.mb_width; x++) {
811  unsigned min = UINT_MAX;
812  int qscale = 1;
813  int mb = y * ctx->m.mb_width + x;
814  for (q = 1; q < avctx->qmax; q++) {
815  unsigned score = ctx->mb_rc[q][mb].bits * lambda +
816  ((unsigned) ctx->mb_rc[q][mb].ssd << LAMBDA_FRAC_BITS);
817  if (score < min) {
818  min = score;
819  qscale = q;
820  }
821  }
822  bits += ctx->mb_rc[qscale][mb].bits;
823  ctx->mb_qscale[mb] = qscale;
824  ctx->mb_bits[mb] = ctx->mb_rc[qscale][mb].bits;
825  }
826  bits = (bits + 31) & ~31; // padding
827  if (bits > ctx->frame_bits)
828  break;
829  }
830  // av_dlog(ctx->m.avctx,
831  // "lambda %d, up %u, down %u, bits %d, frame %d\n",
832  // lambda, last_higher, last_lower, bits, ctx->frame_bits);
833  if (end) {
834  if (bits > ctx->frame_bits)
835  return AVERROR(EINVAL);
836  break;
837  }
838  if (bits < ctx->frame_bits) {
839  last_lower = FFMIN(lambda, last_lower);
840  if (last_higher != 0)
841  lambda = (lambda+last_higher)>>1;
842  else
843  lambda -= down_step;
844  down_step = FFMIN((int64_t)down_step*5, INT_MAX);
845  up_step = 1<<LAMBDA_FRAC_BITS;
846  lambda = FFMAX(1, lambda);
847  if (lambda == last_lower)
848  break;
849  } else {
850  last_higher = FFMAX(lambda, last_higher);
851  if (last_lower != INT_MAX)
852  lambda = (lambda+last_lower)>>1;
853  else if ((int64_t)lambda + up_step > INT_MAX)
854  return AVERROR(EINVAL);
855  else
856  lambda += up_step;
857  up_step = FFMIN((int64_t)up_step*5, INT_MAX);
858  down_step = 1<<LAMBDA_FRAC_BITS;
859  }
860  }
861  //av_dlog(ctx->m.avctx, "out lambda %d\n", lambda);
862  ctx->lambda = lambda;
863  return 0;
864 }
865 
867 {
868  int bits = 0;
869  int up_step = 1;
870  int down_step = 1;
871  int last_higher = 0;
872  int last_lower = INT_MAX;
873  int qscale;
874  int x, y;
875 
876  qscale = ctx->qscale;
877  for (;;) {
878  bits = 0;
879  ctx->qscale = qscale;
880  // XXX avoid recalculating bits
882  NULL, NULL, ctx->m.mb_height);
883  for (y = 0; y < ctx->m.mb_height; y++) {
884  for (x = 0; x < ctx->m.mb_width; x++)
885  bits += ctx->mb_rc[qscale][y*ctx->m.mb_width+x].bits;
886  bits = (bits+31)&~31; // padding
887  if (bits > ctx->frame_bits)
888  break;
889  }
890  // av_dlog(ctx->m.avctx,
891  // "%d, qscale %d, bits %d, frame %d, higher %d, lower %d\n",
892  // ctx->m.avctx->frame_number, qscale, bits, ctx->frame_bits,
893  // last_higher, last_lower);
894  if (bits < ctx->frame_bits) {
895  if (qscale == 1)
896  return 1;
897  if (last_higher == qscale - 1) {
898  qscale = last_higher;
899  break;
900  }
901  last_lower = FFMIN(qscale, last_lower);
902  if (last_higher != 0)
903  qscale = (qscale + last_higher) >> 1;
904  else
905  qscale -= down_step++;
906  if (qscale < 1)
907  qscale = 1;
908  up_step = 1;
909  } else {
910  if (last_lower == qscale + 1)
911  break;
912  last_higher = FFMAX(qscale, last_higher);
913  if (last_lower != INT_MAX)
914  qscale = (qscale + last_lower) >> 1;
915  else
916  qscale += up_step++;
917  down_step = 1;
918  if (qscale >= ctx->m.avctx->qmax)
919  return AVERROR(EINVAL);
920  }
921  }
922  //av_dlog(ctx->m.avctx, "out qscale %d\n", qscale);
923  ctx->qscale = qscale;
924  return 0;
925 }
926 
927 #define BUCKET_BITS 8
928 #define RADIX_PASSES 4
929 #define NBUCKETS (1 << BUCKET_BITS)
930 
931 static inline int get_bucket(int value, int shift)
932 {
933  value >>= shift;
934  value &= NBUCKETS - 1;
935  return NBUCKETS - 1 - value;
936 }
937 
938 static void radix_count(const RCCMPEntry *data, int size,
939  int buckets[RADIX_PASSES][NBUCKETS])
940 {
941  int i, j;
942  memset(buckets, 0, sizeof(buckets[0][0]) * RADIX_PASSES * NBUCKETS);
943  for (i = 0; i < size; i++) {
944  int v = data[i].value;
945  for (j = 0; j < RADIX_PASSES; j++) {
946  buckets[j][get_bucket(v, 0)]++;
947  v >>= BUCKET_BITS;
948  }
949  av_assert1(!v);
950  }
951  for (j = 0; j < RADIX_PASSES; j++) {
952  int offset = size;
953  for (i = NBUCKETS - 1; i >= 0; i--)
954  buckets[j][i] = offset -= buckets[j][i];
955  av_assert1(!buckets[j][0]);
956  }
957 }
958 
959 static void radix_sort_pass(RCCMPEntry *dst, const RCCMPEntry *data,
960  int size, int buckets[NBUCKETS], int pass)
961 {
962  int shift = pass * BUCKET_BITS;
963  int i;
964  for (i = 0; i < size; i++) {
965  int v = get_bucket(data[i].value, shift);
966  int pos = buckets[v]++;
967  dst[pos] = data[i];
968  }
969 }
970 
971 static void radix_sort(RCCMPEntry *data, int size)
972 {
973  int buckets[RADIX_PASSES][NBUCKETS];
974  RCCMPEntry *tmp = av_malloc_array(size, sizeof(*tmp));
975  radix_count(data, size, buckets);
976  radix_sort_pass(tmp, data, size, buckets[0], 0);
977  radix_sort_pass(data, tmp, size, buckets[1], 1);
978  if (buckets[2][NBUCKETS - 1] || buckets[3][NBUCKETS - 1]) {
979  radix_sort_pass(tmp, data, size, buckets[2], 2);
980  radix_sort_pass(data, tmp, size, buckets[3], 3);
981  }
982  av_free(tmp);
983 }
984 
986 {
987  int max_bits = 0;
988  int ret, x, y;
989  if ((ret = dnxhd_find_qscale(ctx)) < 0)
990  return ret;
991  for (y = 0; y < ctx->m.mb_height; y++) {
992  for (x = 0; x < ctx->m.mb_width; x++) {
993  int mb = y * ctx->m.mb_width + x;
994  int delta_bits;
995  ctx->mb_qscale[mb] = ctx->qscale;
996  ctx->mb_bits[mb] = ctx->mb_rc[ctx->qscale][mb].bits;
997  max_bits += ctx->mb_rc[ctx->qscale][mb].bits;
998  if (!RC_VARIANCE) {
999  delta_bits = ctx->mb_rc[ctx->qscale][mb].bits -
1000  ctx->mb_rc[ctx->qscale + 1][mb].bits;
1001  ctx->mb_cmp[mb].mb = mb;
1002  ctx->mb_cmp[mb].value =
1003  delta_bits ? ((ctx->mb_rc[ctx->qscale][mb].ssd -
1004  ctx->mb_rc[ctx->qscale + 1][mb].ssd) * 100) /
1005  delta_bits
1006  : INT_MIN; // avoid increasing qscale
1007  }
1008  }
1009  max_bits += 31; // worst padding
1010  }
1011  if (!ret) {
1012  if (RC_VARIANCE)
1013  avctx->execute2(avctx, dnxhd_mb_var_thread,
1014  NULL, NULL, ctx->m.mb_height);
1015  radix_sort(ctx->mb_cmp, ctx->m.mb_num);
1016  for (x = 0; x < ctx->m.mb_num && max_bits > ctx->frame_bits; x++) {
1017  int mb = ctx->mb_cmp[x].mb;
1018  max_bits -= ctx->mb_rc[ctx->qscale][mb].bits -
1019  ctx->mb_rc[ctx->qscale + 1][mb].bits;
1020  ctx->mb_qscale[mb] = ctx->qscale + 1;
1021  ctx->mb_bits[mb] = ctx->mb_rc[ctx->qscale + 1][mb].bits;
1022  }
1023  }
1024  return 0;
1025 }
1026 
1028 {
1029  int i;
1030 
1031  for (i = 0; i < ctx->m.avctx->thread_count; i++) {
1032  ctx->thread[i]->m.linesize = frame->linesize[0] << ctx->interlaced;
1033  ctx->thread[i]->m.uvlinesize = frame->linesize[1] << ctx->interlaced;
1034  ctx->thread[i]->dct_y_offset = ctx->m.linesize *8;
1035  ctx->thread[i]->dct_uv_offset = ctx->m.uvlinesize*8;
1036  }
1037 
1039  ctx->cur_field = frame->interlaced_frame && !frame->top_field_first;
1040 }
1041 
1043  const AVFrame *frame, int *got_packet)
1044 {
1045  DNXHDEncContext *ctx = avctx->priv_data;
1046  int first_field = 1;
1047  int offset, i, ret;
1048  uint8_t *buf;
1049 
1050  if ((ret = ff_alloc_packet2(avctx, pkt, ctx->cid_table->frame_size)) < 0)
1051  return ret;
1052  buf = pkt->data;
1053 
1054  dnxhd_load_picture(ctx, frame);
1055 
1056 encode_coding_unit:
1057  for (i = 0; i < 3; i++) {
1058  ctx->src[i] = frame->data[i];
1059  if (ctx->interlaced && ctx->cur_field)
1060  ctx->src[i] += frame->linesize[i];
1061  }
1062 
1063  dnxhd_write_header(avctx, buf);
1064 
1065  if (avctx->mb_decision == FF_MB_DECISION_RD)
1066  ret = dnxhd_encode_rdo(avctx, ctx);
1067  else
1068  ret = dnxhd_encode_fast(avctx, ctx);
1069  if (ret < 0) {
1070  av_log(avctx, AV_LOG_ERROR,
1071  "picture could not fit ratecontrol constraints, increase qmax\n");
1072  return ret;
1073  }
1074 
1076 
1077  offset = 0;
1078  for (i = 0; i < ctx->m.mb_height; i++) {
1079  AV_WB32(ctx->msip + i * 4, offset);
1080  offset += ctx->slice_size[i];
1081  av_assert1(!(ctx->slice_size[i] & 3));
1082  }
1083 
1084  avctx->execute2(avctx, dnxhd_encode_thread, buf, NULL, ctx->m.mb_height);
1085 
1086  av_assert1(640 + offset + 4 <= ctx->cid_table->coding_unit_size);
1087  memset(buf + 640 + offset, 0,
1088  ctx->cid_table->coding_unit_size - 4 - offset - 640);
1089 
1090  AV_WB32(buf + ctx->cid_table->coding_unit_size - 4, 0x600DC0DE); // EOF
1091 
1092  if (ctx->interlaced && first_field) {
1093  first_field = 0;
1094  ctx->cur_field ^= 1;
1095  buf += ctx->cid_table->coding_unit_size;
1096  goto encode_coding_unit;
1097  }
1098 
1099  avctx->coded_frame->quality = ctx->qscale * FF_QP2LAMBDA;
1100 
1101  pkt->flags |= AV_PKT_FLAG_KEY;
1102  *got_packet = 1;
1103  return 0;
1104 }
1105 
1107 {
1108  DNXHDEncContext *ctx = avctx->priv_data;
1109  int max_level = 1 << (ctx->cid_table->bit_depth + 2);
1110  int i;
1111 
1112  av_free(ctx->vlc_codes - max_level * 2);
1113  av_free(ctx->vlc_bits - max_level * 2);
1114  av_freep(&ctx->run_codes);
1115  av_freep(&ctx->run_bits);
1116 
1117  av_freep(&ctx->mb_bits);
1118  av_freep(&ctx->mb_qscale);
1119  av_freep(&ctx->mb_rc);
1120  av_freep(&ctx->mb_cmp);
1121  av_freep(&ctx->slice_size);
1122  av_freep(&ctx->slice_offs);
1123 
1124  av_freep(&ctx->qmatrix_c);
1125  av_freep(&ctx->qmatrix_l);
1126  av_freep(&ctx->qmatrix_c16);
1127  av_freep(&ctx->qmatrix_l16);
1128 
1129  for (i = 1; i < avctx->thread_count; i++)
1130  av_freep(&ctx->thread[i]);
1131 
1132  av_frame_free(&avctx->coded_frame);
1133 
1134  return 0;
1135 }
1136 
1137 static const AVCodecDefault dnxhd_defaults[] = {
1138  { "qmax", "1024" }, /* Maximum quantization scale factor allowed for VC-3 */
1139  { NULL },
1140 };
1141 
1143  .name = "dnxhd",
1144  .long_name = NULL_IF_CONFIG_SMALL("VC3/DNxHD"),
1145  .type = AVMEDIA_TYPE_VIDEO,
1146  .id = AV_CODEC_ID_DNXHD,
1147  .priv_data_size = sizeof(DNXHDEncContext),
1149  .encode2 = dnxhd_encode_picture,
1151  .capabilities = CODEC_CAP_SLICE_THREADS,
1152  .pix_fmts = (const enum AVPixelFormat[]) {
1156  },
1157  .priv_class = &dnxhd_class,
1158  .defaults = dnxhd_defaults,
1159 };