3ff6177c74d877b5c5710632af462662d33b31d1
[libav.git] / libavcodec / bmv.c
1 /*
2 * Discworld II BMV video and audio decoder
3 * Copyright (c) 2011 Konstantin Shishkov
4 *
5 * This file is part of Libav.
6 *
7 * Libav 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 * Libav 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 Libav; 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/channel_layout.h"
23 #include "avcodec.h"
24 #include "bytestream.h"
25
26 enum BMVFlags{
27 BMV_NOP = 0,
28 BMV_END,
29 BMV_DELTA,
30 BMV_INTRA,
31
32 BMV_SCROLL = 0x04,
33 BMV_PALETTE = 0x08,
34 BMV_COMMAND = 0x10,
35 BMV_AUDIO = 0x20,
36 BMV_EXT = 0x40,
37 BMV_PRINT = 0x80
38 };
39
40 #define SCREEN_WIDE 640
41 #define SCREEN_HIGH 429
42
43 typedef struct BMVDecContext {
44 AVCodecContext *avctx;
45 AVFrame pic;
46
47 uint8_t *frame, frame_base[SCREEN_WIDE * (SCREEN_HIGH + 1)];
48 uint32_t pal[256];
49 const uint8_t *stream;
50 } BMVDecContext;
51
52 #define NEXT_BYTE(v) v = forward ? v + 1 : v - 1;
53
54 static int decode_bmv_frame(const uint8_t *source, int src_len, uint8_t *frame, int frame_off)
55 {
56 unsigned val, saved_val = 0;
57 int tmplen = src_len;
58 const uint8_t *src, *source_end = source + src_len;
59 uint8_t *frame_end = frame + SCREEN_WIDE * SCREEN_HIGH;
60 uint8_t *dst, *dst_end;
61 int len, mask;
62 int forward = (frame_off <= -SCREEN_WIDE) || (frame_off >= 0);
63 int read_two_nibbles, flag;
64 int advance_mode;
65 int mode = 0;
66 int i;
67
68 if (src_len <= 0)
69 return -1;
70
71 if (forward) {
72 src = source;
73 dst = frame;
74 dst_end = frame_end;
75 } else {
76 src = source + src_len - 1;
77 dst = frame_end - 1;
78 dst_end = frame - 1;
79 }
80 for (;;) {
81 int shift = 0;
82 flag = 0;
83
84 /* The mode/len decoding is a bit strange:
85 * values are coded as variable-length codes with nibble units,
86 * code end is signalled by two top bits in the nibble being nonzero.
87 * And since data is bytepacked and we read two nibbles at a time,
88 * we may get a nibble belonging to the next code.
89 * Hence this convoluted loop.
90 */
91 if (!mode || (tmplen == 4)) {
92 if (src < source || src >= source_end)
93 return -1;
94 val = *src;
95 read_two_nibbles = 1;
96 } else {
97 val = saved_val;
98 read_two_nibbles = 0;
99 }
100 if (!(val & 0xC)) {
101 for (;;) {
102 if (!read_two_nibbles) {
103 if (src < source || src >= source_end)
104 return -1;
105 shift += 2;
106 val |= *src << shift;
107 if (*src & 0xC)
108 break;
109 }
110 // two upper bits of the nibble is zero,
111 // so shift top nibble value down into their place
112 read_two_nibbles = 0;
113 shift += 2;
114 mask = (1 << shift) - 1;
115 val = ((val >> 2) & ~mask) | (val & mask);
116 NEXT_BYTE(src);
117 if ((val & (0xC << shift))) {
118 flag = 1;
119 break;
120 }
121 }
122 } else if (mode) {
123 flag = tmplen != 4;
124 }
125 if (flag) {
126 tmplen = 4;
127 } else {
128 saved_val = val >> (4 + shift);
129 tmplen = 0;
130 val &= (1 << (shift + 4)) - 1;
131 NEXT_BYTE(src);
132 }
133 advance_mode = val & 1;
134 len = (val >> 1) - 1;
135 mode += 1 + advance_mode;
136 if (mode >= 4)
137 mode -= 3;
138 if (FFABS(dst_end - dst) < len)
139 return -1;
140 switch (mode) {
141 case 1:
142 if (forward) {
143 if (dst - frame + SCREEN_WIDE < frame_off ||
144 dst - frame + SCREEN_WIDE + frame_off < 0 ||
145 frame_end - dst < frame_off + len ||
146 frame_end - dst < len)
147 return -1;
148 for (i = 0; i < len; i++)
149 dst[i] = dst[frame_off + i];
150 dst += len;
151 } else {
152 dst -= len;
153 if (dst - frame + SCREEN_WIDE < frame_off ||
154 dst - frame + SCREEN_WIDE + frame_off < 0 ||
155 frame_end - dst < frame_off + len ||
156 frame_end - dst < len)
157 return -1;
158 for (i = len - 1; i >= 0; i--)
159 dst[i] = dst[frame_off + i];
160 }
161 break;
162 case 2:
163 if (forward) {
164 if (source + src_len - src < len)
165 return -1;
166 memcpy(dst, src, len);
167 dst += len;
168 src += len;
169 } else {
170 if (src - source < len)
171 return -1;
172 dst -= len;
173 src -= len;
174 memcpy(dst, src, len);
175 }
176 break;
177 case 3:
178 val = forward ? dst[-1] : dst[1];
179 if (forward) {
180 memset(dst, val, len);
181 dst += len;
182 } else {
183 dst -= len;
184 memset(dst, val, len);
185 }
186 break;
187 default:
188 break;
189 }
190 if (dst == dst_end)
191 return 0;
192 }
193 return 0;
194 }
195
196 static int decode_frame(AVCodecContext *avctx, void *data, int *data_size, AVPacket *pkt)
197 {
198 BMVDecContext * const c = avctx->priv_data;
199 int type, scr_off;
200 int i, ret;
201 uint8_t *srcptr, *outptr;
202
203 c->stream = pkt->data;
204 type = bytestream_get_byte(&c->stream);
205 if (type & BMV_AUDIO) {
206 int blobs = bytestream_get_byte(&c->stream);
207 if (pkt->size < blobs * 65 + 2) {
208 av_log(avctx, AV_LOG_ERROR, "Audio data doesn't fit in frame\n");
209 return AVERROR_INVALIDDATA;
210 }
211 c->stream += blobs * 65;
212 }
213 if (type & BMV_COMMAND) {
214 int command_size = (type & BMV_PRINT) ? 8 : 10;
215 if (c->stream - pkt->data + command_size > pkt->size) {
216 av_log(avctx, AV_LOG_ERROR, "Command data doesn't fit in frame\n");
217 return AVERROR_INVALIDDATA;
218 }
219 c->stream += command_size;
220 }
221 if (type & BMV_PALETTE) {
222 if (c->stream - pkt->data > pkt->size - 768) {
223 av_log(avctx, AV_LOG_ERROR, "Palette data doesn't fit in frame\n");
224 return AVERROR_INVALIDDATA;
225 }
226 for (i = 0; i < 256; i++)
227 c->pal[i] = bytestream_get_be24(&c->stream);
228 }
229 if (type & BMV_SCROLL) {
230 if (c->stream - pkt->data > pkt->size - 2) {
231 av_log(avctx, AV_LOG_ERROR, "Screen offset data doesn't fit in frame\n");
232 return AVERROR_INVALIDDATA;
233 }
234 scr_off = (int16_t)bytestream_get_le16(&c->stream);
235 } else if ((type & BMV_INTRA) == BMV_INTRA) {
236 scr_off = -640;
237 } else {
238 scr_off = 0;
239 }
240
241 if (c->pic.data[0])
242 avctx->release_buffer(avctx, &c->pic);
243
244 c->pic.reference = 3;
245 if ((ret = avctx->get_buffer(avctx, &c->pic)) < 0) {
246 av_log(avctx, AV_LOG_ERROR, "get_buffer() failed\n");
247 return ret;
248 }
249
250 if (decode_bmv_frame(c->stream, pkt->size - (c->stream - pkt->data), c->frame, scr_off)) {
251 av_log(avctx, AV_LOG_ERROR, "Error decoding frame data\n");
252 return AVERROR_INVALIDDATA;
253 }
254
255 memcpy(c->pic.data[1], c->pal, AVPALETTE_SIZE);
256 c->pic.palette_has_changed = type & BMV_PALETTE;
257
258 outptr = c->pic.data[0];
259 srcptr = c->frame;
260
261 for (i = 0; i < avctx->height; i++) {
262 memcpy(outptr, srcptr, avctx->width);
263 srcptr += avctx->width;
264 outptr += c->pic.linesize[0];
265 }
266
267 *data_size = sizeof(AVFrame);
268 *(AVFrame*)data = c->pic;
269
270 /* always report that the buffer was completely consumed */
271 return pkt->size;
272 }
273
274 static av_cold int decode_init(AVCodecContext *avctx)
275 {
276 BMVDecContext * const c = avctx->priv_data;
277
278 c->avctx = avctx;
279 avctx->pix_fmt = AV_PIX_FMT_PAL8;
280
281 c->frame = c->frame_base + 640;
282
283 return 0;
284 }
285
286 static av_cold int decode_end(AVCodecContext *avctx)
287 {
288 BMVDecContext *c = avctx->priv_data;
289
290 if (c->pic.data[0])
291 avctx->release_buffer(avctx, &c->pic);
292
293 return 0;
294 }
295
296 typedef struct BMVAudioDecContext {
297 AVFrame frame;
298 } BMVAudioDecContext;
299
300 static const int bmv_aud_mults[16] = {
301 16512, 8256, 4128, 2064, 1032, 516, 258, 192, 129, 88, 64, 56, 48, 40, 36, 32
302 };
303
304 static av_cold int bmv_aud_decode_init(AVCodecContext *avctx)
305 {
306 BMVAudioDecContext *c = avctx->priv_data;
307
308 avctx->channels = 2;
309 avctx->channel_layout = AV_CH_LAYOUT_STEREO;
310 avctx->sample_fmt = AV_SAMPLE_FMT_S16;
311
312 avcodec_get_frame_defaults(&c->frame);
313 avctx->coded_frame = &c->frame;
314
315 return 0;
316 }
317
318 static int bmv_aud_decode_frame(AVCodecContext *avctx, void *data,
319 int *got_frame_ptr, AVPacket *avpkt)
320 {
321 BMVAudioDecContext *c = avctx->priv_data;
322 const uint8_t *buf = avpkt->data;
323 int buf_size = avpkt->size;
324 int blocks = 0, total_blocks, i;
325 int ret;
326 int16_t *output_samples;
327 int scale[2];
328
329 total_blocks = *buf++;
330 if (buf_size < total_blocks * 65 + 1) {
331 av_log(avctx, AV_LOG_ERROR, "expected %d bytes, got %d\n",
332 total_blocks * 65 + 1, buf_size);
333 return AVERROR_INVALIDDATA;
334 }
335
336 /* get output buffer */
337 c->frame.nb_samples = total_blocks * 32;
338 if ((ret = avctx->get_buffer(avctx, &c->frame)) < 0) {
339 av_log(avctx, AV_LOG_ERROR, "get_buffer() failed\n");
340 return ret;
341 }
342 output_samples = (int16_t *)c->frame.data[0];
343
344 for (blocks = 0; blocks < total_blocks; blocks++) {
345 uint8_t code = *buf++;
346 code = (code >> 1) | (code << 7);
347 scale[0] = bmv_aud_mults[code & 0xF];
348 scale[1] = bmv_aud_mults[code >> 4];
349 for (i = 0; i < 32; i++) {
350 *output_samples++ = av_clip_int16((scale[0] * (int8_t)*buf++) >> 5);
351 *output_samples++ = av_clip_int16((scale[1] * (int8_t)*buf++) >> 5);
352 }
353 }
354
355 *got_frame_ptr = 1;
356 *(AVFrame *)data = c->frame;
357
358 return buf_size;
359 }
360
361 AVCodec ff_bmv_video_decoder = {
362 .name = "bmv_video",
363 .type = AVMEDIA_TYPE_VIDEO,
364 .id = AV_CODEC_ID_BMV_VIDEO,
365 .priv_data_size = sizeof(BMVDecContext),
366 .init = decode_init,
367 .close = decode_end,
368 .decode = decode_frame,
369 .capabilities = CODEC_CAP_DR1,
370 .long_name = NULL_IF_CONFIG_SMALL("Discworld II BMV video"),
371 };
372
373 AVCodec ff_bmv_audio_decoder = {
374 .name = "bmv_audio",
375 .type = AVMEDIA_TYPE_AUDIO,
376 .id = AV_CODEC_ID_BMV_AUDIO,
377 .priv_data_size = sizeof(BMVAudioDecContext),
378 .init = bmv_aud_decode_init,
379 .decode = bmv_aud_decode_frame,
380 .capabilities = CODEC_CAP_DR1,
381 .long_name = NULL_IF_CONFIG_SMALL("Discworld II BMV audio"),
382 };