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.

497 lines
15KB

  1. /*
  2. * Mirillis FIC decoder
  3. *
  4. * Copyright (c) 2014 Konstantin Shishkov
  5. * Copyright (c) 2014 Derek Buitenhuis
  6. *
  7. * This file is part of FFmpeg.
  8. *
  9. * FFmpeg is free software; you can redistribute it and/or
  10. * modify it under the terms of the GNU Lesser General Public
  11. * License as published by the Free Software Foundation; either
  12. * version 2.1 of the License, or (at your option) any later version.
  13. *
  14. * FFmpeg is distributed in the hope that it will be useful,
  15. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  16. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  17. * Lesser General Public License for more details.
  18. *
  19. * You should have received a copy of the GNU Lesser General Public
  20. * License along with FFmpeg; if not, write to the Free Software
  21. * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
  22. */
  23. #include "libavutil/common.h"
  24. #include "libavutil/opt.h"
  25. #include "avcodec.h"
  26. #include "internal.h"
  27. #include "get_bits.h"
  28. #include "golomb.h"
  29. typedef struct FICThreadContext {
  30. DECLARE_ALIGNED(16, int16_t, block)[64];
  31. uint8_t *src;
  32. int slice_h;
  33. int src_size;
  34. int y_off;
  35. int p_frame;
  36. } FICThreadContext;
  37. typedef struct FICContext {
  38. AVClass *class;
  39. AVCodecContext *avctx;
  40. AVFrame *frame;
  41. AVFrame *final_frame;
  42. FICThreadContext *slice_data;
  43. int slice_data_size;
  44. const uint8_t *qmat;
  45. enum AVPictureType cur_frame_type;
  46. int aligned_width, aligned_height;
  47. int num_slices, slice_h;
  48. uint8_t cursor_buf[4096];
  49. int skip_cursor;
  50. } FICContext;
  51. static const uint8_t fic_qmat_hq[64] = {
  52. 1, 2, 2, 2, 3, 3, 3, 4,
  53. 2, 2, 2, 3, 3, 3, 4, 4,
  54. 2, 2, 3, 3, 3, 4, 4, 4,
  55. 2, 2, 3, 3, 3, 4, 4, 5,
  56. 2, 3, 3, 3, 4, 4, 5, 6,
  57. 3, 3, 3, 4, 4, 5, 6, 7,
  58. 3, 3, 3, 4, 4, 5, 7, 7,
  59. 3, 3, 4, 4, 5, 7, 7, 7,
  60. };
  61. static const uint8_t fic_qmat_lq[64] = {
  62. 1, 5, 6, 7, 8, 9, 9, 11,
  63. 5, 5, 7, 8, 9, 9, 11, 12,
  64. 6, 7, 8, 9, 9, 11, 11, 12,
  65. 7, 7, 8, 9, 9, 11, 12, 13,
  66. 7, 8, 9, 9, 10, 11, 13, 16,
  67. 8, 9, 9, 10, 11, 13, 16, 19,
  68. 8, 9, 9, 11, 12, 15, 18, 23,
  69. 9, 9, 11, 12, 15, 18, 23, 27
  70. };
  71. static const uint8_t fic_header[7] = { 0, 0, 1, 'F', 'I', 'C', 'V' };
  72. #define FIC_HEADER_SIZE 27
  73. #define CURSOR_OFFSET 59
  74. static av_always_inline void fic_idct(int16_t *blk, int step, int shift, int rnd)
  75. {
  76. const unsigned t0 = 27246 * blk[3 * step] + 18405 * blk[5 * step];
  77. const unsigned t1 = 27246 * blk[5 * step] - 18405 * blk[3 * step];
  78. const unsigned t2 = 6393 * blk[7 * step] + 32139 * blk[1 * step];
  79. const unsigned t3 = 6393 * blk[1 * step] - 32139 * blk[7 * step];
  80. const unsigned t4 = 5793U * ((int)(t2 + t0 + 0x800) >> 12);
  81. const unsigned t5 = 5793U * ((int)(t3 + t1 + 0x800) >> 12);
  82. const unsigned t6 = t2 - t0;
  83. const unsigned t7 = t3 - t1;
  84. const unsigned t8 = 17734 * blk[2 * step] - 42813 * blk[6 * step];
  85. const unsigned t9 = 17734 * blk[6 * step] + 42814 * blk[2 * step];
  86. const unsigned tA = (blk[0 * step] - blk[4 * step]) * 32768 + rnd;
  87. const unsigned tB = (blk[0 * step] + blk[4 * step]) * 32768 + rnd;
  88. blk[0 * step] = (int)( t4 + t9 + tB) >> shift;
  89. blk[1 * step] = (int)( t6 + t7 + t8 + tA) >> shift;
  90. blk[2 * step] = (int)( t6 - t7 - t8 + tA) >> shift;
  91. blk[3 * step] = (int)( t5 - t9 + tB) >> shift;
  92. blk[4 * step] = (int)( -t5 - t9 + tB) >> shift;
  93. blk[5 * step] = (int)(-(t6 - t7) - t8 + tA) >> shift;
  94. blk[6 * step] = (int)(-(t6 + t7) + t8 + tA) >> shift;
  95. blk[7 * step] = (int)( -t4 + t9 + tB) >> shift;
  96. }
  97. static void fic_idct_put(uint8_t *dst, int stride, int16_t *block)
  98. {
  99. int i, j;
  100. int16_t *ptr;
  101. ptr = block;
  102. fic_idct(ptr++, 8, 13, (1 << 12) + (1 << 17));
  103. for (i = 1; i < 8; i++) {
  104. fic_idct(ptr, 8, 13, 1 << 12);
  105. ptr++;
  106. }
  107. ptr = block;
  108. for (i = 0; i < 8; i++) {
  109. fic_idct(ptr, 1, 20, 0);
  110. ptr += 8;
  111. }
  112. ptr = block;
  113. for (j = 0; j < 8; j++) {
  114. for (i = 0; i < 8; i++)
  115. dst[i] = av_clip_uint8(ptr[i]);
  116. dst += stride;
  117. ptr += 8;
  118. }
  119. }
  120. static int fic_decode_block(FICContext *ctx, GetBitContext *gb,
  121. uint8_t *dst, int stride, int16_t *block, int *is_p)
  122. {
  123. int i, num_coeff;
  124. if (get_bits_left(gb) < 8)
  125. return AVERROR_INVALIDDATA;
  126. /* Is it a skip block? */
  127. if (get_bits1(gb)) {
  128. *is_p = 1;
  129. return 0;
  130. }
  131. memset(block, 0, sizeof(*block) * 64);
  132. num_coeff = get_bits(gb, 7);
  133. if (num_coeff > 64)
  134. return AVERROR_INVALIDDATA;
  135. for (i = 0; i < num_coeff; i++) {
  136. int v = get_se_golomb(gb);
  137. if (v < -2048 || v > 2048)
  138. return AVERROR_INVALIDDATA;
  139. block[ff_zigzag_direct[i]] = v *
  140. ctx->qmat[ff_zigzag_direct[i]];
  141. }
  142. fic_idct_put(dst, stride, block);
  143. return 0;
  144. }
  145. static int fic_decode_slice(AVCodecContext *avctx, void *tdata)
  146. {
  147. FICContext *ctx = avctx->priv_data;
  148. FICThreadContext *tctx = tdata;
  149. GetBitContext gb;
  150. uint8_t *src = tctx->src;
  151. int slice_h = tctx->slice_h;
  152. int src_size = tctx->src_size;
  153. int y_off = tctx->y_off;
  154. int x, y, p, ret;
  155. ret = init_get_bits8(&gb, src, src_size);
  156. if (ret < 0)
  157. return ret;
  158. for (p = 0; p < 3; p++) {
  159. int stride = ctx->frame->linesize[p];
  160. uint8_t* dst = ctx->frame->data[p] + (y_off >> !!p) * stride;
  161. for (y = 0; y < (slice_h >> !!p); y += 8) {
  162. for (x = 0; x < (ctx->aligned_width >> !!p); x += 8) {
  163. int ret;
  164. if ((ret = fic_decode_block(ctx, &gb, dst + x, stride,
  165. tctx->block, &tctx->p_frame)) != 0)
  166. return ret;
  167. }
  168. dst += 8 * stride;
  169. }
  170. }
  171. return 0;
  172. }
  173. static av_always_inline void fic_alpha_blend(uint8_t *dst, uint8_t *src,
  174. int size, uint8_t *alpha)
  175. {
  176. int i;
  177. for (i = 0; i < size; i++)
  178. dst[i] += ((src[i] - dst[i]) * alpha[i]) >> 8;
  179. }
  180. static void fic_draw_cursor(AVCodecContext *avctx, int cur_x, int cur_y)
  181. {
  182. FICContext *ctx = avctx->priv_data;
  183. uint8_t *ptr = ctx->cursor_buf;
  184. uint8_t *dstptr[3];
  185. uint8_t planes[4][1024];
  186. uint8_t chroma[3][256];
  187. int i, j, p;
  188. /* Convert to YUVA444. */
  189. for (i = 0; i < 1024; i++) {
  190. planes[0][i] = (( 25 * ptr[0] + 129 * ptr[1] + 66 * ptr[2]) / 255) + 16;
  191. planes[1][i] = ((-38 * ptr[0] + 112 * ptr[1] + -74 * ptr[2]) / 255) + 128;
  192. planes[2][i] = ((-18 * ptr[0] + 112 * ptr[1] + -94 * ptr[2]) / 255) + 128;
  193. planes[3][i] = ptr[3];
  194. ptr += 4;
  195. }
  196. /* Subsample chroma. */
  197. for (i = 0; i < 32; i += 2)
  198. for (j = 0; j < 32; j += 2)
  199. for (p = 0; p < 3; p++)
  200. chroma[p][16 * (i / 2) + j / 2] = (planes[p + 1][32 * i + j ] +
  201. planes[p + 1][32 * i + j + 1] +
  202. planes[p + 1][32 * (i + 1) + j ] +
  203. planes[p + 1][32 * (i + 1) + j + 1]) / 4;
  204. /* Seek to x/y pos of cursor. */
  205. for (i = 0; i < 3; i++)
  206. dstptr[i] = ctx->final_frame->data[i] +
  207. (ctx->final_frame->linesize[i] * (cur_y >> !!i)) +
  208. (cur_x >> !!i) + !!i;
  209. /* Copy. */
  210. for (i = 0; i < FFMIN(32, avctx->height - cur_y) - 1; i += 2) {
  211. int lsize = FFMIN(32, avctx->width - cur_x);
  212. int csize = lsize / 2;
  213. fic_alpha_blend(dstptr[0],
  214. planes[0] + i * 32, lsize, planes[3] + i * 32);
  215. fic_alpha_blend(dstptr[0] + ctx->final_frame->linesize[0],
  216. planes[0] + (i + 1) * 32, lsize, planes[3] + (i + 1) * 32);
  217. fic_alpha_blend(dstptr[1],
  218. chroma[0] + (i / 2) * 16, csize, chroma[2] + (i / 2) * 16);
  219. fic_alpha_blend(dstptr[2],
  220. chroma[1] + (i / 2) * 16, csize, chroma[2] + (i / 2) * 16);
  221. dstptr[0] += ctx->final_frame->linesize[0] * 2;
  222. dstptr[1] += ctx->final_frame->linesize[1];
  223. dstptr[2] += ctx->final_frame->linesize[2];
  224. }
  225. }
  226. static int fic_decode_frame(AVCodecContext *avctx, void *data,
  227. int *got_frame, AVPacket *avpkt)
  228. {
  229. FICContext *ctx = avctx->priv_data;
  230. uint8_t *src = avpkt->data;
  231. int ret;
  232. int slice, nslices;
  233. int msize;
  234. int tsize;
  235. int cur_x, cur_y;
  236. int skip_cursor = ctx->skip_cursor;
  237. uint8_t *sdata;
  238. if ((ret = ff_reget_buffer(avctx, ctx->frame)) < 0)
  239. return ret;
  240. /* Header + at least one slice (4) */
  241. if (avpkt->size < FIC_HEADER_SIZE + 4) {
  242. av_log(avctx, AV_LOG_ERROR, "Frame data is too small.\n");
  243. return AVERROR_INVALIDDATA;
  244. }
  245. /* Check for header. */
  246. if (memcmp(src, fic_header, 7))
  247. av_log(avctx, AV_LOG_WARNING, "Invalid FIC Header.\n");
  248. /* Is it a skip frame? */
  249. if (src[17]) {
  250. if (!ctx->final_frame) {
  251. av_log(avctx, AV_LOG_WARNING, "Initial frame is skipped\n");
  252. return AVERROR_INVALIDDATA;
  253. }
  254. goto skip;
  255. }
  256. nslices = src[13];
  257. if (!nslices) {
  258. av_log(avctx, AV_LOG_ERROR, "Zero slices found.\n");
  259. return AVERROR_INVALIDDATA;
  260. }
  261. /* High or Low Quality Matrix? */
  262. ctx->qmat = src[23] ? fic_qmat_hq : fic_qmat_lq;
  263. /* Skip cursor data. */
  264. tsize = AV_RB24(src + 24);
  265. if (tsize > avpkt->size - FIC_HEADER_SIZE) {
  266. av_log(avctx, AV_LOG_ERROR,
  267. "Packet is too small to contain cursor (%d vs %d bytes).\n",
  268. tsize, avpkt->size - FIC_HEADER_SIZE);
  269. return AVERROR_INVALIDDATA;
  270. }
  271. if (!tsize || !AV_RL16(src + 37) || !AV_RL16(src + 39))
  272. skip_cursor = 1;
  273. if (!skip_cursor && tsize < 32) {
  274. av_log(avctx, AV_LOG_WARNING,
  275. "Cursor data too small. Skipping cursor.\n");
  276. skip_cursor = 1;
  277. }
  278. /* Cursor position. */
  279. cur_x = AV_RL16(src + 33);
  280. cur_y = AV_RL16(src + 35);
  281. if (!skip_cursor && (cur_x > avctx->width || cur_y > avctx->height)) {
  282. av_log(avctx, AV_LOG_DEBUG,
  283. "Invalid cursor position: (%d,%d). Skipping cursor.\n",
  284. cur_x, cur_y);
  285. skip_cursor = 1;
  286. }
  287. if (!skip_cursor && (AV_RL16(src + 37) != 32 || AV_RL16(src + 39) != 32)) {
  288. av_log(avctx, AV_LOG_WARNING,
  289. "Invalid cursor size. Skipping cursor.\n");
  290. skip_cursor = 1;
  291. }
  292. if (!skip_cursor && avpkt->size < CURSOR_OFFSET + sizeof(ctx->cursor_buf)) {
  293. skip_cursor = 1;
  294. }
  295. /* Slice height for all but the last slice. */
  296. ctx->slice_h = 16 * (ctx->aligned_height >> 4) / nslices;
  297. if (ctx->slice_h % 16)
  298. ctx->slice_h = FFALIGN(ctx->slice_h - 16, 16);
  299. /* First slice offset and remaining data. */
  300. sdata = src + tsize + FIC_HEADER_SIZE + 4 * nslices;
  301. msize = avpkt->size - nslices * 4 - tsize - FIC_HEADER_SIZE;
  302. if (msize <= ctx->aligned_width/8 * (ctx->aligned_height/8) / 8) {
  303. av_log(avctx, AV_LOG_ERROR, "Not enough frame data to decode.\n");
  304. return AVERROR_INVALIDDATA;
  305. }
  306. /* Allocate slice data. */
  307. av_fast_malloc(&ctx->slice_data, &ctx->slice_data_size,
  308. nslices * sizeof(ctx->slice_data[0]));
  309. if (!ctx->slice_data_size) {
  310. av_log(avctx, AV_LOG_ERROR, "Could not allocate slice data.\n");
  311. return AVERROR(ENOMEM);
  312. }
  313. memset(ctx->slice_data, 0, nslices * sizeof(ctx->slice_data[0]));
  314. for (slice = 0; slice < nslices; slice++) {
  315. unsigned slice_off = AV_RB32(src + tsize + FIC_HEADER_SIZE + slice * 4);
  316. unsigned slice_size;
  317. int y_off = ctx->slice_h * slice;
  318. int slice_h = ctx->slice_h;
  319. /*
  320. * Either read the slice size, or consume all data left.
  321. * Also, special case the last slight height.
  322. */
  323. if (slice == nslices - 1) {
  324. slice_size = msize;
  325. slice_h = FFALIGN(avctx->height - ctx->slice_h * (nslices - 1), 16);
  326. } else {
  327. slice_size = AV_RB32(src + tsize + FIC_HEADER_SIZE + slice * 4 + 4);
  328. if (slice_size < slice_off)
  329. return AVERROR_INVALIDDATA;
  330. }
  331. if (slice_size < slice_off || slice_size > msize)
  332. continue;
  333. slice_size -= slice_off;
  334. ctx->slice_data[slice].src = sdata + slice_off;
  335. ctx->slice_data[slice].src_size = slice_size;
  336. ctx->slice_data[slice].slice_h = slice_h;
  337. ctx->slice_data[slice].y_off = y_off;
  338. }
  339. if ((ret = avctx->execute(avctx, fic_decode_slice, ctx->slice_data,
  340. NULL, nslices, sizeof(ctx->slice_data[0]))) < 0)
  341. return ret;
  342. ctx->frame->key_frame = 1;
  343. ctx->frame->pict_type = AV_PICTURE_TYPE_I;
  344. for (slice = 0; slice < nslices; slice++) {
  345. if (ctx->slice_data[slice].p_frame) {
  346. ctx->frame->key_frame = 0;
  347. ctx->frame->pict_type = AV_PICTURE_TYPE_P;
  348. break;
  349. }
  350. }
  351. av_frame_free(&ctx->final_frame);
  352. ctx->final_frame = av_frame_clone(ctx->frame);
  353. if (!ctx->final_frame) {
  354. av_log(avctx, AV_LOG_ERROR, "Could not clone frame buffer.\n");
  355. return AVERROR(ENOMEM);
  356. }
  357. /* Make sure we use a user-supplied buffer. */
  358. if ((ret = ff_reget_buffer(avctx, ctx->final_frame)) < 0) {
  359. av_log(avctx, AV_LOG_ERROR, "Could not make frame writable.\n");
  360. return ret;
  361. }
  362. /* Draw cursor. */
  363. if (!skip_cursor) {
  364. memcpy(ctx->cursor_buf, src + CURSOR_OFFSET, sizeof(ctx->cursor_buf));
  365. fic_draw_cursor(avctx, cur_x, cur_y);
  366. }
  367. skip:
  368. *got_frame = 1;
  369. if ((ret = av_frame_ref(data, ctx->final_frame)) < 0)
  370. return ret;
  371. return avpkt->size;
  372. }
  373. static av_cold int fic_decode_close(AVCodecContext *avctx)
  374. {
  375. FICContext *ctx = avctx->priv_data;
  376. av_freep(&ctx->slice_data);
  377. av_frame_free(&ctx->final_frame);
  378. av_frame_free(&ctx->frame);
  379. return 0;
  380. }
  381. static av_cold int fic_decode_init(AVCodecContext *avctx)
  382. {
  383. FICContext *ctx = avctx->priv_data;
  384. /* Initialize various context values */
  385. ctx->avctx = avctx;
  386. ctx->aligned_width = FFALIGN(avctx->width, 16);
  387. ctx->aligned_height = FFALIGN(avctx->height, 16);
  388. avctx->pix_fmt = AV_PIX_FMT_YUV420P;
  389. avctx->bits_per_raw_sample = 8;
  390. ctx->frame = av_frame_alloc();
  391. if (!ctx->frame)
  392. return AVERROR(ENOMEM);
  393. return 0;
  394. }
  395. static const AVOption options[] = {
  396. { "skip_cursor", "skip the cursor", offsetof(FICContext, skip_cursor), AV_OPT_TYPE_BOOL, {.i64 = 0 }, 0, 1, AV_OPT_FLAG_DECODING_PARAM | AV_OPT_FLAG_VIDEO_PARAM },
  397. { NULL },
  398. };
  399. static const AVClass fic_decoder_class = {
  400. .class_name = "FIC decoder",
  401. .item_name = av_default_item_name,
  402. .option = options,
  403. .version = LIBAVUTIL_VERSION_INT,
  404. };
  405. AVCodec ff_fic_decoder = {
  406. .name = "fic",
  407. .long_name = NULL_IF_CONFIG_SMALL("Mirillis FIC"),
  408. .type = AVMEDIA_TYPE_VIDEO,
  409. .id = AV_CODEC_ID_FIC,
  410. .priv_data_size = sizeof(FICContext),
  411. .init = fic_decode_init,
  412. .decode = fic_decode_frame,
  413. .close = fic_decode_close,
  414. .capabilities = AV_CODEC_CAP_DR1 | AV_CODEC_CAP_SLICE_THREADS,
  415. .priv_class = &fic_decoder_class,
  416. };