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.

595 lines
19KB

  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 "avcodec.h"
  28. #include "get_bits.h"
  29. #include "simple_idct.h"
  30. #include "proresdec.h"
  31. static void permute(uint8_t *dst, const uint8_t *src, const uint8_t permutation[64])
  32. {
  33. int i;
  34. for (i = 0; i < 64; i++)
  35. dst[i] = permutation[src[i]];
  36. }
  37. static const uint8_t progressive_scan[64] = {
  38. 0, 1, 8, 9, 2, 3, 10, 11,
  39. 16, 17, 24, 25, 18, 19, 26, 27,
  40. 4, 5, 12, 20, 13, 6, 7, 14,
  41. 21, 28, 29, 22, 15, 23, 30, 31,
  42. 32, 33, 40, 48, 41, 34, 35, 42,
  43. 49, 56, 57, 50, 43, 36, 37, 44,
  44. 51, 58, 59, 52, 45, 38, 39, 46,
  45. 53, 60, 61, 54, 47, 55, 62, 63
  46. };
  47. static const uint8_t interlaced_scan[64] = {
  48. 0, 8, 1, 9, 16, 24, 17, 25,
  49. 2, 10, 3, 11, 18, 26, 19, 27,
  50. 32, 40, 33, 34, 41, 48, 56, 49,
  51. 42, 35, 43, 50, 57, 58, 51, 59,
  52. 4, 12, 5, 6, 13, 20, 28, 21,
  53. 14, 7, 15, 22, 29, 36, 44, 37,
  54. 30, 23, 31, 38, 45, 52, 60, 53,
  55. 46, 39, 47, 54, 61, 62, 55, 63,
  56. };
  57. static av_cold int decode_init(AVCodecContext *avctx)
  58. {
  59. ProresContext *ctx = avctx->priv_data;
  60. uint8_t idct_permutation[64];
  61. avctx->bits_per_raw_sample = 10;
  62. ff_dsputil_init(&ctx->dsp, avctx);
  63. ff_proresdsp_init(&ctx->prodsp, avctx);
  64. avctx->coded_frame = &ctx->frame;
  65. ctx->frame.type = AV_PICTURE_TYPE_I;
  66. ctx->frame.key_frame = 1;
  67. ff_init_scantable_permutation(idct_permutation,
  68. ctx->prodsp.idct_permutation_type);
  69. permute(ctx->progressive_scan, progressive_scan, idct_permutation);
  70. permute(ctx->interlaced_scan, interlaced_scan, idct_permutation);
  71. return 0;
  72. }
  73. static int decode_frame_header(ProresContext *ctx, const uint8_t *buf,
  74. const int data_size, AVCodecContext *avctx)
  75. {
  76. int hdr_size, width, height, flags;
  77. int version;
  78. const uint8_t *ptr;
  79. hdr_size = AV_RB16(buf);
  80. av_dlog(avctx, "header size %d\n", hdr_size);
  81. if (hdr_size > data_size) {
  82. av_log(avctx, AV_LOG_ERROR, "error, wrong header size\n");
  83. return -1;
  84. }
  85. version = AV_RB16(buf + 2);
  86. av_dlog(avctx, "%.4s version %d\n", buf+4, version);
  87. if (version > 1) {
  88. av_log(avctx, AV_LOG_ERROR, "unsupported version: %d\n", version);
  89. return -1;
  90. }
  91. width = AV_RB16(buf + 8);
  92. height = AV_RB16(buf + 10);
  93. if (width != avctx->width || height != avctx->height) {
  94. av_log(avctx, AV_LOG_ERROR, "picture resolution change: %dx%d -> %dx%d\n",
  95. avctx->width, avctx->height, width, height);
  96. return -1;
  97. }
  98. ctx->frame_type = (buf[12] >> 2) & 3;
  99. av_dlog(avctx, "frame type %d\n", ctx->frame_type);
  100. if (ctx->frame_type == 0) {
  101. ctx->scan = ctx->progressive_scan; // permuted
  102. } else {
  103. ctx->scan = ctx->interlaced_scan; // permuted
  104. ctx->frame.interlaced_frame = 1;
  105. ctx->frame.top_field_first = ctx->frame_type == 1;
  106. }
  107. avctx->pix_fmt = (buf[12] & 0xC0) == 0xC0 ? PIX_FMT_YUV444P10 : PIX_FMT_YUV422P10;
  108. ptr = buf + 20;
  109. flags = buf[19];
  110. av_dlog(avctx, "flags %x\n", flags);
  111. if (flags & 2) {
  112. permute(ctx->qmat_luma, ctx->prodsp.idct_permutation, ptr);
  113. ptr += 64;
  114. } else {
  115. memset(ctx->qmat_luma, 4, 64);
  116. }
  117. if (flags & 1) {
  118. permute(ctx->qmat_chroma, ctx->prodsp.idct_permutation, ptr);
  119. } else {
  120. memset(ctx->qmat_chroma, 4, 64);
  121. }
  122. return hdr_size;
  123. }
  124. static int decode_picture_header(AVCodecContext *avctx, const uint8_t *buf, const int buf_size)
  125. {
  126. ProresContext *ctx = avctx->priv_data;
  127. int i, hdr_size, slice_count;
  128. unsigned pic_data_size;
  129. int log2_slice_mb_width, log2_slice_mb_height;
  130. int slice_mb_count, mb_x, mb_y;
  131. const uint8_t *data_ptr, *index_ptr;
  132. hdr_size = buf[0] >> 3;
  133. if (hdr_size < 8 || hdr_size > buf_size) {
  134. av_log(avctx, AV_LOG_ERROR, "error, wrong picture header size\n");
  135. return -1;
  136. }
  137. pic_data_size = AV_RB32(buf + 1);
  138. if (pic_data_size > buf_size) {
  139. av_log(avctx, AV_LOG_ERROR, "error, wrong picture data size\n");
  140. return -1;
  141. }
  142. log2_slice_mb_width = buf[7] >> 4;
  143. log2_slice_mb_height = buf[7] & 0xF;
  144. if (log2_slice_mb_width > 3 || log2_slice_mb_height) {
  145. av_log(avctx, AV_LOG_ERROR, "unsupported slice resolution: %dx%d\n",
  146. 1 << log2_slice_mb_width, 1 << log2_slice_mb_height);
  147. return -1;
  148. }
  149. ctx->mb_width = (avctx->width + 15) >> 4;
  150. if (ctx->frame_type)
  151. ctx->mb_height = (avctx->height + 31) >> 5;
  152. else
  153. ctx->mb_height = (avctx->height + 15) >> 4;
  154. slice_count = AV_RB16(buf + 5);
  155. if (ctx->slice_count != slice_count || !ctx->slices) {
  156. av_freep(&ctx->slices);
  157. ctx->slices = av_mallocz(slice_count * sizeof(*ctx->slices));
  158. if (!ctx->slices)
  159. return AVERROR(ENOMEM);
  160. ctx->slice_count = slice_count;
  161. }
  162. if (!slice_count)
  163. return AVERROR(EINVAL);
  164. if (hdr_size + slice_count*2 > buf_size) {
  165. av_log(avctx, AV_LOG_ERROR, "error, wrong slice count\n");
  166. return -1;
  167. }
  168. // parse slice information
  169. index_ptr = buf + hdr_size;
  170. data_ptr = index_ptr + slice_count*2;
  171. slice_mb_count = 1 << log2_slice_mb_width;
  172. mb_x = 0;
  173. mb_y = 0;
  174. for (i = 0; i < slice_count; i++) {
  175. SliceContext *slice = &ctx->slices[i];
  176. slice->data = data_ptr;
  177. data_ptr += AV_RB16(index_ptr + i*2);
  178. while (ctx->mb_width - mb_x < slice_mb_count)
  179. slice_mb_count >>= 1;
  180. slice->mb_x = mb_x;
  181. slice->mb_y = mb_y;
  182. slice->mb_count = slice_mb_count;
  183. slice->data_size = data_ptr - slice->data;
  184. if (slice->data_size < 6) {
  185. av_log(avctx, AV_LOG_ERROR, "error, wrong slice data size\n");
  186. return -1;
  187. }
  188. mb_x += slice_mb_count;
  189. if (mb_x == ctx->mb_width) {
  190. slice_mb_count = 1 << log2_slice_mb_width;
  191. mb_x = 0;
  192. mb_y++;
  193. }
  194. if (data_ptr > buf + buf_size) {
  195. av_log(avctx, AV_LOG_ERROR, "error, slice out of bounds\n");
  196. return -1;
  197. }
  198. }
  199. if (mb_x || mb_y != ctx->mb_height) {
  200. av_log(avctx, AV_LOG_ERROR, "error wrong mb count y %d h %d\n",
  201. mb_y, ctx->mb_height);
  202. return -1;
  203. }
  204. return pic_data_size;
  205. }
  206. #define DECODE_CODEWORD(val, codebook) \
  207. do { \
  208. unsigned int rice_order, exp_order, switch_bits; \
  209. unsigned int q, buf, bits; \
  210. \
  211. UPDATE_CACHE(re, gb); \
  212. buf = GET_CACHE(re, gb); \
  213. \
  214. /* number of bits to switch between rice and exp golomb */ \
  215. switch_bits = codebook & 3; \
  216. rice_order = codebook >> 5; \
  217. exp_order = (codebook >> 2) & 7; \
  218. \
  219. q = 31 - av_log2(buf); \
  220. \
  221. if (q > switch_bits) { /* exp golomb */ \
  222. bits = exp_order - switch_bits + (q<<1); \
  223. val = SHOW_UBITS(re, gb, bits) - (1 << exp_order) + \
  224. ((switch_bits + 1) << rice_order); \
  225. SKIP_BITS(re, gb, bits); \
  226. } else if (rice_order) { \
  227. SKIP_BITS(re, gb, q+1); \
  228. val = (q << rice_order) + SHOW_UBITS(re, gb, rice_order); \
  229. SKIP_BITS(re, gb, rice_order); \
  230. } else { \
  231. val = q; \
  232. SKIP_BITS(re, gb, q+1); \
  233. } \
  234. } while (0)
  235. #define TOSIGNED(x) (((x) >> 1) ^ (-((x) & 1)))
  236. #define FIRST_DC_CB 0xB8
  237. static const uint8_t dc_codebook[7] = { 0x04, 0x28, 0x28, 0x4D, 0x4D, 0x70, 0x70};
  238. static av_always_inline void decode_dc_coeffs(GetBitContext *gb, DCTELEM *out,
  239. int blocks_per_slice)
  240. {
  241. DCTELEM prev_dc;
  242. int code, i, sign;
  243. OPEN_READER(re, gb);
  244. DECODE_CODEWORD(code, FIRST_DC_CB);
  245. prev_dc = TOSIGNED(code);
  246. out[0] = prev_dc;
  247. out += 64; // dc coeff for the next block
  248. code = 5;
  249. sign = 0;
  250. for (i = 1; i < blocks_per_slice; i++, out += 64) {
  251. DECODE_CODEWORD(code, dc_codebook[FFMIN(code, 6U)]);
  252. if(code) sign ^= -(code & 1);
  253. else sign = 0;
  254. prev_dc += (((code + 1) >> 1) ^ sign) - sign;
  255. out[0] = prev_dc;
  256. }
  257. CLOSE_READER(re, gb);
  258. }
  259. // adaptive codebook switching lut according to previous run/level values
  260. static const uint8_t run_to_cb[16] = { 0x06, 0x06, 0x05, 0x05, 0x04, 0x29, 0x29, 0x29, 0x29, 0x28, 0x28, 0x28, 0x28, 0x28, 0x28, 0x4C };
  261. static const uint8_t lev_to_cb[10] = { 0x04, 0x0A, 0x05, 0x06, 0x04, 0x28, 0x28, 0x28, 0x28, 0x4C };
  262. static av_always_inline void decode_ac_coeffs(AVCodecContext *avctx, GetBitContext *gb,
  263. DCTELEM *out, int blocks_per_slice)
  264. {
  265. ProresContext *ctx = avctx->priv_data;
  266. int block_mask, sign;
  267. unsigned pos, run, level;
  268. int max_coeffs, i, bits_left;
  269. int log2_block_count = av_log2(blocks_per_slice);
  270. OPEN_READER(re, gb);
  271. UPDATE_CACHE(re, gb); \
  272. run = 4;
  273. level = 2;
  274. max_coeffs = 64 << log2_block_count;
  275. block_mask = blocks_per_slice - 1;
  276. for (pos = block_mask;;) {
  277. bits_left = gb->size_in_bits - re_index;
  278. if (!bits_left || (bits_left < 32 && !SHOW_UBITS(re, gb, bits_left)))
  279. break;
  280. DECODE_CODEWORD(run, run_to_cb[FFMIN(run, 15)]);
  281. pos += run + 1;
  282. if (pos >= max_coeffs) {
  283. av_log(avctx, AV_LOG_ERROR, "ac tex damaged %d, %d\n", pos, max_coeffs);
  284. return;
  285. }
  286. DECODE_CODEWORD(level, lev_to_cb[FFMIN(level, 9)]);
  287. level += 1;
  288. i = pos >> log2_block_count;
  289. sign = SHOW_SBITS(re, gb, 1);
  290. SKIP_BITS(re, gb, 1);
  291. out[((pos & block_mask) << 6) + ctx->scan[i]] = ((level ^ sign) - sign);
  292. }
  293. CLOSE_READER(re, gb);
  294. }
  295. static void decode_slice_luma(AVCodecContext *avctx, SliceContext *slice,
  296. uint16_t *dst, int dst_stride,
  297. const uint8_t *buf, unsigned buf_size,
  298. const int16_t *qmat)
  299. {
  300. ProresContext *ctx = avctx->priv_data;
  301. LOCAL_ALIGNED_16(DCTELEM, blocks, [8*4*64]);
  302. DCTELEM *block;
  303. GetBitContext gb;
  304. int i, blocks_per_slice = slice->mb_count<<2;
  305. for (i = 0; i < blocks_per_slice; i++)
  306. ctx->dsp.clear_block(blocks+(i<<6));
  307. init_get_bits(&gb, buf, buf_size << 3);
  308. decode_dc_coeffs(&gb, blocks, blocks_per_slice);
  309. decode_ac_coeffs(avctx, &gb, blocks, blocks_per_slice);
  310. block = blocks;
  311. for (i = 0; i < slice->mb_count; i++) {
  312. ctx->prodsp.idct_put(dst, dst_stride, block+(0<<6), qmat);
  313. ctx->prodsp.idct_put(dst +8, dst_stride, block+(1<<6), qmat);
  314. ctx->prodsp.idct_put(dst+4*dst_stride , dst_stride, block+(2<<6), qmat);
  315. ctx->prodsp.idct_put(dst+4*dst_stride+8, dst_stride, block+(3<<6), qmat);
  316. block += 4*64;
  317. dst += 16;
  318. }
  319. }
  320. static void decode_slice_chroma(AVCodecContext *avctx, SliceContext *slice,
  321. uint16_t *dst, int dst_stride,
  322. const uint8_t *buf, unsigned buf_size,
  323. const int16_t *qmat, int log2_blocks_per_mb)
  324. {
  325. ProresContext *ctx = avctx->priv_data;
  326. LOCAL_ALIGNED_16(DCTELEM, blocks, [8*4*64]);
  327. DCTELEM *block;
  328. GetBitContext gb;
  329. int i, j, blocks_per_slice = slice->mb_count << log2_blocks_per_mb;
  330. for (i = 0; i < blocks_per_slice; i++)
  331. ctx->dsp.clear_block(blocks+(i<<6));
  332. init_get_bits(&gb, buf, buf_size << 3);
  333. decode_dc_coeffs(&gb, blocks, blocks_per_slice);
  334. decode_ac_coeffs(avctx, &gb, blocks, blocks_per_slice);
  335. block = blocks;
  336. for (i = 0; i < slice->mb_count; i++) {
  337. for (j = 0; j < log2_blocks_per_mb; j++) {
  338. ctx->prodsp.idct_put(dst, dst_stride, block+(0<<6), qmat);
  339. ctx->prodsp.idct_put(dst+4*dst_stride, dst_stride, block+(1<<6), qmat);
  340. block += 2*64;
  341. dst += 8;
  342. }
  343. }
  344. }
  345. static int decode_slice_thread(AVCodecContext *avctx, void *arg, int jobnr, int threadnr)
  346. {
  347. ProresContext *ctx = avctx->priv_data;
  348. SliceContext *slice = &ctx->slices[jobnr];
  349. const uint8_t *buf = slice->data;
  350. AVFrame *pic = avctx->coded_frame;
  351. int i, hdr_size, qscale, log2_chroma_blocks_per_mb;
  352. int luma_stride, chroma_stride;
  353. int y_data_size, u_data_size, v_data_size;
  354. uint8_t *dest_y, *dest_u, *dest_v;
  355. int16_t qmat_luma_scaled[64];
  356. int16_t qmat_chroma_scaled[64];
  357. int mb_x_shift;
  358. //av_log(avctx, AV_LOG_INFO, "slice %d mb width %d mb x %d y %d\n",
  359. // jobnr, slice->mb_count, slice->mb_x, slice->mb_y);
  360. // slice header
  361. hdr_size = buf[0] >> 3;
  362. qscale = av_clip(buf[1], 1, 224);
  363. qscale = qscale > 128 ? qscale - 96 << 2: qscale;
  364. y_data_size = AV_RB16(buf + 2);
  365. u_data_size = AV_RB16(buf + 4);
  366. v_data_size = slice->data_size - y_data_size - u_data_size - hdr_size;
  367. if (hdr_size > 7) v_data_size = AV_RB16(buf + 6);
  368. if (y_data_size < 0 || u_data_size < 0 || v_data_size < 0
  369. || hdr_size+y_data_size+u_data_size+v_data_size > slice->data_size){
  370. av_log(avctx, AV_LOG_ERROR, "invalid plane data size\n");
  371. return -1;
  372. }
  373. buf += hdr_size;
  374. for (i = 0; i < 64; i++) {
  375. qmat_luma_scaled [i] = ctx->qmat_luma [i] * qscale;
  376. qmat_chroma_scaled[i] = ctx->qmat_chroma[i] * qscale;
  377. }
  378. if (ctx->frame_type == 0) {
  379. luma_stride = pic->linesize[0];
  380. chroma_stride = pic->linesize[1];
  381. } else {
  382. luma_stride = pic->linesize[0] << 1;
  383. chroma_stride = pic->linesize[1] << 1;
  384. }
  385. if (avctx->pix_fmt == PIX_FMT_YUV444P10) {
  386. mb_x_shift = 5;
  387. log2_chroma_blocks_per_mb = 2;
  388. } else {
  389. mb_x_shift = 4;
  390. log2_chroma_blocks_per_mb = 1;
  391. }
  392. dest_y = pic->data[0] + (slice->mb_y << 4) * luma_stride + (slice->mb_x << 5);
  393. dest_u = pic->data[1] + (slice->mb_y << 4) * chroma_stride + (slice->mb_x << mb_x_shift);
  394. dest_v = pic->data[2] + (slice->mb_y << 4) * chroma_stride + (slice->mb_x << mb_x_shift);
  395. if (ctx->frame_type && ctx->first_field ^ ctx->frame.top_field_first) {
  396. dest_y += pic->linesize[0];
  397. dest_u += pic->linesize[1];
  398. dest_v += pic->linesize[2];
  399. }
  400. decode_slice_luma(avctx, slice, (uint16_t*)dest_y, luma_stride,
  401. buf, y_data_size, qmat_luma_scaled);
  402. if (!(avctx->flags & CODEC_FLAG_GRAY)) {
  403. decode_slice_chroma(avctx, slice, (uint16_t*)dest_u, chroma_stride,
  404. buf + y_data_size, u_data_size,
  405. qmat_chroma_scaled, log2_chroma_blocks_per_mb);
  406. decode_slice_chroma(avctx, slice, (uint16_t*)dest_v, chroma_stride,
  407. buf + y_data_size + u_data_size, v_data_size,
  408. qmat_chroma_scaled, log2_chroma_blocks_per_mb);
  409. }
  410. return 0;
  411. }
  412. static int decode_picture(AVCodecContext *avctx)
  413. {
  414. ProresContext *ctx = avctx->priv_data;
  415. int i, threads_ret[ctx->slice_count];
  416. avctx->execute2(avctx, decode_slice_thread, NULL, threads_ret, ctx->slice_count);
  417. for (i = 0; i < ctx->slice_count; i++)
  418. if (threads_ret[i] < 0)
  419. return threads_ret[i];
  420. return 0;
  421. }
  422. static int decode_frame(AVCodecContext *avctx, void *data, int *data_size,
  423. AVPacket *avpkt)
  424. {
  425. ProresContext *ctx = avctx->priv_data;
  426. AVFrame *frame = avctx->coded_frame;
  427. const uint8_t *buf = avpkt->data;
  428. int buf_size = avpkt->size;
  429. int frame_hdr_size, pic_size;
  430. if (buf_size < 28 || AV_RL32(buf + 4) != AV_RL32("icpf")) {
  431. av_log(avctx, AV_LOG_ERROR, "invalid frame header\n");
  432. return -1;
  433. }
  434. ctx->first_field = 1;
  435. buf += 8;
  436. buf_size -= 8;
  437. frame_hdr_size = decode_frame_header(ctx, buf, buf_size, avctx);
  438. if (frame_hdr_size < 0)
  439. return -1;
  440. buf += frame_hdr_size;
  441. buf_size -= frame_hdr_size;
  442. if (frame->data[0])
  443. avctx->release_buffer(avctx, frame);
  444. if (avctx->get_buffer(avctx, frame) < 0)
  445. return -1;
  446. decode_picture:
  447. pic_size = decode_picture_header(avctx, buf, buf_size);
  448. if (pic_size < 0) {
  449. av_log(avctx, AV_LOG_ERROR, "error decoding picture header\n");
  450. return -1;
  451. }
  452. if (decode_picture(avctx)) {
  453. av_log(avctx, AV_LOG_ERROR, "error decoding picture\n");
  454. return -1;
  455. }
  456. buf += pic_size;
  457. buf_size -= pic_size;
  458. if (ctx->frame_type && buf_size > 0 && ctx->first_field) {
  459. ctx->first_field = 0;
  460. goto decode_picture;
  461. }
  462. *data_size = sizeof(AVFrame);
  463. *(AVFrame*)data = *frame;
  464. return avpkt->size;
  465. }
  466. static av_cold int decode_close(AVCodecContext *avctx)
  467. {
  468. ProresContext *ctx = avctx->priv_data;
  469. AVFrame *frame = avctx->coded_frame;
  470. if (frame->data[0])
  471. avctx->release_buffer(avctx, frame);
  472. av_freep(&ctx->slices);
  473. return 0;
  474. }
  475. AVCodec ff_prores_decoder = {
  476. .name = "prores",
  477. .type = AVMEDIA_TYPE_VIDEO,
  478. .id = AV_CODEC_ID_PRORES,
  479. .priv_data_size = sizeof(ProresContext),
  480. .init = decode_init,
  481. .close = decode_close,
  482. .decode = decode_frame,
  483. .long_name = NULL_IF_CONFIG_SMALL("ProRes"),
  484. .capabilities = CODEC_CAP_DR1 | CODEC_CAP_SLICE_THREADS,
  485. };