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