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