proresdec.c 29 KB

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