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