Libav
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 Libav.
10  *
11  * Libav 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  * Libav 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 Libav; 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 "dnxhdenc.h"
38 
39 // The largest value that will not lead to overflow for 10bit samples.
40 #define DNX10BIT_QMAT_SHIFT 18
41 #define RC_VARIANCE 1 // use variance or ssd for fast rc
42 #define LAMBDA_FRAC_BITS 10
43 
44 #define VE AV_OPT_FLAG_VIDEO_PARAM | AV_OPT_FLAG_ENCODING_PARAM
45 static const AVOption options[] = {
46  { "nitris_compat", "encode with Avid Nitris compatibility",
47  offsetof(DNXHDEncContext, nitris_compat), AV_OPT_TYPE_INT, { .i64 = 0 }, 0, 1, VE },
48  { NULL }
49 };
50 
51 static const AVClass class = {
52  "dnxhd",
54  options,
56 };
57 
59  const uint8_t *pixels,
60  ptrdiff_t line_size)
61 {
62  int i;
63  for (i = 0; i < 4; i++) {
64  block[0] = pixels[0];
65  block[1] = pixels[1];
66  block[2] = pixels[2];
67  block[3] = pixels[3];
68  block[4] = pixels[4];
69  block[5] = pixels[5];
70  block[6] = pixels[6];
71  block[7] = pixels[7];
72  pixels += line_size;
73  block += 8;
74  }
75  memcpy(block, block - 8, sizeof(*block) * 8);
76  memcpy(block + 8, block - 16, sizeof(*block) * 8);
77  memcpy(block + 16, block - 24, sizeof(*block) * 8);
78  memcpy(block + 24, block - 32, sizeof(*block) * 8);
79 }
80 
81 static av_always_inline
83  const uint8_t *pixels,
84  ptrdiff_t line_size)
85 {
86  int i;
87 
88  block += 32;
89 
90  for (i = 0; i < 4; i++) {
91  memcpy(block + i * 8, pixels + i * line_size, 8 * sizeof(*block));
92  memcpy(block - (i + 1) * 8, pixels + i * line_size, 8 * sizeof(*block));
93  }
94 }
95 
96 static int dnxhd_10bit_dct_quantize(MpegEncContext *ctx, int16_t *block,
97  int n, int qscale, int *overflow)
98 {
99  const uint8_t *scantable= ctx->intra_scantable.scantable;
100  const int *qmat = ctx->q_intra_matrix[qscale];
101  int last_non_zero = 0;
102  int i;
103 
104  ctx->fdsp.fdct(block);
105 
106  // Divide by 4 with rounding, to compensate scaling of DCT coefficients
107  block[0] = (block[0] + 2) >> 2;
108 
109  for (i = 1; i < 64; ++i) {
110  int j = scantable[i];
111  int sign = FF_SIGNBIT(block[j]);
112  int level = (block[j] ^ sign) - sign;
113  level = level * qmat[j] >> DNX10BIT_QMAT_SHIFT;
114  block[j] = (level ^ sign) - sign;
115  if (level)
116  last_non_zero = i;
117  }
118 
119  return last_non_zero;
120 }
121 
123 {
124  int i, j, level, run;
125  int max_level = 1 << (ctx->cid_table->bit_depth + 2);
126 
127  FF_ALLOCZ_OR_GOTO(ctx->m.avctx, ctx->vlc_codes,
128  max_level * 4 * sizeof(*ctx->vlc_codes), fail);
129  FF_ALLOCZ_OR_GOTO(ctx->m.avctx, ctx->vlc_bits,
130  max_level * 4 * sizeof(*ctx->vlc_bits), fail);
131  FF_ALLOCZ_OR_GOTO(ctx->m.avctx, ctx->run_codes,
132  63 * 2, fail);
133  FF_ALLOCZ_OR_GOTO(ctx->m.avctx, ctx->run_bits,
134  63, fail);
135 
136  ctx->vlc_codes += max_level * 2;
137  ctx->vlc_bits += max_level * 2;
138  for (level = -max_level; level < max_level; level++) {
139  for (run = 0; run < 2; run++) {
140  int index = (level << 1) | run;
141  int sign, offset = 0, alevel = level;
142 
143  MASK_ABS(sign, alevel);
144  if (alevel > 64) {
145  offset = (alevel - 1) >> 6;
146  alevel -= offset << 6;
147  }
148  for (j = 0; j < 257; j++) {
149  if (ctx->cid_table->ac_level[j] == alevel &&
150  (!offset || (ctx->cid_table->ac_index_flag[j] && offset)) &&
151  (!run || (ctx->cid_table->ac_run_flag [j] && run))) {
152  assert(!ctx->vlc_codes[index]);
153  if (alevel) {
154  ctx->vlc_codes[index] =
155  (ctx->cid_table->ac_codes[j] << 1) | (sign & 1);
156  ctx->vlc_bits[index] = ctx->cid_table->ac_bits[j] + 1;
157  } else {
158  ctx->vlc_codes[index] = ctx->cid_table->ac_codes[j];
159  ctx->vlc_bits[index] = ctx->cid_table->ac_bits[j];
160  }
161  break;
162  }
163  }
164  assert(!alevel || j < 257);
165  if (offset) {
166  ctx->vlc_codes[index] =
167  (ctx->vlc_codes[index] << ctx->cid_table->index_bits) | offset;
168  ctx->vlc_bits[index] += ctx->cid_table->index_bits;
169  }
170  }
171  }
172  for (i = 0; i < 62; i++) {
173  int run = ctx->cid_table->run[i];
174  assert(run < 63);
175  ctx->run_codes[run] = ctx->cid_table->run_codes[i];
176  ctx->run_bits[run] = ctx->cid_table->run_bits[i];
177  }
178  return 0;
179 fail:
180  return AVERROR(ENOMEM);
181 }
182 
183 static av_cold int dnxhd_init_qmat(DNXHDEncContext *ctx, int lbias, int cbias)
184 {
185  // init first elem to 1 to avoid div by 0 in convert_matrix
186  uint16_t weight_matrix[64] = { 1, }; // convert_matrix needs uint16_t*
187  int qscale, i;
188  const uint8_t *luma_weight_table = ctx->cid_table->luma_weight;
189  const uint8_t *chroma_weight_table = ctx->cid_table->chroma_weight;
190 
191  FF_ALLOCZ_OR_GOTO(ctx->m.avctx, ctx->qmatrix_l,
192  (ctx->m.avctx->qmax + 1) * 64 * sizeof(int), fail);
193  FF_ALLOCZ_OR_GOTO(ctx->m.avctx, ctx->qmatrix_c,
194  (ctx->m.avctx->qmax + 1) * 64 * sizeof(int), fail);
196  (ctx->m.avctx->qmax + 1) * 64 * 2 * sizeof(uint16_t),
197  fail);
199  (ctx->m.avctx->qmax + 1) * 64 * 2 * sizeof(uint16_t),
200  fail);
201 
202  if (ctx->cid_table->bit_depth == 8) {
203  for (i = 1; i < 64; i++) {
204  int j = ctx->m.idsp.idct_permutation[ff_zigzag_direct[i]];
205  weight_matrix[j] = ctx->cid_table->luma_weight[i];
206  }
207  ff_convert_matrix(&ctx->m, ctx->qmatrix_l, ctx->qmatrix_l16,
208  weight_matrix, ctx->m.intra_quant_bias, 1,
209  ctx->m.avctx->qmax, 1);
210  for (i = 1; i < 64; i++) {
211  int j = ctx->m.idsp.idct_permutation[ff_zigzag_direct[i]];
212  weight_matrix[j] = ctx->cid_table->chroma_weight[i];
213  }
214  ff_convert_matrix(&ctx->m, ctx->qmatrix_c, ctx->qmatrix_c16,
215  weight_matrix, ctx->m.intra_quant_bias, 1,
216  ctx->m.avctx->qmax, 1);
217 
218  for (qscale = 1; qscale <= ctx->m.avctx->qmax; qscale++) {
219  for (i = 0; i < 64; i++) {
220  ctx->qmatrix_l[qscale][i] <<= 2;
221  ctx->qmatrix_c[qscale][i] <<= 2;
222  ctx->qmatrix_l16[qscale][0][i] <<= 2;
223  ctx->qmatrix_l16[qscale][1][i] <<= 2;
224  ctx->qmatrix_c16[qscale][0][i] <<= 2;
225  ctx->qmatrix_c16[qscale][1][i] <<= 2;
226  }
227  }
228  } else {
229  // 10-bit
230  for (qscale = 1; qscale <= ctx->m.avctx->qmax; qscale++) {
231  for (i = 1; i < 64; i++) {
232  int j = ctx->m.idsp.idct_permutation[ff_zigzag_direct[i]];
233 
234  /* The quantization formula from the VC-3 standard is:
235  * quantized = sign(block[i]) * floor(abs(block[i]/s) * p /
236  * (qscale * weight_table[i]))
237  * Where p is 32 for 8-bit samples and 8 for 10-bit ones.
238  * The s factor compensates scaling of DCT coefficients done by
239  * the DCT routines, and therefore is not present in standard.
240  * It's 8 for 8-bit samples and 4 for 10-bit ones.
241  * We want values of ctx->qtmatrix_l and ctx->qtmatrix_r to be:
242  * ((1 << DNX10BIT_QMAT_SHIFT) * (p / s)) /
243  * (qscale * weight_table[i])
244  * For 10-bit samples, p / s == 2 */
245  ctx->qmatrix_l[qscale][j] = (1 << (DNX10BIT_QMAT_SHIFT + 1)) /
246  (qscale * luma_weight_table[i]);
247  ctx->qmatrix_c[qscale][j] = (1 << (DNX10BIT_QMAT_SHIFT + 1)) /
248  (qscale * chroma_weight_table[i]);
249  }
250  }
251  }
252 
253  return 0;
254 fail:
255  return AVERROR(ENOMEM);
256 }
257 
259 {
260  FF_ALLOCZ_OR_GOTO(ctx->m.avctx, ctx->mb_rc,
261  8160 * ctx->m.avctx->qmax * sizeof(RCEntry), fail);
262  if (ctx->m.avctx->mb_decision != FF_MB_DECISION_RD)
263  FF_ALLOCZ_OR_GOTO(ctx->m.avctx, ctx->mb_cmp,
264  ctx->m.mb_num * sizeof(RCCMPEntry), fail);
265 
266  ctx->frame_bits = (ctx->cid_table->coding_unit_size -
267  640 - 4 - ctx->min_padding) * 8;
268  ctx->qscale = 1;
269  ctx->lambda = 2 << LAMBDA_FRAC_BITS; // qscale 2
270  return 0;
271 fail:
272  return AVERROR(ENOMEM);
273 }
274 
276 {
277  DNXHDEncContext *ctx = avctx->priv_data;
278  int i, index, bit_depth, ret;
279 
280  switch (avctx->pix_fmt) {
281  case AV_PIX_FMT_YUV422P:
282  bit_depth = 8;
283  break;
285  bit_depth = 10;
286  break;
287  default:
288  av_log(avctx, AV_LOG_ERROR,
289  "pixel format is incompatible with DNxHD\n");
290  return AVERROR(EINVAL);
291  }
292 
293  ctx->cid = ff_dnxhd_find_cid(avctx, bit_depth);
294  if (!ctx->cid) {
295  av_log(avctx, AV_LOG_ERROR,
296  "video parameters incompatible with DNxHD\n");
297  return AVERROR(EINVAL);
298  }
299  av_log(avctx, AV_LOG_DEBUG, "cid %d\n", ctx->cid);
300 
301  index = ff_dnxhd_get_cid_table(ctx->cid);
303 
304  ctx->m.avctx = avctx;
305  ctx->m.mb_intra = 1;
306  ctx->m.h263_aic = 1;
307 
308  avctx->bits_per_raw_sample = ctx->cid_table->bit_depth;
309 
310  ff_blockdsp_init(&ctx->bdsp, avctx);
311  ff_fdctdsp_init(&ctx->m.fdsp, avctx);
312  ff_mpv_idct_init(&ctx->m);
313  ff_mpegvideoencdsp_init(&ctx->m.mpvencdsp, avctx);
314  ff_pixblockdsp_init(&ctx->m.pdsp, avctx);
315  if (!ctx->m.dct_quantize)
317 
318  if (ctx->cid_table->bit_depth == 10) {
321  ctx->block_width_l2 = 4;
322  } else {
324  ctx->block_width_l2 = 3;
325  }
326 
327  if (ARCH_X86)
329 
330  ctx->m.mb_height = (avctx->height + 15) / 16;
331  ctx->m.mb_width = (avctx->width + 15) / 16;
332 
333  if (avctx->flags & CODEC_FLAG_INTERLACED_DCT) {
334  ctx->interlaced = 1;
335  ctx->m.mb_height /= 2;
336  }
337 
338  ctx->m.mb_num = ctx->m.mb_height * ctx->m.mb_width;
339 
341  ctx->m.intra_quant_bias = avctx->intra_quant_bias;
342  // XXX tune lbias/cbias
343  if ((ret = dnxhd_init_qmat(ctx, ctx->m.intra_quant_bias, 0)) < 0)
344  return ret;
345 
346  /* Avid Nitris hardware decoder requires a minimum amount of padding
347  * in the coding unit payload */
348  if (ctx->nitris_compat)
349  ctx->min_padding = 1600;
350 
351  if ((ret = dnxhd_init_vlc(ctx)) < 0)
352  return ret;
353  if ((ret = dnxhd_init_rc(ctx)) < 0)
354  return ret;
355 
356  FF_ALLOCZ_OR_GOTO(ctx->m.avctx, ctx->slice_size,
357  ctx->m.mb_height * sizeof(uint32_t), fail);
358  FF_ALLOCZ_OR_GOTO(ctx->m.avctx, ctx->slice_offs,
359  ctx->m.mb_height * sizeof(uint32_t), fail);
360  FF_ALLOCZ_OR_GOTO(ctx->m.avctx, ctx->mb_bits,
361  ctx->m.mb_num * sizeof(uint16_t), fail);
362  FF_ALLOCZ_OR_GOTO(ctx->m.avctx, ctx->mb_qscale,
363  ctx->m.mb_num * sizeof(uint8_t), fail);
364 
365  avctx->coded_frame = av_frame_alloc();
366  if (!avctx->coded_frame)
367  return AVERROR(ENOMEM);
368 
369  avctx->coded_frame->key_frame = 1;
371 
372  if (avctx->thread_count > MAX_THREADS) {
373  av_log(avctx, AV_LOG_ERROR, "too many threads\n");
374  return AVERROR(EINVAL);
375  }
376 
377  ctx->thread[0] = ctx;
378  for (i = 1; i < avctx->thread_count; i++) {
379  ctx->thread[i] = av_malloc(sizeof(DNXHDEncContext));
380  memcpy(ctx->thread[i], ctx, sizeof(DNXHDEncContext));
381  }
382 
383  return 0;
384 fail: // for FF_ALLOCZ_OR_GOTO
385  return AVERROR(ENOMEM);
386 }
387 
388 static int dnxhd_write_header(AVCodecContext *avctx, uint8_t *buf)
389 {
390  DNXHDEncContext *ctx = avctx->priv_data;
391  const uint8_t header_prefix[5] = { 0x00, 0x00, 0x02, 0x80, 0x01 };
392 
393  memset(buf, 0, 640);
394 
395  memcpy(buf, header_prefix, 5);
396  buf[5] = ctx->interlaced ? ctx->cur_field + 2 : 0x01;
397  buf[6] = 0x80; // crc flag off
398  buf[7] = 0xa0; // reserved
399  AV_WB16(buf + 0x18, avctx->height >> ctx->interlaced); // ALPF
400  AV_WB16(buf + 0x1a, avctx->width); // SPL
401  AV_WB16(buf + 0x1d, avctx->height >> ctx->interlaced); // NAL
402 
403  buf[0x21] = ctx->cid_table->bit_depth == 10 ? 0x58 : 0x38;
404  buf[0x22] = 0x88 + (ctx->interlaced << 2);
405  AV_WB32(buf + 0x28, ctx->cid); // CID
406  buf[0x2c] = ctx->interlaced ? 0 : 0x80;
407 
408  buf[0x5f] = 0x01; // UDL
409 
410  buf[0x167] = 0x02; // reserved
411  AV_WB16(buf + 0x16a, ctx->m.mb_height * 4 + 4); // MSIPS
412  buf[0x16d] = ctx->m.mb_height; // Ns
413  buf[0x16f] = 0x10; // reserved
414 
415  ctx->msip = buf + 0x170;
416  return 0;
417 }
418 
420 {
421  int nbits;
422  if (diff < 0) {
423  nbits = av_log2_16bit(-2 * diff);
424  diff--;
425  } else {
426  nbits = av_log2_16bit(2 * diff);
427  }
428  put_bits(&ctx->m.pb, ctx->cid_table->dc_bits[nbits] + nbits,
429  (ctx->cid_table->dc_codes[nbits] << nbits) +
430  (diff & ((1 << nbits) - 1)));
431 }
432 
433 static av_always_inline
435  int last_index, int n)
436 {
437  int last_non_zero = 0;
438  int slevel, i, j;
439 
440  dnxhd_encode_dc(ctx, block[0] - ctx->m.last_dc[n]);
441  ctx->m.last_dc[n] = block[0];
442 
443  for (i = 1; i <= last_index; i++) {
444  j = ctx->m.intra_scantable.permutated[i];
445  slevel = block[j];
446  if (slevel) {
447  int run_level = i - last_non_zero - 1;
448  int rlevel = (slevel << 1) | !!run_level;
449  put_bits(&ctx->m.pb, ctx->vlc_bits[rlevel], ctx->vlc_codes[rlevel]);
450  if (run_level)
451  put_bits(&ctx->m.pb, ctx->run_bits[run_level],
452  ctx->run_codes[run_level]);
453  last_non_zero = i;
454  }
455  }
456  put_bits(&ctx->m.pb, ctx->vlc_bits[0], ctx->vlc_codes[0]); // EOB
457 }
458 
459 static av_always_inline
460 void dnxhd_unquantize_c(DNXHDEncContext *ctx, int16_t *block, int n,
461  int qscale, int last_index)
462 {
463  const uint8_t *weight_matrix;
464  int level;
465  int i;
466 
467  weight_matrix = (n & 2) ? ctx->cid_table->chroma_weight
468  : ctx->cid_table->luma_weight;
469 
470  for (i = 1; i <= last_index; i++) {
471  int j = ctx->m.intra_scantable.permutated[i];
472  level = block[j];
473  if (level) {
474  if (level < 0) {
475  level = (1 - 2 * level) * qscale * weight_matrix[i];
476  if (ctx->cid_table->bit_depth == 10) {
477  if (weight_matrix[i] != 8)
478  level += 8;
479  level >>= 4;
480  } else {
481  if (weight_matrix[i] != 32)
482  level += 32;
483  level >>= 6;
484  }
485  level = -level;
486  } else {
487  level = (2 * level + 1) * qscale * weight_matrix[i];
488  if (ctx->cid_table->bit_depth == 10) {
489  if (weight_matrix[i] != 8)
490  level += 8;
491  level >>= 4;
492  } else {
493  if (weight_matrix[i] != 32)
494  level += 32;
495  level >>= 6;
496  }
497  }
498  block[j] = level;
499  }
500  }
501 }
502 
503 static av_always_inline int dnxhd_ssd_block(int16_t *qblock, int16_t *block)
504 {
505  int score = 0;
506  int i;
507  for (i = 0; i < 64; i++)
508  score += (block[i] - qblock[i]) * (block[i] - qblock[i]);
509  return score;
510 }
511 
512 static av_always_inline
513 int dnxhd_calc_ac_bits(DNXHDEncContext *ctx, int16_t *block, int last_index)
514 {
515  int last_non_zero = 0;
516  int bits = 0;
517  int i, j, level;
518  for (i = 1; i <= last_index; i++) {
519  j = ctx->m.intra_scantable.permutated[i];
520  level = block[j];
521  if (level) {
522  int run_level = i - last_non_zero - 1;
523  bits += ctx->vlc_bits[(level << 1) |
524  !!run_level] + ctx->run_bits[run_level];
525  last_non_zero = i;
526  }
527  }
528  return bits;
529 }
530 
531 static av_always_inline
532 void dnxhd_get_blocks(DNXHDEncContext *ctx, int mb_x, int mb_y)
533 {
534  const int bs = ctx->block_width_l2;
535  const int bw = 1 << bs;
536  const uint8_t *ptr_y = ctx->thread[0]->src[0] +
537  ((mb_y << 4) * ctx->m.linesize) + (mb_x << bs + 1);
538  const uint8_t *ptr_u = ctx->thread[0]->src[1] +
539  ((mb_y << 4) * ctx->m.uvlinesize) + (mb_x << bs);
540  const uint8_t *ptr_v = ctx->thread[0]->src[2] +
541  ((mb_y << 4) * ctx->m.uvlinesize) + (mb_x << bs);
542  PixblockDSPContext *pdsp = &ctx->m.pdsp;
543 
544  pdsp->get_pixels(ctx->blocks[0], ptr_y, ctx->m.linesize);
545  pdsp->get_pixels(ctx->blocks[1], ptr_y + bw, ctx->m.linesize);
546  pdsp->get_pixels(ctx->blocks[2], ptr_u, ctx->m.uvlinesize);
547  pdsp->get_pixels(ctx->blocks[3], ptr_v, ctx->m.uvlinesize);
548 
549  if (mb_y + 1 == ctx->m.mb_height && ctx->m.avctx->height == 1080) {
550  if (ctx->interlaced) {
551  ctx->get_pixels_8x4_sym(ctx->blocks[4],
552  ptr_y + ctx->dct_y_offset,
553  ctx->m.linesize);
554  ctx->get_pixels_8x4_sym(ctx->blocks[5],
555  ptr_y + ctx->dct_y_offset + bw,
556  ctx->m.linesize);
557  ctx->get_pixels_8x4_sym(ctx->blocks[6],
558  ptr_u + ctx->dct_uv_offset,
559  ctx->m.uvlinesize);
560  ctx->get_pixels_8x4_sym(ctx->blocks[7],
561  ptr_v + ctx->dct_uv_offset,
562  ctx->m.uvlinesize);
563  } else {
564  ctx->bdsp.clear_block(ctx->blocks[4]);
565  ctx->bdsp.clear_block(ctx->blocks[5]);
566  ctx->bdsp.clear_block(ctx->blocks[6]);
567  ctx->bdsp.clear_block(ctx->blocks[7]);
568  }
569  } else {
570  pdsp->get_pixels(ctx->blocks[4],
571  ptr_y + ctx->dct_y_offset, ctx->m.linesize);
572  pdsp->get_pixels(ctx->blocks[5],
573  ptr_y + ctx->dct_y_offset + bw, ctx->m.linesize);
574  pdsp->get_pixels(ctx->blocks[6],
575  ptr_u + ctx->dct_uv_offset, ctx->m.uvlinesize);
576  pdsp->get_pixels(ctx->blocks[7],
577  ptr_v + ctx->dct_uv_offset, ctx->m.uvlinesize);
578  }
579 }
580 
581 static av_always_inline
583 {
584  if (i & 2) {
585  ctx->m.q_intra_matrix16 = ctx->qmatrix_c16;
586  ctx->m.q_intra_matrix = ctx->qmatrix_c;
587  return 1 + (i & 1);
588  } else {
589  ctx->m.q_intra_matrix16 = ctx->qmatrix_l16;
590  ctx->m.q_intra_matrix = ctx->qmatrix_l;
591  return 0;
592  }
593 }
594 
595 static int dnxhd_calc_bits_thread(AVCodecContext *avctx, void *arg,
596  int jobnr, int threadnr)
597 {
598  DNXHDEncContext *ctx = avctx->priv_data;
599  int mb_y = jobnr, mb_x;
600  int qscale = ctx->qscale;
601  LOCAL_ALIGNED_16(int16_t, block, [64]);
602  ctx = ctx->thread[threadnr];
603 
604  ctx->m.last_dc[0] =
605  ctx->m.last_dc[1] =
606  ctx->m.last_dc[2] = 1 << (ctx->cid_table->bit_depth + 2);
607 
608  for (mb_x = 0; mb_x < ctx->m.mb_width; mb_x++) {
609  unsigned mb = mb_y * ctx->m.mb_width + mb_x;
610  int ssd = 0;
611  int ac_bits = 0;
612  int dc_bits = 0;
613  int i;
614 
615  dnxhd_get_blocks(ctx, mb_x, mb_y);
616 
617  for (i = 0; i < 8; i++) {
618  int16_t *src_block = ctx->blocks[i];
619  int overflow, nbits, diff, last_index;
620  int n = dnxhd_switch_matrix(ctx, i);
621 
622  memcpy(block, src_block, 64 * sizeof(*block));
623  last_index = ctx->m.dct_quantize(&ctx->m, block, i,
624  qscale, &overflow);
625  ac_bits += dnxhd_calc_ac_bits(ctx, block, last_index);
626 
627  diff = block[0] - ctx->m.last_dc[n];
628  if (diff < 0)
629  nbits = av_log2_16bit(-2 * diff);
630  else
631  nbits = av_log2_16bit(2 * diff);
632 
633  assert(nbits < ctx->cid_table->bit_depth + 4);
634  dc_bits += ctx->cid_table->dc_bits[nbits] + nbits;
635 
636  ctx->m.last_dc[n] = block[0];
637 
638  if (avctx->mb_decision == FF_MB_DECISION_RD || !RC_VARIANCE) {
639  dnxhd_unquantize_c(ctx, block, i, qscale, last_index);
640  ctx->m.idsp.idct(block);
641  ssd += dnxhd_ssd_block(block, src_block);
642  }
643  }
644  ctx->mb_rc[qscale][mb].ssd = ssd;
645  ctx->mb_rc[qscale][mb].bits = ac_bits + dc_bits + 12 +
646  8 * ctx->vlc_bits[0];
647  }
648  return 0;
649 }
650 
651 static int dnxhd_encode_thread(AVCodecContext *avctx, void *arg,
652  int jobnr, int threadnr)
653 {
654  DNXHDEncContext *ctx = avctx->priv_data;
655  int mb_y = jobnr, mb_x;
656  ctx = ctx->thread[threadnr];
657  init_put_bits(&ctx->m.pb, (uint8_t *)arg + 640 + ctx->slice_offs[jobnr],
658  ctx->slice_size[jobnr]);
659 
660  ctx->m.last_dc[0] =
661  ctx->m.last_dc[1] =
662  ctx->m.last_dc[2] = 1 << (ctx->cid_table->bit_depth + 2);
663  for (mb_x = 0; mb_x < ctx->m.mb_width; mb_x++) {
664  unsigned mb = mb_y * ctx->m.mb_width + mb_x;
665  int qscale = ctx->mb_qscale[mb];
666  int i;
667 
668  put_bits(&ctx->m.pb, 12, qscale << 1);
669 
670  dnxhd_get_blocks(ctx, mb_x, mb_y);
671 
672  for (i = 0; i < 8; i++) {
673  int16_t *block = ctx->blocks[i];
674  int overflow, n = dnxhd_switch_matrix(ctx, i);
675  int last_index = ctx->m.dct_quantize(&ctx->m, block, i,
676  qscale, &overflow);
677  // START_TIMER;
678  dnxhd_encode_block(ctx, block, last_index, n);
679  // STOP_TIMER("encode_block");
680  }
681  }
682  if (put_bits_count(&ctx->m.pb) & 31)
683  put_bits(&ctx->m.pb, 32 - (put_bits_count(&ctx->m.pb) & 31), 0);
684  flush_put_bits(&ctx->m.pb);
685  return 0;
686 }
687 
689 {
690  int mb_y, mb_x;
691  int offset = 0;
692  for (mb_y = 0; mb_y < ctx->m.mb_height; mb_y++) {
693  int thread_size;
694  ctx->slice_offs[mb_y] = offset;
695  ctx->slice_size[mb_y] = 0;
696  for (mb_x = 0; mb_x < ctx->m.mb_width; mb_x++) {
697  unsigned mb = mb_y * ctx->m.mb_width + mb_x;
698  ctx->slice_size[mb_y] += ctx->mb_bits[mb];
699  }
700  ctx->slice_size[mb_y] = (ctx->slice_size[mb_y] + 31) & ~31;
701  ctx->slice_size[mb_y] >>= 3;
702  thread_size = ctx->slice_size[mb_y];
703  offset += thread_size;
704  }
705 }
706 
707 static int dnxhd_mb_var_thread(AVCodecContext *avctx, void *arg,
708  int jobnr, int threadnr)
709 {
710  DNXHDEncContext *ctx = avctx->priv_data;
711  int mb_y = jobnr, mb_x, x, y;
712  int partial_last_row = (mb_y == ctx->m.mb_height - 1) &&
713  ((avctx->height >> ctx->interlaced) & 0xF);
714 
715  ctx = ctx->thread[threadnr];
716  if (ctx->cid_table->bit_depth == 8) {
717  uint8_t *pix = ctx->thread[0]->src[0] + ((mb_y << 4) * ctx->m.linesize);
718  for (mb_x = 0; mb_x < ctx->m.mb_width; ++mb_x, pix += 16) {
719  unsigned mb = mb_y * ctx->m.mb_width + mb_x;
720  int sum;
721  int varc;
722 
723  if (!partial_last_row && mb_x * 16 <= avctx->width - 16) {
724  sum = ctx->m.mpvencdsp.pix_sum(pix, ctx->m.linesize);
725  varc = ctx->m.mpvencdsp.pix_norm1(pix, ctx->m.linesize);
726  } else {
727  int bw = FFMIN(avctx->width - 16 * mb_x, 16);
728  int bh = FFMIN((avctx->height >> ctx->interlaced) - 16 * mb_y, 16);
729  sum = varc = 0;
730  for (y = 0; y < bh; y++) {
731  for (x = 0; x < bw; x++) {
732  uint8_t val = pix[x + y * ctx->m.linesize];
733  sum += val;
734  varc += val * val;
735  }
736  }
737  }
738  varc = (varc - (((unsigned) sum * sum) >> 8) + 128) >> 8;
739 
740  ctx->mb_cmp[mb].value = varc;
741  ctx->mb_cmp[mb].mb = mb;
742  }
743  } else { // 10-bit
744  int const linesize = ctx->m.linesize >> 1;
745  for (mb_x = 0; mb_x < ctx->m.mb_width; ++mb_x) {
746  uint16_t *pix = (uint16_t *)ctx->thread[0]->src[0] +
747  ((mb_y << 4) * linesize) + (mb_x << 4);
748  unsigned mb = mb_y * ctx->m.mb_width + mb_x;
749  int sum = 0;
750  int sqsum = 0;
751  int mean, sqmean;
752  int i, j;
753  // Macroblocks are 16x16 pixels, unlike DCT blocks which are 8x8.
754  for (i = 0; i < 16; ++i) {
755  for (j = 0; j < 16; ++j) {
756  // Turn 16-bit pixels into 10-bit ones.
757  int const sample = (unsigned) pix[j] >> 6;
758  sum += sample;
759  sqsum += sample * sample;
760  // 2^10 * 2^10 * 16 * 16 = 2^28, which is less than INT_MAX
761  }
762  pix += linesize;
763  }
764  mean = sum >> 8; // 16*16 == 2^8
765  sqmean = sqsum >> 8;
766  ctx->mb_cmp[mb].value = sqmean - mean * mean;
767  ctx->mb_cmp[mb].mb = mb;
768  }
769  }
770  return 0;
771 }
772 
774 {
775  int lambda, up_step, down_step;
776  int last_lower = INT_MAX, last_higher = 0;
777  int x, y, q;
778 
779  for (q = 1; q < avctx->qmax; q++) {
780  ctx->qscale = q;
781  avctx->execute2(avctx, dnxhd_calc_bits_thread,
782  NULL, NULL, ctx->m.mb_height);
783  }
784  up_step = down_step = 2 << LAMBDA_FRAC_BITS;
785  lambda = ctx->lambda;
786 
787  for (;;) {
788  int bits = 0;
789  int end = 0;
790  if (lambda == last_higher) {
791  lambda++;
792  end = 1; // need to set final qscales/bits
793  }
794  for (y = 0; y < ctx->m.mb_height; y++) {
795  for (x = 0; x < ctx->m.mb_width; x++) {
796  unsigned min = UINT_MAX;
797  int qscale = 1;
798  int mb = y * ctx->m.mb_width + x;
799  for (q = 1; q < avctx->qmax; q++) {
800  unsigned score = ctx->mb_rc[q][mb].bits * lambda +
801  ((unsigned) ctx->mb_rc[q][mb].ssd << LAMBDA_FRAC_BITS);
802  if (score < min) {
803  min = score;
804  qscale = q;
805  }
806  }
807  bits += ctx->mb_rc[qscale][mb].bits;
808  ctx->mb_qscale[mb] = qscale;
809  ctx->mb_bits[mb] = ctx->mb_rc[qscale][mb].bits;
810  }
811  bits = (bits + 31) & ~31; // padding
812  if (bits > ctx->frame_bits)
813  break;
814  }
815  // av_dlog(ctx->m.avctx,
816  // "lambda %d, up %u, down %u, bits %d, frame %d\n",
817  // lambda, last_higher, last_lower, bits, ctx->frame_bits);
818  if (end) {
819  if (bits > ctx->frame_bits)
820  return AVERROR(EINVAL);
821  break;
822  }
823  if (bits < ctx->frame_bits) {
824  last_lower = FFMIN(lambda, last_lower);
825  if (last_higher != 0)
826  lambda = (lambda+last_higher)>>1;
827  else
828  lambda -= down_step;
829  down_step = FFMIN((int64_t)down_step*5, INT_MAX);
830  up_step = 1<<LAMBDA_FRAC_BITS;
831  lambda = FFMAX(1, lambda);
832  if (lambda == last_lower)
833  break;
834  } else {
835  last_higher = FFMAX(lambda, last_higher);
836  if (last_lower != INT_MAX)
837  lambda = (lambda+last_lower)>>1;
838  else if ((int64_t)lambda + up_step > INT_MAX)
839  return AVERROR(EINVAL);
840  else
841  lambda += up_step;
842  up_step = FFMIN((int64_t)up_step*5, INT_MAX);
843  down_step = 1<<LAMBDA_FRAC_BITS;
844  }
845  }
846  //av_dlog(ctx->m.avctx, "out lambda %d\n", lambda);
847  ctx->lambda = lambda;
848  return 0;
849 }
850 
852 {
853  int bits = 0;
854  int up_step = 1;
855  int down_step = 1;
856  int last_higher = 0;
857  int last_lower = INT_MAX;
858  int qscale;
859  int x, y;
860 
861  qscale = ctx->qscale;
862  for (;;) {
863  bits = 0;
864  ctx->qscale = qscale;
865  // XXX avoid recalculating bits
867  NULL, NULL, ctx->m.mb_height);
868  for (y = 0; y < ctx->m.mb_height; y++) {
869  for (x = 0; x < ctx->m.mb_width; x++)
870  bits += ctx->mb_rc[qscale][y*ctx->m.mb_width+x].bits;
871  bits = (bits+31)&~31; // padding
872  if (bits > ctx->frame_bits)
873  break;
874  }
875  // av_dlog(ctx->m.avctx,
876  // "%d, qscale %d, bits %d, frame %d, higher %d, lower %d\n",
877  // ctx->m.avctx->frame_number, qscale, bits, ctx->frame_bits,
878  // last_higher, last_lower);
879  if (bits < ctx->frame_bits) {
880  if (qscale == 1)
881  return 1;
882  if (last_higher == qscale - 1) {
883  qscale = last_higher;
884  break;
885  }
886  last_lower = FFMIN(qscale, last_lower);
887  if (last_higher != 0)
888  qscale = (qscale + last_higher) >> 1;
889  else
890  qscale -= down_step++;
891  if (qscale < 1)
892  qscale = 1;
893  up_step = 1;
894  } else {
895  if (last_lower == qscale + 1)
896  break;
897  last_higher = FFMAX(qscale, last_higher);
898  if (last_lower != INT_MAX)
899  qscale = (qscale + last_lower) >> 1;
900  else
901  qscale += up_step++;
902  down_step = 1;
903  if (qscale >= ctx->m.avctx->qmax)
904  return AVERROR(EINVAL);
905  }
906  }
907  //av_dlog(ctx->m.avctx, "out qscale %d\n", qscale);
908  ctx->qscale = qscale;
909  return 0;
910 }
911 
912 #define BUCKET_BITS 8
913 #define RADIX_PASSES 4
914 #define NBUCKETS (1 << BUCKET_BITS)
915 
916 static inline int get_bucket(int value, int shift)
917 {
918  value >>= shift;
919  value &= NBUCKETS - 1;
920  return NBUCKETS - 1 - value;
921 }
922 
923 static void radix_count(const RCCMPEntry *data, int size,
924  int buckets[RADIX_PASSES][NBUCKETS])
925 {
926  int i, j;
927  memset(buckets, 0, sizeof(buckets[0][0]) * RADIX_PASSES * NBUCKETS);
928  for (i = 0; i < size; i++) {
929  int v = data[i].value;
930  for (j = 0; j < RADIX_PASSES; j++) {
931  buckets[j][get_bucket(v, 0)]++;
932  v >>= BUCKET_BITS;
933  }
934  assert(!v);
935  }
936  for (j = 0; j < RADIX_PASSES; j++) {
937  int offset = size;
938  for (i = NBUCKETS - 1; i >= 0; i--)
939  buckets[j][i] = offset -= buckets[j][i];
940  assert(!buckets[j][0]);
941  }
942 }
943 
944 static void radix_sort_pass(RCCMPEntry *dst, const RCCMPEntry *data,
945  int size, int buckets[NBUCKETS], int pass)
946 {
947  int shift = pass * BUCKET_BITS;
948  int i;
949  for (i = 0; i < size; i++) {
950  int v = get_bucket(data[i].value, shift);
951  int pos = buckets[v]++;
952  dst[pos] = data[i];
953  }
954 }
955 
956 static void radix_sort(RCCMPEntry *data, int size)
957 {
958  int buckets[RADIX_PASSES][NBUCKETS];
959  RCCMPEntry *tmp = av_malloc(sizeof(*tmp) * size);
960  radix_count(data, size, buckets);
961  radix_sort_pass(tmp, data, size, buckets[0], 0);
962  radix_sort_pass(data, tmp, size, buckets[1], 1);
963  if (buckets[2][NBUCKETS - 1] || buckets[3][NBUCKETS - 1]) {
964  radix_sort_pass(tmp, data, size, buckets[2], 2);
965  radix_sort_pass(data, tmp, size, buckets[3], 3);
966  }
967  av_free(tmp);
968 }
969 
971 {
972  int max_bits = 0;
973  int ret, x, y;
974  if ((ret = dnxhd_find_qscale(ctx)) < 0)
975  return ret;
976  for (y = 0; y < ctx->m.mb_height; y++) {
977  for (x = 0; x < ctx->m.mb_width; x++) {
978  int mb = y * ctx->m.mb_width + x;
979  int delta_bits;
980  ctx->mb_qscale[mb] = ctx->qscale;
981  ctx->mb_bits[mb] = ctx->mb_rc[ctx->qscale][mb].bits;
982  max_bits += ctx->mb_rc[ctx->qscale][mb].bits;
983  if (!RC_VARIANCE) {
984  delta_bits = ctx->mb_rc[ctx->qscale][mb].bits -
985  ctx->mb_rc[ctx->qscale + 1][mb].bits;
986  ctx->mb_cmp[mb].mb = mb;
987  ctx->mb_cmp[mb].value =
988  delta_bits ? ((ctx->mb_rc[ctx->qscale][mb].ssd -
989  ctx->mb_rc[ctx->qscale + 1][mb].ssd) * 100) /
990  delta_bits
991  : INT_MIN; // avoid increasing qscale
992  }
993  }
994  max_bits += 31; // worst padding
995  }
996  if (!ret) {
997  if (RC_VARIANCE)
998  avctx->execute2(avctx, dnxhd_mb_var_thread,
999  NULL, NULL, ctx->m.mb_height);
1000  radix_sort(ctx->mb_cmp, ctx->m.mb_num);
1001  for (x = 0; x < ctx->m.mb_num && max_bits > ctx->frame_bits; x++) {
1002  int mb = ctx->mb_cmp[x].mb;
1003  max_bits -= ctx->mb_rc[ctx->qscale][mb].bits -
1004  ctx->mb_rc[ctx->qscale + 1][mb].bits;
1005  ctx->mb_qscale[mb] = ctx->qscale + 1;
1006  ctx->mb_bits[mb] = ctx->mb_rc[ctx->qscale + 1][mb].bits;
1007  }
1008  }
1009  return 0;
1010 }
1011 
1012 static void dnxhd_load_picture(DNXHDEncContext *ctx, const AVFrame *frame)
1013 {
1014  int i;
1015 
1016  for (i = 0; i < ctx->m.avctx->thread_count; i++) {
1017  ctx->thread[i]->m.linesize = frame->linesize[0] << ctx->interlaced;
1018  ctx->thread[i]->m.uvlinesize = frame->linesize[1] << ctx->interlaced;
1019  ctx->thread[i]->dct_y_offset = ctx->m.linesize *8;
1020  ctx->thread[i]->dct_uv_offset = ctx->m.uvlinesize*8;
1021  }
1022 
1024  ctx->cur_field = frame->interlaced_frame && !frame->top_field_first;
1025 }
1026 
1028  const AVFrame *frame, int *got_packet)
1029 {
1030  DNXHDEncContext *ctx = avctx->priv_data;
1031  int first_field = 1;
1032  int offset, i, ret;
1033  uint8_t *buf;
1034 
1035  if ((ret = ff_alloc_packet(pkt, ctx->cid_table->frame_size)) < 0) {
1036  av_log(avctx, AV_LOG_ERROR,
1037  "output buffer is too small to compress picture\n");
1038  return ret;
1039  }
1040  buf = pkt->data;
1041 
1042  dnxhd_load_picture(ctx, frame);
1043 
1044 encode_coding_unit:
1045  for (i = 0; i < 3; i++) {
1046  ctx->src[i] = frame->data[i];
1047  if (ctx->interlaced && ctx->cur_field)
1048  ctx->src[i] += frame->linesize[i];
1049  }
1050 
1051  dnxhd_write_header(avctx, buf);
1052 
1053  if (avctx->mb_decision == FF_MB_DECISION_RD)
1054  ret = dnxhd_encode_rdo(avctx, ctx);
1055  else
1056  ret = dnxhd_encode_fast(avctx, ctx);
1057  if (ret < 0) {
1058  av_log(avctx, AV_LOG_ERROR,
1059  "picture could not fit ratecontrol constraints, increase qmax\n");
1060  return ret;
1061  }
1062 
1064 
1065  offset = 0;
1066  for (i = 0; i < ctx->m.mb_height; i++) {
1067  AV_WB32(ctx->msip + i * 4, offset);
1068  offset += ctx->slice_size[i];
1069  assert(!(ctx->slice_size[i] & 3));
1070  }
1071 
1072  avctx->execute2(avctx, dnxhd_encode_thread, buf, NULL, ctx->m.mb_height);
1073 
1074  assert(640 + offset + 4 <= ctx->cid_table->coding_unit_size);
1075  memset(buf + 640 + offset, 0,
1076  ctx->cid_table->coding_unit_size - 4 - offset - 640);
1077 
1078  AV_WB32(buf + ctx->cid_table->coding_unit_size - 4, 0x600DC0DE); // EOF
1079 
1080  if (ctx->interlaced && first_field) {
1081  first_field = 0;
1082  ctx->cur_field ^= 1;
1083  buf += ctx->cid_table->coding_unit_size;
1084  goto encode_coding_unit;
1085  }
1086 
1087  avctx->coded_frame->quality = ctx->qscale * FF_QP2LAMBDA;
1088 
1089  pkt->flags |= AV_PKT_FLAG_KEY;
1090  *got_packet = 1;
1091  return 0;
1092 }
1093 
1095 {
1096  DNXHDEncContext *ctx = avctx->priv_data;
1097  int max_level = 1 << (ctx->cid_table->bit_depth + 2);
1098  int i;
1099 
1100  av_free(ctx->vlc_codes - max_level * 2);
1101  av_free(ctx->vlc_bits - max_level * 2);
1102  av_freep(&ctx->run_codes);
1103  av_freep(&ctx->run_bits);
1104 
1105  av_freep(&ctx->mb_bits);
1106  av_freep(&ctx->mb_qscale);
1107  av_freep(&ctx->mb_rc);
1108  av_freep(&ctx->mb_cmp);
1109  av_freep(&ctx->slice_size);
1110  av_freep(&ctx->slice_offs);
1111 
1112  av_freep(&ctx->qmatrix_c);
1113  av_freep(&ctx->qmatrix_l);
1114  av_freep(&ctx->qmatrix_c16);
1115  av_freep(&ctx->qmatrix_l16);
1116 
1117  for (i = 1; i < avctx->thread_count; i++)
1118  av_freep(&ctx->thread[i]);
1119 
1120  av_frame_free(&avctx->coded_frame);
1121 
1122  return 0;
1123 }
1124 
1126  .name = "dnxhd",
1127  .long_name = NULL_IF_CONFIG_SMALL("VC3/DNxHD"),
1128  .type = AVMEDIA_TYPE_VIDEO,
1129  .id = AV_CODEC_ID_DNXHD,
1130  .priv_data_size = sizeof(DNXHDEncContext),
1132  .encode2 = dnxhd_encode_picture,
1134  .capabilities = CODEC_CAP_SLICE_THREADS,
1135  .pix_fmts = (const enum AVPixelFormat[]) {
1139  },
1140  .priv_class = &class,
1141 };
static av_always_inline int dnxhd_ssd_block(int16_t *qblock, int16_t *block)
Definition: dnxhdenc.c:503
#define MASK_ABS(mask, level)
Definition: mathops.h:152
IDCTDSPContext idsp
Definition: mpegvideo.h:354
static void radix_count(const RCCMPEntry *data, int size, int buckets[RADIX_PASSES][NBUCKETS])
Definition: dnxhdenc.c:923
void * av_malloc(size_t size)
Allocate a block of size bytes with alignment suitable for all memory accesses (including vectors if ...
Definition: mem.c:62
static av_always_inline void dnxhd_get_blocks(DNXHDEncContext *ctx, int mb_x, int mb_y)
Definition: dnxhdenc.c:532
int size
This structure describes decoded (raw) audio or video data.
Definition: frame.h:135
const uint8_t * ac_level
Definition: dnxhddata.h:39
const uint8_t * dc_bits
Definition: dnxhddata.h:37
AVOption.
Definition: opt.h:234
void(* clear_block)(int16_t *block)
Definition: blockdsp.h:35
#define LAMBDA_FRAC_BITS
Definition: dnxhdenc.c:42
const uint8_t * luma_weight
Definition: dnxhddata.h:36
AVFrame * coded_frame
the picture in the bitstream
Definition: avcodec.h:2532
int(* qmatrix_l)[64]
Definition: dnxhdenc.h:70
const CIDEntry ff_dnxhd_cid_table[]
Definition: dnxhddata.c:1076
const uint16_t * run_codes
Definition: dnxhddata.h:41
#define ARCH_X86
Definition: config.h:33
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:80
enum AVPixelFormat pix_fmt
Pixel format, see AV_PIX_FMT_xxx.
Definition: avcodec.h:1254
unsigned dct_uv_offset
Definition: dnxhdenc.h:58
static av_always_inline void dnxhd_encode_dc(DNXHDEncContext *ctx, int diff)
Definition: dnxhdenc.c:419
static av_cold int dnxhd_init_vlc(DNXHDEncContext *ctx)
Definition: dnxhdenc.c:122
mpegvideo header.
uint8_t permutated[64]
Definition: idctdsp.h:31
int ff_dnxhd_find_cid(AVCodecContext *avctx, int bit_depth)
Definition: dnxhddata.c:1165
int intra_quant_bias
intra quantizer bias
Definition: avcodec.h:1546
uint8_t run
Definition: svq3.c:146
int bits_per_raw_sample
Bits per sample/pixel of internal libavcodec pixel/sample format.
Definition: avcodec.h:2514
static const AVOption options[]
Definition: dnxhdenc.c:45
int mb_num
number of MBs of a picture
Definition: mpegvideo.h:259
av_cold void ff_fdctdsp_init(FDCTDSPContext *c, AVCodecContext *avctx)
Definition: fdctdsp.c:26
struct DNXHDEncContext * thread[MAX_THREADS]
Definition: dnxhdenc.h:53
#define sample
AVCodec.
Definition: avcodec.h:2796
static av_always_inline int dnxhd_calc_ac_bits(DNXHDEncContext *ctx, int16_t *block, int last_index)
Definition: dnxhdenc.c:513
int h263_aic
Advanded INTRA Coding (AIC)
Definition: mpegvideo.h:210
Macro definitions for various function/variable attributes.
void av_freep(void *arg)
Free a memory block which has been allocated with av_malloc(z)() or av_realloc() and set the pointer ...
Definition: mem.c:198
#define MAX_THREADS
Definition: mpegvideo.h:66
int16_t blocks[8][64]
Definition: dnxhdenc.h:67
av_cold void ff_mpegvideoencdsp_init(MpegvideoEncDSPContext *c, AVCodecContext *avctx)
uint8_t bits
Definition: crc.c:251
uint8_t
#define av_cold
Definition: attributes.h:66
void(* get_pixels_8x4_sym)(int16_t *, const uint8_t *, ptrdiff_t)
Definition: dnxhdenc.h:95
AVFrame * av_frame_alloc(void)
Allocate an AVFrame and set its fields to default values.
Definition: frame.c:57
AVOptions.
#define AV_WB32(p, d)
Definition: intreadwrite.h:239
static int get_bucket(int value, int shift)
Definition: dnxhdenc.c:916
static int dnxhd_encode_fast(AVCodecContext *avctx, DNXHDEncContext *ctx)
Definition: dnxhdenc.c:970
uint32_t * slice_size
Definition: dnxhdenc.h:50
int(* qmatrix_c)[64]
Definition: dnxhdenc.h:69
#define RADIX_PASSES
Definition: dnxhdenc.c:913
uint32_t * slice_offs
Definition: dnxhdenc.h:51
unsigned qscale
Definition: dnxhdenc.h:84
const char data[16]
Definition: mxf.c:70
uint8_t * data
Definition: avcodec.h:973
static void radix_sort_pass(RCCMPEntry *dst, const RCCMPEntry *data, int size, int buckets[NBUCKETS], int pass)
Definition: dnxhdenc.c:944
const uint8_t * run_bits
Definition: dnxhddata.h:42
#define BUCKET_BITS
Definition: dnxhdenc.c:912
const uint8_t * scantable
Definition: idctdsp.h:30
int interlaced_frame
The content of the picture is interlaced.
Definition: frame.h:320
av_cold void ff_mpv_idct_init(MpegEncContext *s)
Definition: mpegvideo.c:408
int mb_height
number of MBs horizontally & vertically
Definition: mpegvideo.h:255
static av_cold int dnxhd_init_qmat(DNXHDEncContext *ctx, int lbias, int cbias)
Definition: dnxhdenc.c:183
unsigned int coding_unit_size
Definition: dnxhddata.h:33
high precision timer, useful to profile code
BlockDSPContext bdsp
Definition: dnxhdenc.h:44
const uint8_t * ac_index_flag
Definition: dnxhddata.h:40
static av_cold int dnxhd_encode_end(AVCodecContext *avctx)
Definition: dnxhdenc.c:1094
#define AV_PKT_FLAG_KEY
The packet contains a keyframe.
Definition: avcodec.h:1019
const uint8_t * ac_bits
Definition: dnxhddata.h:39
int(* q_intra_matrix)[64]
precomputed matrix (combine qscale and DCT renorm)
Definition: mpegvideo.h:442
static av_always_inline int dnxhd_switch_matrix(DNXHDEncContext *ctx, int i)
Definition: dnxhdenc.c:582
const uint16_t * ac_codes
Definition: dnxhddata.h:38
static int dnxhd_calc_bits_thread(AVCodecContext *avctx, void *arg, int jobnr, int threadnr)
Definition: dnxhdenc.c:595
#define AV_LOG_ERROR
Something went wrong and cannot losslessly be recovered.
Definition: log.h:123
void av_free(void *ptr)
Free a memory block which has been allocated with av_malloc(z)() or av_realloc(). ...
Definition: mem.c:186
int ff_dnxhd_get_cid_table(int cid)
Definition: dnxhddata.c:1156
int last_dc[3]
last DC values for MPEG1
Definition: mpegvideo.h:311
static int dnxhd_find_qscale(DNXHDEncContext *ctx)
Definition: dnxhdenc.c:851
#define FF_SIGNBIT(x)
Definition: internal.h:38
#define AVERROR(e)
Definition: error.h:43
#define RC_VARIANCE
Definition: dnxhdenc.c:41
void(* get_pixels)(int16_t *block, const uint8_t *pixels, int line_size)
Definition: pixblockdsp.h:27
void av_frame_free(AVFrame **frame)
Free the frame and any dynamically allocated objects in it, e.g.
Definition: frame.c:69
int qmax
maximum quantizer
Definition: avcodec.h:2080
#define NULL_IF_CONFIG_SMALL(x)
Return NULL if CONFIG_SMALL is true, otherwise the argument without modification. ...
Definition: internal.h:150
AVCodec ff_dnxhd_encoder
Definition: dnxhdenc.c:1125
static av_always_inline void dnxhd_unquantize_c(DNXHDEncContext *ctx, int16_t *block, int n, int qscale, int last_index)
Definition: dnxhdenc.c:460
#define AV_LOG_DEBUG
Stuff which is only useful for libav* developers.
Definition: log.h:144
PixblockDSPContext pdsp
Definition: mpegvideo.h:358
int flags
CODEC_FLAG_*.
Definition: avcodec.h:1144
static int dnxhd_write_header(AVCodecContext *avctx, uint8_t *buf)
Definition: dnxhdenc.c:388
const uint8_t * dc_codes
Definition: dnxhddata.h:37
void av_log(void *avcl, int level, const char *fmt,...)
Definition: log.c:169
MpegvideoEncDSPContext mpvencdsp
Definition: mpegvideo.h:357
const char * name
Name of the codec implementation.
Definition: avcodec.h:2803
static void put_bits(PutBitContext *s, int n, unsigned int value)
Write up to 31 bits into a bitstream.
Definition: put_bits.h:134
#define FFMAX(a, b)
Definition: common.h:55
int flags
A combination of AV_PKT_FLAG values.
Definition: avcodec.h:979
static int put_bits_count(PutBitContext *s)
Definition: put_bits.h:67
planar YUV 4:2:2, 16bpp, (1 Cr & Cb sample per 2x1 Y samples)
Definition: pixfmt.h:69
#define pass
Definition: fft_template.c:335
const uint8_t * chroma_weight
Definition: dnxhddata.h:36
uint16_t * run_codes
Definition: dnxhdenc.h:79
common internal API header
static void dnxhd_8bit_get_pixels_8x4_sym(int16_t *restrict block, const uint8_t *pixels, ptrdiff_t line_size)
Definition: dnxhdenc.c:58
enum AVPictureType pict_type
Picture type of the frame.
Definition: frame.h:196
MpegEncContext m
Used for quantization dsp functions.
Definition: dnxhdenc.h:45
#define FFMIN(a, b)
Definition: common.h:57
static av_always_inline void dnxhd_encode_block(DNXHDEncContext *ctx, int16_t *block, int last_index, int n)
Definition: dnxhdenc.c:434
uint8_t * run_bits
Definition: dnxhdenc.h:80
int intra_quant_bias
bias for the quantizer
Definition: mpegvideo.h:427
int width
picture width / height.
Definition: avcodec.h:1224
const uint8_t * run
Definition: dnxhddata.h:42
int(* pix_sum)(uint8_t *pix, int line_size)
unsigned frame_bits
Definition: dnxhdenc.h:74
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:445
uint16_t(* qmatrix_l16)[2][64]
Definition: dnxhdenc.h:71
int quality
quality (between 1 (good) and FF_LAMBDA_MAX (bad))
Definition: frame.h:235
int ff_alloc_packet(AVPacket *avpkt, int size)
Check AVPacket size and/or allocate data.
Definition: utils.c:1245
#define CODEC_FLAG_INTERLACED_DCT
Use interlaced DCT.
Definition: avcodec.h:655
uint8_t * msip
Macroblock Scan Indexes Payload.
Definition: dnxhdenc.h:49
uint8_t idct_permutation[64]
IDCT input permutation.
Definition: idctdsp.h:94
int value
Definition: dnxhdenc.h:34
int mb_decision
macroblock decision mode
Definition: avcodec.h:1581
uint8_t * vlc_bits
Definition: dnxhdenc.h:78
LIBAVUTIL_VERSION_INT
Definition: eval.c:55
int thread_count
thread count is used to decide how many independent tasks should be passed to execute() ...
Definition: avcodec.h:2540
int ff_dct_quantize_c(MpegEncContext *s, int16_t *block, int n, int qscale, int *overflow)
static void radix_sort(RCCMPEntry *data, int size)
Definition: dnxhdenc.c:956
static int dnxhd_encode_rdo(AVCodecContext *avctx, DNXHDEncContext *ctx)
Definition: dnxhdenc.c:773
int bit_depth
Definition: dnxhddata.h:35
NULL
Definition: eval.c:55
int index_bits
Definition: dnxhddata.h:34
Libavcodec external API header.
static int dnxhd_10bit_dct_quantize(MpegEncContext *ctx, int16_t *block, int n, int qscale, int *overflow)
Definition: dnxhdenc.c:96
ptrdiff_t linesize
line size, in bytes, may be different from width
Definition: mpegvideo.h:260
av_cold void ff_blockdsp_init(BlockDSPContext *c, AVCodecContext *avctx)
Definition: blockdsp.c:58
int linesize[AV_NUM_DATA_POINTERS]
For video, size in bytes of each picture line.
Definition: frame.h:153
static int dnxhd_encode_picture(AVCodecContext *avctx, AVPacket *pkt, const AVFrame *frame, int *got_packet)
Definition: dnxhdenc.c:1027
av_default_item_name
Definition: dnxhdenc.c:52
const uint8_t * ac_run_flag
Definition: dnxhddata.h:40
void(* fdct)(int16_t *block)
Definition: fdctdsp.h:27
main external API structure.
Definition: avcodec.h:1050
static void close(AVCodecParserContext *s)
Definition: h264_parser.c:490
unsigned block_width_l2
Definition: dnxhdenc.h:59
ScanTable intra_scantable
Definition: mpegvideo.h:214
#define FF_DEFAULT_QUANT_BIAS
Definition: avcodec.h:1547
static av_cold int dnxhd_init_rc(DNXHDEncContext *ctx)
Definition: dnxhdenc.c:258
unsigned lambda
Definition: dnxhdenc.h:85
FDCTDSPContext fdsp
Definition: mpegvideo.h:352
void ff_dnxhdenc_init_x86(DNXHDEncContext *ctx)
Definition: dnxhdenc_init.c:31
Describe the class of an AVClass context structure.
Definition: log.h:33
int(* pix_norm1)(uint8_t *pix, int line_size)
int index
Definition: gxfenc.c:72
#define FF_MB_DECISION_RD
rate distortion
Definition: avcodec.h:1584
#define AV_WB16(p, d)
Definition: intreadwrite.h:213
#define NBUCKETS
Definition: dnxhdenc.c:914
const uint8_t ff_zigzag_direct[64]
Definition: mathtables.c:115
av_cold void ff_pixblockdsp_init(PixblockDSPContext *c, AVCodecContext *avctx)
Definition: pixblockdsp.c:54
ptrdiff_t uvlinesize
line size, for chroma in bytes, may be different from width
Definition: mpegvideo.h:261
int ssd
Definition: dnxhdenc.h:38
static void dnxhd_setup_threads_slices(DNXHDEncContext *ctx)
Definition: dnxhdenc.c:688
const CIDEntry * cid_table
Definition: dnxhdenc.h:48
uint16_t(* qmatrix_c16)[2][64]
Definition: dnxhdenc.h:72
unsigned dct_y_offset
Definition: dnxhdenc.h:57
unsigned min_padding
Definition: dnxhdenc.h:65
static void dnxhd_load_picture(DNXHDEncContext *ctx, const AVFrame *frame)
Definition: dnxhdenc.c:1012
#define AV_PIX_FMT_YUV422P10
Definition: pixfmt.h:244
#define DNX10BIT_QMAT_SHIFT
Definition: dnxhdenc.c:40
uint8_t * data[AV_NUM_DATA_POINTERS]
pointer to the picture/channel planes.
Definition: frame.h:141
unsigned int frame_size
Definition: dnxhddata.h:32
uint8_t level
Definition: svq3.c:147
uint16_t * mb_bits
Definition: dnxhdenc.h:89
MpegEncContext.
Definition: mpegvideo.h:204
struct AVCodecContext * avctx
Definition: mpegvideo.h:221
PutBitContext pb
bit output
Definition: mpegvideo.h:277
static int dnxhd_mb_var_thread(AVCodecContext *avctx, void *arg, int jobnr, int threadnr)
Definition: dnxhdenc.c:707
static int dnxhd_encode_thread(AVCodecContext *avctx, void *arg, int jobnr, int threadnr)
Definition: dnxhdenc.c:651
static av_cold int dnxhd_encode_init(AVCodecContext *avctx)
Definition: dnxhdenc.c:275
#define VE
Definition: dnxhdenc.c:44
#define CODEC_CAP_SLICE_THREADS
Codec supports slice-based (or partition-based) multithreading.
Definition: avcodec.h:759
common internal api header.
static void flush_put_bits(PutBitContext *s)
Pad the end of the output stream with zeros.
Definition: put_bits.h:83
#define restrict
Definition: config.h:8
static void init_put_bits(PutBitContext *s, uint8_t *buffer, int buffer_size)
Initialize the PutBitContext s.
Definition: put_bits.h:48
static av_cold int init(AVCodecParserContext *s)
Definition: h264_parser.c:499
void * priv_data
Definition: avcodec.h:1092
static av_always_inline void dnxhd_10bit_get_pixels_8x4_sym(int16_t *restrict block, const uint8_t *pixels, ptrdiff_t line_size)
Definition: dnxhdenc.c:82
RCCMPEntry * mb_cmp
Definition: dnxhdenc.h:92
int top_field_first
If the content is interlaced, is top field displayed first.
Definition: frame.h:325
int nitris_compat
Definition: dnxhdenc.h:64
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:2600
#define av_log2_16bit
Definition: intmath.h:86
int bits
Definition: dnxhdenc.h:39
int key_frame
1 -> keyframe, 0-> not
Definition: frame.h:191
#define FF_QP2LAMBDA
factor to convert from H.263 QP to lambda
Definition: avutil.h:207
#define LOCAL_ALIGNED_16(t, v,...)
Definition: internal.h:114
#define av_always_inline
Definition: attributes.h:40
static int first_field(int fd)
Definition: v4l2.c:212
int(* dct_quantize)(struct MpegEncContext *s, int16_t *block, int n, int qscale, int *overflow)
Definition: mpegvideo.h:624
uint16_t mb
Definition: dnxhdenc.h:33
float min
AVPixelFormat
Pixel format.
Definition: pixfmt.h:63
This structure stores compressed data.
Definition: avcodec.h:950
RCEntry(* mb_rc)[8160]
Definition: dnxhdenc.h:93
uint32_t * vlc_codes
Definition: dnxhdenc.h:77
for(j=16;j >0;--j)
#define FF_ALLOCZ_OR_GOTO(ctx, p, size, label)
Definition: internal.h:126
uint8_t * src[3]
Definition: dnxhdenc.h:75
uint8_t * mb_qscale
Definition: dnxhdenc.h:90
void(* idct)(int16_t *block)
Definition: idctdsp.h:63
static int16_t block[64]
Definition: dct-test.c:88