You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

695 lines
23KB

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