00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
00027
00028
00029 #define LONG_BITSTREAM_READER
00030
00031 #include "avcodec.h"
00032 #include "get_bits.h"
00033 #include "internal.h"
00034 #include "simple_idct.h"
00035 #include "proresdec.h"
00036
00037 static void permute(uint8_t *dst, const uint8_t *src, const uint8_t permutation[64])
00038 {
00039 int i;
00040 for (i = 0; i < 64; i++)
00041 dst[i] = permutation[src[i]];
00042 }
00043
00044 static const uint8_t progressive_scan[64] = {
00045 0, 1, 8, 9, 2, 3, 10, 11,
00046 16, 17, 24, 25, 18, 19, 26, 27,
00047 4, 5, 12, 20, 13, 6, 7, 14,
00048 21, 28, 29, 22, 15, 23, 30, 31,
00049 32, 33, 40, 48, 41, 34, 35, 42,
00050 49, 56, 57, 50, 43, 36, 37, 44,
00051 51, 58, 59, 52, 45, 38, 39, 46,
00052 53, 60, 61, 54, 47, 55, 62, 63
00053 };
00054
00055 static const uint8_t interlaced_scan[64] = {
00056 0, 8, 1, 9, 16, 24, 17, 25,
00057 2, 10, 3, 11, 18, 26, 19, 27,
00058 32, 40, 33, 34, 41, 48, 56, 49,
00059 42, 35, 43, 50, 57, 58, 51, 59,
00060 4, 12, 5, 6, 13, 20, 28, 21,
00061 14, 7, 15, 22, 29, 36, 44, 37,
00062 30, 23, 31, 38, 45, 52, 60, 53,
00063 46, 39, 47, 54, 61, 62, 55, 63,
00064 };
00065
00066 static av_cold int decode_init(AVCodecContext *avctx)
00067 {
00068 ProresContext *ctx = avctx->priv_data;
00069 uint8_t idct_permutation[64];
00070
00071 avctx->bits_per_raw_sample = 10;
00072
00073 ff_dsputil_init(&ctx->dsp, avctx);
00074 ff_proresdsp_init(&ctx->prodsp, avctx);
00075
00076 avctx->coded_frame = &ctx->frame;
00077 ctx->frame.type = AV_PICTURE_TYPE_I;
00078 ctx->frame.key_frame = 1;
00079
00080 ff_init_scantable_permutation(idct_permutation,
00081 ctx->prodsp.idct_permutation_type);
00082
00083 permute(ctx->progressive_scan, progressive_scan, idct_permutation);
00084 permute(ctx->interlaced_scan, interlaced_scan, idct_permutation);
00085
00086 return 0;
00087 }
00088
00089 static int decode_frame_header(ProresContext *ctx, const uint8_t *buf,
00090 const int data_size, AVCodecContext *avctx)
00091 {
00092 int hdr_size, width, height, flags;
00093 int version;
00094 const uint8_t *ptr;
00095
00096 hdr_size = AV_RB16(buf);
00097 av_dlog(avctx, "header size %d\n", hdr_size);
00098 if (hdr_size > data_size) {
00099 av_log(avctx, AV_LOG_ERROR, "error, wrong header size\n");
00100 return -1;
00101 }
00102
00103 version = AV_RB16(buf + 2);
00104 av_dlog(avctx, "%.4s version %d\n", buf+4, version);
00105 if (version > 1) {
00106 av_log(avctx, AV_LOG_ERROR, "unsupported version: %d\n", version);
00107 return -1;
00108 }
00109
00110 width = AV_RB16(buf + 8);
00111 height = AV_RB16(buf + 10);
00112 if (width != avctx->width || height != avctx->height) {
00113 av_log(avctx, AV_LOG_ERROR, "picture resolution change: %dx%d -> %dx%d\n",
00114 avctx->width, avctx->height, width, height);
00115 return -1;
00116 }
00117
00118 ctx->frame_type = (buf[12] >> 2) & 3;
00119
00120 av_dlog(avctx, "frame type %d\n", ctx->frame_type);
00121
00122 if (ctx->frame_type == 0) {
00123 ctx->scan = ctx->progressive_scan;
00124 } else {
00125 ctx->scan = ctx->interlaced_scan;
00126 ctx->frame.interlaced_frame = 1;
00127 ctx->frame.top_field_first = ctx->frame_type == 1;
00128 }
00129
00130 avctx->pix_fmt = (buf[12] & 0xC0) == 0xC0 ? AV_PIX_FMT_YUV444P10 : AV_PIX_FMT_YUV422P10;
00131
00132 ptr = buf + 20;
00133 flags = buf[19];
00134 av_dlog(avctx, "flags %x\n", flags);
00135
00136 if (flags & 2) {
00137 if(buf + data_size - ptr < 64) {
00138 av_log(avctx, AV_LOG_ERROR, "Header truncated\n");
00139 return -1;
00140 }
00141 permute(ctx->qmat_luma, ctx->prodsp.idct_permutation, ptr);
00142 ptr += 64;
00143 } else {
00144 memset(ctx->qmat_luma, 4, 64);
00145 }
00146
00147 if (flags & 1) {
00148 if(buf + data_size - ptr < 64) {
00149 av_log(avctx, AV_LOG_ERROR, "Header truncated\n");
00150 return -1;
00151 }
00152 permute(ctx->qmat_chroma, ctx->prodsp.idct_permutation, ptr);
00153 } else {
00154 memset(ctx->qmat_chroma, 4, 64);
00155 }
00156
00157 return hdr_size;
00158 }
00159
00160 static int decode_picture_header(AVCodecContext *avctx, const uint8_t *buf, const int buf_size)
00161 {
00162 ProresContext *ctx = avctx->priv_data;
00163 int i, hdr_size, slice_count;
00164 unsigned pic_data_size;
00165 int log2_slice_mb_width, log2_slice_mb_height;
00166 int slice_mb_count, mb_x, mb_y;
00167 const uint8_t *data_ptr, *index_ptr;
00168
00169 hdr_size = buf[0] >> 3;
00170 if (hdr_size < 8 || hdr_size > buf_size) {
00171 av_log(avctx, AV_LOG_ERROR, "error, wrong picture header size\n");
00172 return -1;
00173 }
00174
00175 pic_data_size = AV_RB32(buf + 1);
00176 if (pic_data_size > buf_size) {
00177 av_log(avctx, AV_LOG_ERROR, "error, wrong picture data size\n");
00178 return -1;
00179 }
00180
00181 log2_slice_mb_width = buf[7] >> 4;
00182 log2_slice_mb_height = buf[7] & 0xF;
00183 if (log2_slice_mb_width > 3 || log2_slice_mb_height) {
00184 av_log(avctx, AV_LOG_ERROR, "unsupported slice resolution: %dx%d\n",
00185 1 << log2_slice_mb_width, 1 << log2_slice_mb_height);
00186 return -1;
00187 }
00188
00189 ctx->mb_width = (avctx->width + 15) >> 4;
00190 if (ctx->frame_type)
00191 ctx->mb_height = (avctx->height + 31) >> 5;
00192 else
00193 ctx->mb_height = (avctx->height + 15) >> 4;
00194
00195 slice_count = AV_RB16(buf + 5);
00196
00197 if (ctx->slice_count != slice_count || !ctx->slices) {
00198 av_freep(&ctx->slices);
00199 ctx->slices = av_mallocz(slice_count * sizeof(*ctx->slices));
00200 if (!ctx->slices)
00201 return AVERROR(ENOMEM);
00202 ctx->slice_count = slice_count;
00203 }
00204
00205 if (!slice_count)
00206 return AVERROR(EINVAL);
00207
00208 if (hdr_size + slice_count*2 > buf_size) {
00209 av_log(avctx, AV_LOG_ERROR, "error, wrong slice count\n");
00210 return -1;
00211 }
00212
00213
00214 index_ptr = buf + hdr_size;
00215 data_ptr = index_ptr + slice_count*2;
00216
00217 slice_mb_count = 1 << log2_slice_mb_width;
00218 mb_x = 0;
00219 mb_y = 0;
00220
00221 for (i = 0; i < slice_count; i++) {
00222 SliceContext *slice = &ctx->slices[i];
00223
00224 slice->data = data_ptr;
00225 data_ptr += AV_RB16(index_ptr + i*2);
00226
00227 while (ctx->mb_width - mb_x < slice_mb_count)
00228 slice_mb_count >>= 1;
00229
00230 slice->mb_x = mb_x;
00231 slice->mb_y = mb_y;
00232 slice->mb_count = slice_mb_count;
00233 slice->data_size = data_ptr - slice->data;
00234
00235 if (slice->data_size < 6) {
00236 av_log(avctx, AV_LOG_ERROR, "error, wrong slice data size\n");
00237 return -1;
00238 }
00239
00240 mb_x += slice_mb_count;
00241 if (mb_x == ctx->mb_width) {
00242 slice_mb_count = 1 << log2_slice_mb_width;
00243 mb_x = 0;
00244 mb_y++;
00245 }
00246 if (data_ptr > buf + buf_size) {
00247 av_log(avctx, AV_LOG_ERROR, "error, slice out of bounds\n");
00248 return -1;
00249 }
00250 }
00251
00252 if (mb_x || mb_y != ctx->mb_height) {
00253 av_log(avctx, AV_LOG_ERROR, "error wrong mb count y %d h %d\n",
00254 mb_y, ctx->mb_height);
00255 return -1;
00256 }
00257
00258 return pic_data_size;
00259 }
00260
00261 #define DECODE_CODEWORD(val, codebook) \
00262 do { \
00263 unsigned int rice_order, exp_order, switch_bits; \
00264 unsigned int q, buf, bits; \
00265 \
00266 UPDATE_CACHE(re, gb); \
00267 buf = GET_CACHE(re, gb); \
00268 \
00269 \
00270 switch_bits = codebook & 3; \
00271 rice_order = codebook >> 5; \
00272 exp_order = (codebook >> 2) & 7; \
00273 \
00274 q = 31 - av_log2(buf); \
00275 \
00276 if (q > switch_bits) { \
00277 bits = exp_order - switch_bits + (q<<1); \
00278 val = SHOW_UBITS(re, gb, bits) - (1 << exp_order) + \
00279 ((switch_bits + 1) << rice_order); \
00280 SKIP_BITS(re, gb, bits); \
00281 } else if (rice_order) { \
00282 SKIP_BITS(re, gb, q+1); \
00283 val = (q << rice_order) + SHOW_UBITS(re, gb, rice_order); \
00284 SKIP_BITS(re, gb, rice_order); \
00285 } else { \
00286 val = q; \
00287 SKIP_BITS(re, gb, q+1); \
00288 } \
00289 } while (0)
00290
00291 #define TOSIGNED(x) (((x) >> 1) ^ (-((x) & 1)))
00292
00293 #define FIRST_DC_CB 0xB8
00294
00295 static const uint8_t dc_codebook[7] = { 0x04, 0x28, 0x28, 0x4D, 0x4D, 0x70, 0x70};
00296
00297 static av_always_inline void decode_dc_coeffs(GetBitContext *gb, DCTELEM *out,
00298 int blocks_per_slice)
00299 {
00300 DCTELEM prev_dc;
00301 int code, i, sign;
00302
00303 OPEN_READER(re, gb);
00304
00305 DECODE_CODEWORD(code, FIRST_DC_CB);
00306 prev_dc = TOSIGNED(code);
00307 out[0] = prev_dc;
00308
00309 out += 64;
00310
00311 code = 5;
00312 sign = 0;
00313 for (i = 1; i < blocks_per_slice; i++, out += 64) {
00314 DECODE_CODEWORD(code, dc_codebook[FFMIN(code, 6U)]);
00315 if(code) sign ^= -(code & 1);
00316 else sign = 0;
00317 prev_dc += (((code + 1) >> 1) ^ sign) - sign;
00318 out[0] = prev_dc;
00319 }
00320 CLOSE_READER(re, gb);
00321 }
00322
00323
00324 static const uint8_t run_to_cb[16] = { 0x06, 0x06, 0x05, 0x05, 0x04, 0x29, 0x29, 0x29, 0x29, 0x28, 0x28, 0x28, 0x28, 0x28, 0x28, 0x4C };
00325 static const uint8_t lev_to_cb[10] = { 0x04, 0x0A, 0x05, 0x06, 0x04, 0x28, 0x28, 0x28, 0x28, 0x4C };
00326
00327 static av_always_inline void decode_ac_coeffs(AVCodecContext *avctx, GetBitContext *gb,
00328 DCTELEM *out, int blocks_per_slice)
00329 {
00330 ProresContext *ctx = avctx->priv_data;
00331 int block_mask, sign;
00332 unsigned pos, run, level;
00333 int max_coeffs, i, bits_left;
00334 int log2_block_count = av_log2(blocks_per_slice);
00335
00336 OPEN_READER(re, gb);
00337 UPDATE_CACHE(re, gb); \
00338 run = 4;
00339 level = 2;
00340
00341 max_coeffs = 64 << log2_block_count;
00342 block_mask = blocks_per_slice - 1;
00343
00344 for (pos = block_mask;;) {
00345 bits_left = gb->size_in_bits - re_index;
00346 if (!bits_left || (bits_left < 32 && !SHOW_UBITS(re, gb, bits_left)))
00347 break;
00348
00349 DECODE_CODEWORD(run, run_to_cb[FFMIN(run, 15)]);
00350 pos += run + 1;
00351 if (pos >= max_coeffs) {
00352 av_log(avctx, AV_LOG_ERROR, "ac tex damaged %d, %d\n", pos, max_coeffs);
00353 return;
00354 }
00355
00356 DECODE_CODEWORD(level, lev_to_cb[FFMIN(level, 9)]);
00357 level += 1;
00358
00359 i = pos >> log2_block_count;
00360
00361 sign = SHOW_SBITS(re, gb, 1);
00362 SKIP_BITS(re, gb, 1);
00363 out[((pos & block_mask) << 6) + ctx->scan[i]] = ((level ^ sign) - sign);
00364 }
00365
00366 CLOSE_READER(re, gb);
00367 }
00368
00369 static void decode_slice_luma(AVCodecContext *avctx, SliceContext *slice,
00370 uint16_t *dst, int dst_stride,
00371 const uint8_t *buf, unsigned buf_size,
00372 const int16_t *qmat)
00373 {
00374 ProresContext *ctx = avctx->priv_data;
00375 LOCAL_ALIGNED_16(DCTELEM, blocks, [8*4*64]);
00376 DCTELEM *block;
00377 GetBitContext gb;
00378 int i, blocks_per_slice = slice->mb_count<<2;
00379
00380 for (i = 0; i < blocks_per_slice; i++)
00381 ctx->dsp.clear_block(blocks+(i<<6));
00382
00383 init_get_bits(&gb, buf, buf_size << 3);
00384
00385 decode_dc_coeffs(&gb, blocks, blocks_per_slice);
00386 decode_ac_coeffs(avctx, &gb, blocks, blocks_per_slice);
00387
00388 block = blocks;
00389 for (i = 0; i < slice->mb_count; i++) {
00390 ctx->prodsp.idct_put(dst, dst_stride, block+(0<<6), qmat);
00391 ctx->prodsp.idct_put(dst +8, dst_stride, block+(1<<6), qmat);
00392 ctx->prodsp.idct_put(dst+4*dst_stride , dst_stride, block+(2<<6), qmat);
00393 ctx->prodsp.idct_put(dst+4*dst_stride+8, dst_stride, block+(3<<6), qmat);
00394 block += 4*64;
00395 dst += 16;
00396 }
00397 }
00398
00399 static void decode_slice_chroma(AVCodecContext *avctx, SliceContext *slice,
00400 uint16_t *dst, int dst_stride,
00401 const uint8_t *buf, unsigned buf_size,
00402 const int16_t *qmat, int log2_blocks_per_mb)
00403 {
00404 ProresContext *ctx = avctx->priv_data;
00405 LOCAL_ALIGNED_16(DCTELEM, blocks, [8*4*64]);
00406 DCTELEM *block;
00407 GetBitContext gb;
00408 int i, j, blocks_per_slice = slice->mb_count << log2_blocks_per_mb;
00409
00410 for (i = 0; i < blocks_per_slice; i++)
00411 ctx->dsp.clear_block(blocks+(i<<6));
00412
00413 init_get_bits(&gb, buf, buf_size << 3);
00414
00415 decode_dc_coeffs(&gb, blocks, blocks_per_slice);
00416 decode_ac_coeffs(avctx, &gb, blocks, blocks_per_slice);
00417
00418 block = blocks;
00419 for (i = 0; i < slice->mb_count; i++) {
00420 for (j = 0; j < log2_blocks_per_mb; j++) {
00421 ctx->prodsp.idct_put(dst, dst_stride, block+(0<<6), qmat);
00422 ctx->prodsp.idct_put(dst+4*dst_stride, dst_stride, block+(1<<6), qmat);
00423 block += 2*64;
00424 dst += 8;
00425 }
00426 }
00427 }
00428
00429 static int decode_slice_thread(AVCodecContext *avctx, void *arg, int jobnr, int threadnr)
00430 {
00431 ProresContext *ctx = avctx->priv_data;
00432 SliceContext *slice = &ctx->slices[jobnr];
00433 const uint8_t *buf = slice->data;
00434 AVFrame *pic = avctx->coded_frame;
00435 int i, hdr_size, qscale, log2_chroma_blocks_per_mb;
00436 int luma_stride, chroma_stride;
00437 int y_data_size, u_data_size, v_data_size;
00438 uint8_t *dest_y, *dest_u, *dest_v;
00439 int16_t qmat_luma_scaled[64];
00440 int16_t qmat_chroma_scaled[64];
00441 int mb_x_shift;
00442
00443 slice->ret = -1;
00444
00445
00446
00447
00448 hdr_size = buf[0] >> 3;
00449 qscale = av_clip(buf[1], 1, 224);
00450 qscale = qscale > 128 ? qscale - 96 << 2: qscale;
00451 y_data_size = AV_RB16(buf + 2);
00452 u_data_size = AV_RB16(buf + 4);
00453 v_data_size = slice->data_size - y_data_size - u_data_size - hdr_size;
00454 if (hdr_size > 7) v_data_size = AV_RB16(buf + 6);
00455
00456 if (y_data_size < 0 || u_data_size < 0 || v_data_size < 0
00457 || hdr_size+y_data_size+u_data_size+v_data_size > slice->data_size){
00458 av_log(avctx, AV_LOG_ERROR, "invalid plane data size\n");
00459 return -1;
00460 }
00461
00462 buf += hdr_size;
00463
00464 for (i = 0; i < 64; i++) {
00465 qmat_luma_scaled [i] = ctx->qmat_luma [i] * qscale;
00466 qmat_chroma_scaled[i] = ctx->qmat_chroma[i] * qscale;
00467 }
00468
00469 if (ctx->frame_type == 0) {
00470 luma_stride = pic->linesize[0];
00471 chroma_stride = pic->linesize[1];
00472 } else {
00473 luma_stride = pic->linesize[0] << 1;
00474 chroma_stride = pic->linesize[1] << 1;
00475 }
00476
00477 if (avctx->pix_fmt == AV_PIX_FMT_YUV444P10) {
00478 mb_x_shift = 5;
00479 log2_chroma_blocks_per_mb = 2;
00480 } else {
00481 mb_x_shift = 4;
00482 log2_chroma_blocks_per_mb = 1;
00483 }
00484
00485 dest_y = pic->data[0] + (slice->mb_y << 4) * luma_stride + (slice->mb_x << 5);
00486 dest_u = pic->data[1] + (slice->mb_y << 4) * chroma_stride + (slice->mb_x << mb_x_shift);
00487 dest_v = pic->data[2] + (slice->mb_y << 4) * chroma_stride + (slice->mb_x << mb_x_shift);
00488
00489 if (ctx->frame_type && ctx->first_field ^ ctx->frame.top_field_first) {
00490 dest_y += pic->linesize[0];
00491 dest_u += pic->linesize[1];
00492 dest_v += pic->linesize[2];
00493 }
00494
00495 decode_slice_luma(avctx, slice, (uint16_t*)dest_y, luma_stride,
00496 buf, y_data_size, qmat_luma_scaled);
00497
00498 if (!(avctx->flags & CODEC_FLAG_GRAY)) {
00499 decode_slice_chroma(avctx, slice, (uint16_t*)dest_u, chroma_stride,
00500 buf + y_data_size, u_data_size,
00501 qmat_chroma_scaled, log2_chroma_blocks_per_mb);
00502 decode_slice_chroma(avctx, slice, (uint16_t*)dest_v, chroma_stride,
00503 buf + y_data_size + u_data_size, v_data_size,
00504 qmat_chroma_scaled, log2_chroma_blocks_per_mb);
00505 }
00506
00507 slice->ret = 0;
00508 return 0;
00509 }
00510
00511 static int decode_picture(AVCodecContext *avctx)
00512 {
00513 ProresContext *ctx = avctx->priv_data;
00514 int i;
00515
00516 avctx->execute2(avctx, decode_slice_thread, NULL, NULL, ctx->slice_count);
00517
00518 for (i = 0; i < ctx->slice_count; i++)
00519 if (ctx->slices[i].ret < 0)
00520 return ctx->slices[i].ret;
00521
00522 return 0;
00523 }
00524
00525 static int decode_frame(AVCodecContext *avctx, void *data, int *got_frame,
00526 AVPacket *avpkt)
00527 {
00528 ProresContext *ctx = avctx->priv_data;
00529 AVFrame *frame = avctx->coded_frame;
00530 const uint8_t *buf = avpkt->data;
00531 int buf_size = avpkt->size;
00532 int frame_hdr_size, pic_size;
00533
00534 if (buf_size < 28 || AV_RL32(buf + 4) != AV_RL32("icpf")) {
00535 av_log(avctx, AV_LOG_ERROR, "invalid frame header\n");
00536 return -1;
00537 }
00538
00539 ctx->first_field = 1;
00540
00541 buf += 8;
00542 buf_size -= 8;
00543
00544 frame_hdr_size = decode_frame_header(ctx, buf, buf_size, avctx);
00545 if (frame_hdr_size < 0)
00546 return -1;
00547
00548 buf += frame_hdr_size;
00549 buf_size -= frame_hdr_size;
00550
00551 if (frame->data[0])
00552 avctx->release_buffer(avctx, frame);
00553
00554 if (ff_get_buffer(avctx, frame) < 0)
00555 return -1;
00556
00557 decode_picture:
00558 pic_size = decode_picture_header(avctx, buf, buf_size);
00559 if (pic_size < 0) {
00560 av_log(avctx, AV_LOG_ERROR, "error decoding picture header\n");
00561 return -1;
00562 }
00563
00564 if (decode_picture(avctx)) {
00565 av_log(avctx, AV_LOG_ERROR, "error decoding picture\n");
00566 return -1;
00567 }
00568
00569 buf += pic_size;
00570 buf_size -= pic_size;
00571
00572 if (ctx->frame_type && buf_size > 0 && ctx->first_field) {
00573 ctx->first_field = 0;
00574 goto decode_picture;
00575 }
00576
00577 *got_frame = 1;
00578 *(AVFrame*)data = *frame;
00579
00580 return avpkt->size;
00581 }
00582
00583 static av_cold int decode_close(AVCodecContext *avctx)
00584 {
00585 ProresContext *ctx = avctx->priv_data;
00586
00587 AVFrame *frame = avctx->coded_frame;
00588 if (frame->data[0])
00589 avctx->release_buffer(avctx, frame);
00590 av_freep(&ctx->slices);
00591
00592 return 0;
00593 }
00594
00595 AVCodec ff_prores_decoder = {
00596 .name = "prores",
00597 .type = AVMEDIA_TYPE_VIDEO,
00598 .id = AV_CODEC_ID_PRORES,
00599 .priv_data_size = sizeof(ProresContext),
00600 .init = decode_init,
00601 .close = decode_close,
00602 .decode = decode_frame,
00603 .long_name = NULL_IF_CONFIG_SMALL("ProRes"),
00604 .capabilities = CODEC_CAP_DR1 | CODEC_CAP_SLICE_THREADS,
00605 };