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