proresdec_lgpl.c 23 KB

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