Coverage Report

Created: 2025-12-31 07:57

next uncovered line (L), next uncovered region (R), next uncovered branch (B)
/src/ffmpeg/libavcodec/anm.c
Line
Count
Source
1
/*
2
 * Deluxe Paint Animation decoder
3
 * Copyright (c) 2009 Peter Ross
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
/**
23
 * @file
24
 * Deluxe Paint Animation decoder
25
 */
26
27
#include "avcodec.h"
28
#include "bytestream.h"
29
#include "codec_internal.h"
30
#include "decode.h"
31
32
typedef struct AnmContext {
33
    AVFrame *frame;
34
    int palette[AVPALETTE_COUNT];
35
} AnmContext;
36
37
static av_cold int decode_init(AVCodecContext *avctx)
38
595
{
39
595
    AnmContext *s = avctx->priv_data;
40
595
    GetByteContext gb;
41
595
    int i;
42
43
595
    if (avctx->extradata_size < 16 * 8 + 4 * 256)
44
205
        return AVERROR_INVALIDDATA;
45
46
390
    avctx->pix_fmt = AV_PIX_FMT_PAL8;
47
48
390
    s->frame = av_frame_alloc();
49
390
    if (!s->frame)
50
0
        return AVERROR(ENOMEM);
51
52
390
    bytestream2_init(&gb, avctx->extradata, avctx->extradata_size);
53
390
    bytestream2_skipu(&gb, 16 * 8);
54
100k
    for (i = 0; i < 256; i++)
55
99.8k
        s->palette[i] = (0xFFU << 24) | bytestream2_get_le32u(&gb);
56
57
390
    return 0;
58
390
}
59
60
/**
61
 * Perform decode operation
62
 * @param dst     pointer to destination image buffer
63
 * @param dst_end pointer to end of destination image buffer
64
 * @param gb GetByteContext (optional, see below)
65
 * @param pixel Fill color (optional, see below)
66
 * @param count Pixel count
67
 * @param x Pointer to x-axis counter
68
 * @param width Image width
69
 * @param linesize Destination image buffer linesize
70
 * @return non-zero if destination buffer is exhausted
71
 *
72
 * a copy operation is achieved when 'gb' is set
73
 * a fill operation is achieved when 'gb' is null and pixel is >= 0
74
 * a skip operation is achieved when 'gb' is null and pixel is < 0
75
 */
76
static inline int op(uint8_t **dst, const uint8_t *dst_end,
77
                     GetByteContext *gb,
78
                     int pixel, int count,
79
                     int *x, int width, int linesize)
80
1.00M
{
81
1.00M
    int remaining = width - *x;
82
3.38M
    while(count > 0) {
83
2.49M
        int striplen = FFMIN(count, remaining);
84
2.49M
        if (gb) {
85
155k
            if (bytestream2_get_bytes_left(gb) < striplen)
86
83.5k
                goto exhausted;
87
72.0k
            bytestream2_get_bufferu(gb, *dst, striplen);
88
2.33M
        } else if (pixel >= 0)
89
1.01M
            memset(*dst, pixel, striplen);
90
2.41M
        *dst      += striplen;
91
2.41M
        remaining -= striplen;
92
2.41M
        count     -= striplen;
93
2.41M
        if (remaining <= 0) {
94
1.65M
            *dst      += linesize - width;
95
1.65M
            remaining  = width;
96
1.65M
        }
97
2.41M
        if (linesize > 0) {
98
2.41M
            if (*dst >= dst_end) goto exhausted;
99
2.41M
        } else {
100
0
            if (*dst <= dst_end) goto exhausted;
101
0
        }
102
2.41M
    }
103
890k
    *x = width - remaining;
104
890k
    return 0;
105
106
113k
exhausted:
107
113k
    *x = width - remaining;
108
113k
    return 1;
109
1.00M
}
110
111
static int decode_frame(AVCodecContext *avctx, AVFrame *rframe,
112
                        int *got_frame, AVPacket *avpkt)
