GCC Code Coverage Report
Directory: ../../../ffmpeg/ Exec Total Coverage
File: src/libavcodec/fic.c Lines: 194 229 84.7 %
Date: 2021-04-20 15:25:36 Branches: 85 118 72.0 %

Line Branch Exec Source
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/mem_internal.h"
26
#include "libavutil/opt.h"
27
#include "avcodec.h"
28
#include "internal.h"
29
#include "get_bits.h"
30
#include "golomb.h"
31
32
typedef struct FICThreadContext {
33
    DECLARE_ALIGNED(16, int16_t, block)[64];
34
    uint8_t *src;
35
    int slice_h;
36
    int src_size;
37
    int y_off;
38
    int p_frame;
39
} FICThreadContext;
40
41
typedef struct FICContext {
42
    AVClass *class;
43
    AVCodecContext *avctx;
44
    AVFrame *frame;
45
    AVFrame *final_frame;
46
47
    FICThreadContext *slice_data;
48
    int slice_data_size;
49
50
    const uint8_t *qmat;
51
52
    enum AVPictureType cur_frame_type;
53
54
    int aligned_width, aligned_height;
55
    int num_slices, slice_h;
56
57
    uint8_t cursor_buf[4096];
58
    int skip_cursor;
59
} FICContext;
60
61
static const uint8_t fic_qmat_hq[64] = {
62
    1, 2, 2, 2, 3, 3, 3, 4,
63
    2, 2, 2, 3, 3, 3, 4, 4,
64
    2, 2, 3, 3, 3, 4, 4, 4,
65
    2, 2, 3, 3, 3, 4, 4, 5,
66
    2, 3, 3, 3, 4, 4, 5, 6,
67
    3, 3, 3, 4, 4, 5, 6, 7,
68
    3, 3, 3, 4, 4, 5, 7, 7,
69
    3, 3, 4, 4, 5, 7, 7, 7,
70
};
71
72
static const uint8_t fic_qmat_lq[64] = {
73
    1,  5,  6,  7,  8,  9,  9, 11,
74
    5,  5,  7,  8,  9,  9, 11, 12,
75
    6,  7,  8,  9,  9, 11, 11, 12,
76
    7,  7,  8,  9,  9, 11, 12, 13,
77
    7,  8,  9,  9, 10, 11, 13, 16,
78
    8,  9,  9, 10, 11, 13, 16, 19,
79
    8,  9,  9, 11, 12, 15, 18, 23,
80
    9,  9, 11, 12, 15, 18, 23, 27
81
};
82
83
static const uint8_t fic_header[7] = { 0, 0, 1, 'F', 'I', 'C', 'V' };
84
85
#define FIC_HEADER_SIZE 27
86
#define CURSOR_OFFSET 59
87
88
1030768
static av_always_inline void fic_idct(int16_t *blk, int step, int shift, int rnd)
89
{
90
1030768
    const unsigned t0 =  27246 * blk[3 * step] + 18405 * blk[5 * step];
91
1030768
    const unsigned t1 =  27246 * blk[5 * step] - 18405 * blk[3 * step];
92
1030768
    const unsigned t2 =   6393 * blk[7 * step] + 32139 * blk[1 * step];
93
1030768
    const unsigned t3 =   6393 * blk[1 * step] - 32139 * blk[7 * step];
94
1030768
    const unsigned t4 = 5793U * ((int)(t2 + t0 + 0x800) >> 12);
95
1030768
    const unsigned t5 = 5793U * ((int)(t3 + t1 + 0x800) >> 12);
96
1030768
    const unsigned t6 = t2 - t0;
97
1030768
    const unsigned t7 = t3 - t1;
98
1030768
    const unsigned t8 =  17734 * blk[2 * step] - 42813 * blk[6 * step];
99
1030768
    const unsigned t9 =  17734 * blk[6 * step] + 42814 * blk[2 * step];
100
1030768
    const unsigned tA = (blk[0 * step] - blk[4 * step]) * 32768 + rnd;
101
1030768
    const unsigned tB = (blk[0 * step] + blk[4 * step]) * 32768 + rnd;
102
1030768
    blk[0 * step] = (int)(  t4       + t9 + tB) >> shift;
103
1030768
    blk[1 * step] = (int)(  t6 + t7  + t8 + tA) >> shift;
104
1030768
    blk[2 * step] = (int)(  t6 - t7  - t8 + tA) >> shift;
105
1030768
    blk[3 * step] = (int)(  t5       - t9 + tB) >> shift;
106
1030768
    blk[4 * step] = (int)( -t5       - t9 + tB) >> shift;
107
1030768
    blk[5 * step] = (int)(-(t6 - t7) - t8 + tA) >> shift;
108
1030768
    blk[6 * step] = (int)(-(t6 + t7) + t8 + tA) >> shift;
109
1030768
    blk[7 * step] = (int)( -t4       + t9 + tB) >> shift;
110
1030768
}
111
112
64423
static void fic_idct_put(uint8_t *dst, int stride, int16_t *block)
113
{
114
    int i, j;
115
    int16_t *ptr;
116
117
64423
    ptr = block;
118
64423
    fic_idct(ptr++, 8, 13, (1 << 12) + (1 << 17));
119
515384
    for (i = 1; i < 8; i++) {
120
450961
        fic_idct(ptr, 8, 13, 1 << 12);
121
450961
        ptr++;
122
    }
123
124
64423
    ptr = block;
125
579807
    for (i = 0; i < 8; i++) {
126
515384
        fic_idct(ptr, 1, 20, 0);
127
515384
        ptr += 8;
128
    }
129
130
64423
    ptr = block;
131
579807
    for (j = 0; j < 8; j++) {
132
4638456
        for (i = 0; i < 8; i++)
133
4123072
            dst[i] = av_clip_uint8(ptr[i]);
134
515384
        dst += stride;
135
515384
        ptr += 8;
136
    }
137
64423
}
138
538550
static int fic_decode_block(FICContext *ctx, GetBitContext *gb,
139
                            uint8_t *dst, int stride, int16_t *block, int *is_p)
