2 * Copyright (c) 2010-2011 Maxim Poliakovski
3 * Copyright (c) 2010-2011 Elvis Presley
5 * This file is part of FFmpeg.
7 * FFmpeg is free software; you can redistribute it and/or
8 * modify it under the terms of the GNU General Public
9 * License as published by the Free Software Foundation;
10 * version 2 of the License.
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 * General Public License for more details.
17 * You should have received a copy of the GNU 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
23 * @file libavcodec/proresdec.c
24 * Known FOURCCs: 'apch' (HQ), 'apcn' (SD), 'apcs' (LT), 'acpo' (Proxy), 'ap4c' (4444)
29 #define A32_BITSTREAM_READER
34 #include "simple_idct.h"
47 int frame_type; ///< 0 = progressive, 1 = tff, 2 = bff
48 uint8_t qmat_luma[64];
49 uint8_t qmat_chroma[64];
51 int slice_count; ///< number of slices in the current picture
52 unsigned mb_width; ///< width of the current picture in mb
53 unsigned mb_height; ///< height of the current picture in mb
54 uint8_t progressive_scan[64];
55 uint8_t interlaced_scan[64];
58 void (*idct_put)(DCTELEM *, uint8_t *restrict, int);
61 static void permute(uint8_t *dst, const uint8_t *src, const uint8_t permutation[64])
64 for (i = 0; i < 64; i++)
65 dst[i] = permutation[src[i]];
68 static av_always_inline void put_pixels(const DCTELEM *block, uint8_t *restrict pixels, int stride)
70 int16_t *p = (int16_t*)pixels;
74 for(i = 0; i < 8; i++) {
75 for (j = 0; j < 8; j++) {
76 p[j] = av_clip(block[j], 4, 1019);
83 static void idct_put(DCTELEM *block, uint8_t *restrict pixels, int stride)
85 ff_simple_idct_10(block);
86 put_pixels(block, pixels, stride);
89 static const uint8_t progressive_scan[64] = {
90 0, 1, 8, 9, 2, 3, 10, 11,
91 16, 17, 24, 25, 18, 19, 26, 27,
92 4, 5, 12, 20, 13, 6, 7, 14,
93 21, 28, 29, 22, 15, 23, 30, 31,
94 32, 33, 40, 48, 41, 34, 35, 42,
95 49, 56, 57, 50, 43, 36, 37, 44,
96 51, 58, 59, 52, 45, 38, 39, 46,
97 53, 60, 61, 54, 47, 55, 62, 63
100 static const uint8_t interlaced_scan[64] = {
101 0, 8, 1, 9, 16, 24, 17, 25,
102 2, 10, 3, 11, 18, 26, 19, 27,
103 32, 40, 33, 34, 41, 48, 56, 49,
104 42, 35, 43, 50, 57, 58, 51, 59,
105 4, 12, 5, 6, 13, 20, 28, 21,
106 14, 7, 15, 22, 29, 36, 44, 37,
107 30, 23, 31, 38, 45, 52, 60, 53,
108 46, 39, 47, 54, 61, 62, 55, 63,
111 static av_cold int decode_init(AVCodecContext *avctx)
113 ProresContext *ctx = avctx->priv_data;
115 avctx->bits_per_raw_sample = 10;
117 dsputil_init(&ctx->dsp, avctx);
119 avctx->coded_frame = &ctx->frame;
120 ctx->frame.type = FF_I_TYPE;
121 ctx->frame.key_frame = 1;
123 ctx->idct_put = idct_put;
124 memcpy(ctx->progressive_scan, progressive_scan, sizeof(progressive_scan));
125 memcpy(ctx->interlaced_scan, interlaced_scan, sizeof(interlaced_scan));
130 static int decode_frame_header(ProresContext *ctx, const uint8_t *buf,
131 const int data_size, AVCodecContext *avctx)
133 int hdr_size, width, height, flags;
138 hdr_size = AV_RB16(buf);
139 av_dlog(avctx, "header size %d\n", hdr_size);
140 if (hdr_size > data_size) {
141 av_log(avctx, AV_LOG_ERROR, "error, wrong header size\n");
145 version = AV_RB16(buf + 2);
146 av_dlog(avctx, "%.4s version %d\n", buf+4, version);
148 av_log(avctx, AV_LOG_ERROR, "unsupported version: %d\n", version);
152 width = AV_RB16(buf + 8);
153 height = AV_RB16(buf + 10);
154 if (width != avctx->width || height != avctx->height) {
155 av_log(avctx, AV_LOG_ERROR, "picture resolution change: %dx%d -> %dx%d\n",
156 avctx->width, avctx->height, width, height);
160 ctx->frame_type = (buf[12] >> 2) & 3;
162 av_dlog(avctx, "frame type %d\n", ctx->frame_type);
164 if (ctx->frame_type == 0) {
165 scan = progressive_scan;
166 ctx->scan = ctx->progressive_scan; // permuted
168 scan = interlaced_scan;
169 ctx->scan = ctx->interlaced_scan; // permuted
170 ctx->frame.interlaced_frame = 1;
171 ctx->frame.top_field_first = ctx->frame_type == 1;
174 avctx->pix_fmt = ((buf[12] & 0xC0) == 0xC0) ? PIX_FMT_YUV444P10 : PIX_FMT_YUV422P10;
178 av_dlog(avctx, "flags %x\n", flags);
181 permute(ctx->qmat_luma, scan, ptr);
184 memset(ctx->qmat_luma, 4, 64);
188 permute(ctx->qmat_chroma, scan, ptr);
190 memset(ctx->qmat_chroma, 4, 64);
196 static int decode_picture_header(AVCodecContext *avctx, const uint8_t *buf, const int buf_size)
198 ProresContext *ctx = avctx->priv_data;
199 int i, hdr_size, slice_count;
200 unsigned pic_data_size;
201 int log2_slice_mb_width, log2_slice_mb_height;
202 int slice_mb_count, mb_x, mb_y;
203 const uint8_t *data_ptr, *index_ptr;
205 hdr_size = buf[0] >> 3;
206 if (hdr_size < 8 || hdr_size > buf_size) {
207 av_log(avctx, AV_LOG_ERROR, "error, wrong picture header size\n");
211 pic_data_size = AV_RB32(buf + 1);
212 if (pic_data_size > buf_size) {
213 av_log(avctx, AV_LOG_ERROR, "error, wrong picture data size\n");
217 log2_slice_mb_width = buf[7] >> 4;
218 log2_slice_mb_height = buf[7] & 0xF;
219 if (log2_slice_mb_width > 3 || log2_slice_mb_height) {
220 av_log(avctx, AV_LOG_ERROR, "unsupported slice resolution: %dx%d\n",
221 1 << log2_slice_mb_width, 1 << log2_slice_mb_height);
225 ctx->mb_width = (avctx->width + 15) >> 4;
226 ctx->mb_height = (avctx->height + 15) >> 4;
228 slice_count = AV_RB16(buf + 5);
230 if (ctx->slice_count != slice_count || !ctx->slices) {
231 av_freep(&ctx->slices);
232 ctx->slices = av_mallocz(slice_count * sizeof(*ctx->slices));
234 return AVERROR(ENOMEM);
235 ctx->slice_count = slice_count;
239 return AVERROR(EINVAL);
241 if (hdr_size + slice_count*2 > buf_size) {
242 av_log(avctx, AV_LOG_ERROR, "error, wrong slice count\n");
246 // parse slice information
247 index_ptr = buf + hdr_size;
248 data_ptr = index_ptr + slice_count*2;
250 slice_mb_count = 1 << log2_slice_mb_width;
254 for (i = 0; i < slice_count; i++) {
255 SliceContext *slice = &ctx->slices[i];
257 slice->data = data_ptr;
258 data_ptr += AV_RB16(index_ptr + i*2);
260 while (ctx->mb_width - mb_x < slice_mb_count)
261 slice_mb_count >>= 1;
265 slice->mb_count = slice_mb_count;
266 slice->data_size = data_ptr - slice->data;
268 if (slice->data_size < 6) {
269 av_log(avctx, AV_LOG_ERROR, "error, wrong slice data size\n");
273 mb_x += slice_mb_count;
274 if (mb_x == ctx->mb_width) {
275 slice_mb_count = 1 << log2_slice_mb_width;
279 if (data_ptr > buf + buf_size) {
280 av_log(avctx, AV_LOG_ERROR, "error, slice out of bounds\n");
285 return pic_data_size;
288 #define DECODE_CODEWORD(val, codebook) \
290 unsigned int rice_order, exp_order, switch_bits; \
291 unsigned int q, buf, bits; \
293 UPDATE_CACHE(re, gb); \
294 buf = GET_CACHE(re, gb); \
296 /* number of bits to switch between rice and exp golomb */ \
297 switch_bits = codebook & 3; \
298 rice_order = codebook >> 5; \
299 exp_order = (codebook >> 2) & 7; \
301 q = 31-av_log2(buf); \
303 if (q > switch_bits) { /* exp golomb */ \
304 bits = exp_order - switch_bits + (q<<1); \
305 val = SHOW_UBITS(re, gb, bits) - (1 << exp_order) + \
306 ((switch_bits + 1) << rice_order); \
307 SKIP_BITS(re, gb, bits); \
308 } else if (rice_order) { \
309 SKIP_BITS(re, gb, q+1); \
310 val = (q << rice_order) + SHOW_UBITS(re, gb, rice_order); \
311 SKIP_BITS(re, gb, rice_order); \
314 SKIP_BITS(re, gb, q+1); \
318 #define TOSIGNED(x) (((x) >> 1) ^ (-((x) & 1)))
320 #define FIRST_DC_CB 0xB8
322 static const uint8_t dc_codebook[7] = { 0x04, 0x28, 0x28, 0x4D, 0x4D, 0x70, 0x70};
324 static av_always_inline void decode_dc_coeffs(GetBitContext *gb, DCTELEM *out,
325 int blocks_per_slice, const int *qmat)
332 DECODE_CODEWORD(code, FIRST_DC_CB);
333 prev_dc = TOSIGNED(code);
334 out[0] = 4096 + ((prev_dc * qmat[0]) >> 2);
336 out += 64; // dc coeff for the next block
340 for (i = 1; i < blocks_per_slice; i++, out += 64) {
341 DECODE_CODEWORD(code, dc_codebook[FFMIN(code, 6)]);
342 if(code) sign ^= -(code & 1);
344 prev_dc += (((code + 1) >> 1) ^ sign) - sign;
345 out[0] = 4096 + ((prev_dc * qmat[0]) >> 2);
347 CLOSE_READER(re, gb);
350 // adaptive codebook switching lut according to previous run/level values
351 static const uint8_t run_to_cb[16] = { 0x06, 0x06, 0x05, 0x05, 0x04, 0x29, 0x29, 0x29, 0x29, 0x28, 0x28, 0x28, 0x28, 0x28, 0x28, 0x4C };
352 static const uint8_t lev_to_cb[10] = { 0x04, 0x0A, 0x05, 0x06, 0x04, 0x28, 0x28, 0x28, 0x28, 0x4C };
354 static av_always_inline void decode_ac_coeffs(AVCodecContext *avctx, GetBitContext *gb,
355 DCTELEM *out, int blocks_per_slice,
358 ProresContext *ctx = avctx->priv_data;
359 int block_mask, sign;
360 unsigned pos, run, level;
361 int max_coeffs, i, bits_left;
362 int log2_block_count = av_log2(blocks_per_slice);
369 max_coeffs = 64 << log2_block_count;
370 block_mask = blocks_per_slice - 1;
372 for (pos = block_mask;;) {
373 bits_left = gb->size_in_bits - (((uint8_t*)re_buffer_ptr - gb->buffer)*8 - 32 + re_bit_count);
374 if (!bits_left || (bits_left < 32 && !SHOW_UBITS(re, gb, bits_left)))
377 DECODE_CODEWORD(run, run_to_cb[FFMIN(run, 15)]);
379 if (pos >= max_coeffs) {
380 av_log(avctx, AV_LOG_ERROR, "ac tex damaged %d, %d\n", pos, max_coeffs);
384 DECODE_CODEWORD(level, lev_to_cb[FFMIN(level, 9)]);
387 i = pos >> log2_block_count;
389 sign = SHOW_SBITS(re, gb, 1);
390 SKIP_BITS(re, gb, 1);
391 out[((pos & block_mask) << 6) + ctx->scan[i]] = (((level ^ sign) - sign) * qmat[i]) >> 2;
394 CLOSE_READER(re, gb);
397 static void decode_slice_luma(AVCodecContext *avctx, SliceContext *slice,
398 uint8_t *dst, int dst_stride,
399 const uint8_t *buf, unsigned buf_size,
402 ProresContext *ctx = avctx->priv_data;
403 LOCAL_ALIGNED_16(DCTELEM, blocks, [8*4*64]);
406 int i, blocks_per_slice = slice->mb_count<<2;
408 for (i = 0; i < blocks_per_slice; i++)
409 ctx->dsp.clear_block(blocks+(i<<6));
411 init_get_bits(&gb, buf, buf_size << 3);
413 decode_dc_coeffs(&gb, blocks, blocks_per_slice, qmat);
414 decode_ac_coeffs(avctx, &gb, blocks, blocks_per_slice, qmat);
418 for (i = 0; i < slice->mb_count; i++) {
419 ctx->idct_put(block+(0<<6), dst, dst_stride);
420 ctx->idct_put(block+(1<<6), dst+16, dst_stride);
421 ctx->idct_put(block+(2<<6), dst+8*dst_stride, dst_stride);
422 ctx->idct_put(block+(3<<6), dst+8*dst_stride+16, dst_stride);
429 static void decode_slice_chroma(AVCodecContext *avctx, SliceContext *slice,
430 uint8_t *dst, int dst_stride,
431 const uint8_t *buf, unsigned buf_size,
432 const int *qmat, int log2_blocks_per_mb)
434 ProresContext *ctx = avctx->priv_data;
435 LOCAL_ALIGNED_16(DCTELEM, blocks, [8*4*64]);
438 int i, j, blocks_per_slice = slice->mb_count << log2_blocks_per_mb;
440 for (i = 0; i < blocks_per_slice; i++)
441 ctx->dsp.clear_block(blocks+(i<<6));
443 init_get_bits(&gb, buf, buf_size << 3);
445 decode_dc_coeffs(&gb, blocks, blocks_per_slice, qmat);
446 decode_ac_coeffs(avctx, &gb, blocks, blocks_per_slice, qmat);
449 for (i = 0; i < slice->mb_count; i++) {
450 for (j = 0; j < log2_blocks_per_mb; j++) {
451 ctx->idct_put(block+(0<<6), dst, dst_stride);
452 ctx->idct_put(block+(1<<6), dst+8*dst_stride, dst_stride);
459 static int decode_slice_thread(AVCodecContext *avctx, void *arg, int jobnr, int threadnr)
461 ProresContext *ctx = avctx->priv_data;
462 SliceContext *slice = &ctx->slices[jobnr];
463 const uint8_t *buf = slice->data;
464 AVFrame *pic = avctx->coded_frame;
465 int i, hdr_size, qscale, log2_chroma_blocks_per_mb;
466 int luma_stride, chroma_stride;
467 int y_data_size, u_data_size, v_data_size;
468 uint8_t *dest_y, *dest_u, *dest_v;
469 int qmat_luma_scaled[64];
470 int qmat_chroma_scaled[64];
473 //av_log(avctx, AV_LOG_INFO, "slice %d mb width %d mb x %d y %d\n",
474 // jobnr, slice->mb_count, slice->mb_x, slice->mb_y);
477 hdr_size = buf[0] >> 3;
478 qscale = av_clip(buf[1], 1, 224);
479 qscale = qscale > 128 ? qscale - 96 << 2: qscale;
480 y_data_size = AV_RB16(buf + 2);
481 u_data_size = AV_RB16(buf + 4);
482 v_data_size = slice->data_size - y_data_size - u_data_size - hdr_size;
483 if (hdr_size > 7) v_data_size = AV_RB16(buf + 6);
485 if (y_data_size < 0 || u_data_size < 0 || v_data_size < 0) {
486 av_log(avctx, AV_LOG_ERROR, "invalid plane data size\n");
492 for (i = 0; i < 64; i++) {
493 qmat_luma_scaled[i] = ctx->qmat_luma[i] * qscale;
494 qmat_chroma_scaled[i] = ctx->qmat_chroma[i] * qscale;
497 if (ctx->frame_type == 0) {
498 luma_stride = pic->linesize[0];
499 chroma_stride = pic->linesize[1];
501 luma_stride = pic->linesize[0] << 1;
502 chroma_stride = pic->linesize[1] << 1;
505 if (avctx->pix_fmt == PIX_FMT_YUV444P10) {
507 log2_chroma_blocks_per_mb = 2;
510 log2_chroma_blocks_per_mb = 1;
513 dest_y = pic->data[0] + (slice->mb_y << 4) * luma_stride + (slice->mb_x << 5);
514 dest_u = pic->data[1] + (slice->mb_y << 4) * chroma_stride + (slice->mb_x << mb_x_shift);
515 dest_v = pic->data[2] + (slice->mb_y << 4) * chroma_stride + (slice->mb_x << mb_x_shift);
517 if (ctx->frame_type && ctx->first_field ^ ctx->frame.top_field_first) {
518 dest_y += pic->linesize[0];
519 dest_u += pic->linesize[1];
520 dest_v += pic->linesize[2];
523 decode_slice_luma(avctx, slice, dest_y, luma_stride,
524 buf, y_data_size, qmat_luma_scaled);
526 if (!(avctx->flags & CODEC_FLAG_GRAY)) {
527 decode_slice_chroma(avctx, slice, dest_u, chroma_stride,
528 buf + y_data_size, u_data_size,
529 qmat_chroma_scaled, log2_chroma_blocks_per_mb);
530 decode_slice_chroma(avctx, slice, dest_v, chroma_stride,
531 buf + y_data_size + u_data_size, v_data_size,
532 qmat_chroma_scaled, log2_chroma_blocks_per_mb);
538 static int decode_picture(AVCodecContext *avctx)
540 ProresContext *ctx = avctx->priv_data;
541 int i, threads_ret[ctx->slice_count];
543 avctx->execute2(avctx, decode_slice_thread, NULL, threads_ret, ctx->slice_count);
545 for (i = 0; i < ctx->slice_count; i++)
546 if (threads_ret[i] < 0)
547 return threads_ret[i];
552 static int decode_frame(AVCodecContext *avctx, void *data, int *data_size,
555 ProresContext *ctx = avctx->priv_data;
556 AVFrame *frame = avctx->coded_frame;
557 const uint8_t *buf = avpkt->data;
558 int buf_size = avpkt->size;
559 int frame_hdr_size, pic_size;
561 if (buf_size < 28 || AV_RL32(buf + 4) != AV_RL32("icpf")) {
562 av_log(avctx, AV_LOG_ERROR, "invalid frame header\n");
566 ctx->first_field = 1;
571 frame_hdr_size = decode_frame_header(ctx, buf, buf_size, avctx);
572 if (frame_hdr_size < 0)
575 buf += frame_hdr_size;
576 buf_size -= frame_hdr_size;
579 pic_size = decode_picture_header(avctx, buf, buf_size);
581 av_log(avctx, AV_LOG_ERROR, "error decoding picture header\n");
586 avctx->release_buffer(avctx, frame);
588 if (avctx->get_buffer(avctx, frame) < 0)
591 if (decode_picture(avctx)) {
592 av_log(avctx, AV_LOG_ERROR, "error decoding picture\n");
597 buf_size -= pic_size;
599 if (ctx->frame_type && buf_size > 0 && ctx->first_field) {
600 ctx->first_field = 0;
604 *data_size = sizeof(AVFrame);
605 *(AVFrame*)data = *frame;
610 static av_cold int decode_close(AVCodecContext *avctx)
612 ProresContext *ctx = avctx->priv_data;
614 AVFrame *frame = avctx->coded_frame;
616 avctx->release_buffer(avctx, frame);
617 av_freep(&ctx->slices);
622 AVCodec ff_prores_gpl_decoder = {
623 .name = "prores_gpl",
624 .type = AVMEDIA_TYPE_VIDEO,
625 .id = CODEC_ID_PRORES,
626 .priv_data_size = sizeof(ProresContext),
628 .close = decode_close,
629 .decode = decode_frame,
630 .long_name = NULL_IF_CONFIG_SMALL("ProRes"),
631 .capabilities = CODEC_CAP_SLICE_THREADS,