2 * Sierra VMD Audio & Video Decoders
3 * Copyright (C) 2004 the ffmpeg project
5 * This library is free software; you can redistribute it and/or
6 * modify it under the terms of the GNU Lesser General Public
7 * License as published by the Free Software Foundation; either
8 * version 2 of the License, or (at your option) any later version.
10 * This library is distributed in the hope that it will be useful,
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
13 * Lesser General Public License for more details.
15 * You should have received a copy of the GNU Lesser General Public
16 * License along with this library; if not, write to the Free Software
17 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
23 * Sierra VMD audio & video decoders
24 * by Vladimir "VAG" Gneushev (vagsoft at mail.ru)
26 * The video decoder outputs PAL8 colorspace data. The decoder expects
27 * a 0x330-byte VMD file header to be transmitted via extradata during
28 * codec initialization. Each encoded frame that is sent to this decoder
29 * is expected to be prepended with the appropriate 16-byte frame
30 * information record from the VMD file.
32 * The audio decoder, like the video decoder, expects each encoded data
33 * chunk to be prepended with the approriate 16-byte frame information
34 * record from the VMD file. It does not require the 0x330-byte VMD file
35 * header, but it does need the audio setup parameters passed in through
36 * normal libavcodec API means.
48 #define printf(...) {} //(f)printf() usage is forbidden in libavcodec, use av_log
49 #define fprintf(...) {}
51 #define VMD_HEADER_SIZE 0x330
52 #define PALETTE_COUNT 256
54 #define LE_16(x) ((((uint8_t*)(x))[1] << 8) | ((uint8_t*)(x))[0])
55 #define LE_32(x) ((((uint8_t*)(x))[3] << 24) | \
56 (((uint8_t*)(x))[2] << 16) | \
57 (((uint8_t*)(x))[1] << 8) | \
64 typedef struct VmdVideoContext {
66 AVCodecContext *avctx;
74 unsigned char palette[PALETTE_COUNT * 4];
75 unsigned char *unpack_buffer;
79 #define QUEUE_SIZE 0x1000
80 #define QUEUE_MASK 0x0FFF
82 static void lz_unpack(unsigned char *src, unsigned char *dest)
86 unsigned char queue[QUEUE_SIZE];
88 unsigned int dataleft;
89 unsigned int chainofs;
90 unsigned int chainlen;
99 memset(queue, QUEUE_SIZE, 0x20);
100 if (LE_32(s) == 0x56781234) {
106 speclen = 100; /* no speclen */
109 while (dataleft > 0) {
111 if ((tag == 0xFF) && (dataleft > 8)) {
112 for (i = 0; i < 8; i++) {
113 queue[qpos++] = *d++ = *s++;
118 for (i = 0; i < 8; i++) {
122 queue[qpos++] = *d++ = *s++;
127 chainofs |= ((*s & 0xF0) << 4);
128 chainlen = (*s++ & 0x0F) + 3;
129 if (chainlen == speclen)
130 chainlen = *s++ + 0xF + 3;
131 for (j = 0; j < chainlen; j++) {
132 *d = queue[chainofs++ & QUEUE_MASK];
133 queue[qpos++] = *d++;
136 dataleft -= chainlen;
144 static int rle_unpack(unsigned char *src, unsigned char *dest, int len)
165 for (i = 0; i < l; i++) {
177 static void vmd_decode(VmdVideoContext *s)
180 unsigned int *palette32;
181 unsigned char r, g, b;
183 /* point to the start of the encoded data */
184 unsigned char *p = s->buf + 16;
188 unsigned char *dp; /* pointer to current frame */
189 unsigned char *pp; /* pointer to previous frame */
193 int frame_x, frame_y;
194 int frame_width, frame_height;
196 frame_x = LE_16(&s->buf[6]);
197 frame_y = LE_16(&s->buf[8]);
198 frame_width = LE_16(&s->buf[10]) - frame_x + 1;
199 frame_height = LE_16(&s->buf[12]) - frame_y + 1;
201 /* if only a certain region will be updated, copy the entire previous
202 * frame before the decode */
203 if (frame_x || frame_y || (frame_width != s->avctx->width) ||
204 (frame_height != s->avctx->height)) {
206 memcpy(s->frame.data[0], s->prev_frame.data[0],
207 s->avctx->height * s->frame.linesize[0]);
210 /* check if there is a new palette */
211 if (s->buf[15] & 0x02) {
213 palette32 = (unsigned int *)s->palette;
214 for (i = 0; i < PALETTE_COUNT; i++) {
218 palette32[i] = (r << 16) | (g << 8) | (b);
220 s->size -= (256 * 3 + 2);
223 /* originally UnpackFrame in VAG's code */
227 lz_unpack(pb, s->unpack_buffer);
229 pb = s->unpack_buffer;
232 dp = &s->frame.data[0][frame_y * s->frame.linesize[0] + frame_x];
233 pp = &s->prev_frame.data[0][frame_y * s->prev_frame.linesize[0] + frame_x];
236 for (i = 0; i < frame_height; i++) {
241 len = (len & 0x7F) + 1;
242 memcpy(&dp[ofs], pb, len);
246 /* interframe pixel copy */
247 memcpy(&dp[ofs], &pp[ofs], len + 1);
250 } while (ofs < frame_width);
251 if (ofs > frame_width) {
252 printf (" VMD video: offset > width (%d > %d)\n",
256 dp += s->frame.linesize[0];
257 pp += s->prev_frame.linesize[0];
262 for (i = 0; i < frame_height; i++) {
263 memcpy(dp, pb, frame_width);
265 dp += s->frame.linesize[0];
266 pp += s->prev_frame.linesize[0];
271 for (i = 0; i < frame_height; i++) {
276 len = (len & 0x7F) + 1;
278 len = rle_unpack(pb, dp, len);
280 memcpy(&dp[ofs], pb, len);
284 /* interframe pixel copy */
285 memcpy(&dp[ofs], &pp[ofs], len + 1);
288 } while (ofs < frame_width);
289 if (ofs > frame_width) {
290 printf (" VMD video: offset > width (%d > %d)\n",
293 dp += s->frame.linesize[0];
294 pp += s->prev_frame.linesize[0];
301 static int vmdvideo_decode_init(AVCodecContext *avctx)
303 VmdVideoContext *s = (VmdVideoContext *)avctx->priv_data;
305 unsigned int *palette32;
306 int palette_index = 0;
307 unsigned char r, g, b;
308 unsigned char *vmd_header;
309 unsigned char *raw_palette;
312 avctx->pix_fmt = PIX_FMT_PAL8;
313 avctx->has_b_frames = 0;
314 dsputil_init(&s->dsp, avctx);
316 /* make sure the VMD header made it */
317 if (s->avctx->extradata_size != VMD_HEADER_SIZE) {
318 printf(" VMD video: expected extradata size of %d\n",
322 vmd_header = (unsigned char *)avctx->extradata;
324 s->unpack_buffer = av_malloc(LE_32(&vmd_header[800]));
325 if (!s->unpack_buffer)
328 /* load up the initial palette */
329 raw_palette = &vmd_header[28];
330 palette32 = (unsigned int *)s->palette;
331 for (i = 0; i < PALETTE_COUNT; i++) {
332 r = raw_palette[palette_index++] * 4;
333 g = raw_palette[palette_index++] * 4;
334 b = raw_palette[palette_index++] * 4;
335 palette32[i] = (r << 16) | (g << 8) | (b);
338 s->frame.data[0] = s->prev_frame.data[0] = NULL;
343 static int vmdvideo_decode_frame(AVCodecContext *avctx,
344 void *data, int *data_size,
345 uint8_t *buf, int buf_size)
347 VmdVideoContext *s = (VmdVideoContext *)avctx->priv_data;
352 s->frame.reference = 1;
353 if (avctx->get_buffer(avctx, &s->frame)) {
354 printf (" VMD Video: get_buffer() failed\n");
360 /* make the palette available on the way out */
361 memcpy(s->frame.data[1], s->palette, PALETTE_COUNT * 4);
363 if (s->prev_frame.data[0])
364 avctx->release_buffer(avctx, &s->prev_frame);
367 s->prev_frame = s->frame;
369 *data_size = sizeof(AVFrame);
370 *(AVFrame*)data = s->frame;
372 /* report that the buffer was completely consumed */
376 static int vmdvideo_decode_end(AVCodecContext *avctx)
378 VmdVideoContext *s = (VmdVideoContext *)avctx->priv_data;
380 if (s->prev_frame.data[0])
381 avctx->release_buffer(avctx, &s->prev_frame);
382 av_free(s->unpack_buffer);
392 typedef struct VmdAudioContext {
396 unsigned char steps8[16];
397 unsigned short steps16[16];
398 unsigned short steps128[256];
402 static int vmdaudio_decode_init(AVCodecContext *avctx)
404 VmdAudioContext *s = (VmdAudioContext *)avctx->priv_data;
407 s->channels = avctx->channels;
408 s->bits = avctx->bits_per_sample;
409 s->block_align = avctx->block_align;
411 printf (" %d channels, %d bits/sample, block align = %d\n",
412 s->channels, s->bits, s->block_align);
414 /* set up the steps8 and steps16 tables */
415 for (i = 0; i < 8; i++) {
419 s->steps8[i] = s->steps8[i - 1] + i - 1;
428 s->steps16[i] = 1 << (i + 4);
431 /* set up the step128 table */
434 for (i = 0x02; i <= 0x20; i++)
435 s->steps128[i] = (i - 1) << 4;
436 for (i = 0x21; i <= 0x60; i++)
437 s->steps128[i] = (i + 0x1F) << 3;
438 for (i = 0x61; i <= 0x70; i++)
439 s->steps128[i] = (i - 0x51) << 6;
440 for (i = 0x71; i <= 0x78; i++)
441 s->steps128[i] = (i - 0x69) << 8;
442 for (i = 0x79; i <= 0x7D; i++)
443 s->steps128[i] = (i - 0x75) << 10;
444 s->steps128[0x7E] = 0x3000;
445 s->steps128[0x7F] = 0x4000;
447 /* set up the negative half of each table */
448 for (i = 0; i < 8; i++) {
449 s->steps8[i + 8] = -s->steps8[i];
450 s->steps16[i + 8] = -s->steps16[i];
452 for (i = 0; i < 128; i++)
453 s->steps128[i + 128] = -s->steps128[i];
458 static void vmdaudio_decode_audio(VmdAudioContext *s, unsigned char *data,
459 uint8_t *buf, int ratio) {
463 static void vmdaudio_loadsound(VmdAudioContext *s, unsigned char *data,
464 uint8_t *buf, int silence)
466 if (s->channels == 2) {
467 if ((s->block_align & 0x01) == 0) {
469 memset(data, 0, s->block_align * 2);
471 vmdaudio_decode_audio(s, data, buf, 1);
474 memset(data, 0, s->block_align * 2);
476 // vmdaudio_decode_audio(s, data, buf, 1);
482 static int vmdaudio_decode_frame(AVCodecContext *avctx,
483 void *data, int *data_size,
484 uint8_t *buf, int buf_size)
486 VmdAudioContext *s = (VmdAudioContext *)avctx->priv_data;
487 unsigned int sound_flags;
488 unsigned char *output_samples = (unsigned char *)data;
490 /* point to the start of the encoded data */
491 unsigned char *p = buf + 16;
492 unsigned char *p_end = buf + buf_size;
495 /* the chunk contains audio */
496 vmdaudio_loadsound(s, output_samples, p, 0);
497 } else if (buf[6] == 2) {
498 /* the chunk contains audio and silence mixed together */
499 sound_flags = LE_32(p);
502 /* do something with extrabufs here? */
505 if (sound_flags & 0x01)
507 vmdaudio_loadsound(s, output_samples, p, 1);
510 vmdaudio_loadsound(s, output_samples, p, 0);
512 output_samples += (s->block_align * s->bits / 8);
515 } else if (buf[6] == 3) {
517 vmdaudio_loadsound(s, output_samples, p, 1);
527 * Public Data Structures
530 AVCodec vmdvideo_decoder = {
534 sizeof(VmdVideoContext),
535 vmdvideo_decode_init,
538 vmdvideo_decode_frame,
542 AVCodec vmdaudio_decoder = {
546 sizeof(VmdAudioContext),
547 vmdaudio_decode_init,
550 vmdaudio_decode_frame,