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.

460 lines
14KB

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