]> git.sesse.net Git - ffmpeg/blob - libavcodec/proresdec.c
lavc: add CODEC_FLAG2_SHOW_ALL
[ffmpeg] / libavcodec / proresdec.c
1 /*
2  * This file is part of FFmpeg.
3  *
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.
8  *
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.
13  *
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
17  */
18
19 /**
20  * @file libavcodec/proresdec.c
21  * Known FOURCCs: 'apch' (HQ), 'apcn' (SD), 'apcs' (LT), 'acpo' (Proxy), 'ap4c' (4444)
22  */
23
24 //#define DEBUG
25
26 #define A32_BITSTREAM_READER
27
28 #include "avcodec.h"
29 #include "get_bits.h"
30 #include "dsputil.h"
31 #include "simple_idct.h"
32
33 typedef struct {
34     const uint8_t *data;
35     unsigned mb_x;
36     unsigned mb_y;
37     unsigned mb_count;
38     unsigned data_size;
39 } SliceContext;
40
41 typedef struct {
42     AVFrame frame;
43     DSPContext dsp;
44     int frame_type;              ///< 0 = progressive, 1 = tff, 2 = bff
45     uint8_t qmat_luma[64];
46     uint8_t qmat_chroma[64];
47     SliceContext *slices;
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];
53     const uint8_t *scan;
54     int first_field;
55     void (*idct_put)(DCTELEM *, uint8_t *restrict, int);
56 } ProresContext;
57
58 static void permute(uint8_t *dst, const uint8_t *src, const uint8_t permutation[64])
59 {
60     int i;
61     for (i = 0; i < 64; i++)
62         dst[i] = permutation[src[i]];
63 }
64
65 static av_always_inline void put_pixels(const DCTELEM *block, uint8_t *restrict pixels, int stride)
66 {
67     int16_t *p = (int16_t*)pixels;
68     int i, j;
69
70     stride >>= 1;
71     for(i = 0; i < 8; i++) {
72         for (j = 0; j < 8; j++) {
73             p[j] = av_clip(block[j], 4, 1019);
74         }
75         p += stride;
76         block += 8;
77     }
78 }
79
80 static void idct_put(DCTELEM *block, uint8_t *restrict pixels, int stride)
81 {
82     ff_simple_idct_10(block);
83     put_pixels(block, pixels, stride);
84 }
85
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
95 };
96
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,
106 };
107
108 static av_cold int decode_init(AVCodecContext *avctx)
109 {
110     ProresContext *ctx = avctx->priv_data;
111
112     avctx->bits_per_raw_sample = 10;
113
114     dsputil_init(&ctx->dsp, avctx);
115
116     avctx->coded_frame = &ctx->frame;
117     ctx->frame.type = FF_I_TYPE;
118     ctx->frame.key_frame = 1;
119
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));
123
124     return 0;
125 }
126
127 static int decode_frame_header(ProresContext *ctx, const uint8_t *buf,
128                                const int data_size, AVCodecContext *avctx)
129 {
130     int hdr_size, width, height, flags;
131     int version;
132     const uint8_t *ptr;
133     const uint8_t *scan;
134
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");
139         return -1;
140     }
141
142     version = AV_RB16(buf + 2);
143     av_dlog(avctx, "%.4s version %d\n", buf+4, version);
144     if (version != 0) {
145         av_log(avctx, AV_LOG_ERROR, "unsupported version: %d\n", version);
146         return -1;
147     }
148
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);
154         return -1;
155     }
156
157     ctx->frame_type = (buf[12] >> 2) & 3;
158
159     av_dlog(avctx, "frame type %d\n", ctx->frame_type);
160
161     if (ctx->frame_type == 0) {
162         scan = progressive_scan;
163         ctx->scan = ctx->progressive_scan; // permuted
164     } else {
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;
169     }
170
171     avctx->pix_fmt = PIX_FMT_YUV422P10;
172
173     ptr   = buf + 20;
174     flags = buf[19];
175     av_dlog(avctx, "flags %x\n", flags);
176
177     if (flags & 2) {
178         permute(ctx->qmat_luma, scan, ptr);
179         ptr += 64;
180     } else {
181         memset(ctx->qmat_luma, 4, 64);
182     }
183
184     if (flags & 1) {
185         permute(ctx->qmat_chroma, scan, ptr);
186     } else {
187         memset(ctx->qmat_chroma, 4, 64);
188     }
189
190     return hdr_size;
191 }
192
193 static int decode_picture_header(AVCodecContext *avctx, const uint8_t *buf, const int buf_size)
194 {
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;
201
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");
205         return -1;
206     }
207
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");
211         return -1;
212     }
213
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);
219         return -1;
220     }
221
222     ctx->mb_width  = (avctx->width  + 15) >> 4;
223     ctx->mb_height = (avctx->height + 15) >> 4;
224
225     slice_count = AV_RB16(buf + 5);
226
227     if (ctx->slice_count != slice_count || !ctx->slices) {
228         av_freep(&ctx->slices);
229         ctx->slices = av_mallocz(slice_count * sizeof(*ctx->slices));
230         if (!ctx->slices)
231             return AVERROR(ENOMEM);
232         ctx->slice_count = slice_count;
233     }
234
235     if (!slice_count)
236         return AVERROR(EINVAL);
237
238     if (hdr_size + slice_count*2 > buf_size) {
239         av_log(avctx, AV_LOG_ERROR, "error, wrong slice count\n");
240         return -1;
241     }
242
243     // parse slice information
244     index_ptr = buf + hdr_size;
245     data_ptr  = index_ptr + slice_count*2;
246
247     slice_mb_count = 1 << log2_slice_mb_width;
248     mb_x = 0;
249     mb_y = 0;
250
251     for (i = 0; i < slice_count; i++) {
252         SliceContext *slice = &ctx->slices[i];
253
254         slice->data = data_ptr;
255         data_ptr += AV_RB16(index_ptr + i*2);
256
257         while (ctx->mb_width - mb_x < slice_mb_count)
258             slice_mb_count >>= 1;
259
260         slice->mb_x = mb_x;
261         slice->mb_y = mb_y;
262         slice->mb_count = slice_mb_count;
263         slice->data_size = data_ptr - slice->data;
264
265         if (slice->data_size < 6) {
266             av_log(avctx, AV_LOG_ERROR, "error, wrong slice data size\n");
267             return -1;
268         }
269
270         mb_x += slice_mb_count;
271         if (mb_x == ctx->mb_width) {
272             slice_mb_count = 1 << log2_slice_mb_width;
273             mb_x = 0;
274             mb_y++;
275         }
276         if (data_ptr > buf + buf_size) {
277             av_log(avctx, AV_LOG_ERROR, "error, slice out of bounds\n");
278             return -1;
279         }
280     }
281
282     return pic_data_size;
283 }
284
285 #define DECODE_CODEWORD(val, codebook)                                  \
286     do {                                                                \
287         unsigned int rice_order, exp_order, switch_bits;                \
288         unsigned int q, buf, bits;                                      \
289                                                                         \
290         UPDATE_CACHE(re, gb);                                           \
291         buf = GET_CACHE(re, gb);                                        \
292                                                                         \
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;                              \
297                                                                         \
298         q = 31-av_log2(buf);                                         \
299                                                                         \
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);                              \
309         } else {                                                        \
310             val = q;                                                    \
311             SKIP_BITS(re, gb, q+1);                                     \
312         }                                                               \
313     } while (0);                                                        \
314
315 #define TOSIGNED(x) (((x) >> 1) ^ (-((x) & 1)))
316
317 #define FIRST_DC_CB 0xB8
318
319 static const uint8_t dc_codebook[7] = { 0x04, 0x28, 0x28, 0x4D, 0x4D, 0x70, 0x70};
320
321 static av_always_inline void decode_dc_coeffs(GetBitContext *gb, DCTELEM *out,
322                                               int blocks_per_slice, const int *qmat)
323 {
324     DCTELEM prev_dc;
325     int code, code2, i, sign;
326
327     OPEN_READER(re, gb);
328
329     DECODE_CODEWORD(code, FIRST_DC_CB);
330     prev_dc = TOSIGNED(code);
331     out[0] = 4096 + ((prev_dc * qmat[0]) >> 2);
332
333     out += 64; // dc coeff for the next block
334
335     code = 5;
336     sign = 0;
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);
340         else     sign  = 0;
341         prev_dc += (((code + 1) >> 1) ^ sign) - sign;
342         out[0] = 4096 + ((prev_dc * qmat[0]) >> 2);
343     }
344     CLOSE_READER(re, gb);
345 }
346
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 };
350
351 static av_always_inline void decode_ac_coeffs(AVCodecContext *avctx, GetBitContext *gb,
352                                               DCTELEM *out, int blocks_per_slice,
353                                               const int *qmat)
354 {
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);
360
361     OPEN_READER(re, gb);
362
363     run   = 4;
364     level = 2;
365
366     max_coeffs = 64 << log2_block_count;
367     block_mask = blocks_per_slice - 1;
368
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)))
372             break;
373
374         DECODE_CODEWORD(run, run_to_cb[FFMIN(run,  15)]);
375         pos += run + 1;
376         if (pos >= max_coeffs) {
377             av_log(avctx, AV_LOG_ERROR, "ac tex damaged %d, %d\n", pos, max_coeffs);
378             return;
379         }
380
381         DECODE_CODEWORD(level, lev_to_cb[FFMIN(level, 9)]);
382         level += 1;
383
384         i = pos >> log2_block_count;
385
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;
389     }
390
391     CLOSE_READER(re, gb);
392 }
393
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,
397                               const int *qmat)
398 {
399     ProresContext *ctx = avctx->priv_data;
400     DECLARE_ALIGNED(16, DCTELEM, blocks)[8*4*64], *block;
401     GetBitContext gb;
402     int i, blocks_per_slice = slice->mb_count<<2;
403
404     for (i = 0; i < blocks_per_slice; i++)
405         ctx->dsp.clear_block(blocks+(i<<6));
406
407     init_get_bits(&gb, buf, buf_size << 3);
408
409     decode_dc_coeffs(&gb, blocks, blocks_per_slice, qmat);
410     decode_ac_coeffs(avctx, &gb, blocks, blocks_per_slice, qmat);
411
412     block = blocks;
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);
418         block += 4*64;
419         dst += 32;
420     }
421 }
422
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,
426                                 const int *qmat)
427 {
428     ProresContext *ctx = avctx->priv_data;
429     DECLARE_ALIGNED(16, DCTELEM, blocks)[8*4*64], *block;
430     GetBitContext gb;
431     int i, blocks_per_slice = slice->mb_count*2;
432
433     for (i = 0; i < blocks_per_slice; i++)
434         ctx->dsp.clear_block(blocks+(i<<6));
435
436     init_get_bits(&gb, buf, buf_size << 3);
437
438     decode_dc_coeffs(&gb, blocks, blocks_per_slice, qmat);
439     decode_ac_coeffs(avctx, &gb, blocks, blocks_per_slice, qmat);
440
441     block = blocks;
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);
445         block += 2*64;
446         dst += 16;
447     }
448 }
449
450 static int decode_slice_thread(AVCodecContext *avctx, void *arg, int jobnr, int threadnr)
451 {
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];
462
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);
465
466     // slice header
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;
473
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");
476         return -1;
477     }
478
479     buf += hdr_size;
480
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;
484     }
485
486     if (ctx->frame_type == 0) {
487         luma_stride   = pic->linesize[0];
488         chroma_stride = pic->linesize[1];
489     } else {
490         luma_stride   = pic->linesize[0] << 1;
491         chroma_stride = pic->linesize[1] << 1;
492     }
493
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);
497
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];
502     }
503
504     decode_slice_luma(avctx, slice, dest_y, luma_stride,
505                       buf, y_data_size, qmat_luma_scaled);
506
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,
510                             qmat_chroma_scaled);
511         decode_slice_chroma(avctx, slice, dest_v, chroma_stride,
512                             buf + y_data_size + u_data_size, v_data_size,
513                             qmat_chroma_scaled);
514     }
515
516     return 0;
517 }
518
519 static int decode_picture(AVCodecContext *avctx)
520 {
521     ProresContext *ctx = avctx->priv_data;
522     int i, threads_ret[ctx->slice_count];
523
524     avctx->execute2(avctx, decode_slice_thread, NULL, threads_ret, ctx->slice_count);
525
526     for (i = 0; i < ctx->slice_count; i++)
527         if (threads_ret[i] < 0)
528             return threads_ret[i];
529
530     return 0;
531 }
532
533 static int decode_frame(AVCodecContext *avctx, void *data, int *data_size,
534                         AVPacket *avpkt)
535 {
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;
541
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");
545         return -1;
546     }
547
548     ctx->first_field = 1;
549
550     buf += 8;
551     buf_size -= 8;
552
553     frame_hdr_size = decode_frame_header(ctx, buf, buf_size, avctx);
554     if (frame_hdr_size < 0)
555         return -1;
556
557     buf += frame_hdr_size;
558     buf_size -= frame_hdr_size;
559
560  decode_picture:
561     pic_size = decode_picture_header(avctx, buf, buf_size);
562     if (pic_size < 0) {
563         av_log(avctx, AV_LOG_ERROR, "error decoding picture header\n");
564         return -1;
565     }
566
567     if (frame->data[0])
568         avctx->release_buffer(avctx, frame);
569
570     if (avctx->get_buffer(avctx, frame) < 0)
571         return -1;
572
573     if (decode_picture(avctx)) {
574         av_log(avctx, AV_LOG_ERROR, "error decoding picture\n");
575         return -1;
576     }
577
578     buf += pic_size;
579     buf_size -= pic_size;
580
581     if (ctx->frame_type && buf_size > 0 && ctx->first_field) {
582         ctx->first_field = 0;
583         goto decode_picture;
584     }
585
586     *data_size = sizeof(AVFrame);
587     *(AVFrame*)data = *frame;
588
589     return avpkt->size;
590 }
591
592 static av_cold int decode_close(AVCodecContext *avctx)
593 {
594     ProresContext *ctx = avctx->priv_data;
595
596     AVFrame *frame = avctx->coded_frame;
597     if (frame->data[0])
598         avctx->release_buffer(avctx, frame);
599     av_freep(&ctx->slices);
600
601     return 0;
602 }
603
604 AVCodec ff_prores_decoder = {
605     .name           = "prores",
606     .type           = AVMEDIA_TYPE_VIDEO,
607     .id             = CODEC_ID_PRORES,
608     .priv_data_size = sizeof(ProresContext),
609     .init           = decode_init,
610     .close          = decode_close,
611     .decode         = decode_frame,
612     .long_name      = NULL_IF_CONFIG_SMALL("ProRes"),
613     .capabilities   = CODEC_CAP_SLICE_THREADS,
614 };