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.

655 lines
21KB

  1. /*
  2. * Apple ProRes compatible decoder
  3. *
  4. * Copyright (c) 2010-2011 Maxim Poliakovski
  5. *
  6. * This file is part of Libav.
  7. *
  8. * Libav is free software; you can redistribute it and/or
  9. * modify it under the terms of the GNU Lesser General Public
  10. * License as published by the Free Software Foundation; either
  11. * version 2.1 of the License, or (at your option) any later version.
  12. *
  13. * Libav is distributed in the hope that it will be useful,
  14. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  15. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  16. * Lesser General Public License for more details.
  17. *
  18. * You should have received a copy of the GNU Lesser General Public
  19. * License along with Libav; if not, write to the Free Software
  20. * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
  21. */
  22. /**
  23. * @file
  24. * This is a decoder for Apple ProRes 422 SD/HQ/LT/Proxy and ProRes 4444.
  25. * It is used for storing and editing high definition video data in Apple's Final Cut Pro.
  26. *
  27. * @see http://wiki.multimedia.cx/index.php?title=Apple_ProRes
  28. */
  29. #define LONG_BITSTREAM_READER // some ProRes vlc codes require up to 28 bits to be read at once
  30. #include <stdint.h>
  31. #include "libavutil/intmath.h"
  32. #include "avcodec.h"
  33. #include "proresdata.h"
  34. #include "proresdsp.h"
  35. #include "get_bits.h"
  36. typedef struct {
  37. const uint8_t *index; ///< pointers to the data of this slice
  38. int slice_num;
  39. int x_pos, y_pos;
  40. int slice_width;
  41. int prev_slice_sf; ///< scalefactor of the previous decoded slice
  42. DECLARE_ALIGNED(16, DCTELEM, blocks)[8 * 4 * 64];
  43. DECLARE_ALIGNED(16, int16_t, qmat_luma_scaled)[64];
  44. DECLARE_ALIGNED(16, int16_t, qmat_chroma_scaled)[64];
  45. } ProresThreadData;
  46. typedef struct {
  47. ProresDSPContext dsp;
  48. AVFrame picture;
  49. ScanTable scantable;
  50. int scantable_type; ///< -1 = uninitialized, 0 = progressive, 1/2 = interlaced
  51. int frame_type; ///< 0 = progressive, 1 = top-field first, 2 = bottom-field first
  52. int pic_format; ///< 2 = 422, 3 = 444
  53. uint8_t qmat_luma[64]; ///< dequantization matrix for luma
  54. uint8_t qmat_chroma[64]; ///< dequantization matrix for chroma
  55. int qmat_changed; ///< 1 - global quantization matrices changed
  56. int total_slices; ///< total number of slices in a picture
  57. ProresThreadData *slice_data;
  58. int pic_num;
  59. int chroma_factor;
  60. int mb_chroma_factor;
  61. int num_chroma_blocks; ///< number of chrominance blocks in a macroblock
  62. int num_x_slices;
  63. int num_y_slices;
  64. int slice_width_factor;
  65. int slice_height_factor;
  66. int num_x_mbs;
  67. int num_y_mbs;
  68. int alpha_info;
  69. } ProresContext;
  70. static av_cold int decode_init(AVCodecContext *avctx)
  71. {
  72. ProresContext *ctx = avctx->priv_data;
  73. ctx->total_slices = 0;
  74. ctx->slice_data = NULL;
  75. avctx->bits_per_raw_sample = PRORES_BITS_PER_SAMPLE;
  76. ff_proresdsp_init(&ctx->dsp);
  77. avctx->coded_frame = &ctx->picture;
  78. avcodec_get_frame_defaults(&ctx->picture);
  79. ctx->picture.type = AV_PICTURE_TYPE_I;
  80. ctx->picture.key_frame = 1;
  81. ctx->scantable_type = -1; // set scantable type to uninitialized
  82. memset(ctx->qmat_luma, 4, 64);
  83. memset(ctx->qmat_chroma, 4, 64);
  84. return 0;
  85. }
  86. static int decode_frame_header(ProresContext *ctx, const uint8_t *buf,
  87. const int data_size, AVCodecContext *avctx)
  88. {
  89. int hdr_size, version, width, height, flags;
  90. const uint8_t *ptr;
  91. hdr_size = AV_RB16(buf);
  92. if (hdr_size > data_size) {
  93. av_log(avctx, AV_LOG_ERROR, "frame data too small\n");
  94. return AVERROR_INVALIDDATA;
  95. }
  96. version = AV_RB16(buf + 2);
  97. if (version >= 2) {
  98. av_log(avctx, AV_LOG_ERROR,
  99. "unsupported header version: %d\n", version);
  100. return AVERROR_INVALIDDATA;
  101. }
  102. width = AV_RB16(buf + 8);
  103. height = AV_RB16(buf + 10);
  104. if (width != avctx->width || height != avctx->height) {
  105. av_log(avctx, AV_LOG_ERROR,
  106. "picture dimension changed: old: %d x %d, new: %d x %d\n",
  107. avctx->width, avctx->height, width, height);
  108. return AVERROR_INVALIDDATA;
  109. }
  110. ctx->frame_type = (buf[12] >> 2) & 3;
  111. if (ctx->frame_type > 2) {
  112. av_log(avctx, AV_LOG_ERROR,
  113. "unsupported frame type: %d\n", ctx->frame_type);
  114. return AVERROR_INVALIDDATA;
  115. }
  116. ctx->chroma_factor = (buf[12] >> 6) & 3;
  117. ctx->mb_chroma_factor = ctx->chroma_factor + 2;
  118. ctx->num_chroma_blocks = (1 << ctx->chroma_factor) >> 1;
  119. switch (ctx->chroma_factor) {
  120. case 2:
  121. avctx->pix_fmt = PIX_FMT_YUV422P10;
  122. break;
  123. case 3:
  124. avctx->pix_fmt = PIX_FMT_YUV444P10;
  125. break;
  126. default:
  127. av_log(avctx, AV_LOG_ERROR,
  128. "unsupported picture format: %d\n", ctx->pic_format);
  129. return AVERROR_INVALIDDATA;
  130. }
  131. if (ctx->scantable_type != ctx->frame_type) {
  132. if (!ctx->frame_type)
  133. ff_init_scantable(ctx->dsp.idct_permutation, &ctx->scantable,
  134. ff_prores_progressive_scan);
  135. else
  136. ff_init_scantable(ctx->dsp.idct_permutation, &ctx->scantable,
  137. ff_prores_interlaced_scan);
  138. ctx->scantable_type = ctx->frame_type;
  139. }
  140. if (ctx->frame_type) { /* if interlaced */
  141. ctx->picture.interlaced_frame = 1;
  142. ctx->picture.top_field_first = ctx->frame_type & 1;
  143. }
  144. ctx->alpha_info = buf[17] & 0xf;
  145. if (ctx->alpha_info)
  146. av_log_missing_feature(avctx, "alpha channel", 0);
  147. ctx->qmat_changed = 0;
  148. ptr = buf + 20;
  149. flags = buf[19];
  150. if (flags & 2) {
  151. if (ptr - buf > hdr_size - 64) {
  152. av_log(avctx, AV_LOG_ERROR, "header data too small\n");
  153. return AVERROR_INVALIDDATA;
  154. }
  155. if (memcmp(ctx->qmat_luma, ptr, 64)) {
  156. memcpy(ctx->qmat_luma, ptr, 64);
  157. ctx->qmat_changed = 1;
  158. }
  159. ptr += 64;
  160. } else {
  161. memset(ctx->qmat_luma, 4, 64);
  162. ctx->qmat_changed = 1;
  163. }
  164. if (flags & 1) {
  165. if (ptr - buf > hdr_size - 64) {
  166. av_log(avctx, AV_LOG_ERROR, "header data too small\n");
  167. return -1;
  168. }
  169. if (memcmp(ctx->qmat_chroma, ptr, 64)) {
  170. memcpy(ctx->qmat_chroma, ptr, 64);
  171. ctx->qmat_changed = 1;
  172. }
  173. } else {
  174. memset(ctx->qmat_chroma, 4, 64);
  175. ctx->qmat_changed = 1;
  176. }
  177. return hdr_size;
  178. }
  179. static int decode_picture_header(ProresContext *ctx, const uint8_t *buf,
  180. const int data_size, AVCodecContext *avctx)
  181. {
  182. int i, hdr_size, pic_data_size, num_slices;
  183. int slice_width_factor, slice_height_factor;
  184. int remainder, num_x_slices;
  185. const uint8_t *data_ptr, *index_ptr;
  186. hdr_size = data_size > 0 ? buf[0] >> 3 : 0;
  187. if (hdr_size < 8 || hdr_size > data_size) {
  188. av_log(avctx, AV_LOG_ERROR, "picture header too small\n");
  189. return AVERROR_INVALIDDATA;
  190. }
  191. pic_data_size = AV_RB32(buf + 1);
  192. if (pic_data_size > data_size) {
  193. av_log(avctx, AV_LOG_ERROR, "picture data too small\n");
  194. return AVERROR_INVALIDDATA;
  195. }
  196. slice_width_factor = buf[7] >> 4;
  197. slice_height_factor = buf[7] & 0xF;
  198. if (slice_width_factor > 3 || slice_height_factor) {
  199. av_log(avctx, AV_LOG_ERROR,
  200. "unsupported slice dimension: %d x %d\n",
  201. 1 << slice_width_factor, 1 << slice_height_factor);
  202. return AVERROR_INVALIDDATA;
  203. }
  204. ctx->slice_width_factor = slice_width_factor;
  205. ctx->slice_height_factor = slice_height_factor;
  206. ctx->num_x_mbs = (avctx->width + 15) >> 4;
  207. ctx->num_y_mbs = (avctx->height +
  208. (1 << (4 + ctx->picture.interlaced_frame)) - 1) >>
  209. (4 + ctx->picture.interlaced_frame);
  210. remainder = ctx->num_x_mbs & ((1 << slice_width_factor) - 1);
  211. num_x_slices = (ctx->num_x_mbs >> slice_width_factor) + (remainder & 1) +
  212. ((remainder >> 1) & 1) + ((remainder >> 2) & 1);
  213. num_slices = num_x_slices * ctx->num_y_mbs;
  214. if (num_slices != AV_RB16(buf + 5)) {
  215. av_log(avctx, AV_LOG_ERROR, "invalid number of slices\n");
  216. return AVERROR_INVALIDDATA;
  217. }
  218. if (ctx->total_slices != num_slices) {
  219. av_freep(&ctx->slice_data);
  220. ctx->slice_data = av_malloc((num_slices + 1) * sizeof(ctx->slice_data[0]));
  221. if (!ctx->slice_data)
  222. return AVERROR(ENOMEM);
  223. ctx->total_slices = num_slices;
  224. }
  225. if (hdr_size + num_slices * 2 > data_size) {
  226. av_log(avctx, AV_LOG_ERROR, "slice table too small\n");
  227. return AVERROR_INVALIDDATA;
  228. }
  229. /* parse slice table allowing quick access to the slice data */
  230. index_ptr = buf + hdr_size;
  231. data_ptr = index_ptr + num_slices * 2;
  232. for (i = 0; i < num_slices; i++) {
  233. ctx->slice_data[i].index = data_ptr;
  234. ctx->slice_data[i].prev_slice_sf = 0;
  235. data_ptr += AV_RB16(index_ptr + i * 2);
  236. }
  237. ctx->slice_data[i].index = data_ptr;
  238. ctx->slice_data[i].prev_slice_sf = 0;
  239. if (data_ptr > buf + data_size) {
  240. av_log(avctx, AV_LOG_ERROR, "out of slice data\n");
  241. return -1;
  242. }
  243. return pic_data_size;
  244. }
  245. /**
  246. * Read an unsigned rice/exp golomb codeword.
  247. */
  248. static inline int decode_vlc_codeword(GetBitContext *gb, unsigned codebook)
  249. {
  250. unsigned int rice_order, exp_order, switch_bits;
  251. unsigned int buf, code;
  252. int log, prefix_len, len;
  253. OPEN_READER(re, gb);
  254. UPDATE_CACHE(re, gb);
  255. buf = GET_CACHE(re, gb);
  256. /* number of prefix bits to switch between Rice and expGolomb */
  257. switch_bits = (codebook & 3) + 1;
  258. rice_order = codebook >> 5; /* rice code order */
  259. exp_order = (codebook >> 2) & 7; /* exp golomb code order */
  260. log = 31 - av_log2(buf); /* count prefix bits (zeroes) */
  261. if (log < switch_bits) { /* ok, we got a rice code */
  262. if (!rice_order) {
  263. /* shortcut for faster decoding of rice codes without remainder */
  264. code = log;
  265. LAST_SKIP_BITS(re, gb, log + 1);
  266. } else {
  267. prefix_len = log + 1;
  268. code = (log << rice_order) + NEG_USR32(buf << prefix_len, rice_order);
  269. LAST_SKIP_BITS(re, gb, prefix_len + rice_order);
  270. }
  271. } else { /* otherwise we got a exp golomb code */
  272. len = (log << 1) - switch_bits + exp_order + 1;
  273. code = NEG_USR32(buf, len) - (1 << exp_order) + (switch_bits << rice_order);
  274. LAST_SKIP_BITS(re, gb, len);
  275. }
  276. CLOSE_READER(re, gb);
  277. return code;
  278. }
  279. #define LSB2SIGN(x) (-((x) & 1))
  280. #define TOSIGNED(x) (((x) >> 1) ^ LSB2SIGN(x))
  281. /**
  282. * Decode DC coefficients for all blocks in a slice.
  283. */
  284. static inline void decode_dc_coeffs(GetBitContext *gb, DCTELEM *out,
  285. int nblocks)
  286. {
  287. DCTELEM prev_dc;
  288. int i, sign;
  289. int16_t delta;
  290. unsigned int code;
  291. code = decode_vlc_codeword(gb, FIRST_DC_CB);
  292. out[0] = prev_dc = TOSIGNED(code);
  293. out += 64; /* move to the DC coeff of the next block */
  294. delta = 3;
  295. for (i = 1; i < nblocks; i++, out += 64) {
  296. code = decode_vlc_codeword(gb, ff_prores_dc_codebook[FFMIN(FFABS(delta), 3)]);
  297. sign = -(((delta >> 15) & 1) ^ (code & 1));
  298. delta = (((code + 1) >> 1) ^ sign) - sign;
  299. prev_dc += delta;
  300. out[0] = prev_dc;
  301. }
  302. }
  303. /**
  304. * Decode AC coefficients for all blocks in a slice.
  305. */
  306. static inline void decode_ac_coeffs(GetBitContext *gb, DCTELEM *out,
  307. int blocks_per_slice,
  308. int plane_size_factor,
  309. const uint8_t *scan)
  310. {
  311. int pos, block_mask, run, level, sign, run_cb_index, lev_cb_index;
  312. int max_coeffs, bits_left;
  313. /* set initial prediction values */
  314. run = 4;
  315. level = 2;
  316. max_coeffs = blocks_per_slice << 6;
  317. block_mask = blocks_per_slice - 1;
  318. for (pos = blocks_per_slice - 1; pos < max_coeffs;) {
  319. run_cb_index = ff_prores_run_to_cb_index[FFMIN(run, 15)];
  320. lev_cb_index = ff_prores_lev_to_cb_index[FFMIN(level, 9)];
  321. bits_left = get_bits_left(gb);
  322. if (bits_left <= 0 || (bits_left <= 8 && !show_bits(gb, bits_left)))
  323. return;
  324. run = decode_vlc_codeword(gb, ff_prores_ac_codebook[run_cb_index]);
  325. bits_left = get_bits_left(gb);
  326. if (bits_left <= 0 || (bits_left <= 8 && !show_bits(gb, bits_left)))
  327. return;
  328. level = decode_vlc_codeword(gb, ff_prores_ac_codebook[lev_cb_index]) + 1;
  329. pos += run + 1;
  330. if (pos >= max_coeffs)
  331. break;
  332. sign = get_sbits(gb, 1);
  333. out[((pos & block_mask) << 6) + scan[pos >> plane_size_factor]] =
  334. (level ^ sign) - sign;
  335. }
  336. }
  337. /**
  338. * Decode a slice plane (luma or chroma).
  339. */
  340. static void decode_slice_plane(ProresContext *ctx, ProresThreadData *td,
  341. const uint8_t *buf,
  342. int data_size, uint16_t *out_ptr,
  343. int linesize, int mbs_per_slice,
  344. int blocks_per_mb, int plane_size_factor,
  345. const int16_t *qmat)
  346. {
  347. GetBitContext gb;
  348. DCTELEM *block_ptr;
  349. int mb_num, blocks_per_slice;
  350. blocks_per_slice = mbs_per_slice * blocks_per_mb;
  351. memset(td->blocks, 0, 8 * 4 * 64 * sizeof(*td->blocks));
  352. init_get_bits(&gb, buf, data_size << 3);
  353. decode_dc_coeffs(&gb, td->blocks, blocks_per_slice);
  354. decode_ac_coeffs(&gb, td->blocks, blocks_per_slice,
  355. plane_size_factor, ctx->scantable.permutated);
  356. /* inverse quantization, inverse transform and output */
  357. block_ptr = td->blocks;
  358. for (mb_num = 0; mb_num < mbs_per_slice; mb_num++, out_ptr += blocks_per_mb * 4) {
  359. ctx->dsp.idct_put(out_ptr, linesize, block_ptr, qmat);
  360. block_ptr += 64;
  361. if (blocks_per_mb > 2) {
  362. ctx->dsp.idct_put(out_ptr + 8, linesize, block_ptr, qmat);
  363. block_ptr += 64;
  364. }
  365. ctx->dsp.idct_put(out_ptr + linesize * 4, linesize, block_ptr, qmat);
  366. block_ptr += 64;
  367. if (blocks_per_mb > 2) {
  368. ctx->dsp.idct_put(out_ptr + linesize * 4 + 8, linesize, block_ptr, qmat);
  369. block_ptr += 64;
  370. }
  371. }
  372. }
  373. static int decode_slice(AVCodecContext *avctx, void *tdata)
  374. {
  375. ProresThreadData *td = tdata;
  376. ProresContext *ctx = avctx->priv_data;
  377. int mb_x_pos = td->x_pos;
  378. int mb_y_pos = td->y_pos;
  379. int pic_num = ctx->pic_num;
  380. int slice_num = td->slice_num;
  381. int mbs_per_slice = td->slice_width;
  382. const uint8_t *buf;
  383. uint8_t *y_data, *u_data, *v_data;
  384. AVFrame *pic = avctx->coded_frame;
  385. int i, sf, slice_width_factor;
  386. int slice_data_size, hdr_size, y_data_size, u_data_size, v_data_size;
  387. int y_linesize, u_linesize, v_linesize;
  388. buf = ctx->slice_data[slice_num].index;
  389. slice_data_size = ctx->slice_data[slice_num + 1].index - buf;
  390. slice_width_factor = av_log2(mbs_per_slice);
  391. y_data = pic->data[0];
  392. u_data = pic->data[1];
  393. v_data = pic->data[2];
  394. y_linesize = pic->linesize[0];
  395. u_linesize = pic->linesize[1];
  396. v_linesize = pic->linesize[2];
  397. if (pic->interlaced_frame) {
  398. if (!(pic_num ^ pic->top_field_first)) {
  399. y_data += y_linesize;
  400. u_data += u_linesize;
  401. v_data += v_linesize;
  402. }
  403. y_linesize <<= 1;
  404. u_linesize <<= 1;
  405. v_linesize <<= 1;
  406. }
  407. if (slice_data_size < 6) {
  408. av_log(avctx, AV_LOG_ERROR, "slice data too small\n");
  409. return AVERROR_INVALIDDATA;
  410. }
  411. /* parse slice header */
  412. hdr_size = buf[0] >> 3;
  413. y_data_size = AV_RB16(buf + 2);
  414. u_data_size = AV_RB16(buf + 4);
  415. v_data_size = hdr_size > 7 ? AV_RB16(buf + 6) :
  416. slice_data_size - y_data_size - u_data_size - hdr_size;
  417. if (hdr_size + y_data_size + u_data_size + v_data_size > slice_data_size ||
  418. v_data_size < 0 || hdr_size < 6) {
  419. av_log(avctx, AV_LOG_ERROR, "invalid data size\n");
  420. return AVERROR_INVALIDDATA;
  421. }
  422. sf = av_clip(buf[1], 1, 224);
  423. sf = sf > 128 ? (sf - 96) << 2 : sf;
  424. /* scale quantization matrixes according with slice's scale factor */
  425. /* TODO: this can be SIMD-optimized a lot */
  426. if (ctx->qmat_changed || sf != td->prev_slice_sf) {
  427. td->prev_slice_sf = sf;
  428. for (i = 0; i < 64; i++) {
  429. td->qmat_luma_scaled[ctx->dsp.idct_permutation[i]] = ctx->qmat_luma[i] * sf;
  430. td->qmat_chroma_scaled[ctx->dsp.idct_permutation[i]] = ctx->qmat_chroma[i] * sf;
  431. }
  432. }
  433. /* decode luma plane */
  434. decode_slice_plane(ctx, td, buf + hdr_size, y_data_size,
  435. (uint16_t*) (y_data + (mb_y_pos << 4) * y_linesize +
  436. (mb_x_pos << 5)), y_linesize,
  437. mbs_per_slice, 4, slice_width_factor + 2,
  438. td->qmat_luma_scaled);
  439. /* decode U chroma plane */
  440. decode_slice_plane(ctx, td, buf + hdr_size + y_data_size, u_data_size,
  441. (uint16_t*) (u_data + (mb_y_pos << 4) * u_linesize +
  442. (mb_x_pos << ctx->mb_chroma_factor)),
  443. u_linesize, mbs_per_slice, ctx->num_chroma_blocks,
  444. slice_width_factor + ctx->chroma_factor - 1,
  445. td->qmat_chroma_scaled);
  446. /* decode V chroma plane */
  447. decode_slice_plane(ctx, td, buf + hdr_size + y_data_size + u_data_size,
  448. v_data_size,
  449. (uint16_t*) (v_data + (mb_y_pos << 4) * v_linesize +
  450. (mb_x_pos << ctx->mb_chroma_factor)),
  451. v_linesize, mbs_per_slice, ctx->num_chroma_blocks,
  452. slice_width_factor + ctx->chroma_factor - 1,
  453. td->qmat_chroma_scaled);
  454. return 0;
  455. }
  456. static int decode_picture(ProresContext *ctx, int pic_num,
  457. AVCodecContext *avctx)
  458. {
  459. int slice_num, slice_width, x_pos, y_pos;
  460. slice_num = 0;
  461. ctx->pic_num = pic_num;
  462. for (y_pos = 0; y_pos < ctx->num_y_mbs; y_pos++) {
  463. slice_width = 1 << ctx->slice_width_factor;
  464. for (x_pos = 0; x_pos < ctx->num_x_mbs && slice_width;
  465. x_pos += slice_width) {
  466. while (ctx->num_x_mbs - x_pos < slice_width)
  467. slice_width >>= 1;
  468. ctx->slice_data[slice_num].slice_num = slice_num;
  469. ctx->slice_data[slice_num].x_pos = x_pos;
  470. ctx->slice_data[slice_num].y_pos = y_pos;
  471. ctx->slice_data[slice_num].slice_width = slice_width;
  472. slice_num++;
  473. }
  474. }
  475. return avctx->execute(avctx, decode_slice,
  476. ctx->slice_data, NULL, slice_num,
  477. sizeof(ctx->slice_data[0]));
  478. }
  479. #define MOVE_DATA_PTR(nbytes) buf += (nbytes); buf_size -= (nbytes)
  480. static int decode_frame(AVCodecContext *avctx, void *data, int *data_size,
  481. AVPacket *avpkt)
  482. {
  483. ProresContext *ctx = avctx->priv_data;
  484. AVFrame *picture = avctx->coded_frame;
  485. const uint8_t *buf = avpkt->data;
  486. int buf_size = avpkt->size;
  487. int frame_hdr_size, pic_num, pic_data_size;
  488. /* check frame atom container */
  489. if (buf_size < 28 || buf_size < AV_RB32(buf) ||
  490. AV_RB32(buf + 4) != FRAME_ID) {
  491. av_log(avctx, AV_LOG_ERROR, "invalid frame\n");
  492. return AVERROR_INVALIDDATA;
  493. }
  494. MOVE_DATA_PTR(8);
  495. frame_hdr_size = decode_frame_header(ctx, buf, buf_size, avctx);
  496. if (frame_hdr_size < 0)
  497. return AVERROR_INVALIDDATA;
  498. MOVE_DATA_PTR(frame_hdr_size);
  499. if (picture->data[0])
  500. avctx->release_buffer(avctx, picture);
  501. picture->reference = 0;
  502. if (avctx->get_buffer(avctx, picture) < 0)
  503. return -1;
  504. for (pic_num = 0; ctx->picture.interlaced_frame - pic_num + 1; pic_num++) {
  505. pic_data_size = decode_picture_header(ctx, buf, buf_size, avctx);
  506. if (pic_data_size < 0)
  507. return AVERROR_INVALIDDATA;
  508. if (decode_picture(ctx, pic_num, avctx))
  509. return -1;
  510. MOVE_DATA_PTR(pic_data_size);
  511. }
  512. *data_size = sizeof(AVPicture);
  513. *(AVFrame*) data = *avctx->coded_frame;
  514. return avpkt->size;
  515. }
  516. static av_cold int decode_close(AVCodecContext *avctx)
  517. {
  518. ProresContext *ctx = avctx->priv_data;
  519. if (ctx->picture.data[0])
  520. avctx->release_buffer(avctx, &ctx->picture);
  521. av_freep(&ctx->slice_data);
  522. return 0;
  523. }
  524. AVCodec ff_prores_decoder = {
  525. .name = "prores",
  526. .type = AVMEDIA_TYPE_VIDEO,
  527. .id = CODEC_ID_PRORES,
  528. .priv_data_size = sizeof(ProresContext),
  529. .init = decode_init,
  530. .close = decode_close,
  531. .decode = decode_frame,
  532. .capabilities = CODEC_CAP_DR1 | CODEC_CAP_SLICE_THREADS,
  533. .long_name = NULL_IF_CONFIG_SMALL("Apple ProRes (iCodec Pro)")
  534. };