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.

783 lines
25KB

  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 "idctdsp.h"
  34. #include "internal.h"
  35. #include "proresdata.h"
  36. #include "proresdsp.h"
  37. #include "get_bits.h"
  38. typedef struct ProresThreadData {
  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 ProresContext {
  49. ProresDSPContext dsp;
  50. AVFrame *frame;
  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);
  79. ctx->scantable_type = -1; // set scantable type to uninitialized
  80. memset(ctx->qmat_luma, 4, 64);
  81. memset(ctx->qmat_chroma, 4, 64);
  82. return 0;
  83. }
  84. static int decode_frame_header(ProresContext *ctx, const uint8_t *buf,
  85. const int data_size, AVCodecContext *avctx)
  86. {
  87. int hdr_size, version, width, height, flags;
  88. const uint8_t *ptr;
  89. hdr_size = AV_RB16(buf);
  90. if (hdr_size > data_size) {
  91. av_log(avctx, AV_LOG_ERROR, "frame data too small\n");
  92. return AVERROR_INVALIDDATA;
  93. }
  94. version = AV_RB16(buf + 2);
  95. if (version >= 2) {
  96. av_log(avctx, AV_LOG_ERROR,
  97. "unsupported header version: %d\n", version);
  98. return AVERROR_INVALIDDATA;
  99. }
  100. width = AV_RB16(buf + 8);
  101. height = AV_RB16(buf + 10);
  102. if (width != avctx->width || height != avctx->height) {
  103. av_log(avctx, AV_LOG_ERROR,
  104. "picture dimension changed: old: %d x %d, new: %d x %d\n",
  105. avctx->width, avctx->height, width, height);
  106. return AVERROR_INVALIDDATA;
  107. }
  108. ctx->frame_type = (buf[12] >> 2) & 3;
  109. if (ctx->frame_type > 2) {
  110. av_log(avctx, AV_LOG_ERROR,
  111. "unsupported frame type: %d\n", ctx->frame_type);
  112. return AVERROR_INVALIDDATA;
  113. }
  114. ctx->chroma_factor = (buf[12] >> 6) & 3;
  115. ctx->mb_chroma_factor = ctx->chroma_factor + 2;
  116. ctx->num_chroma_blocks = (1 << ctx->chroma_factor) >> 1;
  117. ctx->alpha_info = buf[17] & 0xf;
  118. if (ctx->alpha_info > 2) {
  119. av_log(avctx, AV_LOG_ERROR, "Invalid alpha mode %d\n", ctx->alpha_info);
  120. return AVERROR_INVALIDDATA;
  121. }
  122. switch (ctx->chroma_factor) {
  123. case 2:
  124. avctx->pix_fmt = ctx->alpha_info ? AV_PIX_FMT_YUVA422P10
  125. : AV_PIX_FMT_YUV422P10;
  126. break;
  127. case 3:
  128. avctx->pix_fmt = ctx->alpha_info ? AV_PIX_FMT_YUVA444P10
  129. : AV_PIX_FMT_YUV444P10;
  130. break;
  131. default:
  132. av_log(avctx, AV_LOG_ERROR,
  133. "unsupported picture format: %d\n", ctx->pic_format);
  134. return AVERROR_INVALIDDATA;
  135. }
  136. if (ctx->scantable_type != ctx->frame_type) {
  137. if (!ctx->frame_type)
  138. ff_init_scantable(ctx->dsp.idct_permutation, &ctx->scantable,
  139. ff_prores_progressive_scan);
  140. else
  141. ff_init_scantable(ctx->dsp.idct_permutation, &ctx->scantable,
  142. ff_prores_interlaced_scan);
  143. ctx->scantable_type = ctx->frame_type;
  144. }
  145. if (ctx->frame_type) { /* if interlaced */
  146. ctx->frame->interlaced_frame = 1;
  147. ctx->frame->top_field_first = ctx->frame_type & 1;
  148. } else {
  149. ctx->frame->interlaced_frame = 0;
  150. }
  151. avctx->color_primaries = buf[14];
  152. avctx->color_trc = buf[15];
  153. avctx->colorspace = buf[16];
  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->frame->interlaced_frame)) - 1) >>
  216. (4 + ctx->frame->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. #define MAX_PADDING 16
  311. /**
  312. * Decode AC coefficients for all blocks in a slice.
  313. */
  314. static inline int decode_ac_coeffs(GetBitContext *gb, int16_t *out,
  315. int blocks_per_slice,
  316. int plane_size_factor,
  317. const uint8_t *scan)
  318. {
  319. int pos, block_mask, run, level, sign, run_cb_index, lev_cb_index;
  320. int max_coeffs, bits_left;
  321. /* set initial prediction values */
  322. run = 4;
  323. level = 2;
  324. max_coeffs = blocks_per_slice << 6;
  325. block_mask = blocks_per_slice - 1;
  326. for (pos = blocks_per_slice - 1; pos < max_coeffs;) {
  327. run_cb_index = ff_prores_run_to_cb_index[FFMIN(run, 15)];
  328. lev_cb_index = ff_prores_lev_to_cb_index[FFMIN(level, 9)];
  329. bits_left = get_bits_left(gb);
  330. if (bits_left <= 0 || (bits_left <= MAX_PADDING && !show_bits(gb, bits_left)))
  331. return 0;
  332. run = decode_vlc_codeword(gb, ff_prores_ac_codebook[run_cb_index]);
  333. if (run < 0)
  334. return AVERROR_INVALIDDATA;
  335. bits_left = get_bits_left(gb);
  336. if (bits_left <= 0 || (bits_left <= MAX_PADDING && !show_bits(gb, bits_left)))
  337. return AVERROR_INVALIDDATA;
  338. level = decode_vlc_codeword(gb, ff_prores_ac_codebook[lev_cb_index]) + 1;
  339. if (level < 0)
  340. return AVERROR_INVALIDDATA;
  341. pos += run + 1;
  342. if (pos >= max_coeffs)
  343. break;
  344. sign = get_sbits(gb, 1);
  345. out[((pos & block_mask) << 6) + scan[pos >> plane_size_factor]] =
  346. (level ^ sign) - sign;
  347. }
  348. return 0;
  349. }
  350. /**
  351. * Decode a slice plane (luma or chroma).
  352. */
  353. static int decode_slice_plane(ProresContext *ctx, ProresThreadData *td,
  354. const uint8_t *buf,
  355. int data_size, uint16_t *out_ptr,
  356. int linesize, int mbs_per_slice,
  357. int blocks_per_mb, int plane_size_factor,
  358. const int16_t *qmat, int is_chroma)
  359. {
  360. GetBitContext gb;
  361. int16_t *block_ptr;
  362. int mb_num, blocks_per_slice, ret;
  363. blocks_per_slice = mbs_per_slice * blocks_per_mb;
  364. memset(td->blocks, 0, 8 * 4 * 64 * sizeof(*td->blocks));
  365. init_get_bits(&gb, buf, data_size << 3);
  366. decode_dc_coeffs(&gb, td->blocks, blocks_per_slice);
  367. ret = decode_ac_coeffs(&gb, td->blocks, blocks_per_slice,
  368. plane_size_factor, ctx->scantable.permutated);
  369. if (ret < 0)
  370. return ret;
  371. /* inverse quantization, inverse transform and output */
  372. block_ptr = td->blocks;
  373. if (!is_chroma) {
  374. for (mb_num = 0; mb_num < mbs_per_slice; mb_num++, out_ptr += blocks_per_mb * 4) {
  375. ctx->dsp.idct_put(out_ptr, linesize, block_ptr, qmat);
  376. block_ptr += 64;
  377. if (blocks_per_mb > 2) {
  378. ctx->dsp.idct_put(out_ptr + 8, linesize, block_ptr, qmat);
  379. block_ptr += 64;
  380. }
  381. ctx->dsp.idct_put(out_ptr + linesize * 4, linesize, block_ptr, qmat);
  382. block_ptr += 64;
  383. if (blocks_per_mb > 2) {
  384. ctx->dsp.idct_put(out_ptr + linesize * 4 + 8, linesize, block_ptr, qmat);
  385. block_ptr += 64;
  386. }
  387. }
  388. } else {
  389. for (mb_num = 0; mb_num < mbs_per_slice; mb_num++, out_ptr += blocks_per_mb * 4) {
  390. ctx->dsp.idct_put(out_ptr, linesize, block_ptr, qmat);
  391. block_ptr += 64;
  392. ctx->dsp.idct_put(out_ptr + linesize * 4, linesize, block_ptr, qmat);
  393. block_ptr += 64;
  394. if (blocks_per_mb > 2) {
  395. ctx->dsp.idct_put(out_ptr + 8, linesize, block_ptr, qmat);
  396. block_ptr += 64;
  397. ctx->dsp.idct_put(out_ptr + linesize * 4 + 8, linesize, block_ptr, qmat);
  398. block_ptr += 64;
  399. }
  400. }
  401. }
  402. return 0;
  403. }
  404. static void unpack_alpha(GetBitContext *gb, uint16_t *dst, int num_coeffs,
  405. const int num_bits)
  406. {
  407. const int mask = (1 << num_bits) - 1;
  408. int i, idx, val, alpha_val;
  409. idx = 0;
  410. alpha_val = mask;
  411. do {
  412. do {
  413. if (get_bits1(gb))
  414. val = get_bits(gb, num_bits);
  415. else {
  416. int sign;
  417. val = get_bits(gb, num_bits == 16 ? 7 : 4);
  418. sign = val & 1;
  419. val = (val + 2) >> 1;
  420. if (sign)
  421. val = -val;
  422. }
  423. alpha_val = (alpha_val + val) & mask;
  424. if (num_bits == 16)
  425. dst[idx++] = alpha_val >> 6;
  426. else
  427. dst[idx++] = (alpha_val << 2) | (alpha_val >> 6);
  428. if (idx >= num_coeffs - 1)
  429. break;
  430. } while (get_bits1(gb));
  431. val = get_bits(gb, 4);
  432. if (!val)
  433. val = get_bits(gb, 11);
  434. if (idx + val > num_coeffs)
  435. val = num_coeffs - idx;
  436. if (num_bits == 16)
  437. for (i = 0; i < val; i++)
  438. dst[idx++] = alpha_val >> 6;
  439. else
  440. for (i = 0; i < val; i++)
  441. dst[idx++] = (alpha_val << 2) | (alpha_val >> 6);
  442. } while (idx < num_coeffs);
  443. }
  444. /**
  445. * Decode alpha slice plane.
  446. */
  447. static void decode_alpha_plane(ProresContext *ctx, ProresThreadData *td,
  448. const uint8_t *buf, int data_size,
  449. uint16_t *out_ptr, int linesize,
  450. int mbs_per_slice)
  451. {
  452. GetBitContext gb;
  453. int i;
  454. uint16_t *block_ptr;
  455. memset(td->blocks, 0, 8 * 4 * 64 * sizeof(*td->blocks));
  456. init_get_bits(&gb, buf, data_size << 3);
  457. if (ctx->alpha_info == 2)
  458. unpack_alpha(&gb, td->blocks, mbs_per_slice * 4 * 64, 16);
  459. else
  460. unpack_alpha(&gb, td->blocks, mbs_per_slice * 4 * 64, 8);
  461. block_ptr = td->blocks;
  462. for (i = 0; i < 16; i++) {
  463. memcpy(out_ptr, block_ptr, 16 * mbs_per_slice * sizeof(*out_ptr));
  464. out_ptr += linesize >> 1;
  465. block_ptr += 16 * mbs_per_slice;
  466. }
  467. }
  468. static int decode_slice(AVCodecContext *avctx, void *tdata)
  469. {
  470. ProresThreadData *td = tdata;
  471. ProresContext *ctx = avctx->priv_data;
  472. int mb_x_pos = td->x_pos;
  473. int mb_y_pos = td->y_pos;
  474. int pic_num = ctx->pic_num;
  475. int slice_num = td->slice_num;
  476. int mbs_per_slice = td->slice_width;
  477. const uint8_t *buf;
  478. uint8_t *y_data, *u_data, *v_data, *a_data;
  479. AVFrame *pic = ctx->frame;
  480. int i, sf, slice_width_factor;
  481. int slice_data_size, hdr_size;
  482. int y_data_size, u_data_size, v_data_size, a_data_size;
  483. int y_linesize, u_linesize, v_linesize, a_linesize;
  484. int coff[4];
  485. int ret;
  486. buf = ctx->slice_data[slice_num].index;
  487. slice_data_size = ctx->slice_data[slice_num + 1].index - buf;
  488. slice_width_factor = av_log2(mbs_per_slice);
  489. y_data = pic->data[0];
  490. u_data = pic->data[1];
  491. v_data = pic->data[2];
  492. a_data = pic->data[3];
  493. y_linesize = pic->linesize[0];
  494. u_linesize = pic->linesize[1];
  495. v_linesize = pic->linesize[2];
  496. a_linesize = pic->linesize[3];
  497. if (pic->interlaced_frame) {
  498. if (!(pic_num ^ pic->top_field_first)) {
  499. y_data += y_linesize;
  500. u_data += u_linesize;
  501. v_data += v_linesize;
  502. if (a_data)
  503. a_data += a_linesize;
  504. }
  505. y_linesize <<= 1;
  506. u_linesize <<= 1;
  507. v_linesize <<= 1;
  508. a_linesize <<= 1;
  509. }
  510. y_data += (mb_y_pos << 4) * y_linesize + (mb_x_pos << 5);
  511. u_data += (mb_y_pos << 4) * u_linesize + (mb_x_pos << ctx->mb_chroma_factor);
  512. v_data += (mb_y_pos << 4) * v_linesize + (mb_x_pos << ctx->mb_chroma_factor);
  513. if (a_data)
  514. a_data += (mb_y_pos << 4) * a_linesize + (mb_x_pos << 5);
  515. if (slice_data_size < 6) {
  516. av_log(avctx, AV_LOG_ERROR, "slice data too small\n");
  517. return AVERROR_INVALIDDATA;
  518. }
  519. /* parse slice header */
  520. hdr_size = buf[0] >> 3;
  521. coff[0] = hdr_size;
  522. y_data_size = AV_RB16(buf + 2);
  523. coff[1] = coff[0] + y_data_size;
  524. u_data_size = AV_RB16(buf + 4);
  525. coff[2] = coff[1] + u_data_size;
  526. v_data_size = hdr_size > 7 ? AV_RB16(buf + 6) : slice_data_size - coff[2];
  527. coff[3] = coff[2] + v_data_size;
  528. a_data_size = slice_data_size - coff[3];
  529. /* if V or alpha component size is negative that means that previous
  530. component sizes are too large */
  531. if (v_data_size < 0 || a_data_size < 0 || hdr_size < 6) {
  532. av_log(avctx, AV_LOG_ERROR, "invalid data size\n");
  533. return AVERROR_INVALIDDATA;
  534. }
  535. sf = av_clip(buf[1], 1, 224);
  536. sf = sf > 128 ? (sf - 96) << 2 : sf;
  537. /* scale quantization matrixes according with slice's scale factor */
  538. /* TODO: this can be SIMD-optimized a lot */
  539. if (ctx->qmat_changed || sf != td->prev_slice_sf) {
  540. td->prev_slice_sf = sf;
  541. for (i = 0; i < 64; i++) {
  542. td->qmat_luma_scaled[ctx->dsp.idct_permutation[i]] = ctx->qmat_luma[i] * sf;
  543. td->qmat_chroma_scaled[ctx->dsp.idct_permutation[i]] = ctx->qmat_chroma[i] * sf;
  544. }
  545. }
  546. /* decode luma plane */
  547. ret = decode_slice_plane(ctx, td, buf + coff[0], y_data_size,
  548. (uint16_t*) y_data, y_linesize,
  549. mbs_per_slice, 4, slice_width_factor + 2,
  550. td->qmat_luma_scaled, 0);
  551. if (ret < 0)
  552. return ret;
  553. /* decode U chroma plane */
  554. ret = decode_slice_plane(ctx, td, buf + coff[1], u_data_size,
  555. (uint16_t*) u_data, u_linesize,
  556. mbs_per_slice, ctx->num_chroma_blocks,
  557. slice_width_factor + ctx->chroma_factor - 1,
  558. td->qmat_chroma_scaled, 1);
  559. if (ret < 0)
  560. return ret;
  561. /* decode V chroma plane */
  562. ret = decode_slice_plane(ctx, td, buf + coff[2], v_data_size,
  563. (uint16_t*) v_data, v_linesize,
  564. mbs_per_slice, ctx->num_chroma_blocks,
  565. slice_width_factor + ctx->chroma_factor - 1,
  566. td->qmat_chroma_scaled, 1);
  567. if (ret < 0)
  568. return ret;
  569. /* decode alpha plane if available */
  570. if (a_data && a_data_size)
  571. decode_alpha_plane(ctx, td, buf + coff[3], a_data_size,
  572. (uint16_t*) a_data, a_linesize,
  573. mbs_per_slice);
  574. return 0;
  575. }
  576. static int decode_picture(ProresContext *ctx, int pic_num,
  577. AVCodecContext *avctx)
  578. {
  579. int slice_num, slice_width, x_pos, y_pos;
  580. slice_num = 0;
  581. ctx->pic_num = pic_num;
  582. for (y_pos = 0; y_pos < ctx->num_y_mbs; y_pos++) {
  583. slice_width = 1 << ctx->slice_width_factor;
  584. for (x_pos = 0; x_pos < ctx->num_x_mbs && slice_width;
  585. x_pos += slice_width) {
  586. while (ctx->num_x_mbs - x_pos < slice_width)
  587. slice_width >>= 1;
  588. ctx->slice_data[slice_num].slice_num = slice_num;
  589. ctx->slice_data[slice_num].x_pos = x_pos;
  590. ctx->slice_data[slice_num].y_pos = y_pos;
  591. ctx->slice_data[slice_num].slice_width = slice_width;
  592. slice_num++;
  593. }
  594. }
  595. return avctx->execute(avctx, decode_slice,
  596. ctx->slice_data, NULL, slice_num,
  597. sizeof(ctx->slice_data[0]));
  598. }
  599. #define MOVE_DATA_PTR(nbytes) buf += (nbytes); buf_size -= (nbytes)
  600. static int decode_frame(AVCodecContext *avctx, void *data, int *got_frame,
  601. AVPacket *avpkt)
  602. {
  603. ProresContext *ctx = avctx->priv_data;
  604. const uint8_t *buf = avpkt->data;
  605. int buf_size = avpkt->size;
  606. int frame_hdr_size, pic_num, pic_data_size;
  607. ctx->frame = data;
  608. ctx->frame->pict_type = AV_PICTURE_TYPE_I;
  609. ctx->frame->key_frame = 1;
  610. /* check frame atom container */
  611. if (buf_size < 28 || buf_size < AV_RB32(buf) ||
  612. AV_RB32(buf + 4) != FRAME_ID) {
  613. av_log(avctx, AV_LOG_ERROR, "invalid frame\n");
  614. return AVERROR_INVALIDDATA;
  615. }
  616. MOVE_DATA_PTR(8);
  617. frame_hdr_size = decode_frame_header(ctx, buf, buf_size, avctx);
  618. if (frame_hdr_size < 0)
  619. return AVERROR_INVALIDDATA;
  620. MOVE_DATA_PTR(frame_hdr_size);
  621. if (ff_get_buffer(avctx, ctx->frame, 0) < 0)
  622. return -1;
  623. for (pic_num = 0; ctx->frame->interlaced_frame - pic_num + 1; pic_num++) {
  624. pic_data_size = decode_picture_header(ctx, buf, buf_size, avctx);
  625. if (pic_data_size < 0)
  626. return AVERROR_INVALIDDATA;
  627. if (decode_picture(ctx, pic_num, avctx))
  628. return -1;
  629. MOVE_DATA_PTR(pic_data_size);
  630. }
  631. ctx->frame = NULL;
  632. *got_frame = 1;
  633. return avpkt->size;
  634. }
  635. static av_cold int decode_close(AVCodecContext *avctx)
  636. {
  637. ProresContext *ctx = avctx->priv_data;
  638. av_freep(&ctx->slice_data);
  639. return 0;
  640. }
  641. AVCodec ff_prores_decoder = {
  642. .name = "prores",
  643. .long_name = NULL_IF_CONFIG_SMALL("Apple ProRes (iCodec Pro)"),
  644. .type = AVMEDIA_TYPE_VIDEO,
  645. .id = AV_CODEC_ID_PRORES,
  646. .priv_data_size = sizeof(ProresContext),
  647. .init = decode_init,
  648. .close = decode_close,
  649. .decode = decode_frame,
  650. .capabilities = AV_CODEC_CAP_DR1 | AV_CODEC_CAP_SLICE_THREADS,
  651. };