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.

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