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