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 
22 #include "avcodec.h"
23 #include "internal.h"
24 #include "bytestream.h"
25 
26 enum BMVFlags{
27  BMV_NOP = 0,
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 {
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  int 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 (len <= 0 || 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  frame_end - dst < frame_off + len)
145  return -1;
146  for (i = 0; i < len; i++)
147  dst[i] = dst[frame_off + i];
148  dst += len;
149  } else {
150  dst -= len;
151  if (dst - frame + SCREEN_WIDE < frame_off ||
152  frame_end - dst < frame_off + len)
153  return -1;
154  for (i = len - 1; i >= 0; i--)
155  dst[i] = dst[frame_off + i];
156  }
157  break;
158  case 2:
159  if (forward) {
160  if (source + src_len - src < len)
161  return -1;
162  memcpy(dst, src, len);
163  dst += len;
164  src += len;
165  } else {
166  if (src - source < len)
167  return -1;
168  dst -= len;
169  src -= len;
170  memcpy(dst, src, len);
171  }
172  break;
173  case 3:
174  val = forward ? dst[-1] : dst[1];
175  if (forward) {
176  memset(dst, val, len);
177  dst += len;
178  } else {
179  dst -= len;
180  memset(dst, val, len);
181  }
182  break;
183  default:
184  break;
185  }
186  if (dst == dst_end)
187  return 0;
188  }
189  return 0;
190 }
191 
192 static int decode_frame(AVCodecContext *avctx, void *data, int *data_size, AVPacket *pkt)
193 {
194  BMVDecContext * const c = avctx->priv_data;
195  int type, scr_off;
196  int i;
197  uint8_t *srcptr, *outptr;
198 
199  c->stream = pkt->data;
200  type = bytestream_get_byte(&c->stream);
201  if (type & BMV_AUDIO) {
202  int blobs = bytestream_get_byte(&c->stream);
203  if (pkt->size < blobs * 65 + 2) {
204  av_log(avctx, AV_LOG_ERROR, "Audio data doesn't fit in frame\n");
205  return AVERROR_INVALIDDATA;
206  }
207  c->stream += blobs * 65;
208  }
209  if (type & BMV_COMMAND) {
210  int command_size = (type & BMV_PRINT) ? 8 : 10;
211  if (c->stream - pkt->data + command_size > pkt->size) {
212  av_log(avctx, AV_LOG_ERROR, "Command data doesn't fit in frame\n");
213  return AVERROR_INVALIDDATA;
214  }
215  c->stream += command_size;
216  }
217  if (type & BMV_PALETTE) {
218  if (c->stream - pkt->data > pkt->size - 768) {
219  av_log(avctx, AV_LOG_ERROR, "Palette data doesn't fit in frame\n");
220  return AVERROR_INVALIDDATA;
221  }
222  for (i = 0; i < 256; i++)
223  c->pal[i] = bytestream_get_be24(&c->stream);
224  }
225  if (type & BMV_SCROLL) {
226  if (c->stream - pkt->data > pkt->size - 2) {
227  av_log(avctx, AV_LOG_ERROR, "Screen offset data doesn't fit in frame\n");
228  return AVERROR_INVALIDDATA;
229  }
230  scr_off = (int16_t)bytestream_get_le16(&c->stream);
231  } else if ((type & BMV_INTRA) == BMV_INTRA) {
232  scr_off = -640;
233  } else {
234  scr_off = 0;
235  }
236 
237  if (decode_bmv_frame(c->stream, pkt->size - (c->stream - pkt->data), c->frame, scr_off)) {
238  av_log(avctx, AV_LOG_ERROR, "Error decoding frame data\n");
239  return AVERROR_INVALIDDATA;
240  }
241 
242  memcpy(c->pic.data[1], c->pal, AVPALETTE_SIZE);
243  c->pic.palette_has_changed = type & BMV_PALETTE;
244 
245  outptr = c->pic.data[0];
246  srcptr = c->frame;
247 
248  for (i = 0; i < avctx->height; i++) {
249  memcpy(outptr, srcptr, avctx->width);
250  srcptr += avctx->width;
251  outptr += c->pic.linesize[0];
252  }
253 
254  *data_size = sizeof(AVFrame);
255  *(AVFrame*)data = c->pic;
256 
257  /* always report that the buffer was completely consumed */
258  return pkt->size;
259 }
260 
262 {
263  BMVDecContext * const c = avctx->priv_data;
264 
265  c->avctx = avctx;
266  avctx->pix_fmt = PIX_FMT_PAL8;
267 
268  c->pic.reference = 1;
269  if (ff_get_buffer(avctx, &c->pic) < 0) {
270  av_log(avctx, AV_LOG_ERROR, "get_buffer() failed\n");
271  return -1;
272  }
273 
274  c->frame = c->frame_base + 640;
275 
276  return 0;
277 }
278 
280 {
281  BMVDecContext *c = avctx->priv_data;
282 
283  if (c->pic.data[0])
284  avctx->release_buffer(avctx, &c->pic);
285 
286  return 0;
287 }
288 
289 typedef struct BMVAudioDecContext {
292 
293 static const int bmv_aud_mults[16] = {
294  16512, 8256, 4128, 2064, 1032, 516, 258, 192, 129, 88, 64, 56, 48, 40, 36, 32
295 };
296 
298 {
299  BMVAudioDecContext *c = avctx->priv_data;
300 
301  if (avctx->channels != 2) {
302  av_log(avctx, AV_LOG_INFO, "invalid number of channels\n");
303  return AVERROR(EINVAL);
304  }
305 
306  avctx->sample_fmt = AV_SAMPLE_FMT_S16;
307 
309  avctx->coded_frame = &c->frame;
310 
311  return 0;
312 }
313 
314 static int bmv_aud_decode_frame(AVCodecContext *avctx, void *data,
315  int *got_frame_ptr, AVPacket *avpkt)
316 {
317  BMVAudioDecContext *c = avctx->priv_data;
318  const uint8_t *buf = avpkt->data;
319  int buf_size = avpkt->size;
320  int blocks = 0, total_blocks, i;
321  int ret;
322  int16_t *output_samples;
323  int scale[2];
324 
325  total_blocks = *buf++;
326  if (buf_size < total_blocks * 65 + 1) {
327  av_log(avctx, AV_LOG_ERROR, "expected %d bytes, got %d\n",
328  total_blocks * 65 + 1, buf_size);
329  return AVERROR_INVALIDDATA;
330  }
331 
332  /* get output buffer */
333  c->frame.nb_samples = total_blocks * 32;
334  if ((ret = ff_get_buffer(avctx, &c->frame)) < 0) {
335  av_log(avctx, AV_LOG_ERROR, "get_buffer() failed\n");
336  return ret;
337  }
338  output_samples = (int16_t *)c->frame.data[0];
339 
340  for (blocks = 0; blocks < total_blocks; blocks++) {
341  uint8_t code = *buf++;
342  code = (code >> 1) | (code << 7);
343  scale[0] = bmv_aud_mults[code & 0xF];
344  scale[1] = bmv_aud_mults[code >> 4];
345  for (i = 0; i < 32; i++) {
346  *output_samples++ = av_clip_int16((scale[0] * (int8_t)*buf++) >> 5);
347  *output_samples++ = av_clip_int16((scale[1] * (int8_t)*buf++) >> 5);
348  }
349  }
350 
351  *got_frame_ptr = 1;
352  *(AVFrame *)data = c->frame;
353 
354  return buf_size;
355 }
356 
358  .name = "bmv_video",
359  .type = AVMEDIA_TYPE_VIDEO,
360  .id = CODEC_ID_BMV_VIDEO,
361  .priv_data_size = sizeof(BMVDecContext),
362  .init = decode_init,
363  .close = decode_end,
364  .decode = decode_frame,
365  .long_name = NULL_IF_CONFIG_SMALL("Discworld II BMV video"),
366 };
367 
369  .name = "bmv_audio",
370  .type = AVMEDIA_TYPE_AUDIO,
371  .id = CODEC_ID_BMV_AUDIO,
372  .priv_data_size = sizeof(BMVAudioDecContext),
375  .capabilities = CODEC_CAP_DR1,
376  .long_name = NULL_IF_CONFIG_SMALL("Discworld II BMV audio"),
377 };
static int bmv_aud_decode_frame(AVCodecContext *avctx, void *data, int *got_frame_ptr, AVPacket *avpkt)
Definition: bmv.c:314
AVCodec ff_bmv_video_decoder
Definition: bmv.c:357
enum PixelFormat pix_fmt
Pixel format, see PIX_FMT_xxx.
Definition: avcodec.h:1426
#define AVERROR_INVALIDDATA
Invalid data found when processing input.
Definition: error.h:54
Audio Video Frame.
Definition: avcodec.h:985
void(* release_buffer)(struct AVCodecContext *c, AVFrame *pic)
Called to release buffers which were allocated with get_buffer.
Definition: avcodec.h:1737
Definition: bmv.c:27
AVFrame * coded_frame
the picture in the bitstream
Definition: avcodec.h:2000
static int decode_frame(AVCodecContext *avctx, void *data, int *data_size, AVPacket *pkt)
Definition: bmv.c:192
AVCodec ff_bmv_audio_decoder
Definition: bmv.c:368
Definition: bmv.c:29
int size
Definition: avcodec.h:909
const uint8_t * stream
Definition: bmv.c:49
signed 16 bits
Definition: samplefmt.h:30
Definition: bmv.c:37
AVCodec.
Definition: avcodec.h:3189
AVCodecContext * avctx
Definition: bmv.c:44
struct BMVDecContext BMVDecContext
AVFrame pic
Definition: bmv.c:45
static int decode(MimicContext *ctx, int quality, int num_coeffs, int is_iframe)
Definition: mimic.c:228
enum AVSampleFormat sample_fmt
audio sample format
Definition: avcodec.h:1464
#define av_cold
Definition: attributes.h:71
const char data[16]
Definition: mxf.c:60
uint8_t * data
Definition: avcodec.h:908
Definition: bmv.c:30
Definition: bmv.c:36
AVFrame frame
Definition: bmv.c:290
static int init(AVCodecParserContext *s)
Definition: h264_parser.c:336
static int decode_bmv_frame(const uint8_t *source, int src_len, uint8_t *frame, int frame_off)
Definition: bmv.c:54
#define CODEC_CAP_DR1
Codec uses get_buffer() for allocating buffers and supports custom allocators.
Definition: avcodec.h:719
static const uint16_t mask[17]
Definition: lzw.c:36
int reference
is this picture used as reference The values for this are the same as the MpegEncContext.picture_structure variable, that is 1->top field, 2->bottom field, 3->frame/both fields.
Definition: avcodec.h:1074
#define AVERROR(e)
Definition: error.h:43
#define NULL_IF_CONFIG_SMALL(x)
Return NULL if CONFIG_SMALL is true, otherwise the argument without modification. ...
Definition: internal.h:191
void av_log(void *avcl, int level, const char *fmt,...)
Definition: log.c:140
const char * name
Name of the codec implementation.
Definition: avcodec.h:3196
#define NEXT_BYTE(v)
Definition: bmv.c:52
#define SCREEN_HIGH
Definition: bmv.c:41
int width
picture width / height.
Definition: avcodec.h:1408
struct AVFrame AVFrame
Audio Video Frame.
static av_cold int decode_init(AVCodecContext *avctx)
Definition: bmv.c:261
int ff_get_buffer(AVCodecContext *avctx, AVFrame *frame)
Definition: utils.c:1867
static const int bmv_aud_mults[16]
Definition: bmv.c:293
#define FFABS(a)
Definition: common.h:50
Definition: bmv.c:32
Definition: bmv.c:28
#define SCREEN_WIDE
Definition: bmv.c:40
external API header
int linesize[AV_NUM_DATA_POINTERS]
Size, in bytes, of the data for each picture/channel plane.
Definition: avcodec.h:1008
main external API structure.
Definition: avcodec.h:1329
static void close(AVCodecParserContext *s)
Definition: h264_parser.c:327
#define AV_LOG_ERROR
Something went wrong and cannot losslessly be recovered.
Definition: log.h:111
uint32_t pal[256]
Definition: bmv.c:48
int palette_has_changed
Tell user application that palette has changed from previous frame.
Definition: avcodec.h:1188
BMVFlags
Definition: bmv.c:26
uint8_t * frame
Definition: bmv.c:47
static const uint16_t scale[4]
uint8_t * data[AV_NUM_DATA_POINTERS]
pointer to the picture/channel planes.
Definition: avcodec.h:997
common internal api header.
#define AVPALETTE_SIZE
Definition: avcodec.h:3372
8 bit with PIX_FMT_RGB32 palette
Definition: pixfmt.h:75
struct BMVAudioDecContext BMVAudioDecContext
void * priv_data
Definition: avcodec.h:1531
int len
int channels
number of audio channels
Definition: avcodec.h:1457
Definition: bmv.c:35
#define AV_LOG_INFO
Definition: log.h:119
void avcodec_get_frame_defaults(AVFrame *pic)
Set the fields of the given AVFrame to default values.
Definition: utils.c:609
static av_cold int bmv_aud_decode_init(AVCodecContext *avctx)
Definition: bmv.c:297
uint8_t frame_base[SCREEN_WIDE *(SCREEN_HIGH+1)]
Definition: bmv.c:47
static av_cold int decode_end(AVCodecContext *avctx)
Definition: bmv.c:279
int nb_samples
number of audio samples (per channel) described by this frame
Definition: avcodec.h:1265
for(j=16;j >0;--j)