FFmpeg coverage


Directory: ../../../ffmpeg/
File: src/libavcodec/bmvvideo.c
Date: 2024-03-28 04:31:58
Exec Total Coverage
Lines: 114 157 72.6%
Functions: 3 3 100.0%
Branches: 67 118 56.8%

Line Branch Exec Source
1 /*
2 * Discworld II BMV video decoder
3 * Copyright (c) 2011 Konstantin Shishkov
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/avassert.h"
23 #include "libavutil/common.h"
24
25 #include "avcodec.h"
26 #include "bytestream.h"
27 #include "codec_internal.h"
28 #include "decode.h"
29
30 enum BMVFlags{
31 BMV_NOP = 0,
32 BMV_END,
33 BMV_DELTA,
34 BMV_INTRA,
35
36 BMV_SCROLL = 0x04,
37 BMV_PALETTE = 0x08,
38 BMV_COMMAND = 0x10,
39 BMV_AUDIO = 0x20,
40 BMV_EXT = 0x40,
41 BMV_PRINT = 0x80
42 };
43
44 #define SCREEN_WIDE 640
45 #define SCREEN_HIGH 429
46
47 typedef struct BMVDecContext {
48 AVCodecContext *avctx;
49
50 uint8_t *frame, frame_base[SCREEN_WIDE * (SCREEN_HIGH + 1)];
51 uint32_t pal[256];
52 const uint8_t *stream;
53 } BMVDecContext;
54
55 #define NEXT_BYTE(v) (v) = forward ? (v) + 1 : (v) - 1;
56
57 21 static int decode_bmv_frame(const uint8_t *source, int src_len, uint8_t *frame, int frame_off)
58 {
59 21 unsigned val, saved_val = 0;
60 21 int tmplen = src_len;
61 21 const uint8_t *src, *source_end = source + src_len;
62 21 uint8_t *frame_end = frame + SCREEN_WIDE * SCREEN_HIGH;
63 uint8_t *dst, *dst_end;
64 int len, mask;
65
3/4
✓ Branch 0 taken 10 times.
✓ Branch 1 taken 11 times.
✓ Branch 2 taken 10 times.
✗ Branch 3 not taken.
21 int forward = (frame_off <= -SCREEN_WIDE) || (frame_off >= 0);
66 int read_two_nibbles, flag;
67 int advance_mode;
68 21 int mode = 0;
69 int i;
70
71
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 21 times.
21 if (src_len <= 0)
72 return AVERROR_INVALIDDATA;
73
74
1/2
✓ Branch 0 taken 21 times.
✗ Branch 1 not taken.
21 if (forward) {
75 21 src = source;
76 21 dst = frame;
77 21 dst_end = frame_end;
78 } else {
79 src = source + src_len - 1;
80 dst = frame_end - 1;
81 dst_end = frame - 1;
82 }
83 783652 for (;;) {
84 783673 int shift = 0;
85 783673 flag = 0;
86
87 /* The mode/len decoding is a bit strange:
88 * values are coded as variable-length codes with nibble units,
89 * code end is signalled by two top bits in the nibble being nonzero.
90 * And since data is bytepacked and we read two nibbles at a time,
91 * we may get a nibble belonging to the next code.
92 * Hence this convoluted loop.
93 */
94
4/4
✓ Branch 0 taken 783652 times.
✓ Branch 1 taken 21 times.
✓ Branch 2 taken 391866 times.
✓ Branch 3 taken 391786 times.
783673 if (!mode || (tmplen == 4)) {
95
2/4
✓ Branch 0 taken 391887 times.
✗ Branch 1 not taken.
✗ Branch 2 not taken.
✓ Branch 3 taken 391887 times.
391887 if (src < source || src >= source_end)
96 return AVERROR_INVALIDDATA;
97 391887 val = *src;
98 391887 read_two_nibbles = 1;
99 } else {
100 391786 val = saved_val;
101 391786 read_two_nibbles = 0;
102 }
103
2/2
✓ Branch 0 taken 71035 times.
✓ Branch 1 taken 712638 times.
783673 if (!(val & 0xC)) {
104 for (;;) {
105
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 73874 times.
73874 if(shift>22)
106 return -1;
107
2/2
✓ Branch 0 taken 38323 times.
✓ Branch 1 taken 35551 times.
73874 if (!read_two_nibbles) {
108
2/4
✓ Branch 0 taken 38323 times.
✗ Branch 1 not taken.
✗ Branch 2 not taken.
✓ Branch 3 taken 38323 times.
38323 if (src < source || src >= source_end)
109 return AVERROR_INVALIDDATA;
110 38323 shift += 2;
111 38323 val |= (unsigned)*src << shift;
112
2/2
✓ Branch 0 taken 35465 times.
✓ Branch 1 taken 2858 times.
38323 if (*src & 0xC)
113 35465 break;
114 }
115 // two upper bits of the nibble is zero,
116 // so shift top nibble value down into their place
117 38409 read_two_nibbles = 0;
118 38409 shift += 2;
119 38409 mask = (1 << shift) - 1;
120 38409 val = ((val >> 2) & ~mask) | (val & mask);
121
1/2
✓ Branch 0 taken 38409 times.
✗ Branch 1 not taken.
38409 NEXT_BYTE(src);
122
2/2
✓ Branch 0 taken 35570 times.
✓ Branch 1 taken 2839 times.
38409 if ((val & (0xC << shift))) {
123 35570 flag = 1;
124 35570 break;
125 }
126 }
127
2/2
✓ Branch 0 taken 712627 times.
✓ Branch 1 taken 11 times.
712638 } else if (mode) {
128 712627 flag = tmplen != 4;
129 }
130
2/2
✓ Branch 0 taken 391872 times.
✓ Branch 1 taken 391801 times.
783673 if (flag) {
131 391872 tmplen = 4;
132 } else {
133 391801 saved_val = val >> (4 + shift);
134 391801 tmplen = 0;
135 391801 val &= (1 << (shift + 4)) - 1;
136
1/2
✓ Branch 0 taken 391801 times.
✗ Branch 1 not taken.
391801 NEXT_BYTE(src);
137 }
138 783673 advance_mode = val & 1;
139 783673 len = (val >> 1) - 1;
140
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 783673 times.
783673 av_assert0(len>0);
141 783673 mode += 1 + advance_mode;
142
2/2
✓ Branch 0 taken 380507 times.
✓ Branch 1 taken 403166 times.
783673 if (mode >= 4)
143 380507 mode -= 3;
144
3/6
✓ Branch 0 taken 783673 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 783673 times.
✗ Branch 3 not taken.
✗ Branch 4 not taken.
✓ Branch 5 taken 783673 times.
783673 if (len <= 0 || FFABS(dst_end - dst) < len)
145 return AVERROR_INVALIDDATA;
146
3/4
✓ Branch 0 taken 207200 times.
✓ Branch 1 taken 312625 times.
✓ Branch 2 taken 263848 times.
✗ Branch 3 not taken.
783673 switch (mode) {
147 207200 case 1:
148
1/2
✓ Branch 0 taken 207200 times.
✗ Branch 1 not taken.
207200 if (forward) {
149
1/2
✓ Branch 0 taken 207200 times.
✗ Branch 1 not taken.
207200 if (dst - frame + SCREEN_WIDE < frame_off ||
150
1/2
✓ Branch 0 taken 207200 times.
✗ Branch 1 not taken.
207200 dst - frame + SCREEN_WIDE + frame_off < 0 ||
151
1/2
✓ Branch 0 taken 207200 times.
✗ Branch 1 not taken.
207200 frame_end - dst < frame_off + len ||
152
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 207200 times.
207200 frame_end - dst < len)
153 return AVERROR_INVALIDDATA;
154
2/2
✓ Branch 0 taken 3972210 times.
✓ Branch 1 taken 207200 times.
4179410 for (i = 0; i < len; i++)
155 3972210 dst[i] = dst[frame_off + i];
156 207200 dst += len;
157 } else {
158 dst -= len;
159 if (dst - frame + SCREEN_WIDE < frame_off ||
160 dst - frame + SCREEN_WIDE + frame_off < 0 ||
161 frame_end - dst < frame_off + len ||
162 frame_end - dst < len)
163 return AVERROR_INVALIDDATA;
164 for (i = len - 1; i >= 0; i--)
165 dst[i] = dst[frame_off + i];
166 }
167 207200 break;
168 312625 case 2:
169
1/2
✓ Branch 0 taken 312625 times.
✗ Branch 1 not taken.
312625 if (forward) {
170
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 312625 times.
312625 if (source + src_len - src < len)
171 return AVERROR_INVALIDDATA;
172 312625 memcpy(dst, src, len);
173 312625 dst += len;
174 312625 src += len;
175 } else {
176 if (src - source < len)
177 return AVERROR_INVALIDDATA;
178 dst -= len;
179 src -= len;
180 memcpy(dst, src, len);
181 }
182 312625 break;
183 263848 case 3:
184
1/2
✓ Branch 0 taken 263848 times.
✗ Branch 1 not taken.
263848 val = forward ? dst[-1] : dst[1];
185
1/2
✓ Branch 0 taken 263848 times.
✗ Branch 1 not taken.
263848 if (forward) {
186 263848 memset(dst, val, len);
187 263848 dst += len;
188 } else {
189 dst -= len;
190 memset(dst, val, len);
191 }
192 263848 break;
193 }
194
2/2
✓ Branch 0 taken 21 times.
✓ Branch 1 taken 783652 times.
783673 if (dst == dst_end)
195 21 return 0;
196 }
197 }
198
199 21 static int decode_frame(AVCodecContext *avctx, AVFrame *frame,
200 int *got_frame, AVPacket *pkt)
201 {
202 21 BMVDecContext * const c = avctx->priv_data;
203 int type, scr_off;
204 int i, ret;
205 uint8_t *srcptr, *outptr;
206
207 21 c->stream = pkt->data;
208 21 type = bytestream_get_byte(&c->stream);
209
1/2
✓ Branch 0 taken 21 times.
✗ Branch 1 not taken.
21 if (type & BMV_AUDIO) {
210 21 int blobs = bytestream_get_byte(&c->stream);
211
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 21 times.
21 if (pkt->size < blobs * 65 + 2) {
212 av_log(avctx, AV_LOG_ERROR, "Audio data doesn't fit in frame\n");
213 return AVERROR_INVALIDDATA;
214 }
215 21 c->stream += blobs * 65;
216 }
217
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 21 times.
21 if (type & BMV_COMMAND) {
218 int command_size = (type & BMV_PRINT) ? 8 : 10;
219 if (c->stream - pkt->data + command_size > pkt->size) {
220 av_log(avctx, AV_LOG_ERROR, "Command data doesn't fit in frame\n");
221 return AVERROR_INVALIDDATA;
222 }
223 c->stream += command_size;
224 }
225
2/2
✓ Branch 0 taken 1 times.
✓ Branch 1 taken 20 times.
21 if (type & BMV_PALETTE) {
226
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 1 times.
1 if (c->stream - pkt->data > pkt->size - 768) {
227 av_log(avctx, AV_LOG_ERROR, "Palette data doesn't fit in frame\n");
228 return AVERROR_INVALIDDATA;
229 }
230
2/2
✓ Branch 0 taken 256 times.
✓ Branch 1 taken 1 times.
257 for (i = 0; i < 256; i++)
231 256 c->pal[i] = 0xFFU << 24 | bytestream_get_be24(&c->stream);
232 }
233
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 21 times.
21 if (type & BMV_SCROLL) {
234 if (c->stream - pkt->data > pkt->size - 2) {
235 av_log(avctx, AV_LOG_ERROR, "Screen offset data doesn't fit in frame\n");
236 return AVERROR_INVALIDDATA;
237 }
238 scr_off = (int16_t)bytestream_get_le16(&c->stream);
239
2/2
✓ Branch 0 taken 11 times.
✓ Branch 1 taken 10 times.
21 } else if ((type & BMV_INTRA) == BMV_INTRA) {
240 11 scr_off = -640;
241 } else {
242 10 scr_off = 0;
243 }
244
245
1/2
✗ Branch 1 not taken.
✓ Branch 2 taken 21 times.
21 if ((ret = ff_get_buffer(avctx, frame, 0)) < 0)
246 return ret;
247
248
1/2
✗ Branch 1 not taken.
✓ Branch 2 taken 21 times.
21 if (decode_bmv_frame(c->stream, pkt->size - (c->stream - pkt->data), c->frame, scr_off)) {
249 av_log(avctx, AV_LOG_ERROR, "Error decoding frame data\n");
250 return AVERROR_INVALIDDATA;
251 }
252
253 21 memcpy(frame->data[1], c->pal, AVPALETTE_SIZE);
254 #if FF_API_PALETTE_HAS_CHANGED
255 FF_DISABLE_DEPRECATION_WARNINGS
256 21 frame->palette_has_changed = type & BMV_PALETTE;
257 FF_ENABLE_DEPRECATION_WARNINGS
258 #endif
259
260 21 outptr = frame->data[0];
261 21 srcptr = c->frame;
262
263
2/2
✓ Branch 0 taken 9009 times.
✓ Branch 1 taken 21 times.
9030 for (i = 0; i < avctx->height; i++) {
264 9009 memcpy(outptr, srcptr, avctx->width);
265 9009 srcptr += avctx->width;
266 9009 outptr += frame->linesize[0];
267 }
268
269 21 *got_frame = 1;
270
271 /* always report that the buffer was completely consumed */
272 21 return pkt->size;
273 }
274
275 3 static av_cold int decode_init(AVCodecContext *avctx)
276 {
277 3 BMVDecContext * const c = avctx->priv_data;
278
279 3 c->avctx = avctx;
280 3 avctx->pix_fmt = AV_PIX_FMT_PAL8;
281
282
2/4
✓ Branch 0 taken 3 times.
✗ Branch 1 not taken.
✗ Branch 2 not taken.
✓ Branch 3 taken 3 times.
3 if (avctx->width != SCREEN_WIDE || avctx->height != SCREEN_HIGH) {
283 av_log(avctx, AV_LOG_ERROR, "Invalid dimension %dx%d\n", avctx->width, avctx->height);
284 return AVERROR_INVALIDDATA;
285 }
286
287 3 c->frame = c->frame_base + 640;
288
289 3 return 0;
290 }
291
292 const FFCodec ff_bmv_video_decoder = {
293 .p.name = "bmv_video",
294 CODEC_LONG_NAME("Discworld II BMV video"),
295 .p.type = AVMEDIA_TYPE_VIDEO,
296 .p.id = AV_CODEC_ID_BMV_VIDEO,
297 .priv_data_size = sizeof(BMVDecContext),
298 .init = decode_init,
299 FF_CODEC_DECODE_CB(decode_frame),
300 .p.capabilities = AV_CODEC_CAP_DR1,
301 };
302