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.

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