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