/src/ffmpeg/libavcodec/proresdec.c
Line | Count | Source (jump to first uncovered line) |
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 | 10.5M | #define ALPHA_SHIFT_16_TO_10(alpha_val) (alpha_val >> 6) |
48 | 3.81M | #define ALPHA_SHIFT_8_TO_10(alpha_val) ((alpha_val << 2) | (alpha_val >> 6)) |
49 | 128k | #define ALPHA_SHIFT_16_TO_12(alpha_val) (alpha_val >> 4) |
50 | 499k | #define ALPHA_SHIFT_8_TO_12(alpha_val) ((alpha_val << 4) | (alpha_val >> 4)) |
51 | | |
52 | | static void inline unpack_alpha(GetBitContext *gb, uint16_t *dst, int num_coeffs, |
53 | 43.7k | const int num_bits, const int decode_precision) { |
54 | 43.7k | const int mask = (1 << num_bits) - 1; |
55 | 43.7k | int i, idx, val, alpha_val; |
56 | | |
57 | 43.7k | idx = 0; |
58 | 43.7k | alpha_val = mask; |
59 | 1.08M | do { |
60 | 1.11M | do { |
61 | 1.11M | if (get_bits1(gb)) { |
62 | 378k | val = get_bits(gb, num_bits); |
63 | 736k | } else { |
64 | 736k | int sign; |
65 | 736k | val = get_bits(gb, num_bits == 16 ? 7 : 4); |
66 | 736k | sign = val & 1; |
67 | 736k | val = (val + 2) >> 1; |
68 | 736k | if (sign) |
69 | 158k | val = -val; |
70 | 736k | } |
71 | 1.11M | alpha_val = (alpha_val + val) & mask; |
72 | 1.11M | if (num_bits == 16) { |
73 | 650k | if (decode_precision == 10) { |
74 | 643k | dst[idx++] = ALPHA_SHIFT_16_TO_10(alpha_val); |
75 | 643k | } else { /* 12b */ |
76 | 7.31k | dst[idx++] = ALPHA_SHIFT_16_TO_12(alpha_val); |
77 | 7.31k | } |
78 | 650k | } else { |
79 | 464k | if (decode_precision == 10) { |
80 | 429k | dst[idx++] = ALPHA_SHIFT_8_TO_10(alpha_val); |
81 | 429k | } else { /* 12b */ |
82 | 34.8k | dst[idx++] = ALPHA_SHIFT_8_TO_12(alpha_val); |
83 | 34.8k | } |
84 | 464k | } |
85 | 1.11M | if (idx >= num_coeffs) |
86 | 1.62k | break; |
87 | 1.11M | } while (get_bits_left(gb)>0 && get_bits1(gb)); |
88 | 0 | val = get_bits(gb, 4); |
89 | 1.08M | if (!val) |
90 | 594k | val = get_bits(gb, 11); |
91 | 1.08M | if (idx + val > num_coeffs) |
92 | 4.08k | val = num_coeffs - idx; |
93 | 1.08M | if (num_bits == 16) { |
94 | 10.7M | for (i = 0; i < val; i++) { |
95 | 10.0M | if (decode_precision == 10) { |
96 | 9.95M | dst[idx++] = ALPHA_SHIFT_16_TO_10(alpha_val); |
97 | 9.95M | } else { /* 12b */ |
98 | 120k | dst[idx++] = ALPHA_SHIFT_16_TO_12(alpha_val); |
99 | 120k | } |
100 | 10.0M | } |
101 | 648k | } else { |
102 | 4.28M | for (i = 0; i < val; i++) { |
103 | 3.84M | if (decode_precision == 10) { |
104 | 3.38M | dst[idx++] = ALPHA_SHIFT_8_TO_10(alpha_val); |
105 | 3.38M | } else { /* 12b */ |
106 | 464k | dst[idx++] = ALPHA_SHIFT_8_TO_12(alpha_val); |
107 | 464k | } |
108 | 3.84M | } |
109 | 439k | } |
110 | 1.08M | } while (idx < num_coeffs); |
111 | 43.7k | } |
112 | | |
113 | | static void unpack_alpha_10(GetBitContext *gb, uint16_t *dst, int num_coeffs, |
114 | | const int num_bits) |
115 | 42.9k | { |
116 | 42.9k | if (num_bits == 16) { |
117 | 40.1k | unpack_alpha(gb, dst, num_coeffs, 16, 10); |
118 | 40.1k | } else { /* 8 bits alpha */ |
119 | 2.76k | unpack_alpha(gb, dst, num_coeffs, 8, 10); |
120 | 2.76k | } |
121 | 42.9k | } |
122 | | |
123 | | static void unpack_alpha_12(GetBitContext *gb, uint16_t *dst, int num_coeffs, |
124 | | const int num_bits) |
125 | 813 | { |
126 | 813 | if (num_bits == 16) { |
127 | 410 | unpack_alpha(gb, dst, num_coeffs, 16, 12); |
128 | 410 | } else { /* 8 bits alpha */ |
129 | 403 | unpack_alpha(gb, dst, num_coeffs, 8, 12); |
130 | 403 | } |
131 | 813 | } |
132 | | |
133 | | static av_cold int decode_init(AVCodecContext *avctx) |
134 | 2.76k | { |
135 | 2.76k | ProresContext *ctx = avctx->priv_data; |
136 | | |
137 | 2.76k | avctx->bits_per_raw_sample = 10; |
138 | | |
139 | 2.76k | switch (avctx->codec_tag) { |
140 | 1 | case MKTAG('a','p','c','o'): |
141 | 1 | avctx->profile = AV_PROFILE_PRORES_PROXY; |
142 | 1 | break; |
143 | 1 | case MKTAG('a','p','c','s'): |
144 | 1 | avctx->profile = AV_PROFILE_PRORES_LT; |
145 | 1 | break; |
146 | 1 | case MKTAG('a','p','c','n'): |
147 | 1 | avctx->profile = AV_PROFILE_PRORES_STANDARD; |
148 | 1 | break; |
149 | 1 | case MKTAG('a','p','c','h'): |
150 | 1 | avctx->profile = AV_PROFILE_PRORES_HQ; |
151 | 1 | break; |
152 | 190 | case MKTAG('a','p','4','h'): |
153 | 190 | avctx->profile = AV_PROFILE_PRORES_4444; |
154 | 190 | avctx->bits_per_raw_sample = 12; |
155 | 190 | break; |
156 | 1 | case MKTAG('a','p','4','x'): |
157 | 1 | avctx->profile = AV_PROFILE_PRORES_XQ; |
158 | 1 | avctx->bits_per_raw_sample = 12; |
159 | 1 | break; |
160 | 2.56k | default: |
161 | 2.56k | avctx->profile = AV_PROFILE_UNKNOWN; |
162 | 2.56k | av_log(avctx, AV_LOG_WARNING, "Unknown prores profile %d\n", avctx->codec_tag); |
163 | 2.76k | } |
164 | | |
165 | 2.76k | ctx->unpack_alpha = avctx->bits_per_raw_sample == 10 ? |
166 | 2.57k | unpack_alpha_10 : unpack_alpha_12; |
167 | | |
168 | 2.76k | av_log(avctx, AV_LOG_DEBUG, |
169 | 2.76k | "Auto bitdepth precision. Use %db decoding based on codec tag.\n", |
170 | 2.76k | avctx->bits_per_raw_sample); |
171 | | |
172 | 2.76k | ff_blockdsp_init(&ctx->bdsp); |
173 | 2.76k | ff_proresdsp_init(&ctx->prodsp, avctx->bits_per_raw_sample); |
174 | | |
175 | 2.76k | ff_permute_scantable(ctx->progressive_scan, ff_prores_progressive_scan, |
176 | 2.76k | ctx->prodsp.idct_permutation); |
177 | 2.76k | ff_permute_scantable(ctx->interlaced_scan, ff_prores_interlaced_scan, |
178 | 2.76k | ctx->prodsp.idct_permutation); |
179 | | |
180 | 2.76k | ctx->pix_fmt = AV_PIX_FMT_NONE; |
181 | | |
182 | 2.76k | return 0; |
183 | 2.76k | } |
184 | | |
185 | | static int decode_frame_header(ProresContext *ctx, const uint8_t *buf, |
186 | | const int data_size, AVCodecContext *avctx) |
187 | 47.1k | { |
188 | 47.1k | int hdr_size, width, height, flags; |
189 | 47.1k | int version; |
190 | 47.1k | const uint8_t *ptr; |
191 | 47.1k | enum AVPixelFormat pix_fmt; |
192 | | |
193 | 47.1k | hdr_size = AV_RB16(buf); |
194 | 47.1k | ff_dlog(avctx, "header size %d\n", hdr_size); |
195 | 47.1k | if (hdr_size > data_size) { |
196 | 451 | av_log(avctx, AV_LOG_ERROR, "error, wrong header size\n"); |
197 | 451 | return AVERROR_INVALIDDATA; |
198 | 451 | } |
199 | | |
200 | 46.6k | version = AV_RB16(buf + 2); |
201 | 46.6k | ff_dlog(avctx, "%.4s version %d\n", buf+4, version); |
202 | 46.6k | if (version > 1) { |
203 | 423 | av_log(avctx, AV_LOG_ERROR, "unsupported version: %d\n", version); |
204 | 423 | return AVERROR_PATCHWELCOME; |
205 | 423 | } |
206 | | |
207 | 46.2k | width = AV_RB16(buf + 8); |
208 | 46.2k | height = AV_RB16(buf + 10); |
209 | | |
210 | 46.2k | if (width != avctx->width || height != avctx->height) { |
211 | 7.58k | int ret; |
212 | | |
213 | 7.58k | av_log(avctx, AV_LOG_WARNING, "picture resolution change: %dx%d -> %dx%d\n", |
214 | 7.58k | avctx->width, avctx->height, width, height); |
215 | 7.58k | if ((ret = ff_set_dimensions(avctx, width, height)) < 0) |
216 | 1.66k | return ret; |
217 | 7.58k | } |
218 | | |
219 | 44.5k | ctx->frame_type = (buf[12] >> 2) & 3; |
220 | 44.5k | ctx->alpha_info = buf[17] & 0xf; |
221 | | |
222 | 44.5k | if (ctx->alpha_info > 2) { |
223 | 3.50k | av_log(avctx, AV_LOG_ERROR, "Invalid alpha mode %d\n", ctx->alpha_info); |
224 | 3.50k | return AVERROR_INVALIDDATA; |
225 | 3.50k | } |
226 | 41.0k | if (avctx->skip_alpha) ctx->alpha_info = 0; |
227 | | |
228 | 41.0k | ff_dlog(avctx, "frame type %d\n", ctx->frame_type); |
229 | | |
230 | 41.0k | if (ctx->frame_type == 0) { |
231 | 6.12k | ctx->scan = ctx->progressive_scan; // permuted |
232 | 34.9k | } else { |
233 | 34.9k | ctx->scan = ctx->interlaced_scan; // permuted |
234 | 34.9k | ctx->frame->flags |= AV_FRAME_FLAG_INTERLACED; |
235 | 34.9k | if (ctx->frame_type == 1) |
236 | 24.5k | ctx->frame->flags |= AV_FRAME_FLAG_TOP_FIELD_FIRST; |
237 | 34.9k | } |
238 | | |
239 | 41.0k | if (ctx->alpha_info) { |
240 | 34.1k | if (avctx->bits_per_raw_sample == 10) { |
241 | 33.1k | pix_fmt = (buf[12] & 0xC0) == 0xC0 ? AV_PIX_FMT_YUVA444P10 : AV_PIX_FMT_YUVA422P10; |
242 | 33.1k | } else { /* 12b */ |
243 | 1.01k | pix_fmt = (buf[12] & 0xC0) == 0xC0 ? AV_PIX_FMT_YUVA444P12 : AV_PIX_FMT_YUVA422P12; |
244 | 1.01k | } |
245 | 34.1k | } else { |
246 | 6.94k | if (avctx->bits_per_raw_sample == 10) { |
247 | 6.23k | pix_fmt = (buf[12] & 0xC0) == 0xC0 ? AV_PIX_FMT_YUV444P10 : AV_PIX_FMT_YUV422P10; |
248 | 6.23k | } else { /* 12b */ |
249 | 708 | pix_fmt = (buf[12] & 0xC0) == 0xC0 ? AV_PIX_FMT_YUV444P12 : AV_PIX_FMT_YUV422P12; |
250 | 708 | } |
251 | 6.94k | } |
252 | | |
253 | 41.0k | if (pix_fmt != ctx->pix_fmt) { |
254 | 78.0k | #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 | 3.85k | avctx->pix_fmt = ctx->pix_fmt = pix_fmt; |
273 | 3.85k | #endif |
274 | 3.85k | } |
275 | | |
276 | 41.0k | ctx->frame->color_primaries = buf[14]; |
277 | 41.0k | ctx->frame->color_trc = buf[15]; |
278 | 41.0k | ctx->frame->colorspace = buf[16]; |
279 | 41.0k | ctx->frame->color_range = AVCOL_RANGE_MPEG; |
280 | | |
281 | 41.0k | ptr = buf + 20; |
282 | 41.0k | flags = buf[19]; |
283 | 41.0k | ff_dlog(avctx, "flags %x\n", flags); |
284 | | |
285 | 41.0k | if (flags & 2) { |
286 | 4.43k | if(buf + data_size - ptr < 64) { |
287 | 614 | av_log(avctx, AV_LOG_ERROR, "Header truncated\n"); |
288 | 614 | return AVERROR_INVALIDDATA; |
289 | 614 | } |
290 | 3.82k | ff_permute_scantable(ctx->qmat_luma, ctx->prodsp.idct_permutation, ptr); |
291 | 3.82k | ptr += 64; |
292 | 36.6k | } else { |
293 | 36.6k | memset(ctx->qmat_luma, 4, 64); |
294 | 36.6k | } |
295 | | |
296 | 40.4k | if (flags & 1) { |
297 | 4.82k | if(buf + data_size - ptr < 64) { |
298 | 631 | av_log(avctx, AV_LOG_ERROR, "Header truncated\n"); |
299 | 631 | return AVERROR_INVALIDDATA; |
300 | 631 | } |
301 | 4.19k | ff_permute_scantable(ctx->qmat_chroma, ctx->prodsp.idct_permutation, ptr); |
302 | 35.6k | } else { |
303 | 35.6k | memcpy(ctx->qmat_chroma, ctx->qmat_luma, 64); |
304 | 35.6k | } |
305 | | |
306 | 39.8k | return hdr_size; |
307 | 40.4k | } |
308 | | |
309 | | static int decode_picture_header(AVCodecContext *avctx, const uint8_t *buf, const int buf_size) |
310 | 62.6k | { |
311 | 62.6k | ProresContext *ctx = avctx->priv_data; |
312 | 62.6k | int i, hdr_size, slice_count; |
313 | 62.6k | unsigned pic_data_size; |
314 | 62.6k | int log2_slice_mb_width, log2_slice_mb_height; |
315 | 62.6k | int slice_mb_count, mb_x, mb_y; |
316 | 62.6k | const uint8_t *data_ptr, *index_ptr; |
317 | | |
318 | 62.6k | hdr_size = buf[0] >> 3; |
319 | 62.6k | if (hdr_size < 8 || hdr_size > buf_size) { |
320 | 5.74k | av_log(avctx, AV_LOG_ERROR, "error, wrong picture header size\n"); |
321 | 5.74k | return AVERROR_INVALIDDATA; |
322 | 5.74k | } |
323 | | |
324 | 56.8k | pic_data_size = AV_RB32(buf + 1); |
325 | 56.8k | if (pic_data_size > buf_size) { |
326 | 780 | av_log(avctx, AV_LOG_ERROR, "error, wrong picture data size\n"); |
327 | 780 | return AVERROR_INVALIDDATA; |
328 | 780 | } |
329 | | |
330 | 56.0k | log2_slice_mb_width = buf[7] >> 4; |
331 | 56.0k | log2_slice_mb_height = buf[7] & 0xF; |
332 | 56.0k | if (log2_slice_mb_width > 3 || log2_slice_mb_height) { |
333 | 1.08k | av_log(avctx, AV_LOG_ERROR, "unsupported slice resolution: %dx%d\n", |
334 | 1.08k | 1 << log2_slice_mb_width, 1 << log2_slice_mb_height); |
335 | 1.08k | return AVERROR_INVALIDDATA; |
336 | 1.08k | } |
337 | | |
338 | 54.9k | ctx->mb_width = (avctx->width + 15) >> 4; |
339 | 54.9k | if (ctx->frame_type) |
340 | 53.4k | ctx->mb_height = (avctx->height + 31) >> 5; |
341 | 1.54k | else |
342 | 1.54k | ctx->mb_height = (avctx->height + 15) >> 4; |
343 | | |
344 | | // QT ignores the written value |
345 | | // slice_count = AV_RB16(buf + 5); |
346 | 54.9k | slice_count = ctx->mb_height * ((ctx->mb_width >> log2_slice_mb_width) + |
347 | 54.9k | av_popcount(ctx->mb_width & (1 << log2_slice_mb_width) - 1)); |
348 | | |
349 | 54.9k | if (ctx->slice_count != slice_count || !ctx->slices) { |
350 | 2.56k | av_freep(&ctx->slices); |
351 | 2.56k | ctx->slice_count = 0; |
352 | 2.56k | ctx->slices = av_calloc(slice_count, sizeof(*ctx->slices)); |
353 | 2.56k | if (!ctx->slices) |
354 | 0 | return AVERROR(ENOMEM); |
355 | 2.56k | ctx->slice_count = slice_count; |
356 | 2.56k | } |
357 | | |
358 | 54.9k | if (!slice_count) |
359 | 0 | return AVERROR(EINVAL); |
360 | | |
361 | 54.9k | if (hdr_size + slice_count*2 > buf_size) { |
362 | 433 | av_log(avctx, AV_LOG_ERROR, "error, wrong slice count\n"); |
363 | 433 | return AVERROR_INVALIDDATA; |
364 | 433 | } |
365 | | |
366 | | // parse slice information |
367 | 54.5k | index_ptr = buf + hdr_size; |
368 | 54.5k | data_ptr = index_ptr + slice_count*2; |
369 | | |
370 | 54.5k | slice_mb_count = 1 << log2_slice_mb_width; |
371 | 54.5k | mb_x = 0; |
372 | 54.5k | mb_y = 0; |
373 | | |
374 | 116k | for (i = 0; i < slice_count; i++) { |
375 | 63.5k | SliceContext *slice = &ctx->slices[i]; |
376 | | |
377 | 63.5k | slice->data = data_ptr; |
378 | 63.5k | data_ptr += AV_RB16(index_ptr + i*2); |
379 | | |
380 | 75.9k | while (ctx->mb_width - mb_x < slice_mb_count) |
381 | 12.4k | slice_mb_count >>= 1; |
382 | | |
383 | 63.5k | slice->mb_x = mb_x; |
384 | 63.5k | slice->mb_y = mb_y; |
385 | 63.5k | slice->mb_count = slice_mb_count; |
386 | 63.5k | slice->data_size = data_ptr - slice->data; |
387 | | |
388 | 63.5k | if (slice->data_size < 6) { |
389 | 270 | av_log(avctx, AV_LOG_ERROR, "error, wrong slice data size\n"); |
390 | 270 | return AVERROR_INVALIDDATA; |
391 | 270 | } |
392 | | |
393 | 63.2k | mb_x += slice_mb_count; |
394 | 63.2k | if (mb_x == ctx->mb_width) { |
395 | 55.7k | slice_mb_count = 1 << log2_slice_mb_width; |
396 | 55.7k | mb_x = 0; |
397 | 55.7k | mb_y++; |
398 | 55.7k | } |
399 | 63.2k | if (data_ptr > buf + buf_size) { |
400 | 1.24k | av_log(avctx, AV_LOG_ERROR, "error, slice out of bounds\n"); |
401 | 1.24k | return AVERROR_INVALIDDATA; |
402 | 1.24k | } |
403 | 63.2k | } |
404 | | |
405 | 53.0k | if (mb_x || mb_y != ctx->mb_height) { |
406 | 0 | av_log(avctx, AV_LOG_ERROR, "error wrong mb count y %d h %d\n", |
407 | 0 | mb_y, ctx->mb_height); |
408 | 0 | return AVERROR_INVALIDDATA; |
409 | 0 | } |
410 | | |
411 | 53.0k | return pic_data_size; |
412 | 53.0k | } |
413 | | |
414 | | #define DECODE_CODEWORD(val, codebook, SKIP) \ |
415 | 2.57M | do { \ |
416 | 2.57M | unsigned int rice_order, exp_order, switch_bits; \ |
417 | 2.57M | unsigned int q, buf, bits; \ |
418 | 2.57M | \ |
419 | 2.57M | UPDATE_CACHE_32(re, gb); /* We really need 32 bits */ \ |
420 | 2.57M | buf = GET_CACHE(re, gb); \ |
421 | 2.57M | \ |
422 | 2.57M | /* number of bits to switch between rice and exp golomb */ \ |
423 | 5.01M | switch_bits = codebook & 3; \ |
424 | 5.01M | rice_order = codebook >> 5; \ |
425 | 5.01M | exp_order = (codebook >> 2) & 7; \ |
426 | 2.57M | \ |
427 | 2.57M | q = 31 - av_log2(buf); \ |
428 | 2.57M | \ |
429 | 2.57M | if (q > switch_bits) { /* exp golomb */ \ |
430 | 570k | bits = exp_order - switch_bits + (q<<1); \ |
431 | 570k | if (bits > 31) \ |
432 | 570k | return AVERROR_INVALIDDATA; \ |
433 | 570k | val = SHOW_UBITS(re, gb, bits) - (1 << exp_order) + \ |
434 | 566k | ((switch_bits + 1) << rice_order); \ |
435 | 566k | SKIP(re, gb, bits); \ |
436 | 2.00M | } else if (rice_order) { \ |
437 | 164k | SKIP_BITS(re, gb, q+1); \ |
438 | 164k | val = (q << rice_order) + SHOW_UBITS(re, gb, rice_order); \ |
439 | 164k | SKIP(re, gb, rice_order); \ |
440 | 1.83M | } else { \ |
441 | 1.83M | val = q; \ |
442 | 1.83M | SKIP(re, gb, q+1); \ |
443 | 1.83M | } \ |
444 | 2.57M | } while (0) |
445 | | |
446 | 64.5k | #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 | | static av_always_inline int decode_dc_coeffs(GetBitContext *gb, int16_t *out, |
453 | | int blocks_per_slice) |
454 | 65.8k | { |
455 | 65.8k | int16_t prev_dc; |
456 | 65.8k | int code, i, sign; |
457 | | |
458 | 65.8k | OPEN_READER(re, gb); |
459 | | |
460 | 65.8k | DECODE_CODEWORD(code, FIRST_DC_CB, LAST_SKIP_BITS); |
461 | 64.5k | prev_dc = TOSIGNED(code); |
462 | 64.5k | out[0] = prev_dc; |
463 | | |
464 | 64.5k | out += 64; // dc coeff for the next block |
465 | | |
466 | 64.5k | code = 5; |
467 | 64.5k | sign = 0; |
468 | 434k | for (i = 1; i < blocks_per_slice; i++, out += 64) { |
469 | 371k | DECODE_CODEWORD(code, dc_codebook[FFMIN(code, 6U)], LAST_SKIP_BITS); |
470 | 369k | if(code) sign ^= -(code & 1); |
471 | 57.8k | else sign = 0; |
472 | 369k | prev_dc += (((code + 1) >> 1) ^ sign) - sign; |
473 | 369k | out[0] = prev_dc; |
474 | 369k | } |
475 | 63.3k | CLOSE_READER(re, gb); |
476 | 63.3k | return 0; |
477 | 64.5k | } |
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 | | static av_always_inline int decode_ac_coeffs(AVCodecContext *avctx, GetBitContext *gb, |
484 | | int16_t *out, int blocks_per_slice) |
485 | 63.3k | { |
486 | 63.3k | const ProresContext *ctx = avctx->priv_data; |
487 | 63.3k | int block_mask, sign; |
488 | 63.3k | unsigned pos, run, level; |
489 | 63.3k | int max_coeffs, i, bits_left; |
490 | 63.3k | int log2_block_count = av_log2(blocks_per_slice); |
491 | | |
492 | 63.3k | OPEN_READER(re, gb); |
493 | 63.3k | UPDATE_CACHE_32(re, gb); |
494 | 63.3k | run = 4; |
495 | 63.3k | level = 2; |
496 | | |
497 | 63.3k | max_coeffs = 64 << log2_block_count; |
498 | 63.3k | block_mask = blocks_per_slice - 1; |
499 | | |
500 | 1.12M | for (pos = block_mask;;) { |
501 | 1.12M | bits_left = gb->size_in_bits - re_index; |
502 | 1.12M | if (bits_left <= 0 || (bits_left < 32 && !SHOW_UBITS(re, gb, bits_left))) |
503 | 60.7k | break; |
504 | | |
505 | 1.06M | DECODE_CODEWORD(run, run_to_cb[FFMIN(run, 15)], LAST_SKIP_BITS); |
506 | 1.06M | pos += run + 1; |
507 | 1.06M | if (pos >= max_coeffs) { |
508 | 1.26k | av_log(avctx, AV_LOG_ERROR, "ac tex damaged %d, %d\n", pos, max_coeffs); |
509 | 1.26k | return AVERROR_INVALIDDATA; |
510 | 1.26k | } |
511 | | |
512 | 1.06M | DECODE_CODEWORD(level, lev_to_cb[FFMIN(level, 9)], SKIP_BITS); |
513 | 1.06M | level += 1; |
514 | | |
515 | 1.06M | i = pos >> log2_block_count; |
516 | | |
517 | 1.06M | sign = SHOW_SBITS(re, gb, 1); |
518 | 1.06M | SKIP_BITS(re, gb, 1); |
519 | 1.06M | out[((pos & block_mask) << 6) + ctx->scan[i]] = ((level ^ sign) - sign); |
520 | 1.06M | } |
521 | | |
522 | 60.7k | CLOSE_READER(re, gb); |
523 | 60.7k | return 0; |
524 | 63.3k | } |
525 | | |
526 | | 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 | 54.4k | { |
531 | 54.4k | const ProresContext *ctx = avctx->priv_data; |
532 | 54.4k | LOCAL_ALIGNED_32(int16_t, blocks, [8*4*64]); |
533 | 54.4k | int16_t *block; |
534 | 54.4k | GetBitContext gb; |
535 | 54.4k | int i, blocks_per_slice = slice->mb_count<<2; |
536 | 54.4k | int ret; |
537 | | |
538 | 376k | for (i = 0; i < blocks_per_slice; i++) |
539 | 322k | ctx->bdsp.clear_block(blocks+(i<<6)); |
540 | | |
541 | 54.4k | init_get_bits(&gb, buf, buf_size << 3); |
542 | | |
543 | 54.4k | if ((ret = decode_dc_coeffs(&gb, blocks, blocks_per_slice)) < 0) |
544 | 1.68k | return ret; |
545 | 52.7k | if ((ret = decode_ac_coeffs(avctx, &gb, blocks, blocks_per_slice)) < 0) |
546 | 1.13k | return ret; |
547 | | |
548 | 51.5k | block = blocks; |
549 | 126k | for (i = 0; i < slice->mb_count; i++) { |
550 | 74.8k | ctx->prodsp.idct_put(dst, dst_stride, block+(0<<6), qmat); |
551 | 74.8k | ctx->prodsp.idct_put(dst +8, dst_stride, block+(1<<6), qmat); |
552 | 74.8k | ctx->prodsp.idct_put(dst+4*dst_stride , dst_stride, block+(2<<6), qmat); |
553 | 74.8k | ctx->prodsp.idct_put(dst+4*dst_stride+8, dst_stride, block+(3<<6), qmat); |
554 | 74.8k | block += 4*64; |
555 | 74.8k | dst += 16; |
556 | 74.8k | } |
557 | 51.5k | return 0; |
558 | 52.7k | } |
559 | | |
560 | | 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 | 102k | { |
565 | 102k | ProresContext *ctx = avctx->priv_data; |
566 | 102k | LOCAL_ALIGNED_32(int16_t, blocks, [8*4*64]); |
567 | 102k | int16_t *block; |
568 | 102k | GetBitContext gb; |
569 | 102k | int i, j, blocks_per_slice = slice->mb_count << log2_blocks_per_mb; |
570 | 102k | int ret; |
571 | | |
572 | 516k | for (i = 0; i < blocks_per_slice; i++) |
573 | 414k | ctx->bdsp.clear_block(blocks+(i<<6)); |
574 | | |
575 | | /* Some encodes have empty chroma scans to simulate grayscale */ |
576 | 102k | if (buf_size) { |
577 | 11.4k | init_get_bits(&gb, buf, buf_size << 3); |
578 | | |
579 | 11.4k | if ((ret = decode_dc_coeffs(&gb, blocks, blocks_per_slice)) < 0) |
580 | 836 | return ret; |
581 | 10.6k | if ((ret = decode_ac_coeffs(avctx, &gb, blocks, blocks_per_slice)) < 0) |
582 | 1.51k | return ret; |
583 | 10.6k | } |
584 | | |
585 | 99.9k | block = blocks; |
586 | 241k | for (i = 0; i < slice->mb_count; i++) { |
587 | 336k | for (j = 0; j < log2_blocks_per_mb; j++) { |
588 | 194k | ctx->prodsp.idct_put(dst, dst_stride, block+(0<<6), qmat); |
589 | 194k | ctx->prodsp.idct_put(dst+4*dst_stride, dst_stride, block+(1<<6), qmat); |
590 | 194k | block += 2*64; |
591 | 194k | dst += 8; |
592 | 194k | } |
593 | 141k | } |
594 | 99.9k | return 0; |
595 | 102k | } |
596 | | |
597 | | /** |
598 | | * Decode alpha slice plane. |
599 | | */ |
600 | | 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 | 43.7k | { |
605 | 43.7k | GetBitContext gb; |
606 | 43.7k | int i; |
607 | 43.7k | LOCAL_ALIGNED_32(int16_t, blocks, [8*4*64]); |
608 | 43.7k | int16_t *block; |
609 | | |
610 | 278k | for (i = 0; i < blocks_per_slice<<2; i++) |
611 | 234k | ctx->bdsp.clear_block(blocks+(i<<6)); |
612 | | |
613 | 43.7k | init_get_bits(&gb, buf, buf_size << 3); |
614 | | |
615 | 43.7k | if (ctx->alpha_info == 2) { |
616 | 40.5k | ctx->unpack_alpha(&gb, blocks, blocks_per_slice * 4 * 64, 16); |
617 | 40.5k | } else { |
618 | 3.17k | ctx->unpack_alpha(&gb, blocks, blocks_per_slice * 4 * 64, 8); |
619 | 3.17k | } |
620 | | |
621 | 43.7k | block = blocks; |
622 | | |
623 | 743k | for (i = 0; i < 16; i++) { |
624 | 699k | memcpy(dst, block, 16 * blocks_per_slice * sizeof(*dst)); |
625 | 699k | dst += dst_stride >> 1; |
626 | 699k | block += 16 * blocks_per_slice; |
627 | 699k | } |
628 | 43.7k | } |
629 | | |
630 | | static int decode_slice_thread(AVCodecContext *avctx, void *arg, int jobnr, int threadnr) |
631 | 61.6k | { |
632 | 61.6k | const ProresContext *ctx = avctx->priv_data; |
633 | 61.6k | SliceContext *slice = &ctx->slices[jobnr]; |
634 | 61.6k | const uint8_t *buf = slice->data; |
635 | 61.6k | AVFrame *pic = ctx->frame; |
636 | 61.6k | int i, hdr_size, qscale, log2_chroma_blocks_per_mb; |
637 | 61.6k | int luma_stride, chroma_stride; |
638 | 61.6k | int y_data_size, u_data_size, v_data_size, a_data_size, offset; |
639 | 61.6k | uint8_t *dest_y, *dest_u, *dest_v; |
640 | 61.6k | LOCAL_ALIGNED_16(int16_t, qmat_luma_scaled, [64]); |
641 | 61.6k | LOCAL_ALIGNED_16(int16_t, qmat_chroma_scaled,[64]); |
642 | 61.6k | int mb_x_shift; |
643 | 61.6k | int ret; |
644 | | |
645 | 61.6k | 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 | 61.6k | hdr_size = buf[0] >> 3; |
651 | 61.6k | qscale = av_clip(buf[1], 1, 224); |
652 | 61.6k | qscale = qscale > 128 ? qscale - 96 << 2: qscale; |
653 | 61.6k | y_data_size = AV_RB16(buf + 2); |
654 | 61.6k | u_data_size = AV_RB16(buf + 4); |
655 | 61.6k | v_data_size = slice->data_size - y_data_size - u_data_size - hdr_size; |
656 | 61.6k | if (hdr_size > 7) v_data_size = AV_RB16(buf + 6); |
657 | 61.6k | a_data_size = slice->data_size - y_data_size - u_data_size - |
658 | 61.6k | v_data_size - hdr_size; |
659 | | |
660 | 61.6k | if (y_data_size < 0 || u_data_size < 0 || v_data_size < 0 |
661 | 61.6k | || hdr_size+y_data_size+u_data_size+v_data_size > slice->data_size){ |
662 | 7.29k | av_log(avctx, AV_LOG_ERROR, "invalid plane data size\n"); |
663 | 7.29k | return AVERROR_INVALIDDATA; |
664 | 7.29k | } |
665 | | |
666 | 54.4k | buf += hdr_size; |
667 | | |
668 | 3.53M | for (i = 0; i < 64; i++) { |
669 | 3.48M | qmat_luma_scaled [i] = ctx->qmat_luma [i] * qscale; |
670 | 3.48M | qmat_chroma_scaled[i] = ctx->qmat_chroma[i] * qscale; |
671 | 3.48M | } |
672 | | |
673 | 54.4k | if (ctx->frame_type == 0) { |
674 | 1.69k | luma_stride = pic->linesize[0]; |
675 | 1.69k | chroma_stride = pic->linesize[1]; |
676 | 52.7k | } else { |
677 | 52.7k | luma_stride = pic->linesize[0] << 1; |
678 | 52.7k | chroma_stride = pic->linesize[1] << 1; |
679 | 52.7k | } |
680 | | |
681 | 54.4k | if (avctx->pix_fmt == AV_PIX_FMT_YUV444P10 || avctx->pix_fmt == AV_PIX_FMT_YUVA444P10 || |
682 | 54.4k | avctx->pix_fmt == AV_PIX_FMT_YUV444P12 || avctx->pix_fmt == AV_PIX_FMT_YUVA444P12) { |
683 | 11.8k | mb_x_shift = 5; |
684 | 11.8k | log2_chroma_blocks_per_mb = 2; |
685 | 42.5k | } else { |
686 | 42.5k | mb_x_shift = 4; |
687 | 42.5k | log2_chroma_blocks_per_mb = 1; |
688 | 42.5k | } |
689 | | |
690 | 54.4k | offset = (slice->mb_y << 4) * luma_stride + (slice->mb_x << 5); |
691 | 54.4k | dest_y = pic->data[0] + offset; |
692 | 54.4k | dest_u = pic->data[1] + (slice->mb_y << 4) * chroma_stride + (slice->mb_x << mb_x_shift); |
693 | 54.4k | dest_v = pic->data[2] + (slice->mb_y << 4) * chroma_stride + (slice->mb_x << mb_x_shift); |
694 | | |
695 | 54.4k | if (ctx->frame_type && ctx->first_field ^ !!(ctx->frame->flags & AV_FRAME_FLAG_TOP_FIELD_FIRST)) { |
696 | 27.1k | dest_y += pic->linesize[0]; |
697 | 27.1k | dest_u += pic->linesize[1]; |
698 | 27.1k | dest_v += pic->linesize[2]; |
699 | 27.1k | offset += pic->linesize[3]; |
700 | 27.1k | } |
701 | | |
702 | 54.4k | ret = decode_slice_luma(avctx, slice, (uint16_t*)dest_y, luma_stride, |
703 | 54.4k | buf, y_data_size, qmat_luma_scaled); |
704 | 54.4k | if (ret < 0) |
705 | 2.82k | return ret; |
706 | | |
707 | 51.5k | if (!(avctx->flags & AV_CODEC_FLAG_GRAY)) { |
708 | 51.5k | ret = decode_slice_chroma(avctx, slice, (uint16_t*)dest_u, chroma_stride, |
709 | 51.5k | buf + y_data_size, u_data_size, |
710 | 51.5k | qmat_chroma_scaled, log2_chroma_blocks_per_mb); |
711 | 51.5k | if (ret < 0) |
712 | 851 | return ret; |
713 | | |
714 | 50.7k | ret = decode_slice_chroma(avctx, slice, (uint16_t*)dest_v, chroma_stride, |
715 | 50.7k | buf + y_data_size + u_data_size, v_data_size, |
716 | 50.7k | qmat_chroma_scaled, log2_chroma_blocks_per_mb); |
717 | 50.7k | if (ret < 0) |
718 | 1.49k | return ret; |
719 | 50.7k | } |
720 | | |
721 | | /* decode alpha plane if available */ |
722 | 49.2k | if (ctx->alpha_info && pic->data[3] && a_data_size) { |
723 | 43.7k | uint8_t *dest_a = pic->data[3] + offset; |
724 | 43.7k | decode_slice_alpha(ctx, (uint16_t*)dest_a, luma_stride, |
725 | 43.7k | buf + y_data_size + u_data_size + v_data_size, |
726 | 43.7k | a_data_size, slice->mb_count); |
727 | 43.7k | } |
728 | | |
729 | 49.2k | slice->ret = 0; |
730 | 49.2k | return 0; |
731 | 51.5k | } |
732 | | |
733 | | static int decode_picture(AVCodecContext *avctx) |
734 | 53.0k | { |
735 | 53.0k | ProresContext *ctx = avctx->priv_data; |
736 | 53.0k | int i; |
737 | 53.0k | int error = 0; |
738 | | |
739 | 53.0k | avctx->execute2(avctx, decode_slice_thread, NULL, NULL, ctx->slice_count); |
740 | | |
741 | 114k | for (i = 0; i < ctx->slice_count; i++) |
742 | 61.6k | error += ctx->slices[i].ret < 0; |
743 | | |
744 | 53.0k | if (error) |
745 | 9.17k | ctx->frame->decode_error_flags = FF_DECODE_ERROR_INVALID_BITSTREAM; |
746 | 53.0k | if (error < ctx->slice_count) |
747 | 48.0k | return 0; |
748 | | |
749 | 4.98k | return ctx->slices[0].ret; |
750 | 53.0k | } |
751 | | |
752 | | static int decode_frame(AVCodecContext *avctx, AVFrame *frame, |
753 | | int *got_frame, AVPacket *avpkt) |
754 | 153k | { |
755 | 153k | ProresContext *ctx = avctx->priv_data; |
756 | 153k | const uint8_t *buf = avpkt->data; |
757 | 153k | int buf_size = avpkt->size; |
758 | 153k | int frame_hdr_size, pic_size, ret; |
759 | | |
760 | 153k | if (buf_size < 28 || AV_RL32(buf + 4) != AV_RL32("icpf")) { |
761 | 106k | av_log(avctx, AV_LOG_ERROR, "invalid frame header\n"); |
762 | 106k | return AVERROR_INVALIDDATA; |
763 | 106k | } |
764 | | |
765 | 47.1k | ctx->frame = frame; |
766 | 47.1k | ctx->first_field = 1; |
767 | | |
768 | 47.1k | buf += 8; |
769 | 47.1k | buf_size -= 8; |
770 | | |
771 | 47.1k | frame_hdr_size = decode_frame_header(ctx, buf, buf_size, avctx); |
772 | 47.1k | if (frame_hdr_size < 0) |
773 | 7.28k | return frame_hdr_size; |
774 | | |
775 | 39.8k | buf += frame_hdr_size; |
776 | 39.8k | buf_size -= frame_hdr_size; |
777 | | |
778 | 39.8k | if ((ret = ff_thread_get_buffer(avctx, frame, 0)) < 0) |
779 | 771 | return ret; |
780 | 39.0k | ff_thread_finish_setup(avctx); |
781 | | |
782 | 39.0k | if (HWACCEL_MAX && avctx->hwaccel) { |
783 | 0 | const FFHWAccel *hwaccel = ffhwaccel(avctx->hwaccel); |
784 | 0 | ret = hwaccel->start_frame(avctx, avpkt->buf, avpkt->data, avpkt->size); |
785 | 0 | if (ret < 0) |
786 | 0 | return ret; |
787 | 0 | ret = hwaccel->decode_slice(avctx, avpkt->data, avpkt->size); |
788 | 0 | if (ret < 0) |
789 | 0 | return ret; |
790 | 0 | ret = hwaccel->end_frame(avctx); |
791 | 0 | if (ret < 0) |
792 | 0 | return ret; |
793 | 0 | goto finish; |
794 | 0 | } |
795 | | |
796 | 62.6k | decode_picture: |
797 | 62.6k | pic_size = decode_picture_header(avctx, buf, buf_size); |
798 | 62.6k | if (pic_size < 0) { |
799 | 9.55k | av_log(avctx, AV_LOG_ERROR, "error decoding picture header\n"); |
800 | 9.55k | return pic_size; |
801 | 9.55k | } |
802 | | |
803 | 53.0k | if ((ret = decode_picture(avctx)) < 0) { |
804 | 4.98k | av_log(avctx, AV_LOG_ERROR, "error decoding picture\n"); |
805 | 4.98k | return ret; |
806 | 4.98k | } |
807 | | |
808 | 48.0k | buf += pic_size; |
809 | 48.0k | buf_size -= pic_size; |
810 | | |
811 | 48.0k | if (ctx->frame_type && buf_size > 0 && ctx->first_field) { |
812 | 23.5k | ctx->first_field = 0; |
813 | 23.5k | goto decode_picture; |
814 | 23.5k | } |
815 | | |
816 | 24.5k | finish: |
817 | 24.5k | *got_frame = 1; |
818 | | |
819 | 24.5k | return avpkt->size; |
820 | 48.0k | } |
821 | | |
822 | | static av_cold int decode_close(AVCodecContext *avctx) |
823 | 2.76k | { |
824 | 2.76k | ProresContext *ctx = avctx->priv_data; |
825 | | |
826 | 2.76k | av_freep(&ctx->slices); |
827 | | |
828 | 2.76k | return 0; |
829 | 2.76k | } |
830 | | |
831 | | #if HAVE_THREADS |
832 | | static int update_thread_context(AVCodecContext *dst, const AVCodecContext *src) |
833 | 0 | { |
834 | 0 | ProresContext *csrc = src->priv_data; |
835 | 0 | ProresContext *cdst = dst->priv_data; |
836 | |
|
837 | 0 | cdst->pix_fmt = csrc->pix_fmt; |
838 | |
|
839 | 0 | return 0; |
840 | 0 | } |
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 | | }; |