2 * This file is part of FFmpeg.
4 * FFmpeg is free software; you can redistribute it and/or
5 * modify it under the terms of the GNU General Public
6 * License as published by the Free Software Foundation;
7 * version 2 of the License.
9 * FFmpeg is distributed in the hope that it will be useful,
10 * but WITHOUT ANY WARRANTY; without even the implied warranty of
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
12 * General Public License for more details.
14 * You should have received a copy of the GNU General Public
15 * License along with FFmpeg; if not, write to the Free Software
16 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
20 * @file libavcodec/proresdec.c
21 * Known FOURCCs: 'apch' (HQ), 'apcn' (SD), 'apcs' (LT), 'acpo' (Proxy), 'ap4c' (4444)
26 #define A32_BITSTREAM_READER
31 #include "simple_idct.h"
44 int frame_type; ///< 0 = progressive, 1 = tff, 2 = bff
45 uint8_t qmat_luma[64];
46 uint8_t qmat_chroma[64];
48 int slice_count; ///< number of slices in the current picture
49 unsigned mb_width; ///< width of the current picture in mb
50 unsigned mb_height; ///< height of the current picture in mb
51 uint8_t progressive_scan[64];
52 uint8_t interlaced_scan[64];
55 void (*idct_put)(DCTELEM *, uint8_t *restrict, int);
58 static void permute(uint8_t *dst, const uint8_t *src, const uint8_t permutation[64])
61 for (i = 0; i < 64; i++)
62 dst[i] = permutation[src[i]];
65 static av_always_inline void put_pixels(const DCTELEM *block, uint8_t *restrict pixels, int stride)
67 int16_t *p = (int16_t*)pixels;
71 for(i = 0; i < 8; i++) {
72 for (j = 0; j < 8; j++) {
73 p[j] = av_clip(block[j], 4, 1019);
80 static void idct_put(DCTELEM *block, uint8_t *restrict pixels, int stride)
82 ff_simple_idct_10(block);
83 put_pixels(block, pixels, stride);
86 static const uint8_t progressive_scan[64] = {
87 0, 1, 8, 9, 2, 3, 10, 11,
88 16, 17, 24, 25, 18, 19, 26, 27,
89 4, 5, 12, 20, 13, 6, 7, 14,
90 21, 28, 29, 22, 15, 23, 30, 31,
91 32, 33, 40, 48, 41, 34, 35, 42,
92 49, 56, 57, 50, 43, 36, 37, 44,
93 51, 58, 59, 52, 45, 38, 39, 46,
94 53, 60, 61, 54, 47, 55, 62, 63
97 static const uint8_t interlaced_scan[64] = {
98 0, 8, 1, 9, 16, 24, 17, 25,
99 2, 10, 3, 11, 18, 26, 19, 27,
100 32, 40, 33, 34, 41, 48, 56, 49,
101 42, 35, 43, 50, 57, 58, 51, 59,
102 4, 12, 5, 6, 13, 20, 28, 21,
103 14, 7, 15, 22, 29, 36, 44, 37,
104 30, 23, 31, 38, 45, 52, 60, 53,
105 46, 39, 47, 54, 61, 62, 55, 63,
108 static av_cold int decode_init(AVCodecContext *avctx)
110 ProresContext *ctx = avctx->priv_data;
112 avctx->bits_per_raw_sample = 10;
114 dsputil_init(&ctx->dsp, avctx);
116 avctx->coded_frame = &ctx->frame;
117 ctx->frame.type = FF_I_TYPE;
118 ctx->frame.key_frame = 1;
120 ctx->idct_put = idct_put;
121 memcpy(ctx->progressive_scan, progressive_scan, sizeof(progressive_scan));
122 memcpy(ctx->interlaced_scan, interlaced_scan, sizeof(interlaced_scan));
127 static int decode_frame_header(ProresContext *ctx, const uint8_t *buf,
128 const int data_size, AVCodecContext *avctx)
130 int hdr_size, width, height, flags;
135 hdr_size = AV_RB16(buf);
136 av_dlog(avctx, "header size %d\n", hdr_size);
137 if (hdr_size > data_size) {
138 av_log(avctx, AV_LOG_ERROR, "error, wrong header size\n");
142 version = AV_RB16(buf + 2);
143 av_dlog(avctx, "%.4s version %d\n", buf+4, version);
145 av_log(avctx, AV_LOG_ERROR, "unsupported version: %d\n", version);
149 width = AV_RB16(buf + 8);
150 height = AV_RB16(buf + 10);
151 if (width != avctx->width || height != avctx->height) {
152 av_log(avctx, AV_LOG_ERROR, "picture resolution change: %dx%d -> %dx%d\n",
153 avctx->width, avctx->height, width, height);
157 ctx->frame_type = (buf[12] >> 2) & 3;
159 av_dlog(avctx, "frame type %d\n", ctx->frame_type);
161 if (ctx->frame_type == 0) {
162 scan = progressive_scan;
163 ctx->scan = ctx->progressive_scan; // permuted
165 scan = interlaced_scan;
166 ctx->scan = ctx->interlaced_scan; // permuted
167 ctx->frame.interlaced_frame = 1;
168 ctx->frame.top_field_first = ctx->frame_type == 1;
171 avctx->pix_fmt = PIX_FMT_YUV422P10;
175 av_dlog(avctx, "flags %x\n", flags);
178 permute(ctx->qmat_luma, scan, ptr);
181 memset(ctx->qmat_luma, 4, 64);
185 permute(ctx->qmat_chroma, scan, ptr);
187 memset(ctx->qmat_chroma, 4, 64);
193 static int decode_picture_header(AVCodecContext *avctx, const uint8_t *buf, const int buf_size)
195 ProresContext *ctx = avctx->priv_data;
196 int i, hdr_size, slice_count;
197 unsigned pic_data_size;
198 int log2_slice_mb_width, log2_slice_mb_height;
199 int slice_mb_count, mb_x, mb_y;
200 const uint8_t *data_ptr, *index_ptr;
202 hdr_size = buf[0] >> 3;
203 if (hdr_size < 8 || hdr_size > buf_size) {
204 av_log(avctx, AV_LOG_ERROR, "error, wrong picture header size\n");
208 pic_data_size = AV_RB32(buf + 1);
209 if (pic_data_size > buf_size) {
210 av_log(avctx, AV_LOG_ERROR, "error, wrong picture data size\n");
214 log2_slice_mb_width = buf[7] >> 4;
215 log2_slice_mb_height = buf[7] & 0xF;
216 if (log2_slice_mb_width > 3 || log2_slice_mb_height) {
217 av_log(avctx, AV_LOG_ERROR, "unsupported slice resolution: %dx%d\n",
218 1 << log2_slice_mb_width, 1 << log2_slice_mb_height);
222 ctx->mb_width = (avctx->width + 15) >> 4;
223 ctx->mb_height = (avctx->height + 15) >> 4;
225 slice_count = AV_RB16(buf + 5);
227 if (ctx->slice_count != slice_count || !ctx->slices) {
228 av_freep(&ctx->slices);
229 ctx->slices = av_mallocz(slice_count * sizeof(*ctx->slices));
231 return AVERROR(ENOMEM);
232 ctx->slice_count = slice_count;
236 return AVERROR(EINVAL);
238 if (hdr_size + slice_count*2 > buf_size) {
239 av_log(avctx, AV_LOG_ERROR, "error, wrong slice count\n");
243 // parse slice information
244 index_ptr = buf + hdr_size;
245 data_ptr = index_ptr + slice_count*2;
247 slice_mb_count = 1 << log2_slice_mb_width;
251 for (i = 0; i < slice_count; i++) {
252 SliceContext *slice = &ctx->slices[i];
254 slice->data = data_ptr;
255 data_ptr += AV_RB16(index_ptr + i*2);
257 while (ctx->mb_width - mb_x < slice_mb_count)
258 slice_mb_count >>= 1;
262 slice->mb_count = slice_mb_count;
263 slice->data_size = data_ptr - slice->data;
265 if (slice->data_size < 6) {
266 av_log(avctx, AV_LOG_ERROR, "error, wrong slice data size\n");
270 mb_x += slice_mb_count;
271 if (mb_x == ctx->mb_width) {
272 slice_mb_count = 1 << log2_slice_mb_width;
276 if (data_ptr > buf + buf_size) {
277 av_log(avctx, AV_LOG_ERROR, "error, slice out of bounds\n");
282 return pic_data_size;
285 #define DECODE_CODEWORD(val, codebook) \
287 unsigned int rice_order, exp_order, switch_bits; \
288 unsigned int q, buf, bits; \
290 UPDATE_CACHE(re, gb); \
291 buf = GET_CACHE(re, gb); \
293 /* number of bits to switch between rice and exp golomb */ \
294 switch_bits = codebook & 3; \
295 rice_order = codebook >> 5; \
296 exp_order = (codebook >> 2) & 7; \
298 q = 31-av_log2(buf); \
300 if (q > switch_bits) { /* exp golomb */ \
301 bits = exp_order - switch_bits + (q<<1); \
302 val = SHOW_UBITS(re, gb, bits) - (1 << exp_order) + \
303 ((switch_bits + 1) << rice_order); \
304 SKIP_BITS(re, gb, bits); \
305 } else if (rice_order) { \
306 SKIP_BITS(re, gb, q+1); \
307 val = (q << rice_order) + SHOW_UBITS(re, gb, rice_order); \
308 SKIP_BITS(re, gb, rice_order); \
311 SKIP_BITS(re, gb, q+1); \
315 #define TOSIGNED(x) (((x) >> 1) ^ (-((x) & 1)))
317 #define FIRST_DC_CB 0xB8
319 static const uint8_t dc_codebook[7] = { 0x04, 0x28, 0x28, 0x4D, 0x4D, 0x70, 0x70};
321 static av_always_inline void decode_dc_coeffs(GetBitContext *gb, DCTELEM *out,
322 int blocks_per_slice, const int *qmat)
325 int code, code2, i, sign;
329 DECODE_CODEWORD(code, FIRST_DC_CB);
330 prev_dc = TOSIGNED(code);
331 out[0] = 4096 + ((prev_dc * qmat[0]) >> 2);
333 out += 64; // dc coeff for the next block
337 for (i = 1; i < blocks_per_slice; i++, out += 64) {
338 DECODE_CODEWORD(code, dc_codebook[FFMIN(code, 6)]);
339 if(code) sign ^= -(code & 1);
341 prev_dc += (((code + 1) >> 1) ^ sign) - sign;
342 out[0] = 4096 + ((prev_dc * qmat[0]) >> 2);
344 CLOSE_READER(re, gb);
347 // adaptive codebook switching lut according to previous run/level values
348 static const uint8_t run_to_cb[16] = { 0x06, 0x06, 0x05, 0x05, 0x04, 0x29, 0x29, 0x29, 0x29, 0x28, 0x28, 0x28, 0x28, 0x28, 0x28, 0x4C };
349 static const uint8_t lev_to_cb[10] = { 0x04, 0x0A, 0x05, 0x06, 0x04, 0x28, 0x28, 0x28, 0x28, 0x4C };
351 static av_always_inline void decode_ac_coeffs(AVCodecContext *avctx, GetBitContext *gb,
352 DCTELEM *out, int blocks_per_slice,
355 ProresContext *ctx = avctx->priv_data;
356 int block_mask, sign;
357 unsigned pos, run, level;
358 int max_coeffs, i, bits_left;
359 int log2_block_count = av_log2(blocks_per_slice);
366 max_coeffs = 64 << log2_block_count;
367 block_mask = blocks_per_slice - 1;
369 for (pos = block_mask;;) {
370 bits_left = gb->size_in_bits - (((uint8_t*)re_buffer_ptr - gb->buffer)*8 - 32 + re_bit_count);
371 if (!bits_left || (bits_left < 32 && !SHOW_UBITS(re, gb, bits_left)))
374 DECODE_CODEWORD(run, run_to_cb[FFMIN(run, 15)]);
376 if (pos >= max_coeffs) {
377 av_log(avctx, AV_LOG_ERROR, "ac tex damaged %d, %d\n", pos, max_coeffs);
381 DECODE_CODEWORD(level, lev_to_cb[FFMIN(level, 9)]);
384 i = pos >> log2_block_count;
386 sign = SHOW_SBITS(re, gb, 1);
387 SKIP_BITS(re, gb, 1);
388 out[((pos & block_mask) << 6) + ctx->scan[i]] = (((level ^ sign) - sign) * qmat[i]) >> 2;
391 CLOSE_READER(re, gb);
394 static void decode_slice_luma(AVCodecContext *avctx, SliceContext *slice,
395 uint8_t *dst, int dst_stride,
396 const uint8_t *buf, unsigned buf_size,
399 ProresContext *ctx = avctx->priv_data;
400 DECLARE_ALIGNED(16, DCTELEM, blocks)[8*4*64], *block;
402 int i, blocks_per_slice = slice->mb_count<<2;
404 for (i = 0; i < blocks_per_slice; i++)
405 ctx->dsp.clear_block(blocks+(i<<6));
407 init_get_bits(&gb, buf, buf_size << 3);
409 decode_dc_coeffs(&gb, blocks, blocks_per_slice, qmat);
410 decode_ac_coeffs(avctx, &gb, blocks, blocks_per_slice, qmat);
413 for (i = 0; i < slice->mb_count; i++) {
414 ctx->idct_put(block+(0<<6), dst, dst_stride);
415 ctx->idct_put(block+(1<<6), dst+16, dst_stride);
416 ctx->idct_put(block+(2<<6), dst+8*dst_stride, dst_stride);
417 ctx->idct_put(block+(3<<6), dst+8*dst_stride+16, dst_stride);
423 static void decode_slice_chroma(AVCodecContext *avctx, SliceContext *slice,
424 uint8_t *dst, int dst_stride,
425 const uint8_t *buf, unsigned buf_size,
428 ProresContext *ctx = avctx->priv_data;
429 DECLARE_ALIGNED(16, DCTELEM, blocks)[8*4*64], *block;
431 int i, blocks_per_slice = slice->mb_count*2;
433 for (i = 0; i < blocks_per_slice; i++)
434 ctx->dsp.clear_block(blocks+(i<<6));
436 init_get_bits(&gb, buf, buf_size << 3);
438 decode_dc_coeffs(&gb, blocks, blocks_per_slice, qmat);
439 decode_ac_coeffs(avctx, &gb, blocks, blocks_per_slice, qmat);
442 for (i = 0; i < slice->mb_count; i++) {
443 ctx->idct_put(block+(0<<6), dst, dst_stride);
444 ctx->idct_put(block+(1<<6), dst+8*dst_stride, dst_stride);
450 static int decode_slice_thread(AVCodecContext *avctx, void *arg, int jobnr, int threadnr)
452 ProresContext *ctx = avctx->priv_data;
453 SliceContext *slice = &ctx->slices[jobnr];
454 const uint8_t *buf = slice->data;
455 AVFrame *pic = avctx->coded_frame;
456 int i, hdr_size, qscale;
457 int luma_stride, chroma_stride;
458 int y_data_size, u_data_size, v_data_size;
459 uint8_t *dest_y, *dest_u, *dest_v;
460 int qmat_luma_scaled[64];
461 int qmat_chroma_scaled[64];
463 //av_log(avctx, AV_LOG_INFO, "slice %d mb width %d mb x %d y %d\n",
464 // jobnr, slice->mb_count, slice->mb_x, slice->mb_y);
467 hdr_size = buf[0] >> 3;
468 qscale = av_clip(buf[1], 1, 224);
469 qscale = qscale > 128 ? qscale - 96 << 2: qscale;
470 y_data_size = AV_RB16(buf + 2);
471 u_data_size = AV_RB16(buf + 4);
472 v_data_size = slice->data_size - y_data_size - u_data_size - hdr_size;
474 if (y_data_size < 0 || u_data_size < 0 || v_data_size < 0) {
475 av_log(avctx, AV_LOG_ERROR, "invalid plane data size\n");
481 for (i = 0; i < 64; i++) {
482 qmat_luma_scaled[i] = ctx->qmat_luma[i] * qscale;
483 qmat_chroma_scaled[i] = ctx->qmat_chroma[i] * qscale;
486 if (ctx->frame_type == 0) {
487 luma_stride = pic->linesize[0];
488 chroma_stride = pic->linesize[1];
490 luma_stride = pic->linesize[0] << 1;
491 chroma_stride = pic->linesize[1] << 1;
494 dest_y = pic->data[0] + (slice->mb_y << 4) * luma_stride + (slice->mb_x << 5);
495 dest_u = pic->data[1] + (slice->mb_y << 4) * chroma_stride + (slice->mb_x << 4);
496 dest_v = pic->data[2] + (slice->mb_y << 4) * chroma_stride + (slice->mb_x << 4);
498 if (ctx->frame_type && ctx->first_field ^ ctx->frame.top_field_first) {
499 dest_y += pic->linesize[0];
500 dest_u += pic->linesize[1];
501 dest_v += pic->linesize[2];
504 decode_slice_luma(avctx, slice, dest_y, luma_stride,
505 buf, y_data_size, qmat_luma_scaled);
507 if (!(avctx->flags & CODEC_FLAG_GRAY)) {
508 decode_slice_chroma(avctx, slice, dest_u, chroma_stride,
509 buf + y_data_size, u_data_size,
511 decode_slice_chroma(avctx, slice, dest_v, chroma_stride,
512 buf + y_data_size + u_data_size, v_data_size,
519 static int decode_picture(AVCodecContext *avctx)
521 ProresContext *ctx = avctx->priv_data;
522 int i, threads_ret[ctx->slice_count];
524 avctx->execute2(avctx, decode_slice_thread, NULL, threads_ret, ctx->slice_count);
526 for (i = 0; i < ctx->slice_count; i++)
527 if (threads_ret[i] < 0)
528 return threads_ret[i];
533 static int decode_frame(AVCodecContext *avctx, void *data, int *data_size,
536 ProresContext *ctx = avctx->priv_data;
537 AVFrame *frame = avctx->coded_frame;
538 const uint8_t *buf = avpkt->data;
539 int buf_size = avpkt->size;
540 int frame_hdr_size, pic_size;
542 if (buf_size < 28 || buf_size != AV_RB32(buf) ||
543 AV_RL32(buf + 4) != AV_RL32("icpf")) {
544 av_log(avctx, AV_LOG_ERROR, "invalid frame header\n");
548 ctx->first_field = 1;
553 frame_hdr_size = decode_frame_header(ctx, buf, buf_size, avctx);
554 if (frame_hdr_size < 0)
557 buf += frame_hdr_size;
558 buf_size -= frame_hdr_size;
561 pic_size = decode_picture_header(avctx, buf, buf_size);
563 av_log(avctx, AV_LOG_ERROR, "error decoding picture header\n");
568 avctx->release_buffer(avctx, frame);
570 if (avctx->get_buffer(avctx, frame) < 0)
573 if (decode_picture(avctx)) {
574 av_log(avctx, AV_LOG_ERROR, "error decoding picture\n");
579 buf_size -= pic_size;
581 if (ctx->frame_type && buf_size > 0 && ctx->first_field) {
582 ctx->first_field = 0;
586 *data_size = sizeof(AVFrame);
587 *(AVFrame*)data = *frame;
592 static av_cold int decode_close(AVCodecContext *avctx)
594 ProresContext *ctx = avctx->priv_data;
596 AVFrame *frame = avctx->coded_frame;
598 avctx->release_buffer(avctx, frame);
599 av_freep(&ctx->slices);
604 AVCodec ff_prores_decoder = {
606 .type = AVMEDIA_TYPE_VIDEO,
607 .id = CODEC_ID_PRORES,
608 .priv_data_size = sizeof(ProresContext),
610 .close = decode_close,
611 .decode = decode_frame,
612 .long_name = NULL_IF_CONFIG_SMALL("ProRes"),
613 .capabilities = CODEC_CAP_SLICE_THREADS,