• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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 "libavutil/internal.h"
32 #include "libavutil/mem_internal.h"
33 
34 #include "avcodec.h"
35 #include "get_bits.h"
36 #include "idctdsp.h"
37 #include "internal.h"
38 #include "profiles.h"
39 #include "simple_idct.h"
40 #include "proresdec.h"
41 #include "proresdata.h"
42 #include "thread.h"
43 
permute(uint8_t * dst,const uint8_t * src,const uint8_t permutation[64])44 static void permute(uint8_t *dst, const uint8_t *src, const uint8_t permutation[64])
45 {
46     int i;
47     for (i = 0; i < 64; i++)
48         dst[i] = permutation[src[i]];
49 }
50 
51 #define ALPHA_SHIFT_16_TO_10(alpha_val) (alpha_val >> 6)
52 #define ALPHA_SHIFT_8_TO_10(alpha_val)  ((alpha_val << 2) | (alpha_val >> 6))
53 #define ALPHA_SHIFT_16_TO_12(alpha_val) (alpha_val >> 4)
54 #define ALPHA_SHIFT_8_TO_12(alpha_val)  ((alpha_val << 4) | (alpha_val >> 4))
55 
unpack_alpha(GetBitContext * gb,uint16_t * dst,int num_coeffs,const int num_bits,const int decode_precision)56 static void inline unpack_alpha(GetBitContext *gb, uint16_t *dst, int num_coeffs,
57                                 const int num_bits, const int decode_precision) {
58     const int mask = (1 << num_bits) - 1;
59     int i, idx, val, alpha_val;
60 
61     idx       = 0;
62     alpha_val = mask;
63     do {
64         do {
65             if (get_bits1(gb)) {
66                 val = get_bits(gb, num_bits);
67             } else {
68                 int sign;
69                 val  = get_bits(gb, num_bits == 16 ? 7 : 4);
70                 sign = val & 1;
71                 val  = (val + 2) >> 1;
72                 if (sign)
73                     val = -val;
74             }
75             alpha_val = (alpha_val + val) & mask;
76             if (num_bits == 16) {
77                 if (decode_precision == 10) {
78                     dst[idx++] = ALPHA_SHIFT_16_TO_10(alpha_val);
79                 } else { /* 12b */
80                     dst[idx++] = ALPHA_SHIFT_16_TO_12(alpha_val);
81                 }
82             } else {
83                 if (decode_precision == 10) {
84                     dst[idx++] = ALPHA_SHIFT_8_TO_10(alpha_val);
85                 } else { /* 12b */
86                     dst[idx++] = ALPHA_SHIFT_8_TO_12(alpha_val);
87                 }
88             }
89             if (idx >= num_coeffs)
90                 break;
91         } while (get_bits_left(gb)>0 && get_bits1(gb));
92         val = get_bits(gb, 4);
93         if (!val)
94             val = get_bits(gb, 11);
95         if (idx + val > num_coeffs)
96             val = num_coeffs - idx;
97         if (num_bits == 16) {
98             for (i = 0; i < val; i++) {
99                 if (decode_precision == 10) {
100                     dst[idx++] = ALPHA_SHIFT_16_TO_10(alpha_val);
101                 } else { /* 12b */
102                     dst[idx++] = ALPHA_SHIFT_16_TO_12(alpha_val);
103                 }
104             }
105         } else {
106             for (i = 0; i < val; i++) {
107                 if (decode_precision == 10) {
108                     dst[idx++] = ALPHA_SHIFT_8_TO_10(alpha_val);
109                 } else { /* 12b */
110                     dst[idx++] = ALPHA_SHIFT_8_TO_12(alpha_val);
111                 }
112             }
113         }
114     } while (idx < num_coeffs);
115 }
116 
unpack_alpha_10(GetBitContext * gb,uint16_t * dst,int num_coeffs,const int num_bits)117 static void unpack_alpha_10(GetBitContext *gb, uint16_t *dst, int num_coeffs,
118                             const int num_bits)
119 {
120     if (num_bits == 16) {
121         unpack_alpha(gb, dst, num_coeffs, 16, 10);
122     } else { /* 8 bits alpha */
123         unpack_alpha(gb, dst, num_coeffs, 8, 10);
124     }
125 }
126 
unpack_alpha_12(GetBitContext * gb,uint16_t * dst,int num_coeffs,const int num_bits)127 static void unpack_alpha_12(GetBitContext *gb, uint16_t *dst, int num_coeffs,
128                             const int num_bits)
129 {
130     if (num_bits == 16) {
131         unpack_alpha(gb, dst, num_coeffs, 16, 12);
132     } else { /* 8 bits alpha */
133         unpack_alpha(gb, dst, num_coeffs, 8, 12);
134     }
135 }
136 
decode_init(AVCodecContext * avctx)137 static av_cold int decode_init(AVCodecContext *avctx)
138 {
139     int ret = 0;
140     ProresContext *ctx = avctx->priv_data;
141     uint8_t idct_permutation[64];
142 
143     avctx->bits_per_raw_sample = 10;
144 
145     switch (avctx->codec_tag) {
146     case MKTAG('a','p','c','o'):
147         avctx->profile = FF_PROFILE_PRORES_PROXY;
148         break;
149     case MKTAG('a','p','c','s'):
150         avctx->profile = FF_PROFILE_PRORES_LT;
151         break;
152     case MKTAG('a','p','c','n'):
153         avctx->profile = FF_PROFILE_PRORES_STANDARD;
154         break;
155     case MKTAG('a','p','c','h'):
156         avctx->profile = FF_PROFILE_PRORES_HQ;
157         break;
158     case MKTAG('a','p','4','h'):
159         avctx->profile = FF_PROFILE_PRORES_4444;
160         avctx->bits_per_raw_sample = 12;
161         break;
162     case MKTAG('a','p','4','x'):
163         avctx->profile = FF_PROFILE_PRORES_XQ;
164         avctx->bits_per_raw_sample = 12;
165         break;
166     default:
167         avctx->profile = FF_PROFILE_UNKNOWN;
168         av_log(avctx, AV_LOG_WARNING, "Unknown prores profile %d\n", avctx->codec_tag);
169     }
170 
171     if (avctx->bits_per_raw_sample == 10) {
172         av_log(avctx, AV_LOG_DEBUG, "Auto bitdepth precision. Use 10b decoding based on codec tag.\n");
173     } else { /* 12b */
174         av_log(avctx, AV_LOG_DEBUG, "Auto bitdepth precision. Use 12b decoding based on codec tag.\n");
175     }
176 
177     ff_blockdsp_init(&ctx->bdsp, avctx);
178     ret = ff_proresdsp_init(&ctx->prodsp, avctx);
179     if (ret < 0) {
180         av_log(avctx, AV_LOG_ERROR, "Fail to init proresdsp for bits per raw sample %d\n", avctx->bits_per_raw_sample);
181         return ret;
182     }
183 
184     ff_init_scantable_permutation(idct_permutation,
185                                   ctx->prodsp.idct_permutation_type);
186 
187     permute(ctx->progressive_scan, ff_prores_progressive_scan, idct_permutation);
188     permute(ctx->interlaced_scan, ff_prores_interlaced_scan, idct_permutation);
189 
190     if (avctx->bits_per_raw_sample == 10){
191         ctx->unpack_alpha = unpack_alpha_10;
192     } else if (avctx->bits_per_raw_sample == 12){
193         ctx->unpack_alpha = unpack_alpha_12;
194     } else {
195         av_log(avctx, AV_LOG_ERROR, "Fail to set unpack_alpha for bits per raw sample %d\n", avctx->bits_per_raw_sample);
196         return AVERROR_BUG;
197     }
198     return ret;
199 }
200 
decode_frame_header(ProresContext * ctx,const uint8_t * buf,const int data_size,AVCodecContext * avctx)201 static int decode_frame_header(ProresContext *ctx, const uint8_t *buf,
202                                const int data_size, AVCodecContext *avctx)
203 {
204     int hdr_size, width, height, flags;
205     int version;
206     const uint8_t *ptr;
207 
208     hdr_size = AV_RB16(buf);
209     ff_dlog(avctx, "header size %d\n", hdr_size);
210     if (hdr_size > data_size) {
211         av_log(avctx, AV_LOG_ERROR, "error, wrong header size\n");
212         return AVERROR_INVALIDDATA;
213     }
214 
215     version = AV_RB16(buf + 2);
216     ff_dlog(avctx, "%.4s version %d\n", buf+4, version);
217     if (version > 1) {
218         av_log(avctx, AV_LOG_ERROR, "unsupported version: %d\n", version);
219         return AVERROR_PATCHWELCOME;
220     }
221 
222     width  = AV_RB16(buf + 8);
223     height = AV_RB16(buf + 10);
224 
225     if (width != avctx->width || height != avctx->height) {
226         int ret;
227 
228         av_log(avctx, AV_LOG_WARNING, "picture resolution change: %dx%d -> %dx%d\n",
229                avctx->width, avctx->height, width, height);
230         if ((ret = ff_set_dimensions(avctx, width, height)) < 0)
231             return ret;
232     }
233 
234     ctx->frame_type = (buf[12] >> 2) & 3;
235     ctx->alpha_info = buf[17] & 0xf;
236 
237     if (ctx->alpha_info > 2) {
238         av_log(avctx, AV_LOG_ERROR, "Invalid alpha mode %d\n", ctx->alpha_info);
239         return AVERROR_INVALIDDATA;
240     }
241     if (avctx->skip_alpha) ctx->alpha_info = 0;
242 
243     ff_dlog(avctx, "frame type %d\n", ctx->frame_type);
244 
245     if (ctx->frame_type == 0) {
246         ctx->scan = ctx->progressive_scan; // permuted
247     } else {
248         ctx->scan = ctx->interlaced_scan; // permuted
249         ctx->frame->interlaced_frame = 1;
250         ctx->frame->top_field_first = ctx->frame_type == 1;
251     }
252 
253     if (ctx->alpha_info) {
254         if (avctx->bits_per_raw_sample == 10) {
255             avctx->pix_fmt = (buf[12] & 0xC0) == 0xC0 ? AV_PIX_FMT_YUVA444P10 : AV_PIX_FMT_YUVA422P10;
256         } else { /* 12b */
257             avctx->pix_fmt = (buf[12] & 0xC0) == 0xC0 ? AV_PIX_FMT_YUVA444P12 : AV_PIX_FMT_YUVA422P12;
258         }
259     } else {
260         if (avctx->bits_per_raw_sample == 10) {
261             avctx->pix_fmt = (buf[12] & 0xC0) == 0xC0 ? AV_PIX_FMT_YUV444P10 : AV_PIX_FMT_YUV422P10;
262         } else { /* 12b */
263             avctx->pix_fmt = (buf[12] & 0xC0) == 0xC0 ? AV_PIX_FMT_YUV444P12 : AV_PIX_FMT_YUV422P12;
264         }
265     }
266 
267     avctx->color_primaries = buf[14];
268     avctx->color_trc       = buf[15];
269     avctx->colorspace      = buf[16];
270     avctx->color_range     = AVCOL_RANGE_MPEG;
271 
272     ptr   = buf + 20;
273     flags = buf[19];
274     ff_dlog(avctx, "flags %x\n", flags);
275 
276     if (flags & 2) {
277         if(buf + data_size - ptr < 64) {
278             av_log(avctx, AV_LOG_ERROR, "Header truncated\n");
279             return AVERROR_INVALIDDATA;
280         }
281         permute(ctx->qmat_luma, ctx->prodsp.idct_permutation, ptr);
282         ptr += 64;
283     } else {
284         memset(ctx->qmat_luma, 4, 64);
285     }
286 
287     if (flags & 1) {
288         if(buf + data_size - ptr < 64) {
289             av_log(avctx, AV_LOG_ERROR, "Header truncated\n");
290             return AVERROR_INVALIDDATA;
291         }
292         permute(ctx->qmat_chroma, ctx->prodsp.idct_permutation, ptr);
293     } else {
294         memcpy(ctx->qmat_chroma, ctx->qmat_luma, 64);
295     }
296 
297     return hdr_size;
298 }
299 
decode_picture_header(AVCodecContext * avctx,const uint8_t * buf,const int buf_size)300 static int decode_picture_header(AVCodecContext *avctx, const uint8_t *buf, const int buf_size)
301 {
302     ProresContext *ctx = avctx->priv_data;
303     int i, hdr_size, slice_count;
304     unsigned pic_data_size;
305     int log2_slice_mb_width, log2_slice_mb_height;
306     int slice_mb_count, mb_x, mb_y;
307     const uint8_t *data_ptr, *index_ptr;
308 
309     hdr_size = buf[0] >> 3;
310     if (hdr_size < 8 || hdr_size > buf_size) {
311         av_log(avctx, AV_LOG_ERROR, "error, wrong picture header size\n");
312         return AVERROR_INVALIDDATA;
313     }
314 
315     pic_data_size = AV_RB32(buf + 1);
316     if (pic_data_size > buf_size) {
317         av_log(avctx, AV_LOG_ERROR, "error, wrong picture data size\n");
318         return AVERROR_INVALIDDATA;
319     }
320 
321     log2_slice_mb_width  = buf[7] >> 4;
322     log2_slice_mb_height = buf[7] & 0xF;
323     if (log2_slice_mb_width > 3 || log2_slice_mb_height) {
324         av_log(avctx, AV_LOG_ERROR, "unsupported slice resolution: %dx%d\n",
325                1 << log2_slice_mb_width, 1 << log2_slice_mb_height);
326         return AVERROR_INVALIDDATA;
327     }
328 
329     ctx->mb_width  = (avctx->width  + 15) >> 4;
330     if (ctx->frame_type)
331         ctx->mb_height = (avctx->height + 31) >> 5;
332     else
333         ctx->mb_height = (avctx->height + 15) >> 4;
334 
335     // QT ignores the written value
336     // slice_count = AV_RB16(buf + 5);
337     slice_count = ctx->mb_height * ((ctx->mb_width >> log2_slice_mb_width) +
338                                     av_popcount(ctx->mb_width & (1 << log2_slice_mb_width) - 1));
339 
340     if (ctx->slice_count != slice_count || !ctx->slices) {
341         av_freep(&ctx->slices);
342         ctx->slice_count = 0;
343         ctx->slices = av_mallocz_array(slice_count, sizeof(*ctx->slices));
344         if (!ctx->slices)
345             return AVERROR(ENOMEM);
346         ctx->slice_count = slice_count;
347     }
348 
349     if (!slice_count)
350         return AVERROR(EINVAL);
351 
352     if (hdr_size + slice_count*2 > buf_size) {
353         av_log(avctx, AV_LOG_ERROR, "error, wrong slice count\n");
354         return AVERROR_INVALIDDATA;
355     }
356 
357     // parse slice information
358     index_ptr = buf + hdr_size;
359     data_ptr  = index_ptr + slice_count*2;
360 
361     slice_mb_count = 1 << log2_slice_mb_width;
362     mb_x = 0;
363     mb_y = 0;
364 
365     for (i = 0; i < slice_count; i++) {
366         SliceContext *slice = &ctx->slices[i];
367 
368         slice->data = data_ptr;
369         data_ptr += AV_RB16(index_ptr + i*2);
370 
371         while (ctx->mb_width - mb_x < slice_mb_count)
372             slice_mb_count >>= 1;
373 
374         slice->mb_x = mb_x;
375         slice->mb_y = mb_y;
376         slice->mb_count = slice_mb_count;
377         slice->data_size = data_ptr - slice->data;
378 
379         if (slice->data_size < 6) {
380             av_log(avctx, AV_LOG_ERROR, "error, wrong slice data size\n");
381             return AVERROR_INVALIDDATA;
382         }
383 
384         mb_x += slice_mb_count;
385         if (mb_x == ctx->mb_width) {
386             slice_mb_count = 1 << log2_slice_mb_width;
387             mb_x = 0;
388             mb_y++;
389         }
390         if (data_ptr > buf + buf_size) {
391             av_log(avctx, AV_LOG_ERROR, "error, slice out of bounds\n");
392             return AVERROR_INVALIDDATA;
393         }
394     }
395 
396     if (mb_x || mb_y != ctx->mb_height) {
397         av_log(avctx, AV_LOG_ERROR, "error wrong mb count y %d h %d\n",
398                mb_y, ctx->mb_height);
399         return AVERROR_INVALIDDATA;
400     }
401 
402     return pic_data_size;
403 }
404 
405 #define DECODE_CODEWORD(val, codebook, SKIP)                            \
406     do {                                                                \
407         unsigned int rice_order, exp_order, switch_bits;                \
408         unsigned int q, buf, bits;                                      \
409                                                                         \
410         UPDATE_CACHE(re, gb);                                           \
411         buf = GET_CACHE(re, gb);                                        \
412                                                                         \
413         /* number of bits to switch between rice and exp golomb */      \
414         switch_bits =  codebook & 3;                                    \
415         rice_order  =  codebook >> 5;                                   \
416         exp_order   = (codebook >> 2) & 7;                              \
417                                                                         \
418         q = 31 - av_log2(buf);                                          \
419                                                                         \
420         if (q > switch_bits) { /* exp golomb */                         \
421             bits = exp_order - switch_bits + (q<<1);                    \
422             if (bits > FFMIN(MIN_CACHE_BITS, 31))                       \
423                 return AVERROR_INVALIDDATA;                             \
424             val = SHOW_UBITS(re, gb, bits) - (1 << exp_order) +         \
425                 ((switch_bits + 1) << rice_order);                      \
426             SKIP(re, gb, bits);                                         \
427         } else if (rice_order) {                                        \
428             SKIP_BITS(re, gb, q+1);                                     \
429             val = (q << rice_order) + SHOW_UBITS(re, gb, rice_order);   \
430             SKIP(re, gb, rice_order);                                   \
431         } else {                                                        \
432             val = q;                                                    \
433             SKIP(re, gb, q+1);                                          \
434         }                                                               \
435     } while (0)
436 
437 #define TOSIGNED(x) (((x) >> 1) ^ (-((x) & 1)))
438 
439 #define FIRST_DC_CB 0xB8
440 
441 static const uint8_t dc_codebook[7] = { 0x04, 0x28, 0x28, 0x4D, 0x4D, 0x70, 0x70};
442 
decode_dc_coeffs(GetBitContext * gb,int16_t * out,int blocks_per_slice)443 static av_always_inline int decode_dc_coeffs(GetBitContext *gb, int16_t *out,
444                                               int blocks_per_slice)
445 {
446     int16_t prev_dc;
447     int code, i, sign;
448 
449     OPEN_READER(re, gb);
450 
451     DECODE_CODEWORD(code, FIRST_DC_CB, LAST_SKIP_BITS);
452     prev_dc = TOSIGNED(code);
453     out[0] = prev_dc;
454 
455     out += 64; // dc coeff for the next block
456 
457     code = 5;
458     sign = 0;
459     for (i = 1; i < blocks_per_slice; i++, out += 64) {
460         DECODE_CODEWORD(code, dc_codebook[FFMIN(code, 6U)], LAST_SKIP_BITS);
461         if(code) sign ^= -(code & 1);
462         else     sign  = 0;
463         prev_dc += (((code + 1) >> 1) ^ sign) - sign;
464         out[0] = prev_dc;
465     }
466     CLOSE_READER(re, gb);
467     return 0;
468 }
469 
470 // adaptive codebook switching lut according to previous run/level values
471 static const uint8_t run_to_cb[16] = { 0x06, 0x06, 0x05, 0x05, 0x04, 0x29, 0x29, 0x29, 0x29, 0x28, 0x28, 0x28, 0x28, 0x28, 0x28, 0x4C };
472 static const uint8_t lev_to_cb[10] = { 0x04, 0x0A, 0x05, 0x06, 0x04, 0x28, 0x28, 0x28, 0x28, 0x4C };
473 
decode_ac_coeffs(AVCodecContext * avctx,GetBitContext * gb,int16_t * out,int blocks_per_slice)474 static av_always_inline int decode_ac_coeffs(AVCodecContext *avctx, GetBitContext *gb,
475                                              int16_t *out, int blocks_per_slice)
476 {
477     ProresContext *ctx = avctx->priv_data;
478     int block_mask, sign;
479     unsigned pos, run, level;
480     int max_coeffs, i, bits_left;
481     int log2_block_count = av_log2(blocks_per_slice);
482 
483     OPEN_READER(re, gb);
484     UPDATE_CACHE(re, gb);                                           \
485     run   = 4;
486     level = 2;
487 
488     max_coeffs = 64 << log2_block_count;
489     block_mask = blocks_per_slice - 1;
490 
491     for (pos = block_mask;;) {
492         bits_left = gb->size_in_bits - re_index;
493         if (!bits_left || (bits_left < 32 && !SHOW_UBITS(re, gb, bits_left)))
494             break;
495 
496         DECODE_CODEWORD(run, run_to_cb[FFMIN(run,  15)], LAST_SKIP_BITS);
497         pos += run + 1;
498         if (pos >= max_coeffs) {
499             av_log(avctx, AV_LOG_ERROR, "ac tex damaged %d, %d\n", pos, max_coeffs);
500             return AVERROR_INVALIDDATA;
501         }
502 
503         DECODE_CODEWORD(level, lev_to_cb[FFMIN(level, 9)], SKIP_BITS);
504         level += 1;
505 
506         i = pos >> log2_block_count;
507 
508         sign = SHOW_SBITS(re, gb, 1);
509         SKIP_BITS(re, gb, 1);
510         out[((pos & block_mask) << 6) + ctx->scan[i]] = ((level ^ sign) - sign);
511     }
512 
513     CLOSE_READER(re, gb);
514     return 0;
515 }
516 
decode_slice_luma(AVCodecContext * avctx,SliceContext * slice,uint16_t * dst,int dst_stride,const uint8_t * buf,unsigned buf_size,const int16_t * qmat)517 static int decode_slice_luma(AVCodecContext *avctx, SliceContext *slice,
518                              uint16_t *dst, int dst_stride,
519                              const uint8_t *buf, unsigned buf_size,
520                              const int16_t *qmat)
521 {
522     ProresContext *ctx = avctx->priv_data;
523     LOCAL_ALIGNED_32(int16_t, blocks, [8*4*64]);
524     int16_t *block;
525     GetBitContext gb;
526     int i, blocks_per_slice = slice->mb_count<<2;
527     int ret;
528 
529     for (i = 0; i < blocks_per_slice; i++)
530         ctx->bdsp.clear_block(blocks+(i<<6));
531 
532     init_get_bits(&gb, buf, buf_size << 3);
533 
534     if ((ret = decode_dc_coeffs(&gb, blocks, blocks_per_slice)) < 0)
535         return ret;
536     if ((ret = decode_ac_coeffs(avctx, &gb, blocks, blocks_per_slice)) < 0)
537         return ret;
538 
539     block = blocks;
540     for (i = 0; i < slice->mb_count; i++) {
541         ctx->prodsp.idct_put(dst, dst_stride, block+(0<<6), qmat);
542         ctx->prodsp.idct_put(dst             +8, dst_stride, block+(1<<6), qmat);
543         ctx->prodsp.idct_put(dst+4*dst_stride  , dst_stride, block+(2<<6), qmat);
544         ctx->prodsp.idct_put(dst+4*dst_stride+8, dst_stride, block+(3<<6), qmat);
545         block += 4*64;
546         dst += 16;
547     }
548     return 0;
549 }
550 
decode_slice_chroma(AVCodecContext * avctx,SliceContext * slice,uint16_t * dst,int dst_stride,const uint8_t * buf,unsigned buf_size,const int16_t * qmat,int log2_blocks_per_mb)551 static int decode_slice_chroma(AVCodecContext *avctx, SliceContext *slice,
552                                uint16_t *dst, int dst_stride,
553                                const uint8_t *buf, unsigned buf_size,
554                                const int16_t *qmat, int log2_blocks_per_mb)
555 {
556     ProresContext *ctx = avctx->priv_data;
557     LOCAL_ALIGNED_32(int16_t, blocks, [8*4*64]);
558     int16_t *block;
559     GetBitContext gb;
560     int i, j, blocks_per_slice = slice->mb_count << log2_blocks_per_mb;
561     int ret;
562 
563     for (i = 0; i < blocks_per_slice; i++)
564         ctx->bdsp.clear_block(blocks+(i<<6));
565 
566     init_get_bits(&gb, buf, buf_size << 3);
567 
568     if ((ret = decode_dc_coeffs(&gb, blocks, blocks_per_slice)) < 0)
569         return ret;
570     if ((ret = decode_ac_coeffs(avctx, &gb, blocks, blocks_per_slice)) < 0)
571         return ret;
572 
573     block = blocks;
574     for (i = 0; i < slice->mb_count; i++) {
575         for (j = 0; j < log2_blocks_per_mb; j++) {
576             ctx->prodsp.idct_put(dst,              dst_stride, block+(0<<6), qmat);
577             ctx->prodsp.idct_put(dst+4*dst_stride, dst_stride, block+(1<<6), qmat);
578             block += 2*64;
579             dst += 8;
580         }
581     }
582     return 0;
583 }
584 
585 /**
586  * Decode alpha slice plane.
587  */
decode_slice_alpha(ProresContext * ctx,uint16_t * dst,int dst_stride,const uint8_t * buf,int buf_size,int blocks_per_slice)588 static void decode_slice_alpha(ProresContext *ctx,
589                                uint16_t *dst, int dst_stride,
590                                const uint8_t *buf, int buf_size,
591                                int blocks_per_slice)
592 {
593     GetBitContext gb;
594     int i;
595     LOCAL_ALIGNED_32(int16_t, blocks, [8*4*64]);
596     int16_t *block;
597 
598     for (i = 0; i < blocks_per_slice<<2; i++)
599         ctx->bdsp.clear_block(blocks+(i<<6));
600 
601     init_get_bits(&gb, buf, buf_size << 3);
602 
603     if (ctx->alpha_info == 2) {
604         ctx->unpack_alpha(&gb, blocks, blocks_per_slice * 4 * 64, 16);
605     } else {
606         ctx->unpack_alpha(&gb, blocks, blocks_per_slice * 4 * 64, 8);
607     }
608 
609     block = blocks;
610 
611     for (i = 0; i < 16; i++) {
612         memcpy(dst, block, 16 * blocks_per_slice * sizeof(*dst));
613         dst   += dst_stride >> 1;
614         block += 16 * blocks_per_slice;
615     }
616 }
617 
decode_slice_thread(AVCodecContext * avctx,void * arg,int jobnr,int threadnr)618 static int decode_slice_thread(AVCodecContext *avctx, void *arg, int jobnr, int threadnr)
619 {
620     ProresContext *ctx = avctx->priv_data;
621     SliceContext *slice = &ctx->slices[jobnr];
622     const uint8_t *buf = slice->data;
623     AVFrame *pic = ctx->frame;
624     int i, hdr_size, qscale, log2_chroma_blocks_per_mb;
625     int luma_stride, chroma_stride;
626     int y_data_size, u_data_size, v_data_size, a_data_size, offset;
627     uint8_t *dest_y, *dest_u, *dest_v;
628     LOCAL_ALIGNED_16(int16_t, qmat_luma_scaled,  [64]);
629     LOCAL_ALIGNED_16(int16_t, qmat_chroma_scaled,[64]);
630     int mb_x_shift;
631     int ret;
632     uint16_t val_no_chroma;
633 
634     slice->ret = -1;
635     //av_log(avctx, AV_LOG_INFO, "slice %d mb width %d mb x %d y %d\n",
636     //       jobnr, slice->mb_count, slice->mb_x, slice->mb_y);
637 
638     // slice header
639     hdr_size = buf[0] >> 3;
640     qscale = av_clip(buf[1], 1, 224);
641     qscale = qscale > 128 ? qscale - 96 << 2: qscale;
642     y_data_size = AV_RB16(buf + 2);
643     u_data_size = AV_RB16(buf + 4);
644     v_data_size = slice->data_size - y_data_size - u_data_size - hdr_size;
645     if (hdr_size > 7) v_data_size = AV_RB16(buf + 6);
646     a_data_size = slice->data_size - y_data_size - u_data_size -
647                   v_data_size - hdr_size;
648 
649     if (y_data_size < 0 || u_data_size < 0 || v_data_size < 0
650         || hdr_size+y_data_size+u_data_size+v_data_size > slice->data_size){
651         av_log(avctx, AV_LOG_ERROR, "invalid plane data size\n");
652         return AVERROR_INVALIDDATA;
653     }
654 
655     buf += hdr_size;
656 
657     for (i = 0; i < 64; i++) {
658         qmat_luma_scaled  [i] = ctx->qmat_luma  [i] * qscale;
659         qmat_chroma_scaled[i] = ctx->qmat_chroma[i] * qscale;
660     }
661 
662     if (ctx->frame_type == 0) {
663         luma_stride   = pic->linesize[0];
664         chroma_stride = pic->linesize[1];
665     } else {
666         luma_stride   = pic->linesize[0] << 1;
667         chroma_stride = pic->linesize[1] << 1;
668     }
669 
670     if (avctx->pix_fmt == AV_PIX_FMT_YUV444P10 || avctx->pix_fmt == AV_PIX_FMT_YUVA444P10 ||
671         avctx->pix_fmt == AV_PIX_FMT_YUV444P12 || avctx->pix_fmt == AV_PIX_FMT_YUVA444P12) {
672         mb_x_shift = 5;
673         log2_chroma_blocks_per_mb = 2;
674     } else {
675         mb_x_shift = 4;
676         log2_chroma_blocks_per_mb = 1;
677     }
678 
679     offset = (slice->mb_y << 4) * luma_stride + (slice->mb_x << 5);
680     dest_y = pic->data[0] + offset;
681     dest_u = pic->data[1] + (slice->mb_y << 4) * chroma_stride + (slice->mb_x << mb_x_shift);
682     dest_v = pic->data[2] + (slice->mb_y << 4) * chroma_stride + (slice->mb_x << mb_x_shift);
683 
684     if (ctx->frame_type && ctx->first_field ^ ctx->frame->top_field_first) {
685         dest_y += pic->linesize[0];
686         dest_u += pic->linesize[1];
687         dest_v += pic->linesize[2];
688         offset += pic->linesize[3];
689     }
690 
691     ret = decode_slice_luma(avctx, slice, (uint16_t*)dest_y, luma_stride,
692                             buf, y_data_size, qmat_luma_scaled);
693     if (ret < 0)
694         return ret;
695 
696     if (!(avctx->flags & AV_CODEC_FLAG_GRAY) && (u_data_size + v_data_size) > 0) {
697         ret = decode_slice_chroma(avctx, slice, (uint16_t*)dest_u, chroma_stride,
698                                   buf + y_data_size, u_data_size,
699                                   qmat_chroma_scaled, log2_chroma_blocks_per_mb);
700         if (ret < 0)
701             return ret;
702 
703         ret = decode_slice_chroma(avctx, slice, (uint16_t*)dest_v, chroma_stride,
704                                   buf + y_data_size + u_data_size, v_data_size,
705                                   qmat_chroma_scaled, log2_chroma_blocks_per_mb);
706         if (ret < 0)
707             return ret;
708     }
709     else {
710         size_t mb_max_x = slice->mb_count << (mb_x_shift - 1);
711         size_t i, j;
712         if (avctx->bits_per_raw_sample == 10) {
713             val_no_chroma = 511;
714         } else { /* 12b */
715             val_no_chroma = 511 * 4;
716         }
717         for (i = 0; i < 16; ++i)
718             for (j = 0; j < mb_max_x; ++j) {
719                 *(uint16_t*)(dest_u + (i * chroma_stride) + (j << 1)) = val_no_chroma;
720                 *(uint16_t*)(dest_v + (i * chroma_stride) + (j << 1)) = val_no_chroma;
721             }
722     }
723 
724     /* decode alpha plane if available */
725     if (ctx->alpha_info && pic->data[3] && a_data_size) {
726         uint8_t *dest_a = pic->data[3] + offset;
727         decode_slice_alpha(ctx, (uint16_t*)dest_a, luma_stride,
728                            buf + y_data_size + u_data_size + v_data_size,
729                            a_data_size, slice->mb_count);
730     }
731 
732     slice->ret = 0;
733     return 0;
734 }
735 
decode_picture(AVCodecContext * avctx)736 static int decode_picture(AVCodecContext *avctx)
737 {
738     ProresContext *ctx = avctx->priv_data;
739     int i;
740     int error = 0;
741 
742     avctx->execute2(avctx, decode_slice_thread, NULL, NULL, ctx->slice_count);
743 
744     for (i = 0; i < ctx->slice_count; i++)
745         error += ctx->slices[i].ret < 0;
746 
747     if (error)
748         ctx->frame->decode_error_flags = FF_DECODE_ERROR_INVALID_BITSTREAM;
749     if (error < ctx->slice_count)
750         return 0;
751 
752     return ctx->slices[0].ret;
753 }
754 
decode_frame(AVCodecContext * avctx,void * data,int * got_frame,AVPacket * avpkt)755 static int decode_frame(AVCodecContext *avctx, void *data, int *got_frame,
756                         AVPacket *avpkt)
757 {
758     ProresContext *ctx = avctx->priv_data;
759     ThreadFrame tframe = { .f = data };
760     AVFrame *frame = data;
761     const uint8_t *buf = avpkt->data;
762     int buf_size = avpkt->size;
763     int frame_hdr_size, pic_size, ret;
764 
765     if (buf_size < 28 || AV_RL32(buf + 4) != AV_RL32("icpf")) {
766         av_log(avctx, AV_LOG_ERROR, "invalid frame header\n");
767         return AVERROR_INVALIDDATA;
768     }
769 
770     ctx->frame = frame;
771     ctx->frame->pict_type = AV_PICTURE_TYPE_I;
772     ctx->frame->key_frame = 1;
773     ctx->first_field = 1;
774 
775     buf += 8;
776     buf_size -= 8;
777 
778     frame_hdr_size = decode_frame_header(ctx, buf, buf_size, avctx);
779     if (frame_hdr_size < 0)
780         return frame_hdr_size;
781 
782     buf += frame_hdr_size;
783     buf_size -= frame_hdr_size;
784 
785  decode_picture:
786     pic_size = decode_picture_header(avctx, buf, buf_size);
787     if (pic_size < 0) {
788         av_log(avctx, AV_LOG_ERROR, "error decoding picture header\n");
789         return pic_size;
790     }
791 
792     if (ctx->first_field)
793         if ((ret = ff_thread_get_buffer(avctx, &tframe, 0)) < 0)
794             return ret;
795 
796     if ((ret = decode_picture(avctx)) < 0) {
797         av_log(avctx, AV_LOG_ERROR, "error decoding picture\n");
798         return ret;
799     }
800 
801     buf += pic_size;
802     buf_size -= pic_size;
803 
804     if (ctx->frame_type && buf_size > 0 && ctx->first_field) {
805         ctx->first_field = 0;
806         goto decode_picture;
807     }
808 
809     *got_frame      = 1;
810 
811     return avpkt->size;
812 }
813 
decode_close(AVCodecContext * avctx)814 static av_cold int decode_close(AVCodecContext *avctx)
815 {
816     ProresContext *ctx = avctx->priv_data;
817 
818     av_freep(&ctx->slices);
819 
820     return 0;
821 }
822 
823 AVCodec ff_prores_decoder = {
824     .name           = "prores",
825     .long_name      = NULL_IF_CONFIG_SMALL("Apple ProRes (iCodec Pro)"),
826     .type           = AVMEDIA_TYPE_VIDEO,
827     .id             = AV_CODEC_ID_PRORES,
828     .priv_data_size = sizeof(ProresContext),
829     .init           = decode_init,
830     .close          = decode_close,
831     .decode         = decode_frame,
832     .capabilities   = AV_CODEC_CAP_DR1 | AV_CODEC_CAP_SLICE_THREADS | AV_CODEC_CAP_FRAME_THREADS,
833     .profiles       = NULL_IF_CONFIG_SMALL(ff_prores_profiles),
834 };
835