GCC Code Coverage Report
Directory: ../../../ffmpeg/ Exec Total Coverage
File: src/libavcodec/pafvideo.c Lines: 193 225 85.8 %
Date: 2021-04-20 04:37:23 Branches: 92 126 73.0 %

Line Branch Exec Source
1
/*
2
 * Packed Animation File video decoder
3
 * Copyright (c) 2012 Paul B Mahol
4
 *
5
 * This file is part of FFmpeg.
6
 *
7
 * FFmpeg is free software; you can redistribute it and/or
8
 * modify it under the terms of the GNU Lesser General Public
9
 * License as published by the Free Software Foundation; either
10
 * version 2.1 of the License, or (at your option) any later version.
11
 *
12
 * FFmpeg is distributed in the hope that it will be useful,
13
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
14
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
15
 * Lesser General Public License for more details.
16
 *
17
 * You should have received a copy of the GNU Lesser General Public
18
 * License along with FFmpeg; if not, write to the Free Software
19
 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
20
 */
21
22
#include "libavutil/imgutils.h"
23
24
#include "avcodec.h"
25
#include "bytestream.h"
26
#include "copy_block.h"
27
#include "internal.h"
28
29
30
static const uint8_t block_sequences[16][8] = {
31
    { 0, 0, 0, 0, 0, 0, 0, 0 },
32
    { 2, 0, 0, 0, 0, 0, 0, 0 },
33
    { 5, 7, 0, 0, 0, 0, 0, 0 },
34
    { 5, 0, 0, 0, 0, 0, 0, 0 },
35
    { 6, 0, 0, 0, 0, 0, 0, 0 },
36
    { 5, 7, 5, 7, 0, 0, 0, 0 },
37
    { 5, 7, 5, 0, 0, 0, 0, 0 },
38
    { 5, 7, 6, 0, 0, 0, 0, 0 },
39
    { 5, 5, 0, 0, 0, 0, 0, 0 },
40
    { 3, 0, 0, 0, 0, 0, 0, 0 },
41
    { 6, 6, 0, 0, 0, 0, 0, 0 },
42
    { 2, 4, 0, 0, 0, 0, 0, 0 },
43
    { 2, 4, 5, 7, 0, 0, 0, 0 },
44
    { 2, 4, 5, 0, 0, 0, 0, 0 },
45
    { 2, 4, 6, 0, 0, 0, 0, 0 },
46
    { 2, 4, 5, 7, 5, 7, 0, 0 },
47
};
48
49
typedef struct PAFVideoDecContext {
50
    AVFrame  *pic;
51
    GetByteContext gb;
52
53
    int width;
54
    int height;
55
56
    int current_frame;
57
    uint8_t *frame[4];
58
    int dirty[4];
59
    int frame_size;
60
    int video_size;
61
62
    uint8_t *opcodes;
63
} PAFVideoDecContext;
64
65
4
static av_cold int paf_video_close(AVCodecContext *avctx)
66
{
67
4
    PAFVideoDecContext *c = avctx->priv_data;
68
    int i;
69
70
4
    av_frame_free(&c->pic);
71
72
20
    for (i = 0; i < 4; i++)
73
16
        av_freep(&c->frame[i]);
74
75
4
    return 0;
76
}
77
78
4
static av_cold int paf_video_init(AVCodecContext *avctx)
79
{
80
4
    PAFVideoDecContext *c = avctx->priv_data;
81
    int i;
82
    int ret;
83
84
4
    c->width  = avctx->width;
85
4
    c->height = avctx->height;
86
87

4
    if (avctx->height & 3 || avctx->width & 3) {
88
        av_log(avctx, AV_LOG_ERROR,
89
               "width %d and height %d must be multiplie of 4.\n",
90
               avctx->width, avctx->height);
91
        return AVERROR_INVALIDDATA;
92
    }
93
94
4
    avctx->pix_fmt = AV_PIX_FMT_PAL8;
95
4
    ret = av_image_check_size2(avctx->width, FFALIGN(avctx->height, 256), avctx->max_pixels, avctx->pix_fmt, 0, avctx);
96
4
    if (ret < 0)
97
        return ret;
98
99
4
    c->pic = av_frame_alloc();
100
4
    if (!c->pic)
101
        return AVERROR(ENOMEM);
102
103
4
    c->frame_size = avctx->width * FFALIGN(avctx->height, 256);
104
4
    c->video_size = avctx->width * avctx->height;
105
20
    for (i = 0; i < 4; i++) {
106
16
        c->frame[i] = av_mallocz(c->frame_size);
107
16
        if (!c->frame[i])
108
            return AVERROR(ENOMEM);
109
    }
110
111
4
    return 0;
112
}
113
114
612
static void read4x4block(PAFVideoDecContext *c, uint8_t *dst, int width)
115
{
116
    int i;
117
118
3060
    for (i = 0; i < 4; i++) {
119
2448
        bytestream2_get_buffer(&c->gb, dst, 4);
120
2448
        dst += width;
121
    }
122
612
}
123
124
9005
static void copy_color_mask(uint8_t *dst, int width, uint8_t mask, uint8_t color)
125
{
126
    int i;
127
128
45025
    for (i = 0; i < 4; i++) {
129
36020
        if (mask & (1 << 7 - i))
130
7465
            dst[i] = color;
131
36020
        if (mask & (1 << 3 - i))
132
7596
            dst[width + i] = color;
133
    }
134
9005
}
135
136
10810
static void copy_src_mask(uint8_t *dst, int width, uint8_t mask, const uint8_t *src)
137
{
138
    int i;
139
140
54050
    for (i = 0; i < 4; i++) {
141
43240
        if (mask & (1 << 7 - i))
142
14148
            dst[i] = src[i];
143
43240
        if (mask & (1 << 3 - i))
144
14674
            dst[width + i] = src[width + i];
145
    }
146
10810
}
147
148
24878
static void set_src_position(PAFVideoDecContext *c,
149
                             const uint8_t **p,
150
                             const uint8_t **pend)
