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)
25 * for more information on the Sierra VMD format, visit:
26 * http://www.pcisys.net/~melanson/codecs/
28 * The video decoder outputs PAL8 colorspace data. The decoder expects
29 * a 0x330-byte VMD file header to be transmitted via extradata during
30 * codec initialization. Each encoded frame that is sent to this decoder
31 * is expected to be prepended with the appropriate 16-byte frame
32 * information record from the VMD file.
34 * The audio decoder, like the video decoder, expects each encoded data
35 * chunk to be prepended with the appropriate 16-byte frame information
36 * record from the VMD file. It does not require the 0x330-byte VMD file
37 * header, but it does need the audio setup parameters passed in through
38 * normal libavcodec API means.
50 #define printf(...) {} //(f)printf() usage is forbidden in libavcodec, use av_log
51 #define fprintf(...) {}
53 #define VMD_HEADER_SIZE 0x330
54 #define PALETTE_COUNT 256
60 typedef struct VmdVideoContext {
62 AVCodecContext *avctx;
70 unsigned char palette[PALETTE_COUNT * 4];
71 unsigned char *unpack_buffer;
75 #define QUEUE_SIZE 0x1000
76 #define QUEUE_MASK 0x0FFF
78 static void lz_unpack(unsigned char *src, unsigned char *dest)
82 unsigned char queue[QUEUE_SIZE];
84 unsigned int dataleft;
85 unsigned int chainofs;
86 unsigned int chainlen;
95 memset(queue, QUEUE_SIZE, 0x20);
96 if (LE_32(s) == 0x56781234) {
102 speclen = 100; /* no speclen */
105 while (dataleft > 0) {
107 if ((tag == 0xFF) && (dataleft > 8)) {
108 for (i = 0; i < 8; i++) {
109 queue[qpos++] = *d++ = *s++;
114 for (i = 0; i < 8; i++) {
118 queue[qpos++] = *d++ = *s++;
123 chainofs |= ((*s & 0xF0) << 4);
124 chainlen = (*s++ & 0x0F) + 3;
125 if (chainlen == speclen)
126 chainlen = *s++ + 0xF + 3;
127 for (j = 0; j < chainlen; j++) {
128 *d = queue[chainofs++ & QUEUE_MASK];
129 queue[qpos++] = *d++;
132 dataleft -= chainlen;
140 static int rle_unpack(unsigned char *src, unsigned char *dest, int len)
161 for (i = 0; i < l; i++) {
173 static void vmd_decode(VmdVideoContext *s)
176 unsigned int *palette32;
177 unsigned char r, g, b;
179 /* point to the start of the encoded data */
180 unsigned char *p = s->buf + 16;
184 unsigned char *dp; /* pointer to current frame */
185 unsigned char *pp; /* pointer to previous frame */
189 int frame_x, frame_y;
190 int frame_width, frame_height;
192 frame_x = LE_16(&s->buf[6]);
193 frame_y = LE_16(&s->buf[8]);
194 frame_width = LE_16(&s->buf[10]) - frame_x + 1;
195 frame_height = LE_16(&s->buf[12]) - frame_y + 1;
197 /* if only a certain region will be updated, copy the entire previous
198 * frame before the decode */
199 if (frame_x || frame_y || (frame_width != s->avctx->width) ||
200 (frame_height != s->avctx->height)) {
202 memcpy(s->frame.data[0], s->prev_frame.data[0],
203 s->avctx->height * s->frame.linesize[0]);
206 /* check if there is a new palette */
207 if (s->buf[15] & 0x02) {
209 palette32 = (unsigned int *)s->palette;
210 for (i = 0; i < PALETTE_COUNT; i++) {
214 palette32[i] = (r << 16) | (g << 8) | (b);
216 s->size -= (256 * 3 + 2);
219 /* originally UnpackFrame in VAG's code */
223 lz_unpack(pb, s->unpack_buffer);
225 pb = s->unpack_buffer;
228 dp = &s->frame.data[0][frame_y * s->frame.linesize[0] + frame_x];
229 pp = &s->prev_frame.data[0][frame_y * s->prev_frame.linesize[0] + frame_x];
232 for (i = 0; i < frame_height; i++) {
237 len = (len & 0x7F) + 1;
238 memcpy(&dp[ofs], pb, len);
242 /* interframe pixel copy */
243 memcpy(&dp[ofs], &pp[ofs], len + 1);
246 } while (ofs < frame_width);
247 if (ofs > frame_width) {
248 printf (" VMD video: offset > width (%d > %d)\n",
252 dp += s->frame.linesize[0];
253 pp += s->prev_frame.linesize[0];
258 for (i = 0; i < frame_height; i++) {
259 memcpy(dp, pb, frame_width);
261 dp += s->frame.linesize[0];
262 pp += s->prev_frame.linesize[0];
267 for (i = 0; i < frame_height; i++) {
272 len = (len & 0x7F) + 1;
274 len = rle_unpack(pb, &dp[ofs], len);
276 memcpy(&dp[ofs], pb, len);
280 /* interframe pixel copy */
281 memcpy(&dp[ofs], &pp[ofs], len + 1);
284 } while (ofs < frame_width);
285 if (ofs > frame_width) {
286 printf (" VMD video: offset > width (%d > %d)\n",
289 dp += s->frame.linesize[0];
290 pp += s->prev_frame.linesize[0];
297 static int vmdvideo_decode_init(AVCodecContext *avctx)
299 VmdVideoContext *s = (VmdVideoContext *)avctx->priv_data;
301 unsigned int *palette32;
302 int palette_index = 0;
303 unsigned char r, g, b;
304 unsigned char *vmd_header;
305 unsigned char *raw_palette;
308 avctx->pix_fmt = PIX_FMT_PAL8;
309 avctx->has_b_frames = 0;
310 dsputil_init(&s->dsp, avctx);
312 /* make sure the VMD header made it */
313 if (s->avctx->extradata_size != VMD_HEADER_SIZE) {
314 printf(" VMD video: expected extradata size of %d\n",
318 vmd_header = (unsigned char *)avctx->extradata;
320 s->unpack_buffer = av_malloc(LE_32(&vmd_header[800]));
321 if (!s->unpack_buffer)
324 /* load up the initial palette */
325 raw_palette = &vmd_header[28];
326 palette32 = (unsigned int *)s->palette;
327 for (i = 0; i < PALETTE_COUNT; i++) {
328 r = raw_palette[palette_index++] * 4;
329 g = raw_palette[palette_index++] * 4;
330 b = raw_palette[palette_index++] * 4;
331 palette32[i] = (r << 16) | (g << 8) | (b);
334 s->frame.data[0] = s->prev_frame.data[0] = NULL;
339 static int vmdvideo_decode_frame(AVCodecContext *avctx,
340 void *data, int *data_size,
341 uint8_t *buf, int buf_size)
343 VmdVideoContext *s = (VmdVideoContext *)avctx->priv_data;
351 s->frame.reference = 1;
352 if (avctx->get_buffer(avctx, &s->frame)) {
353 printf (" VMD Video: get_buffer() failed\n");
359 /* make the palette available on the way out */
360 memcpy(s->frame.data[1], s->palette, PALETTE_COUNT * 4);
362 if (s->prev_frame.data[0])
363 avctx->release_buffer(avctx, &s->prev_frame);
366 s->prev_frame = s->frame;
368 *data_size = sizeof(AVFrame);
369 *(AVFrame*)data = s->frame;
371 /* report that the buffer was completely consumed */
375 static int vmdvideo_decode_end(AVCodecContext *avctx)
377 VmdVideoContext *s = (VmdVideoContext *)avctx->priv_data;
379 if (s->prev_frame.data[0])
380 avctx->release_buffer(avctx, &s->prev_frame);
381 av_free(s->unpack_buffer);
391 typedef struct VmdAudioContext {
395 unsigned char steps8[16];
396 unsigned short steps16[16];
397 unsigned short steps128[256];
401 static int vmdaudio_decode_init(AVCodecContext *avctx)
403 VmdAudioContext *s = (VmdAudioContext *)avctx->priv_data;
406 s->channels = avctx->channels;
407 s->bits = avctx->bits_per_sample;
408 s->block_align = avctx->block_align;
410 printf (" %d channels, %d bits/sample, block align = %d, sample rate = %d\n",
411 s->channels, s->bits, s->block_align, avctx->sample_rate);
413 /* set up the steps8 and steps16 tables */
414 for (i = 0; i < 8; i++) {
418 s->steps8[i] = s->steps8[i - 1] + i - 1;
427 s->steps16[i] = 1 << (i + 4);
430 /* set up the step128 table */
433 for (i = 0x02; i <= 0x20; i++)
434 s->steps128[i] = (i - 1) << 4;
435 for (i = 0x21; i <= 0x60; i++)
436 s->steps128[i] = (i + 0x1F) << 3;
437 for (i = 0x61; i <= 0x70; i++)
438 s->steps128[i] = (i - 0x51) << 6;
439 for (i = 0x71; i <= 0x78; i++)
440 s->steps128[i] = (i - 0x69) << 8;
441 for (i = 0x79; i <= 0x7D; i++)
442 s->steps128[i] = (i - 0x75) << 10;
443 s->steps128[0x7E] = 0x3000;
444 s->steps128[0x7F] = 0x4000;
446 /* set up the negative half of each table */
447 for (i = 0; i < 8; i++) {
448 s->steps8[i + 8] = -s->steps8[i];
449 s->steps16[i + 8] = -s->steps16[i];
451 for (i = 0; i < 128; i++)
452 s->steps128[i + 128] = -s->steps128[i];
457 static void vmdaudio_decode_audio(VmdAudioContext *s, unsigned char *data,
458 uint8_t *buf, int ratio) {
462 static int vmdaudio_loadsound(VmdAudioContext *s, unsigned char *data,
463 uint8_t *buf, int silence)
465 int bytes_decoded = 0;
469 printf (" silent block!\n");
470 if (s->channels == 2) {
472 /* stereo handling */
473 if ((s->block_align & 0x01) == 0) {
475 memset(data, 0, s->block_align * 2);
477 vmdaudio_decode_audio(s, data, buf, 1);
480 memset(data, 0, s->block_align * 2);
482 vmdaudio_decode_audio(s, data, buf, 1);
489 memset(data, 0, s->block_align * 2);
490 bytes_decoded = s->block_align * 2;
492 // memset(data, 0x00, s->block_align);
493 // bytes_decoded = s->block_align;
494 memset(data, 0x00, s->block_align * 2);
495 bytes_decoded = s->block_align * 2;
500 /* copy the data but convert it to signed */
501 for (i = 0; i < s->block_align; i++)
502 data[i * 2 + 1] = buf[i] + 0x80;
503 bytes_decoded = s->block_align * 2;
508 return bytes_decoded;
511 static int vmdaudio_decode_frame(AVCodecContext *avctx,
512 void *data, int *data_size,
513 uint8_t *buf, int buf_size)
515 VmdAudioContext *s = (VmdAudioContext *)avctx->priv_data;
516 unsigned int sound_flags;
517 unsigned char *output_samples = (unsigned char *)data;
519 /* point to the start of the encoded data */
520 unsigned char *p = buf + 16;
521 unsigned char *p_end = buf + buf_size;
523 printf (" processing audio frame with %d bytes\n", buf_size);
529 /* the chunk contains audio */
530 *data_size = vmdaudio_loadsound(s, output_samples, p, 0);
531 } else if (buf[6] == 2) {
532 printf (" hey! audio case #2\n");
533 /* the chunk contains audio and silence mixed together */
534 sound_flags = LE_32(p);
537 /* do something with extrabufs here? */
540 if (sound_flags & 0x01)
542 *data_size += vmdaudio_loadsound(s, output_samples, p, 1);
545 *data_size += vmdaudio_loadsound(s, output_samples, p, 0);
548 output_samples += (s->block_align * s->bits / 8);
551 } else if (buf[6] == 3) {
552 printf (" hey! audio case #3\n");
554 *data_size = vmdaudio_loadsound(s, output_samples, p, 1);
557 printf (" final sample count = %d, byte count = %d\n", (*data_size) / 2,
564 * Public Data Structures
567 AVCodec vmdvideo_decoder = {
571 sizeof(VmdVideoContext),
572 vmdvideo_decode_init,
575 vmdvideo_decode_frame,
579 AVCodec vmdaudio_decoder = {
583 sizeof(VmdAudioContext),
584 vmdaudio_decode_init,
587 vmdaudio_decode_frame,