113
197k
{
114
197k
    AnmContext *s = avctx->priv_data;
115
197k
    const int buf_size = avpkt->size;
116
197k
    uint8_t *dst, *dst_end;
117
197k
    GetByteContext gb;
118
197k
    int count, ret, x = 0;
119
120
197k
    if (buf_size < 7)
121
4.84k
        return AVERROR_INVALIDDATA;
122
123
192k
    if ((ret = ff_reget_buffer(avctx, s->frame, 0)) < 0)
124
19.8k
        return ret;
125
172k
    dst     = s->frame->data[0];
126
172k
    dst_end = s->frame->data[0] + s->frame->linesize[0]*avctx->height;
127
128
172k
    bytestream2_init(&gb, avpkt->data, buf_size);
129
130
172k
    if (bytestream2_get_byte(&gb) != 0x42) {
131
1.60k
        avpriv_request_sample(avctx, "Unknown record type");
132
1.60k
        return AVERROR_INVALIDDATA;
133
1.60k
    }
134
171k
    if (bytestream2_get_byte(&gb)) {
135
1.33k
        avpriv_request_sample(avctx, "Padding bytes");
136
1.33k
        return AVERROR_PATCHWELCOME;
137
1.33k
    }
138
169k
    bytestream2_skip(&gb, 2);
139
140
1.00M
    do {
141
        /* if statements are ordered by probability */
142
1.00M
#define OP(gb, pixel, count) \
143
1.58M
    op(&dst, dst_end, (gb), (pixel), (count), &x, avctx->width, s->frame->linesize[0])
144
145
1.00M
        int type = bytestream2_get_byte(&gb);
146
1.00M
        count = type & 0x7F;
147
1.00M
        type >>= 7;
148
1.00M
        if (count) {
149
762k
            if (OP(type ? NULL : &gb, -1, count)) break;
150
762k
        } else if (!type) {
151
209k
            int pixel;
152
209k
            count = bytestream2_get_byte(&gb);  /* count==0 gives nop */
153
209k
            pixel = bytestream2_get_byte(&gb);
154
209k
            if (OP(NULL, pixel, count)) break;
155
209k
        } else {
156
33.8k
            int pixel;
157
33.8k
            type = bytestream2_get_le16(&gb);
158
33.8k
            count = type & 0x3FFF;
159
33.8k
            type >>= 14;
160
33.8k
            if (!count) {
161
1.65k
                if (type == 0)
162
315
                    break; // stop
163
1.34k
                if (type == 2) {
164
365
                    avpriv_request_sample(avctx, "Unknown opcode");
165
365
                    return AVERROR_PATCHWELCOME;
166
365
                }
167
978
                continue;
168
1.34k
            }
169
32.2k
            pixel = type == 3 ? bytestream2_get_byte(&gb) : -1;
170
32.2k
            if (type == 1) count += 0x4000;
171
32.2k
            if (OP(type == 2 ? &gb : NULL, pixel, count)) break;
172
32.2k
        }
173
1.00M
    } while (bytestream2_get_bytes_left(&gb) > 0);
174
175
169k
    memcpy(s->frame->data[1], s->palette, AVPALETTE_SIZE);
176
177
169k
    *got_frame = 1;
178
169k
    if ((ret = av_frame_ref(rframe, s->frame)) < 0)
179
0
        return ret;
180
181
169k
    return buf_size;
182
169k
}
183
184
static av_cold int decode_end(AVCodecContext *avctx)
185
390
{
186
390
    AnmContext *s = avctx->priv_data;
187
188
390
    av_frame_free(&s->frame);
189
390
    return 0;
190
390
}
191
192
const FFCodec ff_anm_decoder = {
193
    .p.name         = "anm",
194
    CODEC_LONG_NAME("Deluxe Paint Animation"),
195
    .p.type         = AVMEDIA_TYPE_VIDEO,
196
    .p.id           = AV_CODEC_ID_ANM,
197
    .priv_data_size = sizeof(AnmContext),
198
    .init           = decode_init,
199
    .close          = decode_end,
200
    FF_CODEC_DECODE_CB(decode_frame),
201
    .p.capabilities = AV_CODEC_CAP_DR1,
202
};