FFmpeg coverage


Directory: ../../../ffmpeg/
File: src/libavcodec/proresdec.c
Date: 2025-10-10 03:51:19
Exec Total Coverage
Lines: 336 418 80.4%
Functions: 14 16 87.5%
Branches: 174 253 68.8%

Line Branch Exec Source
1 /*
2 * Copyright (c) 2010-2011 Maxim Poliakovski
3 * Copyright (c) 2010-2011 Elvis Presley
4 *
5 * This file is part of FFmpeg.
6 *
7 * FFmpeg is free software; you can redistribute it and/or
8 * modify it under the terms of the GNU Lesser General Public
9 * License as published by the Free Software Foundation; either
10 * version 2.1 of the License, or (at your option) any later version.
11 *
12 * FFmpeg is distributed in the hope that it will be useful,
13 * but WITHOUT ANY WARRANTY; without even the implied warranty of
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
15 * Lesser General Public License for more details.
16 *
17 * You should have received a copy of the GNU Lesser General Public
18 * License along with FFmpeg; if not, write to the Free Software
19 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
20 */
21
22 /**
23 * @file
24 * Known FOURCCs: 'apch' (HQ), 'apcn' (SD), 'apcs' (LT), 'apco' (Proxy), 'ap4h' (4444), 'ap4x' (4444 XQ)
25 */
26
27 //#define DEBUG
28
29 #include "config_components.h"
30
31 #include "libavutil/internal.h"
32 #include "libavutil/mem.h"
33 #include "libavutil/mem_internal.h"
34
35 #include "avcodec.h"
36 #include "codec_internal.h"
37 #include "decode.h"
38 #include "get_bits.h"
39 #include "hwaccel_internal.h"
40 #include "hwconfig.h"
41 #include "idctdsp.h"
42 #include "profiles.h"
43 #include "proresdec.h"
44 #include "proresdata.h"
45 #include "thread.h"
46
47 #define ALPHA_SHIFT_16_TO_10(alpha_val) (alpha_val >> 6)
48 #define ALPHA_SHIFT_8_TO_10(alpha_val) ((alpha_val << 2) | (alpha_val >> 6))
49 #define ALPHA_SHIFT_16_TO_12(alpha_val) (alpha_val >> 4)
50 #define ALPHA_SHIFT_8_TO_12(alpha_val) ((alpha_val << 4) | (alpha_val >> 4))
51
52 5100 static void inline unpack_alpha(GetBitContext *gb, uint16_t *dst, int num_coeffs,
53 const int num_bits, const int decode_precision) {
54 5100 const int mask = (1 << num_bits) - 1;
55 int i, idx, val, alpha_val;
56
57 5100 idx = 0;
58 5100 alpha_val = mask;
59 do {
60 do {
61
2/2
✓ Branch 1 taken 43404 times.
✓ Branch 2 taken 14996 times.
58400 if (get_bits1(gb)) {
62 43404 val = get_bits(gb, num_bits);
63 } else {
64 int sign;
65
1/2
✓ Branch 0 taken 14996 times.
✗ Branch 1 not taken.
14996 val = get_bits(gb, num_bits == 16 ? 7 : 4);
66 14996 sign = val & 1;
67 14996 val = (val + 2) >> 1;
68
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 14996 times.
14996 if (sign)
69 val = -val;
70 }
71 58400 alpha_val = (alpha_val + val) & mask;
72
1/2
✓ Branch 0 taken 58400 times.
✗ Branch 1 not taken.
58400 if (num_bits == 16) {
73
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 58400 times.
58400 if (decode_precision == 10) {
74 dst[idx++] = ALPHA_SHIFT_16_TO_10(alpha_val);
75 } else { /* 12b */
76 58400 dst[idx++] = ALPHA_SHIFT_16_TO_12(alpha_val);
77 }
78 } else {
79 if (decode_precision == 10) {
80 dst[idx++] = ALPHA_SHIFT_8_TO_10(alpha_val);
81 } else { /* 12b */
82 dst[idx++] = ALPHA_SHIFT_8_TO_12(alpha_val);
83 }
84 }
85
2/2
✓ Branch 0 taken 36 times.
✓ Branch 1 taken 58364 times.
58400 if (idx >= num_coeffs)
86 36 break;
87
4/4
✓ Branch 1 taken 45296 times.
✓ Branch 2 taken 13068 times.
✓ Branch 4 taken 20848 times.
✓ Branch 5 taken 24448 times.
58364 } while (get_bits_left(gb)>0 && get_bits1(gb));
88 37552 val = get_bits(gb, 4);
89
2/2
✓ Branch 0 taken 35779 times.
✓ Branch 1 taken 1773 times.
37552 if (!val)
90 35779 val = get_bits(gb, 11);
91
2/2
✓ Branch 0 taken 140 times.
✓ Branch 1 taken 37412 times.
37552 if (idx + val > num_coeffs)
92 140 val = num_coeffs - idx;
93
1/2
✓ Branch 0 taken 37552 times.
✗ Branch 1 not taken.
37552 if (num_bits == 16) {
94
2/2
✓ Branch 0 taken 10386400 times.
✓ Branch 1 taken 37552 times.
10423952 for (i = 0; i < val; i++) {
95
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 10386400 times.
10386400 if (decode_precision == 10) {
96 dst[idx++] = ALPHA_SHIFT_16_TO_10(alpha_val);
97 } else { /* 12b */
98 10386400 dst[idx++] = ALPHA_SHIFT_16_TO_12(alpha_val);
99 }
100 }
101 } else {
102 for (i = 0; i < val; i++) {
103 if (decode_precision == 10) {
104 dst[idx++] = ALPHA_SHIFT_8_TO_10(alpha_val);
105 } else { /* 12b */
106 dst[idx++] = ALPHA_SHIFT_8_TO_12(alpha_val);
107 }
108 }
109 }
110
2/2
✓ Branch 0 taken 32452 times.
✓ Branch 1 taken 5100 times.
37552 } while (idx < num_coeffs);
111 5100 }
112
113 static void unpack_alpha_10(GetBitContext *gb, uint16_t *dst, int num_coeffs,
114 const int num_bits)
115 {
116 if (num_bits == 16) {
117 unpack_alpha(gb, dst, num_coeffs, 16, 10);
118 } else { /* 8 bits alpha */
119 unpack_alpha(gb, dst, num_coeffs, 8, 10);
120 }
121 }
122
123 5100 static void unpack_alpha_12(GetBitContext *gb, uint16_t *dst, int num_coeffs,
124 const int num_bits)
125 {
126
1/2
✓ Branch 0 taken 5100 times.
✗ Branch 1 not taken.
5100 if (num_bits == 16) {
127 5100 unpack_alpha(gb, dst, num_coeffs, 16, 12);
128 } else { /* 8 bits alpha */
129 unpack_alpha(gb, dst, num_coeffs, 8, 12);
130 }
131 5100 }
132
133 76 static av_cold int decode_init(AVCodecContext *avctx)
134 {
135 76 ProresContext *ctx = avctx->priv_data;
136
137 76 avctx->bits_per_raw_sample = 10;
138
139
5/7
✓ Branch 0 taken 14 times.
✓ Branch 1 taken 2 times.
✓ Branch 2 taken 21 times.
✓ Branch 3 taken 15 times.
✓ Branch 4 taken 24 times.
✗ Branch 5 not taken.
✗ Branch 6 not taken.
76 switch (avctx->codec_tag) {
140 14 case MKTAG('a','p','c','o'):
141 14 avctx->profile = AV_PROFILE_PRORES_PROXY;
142 14 break;
143 2 case MKTAG('a','p','c','s'):
144 2 avctx->profile = AV_PROFILE_PRORES_LT;
145 2 break;
146 21 case MKTAG('a','p','c','n'):
147 21 avctx->profile = AV_PROFILE_PRORES_STANDARD;
148 21 break;
149 15 case MKTAG('a','p','c','h'):
150 15 avctx->profile = AV_PROFILE_PRORES_HQ;
151 15 break;
152 24 case MKTAG('a','p','4','h'):
153 24 avctx->profile = AV_PROFILE_PRORES_4444;
154 24 avctx->bits_per_raw_sample = 12;
155 24 break;
156 case MKTAG('a','p','4','x'):
157 avctx->profile = AV_PROFILE_PRORES_XQ;
158 avctx->bits_per_raw_sample = 12;
159 break;
160 default:
161 avctx->profile = AV_PROFILE_UNKNOWN;
162 av_log(avctx, AV_LOG_WARNING, "Unknown prores profile %d\n", avctx->codec_tag);
163 }
164
165 152 ctx->unpack_alpha = avctx->bits_per_raw_sample == 10 ?
166
2/2
✓ Branch 0 taken 52 times.
✓ Branch 1 taken 24 times.
76 unpack_alpha_10 : unpack_alpha_12;
167
168 76 av_log(avctx, AV_LOG_DEBUG,
169 "Auto bitdepth precision. Use %db decoding based on codec tag.\n",
170 avctx->bits_per_raw_sample);
171
172 76 ff_blockdsp_init(&ctx->bdsp);
173 76 ff_proresdsp_init(&ctx->prodsp, avctx->bits_per_raw_sample);
174
175 76 ff_permute_scantable(ctx->progressive_scan, ff_prores_progressive_scan,
176 76 ctx->prodsp.idct_permutation);
177 76 ff_permute_scantable(ctx->interlaced_scan, ff_prores_interlaced_scan,
178 76 ctx->prodsp.idct_permutation);
179
180 76 ctx->pix_fmt = AV_PIX_FMT_NONE;
181
182 76 return 0;
183 }
184
185 1065 static int decode_frame_header(ProresContext *ctx, const uint8_t *buf,
186 const int data_size, AVCodecContext *avctx)
187 {
188 int hdr_size, width, height, flags;
189 int version;
190 const uint8_t *ptr;
191 enum AVPixelFormat pix_fmt;
192
193 1065 hdr_size = AV_RB16(buf);
194 ff_dlog(avctx, "header size %d\n", hdr_size);
195
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 1065 times.
1065 if (hdr_size > data_size) {
196 av_log(avctx, AV_LOG_ERROR, "error, wrong header size\n");
197 return AVERROR_INVALIDDATA;
198 }
199
200 1065 version = AV_RB16(buf + 2);
201 ff_dlog(avctx, "%.4s version %d\n", buf+4, version);
202
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 1065 times.
1065 if (version > 1) {
203 av_log(avctx, AV_LOG_ERROR, "unsupported version: %d\n", version);
204 return AVERROR_PATCHWELCOME;
205 }
206
207 1065 width = AV_RB16(buf + 8);
208 1065 height = AV_RB16(buf + 10);
209
210
2/4
✓ Branch 0 taken 1065 times.
✗ Branch 1 not taken.
✗ Branch 2 not taken.
✓ Branch 3 taken 1065 times.
1065 if (width != avctx->width || height != avctx->height) {
211 int ret;
212
213 av_log(avctx, AV_LOG_WARNING, "picture resolution change: %dx%d -> %dx%d\n",
214 avctx->width, avctx->height, width, height);
215 if ((ret = ff_set_dimensions(avctx, width, height)) < 0)
216 return ret;
217 }
218
219 1065 ctx->frame_type = (buf[12] >> 2) & 3;
220 1065 ctx->alpha_info = buf[17] & 0xf;
221
222
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 1065 times.
1065 if (ctx->alpha_info > 2) {
223 av_log(avctx, AV_LOG_ERROR, "Invalid alpha mode %d\n", ctx->alpha_info);
224 return AVERROR_INVALIDDATA;
225 }
226
2/2
✓ Branch 0 taken 5 times.
✓ Branch 1 taken 1060 times.
1065 if (avctx->skip_alpha) ctx->alpha_info = 0;
227
228 ff_dlog(avctx, "frame type %d\n", ctx->frame_type);
229
230
2/2
✓ Branch 0 taken 633 times.
✓ Branch 1 taken 432 times.
1065 if (ctx->frame_type == 0) {
231 633 ctx->scan = ctx->progressive_scan; // permuted
232 } else {
233 432 ctx->scan = ctx->interlaced_scan; // permuted
234 432 ctx->frame->flags |= AV_FRAME_FLAG_INTERLACED;
235
2/2
✓ Branch 0 taken 408 times.
✓ Branch 1 taken 24 times.
432 if (ctx->frame_type == 1)
236 408 ctx->frame->flags |= AV_FRAME_FLAG_TOP_FIELD_FIRST;
237 }
238
239
2/2
✓ Branch 0 taken 5 times.
✓ Branch 1 taken 1060 times.
1065 if (ctx->alpha_info) {
240
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 5 times.
5 if (avctx->bits_per_raw_sample == 10) {
241 pix_fmt = (buf[12] & 0xC0) == 0xC0 ? AV_PIX_FMT_YUVA444P10 : AV_PIX_FMT_YUVA422P10;
242 } else { /* 12b */
243
1/2
✓ Branch 0 taken 5 times.
✗ Branch 1 not taken.
5 pix_fmt = (buf[12] & 0xC0) == 0xC0 ? AV_PIX_FMT_YUVA444P12 : AV_PIX_FMT_YUVA422P12;
244 }
245 } else {
246
2/2
✓ Branch 0 taken 647 times.
✓ Branch 1 taken 413 times.
1060 if (avctx->bits_per_raw_sample == 10) {
247
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 647 times.
647 pix_fmt = (buf[12] & 0xC0) == 0xC0 ? AV_PIX_FMT_YUV444P10 : AV_PIX_FMT_YUV422P10;
248 } else { /* 12b */
249
1/2
✓ Branch 0 taken 413 times.
✗ Branch 1 not taken.
413 pix_fmt = (buf[12] & 0xC0) == 0xC0 ? AV_PIX_FMT_YUV444P12 : AV_PIX_FMT_YUV422P12;
250 }
251 }
252
253
2/2
✓ Branch 0 taken 73 times.
✓ Branch 1 taken 992 times.
1065 if (pix_fmt != ctx->pix_fmt) {
254 #define HWACCEL_MAX (CONFIG_PRORES_VIDEOTOOLBOX_HWACCEL)
255 #if HWACCEL_MAX
256 enum AVPixelFormat pix_fmts[HWACCEL_MAX + 2], *fmtp = pix_fmts;
257 int ret;
258
259 ctx->pix_fmt = pix_fmt;
260
261 #if CONFIG_PRORES_VIDEOTOOLBOX_HWACCEL
262 *fmtp++ = AV_PIX_FMT_VIDEOTOOLBOX;
263 #endif
264 *fmtp++ = ctx->pix_fmt;
265 *fmtp = AV_PIX_FMT_NONE;
266
267 if ((ret = ff_get_format(avctx, pix_fmts)) < 0)
268 return ret;
269
270 avctx->pix_fmt = ret;
271 #else
272 73 avctx->pix_fmt = ctx->pix_fmt = pix_fmt;
273 #endif
274 }
275
276 1065 ctx->frame->color_primaries = buf[14];
277 1065 ctx->frame->color_trc = buf[15];
278 1065 ctx->frame->colorspace = buf[16];
279 1065 ctx->frame->color_range = AVCOL_RANGE_MPEG;
280
281 1065 ptr = buf + 20;
282 1065 flags = buf[19];
283 ff_dlog(avctx, "flags %x\n", flags);
284
285
1/2
✓ Branch 0 taken 1065 times.
✗ Branch 1 not taken.
1065 if (flags & 2) {
286
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 1065 times.
1065 if(buf + data_size - ptr < 64) {
287 av_log(avctx, AV_LOG_ERROR, "Header truncated\n");
288 return AVERROR_INVALIDDATA;
289 }
290 1065 ff_permute_scantable(ctx->qmat_luma, ctx->prodsp.idct_permutation, ptr);
291 1065 ptr += 64;
292 } else {
293 memset(ctx->qmat_luma, 4, 64);
294 }
295
296
1/2
✓ Branch 0 taken 1065 times.
✗ Branch 1 not taken.
1065 if (flags & 1) {
297
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 1065 times.
1065 if(buf + data_size - ptr < 64) {
298 av_log(avctx, AV_LOG_ERROR, "Header truncated\n");
299 return AVERROR_INVALIDDATA;
300 }
301 1065 ff_permute_scantable(ctx->qmat_chroma, ctx->prodsp.idct_permutation, ptr);
302 } else {
303 memcpy(ctx->qmat_chroma, ctx->qmat_luma, 64);
304 }
305
306 1065 return hdr_size;
307 }
308
309 1497 static int decode_picture_header(AVCodecContext *avctx, const uint8_t *buf, const int buf_size)
310 {
311 1497 ProresContext *ctx = avctx->priv_data;
312 int i, hdr_size, slice_count;
313 unsigned pic_data_size;
314 int log2_slice_mb_width, log2_slice_mb_height;
315 int slice_mb_count, mb_x, mb_y;
316 const uint8_t *data_ptr, *index_ptr;
317
318 1497 hdr_size = buf[0] >> 3;
319
2/4
✓ Branch 0 taken 1497 times.
✗ Branch 1 not taken.
✗ Branch 2 not taken.
✓ Branch 3 taken 1497 times.
1497 if (hdr_size < 8 || hdr_size > buf_size) {
320 av_log(avctx, AV_LOG_ERROR, "error, wrong picture header size\n");
321 return AVERROR_INVALIDDATA;
322 }
323
324 1497 pic_data_size = AV_RB32(buf + 1);
325
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 1497 times.
1497 if (pic_data_size > buf_size) {
326 av_log(avctx, AV_LOG_ERROR, "error, wrong picture data size\n");
327 return AVERROR_INVALIDDATA;
328 }
329
330 1497 log2_slice_mb_width = buf[7] >> 4;
331 1497 log2_slice_mb_height = buf[7] & 0xF;
332
2/4
✓ Branch 0 taken 1497 times.
✗ Branch 1 not taken.
✗ Branch 2 not taken.
✓ Branch 3 taken 1497 times.
1497 if (log2_slice_mb_width > 3 || log2_slice_mb_height) {
333 av_log(avctx, AV_LOG_ERROR, "unsupported slice resolution: %dx%d\n",
334 1 << log2_slice_mb_width, 1 << log2_slice_mb_height);
335 return AVERROR_INVALIDDATA;
336 }
337
338 1497 ctx->mb_width = (avctx->width + 15) >> 4;
339
2/2
✓ Branch 0 taken 864 times.
✓ Branch 1 taken 633 times.
1497 if (ctx->frame_type)
340 864 ctx->mb_height = (avctx->height + 31) >> 5;
341 else
342 633 ctx->mb_height = (avctx->height + 15) >> 4;
343
344 // QT ignores the written value
345 // slice_count = AV_RB16(buf + 5);
346 1497 slice_count = ctx->mb_height * ((ctx->mb_width >> log2_slice_mb_width) +
347 1497 av_popcount(ctx->mb_width & (1 << log2_slice_mb_width) - 1));
348
349
3/4
✓ Branch 0 taken 1424 times.
✓ Branch 1 taken 73 times.
✗ Branch 2 not taken.
✓ Branch 3 taken 1424 times.
1497 if (ctx->slice_count != slice_count || !ctx->slices) {
350 73 av_freep(&ctx->slices);
351 73 ctx->slice_count = 0;
352 73 ctx->slices = av_calloc(slice_count, sizeof(*ctx->slices));
353
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 73 times.
73 if (!ctx->slices)
354 return AVERROR(ENOMEM);
355 73 ctx->slice_count = slice_count;
356 }
357
358
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 1497 times.
1497 if (!slice_count)
359 return AVERROR(EINVAL);
360
361
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 1497 times.
1497 if (hdr_size + slice_count*2 > buf_size) {
362 av_log(avctx, AV_LOG_ERROR, "error, wrong slice count\n");
363 return AVERROR_INVALIDDATA;
364 }
365
366 // parse slice information
367 1497 index_ptr = buf + hdr_size;
368 1497 data_ptr = index_ptr + slice_count*2;
369
370 1497 slice_mb_count = 1 << log2_slice_mb_width;
371 1497 mb_x = 0;
372 1497 mb_y = 0;
373
374
2/2
✓ Branch 0 taken 91950 times.
✓ Branch 1 taken 1497 times.
93447 for (i = 0; i < slice_count; i++) {
375 91950 SliceContext *slice = &ctx->slices[i];
376
377 91950 slice->data = data_ptr;
378 91950 data_ptr += AV_RB16(index_ptr + i*2);
379
380
2/2
✓ Branch 0 taken 30645 times.
✓ Branch 1 taken 91950 times.
122595 while (ctx->mb_width - mb_x < slice_mb_count)
381 30645 slice_mb_count >>= 1;
382
383 91950 slice->mb_x = mb_x;
384 91950 slice->mb_y = mb_y;
385 91950 slice->mb_count = slice_mb_count;
386 91950 slice->data_size = data_ptr - slice->data;
387
388
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 91950 times.
91950 if (slice->data_size < 6) {
389 av_log(avctx, AV_LOG_ERROR, "error, wrong slice data size\n");
390 return AVERROR_INVALIDDATA;
391 }
392
393 91950 mb_x += slice_mb_count;
394
2/2
✓ Branch 0 taken 17249 times.
✓ Branch 1 taken 74701 times.
91950 if (mb_x == ctx->mb_width) {
395 17249 slice_mb_count = 1 << log2_slice_mb_width;
396 17249 mb_x = 0;
397 17249 mb_y++;
398 }
399
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 91950 times.
91950 if (data_ptr > buf + buf_size) {
400 av_log(avctx, AV_LOG_ERROR, "error, slice out of bounds\n");
401 return AVERROR_INVALIDDATA;
402 }
403 }
404
405
2/4
✓ Branch 0 taken 1497 times.
✗ Branch 1 not taken.
✗ Branch 2 not taken.
✓ Branch 3 taken 1497 times.
1497 if (mb_x || mb_y != ctx->mb_height) {
406 av_log(avctx, AV_LOG_ERROR, "error wrong mb count y %d h %d\n",
407 mb_y, ctx->mb_height);
408 return AVERROR_INVALIDDATA;
409 }
410
411 1497 return pic_data_size;
412 }
413
414 #define DECODE_CODEWORD(val, codebook, SKIP) \
415 do { \
416 unsigned int rice_order, exp_order, switch_bits; \
417 unsigned int q, buf, bits; \
418 \
419 UPDATE_CACHE_32(re, gb); /* We really need 32 bits */ \
420 buf = GET_CACHE(re, gb); \
421 \
422 /* number of bits to switch between rice and exp golomb */ \
423 switch_bits = codebook & 3; \
424 rice_order = codebook >> 5; \
425 exp_order = (codebook >> 2) & 7; \
426 \
427 q = 31 - av_log2(buf); \
428 \
429 if (q > switch_bits) { /* exp golomb */ \
430 bits = exp_order - switch_bits + (q<<1); \
431 if (bits > 31) \
432 return AVERROR_INVALIDDATA; \
433 val = SHOW_UBITS(re, gb, bits) - (1 << exp_order) + \
434 ((switch_bits + 1) << rice_order); \
435 SKIP(re, gb, bits); \
436 } else if (rice_order) { \
437 SKIP_BITS(re, gb, q+1); \
438 val = (q << rice_order) + SHOW_UBITS(re, gb, rice_order); \
439 SKIP(re, gb, rice_order); \
440 } else { \
441 val = q; \
442 SKIP(re, gb, q+1); \
443 } \
444 } while (0)
445
446 #define TOSIGNED(x) (((x) >> 1) ^ (-((x) & 1)))
447
448 #define FIRST_DC_CB 0xB8
449
450 static const uint8_t dc_codebook[7] = { 0x04, 0x28, 0x28, 0x4D, 0x4D, 0x70, 0x70};
451
452 274338 static av_always_inline int decode_dc_coeffs(GetBitContext *gb, int16_t *out,
453 int blocks_per_slice)
454 {
455 int16_t prev_dc;
456 int code, i, sign;
457
458 274338 OPEN_READER(re, gb);
459
460
4/6
✓ Branch 0 taken 229079 times.
✓ Branch 1 taken 45259 times.
✗ Branch 2 not taken.
✓ Branch 3 taken 229079 times.
✓ Branch 5 taken 45259 times.
✗ Branch 6 not taken.
274338 DECODE_CODEWORD(code, FIRST_DC_CB, LAST_SKIP_BITS);
461 274338 prev_dc = TOSIGNED(code);
462 274338 out[0] = prev_dc;
463
464 274338 out += 64; // dc coeff for the next block
465
466 274338 code = 5;
467 274338 sign = 0;
468
2/2
✓ Branch 0 taken 5199858 times.
✓ Branch 1 taken 274338 times.
5474196 for (i = 1; i < blocks_per_slice; i++, out += 64) {
469
5/6
✓ Branch 0 taken 2892583 times.
✓ Branch 1 taken 2307275 times.
✗ Branch 2 not taken.
✓ Branch 3 taken 2892583 times.
✓ Branch 5 taken 442866 times.
✓ Branch 6 taken 1864409 times.
5199858 DECODE_CODEWORD(code, dc_codebook[FFMIN(code, 6U)], LAST_SKIP_BITS);
470
2/2
✓ Branch 0 taken 3119514 times.
✓ Branch 1 taken 2080344 times.
5199858 if(code) sign ^= -(code & 1);
471 2080344 else sign = 0;
472 5199858 prev_dc += (((code + 1) >> 1) ^ sign) - sign;
473 5199858 out[0] = prev_dc;
474 }
475 274338 CLOSE_READER(re, gb);
476 274338 return 0;
477 }
478
479 // adaptive codebook switching lut according to previous run/level values
480 static const uint8_t run_to_cb[16] = { 0x06, 0x06, 0x05, 0x05, 0x04, 0x29, 0x29, 0x29, 0x29, 0x28, 0x28, 0x28, 0x28, 0x28, 0x28, 0x4C };
481 static const uint8_t lev_to_cb[10] = { 0x04, 0x0A, 0x05, 0x06, 0x04, 0x28, 0x28, 0x28, 0x28, 0x4C };
482
483 274338 static av_always_inline int decode_ac_coeffs(AVCodecContext *avctx, GetBitContext *gb,
484 int16_t *out, int blocks_per_slice)
485 {
486 274338 const ProresContext *ctx = avctx->priv_data;
487 int block_mask, sign;
488 unsigned pos, run, level;
489 int max_coeffs, i, bits_left;
490 274338 int log2_block_count = av_log2(blocks_per_slice);
491
492 274338 OPEN_READER(re, gb);
493 274338 UPDATE_CACHE_32(re, gb);
494 274338 run = 4;
495 274338 level = 2;
496
497 274338 max_coeffs = 64 << log2_block_count;
498 274338 block_mask = blocks_per_slice - 1;
499
500 274338 for (pos = block_mask;;) {
501 85051918 bits_left = gb->size_in_bits - re_index;
502
6/6
✓ Branch 0 taken 84997929 times.
✓ Branch 1 taken 53989 times.
✓ Branch 2 taken 853260 times.
✓ Branch 3 taken 84144669 times.
✓ Branch 5 taken 632911 times.
✓ Branch 6 taken 220349 times.
85051918 if (bits_left <= 0 || (bits_left < 32 && !SHOW_UBITS(re, gb, bits_left)))
503 break;
504
505
5/6
✓ Branch 0 taken 8109361 times.
✓ Branch 1 taken 76668219 times.
✗ Branch 2 not taken.
✓ Branch 3 taken 8109361 times.
✓ Branch 5 taken 2491176 times.
✓ Branch 6 taken 74177043 times.
84777580 DECODE_CODEWORD(run, run_to_cb[FFMIN(run, 15)], LAST_SKIP_BITS);
506 84777580 pos += run + 1;
507
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 84777580 times.
84777580 if (pos >= max_coeffs) {
508 av_log(avctx, AV_LOG_ERROR, "ac tex damaged %d, %d\n", pos, max_coeffs);
509 return AVERROR_INVALIDDATA;
510 }
511
512
5/6
✓ Branch 0 taken 45737211 times.
✓ Branch 1 taken 39040369 times.
✗ Branch 2 not taken.
✓ Branch 3 taken 45737211 times.
✓ Branch 5 taken 9491662 times.
✓ Branch 6 taken 29548707 times.
84777580 DECODE_CODEWORD(level, lev_to_cb[FFMIN(level, 9)], SKIP_BITS);
513 84777580 level += 1;
514
515 84777580 i = pos >> log2_block_count;
516
517 84777580 sign = SHOW_SBITS(re, gb, 1);
518 84777580 SKIP_BITS(re, gb, 1);
519 84777580 out[((pos & block_mask) << 6) + ctx->scan[i]] = ((level ^ sign) - sign);
520 }
521
522 274338 CLOSE_READER(re, gb);
523 274338 return 0;
524 }
525
526 91950 static int decode_slice_luma(AVCodecContext *avctx, SliceContext *slice,
527 uint16_t *dst, int dst_stride,
528 const uint8_t *buf, unsigned buf_size,
529 const int16_t *qmat)
530 {
531 91950 const ProresContext *ctx = avctx->priv_data;
532 91950 LOCAL_ALIGNED_32(int16_t, blocks, [8*4*64]);
533 int16_t *block;
534 GetBitContext gb;
535 91950 int i, blocks_per_slice = slice->mb_count<<2;
536 int ret;
537
538
2/2
✓ Branch 0 taken 2339124 times.
✓ Branch 1 taken 91950 times.
2431074 for (i = 0; i < blocks_per_slice; i++)
539 2339124 ctx->bdsp.clear_block(blocks+(i<<6));
540
541 91950 init_get_bits(&gb, buf, buf_size << 3);
542
543
1/2
✗ Branch 1 not taken.
✓ Branch 2 taken 91950 times.
91950 if ((ret = decode_dc_coeffs(&gb, blocks, blocks_per_slice)) < 0)
544 return ret;
545
1/2
✗ Branch 1 not taken.
✓ Branch 2 taken 91950 times.
91950 if ((ret = decode_ac_coeffs(avctx, &gb, blocks, blocks_per_slice)) < 0)
546 return ret;
547
548 91950 block = blocks;
549
2/2
✓ Branch 0 taken 584781 times.
✓ Branch 1 taken 91950 times.
676731 for (i = 0; i < slice->mb_count; i++) {
550 584781 ctx->prodsp.idct_put(dst, dst_stride, block+(0<<6), qmat);
551 584781 ctx->prodsp.idct_put(dst +8, dst_stride, block+(1<<6), qmat);
552 584781 ctx->prodsp.idct_put(dst+4*dst_stride , dst_stride, block+(2<<6), qmat);
553 584781 ctx->prodsp.idct_put(dst+4*dst_stride+8, dst_stride, block+(3<<6), qmat);
554 584781 block += 4*64;
555 584781 dst += 16;
556 }
557 91950 return 0;
558 }
559
560 183900 static int decode_slice_chroma(AVCodecContext *avctx, SliceContext *slice,
561 uint16_t *dst, int dst_stride,
562 const uint8_t *buf, unsigned buf_size,
563 const int16_t *qmat, int log2_blocks_per_mb)
564 {
565 183900 ProresContext *ctx = avctx->priv_data;
566 183900 LOCAL_ALIGNED_32(int16_t, blocks, [8*4*64]);
567 int16_t *block;
568 GetBitContext gb;
569 183900 int i, j, blocks_per_slice = slice->mb_count << log2_blocks_per_mb;
570 int ret;
571
572
2/2
✓ Branch 0 taken 3154512 times.
✓ Branch 1 taken 183900 times.
3338412 for (i = 0; i < blocks_per_slice; i++)
573 3154512 ctx->bdsp.clear_block(blocks+(i<<6));
574
575 /* Some encodes have empty chroma scans to simulate grayscale */
576
2/2
✓ Branch 0 taken 182388 times.
✓ Branch 1 taken 1512 times.
183900 if (buf_size) {
577 182388 init_get_bits(&gb, buf, buf_size << 3);
578
579
1/2
✗ Branch 1 not taken.
✓ Branch 2 taken 182388 times.
182388 if ((ret = decode_dc_coeffs(&gb, blocks, blocks_per_slice)) < 0)
580 return ret;
581
1/2
✗ Branch 1 not taken.
✓ Branch 2 taken 182388 times.
182388 if ((ret = decode_ac_coeffs(avctx, &gb, blocks, blocks_per_slice)) < 0)
582 return ret;
583 }
584
585 183900 block = blocks;
586
2/2
✓ Branch 0 taken 1169562 times.
✓ Branch 1 taken 183900 times.
1353462 for (i = 0; i < slice->mb_count; i++) {
587
2/2
✓ Branch 0 taken 1577256 times.
✓ Branch 1 taken 1169562 times.
2746818 for (j = 0; j < log2_blocks_per_mb; j++) {
588 1577256 ctx->prodsp.idct_put(dst, dst_stride, block+(0<<6), qmat);
589 1577256 ctx->prodsp.idct_put(dst+4*dst_stride, dst_stride, block+(1<<6), qmat);
590 1577256 block += 2*64;
591 1577256 dst += 8;
592 }
593 }
594 183900 return 0;
595 }
596
597 /**
598 * Decode alpha slice plane.
599 */
600 5100 static void decode_slice_alpha(const ProresContext *ctx,
601 uint16_t *dst, int dst_stride,
602 const uint8_t *buf, int buf_size,
603 int blocks_per_slice)
604 {
605 GetBitContext gb;
606 int i;
607 5100 LOCAL_ALIGNED_32(int16_t, blocks, [8*4*64]);
608 int16_t *block;
609
610
2/2
✓ Branch 0 taken 163200 times.
✓ Branch 1 taken 5100 times.
168300 for (i = 0; i < blocks_per_slice<<2; i++)
611 163200 ctx->bdsp.clear_block(blocks+(i<<6));
612
613 5100 init_get_bits(&gb, buf, buf_size << 3);
614
615
1/2
✓ Branch 0 taken 5100 times.
✗ Branch 1 not taken.
5100 if (ctx->alpha_info == 2) {
616 5100 ctx->unpack_alpha(&gb, blocks, blocks_per_slice * 4 * 64, 16);
617 } else {
618 ctx->unpack_alpha(&gb, blocks, blocks_per_slice * 4 * 64, 8);
619 }
620
621 5100 block = blocks;
622
623
2/2
✓ Branch 0 taken 81600 times.
✓ Branch 1 taken 5100 times.
86700 for (i = 0; i < 16; i++) {
624 81600 memcpy(dst, block, 16 * blocks_per_slice * sizeof(*dst));
625 81600 dst += dst_stride >> 1;
626 81600 block += 16 * blocks_per_slice;
627 }
628 5100 }
629
630 91950 static int decode_slice_thread(AVCodecContext *avctx, void *arg, int jobnr, int threadnr)
631 {
632 91950 const ProresContext *ctx = avctx->priv_data;
633 91950 SliceContext *slice = &ctx->slices[jobnr];
634 91950 const uint8_t *buf = slice->data;
635 91950 AVFrame *pic = ctx->frame;
636 int i, hdr_size, qscale, log2_chroma_blocks_per_mb;
637 int luma_stride, chroma_stride;
638 int y_data_size, u_data_size, v_data_size, a_data_size, offset;
639 uint8_t *dest_y, *dest_u, *dest_v;
640 91950 LOCAL_ALIGNED_16(int16_t, qmat_luma_scaled, [64]);
641 91950 LOCAL_ALIGNED_16(int16_t, qmat_chroma_scaled,[64]);
642 int mb_x_shift;
643 int ret;
644
645 91950 slice->ret = -1;
646 //av_log(avctx, AV_LOG_INFO, "slice %d mb width %d mb x %d y %d\n",
647 // jobnr, slice->mb_count, slice->mb_x, slice->mb_y);
648
649 // slice header
650 91950 hdr_size = buf[0] >> 3;
651 91950 qscale = av_clip(buf[1], 1, 224);
652
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 91950 times.
91950 qscale = qscale > 128 ? qscale - 96 << 2: qscale;
653 91950 y_data_size = AV_RB16(buf + 2);
654 91950 u_data_size = AV_RB16(buf + 4);
655 91950 v_data_size = slice->data_size - y_data_size - u_data_size - hdr_size;
656
2/2
✓ Branch 0 taken 10200 times.
✓ Branch 1 taken 81750 times.
91950 if (hdr_size > 7) v_data_size = AV_RB16(buf + 6);
657 91950 a_data_size = slice->data_size - y_data_size - u_data_size -
658 91950 v_data_size - hdr_size;
659
660
3/6
✓ Branch 0 taken 91950 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 91950 times.
✗ Branch 3 not taken.
✓ Branch 4 taken 91950 times.
✗ Branch 5 not taken.
91950 if (y_data_size < 0 || u_data_size < 0 || v_data_size < 0
661
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 91950 times.
91950 || hdr_size+y_data_size+u_data_size+v_data_size > slice->data_size){
662 av_log(avctx, AV_LOG_ERROR, "invalid plane data size\n");
663 return AVERROR_INVALIDDATA;
664 }
665
666 91950 buf += hdr_size;
667
668
2/2
✓ Branch 0 taken 5884800 times.
✓ Branch 1 taken 91950 times.
5976750 for (i = 0; i < 64; i++) {
669 5884800 qmat_luma_scaled [i] = ctx->qmat_luma [i] * qscale;
670 5884800 qmat_chroma_scaled[i] = ctx->qmat_chroma[i] * qscale;
671 }
672
673
2/2
✓ Branch 0 taken 44622 times.
✓ Branch 1 taken 47328 times.
91950 if (ctx->frame_type == 0) {
674 44622 luma_stride = pic->linesize[0];
675 44622 chroma_stride = pic->linesize[1];
676 } else {
677 47328 luma_stride = pic->linesize[0] << 1;
678 47328 chroma_stride = pic->linesize[1] << 1;
679 }
680
681
2/4
✓ Branch 0 taken 91950 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 91950 times.
✗ Branch 3 not taken.
91950 if (avctx->pix_fmt == AV_PIX_FMT_YUV444P10 || avctx->pix_fmt == AV_PIX_FMT_YUVA444P10 ||
682
4/4
✓ Branch 0 taken 64104 times.
✓ Branch 1 taken 27846 times.
✓ Branch 2 taken 5100 times.
✓ Branch 3 taken 59004 times.
91950 avctx->pix_fmt == AV_PIX_FMT_YUV444P12 || avctx->pix_fmt == AV_PIX_FMT_YUVA444P12) {
683 32946 mb_x_shift = 5;
684 32946 log2_chroma_blocks_per_mb = 2;
685 } else {
686 59004 mb_x_shift = 4;
687 59004 log2_chroma_blocks_per_mb = 1;
688 }
689
690 91950 offset = (slice->mb_y << 4) * luma_stride + (slice->mb_x << 5);
691 91950 dest_y = pic->data[0] + offset;
692 91950 dest_u = pic->data[1] + (slice->mb_y << 4) * chroma_stride + (slice->mb_x << mb_x_shift);
693 91950 dest_v = pic->data[2] + (slice->mb_y << 4) * chroma_stride + (slice->mb_x << mb_x_shift);
694
695
4/4
✓ Branch 0 taken 47328 times.
✓ Branch 1 taken 44622 times.
✓ Branch 2 taken 23664 times.
✓ Branch 3 taken 23664 times.
91950 if (ctx->frame_type && ctx->first_field ^ !!(ctx->frame->flags & AV_FRAME_FLAG_TOP_FIELD_FIRST)) {
696 23664 dest_y += pic->linesize[0];
697 23664 dest_u += pic->linesize[1];
698 23664 dest_v += pic->linesize[2];
699 23664 offset += pic->linesize[3];
700 }
701
702 91950 ret = decode_slice_luma(avctx, slice, (uint16_t*)dest_y, luma_stride,
703 buf, y_data_size, qmat_luma_scaled);
704
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 91950 times.
91950 if (ret < 0)
705 return ret;
706
707
1/2
✓ Branch 0 taken 91950 times.
✗ Branch 1 not taken.
91950 if (!(avctx->flags & AV_CODEC_FLAG_GRAY)) {
708 91950 ret = decode_slice_chroma(avctx, slice, (uint16_t*)dest_u, chroma_stride,
709 buf + y_data_size, u_data_size,
710 qmat_chroma_scaled, log2_chroma_blocks_per_mb);
711
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 91950 times.
91950 if (ret < 0)
712 return ret;
713
714 91950 ret = decode_slice_chroma(avctx, slice, (uint16_t*)dest_v, chroma_stride,
715 91950 buf + y_data_size + u_data_size, v_data_size,
716 qmat_chroma_scaled, log2_chroma_blocks_per_mb);
717
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 91950 times.
91950 if (ret < 0)
718 return ret;
719 }
720
721 /* decode alpha plane if available */
722
4/6
✓ Branch 0 taken 5100 times.
✓ Branch 1 taken 86850 times.
✓ Branch 2 taken 5100 times.
✗ Branch 3 not taken.
✓ Branch 4 taken 5100 times.
✗ Branch 5 not taken.
91950 if (ctx->alpha_info && pic->data[3] && a_data_size) {
723 5100 uint8_t *dest_a = pic->data[3] + offset;
724 5100 decode_slice_alpha(ctx, (uint16_t*)dest_a, luma_stride,
725 5100 buf + y_data_size + u_data_size + v_data_size,
726 5100 a_data_size, slice->mb_count);
727 }
728
729 91950 slice->ret = 0;
730 91950 return 0;
731 }
732
733 1497 static int decode_picture(AVCodecContext *avctx)
734 {
735 1497 ProresContext *ctx = avctx->priv_data;
736 int i;
737 1497 int error = 0;
738
739 1497 avctx->execute2(avctx, decode_slice_thread, NULL, NULL, ctx->slice_count);
740
741
2/2
✓ Branch 0 taken 91950 times.
✓ Branch 1 taken 1497 times.
93447 for (i = 0; i < ctx->slice_count; i++)
742 91950 error += ctx->slices[i].ret < 0;
743
744
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 1497 times.
1497 if (error)
745 ctx->frame->decode_error_flags = FF_DECODE_ERROR_INVALID_BITSTREAM;
746
1/2
✓ Branch 0 taken 1497 times.
✗ Branch 1 not taken.
1497 if (error < ctx->slice_count)
747 1497 return 0;
748
749 return ctx->slices[0].ret;
750 }
751
752 1065 static int decode_frame(AVCodecContext *avctx, AVFrame *frame,
753 int *got_frame, AVPacket *avpkt)
754 {
755 1065 ProresContext *ctx = avctx->priv_data;
756 1065 const uint8_t *buf = avpkt->data;
757 1065 int buf_size = avpkt->size;
758 int frame_hdr_size, pic_size, ret;
759
760
2/4
✓ Branch 0 taken 1065 times.
✗ Branch 1 not taken.
✗ Branch 2 not taken.
✓ Branch 3 taken 1065 times.
1065 if (buf_size < 28 || AV_RL32(buf + 4) != AV_RL32("icpf")) {
761 av_log(avctx, AV_LOG_ERROR, "invalid frame header\n");
762 return AVERROR_INVALIDDATA;
763 }
764
765 1065 ctx->frame = frame;
766 1065 ctx->first_field = 1;
767
768 1065 buf += 8;
769 1065 buf_size -= 8;
770
771 1065 frame_hdr_size = decode_frame_header(ctx, buf, buf_size, avctx);
772
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 1065 times.
1065 if (frame_hdr_size < 0)
773 return frame_hdr_size;
774
775 1065 buf += frame_hdr_size;
776 1065 buf_size -= frame_hdr_size;
777
778
1/2
✗ Branch 1 not taken.
✓ Branch 2 taken 1065 times.
1065 if ((ret = ff_thread_get_buffer(avctx, frame, 0)) < 0)
779 return ret;
780 1065 ff_thread_finish_setup(avctx);
781
782 if (HWACCEL_MAX && avctx->hwaccel) {
783 const FFHWAccel *hwaccel = ffhwaccel(avctx->hwaccel);
784 ret = hwaccel->start_frame(avctx, avpkt->buf, avpkt->data, avpkt->size);
785 if (ret < 0)
786 return ret;
787 ret = hwaccel->decode_slice(avctx, avpkt->data, avpkt->size);
788 if (ret < 0)
789 return ret;
790 ret = hwaccel->end_frame(avctx);
791 if (ret < 0)
792 return ret;
793 goto finish;
794 }
795
796 1065 decode_picture:
797 1497 pic_size = decode_picture_header(avctx, buf, buf_size);
798
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 1497 times.
1497 if (pic_size < 0) {
799 av_log(avctx, AV_LOG_ERROR, "error decoding picture header\n");
800 return pic_size;
801 }
802
803
1/2
✗ Branch 1 not taken.
✓ Branch 2 taken 1497 times.
1497 if ((ret = decode_picture(avctx)) < 0) {
804 av_log(avctx, AV_LOG_ERROR, "error decoding picture\n");
805 return ret;
806 }
807
808 1497 buf += pic_size;
809 1497 buf_size -= pic_size;
810
811
6/6
✓ Branch 0 taken 864 times.
✓ Branch 1 taken 633 times.
✓ Branch 2 taken 408 times.
✓ Branch 3 taken 456 times.
✓ Branch 4 taken 24 times.
✓ Branch 5 taken 432 times.
1497 if (ctx->frame_type && buf_size > 0 && ctx->first_field) {
812 432 ctx->first_field = 0;
813 432 goto decode_picture;
814 }
815
816 1065 finish:
817 1065 *got_frame = 1;
818
819 1065 return avpkt->size;
820 }
821
822 76 static av_cold int decode_close(AVCodecContext *avctx)
823 {
824 76 ProresContext *ctx = avctx->priv_data;
825
826 76 av_freep(&ctx->slices);
827
828 76 return 0;
829 }
830
831 #if HAVE_THREADS
832 static int update_thread_context(AVCodecContext *dst, const AVCodecContext *src)
833 {
834 ProresContext *csrc = src->priv_data;
835 ProresContext *cdst = dst->priv_data;
836
837 cdst->pix_fmt = csrc->pix_fmt;
838
839 return 0;
840 }
841 #endif
842
843 const FFCodec ff_prores_decoder = {
844 .p.name = "prores",
845 CODEC_LONG_NAME("Apple ProRes (iCodec Pro)"),
846 .p.type = AVMEDIA_TYPE_VIDEO,
847 .p.id = AV_CODEC_ID_PRORES,
848 .priv_data_size = sizeof(ProresContext),
849 .init = decode_init,
850 .close = decode_close,
851 FF_CODEC_DECODE_CB(decode_frame),
852 UPDATE_THREAD_CONTEXT(update_thread_context),
853 .p.capabilities = AV_CODEC_CAP_DR1 | AV_CODEC_CAP_SLICE_THREADS | AV_CODEC_CAP_FRAME_THREADS,
854 .p.profiles = NULL_IF_CONFIG_SMALL(ff_prores_profiles),
855 #if HWACCEL_MAX
856 .hw_configs = (const AVCodecHWConfigInternal *const []) {
857 #if CONFIG_PRORES_VIDEOTOOLBOX_HWACCEL
858 HWACCEL_VIDEOTOOLBOX(prores),
859 #endif
860 NULL
861 },
862 #endif
863 };
864