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.

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