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