140
{
141
    int i, num_coeff;
142
143
538550
    if (get_bits_left(gb) < 8)
144
3
        return AVERROR_INVALIDDATA;
145
146
    /* Is it a skip block? */
147
538547
    if (get_bits1(gb)) {
148
474124
        *is_p = 1;
149
474124
        return 0;
150
    }
151
152
64423
    memset(block, 0, sizeof(*block) * 64);
153
154
64423
    num_coeff = get_bits(gb, 7);
155
64423
    if (num_coeff > 64)
156
        return AVERROR_INVALIDDATA;
157
158
1189311
    for (i = 0; i < num_coeff; i++) {
159
1124888
        int v = get_se_golomb(gb);
160

1124888
        if (v < -2048 || v > 2048)
161
             return AVERROR_INVALIDDATA;
162
1124888
        block[ff_zigzag_direct[i]] = v *
163
1124888
                                     ctx->qmat[ff_zigzag_direct[i]];
164
    }
165
166
64423
    fic_idct_put(dst, stride, block);
167
168
64423
    return 0;
169
}
170
171
92
static int fic_decode_slice(AVCodecContext *avctx, void *tdata)
172
{
173
92
    FICContext *ctx        = avctx->priv_data;
174
92
    FICThreadContext *tctx = tdata;
175
    GetBitContext gb;
176
92
    uint8_t *src = tctx->src;
177
92
    int slice_h  = tctx->slice_h;
178
92
    int src_size = tctx->src_size;
179
92
    int y_off    = tctx->y_off;
180
    int x, y, p, ret;
181
182
92
    ret = init_get_bits8(&gb, src, src_size);
183
92
    if (ret < 0)
184
4
        return ret;
185
186
349
    for (p = 0; p < 3; p++) {
187
264
        int stride   = ctx->frame->linesize[p];
188
264
        uint8_t* dst = ctx->frame->data[p] + (y_off >> !!p) * stride;
189
190
4485
        for (y = 0; y < (slice_h >> !!p); y += 8) {
191
542771
            for (x = 0; x < (ctx->aligned_width >> !!p); x += 8) {
192
                int ret;
193
194
538550
                if ((ret = fic_decode_block(ctx, &gb, dst + x, stride,
195
538550
                                            tctx->block, &tctx->p_frame)) != 0)
196
3
                    return ret;
197
            }
198
199
4221
            dst += 8 * stride;
200
        }
201
    }
202
203
85
    return 0;
204
}
205
206
1472
static av_always_inline void fic_alpha_blend(uint8_t *dst, uint8_t *src,
207
                                             int size, uint8_t *alpha)
