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.

304 lines
8.5KB

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