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