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