151
{
152
24878
    int val  = bytestream2_get_be16(&c->gb);
153
24878
    int page = val >> 14;
154
24878
    int x    = (val & 0x7F);
155
24878
    int y    = ((val >> 7) & 0x7F);
156
157
24878
    *p    = c->frame[page] + x * 2 + y * 2 * c->width;
158
24878
    *pend = c->frame[page] + c->frame_size;
159
24878
}
160
161
6
static int decode_0(PAFVideoDecContext *c, uint8_t *pkt, uint8_t code)
162
{
163
    uint32_t opcode_size, offset;
164
6
    uint8_t *dst, *dend, mask = 0, color = 0;
165
    const uint8_t *src, *send, *opcodes;
166
6
    int i, j, op = 0;
167
168
6
    i = bytestream2_get_byte(&c->gb);
169
6
    if (i) {
170
6
        if (code & 0x10) {
171
            int align;
172
173
6
            align = bytestream2_tell(&c->gb) & 3;
174
6
            if (align)
175
6
                bytestream2_skip(&c->gb, 4 - align);
176
        }
177
        do {
178
            int page, val, x, y;
179
6
            val    = bytestream2_get_be16(&c->gb);
180
6
            page   = val >> 14;
181
6
            x      = (val & 0x7F) * 2;
182
6
            y      = ((val >> 7) & 0x7F) * 2;
183
6
            dst    = c->frame[page] + x + y * c->width;
184
6
            dend   = c->frame[page] + c->frame_size;
185
6
            offset = (x & 0x7F) * 2;
186
6
            j      = bytestream2_get_le16(&c->gb) + offset;
187
6
            if (bytestream2_get_bytes_left(&c->gb) < (j - offset) * 16)
188
                return AVERROR_INVALIDDATA;
189
6
            c->dirty[page] = 1;
190
            do {
191
612
                offset++;
192
612
                if (dst + 3 * c->width + 4 > dend)
193
                    return AVERROR_INVALIDDATA;
194
612
                read4x4block(c, dst, c->width);
195
612
                if ((offset & 0x3F) == 0)
196
7
                    dst += c->width * 3;
197
612
                dst += 4;
198
612
            } while (offset < j);
199
6
        } while (--i);
200
    }
201
202
6
    dst  = c->frame[c->current_frame];
203
6
    dend = c->frame[c->current_frame] + c->frame_size;
204
    do {
205
18432
        set_src_position(c, &src, &send);
206
18432
        if ((src + 3 * c->width + 4 > send) ||
207

36864
            (dst + 3 * c->width + 4 > dend) ||
208
18432
            bytestream2_get_bytes_left(&c->gb) < 4)
209
            return AVERROR_INVALIDDATA;
210
18432
        copy_block4(dst, src, c->width, c->width, 4);
211
18432
        i++;
212
18432
        if ((i & 0x3F) == 0)
213
288
            dst += c->width * 3;
214
18432
        dst += 4;
215
18432
    } while (i < c->video_size / 16);
216
217
6
    opcode_size = bytestream2_get_le16(&c->gb);
218
6
    bytestream2_skip(&c->gb, 2);
219
220
6
    if (bytestream2_get_bytes_left(&c->gb) < opcode_size)
221
        return AVERROR_INVALIDDATA;
222
223
6
    opcodes = pkt + bytestream2_tell(&c->gb);
224
6
    bytestream2_skipu(&c->gb, opcode_size);
225
226
6
    dst = c->frame[c->current_frame];
227
228
294
    for (i = 0; i < c->height; i += 4, dst += c->width * 3)
229
18720
        for (j = 0; j < c->width; j += 4, dst += 4) {
230
18432
            int opcode, k = 0;
231
18432
            if (op > opcode_size)
232
                return AVERROR_INVALIDDATA;
233
18432
            if (j & 4) {
234
9216
                opcode = opcodes[op] & 15;
235
9216
                op++;
236
            } else {
237
9216
                opcode = opcodes[op] >> 4;
238
            }
239
240
18432
            while (block_sequences[opcode][k]) {
241
19815
                offset = c->width * 2;
242
19815
                code   = block_sequences[opcode][k++];
243
244

19815
                switch (code) {
245
4435
                case 2:
246
4435
                    offset = 0;
247
5835
                case 3:
248
5835
                    color = bytestream2_get_byte(&c->gb);
249
9005
                case 4:
250
9005
                    mask = bytestream2_get_byte(&c->gb);
251
9005
                    copy_color_mask(dst + offset, c->width, mask, color);
252
9005
                    break;
253
5422
                case 5:
254
5422
                    offset = 0;
255
6446
                case 6:
256
6446
                    set_src_position(c, &src, &send);
257
10810
                case 7:
258
10810
                    if (src + offset + c->width + 4 > send)
259
                        return AVERROR_INVALIDDATA;
260
10810
                    mask = bytestream2_get_byte(&c->gb);
261
10810
                    copy_src_mask(dst + offset, c->width, mask, src + offset);
262
10810
                    break;
263
                }
264
38247
            }
265
        }
266
267
6
    return 0;
268
}
269
270
152
static int paf_video_decode(AVCodecContext *avctx, void *data,
271
                            int *got_frame, AVPacket *pkt)
