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