Libav
bmv.c
Go to the documentation of this file.
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 
23 #include "avcodec.h"
24 #include "bytestream.h"
25 #include "internal.h"
26 
27 enum BMVFlags{
28  BMV_NOP = 0,
32 
33  BMV_SCROLL = 0x04,
34  BMV_PALETTE = 0x08,
35  BMV_COMMAND = 0x10,
36  BMV_AUDIO = 0x20,
37  BMV_EXT = 0x40,
38  BMV_PRINT = 0x80
39 };
40 
41 #define SCREEN_WIDE 640
42 #define SCREEN_HIGH 429
43 
44 typedef struct BMVDecContext {
46 
48  uint32_t pal[256];
49  const uint8_t *stream;
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;
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 AVERROR_INVALIDDATA;
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 AVERROR_INVALIDDATA;
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 AVERROR_INVALIDDATA;
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 (len <= 0 || FFABS(dst_end - dst) < len)
139  return AVERROR_INVALIDDATA;
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 AVERROR_INVALIDDATA;
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 AVERROR_INVALIDDATA;
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 AVERROR_INVALIDDATA;
166  memcpy(dst, src, len);
167  dst += len;
168  src += len;
169  } else {
170  if (src - source < len)
171  return AVERROR_INVALIDDATA;
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 *got_frame,
197  AVPacket *pkt)
198 {
199  BMVDecContext * const c = avctx->priv_data;
200  AVFrame *frame = data;
201  int type, scr_off;
202  int i, ret;
203  uint8_t *srcptr, *outptr;
204 
205  c->stream = pkt->data;
206  type = bytestream_get_byte(&c->stream);
207  if (type & BMV_AUDIO) {
208  int blobs = bytestream_get_byte(&c->stream);
209  if (pkt->size < blobs * 65 + 2) {
210  av_log(avctx, AV_LOG_ERROR, "Audio data doesn't fit in frame\n");
211  return AVERROR_INVALIDDATA;
212  }
213  c->stream += blobs * 65;
214  }
215  if (type & BMV_COMMAND) {
216  int command_size = (type & BMV_PRINT) ? 8 : 10;
217  if (c->stream - pkt->data + command_size > pkt->size) {
218  av_log(avctx, AV_LOG_ERROR, "Command data doesn't fit in frame\n");
219  return AVERROR_INVALIDDATA;
220  }
221  c->stream += command_size;
222  }
223  if (type & BMV_PALETTE) {
224  if (c->stream - pkt->data > pkt->size - 768) {
225  av_log(avctx, AV_LOG_ERROR, "Palette data doesn't fit in frame\n");
226  return AVERROR_INVALIDDATA;
227  }
228  for (i = 0; i < 256; i++)
229  c->pal[i] = bytestream_get_be24(&c->stream);
230  }
231  if (type & BMV_SCROLL) {
232  if (c->stream - pkt->data > pkt->size - 2) {
233  av_log(avctx, AV_LOG_ERROR, "Screen offset data doesn't fit in frame\n");
234  return AVERROR_INVALIDDATA;
235  }
236  scr_off = (int16_t)bytestream_get_le16(&c->stream);
237  } else if ((type & BMV_INTRA) == BMV_INTRA) {
238  scr_off = -640;
239  } else {
240  scr_off = 0;
241  }
242 
243  if ((ret = ff_get_buffer(avctx, frame, 0)) < 0) {
244  av_log(avctx, AV_LOG_ERROR, "get_buffer() failed\n");
245  return ret;
246  }
247 
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;
251  }
252 
253  memcpy(frame->data[1], c->pal, AVPALETTE_SIZE);
254  frame->palette_has_changed = type & BMV_PALETTE;
255 
256  outptr = frame->data[0];
257  srcptr = c->frame;
258 
259  for (i = 0; i < avctx->height; i++) {
260  memcpy(outptr, srcptr, avctx->width);
261  srcptr += avctx->width;
262  outptr += frame->linesize[0];
263  }
264 
265  *got_frame = 1;
266 
267  /* always report that the buffer was completely consumed */
268  return pkt->size;
269 }
270 
272 {
273  BMVDecContext * const c = avctx->priv_data;
274 
275  c->avctx = avctx;
276  avctx->pix_fmt = AV_PIX_FMT_PAL8;
277 
278  c->frame = c->frame_base + 640;
279 
280  return 0;
281 }
282 
283 static const int bmv_aud_mults[16] = {
284  16512, 8256, 4128, 2064, 1032, 516, 258, 192, 129, 88, 64, 56, 48, 40, 36, 32
285 };
286 
288 {
289  avctx->channels = 2;
291  avctx->sample_fmt = AV_SAMPLE_FMT_S16;
292 
293  return 0;
294 }
295 
296 static int bmv_aud_decode_frame(AVCodecContext *avctx, void *data,
297  int *got_frame_ptr, AVPacket *avpkt)
298 {
299  AVFrame *frame = data;
300  const uint8_t *buf = avpkt->data;
301  int buf_size = avpkt->size;
302  int blocks = 0, total_blocks, i;
303  int ret;
304  int16_t *output_samples;
305  int scale[2];
306 
307  total_blocks = *buf++;
308  if (buf_size < total_blocks * 65 + 1) {
309  av_log(avctx, AV_LOG_ERROR, "expected %d bytes, got %d\n",
310  total_blocks * 65 + 1, buf_size);
311  return AVERROR_INVALIDDATA;
312  }
313 
314  /* get output buffer */
315  frame->nb_samples = total_blocks * 32;
316  if ((ret = ff_get_buffer(avctx, frame, 0)) < 0) {
317  av_log(avctx, AV_LOG_ERROR, "get_buffer() failed\n");
318  return ret;
319  }
320  output_samples = (int16_t *)frame->data[0];
321 
322  for (blocks = 0; blocks < total_blocks; blocks++) {
323  uint8_t code = *buf++;
324  code = (code >> 1) | (code << 7);
325  scale[0] = bmv_aud_mults[code & 0xF];
326  scale[1] = bmv_aud_mults[code >> 4];
327  for (i = 0; i < 32; i++) {
328  *output_samples++ = av_clip_int16((scale[0] * (int8_t)*buf++) >> 5);
329  *output_samples++ = av_clip_int16((scale[1] * (int8_t)*buf++) >> 5);
330  }
331  }
332 
333  *got_frame_ptr = 1;
334 
335  return buf_size;
336 }
337 
339  .name = "bmv_video",
340  .long_name = NULL_IF_CONFIG_SMALL("Discworld II BMV video"),
341  .type = AVMEDIA_TYPE_VIDEO,
342  .id = AV_CODEC_ID_BMV_VIDEO,
343  .priv_data_size = sizeof(BMVDecContext),
344  .init = decode_init,
345  .decode = decode_frame,
346  .capabilities = CODEC_CAP_DR1,
347 };
348 
350  .name = "bmv_audio",
351  .long_name = NULL_IF_CONFIG_SMALL("Discworld II BMV audio"),
352  .type = AVMEDIA_TYPE_AUDIO,
353  .id = AV_CODEC_ID_BMV_AUDIO,
354  .init = bmv_aud_decode_init,
355  .decode = bmv_aud_decode_frame,
356  .capabilities = CODEC_CAP_DR1,
357 };