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.

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