GCC Code Coverage Report
Directory: ../../../ffmpeg/ Exec Total Coverage
File: src/libavcodec/avs.c Lines: 80 91 87.9 %
Date: 2019-11-22 03:34:36 Branches: 35 47 74.5 %

Line Branch Exec 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 "get_bits.h"
24
#include "internal.h"
25
26
typedef struct AvsContext {
27
    AVFrame *frame;
28
} AvsContext;
29
30
typedef enum {
31
    AVS_VIDEO     = 0x01,
32
    AVS_AUDIO     = 0x02,
33
    AVS_PALETTE   = 0x03,
34
    AVS_GAME_DATA = 0x04,
35
} AvsBlockType;
36
37
typedef enum {
38
    AVS_I_FRAME     = 0x00,
39
    AVS_P_FRAME_3X3 = 0x01,
40
    AVS_P_FRAME_2X2 = 0x02,
41
    AVS_P_FRAME_2X3 = 0x03,
42
} AvsVideoSubType;
43
44
45
static int
46
56
avs_decode_frame(AVCodecContext * avctx,
47
                 void *data, int *got_frame, AVPacket *avpkt)
48
{
49
56
    const uint8_t *buf = avpkt->data;
50
56
    const uint8_t *buf_end = avpkt->data + avpkt->size;
51
56
    int buf_size = avpkt->size;
52
56
    AvsContext *const avs = avctx->priv_data;
53
56
    AVFrame *picture = data;
54
56
    AVFrame *const p =  avs->frame;
55
    const uint8_t *table, *vect;
56
    uint8_t *out;
57
56
    int i, j, x, y, stride, ret, vect_w = 3, vect_h = 3;
58
    AvsVideoSubType sub_type;
59
    AvsBlockType type;
60
56
    GetBitContext change_map = {0}; //init to silence warning
61
62
56
    if ((ret = ff_reget_buffer(avctx, p, 0)) < 0)
63
        return ret;
64
56
    p->pict_type = AV_PICTURE_TYPE_P;
65
56
    p->key_frame = 0;
66
67
56
    out    = p->data[0];
68
56
    stride = p->linesize[0];
69
70
56
    if (buf_end - buf < 4)
71
        return AVERROR_INVALIDDATA;
72
56
    sub_type = buf[0];
73
56
    type = buf[1];
74
56
    buf += 4;
75
76
56
    if (type == AVS_PALETTE) {
77
        int first, last;
78
1
        uint32_t *pal = (uint32_t *) p->data[1];
79
80
1
        first = AV_RL16(buf);
81
1
        last = first + AV_RL16(buf + 2);
82

1
        if (first >= 256 || last > 256 || buf_end - buf < 4 + 4 + 3 * (last - first))
83
            return AVERROR_INVALIDDATA;
84
1
        buf += 4;
85
120
        for (i=first; i<last; i++, buf+=3) {
86
119
            pal[i] = (buf[0] << 18) | (buf[1] << 10) | (buf[2] << 2);
87
119
            pal[i] |= 0xFFU << 24 | (pal[i] >> 6) & 0x30303;
88
        }
89
90
1
        sub_type = buf[0];
91
1
        type = buf[1];
92
1
        buf += 4;
93
    }
94
95
56
    if (type != AVS_VIDEO)
96
        return AVERROR_INVALIDDATA;
97
98

56
    switch (sub_type) {
99
5
    case AVS_I_FRAME:
100
5
        p->pict_type = AV_PICTURE_TYPE_I;
101
5
        p->key_frame = 1;
102
14
    case AVS_P_FRAME_3X3:
103
14
        vect_w = 3;
104
14
        vect_h = 3;
105
14
        break;
106
107
5
    case AVS_P_FRAME_2X2:
108
5
        vect_w = 2;
109
5
        vect_h = 2;
110
5
        break;
111
112
37
    case AVS_P_FRAME_2X3:
113
37
        vect_w = 2;
114
37
        vect_h = 3;
115
37
        break;
116
117
    default:
118
      return AVERROR_INVALIDDATA;
119
    }
120
121
56
    if (buf_end - buf < 256 * vect_w * vect_h)
122
        return AVERROR_INVALIDDATA;
123
56
    table = buf + (256 * vect_w * vect_h);
124
56
    if (sub_type != AVS_I_FRAME) {
125
51
        int map_size = ((318 / vect_w + 7) / 8) * (198 / vect_h);
126
51
        if (buf_end - table < map_size)
127
            return AVERROR_INVALIDDATA;
128
51
        init_get_bits(&change_map, table, map_size * 8);
129
51
        table += map_size;
130
    }
131
132
3917
    for (y=0; y<198; y+=vect_h) {
133
568788
        for (x=0; x<318; x+=vect_w) {
134

564927
            if (sub_type == AVS_I_FRAME || get_bits1(&change_map)) {
135
271490
                if (buf_end - table < 1)
136
                    return AVERROR_INVALIDDATA;
137
271490
                vect = &buf[*table++ * (vect_w * vect_h)];
138
890084
                for (j=0; j<vect_w; j++) {
139
618594
                    out[(y + 0) * stride + x + j] = vect[(0 * vect_w) + j];
140
618594
                    out[(y + 1) * stride + x + j] = vect[(1 * vect_w) + j];
141
618594
                    if (vect_h == 3)
142
568114
                        out[(y + 2) * stride + x + j] =
143
568114
                            vect[(2 * vect_w) + j];
144
                }
145
            }
146
        }
147
3861
        if (sub_type != AVS_I_FRAME)
148
3531
            align_get_bits(&change_map);
149
    }
150
151
56
    if ((ret = av_frame_ref(picture, p)) < 0)
152
        return ret;
153
56
    *got_frame = 1;
154
155
56
    return buf_size;
156
}
157
158
2
static av_cold int avs_decode_init(AVCodecContext * avctx)
159
{
160
2
    AvsContext *s = avctx->priv_data;
161
162
2
    s->frame = av_frame_alloc();
163
2
    if (!s->frame)
164
        return AVERROR(ENOMEM);
165
166
2
    avctx->pix_fmt = AV_PIX_FMT_PAL8;
167
168
2
    return ff_set_dimensions(avctx, 318, 198);
169
}
170
171
2
static av_cold int avs_decode_end(AVCodecContext *avctx)
172
{
173
2
    AvsContext *s = avctx->priv_data;
174
2
    av_frame_free(&s->frame);
175
2
    return 0;
176
}
177
178
179
AVCodec ff_avs_decoder = {
180
    .name           = "avs",
181
    .long_name      = NULL_IF_CONFIG_SMALL("AVS (Audio Video Standard) video"),
182
    .type           = AVMEDIA_TYPE_VIDEO,
183
    .id             = AV_CODEC_ID_AVS,
184
    .priv_data_size = sizeof(AvsContext),
185
    .init           = avs_decode_init,
186
    .decode         = avs_decode_frame,
187
    .close          = avs_decode_end,
188
    .capabilities   = AV_CODEC_CAP_DR1,
189
    .caps_internal  = FF_CODEC_CAP_INIT_THREADSAFE,
190
};