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.

347 lines
10KB

  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 "internal.h"
  26. #include "get_bits.h"
  27. #include "golomb.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. FICThreadContext *slice_data;
  39. int slice_data_size;
  40. const uint8_t *qmat;
  41. enum AVPictureType cur_frame_type;
  42. int aligned_width, aligned_height;
  43. int num_slices, slice_h;
  44. } FICContext;
  45. static const uint8_t fic_qmat_hq[64] = {
  46. 1, 2, 2, 2, 3, 3, 3, 4,
  47. 2, 2, 2, 3, 3, 3, 4, 4,
  48. 2, 2, 3, 3, 3, 4, 4, 4,
  49. 2, 2, 3, 3, 3, 4, 4, 5,
  50. 2, 3, 3, 3, 4, 4, 5, 6,
  51. 3, 3, 3, 4, 4, 5, 6, 7,
  52. 3, 3, 3, 4, 4, 5, 7, 7,
  53. 3, 3, 4, 4, 5, 7, 7, 7,
  54. };
  55. static const uint8_t fic_qmat_lq[64] = {
  56. 1, 5, 6, 7, 8, 9, 9, 11,
  57. 5, 5, 7, 8, 9, 9, 11, 12,
  58. 6, 7, 8, 9, 9, 11, 11, 12,
  59. 7, 7, 8, 9, 9, 11, 12, 13,
  60. 7, 8, 9, 9, 10, 11, 13, 16,
  61. 8, 9, 9, 10, 11, 13, 16, 19,
  62. 8, 9, 9, 11, 12, 15, 18, 23,
  63. 9, 9, 11, 12, 15, 18, 23, 27
  64. };
  65. static const uint8_t fic_header[7] = { 0, 0, 1, 'F', 'I', 'C', 'V' };
  66. #define FIC_HEADER_SIZE 27
  67. static av_always_inline void fic_idct(int16_t *blk, int step, int shift)
  68. {
  69. const int t0 = 27246 * blk[3 * step] + 18405 * blk[5 * step];
  70. const int t1 = 27246 * blk[5 * step] - 18405 * blk[3 * step];
  71. const int t2 = 6393 * blk[7 * step] + 32139 * blk[1 * step];
  72. const int t3 = 6393 * blk[1 * step] - 32139 * blk[7 * step];
  73. const int t4 = 5793 * (t2 + t0 + 0x800 >> 12);
  74. const int t5 = 5793 * (t3 + t1 + 0x800 >> 12);
  75. const int t6 = t2 - t0;
  76. const int t7 = t3 - t1;
  77. const int t8 = 17734 * blk[2 * step] - 42813 * blk[6 * step];
  78. const int t9 = 17734 * blk[6 * step] + 42814 * blk[2 * step];
  79. const int tA = (blk[0 * step] - blk[4 * step] << 15) + (1 << shift - 1);
  80. const int tB = (blk[0 * step] + blk[4 * step] << 15) + (1 << shift - 1);
  81. blk[0 * step] = ( t4 + t9 + tB) >> shift;
  82. blk[1 * step] = ( t6 + t7 + t8 + tA) >> shift;
  83. blk[2 * step] = ( t6 - t7 - t8 + tA) >> shift;
  84. blk[3 * step] = ( t5 - t9 + tB) >> shift;
  85. blk[4 * step] = ( -t5 - t9 + tB) >> shift;
  86. blk[5 * step] = (-(t6 - t7) - t8 + tA) >> shift;
  87. blk[6 * step] = (-(t6 + t7) + t8 + tA) >> shift;
  88. blk[7 * step] = ( -t4 + t9 + tB) >> shift;
  89. }
  90. static void fic_idct_put(uint8_t *dst, int stride, int16_t *block)
  91. {
  92. int i, j;
  93. int16_t *ptr;
  94. ptr = block;
  95. for (i = 0; i < 8; i++) {
  96. fic_idct(ptr, 8, 13);
  97. ptr++;
  98. }
  99. ptr = block;
  100. for (i = 0; i < 8; i++) {
  101. fic_idct(ptr, 1, 20);
  102. ptr += 8;
  103. }
  104. ptr = block;
  105. for (j = 0; j < 8; j++) {
  106. for (i = 0; i < 8; i++)
  107. dst[i] = av_clip_uint8(ptr[i]);
  108. dst += stride;
  109. ptr += 8;
  110. }
  111. }
  112. static int fic_decode_block(FICContext *ctx, GetBitContext *gb,
  113. uint8_t *dst, int stride, int16_t *block)
  114. {
  115. int i, num_coeff;
  116. /* Is it a skip block? */
  117. if (get_bits1(gb)) {
  118. /* This is a P-frame. */
  119. ctx->frame->key_frame = 0;
  120. ctx->frame->pict_type = AV_PICTURE_TYPE_P;
  121. return 0;
  122. }
  123. memset(block, 0, sizeof(*block) * 64);
  124. num_coeff = get_bits(gb, 7);
  125. if (num_coeff > 64)
  126. return AVERROR_INVALIDDATA;
  127. for (i = 0; i < num_coeff; i++)
  128. block[ff_zigzag_direct[i]] = get_se_golomb(gb) *
  129. ctx->qmat[ff_zigzag_direct[i]];
  130. fic_idct_put(dst, stride, block);
  131. return 0;
  132. }
  133. static int fic_decode_slice(AVCodecContext *avctx, void *tdata)
  134. {
  135. FICContext *ctx = avctx->priv_data;
  136. FICThreadContext *tctx = tdata;
  137. GetBitContext gb;
  138. uint8_t *src = tctx->src;
  139. int slice_h = tctx->slice_h;
  140. int src_size = tctx->src_size;
  141. int y_off = tctx->y_off;
  142. int x, y, p;
  143. init_get_bits(&gb, src, src_size * 8);
  144. for (p = 0; p < 3; p++) {
  145. int stride = ctx->frame->linesize[p];
  146. uint8_t* dst = ctx->frame->data[p] + (y_off >> !!p) * stride;
  147. for (y = 0; y < (slice_h >> !!p); y += 8) {
  148. for (x = 0; x < (ctx->aligned_width >> !!p); x += 8) {
  149. int ret;
  150. if ((ret = fic_decode_block(ctx, &gb, dst + x, stride, tctx->block)) != 0)
  151. return ret;
  152. }
  153. dst += 8 * stride;
  154. }
  155. }
  156. return 0;
  157. }
  158. static int fic_decode_frame(AVCodecContext *avctx, void *data,
  159. int *got_frame, AVPacket *avpkt)
  160. {
  161. FICContext *ctx = avctx->priv_data;
  162. uint8_t *src = avpkt->data;
  163. int ret;
  164. int slice, nslices;
  165. int msize;
  166. int tsize;
  167. uint8_t *sdata;
  168. if ((ret = ff_reget_buffer(avctx, ctx->frame)) < 0) {
  169. av_log(avctx, AV_LOG_ERROR, "reget_buffer() failed\n");
  170. return ret;
  171. }
  172. /* Header + at least one slice (4) */
  173. if (avpkt->size < FIC_HEADER_SIZE + 4) {
  174. av_log(avctx, AV_LOG_ERROR, "Frame data is too small.\n");
  175. return AVERROR_INVALIDDATA;
  176. }
  177. /* Check for header. */
  178. if (memcmp(src, fic_header, 7))
  179. av_log(avctx, AV_LOG_WARNING, "Invalid FIC Header.\n");
  180. /* Is it a skip frame? */
  181. if (src[17])
  182. goto skip;
  183. nslices = src[13];
  184. if (!nslices) {
  185. av_log(avctx, AV_LOG_ERROR, "Zero slices found.\n");
  186. return AVERROR_INVALIDDATA;
  187. }
  188. /* High or Low Quality Matrix? */
  189. ctx->qmat = src[23] ? fic_qmat_hq : fic_qmat_lq;
  190. /* Skip cursor data. */
  191. tsize = AV_RB24(src + 24);
  192. if (tsize > avpkt->size - FIC_HEADER_SIZE) {
  193. av_log(avctx, AV_LOG_ERROR, "Invalid cursor data size.\n");
  194. return AVERROR_INVALIDDATA;
  195. }
  196. /* Slice height for all but the last slice. */
  197. ctx->slice_h = 16 * (ctx->aligned_height >> 4) / nslices;
  198. if (ctx->slice_h % 16)
  199. ctx->slice_h = FFALIGN(ctx->slice_h - 16, 16);
  200. /* First slice offset and remaining data. */
  201. sdata = src + tsize + FIC_HEADER_SIZE + 4 * nslices;
  202. msize = avpkt->size - nslices * 4 - tsize - FIC_HEADER_SIZE;
  203. if (msize <= 0) {
  204. av_log(avctx, AV_LOG_ERROR, "Not enough frame data to decode.\n");
  205. return AVERROR_INVALIDDATA;
  206. }
  207. /*
  208. * Set the frametype to I initially. It will be set to P if the frame
  209. * has any dependencies (skip blocks). There will be a race condition
  210. * inside the slice decode function to set these, but we do not care.
  211. * since they will only ever be set to 0/P.
  212. */
  213. ctx->frame->key_frame = 1;
  214. ctx->frame->pict_type = AV_PICTURE_TYPE_I;
  215. /* Allocate slice data. */
  216. av_fast_malloc(&ctx->slice_data, &ctx->slice_data_size,
  217. nslices * sizeof(ctx->slice_data[0]));
  218. if (!ctx->slice_data_size) {
  219. av_log(avctx, AV_LOG_ERROR, "Could not allocate slice data.\n");
  220. return AVERROR(ENOMEM);
  221. }
  222. memset(ctx->slice_data, 0, nslices * sizeof(ctx->slice_data[0]));
  223. for (slice = 0; slice < nslices; slice++) {
  224. unsigned slice_off = AV_RB32(src + tsize + FIC_HEADER_SIZE + slice * 4);
  225. unsigned slice_size;
  226. int y_off = ctx->slice_h * slice;
  227. int slice_h = ctx->slice_h;
  228. /*
  229. * Either read the slice size, or consume all data left.
  230. * Also, special case the last slight height.
  231. */
  232. if (slice == nslices - 1) {
  233. slice_size = msize;
  234. slice_h = FFALIGN(avctx->height - ctx->slice_h * (nslices - 1), 16);
  235. } else {
  236. slice_size = AV_RB32(src + tsize + FIC_HEADER_SIZE + slice * 4 + 4);
  237. }
  238. if (slice_size < slice_off || slice_size > msize)
  239. continue;
  240. slice_size -= slice_off;
  241. ctx->slice_data[slice].src = sdata + slice_off;
  242. ctx->slice_data[slice].src_size = slice_size;
  243. ctx->slice_data[slice].slice_h = slice_h;
  244. ctx->slice_data[slice].y_off = y_off;
  245. }
  246. if ((ret = avctx->execute(avctx, fic_decode_slice, ctx->slice_data,
  247. NULL, nslices, sizeof(ctx->slice_data[0]))) < 0)
  248. return ret;
  249. skip:
  250. *got_frame = 1;
  251. if ((ret = av_frame_ref(data, ctx->frame)) < 0)
  252. return ret;
  253. return avpkt->size;
  254. }
  255. static av_cold int fic_decode_close(AVCodecContext *avctx)
  256. {
  257. FICContext *ctx = avctx->priv_data;
  258. av_freep(&ctx->slice_data);
  259. av_frame_free(&ctx->frame);
  260. return 0;
  261. }
  262. static av_cold int fic_decode_init(AVCodecContext *avctx)
  263. {
  264. FICContext *ctx = avctx->priv_data;
  265. /* Initialize various context values */
  266. ctx->avctx = avctx;
  267. ctx->aligned_width = FFALIGN(avctx->width, 16);
  268. ctx->aligned_height = FFALIGN(avctx->height, 16);
  269. avctx->pix_fmt = AV_PIX_FMT_YUV420P;
  270. avctx->bits_per_raw_sample = 8;
  271. ctx->frame = av_frame_alloc();
  272. if (!ctx->frame)
  273. return AVERROR(ENOMEM);
  274. return 0;
  275. }
  276. AVCodec ff_fic_decoder = {
  277. .name = "fic",
  278. .long_name = NULL_IF_CONFIG_SMALL("Mirillis FIC"),
  279. .type = AVMEDIA_TYPE_VIDEO,
  280. .id = AV_CODEC_ID_FIC,
  281. .priv_data_size = sizeof(FICContext),
  282. .init = fic_decode_init,
  283. .decode = fic_decode_frame,
  284. .close = fic_decode_close,
  285. .capabilities = CODEC_CAP_DR1 | CODEC_CAP_SLICE_THREADS,
  286. };