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.

760 lines
25KB

  1. /*
  2. * Copyright (c) 2010-2011 Maxim Poliakovski
  3. * Copyright (c) 2010-2011 Elvis Presley
  4. *
  5. * This file is part of FFmpeg.
  6. *
  7. * FFmpeg is free software; you can redistribute it and/or
  8. * modify it under the terms of the GNU Lesser General Public
  9. * License as published by the Free Software Foundation; either
  10. * version 2.1 of the License, or (at your option) any later version.
  11. *
  12. * FFmpeg is distributed in the hope that it will be useful,
  13. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  14. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  15. * Lesser General Public License for more details.
  16. *
  17. * You should have received a copy of the GNU Lesser General Public
  18. * License along with FFmpeg; if not, write to the Free Software
  19. * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
  20. */
  21. /**
  22. * @file
  23. * Known FOURCCs: 'apch' (HQ), 'apcn' (SD), 'apcs' (LT), 'acpo' (Proxy), 'ap4h' (4444)
  24. */
  25. //#define DEBUG
  26. #define LONG_BITSTREAM_READER
  27. #include "libavutil/internal.h"
  28. #include "avcodec.h"
  29. #include "get_bits.h"
  30. #include "idctdsp.h"
  31. #include "internal.h"
  32. #include "profiles.h"
  33. #include "simple_idct.h"
  34. #include "proresdec.h"
  35. #include "proresdata.h"
  36. #include "thread.h"
  37. static void permute(uint8_t *dst, const uint8_t *src, const uint8_t permutation[64])
  38. {
  39. int i;
  40. for (i = 0; i < 64; i++)
  41. dst[i] = permutation[src[i]];
  42. }
  43. static av_cold int decode_init(AVCodecContext *avctx)
  44. {
  45. ProresContext *ctx = avctx->priv_data;
  46. uint8_t idct_permutation[64];
  47. avctx->bits_per_raw_sample = 10;
  48. ff_blockdsp_init(&ctx->bdsp, avctx);
  49. ff_proresdsp_init(&ctx->prodsp, avctx);
  50. ff_init_scantable_permutation(idct_permutation,
  51. ctx->prodsp.idct_permutation_type);
  52. permute(ctx->progressive_scan, ff_prores_progressive_scan, idct_permutation);
  53. permute(ctx->interlaced_scan, ff_prores_interlaced_scan, idct_permutation);
  54. switch (avctx->codec_tag) {
  55. case MKTAG('a','p','c','o'):
  56. avctx->profile = FF_PROFILE_PRORES_PROXY;
  57. break;
  58. case MKTAG('a','p','c','s'):
  59. avctx->profile = FF_PROFILE_PRORES_LT;
  60. break;
  61. case MKTAG('a','p','c','n'):
  62. avctx->profile = FF_PROFILE_PRORES_STANDARD;
  63. break;
  64. case MKTAG('a','p','c','h'):
  65. avctx->profile = FF_PROFILE_PRORES_HQ;
  66. break;
  67. case MKTAG('a','p','4','h'):
  68. avctx->profile = FF_PROFILE_PRORES_4444;
  69. break;
  70. case MKTAG('a','p','4','x'):
  71. avctx->profile = FF_PROFILE_PRORES_XQ;
  72. break;
  73. default:
  74. avctx->profile = FF_PROFILE_UNKNOWN;
  75. av_log(avctx, AV_LOG_WARNING, "Unknown prores profile %d\n", avctx->codec_tag);
  76. }
  77. return 0;
  78. }
  79. static int decode_frame_header(ProresContext *ctx, const uint8_t *buf,
  80. const int data_size, AVCodecContext *avctx)
  81. {
  82. int hdr_size, width, height, flags;
  83. int version;
  84. const uint8_t *ptr;
  85. hdr_size = AV_RB16(buf);
  86. ff_dlog(avctx, "header size %d\n", hdr_size);
  87. if (hdr_size > data_size) {
  88. av_log(avctx, AV_LOG_ERROR, "error, wrong header size\n");
  89. return AVERROR_INVALIDDATA;
  90. }
  91. version = AV_RB16(buf + 2);
  92. ff_dlog(avctx, "%.4s version %d\n", buf+4, version);
  93. if (version > 1) {
  94. av_log(avctx, AV_LOG_ERROR, "unsupported version: %d\n", version);
  95. return AVERROR_PATCHWELCOME;
  96. }
  97. width = AV_RB16(buf + 8);
  98. height = AV_RB16(buf + 10);
  99. if (width != avctx->width || height != avctx->height) {
  100. av_log(avctx, AV_LOG_ERROR, "picture resolution change: %dx%d -> %dx%d\n",
  101. avctx->width, avctx->height, width, height);
  102. return AVERROR_PATCHWELCOME;
  103. }
  104. ctx->frame_type = (buf[12] >> 2) & 3;
  105. ctx->alpha_info = buf[17] & 0xf;
  106. if (ctx->alpha_info > 2) {
  107. av_log(avctx, AV_LOG_ERROR, "Invalid alpha mode %d\n", ctx->alpha_info);
  108. return AVERROR_INVALIDDATA;
  109. }
  110. if (avctx->skip_alpha) ctx->alpha_info = 0;
  111. ff_dlog(avctx, "frame type %d\n", ctx->frame_type);
  112. if (ctx->frame_type == 0) {
  113. ctx->scan = ctx->progressive_scan; // permuted
  114. } else {
  115. ctx->scan = ctx->interlaced_scan; // permuted
  116. ctx->frame->interlaced_frame = 1;
  117. ctx->frame->top_field_first = ctx->frame_type == 1;
  118. }
  119. if (ctx->alpha_info) {
  120. avctx->pix_fmt = (buf[12] & 0xC0) == 0xC0 ? AV_PIX_FMT_YUVA444P10 : AV_PIX_FMT_YUVA422P10;
  121. } else {
  122. avctx->pix_fmt = (buf[12] & 0xC0) == 0xC0 ? AV_PIX_FMT_YUV444P10 : AV_PIX_FMT_YUV422P10;
  123. }
  124. avctx->color_primaries = buf[14];
  125. avctx->color_trc = buf[15];
  126. avctx->colorspace = buf[16];
  127. avctx->color_range = AVCOL_RANGE_MPEG;
  128. ptr = buf + 20;
  129. flags = buf[19];
  130. ff_dlog(avctx, "flags %x\n", flags);
  131. if (flags & 2) {
  132. if(buf + data_size - ptr < 64) {
  133. av_log(avctx, AV_LOG_ERROR, "Header truncated\n");
  134. return AVERROR_INVALIDDATA;
  135. }
  136. permute(ctx->qmat_luma, ctx->prodsp.idct_permutation, ptr);
  137. ptr += 64;
  138. } else {
  139. memset(ctx->qmat_luma, 4, 64);
  140. }
  141. if (flags & 1) {
  142. if(buf + data_size - ptr < 64) {
  143. av_log(avctx, AV_LOG_ERROR, "Header truncated\n");
  144. return AVERROR_INVALIDDATA;
  145. }
  146. permute(ctx->qmat_chroma, ctx->prodsp.idct_permutation, ptr);
  147. } else {
  148. memset(ctx->qmat_chroma, 4, 64);
  149. }
  150. return hdr_size;
  151. }
  152. static int decode_picture_header(AVCodecContext *avctx, const uint8_t *buf, const int buf_size)
  153. {
  154. ProresContext *ctx = avctx->priv_data;
  155. int i, hdr_size, slice_count;
  156. unsigned pic_data_size;
  157. int log2_slice_mb_width, log2_slice_mb_height;
  158. int slice_mb_count, mb_x, mb_y;
  159. const uint8_t *data_ptr, *index_ptr;
  160. hdr_size = buf[0] >> 3;
  161. if (hdr_size < 8 || hdr_size > buf_size) {
  162. av_log(avctx, AV_LOG_ERROR, "error, wrong picture header size\n");
  163. return AVERROR_INVALIDDATA;
  164. }
  165. pic_data_size = AV_RB32(buf + 1);
  166. if (pic_data_size > buf_size) {
  167. av_log(avctx, AV_LOG_ERROR, "error, wrong picture data size\n");
  168. return AVERROR_INVALIDDATA;
  169. }
  170. log2_slice_mb_width = buf[7] >> 4;
  171. log2_slice_mb_height = buf[7] & 0xF;
  172. if (log2_slice_mb_width > 3 || log2_slice_mb_height) {
  173. av_log(avctx, AV_LOG_ERROR, "unsupported slice resolution: %dx%d\n",
  174. 1 << log2_slice_mb_width, 1 << log2_slice_mb_height);
  175. return AVERROR_INVALIDDATA;
  176. }
  177. ctx->mb_width = (avctx->width + 15) >> 4;
  178. if (ctx->frame_type)
  179. ctx->mb_height = (avctx->height + 31) >> 5;
  180. else
  181. ctx->mb_height = (avctx->height + 15) >> 4;
  182. // QT ignores the written value
  183. // slice_count = AV_RB16(buf + 5);
  184. slice_count = ctx->mb_height * ((ctx->mb_width >> log2_slice_mb_width) +
  185. av_popcount(ctx->mb_width & (1 << log2_slice_mb_width) - 1));
  186. if (ctx->slice_count != slice_count || !ctx->slices) {
  187. av_freep(&ctx->slices);
  188. ctx->slice_count = 0;
  189. ctx->slices = av_mallocz_array(slice_count, sizeof(*ctx->slices));
  190. if (!ctx->slices)
  191. return AVERROR(ENOMEM);
  192. ctx->slice_count = slice_count;
  193. }
  194. if (!slice_count)
  195. return AVERROR(EINVAL);
  196. if (hdr_size + slice_count*2 > buf_size) {
  197. av_log(avctx, AV_LOG_ERROR, "error, wrong slice count\n");
  198. return AVERROR_INVALIDDATA;
  199. }
  200. // parse slice information
  201. index_ptr = buf + hdr_size;
  202. data_ptr = index_ptr + slice_count*2;
  203. slice_mb_count = 1 << log2_slice_mb_width;
  204. mb_x = 0;
  205. mb_y = 0;
  206. for (i = 0; i < slice_count; i++) {
  207. SliceContext *slice = &ctx->slices[i];
  208. slice->data = data_ptr;
  209. data_ptr += AV_RB16(index_ptr + i*2);
  210. while (ctx->mb_width - mb_x < slice_mb_count)
  211. slice_mb_count >>= 1;
  212. slice->mb_x = mb_x;
  213. slice->mb_y = mb_y;
  214. slice->mb_count = slice_mb_count;
  215. slice->data_size = data_ptr - slice->data;
  216. if (slice->data_size < 6) {
  217. av_log(avctx, AV_LOG_ERROR, "error, wrong slice data size\n");
  218. return AVERROR_INVALIDDATA;
  219. }
  220. mb_x += slice_mb_count;
  221. if (mb_x == ctx->mb_width) {
  222. slice_mb_count = 1 << log2_slice_mb_width;
  223. mb_x = 0;
  224. mb_y++;
  225. }
  226. if (data_ptr > buf + buf_size) {
  227. av_log(avctx, AV_LOG_ERROR, "error, slice out of bounds\n");
  228. return AVERROR_INVALIDDATA;
  229. }
  230. }
  231. if (mb_x || mb_y != ctx->mb_height) {
  232. av_log(avctx, AV_LOG_ERROR, "error wrong mb count y %d h %d\n",
  233. mb_y, ctx->mb_height);
  234. return AVERROR_INVALIDDATA;
  235. }
  236. return pic_data_size;
  237. }
  238. #define DECODE_CODEWORD(val, codebook, SKIP) \
  239. do { \
  240. unsigned int rice_order, exp_order, switch_bits; \
  241. unsigned int q, buf, bits; \
  242. \
  243. UPDATE_CACHE(re, gb); \
  244. buf = GET_CACHE(re, gb); \
  245. \
  246. /* number of bits to switch between rice and exp golomb */ \
  247. switch_bits = codebook & 3; \
  248. rice_order = codebook >> 5; \
  249. exp_order = (codebook >> 2) & 7; \
  250. \
  251. q = 31 - av_log2(buf); \
  252. \
  253. if (q > switch_bits) { /* exp golomb */ \
  254. bits = exp_order - switch_bits + (q<<1); \
  255. if (bits > FFMIN(MIN_CACHE_BITS, 31)) \
  256. return AVERROR_INVALIDDATA; \
  257. val = SHOW_UBITS(re, gb, bits) - (1 << exp_order) + \
  258. ((switch_bits + 1) << rice_order); \
  259. SKIP(re, gb, bits); \
  260. } else if (rice_order) { \
  261. SKIP_BITS(re, gb, q+1); \
  262. val = (q << rice_order) + SHOW_UBITS(re, gb, rice_order); \
  263. SKIP(re, gb, rice_order); \
  264. } else { \
  265. val = q; \
  266. SKIP(re, gb, q+1); \
  267. } \
  268. } while (0)
  269. #define TOSIGNED(x) (((x) >> 1) ^ (-((x) & 1)))
  270. #define FIRST_DC_CB 0xB8
  271. static const uint8_t dc_codebook[7] = { 0x04, 0x28, 0x28, 0x4D, 0x4D, 0x70, 0x70};
  272. static av_always_inline int decode_dc_coeffs(GetBitContext *gb, int16_t *out,
  273. int blocks_per_slice)
  274. {
  275. int16_t prev_dc;
  276. int code, i, sign;
  277. OPEN_READER(re, gb);
  278. DECODE_CODEWORD(code, FIRST_DC_CB, LAST_SKIP_BITS);
  279. prev_dc = TOSIGNED(code);
  280. out[0] = prev_dc;
  281. out += 64; // dc coeff for the next block
  282. code = 5;
  283. sign = 0;
  284. for (i = 1; i < blocks_per_slice; i++, out += 64) {
  285. DECODE_CODEWORD(code, dc_codebook[FFMIN(code, 6U)], LAST_SKIP_BITS);
  286. if(code) sign ^= -(code & 1);
  287. else sign = 0;
  288. prev_dc += (((code + 1) >> 1) ^ sign) - sign;
  289. out[0] = prev_dc;
  290. }
  291. CLOSE_READER(re, gb);
  292. return 0;
  293. }
  294. // adaptive codebook switching lut according to previous run/level values
  295. static const uint8_t run_to_cb[16] = { 0x06, 0x06, 0x05, 0x05, 0x04, 0x29, 0x29, 0x29, 0x29, 0x28, 0x28, 0x28, 0x28, 0x28, 0x28, 0x4C };
  296. static const uint8_t lev_to_cb[10] = { 0x04, 0x0A, 0x05, 0x06, 0x04, 0x28, 0x28, 0x28, 0x28, 0x4C };
  297. static av_always_inline int decode_ac_coeffs(AVCodecContext *avctx, GetBitContext *gb,
  298. int16_t *out, int blocks_per_slice)
  299. {
  300. ProresContext *ctx = avctx->priv_data;
  301. int block_mask, sign;
  302. unsigned pos, run, level;
  303. int max_coeffs, i, bits_left;
  304. int log2_block_count = av_log2(blocks_per_slice);
  305. OPEN_READER(re, gb);
  306. UPDATE_CACHE(re, gb); \
  307. run = 4;
  308. level = 2;
  309. max_coeffs = 64 << log2_block_count;
  310. block_mask = blocks_per_slice - 1;
  311. for (pos = block_mask;;) {
  312. bits_left = gb->size_in_bits - re_index;
  313. if (!bits_left || (bits_left < 32 && !SHOW_UBITS(re, gb, bits_left)))
  314. break;
  315. DECODE_CODEWORD(run, run_to_cb[FFMIN(run, 15)], LAST_SKIP_BITS);
  316. pos += run + 1;
  317. if (pos >= max_coeffs) {
  318. av_log(avctx, AV_LOG_ERROR, "ac tex damaged %d, %d\n", pos, max_coeffs);
  319. return AVERROR_INVALIDDATA;
  320. }
  321. DECODE_CODEWORD(level, lev_to_cb[FFMIN(level, 9)], SKIP_BITS);
  322. level += 1;
  323. i = pos >> log2_block_count;
  324. sign = SHOW_SBITS(re, gb, 1);
  325. SKIP_BITS(re, gb, 1);
  326. out[((pos & block_mask) << 6) + ctx->scan[i]] = ((level ^ sign) - sign);
  327. }
  328. CLOSE_READER(re, gb);
  329. return 0;
  330. }
  331. static int decode_slice_luma(AVCodecContext *avctx, SliceContext *slice,
  332. uint16_t *dst, int dst_stride,
  333. const uint8_t *buf, unsigned buf_size,
  334. const int16_t *qmat)
  335. {
  336. ProresContext *ctx = avctx->priv_data;
  337. LOCAL_ALIGNED_32(int16_t, blocks, [8*4*64]);
  338. int16_t *block;
  339. GetBitContext gb;
  340. int i, blocks_per_slice = slice->mb_count<<2;
  341. int ret;
  342. for (i = 0; i < blocks_per_slice; i++)
  343. ctx->bdsp.clear_block(blocks+(i<<6));
  344. init_get_bits(&gb, buf, buf_size << 3);
  345. if ((ret = decode_dc_coeffs(&gb, blocks, blocks_per_slice)) < 0)
  346. return ret;
  347. if ((ret = decode_ac_coeffs(avctx, &gb, blocks, blocks_per_slice)) < 0)
  348. return ret;
  349. block = blocks;
  350. for (i = 0; i < slice->mb_count; i++) {
  351. ctx->prodsp.idct_put(dst, dst_stride, block+(0<<6), qmat);
  352. ctx->prodsp.idct_put(dst +8, dst_stride, block+(1<<6), qmat);
  353. ctx->prodsp.idct_put(dst+4*dst_stride , dst_stride, block+(2<<6), qmat);
  354. ctx->prodsp.idct_put(dst+4*dst_stride+8, dst_stride, block+(3<<6), qmat);
  355. block += 4*64;
  356. dst += 16;
  357. }
  358. return 0;
  359. }
  360. static int decode_slice_chroma(AVCodecContext *avctx, SliceContext *slice,
  361. uint16_t *dst, int dst_stride,
  362. const uint8_t *buf, unsigned buf_size,
  363. const int16_t *qmat, int log2_blocks_per_mb)
  364. {
  365. ProresContext *ctx = avctx->priv_data;
  366. LOCAL_ALIGNED_32(int16_t, blocks, [8*4*64]);
  367. int16_t *block;
  368. GetBitContext gb;
  369. int i, j, blocks_per_slice = slice->mb_count << log2_blocks_per_mb;
  370. int ret;
  371. for (i = 0; i < blocks_per_slice; i++)
  372. ctx->bdsp.clear_block(blocks+(i<<6));
  373. init_get_bits(&gb, buf, buf_size << 3);
  374. if ((ret = decode_dc_coeffs(&gb, blocks, blocks_per_slice)) < 0)
  375. return ret;
  376. if ((ret = decode_ac_coeffs(avctx, &gb, blocks, blocks_per_slice)) < 0)
  377. return ret;
  378. block = blocks;
  379. for (i = 0; i < slice->mb_count; i++) {
  380. for (j = 0; j < log2_blocks_per_mb; j++) {
  381. ctx->prodsp.idct_put(dst, dst_stride, block+(0<<6), qmat);
  382. ctx->prodsp.idct_put(dst+4*dst_stride, dst_stride, block+(1<<6), qmat);
  383. block += 2*64;
  384. dst += 8;
  385. }
  386. }
  387. return 0;
  388. }
  389. static void unpack_alpha(GetBitContext *gb, uint16_t *dst, int num_coeffs,
  390. const int num_bits)
  391. {
  392. const int mask = (1 << num_bits) - 1;
  393. int i, idx, val, alpha_val;
  394. idx = 0;
  395. alpha_val = mask;
  396. do {
  397. do {
  398. if (get_bits1(gb)) {
  399. val = get_bits(gb, num_bits);
  400. } else {
  401. int sign;
  402. val = get_bits(gb, num_bits == 16 ? 7 : 4);
  403. sign = val & 1;
  404. val = (val + 2) >> 1;
  405. if (sign)
  406. val = -val;
  407. }
  408. alpha_val = (alpha_val + val) & mask;
  409. if (num_bits == 16) {
  410. dst[idx++] = alpha_val >> 6;
  411. } else {
  412. dst[idx++] = (alpha_val << 2) | (alpha_val >> 6);
  413. }
  414. if (idx >= num_coeffs)
  415. break;
  416. } while (get_bits_left(gb)>0 && get_bits1(gb));
  417. val = get_bits(gb, 4);
  418. if (!val)
  419. val = get_bits(gb, 11);
  420. if (idx + val > num_coeffs)
  421. val = num_coeffs - idx;
  422. if (num_bits == 16) {
  423. for (i = 0; i < val; i++)
  424. dst[idx++] = alpha_val >> 6;
  425. } else {
  426. for (i = 0; i < val; i++)
  427. dst[idx++] = (alpha_val << 2) | (alpha_val >> 6);
  428. }
  429. } while (idx < num_coeffs);
  430. }
  431. /**
  432. * Decode alpha slice plane.
  433. */
  434. static void decode_slice_alpha(ProresContext *ctx,
  435. uint16_t *dst, int dst_stride,
  436. const uint8_t *buf, int buf_size,
  437. int blocks_per_slice)
  438. {
  439. GetBitContext gb;
  440. int i;
  441. LOCAL_ALIGNED_32(int16_t, blocks, [8*4*64]);
  442. int16_t *block;
  443. for (i = 0; i < blocks_per_slice<<2; i++)
  444. ctx->bdsp.clear_block(blocks+(i<<6));
  445. init_get_bits(&gb, buf, buf_size << 3);
  446. if (ctx->alpha_info == 2) {
  447. unpack_alpha(&gb, blocks, blocks_per_slice * 4 * 64, 16);
  448. } else {
  449. unpack_alpha(&gb, blocks, blocks_per_slice * 4 * 64, 8);
  450. }
  451. block = blocks;
  452. for (i = 0; i < 16; i++) {
  453. memcpy(dst, block, 16 * blocks_per_slice * sizeof(*dst));
  454. dst += dst_stride >> 1;
  455. block += 16 * blocks_per_slice;
  456. }
  457. }
  458. static int decode_slice_thread(AVCodecContext *avctx, void *arg, int jobnr, int threadnr)
  459. {
  460. ProresContext *ctx = avctx->priv_data;
  461. SliceContext *slice = &ctx->slices[jobnr];
  462. const uint8_t *buf = slice->data;
  463. AVFrame *pic = ctx->frame;
  464. int i, hdr_size, qscale, log2_chroma_blocks_per_mb;
  465. int luma_stride, chroma_stride;
  466. int y_data_size, u_data_size, v_data_size, a_data_size;
  467. uint8_t *dest_y, *dest_u, *dest_v, *dest_a;
  468. LOCAL_ALIGNED_16(int16_t, qmat_luma_scaled, [64]);
  469. LOCAL_ALIGNED_16(int16_t, qmat_chroma_scaled,[64]);
  470. int mb_x_shift;
  471. int ret;
  472. slice->ret = -1;
  473. //av_log(avctx, AV_LOG_INFO, "slice %d mb width %d mb x %d y %d\n",
  474. // jobnr, slice->mb_count, slice->mb_x, slice->mb_y);
  475. // slice header
  476. hdr_size = buf[0] >> 3;
  477. qscale = av_clip(buf[1], 1, 224);
  478. qscale = qscale > 128 ? qscale - 96 << 2: qscale;
  479. y_data_size = AV_RB16(buf + 2);
  480. u_data_size = AV_RB16(buf + 4);
  481. v_data_size = slice->data_size - y_data_size - u_data_size - hdr_size;
  482. if (hdr_size > 7) v_data_size = AV_RB16(buf + 6);
  483. a_data_size = slice->data_size - y_data_size - u_data_size -
  484. v_data_size - hdr_size;
  485. if (y_data_size < 0 || u_data_size < 0 || v_data_size < 0
  486. || hdr_size+y_data_size+u_data_size+v_data_size > slice->data_size){
  487. av_log(avctx, AV_LOG_ERROR, "invalid plane data size\n");
  488. return AVERROR_INVALIDDATA;
  489. }
  490. buf += hdr_size;
  491. for (i = 0; i < 64; i++) {
  492. qmat_luma_scaled [i] = ctx->qmat_luma [i] * qscale;
  493. qmat_chroma_scaled[i] = ctx->qmat_chroma[i] * qscale;
  494. }
  495. if (ctx->frame_type == 0) {
  496. luma_stride = pic->linesize[0];
  497. chroma_stride = pic->linesize[1];
  498. } else {
  499. luma_stride = pic->linesize[0] << 1;
  500. chroma_stride = pic->linesize[1] << 1;
  501. }
  502. if (avctx->pix_fmt == AV_PIX_FMT_YUV444P10 || avctx->pix_fmt == AV_PIX_FMT_YUVA444P10) {
  503. mb_x_shift = 5;
  504. log2_chroma_blocks_per_mb = 2;
  505. } else {
  506. mb_x_shift = 4;
  507. log2_chroma_blocks_per_mb = 1;
  508. }
  509. dest_y = pic->data[0] + (slice->mb_y << 4) * luma_stride + (slice->mb_x << 5);
  510. dest_u = pic->data[1] + (slice->mb_y << 4) * chroma_stride + (slice->mb_x << mb_x_shift);
  511. dest_v = pic->data[2] + (slice->mb_y << 4) * chroma_stride + (slice->mb_x << mb_x_shift);
  512. dest_a = pic->data[3] + (slice->mb_y << 4) * luma_stride + (slice->mb_x << 5);
  513. if (ctx->frame_type && ctx->first_field ^ ctx->frame->top_field_first) {
  514. dest_y += pic->linesize[0];
  515. dest_u += pic->linesize[1];
  516. dest_v += pic->linesize[2];
  517. dest_a += pic->linesize[3];
  518. }
  519. ret = decode_slice_luma(avctx, slice, (uint16_t*)dest_y, luma_stride,
  520. buf, y_data_size, qmat_luma_scaled);
  521. if (ret < 0)
  522. return ret;
  523. if (!(avctx->flags & AV_CODEC_FLAG_GRAY) && (u_data_size + v_data_size) > 0) {
  524. ret = decode_slice_chroma(avctx, slice, (uint16_t*)dest_u, chroma_stride,
  525. buf + y_data_size, u_data_size,
  526. qmat_chroma_scaled, log2_chroma_blocks_per_mb);
  527. if (ret < 0)
  528. return ret;
  529. ret = decode_slice_chroma(avctx, slice, (uint16_t*)dest_v, chroma_stride,
  530. buf + y_data_size + u_data_size, v_data_size,
  531. qmat_chroma_scaled, log2_chroma_blocks_per_mb);
  532. if (ret < 0)
  533. return ret;
  534. }
  535. else {
  536. size_t mb_max_x = slice->mb_count << (mb_x_shift - 1);
  537. size_t i, j;
  538. for (i = 0; i < 16; ++i)
  539. for (j = 0; j < mb_max_x; ++j) {
  540. *(uint16_t*)(dest_u + (i * chroma_stride) + (j << 1)) = 511;
  541. *(uint16_t*)(dest_v + (i * chroma_stride) + (j << 1)) = 511;
  542. }
  543. }
  544. /* decode alpha plane if available */
  545. if (ctx->alpha_info && pic->data[3] && a_data_size)
  546. decode_slice_alpha(ctx, (uint16_t*)dest_a, luma_stride,
  547. buf + y_data_size + u_data_size + v_data_size,
  548. a_data_size, slice->mb_count);
  549. slice->ret = 0;
  550. return 0;
  551. }
  552. static int decode_picture(AVCodecContext *avctx)
  553. {
  554. ProresContext *ctx = avctx->priv_data;
  555. int i;
  556. int error = 0;
  557. avctx->execute2(avctx, decode_slice_thread, NULL, NULL, ctx->slice_count);
  558. for (i = 0; i < ctx->slice_count; i++)
  559. error += ctx->slices[i].ret < 0;
  560. if (error)
  561. ctx->frame->decode_error_flags = FF_DECODE_ERROR_INVALID_BITSTREAM;
  562. if (error < ctx->slice_count)
  563. return 0;
  564. return ctx->slices[0].ret;
  565. }
  566. static int decode_frame(AVCodecContext *avctx, void *data, int *got_frame,
  567. AVPacket *avpkt)
  568. {
  569. ProresContext *ctx = avctx->priv_data;
  570. ThreadFrame tframe = { .f = data };
  571. AVFrame *frame = data;
  572. const uint8_t *buf = avpkt->data;
  573. int buf_size = avpkt->size;
  574. int frame_hdr_size, pic_size, ret;
  575. if (buf_size < 28 || AV_RL32(buf + 4) != AV_RL32("icpf")) {
  576. av_log(avctx, AV_LOG_ERROR, "invalid frame header\n");
  577. return AVERROR_INVALIDDATA;
  578. }
  579. ctx->frame = frame;
  580. ctx->frame->pict_type = AV_PICTURE_TYPE_I;
  581. ctx->frame->key_frame = 1;
  582. ctx->first_field = 1;
  583. buf += 8;
  584. buf_size -= 8;
  585. frame_hdr_size = decode_frame_header(ctx, buf, buf_size, avctx);
  586. if (frame_hdr_size < 0)
  587. return frame_hdr_size;
  588. buf += frame_hdr_size;
  589. buf_size -= frame_hdr_size;
  590. if ((ret = ff_thread_get_buffer(avctx, &tframe, 0)) < 0)
  591. return ret;
  592. decode_picture:
  593. pic_size = decode_picture_header(avctx, buf, buf_size);
  594. if (pic_size < 0) {
  595. av_log(avctx, AV_LOG_ERROR, "error decoding picture header\n");
  596. return pic_size;
  597. }
  598. if ((ret = decode_picture(avctx)) < 0) {
  599. av_log(avctx, AV_LOG_ERROR, "error decoding picture\n");
  600. return ret;
  601. }
  602. buf += pic_size;
  603. buf_size -= pic_size;
  604. if (ctx->frame_type && buf_size > 0 && ctx->first_field) {
  605. ctx->first_field = 0;
  606. goto decode_picture;
  607. }
  608. *got_frame = 1;
  609. return avpkt->size;
  610. }
  611. #if HAVE_THREADS
  612. static int decode_init_thread_copy(AVCodecContext *avctx)
  613. {
  614. ProresContext *ctx = avctx->priv_data;
  615. ctx->slices = NULL;
  616. return 0;
  617. }
  618. #endif
  619. static av_cold int decode_close(AVCodecContext *avctx)
  620. {
  621. ProresContext *ctx = avctx->priv_data;
  622. av_freep(&ctx->slices);
  623. return 0;
  624. }
  625. AVCodec ff_prores_decoder = {
  626. .name = "prores",
  627. .long_name = NULL_IF_CONFIG_SMALL("ProRes (iCodec Pro)"),
  628. .type = AVMEDIA_TYPE_VIDEO,
  629. .id = AV_CODEC_ID_PRORES,
  630. .priv_data_size = sizeof(ProresContext),
  631. .init = decode_init,
  632. .init_thread_copy = ONLY_IF_THREADS_ENABLED(decode_init_thread_copy),
  633. .close = decode_close,
  634. .decode = decode_frame,
  635. .capabilities = AV_CODEC_CAP_DR1 | AV_CODEC_CAP_SLICE_THREADS | AV_CODEC_CAP_FRAME_THREADS,
  636. .profiles = NULL_IF_CONFIG_SMALL(ff_prores_profiles),
  637. };