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.

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