../../../ffmpeg/
Coverage:
low: ≥ 0%
medium: ≥ 75.0%
high: ≥ 90.0%
Lines:
0 / 325
0.0%
Branches:
0 / 140
0.0%
Functions:
0 / 9
0.0%

libavcodec/prores_raw.c
Line Branch Exec Source
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/intfloat.h"
25 #include "libavutil/intreadwrite.h"
26 #include "libavutil/mem_internal.h"
27 #include "libavutil/mem.h"
28 #include "libavutil/raw_color_params.h"
29 #include "libavutil/rational.h"
30
31 #define CACHED_BITSTREAM_READER !ARCH_X86_32
32
33 #include "config_components.h"
34 #include "avcodec.h"
35 #include "bytestream.h"
36 #include "codec_internal.h"
37 #include "decode.h"
38 #include "get_bits.h"
39 #include "proresdata.h"
40 #include "thread.h"
41 #include "hwconfig.h"
42 #include "hwaccel_internal.h"
43
44 #include "prores_raw.h"
45
46 ✗ static av_cold int decode_init(AVCodecContext *avctx)
47 {
48 ✗ ProResRAWContext *s = avctx->priv_data;
49
50 /* The codec outputs linear data, with the transfer function of the
51 * camera and any adjustments built into an 8-point linearization curve */
52 ✗ avctx->bits_per_raw_sample = 16;
53 ✗ avctx->color_trc = AVCOL_TRC_LINEAR;
54 ✗ avctx->color_primaries = AVCOL_PRI_UNSPECIFIED;
55 ✗ avctx->colorspace = AVCOL_SPC_UNSPECIFIED;
56
57 ✗ s->pix_fmt = AV_PIX_FMT_NONE;
58
59 ✗ ff_blockdsp_init(&s->bdsp);
60 ✗ ff_proresdsp_init(&s->prodsp, 12);
61
62 ✗ return 0;
63 }
64
65 /* Decodes a symbol from the next 32 bits, zero-filled past the end of the
66 * data. Returns a negative value once those are all zero, which is where
67 * the reference decoder stops decoding a component. */
68 ✗ static av_always_inline int get_value(GetBitContext *gb, int16_t codebook)
69 {
70 ✗ const int switch_bits = codebook >> 8;
71 ✗ const int rice_order = codebook & 0xf;
72 ✗ const int exp_order = (codebook >> 4) & 0xf;
73 ✗ int left = get_bits_left(gb);
74 uint32_t b;
75 int q, bits;
76
77 ✗ b = show_bits_long(gb, 32);
78
0/2
✗ Branch 4 → 5 not taken.
✗ Branch 4 → 8 not taken.
✗ if (left < 32) {
79 /* The reader has read past the end of the data; mask that off */
80
0/2
✗ Branch 5 → 6 not taken.
✗ Branch 5 → 7 not taken.
✗ if (left <= 0)
81 ✗ return -1;
82 ✗ b &= 0xFFFFFFFFu << (32 - left);
83 }
84
0/2
✗ Branch 8 → 9 not taken.
✗ Branch 8 → 10 not taken.
✗ if (!b)
85 ✗ return -1;
86
87 ✗ q = ff_clz(b);
88
89
0/2
✗ Branch 10 → 11 not taken.
✗ Branch 10 → 13 not taken.
✗ if (b & 0x80000000) {
90 ✗ skip_bits_long(gb, 1 + rice_order);
91 ✗ return (b & 0x7FFFFFFF) >> (31 - rice_order);
92 }
93
94
0/2
✗ Branch 13 → 14 not taken.
✗ Branch 13 → 16 not taken.
✗ if (q <= switch_bits) {
95 ✗ skip_bits_long(gb, 1 + rice_order + q);
96 ✗ return (q << rice_order) +
97 ✗ (((b << (q + 1)) >> 1) >> (31 - rice_order));
98 }
99
100 /* No valid code is longer than the window */
101 ✗ bits = exp_order + (q << 1) - switch_bits;
102
0/2
✗ Branch 16 → 17 not taken.
✗ Branch 16 → 18 not taken.
✗ if (bits > 32)
103 ✗ return -1;
104 ✗ skip_bits_long(gb, bits);
105 ✗ return (b >> (32 - bits)) +
106 ✗ ((switch_bits + 1) << rice_order) -
107 ✗ (1 << exp_order);
108 }
109
110 #define TODCCODEBOOK(x) ((x + 1) >> 1)
111
112 #define DC_CB_MAX 12
113 const uint8_t ff_prores_raw_dc_cb[DC_CB_MAX + 1] = {
114 0x010, 0x021, 0x032, 0x033, 0x033, 0x033, 0x044, 0x044, 0x044, 0x044, 0x044, 0x044, 0x076,
115 };
116
117 #define AC_CB_MAX 94
118 const int16_t ff_prores_raw_ac_cb[AC_CB_MAX + 1] = {
119 0x000, 0x211, 0x111, 0x111, 0x222, 0x222, 0x222, 0x122, 0x122, 0x122,
120 0x233, 0x233, 0x233, 0x233, 0x233, 0x233, 0x233, 0x233, 0x133, 0x133,
121 0x244, 0x244, 0x244, 0x244, 0x244, 0x244, 0x244, 0x244, 0x244, 0x244, 0x244,
122 0x244, 0x244, 0x244, 0x244, 0x244, 0x244, 0x244, 0x244, 0x244, 0x244, 0x244,
123 0x355, 0x355, 0x355, 0x355, 0x355, 0x355, 0x355, 0x355, 0x355, 0x355, 0x355, 0x355, 0x355,
124 0x355, 0x355, 0x355, 0x355, 0x355, 0x355, 0x355, 0x355, 0x355, 0x355, 0x355, 0x355, 0x355,
125 0x355, 0x355, 0x355, 0x355, 0x355, 0x355, 0x355, 0x355, 0x355, 0x355, 0x355, 0x355, 0x355,
126 0x355, 0x355, 0x355, 0x355, 0x355, 0x355, 0x355, 0x355, 0x355, 0x355, 0x355, 0x355, 0x355, 0x166,
127 };
128
129 #define RN_CB_MAX 27
130 const int16_t ff_prores_raw_rn_cb[RN_CB_MAX + 1] = {
131 0x200, 0x100, 0x000, 0x000, 0x211, 0x211, 0x111, 0x111, 0x011, 0x011, 0x021, 0x021, 0x222, 0x022,
132 0x022, 0x022, 0x022, 0x022, 0x022, 0x022, 0x022, 0x022, 0x022, 0x022, 0x022, 0x032, 0x032, 0x044
133 };
134
135 #define LN_CB_MAX 14
136 const int16_t ff_prores_raw_ln_cb[LN_CB_MAX + 1] = {
137 0x100, 0x111, 0x222, 0x222, 0x122, 0x122, 0x433, 0x433, 0x233, 0x233, 0x233, 0x233, 0x233, 0x233, 0x033,
138 };
139
140 ✗ static int decode_comp(AVCodecContext *avctx, TileContext *tile,
141 AVFrame *frame, const uint8_t *data, int size,
142 int component, int16_t *qmat)
143 {
144 int ret;
145 ✗ ProResRAWContext *s = avctx->priv_data;
146 ✗ const ptrdiff_t linesize = frame->linesize[0] >> 1;
147 ✗ uint16_t *dst = (uint16_t *)(frame->data[0] + tile->y*frame->linesize[0] + 2*tile->x);
148
149 int idx;
150 ✗ const int log2_nb_blocks = tile->log2_nb_blocks;
151 ✗ const int nb_blocks = 1 << log2_nb_blocks;
152 ✗ const int block_mask = nb_blocks - 1;
153 ✗ const int nb_codes = 64 * nb_blocks;
154
155 ✗ LOCAL_ALIGNED_32(int32_t, block, [64*16]);
156
157 ✗ int sign = 0;
158 ✗ int dc_add = 0;
159 int16_t dc_codebook;
160
161 int ac, rn, ln;
162 ✗ int16_t ac_codebook = 49;
163 ✗ int16_t rn_codebook = 0;
164 ✗ int16_t ln_codebook = 66;
165
166 ✗ const uint8_t *scan = ff_prores_interlaced_scan;
167 GetBitContext gb;
168
169
0/2
✗ Branch 2 → 3 not taken.
✗ Branch 2 → 4 not taken.
✗ if (component > 1)
170 ✗ dst += linesize;
171 ✗ dst += component & 1;
172
173
0/2
✗ Branch 5 → 6 not taken.
✗ Branch 5 → 7 not taken.
✗ if ((ret = init_get_bits8(&gb, data, size)) < 0)
174 ✗ return ret;
175
176 ✗ memset(block, 0, nb_blocks * 64 * sizeof(*block));
177
178 /* Special handling for first block */
179 ✗ int dc = get_value(&gb, 700);
180
0/2
✗ Branch 8 → 9 not taken.
✗ Branch 8 → 10 not taken.
✗ if (dc < 0)
181 ✗ goto end;
182 ✗ int prev_dc = (dc >> 1) ^ -(dc & 1);
183 ✗ block[0] = prev_dc;
184
185
0/2
✗ Branch 18 → 11 not taken.
✗ Branch 18 → 19 not taken.
✗ for (int n = 1; n < nb_blocks; n++) {
186
0/2
✗ Branch 11 → 12 not taken.
✗ Branch 11 → 13 not taken.
✗ if ((n & 15) == 1)
187 ✗ dc_codebook = 100;
188 else
189 ✗ dc_codebook = ff_prores_raw_dc_cb[FFMIN(TODCCODEBOOK(dc), DC_CB_MAX)];
190
191 ✗ dc = get_value(&gb, dc_codebook);
192
0/2
✗ Branch 15 → 16 not taken.
✗ Branch 15 → 17 not taken.
✗ if (dc < 0)
193 ✗ goto end;
194
195 ✗ sign = sign ^ dc & 1;
196 ✗ dc_add = (-sign ^ TODCCODEBOOK(dc)) + sign;
197 ✗ sign = dc_add < 0;
198 ✗ prev_dc += dc_add;
199
200 ✗ block[n*64] = prev_dc;
201 }
202
203
0/2
✗ Branch 42 → 20 not taken.
✗ Branch 42 → 43 not taken.
✗ for (int n = nb_blocks; n < nb_codes;) {
204 ✗ ln = get_value(&gb, ln_codebook);
205
0/2
✗ Branch 21 → 22 not taken.
✗ Branch 21 → 23 not taken.
✗ if (ln < 0)
206 ✗ goto end;
207
208
0/2
✗ Branch 31 → 24 not taken.
✗ Branch 31 → 32 not taken.
✗ for (int i = 0; i < ln; i++) {
209 ✗ ac = get_value(&gb, ac_codebook);
210
0/2
✗ Branch 25 → 26 not taken.
✗ Branch 25 → 27 not taken.
✗ if (ac < 0)
211 ✗ goto end;
212 ✗ ac_codebook = ff_prores_raw_ac_cb[FFMIN(ac, AC_CB_MAX)];
213 ✗ sign = -get_bits1(&gb);
214
215 ✗ idx = scan[n >> log2_nb_blocks] + ((n & block_mask) << 6);
216 ✗ block[idx] = ((ac + 1) ^ sign) - sign;
217
218
0/2
✗ Branch 28 → 29 not taken.
✗ Branch 28 → 30 not taken.
✗ if (++n == nb_codes)
219 ✗ goto end;
220 }
221
222 ✗ rn = get_value(&gb, rn_codebook);
223
0/2
✗ Branch 33 → 34 not taken.
✗ Branch 33 → 35 not taken.
✗ if (rn < 0)
224 ✗ goto end;
225 ✗ rn_codebook = ff_prores_raw_rn_cb[FFMIN(rn, RN_CB_MAX)];
226
227 ✗ n += rn + 1;
228
0/2
✗ Branch 35 → 36 not taken.
✗ Branch 35 → 37 not taken.
✗ if (n >= nb_codes)
229 ✗ break;
230
231 ✗ ac = get_value(&gb, ac_codebook);
232
0/2
✗ Branch 38 → 39 not taken.
✗ Branch 38 → 40 not taken.
✗ if (ac < 0)
233 ✗ goto end;
234 ✗ ac_codebook = ff_prores_raw_ac_cb[FFMIN(ac, AC_CB_MAX)];
235 ✗ ln_codebook = ff_prores_raw_ln_cb[FFMIN(ac, LN_CB_MAX)];
236 ✗ sign = -get_bits1(&gb);
237
238 ✗ idx = scan[n >> log2_nb_blocks] + ((n & block_mask) << 6);
239 ✗ block[idx] = ((ac + 1) ^ sign) - sign;
240
241 ✗ n++;
242 }
243
244 ✗ end:
245
0/2
✗ Branch 47 → 45 not taken.
✗ Branch 47 → 48 not taken.
✗ for (int n = 0; n < nb_blocks; n++) {
246 ✗ uint16_t *ptr = dst + n*16;
247 ✗ s->prodsp.idct_put_bayer(ptr, linesize, block + n*64, qmat, s->lin_curve);
248 }
249
250 ✗ return 0;
251 }
252
253 ✗ static int decode_tile(AVCodecContext *avctx, TileContext *tile,
254 AVFrame *frame)
255 {
256 int ret;
257 ✗ ProResRAWContext *s = avctx->priv_data;
258
259 ✗ GetByteContext *gb = &tile->gb;
260 ✗ LOCAL_ALIGNED_32(int16_t, qmat, [64]);
261
262
0/2
✗ Branch 2 → 3 not taken.
✗ Branch 2 → 4 not taken.
✗ if (tile->x >= avctx->width)
263 ✗ return 0;
264
265 /* Tile header */
266 ✗ int header_len = bytestream2_get_byteu(gb) >> 3;
267 ✗ int16_t scale = bytestream2_get_byteu(gb);
268
269 int size[4];
270 ✗ size[0] = bytestream2_get_be16(gb);
271 ✗ size[1] = bytestream2_get_be16(gb);
272 ✗ size[2] = bytestream2_get_be16(gb);
273 ✗ size[3] = bytestream2_size(gb) - size[0] - size[1] - size[2] - header_len;
274
0/2
✗ Branch 10 → 11 not taken.
✗ Branch 10 → 12 not taken.
✗ if (size[3] < 0)
275 ✗ return AVERROR_INVALIDDATA;
276
277
0/2
✗ Branch 14 → 13 not taken.
✗ Branch 14 → 15 not taken.
✗ for (int i = 0; i < 64; i++)
278 ✗ qmat[i] = s->qmat[i] * scale;
279
280 ✗ const uint8_t *comp_start = gb->buffer_start + header_len;
281
282 ✗ ret = decode_comp(avctx, tile, frame, comp_start,
283 size[0], 2, qmat);
284
0/2
✗ Branch 16 → 17 not taken.
✗ Branch 16 → 18 not taken.
✗ if (ret < 0)
285 ✗ goto fail;
286
287 ✗ ret = decode_comp(avctx, tile, frame, comp_start + size[0],
288 size[1], 1, qmat);
289
0/2
✗ Branch 19 → 20 not taken.
✗ Branch 19 → 21 not taken.
✗ if (ret < 0)
290 ✗ goto fail;
291
292 ✗ ret = decode_comp(avctx, tile, frame, comp_start + size[0] + size[1],
293 size[2], 3, qmat);
294
0/2
✗ Branch 22 → 23 not taken.
✗ Branch 22 → 24 not taken.
✗ if (ret < 0)
295 ✗ goto fail;
296
297 ✗ ret = decode_comp(avctx, tile, frame, comp_start + size[0] + size[1] + size[2],
298 size[3], 0, qmat);
299
0/2
✗ Branch 25 → 26 not taken.
✗ Branch 25 → 27 not taken.
✗ if (ret < 0)
300 ✗ goto fail;
301
302 ✗ return 0;
303 ✗ fail:
304 ✗ av_log(avctx, AV_LOG_ERROR, "tile %d/%d decoding error\n", tile->x, tile->y);
305 ✗ return ret;
306 }
307
308 ✗ static int decode_tiles(AVCodecContext *avctx, void *arg,
309 int n, int thread_nb)
310 {
311 ✗ ProResRAWContext *s = avctx->priv_data;
312 ✗ TileContext *tile = &s->tiles[n];
313 ✗ AVFrame *frame = arg;
314
315 ✗ return decode_tile(avctx, tile, frame);
316 }
317
318 ✗ static enum AVPixelFormat get_pixel_format(AVCodecContext *avctx,
319 enum AVPixelFormat pix_fmt)
320 {
321 ✗ enum AVPixelFormat pix_fmts[] = {
322 #if CONFIG_PRORES_RAW_VULKAN_HWACCEL
323 AV_PIX_FMT_VULKAN,
324 #endif
325 #if CONFIG_PRORES_RAW_VIDEOTOOLBOX_HWACCEL
326 AV_PIX_FMT_VIDEOTOOLBOX,
327 #endif
328 pix_fmt,
329 AV_PIX_FMT_NONE,
330 };
331
332 ✗ return ff_get_format(avctx, pix_fmts);
333 }
334
335 ✗ static int decode_frame(AVCodecContext *avctx,
336 AVFrame *frame, int *got_frame_ptr,
337 AVPacket *avpkt)
338 {
339 ✗ ProResRAWContext *s = avctx->priv_data;
340 ✗ int ret, dimensions_changed = 0, old_version = s->version;
341 DECLARE_ALIGNED(32, uint8_t, qmat)[64];
342 ✗ memset(qmat, 1, 64);
343
344
0/4
✗ Branch 2 → 3 not taken.
✗ Branch 2 → 4 not taken.
✗ Branch 2 → 5 not taken.
✗ Branch 2 → 6 not taken.
✗ switch (avctx->codec_tag) {
345 ✗ case 0:
346 ✗ break;
347 ✗ case MKTAG('a','p','r','n'):
348 ✗ avctx->profile = AV_PROFILE_PRORES_RAW;
349 ✗ break;
350 ✗ case MKTAG('a','p','r','h'):
351 ✗ avctx->profile = AV_PROFILE_PRORES_RAW_HQ;
352 ✗ break;
353 ✗ default:
354 ✗ avpriv_request_sample(avctx, "Profile %d", avctx->codec_tag);
355 ✗ return AVERROR_PATCHWELCOME;
356 break;
357 }
358
359 GetByteContext gb;
360 ✗ bytestream2_init(&gb, avpkt->data, avpkt->size);
361
0/2
✗ Branch 10 → 11 not taken.
✗ Branch 10 → 12 not taken.
✗ if (bytestream2_get_be32(&gb) != avpkt->size)
362 ✗ return AVERROR_INVALIDDATA;
363
364 /* ProRes RAW frame */
365
0/2
✗ Branch 13 → 14 not taken.
✗ Branch 13 → 15 not taken.
✗ if (bytestream2_get_be32(&gb) != MKBETAG('p','r','r','f'))
366 ✗ return AVERROR_INVALIDDATA;
367
368 ✗ int header_len = bytestream2_get_be16(&gb);
369
0/4
✗ Branch 16 → 17 not taken.
✗ Branch 16 → 19 not taken.
✗ Branch 18 → 19 not taken.
✗ Branch 18 → 20 not taken.
✗ if (header_len < 62 || bytestream2_get_bytes_left(&gb) < header_len - 2)
370 ✗ return AVERROR_INVALIDDATA;
371
372 GetByteContext gb_hdr;
373 ✗ bytestream2_init(&gb_hdr, gb.buffer, header_len - 2);
374 ✗ bytestream2_skip(&gb, header_len - 2);
375
376 ✗ bytestream2_skip(&gb_hdr, 1); /* 1 reserved byte */
377 ✗ s->version = bytestream2_get_byte(&gb_hdr);
378
0/2
✗ Branch 24 → 25 not taken.
✗ Branch 24 → 27 not taken.
✗ if (s->version > 1) {
379 ✗ avpriv_request_sample(avctx, "Version %d", s->version);
380 ✗ return AVERROR_PATCHWELCOME;
381 }
382
383 /* Vendor header (e.g. "peac" for Panasonic or "atm0" for Atmos) */
384 ✗ bytestream2_skip(&gb_hdr, 4);
385
386 /* Width and height must always be even */
387 ✗ int w = bytestream2_get_be16(&gb_hdr);
388 ✗ int h = bytestream2_get_be16(&gb_hdr);
389
0/4
✗ Branch 30 → 31 not taken.
✗ Branch 30 → 32 not taken.
✗ Branch 31 → 32 not taken.
✗ Branch 31 → 33 not taken.
✗ if ((w & 1) || (h & 1))
390 ✗ return AVERROR_INVALIDDATA;
391
392
0/4
✗ Branch 33 → 34 not taken.
✗ Branch 33 → 35 not taken.
✗ Branch 34 → 35 not taken.
✗ Branch 34 → 40 not taken.
✗ if (w != avctx->width || h != avctx->height) {
393 ✗ av_log(avctx, AV_LOG_WARNING, "picture resolution change: %ix%i -> %ix%i\n",
394 avctx->width, avctx->height, w, h);
395
0/2
✗ Branch 37 → 38 not taken.
✗ Branch 37 → 39 not taken.
✗ if ((ret = ff_set_dimensions(avctx, w, h)) < 0)
396 ✗ return ret;
397 ✗ dimensions_changed = 1;
398 }
399
400 ✗ avctx->coded_width = FFALIGN(w, 16);
401 ✗ avctx->coded_height = FFALIGN(h, 16);
402
403 ✗ enum AVPixelFormat pix_fmt = AV_PIX_FMT_BAYER_RGGB16;
404
0/4
✗ Branch 40 → 41 not taken.
✗ Branch 40 → 43 not taken.
✗ Branch 41 → 42 not taken.
✗ Branch 41 → 43 not taken.
✗ if (pix_fmt != s->pix_fmt || dimensions_changed ||
405
0/2
✗ Branch 42 → 43 not taken.
✗ Branch 42 → 47 not taken.
✗ s->version != old_version) {
406 ✗ s->pix_fmt = pix_fmt;
407
408 ✗ ret = get_pixel_format(avctx, pix_fmt);
409
0/2
✗ Branch 44 → 45 not taken.
✗ Branch 44 → 46 not taken.
✗ if (ret < 0)
410 ✗ return ret;
411
412 ✗ avctx->pix_fmt = ret;
413 }
414
415 /* RecommendedCrop: pixel margins to discard after debayer. Order is
416 * left/right/top/bottom */
417 ✗ uint8_t crop_l = bytestream2_get_byte(&gb_hdr);
418 ✗ uint8_t crop_r = bytestream2_get_byte(&gb_hdr);
419 ✗ uint8_t crop_t = bytestream2_get_byte(&gb_hdr);
420 ✗ uint8_t crop_b = bytestream2_get_byte(&gb_hdr);
421
422 /* BayerPattern: 0=RGGB, 1/2/3 = alternates */
423 ✗ int bayer_pattern = bytestream2_get_be16(&gb_hdr) & 0x3;
424
0/2
✗ Branch 52 → 53 not taken.
✗ Branch 52 → 55 not taken.
✗ if (bayer_pattern != 0) {
425 ✗ avpriv_request_sample(avctx, "Bayer pattern %d", bayer_pattern);
426 ✗ return AVERROR_PATCHWELCOME;
427 }
428
429 /* senselValueRange: black_level is hardcoded to 0x100,
430 * white_level = senselValueRange + 0x100 */
431 ✗ uint16_t black_level = 0x100;
432 ✗ uint16_t white_level = bytestream2_get_be16(&gb_hdr) + 0x100;
433
434 ✗ float wb_red = av_int2float(bytestream2_get_be32(&gb_hdr)); /* WhiteBalanceRedFactor */
435 ✗ float wb_blue = av_int2float(bytestream2_get_be32(&gb_hdr)); /* WhiteBalanceBlueFactor */
436
437 /* ColorMatrix (3x3 float, camera RGB -> CIE 1931 XYZ D65, row-major) */
438 float color_matrix[3][3];
439
0/2
✗ Branch 67 → 61 not taken.
✗ Branch 67 → 68 not taken.
✗ for (int r = 0; r < 3; r++)
440
0/2
✗ Branch 65 → 62 not taken.
✗ Branch 65 → 66 not taken.
✗ for (int c = 0; c < 3; c++)
441 ✗ color_matrix[r][c] = av_int2float(bytestream2_get_be32(&gb_hdr));
442
443 ✗ float gain = av_int2float(bytestream2_get_be32(&gb_hdr)); /* GainFactor (post-matrix mult) */
444 ✗ uint16_t wb_cct = bytestream2_get_be16(&gb_hdr); /* WhiteBalanceCCT (Kelvin, informational) */
445
446 /* Flags */
447 ✗ int flags = bytestream2_get_be16(&gb_hdr);
448 ✗ int align = (flags >> 1) & 0x7;
449
0/2
✗ Branch 72 → 73 not taken.
✗ Branch 72 → 75 not taken.
✗ if (align > 4) {
450 ✗ av_log(avctx, AV_LOG_ERROR,
451 "Invalid tile alignment %d (max 4)\n", align);
452 ✗ return AVERROR_INVALIDDATA;
453 }
454
455 /* Quantization matrix */
456
0/2
✗ Branch 75 → 76 not taken.
✗ Branch 75 → 77 not taken.
✗ if (flags & 1)
457 ✗ bytestream2_get_buffer(&gb_hdr, qmat, 64);
458
459
0/2
✗ Branch 77 → 78 not taken.
✗ Branch 77 → 82 not taken.
✗ if ((flags >> 4) & 1) {
460 /* 8-poing 16-bit control points, defining the combined linearization
461 * curve (inv. transfer fn + encoder-defined shaping) */
462
0/2
✗ Branch 81 → 79 not taken.
✗ Branch 81 → 83 not taken.
✗ for (int i = 0; i < 8; i++)
463 ✗ s->lin_curve[i] = bytestream2_get_be16(&gb_hdr);
464 } else {
465 /* default curve: ptwos */
466 static const uint16_t default_lin_curve[8] =
467 { 0, 512, 1024, 2048, 4096, 8192, 16384, 32768 };
468 ✗ memcpy(s->lin_curve, default_lin_curve, sizeof(s->lin_curve));
469 }
470
471 ✗ memcpy(s->qmat, qmat, sizeof(s->qmat));
472
473 ✗ int tw16 = (w + 15) >> 4;
474 ✗ s->nb_tw = (tw16 >> align) + av_popcount(~(-1 * (1 << align)) & tw16);
475 ✗ s->nb_th = (h + 15) >> 4;
476 ✗ s->nb_tiles = s->nb_tw * s->nb_th;
477 ✗ av_log(avctx, AV_LOG_DEBUG, "%dx%d | nb_tiles: %d\n", s->nb_tw, s->nb_th, s->nb_tiles);
478
479 ✗ s->th = 16;
480
481 ✗ av_fast_mallocz(&s->tiles, &s->tiles_size, s->nb_tiles * sizeof(*s->tiles));
482
0/2
✗ Branch 85 → 86 not taken.
✗ Branch 85 → 87 not taken.
✗ if (!s->tiles)
483 ✗ return AVERROR(ENOMEM);
484
485
0/2
✗ Branch 88 → 89 not taken.
✗ Branch 88 → 90 not taken.
✗ if (bytestream2_get_bytes_left(&gb) < s->nb_tiles * 2)
486 ✗ return AVERROR_INVALIDDATA;
487
488 /* First tile that extends past the right edge gets halved in width,
489 * next one gets quartered, and so on */
490 ✗ int offset = bytestream2_tell(&gb) + s->nb_tiles * 2;
491 ✗ int n = 0;
492
0/2
✗ Branch 107 → 92 not taken.
✗ Branch 107 → 108 not taken.
✗ for (int ty = 0; ty < s->nb_th; ty++) {
493 ✗ unsigned tx = 0;
494 ✗ int rem = tw16;
495
0/2
✗ Branch 105 → 93 not taken.
✗ Branch 105 → 106 not taken.
✗ for (int e = align; rem > 0; e--) {
496 ✗ int unit = 1 << e;
497
0/2
✗ Branch 103 → 94 not taken.
✗ Branch 103 → 104 not taken.
✗ while (unit <= rem) {
498 ✗ TileContext *tile = &s->tiles[n++];
499 ✗ int size = bytestream2_get_be16(&gb);
500
501
0/2
✗ Branch 95 → 96 not taken.
✗ Branch 95 → 97 not taken.
✗ if (offset >= avpkt->size)
502 ✗ return AVERROR_INVALIDDATA;
503
0/2
✗ Branch 97 → 98 not taken.
✗ Branch 97 → 99 not taken.
✗ if (size >= avpkt->size)
504 ✗ return AVERROR_INVALIDDATA;
505
0/2
✗ Branch 99 → 100 not taken.
✗ Branch 99 → 101 not taken.
✗ if (offset > avpkt->size - size)
506 ✗ return AVERROR_INVALIDDATA;
507
508 ✗ bytestream2_init(&tile->gb, avpkt->data + offset, size);
509 ✗ tile->x = tx * 16;
510 ✗ tile->y = ty * s->th;
511 ✗ tile->log2_nb_blocks = e;
512 ✗ offset += size;
513
514 ✗ tx += unit;
515 ✗ rem -= unit;
516 }
517 }
518 }
519 av_assert1(n == s->nb_tiles);
520
521 /* The stream parameters are all set by now, which is all that probing
522 * with frames skipped needs */
523
0/2
✗ Branch 108 → 109 not taken.
✗ Branch 108 → 110 not taken.
✗ if (avctx->skip_frame >= AVDISCARD_ALL)
524 ✗ return avpkt->size;
525
526 /**
527 * Any data between last tile and frame end is vendor-specific metadata:
528 * [psim record] 4 be32 size + "psim" + pascal string + payload
529 * <more records, if any>, or:
530 * [eomd] 4 be32 size=8 + "eomd" fourcc (end of metadata)
531 * [padding] zero-fill to next-frame alignment
532 *
533 * Known records (feel free to extend):
534 * com.panasonic.Semi-Pro.optical_correction (IEEE doubles):
535 * R - radial polynomial? + padding: [ k0, k1, k2, k3, pad0, pad1 ]
536 * G, B: same
537 * Trailer: optical center in normalized frame coords: [ x, y ]
538 */
539
540 ✗ ret = ff_thread_get_buffer(avctx, frame, 0);
541
0/2
✗ Branch 111 → 112 not taken.
✗ Branch 111 → 113 not taken.
✗ if (ret < 0)
542 ✗ return ret;
543
544 ✗ s->frame = frame;
545
546 ✗ av_refstruct_unref(&s->hwaccel_picture_private);
547 ✗ ret = ff_hwaccel_frame_priv_alloc(avctx, &s->hwaccel_picture_private);
548
0/2
✗ Branch 115 → 116 not taken.
✗ Branch 115 → 117 not taken.
✗ if (ret < 0)
549 ✗ return ret;
550
551 /* Everything the next frame thread needs is known, let it start */
552 ✗ ff_thread_finish_setup(avctx);
553
554 /* Start */
555
0/2
✗ Branch 118 → 119 not taken.
✗ Branch 118 → 133 not taken.
✗ if (avctx->hwaccel) {
556 ✗ const FFHWAccel *hwaccel = ffhwaccel(avctx->hwaccel);
557
558 ✗ ret = hwaccel->start_frame(avctx, avpkt->buf, avpkt->data, avpkt->size);
559
0/2
✗ Branch 121 → 122 not taken.
✗ Branch 121 → 123 not taken.
✗ if (ret < 0)
560 ✗ return ret;
561
562
0/2
✗ Branch 128 → 124 not taken.
✗ Branch 128 → 129 not taken.
✗ for (int n = 0; n < s->nb_tiles; n++) {
563 ✗ TileContext *tile = &s->tiles[n];
564 ✗ ret = hwaccel->decode_slice(avctx, tile->gb.buffer,
565 ✗ tile->gb.buffer_end - tile->gb.buffer);
566
0/2
✗ Branch 125 → 126 not taken.
✗ Branch 125 → 127 not taken.
✗ if (ret < 0)
567 ✗ return ret;
568 }
569
570 ✗ ret = hwaccel->end_frame(avctx);
571
0/2
✗ Branch 130 → 131 not taken.
✗ Branch 130 → 132 not taken.
✗ if (ret < 0)
572 ✗ return ret;
573
574 ✗ av_refstruct_unref(&s->hwaccel_picture_private);
575 } else {
576 ✗ avctx->execute2(avctx, decode_tiles, frame, NULL, s->nb_tiles);
577 }
578
579 ✗ frame->pict_type = AV_PICTURE_TYPE_I;
580 ✗ frame->flags |= AV_FRAME_FLAG_KEY;
581 ✗ frame->crop_left = crop_l;
582 ✗ frame->crop_right = crop_r;
583 ✗ frame->crop_top = crop_t;
584 ✗ frame->crop_bottom = crop_b;
585
586 ✗ AVRawColorParams *rcp = av_raw_color_params_create_side_data(frame);
587
0/2
✗ Branch 135 → 136 not taken.
✗ Branch 135 → 137 not taken.
✗ if (!rcp)
588 ✗ return AVERROR(ENOMEM);
589 ✗ rcp->type = AV_RAW_COLOR_PARAMS_PRORES_RAW;
590 ✗ rcp->black_level = av_make_q(black_level, 65535);
591 ✗ rcp->white_level = av_make_q(white_level, 65535);
592 ✗ rcp->wb_cct = wb_cct;
593
594 ✗ AVProResRawColorParams *pr = &rcp->codec.prores_raw;
595 ✗ pr->wb_red = av_d2q(wb_red, INT_MAX);
596 ✗ pr->wb_blue = av_d2q(wb_blue, INT_MAX);
597 ✗ pr->gain = av_d2q(gain, INT_MAX);
598
0/2
✗ Branch 144 → 140 not taken.
✗ Branch 144 → 145 not taken.
✗ for (int r = 0; r < 3; r++)
599
0/2
✗ Branch 142 → 141 not taken.
✗ Branch 142 → 143 not taken.
✗ for (int c = 0; c < 3; c++)
600 ✗ pr->color_matrix[r][c] = av_d2q(color_matrix[r][c], INT_MAX);
601
602 ✗ *got_frame_ptr = 1;
603
604 ✗ return avpkt->size;
605 }
606
607 ✗ static av_cold int decode_end(AVCodecContext *avctx)
608 {
609 ✗ ProResRAWContext *s = avctx->priv_data;
610 ✗ av_refstruct_unref(&s->hwaccel_picture_private);
611 ✗ av_freep(&s->tiles);
612 ✗ return 0;
613 }
614
615 #if HAVE_THREADS
616 ✗ static int update_thread_context(AVCodecContext *dst, const AVCodecContext *src)
617 {
618 ✗ ProResRAWContext *rsrc = src->priv_data;
619 ✗ ProResRAWContext *rdst = dst->priv_data;
620
621 ✗ rdst->pix_fmt = rsrc->pix_fmt;
622 ✗ rdst->version = rsrc->version;
623
624 ✗ return 0;
625 }
626 #endif
627
628 const FFCodec ff_prores_raw_decoder = {
629 .p.name = "prores_raw",
630 CODEC_LONG_NAME("Apple ProRes RAW"),
631 .p.type = AVMEDIA_TYPE_VIDEO,
632 .p.id = AV_CODEC_ID_PRORES_RAW,
633 .priv_data_size = sizeof(ProResRAWContext),
634 .init = decode_init,
635 .close = decode_end,
636 FF_CODEC_DECODE_CB(decode_frame),
637 UPDATE_THREAD_CONTEXT(update_thread_context),
638 .p.capabilities = AV_CODEC_CAP_DR1 |
639 AV_CODEC_CAP_FRAME_THREADS |
640 AV_CODEC_CAP_SLICE_THREADS,
641 .caps_internal = FF_CODEC_CAP_INIT_CLEANUP |
642 FF_CODEC_CAP_SKIP_FRAME_FILL_PARAM,
643 .hw_configs = (const AVCodecHWConfigInternal *const []) {
644 #if CONFIG_PRORES_RAW_VULKAN_HWACCEL
645 HWACCEL_VULKAN(prores_raw),
646 #endif
647 #if CONFIG_PRORES_RAW_VIDEOTOOLBOX_HWACCEL
648 HWACCEL_VIDEOTOOLBOX(prores_raw),
649 #endif
650 NULL
651 },
652 };
653