208
{
209
    int i;
210
211
36800
    for (i = 0; i < size; i++)
212
35328
        dst[i] += ((src[i] - dst[i]) * alpha[i]) >> 8;
213
1472
}
214
215
23
static void fic_draw_cursor(AVCodecContext *avctx, int cur_x, int cur_y)
216
{
217
23
    FICContext *ctx = avctx->priv_data;
218
23
    uint8_t *ptr    = ctx->cursor_buf;
219
    uint8_t *dstptr[3];
220
    uint8_t planes[4][1024];
221
    uint8_t chroma[3][256];
222
    int i, j, p;
223
224
    /* Convert to YUVA444. */
225
23575
    for (i = 0; i < 1024; i++) {
226
23552
        planes[0][i] = (( 25 * ptr[0] + 129 * ptr[1] +  66 * ptr[2]) / 255) + 16;
227
23552
        planes[1][i] = ((-38 * ptr[0] + 112 * ptr[1] + -74 * ptr[2]) / 255) + 128;
228
23552
        planes[2][i] = ((-18 * ptr[0] + 112 * ptr[1] + -94 * ptr[2]) / 255) + 128;
229
23552
        planes[3][i] = ptr[3];
230
231
23552
        ptr += 4;
232
    }
233
234
    /* Subsample chroma. */
235
391
    for (i = 0; i < 32; i += 2)
236
6256
        for (j = 0; j < 32; j += 2)
237
23552
            for (p = 0; p < 3; p++)
238
17664
                chroma[p][16 * (i / 2) + j / 2] = (planes[p + 1][32 *  i      + j    ] +
239
17664
                                                   planes[p + 1][32 *  i      + j + 1] +
240
17664
                                                   planes[p + 1][32 * (i + 1) + j    ] +
241
17664
                                                   planes[p + 1][32 * (i + 1) + j + 1]) / 4;
242
243
    /* Seek to x/y pos of cursor. */
244
92
    for (i = 0; i < 3; i++)
245
69
        dstptr[i] = ctx->final_frame->data[i]                        +
246
69
                    (ctx->final_frame->linesize[i] * (cur_y >> !!i)) +
247
69
                    (cur_x >> !!i) + !!i;
248
249
    /* Copy. */
250
391
    for (i = 0; i < FFMIN(32, avctx->height - cur_y) - 1; i += 2) {
251
368
        int lsize = FFMIN(32, avctx->width - cur_x);
252
368
        int csize = lsize / 2;
253
254
368
        fic_alpha_blend(dstptr[0],
255
368
                        planes[0] + i * 32, lsize, planes[3] + i * 32);
256
368
        fic_alpha_blend(dstptr[0] + ctx->final_frame->linesize[0],
257
368
                        planes[0] + (i + 1) * 32, lsize, planes[3] + (i + 1) * 32);
258
368
        fic_alpha_blend(dstptr[1],
259
368
                        chroma[0] + (i / 2) * 16, csize, chroma[2] + (i / 2) * 16);
260
368
        fic_alpha_blend(dstptr[2],
261
368
                        chroma[1] + (i / 2) * 16, csize, chroma[2] + (i / 2) * 16);
262
263
368
        dstptr[0] += ctx->final_frame->linesize[0] * 2;
264
368
        dstptr[1] += ctx->final_frame->linesize[1];
265
368
        dstptr[2] += ctx->final_frame->linesize[2];
266
    }
267
23
}
268
269
121
static int fic_decode_frame(AVCodecContext *avctx, void *data,
270
                            int *got_frame, AVPacket *avpkt)
