FFmpeg  4.1.11
proresenc_kostya.c
Go to the documentation of this file.
1 /*
2  * Apple ProRes encoder
3  *
4  * Copyright (c) 2012 Konstantin Shishkov
5  *
6  * This encoder appears to be based on Anatoliy Wassermans considering
7  * similarities in the bugs.
8  *
9  * This file is part of FFmpeg.
10  *
11  * FFmpeg is free software; you can redistribute it and/or
12  * modify it under the terms of the GNU Lesser General Public
13  * License as published by the Free Software Foundation; either
14  * version 2.1 of the License, or (at your option) any later version.
15  *
16  * FFmpeg is distributed in the hope that it will be useful,
17  * but WITHOUT ANY WARRANTY; without even the implied warranty of
18  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
19  * Lesser General Public License for more details.
20  *
21  * You should have received a copy of the GNU Lesser General Public
22  * License along with FFmpeg; if not, write to the Free Software
23  * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
24  */
25 
26 #include "libavutil/opt.h"
27 #include "libavutil/pixdesc.h"
28 #include "avcodec.h"
29 #include "fdctdsp.h"
30 #include "put_bits.h"
31 #include "bytestream.h"
32 #include "internal.h"
33 #include "proresdata.h"
34 
35 #define CFACTOR_Y422 2
36 #define CFACTOR_Y444 3
37 
38 #define MAX_MBS_PER_SLICE 8
39 
40 #define MAX_PLANES 4
41 
42 enum {
50 };
51 
52 enum {
60 };
61 
62 static const uint8_t prores_quant_matrices[][64] = {
63  { // proxy
64  4, 7, 9, 11, 13, 14, 15, 63,
65  7, 7, 11, 12, 14, 15, 63, 63,
66  9, 11, 13, 14, 15, 63, 63, 63,
67  11, 11, 13, 14, 63, 63, 63, 63,
68  11, 13, 14, 63, 63, 63, 63, 63,
69  13, 14, 63, 63, 63, 63, 63, 63,
70  13, 63, 63, 63, 63, 63, 63, 63,
71  63, 63, 63, 63, 63, 63, 63, 63,
72  },
73  { // proxy chromas
74  4, 7, 9, 11, 13, 14, 63, 63,
75  7, 7, 11, 12, 14, 63, 63, 63,
76  9, 11, 13, 14, 63, 63, 63, 63,
77  11, 11, 13, 14, 63, 63, 63, 63,
78  11, 13, 14, 63, 63, 63, 63, 63,
79  13, 14, 63, 63, 63, 63, 63, 63,
80  13, 63, 63, 63, 63, 63, 63, 63,
81  63, 63, 63, 63, 63, 63, 63, 63
82  },
83  { // LT
84  4, 5, 6, 7, 9, 11, 13, 15,
85  5, 5, 7, 8, 11, 13, 15, 17,
86  6, 7, 9, 11, 13, 15, 15, 17,
87  7, 7, 9, 11, 13, 15, 17, 19,
88  7, 9, 11, 13, 14, 16, 19, 23,
89  9, 11, 13, 14, 16, 19, 23, 29,
90  9, 11, 13, 15, 17, 21, 28, 35,
91  11, 13, 16, 17, 21, 28, 35, 41,
92  },
93  { // standard
94  4, 4, 5, 5, 6, 7, 7, 9,
95  4, 4, 5, 6, 7, 7, 9, 9,
96  5, 5, 6, 7, 7, 9, 9, 10,
97  5, 5, 6, 7, 7, 9, 9, 10,
98  5, 6, 7, 7, 8, 9, 10, 12,
99  6, 7, 7, 8, 9, 10, 12, 15,
100  6, 7, 7, 9, 10, 11, 14, 17,
101  7, 7, 9, 10, 11, 14, 17, 21,
102  },
103  { // high quality
104  4, 4, 4, 4, 4, 4, 4, 4,
105  4, 4, 4, 4, 4, 4, 4, 4,
106  4, 4, 4, 4, 4, 4, 4, 4,
107  4, 4, 4, 4, 4, 4, 4, 5,
108  4, 4, 4, 4, 4, 4, 5, 5,
109  4, 4, 4, 4, 4, 5, 5, 6,
110  4, 4, 4, 4, 5, 5, 6, 7,
111  4, 4, 4, 4, 5, 6, 7, 7,
112  },
113  { // XQ luma
114  2, 2, 2, 2, 2, 2, 2, 2,
115  2, 2, 2, 2, 2, 2, 2, 2,
116  2, 2, 2, 2, 2, 2, 2, 2,
117  2, 2, 2, 2, 2, 2, 2, 3,
118  2, 2, 2, 2, 2, 2, 3, 3,
119  2, 2, 2, 2, 2, 3, 3, 3,
120  2, 2, 2, 2, 3, 3, 3, 4,
121  2, 2, 2, 2, 3, 3, 4, 4,
122  },
123  { // codec default
124  4, 4, 4, 4, 4, 4, 4, 4,
125  4, 4, 4, 4, 4, 4, 4, 4,
126  4, 4, 4, 4, 4, 4, 4, 4,
127  4, 4, 4, 4, 4, 4, 4, 4,
128  4, 4, 4, 4, 4, 4, 4, 4,
129  4, 4, 4, 4, 4, 4, 4, 4,
130  4, 4, 4, 4, 4, 4, 4, 4,
131  4, 4, 4, 4, 4, 4, 4, 4,
132  },
133 };
134 
135 #define NUM_MB_LIMITS 4
136 static const int prores_mb_limits[NUM_MB_LIMITS] = {
137  1620, // up to 720x576
138  2700, // up to 960x720
139  6075, // up to 1440x1080
140  9216, // up to 2048x1152
141 };
142 
143 static const struct prores_profile {
144  const char *full_name;
145  uint32_t tag;
149  int quant;
151 } prores_profile_info[6] = {
152  {
153  .full_name = "proxy",
154  .tag = MKTAG('a', 'p', 'c', 'o'),
155  .min_quant = 4,
156  .max_quant = 8,
157  .br_tab = { 300, 242, 220, 194 },
158  .quant = QUANT_MAT_PROXY,
159  .quant_chroma = QUANT_MAT_PROXY_CHROMA,
160  },
161  {
162  .full_name = "LT",
163  .tag = MKTAG('a', 'p', 'c', 's'),
164  .min_quant = 1,
165  .max_quant = 9,
166  .br_tab = { 720, 560, 490, 440 },
167  .quant = QUANT_MAT_LT,
168  .quant_chroma = QUANT_MAT_LT,
169  },
170  {
171  .full_name = "standard",
172  .tag = MKTAG('a', 'p', 'c', 'n'),
173  .min_quant = 1,
174  .max_quant = 6,
175  .br_tab = { 1050, 808, 710, 632 },
176  .quant = QUANT_MAT_STANDARD,
177  .quant_chroma = QUANT_MAT_STANDARD,
178  },
179  {
180  .full_name = "high quality",
181  .tag = MKTAG('a', 'p', 'c', 'h'),
182  .min_quant = 1,
183  .max_quant = 6,
184  .br_tab = { 1566, 1216, 1070, 950 },
185  .quant = QUANT_MAT_HQ,
186  .quant_chroma = QUANT_MAT_HQ,
187  },
188  {
189  .full_name = "4444",
190  .tag = MKTAG('a', 'p', '4', 'h'),
191  .min_quant = 1,
192  .max_quant = 6,
193  .br_tab = { 2350, 1828, 1600, 1425 },
194  .quant = QUANT_MAT_HQ,
195  .quant_chroma = QUANT_MAT_HQ,
196  },
197  {
198  .full_name = "4444XQ",
199  .tag = MKTAG('a', 'p', '4', 'x'),
200  .min_quant = 1,
201  .max_quant = 6,
202  .br_tab = { 3525, 2742, 2400, 2137 },
203  .quant = QUANT_MAT_HQ, /* Fix me : use QUANT_MAT_XQ_LUMA */
204  .quant_chroma = QUANT_MAT_HQ,
205  }
206 };
207 
208 #define TRELLIS_WIDTH 16
209 #define SCORE_LIMIT INT_MAX / 2
210 
211 struct TrellisNode {
213  int quant;
214  int bits;
215  int score;
216 };
217 
218 #define MAX_STORED_Q 16
219 
220 typedef struct ProresThreadData {
221  DECLARE_ALIGNED(16, int16_t, blocks)[MAX_PLANES][64 * 4 * MAX_MBS_PER_SLICE];
222  DECLARE_ALIGNED(16, uint16_t, emu_buf)[16 * 16];
223  int16_t custom_q[64];
224  int16_t custom_chroma_q[64];
227 
228 typedef struct ProresContext {
229  AVClass *class;
230  DECLARE_ALIGNED(16, int16_t, blocks)[MAX_PLANES][64 * 4 * MAX_MBS_PER_SLICE];
231  DECLARE_ALIGNED(16, uint16_t, emu_buf)[16*16];
232  int16_t quants[MAX_STORED_Q][64];
233  int16_t quants_chroma[MAX_STORED_Q][64];
234  int16_t custom_q[64];
235  int16_t custom_chroma_q[64];
239 
240  void (*fdct)(FDCTDSPContext *fdsp, const uint16_t *src,
241  ptrdiff_t linesize, int16_t *block);
242  FDCTDSPContext fdsp;
243 
244  const AVFrame *pic;
245  int mb_width, mb_height;
247  int num_chroma_blocks, chroma_factor;
250  int pictures_per_frame; // 1 for progressive, 2 for interlaced
256  int warn;
257 
258  char *vendor;
260 
262 
263  int profile;
265 
266  int *slice_q;
267 
269 } ProresContext;
270 
271 static void get_slice_data(ProresContext *ctx, const uint16_t *src,
272  ptrdiff_t linesize, int x, int y, int w, int h,
273  int16_t *blocks, uint16_t *emu_buf,
274  int mbs_per_slice, int blocks_per_mb, int is_chroma)
275 {
276  const uint16_t *esrc;
277  const int mb_width = 4 * blocks_per_mb;
278  ptrdiff_t elinesize;
279  int i, j, k;
280 
281  for (i = 0; i < mbs_per_slice; i++, src += mb_width) {
282  if (x >= w) {
283  memset(blocks, 0, 64 * (mbs_per_slice - i) * blocks_per_mb
284  * sizeof(*blocks));
285  return;
286  }
287  if (x + mb_width <= w && y + 16 <= h) {
288  esrc = src;
289  elinesize = linesize;
290  } else {
291  int bw, bh, pix;
292 
293  esrc = emu_buf;
294  elinesize = 16 * sizeof(*emu_buf);
295 
296  bw = FFMIN(w - x, mb_width);
297  bh = FFMIN(h - y, 16);
298 
299  for (j = 0; j < bh; j++) {
300  memcpy(emu_buf + j * 16,
301  (const uint8_t*)src + j * linesize,
302  bw * sizeof(*src));
303  pix = emu_buf[j * 16 + bw - 1];
304  for (k = bw; k < mb_width; k++)
305  emu_buf[j * 16 + k] = pix;
306  }
307  for (; j < 16; j++)
308  memcpy(emu_buf + j * 16,
309  emu_buf + (bh - 1) * 16,
310  mb_width * sizeof(*emu_buf));
311  }
312  if (!is_chroma) {
313  ctx->fdct(&ctx->fdsp, esrc, elinesize, blocks);
314  blocks += 64;
315  if (blocks_per_mb > 2) {
316  ctx->fdct(&ctx->fdsp, esrc + 8, elinesize, blocks);
317  blocks += 64;
318  }
319  ctx->fdct(&ctx->fdsp, esrc + elinesize * 4, elinesize, blocks);
320  blocks += 64;
321  if (blocks_per_mb > 2) {
322  ctx->fdct(&ctx->fdsp, esrc + elinesize * 4 + 8, elinesize, blocks);
323  blocks += 64;
324  }
325  } else {
326  ctx->fdct(&ctx->fdsp, esrc, elinesize, blocks);
327  blocks += 64;
328  ctx->fdct(&ctx->fdsp, esrc + elinesize * 4, elinesize, blocks);
329  blocks += 64;
330  if (blocks_per_mb > 2) {
331  ctx->fdct(&ctx->fdsp, esrc + 8, elinesize, blocks);
332  blocks += 64;
333  ctx->fdct(&ctx->fdsp, esrc + elinesize * 4 + 8, elinesize, blocks);
334  blocks += 64;
335  }
336  }
337 
338  x += mb_width;
339  }
340 }
341 
342 static void get_alpha_data(ProresContext *ctx, const uint16_t *src,
343  ptrdiff_t linesize, int x, int y, int w, int h,
344  int16_t *blocks, int mbs_per_slice, int abits)
345 {
346  const int slice_width = 16 * mbs_per_slice;
347  int i, j, copy_w, copy_h;
348 
349  copy_w = FFMIN(w - x, slice_width);
350  copy_h = FFMIN(h - y, 16);
351  for (i = 0; i < copy_h; i++) {
352  memcpy(blocks, src, copy_w * sizeof(*src));
353  if (abits == 8)
354  for (j = 0; j < copy_w; j++)
355  blocks[j] >>= 2;
356  else
357  for (j = 0; j < copy_w; j++)
358  blocks[j] = (blocks[j] << 6) | (blocks[j] >> 4);
359  for (j = copy_w; j < slice_width; j++)
360  blocks[j] = blocks[copy_w - 1];
361  blocks += slice_width;
362  src += linesize >> 1;
363  }
364  for (; i < 16; i++) {
365  memcpy(blocks, blocks - slice_width, slice_width * sizeof(*blocks));
366  blocks += slice_width;
367  }
368 }
369 
370 /**
371  * Write an unsigned rice/exp golomb codeword.
372  */
373 static inline void encode_vlc_codeword(PutBitContext *pb, unsigned codebook, int val)
374 {
375  unsigned int rice_order, exp_order, switch_bits, switch_val;
376  int exponent;
377 
378  /* number of prefix bits to switch between Rice and expGolomb */
379  switch_bits = (codebook & 3) + 1;
380  rice_order = codebook >> 5; /* rice code order */
381  exp_order = (codebook >> 2) & 7; /* exp golomb code order */
382 
383  switch_val = switch_bits << rice_order;
384 
385  if (val >= switch_val) {
386  val -= switch_val - (1 << exp_order);
387  exponent = av_log2(val);
388 
389  put_bits(pb, exponent - exp_order + switch_bits, 0);
390  put_bits(pb, exponent + 1, val);
391  } else {
392  exponent = val >> rice_order;
393 
394  if (exponent)
395  put_bits(pb, exponent, 0);
396  put_bits(pb, 1, 1);
397  if (rice_order)
398  put_sbits(pb, rice_order, val);
399  }
400 }
401 
402 #define GET_SIGN(x) ((x) >> 31)
403 #define MAKE_CODE(x) ((((x)) * 2) ^ GET_SIGN(x))
404 
405 static void encode_dcs(PutBitContext *pb, int16_t *blocks,
406  int blocks_per_slice, int scale)
407 {
408  int i;
409  int codebook = 3, code, dc, prev_dc, delta, sign, new_sign;
410 
411  prev_dc = (blocks[0] - 0x4000) / scale;
413  sign = 0;
414  codebook = 3;
415  blocks += 64;
416 
417  for (i = 1; i < blocks_per_slice; i++, blocks += 64) {
418  dc = (blocks[0] - 0x4000) / scale;
419  delta = dc - prev_dc;
420  new_sign = GET_SIGN(delta);
421  delta = (delta ^ sign) - sign;
422  code = MAKE_CODE(delta);
423  encode_vlc_codeword(pb, ff_prores_dc_codebook[codebook], code);
424  codebook = (code + (code & 1)) >> 1;
425  codebook = FFMIN(codebook, 3);
426  sign = new_sign;
427  prev_dc = dc;
428  }
429 }
430 
431 static void encode_acs(PutBitContext *pb, int16_t *blocks,
432  int blocks_per_slice,
433  int plane_size_factor,
434  const uint8_t *scan, const int16_t *qmat)
435 {
436  int idx, i;
437  int run, level, run_cb, lev_cb;
438  int max_coeffs, abs_level;
439 
440  max_coeffs = blocks_per_slice << 6;
441  run_cb = ff_prores_run_to_cb_index[4];
442  lev_cb = ff_prores_lev_to_cb_index[2];
443  run = 0;
444 
445  for (i = 1; i < 64; i++) {
446  for (idx = scan[i]; idx < max_coeffs; idx += 64) {
447  level = blocks[idx] / qmat[scan[i]];
448  if (level) {
449  abs_level = FFABS(level);
450  encode_vlc_codeword(pb, ff_prores_ac_codebook[run_cb], run);
452  abs_level - 1);
453  put_sbits(pb, 1, GET_SIGN(level));
454 
455  run_cb = ff_prores_run_to_cb_index[FFMIN(run, 15)];
456  lev_cb = ff_prores_lev_to_cb_index[FFMIN(abs_level, 9)];
457  run = 0;
458  } else {
459  run++;
460  }
461  }
462  }
463 }
464 
466  const uint16_t *src, ptrdiff_t linesize,
467  int mbs_per_slice, int16_t *blocks,
468  int blocks_per_mb, int plane_size_factor,
469  const int16_t *qmat)
470 {
471  int blocks_per_slice, saved_pos;
472 
473  saved_pos = put_bits_count(pb);
474  blocks_per_slice = mbs_per_slice * blocks_per_mb;
475 
476  encode_dcs(pb, blocks, blocks_per_slice, qmat[0]);
477  encode_acs(pb, blocks, blocks_per_slice, plane_size_factor,
478  ctx->scantable, qmat);
479  flush_put_bits(pb);
480 
481  return (put_bits_count(pb) - saved_pos) >> 3;
482 }
483 
484 static void put_alpha_diff(PutBitContext *pb, int cur, int prev, int abits)
485 {
486  const int dbits = (abits == 8) ? 4 : 7;
487  const int dsize = 1 << dbits - 1;
488  int diff = cur - prev;
489 
490  diff = av_mod_uintp2(diff, abits);
491  if (diff >= (1 << abits) - dsize)
492  diff -= 1 << abits;
493  if (diff < -dsize || diff > dsize || !diff) {
494  put_bits(pb, 1, 1);
495  put_bits(pb, abits, diff);
496  } else {
497  put_bits(pb, 1, 0);
498  put_bits(pb, dbits - 1, FFABS(diff) - 1);
499  put_bits(pb, 1, diff < 0);
500  }
501 }
502 
503 static void put_alpha_run(PutBitContext *pb, int run)
504 {
505  if (run) {
506  put_bits(pb, 1, 0);
507  if (run < 0x10)
508  put_bits(pb, 4, run);
509  else
510  put_bits(pb, 15, run);
511  } else {
512  put_bits(pb, 1, 1);
513  }
514 }
515 
516 // todo alpha quantisation for high quants
518  int mbs_per_slice, uint16_t *blocks,
519  int quant)
520 {
521  const int abits = ctx->alpha_bits;
522  const int mask = (1 << abits) - 1;
523  const int num_coeffs = mbs_per_slice * 256;
524  int saved_pos = put_bits_count(pb);
525  int prev = mask, cur;
526  int idx = 0;
527  int run = 0;
528 
529  cur = blocks[idx++];
530  put_alpha_diff(pb, cur, prev, abits);
531  prev = cur;
532  do {
533  cur = blocks[idx++];
534  if (cur != prev) {
535  put_alpha_run (pb, run);
536  put_alpha_diff(pb, cur, prev, abits);
537  prev = cur;
538  run = 0;
539  } else {
540  run++;
541  }
542  } while (idx < num_coeffs);
543  if (run)
544  put_alpha_run(pb, run);
545  flush_put_bits(pb);
546  return (put_bits_count(pb) - saved_pos) >> 3;
547 }
548 
549 static int encode_slice(AVCodecContext *avctx, const AVFrame *pic,
550  PutBitContext *pb,
551  int sizes[4], int x, int y, int quant,
552  int mbs_per_slice)
553 {
554  ProresContext *ctx = avctx->priv_data;
555  int i, xp, yp;
556  int total_size = 0;
557  const uint16_t *src;
558  int slice_width_factor = av_log2(mbs_per_slice);
559  int num_cblocks, pwidth, line_add;
560  ptrdiff_t linesize;
561  int plane_factor, is_chroma;
562  uint16_t *qmat;
563  uint16_t *qmat_chroma;
564 
565  if (ctx->pictures_per_frame == 1)
566  line_add = 0;
567  else
568  line_add = ctx->cur_picture_idx ^ !pic->top_field_first;
569 
570  if (ctx->force_quant) {
571  qmat = ctx->quants[0];
572  qmat_chroma = ctx->quants_chroma[0];
573  } else if (quant < MAX_STORED_Q) {
574  qmat = ctx->quants[quant];
575  qmat_chroma = ctx->quants_chroma[quant];
576  } else {
577  qmat = ctx->custom_q;
578  qmat_chroma = ctx->custom_chroma_q;
579  for (i = 0; i < 64; i++) {
580  qmat[i] = ctx->quant_mat[i] * quant;
581  qmat_chroma[i] = ctx->quant_chroma_mat[i] * quant;
582  }
583  }
584 
585  for (i = 0; i < ctx->num_planes; i++) {
586  is_chroma = (i == 1 || i == 2);
587  plane_factor = slice_width_factor + 2;
588  if (is_chroma)
589  plane_factor += ctx->chroma_factor - 3;
590  if (!is_chroma || ctx->chroma_factor == CFACTOR_Y444) {
591  xp = x << 4;
592  yp = y << 4;
593  num_cblocks = 4;
594  pwidth = avctx->width;
595  } else {
596  xp = x << 3;
597  yp = y << 4;
598  num_cblocks = 2;
599  pwidth = avctx->width >> 1;
600  }
601 
602  linesize = pic->linesize[i] * ctx->pictures_per_frame;
603  src = (const uint16_t*)(pic->data[i] + yp * linesize +
604  line_add * pic->linesize[i]) + xp;
605 
606  if (i < 3) {
607  get_slice_data(ctx, src, linesize, xp, yp,
608  pwidth, avctx->height / ctx->pictures_per_frame,
609  ctx->blocks[0], ctx->emu_buf,
610  mbs_per_slice, num_cblocks, is_chroma);
611  if (!is_chroma) {/* luma quant */
612  sizes[i] = encode_slice_plane(ctx, pb, src, linesize,
613  mbs_per_slice, ctx->blocks[0],
614  num_cblocks, plane_factor,
615  qmat);
616  } else { /* chroma plane */
617  sizes[i] = encode_slice_plane(ctx, pb, src, linesize,
618  mbs_per_slice, ctx->blocks[0],
619  num_cblocks, plane_factor,
620  qmat_chroma);
621  }
622  } else {
623  get_alpha_data(ctx, src, linesize, xp, yp,
624  pwidth, avctx->height / ctx->pictures_per_frame,
625  ctx->blocks[0], mbs_per_slice, ctx->alpha_bits);
626  sizes[i] = encode_alpha_plane(ctx, pb, mbs_per_slice,
627  ctx->blocks[0], quant);
628  }
629  total_size += sizes[i];
630  if (put_bits_left(pb) < 0) {
631  av_log(avctx, AV_LOG_ERROR,
632  "Underestimated required buffer size.\n");
633  return AVERROR_BUG;
634  }
635  }
636  return total_size;
637 }
638 
639 static inline int estimate_vlc(unsigned codebook, int val)
640 {
641  unsigned int rice_order, exp_order, switch_bits, switch_val;
642  int exponent;
643 
644  /* number of prefix bits to switch between Rice and expGolomb */
645  switch_bits = (codebook & 3) + 1;
646  rice_order = codebook >> 5; /* rice code order */
647  exp_order = (codebook >> 2) & 7; /* exp golomb code order */
648 
649  switch_val = switch_bits << rice_order;
650 
651  if (val >= switch_val) {
652  val -= switch_val - (1 << exp_order);
653  exponent = av_log2(val);
654 
655  return exponent * 2 - exp_order + switch_bits + 1;
656  } else {
657  return (val >> rice_order) + rice_order + 1;
658  }
659 }
660 
661 static int estimate_dcs(int *error, int16_t *blocks, int blocks_per_slice,
662  int scale)
663 {
664  int i;
665  int codebook = 3, code, dc, prev_dc, delta, sign, new_sign;
666  int bits;
667 
668  prev_dc = (blocks[0] - 0x4000) / scale;
669  bits = estimate_vlc(FIRST_DC_CB, MAKE_CODE(prev_dc));
670  sign = 0;
671  codebook = 3;
672  blocks += 64;
673  *error += FFABS(blocks[0] - 0x4000) % scale;
674 
675  for (i = 1; i < blocks_per_slice; i++, blocks += 64) {
676  dc = (blocks[0] - 0x4000) / scale;
677  *error += FFABS(blocks[0] - 0x4000) % scale;
678  delta = dc - prev_dc;
679  new_sign = GET_SIGN(delta);
680  delta = (delta ^ sign) - sign;
681  code = MAKE_CODE(delta);
682  bits += estimate_vlc(ff_prores_dc_codebook[codebook], code);
683  codebook = (code + (code & 1)) >> 1;
684  codebook = FFMIN(codebook, 3);
685  sign = new_sign;
686  prev_dc = dc;
687  }
688 
689  return bits;
690 }
691 
692 static int estimate_acs(int *error, int16_t *blocks, int blocks_per_slice,
693  int plane_size_factor,
694  const uint8_t *scan, const int16_t *qmat)
695 {
696  int idx, i;
697  int run, level, run_cb, lev_cb;
698  int max_coeffs, abs_level;
699  int bits = 0;
700 
701  max_coeffs = blocks_per_slice << 6;
702  run_cb = ff_prores_run_to_cb_index[4];
703  lev_cb = ff_prores_lev_to_cb_index[2];
704  run = 0;
705 
706  for (i = 1; i < 64; i++) {
707  for (idx = scan[i]; idx < max_coeffs; idx += 64) {
708  level = blocks[idx] / qmat[scan[i]];
709  *error += FFABS(blocks[idx]) % qmat[scan[i]];
710  if (level) {
711  abs_level = FFABS(level);
712  bits += estimate_vlc(ff_prores_ac_codebook[run_cb], run);
713  bits += estimate_vlc(ff_prores_ac_codebook[lev_cb],
714  abs_level - 1) + 1;
715 
716  run_cb = ff_prores_run_to_cb_index[FFMIN(run, 15)];
717  lev_cb = ff_prores_lev_to_cb_index[FFMIN(abs_level, 9)];
718  run = 0;
719  } else {
720  run++;
721  }
722  }
723  }
724 
725  return bits;
726 }
727 
729  const uint16_t *src, ptrdiff_t linesize,
730  int mbs_per_slice,
731  int blocks_per_mb, int plane_size_factor,
732  const int16_t *qmat, ProresThreadData *td)
733 {
734  int blocks_per_slice;
735  int bits;
736 
737  blocks_per_slice = mbs_per_slice * blocks_per_mb;
738 
739  bits = estimate_dcs(error, td->blocks[plane], blocks_per_slice, qmat[0]);
740  bits += estimate_acs(error, td->blocks[plane], blocks_per_slice,
741  plane_size_factor, ctx->scantable, qmat);
742 
743  return FFALIGN(bits, 8);
744 }
745 
746 static int est_alpha_diff(int cur, int prev, int abits)
747 {
748  const int dbits = (abits == 8) ? 4 : 7;
749  const int dsize = 1 << dbits - 1;
750  int diff = cur - prev;
751 
752  diff = av_mod_uintp2(diff, abits);
753  if (diff >= (1 << abits) - dsize)
754  diff -= 1 << abits;
755  if (diff < -dsize || diff > dsize || !diff)
756  return abits + 1;
757  else
758  return dbits + 1;
759 }
760 
762  const uint16_t *src, ptrdiff_t linesize,
763  int mbs_per_slice, int16_t *blocks)
764 {
765  const int abits = ctx->alpha_bits;
766  const int mask = (1 << abits) - 1;
767  const int num_coeffs = mbs_per_slice * 256;
768  int prev = mask, cur;
769  int idx = 0;
770  int run = 0;
771  int bits;
772 
773  cur = blocks[idx++];
774  bits = est_alpha_diff(cur, prev, abits);
775  prev = cur;
776  do {
777  cur = blocks[idx++];
778  if (cur != prev) {
779  if (!run)
780  bits++;
781  else if (run < 0x10)
782  bits += 4;
783  else
784  bits += 15;
785  bits += est_alpha_diff(cur, prev, abits);
786  prev = cur;
787  run = 0;
788  } else {
789  run++;
790  }
791  } while (idx < num_coeffs);
792 
793  if (run) {
794  if (run < 0x10)
795  bits += 4;
796  else
797  bits += 15;
798  }
799 
800  return bits;
801 }
802 
804  int trellis_node, int x, int y, int mbs_per_slice,
806 {
807  ProresContext *ctx = avctx->priv_data;
808  int i, q, pq, xp, yp;
809  const uint16_t *src;
810  int slice_width_factor = av_log2(mbs_per_slice);
811  int num_cblocks[MAX_PLANES], pwidth;
812  int plane_factor[MAX_PLANES], is_chroma[MAX_PLANES];
813  const int min_quant = ctx->profile_info->min_quant;
814  const int max_quant = ctx->profile_info->max_quant;
815  int error, bits, bits_limit;
816  int mbs, prev, cur, new_score;
817  int slice_bits[TRELLIS_WIDTH], slice_score[TRELLIS_WIDTH];
818  int overquant;
819  uint16_t *qmat;
820  uint16_t *qmat_chroma;
821  int linesize[4], line_add;
822  int alpha_bits = 0;
823 
824  if (ctx->pictures_per_frame == 1)
825  line_add = 0;
826  else
827  line_add = ctx->cur_picture_idx ^ !ctx->pic->top_field_first;
828  mbs = x + mbs_per_slice;
829 
830  for (i = 0; i < ctx->num_planes; i++) {
831  is_chroma[i] = (i == 1 || i == 2);
832  plane_factor[i] = slice_width_factor + 2;
833  if (is_chroma[i])
834  plane_factor[i] += ctx->chroma_factor - 3;
835  if (!is_chroma[i] || ctx->chroma_factor == CFACTOR_Y444) {
836  xp = x << 4;
837  yp = y << 4;
838  num_cblocks[i] = 4;
839  pwidth = avctx->width;
840  } else {
841  xp = x << 3;
842  yp = y << 4;
843  num_cblocks[i] = 2;
844  pwidth = avctx->width >> 1;
845  }
846 
847  linesize[i] = ctx->pic->linesize[i] * ctx->pictures_per_frame;
848  src = (const uint16_t *)(ctx->pic->data[i] + yp * linesize[i] +
849  line_add * ctx->pic->linesize[i]) + xp;
850 
851  if (i < 3) {
852  get_slice_data(ctx, src, linesize[i], xp, yp,
853  pwidth, avctx->height / ctx->pictures_per_frame,
854  td->blocks[i], td->emu_buf,
855  mbs_per_slice, num_cblocks[i], is_chroma[i]);
856  } else {
857  get_alpha_data(ctx, src, linesize[i], xp, yp,
858  pwidth, avctx->height / ctx->pictures_per_frame,
859  td->blocks[i], mbs_per_slice, ctx->alpha_bits);
860  }
861  }
862 
863  for (q = min_quant; q < max_quant + 2; q++) {
864  td->nodes[trellis_node + q].prev_node = -1;
865  td->nodes[trellis_node + q].quant = q;
866  }
867 
868  if (ctx->alpha_bits)
869  alpha_bits = estimate_alpha_plane(ctx, src, linesize[3],
870  mbs_per_slice, td->blocks[3]);
871  // todo: maybe perform coarser quantising to fit into frame size when needed
872  for (q = min_quant; q <= max_quant; q++) {
873  bits = alpha_bits;
874  error = 0;
875  bits += estimate_slice_plane(ctx, &error, 0,
876  src, linesize[0],
877  mbs_per_slice,
878  num_cblocks[0], plane_factor[0],
879  ctx->quants[q], td); /* estimate luma plane */
880  for (i = 1; i < ctx->num_planes - !!ctx->alpha_bits; i++) { /* estimate chroma plane */
881  bits += estimate_slice_plane(ctx, &error, i,
882  src, linesize[i],
883  mbs_per_slice,
884  num_cblocks[i], plane_factor[i],
885  ctx->quants_chroma[q], td);
886  }
887  if (bits > 65000 * 8)
888  error = SCORE_LIMIT;
889 
890  slice_bits[q] = bits;
891  slice_score[q] = error;
892  }
893  if (slice_bits[max_quant] <= ctx->bits_per_mb * mbs_per_slice) {
894  slice_bits[max_quant + 1] = slice_bits[max_quant];
895  slice_score[max_quant + 1] = slice_score[max_quant] + 1;
896  overquant = max_quant;
897  } else {
898  for (q = max_quant + 1; q < 128; q++) {
899  bits = alpha_bits;
900  error = 0;
901  if (q < MAX_STORED_Q) {
902  qmat = ctx->quants[q];
903  qmat_chroma = ctx->quants_chroma[q];
904  } else {
905  qmat = td->custom_q;
906  qmat_chroma = td->custom_chroma_q;
907  for (i = 0; i < 64; i++) {
908  qmat[i] = ctx->quant_mat[i] * q;
909  qmat_chroma[i] = ctx->quant_chroma_mat[i] * q;
910  }
911  }
912  bits += estimate_slice_plane(ctx, &error, 0,
913  src, linesize[0],
914  mbs_per_slice,
915  num_cblocks[0], plane_factor[0],
916  qmat, td);/* estimate luma plane */
917  for (i = 1; i < ctx->num_planes - !!ctx->alpha_bits; i++) { /* estimate chroma plane */
918  bits += estimate_slice_plane(ctx, &error, i,
919  src, linesize[i],
920  mbs_per_slice,
921  num_cblocks[i], plane_factor[i],
922  qmat_chroma, td);
923  }
924  if (bits <= ctx->bits_per_mb * mbs_per_slice)
925  break;
926  }
927 
928  slice_bits[max_quant + 1] = bits;
929  slice_score[max_quant + 1] = error;
930  overquant = q;
931  }
932  td->nodes[trellis_node + max_quant + 1].quant = overquant;
933 
934  bits_limit = mbs * ctx->bits_per_mb;
935  for (pq = min_quant; pq < max_quant + 2; pq++) {
936  prev = trellis_node - TRELLIS_WIDTH + pq;
937 
938  for (q = min_quant; q < max_quant + 2; q++) {
939  cur = trellis_node + q;
940 
941  bits = td->nodes[prev].bits + slice_bits[q];
942  error = slice_score[q];
943  if (bits > bits_limit)
944  error = SCORE_LIMIT;
945 
946  if (td->nodes[prev].score < SCORE_LIMIT && error < SCORE_LIMIT)
947  new_score = td->nodes[prev].score + error;
948  else
949  new_score = SCORE_LIMIT;
950  if (td->nodes[cur].prev_node == -1 ||
951  td->nodes[cur].score >= new_score) {
952 
953  td->nodes[cur].bits = bits;
954  td->nodes[cur].score = new_score;
955  td->nodes[cur].prev_node = prev;
956  }
957  }
958  }
959 
960  error = td->nodes[trellis_node + min_quant].score;
961  pq = trellis_node + min_quant;
962  for (q = min_quant + 1; q < max_quant + 2; q++) {
963  if (td->nodes[trellis_node + q].score <= error) {
964  error = td->nodes[trellis_node + q].score;
965  pq = trellis_node + q;
966  }
967  }
968 
969  return pq;
970 }
971 
972 static int find_quant_thread(AVCodecContext *avctx, void *arg,
973  int jobnr, int threadnr)
974 {
975  ProresContext *ctx = avctx->priv_data;
976  ProresThreadData *td = ctx->tdata + threadnr;
977  int mbs_per_slice = ctx->mbs_per_slice;
978  int x, y = jobnr, mb, q = 0;
979 
980  for (x = mb = 0; x < ctx->mb_width; x += mbs_per_slice, mb++) {
981  while (ctx->mb_width - x < mbs_per_slice)
982  mbs_per_slice >>= 1;
983  q = find_slice_quant(avctx,
984  (mb + 1) * TRELLIS_WIDTH, x, y,
985  mbs_per_slice, td);
986  }
987 
988  for (x = ctx->slices_width - 1; x >= 0; x--) {
989  ctx->slice_q[x + y * ctx->slices_width] = td->nodes[q].quant;
990  q = td->nodes[q].prev_node;
991  }
992 
993  return 0;
994 }
995 
997  const AVFrame *pic, int *got_packet)
998 {
999  ProresContext *ctx = avctx->priv_data;
1000  uint8_t *orig_buf, *buf, *slice_hdr, *slice_sizes, *tmp;
1001  uint8_t *picture_size_pos;
1002  PutBitContext pb;
1003  int x, y, i, mb, q = 0;
1004  int sizes[4] = { 0 };
1005  int slice_hdr_size = 2 + 2 * (ctx->num_planes - 1);
1006  int frame_size, picture_size, slice_size;
1007  int pkt_size, ret;
1008  int max_slice_size = (ctx->frame_size_upper_bound - 200) / (ctx->pictures_per_frame * ctx->slices_per_picture + 1);
1009  uint8_t frame_flags;
1010 
1011  ctx->pic = pic;
1012  pkt_size = ctx->frame_size_upper_bound;
1013 
1014  if ((ret = ff_alloc_packet2(avctx, pkt, pkt_size + AV_INPUT_BUFFER_MIN_SIZE, 0)) < 0)
1015  return ret;
1016 
1017  orig_buf = pkt->data;
1018 
1019  // frame atom
1020  orig_buf += 4; // frame size
1021  bytestream_put_be32 (&orig_buf, FRAME_ID); // frame container ID
1022  buf = orig_buf;
1023 
1024  // frame header
1025  tmp = buf;
1026  buf += 2; // frame header size will be stored here
1027  bytestream_put_be16 (&buf, 0); // version 1
1028  bytestream_put_buffer(&buf, ctx->vendor, 4);
1029  bytestream_put_be16 (&buf, avctx->width);
1030  bytestream_put_be16 (&buf, avctx->height);
1031 
1032  frame_flags = ctx->chroma_factor << 6;
1033  if (avctx->flags & AV_CODEC_FLAG_INTERLACED_DCT)
1034  frame_flags |= pic->top_field_first ? 0x04 : 0x08;
1035  bytestream_put_byte (&buf, frame_flags);
1036 
1037  bytestream_put_byte (&buf, 0); // reserved
1038  bytestream_put_byte (&buf, pic->color_primaries);
1039  bytestream_put_byte (&buf, pic->color_trc);
1040  bytestream_put_byte (&buf, pic->colorspace);
1041  bytestream_put_byte (&buf, 0x40 | (ctx->alpha_bits >> 3));
1042  bytestream_put_byte (&buf, 0); // reserved
1043  if (ctx->quant_sel != QUANT_MAT_DEFAULT) {
1044  bytestream_put_byte (&buf, 0x03); // matrix flags - both matrices are present
1045  // luma quantisation matrix
1046  for (i = 0; i < 64; i++)
1047  bytestream_put_byte(&buf, ctx->quant_mat[i]);
1048  // chroma quantisation matrix
1049  for (i = 0; i < 64; i++)
1050  bytestream_put_byte(&buf, ctx->quant_mat[i]);
1051  } else {
1052  bytestream_put_byte (&buf, 0x00); // matrix flags - default matrices are used
1053  }
1054  bytestream_put_be16 (&tmp, buf - orig_buf); // write back frame header size
1055 
1056  for (ctx->cur_picture_idx = 0;
1057  ctx->cur_picture_idx < ctx->pictures_per_frame;
1058  ctx->cur_picture_idx++) {
1059  // picture header
1060  picture_size_pos = buf + 1;
1061  bytestream_put_byte (&buf, 0x40); // picture header size (in bits)
1062  buf += 4; // picture data size will be stored here
1063  bytestream_put_be16 (&buf, ctx->slices_per_picture);
1064  bytestream_put_byte (&buf, av_log2(ctx->mbs_per_slice) << 4); // slice width and height in MBs
1065 
1066  // seek table - will be filled during slice encoding
1067  slice_sizes = buf;
1068  buf += ctx->slices_per_picture * 2;
1069 
1070  // slices
1071  if (!ctx->force_quant) {
1072  ret = avctx->execute2(avctx, find_quant_thread, (void*)pic, NULL,
1073  ctx->mb_height);
1074  if (ret)
1075  return ret;
1076  }
1077 
1078  for (y = 0; y < ctx->mb_height; y++) {
1079  int mbs_per_slice = ctx->mbs_per_slice;
1080  for (x = mb = 0; x < ctx->mb_width; x += mbs_per_slice, mb++) {
1081  q = ctx->force_quant ? ctx->force_quant
1082  : ctx->slice_q[mb + y * ctx->slices_width];
1083 
1084  while (ctx->mb_width - x < mbs_per_slice)
1085  mbs_per_slice >>= 1;
1086 
1087  bytestream_put_byte(&buf, slice_hdr_size << 3);
1088  slice_hdr = buf;
1089  buf += slice_hdr_size - 1;
1090  if (pkt_size <= buf - orig_buf + 2 * max_slice_size) {
1091  uint8_t *start = pkt->data;
1092  // Recompute new size according to max_slice_size
1093  // and deduce delta
1094  int delta = 200 + (ctx->pictures_per_frame *
1095  ctx->slices_per_picture + 1) *
1096  max_slice_size - pkt_size;
1097 
1098  delta = FFMAX(delta, 2 * max_slice_size);
1099  ctx->frame_size_upper_bound += delta;
1100 
1101  if (!ctx->warn) {
1102  avpriv_request_sample(avctx,
1103  "Packet too small: is %i,"
1104  " needs %i (slice: %i). "
1105  "Correct allocation",
1106  pkt_size, delta, max_slice_size);
1107  ctx->warn = 1;
1108  }
1109 
1110  ret = av_grow_packet(pkt, delta);
1111  if (ret < 0)
1112  return ret;
1113 
1114  pkt_size += delta;
1115  // restore pointers
1116  orig_buf = pkt->data + (orig_buf - start);
1117  buf = pkt->data + (buf - start);
1118  picture_size_pos = pkt->data + (picture_size_pos - start);
1119  slice_sizes = pkt->data + (slice_sizes - start);
1120  slice_hdr = pkt->data + (slice_hdr - start);
1121  tmp = pkt->data + (tmp - start);
1122  }
1123  init_put_bits(&pb, buf, (pkt_size - (buf - orig_buf)));
1124  ret = encode_slice(avctx, pic, &pb, sizes, x, y, q,
1125  mbs_per_slice);
1126  if (ret < 0)
1127  return ret;
1128 
1129  bytestream_put_byte(&slice_hdr, q);
1130  slice_size = slice_hdr_size + sizes[ctx->num_planes - 1];
1131  for (i = 0; i < ctx->num_planes - 1; i++) {
1132  bytestream_put_be16(&slice_hdr, sizes[i]);
1133  slice_size += sizes[i];
1134  }
1135  bytestream_put_be16(&slice_sizes, slice_size);
1136  buf += slice_size - slice_hdr_size;
1137  if (max_slice_size < slice_size)
1138  max_slice_size = slice_size;
1139  }
1140  }
1141 
1142  picture_size = buf - (picture_size_pos - 1);
1143  bytestream_put_be32(&picture_size_pos, picture_size);
1144  }
1145 
1146  orig_buf -= 8;
1147  frame_size = buf - orig_buf;
1148  bytestream_put_be32(&orig_buf, frame_size);
1149 
1150  pkt->size = frame_size;
1151  pkt->flags |= AV_PKT_FLAG_KEY;
1152  *got_packet = 1;
1153 
1154  return 0;
1155 }
1156 
1158 {
1159  ProresContext *ctx = avctx->priv_data;
1160  int i;
1161 
1162  if (ctx->tdata) {
1163  for (i = 0; i < avctx->thread_count; i++)
1164  av_freep(&ctx->tdata[i].nodes);
1165  }
1166  av_freep(&ctx->tdata);
1167  av_freep(&ctx->slice_q);
1168 
1169  return 0;
1170 }
1171 
1172 static void prores_fdct(FDCTDSPContext *fdsp, const uint16_t *src,
1173  ptrdiff_t linesize, int16_t *block)
1174 {
1175  int x, y;
1176  const uint16_t *tsrc = src;
1177 
1178  for (y = 0; y < 8; y++) {
1179  for (x = 0; x < 8; x++)
1180  block[y * 8 + x] = tsrc[x];
1181  tsrc += linesize >> 1;
1182  }
1183  fdsp->fdct(block);
1184 }
1185 
1187 {
1188  ProresContext *ctx = avctx->priv_data;
1189  int mps;
1190  int i, j;
1191  int min_quant, max_quant;
1192  int interlaced = !!(avctx->flags & AV_CODEC_FLAG_INTERLACED_DCT);
1193 
1194  avctx->bits_per_raw_sample = 10;
1195 #if FF_API_CODED_FRAME
1198  avctx->coded_frame->key_frame = 1;
1200 #endif
1201 
1202  ctx->fdct = prores_fdct;
1203  ctx->scantable = interlaced ? ff_prores_interlaced_scan
1205  ff_fdctdsp_init(&ctx->fdsp, avctx);
1206 
1207  mps = ctx->mbs_per_slice;
1208  if (mps & (mps - 1)) {
1209  av_log(avctx, AV_LOG_ERROR,
1210  "there should be an integer power of two MBs per slice\n");
1211  return AVERROR(EINVAL);
1212  }
1213  if (ctx->profile == PRORES_PROFILE_AUTO) {
1215  ctx->profile = (desc->flags & AV_PIX_FMT_FLAG_ALPHA ||
1216  !(desc->log2_chroma_w + desc->log2_chroma_h))
1218  av_log(avctx, AV_LOG_INFO, "Autoselected %s. It can be overridden "
1219  "through -profile option.\n", ctx->profile == PRORES_PROFILE_4444
1220  ? "4:4:4:4 profile because of the used input colorspace"
1221  : "HQ profile to keep best quality");
1222  }
1224  if (ctx->profile != PRORES_PROFILE_4444 &&
1225  ctx->profile != PRORES_PROFILE_4444XQ) {
1226  // force alpha and warn
1227  av_log(avctx, AV_LOG_WARNING, "Profile selected will not "
1228  "encode alpha. Override with -profile if needed.\n");
1229  ctx->alpha_bits = 0;
1230  }
1231  if (ctx->alpha_bits & 7) {
1232  av_log(avctx, AV_LOG_ERROR, "alpha bits should be 0, 8 or 16\n");
1233  return AVERROR(EINVAL);
1234  }
1235  avctx->bits_per_coded_sample = 32;
1236  } else {
1237  ctx->alpha_bits = 0;
1238  }
1239 
1240  ctx->chroma_factor = avctx->pix_fmt == AV_PIX_FMT_YUV422P10
1241  ? CFACTOR_Y422
1242  : CFACTOR_Y444;
1244  ctx->num_planes = 3 + !!ctx->alpha_bits;
1245 
1246  ctx->mb_width = FFALIGN(avctx->width, 16) >> 4;
1247 
1248  if (interlaced)
1249  ctx->mb_height = FFALIGN(avctx->height, 32) >> 5;
1250  else
1251  ctx->mb_height = FFALIGN(avctx->height, 16) >> 4;
1252 
1253  ctx->slices_width = ctx->mb_width / mps;
1254  ctx->slices_width += av_popcount(ctx->mb_width - ctx->slices_width * mps);
1255  ctx->slices_per_picture = ctx->mb_height * ctx->slices_width;
1256  ctx->pictures_per_frame = 1 + interlaced;
1257 
1258  if (ctx->quant_sel == -1) {
1261  } else {
1264  }
1265 
1266  if (strlen(ctx->vendor) != 4) {
1267  av_log(avctx, AV_LOG_ERROR, "vendor ID should be 4 bytes\n");
1268  return AVERROR_INVALIDDATA;
1269  }
1270 
1271  ctx->force_quant = avctx->global_quality / FF_QP2LAMBDA;
1272  if (!ctx->force_quant) {
1273  if (!ctx->bits_per_mb) {
1274  for (i = 0; i < NUM_MB_LIMITS - 1; i++)
1275  if (prores_mb_limits[i] >= ctx->mb_width * ctx->mb_height *
1276  ctx->pictures_per_frame)
1277  break;
1278  ctx->bits_per_mb = ctx->profile_info->br_tab[i];
1279  if (ctx->alpha_bits)
1280  ctx->bits_per_mb *= 20;
1281  } else if (ctx->bits_per_mb < 128) {
1282  av_log(avctx, AV_LOG_ERROR, "too few bits per MB, please set at least 128\n");
1283  return AVERROR_INVALIDDATA;
1284  }
1285 
1286  min_quant = ctx->profile_info->min_quant;
1287  max_quant = ctx->profile_info->max_quant;
1288  for (i = min_quant; i < MAX_STORED_Q; i++) {
1289  for (j = 0; j < 64; j++) {
1290  ctx->quants[i][j] = ctx->quant_mat[j] * i;
1291  ctx->quants_chroma[i][j] = ctx->quant_chroma_mat[j] * i;
1292  }
1293  }
1294 
1295  ctx->slice_q = av_malloc(ctx->slices_per_picture * sizeof(*ctx->slice_q));
1296  if (!ctx->slice_q) {
1297  encode_close(avctx);
1298  return AVERROR(ENOMEM);
1299  }
1300 
1301  ctx->tdata = av_mallocz(avctx->thread_count * sizeof(*ctx->tdata));
1302  if (!ctx->tdata) {
1303  encode_close(avctx);
1304  return AVERROR(ENOMEM);
1305  }
1306 
1307  for (j = 0; j < avctx->thread_count; j++) {
1308  ctx->tdata[j].nodes = av_malloc((ctx->slices_width + 1)
1309  * TRELLIS_WIDTH
1310  * sizeof(*ctx->tdata->nodes));
1311  if (!ctx->tdata[j].nodes) {
1312  encode_close(avctx);
1313  return AVERROR(ENOMEM);
1314  }
1315  for (i = min_quant; i < max_quant + 2; i++) {
1316  ctx->tdata[j].nodes[i].prev_node = -1;
1317  ctx->tdata[j].nodes[i].bits = 0;
1318  ctx->tdata[j].nodes[i].score = 0;
1319  }
1320  }
1321  } else {
1322  int ls = 0;
1323  int ls_chroma = 0;
1324 
1325  if (ctx->force_quant > 64) {
1326  av_log(avctx, AV_LOG_ERROR, "too large quantiser, maximum is 64\n");
1327  return AVERROR_INVALIDDATA;
1328  }
1329 
1330  for (j = 0; j < 64; j++) {
1331  ctx->quants[0][j] = ctx->quant_mat[j] * ctx->force_quant;
1332  ctx->quants_chroma[0][j] = ctx->quant_chroma_mat[j] * ctx->force_quant;
1333  ls += av_log2((1 << 11) / ctx->quants[0][j]) * 2 + 1;
1334  ls_chroma += av_log2((1 << 11) / ctx->quants_chroma[0][j]) * 2 + 1;
1335  }
1336 
1337  ctx->bits_per_mb = ls * 4 + ls_chroma * 4;
1338  if (ctx->chroma_factor == CFACTOR_Y444)
1339  ctx->bits_per_mb += ls_chroma * 4;
1340  }
1341 
1343  ctx->slices_per_picture + 1) *
1344  (2 + 2 * ctx->num_planes +
1345  (mps * ctx->bits_per_mb) / 8)
1346  + 200;
1347 
1348  if (ctx->alpha_bits) {
1349  // The alpha plane is run-coded and might exceed the bit budget.
1351  ctx->slices_per_picture + 1) *
1352  /* num pixels per slice */ (ctx->mbs_per_slice * 256 *
1353  /* bits per pixel */ (1 + ctx->alpha_bits + 1) + 7 >> 3);
1354  }
1355 
1356  avctx->codec_tag = ctx->profile_info->tag;
1357 
1358  av_log(avctx, AV_LOG_DEBUG,
1359  "profile %d, %d slices, interlacing: %s, %d bits per MB\n",
1360  ctx->profile, ctx->slices_per_picture * ctx->pictures_per_frame,
1361  interlaced ? "yes" : "no", ctx->bits_per_mb);
1362  av_log(avctx, AV_LOG_DEBUG, "frame size upper bound: %d\n",
1363  ctx->frame_size_upper_bound);
1364 
1365  return 0;
1366 }
1367 
1368 #define OFFSET(x) offsetof(ProresContext, x)
1369 #define VE AV_OPT_FLAG_VIDEO_PARAM | AV_OPT_FLAG_ENCODING_PARAM
1370 
1371 static const AVOption options[] = {
1372  { "mbs_per_slice", "macroblocks per slice", OFFSET(mbs_per_slice),
1373  AV_OPT_TYPE_INT, { .i64 = 8 }, 1, MAX_MBS_PER_SLICE, VE },
1374  { "profile", NULL, OFFSET(profile), AV_OPT_TYPE_INT,
1375  { .i64 = PRORES_PROFILE_AUTO },
1377  { "auto", NULL, 0, AV_OPT_TYPE_CONST, { .i64 = PRORES_PROFILE_AUTO },
1378  0, 0, VE, "profile" },
1379  { "proxy", NULL, 0, AV_OPT_TYPE_CONST, { .i64 = PRORES_PROFILE_PROXY },
1380  0, 0, VE, "profile" },
1381  { "lt", NULL, 0, AV_OPT_TYPE_CONST, { .i64 = PRORES_PROFILE_LT },
1382  0, 0, VE, "profile" },
1383  { "standard", NULL, 0, AV_OPT_TYPE_CONST, { .i64 = PRORES_PROFILE_STANDARD },
1384  0, 0, VE, "profile" },
1385  { "hq", NULL, 0, AV_OPT_TYPE_CONST, { .i64 = PRORES_PROFILE_HQ },
1386  0, 0, VE, "profile" },
1387  { "4444", NULL, 0, AV_OPT_TYPE_CONST, { .i64 = PRORES_PROFILE_4444 },
1388  0, 0, VE, "profile" },
1389  { "4444xq", NULL, 0, AV_OPT_TYPE_CONST, { .i64 = PRORES_PROFILE_4444XQ },
1390  0, 0, VE, "profile" },
1391  { "vendor", "vendor ID", OFFSET(vendor),
1392  AV_OPT_TYPE_STRING, { .str = "Lavc" }, CHAR_MIN, CHAR_MAX, VE },
1393  { "bits_per_mb", "desired bits per macroblock", OFFSET(bits_per_mb),
1394  AV_OPT_TYPE_INT, { .i64 = 0 }, 0, 8192, VE },
1395  { "quant_mat", "quantiser matrix", OFFSET(quant_sel), AV_OPT_TYPE_INT,
1396  { .i64 = -1 }, -1, QUANT_MAT_DEFAULT, VE, "quant_mat" },
1397  { "auto", NULL, 0, AV_OPT_TYPE_CONST, { .i64 = -1 },
1398  0, 0, VE, "quant_mat" },
1399  { "proxy", NULL, 0, AV_OPT_TYPE_CONST, { .i64 = QUANT_MAT_PROXY },
1400  0, 0, VE, "quant_mat" },
1401  { "lt", NULL, 0, AV_OPT_TYPE_CONST, { .i64 = QUANT_MAT_LT },
1402  0, 0, VE, "quant_mat" },
1403  { "standard", NULL, 0, AV_OPT_TYPE_CONST, { .i64 = QUANT_MAT_STANDARD },
1404  0, 0, VE, "quant_mat" },
1405  { "hq", NULL, 0, AV_OPT_TYPE_CONST, { .i64 = QUANT_MAT_HQ },
1406  0, 0, VE, "quant_mat" },
1407  { "default", NULL, 0, AV_OPT_TYPE_CONST, { .i64 = QUANT_MAT_DEFAULT },
1408  0, 0, VE, "quant_mat" },
1409  { "alpha_bits", "bits for alpha plane", OFFSET(alpha_bits), AV_OPT_TYPE_INT,
1410  { .i64 = 16 }, 0, 16, VE },
1411  { NULL }
1412 };
1413 
1414 static const AVClass proresenc_class = {
1415  .class_name = "ProRes encoder",
1416  .item_name = av_default_item_name,
1417  .option = options,
1418  .version = LIBAVUTIL_VERSION_INT,
1419 };
1420 
1422  .name = "prores_ks",
1423  .long_name = NULL_IF_CONFIG_SMALL("Apple ProRes (iCodec Pro)"),
1424  .type = AVMEDIA_TYPE_VIDEO,
1425  .id = AV_CODEC_ID_PRORES,
1426  .priv_data_size = sizeof(ProresContext),
1427  .init = encode_init,
1428  .close = encode_close,
1429  .encode2 = encode_frame,
1431  .pix_fmts = (const enum AVPixelFormat[]) {
1434  },
1435  .priv_class = &proresenc_class,
1436 };
static const AVClass proresenc_class
int plane
Definition: avisynth_c.h:422
#define MAX_MBS_PER_SLICE
#define NULL
Definition: coverity.c:32
#define CFACTOR_Y444
const char const char void * val
Definition: avisynth_c.h:771
static int encode_frame(AVCodecContext *avctx, AVPacket *pkt, const AVFrame *pic, int *got_packet)
#define AVERROR_INVALIDDATA
Invalid data found when processing input.
Definition: error.h:59
const AVPixFmtDescriptor * av_pix_fmt_desc_get(enum AVPixelFormat pix_fmt)
Definition: pixdesc.c:2446
This structure describes decoded (raw) audio or video data.
Definition: frame.h:226
int16_t quants_chroma[MAX_STORED_Q][64]
AVOption.
Definition: opt.h:246
#define AV_CODEC_FLAG_INTERLACED_DCT
Use interlaced DCT.
Definition: avcodec.h:883
static void put_sbits(PutBitContext *pb, int n, int32_t value)
Definition: put_bits.h:240
static av_cold int encode_init(AVCodecContext *avctx)
static void put_bits(Jpeg2000EncoderContext *s, int val, int n)
put n times val bit
Definition: j2kenc.c:208
#define AV_LOG_WARNING
Something somehow does not look correct.
Definition: log.h:182
#define LIBAVUTIL_VERSION_INT
Definition: version.h:85
const uint8_t ff_prores_ac_codebook[7]
Definition: proresdata.c:55
const char * desc
Definition: nvenc.c:65
static av_cold int init(AVCodecContext *avctx)
Definition: avrndec.c:35
#define avpriv_request_sample(...)
static int estimate_acs(int *error, int16_t *blocks, int blocks_per_slice, int plane_size_factor, const uint8_t *scan, const int16_t *qmat)
int16_t custom_chroma_q[64]
int size
Definition: avcodec.h:1446
const char * av_default_item_name(void *ptr)
Return the context name.
Definition: log.c:191
static void prores_fdct(FDCTDSPContext *fdsp, const uint16_t *src, ptrdiff_t linesize, int16_t *block)
enum AVPixelFormat pix_fmt
Pixel format, see AV_PIX_FMT_xxx.
Definition: avcodec.h:1743
uint16_t emu_buf[16 *16]
unsigned mb_height
height of the current picture in mb
Definition: proresdec.h:47
const uint8_t * scantable
static const AVOption options[]
uint8_t run
Definition: svq3.c:206
static AVPacket pkt
int bits_per_raw_sample
Bits per sample/pixel of internal libavcodec pixel/sample format.
Definition: avcodec.h:2757
static av_cold int encode_close(AVCodecContext *avctx)
const uint8_t * quant_chroma_mat
av_cold void ff_fdctdsp_init(FDCTDSPContext *c, AVCodecContext *avctx)
Definition: fdctdsp.c:26
static void get_slice_data(ProresContext *ctx, const uint16_t *src, ptrdiff_t linesize, int x, int y, int w, int h, int16_t *blocks, uint16_t *emu_buf, int mbs_per_slice, int blocks_per_mb, int is_chroma)
#define src
Definition: vp8dsp.c:254
AVCodec.
Definition: avcodec.h:3424
uint8_t log2_chroma_w
Amount to shift the luma width right to find the chroma width.
Definition: pixdesc.h:92
int16_t quants[MAX_STORED_Q][64]
#define AV_CODEC_CAP_INTRA_ONLY
Codec is intra only.
Definition: avcodec.h:1054
static int16_t block[64]
Definition: dct.c:115
const char * class_name
The name of the class; usually it is the same name as the context structure type to which the AVClass...
Definition: log.h:72
int ff_alloc_packet2(AVCodecContext *avctx, AVPacket *avpkt, int64_t size, int64_t min_size)
Check AVPacket size and/or allocate data.
Definition: encode.c:32
const char * full_name
uint8_t
#define av_cold
Definition: attributes.h:82
#define AV_PIX_FMT_FLAG_ALPHA
The pixel format has an alpha channel.
Definition: pixdesc.h:177
#define av_malloc(s)
#define mb
float delta
AVOptions.
static int estimate_vlc(unsigned codebook, int val)
static int encode_slice(AVCodecContext *avctx, const AVFrame *pic, PutBitContext *pb, int sizes[4], int x, int y, int quant, int mbs_per_slice)
#define DECLARE_ALIGNED(n, t, v)
Declare a variable that is aligned in memory.
Definition: mem.h:112
uint8_t * data
Definition: avcodec.h:1445
const uint8_t ff_prores_run_to_cb_index[16]
Lookup tables for adaptive switching between codebooks according with previous run/level value...
Definition: proresdata.c:69
const uint8_t ff_prores_lev_to_cb_index[10]
Definition: proresdata.c:72
int bits_per_coded_sample
bits per sample/pixel from the demuxer (needed for huffyuv).
Definition: avcodec.h:2750
static void encode_acs(PutBitContext *pb, int16_t *blocks, int blocks_per_slice, int plane_size_factor, const uint8_t *scan, const int16_t *qmat)
static int estimate_alpha_plane(ProresContext *ctx, const uint16_t *src, ptrdiff_t linesize, int mbs_per_slice, int16_t *blocks)
#define AV_INPUT_BUFFER_MIN_SIZE
minimum encoding buffer size Used to avoid some checks during header writing.
Definition: avcodec.h:789
#define FFALIGN(x, a)
Definition: macros.h:48
#define av_log(a,...)
static int est_alpha_diff(int cur, int prev, int abits)
#define AV_PKT_FLAG_KEY
The packet contains a keyframe.
Definition: avcodec.h:1477
int16_t blocks[MAX_PLANES][64 *4 *MAX_MBS_PER_SLICE]
unsigned mb_width
width of the current picture in mb
Definition: proresdec.h:46
#define i(width, name, range_min, range_max)
Definition: cbs_h2645.c:258
#define AV_LOG_ERROR
Something went wrong and cannot losslessly be recovered.
Definition: log.h:176
#define td
Definition: regdef.h:70
int16_t custom_chroma_q[64]
uint8_t log2_chroma_h
Amount to shift the luma height right to find the chroma height.
Definition: pixdesc.h:101
static int put_bits_left(PutBitContext *s)
Definition: put_bits.h:93
static const uint16_t mask[17]
Definition: lzw.c:38
static const int sizes[][2]
Definition: img2dec.c:52
#define AVERROR(e)
Definition: error.h:43
#define NULL_IF_CONFIG_SMALL(x)
Return NULL if CONFIG_SMALL is true, otherwise the argument without modification. ...
Definition: internal.h:188
int16_t custom_q[64]
#define AV_LOG_DEBUG
Stuff which is only useful for libav* developers.
Definition: log.h:197
const struct prores_profile * profile_info
uint16_t emu_buf[16 *16]
const char * arg
Definition: jacosubdec.c:66
int flags
AV_CODEC_FLAG_*.
Definition: avcodec.h:1613
enum AVColorSpace colorspace
YUV colorspace type.
Definition: frame.h:482
static const struct prores_profile prores_profile_info[6]
void * av_mallocz(size_t size)
Allocate a memory block with alignment suitable for all memory accesses (including vectors if availab...
Definition: mem.c:236
const char * name
Name of the codec implementation.
Definition: avcodec.h:3431
#define AV_PIX_FMT_YUV444P10
Definition: pixfmt.h:382
static int estimate_slice_plane(ProresContext *ctx, int *error, int plane, const uint16_t *src, ptrdiff_t linesize, int mbs_per_slice, int blocks_per_mb, int plane_size_factor, const int16_t *qmat, ProresThreadData *td)
ProresThreadData * tdata
#define FFMAX(a, b)
Definition: common.h:94
#define AV_CODEC_CAP_FRAME_THREADS
Codec supports frame-level multithreading.
Definition: avcodec.h:1024
static const int prores_mb_limits[NUM_MB_LIMITS]
int flags
A combination of AV_PKT_FLAG values.
Definition: avcodec.h:1451
struct TrellisNode * nodes
static int put_bits_count(PutBitContext *s)
Definition: put_bits.h:85
uint64_t flags
Combination of AV_PIX_FMT_FLAG_...
Definition: pixdesc.h:106
static void encode_vlc_codeword(PutBitContext *pb, unsigned codebook, int val)
Write an unsigned rice/exp golomb codeword.
#define NUM_MB_LIMITS
enum AVPictureType pict_type
Picture type of the frame.
Definition: frame.h:309
#define FFMIN(a, b)
Definition: common.h:96
const AVFrame * pic
uint8_t interlaced
Definition: mxfenc.c:2093
int width
picture width / height.
Definition: avcodec.h:1706
uint8_t w
Definition: llviddspenc.c:38
#define VE
static int encode_slice_plane(ProresContext *ctx, PutBitContext *pb, const uint16_t *src, ptrdiff_t linesize, int mbs_per_slice, int16_t *blocks, int blocks_per_mb, int plane_size_factor, const int16_t *qmat)
AVFormatContext * ctx
Definition: movenc.c:48
const uint8_t ff_prores_dc_codebook[4]
Definition: proresdata.c:48
#define MAX_STORED_Q
#define FFABS(a)
Absolute value, Note, INT_MIN / INT64_MIN result in undefined behavior as they are not representable ...
Definition: common.h:72
static void put_alpha_diff(PutBitContext *pb, int cur, int prev, int abits)
void(* fdct)(int16_t *block)
Definition: fdctdsp.h:27
#define AV_PIX_FMT_YUVA444P10
Definition: pixfmt.h:418
#define SCORE_LIMIT
static void encode_dcs(PutBitContext *pb, int16_t *blocks, int blocks_per_slice, int scale)
static void error(const char *err)
int thread_count
thread count is used to decide how many independent tasks should be passed to execute() ...
Definition: avcodec.h:2785
#define av_log2
Definition: intmath.h:83
#define FIRST_DC_CB
Definition: proresdata.h:33
#define AV_CODEC_CAP_SLICE_THREADS
Codec supports slice-based (or partition-based) multithreading.
Definition: avcodec.h:1028
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:2845
#define AV_LOG_INFO
Standard information.
Definition: log.h:187
const uint8_t ff_prores_interlaced_scan[64]
Definition: proresdata.c:36
int frame_size
Definition: mxfenc.c:2091
Libavcodec external API header.
typedef void(RENAME(mix_any_func_type))
int linesize[AV_NUM_DATA_POINTERS]
For video, size in bytes of each picture line.
Definition: frame.h:257
Descriptor that unambiguously describes how the bits of a pixel are stored in the up to 4 data planes...
Definition: pixdesc.h:81
main external API structure.
Definition: avcodec.h:1533
FDCTDSPContext fdsp
const uint8_t ff_prores_progressive_scan[64]
Definition: proresdata.c:25
unsigned int codec_tag
fourcc (LSB first, so "ABCD" -> (&#39;D&#39;<<24) + (&#39;C&#39;<<16) + (&#39;B&#39;<<8) + &#39;A&#39;).
Definition: avcodec.h:1558
int16_t blocks[MAX_PLANES][64 *4 *MAX_MBS_PER_SLICE]
void * buf
Definition: avisynth_c.h:690
#define AVERROR_BUG
Internal bug, also see AVERROR_BUG2.
Definition: error.h:50
Describe the class of an AVClass context structure.
Definition: log.h:67
int16_t custom_q[64]
static enum AVPixelFormat pix_fmts[]
Definition: libkvazaar.c:266
mfxU16 profile
Definition: qsvenc.c:44
uint8_t pi<< 24) CONV_FUNC_GROUP(AV_SAMPLE_FMT_FLT, float, AV_SAMPLE_FMT_U8, uint8_t,(*(const uint8_t *) pi - 0x80) *(1.0f/(1<< 7))) CONV_FUNC_GROUP(AV_SAMPLE_FMT_DBL, double, AV_SAMPLE_FMT_U8, uint8_t,(*(const uint8_t *) pi - 0x80) *(1.0/(1<< 7))) CONV_FUNC_GROUP(AV_SAMPLE_FMT_U8, uint8_t, AV_SAMPLE_FMT_S16, int16_t,(*(const int16_t *) pi >> 8)+0x80) CONV_FUNC_GROUP(AV_SAMPLE_FMT_FLT, float, AV_SAMPLE_FMT_S16, int16_t, *(const int16_t *) pi *(1.0f/(1<< 15))) CONV_FUNC_GROUP(AV_SAMPLE_FMT_DBL, double, AV_SAMPLE_FMT_S16, int16_t, *(const int16_t *) pi *(1.0/(1<< 15))) CONV_FUNC_GROUP(AV_SAMPLE_FMT_U8, uint8_t, AV_SAMPLE_FMT_S32, int32_t,(*(const int32_t *) pi >> 24)+0x80) CONV_FUNC_GROUP(AV_SAMPLE_FMT_FLT, float, AV_SAMPLE_FMT_S32, int32_t, *(const int32_t *) pi *(1.0f/(1U<< 31))) CONV_FUNC_GROUP(AV_SAMPLE_FMT_DBL, double, AV_SAMPLE_FMT_S32, int32_t, *(const int32_t *) pi *(1.0/(1U<< 31))) CONV_FUNC_GROUP(AV_SAMPLE_FMT_U8, uint8_t, AV_SAMPLE_FMT_FLT, float, av_clip_uint8(lrintf(*(const float *) pi *(1<< 7))+0x80)) CONV_FUNC_GROUP(AV_SAMPLE_FMT_S16, int16_t, AV_SAMPLE_FMT_FLT, float, av_clip_int16(lrintf(*(const float *) pi *(1<< 15)))) CONV_FUNC_GROUP(AV_SAMPLE_FMT_S32, int32_t, AV_SAMPLE_FMT_FLT, float, av_clipl_int32(llrintf(*(const float *) pi *(1U<< 31)))) CONV_FUNC_GROUP(AV_SAMPLE_FMT_U8, uint8_t, AV_SAMPLE_FMT_DBL, double, av_clip_uint8(lrint(*(const double *) pi *(1<< 7))+0x80)) CONV_FUNC_GROUP(AV_SAMPLE_FMT_S16, int16_t, AV_SAMPLE_FMT_DBL, double, av_clip_int16(lrint(*(const double *) pi *(1<< 15)))) CONV_FUNC_GROUP(AV_SAMPLE_FMT_S32, int32_t, AV_SAMPLE_FMT_DBL, double, av_clipl_int32(llrint(*(const double *) pi *(1U<< 31)))) #define SET_CONV_FUNC_GROUP(ofmt, ifmt) static void set_generic_function(AudioConvert *ac) { } void ff_audio_convert_free(AudioConvert **ac) { if(! *ac) return;ff_dither_free(&(*ac) ->dc);av_freep(ac);} AudioConvert *ff_audio_convert_alloc(AVAudioResampleContext *avr, enum AVSampleFormat out_fmt, enum AVSampleFormat in_fmt, int channels, int sample_rate, int apply_map) { AudioConvert *ac;int in_planar, out_planar;ac=av_mallocz(sizeof(*ac));if(!ac) return NULL;ac->avr=avr;ac->out_fmt=out_fmt;ac->in_fmt=in_fmt;ac->channels=channels;ac->apply_map=apply_map;if(avr->dither_method !=AV_RESAMPLE_DITHER_NONE &&av_get_packed_sample_fmt(out_fmt)==AV_SAMPLE_FMT_S16 &&av_get_bytes_per_sample(in_fmt) > 2) { ac->dc=ff_dither_alloc(avr, out_fmt, in_fmt, channels, sample_rate, apply_map);if(!ac->dc) { av_free(ac);return NULL;} return ac;} in_planar=ff_sample_fmt_is_planar(in_fmt, channels);out_planar=ff_sample_fmt_is_planar(out_fmt, channels);if(in_planar==out_planar) { ac->func_type=CONV_FUNC_TYPE_FLAT;ac->planes=in_planar ? ac->channels :1;} else if(in_planar) ac->func_type=CONV_FUNC_TYPE_INTERLEAVE;else ac->func_type=CONV_FUNC_TYPE_DEINTERLEAVE;set_generic_function(ac);if(ARCH_AARCH64) ff_audio_convert_init_aarch64(ac);if(ARCH_ARM) ff_audio_convert_init_arm(ac);if(ARCH_X86) ff_audio_convert_init_x86(ac);return ac;} int ff_audio_convert(AudioConvert *ac, AudioData *out, AudioData *in) { int use_generic=1;int len=in->nb_samples;int p;if(ac->dc) { av_log(ac->avr, AV_LOG_TRACE, "%d samples - audio_convert: %s to %s (dithered)\", len, av_get_sample_fmt_name(ac->in_fmt), av_get_sample_fmt_name(ac->out_fmt));return ff_convert_dither(ac-> dc
int global_quality
Global quality for codecs which cannot change it per frame.
Definition: avcodec.h:1599
#define AV_PIX_FMT_YUV422P10
Definition: pixfmt.h:380
static int find_slice_quant(AVCodecContext *avctx, int trellis_node, int x, int y, int mbs_per_slice, ProresThreadData *td)
uint8_t * data[AV_NUM_DATA_POINTERS]
pointer to the picture/channel planes.
Definition: frame.h:240
uint8_t level
Definition: svq3.c:207
#define GET_SIGN(x)
#define CFACTOR_Y422
int br_tab[NUM_MB_LIMITS]
static void put_alpha_run(PutBitContext *pb, int run)
#define FF_DISABLE_DEPRECATION_WARNINGS
Definition: internal.h:84
#define OFFSET(x)
static int encode_alpha_plane(ProresContext *ctx, PutBitContext *pb, int mbs_per_slice, uint16_t *blocks, int quant)
static const uint8_t prores_quant_matrices[][64]
common internal api header.
static void flush_put_bits(PutBitContext *s)
Pad the end of the output stream with zeros.
Definition: put_bits.h:101
static int find_quant_thread(AVCodecContext *avctx, void *arg, int jobnr, int threadnr)
attribute_deprecated AVFrame * coded_frame
the picture in the bitstream
Definition: avcodec.h:2776
int av_grow_packet(AVPacket *pkt, int grow_by)
Increase packet size, correctly zeroing padding.
Definition: avpacket.c:109
static void init_put_bits(PutBitContext *s, uint8_t *buffer, int buffer_size)
Initialize the PutBitContext s.
Definition: put_bits.h:48
static av_always_inline void bytestream_put_buffer(uint8_t **b, const uint8_t *src, unsigned int size)
Definition: bytestream.h:368
void * priv_data
Definition: avcodec.h:1560
#define TRELLIS_WIDTH
static av_always_inline int diff(const uint32_t a, const uint32_t b)
#define FF_ENABLE_DEPRECATION_WARNINGS
Definition: internal.h:85
int top_field_first
If the content is interlaced, is top field displayed first.
Definition: frame.h:378
int key_frame
1 -> keyframe, 0-> not
Definition: frame.h:304
#define FF_QP2LAMBDA
factor to convert from H.263 QP to lambda
Definition: avutil.h:227
#define MAKE_CODE(x)
enum AVColorPrimaries color_primaries
Definition: frame.h:473
#define av_freep(p)
void INT64 start
Definition: avisynth_c.h:690
enum AVColorTransferCharacteristic color_trc
Definition: frame.h:475
AVCodec ff_prores_ks_encoder
const uint8_t * quant_mat
#define FRAME_ID
Definition: proresdata.h:28
#define MKTAG(a, b, c, d)
Definition: common.h:366
static void get_alpha_data(ProresContext *ctx, const uint16_t *src, ptrdiff_t linesize, int x, int y, int w, int h, int16_t *blocks, int mbs_per_slice, int abits)
AVPixelFormat
Pixel format.
Definition: pixfmt.h:64
This structure stores compressed data.
Definition: avcodec.h:1422
#define MAX_PLANES
static int estimate_dcs(int *error, int16_t *blocks, int blocks_per_slice, int scale)
void(* fdct)(FDCTDSPContext *fdsp, const uint16_t *src, ptrdiff_t linesize, int16_t *block)
static uint8_t tmp[11]
Definition: aes_ctr.c:26
bitstream writer API