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 
365 static int dnxhd_get_hr_frame_size(const CIDEntry* profile, int mb_num)
366 {
367  int result = mb_num * profile->packet_scale.num / profile->packet_scale.den;
368  result = (result + 2048) / 4096 * 4096;
369  return FFMAX(result, 8192);
370 }
371 
373 {
374  DNXHDEncContext *ctx = avctx->priv_data;
375  int i, index, ret;
376 
377  switch (avctx->pix_fmt) {
378  case AV_PIX_FMT_YUV422P:
379  ctx->bit_depth = 8;
380  break;
383  case AV_PIX_FMT_GBRP10:
384  ctx->bit_depth = 10;
385  break;
386  default:
387  av_log(avctx, AV_LOG_ERROR,
388  "pixel format is incompatible with DNxHD\n");
389  return AVERROR(EINVAL);
390  }
391 
392  if ((ctx->profile == FF_PROFILE_DNXHR_444 && (avctx->pix_fmt != AV_PIX_FMT_YUV444P10 &&
393  avctx->pix_fmt != AV_PIX_FMT_GBRP10)) ||
394  (ctx->profile != FF_PROFILE_DNXHR_444 && (avctx->pix_fmt == AV_PIX_FMT_YUV444P10 ||
395  avctx->pix_fmt == AV_PIX_FMT_GBRP10))) {
396  av_log(avctx, AV_LOG_ERROR,
397  "pixel format is incompatible with DNxHD profile\n");
398  return AVERROR(EINVAL);
399  }
400 
401  if (ctx->profile == FF_PROFILE_DNXHR_HQX && avctx->pix_fmt != AV_PIX_FMT_YUV422P10) {
402  av_log(avctx, AV_LOG_ERROR,
403  "pixel format is incompatible with DNxHR HQX profile\n");
404  return AVERROR(EINVAL);
405  }
406 
407  if ((ctx->profile == FF_PROFILE_DNXHR_LB ||
408  ctx->profile == FF_PROFILE_DNXHR_SQ ||
409  ctx->profile == FF_PROFILE_DNXHR_HQ) && avctx->pix_fmt != AV_PIX_FMT_YUV422P) {
410  av_log(avctx, AV_LOG_ERROR,
411  "pixel format is incompatible with DNxHR LB/SQ/HQ profile\n");
412  return AVERROR(EINVAL);
413  }
414 
415  ctx->is_444 = ctx->profile == FF_PROFILE_DNXHR_444;
416  avctx->profile = ctx->profile;
417  ctx->cid = ff_dnxhd_find_cid(avctx, ctx->bit_depth);
418  if (!ctx->cid) {
419  av_log(avctx, AV_LOG_ERROR,
420  "video parameters incompatible with DNxHD. Valid DNxHD profiles:\n");
422  return AVERROR(EINVAL);
423  }
424  av_log(avctx, AV_LOG_DEBUG, "cid %d\n", ctx->cid);
425 
426  if (ctx->cid >= 1270 && ctx->cid <= 1274)
427  avctx->codec_tag = MKTAG('A','V','d','h');
428 
429  if (avctx->width < 256 || avctx->height < 120) {
430  av_log(avctx, AV_LOG_ERROR,
431  "Input dimensions too small, input must be at least 256x120\n");
432  return AVERROR(EINVAL);
433  }
434 
435  index = ff_dnxhd_get_cid_table(ctx->cid);
436  av_assert0(index >= 0);
437 
439 
440  ctx->m.avctx = avctx;
441  ctx->m.mb_intra = 1;
442  ctx->m.h263_aic = 1;
443 
444  avctx->bits_per_raw_sample = ctx->bit_depth;
445 
446  ff_blockdsp_init(&ctx->bdsp, avctx);
447  ff_fdctdsp_init(&ctx->m.fdsp, avctx);
448  ff_mpv_idct_init(&ctx->m);
449  ff_mpegvideoencdsp_init(&ctx->m.mpvencdsp, avctx);
450  ff_pixblockdsp_init(&ctx->m.pdsp, avctx);
451  ff_dct_encode_init(&ctx->m);
452 
453  if (ctx->profile != FF_PROFILE_DNXHD)
454  ff_videodsp_init(&ctx->m.vdsp, ctx->bit_depth);
455 
456  if (!ctx->m.dct_quantize)
458 
459  if (ctx->is_444 || ctx->profile == FF_PROFILE_DNXHR_HQX) {
462  ctx->block_width_l2 = 4;
463  } else if (ctx->bit_depth == 10) {
466  ctx->block_width_l2 = 4;
467  } else {
469  ctx->block_width_l2 = 3;
470  }
471 
472  if (ARCH_X86)
474 
475  ctx->m.mb_height = (avctx->height + 15) / 16;
476  ctx->m.mb_width = (avctx->width + 15) / 16;
477 
478  if (avctx->flags & AV_CODEC_FLAG_INTERLACED_DCT) {
479  ctx->interlaced = 1;
480  ctx->m.mb_height /= 2;
481  }
482 
483  ctx->m.mb_num = ctx->m.mb_height * ctx->m.mb_width;
484 
485  if (ctx->cid_table->frame_size == DNXHD_VARIABLE) {
487  ctx->m.mb_num);
488  ctx->coding_unit_size = ctx->frame_size;
489  } else {
490  ctx->frame_size = ctx->cid_table->frame_size;
492  }
493 
494  if (ctx->m.mb_height > 68)
495  ctx->data_offset = 0x170 + (ctx->m.mb_height << 2);
496  else
497  ctx->data_offset = 0x280;
498 
499 #if FF_API_QUANT_BIAS
502  ctx->intra_quant_bias = avctx->intra_quant_bias;
504 #endif
505  // XXX tune lbias/cbias
506  if ((ret = dnxhd_init_qmat(ctx, ctx->intra_quant_bias, 0)) < 0)
507  return ret;
508 
509  /* Avid Nitris hardware decoder requires a minimum amount of padding
510  * in the coding unit payload */
511  if (ctx->nitris_compat)
512  ctx->min_padding = 1600;
513 
514  if ((ret = dnxhd_init_vlc(ctx)) < 0)
515  return ret;
516  if ((ret = dnxhd_init_rc(ctx)) < 0)
517  return ret;
518 
519  FF_ALLOCZ_OR_GOTO(ctx->m.avctx, ctx->slice_size,
520  ctx->m.mb_height * sizeof(uint32_t), fail);
521  FF_ALLOCZ_OR_GOTO(ctx->m.avctx, ctx->slice_offs,
522  ctx->m.mb_height * sizeof(uint32_t), fail);
523  FF_ALLOCZ_OR_GOTO(ctx->m.avctx, ctx->mb_bits,
524  ctx->m.mb_num * sizeof(uint16_t), fail);
525  FF_ALLOCZ_OR_GOTO(ctx->m.avctx, ctx->mb_qscale,
526  ctx->m.mb_num * sizeof(uint8_t), fail);
527 
528 #if FF_API_CODED_FRAME
530  avctx->coded_frame->key_frame = 1;
533 #endif
534 
535  if (avctx->thread_count > MAX_THREADS) {
536  av_log(avctx, AV_LOG_ERROR, "too many threads\n");
537  return AVERROR(EINVAL);
538  }
539 
540  if (avctx->qmax <= 1) {
541  av_log(avctx, AV_LOG_ERROR, "qmax must be at least 2\n");
542  return AVERROR(EINVAL);
543  }
544 
545  ctx->thread[0] = ctx;
546  for (i = 1; i < avctx->thread_count; i++) {
547  ctx->thread[i] = av_malloc(sizeof(DNXHDEncContext));
548  memcpy(ctx->thread[i], ctx, sizeof(DNXHDEncContext));
549  }
550 
551  return 0;
552 fail: // for FF_ALLOCZ_OR_GOTO
553  return AVERROR(ENOMEM);
554 }
555 
557 {
558  DNXHDEncContext *ctx = avctx->priv_data;
559 
560  memset(buf, 0, ctx->data_offset);
561 
562  // * write prefix */
563  AV_WB16(buf + 0x02, ctx->data_offset);
564  if (ctx->cid >= 1270 && ctx->cid <= 1274)
565  buf[4] = 0x03;
566  else
567  buf[4] = 0x01;
568 
569  buf[5] = ctx->interlaced ? ctx->cur_field + 2 : 0x01;
570  buf[6] = 0x80; // crc flag off
571  buf[7] = 0xa0; // reserved
572  AV_WB16(buf + 0x18, avctx->height >> ctx->interlaced); // ALPF
573  AV_WB16(buf + 0x1a, avctx->width); // SPL
574  AV_WB16(buf + 0x1d, avctx->height >> ctx->interlaced); // NAL
575 
576  buf[0x21] = ctx->bit_depth == 10 ? 0x58 : 0x38;
577  buf[0x22] = 0x88 + (ctx->interlaced << 2);
578  AV_WB32(buf + 0x28, ctx->cid); // CID
579  buf[0x2c] = (!ctx->interlaced << 7) | (ctx->is_444 << 6) | (avctx->pix_fmt == AV_PIX_FMT_YUV444P10);
580 
581  buf[0x5f] = 0x01; // UDL
582 
583  buf[0x167] = 0x02; // reserved
584  AV_WB16(buf + 0x16a, ctx->m.mb_height * 4 + 4); // MSIPS
585  buf[0x16d] = ctx->m.mb_height; // Ns
586  buf[0x16f] = 0x10; // reserved
587 
588  ctx->msip = buf + 0x170;
589  return 0;
590 }
591 
593 {
594  int nbits;
595  if (diff < 0) {
596  nbits = av_log2_16bit(-2 * diff);
597  diff--;
598  } else {
599  nbits = av_log2_16bit(2 * diff);
600  }
601  put_bits(&ctx->m.pb, ctx->cid_table->dc_bits[nbits] + nbits,
602  (ctx->cid_table->dc_codes[nbits] << nbits) +
603  av_mod_uintp2(diff, nbits));
604 }
605 
606 static av_always_inline
608  int last_index, int n)
609 {
610  int last_non_zero = 0;
611  int slevel, i, j;
612 
613  dnxhd_encode_dc(ctx, block[0] - ctx->m.last_dc[n]);
614  ctx->m.last_dc[n] = block[0];
615 
616  for (i = 1; i <= last_index; i++) {
617  j = ctx->m.intra_scantable.permutated[i];
618  slevel = block[j];
619  if (slevel) {
620  int run_level = i - last_non_zero - 1;
621  int rlevel = (slevel << 1) | !!run_level;
622  put_bits(&ctx->m.pb, ctx->vlc_bits[rlevel], ctx->vlc_codes[rlevel]);
623  if (run_level)
624  put_bits(&ctx->m.pb, ctx->run_bits[run_level],
625  ctx->run_codes[run_level]);
626  last_non_zero = i;
627  }
628  }
629  put_bits(&ctx->m.pb, ctx->vlc_bits[0], ctx->vlc_codes[0]); // EOB
630 }
631 
632 static av_always_inline
634  int qscale, int last_index)
635 {
636  const uint8_t *weight_matrix;
637  int level;
638  int i;
639 
640  if (ctx->is_444) {
641  weight_matrix = ((n % 6) < 2) ? ctx->cid_table->luma_weight
642  : ctx->cid_table->chroma_weight;
643  } else {
644  weight_matrix = (n & 2) ? ctx->cid_table->chroma_weight
645  : ctx->cid_table->luma_weight;
646  }
647 
648  for (i = 1; i <= last_index; i++) {
649  int j = ctx->m.intra_scantable.permutated[i];
650  level = block[j];
651  if (level) {
652  if (level < 0) {
653  level = (1 - 2 * level) * qscale * weight_matrix[i];
654  if (ctx->bit_depth == 10) {
655  if (weight_matrix[i] != 8)
656  level += 8;
657  level >>= 4;
658  } else {
659  if (weight_matrix[i] != 32)
660  level += 32;
661  level >>= 6;
662  }
663  level = -level;
664  } else {
665  level = (2 * level + 1) * qscale * weight_matrix[i];
666  if (ctx->bit_depth == 10) {
667  if (weight_matrix[i] != 8)
668  level += 8;
669  level >>= 4;
670  } else {
671  if (weight_matrix[i] != 32)
672  level += 32;
673  level >>= 6;
674  }
675  }
676  block[j] = level;
677  }
678  }
679 }
680 
681 static av_always_inline int dnxhd_ssd_block(int16_t *qblock, int16_t *block)
682 {
683  int score = 0;
684  int i;
685  for (i = 0; i < 64; i++)
686  score += (block[i] - qblock[i]) * (block[i] - qblock[i]);
687  return score;
688 }
689 
690 static av_always_inline
691 int dnxhd_calc_ac_bits(DNXHDEncContext *ctx, int16_t *block, int last_index)
692 {
693  int last_non_zero = 0;
694  int bits = 0;
695  int i, j, level;
696  for (i = 1; i <= last_index; i++) {
697  j = ctx->m.intra_scantable.permutated[i];
698  level = block[j];
699  if (level) {
700  int run_level = i - last_non_zero - 1;
701  bits += ctx->vlc_bits[(level << 1) |
702  !!run_level] + ctx->run_bits[run_level];
703  last_non_zero = i;
704  }
705  }
706  return bits;
707 }
708 
709 static av_always_inline
710 void dnxhd_get_blocks(DNXHDEncContext *ctx, int mb_x, int mb_y)
711 {
712  const int bs = ctx->block_width_l2;
713  const int bw = 1 << bs;
714  int dct_y_offset = ctx->dct_y_offset;
715  int dct_uv_offset = ctx->dct_uv_offset;
716  int linesize = ctx->m.linesize;
717  int uvlinesize = ctx->m.uvlinesize;
718  const uint8_t *ptr_y = ctx->thread[0]->src[0] +
719  ((mb_y << 4) * ctx->m.linesize) + (mb_x << bs + 1);
720  const uint8_t *ptr_u = ctx->thread[0]->src[1] +
721  ((mb_y << 4) * ctx->m.uvlinesize) + (mb_x << bs + ctx->is_444);
722  const uint8_t *ptr_v = ctx->thread[0]->src[2] +
723  ((mb_y << 4) * ctx->m.uvlinesize) + (mb_x << bs + ctx->is_444);
724  PixblockDSPContext *pdsp = &ctx->m.pdsp;
725  VideoDSPContext *vdsp = &ctx->m.vdsp;
726 
727  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 = (y_w + 1) / 2;
732  int uv_h = y_h;
733  linesize = 16;
734  uvlinesize = 8;
735 
736  vdsp->emulated_edge_mc(&ctx->edge_buf_y[0], ptr_y,
737  linesize, ctx->m.linesize,
738  linesize, 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, 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, 16,
747  0, 0, uv_w, uv_h);
748 
749  dct_y_offset = bw * linesize;
750  dct_uv_offset = bw * uvlinesize;
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  } else if (ctx->bit_depth == 10 && vdsp->emulated_edge_mc && ((mb_x << 3) + 8 > ctx->m.avctx->width ||
755  (mb_y << 3) + 8 > ctx->m.avctx->height)) {
756  int y_w = ctx->m.avctx->width - (mb_x << 3);
757  int y_h = ctx->m.avctx->height - (mb_y << 3);
758  int uv_w = ctx->is_444 ? y_w : (y_w + 1) / 2;
759  int uv_h = y_h;
760  linesize = 16;
761  uvlinesize = 8 + 8 * ctx->is_444;
762 
763  vdsp->emulated_edge_mc(&ctx->edge_buf_y[0], ptr_y,
764  linesize, ctx->m.linesize,
765  linesize / 2, 16,
766  0, 0, y_w, y_h);
767  vdsp->emulated_edge_mc(&ctx->edge_buf_uv[0][0], ptr_u,
768  uvlinesize, ctx->m.uvlinesize,
769  uvlinesize / 2, 16,
770  0, 0, uv_w, uv_h);
771  vdsp->emulated_edge_mc(&ctx->edge_buf_uv[1][0], ptr_v,
772  uvlinesize, ctx->m.uvlinesize,
773  uvlinesize / 2, 16,
774  0, 0, uv_w, uv_h);
775 
776  dct_y_offset = bw * linesize;
777  dct_uv_offset = bw * uvlinesize;
778  ptr_y = &ctx->edge_buf_y[0];
779  ptr_u = &ctx->edge_buf_uv[0][0];
780  ptr_v = &ctx->edge_buf_uv[1][0];
781  }
782 
783  if (!ctx->is_444) {
784  pdsp->get_pixels(ctx->blocks[0], ptr_y, linesize);
785  pdsp->get_pixels(ctx->blocks[1], ptr_y + bw, linesize);
786  pdsp->get_pixels(ctx->blocks[2], ptr_u, uvlinesize);
787  pdsp->get_pixels(ctx->blocks[3], ptr_v, uvlinesize);
788 
789  if (mb_y + 1 == ctx->m.mb_height && ctx->m.avctx->height == 1080) {
790  if (ctx->interlaced) {
791  ctx->get_pixels_8x4_sym(ctx->blocks[4],
792  ptr_y + dct_y_offset,
793  linesize);
794  ctx->get_pixels_8x4_sym(ctx->blocks[5],
795  ptr_y + dct_y_offset + bw,
796  linesize);
797  ctx->get_pixels_8x4_sym(ctx->blocks[6],
798  ptr_u + dct_uv_offset,
799  uvlinesize);
800  ctx->get_pixels_8x4_sym(ctx->blocks[7],
801  ptr_v + dct_uv_offset,
802  uvlinesize);
803  } else {
804  ctx->bdsp.clear_block(ctx->blocks[4]);
805  ctx->bdsp.clear_block(ctx->blocks[5]);
806  ctx->bdsp.clear_block(ctx->blocks[6]);
807  ctx->bdsp.clear_block(ctx->blocks[7]);
808  }
809  } else {
810  pdsp->get_pixels(ctx->blocks[4],
811  ptr_y + dct_y_offset, linesize);
812  pdsp->get_pixels(ctx->blocks[5],
813  ptr_y + dct_y_offset + bw, linesize);
814  pdsp->get_pixels(ctx->blocks[6],
815  ptr_u + dct_uv_offset, uvlinesize);
816  pdsp->get_pixels(ctx->blocks[7],
817  ptr_v + dct_uv_offset, uvlinesize);
818  }
819  } else {
820  pdsp->get_pixels(ctx->blocks[0], ptr_y, linesize);
821  pdsp->get_pixels(ctx->blocks[1], ptr_y + bw, linesize);
822  pdsp->get_pixels(ctx->blocks[6], ptr_y + dct_y_offset, linesize);
823  pdsp->get_pixels(ctx->blocks[7], ptr_y + dct_y_offset + bw, linesize);
824 
825  pdsp->get_pixels(ctx->blocks[2], ptr_u, uvlinesize);
826  pdsp->get_pixels(ctx->blocks[3], ptr_u + bw, uvlinesize);
827  pdsp->get_pixels(ctx->blocks[8], ptr_u + dct_uv_offset, uvlinesize);
828  pdsp->get_pixels(ctx->blocks[9], ptr_u + dct_uv_offset + bw, uvlinesize);
829 
830  pdsp->get_pixels(ctx->blocks[4], ptr_v, uvlinesize);
831  pdsp->get_pixels(ctx->blocks[5], ptr_v + bw, uvlinesize);
832  pdsp->get_pixels(ctx->blocks[10], ptr_v + dct_uv_offset, uvlinesize);
833  pdsp->get_pixels(ctx->blocks[11], ptr_v + dct_uv_offset + bw, uvlinesize);
834  }
835 }
836 
837 static av_always_inline
839 {
840  int x;
841 
842  if (ctx->is_444) {
843  x = (i >> 1) % 3;
844  } else {
845  const static uint8_t component[8]={0,0,1,2,0,0,1,2};
846  x = component[i];
847  }
848  return x;
849 }
850 
852  int jobnr, int threadnr)
853 {
854  DNXHDEncContext *ctx = avctx->priv_data;
855  int mb_y = jobnr, mb_x;
856  int qscale = ctx->qscale;
857  LOCAL_ALIGNED_16(int16_t, block, [64]);
858  ctx = ctx->thread[threadnr];
859 
860  ctx->m.last_dc[0] =
861  ctx->m.last_dc[1] =
862  ctx->m.last_dc[2] = 1 << (ctx->bit_depth + 2);
863 
864  for (mb_x = 0; mb_x < ctx->m.mb_width; mb_x++) {
865  unsigned mb = mb_y * ctx->m.mb_width + mb_x;
866  int ssd = 0;
867  int ac_bits = 0;
868  int dc_bits = 0;
869  int i;
870 
871  dnxhd_get_blocks(ctx, mb_x, mb_y);
872 
873  for (i = 0; i < 8 + 4 * ctx->is_444; i++) {
874  int16_t *src_block = ctx->blocks[i];
875  int overflow, nbits, diff, last_index;
876  int n = dnxhd_switch_matrix(ctx, i);
877 
878  memcpy(block, src_block, 64 * sizeof(*block));
879  last_index = ctx->m.dct_quantize(&ctx->m, block,
880  ctx->is_444 ? 4 * (n > 0): 4 & (2*i),
881  qscale, &overflow);
882  ac_bits += dnxhd_calc_ac_bits(ctx, block, last_index);
883 
884  diff = block[0] - ctx->m.last_dc[n];
885  if (diff < 0)
886  nbits = av_log2_16bit(-2 * diff);
887  else
888  nbits = av_log2_16bit(2 * diff);
889 
890  av_assert1(nbits < ctx->bit_depth + 4);
891  dc_bits += ctx->cid_table->dc_bits[nbits] + nbits;
892 
893  ctx->m.last_dc[n] = block[0];
894 
895  if (avctx->mb_decision == FF_MB_DECISION_RD || !RC_VARIANCE) {
896  dnxhd_unquantize_c(ctx, block, i, qscale, last_index);
897  ctx->m.idsp.idct(block);
898  ssd += dnxhd_ssd_block(block, src_block);
899  }
900  }
901  ctx->mb_rc[(qscale * ctx->m.mb_num) + mb].ssd = ssd;
902  ctx->mb_rc[(qscale * ctx->m.mb_num) + mb].bits = ac_bits + dc_bits + 12 +
903  (1 + ctx->is_444) * 8 * ctx->vlc_bits[0];
904  }
905  return 0;
906 }
907 
909  int jobnr, int threadnr)
910 {
911  DNXHDEncContext *ctx = avctx->priv_data;
912  int mb_y = jobnr, mb_x;
913  ctx = ctx->thread[threadnr];
914  init_put_bits(&ctx->m.pb, (uint8_t *)arg + ctx->data_offset + ctx->slice_offs[jobnr],
915  ctx->slice_size[jobnr]);
916 
917  ctx->m.last_dc[0] =
918  ctx->m.last_dc[1] =
919  ctx->m.last_dc[2] = 1 << (ctx->bit_depth + 2);
920  for (mb_x = 0; mb_x < ctx->m.mb_width; mb_x++) {
921  unsigned mb = mb_y * ctx->m.mb_width + mb_x;
922  int qscale = ctx->mb_qscale[mb];
923  int i;
924 
925  put_bits(&ctx->m.pb, 11, qscale);
926  put_bits(&ctx->m.pb, 1, avctx->pix_fmt == AV_PIX_FMT_YUV444P10);
927 
928  dnxhd_get_blocks(ctx, mb_x, mb_y);
929 
930  for (i = 0; i < 8 + 4 * ctx->is_444; i++) {
931  int16_t *block = ctx->blocks[i];
932  int overflow, n = dnxhd_switch_matrix(ctx, i);
933  int last_index = ctx->m.dct_quantize(&ctx->m, block,
934  ctx->is_444 ? (((i >> 1) % 3) < 1 ? 0 : 4): 4 & (2*i),
935  qscale, &overflow);
936  // START_TIMER;
937  dnxhd_encode_block(ctx, block, last_index, n);
938  // STOP_TIMER("encode_block");
939  }
940  }
941  if (put_bits_count(&ctx->m.pb) & 31)
942  put_bits(&ctx->m.pb, 32 - (put_bits_count(&ctx->m.pb) & 31), 0);
943  flush_put_bits(&ctx->m.pb);
944  return 0;
945 }
946 
948 {
949  int mb_y, mb_x;
950  int offset = 0;
951  for (mb_y = 0; mb_y < ctx->m.mb_height; mb_y++) {
952  int thread_size;
953  ctx->slice_offs[mb_y] = offset;
954  ctx->slice_size[mb_y] = 0;
955  for (mb_x = 0; mb_x < ctx->m.mb_width; mb_x++) {
956  unsigned mb = mb_y * ctx->m.mb_width + mb_x;
957  ctx->slice_size[mb_y] += ctx->mb_bits[mb];
958  }
959  ctx->slice_size[mb_y] = (ctx->slice_size[mb_y] + 31) & ~31;
960  ctx->slice_size[mb_y] >>= 3;
961  thread_size = ctx->slice_size[mb_y];
962  offset += thread_size;
963  }
964 }
965 
967  int jobnr, int threadnr)
968 {
969  DNXHDEncContext *ctx = avctx->priv_data;
970  int mb_y = jobnr, mb_x, x, y;
971  int partial_last_row = (mb_y == ctx->m.mb_height - 1) &&
972  ((avctx->height >> ctx->interlaced) & 0xF);
973 
974  ctx = ctx->thread[threadnr];
975  if (ctx->bit_depth == 8) {
976  uint8_t *pix = ctx->thread[0]->src[0] + ((mb_y << 4) * ctx->m.linesize);
977  for (mb_x = 0; mb_x < ctx->m.mb_width; ++mb_x, pix += 16) {
978  unsigned mb = mb_y * ctx->m.mb_width + mb_x;
979  int sum;
980  int varc;
981 
982  if (!partial_last_row && mb_x * 16 <= avctx->width - 16 && (avctx->width % 16) == 0) {
983  sum = ctx->m.mpvencdsp.pix_sum(pix, ctx->m.linesize);
984  varc = ctx->m.mpvencdsp.pix_norm1(pix, ctx->m.linesize);
985  } else {
986  int bw = FFMIN(avctx->width - 16 * mb_x, 16);
987  int bh = FFMIN((avctx->height >> ctx->interlaced) - 16 * mb_y, 16);
988  sum = varc = 0;
989  for (y = 0; y < bh; y++) {
990  for (x = 0; x < bw; x++) {
991  uint8_t val = pix[x + y * ctx->m.linesize];
992  sum += val;
993  varc += val * val;
994  }
995  }
996  }
997  varc = (varc - (((unsigned) sum * sum) >> 8) + 128) >> 8;
998 
999  ctx->mb_cmp[mb].value = varc;
1000  ctx->mb_cmp[mb].mb = mb;
1001  }
1002  } else { // 10-bit
1003  const int linesize = ctx->m.linesize >> 1;
1004  for (mb_x = 0; mb_x < ctx->m.mb_width; ++mb_x) {
1005  uint16_t *pix = (uint16_t *)ctx->thread[0]->src[0] +
1006  ((mb_y << 4) * linesize) + (mb_x << 4);
1007  unsigned mb = mb_y * ctx->m.mb_width + mb_x;
1008  int sum = 0;
1009  int sqsum = 0;
1010  int bw = FFMIN(avctx->width - 16 * mb_x, 16);
1011  int bh = FFMIN((avctx->height >> ctx->interlaced) - 16 * mb_y, 16);
1012  int mean, sqmean;
1013  int i, j;
1014  // Macroblocks are 16x16 pixels, unlike DCT blocks which are 8x8.
1015  for (i = 0; i < bh; ++i) {
1016  for (j = 0; j < bw; ++j) {
1017  // Turn 16-bit pixels into 10-bit ones.
1018  const int sample = (unsigned) pix[j] >> 6;
1019  sum += sample;
1020  sqsum += sample * sample;
1021  // 2^10 * 2^10 * 16 * 16 = 2^28, which is less than INT_MAX
1022  }
1023  pix += linesize;
1024  }
1025  mean = sum >> 8; // 16*16 == 2^8
1026  sqmean = sqsum >> 8;
1027  ctx->mb_cmp[mb].value = sqmean - mean * mean;
1028  ctx->mb_cmp[mb].mb = mb;
1029  }
1030  }
1031  return 0;
1032 }
1033 
1035 {
1036  int lambda, up_step, down_step;
1037  int last_lower = INT_MAX, last_higher = 0;
1038  int x, y, q;
1039 
1040  for (q = 1; q < avctx->qmax; q++) {
1041  ctx->qscale = q;
1042  avctx->execute2(avctx, dnxhd_calc_bits_thread,
1043  NULL, NULL, ctx->m.mb_height);
1044  }
1045  up_step = down_step = 2 << LAMBDA_FRAC_BITS;
1046  lambda = ctx->lambda;
1047 
1048  for (;;) {
1049  int bits = 0;
1050  int end = 0;
1051  if (lambda == last_higher) {
1052  lambda++;
1053  end = 1; // need to set final qscales/bits
1054  }
1055  for (y = 0; y < ctx->m.mb_height; y++) {
1056  for (x = 0; x < ctx->m.mb_width; x++) {
1057  unsigned min = UINT_MAX;
1058  int qscale = 1;
1059  int mb = y * ctx->m.mb_width + x;
1060  int rc = 0;
1061  for (q = 1; q < avctx->qmax; q++) {
1062  int i = (q*ctx->m.mb_num) + mb;
1063  unsigned score = ctx->mb_rc[i].bits * lambda +
1064  ((unsigned) ctx->mb_rc[i].ssd << LAMBDA_FRAC_BITS);
1065  if (score < min) {
1066  min = score;
1067  qscale = q;
1068  rc = i;
1069  }
1070  }
1071  bits += ctx->mb_rc[rc].bits;
1072  ctx->mb_qscale[mb] = qscale;
1073  ctx->mb_bits[mb] = ctx->mb_rc[rc].bits;
1074  }
1075  bits = (bits + 31) & ~31; // padding
1076  if (bits > ctx->frame_bits)
1077  break;
1078  }
1079  if (end) {
1080  if (bits > ctx->frame_bits)
1081  return AVERROR(EINVAL);
1082  break;
1083  }
1084  if (bits < ctx->frame_bits) {
1085  last_lower = FFMIN(lambda, last_lower);
1086  if (last_higher != 0)
1087  lambda = (lambda+last_higher)>>1;
1088  else
1089  lambda -= down_step;
1090  down_step = FFMIN((int64_t)down_step*5, INT_MAX);
1091  up_step = 1<<LAMBDA_FRAC_BITS;
1092  lambda = FFMAX(1, lambda);
1093  if (lambda == last_lower)
1094  break;
1095  } else {
1096  last_higher = FFMAX(lambda, last_higher);
1097  if (last_lower != INT_MAX)
1098  lambda = (lambda+last_lower)>>1;
1099  else if ((int64_t)lambda + up_step > INT_MAX)
1100  return AVERROR(EINVAL);
1101  else
1102  lambda += up_step;
1103  up_step = FFMIN((int64_t)up_step*5, INT_MAX);
1104  down_step = 1<<LAMBDA_FRAC_BITS;
1105  }
1106  }
1107  ctx->lambda = lambda;
1108  return 0;
1109 }
1110 
1112 {
1113  int bits = 0;
1114  int up_step = 1;
1115  int down_step = 1;
1116  int last_higher = 0;
1117  int last_lower = INT_MAX;
1118  int qscale;
1119  int x, y;
1120 
1121  qscale = ctx->qscale;
1122  for (;;) {
1123  bits = 0;
1124  ctx->qscale = qscale;
1125  // XXX avoid recalculating bits
1127  NULL, NULL, ctx->m.mb_height);
1128  for (y = 0; y < ctx->m.mb_height; y++) {
1129  for (x = 0; x < ctx->m.mb_width; x++)
1130  bits += ctx->mb_rc[(qscale*ctx->m.mb_num) + (y*ctx->m.mb_width+x)].bits;
1131  bits = (bits+31)&~31; // padding
1132  if (bits > ctx->frame_bits)
1133  break;
1134  }
1135  if (bits < ctx->frame_bits) {
1136  if (qscale == 1)
1137  return 1;
1138  if (last_higher == qscale - 1) {
1139  qscale = last_higher;
1140  break;
1141  }
1142  last_lower = FFMIN(qscale, last_lower);
1143  if (last_higher != 0)
1144  qscale = (qscale + last_higher) >> 1;
1145  else
1146  qscale -= down_step++;
1147  if (qscale < 1)
1148  qscale = 1;
1149  up_step = 1;
1150  } else {
1151  if (last_lower == qscale + 1)
1152  break;
1153  last_higher = FFMAX(qscale, last_higher);
1154  if (last_lower != INT_MAX)
1155  qscale = (qscale + last_lower) >> 1;
1156  else
1157  qscale += up_step++;
1158  down_step = 1;
1159  if (qscale >= ctx->m.avctx->qmax)
1160  return AVERROR(EINVAL);
1161  }
1162  }
1163  ctx->qscale = qscale;
1164  return 0;
1165 }
1166 
1167 #define BUCKET_BITS 8
1168 #define RADIX_PASSES 4
1169 #define NBUCKETS (1 << BUCKET_BITS)
1170 
1171 static inline int get_bucket(int value, int shift)
1172 {
1173  value >>= shift;
1174  value &= NBUCKETS - 1;
1175  return NBUCKETS - 1 - value;
1176 }
1177 
1178 static void radix_count(const RCCMPEntry *data, int size,
1179  int buckets[RADIX_PASSES][NBUCKETS])
1180 {
1181  int i, j;
1182  memset(buckets, 0, sizeof(buckets[0][0]) * RADIX_PASSES * NBUCKETS);
1183  for (i = 0; i < size; i++) {
1184  int v = data[i].value;
1185  for (j = 0; j < RADIX_PASSES; j++) {
1186  buckets[j][get_bucket(v, 0)]++;
1187  v >>= BUCKET_BITS;
1188  }
1189  av_assert1(!v);
1190  }
1191  for (j = 0; j < RADIX_PASSES; j++) {
1192  int offset = size;
1193  for (i = NBUCKETS - 1; i >= 0; i--)
1194  buckets[j][i] = offset -= buckets[j][i];
1195  av_assert1(!buckets[j][0]);
1196  }
1197 }
1198 
1199 static void radix_sort_pass(RCCMPEntry *dst, const RCCMPEntry *data,
1200  int size, int buckets[NBUCKETS], int pass)
1201 {
1202  int shift = pass * BUCKET_BITS;
1203  int i;
1204  for (i = 0; i < size; i++) {
1205  int v = get_bucket(data[i].value, shift);
1206  int pos = buckets[v]++;
1207  dst[pos] = data[i];
1208  }
1209 }
1210 
1212 {
1213  int buckets[RADIX_PASSES][NBUCKETS];
1214  radix_count(data, size, buckets);
1215  radix_sort_pass(tmp, data, size, buckets[0], 0);
1216  radix_sort_pass(data, tmp, size, buckets[1], 1);
1217  if (buckets[2][NBUCKETS - 1] || buckets[3][NBUCKETS - 1]) {
1218  radix_sort_pass(tmp, data, size, buckets[2], 2);
1219  radix_sort_pass(data, tmp, size, buckets[3], 3);
1220  }
1221 }
1222 
1224 {
1225  int max_bits = 0;
1226  int ret, x, y;
1227  if ((ret = dnxhd_find_qscale(ctx)) < 0)
1228  return ret;
1229  for (y = 0; y < ctx->m.mb_height; y++) {
1230  for (x = 0; x < ctx->m.mb_width; x++) {
1231  int mb = y * ctx->m.mb_width + x;
1232  int rc = (ctx->qscale * ctx->m.mb_num ) + mb;
1233  int delta_bits;
1234  ctx->mb_qscale[mb] = ctx->qscale;
1235  ctx->mb_bits[mb] = ctx->mb_rc[rc].bits;
1236  max_bits += ctx->mb_rc[rc].bits;
1237  if (!RC_VARIANCE) {
1238  delta_bits = ctx->mb_rc[rc].bits -
1239  ctx->mb_rc[rc + ctx->m.mb_num].bits;
1240  ctx->mb_cmp[mb].mb = mb;
1241  ctx->mb_cmp[mb].value =
1242  delta_bits ? ((ctx->mb_rc[rc].ssd -
1243  ctx->mb_rc[rc + ctx->m.mb_num].ssd) * 100) /
1244  delta_bits
1245  : INT_MIN; // avoid increasing qscale
1246  }
1247  }
1248  max_bits += 31; // worst padding
1249  }
1250  if (!ret) {
1251  if (RC_VARIANCE)
1252  avctx->execute2(avctx, dnxhd_mb_var_thread,
1253  NULL, NULL, ctx->m.mb_height);
1254  radix_sort(ctx->mb_cmp, ctx->mb_cmp_tmp, ctx->m.mb_num);
1255  for (x = 0; x < ctx->m.mb_num && max_bits > ctx->frame_bits; x++) {
1256  int mb = ctx->mb_cmp[x].mb;
1257  int rc = (ctx->qscale * ctx->m.mb_num ) + mb;
1258  max_bits -= ctx->mb_rc[rc].bits -
1259  ctx->mb_rc[rc + ctx->m.mb_num].bits;
1260  ctx->mb_qscale[mb] = ctx->qscale + 1;
1261  ctx->mb_bits[mb] = ctx->mb_rc[rc + ctx->m.mb_num].bits;
1262  }
1263  }
1264  return 0;
1265 }
1266 
1268 {
1269  int i;
1270 
1271  for (i = 0; i < ctx->m.avctx->thread_count; i++) {
1272  ctx->thread[i]->m.linesize = frame->linesize[0] << ctx->interlaced;
1273  ctx->thread[i]->m.uvlinesize = frame->linesize[1] << ctx->interlaced;
1274  ctx->thread[i]->dct_y_offset = ctx->m.linesize *8;
1275  ctx->thread[i]->dct_uv_offset = ctx->m.uvlinesize*8;
1276  }
1277 
1278 #if FF_API_CODED_FRAME
1282 #endif
1283  ctx->cur_field = frame->interlaced_frame && !frame->top_field_first;
1284 }
1285 
1287  const AVFrame *frame, int *got_packet)
1288 {
1289  DNXHDEncContext *ctx = avctx->priv_data;
1290  int first_field = 1;
1291  int offset, i, ret;
1292  uint8_t *buf;
1293 
1294  if ((ret = ff_alloc_packet2(avctx, pkt, ctx->frame_size, 0)) < 0)
1295  return ret;
1296  buf = pkt->data;
1297 
1298  dnxhd_load_picture(ctx, frame);
1299 
1300 encode_coding_unit:
1301  for (i = 0; i < 3; i++) {
1302  ctx->src[i] = frame->data[i];
1303  if (ctx->interlaced && ctx->cur_field)
1304  ctx->src[i] += frame->linesize[i];
1305  }
1306 
1307  dnxhd_write_header(avctx, buf);
1308 
1309  if (avctx->mb_decision == FF_MB_DECISION_RD)
1310  ret = dnxhd_encode_rdo(avctx, ctx);
1311  else
1312  ret = dnxhd_encode_fast(avctx, ctx);
1313  if (ret < 0) {
1314  av_log(avctx, AV_LOG_ERROR,
1315  "picture could not fit ratecontrol constraints, increase qmax\n");
1316  return ret;
1317  }
1318 
1320 
1321  offset = 0;
1322  for (i = 0; i < ctx->m.mb_height; i++) {
1323  AV_WB32(ctx->msip + i * 4, offset);
1324  offset += ctx->slice_size[i];
1325  av_assert1(!(ctx->slice_size[i] & 3));
1326  }
1327 
1328  avctx->execute2(avctx, dnxhd_encode_thread, buf, NULL, ctx->m.mb_height);
1329 
1330  av_assert1(ctx->data_offset + offset + 4 <= ctx->coding_unit_size);
1331  memset(buf + ctx->data_offset + offset, 0,
1332  ctx->coding_unit_size - 4 - offset - ctx->data_offset);
1333 
1334  AV_WB32(buf + ctx->coding_unit_size - 4, 0x600DC0DE); // EOF
1335 
1336  if (ctx->interlaced && first_field) {
1337  first_field = 0;
1338  ctx->cur_field ^= 1;
1339  buf += ctx->coding_unit_size;
1340  goto encode_coding_unit;
1341  }
1342 
1343 #if FF_API_CODED_FRAME
1345  avctx->coded_frame->quality = ctx->qscale * FF_QP2LAMBDA;
1347 #endif
1348 
1350 
1351  pkt->flags |= AV_PKT_FLAG_KEY;
1352  *got_packet = 1;
1353  return 0;
1354 }
1355 
1357 {
1358  DNXHDEncContext *ctx = avctx->priv_data;
1359  int max_level = 1 << (ctx->bit_depth + 2);
1360  int i;
1361 
1362  av_free(ctx->vlc_codes - max_level * 2);
1363  av_free(ctx->vlc_bits - max_level * 2);
1364  av_freep(&ctx->run_codes);
1365  av_freep(&ctx->run_bits);
1366 
1367  av_freep(&ctx->mb_bits);
1368  av_freep(&ctx->mb_qscale);
1369  av_freep(&ctx->mb_rc);
1370  av_freep(&ctx->mb_cmp);
1371  av_freep(&ctx->mb_cmp_tmp);
1372  av_freep(&ctx->slice_size);
1373  av_freep(&ctx->slice_offs);
1374 
1375  av_freep(&ctx->qmatrix_c);
1376  av_freep(&ctx->qmatrix_l);
1377  av_freep(&ctx->qmatrix_c16);
1378  av_freep(&ctx->qmatrix_l16);
1379 
1380  for (i = 1; i < avctx->thread_count; i++)
1381  av_freep(&ctx->thread[i]);
1382 
1383  return 0;
1384 }
1385 
1386 static const AVCodecDefault dnxhd_defaults[] = {
1387  { "qmax", "1024" }, /* Maximum quantization scale factor allowed for VC-3 */
1388  { NULL },
1389 };
1390 
1392  .name = "dnxhd",
1393  .long_name = NULL_IF_CONFIG_SMALL("VC3/DNxHD"),
1394  .type = AVMEDIA_TYPE_VIDEO,
1395  .id = AV_CODEC_ID_DNXHD,
1396  .priv_data_size = sizeof(DNXHDEncContext),
1398  .encode2 = dnxhd_encode_picture,
1399  .close = dnxhd_encode_end,
1400  .capabilities = AV_CODEC_CAP_SLICE_THREADS,
1401  .pix_fmts = (const enum AVPixelFormat[]) {
1407  },
1408  .priv_class = &dnxhd_class,
1409  .defaults = dnxhd_defaults,
1411 };
#define FF_PROFILE_DNXHD
Definition: avcodec.h:3250
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:681
#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:227
static void radix_count(const RCCMPEntry *data, int size, int buckets[RADIX_PASSES][NBUCKETS])
Definition: dnxhdenc.c:1178
#define NULL
Definition: coverity.c:32
const char const char void * val
Definition: avisynth_c.h:771
attribute_deprecated int intra_quant_bias
Definition: avcodec.h:2261
static av_always_inline void dnxhd_get_blocks(DNXHDEncContext *ctx, int mb_x, int mb_y)
Definition: dnxhdenc.c:710
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:187
#define FF_ALLOCZ_ARRAY_OR_GOTO(ctx, p, nelem, elsize, label)
Definition: internal.h:160
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:668
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:909
#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:206
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
int num
Numerator.
Definition: rational.h:59
av_cold void ff_blockdsp_init(BlockDSPContext *c, AVCodecContext *avctx)
Definition: blockdsp.c:60
#define AV_PIX_FMT_GBRP10
Definition: pixfmt.h:367
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:1960
unsigned dct_uv_offset
Definition: dnxhdenc.h:63
static av_always_inline void dnxhd_encode_dc(DNXHDEncContext *ctx, int diff)
Definition: dnxhdenc.c:592
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:1139
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:3133
int mb_num
number of MBs of a picture
Definition: mpegvideo.h:130
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:3235
#define sample
int av_log2_16bit(unsigned v)
Definition: intmath.c:31
AVCodec.
Definition: avcodec.h:3681
static av_always_inline int dnxhd_calc_ac_bits(DNXHDEncContext *ctx, int16_t *block, int last_index)
Definition: dnxhdenc.c:691
int h263_aic
Advanced INTRA Coding (AIC)
Definition: mpegvideo.h:84
Macro definitions for various function/variable attributes.
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
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 bits
Definition: crc.c:296
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:1171
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:1223
uint32_t * slice_size
Definition: dnxhdenc.h:55
int(* qmatrix_c)[64]
Definition: dnxhdenc.h:81
#define RADIX_PASSES
Definition: dnxhdenc.c:1168
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
static int dnxhd_get_hr_frame_size(const CIDEntry *profile, int mb_num)
Definition: dnxhdenc.c:365
uint8_t * data
Definition: avcodec.h:1657
static void radix_sort_pass(RCCMPEntry *dst, const RCCMPEntry *data, int size, int buckets[NBUCKETS], int pass)
Definition: dnxhdenc.c:1199
const uint8_t * run_bits
Definition: dnxhddata.h:56
#define BUCKET_BITS
Definition: dnxhdenc.c:1167
const uint8_t * scantable
Definition: idctdsp.h:32
int interlaced_frame
The content of the picture is interlaced.
Definition: frame.h:325
av_cold void ff_mpv_idct_init(MpegEncContext *s)
Definition: mpegvideo.c:330
int mb_height
number of MBs horizontally & vertically
Definition: mpegvideo.h:126
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:410
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:1356
#define AV_PKT_FLAG_KEY
The packet contains a keyframe.
Definition: avcodec.h:1689
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:838
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:851
#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:1090
int last_dc[3]
last DC values for MPEG-1
Definition: mpegvideo.h:182
static int dnxhd_find_qscale(DNXHDEncContext *ctx)
Definition: dnxhdenc.c:1111
AVRational packet_scale
Definition: dnxhddata.h:59
#define FF_SIGNBIT(x)
Definition: internal.h:75
av_default_item_name
#define AVERROR(e)
Definition: error.h:43
#define RC_VARIANCE
Definition: dnxhdenc.c:42
int qmax
maximum quantizer
Definition: avcodec.h:2682
#define NULL_IF_CONFIG_SMALL(x)
Return NULL if CONFIG_SMALL is true, otherwise the argument without modification. ...
Definition: internal.h:179
AVCodec ff_dnxhd_encoder
Definition: dnxhdenc.c:1391
static av_always_inline void dnxhd_unquantize_c(DNXHDEncContext *ctx, int16_t *block, int n, int qscale, int last_index)
Definition: dnxhdenc.c:633
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:231
const char * arg
Definition: jacosubdec.c:66
int flags
AV_CODEC_FLAG_*.
Definition: avcodec.h:1827
static int dnxhd_write_header(AVCodecContext *avctx, uint8_t *buf)
Definition: dnxhdenc.c:556
const uint8_t * dc_codes
Definition: dnxhddata.h:52
MpegvideoEncDSPContext mpvencdsp
Definition: mpegvideo.h:230
const char * name
Name of the codec implementation.
Definition: avcodec.h:3688
#define AV_PIX_FMT_YUV444P10
Definition: pixfmt.h:354
#define FF_PROFILE_DNXHR_LB
Definition: avcodec.h:3251
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:89
int flags
A combination of AV_PKT_FLAG values.
Definition: avcodec.h:1663
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:532
const uint8_t * chroma_weight
Definition: dnxhddata.h:51
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:261
#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:1386
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:607
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:1919
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 void bit_depth(AudioStatsContext *s, uint64_t mask, uint64_t imask, AVRational *depth)
Definition: af_astats.c:150
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
int quality
quality (between 1 (good) and FF_LAMBDA_MAX (bad))
Definition: frame.h:301
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:2295
void ff_dnxhd_print_profiles(AVCodecContext *avctx, int loglevel)
Definition: dnxhddata.c:1169
#define FF_PROFILE_DNXHR_HQ
Definition: avcodec.h:3253
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:3161
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:1034
#define AV_CODEC_CAP_SLICE_THREADS
Codec supports slice-based (or partition-based) multithreading.
Definition: avcodec.h:1061
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:131
int linesize[AV_NUM_DATA_POINTERS]
For video, size in bytes of each picture line.
Definition: frame.h:218
static int dnxhd_encode_picture(AVCodecContext *avctx, AVPacket *pkt, const AVFrame *frame, int *got_packet)
Definition: dnxhdenc.c:1286
void(* fdct)(int16_t *block)
Definition: fdctdsp.h:27
main external API structure.
Definition: avcodec.h:1732
uint8_t edge_buf_uv[2][256]
Definition: dnxhdenc.h:79
unsigned block_width_l2
Definition: dnxhdenc.h:64
ScanTable intra_scantable
Definition: mpegvideo.h:88
#define FF_DEFAULT_QUANT_BIAS
Definition: avcodec.h:2262
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:1764
unsigned lambda
Definition: dnxhdenc.h:97
FDCTDSPContext fdsp
Definition: mpegvideo.h:224
uint8_t edge_buf_y[256]
Definition: dnxhdenc.h:78
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:424
int index
Definition: gxfenc.c:89
#define FF_MB_DECISION_RD
rate distortion
Definition: avcodec.h:2298
#define NBUCKETS
Definition: dnxhdenc.c:1169
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:132
int ssd
Definition: dnxhdenc.h:40
static void dnxhd_setup_threads_slices(DNXHDEncContext *ctx)
Definition: dnxhdenc.c:947
int ff_alloc_packet2(AVCodecContext *avctx, AVPacket *avpkt, int64_t size, int64_t min_size)
Check AVPacket size and/or allocate data.
Definition: utils.c:1736
#define FF_PROFILE_DNXHR_SQ
Definition: avcodec.h:3252
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:262
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:1267
#define AV_PIX_FMT_YUV422P10
Definition: pixfmt.h:352
#define DNX10BIT_QMAT_SHIFT
Definition: dnxhdenc.c:41
uint8_t * data[AV_NUM_DATA_POINTERS]
pointer to the picture/channel planes.
Definition: frame.h:201
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:78
struct AVCodecContext * avctx
Definition: mpegvideo.h:95
PutBitContext pb
bit output
Definition: mpegvideo.h:148
static int dnxhd_mb_var_thread(AVCodecContext *avctx, void *arg, int jobnr, int threadnr)
Definition: dnxhdenc.c:966
#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:3254
static int dnxhd_encode_thread(AVCodecContext *avctx, void *arg, int jobnr, int threadnr)
Definition: dnxhdenc.c:908
static av_cold int dnxhd_encode_init(AVCodecContext *avctx)
Definition: dnxhdenc.c:372
#define VE
Definition: dnxhdenc.c:45
#define FF_DISABLE_DEPRECATION_WARNINGS
Definition: internal.h:83
common internal api header.
static void flush_put_bits(PutBitContext *s)
Pad the end of the output stream with zeros.
Definition: put_bits.h:101
attribute_deprecated AVFrame * coded_frame
the picture in the bitstream
Definition: avcodec.h:3152
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
int den
Denominator.
Definition: rational.h:60
#define FF_PROFILE_DNXHR_444
Definition: avcodec.h:3255
void * priv_data
Definition: avcodec.h:1774
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:233
#define FF_ENABLE_DEPRECATION_WARNINGS
Definition: internal.h:84
static void radix_sort(RCCMPEntry *data, RCCMPEntry *tmp, int size)
Definition: dnxhdenc.c:1211
int top_field_first
If the content is interlaced, is top field displayed first.
Definition: frame.h:330
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:3221
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:256
#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:124
#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:522
uint16_t mb
Definition: dnxhdenc.h:35
#define MKTAG(a, b, c, d)
Definition: common.h:342
float min
AVPixelFormat
Pixel format.
Definition: pixfmt.h:60
This structure stores compressed data.
Definition: avcodec.h:1634
uint32_t * vlc_codes
Definition: dnxhdenc.h:89
#define FF_ALLOCZ_OR_GOTO(ctx, p, size, label)
Definition: internal.h:142
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