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
24
28
34
41 };
42
43 #define SCREEN_WIDE 640
44 #define SCREEN_HIGH 429
45
48
53
54 #define NEXT_BYTE(v) (v) = forward ? (v) + 1 : (v) - 1;
55
57 {
58 unsigned val, saved_val = 0;
59 int tmplen = src_len;
60 const uint8_t *
src, *source_end = source + src_len;
64 int forward = (frame_off <= -
SCREEN_WIDE) || (frame_off >= 0);
65 int read_two_nibbles, flag;
66 int advance_mode;
68 int i;
69
70 if (src_len <= 0)
72
73 if (forward) {
74 src = source;
77 } else {
78 src = source + src_len - 1;
79 dst = frame_end - 1;
80 dst_end = frame - 1;
81 }
82 for (;;) {
84 flag = 0;
85
86 /* The mode/len decoding is a bit strange:
87 * values are coded as variable-length codes with nibble units,
88 * code end is signalled by two top bits in the nibble being nonzero.
89 * And since data is bytepacked and we read two nibbles at a time,
90 * we may get a nibble belonging to the next code.
91 * Hence this convoluted loop.
92 */
93 if (!mode || (tmplen == 4)) {
94 if (src < source || src >= source_end)
97 read_two_nibbles = 1;
98 } else {
99 val = saved_val;
100 read_two_nibbles = 0;
101 }
102 if (!(val & 0xC)) {
103 for (;;) {
104 if(shift>22)
105 return -1;
106 if (!read_two_nibbles) {
107 if (src < source || src >= source_end)
109 shift += 2;
110 val |= *src <<
shift;
111 if (*src & 0xC)
112 break;
113 }
114 // two upper bits of the nibble is zero,
115 // so shift top nibble value down into their place
116 read_two_nibbles = 0;
117 shift += 2;
118 mask = (1 <<
shift) - 1;
119 val = ((val >> 2) & ~mask) | (val &
mask);
121 if ((val & (0xC << shift))) {
122 flag = 1;
123 break;
124 }
125 }
126 } else if (mode) {
127 flag = tmplen != 4;
128 }
129 if (flag) {
130 tmplen = 4;
131 } else {
132 saved_val = val >> (4 +
shift);
133 tmplen = 0;
134 val &= (1 << (shift + 4)) - 1;
136 }
137 advance_mode = val & 1;
138 len = (val >> 1) - 1;
140 mode += 1 + advance_mode;
141 if (mode >= 4)
142 mode -= 3;
143 if (len <= 0 ||
FFABS(dst_end - dst) < len)
145 switch (mode) {
146 case 1:
147 if (forward) {
150 frame_end - dst < frame_off + len ||
151 frame_end - dst < len)
153 for (i = 0; i <
len; i++)
154 dst[i] = dst[frame_off + i];
156 } else {
160 frame_end - dst < frame_off + len ||
161 frame_end - dst < len)
163 for (i = len - 1; i >= 0; i--)
164 dst[i] = dst[frame_off + i];
165 }
166 break;
167 case 2:
168 if (forward) {
169 if (source + src_len - src < len)
171 memcpy(dst, src, len);
174 } else {
175 if (src - source < len)
179 memcpy(dst, src, len);
180 }
181 break;
182 case 3:
183 val = forward ? dst[-1] : dst[1];
184 if (forward) {
185 memset(dst, val, len);
187 } else {
189 memset(dst, val, len);
190 }
191 break;
192 }
193 if (dst == dst_end)
194 return 0;
195 }
196 return 0;
197 }
198
201 {
207
209 type = bytestream_get_byte(&c->
stream);
211 int blobs = bytestream_get_byte(&c->
stream);
212 if (pkt->
size < blobs * 65 + 2) {
215 }
217 }
219 int command_size = (type &
BMV_PRINT) ? 8 : 10;
223 }
224 c->
stream += command_size;
225 }
230 }
231 for (i = 0; i < 256; i++)
232 c->
pal[i] = 0xFFU << 24 | bytestream_get_be24(&c->
stream);
233 }
238 }
239 scr_off = (int16_t)bytestream_get_le16(&c->
stream);
241 scr_off = -640;
242 } else {
243 scr_off = 0;
244 }
245
247 return ret;
248
252 }
253
256
257 outptr = frame->
data[0];
259
260 for (i = 0; i < avctx->
height; i++) {
261 memcpy(outptr, srcptr, avctx->
width);
262 srcptr += avctx->
width;
264 }
265
266 *got_frame = 1;
267
268 /* always report that the buffer was completely consumed */
270 }
271
273 {
275
278
282 }
283
285
286 return 0;
287 }
288
298 };