2 * Discworld II BMV video and audio decoder
3 * Copyright (c) 2011 Konstantin Shishkov
5 * This file is part of Libav.
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.
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.
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
23 #include "bytestream.h"
39 #define SCREEN_WIDE 640
40 #define SCREEN_HIGH 429
42 typedef struct BMVDecContext {
43 AVCodecContext *avctx;
46 uint8_t *frame, frame_base[SCREEN_WIDE * (SCREEN_HIGH + 1)];
48 const uint8_t *stream;
51 #define NEXT_BYTE(v) v = forward ? v + 1 : v - 1;
53 static int decode_bmv_frame(const uint8_t *source, int src_len, uint8_t *frame, int frame_off)
55 int val, saved_val = 0;
57 const uint8_t *src, *source_end = source + src_len;
58 uint8_t *frame_end = frame + SCREEN_WIDE * SCREEN_HIGH;
59 uint8_t *dst, *dst_end;
61 int forward = (frame_off <= -SCREEN_WIDE) || (frame_off >= 0);
62 int read_two_nibbles, flag;
75 src = source + src_len - 1;
83 /* The mode/len decoding is a bit strange:
84 * values are coded as variable-length codes with nibble units,
85 * code end is signalled by two top bits in the nibble being nonzero.
86 * And since data is bytepacked and we read two nibbles at a time,
87 * we may get a nibble belonging to the next code.
88 * Hence this convoluted loop.
90 if (!mode || (tmplen == 4)) {
91 if (src < source || src >= source_end)
101 if (!read_two_nibbles) {
102 if (src < source || src >= source_end)
105 val |= *src << shift;
109 // two upper bits of the nibble is zero,
110 // so shift top nibble value down into their place
111 read_two_nibbles = 0;
113 mask = (1 << shift) - 1;
114 val = ((val >> 2) & ~mask) | (val & mask);
116 if ((val & (0xC << shift))) {
127 saved_val = val >> (4 + shift);
129 val &= (1 << (shift + 4)) - 1;
132 advance_mode = val & 1;
133 len = (val >> 1) - 1;
134 mode += 1 + advance_mode;
137 if (FFABS(dst_end - dst) < len)
142 if (dst - frame + SCREEN_WIDE < frame_off ||
143 frame_end - dst < frame_off + len)
145 for (i = 0; i < len; i++)
146 dst[i] = dst[frame_off + i];
150 if (dst - frame + SCREEN_WIDE < frame_off ||
151 frame_end - dst < frame_off + len)
153 for (i = len - 1; i >= 0; i--)
154 dst[i] = dst[frame_off + i];
159 if (source + src_len - src < len)
161 memcpy(dst, src, len);
165 if (src - source < len)
169 memcpy(dst, src, len);
173 val = forward ? dst[-1] : dst[1];
175 memset(dst, val, len);
179 memset(dst, val, len);
191 static int decode_frame(AVCodecContext *avctx, void *data, int *data_size, AVPacket *pkt)
193 BMVDecContext * const c = avctx->priv_data;
196 uint8_t *srcptr, *outptr;
198 c->stream = pkt->data;
199 type = bytestream_get_byte(&c->stream);
200 if (type & BMV_AUDIO) {
201 int blobs = bytestream_get_byte(&c->stream);
202 if (pkt->size < blobs * 65 + 2) {
203 av_log(avctx, AV_LOG_ERROR, "Audio data doesn't fit in frame\n");
204 return AVERROR_INVALIDDATA;
206 c->stream += blobs * 65;
208 if (type & BMV_COMMAND) {
209 int command_size = (type & BMV_PRINT) ? 8 : 10;
210 if (c->stream - pkt->data + command_size > pkt->size) {
211 av_log(avctx, AV_LOG_ERROR, "Command data doesn't fit in frame\n");
212 return AVERROR_INVALIDDATA;
214 c->stream += command_size;
216 if (type & BMV_PALETTE) {
217 if (c->stream - pkt->data > pkt->size - 768) {
218 av_log(avctx, AV_LOG_ERROR, "Palette data doesn't fit in frame\n");
219 return AVERROR_INVALIDDATA;
221 for (i = 0; i < 256; i++)
222 c->pal[i] = bytestream_get_be24(&c->stream);
224 if (type & BMV_SCROLL) {
225 if (c->stream - pkt->data > pkt->size - 2) {
226 av_log(avctx, AV_LOG_ERROR, "Screen offset data doesn't fit in frame\n");
227 return AVERROR_INVALIDDATA;
229 scr_off = (int16_t)bytestream_get_le16(&c->stream);
230 } else if ((type & BMV_INTRA) == BMV_INTRA) {
236 if (decode_bmv_frame(c->stream, pkt->size - (c->stream - pkt->data), c->frame, scr_off)) {
237 av_log(avctx, AV_LOG_ERROR, "Error decoding frame data\n");
238 return AVERROR_INVALIDDATA;
241 memcpy(c->pic.data[1], c->pal, AVPALETTE_SIZE);
242 c->pic.palette_has_changed = type & BMV_PALETTE;
244 outptr = c->pic.data[0];
247 for (i = 0; i < avctx->height; i++) {
248 memcpy(outptr, srcptr, avctx->width);
249 srcptr += avctx->width;
250 outptr += c->pic.linesize[0];
253 *data_size = sizeof(AVFrame);
254 *(AVFrame*)data = c->pic;
256 /* always report that the buffer was completely consumed */
260 static av_cold int decode_init(AVCodecContext *avctx)
262 BMVDecContext * const c = avctx->priv_data;
265 avctx->pix_fmt = PIX_FMT_PAL8;
267 c->pic.reference = 1;
268 if (avctx->get_buffer(avctx, &c->pic) < 0) {
269 av_log(avctx, AV_LOG_ERROR, "get_buffer() failed\n");
273 c->frame = c->frame_base + 640;
278 static av_cold int decode_end(AVCodecContext *avctx)
280 BMVDecContext *c = avctx->priv_data;
283 avctx->release_buffer(avctx, &c->pic);
288 static const int bmv_aud_mults[16] = {
289 16512, 8256, 4128, 2064, 1032, 516, 258, 192, 129, 88, 64, 56, 48, 40, 36, 32
292 static av_cold int bmv_aud_decode_init(AVCodecContext *avctx)
295 if (avctx->channels != 2) {
296 av_log(avctx, AV_LOG_INFO, "invalid number of channels\n");
297 return AVERROR(EINVAL);
300 avctx->sample_fmt = AV_SAMPLE_FMT_S16;
305 static int bmv_aud_decode_frame(AVCodecContext *avctx, void *data, int *data_size,
308 const uint8_t *buf = avpkt->data;
309 int buf_size = avpkt->size;
310 int blocks = 0, total_blocks, i;
312 int16_t *output_samples = data;
315 total_blocks = *buf++;
316 if (buf_size < total_blocks * 65 + 1) {
317 av_log(avctx, AV_LOG_ERROR, "expected %d bytes, got %d\n",
318 total_blocks * 65 + 1, buf_size);
319 return AVERROR_INVALIDDATA;
321 out_size = total_blocks * 64 * sizeof(*output_samples);
322 if (*data_size < out_size) {
323 av_log(avctx, AV_LOG_ERROR, "Output buffer is too small\n");
324 return AVERROR(EINVAL);
327 for (blocks = 0; blocks < total_blocks; blocks++) {
328 uint8_t code = *buf++;
329 code = (code >> 1) | (code << 7);
330 scale[0] = bmv_aud_mults[code & 0xF];
331 scale[1] = bmv_aud_mults[code >> 4];
332 for (i = 0; i < 32; i++) {
333 *output_samples++ = av_clip_int16((scale[0] * (int8_t)*buf++) >> 5);
334 *output_samples++ = av_clip_int16((scale[1] * (int8_t)*buf++) >> 5);
338 *data_size = out_size;
342 AVCodec ff_bmv_video_decoder = {
344 .type = AVMEDIA_TYPE_VIDEO,
345 .id = CODEC_ID_BMV_VIDEO,
346 .priv_data_size = sizeof(BMVDecContext),
349 .decode = decode_frame,
350 .long_name = NULL_IF_CONFIG_SMALL("Discworld II BMV video"),
353 AVCodec ff_bmv_audio_decoder = {
355 .type = AVMEDIA_TYPE_AUDIO,
356 .id = CODEC_ID_BMV_AUDIO,
357 .init = bmv_aud_decode_init,
358 .decode = bmv_aud_decode_frame,
359 .long_name = NULL_IF_CONFIG_SMALL("Discworld II BMV audio"),