271
{
272
121
    FICContext *ctx = avctx->priv_data;
273
121
    uint8_t *src = avpkt->data;
274
    int ret;
275
    int slice, nslices;
276
    int msize;
277
    int tsize;
278
    int cur_x, cur_y;
279
121
    int skip_cursor = ctx->skip_cursor;
280
    uint8_t *sdata;
281
282
121
    if ((ret = ff_reget_buffer(avctx, ctx->frame, 0)) < 0)
283
        return ret;
284
285
    /* Header + at least one slice (4) */
286
121
    if (avpkt->size < FIC_HEADER_SIZE + 4) {
287
        av_log(avctx, AV_LOG_ERROR, "Frame data is too small.\n");
288
        return AVERROR_INVALIDDATA;
289
    }
290
291
    /* Check for header. */
292
121
    if (memcmp(src, fic_header, 7))
293
        av_log(avctx, AV_LOG_WARNING, "Invalid FIC Header.\n");
294
295
    /* Is it a skip frame? */
296
121
    if (src[17]) {
297
98
        if (!ctx->final_frame) {
298
            av_log(avctx, AV_LOG_WARNING, "Initial frame is skipped\n");
299
            return AVERROR_INVALIDDATA;
300
        }
301
98
        goto skip;
302
    }
303
304
23
    nslices = src[13];
305
23
    if (!nslices) {
306
        av_log(avctx, AV_LOG_ERROR, "Zero slices found.\n");
307
        return AVERROR_INVALIDDATA;
308
    }
309
310
    /* High or Low Quality Matrix? */
311
23
    ctx->qmat = src[23] ? fic_qmat_hq : fic_qmat_lq;
312
313
    /* Skip cursor data. */
314
23
    tsize = AV_RB24(src + 24);
315
23
    if (tsize > avpkt->size - FIC_HEADER_SIZE) {
316
        av_log(avctx, AV_LOG_ERROR,
317
               "Packet is too small to contain cursor (%d vs %d bytes).\n",
318
               tsize, avpkt->size - FIC_HEADER_SIZE);
319
        return AVERROR_INVALIDDATA;
320
    }
321
322

23
    if (!tsize || !AV_RL16(src + 37) || !AV_RL16(src + 39))
323
        skip_cursor = 1;
324
325

23
    if (!skip_cursor && tsize < 32) {
326
        av_log(avctx, AV_LOG_WARNING,
327
               "Cursor data too small. Skipping cursor.\n");
328
        skip_cursor = 1;
329
    }
330
331
    /* Cursor position. */
332
23
    cur_x = AV_RL16(src + 33);
333
23
    cur_y = AV_RL16(src + 35);
334

23
    if (!skip_cursor && (cur_x > avctx->width || cur_y > avctx->height)) {
335
        av_log(avctx, AV_LOG_DEBUG,
336
               "Invalid cursor position: (%d,%d). Skipping cursor.\n",
337
               cur_x, cur_y);
338
        skip_cursor = 1;
339
    }
340
341

23
    if (!skip_cursor && (AV_RL16(src + 37) != 32 || AV_RL16(src + 39) != 32)) {
342
        av_log(avctx, AV_LOG_WARNING,
343
               "Invalid cursor size. Skipping cursor.\n");
344
        skip_cursor = 1;
345
    }
346
347

23
    if (!skip_cursor && avpkt->size < CURSOR_OFFSET + sizeof(ctx->cursor_buf)) {
348
        skip_cursor = 1;
349
    }
350
351
    /* Slice height for all but the last slice. */
352
23
    ctx->slice_h = 16 * (ctx->aligned_height >> 4) / nslices;
353
23
    if (ctx->slice_h % 16)
354
        ctx->slice_h = FFALIGN(ctx->slice_h - 16, 16);
355
356
    /* First slice offset and remaining data. */
357
23
    sdata = src + tsize + FIC_HEADER_SIZE + 4 * nslices;
358
23
    msize = avpkt->size - nslices * 4 - tsize - FIC_HEADER_SIZE;
359
360
23
    if (msize <= ctx->aligned_width/8 * (ctx->aligned_height/8) / 8) {
361
        av_log(avctx, AV_LOG_ERROR, "Not enough frame data to decode.\n");
362
        return AVERROR_INVALIDDATA;
363
    }
364
365
    /* Allocate slice data. */
366
23
    av_fast_malloc(&ctx->slice_data, &ctx->slice_data_size,
367
                   nslices * sizeof(ctx->slice_data[0]));
368
23
    if (!ctx->slice_data_size) {
369
        av_log(avctx, AV_LOG_ERROR, "Could not allocate slice data.\n");
370
        return AVERROR(ENOMEM);
371
    }
372
23
    memset(ctx->slice_data, 0, nslices * sizeof(ctx->slice_data[0]));
373
374
115
    for (slice = 0; slice < nslices; slice++) {
375
92
        unsigned slice_off = AV_RB32(src + tsize + FIC_HEADER_SIZE + slice * 4);
376
        unsigned slice_size;
377
92
        int y_off   = ctx->slice_h * slice;
378
92
        int slice_h = ctx->slice_h;
379
380
        /*
381
         * Either read the slice size, or consume all data left.
382
         * Also, special case the last slight height.
383
         */
384
92
        if (slice == nslices - 1) {
385
23
            slice_size   = msize;
386
23
            slice_h      = FFALIGN(avctx->height - ctx->slice_h * (nslices - 1), 16);
387
        } else {
388
69
            slice_size = AV_RB32(src + tsize + FIC_HEADER_SIZE + slice * 4 + 4);
389
69
            if (slice_size < slice_off)
390
                return AVERROR_INVALIDDATA;
391
        }
392
393

92
        if (slice_size < slice_off || slice_size > msize)
394
4
            continue;
395
396
88
        slice_size -= slice_off;
397
398
88
        ctx->slice_data[slice].src      = sdata + slice_off;
399
88
        ctx->slice_data[slice].src_size = slice_size;
400
88
        ctx->slice_data[slice].slice_h  = slice_h;
401
88
        ctx->slice_data[slice].y_off    = y_off;
402
    }
403
404
23
    if ((ret = avctx->execute(avctx, fic_decode_slice, ctx->slice_data,
405
                              NULL, nslices, sizeof(ctx->slice_data[0]))) < 0)
406
        return ret;
407
408
23
    ctx->frame->key_frame = 1;
409
23
    ctx->frame->pict_type = AV_PICTURE_TYPE_I;
410
35
    for (slice = 0; slice < nslices; slice++) {
411
32
        if (ctx->slice_data[slice].p_frame) {
412
20
            ctx->frame->key_frame = 0;
413
20
            ctx->frame->pict_type = AV_PICTURE_TYPE_P;
414
20
            break;
415
        }
416
    }
417
23
    av_frame_free(&ctx->final_frame);
418
23
    ctx->final_frame = av_frame_clone(ctx->frame);
419
23
    if (!ctx->final_frame) {
420
        av_log(avctx, AV_LOG_ERROR, "Could not clone frame buffer.\n");
421
        return AVERROR(ENOMEM);
422
    }
423
424
    /* Make sure we use a user-supplied buffer. */
425
23
    if ((ret = ff_reget_buffer(avctx, ctx->final_frame, 0)) < 0) {
426
        av_log(avctx, AV_LOG_ERROR, "Could not make frame writable.\n");
427
        return ret;
428
    }
429
430
    /* Draw cursor. */
431
23
    if (!skip_cursor) {
432
23
        memcpy(ctx->cursor_buf, src + CURSOR_OFFSET, sizeof(ctx->cursor_buf));
433
23
        fic_draw_cursor(avctx, cur_x, cur_y);
434
    }
435
436
skip:
437
121
    *got_frame = 1;
438
121
    if ((ret = av_frame_ref(data, ctx->final_frame)) < 0)
439
        return ret;
440
441
121
    return avpkt->size;
442
}
443
444
2
static av_cold int fic_decode_close(AVCodecContext *avctx)
445
{
446
2
    FICContext *ctx = avctx->priv_data;
447
448
2
    av_freep(&ctx->slice_data);
449
2
    av_frame_free(&ctx->final_frame);
450
2
    av_frame_free(&ctx->frame);
451
452
2
    return 0;
453
}
454
455
2
static av_cold int fic_decode_init(AVCodecContext *avctx)
456
{
457
2
    FICContext *ctx = avctx->priv_data;
458
459
    /* Initialize various context values */
460
2
    ctx->avctx            = avctx;
461
2
    ctx->aligned_width    = FFALIGN(avctx->width,  16);
462
2
    ctx->aligned_height   = FFALIGN(avctx->height, 16);
463
464
2
    avctx->pix_fmt             = AV_PIX_FMT_YUV420P;
465
2
    avctx->bits_per_raw_sample = 8;
466
467
2
    ctx->frame = av_frame_alloc();
468
2
    if (!ctx->frame)
469
        return AVERROR(ENOMEM);
470
471
2
    return 0;
472
}
473
474
static const AVOption options[] = {
475
{ "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 },
476
{ NULL },
477
};
478
479
static const AVClass fic_decoder_class = {
480
    .class_name = "FIC decoder",
481
    .item_name  = av_default_item_name,
482
    .option     = options,
483
    .version    = LIBAVUTIL_VERSION_INT,
484
};
485
486
AVCodec ff_fic_decoder = {
487
    .name           = "fic",
488
    .long_name      = NULL_IF_CONFIG_SMALL("Mirillis FIC"),
489
    .type           = AVMEDIA_TYPE_VIDEO,
490
    .id             = AV_CODEC_ID_FIC,
491
    .priv_data_size = sizeof(FICContext),
492
    .init           = fic_decode_init,
493
    .decode         = fic_decode_frame,
494
    .close          = fic_decode_close,
495
    .capabilities   = AV_CODEC_CAP_DR1 | AV_CODEC_CAP_SLICE_THREADS,
496
    .priv_class     = &fic_decoder_class,
497
};