FFmpeg coverage


Directory: ../../../ffmpeg/
File: src/libavcodec/avs.c
Date: 2022-11-28 23:49:43
Exec Total Coverage
Lines: 79 90 87.8%
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 "codec_internal.h"
24 #include "decode.h"
25 #include "get_bits.h"
26
27 typedef struct AvsContext {
28 AVFrame *frame;
29 } AvsContext;
30
31 typedef enum {
32 AVS_VIDEO = 0x01,
33 AVS_AUDIO = 0x02,
34 AVS_PALETTE = 0x03,
35 AVS_GAME_DATA = 0x04,
36 } AvsBlockType;
37
38 typedef enum {
39 AVS_I_FRAME = 0x00,
40 AVS_P_FRAME_3X3 = 0x01,
41 AVS_P_FRAME_2X2 = 0x02,
42 AVS_P_FRAME_2X3 = 0x03,
43 } AvsVideoSubType;
44
45
46 56 static int avs_decode_frame(AVCodecContext * avctx, AVFrame *picture,
47 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 *const p = avs->frame;
54 const uint8_t *table, *vect;
55 uint8_t *out;
56 56 int i, j, x, y, stride, ret, vect_w = 3, vect_h = 3;
57 AvsVideoSubType sub_type;
58 AvsBlockType type;
59 56 GetBitContext change_map = {0}; //init to silence warning
60
61
1/2
✗ Branch 1 not taken.
✓ Branch 2 taken 56 times.
56 if ((ret = ff_reget_buffer(avctx, p, 0)) < 0)
62 return ret;
63 56 p->pict_type = AV_PICTURE_TYPE_P;
64 56 p->key_frame = 0;
65
66 56 out = p->data[0];
67 56 stride = p->linesize[0];
68
69
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 56 times.
56 if (buf_end - buf < 4)
70 return AVERROR_INVALIDDATA;
71 56 sub_type = buf[0];
72 56 type = buf[1];
73 56 buf += 4;
74
75
2/2
✓ Branch 0 taken 1 times.
✓ Branch 1 taken 55 times.
56 if (type == AVS_PALETTE) {
76 int first, last;
77 1 uint32_t *pal = (uint32_t *) p->data[1];
78
79 1 first = AV_RL16(buf);
80 1 last = first + AV_RL16(buf + 2);
81
3/6
✓ Branch 0 taken 1 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 1 times.
✗ Branch 3 not taken.
✗ Branch 4 not taken.
✓ Branch 5 taken 1 times.
1 if (first >= 256 || last > 256 || buf_end - buf < 4 + 4 + 3 * (last - first))
82 return AVERROR_INVALIDDATA;
83 1 buf += 4;
84
2/2
✓ Branch 0 taken 119 times.
✓ Branch 1 taken 1 times.
120 for (i=first; i<last; i++, buf+=3) {
85 119 pal[i] = (buf[0] << 18) | (buf[1] << 10) | (buf[2] << 2);
86 119 pal[i] |= 0xFFU << 24 | (pal[i] >> 6) & 0x30303;
87 }
88
89 1 sub_type = buf[0];
90 1 type = buf[1];
91 1 buf += 4;
92 }
93
94
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 56 times.
56 if (type != AVS_VIDEO)
95 return AVERROR_INVALIDDATA;
96
97
4/5
✓ Branch 0 taken 5 times.
✓ Branch 1 taken 9 times.
✓ Branch 2 taken 5 times.
✓ Branch 3 taken 37 times.
✗ Branch 4 not taken.
56 switch (sub_type) {
98 5 case AVS_I_FRAME:
99 5 p->pict_type = AV_PICTURE_TYPE_I;
100 5 p->key_frame = 1;
101 14 case AVS_P_FRAME_3X3:
102 14 vect_w = 3;
103 14 vect_h = 3;
104 14 break;
105
106 5 case AVS_P_FRAME_2X2:
107 5 vect_w = 2;
108 5 vect_h = 2;
109 5 break;
110
111 37 case AVS_P_FRAME_2X3:
112 37 vect_w = 2;
113 37 vect_h = 3;
114 37 break;
115
116 default:
117 return AVERROR_INVALIDDATA;
118 }
119
120
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 56 times.
56 if (buf_end - buf < 256 * vect_w * vect_h)
121 return AVERROR_INVALIDDATA;
122 56 table = buf + (256 * vect_w * vect_h);
123
2/2
✓ Branch 0 taken 51 times.
✓ Branch 1 taken 5 times.
56 if (sub_type != AVS_I_FRAME) {
124 51 int map_size = ((318 / vect_w + 7) / 8) * (198 / vect_h);
125
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 51 times.
51 if (buf_end - table < map_size)
126 return AVERROR_INVALIDDATA;
127 51 init_get_bits(&change_map, table, map_size * 8);
128 51 table += map_size;
129 }
130
131
2/2
✓ Branch 0 taken 3861 times.
✓ Branch 1 taken 56 times.
3917 for (y=0; y<198; y+=vect_h) {
132
2/2
✓ Branch 0 taken 564927 times.
✓ Branch 1 taken 3861 times.
568788 for (x=0; x<318; x+=vect_w) {
133
4/4
✓ Branch 0 taken 529947 times.
✓ Branch 1 taken 34980 times.
✓ Branch 3 taken 236510 times.
✓ Branch 4 taken 293437 times.
564927 if (sub_type == AVS_I_FRAME || get_bits1(&change_map)) {
134
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 271490 times.
271490 if (buf_end - table < 1)
135 return AVERROR_INVALIDDATA;
136 271490 vect = &buf[*table++ * (vect_w * vect_h)];
137
2/2
✓ Branch 0 taken 618594 times.
✓ Branch 1 taken 271490 times.
890084 for (j=0; j<vect_w; j++) {
138 618594 out[(y + 0) * stride + x + j] = vect[(0 * vect_w) + j];
139 618594 out[(y + 1) * stride + x + j] = vect[(1 * vect_w) + j];
140
2/2
✓ Branch 0 taken 568114 times.
✓ Branch 1 taken 50480 times.
618594 if (vect_h == 3)
141 568114 out[(y + 2) * stride + x + j] =
142 568114 vect[(2 * vect_w) + j];
143 }
144 }
145 }
146
2/2
✓ Branch 0 taken 3531 times.
✓ Branch 1 taken 330 times.
3861 if (sub_type != AVS_I_FRAME)
147 3531 align_get_bits(&change_map);
148 }
149
150
1/2
✗ Branch 1 not taken.
✓ Branch 2 taken 56 times.
56 if ((ret = av_frame_ref(picture, p)) < 0)
151 return ret;
152 56 *got_frame = 1;
153
154 56 return buf_size;
155 }
156
157 2 static av_cold int avs_decode_init(AVCodecContext * avctx)
158 {
159 2 AvsContext *s = avctx->priv_data;
160
161 2 s->frame = av_frame_alloc();
162
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 2 times.
2 if (!s->frame)
163 return AVERROR(ENOMEM);
164
165 2 avctx->pix_fmt = AV_PIX_FMT_PAL8;
166
167 2 return ff_set_dimensions(avctx, 318, 198);
168 }
169
170 2 static av_cold int avs_decode_end(AVCodecContext *avctx)
171 {
172 2 AvsContext *s = avctx->priv_data;
173 2 av_frame_free(&s->frame);
174 2 return 0;
175 }
176
177
178 const FFCodec ff_avs_decoder = {
179 .p.name = "avs",
180 CODEC_LONG_NAME("AVS (Audio Video Standard) video"),
181 .p.type = AVMEDIA_TYPE_VIDEO,
182 .p.id = AV_CODEC_ID_AVS,
183 .priv_data_size = sizeof(AvsContext),
184 .init = avs_decode_init,
185 FF_CODEC_DECODE_CB(avs_decode_frame),
186 .close = avs_decode_end,
187 .p.capabilities = AV_CODEC_CAP_DR1,
188 };
189