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.

714 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, SKIP) \
  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. if (bits > FFMIN(MIN_CACHE_BITS, 31)) \
  227. return AVERROR_INVALIDDATA; \
  228. val = SHOW_UBITS(re, gb, bits) - (1 << exp_order) + \
  229. ((switch_bits + 1) << rice_order); \
  230. SKIP(re, gb, bits); \
  231. } else if (rice_order) { \
  232. SKIP_BITS(re, gb, q+1); \
  233. val = (q << rice_order) + SHOW_UBITS(re, gb, rice_order); \
  234. SKIP(re, gb, rice_order); \
  235. } else { \
  236. val = q; \
  237. SKIP(re, gb, q+1); \
  238. } \
  239. } while (0)
  240. #define TOSIGNED(x) (((x) >> 1) ^ (-((x) & 1)))
  241. #define FIRST_DC_CB 0xB8
  242. static const uint8_t dc_codebook[7] = { 0x04, 0x28, 0x28, 0x4D, 0x4D, 0x70, 0x70};
  243. static av_always_inline int decode_dc_coeffs(GetBitContext *gb, int16_t *out,
  244. int blocks_per_slice)
  245. {
  246. int16_t prev_dc;
  247. int code, i, sign;
  248. OPEN_READER(re, gb);
  249. DECODE_CODEWORD(code, FIRST_DC_CB, LAST_SKIP_BITS);
  250. prev_dc = TOSIGNED(code);
  251. out[0] = prev_dc;
  252. out += 64; // dc coeff for the next block
  253. code = 5;
  254. sign = 0;
  255. for (i = 1; i < blocks_per_slice; i++, out += 64) {
  256. DECODE_CODEWORD(code, dc_codebook[FFMIN(code, 6U)], LAST_SKIP_BITS);
  257. if(code) sign ^= -(code & 1);
  258. else sign = 0;
  259. prev_dc += (((code + 1) >> 1) ^ sign) - sign;
  260. out[0] = prev_dc;
  261. }
  262. CLOSE_READER(re, gb);
  263. return 0;
  264. }
  265. // adaptive codebook switching lut according to previous run/level values
  266. static const uint8_t run_to_cb[16] = { 0x06, 0x06, 0x05, 0x05, 0x04, 0x29, 0x29, 0x29, 0x29, 0x28, 0x28, 0x28, 0x28, 0x28, 0x28, 0x4C };
  267. static const uint8_t lev_to_cb[10] = { 0x04, 0x0A, 0x05, 0x06, 0x04, 0x28, 0x28, 0x28, 0x28, 0x4C };
  268. static av_always_inline int decode_ac_coeffs(AVCodecContext *avctx, GetBitContext *gb,
  269. int16_t *out, int blocks_per_slice)
  270. {
  271. ProresContext *ctx = avctx->priv_data;
  272. int block_mask, sign;
  273. unsigned pos, run, level;
  274. int max_coeffs, i, bits_left;
  275. int log2_block_count = av_log2(blocks_per_slice);
  276. OPEN_READER(re, gb);
  277. UPDATE_CACHE(re, gb); \
  278. run = 4;
  279. level = 2;
  280. max_coeffs = 64 << log2_block_count;
  281. block_mask = blocks_per_slice - 1;
  282. for (pos = block_mask;;) {
  283. bits_left = gb->size_in_bits - re_index;
  284. if (!bits_left || (bits_left < 32 && !SHOW_UBITS(re, gb, bits_left)))
  285. break;
  286. DECODE_CODEWORD(run, run_to_cb[FFMIN(run, 15)], LAST_SKIP_BITS);
  287. pos += run + 1;
  288. if (pos >= max_coeffs) {
  289. av_log(avctx, AV_LOG_ERROR, "ac tex damaged %d, %d\n", pos, max_coeffs);
  290. return AVERROR_INVALIDDATA;
  291. }
  292. DECODE_CODEWORD(level, lev_to_cb[FFMIN(level, 9)], SKIP_BITS);
  293. level += 1;
  294. i = pos >> log2_block_count;
  295. sign = SHOW_SBITS(re, gb, 1);
  296. SKIP_BITS(re, gb, 1);
  297. out[((pos & block_mask) << 6) + ctx->scan[i]] = ((level ^ sign) - sign);
  298. }
  299. CLOSE_READER(re, gb);
  300. return 0;
  301. }
  302. static int decode_slice_luma(AVCodecContext *avctx, SliceContext *slice,
  303. uint16_t *dst, int dst_stride,
  304. const uint8_t *buf, unsigned buf_size,
  305. const int16_t *qmat)
  306. {
  307. ProresContext *ctx = avctx->priv_data;
  308. LOCAL_ALIGNED_32(int16_t, blocks, [8*4*64]);
  309. int16_t *block;
  310. GetBitContext gb;
  311. int i, blocks_per_slice = slice->mb_count<<2;
  312. int ret;
  313. for (i = 0; i < blocks_per_slice; i++)
  314. ctx->bdsp.clear_block(blocks+(i<<6));
  315. init_get_bits(&gb, buf, buf_size << 3);
  316. if ((ret = decode_dc_coeffs(&gb, blocks, blocks_per_slice)) < 0)
  317. return ret;
  318. if ((ret = decode_ac_coeffs(avctx, &gb, blocks, blocks_per_slice)) < 0)
  319. return ret;
  320. block = blocks;
  321. for (i = 0; i < slice->mb_count; i++) {
  322. ctx->prodsp.idct_put(dst, dst_stride, block+(0<<6), qmat);
  323. ctx->prodsp.idct_put(dst +8, dst_stride, block+(1<<6), qmat);
  324. ctx->prodsp.idct_put(dst+4*dst_stride , dst_stride, block+(2<<6), qmat);
  325. ctx->prodsp.idct_put(dst+4*dst_stride+8, dst_stride, block+(3<<6), qmat);
  326. block += 4*64;
  327. dst += 16;
  328. }
  329. return 0;
  330. }
  331. static int decode_slice_chroma(AVCodecContext *avctx, SliceContext *slice,
  332. uint16_t *dst, int dst_stride,
  333. const uint8_t *buf, unsigned buf_size,
  334. const int16_t *qmat, int log2_blocks_per_mb)
  335. {
  336. ProresContext *ctx = avctx->priv_data;
  337. LOCAL_ALIGNED_32(int16_t, blocks, [8*4*64]);
  338. int16_t *block;
  339. GetBitContext gb;
  340. int i, j, blocks_per_slice = slice->mb_count << log2_blocks_per_mb;
  341. int ret;
  342. for (i = 0; i < blocks_per_slice; i++)
  343. ctx->bdsp.clear_block(blocks+(i<<6));
  344. init_get_bits(&gb, buf, buf_size << 3);
  345. if ((ret = decode_dc_coeffs(&gb, blocks, blocks_per_slice)) < 0)
  346. return ret;
  347. if ((ret = decode_ac_coeffs(avctx, &gb, blocks, blocks_per_slice)) < 0)
  348. return ret;
  349. block = blocks;
  350. for (i = 0; i < slice->mb_count; i++) {
  351. for (j = 0; j < log2_blocks_per_mb; j++) {
  352. ctx->prodsp.idct_put(dst, dst_stride, block+(0<<6), qmat);
  353. ctx->prodsp.idct_put(dst+4*dst_stride, dst_stride, block+(1<<6), qmat);
  354. block += 2*64;
  355. dst += 8;
  356. }
  357. }
  358. return 0;
  359. }
  360. static void unpack_alpha(GetBitContext *gb, uint16_t *dst, int num_coeffs,
  361. const int num_bits)
  362. {
  363. const int mask = (1 << num_bits) - 1;
  364. int i, idx, val, alpha_val;
  365. idx = 0;
  366. alpha_val = mask;
  367. do {
  368. do {
  369. if (get_bits1(gb)) {
  370. val = get_bits(gb, num_bits);
  371. } else {
  372. int sign;
  373. val = get_bits(gb, num_bits == 16 ? 7 : 4);
  374. sign = val & 1;
  375. val = (val + 2) >> 1;
  376. if (sign)
  377. val = -val;
  378. }
  379. alpha_val = (alpha_val + val) & mask;
  380. if (num_bits == 16) {
  381. dst[idx++] = alpha_val >> 6;
  382. } else {
  383. dst[idx++] = (alpha_val << 2) | (alpha_val >> 6);
  384. }
  385. if (idx >= num_coeffs)
  386. break;
  387. } while (get_bits_left(gb)>0 && get_bits1(gb));
  388. val = get_bits(gb, 4);
  389. if (!val)
  390. val = get_bits(gb, 11);
  391. if (idx + val > num_coeffs)
  392. val = num_coeffs - idx;
  393. if (num_bits == 16) {
  394. for (i = 0; i < val; i++)
  395. dst[idx++] = alpha_val >> 6;
  396. } else {
  397. for (i = 0; i < val; i++)
  398. dst[idx++] = (alpha_val << 2) | (alpha_val >> 6);
  399. }
  400. } while (idx < num_coeffs);
  401. }
  402. /**
  403. * Decode alpha slice plane.
  404. */
  405. static void decode_slice_alpha(ProresContext *ctx,
  406. uint16_t *dst, int dst_stride,
  407. const uint8_t *buf, int buf_size,
  408. int blocks_per_slice)
  409. {
  410. GetBitContext gb;
  411. int i;
  412. LOCAL_ALIGNED_32(int16_t, blocks, [8*4*64]);
  413. int16_t *block;
  414. for (i = 0; i < blocks_per_slice<<2; i++)
  415. ctx->bdsp.clear_block(blocks+(i<<6));
  416. init_get_bits(&gb, buf, buf_size << 3);
  417. if (ctx->alpha_info == 2) {
  418. unpack_alpha(&gb, blocks, blocks_per_slice * 4 * 64, 16);
  419. } else {
  420. unpack_alpha(&gb, blocks, blocks_per_slice * 4 * 64, 8);
  421. }
  422. block = blocks;
  423. for (i = 0; i < 16; i++) {
  424. memcpy(dst, block, 16 * blocks_per_slice * sizeof(*dst));
  425. dst += dst_stride >> 1;
  426. block += 16 * blocks_per_slice;
  427. }
  428. }
  429. static int decode_slice_thread(AVCodecContext *avctx, void *arg, int jobnr, int threadnr)
  430. {
  431. ProresContext *ctx = avctx->priv_data;
  432. SliceContext *slice = &ctx->slices[jobnr];
  433. const uint8_t *buf = slice->data;
  434. AVFrame *pic = ctx->frame;
  435. int i, hdr_size, qscale, log2_chroma_blocks_per_mb;
  436. int luma_stride, chroma_stride;
  437. int y_data_size, u_data_size, v_data_size, a_data_size;
  438. uint8_t *dest_y, *dest_u, *dest_v, *dest_a;
  439. int16_t qmat_luma_scaled[64];
  440. int16_t qmat_chroma_scaled[64];
  441. int mb_x_shift;
  442. int ret;
  443. slice->ret = -1;
  444. //av_log(avctx, AV_LOG_INFO, "slice %d mb width %d mb x %d y %d\n",
  445. // jobnr, slice->mb_count, slice->mb_x, slice->mb_y);
  446. // slice header
  447. hdr_size = buf[0] >> 3;
  448. qscale = av_clip(buf[1], 1, 224);
  449. qscale = qscale > 128 ? qscale - 96 << 2: qscale;
  450. y_data_size = AV_RB16(buf + 2);
  451. u_data_size = AV_RB16(buf + 4);
  452. v_data_size = slice->data_size - y_data_size - u_data_size - hdr_size;
  453. if (hdr_size > 7) v_data_size = AV_RB16(buf + 6);
  454. a_data_size = slice->data_size - y_data_size - u_data_size -
  455. v_data_size - hdr_size;
  456. if (y_data_size < 0 || u_data_size < 0 || v_data_size < 0
  457. || hdr_size+y_data_size+u_data_size+v_data_size > slice->data_size){
  458. av_log(avctx, AV_LOG_ERROR, "invalid plane data size\n");
  459. return AVERROR_INVALIDDATA;
  460. }
  461. buf += hdr_size;
  462. for (i = 0; i < 64; i++) {
  463. qmat_luma_scaled [i] = ctx->qmat_luma [i] * qscale;
  464. qmat_chroma_scaled[i] = ctx->qmat_chroma[i] * qscale;
  465. }
  466. if (ctx->frame_type == 0) {
  467. luma_stride = pic->linesize[0];
  468. chroma_stride = pic->linesize[1];
  469. } else {
  470. luma_stride = pic->linesize[0] << 1;
  471. chroma_stride = pic->linesize[1] << 1;
  472. }
  473. if (avctx->pix_fmt == AV_PIX_FMT_YUV444P10 || avctx->pix_fmt == AV_PIX_FMT_YUVA444P10) {
  474. mb_x_shift = 5;
  475. log2_chroma_blocks_per_mb = 2;
  476. } else {
  477. mb_x_shift = 4;
  478. log2_chroma_blocks_per_mb = 1;
  479. }
  480. dest_y = pic->data[0] + (slice->mb_y << 4) * luma_stride + (slice->mb_x << 5);
  481. dest_u = pic->data[1] + (slice->mb_y << 4) * chroma_stride + (slice->mb_x << mb_x_shift);
  482. dest_v = pic->data[2] + (slice->mb_y << 4) * chroma_stride + (slice->mb_x << mb_x_shift);
  483. dest_a = pic->data[3] + (slice->mb_y << 4) * luma_stride + (slice->mb_x << 5);
  484. if (ctx->frame_type && ctx->first_field ^ ctx->frame->top_field_first) {
  485. dest_y += pic->linesize[0];
  486. dest_u += pic->linesize[1];
  487. dest_v += pic->linesize[2];
  488. dest_a += pic->linesize[3];
  489. }
  490. ret = decode_slice_luma(avctx, slice, (uint16_t*)dest_y, luma_stride,
  491. buf, y_data_size, qmat_luma_scaled);
  492. if (ret < 0)
  493. return ret;
  494. if (!(avctx->flags & AV_CODEC_FLAG_GRAY) && (u_data_size + v_data_size) > 0) {
  495. ret = decode_slice_chroma(avctx, slice, (uint16_t*)dest_u, chroma_stride,
  496. buf + y_data_size, u_data_size,
  497. qmat_chroma_scaled, log2_chroma_blocks_per_mb);
  498. if (ret < 0)
  499. return ret;
  500. ret = decode_slice_chroma(avctx, slice, (uint16_t*)dest_v, chroma_stride,
  501. buf + y_data_size + u_data_size, v_data_size,
  502. qmat_chroma_scaled, log2_chroma_blocks_per_mb);
  503. if (ret < 0)
  504. return ret;
  505. }
  506. else {
  507. size_t mb_max_x = slice->mb_count << (mb_x_shift - 1);
  508. for (size_t i = 0; i < 16; ++i)
  509. for (size_t j = 0; j < mb_max_x; ++j) {
  510. *(uint16_t*)(dest_u + (i * chroma_stride) + (j << 1)) = 511;
  511. *(uint16_t*)(dest_v + (i * chroma_stride) + (j << 1)) = 511;
  512. }
  513. }
  514. /* decode alpha plane if available */
  515. if (ctx->alpha_info && pic->data[3] && a_data_size)
  516. decode_slice_alpha(ctx, (uint16_t*)dest_a, luma_stride,
  517. buf + y_data_size + u_data_size + v_data_size,
  518. a_data_size, slice->mb_count);
  519. slice->ret = 0;
  520. return 0;
  521. }
  522. static int decode_picture(AVCodecContext *avctx)
  523. {
  524. ProresContext *ctx = avctx->priv_data;
  525. int i;
  526. int error = 0;
  527. avctx->execute2(avctx, decode_slice_thread, NULL, NULL, ctx->slice_count);
  528. for (i = 0; i < ctx->slice_count; i++)
  529. error += ctx->slices[i].ret < 0;
  530. if (error)
  531. ctx->frame->decode_error_flags = FF_DECODE_ERROR_INVALID_BITSTREAM;
  532. if (error < ctx->slice_count)
  533. return 0;
  534. return ctx->slices[0].ret;
  535. }
  536. static int decode_frame(AVCodecContext *avctx, void *data, int *got_frame,
  537. AVPacket *avpkt)
  538. {
  539. ProresContext *ctx = avctx->priv_data;
  540. AVFrame *frame = data;
  541. const uint8_t *buf = avpkt->data;
  542. int buf_size = avpkt->size;
  543. int frame_hdr_size, pic_size, ret;
  544. if (buf_size < 28 || AV_RL32(buf + 4) != AV_RL32("icpf")) {
  545. av_log(avctx, AV_LOG_ERROR, "invalid frame header\n");
  546. return AVERROR_INVALIDDATA;
  547. }
  548. ctx->frame = frame;
  549. ctx->frame->pict_type = AV_PICTURE_TYPE_I;
  550. ctx->frame->key_frame = 1;
  551. ctx->first_field = 1;
  552. buf += 8;
  553. buf_size -= 8;
  554. frame_hdr_size = decode_frame_header(ctx, buf, buf_size, avctx);
  555. if (frame_hdr_size < 0)
  556. return frame_hdr_size;
  557. buf += frame_hdr_size;
  558. buf_size -= frame_hdr_size;
  559. if ((ret = ff_get_buffer(avctx, frame, 0)) < 0)
  560. return ret;
  561. decode_picture:
  562. pic_size = decode_picture_header(avctx, buf, buf_size);
  563. if (pic_size < 0) {
  564. av_log(avctx, AV_LOG_ERROR, "error decoding picture header\n");
  565. return pic_size;
  566. }
  567. if ((ret = decode_picture(avctx)) < 0) {
  568. av_log(avctx, AV_LOG_ERROR, "error decoding picture\n");
  569. return ret;
  570. }
  571. buf += pic_size;
  572. buf_size -= pic_size;
  573. if (ctx->frame_type && buf_size > 0 && ctx->first_field) {
  574. ctx->first_field = 0;
  575. goto decode_picture;
  576. }
  577. *got_frame = 1;
  578. return avpkt->size;
  579. }
  580. static av_cold int decode_close(AVCodecContext *avctx)
  581. {
  582. ProresContext *ctx = avctx->priv_data;
  583. av_freep(&ctx->slices);
  584. return 0;
  585. }
  586. AVCodec ff_prores_decoder = {
  587. .name = "prores",
  588. .long_name = NULL_IF_CONFIG_SMALL("ProRes"),
  589. .type = AVMEDIA_TYPE_VIDEO,
  590. .id = AV_CODEC_ID_PRORES,
  591. .priv_data_size = sizeof(ProresContext),
  592. .init = decode_init,
  593. .close = decode_close,
  594. .decode = decode_frame,
  595. .capabilities = AV_CODEC_CAP_DR1 | AV_CODEC_CAP_SLICE_THREADS,
  596. };