Coverage Report

Created: 2026-01-16 07:48

next uncovered line (L), next uncovered region (R), next uncovered branch (B)
/src/ffmpeg/libavcodec/pafvideo.c
Line
Count
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
#include "libavutil/mem.h"
24
25
#include "avcodec.h"
26
#include "bytestream.h"
27
#include "copy_block.h"
28
#include "codec_internal.h"
29
#include "decode.h"
30
31
32
static const uint8_t block_sequences[16][8] = {
33
    { 0, 0, 0, 0, 0, 0, 0, 0 },
34
    { 2, 0, 0, 0, 0, 0, 0, 0 },
35
    { 5, 7, 0, 0, 0, 0, 0, 0 },
36
    { 5, 0, 0, 0, 0, 0, 0, 0 },
37
    { 6, 0, 0, 0, 0, 0, 0, 0 },
38
    { 5, 7, 5, 7, 0, 0, 0, 0 },
39
    { 5, 7, 5, 0, 0, 0, 0, 0 },
40
    { 5, 7, 6, 0, 0, 0, 0, 0 },
41
    { 5, 5, 0, 0, 0, 0, 0, 0 },
42
    { 3, 0, 0, 0, 0, 0, 0, 0 },
43
    { 6, 6, 0, 0, 0, 0, 0, 0 },
44
    { 2, 4, 0, 0, 0, 0, 0, 0 },
45
    { 2, 4, 5, 7, 0, 0, 0, 0 },
46
    { 2, 4, 5, 0, 0, 0, 0, 0 },
47
    { 2, 4, 6, 0, 0, 0, 0, 0 },
48
    { 2, 4, 5, 7, 5, 7, 0, 0 },
49
};
50
51
typedef struct PAFVideoDecContext {
52
    AVFrame  *pic;
53
    GetByteContext gb;
54
55
    int width;
56
    int height;
57
58
    int current_frame;
59
    uint8_t *frame[4];
60
    int dirty[4];
61
    int frame_size;
62
    int video_size;
63
64
    uint8_t *opcodes;
65
} PAFVideoDecContext;
66
67
static av_cold int paf_video_close(AVCodecContext *avctx)
68
795
{
69
795
    PAFVideoDecContext *c = avctx->priv_data;
70
795
    int i;
71
72
795
    av_frame_free(&c->pic);
73
74
3.97k
    for (i = 0; i < 4; i++)
75
3.18k
        av_freep(&c->frame[i]);
76
77
795
    return 0;
78
795
}
79
80
static av_cold int paf_video_init(AVCodecContext *avctx)
81
795
{
82
795
    PAFVideoDecContext *c = avctx->priv_data;
83
795
    int i;
84
795
    int ret;
85
86
795
    c->width  = avctx->width;
87
795
    c->height = avctx->height;
88
89
795
    if (avctx->height & 3 || avctx->width & 3) {
90
13
        av_log(avctx, AV_LOG_ERROR,
91
13
               "width %d and height %d must be multiplie of 4.\n",
92
13
               avctx->width, avctx->height);
93
13
        return AVERROR_INVALIDDATA;
94
13
    }
95
96
782
    avctx->pix_fmt = AV_PIX_FMT_PAL8;
97
782
    ret = av_image_check_size2(avctx->width, FFALIGN(avctx->height, 256), avctx->max_pixels, avctx->pix_fmt, 0, avctx);
98
782
    if (ret < 0)
99
152
        return ret;
100
101
630
    c->pic = av_frame_alloc();
102
630
    if (!c->pic)
103
0
        return AVERROR(ENOMEM);
104
105
630
    c->frame_size = avctx->width * FFALIGN(avctx->height, 256);
106
630
    c->video_size = avctx->width * avctx->height;
107
3.15k
    for (i = 0; i < 4; i++) {
108
2.52k
        c->frame[i] = av_mallocz(c->frame_size);
109
2.52k
        if (!c->frame[i])
110
0
            return AVERROR(ENOMEM);
111
2.52k
    }
112
113
630
    return 0;
114
630
}
115
116
static void read4x4block(PAFVideoDecContext *c, uint8_t *dst, int width)
117
78.3k
{
118
78.3k
    int i;
119
120
391k
    for (i = 0; i < 4; i++) {
121
313k
        bytestream2_get_buffer(&c->gb, dst, 4);
122
313k
        dst += width;
123
313k
    }
124
78.3k
}
125
126
static void copy_color_mask(uint8_t *dst, int width, uint8_t mask, uint8_t color)
127
135k
{
128
135k
    int i;
129
130
676k
    for (i = 0; i < 4; i++) {
131
541k
        if (mask & (1 << 7 - i))
132
99.7k
            dst[i] = color;
133
541k
        if (mask & (1 << 3 - i))
134
100k
            dst[width + i] = color;
135
541k
    }
136
135k
}
137
138
static void copy_src_mask(uint8_t *dst, int width, uint8_t mask, const uint8_t *src)
139
276k
{
140
276k
    int i;
141
142
1.38M
    for (i = 0; i < 4; i++) {
143
1.10M
        if (mask & (1 << 7 - i))
144
232k
            dst[i] = src[i];
145
1.10M
        if (mask & (1 << 3 - i))
146
235k
            dst[width + i] = src[width + i];
147
1.10M
    }
148
276k
}
149
150
static void set_src_position(PAFVideoDecContext *c,
151
                             const uint8_t **p,
152
                             const uint8_t **pend)
