]> git.sesse.net Git - ffmpeg/blob - libavcodec/proresdec2.c
Merge commit '565e4993c63f797e2d50ad2f1e8f62fdbe299666'
[ffmpeg] / libavcodec / proresdec2.c
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), 'acpo' (Proxy), 'ap4h' (4444)
25  */
26
27 //#define DEBUG
28
29 #define LONG_BITSTREAM_READER
30
31 #include "avcodec.h"
32 #include "get_bits.h"
33 #include "simple_idct.h"
34 #include "proresdec.h"
35
36 static void permute(uint8_t *dst, const uint8_t *src, const uint8_t permutation[64])
37 {
38     int i;
39     for (i = 0; i < 64; i++)
40         dst[i] = permutation[src[i]];
41 }
42
43 static const uint8_t progressive_scan[64] = {
44      0,  1,  8,  9,  2,  3, 10, 11,
45     16, 17, 24, 25, 18, 19, 26, 27,
46      4,  5, 12, 20, 13,  6,  7, 14,
47     21, 28, 29, 22, 15, 23, 30, 31,
48     32, 33, 40, 48, 41, 34, 35, 42,
49     49, 56, 57, 50, 43, 36, 37, 44,
50     51, 58, 59, 52, 45, 38, 39, 46,
51     53, 60, 61, 54, 47, 55, 62, 63
52 };
53
54 static const uint8_t interlaced_scan[64] = {
55      0,  8,  1,  9, 16, 24, 17, 25,
56      2, 10,  3, 11, 18, 26, 19, 27,
57     32, 40, 33, 34, 41, 48, 56, 49,
58     42, 35, 43, 50, 57, 58, 51, 59,
59      4, 12,  5,  6, 13, 20, 28, 21,
60     14,  7, 15, 22, 29, 36, 44, 37,
61     30, 23, 31, 38, 45, 52, 60, 53,
62     46, 39, 47, 54, 61, 62, 55, 63,
63 };
64
65 static av_cold int decode_init(AVCodecContext *avctx)
66 {
67     ProresContext *ctx = avctx->priv_data;
68     uint8_t idct_permutation[64];
69
70     avctx->bits_per_raw_sample = 10;
71
72     ff_dsputil_init(&ctx->dsp, avctx);
73     ff_proresdsp_init(&ctx->prodsp, avctx);
74
75     avctx->coded_frame = &ctx->frame;
76     ctx->frame.type = AV_PICTURE_TYPE_I;
77     ctx->frame.key_frame = 1;
78
79     ff_init_scantable_permutation(idct_permutation,
80                                   ctx->prodsp.idct_permutation_type);
81
82     permute(ctx->progressive_scan, progressive_scan, idct_permutation);
83     permute(ctx->interlaced_scan, interlaced_scan, idct_permutation);
84
85     return 0;
86 }
87
88 static int decode_frame_header(ProresContext *ctx, const uint8_t *buf,
89                                const int data_size, AVCodecContext *avctx)
90 {
91     int hdr_size, width, height, flags;
92     int version;
93     const uint8_t *ptr;
94
95     hdr_size = AV_RB16(buf);
96     av_dlog(avctx, "header size %d\n", hdr_size);
97     if (hdr_size > data_size) {
98         av_log(avctx, AV_LOG_ERROR, "error, wrong header size\n");
99         return -1;
100     }
101
102     version = AV_RB16(buf + 2);
103     av_dlog(avctx, "%.4s version %d\n", buf+4, version);
104     if (version > 1) {
105         av_log(avctx, AV_LOG_ERROR, "unsupported version: %d\n", version);
106         return -1;
107     }
108
109     width  = AV_RB16(buf + 8);
110     height = AV_RB16(buf + 10);
111     if (width != avctx->width || height != avctx->height) {
112         av_log(avctx, AV_LOG_ERROR, "picture resolution change: %dx%d -> %dx%d\n",
113                avctx->width, avctx->height, width, height);
114         return -1;
115     }
116
117     ctx->frame_type = (buf[12] >> 2) & 3;
118
119     av_dlog(avctx, "frame type %d\n", ctx->frame_type);
120
121     if (ctx->frame_type == 0) {
122         ctx->scan = ctx->progressive_scan; // permuted
123     } else {
124         ctx->scan = ctx->interlaced_scan; // permuted
125         ctx->frame.interlaced_frame = 1;
126         ctx->frame.top_field_first = ctx->frame_type == 1;
127     }
128
129     avctx->pix_fmt = (buf[12] & 0xC0) == 0xC0 ? AV_PIX_FMT_YUV444P10 : AV_PIX_FMT_YUV422P10;
130
131     ptr   = buf + 20;
132     flags = buf[19];
133     av_dlog(avctx, "flags %x\n", flags);
134
135     if (flags & 2) {
136         if(buf + data_size - ptr < 64) {
137             av_log(avctx, AV_LOG_ERROR, "Header truncated\n");
138             return -1;
139         }
140         permute(ctx->qmat_luma, ctx->prodsp.idct_permutation, ptr);
141         ptr += 64;
142     } else {
143         memset(ctx->qmat_luma, 4, 64);
144     }
145
146     if (flags & 1) {
147         if(buf + data_size - ptr < 64) {
148             av_log(avctx, AV_LOG_ERROR, "Header truncated\n");
149             return -1;
150         }
151         permute(ctx->qmat_chroma, ctx->prodsp.idct_permutation, ptr);
152     } else {
153         memset(ctx->qmat_chroma, 4, 64);
154     }
155
156     return hdr_size;
157 }
158
159 static int decode_picture_header(AVCodecContext *avctx, const uint8_t *buf, const int buf_size)
160 {
161     ProresContext *ctx = avctx->priv_data;
162     int i, hdr_size, slice_count;
163     unsigned pic_data_size;
164     int log2_slice_mb_width, log2_slice_mb_height;
165     int slice_mb_count, mb_x, mb_y;
166     const uint8_t *data_ptr, *index_ptr;
167
168     hdr_size = buf[0] >> 3;
169     if (hdr_size < 8 || hdr_size > buf_size) {
170         av_log(avctx, AV_LOG_ERROR, "error, wrong picture header size\n");
171         return -1;
172     }
173
174     pic_data_size = AV_RB32(buf + 1);
175     if (pic_data_size > buf_size) {
176         av_log(avctx, AV_LOG_ERROR, "error, wrong picture data size\n");
177         return -1;
178     }
179
180     log2_slice_mb_width  = buf[7] >> 4;
181     log2_slice_mb_height = buf[7] & 0xF;
182     if (log2_slice_mb_width > 3 || log2_slice_mb_height) {
183         av_log(avctx, AV_LOG_ERROR, "unsupported slice resolution: %dx%d\n",
184                1 << log2_slice_mb_width, 1 << log2_slice_mb_height);
185         return -1;
186     }
187
188     ctx->mb_width  = (avctx->width  + 15) >> 4;
189     if (ctx->frame_type)
190         ctx->mb_height = (avctx->height + 31) >> 5;
191     else
192         ctx->mb_height = (avctx->height + 15) >> 4;
193
194     slice_count = AV_RB16(buf + 5);
195
196     if (ctx->slice_count != slice_count || !ctx->slices) {
197         av_freep(&ctx->slices);
198         ctx->slices = av_mallocz(slice_count * sizeof(*ctx->slices));
199         if (!ctx->slices)
200             return AVERROR(ENOMEM);
201         ctx->slice_count = slice_count;
202     }
203
204     if (!slice_count)
205         return AVERROR(EINVAL);
206
207     if (hdr_size + slice_count*2 > buf_size) {
208         av_log(avctx, AV_LOG_ERROR, "error, wrong slice count\n");
209         return -1;
210     }
211
212     // parse slice information
213     index_ptr = buf + hdr_size;
214     data_ptr  = index_ptr + slice_count*2;
215
216     slice_mb_count = 1 << log2_slice_mb_width;
217     mb_x = 0;
218     mb_y = 0;
219
220     for (i = 0; i < slice_count; i++) {
221         SliceContext *slice = &ctx->slices[i];
222
223         slice->data = data_ptr;
224         data_ptr += AV_RB16(index_ptr + i*2);
225
226         while (ctx->mb_width - mb_x < slice_mb_count)
227             slice_mb_count >>= 1;
228
229         slice->mb_x = mb_x;
230         slice->mb_y = mb_y;
231         slice->mb_count = slice_mb_count;
232         slice->data_size = data_ptr - slice->data;
233
234         if (slice->data_size < 6) {
235             av_log(avctx, AV_LOG_ERROR, "error, wrong slice data size\n");
236             return -1;
237         }
238
239         mb_x += slice_mb_count;
240         if (mb_x == ctx->mb_width) {
241             slice_mb_count = 1 << log2_slice_mb_width;
242             mb_x = 0;
243             mb_y++;
244         }
245         if (data_ptr > buf + buf_size) {
246             av_log(avctx, AV_LOG_ERROR, "error, slice out of bounds\n");
247             return -1;
248         }
249     }
250
251     if (mb_x || mb_y != ctx->mb_height) {
252         av_log(avctx, AV_LOG_ERROR, "error wrong mb count y %d h %d\n",
253                mb_y, ctx->mb_height);
254         return -1;
255     }
256
257     return pic_data_size;
258 }
259
260 #define DECODE_CODEWORD(val, codebook)                                  \
261     do {                                                                \
262         unsigned int rice_order, exp_order, switch_bits;                \
263         unsigned int q, buf, bits;                                      \
264                                                                         \
265         UPDATE_CACHE(re, gb);                                           \
266         buf = GET_CACHE(re, gb);                                        \
267                                                                         \
268         /* number of bits to switch between rice and exp golomb */      \
269         switch_bits =  codebook & 3;                                    \
270         rice_order  =  codebook >> 5;                                   \
271         exp_order   = (codebook >> 2) & 7;                              \
272                                                                         \
273         q = 31 - av_log2(buf);                                          \
274                                                                         \
275         if (q > switch_bits) { /* exp golomb */                         \
276             bits = exp_order - switch_bits + (q<<1);                    \
277             val = SHOW_UBITS(re, gb, bits) - (1 << exp_order) +         \
278                 ((switch_bits + 1) << rice_order);                      \
279             SKIP_BITS(re, gb, bits);                                    \
280         } else if (rice_order) {                                        \
281             SKIP_BITS(re, gb, q+1);                                     \
282             val = (q << rice_order) + SHOW_UBITS(re, gb, rice_order);   \
283             SKIP_BITS(re, gb, rice_order);                              \
284         } else {                                                        \
285             val = q;                                                    \
286             SKIP_BITS(re, gb, q+1);                                     \
287         }                                                               \
288     } while (0)
289
290 #define TOSIGNED(x) (((x) >> 1) ^ (-((x) & 1)))
291
292 #define FIRST_DC_CB 0xB8
293
294 static const uint8_t dc_codebook[7] = { 0x04, 0x28, 0x28, 0x4D, 0x4D, 0x70, 0x70};
295
296 static av_always_inline void decode_dc_coeffs(GetBitContext *gb, DCTELEM *out,
297                                               int blocks_per_slice)
298 {
299     DCTELEM prev_dc;
300     int code, i, sign;
301
302     OPEN_READER(re, gb);
303
304     DECODE_CODEWORD(code, FIRST_DC_CB);
305     prev_dc = TOSIGNED(code);
306     out[0] = prev_dc;
307
308     out += 64; // dc coeff for the next block
309
310     code = 5;
311     sign = 0;
312     for (i = 1; i < blocks_per_slice; i++, out += 64) {
313         DECODE_CODEWORD(code, dc_codebook[FFMIN(code, 6U)]);
314         if(code) sign ^= -(code & 1);
315         else     sign  = 0;
316         prev_dc += (((code + 1) >> 1) ^ sign) - sign;
317         out[0] = prev_dc;
318     }
319     CLOSE_READER(re, gb);
320 }
321
322 // adaptive codebook switching lut according to previous run/level values
323 static const uint8_t run_to_cb[16] = { 0x06, 0x06, 0x05, 0x05, 0x04, 0x29, 0x29, 0x29, 0x29, 0x28, 0x28, 0x28, 0x28, 0x28, 0x28, 0x4C };
324 static const uint8_t lev_to_cb[10] = { 0x04, 0x0A, 0x05, 0x06, 0x04, 0x28, 0x28, 0x28, 0x28, 0x4C };
325
326 static av_always_inline void decode_ac_coeffs(AVCodecContext *avctx, GetBitContext *gb,
327                                               DCTELEM *out, int blocks_per_slice)
328 {
329     ProresContext *ctx = avctx->priv_data;
330     int block_mask, sign;
331     unsigned pos, run, level;
332     int max_coeffs, i, bits_left;
333     int log2_block_count = av_log2(blocks_per_slice);
334
335     OPEN_READER(re, gb);
336     UPDATE_CACHE(re, gb);                                           \
337     run   = 4;
338     level = 2;
339
340     max_coeffs = 64 << log2_block_count;
341     block_mask = blocks_per_slice - 1;
342
343     for (pos = block_mask;;) {
344         bits_left = gb->size_in_bits - re_index;
345         if (!bits_left || (bits_left < 32 && !SHOW_UBITS(re, gb, bits_left)))
346             break;
347
348         DECODE_CODEWORD(run, run_to_cb[FFMIN(run,  15)]);
349         pos += run + 1;
350         if (pos >= max_coeffs) {
351             av_log(avctx, AV_LOG_ERROR, "ac tex damaged %d, %d\n", pos, max_coeffs);
352             return;
353         }
354
355         DECODE_CODEWORD(level, lev_to_cb[FFMIN(level, 9)]);
356         level += 1;
357
358         i = pos >> log2_block_count;
359
360         sign = SHOW_SBITS(re, gb, 1);
361         SKIP_BITS(re, gb, 1);
362         out[((pos & block_mask) << 6) + ctx->scan[i]] = ((level ^ sign) - sign);
363     }
364
365     CLOSE_READER(re, gb);
366 }
367
368 static void decode_slice_luma(AVCodecContext *avctx, SliceContext *slice,
369                               uint16_t *dst, int dst_stride,
370                               const uint8_t *buf, unsigned buf_size,
371                               const int16_t *qmat)
372 {
373     ProresContext *ctx = avctx->priv_data;
374     LOCAL_ALIGNED_16(DCTELEM, blocks, [8*4*64]);
375     DCTELEM *block;
376     GetBitContext gb;
377     int i, blocks_per_slice = slice->mb_count<<2;
378
379     for (i = 0; i < blocks_per_slice; i++)
380         ctx->dsp.clear_block(blocks+(i<<6));
381
382     init_get_bits(&gb, buf, buf_size << 3);
383
384     decode_dc_coeffs(&gb, blocks, blocks_per_slice);
385     decode_ac_coeffs(avctx, &gb, blocks, blocks_per_slice);
386
387     block = blocks;
388     for (i = 0; i < slice->mb_count; i++) {
389         ctx->prodsp.idct_put(dst, dst_stride, block+(0<<6), qmat);
390         ctx->prodsp.idct_put(dst             +8, dst_stride, block+(1<<6), qmat);
391         ctx->prodsp.idct_put(dst+4*dst_stride  , dst_stride, block+(2<<6), qmat);
392         ctx->prodsp.idct_put(dst+4*dst_stride+8, dst_stride, block+(3<<6), qmat);
393         block += 4*64;
394         dst += 16;
395     }
396 }
397
398 static void decode_slice_chroma(AVCodecContext *avctx, SliceContext *slice,
399                                 uint16_t *dst, int dst_stride,
400                                 const uint8_t *buf, unsigned buf_size,
401                                 const int16_t *qmat, int log2_blocks_per_mb)
402 {
403     ProresContext *ctx = avctx->priv_data;
404     LOCAL_ALIGNED_16(DCTELEM, blocks, [8*4*64]);
405     DCTELEM *block;
406     GetBitContext gb;
407     int i, j, blocks_per_slice = slice->mb_count << log2_blocks_per_mb;
408
409     for (i = 0; i < blocks_per_slice; i++)
410         ctx->dsp.clear_block(blocks+(i<<6));
411
412     init_get_bits(&gb, buf, buf_size << 3);
413
414     decode_dc_coeffs(&gb, blocks, blocks_per_slice);
415     decode_ac_coeffs(avctx, &gb, blocks, blocks_per_slice);
416
417     block = blocks;
418     for (i = 0; i < slice->mb_count; i++) {
419         for (j = 0; j < log2_blocks_per_mb; j++) {
420             ctx->prodsp.idct_put(dst,              dst_stride, block+(0<<6), qmat);
421             ctx->prodsp.idct_put(dst+4*dst_stride, dst_stride, block+(1<<6), qmat);
422             block += 2*64;
423             dst += 8;
424         }
425     }
426 }
427
428 static int decode_slice_thread(AVCodecContext *avctx, void *arg, int jobnr, int threadnr)
429 {
430     ProresContext *ctx = avctx->priv_data;
431     SliceContext *slice = &ctx->slices[jobnr];
432     const uint8_t *buf = slice->data;
433     AVFrame *pic = avctx->coded_frame;
434     int i, hdr_size, qscale, log2_chroma_blocks_per_mb;
435     int luma_stride, chroma_stride;
436     int y_data_size, u_data_size, v_data_size;
437     uint8_t *dest_y, *dest_u, *dest_v;
438     int16_t qmat_luma_scaled[64];
439     int16_t qmat_chroma_scaled[64];
440     int mb_x_shift;
441
442     slice->ret = -1;
443     //av_log(avctx, AV_LOG_INFO, "slice %d mb width %d mb x %d y %d\n",
444     //       jobnr, slice->mb_count, slice->mb_x, slice->mb_y);
445
446     // slice header
447     hdr_size = buf[0] >> 3;
448     qscale = av_clip(buf[1], 1, 224);
449     qscale = qscale > 128 ? qscale - 96 << 2: qscale;
450     y_data_size = AV_RB16(buf + 2);
451     u_data_size = AV_RB16(buf + 4);
452     v_data_size = slice->data_size - y_data_size - u_data_size - hdr_size;
453     if (hdr_size > 7) v_data_size = AV_RB16(buf + 6);
454
455     if (y_data_size < 0 || u_data_size < 0 || v_data_size < 0
456         || hdr_size+y_data_size+u_data_size+v_data_size > slice->data_size){
457         av_log(avctx, AV_LOG_ERROR, "invalid plane data size\n");
458         return -1;
459     }
460
461     buf += hdr_size;
462
463     for (i = 0; i < 64; i++) {
464         qmat_luma_scaled  [i] = ctx->qmat_luma  [i] * qscale;
465         qmat_chroma_scaled[i] = ctx->qmat_chroma[i] * qscale;
466     }
467
468     if (ctx->frame_type == 0) {
469         luma_stride   = pic->linesize[0];
470         chroma_stride = pic->linesize[1];
471     } else {
472         luma_stride   = pic->linesize[0] << 1;
473         chroma_stride = pic->linesize[1] << 1;
474     }
475
476     if (avctx->pix_fmt == AV_PIX_FMT_YUV444P10) {
477         mb_x_shift = 5;
478         log2_chroma_blocks_per_mb = 2;
479     } else {
480         mb_x_shift = 4;
481         log2_chroma_blocks_per_mb = 1;
482     }
483
484     dest_y = pic->data[0] + (slice->mb_y << 4) * luma_stride + (slice->mb_x << 5);
485     dest_u = pic->data[1] + (slice->mb_y << 4) * chroma_stride + (slice->mb_x << mb_x_shift);
486     dest_v = pic->data[2] + (slice->mb_y << 4) * chroma_stride + (slice->mb_x << mb_x_shift);
487
488     if (ctx->frame_type && ctx->first_field ^ ctx->frame.top_field_first) {
489         dest_y += pic->linesize[0];
490         dest_u += pic->linesize[1];
491         dest_v += pic->linesize[2];
492     }
493
494     decode_slice_luma(avctx, slice, (uint16_t*)dest_y, luma_stride,
495                       buf, y_data_size, qmat_luma_scaled);
496
497     if (!(avctx->flags & CODEC_FLAG_GRAY)) {
498         decode_slice_chroma(avctx, slice, (uint16_t*)dest_u, chroma_stride,
499                             buf + y_data_size, u_data_size,
500                             qmat_chroma_scaled, log2_chroma_blocks_per_mb);
501         decode_slice_chroma(avctx, slice, (uint16_t*)dest_v, chroma_stride,
502                             buf + y_data_size + u_data_size, v_data_size,
503                             qmat_chroma_scaled, log2_chroma_blocks_per_mb);
504     }
505
506     slice->ret = 0;
507     return 0;
508 }
509
510 static int decode_picture(AVCodecContext *avctx)
511 {
512     ProresContext *ctx = avctx->priv_data;
513     int i;
514
515     avctx->execute2(avctx, decode_slice_thread, NULL, NULL, ctx->slice_count);
516
517     for (i = 0; i < ctx->slice_count; i++)
518         if (ctx->slices[i].ret < 0)
519             return ctx->slices[i].ret;
520
521     return 0;
522 }
523
524 static int decode_frame(AVCodecContext *avctx, void *data, int *data_size,
525                         AVPacket *avpkt)
526 {
527     ProresContext *ctx = avctx->priv_data;
528     AVFrame *frame = avctx->coded_frame;
529     const uint8_t *buf = avpkt->data;
530     int buf_size = avpkt->size;
531     int frame_hdr_size, pic_size;
532
533     if (buf_size < 28 || AV_RL32(buf + 4) != AV_RL32("icpf")) {
534         av_log(avctx, AV_LOG_ERROR, "invalid frame header\n");
535         return -1;
536     }
537
538     ctx->first_field = 1;
539
540     buf += 8;
541     buf_size -= 8;
542
543     frame_hdr_size = decode_frame_header(ctx, buf, buf_size, avctx);
544     if (frame_hdr_size < 0)
545         return -1;
546
547     buf += frame_hdr_size;
548     buf_size -= frame_hdr_size;
549
550     if (frame->data[0])
551         avctx->release_buffer(avctx, frame);
552
553     if (avctx->get_buffer(avctx, frame) < 0)
554         return -1;
555
556  decode_picture:
557     pic_size = decode_picture_header(avctx, buf, buf_size);
558     if (pic_size < 0) {
559         av_log(avctx, AV_LOG_ERROR, "error decoding picture header\n");
560         return -1;
561     }
562
563     if (decode_picture(avctx)) {
564         av_log(avctx, AV_LOG_ERROR, "error decoding picture\n");
565         return -1;
566     }
567
568     buf += pic_size;
569     buf_size -= pic_size;
570
571     if (ctx->frame_type && buf_size > 0 && ctx->first_field) {
572         ctx->first_field = 0;
573         goto decode_picture;
574     }
575
576     *data_size = sizeof(AVFrame);
577     *(AVFrame*)data = *frame;
578
579     return avpkt->size;
580 }
581
582 static av_cold int decode_close(AVCodecContext *avctx)
583 {
584     ProresContext *ctx = avctx->priv_data;
585
586     AVFrame *frame = avctx->coded_frame;
587     if (frame->data[0])
588         avctx->release_buffer(avctx, frame);
589     av_freep(&ctx->slices);
590
591     return 0;
592 }
593
594 AVCodec ff_prores_decoder = {
595     .name           = "prores",
596     .type           = AVMEDIA_TYPE_VIDEO,
597     .id             = AV_CODEC_ID_PRORES,
598     .priv_data_size = sizeof(ProresContext),
599     .init           = decode_init,
600     .close          = decode_close,
601     .decode         = decode_frame,
602     .long_name      = NULL_IF_CONFIG_SMALL("ProRes"),
603     .capabilities   = CODEC_CAP_DR1 | CODEC_CAP_SLICE_THREADS,
604 };