FFmpeg
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/mem_internal.h"
29 #include "libavutil/opt.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 "packet_internal.h"
38 #include "profiles.h"
39 #include "dnxhdenc.h"
40 
41 // The largest value that will not lead to overflow for 10-bit samples.
42 #define DNX10BIT_QMAT_SHIFT 18
43 #define RC_VARIANCE 1 // use variance or ssd for fast rc
44 #define LAMBDA_FRAC_BITS 10
45 
46 #define VE AV_OPT_FLAG_VIDEO_PARAM | AV_OPT_FLAG_ENCODING_PARAM
47 static const AVOption options[] = {
48  { "nitris_compat", "encode with Avid Nitris compatibility",
49  offsetof(DNXHDEncContext, nitris_compat), AV_OPT_TYPE_BOOL, { .i64 = 0 }, 0, 1, VE },
50  { "ibias", "intra quant bias",
51  offsetof(DNXHDEncContext, intra_quant_bias), AV_OPT_TYPE_INT,
52  { .i64 = 0 }, INT_MIN, INT_MAX, VE },
53  { "profile", NULL, offsetof(DNXHDEncContext, profile), AV_OPT_TYPE_INT,
54  { .i64 = FF_PROFILE_DNXHD },
56  { "dnxhd", NULL, 0, AV_OPT_TYPE_CONST, { .i64 = FF_PROFILE_DNXHD },
57  0, 0, VE, "profile" },
58  { "dnxhr_444", NULL, 0, AV_OPT_TYPE_CONST, { .i64 = FF_PROFILE_DNXHR_444 },
59  0, 0, VE, "profile" },
60  { "dnxhr_hqx", NULL, 0, AV_OPT_TYPE_CONST, { .i64 = FF_PROFILE_DNXHR_HQX },
61  0, 0, VE, "profile" },
62  { "dnxhr_hq", NULL, 0, AV_OPT_TYPE_CONST, { .i64 = FF_PROFILE_DNXHR_HQ },
63  0, 0, VE, "profile" },
64  { "dnxhr_sq", NULL, 0, AV_OPT_TYPE_CONST, { .i64 = FF_PROFILE_DNXHR_SQ },
65  0, 0, VE, "profile" },
66  { "dnxhr_lb", NULL, 0, AV_OPT_TYPE_CONST, { .i64 = FF_PROFILE_DNXHR_LB },
67  0, 0, VE, "profile" },
68  { NULL }
69 };
70 
71 static const AVClass dnxhd_class = {
72  .class_name = "dnxhd",
73  .item_name = av_default_item_name,
74  .option = options,
75  .version = LIBAVUTIL_VERSION_INT,
76 };
77 
78 static void dnxhd_8bit_get_pixels_8x4_sym(int16_t *av_restrict block,
79  const uint8_t *pixels,
80  ptrdiff_t line_size)
81 {
82  int i;
83  for (i = 0; i < 4; i++) {
84  block[0] = pixels[0];
85  block[1] = pixels[1];
86  block[2] = pixels[2];
87  block[3] = pixels[3];
88  block[4] = pixels[4];
89  block[5] = pixels[5];
90  block[6] = pixels[6];
91  block[7] = pixels[7];
92  pixels += line_size;
93  block += 8;
94  }
95  memcpy(block, block - 8, sizeof(*block) * 8);
96  memcpy(block + 8, block - 16, sizeof(*block) * 8);
97  memcpy(block + 16, block - 24, sizeof(*block) * 8);
98  memcpy(block + 24, block - 32, sizeof(*block) * 8);
99 }
100 
101 static av_always_inline
102 void dnxhd_10bit_get_pixels_8x4_sym(int16_t *av_restrict block,
103  const uint8_t *pixels,
104  ptrdiff_t line_size)
105 {
106  memcpy(block + 0 * 8, pixels + 0 * line_size, 8 * sizeof(*block));
107  memcpy(block + 7 * 8, pixels + 0 * line_size, 8 * sizeof(*block));
108  memcpy(block + 1 * 8, pixels + 1 * line_size, 8 * sizeof(*block));
109  memcpy(block + 6 * 8, pixels + 1 * line_size, 8 * sizeof(*block));
110  memcpy(block + 2 * 8, pixels + 2 * line_size, 8 * sizeof(*block));
111  memcpy(block + 5 * 8, pixels + 2 * line_size, 8 * sizeof(*block));
112  memcpy(block + 3 * 8, pixels + 3 * line_size, 8 * sizeof(*block));
113  memcpy(block + 4 * 8, pixels + 3 * line_size, 8 * sizeof(*block));
114 }
115 
117  int n, int qscale, int *overflow)
118 {
119  int i, j, level, last_non_zero, start_i;
120  const int *qmat;
122  int bias;
123  int max = 0;
124  unsigned int threshold1, threshold2;
125 
126  ctx->fdsp.fdct(block);
127 
128  block[0] = (block[0] + 2) >> 2;
129  start_i = 1;
130  last_non_zero = 0;
131  qmat = n < 4 ? ctx->q_intra_matrix[qscale] : ctx->q_chroma_intra_matrix[qscale];
132  bias= ctx->intra_quant_bias * (1 << (16 - 8));
133  threshold1 = (1 << 16) - bias - 1;
134  threshold2 = (threshold1 << 1);
135 
136  for (i = 63; i >= start_i; i--) {
137  j = scantable[i];
138  level = block[j] * qmat[j];
139 
140  if (((unsigned)(level + threshold1)) > threshold2) {
141  last_non_zero = i;
142  break;
143  } else{
144  block[j]=0;
145  }
146  }
147 
148  for (i = start_i; i <= last_non_zero; i++) {
149  j = scantable[i];
150  level = block[j] * qmat[j];
151 
152  if (((unsigned)(level + threshold1)) > threshold2) {
153  if (level > 0) {
154  level = (bias + level) >> 16;
155  block[j] = level;
156  } else{
157  level = (bias - level) >> 16;
158  block[j] = -level;
159  }
160  max |= level;
161  } else {
162  block[j] = 0;
163  }
164  }
165  *overflow = ctx->max_qcoeff < max; //overflow might have happened
166 
167  /* we need this permutation so that we correct the IDCT, we only permute the !=0 elements */
168  if (ctx->idsp.perm_type != FF_IDCT_PERM_NONE)
170  scantable, last_non_zero);
171 
172  return last_non_zero;
173 }
174 
176  int n, int qscale, int *overflow)
177 {
179  const int *qmat = n<4 ? ctx->q_intra_matrix[qscale] : ctx->q_chroma_intra_matrix[qscale];
180  int last_non_zero = 0;
181  int i;
182 
183  ctx->fdsp.fdct(block);
184 
185  // Divide by 4 with rounding, to compensate scaling of DCT coefficients
186  block[0] = (block[0] + 2) >> 2;
187 
188  for (i = 1; i < 64; ++i) {
189  int j = scantable[i];
190  int sign = FF_SIGNBIT(block[j]);
191  int level = (block[j] ^ sign) - sign;
192  level = level * qmat[j] >> DNX10BIT_QMAT_SHIFT;
193  block[j] = (level ^ sign) - sign;
194  if (level)
195  last_non_zero = i;
196  }
197 
198  /* we need this permutation so that we correct the IDCT, we only permute the !=0 elements */
199  if (ctx->idsp.perm_type != FF_IDCT_PERM_NONE)
201  scantable, last_non_zero);
202 
203  return last_non_zero;
204 }
205 
207 {
208  int i, j, level, run;
209  int max_level = 1 << (ctx->bit_depth + 2);
210 
211  if (!FF_ALLOCZ_TYPED_ARRAY(ctx->orig_vlc_codes, max_level * 4) ||
212  !FF_ALLOCZ_TYPED_ARRAY(ctx->orig_vlc_bits, max_level * 4) ||
213  !(ctx->run_codes = av_mallocz(63 * 2)) ||
214  !(ctx->run_bits = av_mallocz(63)))
215  return AVERROR(ENOMEM);
216  ctx->vlc_codes = ctx->orig_vlc_codes + max_level * 2;
217  ctx->vlc_bits = ctx->orig_vlc_bits + max_level * 2;
218  for (level = -max_level; level < max_level; level++) {
219  for (run = 0; run < 2; run++) {
220  int index = level * (1 << 1) | run;
221  int sign, offset = 0, alevel = level;
222 
223  MASK_ABS(sign, alevel);
224  if (alevel > 64) {
225  offset = (alevel - 1) >> 6;
226  alevel -= offset << 6;
227  }
228  for (j = 0; j < 257; j++) {
229  if (ctx->cid_table->ac_info[2*j+0] >> 1 == alevel &&
230  (!offset || (ctx->cid_table->ac_info[2*j+1] & 1) && offset) &&
231  (!run || (ctx->cid_table->ac_info[2*j+1] & 2) && run)) {
232  av_assert1(!ctx->vlc_codes[index]);
233  if (alevel) {
234  ctx->vlc_codes[index] =
235  (ctx->cid_table->ac_codes[j] << 1) | (sign & 1);
236  ctx->vlc_bits[index] = ctx->cid_table->ac_bits[j] + 1;
237  } else {
238  ctx->vlc_codes[index] = ctx->cid_table->ac_codes[j];
239  ctx->vlc_bits[index] = ctx->cid_table->ac_bits[j];
240  }
241  break;
242  }
243  }
244  av_assert0(!alevel || j < 257);
245  if (offset) {
246  ctx->vlc_codes[index] =
247  (ctx->vlc_codes[index] << ctx->cid_table->index_bits) | offset;
248  ctx->vlc_bits[index] += ctx->cid_table->index_bits;
249  }
250  }
251  }
252  for (i = 0; i < 62; i++) {
253  int run = ctx->cid_table->run[i];
254  av_assert0(run < 63);
255  ctx->run_codes[run] = ctx->cid_table->run_codes[i];
256  ctx->run_bits[run] = ctx->cid_table->run_bits[i];
257  }
258  return 0;
259 }
260 
261 static av_cold int dnxhd_init_qmat(DNXHDEncContext *ctx, int lbias, int cbias)
262 {
263  // init first elem to 1 to avoid div by 0 in convert_matrix
264  uint16_t weight_matrix[64] = { 1, }; // convert_matrix needs uint16_t*
265  int qscale, i;
266  const uint8_t *luma_weight_table = ctx->cid_table->luma_weight;
267  const uint8_t *chroma_weight_table = ctx->cid_table->chroma_weight;
268 
269  if (!FF_ALLOCZ_TYPED_ARRAY(ctx->qmatrix_l, ctx->m.avctx->qmax + 1) ||
270  !FF_ALLOCZ_TYPED_ARRAY(ctx->qmatrix_c, ctx->m.avctx->qmax + 1) ||
271  !FF_ALLOCZ_TYPED_ARRAY(ctx->qmatrix_l16, ctx->m.avctx->qmax + 1) ||
272  !FF_ALLOCZ_TYPED_ARRAY(ctx->qmatrix_c16, ctx->m.avctx->qmax + 1))
273  return AVERROR(ENOMEM);
274 
275  if (ctx->bit_depth == 8) {
276  for (i = 1; i < 64; i++) {
277  int j = ctx->m.idsp.idct_permutation[ff_zigzag_direct[i]];
278  weight_matrix[j] = ctx->cid_table->luma_weight[i];
279  }
280  ff_convert_matrix(&ctx->m, ctx->qmatrix_l, ctx->qmatrix_l16,
281  weight_matrix, ctx->intra_quant_bias, 1,
282  ctx->m.avctx->qmax, 1);
283  for (i = 1; i < 64; i++) {
284  int j = ctx->m.idsp.idct_permutation[ff_zigzag_direct[i]];
285  weight_matrix[j] = ctx->cid_table->chroma_weight[i];
286  }
287  ff_convert_matrix(&ctx->m, ctx->qmatrix_c, ctx->qmatrix_c16,
288  weight_matrix, ctx->intra_quant_bias, 1,
289  ctx->m.avctx->qmax, 1);
290 
291  for (qscale = 1; qscale <= ctx->m.avctx->qmax; qscale++) {
292  for (i = 0; i < 64; i++) {
293  ctx->qmatrix_l[qscale][i] <<= 2;
294  ctx->qmatrix_c[qscale][i] <<= 2;
295  ctx->qmatrix_l16[qscale][0][i] <<= 2;
296  ctx->qmatrix_l16[qscale][1][i] <<= 2;
297  ctx->qmatrix_c16[qscale][0][i] <<= 2;
298  ctx->qmatrix_c16[qscale][1][i] <<= 2;
299  }
300  }
301  } else {
302  // 10-bit
303  for (qscale = 1; qscale <= ctx->m.avctx->qmax; qscale++) {
304  for (i = 1; i < 64; i++) {
305  int j = ff_zigzag_direct[i];
306 
307  /* The quantization formula from the VC-3 standard is:
308  * quantized = sign(block[i]) * floor(abs(block[i]/s) * p /
309  * (qscale * weight_table[i]))
310  * Where p is 32 for 8-bit samples and 8 for 10-bit ones.
311  * The s factor compensates scaling of DCT coefficients done by
312  * the DCT routines, and therefore is not present in standard.
313  * It's 8 for 8-bit samples and 4 for 10-bit ones.
314  * We want values of ctx->qtmatrix_l and ctx->qtmatrix_r to be:
315  * ((1 << DNX10BIT_QMAT_SHIFT) * (p / s)) /
316  * (qscale * weight_table[i])
317  * For 10-bit samples, p / s == 2 */
318  ctx->qmatrix_l[qscale][j] = (1 << (DNX10BIT_QMAT_SHIFT + 1)) /
319  (qscale * luma_weight_table[i]);
320  ctx->qmatrix_c[qscale][j] = (1 << (DNX10BIT_QMAT_SHIFT + 1)) /
321  (qscale * chroma_weight_table[i]);
322  }
323  }
324  }
325 
327  ctx->m.q_chroma_intra_matrix = ctx->qmatrix_c;
328  ctx->m.q_intra_matrix16 = ctx->qmatrix_l16;
329  ctx->m.q_intra_matrix = ctx->qmatrix_l;
330 
331  return 0;
332 }
333 
335 {
336  if (!FF_ALLOCZ_TYPED_ARRAY(ctx->mb_rc, (ctx->m.avctx->qmax + 1) * ctx->m.mb_num))
337  return AVERROR(ENOMEM);
338 
339  if (ctx->m.avctx->mb_decision != FF_MB_DECISION_RD) {
340  if (!FF_ALLOCZ_TYPED_ARRAY(ctx->mb_cmp, ctx->m.mb_num) ||
342  return AVERROR(ENOMEM);
343  }
344  ctx->frame_bits = (ctx->coding_unit_size -
345  ctx->data_offset - 4 - ctx->min_padding) * 8;
346  ctx->qscale = 1;
347  ctx->lambda = 2 << LAMBDA_FRAC_BITS; // qscale 2
348  return 0;
349 }
350 
352 {
353  DNXHDEncContext *ctx = avctx->priv_data;
354  int i, index, ret;
355 
356  switch (avctx->pix_fmt) {
357  case AV_PIX_FMT_YUV422P:
358  ctx->bit_depth = 8;
359  break;
362  case AV_PIX_FMT_GBRP10:
363  ctx->bit_depth = 10;
364  break;
365  default:
366  av_log(avctx, AV_LOG_ERROR,
367  "pixel format is incompatible with DNxHD\n");
368  return AVERROR(EINVAL);
369  }
370 
371  if ((ctx->profile == FF_PROFILE_DNXHR_444 && (avctx->pix_fmt != AV_PIX_FMT_YUV444P10 &&
372  avctx->pix_fmt != AV_PIX_FMT_GBRP10)) ||
373  (ctx->profile != FF_PROFILE_DNXHR_444 && (avctx->pix_fmt == AV_PIX_FMT_YUV444P10 ||
374  avctx->pix_fmt == AV_PIX_FMT_GBRP10))) {
375  av_log(avctx, AV_LOG_ERROR,
376  "pixel format is incompatible with DNxHD profile\n");
377  return AVERROR(EINVAL);
378  }
379 
380  if (ctx->profile == FF_PROFILE_DNXHR_HQX && avctx->pix_fmt != AV_PIX_FMT_YUV422P10) {
381  av_log(avctx, AV_LOG_ERROR,
382  "pixel format is incompatible with DNxHR HQX profile\n");
383  return AVERROR(EINVAL);
384  }
385 
386  if ((ctx->profile == FF_PROFILE_DNXHR_LB ||
387  ctx->profile == FF_PROFILE_DNXHR_SQ ||
388  ctx->profile == FF_PROFILE_DNXHR_HQ) && avctx->pix_fmt != AV_PIX_FMT_YUV422P) {
389  av_log(avctx, AV_LOG_ERROR,
390  "pixel format is incompatible with DNxHR LB/SQ/HQ profile\n");
391  return AVERROR(EINVAL);
392  }
393 
394  ctx->is_444 = ctx->profile == FF_PROFILE_DNXHR_444;
395  avctx->profile = ctx->profile;
396  ctx->cid = ff_dnxhd_find_cid(avctx, ctx->bit_depth);
397  if (!ctx->cid) {
398  av_log(avctx, AV_LOG_ERROR,
399  "video parameters incompatible with DNxHD. Valid DNxHD profiles:\n");
401  return AVERROR(EINVAL);
402  }
403  av_log(avctx, AV_LOG_DEBUG, "cid %d\n", ctx->cid);
404 
405  if (ctx->cid >= 1270 && ctx->cid <= 1274)
406  avctx->codec_tag = MKTAG('A','V','d','h');
407 
408  if (avctx->width < 256 || avctx->height < 120) {
409  av_log(avctx, AV_LOG_ERROR,
410  "Input dimensions too small, input must be at least 256x120\n");
411  return AVERROR(EINVAL);
412  }
413 
414  index = ff_dnxhd_get_cid_table(ctx->cid);
415  av_assert0(index >= 0);
416 
418 
419  ctx->m.avctx = avctx;
420  ctx->m.mb_intra = 1;
421  ctx->m.h263_aic = 1;
422 
423  avctx->bits_per_raw_sample = ctx->bit_depth;
424 
425  ff_blockdsp_init(&ctx->bdsp, avctx);
426  ff_fdctdsp_init(&ctx->m.fdsp, avctx);
427  ff_mpv_idct_init(&ctx->m);
428  ff_mpegvideoencdsp_init(&ctx->m.mpvencdsp, avctx);
429  ff_pixblockdsp_init(&ctx->m.pdsp, avctx);
430  ff_dct_encode_init(&ctx->m);
431 
432  if (ctx->profile != FF_PROFILE_DNXHD)
433  ff_videodsp_init(&ctx->m.vdsp, ctx->bit_depth);
434 
435  if (!ctx->m.dct_quantize)
437 
438  if (ctx->is_444 || ctx->profile == FF_PROFILE_DNXHR_HQX) {
441  ctx->block_width_l2 = 4;
442  } else if (ctx->bit_depth == 10) {
445  ctx->block_width_l2 = 4;
446  } else {
448  ctx->block_width_l2 = 3;
449  }
450 
451  if (ARCH_X86)
453 
454  ctx->m.mb_height = (avctx->height + 15) / 16;
455  ctx->m.mb_width = (avctx->width + 15) / 16;
456 
457  if (avctx->flags & AV_CODEC_FLAG_INTERLACED_DCT) {
458  ctx->interlaced = 1;
459  ctx->m.mb_height /= 2;
460  }
461 
462  if (ctx->interlaced && ctx->profile != FF_PROFILE_DNXHD) {
463  av_log(avctx, AV_LOG_ERROR,
464  "Interlaced encoding is not supported for DNxHR profiles.\n");
465  return AVERROR(EINVAL);
466  }
467 
468  ctx->m.mb_num = ctx->m.mb_height * ctx->m.mb_width;
469 
470  if (ctx->cid_table->frame_size == DNXHD_VARIABLE) {
472  avctx->width, avctx->height);
473  av_assert0(ctx->frame_size >= 0);
474  ctx->coding_unit_size = ctx->frame_size;
475  } else {
476  ctx->frame_size = ctx->cid_table->frame_size;
478  }
479 
480  if (ctx->m.mb_height > 68)
481  ctx->data_offset = 0x170 + (ctx->m.mb_height << 2);
482  else
483  ctx->data_offset = 0x280;
484 
485  // XXX tune lbias/cbias
486  if ((ret = dnxhd_init_qmat(ctx, ctx->intra_quant_bias, 0)) < 0)
487  return ret;
488 
489  /* Avid Nitris hardware decoder requires a minimum amount of padding
490  * in the coding unit payload */
491  if (ctx->nitris_compat)
492  ctx->min_padding = 1600;
493 
494  if ((ret = dnxhd_init_vlc(ctx)) < 0)
495  return ret;
496  if ((ret = dnxhd_init_rc(ctx)) < 0)
497  return ret;
498 
499  if (!FF_ALLOCZ_TYPED_ARRAY(ctx->slice_size, ctx->m.mb_height) ||
501  !FF_ALLOCZ_TYPED_ARRAY(ctx->mb_bits, ctx->m.mb_num) ||
503  return AVERROR(ENOMEM);
504 #if FF_API_CODED_FRAME
506  avctx->coded_frame->key_frame = 1;
509 #endif
510 
511  if (avctx->active_thread_type == FF_THREAD_SLICE) {
512  if (avctx->thread_count > MAX_THREADS) {
513  av_log(avctx, AV_LOG_ERROR, "too many threads\n");
514  return AVERROR(EINVAL);
515  }
516  }
517 
518  if (avctx->qmax <= 1) {
519  av_log(avctx, AV_LOG_ERROR, "qmax must be at least 2\n");
520  return AVERROR(EINVAL);
521  }
522 
523  ctx->thread[0] = ctx;
524  if (avctx->active_thread_type == FF_THREAD_SLICE) {
525  for (i = 1; i < avctx->thread_count; i++) {
526  ctx->thread[i] = av_malloc(sizeof(DNXHDEncContext));
527  if (!ctx->thread[i])
528  return AVERROR(ENOMEM);
529  memcpy(ctx->thread[i], ctx, sizeof(DNXHDEncContext));
530  }
531  }
532 
533  return 0;
534 }
535 
537 {
538  DNXHDEncContext *ctx = avctx->priv_data;
539 
540  memset(buf, 0, ctx->data_offset);
541 
542  // * write prefix */
543  AV_WB16(buf + 0x02, ctx->data_offset);
544  if (ctx->cid >= 1270 && ctx->cid <= 1274)
545  buf[4] = 0x03;
546  else
547  buf[4] = 0x01;
548 
549  buf[5] = ctx->interlaced ? ctx->cur_field + 2 : 0x01;
550  buf[6] = 0x80; // crc flag off
551  buf[7] = 0xa0; // reserved
552  AV_WB16(buf + 0x18, avctx->height >> ctx->interlaced); // ALPF
553  AV_WB16(buf + 0x1a, avctx->width); // SPL
554  AV_WB16(buf + 0x1d, avctx->height >> ctx->interlaced); // NAL
555 
556  buf[0x21] = ctx->bit_depth == 10 ? 0x58 : 0x38;
557  buf[0x22] = 0x88 + (ctx->interlaced << 2);
558  AV_WB32(buf + 0x28, ctx->cid); // CID
559  buf[0x2c] = (!ctx->interlaced << 7) | (ctx->is_444 << 6) | (avctx->pix_fmt == AV_PIX_FMT_YUV444P10);
560 
561  buf[0x5f] = 0x01; // UDL
562 
563  buf[0x167] = 0x02; // reserved
564  AV_WB16(buf + 0x16a, ctx->m.mb_height * 4 + 4); // MSIPS
565  AV_WB16(buf + 0x16c, ctx->m.mb_height); // Ns
566  buf[0x16f] = 0x10; // reserved
567 
568  ctx->msip = buf + 0x170;
569  return 0;
570 }
571 
573 {
574  int nbits;
575  if (diff < 0) {
576  nbits = av_log2_16bit(-2 * diff);
577  diff--;
578  } else {
579  nbits = av_log2_16bit(2 * diff);
580  }
581  put_bits(&ctx->m.pb, ctx->cid_table->dc_bits[nbits] + nbits,
582  (ctx->cid_table->dc_codes[nbits] << nbits) +
583  av_mod_uintp2(diff, nbits));
584 }
585 
586 static av_always_inline
588  int last_index, int n)
589 {
590  int last_non_zero = 0;
591  int slevel, i, j;
592 
593  dnxhd_encode_dc(ctx, block[0] - ctx->m.last_dc[n]);
594  ctx->m.last_dc[n] = block[0];
595 
596  for (i = 1; i <= last_index; i++) {
597  j = ctx->m.intra_scantable.permutated[i];
598  slevel = block[j];
599  if (slevel) {
600  int run_level = i - last_non_zero - 1;
601  int rlevel = slevel * (1 << 1) | !!run_level;
602  put_bits(&ctx->m.pb, ctx->vlc_bits[rlevel], ctx->vlc_codes[rlevel]);
603  if (run_level)
604  put_bits(&ctx->m.pb, ctx->run_bits[run_level],
605  ctx->run_codes[run_level]);
606  last_non_zero = i;
607  }
608  }
609  put_bits(&ctx->m.pb, ctx->vlc_bits[0], ctx->vlc_codes[0]); // EOB
610 }
611 
612 static av_always_inline
614  int qscale, int last_index)
615 {
616  const uint8_t *weight_matrix;
617  int level;
618  int i;
619 
620  if (ctx->is_444) {
621  weight_matrix = ((n % 6) < 2) ? ctx->cid_table->luma_weight
622  : ctx->cid_table->chroma_weight;
623  } else {
624  weight_matrix = (n & 2) ? ctx->cid_table->chroma_weight
625  : ctx->cid_table->luma_weight;
626  }
627 
628  for (i = 1; i <= last_index; i++) {
629  int j = ctx->m.intra_scantable.permutated[i];
630  level = block[j];
631  if (level) {
632  if (level < 0) {
633  level = (1 - 2 * level) * qscale * weight_matrix[i];
634  if (ctx->bit_depth == 10) {
635  if (weight_matrix[i] != 8)
636  level += 8;
637  level >>= 4;
638  } else {
639  if (weight_matrix[i] != 32)
640  level += 32;
641  level >>= 6;
642  }
643  level = -level;
644  } else {
645  level = (2 * level + 1) * qscale * weight_matrix[i];
646  if (ctx->bit_depth == 10) {
647  if (weight_matrix[i] != 8)
648  level += 8;
649  level >>= 4;
650  } else {
651  if (weight_matrix[i] != 32)
652  level += 32;
653  level >>= 6;
654  }
655  }
656  block[j] = level;
657  }
658  }
659 }
660 
661 static av_always_inline int dnxhd_ssd_block(int16_t *qblock, int16_t *block)
662 {
663  int score = 0;
664  int i;
665  for (i = 0; i < 64; i++)
666  score += (block[i] - qblock[i]) * (block[i] - qblock[i]);
667  return score;
668 }
669 
670 static av_always_inline
671 int dnxhd_calc_ac_bits(DNXHDEncContext *ctx, int16_t *block, int last_index)
672 {
673  int last_non_zero = 0;
674  int bits = 0;
675  int i, j, level;
676  for (i = 1; i <= last_index; i++) {
677  j = ctx->m.intra_scantable.permutated[i];
678  level = block[j];
679  if (level) {
680  int run_level = i - last_non_zero - 1;
681  bits += ctx->vlc_bits[level * (1 << 1) |
682  !!run_level] + ctx->run_bits[run_level];
683  last_non_zero = i;
684  }
685  }
686  return bits;
687 }
688 
689 static av_always_inline
690 void dnxhd_get_blocks(DNXHDEncContext *ctx, int mb_x, int mb_y)
691 {
692  const int bs = ctx->block_width_l2;
693  const int bw = 1 << bs;
694  int dct_y_offset = ctx->dct_y_offset;
695  int dct_uv_offset = ctx->dct_uv_offset;
696  int linesize = ctx->m.linesize;
697  int uvlinesize = ctx->m.uvlinesize;
698  const uint8_t *ptr_y = ctx->thread[0]->src[0] +
699  ((mb_y << 4) * ctx->m.linesize) + (mb_x << bs + 1);
700  const uint8_t *ptr_u = ctx->thread[0]->src[1] +
701  ((mb_y << 4) * ctx->m.uvlinesize) + (mb_x << bs + ctx->is_444);
702  const uint8_t *ptr_v = ctx->thread[0]->src[2] +
703  ((mb_y << 4) * ctx->m.uvlinesize) + (mb_x << bs + ctx->is_444);
704  PixblockDSPContext *pdsp = &ctx->m.pdsp;
705  VideoDSPContext *vdsp = &ctx->m.vdsp;
706 
707  if (ctx->bit_depth != 10 && vdsp->emulated_edge_mc && ((mb_x << 4) + 16 > ctx->m.avctx->width ||
708  (mb_y << 4) + 16 > ctx->m.avctx->height)) {
709  int y_w = ctx->m.avctx->width - (mb_x << 4);
710  int y_h = ctx->m.avctx->height - (mb_y << 4);
711  int uv_w = (y_w + 1) / 2;
712  int uv_h = y_h;
713  linesize = 16;
714  uvlinesize = 8;
715 
716  vdsp->emulated_edge_mc(&ctx->edge_buf_y[0], ptr_y,
717  linesize, ctx->m.linesize,
718  linesize, 16,
719  0, 0, y_w, y_h);
720  vdsp->emulated_edge_mc(&ctx->edge_buf_uv[0][0], ptr_u,
721  uvlinesize, ctx->m.uvlinesize,
722  uvlinesize, 16,
723  0, 0, uv_w, uv_h);
724  vdsp->emulated_edge_mc(&ctx->edge_buf_uv[1][0], ptr_v,
725  uvlinesize, ctx->m.uvlinesize,
726  uvlinesize, 16,
727  0, 0, uv_w, uv_h);
728 
729  dct_y_offset = bw * linesize;
730  dct_uv_offset = bw * uvlinesize;
731  ptr_y = &ctx->edge_buf_y[0];
732  ptr_u = &ctx->edge_buf_uv[0][0];
733  ptr_v = &ctx->edge_buf_uv[1][0];
734  } else if (ctx->bit_depth == 10 && vdsp->emulated_edge_mc && ((mb_x << 4) + 16 > ctx->m.avctx->width ||
735  (mb_y << 4) + 16 > ctx->m.avctx->height)) {
736  int y_w = ctx->m.avctx->width - (mb_x << 4);
737  int y_h = ctx->m.avctx->height - (mb_y << 4);
738  int uv_w = ctx->is_444 ? y_w : (y_w + 1) / 2;
739  int uv_h = y_h;
740  linesize = 32;
741  uvlinesize = 16 + 16 * ctx->is_444;
742 
743  vdsp->emulated_edge_mc(&ctx->edge_buf_y[0], ptr_y,
744  linesize, ctx->m.linesize,
745  linesize / 2, 16,
746  0, 0, y_w, y_h);
747  vdsp->emulated_edge_mc(&ctx->edge_buf_uv[0][0], ptr_u,
748  uvlinesize, ctx->m.uvlinesize,
749  uvlinesize / 2, 16,
750  0, 0, uv_w, uv_h);
751  vdsp->emulated_edge_mc(&ctx->edge_buf_uv[1][0], ptr_v,
752  uvlinesize, ctx->m.uvlinesize,
753  uvlinesize / 2, 16,
754  0, 0, uv_w, uv_h);
755 
756  dct_y_offset = bw * linesize / 2;
757  dct_uv_offset = bw * uvlinesize / 2;
758  ptr_y = &ctx->edge_buf_y[0];
759  ptr_u = &ctx->edge_buf_uv[0][0];
760  ptr_v = &ctx->edge_buf_uv[1][0];
761  }
762 
763  if (!ctx->is_444) {
764  pdsp->get_pixels(ctx->blocks[0], ptr_y, linesize);
765  pdsp->get_pixels(ctx->blocks[1], ptr_y + bw, linesize);
766  pdsp->get_pixels(ctx->blocks[2], ptr_u, uvlinesize);
767  pdsp->get_pixels(ctx->blocks[3], ptr_v, uvlinesize);
768 
769  if (mb_y + 1 == ctx->m.mb_height && ctx->m.avctx->height == 1080) {
770  if (ctx->interlaced) {
771  ctx->get_pixels_8x4_sym(ctx->blocks[4],
772  ptr_y + dct_y_offset,
773  linesize);
774  ctx->get_pixels_8x4_sym(ctx->blocks[5],
775  ptr_y + dct_y_offset + bw,
776  linesize);
777  ctx->get_pixels_8x4_sym(ctx->blocks[6],
778  ptr_u + dct_uv_offset,
779  uvlinesize);
780  ctx->get_pixels_8x4_sym(ctx->blocks[7],
781  ptr_v + dct_uv_offset,
782  uvlinesize);
783  } else {
784  ctx->bdsp.clear_block(ctx->blocks[4]);
785  ctx->bdsp.clear_block(ctx->blocks[5]);
786  ctx->bdsp.clear_block(ctx->blocks[6]);
787  ctx->bdsp.clear_block(ctx->blocks[7]);
788  }
789  } else {
790  pdsp->get_pixels(ctx->blocks[4],
791  ptr_y + dct_y_offset, linesize);
792  pdsp->get_pixels(ctx->blocks[5],
793  ptr_y + dct_y_offset + bw, linesize);
794  pdsp->get_pixels(ctx->blocks[6],
795  ptr_u + dct_uv_offset, uvlinesize);
796  pdsp->get_pixels(ctx->blocks[7],
797  ptr_v + dct_uv_offset, uvlinesize);
798  }
799  } else {
800  pdsp->get_pixels(ctx->blocks[0], ptr_y, linesize);
801  pdsp->get_pixels(ctx->blocks[1], ptr_y + bw, linesize);
802  pdsp->get_pixels(ctx->blocks[6], ptr_y + dct_y_offset, linesize);
803  pdsp->get_pixels(ctx->blocks[7], ptr_y + dct_y_offset + bw, linesize);
804 
805  pdsp->get_pixels(ctx->blocks[2], ptr_u, uvlinesize);
806  pdsp->get_pixels(ctx->blocks[3], ptr_u + bw, uvlinesize);
807  pdsp->get_pixels(ctx->blocks[8], ptr_u + dct_uv_offset, uvlinesize);
808  pdsp->get_pixels(ctx->blocks[9], ptr_u + dct_uv_offset + bw, uvlinesize);
809 
810  pdsp->get_pixels(ctx->blocks[4], ptr_v, uvlinesize);
811  pdsp->get_pixels(ctx->blocks[5], ptr_v + bw, uvlinesize);
812  pdsp->get_pixels(ctx->blocks[10], ptr_v + dct_uv_offset, uvlinesize);
813  pdsp->get_pixels(ctx->blocks[11], ptr_v + dct_uv_offset + bw, uvlinesize);
814  }
815 }
816 
817 static av_always_inline
819 {
820  int x;
821 
822  if (ctx->is_444) {
823  x = (i >> 1) % 3;
824  } else {
825  const static uint8_t component[8]={0,0,1,2,0,0,1,2};
826  x = component[i];
827  }
828  return x;
829 }
830 
832  int jobnr, int threadnr)
833 {
834  DNXHDEncContext *ctx = avctx->priv_data;
835  int mb_y = jobnr, mb_x;
836  int qscale = ctx->qscale;
837  LOCAL_ALIGNED_16(int16_t, block, [64]);
838  ctx = ctx->thread[threadnr];
839 
840  ctx->m.last_dc[0] =
841  ctx->m.last_dc[1] =
842  ctx->m.last_dc[2] = 1 << (ctx->bit_depth + 2);
843 
844  for (mb_x = 0; mb_x < ctx->m.mb_width; mb_x++) {
845  unsigned mb = mb_y * ctx->m.mb_width + mb_x;
846  int ssd = 0;
847  int ac_bits = 0;
848  int dc_bits = 0;
849  int i;
850 
851  dnxhd_get_blocks(ctx, mb_x, mb_y);
852 
853  for (i = 0; i < 8 + 4 * ctx->is_444; i++) {
854  int16_t *src_block = ctx->blocks[i];
855  int overflow, nbits, diff, last_index;
856  int n = dnxhd_switch_matrix(ctx, i);
857 
858  memcpy(block, src_block, 64 * sizeof(*block));
859  last_index = ctx->m.dct_quantize(&ctx->m, block,
860  ctx->is_444 ? 4 * (n > 0): 4 & (2*i),
861  qscale, &overflow);
862  ac_bits += dnxhd_calc_ac_bits(ctx, block, last_index);
863 
864  diff = block[0] - ctx->m.last_dc[n];
865  if (diff < 0)
866  nbits = av_log2_16bit(-2 * diff);
867  else
868  nbits = av_log2_16bit(2 * diff);
869 
870  av_assert1(nbits < ctx->bit_depth + 4);
871  dc_bits += ctx->cid_table->dc_bits[nbits] + nbits;
872 
873  ctx->m.last_dc[n] = block[0];
874 
875  if (avctx->mb_decision == FF_MB_DECISION_RD || !RC_VARIANCE) {
876  dnxhd_unquantize_c(ctx, block, i, qscale, last_index);
877  ctx->m.idsp.idct(block);
878  ssd += dnxhd_ssd_block(block, src_block);
879  }
880  }
881  ctx->mb_rc[(qscale * ctx->m.mb_num) + mb].ssd = ssd;
882  ctx->mb_rc[(qscale * ctx->m.mb_num) + mb].bits = ac_bits + dc_bits + 12 +
883  (1 + ctx->is_444) * 8 * ctx->vlc_bits[0];
884  }
885  return 0;
886 }
887 
889  int jobnr, int threadnr)
890 {
891  DNXHDEncContext *ctx = avctx->priv_data;
892  int mb_y = jobnr, mb_x;
893  ctx = ctx->thread[threadnr];
894  init_put_bits(&ctx->m.pb, (uint8_t *)arg + ctx->data_offset + ctx->slice_offs[jobnr],
895  ctx->slice_size[jobnr]);
896 
897  ctx->m.last_dc[0] =
898  ctx->m.last_dc[1] =
899  ctx->m.last_dc[2] = 1 << (ctx->bit_depth + 2);
900  for (mb_x = 0; mb_x < ctx->m.mb_width; mb_x++) {
901  unsigned mb = mb_y * ctx->m.mb_width + mb_x;
902  int qscale = ctx->mb_qscale[mb];
903  int i;
904 
905  put_bits(&ctx->m.pb, 11, qscale);
906  put_bits(&ctx->m.pb, 1, avctx->pix_fmt == AV_PIX_FMT_YUV444P10);
907 
908  dnxhd_get_blocks(ctx, mb_x, mb_y);
909 
910  for (i = 0; i < 8 + 4 * ctx->is_444; i++) {
911  int16_t *block = ctx->blocks[i];
912  int overflow, n = dnxhd_switch_matrix(ctx, i);
913  int last_index = ctx->m.dct_quantize(&ctx->m, block,
914  ctx->is_444 ? (((i >> 1) % 3) < 1 ? 0 : 4): 4 & (2*i),
915  qscale, &overflow);
916 
917  dnxhd_encode_block(ctx, block, last_index, n);
918  }
919  }
920  if (put_bits_count(&ctx->m.pb) & 31)
921  put_bits(&ctx->m.pb, 32 - (put_bits_count(&ctx->m.pb) & 31), 0);
922  flush_put_bits(&ctx->m.pb);
923  return 0;
924 }
925 
927 {
928  int mb_y, mb_x;
929  int offset = 0;
930  for (mb_y = 0; mb_y < ctx->m.mb_height; mb_y++) {
931  int thread_size;
932  ctx->slice_offs[mb_y] = offset;
933  ctx->slice_size[mb_y] = 0;
934  for (mb_x = 0; mb_x < ctx->m.mb_width; mb_x++) {
935  unsigned mb = mb_y * ctx->m.mb_width + mb_x;
936  ctx->slice_size[mb_y] += ctx->mb_bits[mb];
937  }
938  ctx->slice_size[mb_y] = (ctx->slice_size[mb_y] + 31) & ~31;
939  ctx->slice_size[mb_y] >>= 3;
940  thread_size = ctx->slice_size[mb_y];
941  offset += thread_size;
942  }
943 }
944 
946  int jobnr, int threadnr)
947 {
948  DNXHDEncContext *ctx = avctx->priv_data;
949  int mb_y = jobnr, mb_x, x, y;
950  int partial_last_row = (mb_y == ctx->m.mb_height - 1) &&
951  ((avctx->height >> ctx->interlaced) & 0xF);
952 
953  ctx = ctx->thread[threadnr];
954  if (ctx->bit_depth == 8) {
955  uint8_t *pix = ctx->thread[0]->src[0] + ((mb_y << 4) * ctx->m.linesize);
956  for (mb_x = 0; mb_x < ctx->m.mb_width; ++mb_x, pix += 16) {
957  unsigned mb = mb_y * ctx->m.mb_width + mb_x;
958  int sum;
959  int varc;
960 
961  if (!partial_last_row && mb_x * 16 <= avctx->width - 16 && (avctx->width % 16) == 0) {
962  sum = ctx->m.mpvencdsp.pix_sum(pix, ctx->m.linesize);
963  varc = ctx->m.mpvencdsp.pix_norm1(pix, ctx->m.linesize);
964  } else {
965  int bw = FFMIN(avctx->width - 16 * mb_x, 16);
966  int bh = FFMIN((avctx->height >> ctx->interlaced) - 16 * mb_y, 16);
967  sum = varc = 0;
968  for (y = 0; y < bh; y++) {
969  for (x = 0; x < bw; x++) {
970  uint8_t val = pix[x + y * ctx->m.linesize];
971  sum += val;
972  varc += val * val;
973  }
974  }
975  }
976  varc = (varc - (((unsigned) sum * sum) >> 8) + 128) >> 8;
977 
978  ctx->mb_cmp[mb].value = varc;
979  ctx->mb_cmp[mb].mb = mb;
980  }
981  } else { // 10-bit
982  const int linesize = ctx->m.linesize >> 1;
983  for (mb_x = 0; mb_x < ctx->m.mb_width; ++mb_x) {
984  uint16_t *pix = (uint16_t *)ctx->thread[0]->src[0] +
985  ((mb_y << 4) * linesize) + (mb_x << 4);
986  unsigned mb = mb_y * ctx->m.mb_width + mb_x;
987  int sum = 0;
988  int sqsum = 0;
989  int bw = FFMIN(avctx->width - 16 * mb_x, 16);
990  int bh = FFMIN((avctx->height >> ctx->interlaced) - 16 * mb_y, 16);
991  int mean, sqmean;
992  int i, j;
993  // Macroblocks are 16x16 pixels, unlike DCT blocks which are 8x8.
994  for (i = 0; i < bh; ++i) {
995  for (j = 0; j < bw; ++j) {
996  // Turn 16-bit pixels into 10-bit ones.
997  const int sample = (unsigned) pix[j] >> 6;
998  sum += sample;
999  sqsum += sample * sample;
1000  // 2^10 * 2^10 * 16 * 16 = 2^28, which is less than INT_MAX
1001  }
1002  pix += linesize;
1003  }
1004  mean = sum >> 8; // 16*16 == 2^8
1005  sqmean = sqsum >> 8;
1006  ctx->mb_cmp[mb].value = sqmean - mean * mean;
1007  ctx->mb_cmp[mb].mb = mb;
1008  }
1009  }
1010  return 0;
1011 }
1012 
1014 {
1015  int lambda, up_step, down_step;
1016  int last_lower = INT_MAX, last_higher = 0;
1017  int x, y, q;
1018 
1019  for (q = 1; q < avctx->qmax; q++) {
1020  ctx->qscale = q;
1021  avctx->execute2(avctx, dnxhd_calc_bits_thread,
1022  NULL, NULL, ctx->m.mb_height);
1023  }
1024  up_step = down_step = 2 << LAMBDA_FRAC_BITS;
1025  lambda = ctx->lambda;
1026 
1027  for (;;) {
1028  int bits = 0;
1029  int end = 0;
1030  if (lambda == last_higher) {
1031  lambda++;
1032  end = 1; // need to set final qscales/bits
1033  }
1034  for (y = 0; y < ctx->m.mb_height; y++) {
1035  for (x = 0; x < ctx->m.mb_width; x++) {
1036  unsigned min = UINT_MAX;
1037  int qscale = 1;
1038  int mb = y * ctx->m.mb_width + x;
1039  int rc = 0;
1040  for (q = 1; q < avctx->qmax; q++) {
1041  int i = (q*ctx->m.mb_num) + mb;
1042  unsigned score = ctx->mb_rc[i].bits * lambda +
1043  ((unsigned) ctx->mb_rc[i].ssd << LAMBDA_FRAC_BITS);
1044  if (score < min) {
1045  min = score;
1046  qscale = q;
1047  rc = i;
1048  }
1049  }
1050  bits += ctx->mb_rc[rc].bits;
1051  ctx->mb_qscale[mb] = qscale;
1052  ctx->mb_bits[mb] = ctx->mb_rc[rc].bits;
1053  }
1054  bits = (bits + 31) & ~31; // padding
1055  if (bits > ctx->frame_bits)
1056  break;
1057  }
1058  if (end) {
1059  if (bits > ctx->frame_bits)
1060  return AVERROR(EINVAL);
1061  break;
1062  }
1063  if (bits < ctx->frame_bits) {
1064  last_lower = FFMIN(lambda, last_lower);
1065  if (last_higher != 0)
1066  lambda = (lambda+last_higher)>>1;
1067  else
1068  lambda -= down_step;
1069  down_step = FFMIN((int64_t)down_step*5, INT_MAX);
1070  up_step = 1<<LAMBDA_FRAC_BITS;
1071  lambda = FFMAX(1, lambda);
1072  if (lambda == last_lower)
1073  break;
1074  } else {
1075  last_higher = FFMAX(lambda, last_higher);
1076  if (last_lower != INT_MAX)
1077  lambda = (lambda+last_lower)>>1;
1078  else if ((int64_t)lambda + up_step > INT_MAX)
1079  return AVERROR(EINVAL);
1080  else
1081  lambda += up_step;
1082  up_step = FFMIN((int64_t)up_step*5, INT_MAX);
1083  down_step = 1<<LAMBDA_FRAC_BITS;
1084  }
1085  }
1086  ctx->lambda = lambda;
1087  return 0;
1088 }
1089 
1091 {
1092  int bits = 0;
1093  int up_step = 1;
1094  int down_step = 1;
1095  int last_higher = 0;
1096  int last_lower = INT_MAX;
1097  int qscale;
1098  int x, y;
1099 
1100  qscale = ctx->qscale;
1101  for (;;) {
1102  bits = 0;
1103  ctx->qscale = qscale;
1104  // XXX avoid recalculating bits
1106  NULL, NULL, ctx->m.mb_height);
1107  for (y = 0; y < ctx->m.mb_height; y++) {
1108  for (x = 0; x < ctx->m.mb_width; x++)
1109  bits += ctx->mb_rc[(qscale*ctx->m.mb_num) + (y*ctx->m.mb_width+x)].bits;
1110  bits = (bits+31)&~31; // padding
1111  if (bits > ctx->frame_bits)
1112  break;
1113  }
1114  if (bits < ctx->frame_bits) {
1115  if (qscale == 1)
1116  return 1;
1117  if (last_higher == qscale - 1) {
1118  qscale = last_higher;
1119  break;
1120  }
1121  last_lower = FFMIN(qscale, last_lower);
1122  if (last_higher != 0)
1123  qscale = (qscale + last_higher) >> 1;
1124  else
1125  qscale -= down_step++;
1126  if (qscale < 1)
1127  qscale = 1;
1128  up_step = 1;
1129  } else {
1130  if (last_lower == qscale + 1)
1131  break;
1132  last_higher = FFMAX(qscale, last_higher);
1133  if (last_lower != INT_MAX)
1134  qscale = (qscale + last_lower) >> 1;
1135  else
1136  qscale += up_step++;
1137  down_step = 1;
1138  if (qscale >= ctx->m.avctx->qmax)
1139  return AVERROR(EINVAL);
1140  }
1141  }
1142  ctx->qscale = qscale;
1143  return 0;
1144 }
1145 
1146 #define BUCKET_BITS 8
1147 #define RADIX_PASSES 4
1148 #define NBUCKETS (1 << BUCKET_BITS)
1149 
1150 static inline int get_bucket(int value, int shift)
1151 {
1152  value >>= shift;
1153  value &= NBUCKETS - 1;
1154  return NBUCKETS - 1 - value;
1155 }
1156 
1157 static void radix_count(const RCCMPEntry *data, int size,
1158  int buckets[RADIX_PASSES][NBUCKETS])
1159 {
1160  int i, j;
1161  memset(buckets, 0, sizeof(buckets[0][0]) * RADIX_PASSES * NBUCKETS);
1162  for (i = 0; i < size; i++) {
1163  int v = data[i].value;
1164  for (j = 0; j < RADIX_PASSES; j++) {
1165  buckets[j][get_bucket(v, 0)]++;
1166  v >>= BUCKET_BITS;
1167  }
1168  av_assert1(!v);
1169  }
1170  for (j = 0; j < RADIX_PASSES; j++) {
1171  int offset = size;
1172  for (i = NBUCKETS - 1; i >= 0; i--)
1173  buckets[j][i] = offset -= buckets[j][i];
1174  av_assert1(!buckets[j][0]);
1175  }
1176 }
1177 
1178 static void radix_sort_pass(RCCMPEntry *dst, const RCCMPEntry *data,
1179  int size, int buckets[NBUCKETS], int pass)
1180 {
1181  int shift = pass * BUCKET_BITS;
1182  int i;
1183  for (i = 0; i < size; i++) {
1184  int v = get_bucket(data[i].value, shift);
1185  int pos = buckets[v]++;
1186  dst[pos] = data[i];
1187  }
1188 }
1189 
1191 {
1192  int buckets[RADIX_PASSES][NBUCKETS];
1193  radix_count(data, size, buckets);
1194  radix_sort_pass(tmp, data, size, buckets[0], 0);
1195  radix_sort_pass(data, tmp, size, buckets[1], 1);
1196  if (buckets[2][NBUCKETS - 1] || buckets[3][NBUCKETS - 1]) {
1197  radix_sort_pass(tmp, data, size, buckets[2], 2);
1198  radix_sort_pass(data, tmp, size, buckets[3], 3);
1199  }
1200 }
1201 
1203 {
1204  int max_bits = 0;
1205  int ret, x, y;
1206  if ((ret = dnxhd_find_qscale(ctx)) < 0)
1207  return ret;
1208  for (y = 0; y < ctx->m.mb_height; y++) {
1209  for (x = 0; x < ctx->m.mb_width; x++) {
1210  int mb = y * ctx->m.mb_width + x;
1211  int rc = (ctx->qscale * ctx->m.mb_num ) + mb;
1212  int delta_bits;
1213  ctx->mb_qscale[mb] = ctx->qscale;
1214  ctx->mb_bits[mb] = ctx->mb_rc[rc].bits;
1215  max_bits += ctx->mb_rc[rc].bits;
1216  if (!RC_VARIANCE) {
1217  delta_bits = ctx->mb_rc[rc].bits -
1218  ctx->mb_rc[rc + ctx->m.mb_num].bits;
1219  ctx->mb_cmp[mb].mb = mb;
1220  ctx->mb_cmp[mb].value =
1221  delta_bits ? ((ctx->mb_rc[rc].ssd -
1222  ctx->mb_rc[rc + ctx->m.mb_num].ssd) * 100) /
1223  delta_bits
1224  : INT_MIN; // avoid increasing qscale
1225  }
1226  }
1227  max_bits += 31; // worst padding
1228  }
1229  if (!ret) {
1230  if (RC_VARIANCE)
1231  avctx->execute2(avctx, dnxhd_mb_var_thread,
1232  NULL, NULL, ctx->m.mb_height);
1233  radix_sort(ctx->mb_cmp, ctx->mb_cmp_tmp, ctx->m.mb_num);
1234  for (x = 0; x < ctx->m.mb_num && max_bits > ctx->frame_bits; x++) {
1235  int mb = ctx->mb_cmp[x].mb;
1236  int rc = (ctx->qscale * ctx->m.mb_num ) + mb;
1237  max_bits -= ctx->mb_rc[rc].bits -
1238  ctx->mb_rc[rc + ctx->m.mb_num].bits;
1239  ctx->mb_qscale[mb] = ctx->qscale + 1;
1240  ctx->mb_bits[mb] = ctx->mb_rc[rc + ctx->m.mb_num].bits;
1241  }
1242  }
1243  return 0;
1244 }
1245 
1247 {
1248  int i;
1249 
1250  for (i = 0; i < ctx->m.avctx->thread_count; i++) {
1251  ctx->thread[i]->m.linesize = frame->linesize[0] << ctx->interlaced;
1252  ctx->thread[i]->m.uvlinesize = frame->linesize[1] << ctx->interlaced;
1253  ctx->thread[i]->dct_y_offset = ctx->m.linesize *8;
1254  ctx->thread[i]->dct_uv_offset = ctx->m.uvlinesize*8;
1255  }
1256 
1257 #if FF_API_CODED_FRAME
1261 #endif
1262  ctx->cur_field = frame->interlaced_frame && !frame->top_field_first;
1263 }
1264 
1266  const AVFrame *frame, int *got_packet)
1267 {
1268  DNXHDEncContext *ctx = avctx->priv_data;
1269  int first_field = 1;
1270  int offset, i, ret;
1271  uint8_t *buf;
1272 
1273  if ((ret = ff_alloc_packet2(avctx, pkt, ctx->frame_size, 0)) < 0)
1274  return ret;
1275  buf = pkt->data;
1276 
1277  dnxhd_load_picture(ctx, frame);
1278 
1279 encode_coding_unit:
1280  for (i = 0; i < 3; i++) {
1281  ctx->src[i] = frame->data[i];
1282  if (ctx->interlaced && ctx->cur_field)
1283  ctx->src[i] += frame->linesize[i];
1284  }
1285 
1286  dnxhd_write_header(avctx, buf);
1287 
1288  if (avctx->mb_decision == FF_MB_DECISION_RD)
1289  ret = dnxhd_encode_rdo(avctx, ctx);
1290  else
1291  ret = dnxhd_encode_fast(avctx, ctx);
1292  if (ret < 0) {
1293  av_log(avctx, AV_LOG_ERROR,
1294  "picture could not fit ratecontrol constraints, increase qmax\n");
1295  return ret;
1296  }
1297 
1299 
1300  offset = 0;
1301  for (i = 0; i < ctx->m.mb_height; i++) {
1302  AV_WB32(ctx->msip + i * 4, offset);
1303  offset += ctx->slice_size[i];
1304  av_assert1(!(ctx->slice_size[i] & 3));
1305  }
1306 
1307  avctx->execute2(avctx, dnxhd_encode_thread, buf, NULL, ctx->m.mb_height);
1308 
1309  av_assert1(ctx->data_offset + offset + 4 <= ctx->coding_unit_size);
1310  memset(buf + ctx->data_offset + offset, 0,
1311  ctx->coding_unit_size - 4 - offset - ctx->data_offset);
1312 
1313  AV_WB32(buf + ctx->coding_unit_size - 4, 0x600DC0DE); // EOF
1314 
1315  if (ctx->interlaced && first_field) {
1316  first_field = 0;
1317  ctx->cur_field ^= 1;
1318  buf += ctx->coding_unit_size;
1319  goto encode_coding_unit;
1320  }
1321 
1322 #if FF_API_CODED_FRAME
1324  avctx->coded_frame->quality = ctx->qscale * FF_QP2LAMBDA;
1326 #endif
1327 
1329 
1330  pkt->flags |= AV_PKT_FLAG_KEY;
1331  *got_packet = 1;
1332  return 0;
1333 }
1334 
1336 {
1337  DNXHDEncContext *ctx = avctx->priv_data;
1338  int i;
1339 
1340  av_freep(&ctx->orig_vlc_codes);
1341  av_freep(&ctx->orig_vlc_bits);
1342  av_freep(&ctx->run_codes);
1343  av_freep(&ctx->run_bits);
1344 
1345  av_freep(&ctx->mb_bits);
1346  av_freep(&ctx->mb_qscale);
1347  av_freep(&ctx->mb_rc);
1348  av_freep(&ctx->mb_cmp);
1349  av_freep(&ctx->mb_cmp_tmp);
1350  av_freep(&ctx->slice_size);
1351  av_freep(&ctx->slice_offs);
1352 
1353  av_freep(&ctx->qmatrix_c);
1354  av_freep(&ctx->qmatrix_l);
1355  av_freep(&ctx->qmatrix_c16);
1356  av_freep(&ctx->qmatrix_l16);
1357 
1358  if (avctx->active_thread_type == FF_THREAD_SLICE) {
1359  for (i = 1; i < avctx->thread_count; i++)
1360  av_freep(&ctx->thread[i]);
1361  }
1362 
1363  return 0;
1364 }
1365 
1366 static const AVCodecDefault dnxhd_defaults[] = {
1367  { "qmax", "1024" }, /* Maximum quantization scale factor allowed for VC-3 */
1368  { NULL },
1369 };
1370 
1372  .name = "dnxhd",
1373  .long_name = NULL_IF_CONFIG_SMALL("VC3/DNxHD"),
1374  .type = AVMEDIA_TYPE_VIDEO,
1375  .id = AV_CODEC_ID_DNXHD,
1376  .priv_data_size = sizeof(DNXHDEncContext),
1378  .encode2 = dnxhd_encode_picture,
1379  .close = dnxhd_encode_end,
1381  .caps_internal = FF_CODEC_CAP_INIT_CLEANUP,
1382  .pix_fmts = (const enum AVPixelFormat[]) {
1388  },
1389  .priv_class = &dnxhd_class,
1390  .defaults = dnxhd_defaults,
1392 };
#define FF_CODEC_CAP_INIT_CLEANUP
The codec allows calling the close function for deallocation even if the init function returned a fai...
Definition: internal.h:48
#define FF_PROFILE_DNXHD
Definition: avcodec.h:1886
int16_t blocks[12][64]
Definition: dnxhdenc.h:79
static av_always_inline int dnxhd_ssd_block(int16_t *qblock, int16_t *block)
Definition: dnxhdenc.c:661
#define MASK_ABS(mask, level)
Definition: mathops.h:155
av_cold void ff_videodsp_init(VideoDSPContext *ctx, int bpc)
Definition: videodsp.c:38
IDCTDSPContext idsp
Definition: mpegvideo.h:230
static void radix_count(const RCCMPEntry *data, int size, int buckets[RADIX_PASSES][NBUCKETS])
Definition: dnxhdenc.c:1157
#define NULL
Definition: coverity.c:32
static av_always_inline void dnxhd_get_blocks(DNXHDEncContext *ctx, int mb_x, int mb_y)
Definition: dnxhdenc.c:690
RCCMPEntry * mb_cmp_tmp
Definition: dnxhdenc.h:107
static int shift(int a, int b)
Definition: sonic.c:82
RCEntry * mb_rc
Definition: dnxhdenc.h:108
This structure describes decoded (raw) audio or video data.
Definition: frame.h:314
const uint8_t * dc_bits
Definition: dnxhddata.h:52
AVOption.
Definition: opt.h:248
int ff_side_data_set_encoder_stats(AVPacket *pkt, int quality, int64_t *error, int error_count, int pict_type)
Definition: avpacket.c:796
ptrdiff_t const GLvoid * data
Definition: opengl_enc.c:100
#define AV_CODEC_FLAG_INTERLACED_DCT
Use interlaced DCT.
Definition: avcodec.h:321
#define LAMBDA_FRAC_BITS
Definition: dnxhdenc.c:44
static void put_bits(Jpeg2000EncoderContext *s, int val, int n)
put n times val bit
Definition: j2kenc.c:218
const uint8_t * luma_weight
Definition: dnxhddata.h:51
#define LIBAVUTIL_VERSION_INT
Definition: version.h:85
static av_cold int init(AVCodecContext *avctx)
Definition: avrndec.c:35
uint16_t(* q_chroma_intra_matrix16)[2][64]
Definition: mpegvideo.h:328
void(* clear_block)(int16_t *block)
Definition: blockdsp.h:36
int(* qmatrix_l)[64]
Definition: dnxhdenc.h:84
av_cold void ff_blockdsp_init(BlockDSPContext *c, AVCodecContext *avctx)
Definition: blockdsp.c:60
#define AV_PIX_FMT_GBRP10
Definition: pixfmt.h:415
const char * av_default_item_name(void *ptr)
Return the context name.
Definition: log.c:235
const CIDEntry ff_dnxhd_cid_table[]
Definition: dnxhddata.c:935
const uint16_t * run_codes
Definition: dnxhddata.h:55
static const AVClass dnxhd_class
Definition: dnxhdenc.c:71
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:92
enum AVPixelFormat pix_fmt
Pixel format, see AV_PIX_FMT_xxx.
Definition: avcodec.h:741
unsigned dct_uv_offset
Definition: dnxhdenc.h:65
static av_always_inline void dnxhd_encode_dc(DNXHDEncContext *ctx, int diff)
Definition: dnxhdenc.c:572
static av_cold int dnxhd_init_vlc(DNXHDEncContext *ctx)
Definition: dnxhdenc.c:206
mpegvideo header.
void * av_mallocz(size_t size)
Allocate a memory block with alignment suitable for all memory accesses (including vectors if availab...
Definition: mem.c:237
uint8_t permutated[64]
Definition: idctdsp.h:33
int ff_dnxhd_find_cid(AVCodecContext *avctx, int bit_depth)
Definition: dnxhddata.c:1133
uint8_t run
Definition: svq3.c:205
static AVPacket pkt
int bits_per_raw_sample
Bits per sample/pixel of internal libavcodec pixel/sample format.
Definition: avcodec.h:1762
int mb_num
number of MBs of a picture
Definition: mpegvideo.h:133
av_cold void ff_fdctdsp_init(FDCTDSPContext *c, AVCodecContext *avctx)
Definition: fdctdsp.c:26
struct DNXHDEncContext * thread[MAX_THREADS]
Definition: dnxhdenc.h:60
int profile
profile
Definition: avcodec.h:1871
#define sample
int av_log2_16bit(unsigned v)
Definition: intmath.c:31
AVCodec.
Definition: codec.h:190
Undefined Behavior In the C some operations are like signed integer overflow
Definition: undefined.txt:3
static av_always_inline int dnxhd_calc_ac_bits(DNXHDEncContext *ctx, int16_t *block, int last_index)
Definition: dnxhdenc.c:671
int h263_aic
Advanced INTRA Coding (AIC)
Definition: mpegvideo.h:87
Macro definitions for various function/variable attributes.
const uint8_t * buf
Definition: dnxhddec.c:56
#define LOCAL_ALIGNED_16(t, v,...)
Definition: mem_internal.h:130
int coding_unit_size
Definition: dnxhdenc.h:69
int intra_quant_bias
Definition: dnxhdenc.h:77
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
void(* emulated_edge_mc)(uint8_t *dst, const uint8_t *src, ptrdiff_t dst_linesize, ptrdiff_t src_linesize, int block_w, int block_h, int src_x, int src_y, int w, int h)
Copy a rectangular area of samples to a temporary buffer and replicate the border samples...
Definition: videodsp.h:63
#define av_assert0(cond)
assert() equivalent, that is always enabled.
Definition: avassert.h:37
int ff_alloc_packet2(AVCodecContext *avctx, AVPacket *avpkt, int64_t size, int64_t min_size)
Check AVPacket size and/or allocate data.
Definition: encode.c:33
void ff_block_permute(int16_t *block, uint8_t *permutation, const uint8_t *scantable, int last)
Permute an 8x8 block according to permutation.
av_cold void ff_mpegvideoencdsp_init(MpegvideoEncDSPContext *c, AVCodecContext *avctx)
The exact code depends on how similar the blocks are and how related they are to the block
uint8_t
#define av_cold
Definition: attributes.h:88
#define av_malloc(s)
#define mb
AVOptions.
static int get_bucket(int value, int shift)
Definition: dnxhdenc.c:1150
const AVProfile ff_dnxhd_profiles[]
Definition: profiles.c:48
it s the only field you need to keep assuming you have a context There is some magic you don t need to care about around this just let it vf offset
static av_cold int end(AVCodecContext *avctx)
Definition: avrndec.c:92
static int dnxhd_encode_fast(AVCodecContext *avctx, DNXHDEncContext *ctx)
Definition: dnxhdenc.c:1202
uint32_t * slice_size
Definition: dnxhdenc.h:57
int(* qmatrix_c)[64]
Definition: dnxhdenc.h:83
#define RADIX_PASSES
Definition: dnxhdenc.c:1147
GLsizei GLboolean const GLfloat * value
Definition: opengl_enc.c:108
uint32_t * slice_offs
Definition: dnxhdenc.h:58
int(* q_chroma_intra_matrix)[64]
Definition: mpegvideo.h:324
unsigned qscale
Definition: dnxhdenc.h:100
uint8_t * data
Definition: packet.h:363
uint8_t * orig_vlc_bits
Definition: dnxhdenc.h:92
static void radix_sort_pass(RCCMPEntry *dst, const RCCMPEntry *data, int size, int buckets[NBUCKETS], int pass)
Definition: dnxhdenc.c:1178
const uint8_t * run_bits
Definition: dnxhddata.h:56
#define BUCKET_BITS
Definition: dnxhdenc.c:1146
const uint8_t * scantable
Definition: idctdsp.h:32
int interlaced_frame
The content of the picture is interlaced.
Definition: frame.h:461
av_cold void ff_mpv_idct_init(MpegEncContext *s)
Definition: mpegvideo.c:331
int mb_height
number of MBs horizontally & vertically
Definition: mpegvideo.h:129
static av_cold int dnxhd_init_qmat(DNXHDEncContext *ctx, int lbias, int cbias)
Definition: dnxhdenc.c:261
#define max(a, b)
Definition: cuda_runtime.h:33
unsigned int coding_unit_size
Definition: dnxhddata.h:46
ptrdiff_t size
Definition: opengl_enc.c:100
int max_qcoeff
maximum encodable coefficient
Definition: mpegvideo.h:309
#define AV_WB16(p, v)
Definition: intreadwrite.h:405
BlockDSPContext bdsp
Definition: dnxhdenc.h:48
#define av_log(a,...)
static int first_field(const struct video_data *s)
Definition: v4l2.c:234
static av_cold int dnxhd_encode_end(AVCodecContext *avctx)
Definition: dnxhdenc.c:1335
#define AV_PKT_FLAG_KEY
The packet contains a keyframe.
Definition: packet.h:401
const uint8_t * ac_bits
Definition: dnxhddata.h:54
const uint8_t * ac_info
Definition: dnxhddata.h:54
int(* q_intra_matrix)[64]
precomputed matrix (combine qscale and DCT renorm)
Definition: mpegvideo.h:323
static av_always_inline int dnxhd_switch_matrix(DNXHDEncContext *ctx, int i)
Definition: dnxhdenc.c:818
const uint16_t * ac_codes
Definition: dnxhddata.h:53
static int dnxhd_calc_bits_thread(AVCodecContext *avctx, void *arg, int jobnr, int threadnr)
Definition: dnxhdenc.c:831
#define AV_LOG_ERROR
Something went wrong and cannot losslessly be recovered.
Definition: log.h:194
int ff_dnxhd_get_cid_table(int cid)
Definition: dnxhddata.c:1078
int last_dc[3]
last DC values for MPEG-1
Definition: mpegvideo.h:185
static int dnxhd_find_qscale(DNXHDEncContext *ctx)
Definition: dnxhdenc.c:1090
#define FF_SIGNBIT(x)
Definition: internal.h:99
#define RC_VARIANCE
Definition: dnxhdenc.c:43
int qmax
maximum quantizer
Definition: avcodec.h:1380
#define NULL_IF_CONFIG_SMALL(x)
Return NULL if CONFIG_SMALL is true, otherwise the argument without modification. ...
Definition: internal.h:115
int active_thread_type
Which multithreading methods are in use by the codec.
Definition: avcodec.h:1809
AVCodec ff_dnxhd_encoder
Definition: dnxhdenc.c:1371
static av_always_inline void dnxhd_unquantize_c(DNXHDEncContext *ctx, int16_t *block, int n, int qscale, int last_index)
Definition: dnxhdenc.c:613
unsigned int pos
Definition: spdifenc.c:410
#define AV_LOG_DEBUG
Stuff which is only useful for libav* developers.
Definition: log.h:215
PixblockDSPContext pdsp
Definition: mpegvideo.h:234
const char * arg
Definition: jacosubdec.c:66
int flags
AV_CODEC_FLAG_*.
Definition: avcodec.h:611
static int dnxhd_write_header(AVCodecContext *avctx, uint8_t *buf)
Definition: dnxhdenc.c:536
const uint8_t * dc_codes
Definition: dnxhddata.h:52
MpegvideoEncDSPContext mpvencdsp
Definition: mpegvideo.h:233
const char * name
Name of the codec implementation.
Definition: codec.h:197
#define AV_PIX_FMT_YUV444P10
Definition: pixfmt.h:402
uint8_t bits
Definition: vp3data.h:141
#define FF_PROFILE_DNXHR_LB
Definition: avcodec.h:1887
#define FFMAX(a, b)
Definition: common.h:94
av_cold void ff_pixblockdsp_init(PixblockDSPContext *c, AVCodecContext *avctx)
Definition: pixblockdsp.c:81
static float mean(const float *input, int size)
Definition: vf_nnedi.c:933
#define AV_CODEC_CAP_FRAME_THREADS
Codec supports frame-level multithreading.
Definition: codec.h:106
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: packet.h:369
static int put_bits_count(PutBitContext *s)
Definition: put_bits.h:83
planar YUV 4:2:2, 16bpp, (1 Cr & Cb sample per 2x1 Y samples)
Definition: pixfmt.h:70
#define pass
Definition: fft_template.c:605
const uint8_t * chroma_weight
Definition: dnxhddata.h:51
uint8_t edge_buf_y[512]
Definition: dnxhdenc.h:80
uint16_t * run_codes
Definition: dnxhdenc.h:95
common internal API header
#define MAX_THREADS
static const AVOption options[]
Definition: dnxhdenc.c:47
static void dnxhd_8bit_get_pixels_8x4_sym(int16_t *av_restrict block, const uint8_t *pixels, ptrdiff_t line_size)
Definition: dnxhdenc.c:78
enum AVPictureType pict_type
Picture type of the frame.
Definition: frame.h:397
#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:1366
MpegEncContext m
Used for quantization dsp functions.
Definition: dnxhdenc.h:49
#define FFMIN(a, b)
Definition: common.h:96
static av_always_inline void dnxhd_encode_block(DNXHDEncContext *ctx, int16_t *block, int last_index, int n)
Definition: dnxhdenc.c:587
uint8_t * run_bits
Definition: dnxhdenc.h:96
int intra_quant_bias
bias for the quantizer
Definition: mpegvideo.h:306
int width
picture width / height.
Definition: avcodec.h:704
const uint8_t * run
Definition: dnxhddata.h:56
static void bit_depth(AudioStatsContext *s, uint64_t mask, uint64_t imask, AVRational *depth)
Definition: af_astats.c:254
unsigned frame_bits
Definition: dnxhdenc.h:88
AVFormatContext * ctx
Definition: movenc.c:48
static av_always_inline void dnxhd_10bit_get_pixels_8x4_sym(int16_t *av_restrict block, const uint8_t *pixels, ptrdiff_t line_size)
Definition: dnxhdenc.c:102
uint16_t(* q_intra_matrix16)[2][64]
identical to the above but for MMX & these are not permutated, second 64 entries are bias ...
Definition: mpegvideo.h:327
uint16_t(* qmatrix_l16)[2][64]
Definition: dnxhdenc.h:85
these buffered frames must be flushed immediately if a new input produces new the filter must not call request_frame to get more It must just process the frame or queue it The task of requesting more frames is left to the filter s request_frame method or the application If a filter has several the filter must be ready for frames arriving randomly on any input any filter with several inputs will most likely require some kind of queuing mechanism It is perfectly acceptable to have a limited queue and to drop frames when the inputs are too unbalanced request_frame For filters that do not use the this method is called when a frame is wanted on an output For a it should directly call filter_frame on the corresponding output For a if there are queued frames already one of these frames should be pushed If the filter should request a frame on one of its repeatedly until at least one frame has been pushed Return or at least make progress towards producing a frame
#define FF_THREAD_SLICE
Decode more than one part of a single frame at once.
Definition: avcodec.h:1802
int quality
quality (between 1 (good) and FF_LAMBDA_MAX (bad))
Definition: frame.h:437
ScanTable scantable
Definition: dnxhddec.c:67
uint8_t * msip
Macroblock Scan Indexes Payload.
Definition: dnxhdenc.h:56
uint8_t idct_permutation[64]
IDCT input permutation.
Definition: idctdsp.h:96
void(* fdct)(int16_t *block)
Definition: fdctdsp.h:27
int value
Definition: dnxhdenc.h:38
int mb_decision
macroblock decision mode
Definition: avcodec.h:1019
void ff_dnxhd_print_profiles(AVCodecContext *avctx, int loglevel)
Definition: dnxhddata.c:1163
#define FF_PROFILE_DNXHR_HQ
Definition: avcodec.h:1889
uint8_t * vlc_bits
Definition: dnxhdenc.h:94
int thread_count
thread count is used to decide how many independent tasks should be passed to execute() ...
Definition: avcodec.h:1790
int ff_dct_quantize_c(MpegEncContext *s, int16_t *block, int n, int qscale, int *overflow)
static int dnxhd_encode_rdo(AVCodecContext *avctx, DNXHDEncContext *ctx)
Definition: dnxhdenc.c:1013
#define AV_CODEC_CAP_SLICE_THREADS
Codec supports slice-based (or partition-based) multithreading.
Definition: codec.h:110
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:1857
int index_bits
Definition: dnxhddata.h:48
Libavcodec external API header.
static int dnxhd_10bit_dct_quantize(MpegEncContext *ctx, int16_t *block, int n, int qscale, int *overflow)
Definition: dnxhdenc.c:175
ptrdiff_t linesize
line size, in bytes, may be different from width
Definition: mpegvideo.h:134
int linesize[AV_NUM_DATA_POINTERS]
For video, size in bytes of each picture line.
Definition: frame.h:345
static int dnxhd_encode_picture(AVCodecContext *avctx, AVPacket *pkt, const AVFrame *frame, int *got_packet)
Definition: dnxhdenc.c:1265
main external API structure.
Definition: avcodec.h:531
unsigned block_width_l2
Definition: dnxhdenc.h:66
ScanTable intra_scantable
Definition: mpegvideo.h:91
int ff_dct_encode_init(MpegEncContext *s)
static av_cold int dnxhd_init_rc(DNXHDEncContext *ctx)
Definition: dnxhdenc.c:334
uint32_t * orig_vlc_codes
Definition: dnxhdenc.h:91
unsigned int codec_tag
fourcc (LSB first, so "ABCD" -> (&#39;D&#39;<<24) + (&#39;C&#39;<<16) + (&#39;B&#39;<<8) + &#39;A&#39;).
Definition: avcodec.h:556
unsigned lambda
Definition: dnxhdenc.h:101
FDCTDSPContext fdsp
Definition: mpegvideo.h:227
void ff_dnxhdenc_init_x86(DNXHDEncContext *ctx)
Definition: dnxhdenc_init.c:31
Describe the class of an AVClass context structure.
Definition: log.h:67
#define AV_WB32(p, v)
Definition: intreadwrite.h:419
int index
Definition: gxfenc.c:89
#define FF_MB_DECISION_RD
rate distortion
Definition: avcodec.h:1022
#define NBUCKETS
Definition: dnxhdenc.c:1148
const uint8_t ff_zigzag_direct[64]
Definition: mathtables.c:98
ptrdiff_t uvlinesize
line size, for chroma in bytes, may be different from width
Definition: mpegvideo.h:135
int ssd
Definition: dnxhdenc.h:42
static void dnxhd_setup_threads_slices(DNXHDEncContext *ctx)
Definition: dnxhdenc.c:926
#define FF_PROFILE_DNXHR_SQ
Definition: avcodec.h:1888
const CIDEntry * cid_table
Definition: dnxhdenc.h:55
int(* dct_quantize)(struct MpegEncContext *s, int16_t *block, int n, int qscale, int *overflow)
Definition: mpegvideo.h:540
uint16_t(* qmatrix_c16)[2][64]
Definition: dnxhdenc.h:86
unsigned dct_y_offset
Definition: dnxhdenc.h:64
static enum AVPixelFormat pix_fmts[]
Definition: libkvazaar.c:303
unsigned min_padding
Definition: dnxhdenc.h:76
mfxU16 profile
Definition: qsvenc.c:45
static void dnxhd_load_picture(DNXHDEncContext *ctx, const AVFrame *frame)
Definition: dnxhdenc.c:1246
void(* get_pixels)(int16_t *av_restrict block, const uint8_t *pixels, ptrdiff_t stride)
Definition: pixblockdsp.h:29
#define AV_PIX_FMT_YUV422P10
Definition: pixfmt.h:400
#define DNX10BIT_QMAT_SHIFT
Definition: dnxhdenc.c:42
uint8_t * data[AV_NUM_DATA_POINTERS]
pointer to the picture/channel planes.
Definition: frame.h:328
uint8_t edge_buf_uv[2][512]
Definition: dnxhdenc.h:81
unsigned int frame_size
Definition: dnxhddata.h:45
uint8_t level
Definition: svq3.c:206
uint16_t * mb_bits
Definition: dnxhdenc.h:103
MpegEncContext.
Definition: mpegvideo.h:81
struct AVCodecContext * avctx
Definition: mpegvideo.h:98
PutBitContext pb
bit output
Definition: mpegvideo.h:151
static int dnxhd_mb_var_thread(AVCodecContext *avctx, void *arg, int jobnr, int threadnr)
Definition: dnxhdenc.c:945
#define DNXHD_VARIABLE
Indicate that a CIDEntry value must be read in the bitstream.
Definition: dnxhddata.h:40
#define FF_PROFILE_DNXHR_HQX
Definition: avcodec.h:1890
static int dnxhd_encode_thread(AVCodecContext *avctx, void *arg, int jobnr, int threadnr)
Definition: dnxhdenc.c:888
static av_cold int dnxhd_encode_init(AVCodecContext *avctx)
Definition: dnxhdenc.c:351
#define VE
Definition: dnxhdenc.c:46
#define FF_DISABLE_DEPRECATION_WARNINGS
Definition: internal.h:83
common internal api header.
static void flush_put_bits(PutBitContext *s)
Pad the end of the output stream with zeros.
Definition: put_bits.h:117
void(* get_pixels_8x4_sym)(int16_t *av_restrictblock, const uint8_t *pixels, ptrdiff_t line_size)
Definition: dnxhdenc.h:110
attribute_deprecated AVFrame * coded_frame
the picture in the bitstream
Definition: avcodec.h:1781
static int dnxhd_10bit_dct_quantize_444(MpegEncContext *ctx, int16_t *block, int n, int qscale, int *overflow)
Definition: dnxhdenc.c:116
static void init_put_bits(PutBitContext *s, uint8_t *buffer, int buffer_size)
Initialize the PutBitContext s.
Definition: put_bits.h:64
#define FF_PROFILE_DNXHR_444
Definition: avcodec.h:1891
void * priv_data
Definition: avcodec.h:558
static av_always_inline int diff(const uint32_t a, const uint32_t b)
RCCMPEntry * mb_cmp
Definition: dnxhdenc.h:106
VideoDSPContext vdsp
Definition: mpegvideo.h:236
#define FF_ENABLE_DEPRECATION_WARNINGS
Definition: internal.h:84
static void radix_sort(RCCMPEntry *data, RCCMPEntry *tmp, int size)
Definition: dnxhdenc.c:1190
int top_field_first
If the content is interlaced, is top field displayed first.
Definition: frame.h:466
int nitris_compat
Definition: dnxhdenc.h:75
int bits
Definition: dnxhdenc.h:43
AVCodecContext * avctx
Definition: dnxhddec.c:53
int key_frame
1 -> keyframe, 0-> not
Definition: frame.h:392
#define FF_QP2LAMBDA
factor to convert from H.263 QP to lambda
Definition: avutil.h:227
#define av_freep(p)
#define av_always_inline
Definition: attributes.h:45
#define FF_ALLOCZ_TYPED_ARRAY(p, nelem)
Definition: internal.h:103
void(* idct)(int16_t *block)
Definition: idctdsp.h:65
Filter the word “frame” indicates either a video frame or a group of audio as stored in an AVFrame structure Format for each input and each output the list of supported formats For video that means pixel format For audio that means channel sample they are references to shared objects When the negotiation mechanism computes the intersection of the formats supported at each end of a all references to both lists are replaced with a reference to the intersection And when a single format is eventually chosen for a link amongst the remaining all references to the list are updated That means that if a filter requires that its input and output have the same format amongst a supported all it has to do is use a reference to the same list of formats query_formats can leave some formats unset and return AVERROR(EAGAIN) to cause the negotiation mechanism toagain later.That can be used by filters with complex requirements to use the format negotiated on one link to set the formats supported on another.Frame references ownership and permissions
uint16_t mb
Definition: dnxhdenc.h:37
#define MKTAG(a, b, c, d)
Definition: common.h:405
float min
AVPixelFormat
Pixel format.
Definition: pixfmt.h:64
static double val(void *priv, double ch)
Definition: aeval.c:76
This structure stores compressed data.
Definition: packet.h:340
uint32_t * vlc_codes
Definition: dnxhdenc.h:93
int avpriv_dnxhd_get_hr_frame_size(int cid, int w, int h)
Definition: dnxhddata.c:1095
int i
Definition: input.c:407
uint8_t * src[3]
Definition: dnxhdenc.h:89
uint8_t * mb_qscale
Definition: dnxhdenc.h:104
enum idct_permutation_type perm_type
Definition: idctdsp.h:97
static uint8_t tmp[11]
Definition: aes_ctr.c:27