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
22 #include "libavutil/avassert.h"
23 #include "libavutil/channel_layout.h"
25 #include "bytestream.h"
42 #define SCREEN_WIDE 640
43 #define SCREEN_HIGH 429
45 typedef struct BMVDecContext {
46 AVCodecContext *avctx;
48 uint8_t *frame, frame_base[SCREEN_WIDE * (SCREEN_HIGH + 1)];
50 const uint8_t *stream;
53 #define NEXT_BYTE(v) v = forward ? v + 1 : v - 1;
55 static int decode_bmv_frame(const uint8_t *source, int src_len, uint8_t *frame, int frame_off)
57 unsigned val, saved_val = 0;
59 const uint8_t *src, *source_end = source + src_len;
60 uint8_t *frame_end = frame + SCREEN_WIDE * SCREEN_HIGH;
61 uint8_t *dst, *dst_end;
63 int forward = (frame_off <= -SCREEN_WIDE) || (frame_off >= 0);
64 int read_two_nibbles, flag;
70 return AVERROR_INVALIDDATA;
77 src = source + src_len - 1;
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.
92 if (!mode || (tmplen == 4)) {
93 if (src < source || src >= source_end)
94 return AVERROR_INVALIDDATA;
105 if (!read_two_nibbles) {
106 if (src < source || src >= source_end)
107 return AVERROR_INVALIDDATA;
109 val |= *src << shift;
113 // two upper bits of the nibble is zero,
114 // so shift top nibble value down into their place
115 read_two_nibbles = 0;
117 mask = (1 << shift) - 1;
118 val = ((val >> 2) & ~mask) | (val & mask);
120 if ((val & (0xC << shift))) {
131 saved_val = val >> (4 + shift);
133 val &= (1 << (shift + 4)) - 1;
136 advance_mode = val & 1;
137 len = (val >> 1) - 1;
139 mode += 1 + advance_mode;
142 if (len <= 0 || FFABS(dst_end - dst) < len)
143 return AVERROR_INVALIDDATA;
147 if (dst - frame + SCREEN_WIDE < frame_off ||
148 dst - frame + SCREEN_WIDE + frame_off < 0 ||
149 frame_end - dst < frame_off + len ||
150 frame_end - dst < len)
151 return AVERROR_INVALIDDATA;
152 for (i = 0; i < len; i++)
153 dst[i] = dst[frame_off + i];
157 if (dst - frame + SCREEN_WIDE < frame_off ||
158 dst - frame + SCREEN_WIDE + frame_off < 0 ||
159 frame_end - dst < frame_off + len ||
160 frame_end - dst < len)
161 return AVERROR_INVALIDDATA;
162 for (i = len - 1; i >= 0; i--)
163 dst[i] = dst[frame_off + i];
168 if (source + src_len - src < len)
169 return AVERROR_INVALIDDATA;
170 memcpy(dst, src, len);
174 if (src - source < len)
175 return AVERROR_INVALIDDATA;
178 memcpy(dst, src, len);
182 val = forward ? dst[-1] : dst[1];
184 memset(dst, val, len);
188 memset(dst, val, len);
198 static int decode_frame(AVCodecContext *avctx, void *data, int *got_frame,
201 BMVDecContext * const c = avctx->priv_data;
202 AVFrame *frame = data;
205 uint8_t *srcptr, *outptr;
207 c->stream = pkt->data;
208 type = bytestream_get_byte(&c->stream);
209 if (type & BMV_AUDIO) {
210 int blobs = bytestream_get_byte(&c->stream);
211 if (pkt->size < blobs * 65 + 2) {
212 av_log(avctx, AV_LOG_ERROR, "Audio data doesn't fit in frame\n");
213 return AVERROR_INVALIDDATA;
215 c->stream += blobs * 65;
217 if (type & BMV_COMMAND) {
218 int command_size = (type & BMV_PRINT) ? 8 : 10;
219 if (c->stream - pkt->data + command_size > pkt->size) {
220 av_log(avctx, AV_LOG_ERROR, "Command data doesn't fit in frame\n");
221 return AVERROR_INVALIDDATA;
223 c->stream += command_size;
225 if (type & BMV_PALETTE) {
226 if (c->stream - pkt->data > pkt->size - 768) {
227 av_log(avctx, AV_LOG_ERROR, "Palette data doesn't fit in frame\n");
228 return AVERROR_INVALIDDATA;
230 for (i = 0; i < 256; i++)
231 c->pal[i] = 0xFFU << 24 | bytestream_get_be24(&c->stream);
233 if (type & BMV_SCROLL) {
234 if (c->stream - pkt->data > pkt->size - 2) {
235 av_log(avctx, AV_LOG_ERROR, "Screen offset data doesn't fit in frame\n");
236 return AVERROR_INVALIDDATA;
238 scr_off = (int16_t)bytestream_get_le16(&c->stream);
239 } else if ((type & BMV_INTRA) == BMV_INTRA) {
245 if ((ret = ff_get_buffer(avctx, frame, 0)) < 0)
248 if (decode_bmv_frame(c->stream, pkt->size - (c->stream - pkt->data), c->frame, scr_off)) {
249 av_log(avctx, AV_LOG_ERROR, "Error decoding frame data\n");
250 return AVERROR_INVALIDDATA;
253 memcpy(frame->data[1], c->pal, AVPALETTE_SIZE);
254 frame->palette_has_changed = type & BMV_PALETTE;
256 outptr = frame->data[0];
259 for (i = 0; i < avctx->height; i++) {
260 memcpy(outptr, srcptr, avctx->width);
261 srcptr += avctx->width;
262 outptr += frame->linesize[0];
267 /* always report that the buffer was completely consumed */
271 static av_cold int decode_init(AVCodecContext *avctx)
273 BMVDecContext * const c = avctx->priv_data;
276 avctx->pix_fmt = AV_PIX_FMT_PAL8;
278 if (avctx->width != SCREEN_WIDE || avctx->height != SCREEN_HIGH) {
279 av_log(avctx, AV_LOG_ERROR, "Invalid dimension %dx%d\n", avctx->width, avctx->height);
280 return AVERROR_INVALIDDATA;
283 c->frame = c->frame_base + 640;
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 avctx->channel_layout = AV_CH_LAYOUT_STEREO;
296 avctx->sample_fmt = AV_SAMPLE_FMT_S16;
301 static int bmv_aud_decode_frame(AVCodecContext *avctx, void *data,
302 int *got_frame_ptr, AVPacket *avpkt)
304 AVFrame *frame = data;
305 const uint8_t *buf = avpkt->data;
306 int buf_size = avpkt->size;
307 int blocks = 0, total_blocks, i;
309 int16_t *output_samples;
312 total_blocks = *buf++;
313 if (buf_size < total_blocks * 65 + 1) {
314 av_log(avctx, AV_LOG_ERROR, "expected %d bytes, got %d\n",
315 total_blocks * 65 + 1, buf_size);
316 return AVERROR_INVALIDDATA;
319 /* get output buffer */
320 frame->nb_samples = total_blocks * 32;
321 if ((ret = ff_get_buffer(avctx, frame, 0)) < 0)
323 output_samples = (int16_t *)frame->data[0];
325 for (blocks = 0; blocks < total_blocks; blocks++) {
326 uint8_t code = *buf++;
327 code = (code >> 1) | (code << 7);
328 scale[0] = bmv_aud_mults[code & 0xF];
329 scale[1] = bmv_aud_mults[code >> 4];
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);
341 AVCodec ff_bmv_video_decoder = {
343 .type = AVMEDIA_TYPE_VIDEO,
344 .id = AV_CODEC_ID_BMV_VIDEO,
345 .priv_data_size = sizeof(BMVDecContext),
347 .decode = decode_frame,
348 .capabilities = CODEC_CAP_DR1,
349 .long_name = NULL_IF_CONFIG_SMALL("Discworld II BMV video"),
352 AVCodec ff_bmv_audio_decoder = {
354 .type = AVMEDIA_TYPE_AUDIO,
355 .id = AV_CODEC_ID_BMV_AUDIO,
356 .init = bmv_aud_decode_init,
357 .decode = bmv_aud_decode_frame,
358 .capabilities = CODEC_CAP_DR1,
359 .long_name = NULL_IF_CONFIG_SMALL("Discworld II BMV audio"),