FFmpeg
Loading...
Searching...
No Matches
proresdec.c
Go to the documentation of this file.
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"
34
35#include "avcodec.h"
36#include "codec_internal.h"
37#include "decode.h"
38#define CACHED_BITSTREAM_READER 1
39#include "get_bits.h"
40#include "hwaccel_internal.h"
41#include "hwconfig.h"
42#include "idctdsp.h"
43#include "profiles.h"
44#include "proresdec.h"
45#include "proresdata.h"
46#include "thread.h"
47
48#define ALPHA_SHIFT_16_TO_10(alpha_val) (alpha_val >> 6)
49#define ALPHA_SHIFT_8_TO_10(alpha_val) ((alpha_val << 2) | (alpha_val >> 6))
50#define ALPHA_SHIFT_16_TO_12(alpha_val) (alpha_val >> 4)
51#define ALPHA_SHIFT_8_TO_12(alpha_val) ((alpha_val << 4) | (alpha_val >> 4))
52
53static void inline unpack_alpha(GetBitContext *gb, uint16_t *dst, int num_coeffs,
54 const int num_bits, const int decode_precision) {
55 const int mask = (1 << num_bits) - 1;
56 int i, idx, val, alpha_val;
57
58 idx = 0;
59 alpha_val = mask;
60 do {
61 do {
62 if (get_bits1(gb)) {
63 val = get_bits(gb, num_bits);
64 } else {
65 int sign;
66 val = get_bits(gb, num_bits == 16 ? 7 : 4);
67 sign = val & 1;
68 val = (val + 2) >> 1;
69 if (sign)
70 val = -val;
71 }
72 alpha_val = (alpha_val + val) & mask;
73 if (num_bits == 16) {
74 if (decode_precision == 10) {
75 dst[idx++] = ALPHA_SHIFT_16_TO_10(alpha_val);
76 } else { /* 12b */
77 dst[idx++] = ALPHA_SHIFT_16_TO_12(alpha_val);
78 }
79 } else {
80 if (decode_precision == 10) {
81 dst[idx++] = ALPHA_SHIFT_8_TO_10(alpha_val);
82 } else { /* 12b */
83 dst[idx++] = ALPHA_SHIFT_8_TO_12(alpha_val);
84 }
85 }
86 if (idx >= num_coeffs)
87 break;
88 } while (get_bits_left(gb)>0 && get_bits1(gb));
89 val = get_bits(gb, 4);
90 if (!val)
91 val = get_bits(gb, 11);
92 if (idx + val > num_coeffs)
93 val = num_coeffs - idx;
94 if (num_bits == 16) {
95 for (i = 0; i < val; i++) {
96 if (decode_precision == 10) {
97 dst[idx++] = ALPHA_SHIFT_16_TO_10(alpha_val);
98 } else { /* 12b */
99 dst[idx++] = ALPHA_SHIFT_16_TO_12(alpha_val);
100 }
101 }
102 } else {
103 for (i = 0; i < val; i++) {
104 if (decode_precision == 10) {
105 dst[idx++] = ALPHA_SHIFT_8_TO_10(alpha_val);
106 } else { /* 12b */
107 dst[idx++] = ALPHA_SHIFT_8_TO_12(alpha_val);
108 }
109 }
110 }
111 } while (idx < num_coeffs);
112}
113
114static void unpack_alpha_10(GetBitContext *gb, uint16_t *dst, int num_coeffs,
115 const int num_bits)
116{
117 if (num_bits == 16) {
118 unpack_alpha(gb, dst, num_coeffs, 16, 10);
119 } else { /* 8 bits alpha */
120 unpack_alpha(gb, dst, num_coeffs, 8, 10);
121 }
122}
123
124static void unpack_alpha_12(GetBitContext *gb, uint16_t *dst, int num_coeffs,
125 const int num_bits)
126{
127 if (num_bits == 16) {
128 unpack_alpha(gb, dst, num_coeffs, 16, 12);
129 } else { /* 8 bits alpha */
130 unpack_alpha(gb, dst, num_coeffs, 8, 12);
131 }
132}
133
135{
136 ProresContext *ctx = avctx->priv_data;
137
138 avctx->bits_per_raw_sample = 10;
139
140 switch (avctx->codec_tag) {
141 case MKTAG('a','p','c','o'):
143 break;
144 case MKTAG('a','p','c','s'):
146 break;
147 case MKTAG('a','p','c','n'):
149 break;
150 case MKTAG('a','p','c','h'):
152 break;
153 case MKTAG('a','p','4','h'):
155 avctx->bits_per_raw_sample = 12;
156 break;
157 case MKTAG('a','p','4','x'):
159 avctx->bits_per_raw_sample = 12;
160 break;
161 default:
163 av_log(avctx, AV_LOG_WARNING, "Unknown prores profile %d\n", avctx->codec_tag);
164 }
165
166 ctx->unpack_alpha = avctx->bits_per_raw_sample == 10 ?
168
169 av_log(avctx, AV_LOG_DEBUG,
170 "Auto bitdepth precision. Use %db decoding based on codec tag.\n",
171 avctx->bits_per_raw_sample);
172
173 ff_blockdsp_init(&ctx->bdsp);
174 ff_proresdsp_init(&ctx->prodsp, avctx->bits_per_raw_sample);
175
177 ctx->prodsp.idct_permutation);
179 ctx->prodsp.idct_permutation);
180
181 ctx->pix_fmt = AV_PIX_FMT_NONE;
182
183 return 0;
184}
185
186static int decode_frame_header(ProresContext *ctx, const uint8_t *buf,
187 const int data_size, AVCodecContext *avctx)
188{
189 int hdr_size, width, height, flags, dimensions_changed = 0;
190 int version;
191 const uint8_t *ptr;
193 int old_frame_type = ctx->frame_type;
194
195 hdr_size = AV_RB16(buf);
196 ff_dlog(avctx, "header size %d\n", hdr_size);
197 if (hdr_size > data_size) {
198 av_log(avctx, AV_LOG_ERROR, "error, wrong header size\n");
199 return AVERROR_INVALIDDATA;
200 }
201
202 version = AV_RB16(buf + 2);
203 ff_dlog(avctx, "%.4s version %d\n", buf+4, version);
204 if (version > 1) {
205 av_log(avctx, AV_LOG_ERROR, "unsupported version: %d\n", version);
207 }
208
209 width = AV_RB16(buf + 8);
210 height = AV_RB16(buf + 10);
211
212 if (width != avctx->width || height != avctx->height) {
213 int ret;
214
215 av_log(avctx, AV_LOG_WARNING, "picture resolution change: %dx%d -> %dx%d\n",
216 avctx->width, avctx->height, width, height);
217 if ((ret = ff_set_dimensions(avctx, width, height)) < 0)
218 return ret;
219 dimensions_changed = 1;
220 }
221
222 ctx->frame_type = (buf[12] >> 2) & 3;
223 ctx->alpha_info = buf[17] & 0xf;
224
225 if (ctx->alpha_info > 2) {
226 av_log(avctx, AV_LOG_ERROR, "Invalid alpha mode %d\n", ctx->alpha_info);
227 return AVERROR_INVALIDDATA;
228 }
229 if (avctx->skip_alpha) ctx->alpha_info = 0;
230
231 ff_dlog(avctx, "frame type %d\n", ctx->frame_type);
232
233 if (ctx->frame_type == 0) {
234 ctx->scan = ctx->progressive_scan; // permuted
235 } else {
236 ctx->scan = ctx->interlaced_scan; // permuted
237 ctx->frame->flags |= AV_FRAME_FLAG_INTERLACED;
238 if (ctx->frame_type == 1)
239 ctx->frame->flags |= AV_FRAME_FLAG_TOP_FIELD_FIRST;
240 }
241
242 if (ctx->alpha_info) {
243 if (avctx->bits_per_raw_sample == 10) {
244 pix_fmt = (buf[12] & 0xC0) == 0xC0 ? AV_PIX_FMT_YUVA444P10 : AV_PIX_FMT_YUVA422P10;
245 } else { /* 12b */
246 pix_fmt = (buf[12] & 0xC0) == 0xC0 ? AV_PIX_FMT_YUVA444P12 : AV_PIX_FMT_YUVA422P12;
247 }
248 } else {
249 if (avctx->bits_per_raw_sample == 10) {
250 pix_fmt = (buf[12] & 0xC0) == 0xC0 ? AV_PIX_FMT_YUV444P10 : AV_PIX_FMT_YUV422P10;
251 } else { /* 12b */
252 pix_fmt = (buf[12] & 0xC0) == 0xC0 ? AV_PIX_FMT_YUV444P12 : AV_PIX_FMT_YUV422P12;
253 }
254 }
255
256 if (pix_fmt != ctx->pix_fmt || dimensions_changed ||
257 ctx->frame_type != old_frame_type) {
258#define HWACCEL_MAX (CONFIG_PRORES_VIDEOTOOLBOX_HWACCEL + CONFIG_PRORES_VULKAN_HWACCEL)
259#if HWACCEL_MAX
260 enum AVPixelFormat pix_fmts[HWACCEL_MAX + 2], *fmtp = pix_fmts;
261 int ret;
262
263 ctx->pix_fmt = pix_fmt;
264
265#if CONFIG_PRORES_VIDEOTOOLBOX_HWACCEL
266 *fmtp++ = AV_PIX_FMT_VIDEOTOOLBOX;
267#endif
268#if CONFIG_PRORES_VULKAN_HWACCEL
269 *fmtp++ = AV_PIX_FMT_VULKAN;
270#endif
271 *fmtp++ = ctx->pix_fmt;
272 *fmtp = AV_PIX_FMT_NONE;
273
274 if ((ret = ff_get_format(avctx, pix_fmts)) < 0)
275 return ret;
276
277 avctx->pix_fmt = ret;
278#else
279 avctx->pix_fmt = ctx->pix_fmt = pix_fmt;
280#endif
281 }
282
283 avctx->color_primaries = buf[14];
284 avctx->color_trc = buf[15];
285 avctx->colorspace = buf[16];
287
288 ptr = buf + 20;
289 flags = buf[19];
290 ff_dlog(avctx, "flags %x\n", flags);
291
292 if (flags & 2) {
293 if(buf + data_size - ptr < 64) {
294 av_log(avctx, AV_LOG_ERROR, "Header truncated\n");
295 return AVERROR_INVALIDDATA;
296 }
297 ff_permute_scantable(ctx->qmat_luma, ctx->prodsp.idct_permutation, ptr);
298 ptr += 64;
299 } else {
300 memset(ctx->qmat_luma, 4, 64);
301 }
302
303 if (flags & 1) {
304 if(buf + data_size - ptr < 64) {
305 av_log(avctx, AV_LOG_ERROR, "Header truncated\n");
306 return AVERROR_INVALIDDATA;
307 }
308 ff_permute_scantable(ctx->qmat_chroma, ctx->prodsp.idct_permutation, ptr);
309 } else {
310 memcpy(ctx->qmat_chroma, ctx->qmat_luma, 64);
311 }
312
313 return hdr_size;
314}
315
316static int decode_picture_header(AVCodecContext *avctx, const uint8_t *buf, const int buf_size)
317{
318 ProresContext *ctx = avctx->priv_data;
319 int i, hdr_size, slice_count;
320 unsigned pic_data_size;
321 int log2_slice_mb_width, log2_slice_mb_height;
322 int slice_mb_count, mb_x, mb_y;
323 const uint8_t *data_ptr, *index_ptr;
324
325 hdr_size = buf[0] >> 3;
326 if (hdr_size < 8 || hdr_size > buf_size) {
327 av_log(avctx, AV_LOG_ERROR, "error, wrong picture header size\n");
328 return AVERROR_INVALIDDATA;
329 }
330
331 pic_data_size = AV_RB32(buf + 1);
332 if (pic_data_size > buf_size) {
333 av_log(avctx, AV_LOG_ERROR, "error, wrong picture data size\n");
334 return AVERROR_INVALIDDATA;
335 }
336
337 log2_slice_mb_width = buf[7] >> 4;
338 log2_slice_mb_height = buf[7] & 0xF;
339 if (log2_slice_mb_width > 3 || log2_slice_mb_height) {
340 av_log(avctx, AV_LOG_ERROR, "unsupported slice resolution: %dx%d\n",
341 1 << log2_slice_mb_width, 1 << log2_slice_mb_height);
342 return AVERROR_INVALIDDATA;
343 }
344
345 ctx->slice_mb_width = 1 << log2_slice_mb_width;
346 ctx->slice_mb_height = 1 << log2_slice_mb_height;
347
348 ctx->mb_width = (avctx->width + 15) >> 4;
349 if (ctx->frame_type)
350 ctx->mb_height = (avctx->height + 31) >> 5;
351 else
352 ctx->mb_height = (avctx->height + 15) >> 4;
353
354 // QT ignores the written value
355 // slice_count = AV_RB16(buf + 5);
356 slice_count = ctx->mb_height * ((ctx->mb_width >> log2_slice_mb_width) +
357 av_popcount(ctx->mb_width & ctx->slice_mb_width - 1));
358
359 if (ctx->slice_count != slice_count || !ctx->slices) {
360 av_freep(&ctx->slices);
361 ctx->slice_count = 0;
362 ctx->slices = av_calloc(slice_count, sizeof(*ctx->slices));
363 if (!ctx->slices)
364 return AVERROR(ENOMEM);
365 ctx->slice_count = slice_count;
366 }
367
368 if (!slice_count)
369 return AVERROR(EINVAL);
370
371 if (hdr_size + slice_count*2 > buf_size) {
372 av_log(avctx, AV_LOG_ERROR, "error, wrong slice count\n");
373 return AVERROR_INVALIDDATA;
374 }
375
376 // parse slice information
377 index_ptr = buf + hdr_size;
378 data_ptr = index_ptr + slice_count*2;
379
380 slice_mb_count = ctx->slice_mb_width;
381 mb_x = 0;
382 mb_y = 0;
383
384 for (i = 0; i < slice_count; i++) {
385 SliceContext *slice = &ctx->slices[i];
386
387 slice->data = data_ptr;
388 data_ptr += AV_RB16(index_ptr + i*2);
389
390 while (ctx->mb_width - mb_x < slice_mb_count)
391 slice_mb_count >>= 1;
392
393 slice->mb_x = mb_x;
394 slice->mb_y = mb_y;
395 slice->mb_count = slice_mb_count;
396 slice->data_size = data_ptr - slice->data;
397
398 if (slice->data_size < 6) {
399 av_log(avctx, AV_LOG_ERROR, "error, wrong slice data size\n");
400 return AVERROR_INVALIDDATA;
401 }
402
403 mb_x += slice_mb_count;
404 if (mb_x == ctx->mb_width) {
405 slice_mb_count = ctx->slice_mb_width;
406 mb_x = 0;
407 mb_y++;
408 }
409 if (data_ptr > buf + buf_size) {
410 av_log(avctx, AV_LOG_ERROR, "error, slice out of bounds\n");
411 return AVERROR_INVALIDDATA;
412 }
413 }
414
415 if (mb_x || mb_y != ctx->mb_height) {
416 av_log(avctx, AV_LOG_ERROR, "error wrong mb count y %d h %d\n",
417 mb_y, ctx->mb_height);
418 return AVERROR_INVALIDDATA;
419 }
420
421 return pic_data_size;
422}
423
424#define DECODE_CODEWORD(val, codebook, gb) \
425 do { \
426 unsigned int rice_order, exp_order, switch_bits; \
427 unsigned int q, buf, bits; \
428 \
429 buf = show_bits(gb, 32); /* We really need 32 bits */ \
430 \
431 /* number of bits to switch between rice and exp golomb */ \
432 switch_bits = codebook & 3; \
433 rice_order = codebook >> 5; \
434 exp_order = (codebook >> 2) & 7; \
435 \
436 q = 31 - av_log2(buf); \
437 \
438 if (q > switch_bits) { /* exp golomb */ \
439 bits = exp_order - switch_bits + (q<<1); \
440 if (bits > 31) \
441 return AVERROR_INVALIDDATA; \
442 val = (buf >> (32 - bits)) - (1 << exp_order) + \
443 ((switch_bits + 1) << rice_order); \
444 skip_bits(gb, bits); \
445 } else if (rice_order) { \
446 bits = q + 1 + rice_order; \
447 val = (q << rice_order) + \
448 ((buf >> (32 - bits)) & ((1u << rice_order) - 1)); \
449 skip_bits(gb, bits); \
450 } else { \
451 val = q; \
452 skip_bits(gb, q + 1); \
453 } \
454 } while (0)
455
456#define TOSIGNED(x) (((x) >> 1) ^ (-((x) & 1)))
457
458#define FIRST_DC_CB 0xB8
459
460static const uint8_t dc_codebook[7] = { 0x04, 0x28, 0x28, 0x4D, 0x4D, 0x70, 0x70};
461
463 int blocks_per_slice)
464{
465 int16_t prev_dc;
466 int code, i, sign;
467
469 prev_dc = TOSIGNED(code);
470 out[0] = prev_dc;
471
472 out += 64; // dc coeff for the next block
473
474 code = 5;
475 sign = 0;
476 for (i = 1; i < blocks_per_slice; i++, out += 64) {
478 if(code) sign ^= -(code & 1);
479 else sign = 0;
480 prev_dc += (((code + 1) >> 1) ^ sign) - sign;
481 out[0] = prev_dc;
482 }
483 return 0;
484}
485
486// adaptive codebook switching lut according to previous run/level values
487static const uint8_t run_to_cb[16] = { 0x06, 0x06, 0x05, 0x05, 0x04, 0x29, 0x29, 0x29, 0x29, 0x28, 0x28, 0x28, 0x28, 0x28, 0x28, 0x4C };
488static const uint8_t lev_to_cb[10] = { 0x04, 0x0A, 0x05, 0x06, 0x04, 0x28, 0x28, 0x28, 0x28, 0x4C };
489
491 int16_t *out, int blocks_per_slice)
492{
493 const ProresContext *ctx = avctx->priv_data;
494 int block_mask, sign;
495 unsigned pos, run, level;
496 int max_coeffs, i, left_bits;
497 int log2_block_count = av_log2(blocks_per_slice);
498
499 run = 4;
500 level = 2;
501
502 max_coeffs = 64 << log2_block_count;
503 block_mask = blocks_per_slice - 1;
504
505 for (pos = block_mask;;) {
506 left_bits = get_bits_left(gb);
507 if (left_bits <= 0 || (left_bits < 32 && !show_bits(gb, left_bits)))
508 break;
509
511 pos += run + 1;
512 if (pos >= max_coeffs) {
513 av_log(avctx, AV_LOG_ERROR, "ac tex damaged %d, %d\n", pos, max_coeffs);
514 return AVERROR_INVALIDDATA;
515 }
516
518 level += 1;
519
520 i = pos >> log2_block_count;
521
522 sign = -(int)get_bits1(gb);
523 out[((pos & block_mask) << 6) + ctx->scan[i]] = ((level ^ sign) - sign);
524 }
525
526 return 0;
527}
528
530 uint16_t *dst, int dst_stride,
531 const uint8_t *buf, unsigned buf_size,
532 const int16_t *qmat)
533{
534 const ProresContext *ctx = avctx->priv_data;
535 LOCAL_ALIGNED_32(int16_t, blocks, [8*4*64]);
536 int16_t *block;
537 GetBitContext gb;
538 int i, blocks_per_slice = slice->mb_count<<2;
539 int ret;
540
541 for (i = 0; i < blocks_per_slice; i++)
542 ctx->bdsp.clear_block(blocks+(i<<6));
543
544 init_get_bits(&gb, buf, buf_size << 3);
545
546 if ((ret = decode_dc_coeffs(&gb, blocks, blocks_per_slice)) < 0)
547 return ret;
548 if ((ret = decode_ac_coeffs(avctx, &gb, blocks, blocks_per_slice)) < 0)
549 return ret;
550
551 block = blocks;
552 for (i = 0; i < slice->mb_count; i++) {
553 ctx->prodsp.idct_put(dst, dst_stride, block+(0<<6), qmat);
554 ctx->prodsp.idct_put(dst +8, dst_stride, block+(1<<6), qmat);
555 ctx->prodsp.idct_put(dst+4*dst_stride , dst_stride, block+(2<<6), qmat);
556 ctx->prodsp.idct_put(dst+4*dst_stride+8, dst_stride, block+(3<<6), qmat);
557 block += 4*64;
558 dst += 16;
559 }
560 return 0;
561}
562
564 uint16_t *dst, int dst_stride,
565 const uint8_t *buf, unsigned buf_size,
566 const int16_t *qmat, int log2_blocks_per_mb)
567{
568 ProresContext *ctx = avctx->priv_data;
569 LOCAL_ALIGNED_32(int16_t, blocks, [8*4*64]);
570 int16_t *block;
571 GetBitContext gb;
572 int i, j, blocks_per_slice = slice->mb_count << log2_blocks_per_mb;
573 int ret;
574
575 for (i = 0; i < blocks_per_slice; i++)
576 ctx->bdsp.clear_block(blocks+(i<<6));
577
578 /* Some encodes have empty chroma scans to simulate grayscale */
579 if (buf_size) {
580 init_get_bits(&gb, buf, buf_size << 3);
581
582 if ((ret = decode_dc_coeffs(&gb, blocks, blocks_per_slice)) < 0)
583 return ret;
584 if ((ret = decode_ac_coeffs(avctx, &gb, blocks, blocks_per_slice)) < 0)
585 return ret;
586 }
587
588 block = blocks;
589 for (i = 0; i < slice->mb_count; i++) {
590 for (j = 0; j < log2_blocks_per_mb; j++) {
591 ctx->prodsp.idct_put(dst, dst_stride, block+(0<<6), qmat);
592 ctx->prodsp.idct_put(dst+4*dst_stride, dst_stride, block+(1<<6), qmat);
593 block += 2*64;
594 dst += 8;
595 }
596 }
597 return 0;
598}
599
600/**
601 * Decode alpha slice plane.
602 */
604 uint16_t *dst, int dst_stride,
605 const uint8_t *buf, int buf_size,
606 int blocks_per_slice)
607{
608 GetBitContext gb;
609 int i;
610 LOCAL_ALIGNED_32(int16_t, blocks, [8*4*64]);
611 int16_t *block;
612
613 for (i = 0; i < blocks_per_slice<<2; i++)
614 ctx->bdsp.clear_block(blocks+(i<<6));
615
616 init_get_bits(&gb, buf, buf_size << 3);
617
618 if (ctx->alpha_info == 2) {
619 ctx->unpack_alpha(&gb, blocks, blocks_per_slice * 4 * 64, 16);
620 } else {
621 ctx->unpack_alpha(&gb, blocks, blocks_per_slice * 4 * 64, 8);
622 }
623
624 block = blocks;
625
626 for (i = 0; i < 16; i++) {
627 memcpy(dst, block, 16 * blocks_per_slice * sizeof(*dst));
628 dst += dst_stride >> 1;
629 block += 16 * blocks_per_slice;
630 }
631}
632
633static int decode_slice_thread(AVCodecContext *avctx, void *arg, int jobnr, int threadnr)
634{
635 const ProresContext *ctx = avctx->priv_data;
636 SliceContext *slice = &ctx->slices[jobnr];
637 const uint8_t *buf = slice->data;
638 AVFrame *pic = ctx->frame;
639 int i, hdr_size, qscale, log2_chroma_blocks_per_mb;
640 int luma_stride, chroma_stride;
641 int y_data_size, u_data_size, v_data_size, a_data_size, offset;
642 uint8_t *dest_y, *dest_u, *dest_v;
643 LOCAL_ALIGNED_16(int16_t, qmat_luma_scaled, [64]);
644 LOCAL_ALIGNED_16(int16_t, qmat_chroma_scaled,[64]);
645 int mb_x_shift;
646 int ret;
647
648 slice->ret = -1;
649 //av_log(avctx, AV_LOG_INFO, "slice %d mb width %d mb x %d y %d\n",
650 // jobnr, slice->mb_count, slice->mb_x, slice->mb_y);
651
652 // slice header
653 hdr_size = buf[0] >> 3;
654 qscale = av_clip(buf[1], 1, 224);
655 qscale = qscale > 128 ? qscale - 96 << 2: qscale;
656 y_data_size = AV_RB16(buf + 2);
657 u_data_size = AV_RB16(buf + 4);
658 v_data_size = slice->data_size - y_data_size - u_data_size - hdr_size;
659 if (hdr_size > 7) v_data_size = AV_RB16(buf + 6);
660 a_data_size = slice->data_size - y_data_size - u_data_size -
661 v_data_size - hdr_size;
662
663 if (y_data_size < 0 || u_data_size < 0 || v_data_size < 0
664 || hdr_size+y_data_size+u_data_size+v_data_size > slice->data_size){
665 av_log(avctx, AV_LOG_ERROR, "invalid plane data size\n");
666 return AVERROR_INVALIDDATA;
667 }
668
669 buf += hdr_size;
670
671 for (i = 0; i < 64; i++) {
672 qmat_luma_scaled [i] = ctx->qmat_luma [i] * qscale;
673 qmat_chroma_scaled[i] = ctx->qmat_chroma[i] * qscale;
674 }
675
676 if (ctx->frame_type == 0) {
677 luma_stride = pic->linesize[0];
678 chroma_stride = pic->linesize[1];
679 } else {
680 luma_stride = pic->linesize[0] << 1;
681 chroma_stride = pic->linesize[1] << 1;
682 }
683
684 if (avctx->pix_fmt == AV_PIX_FMT_YUV444P10 || avctx->pix_fmt == AV_PIX_FMT_YUVA444P10 ||
686 mb_x_shift = 5;
687 log2_chroma_blocks_per_mb = 2;
688 } else {
689 mb_x_shift = 4;
690 log2_chroma_blocks_per_mb = 1;
691 }
692
693 offset = (slice->mb_y << 4) * luma_stride + (slice->mb_x << 5);
694 dest_y = pic->data[0] + offset;
695 dest_u = pic->data[1] + (slice->mb_y << 4) * chroma_stride + (slice->mb_x << mb_x_shift);
696 dest_v = pic->data[2] + (slice->mb_y << 4) * chroma_stride + (slice->mb_x << mb_x_shift);
697
698 if (ctx->frame_type && ctx->first_field ^ !!(ctx->frame->flags & AV_FRAME_FLAG_TOP_FIELD_FIRST)) {
699 dest_y += pic->linesize[0];
700 dest_u += pic->linesize[1];
701 dest_v += pic->linesize[2];
702 offset += pic->linesize[3];
703 }
704
705 ret = decode_slice_luma(avctx, slice, (uint16_t*)dest_y, luma_stride,
706 buf, y_data_size, qmat_luma_scaled);
707 if (ret < 0)
708 return ret;
709
710 if (!(avctx->flags & AV_CODEC_FLAG_GRAY)) {
711 ret = decode_slice_chroma(avctx, slice, (uint16_t*)dest_u, chroma_stride,
712 buf + y_data_size, u_data_size,
713 qmat_chroma_scaled, log2_chroma_blocks_per_mb);
714 if (ret < 0)
715 return ret;
716
717 ret = decode_slice_chroma(avctx, slice, (uint16_t*)dest_v, chroma_stride,
718 buf + y_data_size + u_data_size, v_data_size,
719 qmat_chroma_scaled, log2_chroma_blocks_per_mb);
720 if (ret < 0)
721 return ret;
722 }
723
724 /* decode alpha plane if available */
725 if (ctx->alpha_info && pic->data[3] && a_data_size) {
726 uint8_t *dest_a = pic->data[3] + offset;
727 decode_slice_alpha(ctx, (uint16_t*)dest_a, luma_stride,
728 buf + y_data_size + u_data_size + v_data_size,
729 a_data_size, slice->mb_count);
730 }
731
732 slice->ret = 0;
733 return 0;
734}
735
737{
738 ProresContext *ctx = avctx->priv_data;
739 int i;
740 int error = 0;
741
742 avctx->execute2(avctx, decode_slice_thread, NULL, NULL, ctx->slice_count);
743
744 for (i = 0; i < ctx->slice_count; i++)
745 error += ctx->slices[i].ret < 0;
746
747 if (error)
748 ctx->frame->decode_error_flags = FF_DECODE_ERROR_INVALID_BITSTREAM;
749 if (error < ctx->slice_count)
750 return 0;
751
752 return ctx->slices[0].ret;
753}
754
756 int *got_frame, AVPacket *avpkt)
757{
758 ProresContext *ctx = avctx->priv_data;
759 const uint8_t *buf = avpkt->data;
760 int buf_size = avpkt->size;
761 int frame_hdr_size, pic_size, ret;
762 int i;
763
764 if (buf_size < 28 || AV_RL32(buf + 4) != AV_RL32("icpf")) {
765 av_log(avctx, AV_LOG_ERROR, "invalid frame header\n");
766 return AVERROR_INVALIDDATA;
767 }
768
769 ctx->frame = frame;
770 ctx->first_field = 1;
771
772 buf += 8;
773 buf_size -= 8;
774
775 frame_hdr_size = decode_frame_header(ctx, buf, buf_size, avctx);
776 if (frame_hdr_size < 0)
777 return frame_hdr_size;
778
779 if (avctx->skip_frame == AVDISCARD_ALL)
780 return 0;
781
782 buf += frame_hdr_size;
783 buf_size -= frame_hdr_size;
784
785 if ((ret = ff_thread_get_buffer(avctx, frame, 0)) < 0)
786 return ret;
787
788 av_refstruct_unref(&ctx->hwaccel_picture_private);
789
790 if ((ret = ff_hwaccel_frame_priv_alloc(avctx, &ctx->hwaccel_picture_private)) < 0)
791 return ret;
792
794
796 pic_size = decode_picture_header(avctx, buf, buf_size);
797 if (pic_size < 0) {
798 av_log(avctx, AV_LOG_ERROR, "error decoding picture header\n");
799 return pic_size;
800 }
801
802 if (HWACCEL_MAX && avctx->hwaccel) {
803 const FFHWAccel *hwaccel = ffhwaccel(avctx->hwaccel);
804
805 ret = hwaccel->start_frame(avctx, avpkt->buf, avpkt->data, avpkt->size);
806 if (ret < 0)
807 return ret;
808
809 for (i = 0; i < ctx->slice_count; ++i) {
810 ret = hwaccel->decode_slice(avctx, ctx->slices[i].data, ctx->slices[i].data_size);
811 if (ret < 0)
812 return ret;
813 }
814
815 ret = hwaccel->end_frame(avctx);
816 if (ret < 0)
817 return ret;
818 } else if ((ret = decode_picture(avctx)) < 0) {
819 av_log(avctx, AV_LOG_ERROR, "error decoding picture\n");
820 return ret;
821 }
822
823 buf += pic_size;
824 buf_size -= pic_size;
825
826 if (ctx->frame_type && buf_size > 0 && ctx->first_field) {
827 ctx->first_field = 0;
828 goto decode_picture;
829 }
830
831 av_refstruct_unref(&ctx->hwaccel_picture_private);
832
833 *got_frame = 1;
834
835 return avpkt->size;
836}
837
839{
840 ProresContext *ctx = avctx->priv_data;
841
842 av_freep(&ctx->slices);
843 av_refstruct_unref(&ctx->hwaccel_picture_private);
844
845 return 0;
846}
847
848#if HAVE_THREADS
849static int update_thread_context(AVCodecContext *dst, const AVCodecContext *src)
850{
851 ProresContext *csrc = src->priv_data;
852 ProresContext *cdst = dst->priv_data;
853
854 cdst->pix_fmt = csrc->pix_fmt;
855 cdst->frame_type = csrc->frame_type;
856
857 return 0;
858}
859#endif
860
862 .p.name = "prores",
863 CODEC_LONG_NAME("Apple ProRes (iCodec Pro)"),
864 .p.type = AVMEDIA_TYPE_VIDEO,
865 .p.id = AV_CODEC_ID_PRORES,
866 .priv_data_size = sizeof(ProresContext),
867 .init = decode_init,
870 UPDATE_THREAD_CONTEXT(update_thread_context),
872 .caps_internal = FF_CODEC_CAP_SKIP_FRAME_FILL_PARAM,
874#if HWACCEL_MAX
875 .hw_configs = (const AVCodecHWConfigInternal *const []) {
876#if CONFIG_PRORES_VIDEOTOOLBOX_HWACCEL
877 HWACCEL_VIDEOTOOLBOX(prores),
878#endif
879#if CONFIG_PRORES_VULKAN_HWACCEL
880 HWACCEL_VULKAN(prores),
881#endif
882 NULL
883 },
884#endif
885};
uint8_t ptrdiff_t const uint8_t ptrdiff_t int intptr_t intptr_t int int16_t * dst
Definition dsp.h:87
static double val(void *priv, double ch)
Definition aeval.c:77
const FFCodec ff_prores_decoder
Definition proresdec.c:861
static FILE * out
static AVFormatContext * ctx
static av_cold void close(AVCodecParserContext *s)
Definition apv_parser.c:197
Libavcodec external API header.
#define flags(name, subs,...)
Definition cbs_h264.c:74
#define i(width, name, range_min, range_max)
Definition cbs_h264.c:63
#define UPDATE_THREAD_CONTEXT(func)
#define FF_CODEC_CAP_SKIP_FRAME_FILL_PARAM
The decoder extracts and fills its parameters even if the frame is skipped due to the skip_frame sett...
#define FF_CODEC_DECODE_CB(func)
#define CODEC_LONG_NAME(str)
#define av_popcount
Definition common.h:154
#define av_clip
Definition common.h:100
#define NULL
Definition coverity.c:32
static int16_t block[64]
Definition dct.c:125
int ff_get_format(AVCodecContext *avctx, const enum AVPixelFormat *fmt)
Select the (possibly hardware accelerated) pixel format.
Definition decode.c:1229
int ff_hwaccel_frame_priv_alloc(AVCodecContext *avctx, void **hwaccel_picture_private)
Allocate a hwaccel frame private data if the provided avctx uses a hwaccel method that needs it.
Definition decode.c:2336
int ff_set_dimensions(AVCodecContext *s, int width, int height)
Definition utils.c:91
#define AV_PROFILE_PRORES_STANDARD
Definition defs.h:183
#define AV_PROFILE_PRORES_4444
Definition defs.h:185
#define AV_PROFILE_PRORES_LT
Definition defs.h:182
#define AV_PROFILE_UNKNOWN
Definition defs.h:65
#define AV_PROFILE_PRORES_PROXY
Definition defs.h:181
#define AV_PROFILE_PRORES_XQ
Definition defs.h:186
#define AV_PROFILE_PRORES_HQ
Definition defs.h:184
static enum AVPixelFormat pix_fmt
static AVFrame * frame
int(* init)(AVBSFContext *ctx)
Definition dts2pts.c:608
static const char * hwaccel
Definition ffplay.c:357
#define FF_DECODE_ERROR_INVALID_BITSTREAM
Definition frame.h:760
bitstream reader API header.
static int get_bits_left(GetBitContext *gb)
Definition get_bits.h:688
static unsigned int get_bits1(GetBitContext *s)
Definition get_bits.h:391
static unsigned int get_bits(GetBitContext *s, int n)
Read 1-25 bits.
Definition get_bits.h:337
static unsigned int show_bits(GetBitContext *s, int n)
Show 1-25 bits.
Definition get_bits.h:373
static int init_get_bits(GetBitContext *s, const uint8_t *buffer, int bit_size)
Initialize GetBitContext.
Definition get_bits.h:517
#define AV_CODEC_CAP_DR1
Codec uses get_buffer() or get_encode_buffer() for allocating buffers and supports custom allocators.
Definition codec.h:49
#define AV_CODEC_FLAG_GRAY
Only decode/encode grayscale.
Definition avcodec.h:302
#define AV_CODEC_CAP_SLICE_THREADS
Codec supports slice-based (or partition-based) multithreading.
Definition codec.h:102
#define AV_CODEC_CAP_FRAME_THREADS
Codec supports frame-level multithreading.
Definition codec.h:98
@ AV_CODEC_ID_PRORES
Definition codec_id.h:198
@ AVDISCARD_ALL
discard all
Definition defs.h:232
#define AVERROR_PATCHWELCOME
Not yet implemented in FFmpeg, patches welcome.
Definition error.h:64
#define AVERROR_INVALIDDATA
Invalid data found when processing input.
Definition error.h:61
#define AVERROR(e)
Definition error.h:45
#define AV_FRAME_FLAG_INTERLACED
A flag to mark frames whose content is interlaced.
Definition frame.h:695
#define AV_FRAME_FLAG_TOP_FIELD_FIRST
A flag to mark frames where the top field is displayed first if the content is interlaced.
Definition frame.h:700
#define AV_LOG_DEBUG
Stuff which is only useful for libav* developers.
Definition log.h:231
#define AV_LOG_WARNING
Something somehow does not look correct.
Definition log.h:216
#define AV_LOG_ERROR
Something went wrong and cannot losslessly be recovered.
Definition log.h:210
@ AVMEDIA_TYPE_VIDEO
Definition avutil.h:200
static int decode_slice_thread(AVCodecContext *avctx, void *arg, int slice_no, int threadnr)
Definition hqx.c:403
static const FFHWAccel * ffhwaccel(const AVHWAccel *codec)
#define HWACCEL_VULKAN(codec)
Definition hwconfig.h:78
#define HWACCEL_VIDEOTOOLBOX(codec)
Definition hwconfig.h:76
#define av_log2
Definition intmath.h:84
#define AV_RL32(p)
#define AV_RB32(p)
#define AV_RB16(p)
unsigned offset
Definition libaomenc.c:763
static av_cold int decode_init(AVCodecContext *avctx)
Definition 4xm.c:998
static int decode_frame(AVCodecContext *avctx, AVFrame *picture, int *got_frame, AVPacket *avpkt)
Definition 4xm.c:837
static av_cold int decode_close(AVCodecContext *avctx)
Definition aacdec.c:1220
#define HWACCEL_MAX
av_cold void ff_blockdsp_init(BlockDSPContext *c)
Definition blockdsp.c:58
av_cold void ff_permute_scantable(uint8_t dst[64], const uint8_t src[64], const uint8_t permutation[64])
Definition idctdsp.c:30
const char * arg
Definition jacosubdec.c:65
Multithreading API for decoders.
#define av_always_inline
Definition attributes.h:72
#define av_cold
Definition attributes.h:117
common internal API header
#define NULL_IF_CONFIG_SMALL(x)
Return NULL if CONFIG_SMALL is true, otherwise the argument without modification.
Definition internal.h:97
version
Definition libkvazaar.c:313
static enum AVPixelFormat pix_fmts[]
Definition libkvazaar.c:296
static const uint16_t mask[17]
Definition lzw.c:38
#define FFMIN(a, b)
Definition macros.h:49
#define MKTAG(a, b, c, d)
Definition macros.h:55
void * av_calloc(size_t nmemb, size_t size)
Definition mem.c:264
Memory handling functions.
#define LOCAL_ALIGNED_32(t, v,...)
#define LOCAL_ALIGNED_16(t, v,...)
#define AV_PIX_FMT_YUV444P12
Definition pixfmt.h:552
@ AVCOL_RANGE_MPEG
Narrow or limited range content.
Definition pixfmt.h:766
#define AV_PIX_FMT_YUVA444P10
Definition pixfmt.h:598
#define AV_PIX_FMT_YUV422P12
Definition pixfmt.h:550
#define AV_PIX_FMT_YUV422P10
Definition pixfmt.h:546
#define AV_PIX_FMT_YUVA422P10
Definition pixfmt.h:597
AVPixelFormat
Pixel format.
Definition pixfmt.h:71
@ AV_PIX_FMT_NONE
Definition pixfmt.h:72
@ AV_PIX_FMT_VULKAN
Vulkan hardware images.
Definition pixfmt.h:379
@ AV_PIX_FMT_VIDEOTOOLBOX
hardware decoding through Videotoolbox
Definition pixfmt.h:305
#define AV_PIX_FMT_YUVA422P12
Definition pixfmt.h:599
#define AV_PIX_FMT_YUVA444P12
Definition pixfmt.h:600
#define AV_PIX_FMT_YUV444P10
Definition pixfmt.h:548
const AVProfile ff_prores_profiles[]
Definition profiles.c:175
const uint8_t ff_prores_progressive_scan[64]
Definition proresdata.c:25
const uint8_t ff_prores_interlaced_scan[64]
Definition proresdata.c:36
#define FIRST_DC_CB
Definition proresdata.h:36
#define ALPHA_SHIFT_16_TO_12(alpha_val)
Definition proresdec.c:50
#define DECODE_CODEWORD(val, codebook, gb)
Definition proresdec.c:424
static av_always_inline int decode_dc_coeffs(GetBitContext *gb, int16_t *out, int blocks_per_slice)
Definition proresdec.c:462
static av_always_inline int decode_ac_coeffs(AVCodecContext *avctx, GetBitContext *gb, int16_t *out, int blocks_per_slice)
Definition proresdec.c:490
static void unpack_alpha_10(GetBitContext *gb, uint16_t *dst, int num_coeffs, const int num_bits)
Definition proresdec.c:114
static int decode_frame(AVCodecContext *avctx, AVFrame *frame, int *got_frame, AVPacket *avpkt)
Definition proresdec.c:755
static av_cold int decode_close(AVCodecContext *avctx)
Definition proresdec.c:838
static const uint8_t run_to_cb[16]
Definition proresdec.c:487
static void unpack_alpha_12(GetBitContext *gb, uint16_t *dst, int num_coeffs, const int num_bits)
Definition proresdec.c:124
static av_cold int decode_init(AVCodecContext *avctx)
Definition proresdec.c:134
static int decode_picture(AVCodecContext *avctx)
Definition proresdec.c:736
#define ALPHA_SHIFT_16_TO_10(alpha_val)
Definition proresdec.c:48
static const uint8_t dc_codebook[7]
Definition proresdec.c:460
static void unpack_alpha(GetBitContext *gb, uint16_t *dst, int num_coeffs, const int num_bits, const int decode_precision)
Definition proresdec.c:53
#define TOSIGNED(x)
Definition proresdec.c:456
static int decode_slice_luma(AVCodecContext *avctx, SliceContext *slice, uint16_t *dst, int dst_stride, const uint8_t *buf, unsigned buf_size, const int16_t *qmat)
Definition proresdec.c:529
static void decode_slice_alpha(const ProresContext *ctx, uint16_t *dst, int dst_stride, const uint8_t *buf, int buf_size, int blocks_per_slice)
Decode alpha slice plane.
Definition proresdec.c:603
static int decode_frame_header(ProresContext *ctx, const uint8_t *buf, const int data_size, AVCodecContext *avctx)
Definition proresdec.c:186
static const uint8_t lev_to_cb[10]
Definition proresdec.c:488
static int decode_slice_thread(AVCodecContext *avctx, void *arg, int jobnr, int threadnr)
Definition proresdec.c:633
#define ALPHA_SHIFT_8_TO_10(alpha_val)
Definition proresdec.c:49
static int decode_slice_chroma(AVCodecContext *avctx, SliceContext *slice, uint16_t *dst, int dst_stride, const uint8_t *buf, unsigned buf_size, const int16_t *qmat, int log2_blocks_per_mb)
Definition proresdec.c:563
static int decode_picture_header(AVCodecContext *avctx, const uint8_t *buf, const int buf_size)
Definition proresdec.c:316
#define ALPHA_SHIFT_8_TO_12(alpha_val)
Definition proresdec.c:51
av_cold void ff_proresdsp_init(ProresDSPContext *dsp, int bits_per_raw_sample)
Definition proresdsp.c:176
int ff_thread_get_buffer(AVCodecContext *avctx, AVFrame *f, int flags)
Wrapper around get_buffer() for frame-multithreaded codecs.
void ff_thread_finish_setup(AVCodecContext *avctx)
If the codec defines update_thread_context(), call this when they are ready for the next thread to st...
void av_refstruct_unref(void *objp)
Decrement the reference count of the underlying object and automatically free the object if there are...
Definition refstruct.c:120
const uint8_t * code
Definition spdifenc.c:433
unsigned int pos
Definition spdifenc.c:431
main external API structure.
Definition avcodec.h:443
enum AVPixelFormat pix_fmt
Pixel format, see AV_PIX_FMT_xxx.
Definition avcodec.h:643
int width
picture width / height.
Definition avcodec.h:604
enum AVColorRange color_range
MPEG vs JPEG YUV range.
Definition avcodec.h:681
unsigned int codec_tag
fourcc (LSB first, so "ABCD" -> ('D'<<24) + ('C'<<16) + ('B'<<8) + 'A').
Definition avcodec.h:468
enum AVColorPrimaries color_primaries
Chromaticity coordinates of the source primaries.
Definition avcodec.h:657
const struct AVHWAccel * hwaccel
Hardware accelerator in use.
Definition avcodec.h:1424
int profile
profile
Definition avcodec.h:1641
int bits_per_raw_sample
Bits per sample/pixel of internal libavcodec pixel/sample format.
Definition avcodec.h:1576
enum AVColorSpace colorspace
YUV colorspace type.
Definition avcodec.h:671
int skip_alpha
Skip processing alpha if supported by codec.
Definition avcodec.h:1686
enum AVColorTransferCharacteristic color_trc
Color Transfer Characteristic.
Definition avcodec.h:664
int flags
AV_CODEC_FLAG_*.
Definition avcodec.h:500
int(* execute2)(struct AVCodecContext *c, int(*func)(struct AVCodecContext *c2, void *arg, int jobnr, int threadnr), void *arg2, int *ret, int count)
The codec may call this to execute several independent things.
Definition avcodec.h:1633
void * priv_data
Definition avcodec.h:470
enum AVDiscard skip_frame
Skip decoding for selected frames.
Definition avcodec.h:1672
This structure describes decoded (raw) audio or video data.
Definition frame.h:472
uint8_t * data[AV_NUM_DATA_POINTERS]
pointer to the picture/channel planes.
Definition frame.h:493
int linesize[AV_NUM_DATA_POINTERS]
For video, a positive or negative value, which is typically indicating the size in bytes of each pict...
Definition frame.h:517
This structure stores compressed data.
Definition packet.h:580
AVBufferRef * buf
A reference to the reference-counted buffer where the packet data is stored.
Definition packet.h:586
int size
Definition packet.h:604
uint8_t * data
Definition packet.h:603
enum AVPixelFormat pix_fmt
Definition proresdec.h:63
int frame_type
0 = progressive, 1 = tff, 2 = bff
Definition proresdec.h:48
unsigned mb_y
Definition proresdec.h:37
unsigned data_size
Definition proresdec.h:39
const uint8_t * data
Definition proresdec.h:35
unsigned mb_count
Definition proresdec.h:38
unsigned mb_x
Definition proresdec.h:36
uint8_t run
Definition svq3.c:207
uint8_t level
Definition svq3.c:208
#define ff_dlog(a,...)
#define av_freep(p)
#define av_log(a,...)
static void error(const char *err)
#define src
Definition vp8dsp.c:248
#define height
Definition dsp.h:89
#define width
Definition dsp.h:89