1 /*
2 * Discworld II BMV video and audio 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
27
33
40 };
41
42 #define SCREEN_WIDE 640
43 #define SCREEN_HIGH 429
44
47
52
53 #define NEXT_BYTE(v) v = forward ? v + 1 : v - 1;
54
56 {
57 unsigned val, saved_val = 0;
58 int tmplen = src_len;
59 const uint8_t *
src, *source_end = source + src_len;
63 int forward = (frame_off <= -
SCREEN_WIDE) || (frame_off >= 0);
64 int read_two_nibbles, flag;
65 int advance_mode;
67 int i;
68
69 if (src_len <= 0)
71
72 if (forward) {
73 src = source;
75 dst_end = frame_end;
76 } else {
77 src = source + src_len - 1;
78 dst = frame_end - 1;
79 dst_end = frame - 1;
80 }
81 for (;;) {
83 flag = 0;
84
85 /* The mode/len decoding is a bit strange:
86 * values are coded as variable-length codes with nibble units,
87 * code end is signalled by two top bits in the nibble being nonzero.
88 * And since data is bytepacked and we read two nibbles at a time,
89 * we may get a nibble belonging to the next code.
90 * Hence this convoluted loop.
91 */
92 if (!mode || (tmplen == 4)) {
93 if (src < source || src >= source_end)
96 read_two_nibbles = 1;
97 } else {
98 val = saved_val;
99 read_two_nibbles = 0;
100 }
101 if (!(val & 0xC)) {
102 for (;;) {
103 if(shift>22)
104 return -1;
105 if (!read_two_nibbles) {
106 if (src < source || src >= source_end)
108 shift += 2;
109 val |= *src <<
shift;
110 if (*src & 0xC)
111 break;
112 }
113 // two upper bits of the nibble is zero,
114 // so shift top nibble value down into their place
115 read_two_nibbles = 0;
116 shift += 2;
117 mask = (1 <<
shift) - 1;
118 val = ((val >> 2) & ~mask) | (val &
mask);
120 if ((val & (0xC << shift))) {
121 flag = 1;
122 break;
123 }
124 }
125 } else if (mode) {
126 flag = tmplen != 4;
127 }
128 if (flag) {
129 tmplen = 4;
130 } else {
131 saved_val = val >> (4 +
shift);
132 tmplen = 0;
133 val &= (1 << (shift + 4)) - 1;
135 }
136 advance_mode = val & 1;
137 len = (val >> 1) - 1;
139 mode += 1 + advance_mode;
140 if (mode >= 4)
141 mode -= 3;
142 if (len <= 0 ||
FFABS(dst_end - dst) < len)
144 switch (mode) {
145 case 1:
146 if (forward) {
149 frame_end - dst < frame_off + len ||
150 frame_end - dst < len)
152 for (i = 0; i <
len; i++)
153 dst[i] = dst[frame_off + i];
155 } else {
159 frame_end - dst < frame_off + len ||
160 frame_end - dst < len)
162 for (i = len - 1; i >= 0; i--)
163 dst[i] = dst[frame_off + i];
164 }
165 break;
166 case 2:
167 if (forward) {
168 if (source + src_len - src < len)
170 memcpy(dst, src, len);
173 } else {
174 if (src - source < len)
178 memcpy(dst, src, len);
179 }
180 break;
181 case 3:
182 val = forward ? dst[-1] : dst[1];
183 if (forward) {
184 memset(dst, val, len);
186 } else {
188 memset(dst, val, len);
189 }
190 break;
191 }
192 if (dst == dst_end)
193 return 0;
194 }
195 return 0;
196 }
197
200 {
206
208 type = bytestream_get_byte(&c->
stream);
210 int blobs = bytestream_get_byte(&c->
stream);
211 if (pkt->
size < blobs * 65 + 2) {
214 }
216 }
218 int command_size = (type &
BMV_PRINT) ? 8 : 10;
222 }
223 c->
stream += command_size;
224 }
229 }
230 for (i = 0; i < 256; i++)
231 c->
pal[i] = 0xFFU << 24 | bytestream_get_be24(&c->
stream);
232 }
237 }
238 scr_off = (int16_t)bytestream_get_le16(&c->
stream);
240 scr_off = -640;
241 } else {
242 scr_off = 0;
243 }
244
246 return ret;
247
251 }
252
255
256 outptr = frame->
data[0];
258
259 for (i = 0; i < avctx->
height; i++) {
260 memcpy(outptr, srcptr, avctx->
width);
261 srcptr += avctx->
width;
263 }
264
265 *got_frame = 1;
266
267 /* always report that the buffer was completely consumed */
269 }
270
272 {
274
277
281 }
282
284
285 return 0;
286 }
287
289 16512, 8256, 4128, 2064, 1032, 516, 258, 192, 129, 88, 64, 56, 48, 40, 36, 32
290 };
291
293 {
297
298 return 0;
299 }
300
302 int *got_frame_ptr,
AVPacket *avpkt)
303 {
306 int buf_size = avpkt->
size;
307 int blocks = 0, total_blocks, i;
309 int16_t *output_samples;
311
312 total_blocks = *buf++;
313 if (buf_size < total_blocks * 65 + 1) {
315 total_blocks * 65 + 1, buf_size);
317 }
318
319 /* get output buffer */
323 output_samples = (int16_t *)frame->
data[0];
324
325 for (blocks = 0; blocks < total_blocks; blocks++) {
327 code = (code >> 1) | (code << 7);
330 for (i = 0; i < 32; i++) {
331 *output_samples++ = av_clip_int16((scale[0] * (int8_t)*buf++) >> 5);
332 *output_samples++ = av_clip_int16((scale[1] * (int8_t)*buf++) >> 5);
333 }
334 }
335
336 *got_frame_ptr = 1;
337
338 return buf_size;
339 }
340
350 };
351
360 };