FFmpeg
prores_raw.c
Go to the documentation of this file.
1 /*
2  * ProRes RAW decoder
3  * Copyright (c) 2023-2025 Paul B Mahol
4  * Copyright (c) 2025 Lynne
5  *
6  * This file is part of FFmpeg.
7  *
8  * FFmpeg is free software; you can redistribute it and/or
9  * modify it under the terms of the GNU Lesser General Public
10  * License as published by the Free Software Foundation; either
11  * version 2.1 of the License, or (at your option) any later version.
12  *
13  * FFmpeg is distributed in the hope that it will be useful,
14  * but WITHOUT ANY WARRANTY; without even the implied warranty of
15  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
16  * Lesser General Public License for more details.
17  *
18  * You should have received a copy of the GNU Lesser General Public
19  * License along with FFmpeg; if not, write to the Free Software
20  * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
21  */
22 
23 #include "libavutil/avassert.h"
24 #include "libavutil/intreadwrite.h"
25 #include "libavutil/mem_internal.h"
26 #include "libavutil/mem.h"
27 
28 #define CACHED_BITSTREAM_READER !ARCH_X86_32
29 
30 #include "config_components.h"
31 #include "avcodec.h"
32 #include "bytestream.h"
33 #include "codec_internal.h"
34 #include "decode.h"
35 #include "get_bits.h"
36 #include "idctdsp.h"
37 #include "proresdata.h"
38 #include "thread.h"
39 #include "hwconfig.h"
40 #include "hwaccel_internal.h"
41 
42 #include "prores_raw.h"
43 
45 {
46  ProResRAWContext *s = avctx->priv_data;
47 
48  /* The codec outputs linear data, with the transfer function of the
49  * camera and any adjustments built into an 8-point linearization curve */
50  avctx->bits_per_raw_sample = 16;
51  avctx->color_trc = AVCOL_TRC_LINEAR;
54 
55  s->pix_fmt = AV_PIX_FMT_NONE;
56 
57  ff_blockdsp_init(&s->bdsp);
58  /* Coefficients and the iDCT are 12-bit, the linearization curve then
59  * expands the result to the 16-bit linear output range. */
60  ff_proresdsp_init(&s->prodsp, 12);
61 
62  ff_permute_scantable(s->scan, ff_prores_interlaced_scan, s->prodsp.idct_permutation);
63 
64  return 0;
65 }
66 
67 static uint16_t get_value(GetBitContext *gb, int16_t codebook)
68 {
69  const int16_t switch_bits = codebook >> 8;
70  const int16_t rice_order = codebook & 0xf;
71  const int16_t exp_order = (codebook >> 4) & 0xf;
72  int16_t q, bits;
73 
74  uint32_t b = show_bits_long(gb, 32);
75  if (!b)
76  return 0;
77  q = ff_clz(b);
78 
79  if (b & 0x80000000) {
80  skip_bits_long(gb, 1 + rice_order);
81  return (b & 0x7FFFFFFF) >> (31 - rice_order);
82  }
83 
84  if (q <= switch_bits) {
85  skip_bits_long(gb, 1 + rice_order + q);
86  return (q << rice_order) +
87  (((b << (q + 1)) >> 1) >> (31 - rice_order));
88  }
89 
90  bits = exp_order + (q << 1) - switch_bits;
91  if (bits > 32)
92  return 0; // we do not return a negative error code so that we dont produce out of range values on errors
93  skip_bits_long(gb, bits);
94  return (b >> (32 - bits)) +
95  ((switch_bits + 1) << rice_order) -
96  (1 << exp_order);
97 }
98 
99 #define TODCCODEBOOK(x) ((x + 1) >> 1)
100 
101 #define DC_CB_MAX 12
102 const uint8_t ff_prores_raw_dc_cb[DC_CB_MAX + 1] = {
103  0x010, 0x021, 0x032, 0x033, 0x033, 0x033, 0x044, 0x044, 0x044, 0x044, 0x044, 0x044, 0x076,
104 };
105 
106 #define AC_CB_MAX 94
107 const int16_t ff_prores_raw_ac_cb[AC_CB_MAX + 1] = {
108  0x000, 0x211, 0x111, 0x111, 0x222, 0x222, 0x222, 0x122, 0x122, 0x122,
109  0x233, 0x233, 0x233, 0x233, 0x233, 0x233, 0x233, 0x233, 0x133, 0x133,
110  0x244, 0x244, 0x244, 0x244, 0x244, 0x244, 0x244, 0x244, 0x244, 0x244, 0x244,
111  0x244, 0x244, 0x244, 0x244, 0x244, 0x244, 0x244, 0x244, 0x244, 0x244, 0x244,
112  0x355, 0x355, 0x355, 0x355, 0x355, 0x355, 0x355, 0x355, 0x355, 0x355, 0x355, 0x355, 0x355,
113  0x355, 0x355, 0x355, 0x355, 0x355, 0x355, 0x355, 0x355, 0x355, 0x355, 0x355, 0x355, 0x355,
114  0x355, 0x355, 0x355, 0x355, 0x355, 0x355, 0x355, 0x355, 0x355, 0x355, 0x355, 0x355, 0x355,
115  0x355, 0x355, 0x355, 0x355, 0x355, 0x355, 0x355, 0x355, 0x355, 0x355, 0x355, 0x355, 0x355, 0x166,
116 };
117 
118 #define RN_CB_MAX 27
119 const int16_t ff_prores_raw_rn_cb[RN_CB_MAX + 1] = {
120  0x200, 0x100, 0x000, 0x000, 0x211, 0x211, 0x111, 0x111, 0x011, 0x011, 0x021, 0x021, 0x222, 0x022,
121  0x022, 0x022, 0x022, 0x022, 0x022, 0x022, 0x022, 0x022, 0x022, 0x022, 0x022, 0x032, 0x032, 0x044
122 };
123 
124 #define LN_CB_MAX 14
125 const int16_t ff_prores_raw_ln_cb[LN_CB_MAX + 1] = {
126  0x100, 0x111, 0x222, 0x222, 0x122, 0x122, 0x433, 0x433, 0x233, 0x233, 0x233, 0x233, 0x233, 0x233, 0x033,
127 };
128 
130  AVFrame *frame, const uint8_t *data, int size,
131  int component, int16_t *qmat)
132 {
133  int ret;
134  ProResRAWContext *s = avctx->priv_data;
135  const ptrdiff_t linesize = frame->linesize[0] >> 1;
136  uint16_t *dst = (uint16_t *)(frame->data[0] + tile->y*frame->linesize[0] + 2*tile->x);
137 
138  int idx;
139  const int log2_nb_blocks = tile->log2_nb_blocks;
140  const int nb_blocks = 1 << log2_nb_blocks;
141  const int block_mask = nb_blocks - 1;
142  const int nb_codes = 64 * nb_blocks;
143 
144  LOCAL_ALIGNED_32(int32_t, block, [64*16]);
145 
146  int16_t sign = 0;
147  int16_t dc_add = 0;
148  int16_t dc_codebook;
149 
150  uint16_t ac, rn, ln;
151  int16_t ac_codebook = 49;
152  int16_t rn_codebook = 0;
153  int16_t ln_codebook = 66;
154 
155  const uint8_t *scan = s->scan;
156  GetBitContext gb;
157 
158  if (component > 1)
159  dst += linesize;
160  dst += component & 1;
161 
162  if ((ret = init_get_bits8(&gb, data, size)) < 0)
163  return ret;
164 
165  memset(block, 0, nb_blocks * 64 * sizeof(*block));
166 
167  /* Special handling for first block */
168  int dc = get_value(&gb, 700);
169  int prev_dc = (dc >> 1) ^ -(dc & 1);
170  block[0] = (((dc&1) + (dc>>1) ^ -(int)(dc & 1)) + (dc & 1)) + 1;
171 
172  for (int n = 1; n < nb_blocks; n++) {
173  if (get_bits_left(&gb) <= 0)
174  break;
175 
176  if ((n & 15) == 1)
177  dc_codebook = 100;
178  else
180 
181  dc = get_value(&gb, dc_codebook);
182 
183  sign = sign ^ dc & 1;
184  dc_add = (-sign ^ TODCCODEBOOK(dc)) + sign;
185  sign = dc_add < 0;
186  prev_dc += dc_add;
187 
188  block[n*64] = prev_dc + 1;
189  }
190 
191  for (int n = nb_blocks; n <= nb_codes;) {
192  if (get_bits_left(&gb) <= 0)
193  break;
194 
195  ln = get_value(&gb, ln_codebook);
196 
197  for (int i = 0; i < ln; i++) {
198  if (get_bits_left(&gb) <= 0)
199  break;
200 
201  if ((n + i) >= nb_codes)
202  break;
203 
204  ac = get_value(&gb, ac_codebook);
205  ac_codebook = ff_prores_raw_ac_cb[FFMIN(ac, AC_CB_MAX)];
206  sign = -get_bits1(&gb);
207 
208  idx = scan[(n + i) >> log2_nb_blocks] + (((n + i) & block_mask) << 6);
209  block[idx] = ((ac + 1) ^ sign) - sign;
210  }
211 
212  n += ln;
213  if (n >= nb_codes)
214  break;
215 
216  rn = get_value(&gb, rn_codebook);
217  rn_codebook = ff_prores_raw_rn_cb[FFMIN(rn, RN_CB_MAX)];
218 
219  n += rn + 1;
220  if (n >= nb_codes)
221  break;
222 
223  if (get_bits_left(&gb) <= 0)
224  break;
225 
226  ac = get_value(&gb, ac_codebook);
227  sign = -get_bits1(&gb);
228 
229  idx = scan[n >> log2_nb_blocks] + ((n & block_mask) << 6);
230  block[idx] = ((ac + 1) ^ sign) - sign;
231 
232  ac_codebook = ff_prores_raw_ac_cb[FFMIN(ac, AC_CB_MAX)];
233  ln_codebook = ff_prores_raw_ln_cb[FFMIN(ac, LN_CB_MAX)];
234 
235  n++;
236  }
237 
238  for (int n = 0; n < nb_blocks; n++) {
239  uint16_t *ptr = dst + n*16;
240  s->prodsp.idct_put_bayer(ptr, linesize, block + n*64, qmat, s->lin_curve);
241  }
242 
243  return 0;
244 }
245 
247  AVFrame *frame)
248 {
249  int ret;
250  ProResRAWContext *s = avctx->priv_data;
251 
252  GetByteContext *gb = &tile->gb;
253  LOCAL_ALIGNED_32(int16_t, qmat, [64]);
254 
255  if (tile->x >= avctx->width)
256  return 0;
257 
258  /* Tile header */
259  int header_len = bytestream2_get_byteu(gb) >> 3;
260  int16_t scale = bytestream2_get_byteu(gb);
261 
262  int size[4];
263  size[0] = bytestream2_get_be16(gb);
264  size[1] = bytestream2_get_be16(gb);
265  size[2] = bytestream2_get_be16(gb);
266  size[3] = bytestream2_size(gb) - size[0] - size[1] - size[2] - header_len;
267  if (size[3] < 0)
268  return AVERROR_INVALIDDATA;
269 
270  for (int i = 0; i < 64; i++)
271  qmat[i] = s->qmat[i] * scale;
272 
273  const uint8_t *comp_start = gb->buffer_start + header_len;
274 
275  ret = decode_comp(avctx, tile, frame, comp_start,
276  size[0], 2, qmat);
277  if (ret < 0)
278  goto fail;
279 
280  ret = decode_comp(avctx, tile, frame, comp_start + size[0],
281  size[1], 1, qmat);
282  if (ret < 0)
283  goto fail;
284 
285  ret = decode_comp(avctx, tile, frame, comp_start + size[0] + size[1],
286  size[2], 3, qmat);
287  if (ret < 0)
288  goto fail;
289 
290  ret = decode_comp(avctx, tile, frame, comp_start + size[0] + size[1] + size[2],
291  size[3], 0, qmat);
292  if (ret < 0)
293  goto fail;
294 
295  return 0;
296 fail:
297  av_log(avctx, AV_LOG_ERROR, "tile %d/%d decoding error\n", tile->x, tile->y);
298  return ret;
299 }
300 
301 static int decode_tiles(AVCodecContext *avctx, void *arg,
302  int n, int thread_nb)
303 {
304  ProResRAWContext *s = avctx->priv_data;
305  TileContext *tile = &s->tiles[n];
306  AVFrame *frame = arg;
307 
308  return decode_tile(avctx, tile, frame);
309 }
310 
312  enum AVPixelFormat pix_fmt)
313 {
314  enum AVPixelFormat pix_fmts[] = {
315 #if CONFIG_PRORES_RAW_VULKAN_HWACCEL
317 #endif
318 #if CONFIG_PRORES_RAW_VIDEOTOOLBOX_HWACCEL
320 #endif
321  pix_fmt,
323  };
324 
325  return ff_get_format(avctx, pix_fmts);
326 }
327 
328 static int decode_frame(AVCodecContext *avctx,
329  AVFrame *frame, int *got_frame_ptr,
330  AVPacket *avpkt)
331 {
332  ProResRAWContext *s = avctx->priv_data;
333  int ret, dimensions_changed = 0, old_version = s->version;
334  DECLARE_ALIGNED(32, uint8_t, qmat)[64];
335  memset(qmat, 1, 64);
336 
337  if (avctx->skip_frame >= AVDISCARD_ALL)
338  return avpkt->size;
339 
340  switch (avctx->codec_tag) {
341  case 0:
342  break;
343  case MKTAG('a','p','r','n'):
345  break;
346  case MKTAG('a','p','r','h'):
348  break;
349  default:
350  avpriv_request_sample(avctx, "Profile %d", avctx->codec_tag);
351  return AVERROR_PATCHWELCOME;
352  break;
353  }
354 
355  GetByteContext gb;
356  bytestream2_init(&gb, avpkt->data, avpkt->size);
357  if (bytestream2_get_be32(&gb) != avpkt->size)
358  return AVERROR_INVALIDDATA;
359 
360  /* ProRes RAW frame */
361  if (bytestream2_get_be32(&gb) != MKBETAG('p','r','r','f'))
362  return AVERROR_INVALIDDATA;
363 
364  int header_len = bytestream2_get_be16(&gb);
365  if (header_len < 62 || bytestream2_get_bytes_left(&gb) < header_len - 2)
366  return AVERROR_INVALIDDATA;
367 
368  GetByteContext gb_hdr;
369  bytestream2_init(&gb_hdr, gb.buffer, header_len - 2);
370  bytestream2_skip(&gb, header_len - 2);
371 
372  bytestream2_skip(&gb_hdr, 1); /* 1 reserved byte */
373  s->version = bytestream2_get_byte(&gb_hdr);
374  if (s->version > 1) {
375  avpriv_request_sample(avctx, "Version %d", s->version);
376  return AVERROR_PATCHWELCOME;
377  }
378 
379  /* Vendor header (e.g. "peac" for Panasonic or "atm0" for Atmos) */
380  bytestream2_skip(&gb_hdr, 4);
381 
382  /* Width and height must always be even */
383  int w = bytestream2_get_be16(&gb_hdr);
384  int h = bytestream2_get_be16(&gb_hdr);
385  if ((w & 1) || (h & 1))
386  return AVERROR_INVALIDDATA;
387 
388  if (w != avctx->width || h != avctx->height) {
389  av_log(avctx, AV_LOG_WARNING, "picture resolution change: %ix%i -> %ix%i\n",
390  avctx->width, avctx->height, w, h);
391  if ((ret = ff_set_dimensions(avctx, w, h)) < 0)
392  return ret;
393  dimensions_changed = 1;
394  }
395 
396  avctx->coded_width = FFALIGN(w, 16);
397  avctx->coded_height = FFALIGN(h, 16);
398 
400  if (pix_fmt != s->pix_fmt || dimensions_changed ||
401  s->version != old_version) {
402  s->pix_fmt = pix_fmt;
403 
404  ret = get_pixel_format(avctx, pix_fmt);
405  if (ret < 0)
406  return ret;
407 
408  avctx->pix_fmt = ret;
409  }
410 
411  bytestream2_skip(&gb_hdr, 1 * 4); /* 4 reserved bytes */
412 
413  /* BayerPattern: 0=RGGB, 1/2/3 = alternates */
414  int bayer_pattern = bytestream2_get_be16(&gb_hdr) & 0x3;
415  if (bayer_pattern != 0) {
416  avpriv_request_sample(avctx, "Bayer pattern %d", bayer_pattern);
417  return AVERROR_PATCHWELCOME;
418  }
419 
420  bytestream2_skip(&gb_hdr, 2); /* senselValueRange (white_level = value + 0x100, black_level = 0x100) */
421  bytestream2_skip(&gb_hdr, 4); /* WhiteBalanceRedFactor (float, pre-debayer R gain) */
422  bytestream2_skip(&gb_hdr, 4); /* WhiteBalanceBlueFactor (float, pre-debayer B gain) */
423  bytestream2_skip(&gb_hdr, 4 * 3 * 3); /* ColorMatrix (3x3 float, camera RGB -> Rec.2020 linear, row-major) */
424  bytestream2_skip(&gb_hdr, 4); /* GainFactor (float, post-matrix scene-linear scale) */
425  bytestream2_skip(&gb_hdr, 2); /* WhiteBalanceCCT (Kelvin, informational) */
426 
427  /* Flags */
428  int flags = bytestream2_get_be16(&gb_hdr);
429  int align = (flags >> 1) & 0x7;
430 
431  /* Quantization matrix */
432  if (flags & 1)
433  bytestream2_get_buffer(&gb_hdr, qmat, 64);
434 
435  if ((flags >> 4) & 1) {
436  /* 8-poing 16-bit control points, defining the combined linearization
437  * curve (inv. transfer fn + encoder-defined shaping) */
438  for (int i = 0; i < 8; i++)
439  s->lin_curve[i] = bytestream2_get_be16(&gb_hdr);
440  } else {
441  /* default curve: ptwos */
442  static const uint16_t default_lin_curve[8] =
443  { 0, 512, 1024, 2048, 4096, 8192, 16384, 32768 };
444  memcpy(s->lin_curve, default_lin_curve, sizeof(s->lin_curve));
445  }
446 
447  ff_permute_scantable(s->qmat, s->prodsp.idct_permutation, qmat);
448 
449  int tw16 = (w + 15) >> 4;
450  s->nb_tw = (tw16 >> align) + av_popcount(~(-1 * (1 << align)) & tw16);
451  s->nb_th = (h + 15) >> 4;
452  s->nb_tiles = s->nb_tw * s->nb_th;
453  av_log(avctx, AV_LOG_DEBUG, "%dx%d | nb_tiles: %d\n", s->nb_tw, s->nb_th, s->nb_tiles);
454 
455  s->th = 16;
456 
457  av_fast_mallocz(&s->tiles, &s->tiles_size, s->nb_tiles * sizeof(*s->tiles));
458  if (!s->tiles)
459  return AVERROR(ENOMEM);
460 
461  if (bytestream2_get_bytes_left(&gb) < s->nb_tiles * 2)
462  return AVERROR_INVALIDDATA;
463 
464  /* First tile that extends past the right edge gets halved in width,
465  * next one gets quartered, and so on */
466  int offset = bytestream2_tell(&gb) + s->nb_tiles * 2;
467  int n = 0;
468  for (int ty = 0; ty < s->nb_th; ty++) {
469  unsigned tx = 0;
470  int rem = tw16;
471  for (int e = align; rem > 0; e--) {
472  int unit = 1 << e;
473  while (unit <= rem) {
474  TileContext *tile = &s->tiles[n++];
475  int size = bytestream2_get_be16(&gb);
476 
477  if (offset >= avpkt->size)
478  return AVERROR_INVALIDDATA;
479  if (size >= avpkt->size)
480  return AVERROR_INVALIDDATA;
481  if (offset > avpkt->size - size)
482  return AVERROR_INVALIDDATA;
483 
484  bytestream2_init(&tile->gb, avpkt->data + offset, size);
485  tile->x = tx * 16;
486  tile->y = ty * s->th;
487  tile->log2_nb_blocks = e;
488  offset += size;
489 
490  tx += unit;
491  rem -= unit;
492  }
493  }
494  }
495  av_assert1(n == s->nb_tiles);
496 
497  ret = ff_thread_get_buffer(avctx, frame, 0);
498  if (ret < 0)
499  return ret;
500 
501  s->frame = frame;
502 
503  /* Start */
504  if (avctx->hwaccel) {
505  const FFHWAccel *hwaccel = ffhwaccel(avctx->hwaccel);
506 
507  ret = ff_hwaccel_frame_priv_alloc(avctx, &s->hwaccel_picture_private);
508  if (ret < 0)
509  return ret;
510 
511  ret = hwaccel->start_frame(avctx, avpkt->buf, avpkt->data, avpkt->size);
512  if (ret < 0)
513  return ret;
514 
515  for (int n = 0; n < s->nb_tiles; n++) {
516  TileContext *tile = &s->tiles[n];
517  ret = hwaccel->decode_slice(avctx, tile->gb.buffer,
518  tile->gb.buffer_end - tile->gb.buffer);
519  if (ret < 0)
520  return ret;
521  }
522 
523  ret = hwaccel->end_frame(avctx);
524  if (ret < 0)
525  return ret;
526 
527  av_refstruct_unref(&s->hwaccel_picture_private);
528  } else {
529  avctx->execute2(avctx, decode_tiles, frame, NULL, s->nb_tiles);
530  }
531 
532  frame->pict_type = AV_PICTURE_TYPE_I;
533  frame->flags |= AV_FRAME_FLAG_KEY;
534 
535  *got_frame_ptr = 1;
536 
537  return avpkt->size;
538 }
539 
541 {
542  ProResRAWContext *s = avctx->priv_data;
543  av_refstruct_unref(&s->hwaccel_picture_private);
544  av_freep(&s->tiles);
545  return 0;
546 }
547 
548 #if HAVE_THREADS
550 {
551  ProResRAWContext *rsrc = src->priv_data;
552  ProResRAWContext *rdst = dst->priv_data;
553 
554  rdst->pix_fmt = rsrc->pix_fmt;
555  rdst->version = rsrc->version;
556 
557  return 0;
558 }
559 #endif
560 
562  .p.name = "prores_raw",
563  CODEC_LONG_NAME("Apple ProRes RAW"),
564  .p.type = AVMEDIA_TYPE_VIDEO,
565  .p.id = AV_CODEC_ID_PRORES_RAW,
566  .priv_data_size = sizeof(ProResRAWContext),
567  .init = decode_init,
568  .close = decode_end,
571  .p.capabilities = AV_CODEC_CAP_DR1 |
574  .caps_internal = FF_CODEC_CAP_INIT_CLEANUP |
576  .hw_configs = (const AVCodecHWConfigInternal *const []) {
577 #if CONFIG_PRORES_RAW_VULKAN_HWACCEL
578  HWACCEL_VULKAN(prores_raw),
579 #endif
580 #if CONFIG_PRORES_RAW_VIDEOTOOLBOX_HWACCEL
581  HWACCEL_VIDEOTOOLBOX(prores_raw),
582 #endif
583  NULL
584  },
585 };
flags
const SwsFlags flags[]
Definition: swscale.c:72
hwconfig.h
AVCodecContext::hwaccel
const struct AVHWAccel * hwaccel
Hardware accelerator in use.
Definition: avcodec.h:1423
skip_bits_long
static void skip_bits_long(GetBitContext *s, int n)
Skips the specified number of bits.
Definition: get_bits.h:280
AV_LOG_WARNING
#define AV_LOG_WARNING
Something somehow does not look correct.
Definition: log.h:216
AVPixelFormat
AVPixelFormat
Pixel format.
Definition: pixfmt.h:71
FF_CODEC_CAP_INIT_CLEANUP
#define FF_CODEC_CAP_INIT_CLEANUP
The codec allows calling the close function for deallocation even if the init function returned a fai...
Definition: codec_internal.h:43
show_bits_long
static unsigned int show_bits_long(GetBitContext *s, int n)
Show 0-32 bits.
Definition: get_bits.h:498
get_bits_left
static int get_bits_left(GetBitContext *gb)
Definition: get_bits.h:688
AVERROR
Filter the word “frame” indicates either a video frame or a group of audio as stored in an AVFrame structure Format for each input and each output the list of supported formats For video that means pixel format For audio that means channel sample they are references to shared objects When the negotiation mechanism computes the intersection of the formats supported at each end of a all references to both lists are replaced with a reference to the intersection And when a single format is eventually chosen for a link amongst the remaining all references to the list are updated That means that if a filter requires that its input and output have the same format amongst a supported all it has to do is use a reference to the same list of formats query_formats can leave some formats unset and return AVERROR(EAGAIN) to cause the negotiation mechanism toagain later. That can be used by filters with complex requirements to use the format negotiated on one link to set the formats supported on another. Frame references ownership and permissions
GetByteContext::buffer_start
const uint8_t * buffer_start
Definition: bytestream.h:34
bytestream2_get_bytes_left
static av_always_inline int bytestream2_get_bytes_left(const GetByteContext *g)
Definition: bytestream.h:158
AVCodecContext::colorspace
enum AVColorSpace colorspace
YUV colorspace type.
Definition: avcodec.h:671
mem_internal.h
decode_end
static av_cold int decode_end(AVCodecContext *avctx)
Definition: prores_raw.c:540
ff_get_format
int ff_get_format(AVCodecContext *avctx, const enum AVPixelFormat *fmt)
Select the (possibly hardware accelerated) pixel format.
Definition: decode.c:1220
GetByteContext
Definition: bytestream.h:33
bytestream2_tell
static av_always_inline int bytestream2_tell(const GetByteContext *g)
Definition: bytestream.h:192
decode_tiles
static int decode_tiles(AVCodecContext *avctx, void *arg, int n, int thread_nb)
Definition: prores_raw.c:301
AVCOL_TRC_LINEAR
@ AVCOL_TRC_LINEAR
"Linear transfer characteristics"
Definition: pixfmt.h:675
ff_clz
#define ff_clz
Definition: intmath.h:141
AVFrame
This structure describes decoded (raw) audio or video data.
Definition: frame.h:459
AVCodecContext::color_trc
enum AVColorTransferCharacteristic color_trc
Color Transfer Characteristic.
Definition: avcodec.h:664
AVPacket::data
uint8_t * data
Definition: packet.h:595
b
#define b
Definition: input.c:43
data
const char data[16]
Definition: mxf.c:149
FFCodec
Definition: codec_internal.h:127
ProResRAWContext::version
int version
Definition: prores_raw.h:53
AC_CB_MAX
#define AC_CB_MAX
Definition: prores_raw.c:106
av_popcount
#define av_popcount
Definition: common.h:154
ff_set_dimensions
int ff_set_dimensions(AVCodecContext *s, int width, int height)
Definition: utils.c:91
ff_prores_raw_ln_cb
const int16_t ff_prores_raw_ln_cb[LN_CB_MAX+1]
Definition: prores_raw.c:125
thread.h
AV_PROFILE_PRORES_RAW_HQ
#define AV_PROFILE_PRORES_RAW_HQ
Definition: defs.h:189
AV_PIX_FMT_VULKAN
@ AV_PIX_FMT_VULKAN
Vulkan hardware images.
Definition: pixfmt.h:379
ff_permute_scantable
av_cold void ff_permute_scantable(uint8_t dst[64], const uint8_t src[64], const uint8_t permutation[64])
Definition: idctdsp.c:30
close
static av_cold void close(AVCodecParserContext *s)
Definition: apv_parser.c:197
bytestream2_skip
static av_always_inline void bytestream2_skip(GetByteContext *g, unsigned int size)
Definition: bytestream.h:168
FFCodec::p
AVCodec p
The public AVCodec.
Definition: codec_internal.h:131
FFHWAccel
Definition: hwaccel_internal.h:34
AVCodecContext::skip_frame
enum AVDiscard skip_frame
Skip decoding for selected frames.
Definition: avcodec.h:1680
fail
#define fail()
Definition: checkasm.h:225
GetBitContext
Definition: get_bits.h:109
AV_PROFILE_PRORES_RAW
#define AV_PROFILE_PRORES_RAW
Definition: defs.h:188
AVCodecContext::coded_height
int coded_height
Definition: avcodec.h:619
AV_CODEC_ID_PRORES_RAW
@ AV_CODEC_ID_PRORES_RAW
Definition: codec_id.h:333
decode_init
static av_cold int decode_init(AVCodecContext *avctx)
Definition: prores_raw.c:44
TODCCODEBOOK
#define TODCCODEBOOK(x)
Definition: prores_raw.c:99
avassert.h
ff_prores_raw_decoder
const FFCodec ff_prores_raw_decoder
Definition: prores_raw.c:561
AVCodecContext::color_primaries
enum AVColorPrimaries color_primaries
Chromaticity coordinates of the source primaries.
Definition: avcodec.h:657
AV_LOG_ERROR
#define AV_LOG_ERROR
Something went wrong and cannot losslessly be recovered.
Definition: log.h:210
av_cold
#define av_cold
Definition: attributes.h:119
init_get_bits8
static int init_get_bits8(GetBitContext *s, const uint8_t *buffer, int byte_size)
Initialize GetBitContext.
Definition: get_bits.h:544
AV_FRAME_FLAG_KEY
#define AV_FRAME_FLAG_KEY
A flag to mark frames that are keyframes.
Definition: frame.h:674
dc_codebook
static const uint8_t dc_codebook[7]
Definition: proresdec.c:459
FF_CODEC_DECODE_CB
#define FF_CODEC_DECODE_CB(func)
Definition: codec_internal.h:347
ff_blockdsp_init
av_cold void ff_blockdsp_init(BlockDSPContext *c)
Definition: blockdsp.c:58
ff_hwaccel_frame_priv_alloc
int ff_hwaccel_frame_priv_alloc(AVCodecContext *avctx, void **hwaccel_picture_private)
Allocate a hwaccel frame private data if the provided avctx uses a hwaccel method that needs it.
Definition: decode.c:2327
intreadwrite.h
s
#define s(width, name)
Definition: cbs_vp9.c:198
pix_fmt
static enum AVPixelFormat pix_fmt
Definition: demux_decode.c:41
ff_thread_get_buffer
int ff_thread_get_buffer(AVCodecContext *avctx, AVFrame *f, int flags)
Wrapper around get_buffer() for frame-multithreaded codecs.
Definition: pthread_frame.c:1044
ff_prores_raw_rn_cb
const int16_t ff_prores_raw_rn_cb[RN_CB_MAX+1]
Definition: prores_raw.c:119
GetByteContext::buffer
const uint8_t * buffer
Definition: bytestream.h:34
prores_raw.h
bits
uint8_t bits
Definition: vp3data.h:128
pix_fmts
static enum AVPixelFormat pix_fmts[]
Definition: libkvazaar.c:296
AVCodecContext::bits_per_raw_sample
int bits_per_raw_sample
Bits per sample/pixel of internal libavcodec pixel/sample format.
Definition: avcodec.h:1571
AV_LOG_DEBUG
#define AV_LOG_DEBUG
Stuff which is only useful for libav* developers.
Definition: log.h:231
decode.h
get_bits.h
ff_prores_interlaced_scan
const uint8_t ff_prores_interlaced_scan[64]
Definition: proresdata.c:36
AVCOL_PRI_UNSPECIFIED
@ AVCOL_PRI_UNSPECIFIED
Definition: pixfmt.h:639
CODEC_LONG_NAME
#define CODEC_LONG_NAME(str)
Definition: codec_internal.h:332
arg
const char * arg
Definition: jacosubdec.c:65
AV_CODEC_CAP_FRAME_THREADS
#define AV_CODEC_CAP_FRAME_THREADS
Codec supports frame-level multithreading.
Definition: codec.h:95
AVDISCARD_ALL
@ AVDISCARD_ALL
discard all
Definition: defs.h:232
AVPacket::buf
AVBufferRef * buf
A reference to the reference-counted buffer where the packet data is stored.
Definition: packet.h:578
ff_prores_raw_dc_cb
const uint8_t ff_prores_raw_dc_cb[DC_CB_MAX+1]
Definition: prores_raw.c:102
NULL
#define NULL
Definition: coverity.c:32
LOCAL_ALIGNED_32
#define LOCAL_ALIGNED_32(t, v,...)
Definition: mem_internal.h:132
AVERROR_PATCHWELCOME
#define AVERROR_PATCHWELCOME
Not yet implemented in FFmpeg, patches welcome.
Definition: error.h:64
hwaccel_internal.h
AV_PICTURE_TYPE_I
@ AV_PICTURE_TYPE_I
Intra.
Definition: avutil.h:278
get_bits1
static unsigned int get_bits1(GetBitContext *s)
Definition: get_bits.h:391
av_fast_mallocz
void av_fast_mallocz(void *ptr, unsigned int *size, size_t min_size)
Allocate and clear a buffer, reusing the given one if large enough.
Definition: mem.c:562
ProResRAWContext
Definition: prores_raw.h:39
bytestream2_get_buffer
static av_always_inline unsigned int bytestream2_get_buffer(GetByteContext *g, uint8_t *dst, unsigned int size)
Definition: bytestream.h:267
UPDATE_THREAD_CONTEXT
#define UPDATE_THREAD_CONTEXT(func)
Definition: codec_internal.h:341
init
int(* init)(AVBSFContext *ctx)
Definition: dts2pts.c:551
AV_CODEC_CAP_DR1
#define AV_CODEC_CAP_DR1
Codec uses get_buffer() or get_encode_buffer() for allocating buffers and supports custom allocators.
Definition: codec.h:52
AVPacket::size
int size
Definition: packet.h:596
dc
Tag MUST be and< 10hcoeff half pel interpolation filter coefficients, hcoeff[0] are the 2 middle coefficients[1] are the next outer ones and so on, resulting in a filter like:...eff[2], hcoeff[1], hcoeff[0], hcoeff[0], hcoeff[1], hcoeff[2] ... the sign of the coefficients is not explicitly stored but alternates after each coeff and coeff[0] is positive, so ...,+,-,+,-,+,+,-,+,-,+,... hcoeff[0] is not explicitly stored but found by subtracting the sum of all stored coefficients with signs from 32 hcoeff[0]=32 - hcoeff[1] - hcoeff[2] - ... a good choice for hcoeff and htaps is htaps=6 hcoeff={40,-10, 2} an alternative which requires more computations at both encoder and decoder side and may or may not be better is htaps=8 hcoeff={42,-14, 6,-2}ref_frames minimum of the number of available reference frames and max_ref_frames for example the first frame after a key frame always has ref_frames=1spatial_decomposition_type wavelet type 0 is a 9/7 symmetric compact integer wavelet 1 is a 5/3 symmetric compact integer wavelet others are reserved stored as delta from last, last is reset to 0 if always_reset||keyframeqlog quality(logarithmic quantizer scale) stored as delta from last, last is reset to 0 if always_reset||keyframemv_scale stored as delta from last, last is reset to 0 if always_reset||keyframe FIXME check that everything works fine if this changes between framesqbias dequantization bias stored as delta from last, last is reset to 0 if always_reset||keyframeblock_max_depth maximum depth of the block tree stored as delta from last, last is reset to 0 if always_reset||keyframequant_table quantization tableHighlevel bitstream structure:==============================--------------------------------------------|Header|--------------------------------------------|------------------------------------|||Block0||||split?||||yes no||||......... intra?||||:Block01 :yes no||||:Block02 :....... ..........||||:Block03 ::y DC ::ref index:||||:Block04 ::cb DC ::motion x :||||......... :cr DC ::motion y :||||....... ..........|||------------------------------------||------------------------------------|||Block1|||...|--------------------------------------------|------------ ------------ ------------|||Y subbands||Cb subbands||Cr subbands||||--- ---||--- ---||--- ---|||||LL0||HL0||||LL0||HL0||||LL0||HL0|||||--- ---||--- ---||--- ---||||--- ---||--- ---||--- ---|||||LH0||HH0||||LH0||HH0||||LH0||HH0|||||--- ---||--- ---||--- ---||||--- ---||--- ---||--- ---|||||HL1||LH1||||HL1||LH1||||HL1||LH1|||||--- ---||--- ---||--- ---||||--- ---||--- ---||--- ---|||||HH1||HL2||||HH1||HL2||||HH1||HL2|||||...||...||...|||------------ ------------ ------------|--------------------------------------------Decoding process:=================------------|||Subbands|------------||||------------|Intra DC||||LL0 subband prediction ------------|\ Dequantization ------------------- \||Reference frames|\ IDWT|------- -------|Motion \|||Frame 0||Frame 1||Compensation . OBMC v -------|------- -------|--------------. \------> Frame n output Frame Frame<----------------------------------/|...|------------------- Range Coder:============Binary Range Coder:------------------- The implemented range coder is an adapted version based upon "Range encoding: an algorithm for removing redundancy from a digitised message." by G. N. N. Martin. The symbols encoded by the Snow range coder are bits(0|1). The associated probabilities are not fix but change depending on the symbol mix seen so far. bit seen|new state ---------+----------------------------------------------- 0|256 - state_transition_table[256 - old_state];1|state_transition_table[old_state];state_transition_table={ 0, 0, 0, 0, 0, 0, 0, 0, 20, 21, 22, 23, 24, 25, 26, 27, 28, 29, 30, 31, 32, 33, 34, 35, 36, 37, 37, 38, 39, 40, 41, 42, 43, 44, 45, 46, 47, 48, 49, 50, 51, 52, 53, 54, 55, 56, 56, 57, 58, 59, 60, 61, 62, 63, 64, 65, 66, 67, 68, 69, 70, 71, 72, 73, 74, 75, 75, 76, 77, 78, 79, 80, 81, 82, 83, 84, 85, 86, 87, 88, 89, 90, 91, 92, 93, 94, 94, 95, 96, 97, 98, 99, 100, 101, 102, 103, 104, 105, 106, 107, 108, 109, 110, 111, 112, 113, 114, 114, 115, 116, 117, 118, 119, 120, 121, 122, 123, 124, 125, 126, 127, 128, 129, 130, 131, 132, 133, 133, 134, 135, 136, 137, 138, 139, 140, 141, 142, 143, 144, 145, 146, 147, 148, 149, 150, 151, 152, 152, 153, 154, 155, 156, 157, 158, 159, 160, 161, 162, 163, 164, 165, 166, 167, 168, 169, 170, 171, 171, 172, 173, 174, 175, 176, 177, 178, 179, 180, 181, 182, 183, 184, 185, 186, 187, 188, 189, 190, 190, 191, 192, 194, 194, 195, 196, 197, 198, 199, 200, 201, 202, 202, 204, 205, 206, 207, 208, 209, 209, 210, 211, 212, 213, 215, 215, 216, 217, 218, 219, 220, 220, 222, 223, 224, 225, 226, 227, 227, 229, 229, 230, 231, 232, 234, 234, 235, 236, 237, 238, 239, 240, 241, 242, 243, 244, 245, 246, 247, 248, 248, 0, 0, 0, 0, 0, 0, 0};FIXME Range Coding of integers:------------------------- FIXME Neighboring Blocks:===================left and top are set to the respective blocks unless they are outside of the image in which case they are set to the Null block top-left is set to the top left block unless it is outside of the image in which case it is set to the left block if this block has no larger parent block or it is at the left side of its parent block and the top right block is not outside of the image then the top right block is used for top-right else the top-left block is used Null block y, cb, cr are 128 level, ref, mx and my are 0 Motion Vector Prediction:=========================1. the motion vectors of all the neighboring blocks are scaled to compensate for the difference of reference frames scaled_mv=(mv *(256 *(current_reference+1)/(mv.reference+1))+128)> the median of the scaled top and top right vectors is used as motion vector prediction the used motion vector is the sum of the predictor and(mvx_diff, mvy_diff) *mv_scale Intra DC Prediction block[y][x] dc[1]
Definition: snow.txt:400
codec_internal.h
decode_frame
static int decode_frame(AVCodecContext *avctx, AVFrame *frame, int *got_frame_ptr, AVPacket *avpkt)
Definition: prores_raw.c:328
DECLARE_ALIGNED
#define DECLARE_ALIGNED(n, t, v)
Definition: mem_internal.h:104
ff_proresdsp_init
av_cold void ff_proresdsp_init(ProresDSPContext *dsp, int bits_per_raw_sample)
Definition: proresdsp.c:176
dst
uint8_t ptrdiff_t const uint8_t ptrdiff_t int intptr_t intptr_t int int16_t * dst
Definition: dsp.h:87
i
#define i(width, name, range_min, range_max)
Definition: cbs_h264.c:63
size
int size
Definition: twinvq_data.h:10344
MKBETAG
#define MKBETAG(a, b, c, d)
Definition: macros.h:56
FF_CODEC_CAP_SKIP_FRAME_FILL_PARAM
#define FF_CODEC_CAP_SKIP_FRAME_FILL_PARAM
The decoder extracts and fills its parameters even if the frame is skipped due to the skip_frame sett...
Definition: codec_internal.h:55
proresdata.h
AVCodecHWConfigInternal
Definition: hwconfig.h:25
align
static const uint8_t *BS_FUNC() align(BSCTX *bc)
Skip bits to a byte boundary.
Definition: bitstream_template.h:419
AV_CODEC_CAP_SLICE_THREADS
#define AV_CODEC_CAP_SLICE_THREADS
Codec supports slice-based (or partition-based) multithreading.
Definition: codec.h:99
offset
it s the only field you need to keep assuming you have a context There is some magic you don t need to care about around this just let it vf offset
Definition: writing_filters.txt:86
get_pixel_format
static enum AVPixelFormat get_pixel_format(AVCodecContext *avctx, enum AVPixelFormat pix_fmt)
Definition: prores_raw.c:311
xf
#define xf(width, name, var, range_min, range_max, subs,...)
Definition: cbs_av1.c:622
ProResRAWContext::pix_fmt
enum AVPixelFormat pix_fmt
Definition: prores_raw.h:49
av_refstruct_unref
void av_refstruct_unref(void *objp)
Decrement the reference count of the underlying object and automatically free the object if there are...
Definition: refstruct.c:120
AV_PIX_FMT_VIDEOTOOLBOX
@ AV_PIX_FMT_VIDEOTOOLBOX
hardware decoding through Videotoolbox
Definition: pixfmt.h:305
LN_CB_MAX
#define LN_CB_MAX
Definition: prores_raw.c:124
bytestream2_size
static av_always_inline int bytestream2_size(const GetByteContext *g)
Definition: bytestream.h:202
av_assert1
#define av_assert1(cond)
assert() equivalent, that does not lie in speed critical code.
Definition: avassert.h:58
FFMIN
#define FFMIN(a, b)
Definition: macros.h:49
AVCodec::name
const char * name
Name of the codec implementation.
Definition: codec.h:179
AVCOL_SPC_UNSPECIFIED
@ AVCOL_SPC_UNSPECIFIED
Definition: pixfmt.h:703
AVCodecContext::height
int height
Definition: avcodec.h:604
AVCodecContext::pix_fmt
enum AVPixelFormat pix_fmt
Pixel format, see AV_PIX_FMT_xxx.
Definition: avcodec.h:643
HWACCEL_VIDEOTOOLBOX
#define HWACCEL_VIDEOTOOLBOX(codec)
Definition: hwconfig.h:74
idctdsp.h
avcodec.h
ret
ret
Definition: filter_design.txt:187
frame
these buffered frames must be flushed immediately if a new input produces new the filter must not call request_frame to get more It must just process the frame or queue it The task of requesting more frames is left to the filter s request_frame method or the application If a filter has several the filter must be ready for frames arriving randomly on any input any filter with several inputs will most likely require some kind of queuing mechanism It is perfectly acceptable to have a limited queue and to drop frames when the inputs are too unbalanced request_frame For filters that do not use the this method is called when a frame is wanted on an output For a it should directly call filter_frame on the corresponding output For a if there are queued frames already one of these frames should be pushed If the filter should request a frame on one of its repeatedly until at least one frame has been pushed Return or at least make progress towards producing a frame
Definition: filter_design.txt:265
tile
static int FUNC() tile(CodedBitstreamContext *ctx, RWContext *rw, APVRawTile *current, int tile_idx, uint32_t tile_size)
Definition: cbs_apv_syntax_template.c:224
hwaccel
static const char * hwaccel
Definition: ffplay.c:356
get_value
static uint16_t get_value(GetBitContext *gb, int16_t codebook)
Definition: prores_raw.c:67
HWACCEL_VULKAN
#define HWACCEL_VULKAN(codec)
Definition: hwconfig.h:76
decode_comp
static int decode_comp(AVCodecContext *avctx, TileContext *tile, AVFrame *frame, const uint8_t *data, int size, int component, int16_t *qmat)
Definition: prores_raw.c:129
AVCodecContext
main external API structure.
Definition: avcodec.h:443
RN_CB_MAX
#define RN_CB_MAX
Definition: prores_raw.c:118
AV_PIX_FMT_NONE
@ AV_PIX_FMT_NONE
Definition: pixfmt.h:72
AVCodecContext::profile
int profile
profile
Definition: avcodec.h:1636
ffhwaccel
static const FFHWAccel * ffhwaccel(const AVHWAccel *codec)
Definition: hwaccel_internal.h:168
update_thread_context
the pkt_dts and pkt_pts fields in AVFrame will work as usual Restrictions on codec whose streams don t reset across will not work because their bitstreams cannot be decoded in parallel *The contents of buffers must not be read before as well as code calling up to before the decode process starts Call have update_thread_context() run it in the next thread. Add AV_CODEC_CAP_FRAME_THREADS to the codec capabilities. There will be very little speed gain at this point but it should work. Use ff_thread_get_buffer()(or ff_progress_frame_get_buffer() in case you have inter-frame dependencies and use the ProgressFrame API) to allocate frame buffers. Call ff_progress_frame_report() after some part of the current picture has decoded. A good place to put this is where draw_horiz_band() is called - add this if it isn 't called anywhere
AVCodecContext::coded_width
int coded_width
Bitstream width / height, may be different from width/height e.g.
Definition: avcodec.h:619
AVMEDIA_TYPE_VIDEO
@ AVMEDIA_TYPE_VIDEO
Definition: avutil.h:200
mem.h
avpriv_request_sample
#define avpriv_request_sample(...)
Definition: tableprint_vlc.h:37
w
uint8_t w
Definition: llvidencdsp.c:39
AVCodecContext::codec_tag
unsigned int codec_tag
fourcc (LSB first, so "ABCD" -> ('D'<<24) + ('C'<<16) + ('B'<<8) + 'A').
Definition: avcodec.h:468
scale
static void scale(int *out, const int *in, const int w, const int h, const int shift)
Definition: intra.c:278
FFALIGN
#define FFALIGN(x, a)
Definition: macros.h:78
AVPacket
This structure stores compressed data.
Definition: packet.h:572
AVCodecContext::priv_data
void * priv_data
Definition: avcodec.h:470
av_freep
#define av_freep(p)
Definition: tableprint_vlc.h:35
ff_prores_raw_ac_cb
const int16_t ff_prores_raw_ac_cb[AC_CB_MAX+1]
Definition: prores_raw.c:107
AVCodecContext::width
int width
picture width / height.
Definition: avcodec.h:604
int32_t
int32_t
Definition: audioconvert.c:56
bytestream.h
bytestream2_init
static av_always_inline void bytestream2_init(GetByteContext *g, const uint8_t *buf, int buf_size)
Definition: bytestream.h:137
block
The exact code depends on how similar the blocks are and how related they are to the block
Definition: filter_design.txt:207
av_log
#define av_log(a,...)
Definition: tableprint_vlc.h:27
DC_CB_MAX
#define DC_CB_MAX
Definition: prores_raw.c:101
TileContext
Definition: prores_raw.h:33
AVERROR_INVALIDDATA
#define AVERROR_INVALIDDATA
Invalid data found when processing input.
Definition: error.h:61
MKTAG
#define MKTAG(a, b, c, d)
Definition: macros.h:55
h
h
Definition: vp9dsp_template.c:2070
AV_PIX_FMT_BAYER_RGGB16
#define AV_PIX_FMT_BAYER_RGGB16
Definition: pixfmt.h:572
AVCodecContext::execute2
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:1628
src
#define src
Definition: vp8dsp.c:248
codebook
static const unsigned codebook[256][2]
Definition: cfhdenc.c:41
decode_tile
static int decode_tile(AVCodecContext *avctx, TileContext *tile, AVFrame *frame)
Definition: prores_raw.c:246