272
{
273
152
    PAFVideoDecContext *c = avctx->priv_data;
274
    uint8_t code, *dst, *end;
275
    int i, frame, ret;
276
277
152
    if (pkt->size < 2)
278
        return AVERROR_INVALIDDATA;
279
280
152
    bytestream2_init(&c->gb, pkt->data, pkt->size);
281
282
152
    code = bytestream2_get_byte(&c->gb);
283

152
    if ((code & 0xF) > 4 || (code & 0xF) == 3) {
284
        avpriv_request_sample(avctx, "unknown/invalid code");
285
        return AVERROR_INVALIDDATA;
286
    }
287
288
152
    if ((code & 0xF) == 0 &&
289
6
        c->video_size / 32 - (int64_t)bytestream2_get_bytes_left(&c->gb) > c->video_size / 32 * (int64_t)avctx->discard_damaged_percentage / 100)
290
        return AVERROR_INVALIDDATA;
291
292
152
    if ((ret = ff_reget_buffer(avctx, c->pic, 0)) < 0)
293
        return ret;
294
295
152
    if (code & 0x20) {  // frame is keyframe
296
2
        memset(c->pic->data[1], 0, AVPALETTE_SIZE);
297
2
        c->current_frame  = 0;
298
2
        c->pic->key_frame = 1;
299
2
        c->pic->pict_type = AV_PICTURE_TYPE_I;
300
    } else {
301
150
        c->pic->key_frame = 0;
302
150
        c->pic->pict_type = AV_PICTURE_TYPE_P;
303
    }
304
305
152
    if (code & 0x40) {  // palette update
306
2
        uint32_t *out = (uint32_t *)c->pic->data[1];
307
        int index, count;
308
309
2
        index = bytestream2_get_byte(&c->gb);
310
2
        count = bytestream2_get_byte(&c->gb) + 1;
311
312
2
        if (index + count > 256)
313
            return AVERROR_INVALIDDATA;
314
2
        if (bytestream2_get_bytes_left(&c->gb) < 3 * count)
315
            return AVERROR_INVALIDDATA;
316
317
2
        out += index;
318
514
        for (i = 0; i < count; i++) {
319
            unsigned r, g, b;
320
321
512
            r = bytestream2_get_byteu(&c->gb);
322
512
            r = r << 2 | r >> 4;
323
512
            g = bytestream2_get_byteu(&c->gb);
324
512
            g = g << 2 | g >> 4;
325
512
            b = bytestream2_get_byteu(&c->gb);
326
512
            b = b << 2 | b >> 4;
327
512
            *out++ = (0xFFU << 24) | (r << 16) | (g << 8) | b;
328
        }
329
2
        c->pic->palette_has_changed = 1;
330
    }
331
332
152
    c->dirty[c->current_frame] = 1;
333
152
    if (code & 0x20)
334
10
        for (i = 0; i < 4; i++) {
335
8
            if (c->dirty[i])
336
5
                memset(c->frame[i], 0, c->frame_size);
337
8
            c->dirty[i] = 0;
338
        }
339
340

152
    switch (code & 0x0F) {
341
6
    case 0:
342
        /* Block-based motion compensation using 4x4 blocks with either
343
         * horizontal or vertical vectors; might incorporate VQ as well. */
344
6
        if ((ret = decode_0(c, pkt->data, code)) < 0)
345
            return ret;
346
6
        break;
347
    case 1:
348
        /* Uncompressed data. This mode specifies that (width * height) bytes
349
         * should be copied directly from the encoded buffer into the output. */
350
        dst = c->frame[c->current_frame];
351
        // possibly chunk length data
352
        bytestream2_skip(&c->gb, 2);
353
        if (bytestream2_get_bytes_left(&c->gb) < c->video_size)
354
            return AVERROR_INVALIDDATA;
355
        bytestream2_get_bufferu(&c->gb, dst, c->video_size);
356
        break;
357
28
    case 2:
358
        /* Copy reference frame: Consume the next byte in the stream as the
359
         * reference frame (which should be 0, 1, 2, or 3, and should not be
360
         * the same as the current frame number). */
361
28
        frame = bytestream2_get_byte(&c->gb);
362
28
        if (frame > 3)
363
            return AVERROR_INVALIDDATA;
364
28
        if (frame != c->current_frame)
365
7
            memcpy(c->frame[c->current_frame], c->frame[frame], c->frame_size);
366
28
        break;
367
118
    case 4:
368
        /* Run length encoding.*/
369
118
        dst = c->frame[c->current_frame];
370
118
        end = dst + c->video_size;
371
372
118
        bytestream2_skip(&c->gb, 2);
373
374
175341
        while (dst < end) {
375
            int8_t code;
376
            int count;
377
378
175223
            if (bytestream2_get_bytes_left(&c->gb) < 2)
379
                return AVERROR_INVALIDDATA;
380
381
175223
            code  = bytestream2_get_byteu(&c->gb);
382
175223
            count = FFABS(code) + 1;
383
384
175223
            if (dst + count > end)
385
                return AVERROR_INVALIDDATA;
386
175223
            if (code < 0)
387
116135
                memset(dst, bytestream2_get_byteu(&c->gb), count);
388
            else
389
59088
                bytestream2_get_buffer(&c->gb, dst, count);
390
175223
            dst += count;
391
        }
392
118
        break;
393
    default:
394
        av_assert0(0);
395
    }
396
397
152
    av_image_copy_plane(c->pic->data[0], c->pic->linesize[0],
398
152
                        c->frame[c->current_frame], c->width,
399
                        c->width, c->height);
400
401
152
    c->current_frame = (c->current_frame + 1) & 3;
402
152
    if ((ret = av_frame_ref(data, c->pic)) < 0)
403
        return ret;
404
405
152
    *got_frame = 1;
406
407
152
    return pkt->size;
408
}
409
410
AVCodec ff_paf_video_decoder = {
411
    .name           = "paf_video",
412
    .long_name      = NULL_IF_CONFIG_SMALL("Amazing Studio Packed Animation File Video"),
413
    .type           = AVMEDIA_TYPE_VIDEO,
414
    .id             = AV_CODEC_ID_PAF_VIDEO,
415
    .priv_data_size = sizeof(PAFVideoDecContext),
416
    .init           = paf_video_init,
417
    .close          = paf_video_close,
418
    .decode         = paf_video_decode,
419
    .capabilities   = AV_CODEC_CAP_DR1,
420
    .caps_internal  = FF_CODEC_CAP_INIT_THREADSAFE | FF_CODEC_CAP_INIT_CLEANUP,
421
};