Coverage Report

Created: 2026-07-25 07:52

next uncovered line (L), next uncovered region (R), next uncovered branch (B)
/src/ffmpeg/libavcodec/avs.c
Line
Count
Source
1
/*
2
 * AVS video decoder.
3
 * Copyright (c) 2006  Aurelien Jacobs <aurel@gnuage.org>
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 "avcodec.h"
23
#include "codec_internal.h"
24
#include "decode.h"
25
#include "get_bits.h"
26
#include "libavutil/attributes.h"
27
28
typedef struct AvsContext {
29
    AVFrame *frame;
30
} AvsContext;
31
32
typedef enum {
33
    AVS_VIDEO     = 0x01,
34
    AVS_AUDIO     = 0x02,
35
    AVS_PALETTE   = 0x03,
36
    AVS_GAME_DATA = 0x04,
37
} AvsBlockType;
38
39
typedef enum {
40
    AVS_I_FRAME     = 0x00,
41
    AVS_P_FRAME_3X3 = 0x01,
42
    AVS_P_FRAME_2X2 = 0x02,
43
    AVS_P_FRAME_2X3 = 0x03,
44
} AvsVideoSubType;
45
46
47
static int avs_decode_frame(AVCodecContext * avctx, AVFrame *picture,
48
                            int *got_frame, AVPacket *avpkt)
49
1.03M
{
50
1.03M
    const uint8_t *buf = avpkt->data;
51
1.03M
    const uint8_t *buf_end = avpkt->data + avpkt->size;
52
1.03M
    int buf_size = avpkt->size;
53
1.03M
    AvsContext *const avs = avctx->priv_data;
54
1.03M
    AVFrame *const p =  avs->frame;
55
1.03M
    const uint8_t *table, *vect;
56
1.03M
    uint8_t *out;
57
1.03M
    int i, j, x, y, stride, ret, vect_w = 3, vect_h = 3;
58
1.03M
    AvsVideoSubType sub_type;
59
1.03M
    AvsBlockType type;
60
1.03M
    GetBitContext change_map = {0}; //init to silence warning
61
62
1.03M
    if ((ret = ff_reget_buffer(avctx, p, 0)) < 0)
63
0
        return ret;
64
1.03M
    p->pict_type = AV_PICTURE_TYPE_P;
65
1.03M
    p->flags &= ~AV_FRAME_FLAG_KEY;
66
67
1.03M
    out    = p->data[0];
68
1.03M
    stride = p->linesize[0];
69
70
1.03M
    if (buf_end - buf < 4)
71
1.01M
        return AVERROR_INVALIDDATA;
72
17.7k
    sub_type = buf[0];
73
17.7k
    type = buf[1];
74
17.7k
    buf += 4;
75
76
17.7k
    if (type == AVS_PALETTE) {
77
1.42k
        int first, last;
78
1.42k
        uint32_t *pal = (uint32_t *) p->data[1];
79
80
1.42k
        first = AV_RL16(buf);
81
1.42k
        last = first + AV_RL16(buf + 2);
82
1.42k
        if (first >= 256 || last > 256 || buf_end - buf < 4 + 4 + 3 * (last - first))
83
723
            return AVERROR_INVALIDDATA;
84
698
        buf += 4;
85
2.30k
        for (i=first; i<last; i++, buf+=3) {
86
1.60k
            pal[i] = (buf[0] << 18) | (buf[1] << 10) | (buf[2] << 2);
87
1.60k
            pal[i] |= 0xFFU << 24 | (pal[i] >> 6) & 0x30303;
88
1.60k
        }
89
90
698
        sub_type = buf[0];
91
698
        type = buf[1];
92
698
        buf += 4;
93
698
    }
94
95
17.0k
    if (type != AVS_VIDEO)
96
8.65k
        return AVERROR_INVALIDDATA;
97
98
8.38k
    switch (sub_type) {
99
502
    case AVS_I_FRAME:
100
502
        p->pict_type = AV_PICTURE_TYPE_I;
101
502
        p->flags |= AV_FRAME_FLAG_KEY;
102
502
        av_fallthrough;
103
731
    case AVS_P_FRAME_3X3:
104
731
        vect_w = 3;
105
731
        vect_h = 3;
106
731
        break;
107
108
473
    case AVS_P_FRAME_2X2:
109
473
        vect_w = 2;
110
473
        vect_h = 2;
111
473
        break;
112
113
6.92k
    case AVS_P_FRAME_2X3:
114
6.92k
        vect_w = 2;
115
6.92k
        vect_h = 3;
116
6.92k
        break;
117
118
253
    default:
119
253
      return AVERROR_INVALIDDATA;
120
8.38k
    }
121
122
8.12k
    if (buf_end - buf < 256 * vect_w * vect_h)
123
1.18k
        return AVERROR_INVALIDDATA;
124
6.94k
    table = buf + (256 * vect_w * vect_h);
125
6.94k
    if (sub_type != AVS_I_FRAME) {
126
6.69k
        int map_size = ((318 / vect_w + 7) / 8) * (198 / vect_h);
127
6.69k
        if (buf_end - table < map_size)
128
220
            return AVERROR_INVALIDDATA;
129
6.47k
        init_get_bits(&change_map, table, map_size * 8);
130
6.47k
        table += map_size;
131
6.47k
    }
132
133
430k
    for (y=0; y<198; y+=vect_h) {
134
67.6M
        for (x=0; x<318; x+=vect_w) {
135
67.2M
            if (sub_type == AVS_I_FRAME || get_bits1(&change_map)) {
136
335k
                if (buf_end - table < 1)
137
376
                    return AVERROR_INVALIDDATA;
138
335k
                vect = &buf[*table++ * (vect_w * vect_h)];
139
1.16M
                for (j=0; j<vect_w; j++) {
140
833k
                    out[(y + 0) * stride + x + j] = vect[(0 * vect_w) + j];
141
833k
                    out[(y + 1) * stride + x + j] = vect[(1 * vect_w) + j];
142
833k
                    if (vect_h == 3)
143
735k
                        out[(y + 2) * stride + x + j] =
144
735k
                            vect[(2 * vect_w) + j];
145
833k
                }
146
335k
            }
147
67.2M
        }
148
423k
        if (sub_type != AVS_I_FRAME)
149
422k
            align_get_bits(&change_map);
150
423k
    }
151
152
6.35k
    if ((ret = av_frame_ref(picture, p)) < 0)
153
0
        return ret;
154
6.35k
    *got_frame = 1;
155
156
6.35k
    return buf_size;
157
6.35k
}
158
159
static av_cold int avs_decode_init(AVCodecContext * avctx)
160
1.00k
{
161
1.00k
    AvsContext *s = avctx->priv_data;
162
163
1.00k
    s->frame = av_frame_alloc();
164
1.00k
    if (!s->frame)
165
0
        return AVERROR(ENOMEM);
166
167
1.00k
    avctx->pix_fmt = AV_PIX_FMT_PAL8;
168
169
1.00k
    return ff_set_dimensions(avctx, 318, 198);
170
1.00k
}
171
172
static av_cold int avs_decode_end(AVCodecContext *avctx)
173
1.00k
{
174
1.00k
    AvsContext *s = avctx->priv_data;
175
1.00k
    av_frame_free(&s->frame);
176
1.00k
    return 0;
177
1.00k
}
178
179
180
const FFCodec ff_avs_decoder = {
181
    .p.name         = "avs",
182
    CODEC_LONG_NAME("AVS (Audio Video Standard) video"),
183
    .p.type         = AVMEDIA_TYPE_VIDEO,
184
    .p.id           = AV_CODEC_ID_AVS,
185
    .priv_data_size = sizeof(AvsContext),
186
    .init           = avs_decode_init,
187
    FF_CODEC_DECODE_CB(avs_decode_frame),
188
    .close          = avs_decode_end,
189
    .p.capabilities = AV_CODEC_CAP_DR1,
190
};