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.

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