153
1.16M
{
154
1.16M
    int val  = bytestream2_get_be16(&c->gb);
155
1.16M
    int page = val >> 14;
156
1.16M
    int x    = (val & 0x7F);
157
1.16M
    int y    = ((val >> 7) & 0x7F);
158
159
1.16M
    *p    = c->frame[page] + x * 2 + y * 2 * c->width;
160
1.16M
    *pend = c->frame[page] + c->frame_size;
161
1.16M
}
162
163
static int decode_0(PAFVideoDecContext *c, const uint8_t *pkt, uint8_t code)
164
5.46k
{
165
5.46k
    uint32_t opcode_size, offset;
166
5.46k
    uint8_t *dst, *dend, mask = 0, color = 0;
167
5.46k
    const uint8_t *src, *send, *opcodes;
168
5.46k
    int i, j, op = 0;
169
170
5.46k
    i = bytestream2_get_byte(&c->gb);
171
5.46k
    if (i) {
172
3.18k
        if (code & 0x10) {
173
1.92k
            int align;
174
175
1.92k
            align = bytestream2_tell(&c->gb) & 3;
176
1.92k
            if (align)
177
1.67k
                bytestream2_skip(&c->gb, 4 - align);
178
1.92k
        }
179
78.1k
        do {
180
78.1k
            int page, val, x, y;
181
78.1k
            val    = bytestream2_get_be16(&c->gb);
182
78.1k
            page   = val >> 14;
183
78.1k
            x      = (val & 0x7F) * 2;
184
78.1k
            y      = ((val >> 7) & 0x7F) * 2;
185
78.1k
            dst    = c->frame[page] + x + y * c->width;
186
78.1k
            dend   = c->frame[page] + c->frame_size;
187
78.1k
            offset = (x & 0x7F) * 2;
188
78.1k
            j      = bytestream2_get_le16(&c->gb) + offset;
189
78.1k
            if (bytestream2_get_bytes_left(&c->gb) < (j - offset) * 16)
190
1.04k
                return AVERROR_INVALIDDATA;
191
77.1k
            c->dirty[page] = 1;
192
78.8k
            do {
193
78.8k
                offset++;
194
78.8k
                if (dst + 3 * c->width + 4 > dend)
195
513
                    return AVERROR_INVALIDDATA;
196
78.3k
                read4x4block(c, dst, c->width);
197
78.3k
                if ((offset & 0x3F) == 0)
198
219
                    dst += c->width * 3;
199
78.3k
                dst += 4;
200
78.3k
            } while (offset < j);
201
77.1k
        } while (--i);
202
3.18k
    }
203
204
3.90k
    dst  = c->frame[c->current_frame];
205
3.90k
    dend = c->frame[c->current_frame] + c->frame_size;
206
974k
    do {
207
974k
        set_src_position(c, &src, &send);
208
974k
        if ((src + 3 * c->width + 4 > send) ||
209
974k
            (dst + 3 * c->width + 4 > dend) ||
210
974k
            bytestream2_get_bytes_left(&c->gb) < 4)
211
2.46k
            return AVERROR_INVALIDDATA;
212
972k
        copy_block4(dst, src, c->width, c->width, 4);
213
972k
        i++;
214
972k
        if ((i & 0x3F) == 0)
215
15.0k
            dst += c->width * 3;
216
972k
        dst += 4;
217
972k
    } while (i < c->video_size / 16);
218
219
1.44k
    opcode_size = bytestream2_get_le16(&c->gb);
220
1.44k
    bytestream2_skip(&c->gb, 2);
221
222
1.44k
    if (bytestream2_get_bytes_left(&c->gb) < opcode_size)
223
469
        return AVERROR_INVALIDDATA;
224
225
977
    opcodes = pkt + bytestream2_tell(&c->gb);
226
977
    bytestream2_skipu(&c->gb, opcode_size);
227
228
977
    dst = c->frame[c->current_frame];
229
230
39.8k
    for (i = 0; i < c->height; i += 4, dst += c->width * 3)
231
248k
        for (j = 0; j < c->width; j += 4, dst += 4) {
232
210k
            int opcode, k = 0;
233
210k
            if (op > opcode_size)
234
284
                return AVERROR_INVALIDDATA;
235
209k
            if (j & 4) {
236
86.8k
                opcode = opcodes[op] & 15;
237
86.8k
                op++;
238
122k
            } else {
239
122k
                opcode = opcodes[op] >> 4;
240
122k
            }
241
242
621k
            while (block_sequences[opcode][k]) {
243
412k
                offset = c->width * 2;
244
412k
                code   = block_sequences[opcode][k++];
245
246
412k
                switch (code) {
247
71.4k
                case 2:
248
71.4k
                    offset = 0;
249
81.6k
                case 3:
250
81.6k
                    color = bytestream2_get_byte(&c->gb);
251
135k
                case 4:
252
135k
                    mask = bytestream2_get_byte(&c->gb);
253
135k
                    copy_color_mask(dst + offset, c->width, mask, color);
254
135k
                    break;
255
145k
                case 5:
256
145k
                    offset = 0;
257
189k
                case 6:
258
189k
                    set_src_position(c, &src, &send);
259
276k
                case 7:
260
276k
                    if (src + offset + c->width + 4 > send)
261
210
                        return AVERROR_INVALIDDATA;
262
276k
                    mask = bytestream2_get_byte(&c->gb);
263
276k
                    copy_src_mask(dst + offset, c->width, mask, src + offset);
264
276k
                    break;
265
412k
                }
266
412k
            }
267
209k
        }
268
269
483
    return 0;
270
977
}
271
272
static int paf_video_decode(AVCodecContext *avctx, AVFrame *rframe,
273
                            int *got_frame, AVPacket *pkt)
