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.

839 lines
28KB

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