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.

674 lines
22KB

  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, avctx);
  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. avctx->color_primaries = buf[14];
  145. avctx->color_trc = buf[15];
  146. avctx->colorspace = buf[16];
  147. ctx->alpha_info = buf[17] & 0xf;
  148. if (ctx->alpha_info)
  149. av_log_missing_feature(avctx, "alpha channel", 0);
  150. ctx->qmat_changed = 0;
  151. ptr = buf + 20;
  152. flags = buf[19];
  153. if (flags & 2) {
  154. if (ptr - buf > hdr_size - 64) {
  155. av_log(avctx, AV_LOG_ERROR, "header data too small\n");
  156. return AVERROR_INVALIDDATA;
  157. }
  158. if (memcmp(ctx->qmat_luma, ptr, 64)) {
  159. memcpy(ctx->qmat_luma, ptr, 64);
  160. ctx->qmat_changed = 1;
  161. }
  162. ptr += 64;
  163. } else {
  164. memset(ctx->qmat_luma, 4, 64);
  165. ctx->qmat_changed = 1;
  166. }
  167. if (flags & 1) {
  168. if (ptr - buf > hdr_size - 64) {
  169. av_log(avctx, AV_LOG_ERROR, "header data too small\n");
  170. return -1;
  171. }
  172. if (memcmp(ctx->qmat_chroma, ptr, 64)) {
  173. memcpy(ctx->qmat_chroma, ptr, 64);
  174. ctx->qmat_changed = 1;
  175. }
  176. } else {
  177. memset(ctx->qmat_chroma, 4, 64);
  178. ctx->qmat_changed = 1;
  179. }
  180. return hdr_size;
  181. }
  182. static int decode_picture_header(ProresContext *ctx, const uint8_t *buf,
  183. const int data_size, AVCodecContext *avctx)
  184. {
  185. int i, hdr_size, pic_data_size, num_slices;
  186. int slice_width_factor, slice_height_factor;
  187. int remainder, num_x_slices;
  188. const uint8_t *data_ptr, *index_ptr;
  189. hdr_size = data_size > 0 ? buf[0] >> 3 : 0;
  190. if (hdr_size < 8 || hdr_size > data_size) {
  191. av_log(avctx, AV_LOG_ERROR, "picture header too small\n");
  192. return AVERROR_INVALIDDATA;
  193. }
  194. pic_data_size = AV_RB32(buf + 1);
  195. if (pic_data_size > data_size) {
  196. av_log(avctx, AV_LOG_ERROR, "picture data too small\n");
  197. return AVERROR_INVALIDDATA;
  198. }
  199. slice_width_factor = buf[7] >> 4;
  200. slice_height_factor = buf[7] & 0xF;
  201. if (slice_width_factor > 3 || slice_height_factor) {
  202. av_log(avctx, AV_LOG_ERROR,
  203. "unsupported slice dimension: %d x %d\n",
  204. 1 << slice_width_factor, 1 << slice_height_factor);
  205. return AVERROR_INVALIDDATA;
  206. }
  207. ctx->slice_width_factor = slice_width_factor;
  208. ctx->slice_height_factor = slice_height_factor;
  209. ctx->num_x_mbs = (avctx->width + 15) >> 4;
  210. ctx->num_y_mbs = (avctx->height +
  211. (1 << (4 + ctx->picture.interlaced_frame)) - 1) >>
  212. (4 + ctx->picture.interlaced_frame);
  213. remainder = ctx->num_x_mbs & ((1 << slice_width_factor) - 1);
  214. num_x_slices = (ctx->num_x_mbs >> slice_width_factor) + (remainder & 1) +
  215. ((remainder >> 1) & 1) + ((remainder >> 2) & 1);
  216. num_slices = num_x_slices * ctx->num_y_mbs;
  217. if (num_slices != AV_RB16(buf + 5)) {
  218. av_log(avctx, AV_LOG_ERROR, "invalid number of slices\n");
  219. return AVERROR_INVALIDDATA;
  220. }
  221. if (ctx->total_slices != num_slices) {
  222. av_freep(&ctx->slice_data);
  223. ctx->slice_data = av_malloc((num_slices + 1) * sizeof(ctx->slice_data[0]));
  224. if (!ctx->slice_data)
  225. return AVERROR(ENOMEM);
  226. ctx->total_slices = num_slices;
  227. }
  228. if (hdr_size + num_slices * 2 > data_size) {
  229. av_log(avctx, AV_LOG_ERROR, "slice table too small\n");
  230. return AVERROR_INVALIDDATA;
  231. }
  232. /* parse slice table allowing quick access to the slice data */
  233. index_ptr = buf + hdr_size;
  234. data_ptr = index_ptr + num_slices * 2;
  235. for (i = 0; i < num_slices; i++) {
  236. ctx->slice_data[i].index = data_ptr;
  237. ctx->slice_data[i].prev_slice_sf = 0;
  238. data_ptr += AV_RB16(index_ptr + i * 2);
  239. }
  240. ctx->slice_data[i].index = data_ptr;
  241. ctx->slice_data[i].prev_slice_sf = 0;
  242. if (data_ptr > buf + data_size) {
  243. av_log(avctx, AV_LOG_ERROR, "out of slice data\n");
  244. return -1;
  245. }
  246. return pic_data_size;
  247. }
  248. /**
  249. * Read an unsigned rice/exp golomb codeword.
  250. */
  251. static inline int decode_vlc_codeword(GetBitContext *gb, unsigned codebook)
  252. {
  253. unsigned int rice_order, exp_order, switch_bits;
  254. unsigned int buf, code;
  255. int log, prefix_len, len;
  256. OPEN_READER(re, gb);
  257. UPDATE_CACHE(re, gb);
  258. buf = GET_CACHE(re, gb);
  259. /* number of prefix bits to switch between Rice and expGolomb */
  260. switch_bits = (codebook & 3) + 1;
  261. rice_order = codebook >> 5; /* rice code order */
  262. exp_order = (codebook >> 2) & 7; /* exp golomb code order */
  263. log = 31 - av_log2(buf); /* count prefix bits (zeroes) */
  264. if (log < switch_bits) { /* ok, we got a rice code */
  265. if (!rice_order) {
  266. /* shortcut for faster decoding of rice codes without remainder */
  267. code = log;
  268. LAST_SKIP_BITS(re, gb, log + 1);
  269. } else {
  270. prefix_len = log + 1;
  271. code = (log << rice_order) + NEG_USR32(buf << prefix_len, rice_order);
  272. LAST_SKIP_BITS(re, gb, prefix_len + rice_order);
  273. }
  274. } else { /* otherwise we got a exp golomb code */
  275. len = (log << 1) - switch_bits + exp_order + 1;
  276. code = NEG_USR32(buf, len) - (1 << exp_order) + (switch_bits << rice_order);
  277. LAST_SKIP_BITS(re, gb, len);
  278. }
  279. CLOSE_READER(re, gb);
  280. return code;
  281. }
  282. #define LSB2SIGN(x) (-((x) & 1))
  283. #define TOSIGNED(x) (((x) >> 1) ^ LSB2SIGN(x))
  284. /**
  285. * Decode DC coefficients for all blocks in a slice.
  286. */
  287. static inline void decode_dc_coeffs(GetBitContext *gb, DCTELEM *out,
  288. int nblocks)
  289. {
  290. DCTELEM prev_dc;
  291. int i, sign;
  292. int16_t delta;
  293. unsigned int code;
  294. code = decode_vlc_codeword(gb, FIRST_DC_CB);
  295. out[0] = prev_dc = TOSIGNED(code);
  296. out += 64; /* move to the DC coeff of the next block */
  297. delta = 3;
  298. for (i = 1; i < nblocks; i++, out += 64) {
  299. code = decode_vlc_codeword(gb, ff_prores_dc_codebook[FFMIN(FFABS(delta), 3)]);
  300. sign = -(((delta >> 15) & 1) ^ (code & 1));
  301. delta = (((code + 1) >> 1) ^ sign) - sign;
  302. prev_dc += delta;
  303. out[0] = prev_dc;
  304. }
  305. }
  306. /**
  307. * Decode AC coefficients for all blocks in a slice.
  308. */
  309. static inline void decode_ac_coeffs(GetBitContext *gb, DCTELEM *out,
  310. int blocks_per_slice,
  311. int plane_size_factor,
  312. const uint8_t *scan)
  313. {
  314. int pos, block_mask, run, level, sign, run_cb_index, lev_cb_index;
  315. int max_coeffs, bits_left;
  316. /* set initial prediction values */
  317. run = 4;
  318. level = 2;
  319. max_coeffs = blocks_per_slice << 6;
  320. block_mask = blocks_per_slice - 1;
  321. for (pos = blocks_per_slice - 1; pos < max_coeffs;) {
  322. run_cb_index = ff_prores_run_to_cb_index[FFMIN(run, 15)];
  323. lev_cb_index = ff_prores_lev_to_cb_index[FFMIN(level, 9)];
  324. bits_left = get_bits_left(gb);
  325. if (bits_left <= 0 || (bits_left <= 8 && !show_bits(gb, bits_left)))
  326. return;
  327. run = decode_vlc_codeword(gb, ff_prores_ac_codebook[run_cb_index]);
  328. bits_left = get_bits_left(gb);
  329. if (bits_left <= 0 || (bits_left <= 8 && !show_bits(gb, bits_left)))
  330. return;
  331. level = decode_vlc_codeword(gb, ff_prores_ac_codebook[lev_cb_index]) + 1;
  332. pos += run + 1;
  333. if (pos >= max_coeffs)
  334. break;
  335. sign = get_sbits(gb, 1);
  336. out[((pos & block_mask) << 6) + scan[pos >> plane_size_factor]] =
  337. (level ^ sign) - sign;
  338. }
  339. }
  340. /**
  341. * Decode a slice plane (luma or chroma).
  342. */
  343. static void decode_slice_plane(ProresContext *ctx, ProresThreadData *td,
  344. const uint8_t *buf,
  345. int data_size, uint16_t *out_ptr,
  346. int linesize, int mbs_per_slice,
  347. int blocks_per_mb, int plane_size_factor,
  348. const int16_t *qmat, int is_chroma)
  349. {
  350. GetBitContext gb;
  351. DCTELEM *block_ptr;
  352. int mb_num, blocks_per_slice;
  353. blocks_per_slice = mbs_per_slice * blocks_per_mb;
  354. memset(td->blocks, 0, 8 * 4 * 64 * sizeof(*td->blocks));
  355. init_get_bits(&gb, buf, data_size << 3);
  356. decode_dc_coeffs(&gb, td->blocks, blocks_per_slice);
  357. decode_ac_coeffs(&gb, td->blocks, blocks_per_slice,
  358. plane_size_factor, ctx->scantable.permutated);
  359. /* inverse quantization, inverse transform and output */
  360. block_ptr = td->blocks;
  361. if (!is_chroma) {
  362. for (mb_num = 0; mb_num < mbs_per_slice; mb_num++, out_ptr += blocks_per_mb * 4) {
  363. ctx->dsp.idct_put(out_ptr, linesize, block_ptr, qmat);
  364. block_ptr += 64;
  365. if (blocks_per_mb > 2) {
  366. ctx->dsp.idct_put(out_ptr + 8, linesize, block_ptr, qmat);
  367. block_ptr += 64;
  368. }
  369. ctx->dsp.idct_put(out_ptr + linesize * 4, linesize, block_ptr, qmat);
  370. block_ptr += 64;
  371. if (blocks_per_mb > 2) {
  372. ctx->dsp.idct_put(out_ptr + linesize * 4 + 8, linesize, block_ptr, qmat);
  373. block_ptr += 64;
  374. }
  375. }
  376. } else {
  377. for (mb_num = 0; mb_num < mbs_per_slice; mb_num++, out_ptr += blocks_per_mb * 4) {
  378. ctx->dsp.idct_put(out_ptr, linesize, block_ptr, qmat);
  379. block_ptr += 64;
  380. ctx->dsp.idct_put(out_ptr + linesize * 4, linesize, block_ptr, qmat);
  381. block_ptr += 64;
  382. if (blocks_per_mb > 2) {
  383. ctx->dsp.idct_put(out_ptr + 8, linesize, block_ptr, qmat);
  384. block_ptr += 64;
  385. ctx->dsp.idct_put(out_ptr + linesize * 4 + 8, linesize, block_ptr, qmat);
  386. block_ptr += 64;
  387. }
  388. }
  389. }
  390. }
  391. static int decode_slice(AVCodecContext *avctx, void *tdata)
  392. {
  393. ProresThreadData *td = tdata;
  394. ProresContext *ctx = avctx->priv_data;
  395. int mb_x_pos = td->x_pos;
  396. int mb_y_pos = td->y_pos;
  397. int pic_num = ctx->pic_num;
  398. int slice_num = td->slice_num;
  399. int mbs_per_slice = td->slice_width;
  400. const uint8_t *buf;
  401. uint8_t *y_data, *u_data, *v_data;
  402. AVFrame *pic = avctx->coded_frame;
  403. int i, sf, slice_width_factor;
  404. int slice_data_size, hdr_size, y_data_size, u_data_size, v_data_size;
  405. int y_linesize, u_linesize, v_linesize;
  406. buf = ctx->slice_data[slice_num].index;
  407. slice_data_size = ctx->slice_data[slice_num + 1].index - buf;
  408. slice_width_factor = av_log2(mbs_per_slice);
  409. y_data = pic->data[0];
  410. u_data = pic->data[1];
  411. v_data = pic->data[2];
  412. y_linesize = pic->linesize[0];
  413. u_linesize = pic->linesize[1];
  414. v_linesize = pic->linesize[2];
  415. if (pic->interlaced_frame) {
  416. if (!(pic_num ^ pic->top_field_first)) {
  417. y_data += y_linesize;
  418. u_data += u_linesize;
  419. v_data += v_linesize;
  420. }
  421. y_linesize <<= 1;
  422. u_linesize <<= 1;
  423. v_linesize <<= 1;
  424. }
  425. if (slice_data_size < 6) {
  426. av_log(avctx, AV_LOG_ERROR, "slice data too small\n");
  427. return AVERROR_INVALIDDATA;
  428. }
  429. /* parse slice header */
  430. hdr_size = buf[0] >> 3;
  431. y_data_size = AV_RB16(buf + 2);
  432. u_data_size = AV_RB16(buf + 4);
  433. v_data_size = hdr_size > 7 ? AV_RB16(buf + 6) :
  434. slice_data_size - y_data_size - u_data_size - hdr_size;
  435. if (hdr_size + y_data_size + u_data_size + v_data_size > slice_data_size ||
  436. v_data_size < 0 || hdr_size < 6) {
  437. av_log(avctx, AV_LOG_ERROR, "invalid data size\n");
  438. return AVERROR_INVALIDDATA;
  439. }
  440. sf = av_clip(buf[1], 1, 224);
  441. sf = sf > 128 ? (sf - 96) << 2 : sf;
  442. /* scale quantization matrixes according with slice's scale factor */
  443. /* TODO: this can be SIMD-optimized a lot */
  444. if (ctx->qmat_changed || sf != td->prev_slice_sf) {
  445. td->prev_slice_sf = sf;
  446. for (i = 0; i < 64; i++) {
  447. td->qmat_luma_scaled[ctx->dsp.idct_permutation[i]] = ctx->qmat_luma[i] * sf;
  448. td->qmat_chroma_scaled[ctx->dsp.idct_permutation[i]] = ctx->qmat_chroma[i] * sf;
  449. }
  450. }
  451. /* decode luma plane */
  452. decode_slice_plane(ctx, td, buf + hdr_size, y_data_size,
  453. (uint16_t*) (y_data + (mb_y_pos << 4) * y_linesize +
  454. (mb_x_pos << 5)), y_linesize,
  455. mbs_per_slice, 4, slice_width_factor + 2,
  456. td->qmat_luma_scaled, 0);
  457. /* decode U chroma plane */
  458. decode_slice_plane(ctx, td, buf + hdr_size + y_data_size, u_data_size,
  459. (uint16_t*) (u_data + (mb_y_pos << 4) * u_linesize +
  460. (mb_x_pos << ctx->mb_chroma_factor)),
  461. u_linesize, mbs_per_slice, ctx->num_chroma_blocks,
  462. slice_width_factor + ctx->chroma_factor - 1,
  463. td->qmat_chroma_scaled, 1);
  464. /* decode V chroma plane */
  465. decode_slice_plane(ctx, td, buf + hdr_size + y_data_size + u_data_size,
  466. v_data_size,
  467. (uint16_t*) (v_data + (mb_y_pos << 4) * v_linesize +
  468. (mb_x_pos << ctx->mb_chroma_factor)),
  469. v_linesize, mbs_per_slice, ctx->num_chroma_blocks,
  470. slice_width_factor + ctx->chroma_factor - 1,
  471. td->qmat_chroma_scaled, 1);
  472. return 0;
  473. }
  474. static int decode_picture(ProresContext *ctx, int pic_num,
  475. AVCodecContext *avctx)
  476. {
  477. int slice_num, slice_width, x_pos, y_pos;
  478. slice_num = 0;
  479. ctx->pic_num = pic_num;
  480. for (y_pos = 0; y_pos < ctx->num_y_mbs; y_pos++) {
  481. slice_width = 1 << ctx->slice_width_factor;
  482. for (x_pos = 0; x_pos < ctx->num_x_mbs && slice_width;
  483. x_pos += slice_width) {
  484. while (ctx->num_x_mbs - x_pos < slice_width)
  485. slice_width >>= 1;
  486. ctx->slice_data[slice_num].slice_num = slice_num;
  487. ctx->slice_data[slice_num].x_pos = x_pos;
  488. ctx->slice_data[slice_num].y_pos = y_pos;
  489. ctx->slice_data[slice_num].slice_width = slice_width;
  490. slice_num++;
  491. }
  492. }
  493. return avctx->execute(avctx, decode_slice,
  494. ctx->slice_data, NULL, slice_num,
  495. sizeof(ctx->slice_data[0]));
  496. }
  497. #define MOVE_DATA_PTR(nbytes) buf += (nbytes); buf_size -= (nbytes)
  498. static int decode_frame(AVCodecContext *avctx, void *data, int *data_size,
  499. AVPacket *avpkt)
  500. {
  501. ProresContext *ctx = avctx->priv_data;
  502. AVFrame *picture = avctx->coded_frame;
  503. const uint8_t *buf = avpkt->data;
  504. int buf_size = avpkt->size;
  505. int frame_hdr_size, pic_num, pic_data_size;
  506. /* check frame atom container */
  507. if (buf_size < 28 || buf_size < AV_RB32(buf) ||
  508. AV_RB32(buf + 4) != FRAME_ID) {
  509. av_log(avctx, AV_LOG_ERROR, "invalid frame\n");
  510. return AVERROR_INVALIDDATA;
  511. }
  512. MOVE_DATA_PTR(8);
  513. frame_hdr_size = decode_frame_header(ctx, buf, buf_size, avctx);
  514. if (frame_hdr_size < 0)
  515. return AVERROR_INVALIDDATA;
  516. MOVE_DATA_PTR(frame_hdr_size);
  517. if (picture->data[0])
  518. avctx->release_buffer(avctx, picture);
  519. picture->reference = 0;
  520. if (avctx->get_buffer(avctx, picture) < 0)
  521. return -1;
  522. for (pic_num = 0; ctx->picture.interlaced_frame - pic_num + 1; pic_num++) {
  523. pic_data_size = decode_picture_header(ctx, buf, buf_size, avctx);
  524. if (pic_data_size < 0)
  525. return AVERROR_INVALIDDATA;
  526. if (decode_picture(ctx, pic_num, avctx))
  527. return -1;
  528. MOVE_DATA_PTR(pic_data_size);
  529. }
  530. *data_size = sizeof(AVPicture);
  531. *(AVFrame*) data = *avctx->coded_frame;
  532. return avpkt->size;
  533. }
  534. static av_cold int decode_close(AVCodecContext *avctx)
  535. {
  536. ProresContext *ctx = avctx->priv_data;
  537. if (ctx->picture.data[0])
  538. avctx->release_buffer(avctx, &ctx->picture);
  539. av_freep(&ctx->slice_data);
  540. return 0;
  541. }
  542. AVCodec ff_prores_lgpl_decoder = {
  543. .name = "prores_lgpl",
  544. .type = AVMEDIA_TYPE_VIDEO,
  545. .id = AV_CODEC_ID_PRORES,
  546. .priv_data_size = sizeof(ProresContext),
  547. .init = decode_init,
  548. .close = decode_close,
  549. .decode = decode_frame,
  550. .capabilities = CODEC_CAP_DR1 | CODEC_CAP_SLICE_THREADS,
  551. .long_name = NULL_IF_CONFIG_SMALL("Apple ProRes (iCodec Pro)")
  552. };