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"
24 #include "libavutil/avassert.h"
40 #define SCREEN_WIDE 640
41 #define SCREEN_HIGH 429
43 typedef struct BMVDecContext {
44 AVCodecContext *avctx;
47 uint8_t *frame, frame_base[SCREEN_WIDE * (SCREEN_HIGH + 1)];
49 const uint8_t *stream;
52 #define NEXT_BYTE(v) v = forward ? v + 1 : v - 1;
54 static int decode_bmv_frame(const uint8_t *source, int src_len, uint8_t *frame, int frame_off)
56 unsigned val, saved_val = 0;
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;
62 int forward = (frame_off <= -SCREEN_WIDE) || (frame_off >= 0);
63 int read_two_nibbles, flag;
76 src = source + src_len - 1;
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.
91 if (!mode || (tmplen == 4)) {
92 if (src < source || src >= source_end)
104 if (!read_two_nibbles) {
105 if (src < source || src >= source_end)
108 val |= *src << shift;
112 // two upper bits of the nibble is zero,
113 // so shift top nibble value down into their place
114 read_two_nibbles = 0;
116 mask = (1 << shift) - 1;
117 val = ((val >> 2) & ~mask) | (val & mask);
119 if ((val & (0xC << shift))) {
130 saved_val = val >> (4 + shift);
132 val &= (1 << (shift + 4)) - 1;
135 advance_mode = val & 1;
136 len = (val >> 1) - 1;
138 mode += 1 + advance_mode;
141 if (FFABS(dst_end - dst) < len)
146 if (dst - frame + SCREEN_WIDE < frame_off ||
147 dst - frame + SCREEN_WIDE + frame_off < 0 ||
148 frame_end - dst < frame_off + len ||
149 frame_end - dst < len)
151 for (i = 0; i < len; i++)
152 dst[i] = dst[frame_off + i];
156 if (dst - frame + SCREEN_WIDE < frame_off ||
157 dst - frame + SCREEN_WIDE + frame_off < 0 ||
158 frame_end - dst < frame_off + len ||
159 frame_end - dst < len)
161 for (i = len - 1; i >= 0; i--)
162 dst[i] = dst[frame_off + i];
167 if (source + src_len - src < len)
169 memcpy(dst, src, len);
173 if (src - source < len)
177 memcpy(dst, src, len);
181 val = forward ? dst[-1] : dst[1];
183 memset(dst, val, len);
187 memset(dst, val, len);
199 static int decode_frame(AVCodecContext *avctx, void *data, int *data_size, AVPacket *pkt)
201 BMVDecContext * const c = avctx->priv_data;
204 uint8_t *srcptr, *outptr;
206 c->stream = pkt->data;
207 type = bytestream_get_byte(&c->stream);
208 if (type & BMV_AUDIO) {
209 int blobs = bytestream_get_byte(&c->stream);
210 if (pkt->size < blobs * 65 + 2) {
211 av_log(avctx, AV_LOG_ERROR, "Audio data doesn't fit in frame\n");
212 return AVERROR_INVALIDDATA;
214 c->stream += blobs * 65;
216 if (type & BMV_COMMAND) {
217 int command_size = (type & BMV_PRINT) ? 8 : 10;
218 if (c->stream - pkt->data + command_size > pkt->size) {
219 av_log(avctx, AV_LOG_ERROR, "Command data doesn't fit in frame\n");
220 return AVERROR_INVALIDDATA;
222 c->stream += command_size;
224 if (type & BMV_PALETTE) {
225 if (c->stream - pkt->data > pkt->size - 768) {
226 av_log(avctx, AV_LOG_ERROR, "Palette data doesn't fit in frame\n");
227 return AVERROR_INVALIDDATA;
229 for (i = 0; i < 256; i++)
230 c->pal[i] = 0xFF << 24 | bytestream_get_be24(&c->stream);
232 if (type & BMV_SCROLL) {
233 if (c->stream - pkt->data > pkt->size - 2) {
234 av_log(avctx, AV_LOG_ERROR, "Screen offset data doesn't fit in frame\n");
235 return AVERROR_INVALIDDATA;
237 scr_off = (int16_t)bytestream_get_le16(&c->stream);
238 } else if ((type & BMV_INTRA) == BMV_INTRA) {
244 if (decode_bmv_frame(c->stream, pkt->size - (c->stream - pkt->data), c->frame, scr_off)) {
245 av_log(avctx, AV_LOG_ERROR, "Error decoding frame data\n");
246 return AVERROR_INVALIDDATA;
249 memcpy(c->pic.data[1], c->pal, AVPALETTE_SIZE);
250 c->pic.palette_has_changed = type & BMV_PALETTE;
252 outptr = c->pic.data[0];
255 for (i = 0; i < avctx->height; i++) {
256 memcpy(outptr, srcptr, avctx->width);
257 srcptr += avctx->width;
258 outptr += c->pic.linesize[0];
261 *data_size = sizeof(AVFrame);
262 *(AVFrame*)data = c->pic;
264 /* always report that the buffer was completely consumed */
268 static av_cold int decode_init(AVCodecContext *avctx)
270 BMVDecContext * const c = avctx->priv_data;
273 avctx->pix_fmt = PIX_FMT_PAL8;
275 if (avctx->width != SCREEN_WIDE || avctx->height != SCREEN_HIGH) {
276 av_log(avctx, AV_LOG_ERROR, "Invalid dimension %dx%d\n", avctx->width, avctx->height);
277 return AVERROR_INVALIDDATA;
280 c->pic.reference = 1;
281 if (avctx->get_buffer(avctx, &c->pic) < 0) {
282 av_log(avctx, AV_LOG_ERROR, "get_buffer() failed\n");
286 c->frame = c->frame_base + 640;
291 static av_cold int decode_end(AVCodecContext *avctx)
293 BMVDecContext *c = avctx->priv_data;
296 avctx->release_buffer(avctx, &c->pic);
301 typedef struct BMVAudioDecContext {
303 } BMVAudioDecContext;
305 static const int bmv_aud_mults[16] = {
306 16512, 8256, 4128, 2064, 1032, 516, 258, 192, 129, 88, 64, 56, 48, 40, 36, 32
309 static av_cold int bmv_aud_decode_init(AVCodecContext *avctx)
311 BMVAudioDecContext *c = avctx->priv_data;
313 if (avctx->channels != 2) {
314 av_log(avctx, AV_LOG_INFO, "invalid number of channels\n");
315 return AVERROR(EINVAL);
318 avctx->sample_fmt = AV_SAMPLE_FMT_S16;
320 avcodec_get_frame_defaults(&c->frame);
321 avctx->coded_frame = &c->frame;
326 static int bmv_aud_decode_frame(AVCodecContext *avctx, void *data,
327 int *got_frame_ptr, AVPacket *avpkt)
329 BMVAudioDecContext *c = avctx->priv_data;
330 const uint8_t *buf = avpkt->data;
331 int buf_size = avpkt->size;
332 int blocks = 0, total_blocks, i;
334 int16_t *output_samples;
337 total_blocks = *buf++;
338 if (buf_size < total_blocks * 65 + 1) {
339 av_log(avctx, AV_LOG_ERROR, "expected %d bytes, got %d\n",
340 total_blocks * 65 + 1, buf_size);
341 return AVERROR_INVALIDDATA;
344 /* get output buffer */
345 c->frame.nb_samples = total_blocks * 32;
346 if ((ret = avctx->get_buffer(avctx, &c->frame)) < 0) {
347 av_log(avctx, AV_LOG_ERROR, "get_buffer() failed\n");
350 output_samples = (int16_t *)c->frame.data[0];
352 for (blocks = 0; blocks < total_blocks; blocks++) {
353 uint8_t code = *buf++;
354 code = (code >> 1) | (code << 7);
355 scale[0] = bmv_aud_mults[code & 0xF];
356 scale[1] = bmv_aud_mults[code >> 4];
357 for (i = 0; i < 32; i++) {
358 *output_samples++ = av_clip_int16((scale[0] * (int8_t)*buf++) >> 5);
359 *output_samples++ = av_clip_int16((scale[1] * (int8_t)*buf++) >> 5);
364 *(AVFrame *)data = c->frame;
369 AVCodec ff_bmv_video_decoder = {
371 .type = AVMEDIA_TYPE_VIDEO,
372 .id = AV_CODEC_ID_BMV_VIDEO,
373 .priv_data_size = sizeof(BMVDecContext),
376 .decode = decode_frame,
377 .capabilities = CODEC_CAP_DR1,
378 .long_name = NULL_IF_CONFIG_SMALL("Discworld II BMV video"),
381 AVCodec ff_bmv_audio_decoder = {
383 .type = AVMEDIA_TYPE_AUDIO,
384 .id = AV_CODEC_ID_BMV_AUDIO,
385 .priv_data_size = sizeof(BMVAudioDecContext),
386 .init = bmv_aud_decode_init,
387 .decode = bmv_aud_decode_frame,
388 .capabilities = CODEC_CAP_DR1,
389 .long_name = NULL_IF_CONFIG_SMALL("Discworld II BMV audio"),