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.

489 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. static av_always_inline void fic_idct(int16_t *blk, int step, int shift, int rnd)
  74. {
  75. const unsigned t0 = 27246 * blk[3 * step] + 18405 * blk[5 * step];
  76. const unsigned t1 = 27246 * blk[5 * step] - 18405 * blk[3 * step];
  77. const unsigned t2 = 6393 * blk[7 * step] + 32139 * blk[1 * step];
  78. const unsigned t3 = 6393 * blk[1 * step] - 32139 * blk[7 * step];
  79. const unsigned t4 = 5793U * ((int)(t2 + t0 + 0x800) >> 12);
  80. const unsigned t5 = 5793U * ((int)(t3 + t1 + 0x800) >> 12);
  81. const unsigned t6 = t2 - t0;
  82. const unsigned t7 = t3 - t1;
  83. const unsigned t8 = 17734 * blk[2 * step] - 42813 * blk[6 * step];
  84. const unsigned t9 = 17734 * blk[6 * step] + 42814 * blk[2 * step];
  85. const unsigned tA = (blk[0 * step] - blk[4 * step]) * 32768 + rnd;
  86. const unsigned tB = (blk[0 * step] + blk[4 * step]) * 32768 + rnd;
  87. blk[0 * step] = (int)( t4 + t9 + tB) >> shift;
  88. blk[1 * step] = (int)( t6 + t7 + t8 + tA) >> shift;
  89. blk[2 * step] = (int)( t6 - t7 - t8 + tA) >> shift;
  90. blk[3 * step] = (int)( t5 - t9 + tB) >> shift;
  91. blk[4 * step] = (int)( -t5 - t9 + tB) >> shift;
  92. blk[5 * step] = (int)(-(t6 - t7) - t8 + tA) >> shift;
  93. blk[6 * step] = (int)(-(t6 + t7) + t8 + tA) >> shift;
  94. blk[7 * step] = (int)( -t4 + t9 + tB) >> shift;
  95. }
  96. static void fic_idct_put(uint8_t *dst, int stride, int16_t *block)
  97. {
  98. int i, j;
  99. int16_t *ptr;
  100. ptr = block;
  101. fic_idct(ptr++, 8, 13, (1 << 12) + (1 << 17));
  102. for (i = 1; i < 8; i++) {
  103. fic_idct(ptr, 8, 13, 1 << 12);
  104. ptr++;
  105. }
  106. ptr = block;
  107. for (i = 0; i < 8; i++) {
  108. fic_idct(ptr, 1, 20, 0);
  109. ptr += 8;
  110. }
  111. ptr = block;
  112. for (j = 0; j < 8; j++) {
  113. for (i = 0; i < 8; i++)
  114. dst[i] = av_clip_uint8(ptr[i]);
  115. dst += stride;
  116. ptr += 8;
  117. }
  118. }
  119. static int fic_decode_block(FICContext *ctx, GetBitContext *gb,
  120. uint8_t *dst, int stride, int16_t *block, int *is_p)
  121. {
  122. int i, num_coeff;
  123. /* Is it a skip block? */
  124. if (get_bits1(gb)) {
  125. *is_p = 1;
  126. return 0;
  127. }
  128. memset(block, 0, sizeof(*block) * 64);
  129. num_coeff = get_bits(gb, 7);
  130. if (num_coeff > 64)
  131. return AVERROR_INVALIDDATA;
  132. for (i = 0; i < num_coeff; i++) {
  133. int v = get_se_golomb(gb);
  134. if (v < -2048 || v > 2048)
  135. return AVERROR_INVALIDDATA;
  136. block[ff_zigzag_direct[i]] = v *
  137. ctx->qmat[ff_zigzag_direct[i]];
  138. }
  139. fic_idct_put(dst, stride, block);
  140. return 0;
  141. }
  142. static int fic_decode_slice(AVCodecContext *avctx, void *tdata)
  143. {
  144. FICContext *ctx = avctx->priv_data;
  145. FICThreadContext *tctx = tdata;
  146. GetBitContext gb;
  147. uint8_t *src = tctx->src;
  148. int slice_h = tctx->slice_h;
  149. int src_size = tctx->src_size;
  150. int y_off = tctx->y_off;
  151. int x, y, p;
  152. init_get_bits(&gb, src, src_size * 8);
  153. for (p = 0; p < 3; p++) {
  154. int stride = ctx->frame->linesize[p];
  155. uint8_t* dst = ctx->frame->data[p] + (y_off >> !!p) * stride;
  156. for (y = 0; y < (slice_h >> !!p); y += 8) {
  157. for (x = 0; x < (ctx->aligned_width >> !!p); x += 8) {
  158. int ret;
  159. if ((ret = fic_decode_block(ctx, &gb, dst + x, stride,
  160. tctx->block, &tctx->p_frame)) != 0)
  161. return ret;
  162. }
  163. dst += 8 * stride;
  164. }
  165. }
  166. return 0;
  167. }
  168. static av_always_inline void fic_alpha_blend(uint8_t *dst, uint8_t *src,
  169. int size, uint8_t *alpha)
  170. {
  171. int i;
  172. for (i = 0; i < size; i++)
  173. dst[i] += ((src[i] - dst[i]) * alpha[i]) >> 8;
  174. }
  175. static void fic_draw_cursor(AVCodecContext *avctx, int cur_x, int cur_y)
  176. {
  177. FICContext *ctx = avctx->priv_data;
  178. uint8_t *ptr = ctx->cursor_buf;
  179. uint8_t *dstptr[3];
  180. uint8_t planes[4][1024];
  181. uint8_t chroma[3][256];
  182. int i, j, p;
  183. /* Convert to YUVA444. */
  184. for (i = 0; i < 1024; i++) {
  185. planes[0][i] = (( 25 * ptr[0] + 129 * ptr[1] + 66 * ptr[2]) / 255) + 16;
  186. planes[1][i] = ((-38 * ptr[0] + 112 * ptr[1] + -74 * ptr[2]) / 255) + 128;
  187. planes[2][i] = ((-18 * ptr[0] + 112 * ptr[1] + -94 * ptr[2]) / 255) + 128;
  188. planes[3][i] = ptr[3];
  189. ptr += 4;
  190. }
  191. /* Subsample chroma. */
  192. for (i = 0; i < 32; i += 2)
  193. for (j = 0; j < 32; j += 2)
  194. for (p = 0; p < 3; p++)
  195. chroma[p][16 * (i / 2) + j / 2] = (planes[p + 1][32 * i + j ] +
  196. planes[p + 1][32 * i + j + 1] +
  197. planes[p + 1][32 * (i + 1) + j ] +
  198. planes[p + 1][32 * (i + 1) + j + 1]) / 4;
  199. /* Seek to x/y pos of cursor. */
  200. for (i = 0; i < 3; i++)
  201. dstptr[i] = ctx->final_frame->data[i] +
  202. (ctx->final_frame->linesize[i] * (cur_y >> !!i)) +
  203. (cur_x >> !!i) + !!i;
  204. /* Copy. */
  205. for (i = 0; i < FFMIN(32, avctx->height - cur_y) - 1; i += 2) {
  206. int lsize = FFMIN(32, avctx->width - cur_x);
  207. int csize = lsize / 2;
  208. fic_alpha_blend(dstptr[0],
  209. planes[0] + i * 32, lsize, planes[3] + i * 32);
  210. fic_alpha_blend(dstptr[0] + ctx->final_frame->linesize[0],
  211. planes[0] + (i + 1) * 32, lsize, planes[3] + (i + 1) * 32);
  212. fic_alpha_blend(dstptr[1],
  213. chroma[0] + (i / 2) * 16, csize, chroma[2] + (i / 2) * 16);
  214. fic_alpha_blend(dstptr[2],
  215. chroma[1] + (i / 2) * 16, csize, chroma[2] + (i / 2) * 16);
  216. dstptr[0] += ctx->final_frame->linesize[0] * 2;
  217. dstptr[1] += ctx->final_frame->linesize[1];
  218. dstptr[2] += ctx->final_frame->linesize[2];
  219. }
  220. }
  221. static int fic_decode_frame(AVCodecContext *avctx, void *data,
  222. int *got_frame, AVPacket *avpkt)
  223. {
  224. FICContext *ctx = avctx->priv_data;
  225. uint8_t *src = avpkt->data;
  226. int ret;
  227. int slice, nslices;
  228. int msize;
  229. int tsize;
  230. int cur_x, cur_y;
  231. int skip_cursor = ctx->skip_cursor;
  232. uint8_t *sdata;
  233. if ((ret = ff_reget_buffer(avctx, ctx->frame)) < 0)
  234. return ret;
  235. /* Header + at least one slice (4) */
  236. if (avpkt->size < FIC_HEADER_SIZE + 4) {
  237. av_log(avctx, AV_LOG_ERROR, "Frame data is too small.\n");
  238. return AVERROR_INVALIDDATA;
  239. }
  240. /* Check for header. */
  241. if (memcmp(src, fic_header, 7))
  242. av_log(avctx, AV_LOG_WARNING, "Invalid FIC Header.\n");
  243. /* Is it a skip frame? */
  244. if (src[17]) {
  245. if (!ctx->final_frame) {
  246. av_log(avctx, AV_LOG_WARNING, "Initial frame is skipped\n");
  247. return AVERROR_INVALIDDATA;
  248. }
  249. goto skip;
  250. }
  251. nslices = src[13];
  252. if (!nslices) {
  253. av_log(avctx, AV_LOG_ERROR, "Zero slices found.\n");
  254. return AVERROR_INVALIDDATA;
  255. }
  256. /* High or Low Quality Matrix? */
  257. ctx->qmat = src[23] ? fic_qmat_hq : fic_qmat_lq;
  258. /* Skip cursor data. */
  259. tsize = AV_RB24(src + 24);
  260. if (tsize > avpkt->size - FIC_HEADER_SIZE) {
  261. av_log(avctx, AV_LOG_ERROR,
  262. "Packet is too small to contain cursor (%d vs %d bytes).\n",
  263. tsize, avpkt->size - FIC_HEADER_SIZE);
  264. return AVERROR_INVALIDDATA;
  265. }
  266. if (!tsize || !AV_RL16(src + 37) || !AV_RL16(src + 39))
  267. skip_cursor = 1;
  268. if (!skip_cursor && tsize < 32) {
  269. av_log(avctx, AV_LOG_WARNING,
  270. "Cursor data too small. Skipping cursor.\n");
  271. skip_cursor = 1;
  272. }
  273. /* Cursor position. */
  274. cur_x = AV_RL16(src + 33);
  275. cur_y = AV_RL16(src + 35);
  276. if (!skip_cursor && (cur_x > avctx->width || cur_y > avctx->height)) {
  277. av_log(avctx, AV_LOG_DEBUG,
  278. "Invalid cursor position: (%d,%d). Skipping cursor.\n",
  279. cur_x, cur_y);
  280. skip_cursor = 1;
  281. }
  282. if (!skip_cursor && (AV_RL16(src + 37) != 32 || AV_RL16(src + 39) != 32)) {
  283. av_log(avctx, AV_LOG_WARNING,
  284. "Invalid cursor size. Skipping cursor.\n");
  285. skip_cursor = 1;
  286. }
  287. if (!skip_cursor && avpkt->size < CURSOR_OFFSET + sizeof(ctx->cursor_buf)) {
  288. skip_cursor = 1;
  289. }
  290. /* Slice height for all but the last slice. */
  291. ctx->slice_h = 16 * (ctx->aligned_height >> 4) / nslices;
  292. if (ctx->slice_h % 16)
  293. ctx->slice_h = FFALIGN(ctx->slice_h - 16, 16);
  294. /* First slice offset and remaining data. */
  295. sdata = src + tsize + FIC_HEADER_SIZE + 4 * nslices;
  296. msize = avpkt->size - nslices * 4 - tsize - FIC_HEADER_SIZE;
  297. if (msize <= 0) {
  298. av_log(avctx, AV_LOG_ERROR, "Not enough frame data to decode.\n");
  299. return AVERROR_INVALIDDATA;
  300. }
  301. /* Allocate slice data. */
  302. av_fast_malloc(&ctx->slice_data, &ctx->slice_data_size,
  303. nslices * sizeof(ctx->slice_data[0]));
  304. if (!ctx->slice_data_size) {
  305. av_log(avctx, AV_LOG_ERROR, "Could not allocate slice data.\n");
  306. return AVERROR(ENOMEM);
  307. }
  308. memset(ctx->slice_data, 0, nslices * sizeof(ctx->slice_data[0]));
  309. for (slice = 0; slice < nslices; slice++) {
  310. unsigned slice_off = AV_RB32(src + tsize + FIC_HEADER_SIZE + slice * 4);
  311. unsigned slice_size;
  312. int y_off = ctx->slice_h * slice;
  313. int slice_h = ctx->slice_h;
  314. /*
  315. * Either read the slice size, or consume all data left.
  316. * Also, special case the last slight height.
  317. */
  318. if (slice == nslices - 1) {
  319. slice_size = msize;
  320. slice_h = FFALIGN(avctx->height - ctx->slice_h * (nslices - 1), 16);
  321. } else {
  322. slice_size = AV_RB32(src + tsize + FIC_HEADER_SIZE + slice * 4 + 4);
  323. }
  324. if (slice_size < slice_off || slice_size > msize)
  325. continue;
  326. slice_size -= slice_off;
  327. ctx->slice_data[slice].src = sdata + slice_off;
  328. ctx->slice_data[slice].src_size = slice_size;
  329. ctx->slice_data[slice].slice_h = slice_h;
  330. ctx->slice_data[slice].y_off = y_off;
  331. }
  332. if ((ret = avctx->execute(avctx, fic_decode_slice, ctx->slice_data,
  333. NULL, nslices, sizeof(ctx->slice_data[0]))) < 0)
  334. return ret;
  335. ctx->frame->key_frame = 1;
  336. ctx->frame->pict_type = AV_PICTURE_TYPE_I;
  337. for (slice = 0; slice < nslices; slice++) {
  338. if (ctx->slice_data[slice].p_frame) {
  339. ctx->frame->key_frame = 0;
  340. ctx->frame->pict_type = AV_PICTURE_TYPE_P;
  341. break;
  342. }
  343. }
  344. av_frame_free(&ctx->final_frame);
  345. ctx->final_frame = av_frame_clone(ctx->frame);
  346. if (!ctx->final_frame) {
  347. av_log(avctx, AV_LOG_ERROR, "Could not clone frame buffer.\n");
  348. return AVERROR(ENOMEM);
  349. }
  350. /* Make sure we use a user-supplied buffer. */
  351. if ((ret = ff_reget_buffer(avctx, ctx->final_frame)) < 0) {
  352. av_log(avctx, AV_LOG_ERROR, "Could not make frame writable.\n");
  353. return ret;
  354. }
  355. /* Draw cursor. */
  356. if (!skip_cursor) {
  357. memcpy(ctx->cursor_buf, src + 59, 32 * 32 * 4);
  358. fic_draw_cursor(avctx, cur_x, cur_y);
  359. }
  360. skip:
  361. *got_frame = 1;
  362. if ((ret = av_frame_ref(data, ctx->final_frame)) < 0)
  363. return ret;
  364. return avpkt->size;
  365. }
  366. static av_cold int fic_decode_close(AVCodecContext *avctx)
  367. {
  368. FICContext *ctx = avctx->priv_data;
  369. av_freep(&ctx->slice_data);
  370. av_frame_free(&ctx->final_frame);
  371. av_frame_free(&ctx->frame);
  372. return 0;
  373. }
  374. static av_cold int fic_decode_init(AVCodecContext *avctx)
  375. {
  376. FICContext *ctx = avctx->priv_data;
  377. /* Initialize various context values */
  378. ctx->avctx = avctx;
  379. ctx->aligned_width = FFALIGN(avctx->width, 16);
  380. ctx->aligned_height = FFALIGN(avctx->height, 16);
  381. avctx->pix_fmt = AV_PIX_FMT_YUV420P;
  382. avctx->bits_per_raw_sample = 8;
  383. ctx->frame = av_frame_alloc();
  384. if (!ctx->frame)
  385. return AVERROR(ENOMEM);
  386. return 0;
  387. }
  388. static const AVOption options[] = {
  389. { "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 },
  390. { NULL },
  391. };
  392. static const AVClass fic_decoder_class = {
  393. .class_name = "FIC encoder",
  394. .item_name = av_default_item_name,
  395. .option = options,
  396. .version = LIBAVUTIL_VERSION_INT,
  397. };
  398. AVCodec ff_fic_decoder = {
  399. .name = "fic",
  400. .long_name = NULL_IF_CONFIG_SMALL("Mirillis FIC"),
  401. .type = AVMEDIA_TYPE_VIDEO,
  402. .id = AV_CODEC_ID_FIC,
  403. .priv_data_size = sizeof(FICContext),
  404. .init = fic_decode_init,
  405. .decode = fic_decode_frame,
  406. .close = fic_decode_close,
  407. .capabilities = AV_CODEC_CAP_DR1 | AV_CODEC_CAP_SLICE_THREADS,
  408. .priv_class = &fic_decoder_class,
  409. };