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.

707 lines
23KB

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