Line | Branch | Exec | Source |
---|---|---|---|
1 | /* | ||
2 | * Beam Software VB decoder | ||
3 | * Copyright (c) 2007 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 | /** | ||
23 | * @file | ||
24 | * VB Video decoder | ||
25 | */ | ||
26 | |||
27 | #include "avcodec.h" | ||
28 | #include "bytestream.h" | ||
29 | #include "codec_internal.h" | ||
30 | #include "decode.h" | ||
31 | |||
32 | enum VBFlags { | ||
33 | VB_HAS_GMC = 0x01, | ||
34 | VB_HAS_AUDIO = 0x04, | ||
35 | VB_HAS_VIDEO = 0x08, | ||
36 | VB_HAS_PALETTE = 0x10, | ||
37 | VB_HAS_LENGTH = 0x20 | ||
38 | }; | ||
39 | |||
40 | typedef struct VBDecContext { | ||
41 | AVCodecContext *avctx; | ||
42 | |||
43 | uint8_t *frame, *prev_frame; | ||
44 | uint32_t pal[AVPALETTE_COUNT]; | ||
45 | GetByteContext stream; | ||
46 | } VBDecContext; | ||
47 | |||
48 | static const uint16_t vb_patterns[64] = { | ||
49 | 0x0660, 0xFF00, 0xCCCC, 0xF000, 0x8888, 0x000F, 0x1111, 0xFEC8, | ||
50 | 0x8CEF, 0x137F, 0xF731, 0xC800, 0x008C, 0x0013, 0x3100, 0xCC00, | ||
51 | 0x00CC, 0x0033, 0x3300, 0x0FF0, 0x6666, 0x00F0, 0x0F00, 0x2222, | ||
52 | 0x4444, 0xF600, 0x8CC8, 0x006F, 0x1331, 0x318C, 0xC813, 0x33CC, | ||
53 | 0x6600, 0x0CC0, 0x0066, 0x0330, 0xF900, 0xC88C, 0x009F, 0x3113, | ||
54 | 0x6000, 0x0880, 0x0006, 0x0110, 0xCC88, 0xFC00, 0x00CF, 0x88CC, | ||
55 | 0x003F, 0x1133, 0x3311, 0xF300, 0x6FF6, 0x0603, 0x08C6, 0x8C63, | ||
56 | 0xC631, 0x6310, 0xC060, 0x0136, 0x136C, 0x36C8, 0x6C80, 0x324C | ||
57 | }; | ||
58 | |||
59 | 1 | static void vb_decode_palette(VBDecContext *c, int data_size) | |
60 | { | ||
61 | int start, size, i; | ||
62 | |||
63 | 1 | start = bytestream2_get_byte(&c->stream); | |
64 | 1 | size = (bytestream2_get_byte(&c->stream) - 1) & 0xFF; | |
65 |
1/2✗ Branch 0 not taken.
✓ Branch 1 taken 1 times.
|
1 | if (start + size > 255) { |
66 | ✗ | av_log(c->avctx, AV_LOG_ERROR, "Palette change runs beyond entry 256\n"); | |
67 | ✗ | return; | |
68 | } | ||
69 |
1/2✗ Branch 0 not taken.
✓ Branch 1 taken 1 times.
|
1 | if (size*3+2 > data_size) { |
70 | ✗ | av_log(c->avctx, AV_LOG_ERROR, "Palette data runs beyond chunk size\n"); | |
71 | ✗ | return; | |
72 | } | ||
73 |
2/2✓ Branch 0 taken 236 times.
✓ Branch 1 taken 1 times.
|
237 | for (i = start; i <= start + size; i++) |
74 | 236 | c->pal[i] = 0xFFU << 24 | bytestream2_get_be24(&c->stream); | |
75 | } | ||
76 | |||
77 | 67819 | static inline int check_pixel(uint8_t *buf, uint8_t *start, uint8_t *end) | |
78 | { | ||
79 |
2/4✓ Branch 0 taken 67819 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 67819 times.
✗ Branch 3 not taken.
|
67819 | return buf >= start && buf < end; |
80 | } | ||
81 | |||
82 | 369468 | static inline int check_line(uint8_t *buf, uint8_t *start, uint8_t *end) | |
83 | { | ||
84 |
2/4✓ Branch 0 taken 369468 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 369468 times.
✗ Branch 3 not taken.
|
369468 | return buf >= start && (buf + 4) <= end; |
85 | } | ||
86 | |||
87 | 37 | static int vb_decode_framedata(VBDecContext *c, int offset) | |
88 | { | ||
89 | GetByteContext g; | ||
90 | uint8_t *prev, *cur; | ||
91 | int blk, blocks, t, blk2; | ||
92 | 37 | int blocktypes = 0; | |
93 | int x, y, a, b; | ||
94 | int pattype, pattern; | ||
95 | 37 | const int width = c->avctx->width; | |
96 | 37 | uint8_t *pstart = c->prev_frame; | |
97 | 37 | uint8_t *pend = c->prev_frame + width*c->avctx->height; | |
98 | |||
99 | 37 | g = c->stream; | |
100 | |||
101 | 37 | prev = c->prev_frame + offset; | |
102 | 37 | cur = c->frame; | |
103 | |||
104 | 37 | blocks = (c->avctx->width >> 2) * (c->avctx->height >> 2); | |
105 | 37 | blk2 = 0; | |
106 |
2/2✓ Branch 0 taken 177600 times.
✓ Branch 1 taken 37 times.
|
177637 | for (blk = 0; blk < blocks; blk++) { |
107 |
2/2✓ Branch 0 taken 44400 times.
✓ Branch 1 taken 133200 times.
|
177600 | if (!(blk & 3)) { |
108 |
1/2✗ Branch 1 not taken.
✓ Branch 2 taken 44400 times.
|
44400 | if (bytestream2_get_bytes_left(&g) < 1) { |
109 | ✗ | av_log(c->avctx, AV_LOG_ERROR, "Insufficient data\n"); | |
110 | ✗ | return AVERROR_INVALIDDATA; | |
111 | } | ||
112 | 44400 | blocktypes = bytestream2_get_byte(&g); | |
113 | } | ||
114 |
4/5✓ Branch 0 taken 14413 times.
✓ Branch 1 taken 93230 times.
✓ Branch 2 taken 11328 times.
✓ Branch 3 taken 58629 times.
✗ Branch 4 not taken.
|
177600 | switch (blocktypes & 0xC0) { |
115 | 14413 | case 0x00: //skip | |
116 |
2/2✓ Branch 0 taken 57652 times.
✓ Branch 1 taken 14413 times.
|
72065 | for (y = 0; y < 4; y++) |
117 |
1/2✓ Branch 1 taken 57652 times.
✗ Branch 2 not taken.
|
57652 | if (check_line(prev + y*width, pstart, pend)) |
118 | 57652 | memcpy(cur + y*width, prev + y*width, 4); | |
119 | else | ||
120 | ✗ | memset(cur + y*width, 0, 4); | |
121 | 14413 | break; | |
122 | 93230 | case 0x40: | |
123 | 93230 | t = bytestream2_get_byte(&g); | |
124 |
2/2✓ Branch 0 taken 15276 times.
✓ Branch 1 taken 77954 times.
|
93230 | if (!t) { //raw block |
125 |
1/2✗ Branch 1 not taken.
✓ Branch 2 taken 15276 times.
|
15276 | if (bytestream2_get_bytes_left(&g) < 16) { |
126 | ✗ | av_log(c->avctx, AV_LOG_ERROR, "Insufficient data\n"); | |
127 | ✗ | return AVERROR_INVALIDDATA; | |
128 | } | ||
129 |
2/2✓ Branch 0 taken 61104 times.
✓ Branch 1 taken 15276 times.
|
76380 | for (y = 0; y < 4; y++) |
130 | 61104 | bytestream2_get_buffer(&g, cur + y * width, 4); | |
131 | } else { // motion compensation | ||
132 | 77954 | x = ((t & 0xF)^8) - 8; | |
133 | 77954 | y = ((t >> 4) ^8) - 8; | |
134 | 77954 | t = x + y*width; | |
135 |
2/2✓ Branch 0 taken 311816 times.
✓ Branch 1 taken 77954 times.
|
389770 | for (y = 0; y < 4; y++) |
136 |
1/2✓ Branch 1 taken 311816 times.
✗ Branch 2 not taken.
|
311816 | if (check_line(prev + t + y*width, pstart, pend)) |
137 | 311816 | memcpy(cur + y*width, prev + t + y*width, 4); | |
138 | else | ||
139 | ✗ | memset(cur + y*width, 0, 4); | |
140 | } | ||
141 | 93230 | break; | |
142 | 11328 | case 0x80: // fill | |
143 | 11328 | t = bytestream2_get_byte(&g); | |
144 |
2/2✓ Branch 0 taken 45312 times.
✓ Branch 1 taken 11328 times.
|
56640 | for (y = 0; y < 4; y++) |
145 | 45312 | memset(cur + y*width, t, 4); | |
146 | 11328 | break; | |
147 | 58629 | case 0xC0: // pattern fill | |
148 | 58629 | t = bytestream2_get_byte(&g); | |
149 | 58629 | pattype = t >> 6; | |
150 |
3/5✓ Branch 0 taken 51372 times.
✓ Branch 1 taken 4746 times.
✓ Branch 2 taken 2511 times.
✗ Branch 3 not taken.
✗ Branch 4 not taken.
|
58629 | pattern = vb_patterns[t & 0x3F]; |
151 | switch (pattype) { | ||
152 | 51372 | case 0: | |
153 | 51372 | a = bytestream2_get_byte(&g); | |
154 | 51372 | b = bytestream2_get_byte(&g); | |
155 |
2/2✓ Branch 0 taken 205488 times.
✓ Branch 1 taken 51372 times.
|
256860 | for (y = 0; y < 4; y++) |
156 |
2/2✓ Branch 0 taken 821952 times.
✓ Branch 1 taken 205488 times.
|
1027440 | for (x = 0; x < 4; x++, pattern >>= 1) |
157 |
2/2✓ Branch 0 taken 288111 times.
✓ Branch 1 taken 533841 times.
|
821952 | cur[x + y*width] = (pattern & 1) ? b : a; |
158 | 51372 | break; | |
159 | 4746 | case 1: | |
160 | 4746 | pattern = ~pattern; | |
161 | 7257 | case 2: | |
162 | 7257 | a = bytestream2_get_byte(&g); | |
163 |
2/2✓ Branch 0 taken 29028 times.
✓ Branch 1 taken 7257 times.
|
36285 | for (y = 0; y < 4; y++) |
164 |
2/2✓ Branch 0 taken 116112 times.
✓ Branch 1 taken 29028 times.
|
145140 | for (x = 0; x < 4; x++, pattern >>= 1) |
165 |
3/4✓ Branch 0 taken 67819 times.
✓ Branch 1 taken 48293 times.
✓ Branch 3 taken 67819 times.
✗ Branch 4 not taken.
|
116112 | if (pattern & 1 && check_pixel(prev + x + y*width, pstart, pend)) |
166 | 67819 | cur[x + y*width] = prev[x + y*width]; | |
167 | else | ||
168 | 48293 | cur[x + y*width] = a; | |
169 | 7257 | break; | |
170 | ✗ | case 3: | |
171 | ✗ | av_log(c->avctx, AV_LOG_ERROR, "Invalid opcode seen @%d\n", blk); | |
172 | ✗ | return AVERROR_INVALIDDATA; | |
173 | } | ||
174 | 58629 | break; | |
175 | } | ||
176 | 177600 | blocktypes <<= 2; | |
177 | 177600 | cur += 4; | |
178 | 177600 | prev += 4; | |
179 | 177600 | blk2++; | |
180 |
2/2✓ Branch 0 taken 2220 times.
✓ Branch 1 taken 175380 times.
|
177600 | if (blk2 == (width >> 2)) { |
181 | 2220 | blk2 = 0; | |
182 | 2220 | cur += width * 3; | |
183 | 2220 | prev += width * 3; | |
184 | } | ||
185 | } | ||
186 | 37 | return 0; | |
187 | } | ||
188 | |||
189 | 37 | static int decode_frame(AVCodecContext *avctx, AVFrame *frame, | |
190 | int *got_frame, AVPacket *avpkt) | ||
191 | { | ||
192 | 37 | VBDecContext * const c = avctx->priv_data; | |
193 | uint8_t *outptr, *srcptr; | ||
194 | int i, j, ret; | ||
195 | int flags; | ||
196 | uint32_t size; | ||
197 | 37 | int offset = 0; | |
198 | |||
199 |
1/2✗ Branch 0 not taken.
✓ Branch 1 taken 37 times.
|
37 | if (avpkt->size < 2) |
200 | ✗ | return AVERROR_INVALIDDATA; | |
201 | |||
202 | 37 | bytestream2_init(&c->stream, avpkt->data, avpkt->size); | |
203 | |||
204 |
1/2✗ Branch 1 not taken.
✓ Branch 2 taken 37 times.
|
37 | if ((ret = ff_get_buffer(avctx, frame, 0)) < 0) |
205 | ✗ | return ret; | |
206 | |||
207 | 37 | flags = bytestream2_get_le16(&c->stream); | |
208 | |||
209 |
2/2✓ Branch 0 taken 8 times.
✓ Branch 1 taken 29 times.
|
37 | if (flags & VB_HAS_GMC) { |
210 | 8 | i = (int16_t)bytestream2_get_le16(&c->stream); | |
211 | 8 | j = (int16_t)bytestream2_get_le16(&c->stream); | |
212 |
1/2✗ Branch 0 not taken.
✓ Branch 1 taken 8 times.
|
8 | if (FFABS(j) > avctx->height) { |
213 | ✗ | av_log(avctx, AV_LOG_ERROR, "GMV out of range\n"); | |
214 | ✗ | return AVERROR_INVALIDDATA; | |
215 | } | ||
216 | 8 | offset = i + j * avctx->width; | |
217 | } | ||
218 |
1/2✓ Branch 0 taken 37 times.
✗ Branch 1 not taken.
|
37 | if (flags & VB_HAS_VIDEO) { |
219 | 37 | size = bytestream2_get_le32(&c->stream); | |
220 |
2/4✓ Branch 1 taken 37 times.
✗ Branch 2 not taken.
✗ Branch 3 not taken.
✓ Branch 4 taken 37 times.
|
37 | if(size > bytestream2_get_bytes_left(&c->stream)+4 || size<4){ |
221 | ✗ | av_log(avctx, AV_LOG_ERROR, "Frame size invalid\n"); | |
222 | ✗ | return -1; | |
223 | } | ||
224 | 37 | vb_decode_framedata(c, offset); | |
225 | 37 | bytestream2_skip(&c->stream, size - 4); | |
226 | } | ||
227 |
2/2✓ Branch 0 taken 1 times.
✓ Branch 1 taken 36 times.
|
37 | if (flags & VB_HAS_PALETTE) { |
228 | 1 | size = bytestream2_get_le32(&c->stream); | |
229 | 1 | vb_decode_palette(c, size); | |
230 | } | ||
231 | |||
232 | 37 | memcpy(frame->data[1], c->pal, AVPALETTE_SIZE); | |
233 | #if FF_API_PALETTE_HAS_CHANGED | ||
234 | FF_DISABLE_DEPRECATION_WARNINGS | ||
235 | 37 | frame->palette_has_changed = flags & VB_HAS_PALETTE; | |
236 | FF_ENABLE_DEPRECATION_WARNINGS | ||
237 | #endif | ||
238 | |||
239 | 37 | outptr = frame->data[0]; | |
240 | 37 | srcptr = c->frame; | |
241 | |||
242 |
2/2✓ Branch 0 taken 8880 times.
✓ Branch 1 taken 37 times.
|
8917 | for (i = 0; i < avctx->height; i++) { |
243 | 8880 | memcpy(outptr, srcptr, avctx->width); | |
244 | 8880 | srcptr += avctx->width; | |
245 | 8880 | outptr += frame->linesize[0]; | |
246 | } | ||
247 | |||
248 | 37 | FFSWAP(uint8_t*, c->frame, c->prev_frame); | |
249 | |||
250 | 37 | *got_frame = 1; | |
251 | |||
252 | /* always report that the buffer was completely consumed */ | ||
253 | 37 | return avpkt->size; | |
254 | } | ||
255 | |||
256 | 3 | static av_cold int decode_init(AVCodecContext *avctx) | |
257 | { | ||
258 | 3 | VBDecContext * const c = avctx->priv_data; | |
259 | |||
260 | 3 | c->avctx = avctx; | |
261 | 3 | avctx->pix_fmt = AV_PIX_FMT_PAL8; | |
262 | |||
263 | 3 | c->frame = av_mallocz(avctx->width * avctx->height); | |
264 | 3 | c->prev_frame = av_mallocz(avctx->width * avctx->height); | |
265 | |||
266 |
2/4✓ Branch 0 taken 3 times.
✗ Branch 1 not taken.
✗ Branch 2 not taken.
✓ Branch 3 taken 3 times.
|
3 | if (!c->frame || !c->prev_frame) |
267 | ✗ | return AVERROR(ENOMEM); | |
268 | |||
269 | 3 | return 0; | |
270 | } | ||
271 | |||
272 | 3 | static av_cold int decode_end(AVCodecContext *avctx) | |
273 | { | ||
274 | 3 | VBDecContext *c = avctx->priv_data; | |
275 | |||
276 | 3 | av_freep(&c->frame); | |
277 | 3 | av_freep(&c->prev_frame); | |
278 | |||
279 | 3 | return 0; | |
280 | } | ||
281 | |||
282 | const FFCodec ff_vb_decoder = { | ||
283 | .p.name = "vb", | ||
284 | CODEC_LONG_NAME("Beam Software VB"), | ||
285 | .p.type = AVMEDIA_TYPE_VIDEO, | ||
286 | .p.id = AV_CODEC_ID_VB, | ||
287 | .priv_data_size = sizeof(VBDecContext), | ||
288 | .init = decode_init, | ||
289 | .close = decode_end, | ||
290 | FF_CODEC_DECODE_CB(decode_frame), | ||
291 | .p.capabilities = AV_CODEC_CAP_DR1, | ||
292 | .caps_internal = FF_CODEC_CAP_INIT_CLEANUP, | ||
293 | }; | ||
294 |