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