274
236k
{
275
236k
    PAFVideoDecContext *c = avctx->priv_data;
276
236k
    uint8_t code, *dst, *end;
277
236k
    int i, frame, ret;
278
279
236k
    if (pkt->size < 2)
280
35.4k
        return AVERROR_INVALIDDATA;
281
282
200k
    bytestream2_init(&c->gb, pkt->data, pkt->size);
283
284
200k
    code = bytestream2_get_byte(&c->gb);
285
200k
    if ((code & 0xF) > 4 || (code & 0xF) == 3) {
286
9.15k
        avpriv_request_sample(avctx, "unknown/invalid code");
287
9.15k
        return AVERROR_INVALIDDATA;
288
9.15k
    }
289
290
191k
    if ((code & 0xF) == 0 &&
291
8.78k
        c->video_size / 32 - (int64_t)bytestream2_get_bytes_left(&c->gb) > c->video_size / 32 * (int64_t)avctx->discard_damaged_percentage / 100)
292
3.26k
        return AVERROR_INVALIDDATA;
293
294
188k
    if ((ret = ff_reget_buffer(avctx, c->pic, 0)) < 0)
295
538
        return ret;
296
297
187k
    if (code & 0x20) {  // frame is keyframe
298
5.01k
        memset(c->pic->data[1], 0, AVPALETTE_SIZE);
299
5.01k
        c->current_frame  = 0;
300
5.01k
        c->pic->flags |= AV_FRAME_FLAG_KEY;
301
5.01k
        c->pic->pict_type = AV_PICTURE_TYPE_I;
302
182k
    } else {
303
182k
        c->pic->flags &= ~AV_FRAME_FLAG_KEY;
304
182k
        c->pic->pict_type = AV_PICTURE_TYPE_P;
305
182k
    }
306
307
187k
    if (code & 0x40) {  // palette update
308
3.29k
        uint32_t *out = (uint32_t *)c->pic->data[1];
309
3.29k
        int index, count;
310
311
3.29k
        index = bytestream2_get_byte(&c->gb);
312
3.29k
        count = bytestream2_get_byte(&c->gb) + 1;
313
314
3.29k
        if (index + count > 256)
315
332
            return AVERROR_INVALIDDATA;
316
2.96k
        if (bytestream2_get_bytes_left(&c->gb) < 3 * count)
317
1.36k
            return AVERROR_INVALIDDATA;
318
319
1.59k
        out += index;
320
7.50k
        for (i = 0; i < count; i++) {
321
5.90k
            unsigned r, g, b;
322
323
5.90k
            r = bytestream2_get_byteu(&c->gb);
324
5.90k
            r = r << 2 | r >> 4;
325
5.90k
            g = bytestream2_get_byteu(&c->gb);
326
5.90k
            g = g << 2 | g >> 4;
327
5.90k
            b = bytestream2_get_byteu(&c->gb);
328
5.90k
            b = b << 2 | b >> 4;
329
5.90k
            *out++ = (0xFFU << 24) | (r << 16) | (g << 8) | b;
330
5.90k
        }
331
1.59k
    }
332
333
185k
    c->dirty[c->current_frame] = 1;
334
185k
    if (code & 0x20)
335
19.4k
        for (i = 0; i < 4; i++) {
336
15.5k
            if (c->dirty[i])
337
5.00k
                memset(c->frame[i], 0, c->frame_size);
338
15.5k
            c->dirty[i] = 0;
339
15.5k
        }
340
341
185k
    switch (code & 0x0F) {
342
5.46k
    case 0:
343
        /* Block-based motion compensation using 4x4 blocks with either
344
         * horizontal or vertical vectors; might incorporate VQ as well. */
345
5.46k
        if ((ret = decode_0(c, pkt->data, code)) < 0)
346
4.98k
            return ret;
347
483
        break;
348
2.18k
    case 1:
349
        /* Uncompressed data. This mode specifies that (width * height) bytes
350
         * should be copied directly from the encoded buffer into the output. */
351
2.18k
        dst = c->frame[c->current_frame];
352
        // possibly chunk length data
353
2.18k
        bytestream2_skip(&c->gb, 2);
354
2.18k
        if (bytestream2_get_bytes_left(&c->gb) < c->video_size)
355
1.95k
            return AVERROR_INVALIDDATA;
356
230
        bytestream2_get_bufferu(&c->gb, dst, c->video_size);
357
230
        break;
358
176k
    case 2:
359
        /* Copy reference frame: Consume the next byte in the stream as the
360
         * reference frame (which should be 0, 1, 2, or 3, and should not be
361
         * the same as the current frame number). */
362
176k
        frame = bytestream2_get_byte(&c->gb);
363
176k
        if (frame > 3)
364
58.3k
            return AVERROR_INVALIDDATA;
365
118k
        if (frame != c->current_frame)
366
87.9k
            memcpy(c->frame[c->current_frame], c->frame[frame], c->frame_size);
367
118k
        break;
368
1.96k
    case 4:
369
        /* Run length encoding.*/
370
1.96k
        dst = c->frame[c->current_frame];
371
1.96k
        end = dst + c->video_size;
372
373
1.96k
        bytestream2_skip(&c->gb, 2);
374
375
414k
        while (dst < end) {
376
414k
            int8_t code;
377
414k
            int count;
378
379
414k
            if (bytestream2_get_bytes_left(&c->gb) < 2)
380
1.38k
                return AVERROR_INVALIDDATA;
381
382
412k
            code  = bytestream2_get_byteu(&c->gb);
383
412k
            count = FFABS(code) + 1;
384
385
412k
            if (dst + count > end)
386
352
                return AVERROR_INVALIDDATA;
387
412k
            if (code < 0)
388
108k
                memset(dst, bytestream2_get_byteu(&c->gb), count);
389
304k
            else
390
304k
                bytestream2_get_buffer(&c->gb, dst, count);
391
412k
            dst += count;
392
412k
        }
393
223
        break;
394
223
    default:
395
0
        av_assert0(0);
396
185k
    }
397
398
118k
    av_image_copy_plane(c->pic->data[0], c->pic->linesize[0],
399
118k
                        c->frame[c->current_frame], c->width,
400
118k
                        c->width, c->height);
401
402
118k
    c->current_frame = (c->current_frame + 1) & 3;
403
118k
    if ((ret = av_frame_ref(rframe, c->pic)) < 0)
404
0
        return ret;
405
406
118k
    *got_frame = 1;
407
408
118k
    return pkt->size;
409
118k
}
410
411
const FFCodec ff_paf_video_decoder = {
412
    .p.name         = "paf_video",
413
    CODEC_LONG_NAME("Amazing Studio Packed Animation File Video"),
414
    .p.type         = AVMEDIA_TYPE_VIDEO,
415
    .p.id           = AV_CODEC_ID_PAF_VIDEO,
416
    .priv_data_size = sizeof(PAFVideoDecContext),
417
    .init           = paf_video_init,
418
    .close          = paf_video_close,
419
    FF_CODEC_DECODE_CB(paf_video_decode),
420
    .p.capabilities = AV_CODEC_CAP_DR1,
421
    .caps_internal  = FF_CODEC_CAP_INIT_CLEANUP,
422
};