proresdec2.c 29 KB

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