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 VMD_HEADER_SIZE 0x330
51 #define PALETTE_COUNT 256
57 typedef struct VmdVideoContext
{
59 AVCodecContext
*avctx
;
67 unsigned char palette
[PALETTE_COUNT
* 4];
68 unsigned char *unpack_buffer
;
72 #define QUEUE_SIZE 0x1000
73 #define QUEUE_MASK 0x0FFF
75 static void lz_unpack(unsigned char *src
, unsigned char *dest
)
79 unsigned char queue
[QUEUE_SIZE
];
81 unsigned int dataleft
;
82 unsigned int chainofs
;
83 unsigned int chainlen
;
92 memset(queue
, QUEUE_SIZE
, 0x20);
93 if (LE_32(s
) == 0x56781234) {
99 speclen
= 100; /* no speclen */
102 while (dataleft
> 0) {
104 if ((tag
== 0xFF) && (dataleft
> 8)) {
105 for (i
= 0; i
< 8; i
++) {
106 queue
[qpos
++] = *d
++ = *s
++;
111 for (i
= 0; i
< 8; i
++) {
115 queue
[qpos
++] = *d
++ = *s
++;
120 chainofs
|= ((*s
& 0xF0) << 4);
121 chainlen
= (*s
++ & 0x0F) + 3;
122 if (chainlen
== speclen
)
123 chainlen
= *s
++ + 0xF + 3;
124 for (j
= 0; j
< chainlen
; j
++) {
125 *d
= queue
[chainofs
++ & QUEUE_MASK
];
126 queue
[qpos
++] = *d
++;
129 dataleft
-= chainlen
;
137 static int rle_unpack(unsigned char *src
, unsigned char *dest
, int len
)
158 for (i
= 0; i
< l
; i
++) {
170 static void vmd_decode(VmdVideoContext
*s
)
173 unsigned int *palette32
;
174 unsigned char r
, g
, b
;
176 /* point to the start of the encoded data */
177 unsigned char *p
= s
->buf
+ 16;
181 unsigned char *dp
; /* pointer to current frame */
182 unsigned char *pp
; /* pointer to previous frame */
186 int frame_x
, frame_y
;
187 int frame_width
, frame_height
;
189 frame_x
= LE_16(&s
->buf
[6]);
190 frame_y
= LE_16(&s
->buf
[8]);
191 frame_width
= LE_16(&s
->buf
[10]) - frame_x
+ 1;
192 frame_height
= LE_16(&s
->buf
[12]) - frame_y
+ 1;
194 /* if only a certain region will be updated, copy the entire previous
195 * frame before the decode */
196 if (frame_x
|| frame_y
|| (frame_width
!= s
->avctx
->width
) ||
197 (frame_height
!= s
->avctx
->height
)) {
199 memcpy(s
->frame
.data
[0], s
->prev_frame
.data
[0],
200 s
->avctx
->height
* s
->frame
.linesize
[0]);
203 /* check if there is a new palette */
204 if (s
->buf
[15] & 0x02) {
206 palette32
= (unsigned int *)s
->palette
;
207 for (i
= 0; i
< PALETTE_COUNT
; i
++) {
211 palette32
[i
] = (r
<< 16) | (g
<< 8) | (b
);
213 s
->size
-= (256 * 3 + 2);
216 /* originally UnpackFrame in VAG's code */
220 lz_unpack(pb
, s
->unpack_buffer
);
222 pb
= s
->unpack_buffer
;
225 dp
= &s
->frame
.data
[0][frame_y
* s
->frame
.linesize
[0] + frame_x
];
226 pp
= &s
->prev_frame
.data
[0][frame_y
* s
->prev_frame
.linesize
[0] + frame_x
];
229 for (i
= 0; i
< frame_height
; i
++) {
234 len
= (len
& 0x7F) + 1;
235 memcpy(&dp
[ofs
], pb
, len
);
239 /* interframe pixel copy */
240 memcpy(&dp
[ofs
], &pp
[ofs
], len
+ 1);
243 } while (ofs
< frame_width
);
244 if (ofs
> frame_width
) {
245 av_log(s
->avctx
, AV_LOG_ERROR
, "VMD video: offset > width (%d > %d)\n",
249 dp
+= s
->frame
.linesize
[0];
250 pp
+= s
->prev_frame
.linesize
[0];
255 for (i
= 0; i
< frame_height
; i
++) {
256 memcpy(dp
, pb
, frame_width
);
258 dp
+= s
->frame
.linesize
[0];
259 pp
+= s
->prev_frame
.linesize
[0];
264 for (i
= 0; i
< frame_height
; i
++) {
269 len
= (len
& 0x7F) + 1;
271 len
= rle_unpack(pb
, &dp
[ofs
], len
);
273 memcpy(&dp
[ofs
], pb
, len
);
277 /* interframe pixel copy */
278 memcpy(&dp
[ofs
], &pp
[ofs
], len
+ 1);
281 } while (ofs
< frame_width
);
282 if (ofs
> frame_width
) {
283 av_log(s
->avctx
, AV_LOG_ERROR
, "VMD video: offset > width (%d > %d)\n",
286 dp
+= s
->frame
.linesize
[0];
287 pp
+= s
->prev_frame
.linesize
[0];
294 static int vmdvideo_decode_init(AVCodecContext
*avctx
)
296 VmdVideoContext
*s
= (VmdVideoContext
*)avctx
->priv_data
;
298 unsigned int *palette32
;
299 int palette_index
= 0;
300 unsigned char r
, g
, b
;
301 unsigned char *vmd_header
;
302 unsigned char *raw_palette
;
305 avctx
->pix_fmt
= PIX_FMT_PAL8
;
306 avctx
->has_b_frames
= 0;
307 dsputil_init(&s
->dsp
, avctx
);
309 /* make sure the VMD header made it */
310 if (s
->avctx
->extradata_size
!= VMD_HEADER_SIZE
) {
311 av_log(s
->avctx
, AV_LOG_ERROR
, "VMD video: expected extradata size of %d\n",
315 vmd_header
= (unsigned char *)avctx
->extradata
;
317 s
->unpack_buffer
= av_malloc(LE_32(&vmd_header
[800]));
318 if (!s
->unpack_buffer
)
321 /* load up the initial palette */
322 raw_palette
= &vmd_header
[28];
323 palette32
= (unsigned int *)s
->palette
;
324 for (i
= 0; i
< PALETTE_COUNT
; i
++) {
325 r
= raw_palette
[palette_index
++] * 4;
326 g
= raw_palette
[palette_index
++] * 4;
327 b
= raw_palette
[palette_index
++] * 4;
328 palette32
[i
] = (r
<< 16) | (g
<< 8) | (b
);
331 s
->frame
.data
[0] = s
->prev_frame
.data
[0] = NULL
;
336 static int vmdvideo_decode_frame(AVCodecContext
*avctx
,
337 void *data
, int *data_size
,
338 uint8_t *buf
, int buf_size
)
340 VmdVideoContext
*s
= (VmdVideoContext
*)avctx
->priv_data
;
348 s
->frame
.reference
= 1;
349 if (avctx
->get_buffer(avctx
, &s
->frame
)) {
350 av_log(s
->avctx
, AV_LOG_ERROR
, "VMD Video: get_buffer() failed\n");
356 /* make the palette available on the way out */
357 memcpy(s
->frame
.data
[1], s
->palette
, PALETTE_COUNT
* 4);
359 if (s
->prev_frame
.data
[0])
360 avctx
->release_buffer(avctx
, &s
->prev_frame
);
363 s
->prev_frame
= s
->frame
;
365 *data_size
= sizeof(AVFrame
);
366 *(AVFrame
*)data
= s
->frame
;
368 /* report that the buffer was completely consumed */
372 static int vmdvideo_decode_end(AVCodecContext
*avctx
)
374 VmdVideoContext
*s
= (VmdVideoContext
*)avctx
->priv_data
;
376 if (s
->prev_frame
.data
[0])
377 avctx
->release_buffer(avctx
, &s
->prev_frame
);
378 av_free(s
->unpack_buffer
);
388 typedef struct VmdAudioContext
{
389 AVCodecContext
*avctx
;
393 unsigned char steps8
[16];
394 unsigned short steps16
[16];
395 unsigned short steps128
[256];
399 static int vmdaudio_decode_init(AVCodecContext
*avctx
)
401 VmdAudioContext
*s
= (VmdAudioContext
*)avctx
->priv_data
;
405 s
->channels
= avctx
->channels
;
406 s
->bits
= avctx
->bits_per_sample
;
407 s
->block_align
= avctx
->block_align
;
409 av_log(s
->avctx
, AV_LOG_DEBUG
, "%d channels, %d bits/sample, block align = %d, sample rate = %d\n",
410 s
->channels
, s
->bits
, s
->block_align
, avctx
->sample_rate
);
412 /* set up the steps8 and steps16 tables */
413 for (i
= 0; i
< 8; i
++) {
417 s
->steps8
[i
] = s
->steps8
[i
- 1] + i
- 1;
426 s
->steps16
[i
] = 1 << (i
+ 4);
429 /* set up the step128 table */
432 for (i
= 0x02; i
<= 0x20; i
++)
433 s
->steps128
[i
] = (i
- 1) << 4;
434 for (i
= 0x21; i
<= 0x60; i
++)
435 s
->steps128
[i
] = (i
+ 0x1F) << 3;
436 for (i
= 0x61; i
<= 0x70; i
++)
437 s
->steps128
[i
] = (i
- 0x51) << 6;
438 for (i
= 0x71; i
<= 0x78; i
++)
439 s
->steps128
[i
] = (i
- 0x69) << 8;
440 for (i
= 0x79; i
<= 0x7D; i
++)
441 s
->steps128
[i
] = (i
- 0x75) << 10;
442 s
->steps128
[0x7E] = 0x3000;
443 s
->steps128
[0x7F] = 0x4000;
445 /* set up the negative half of each table */
446 for (i
= 0; i
< 8; i
++) {
447 s
->steps8
[i
+ 8] = -s
->steps8
[i
];
448 s
->steps16
[i
+ 8] = -s
->steps16
[i
];
450 for (i
= 0; i
< 128; i
++)
451 s
->steps128
[i
+ 128] = -s
->steps128
[i
];
456 static void vmdaudio_decode_audio(VmdAudioContext
*s
, unsigned char *data
,
457 uint8_t *buf
, int ratio
) {
461 static int vmdaudio_loadsound(VmdAudioContext
*s
, unsigned char *data
,
462 uint8_t *buf
, int silence
)
464 int bytes_decoded
= 0;
468 av_log(s
->avctx
, AV_LOG_INFO
, "silent block!\n");
469 if (s
->channels
== 2) {
471 /* stereo handling */
472 if ((s
->block_align
& 0x01) == 0) {
474 memset(data
, 0, s
->block_align
* 2);
476 vmdaudio_decode_audio(s
, data
, buf
, 1);
479 memset(data
, 0, s
->block_align
* 2);
481 vmdaudio_decode_audio(s
, data
, buf
, 1);
488 memset(data
, 0, s
->block_align
* 2);
489 bytes_decoded
= s
->block_align
* 2;
491 // memset(data, 0x00, s->block_align);
492 // bytes_decoded = s->block_align;
493 memset(data
, 0x00, s
->block_align
* 2);
494 bytes_decoded
= s
->block_align
* 2;
497 /* copy the data but convert it to signed */
498 for (i
= 0; i
< s
->block_align
; i
++)
499 data
[i
* 2 + 1] = buf
[i
] + 0x80;
500 bytes_decoded
= s
->block_align
* 2;
504 return bytes_decoded
;
507 static int vmdaudio_decode_frame(AVCodecContext
*avctx
,
508 void *data
, int *data_size
,
509 uint8_t *buf
, int buf_size
)
511 VmdAudioContext
*s
= (VmdAudioContext
*)avctx
->priv_data
;
512 unsigned int sound_flags
;
513 unsigned char *output_samples
= (unsigned char *)data
;
515 /* point to the start of the encoded data */
516 unsigned char *p
= buf
+ 16;
517 unsigned char *p_end
= buf
+ buf_size
;
523 /* the chunk contains audio */
524 *data_size
= vmdaudio_loadsound(s
, output_samples
, p
, 0);
525 } else if (buf
[6] == 2) {
526 /* the chunk contains audio and silence mixed together */
527 sound_flags
= LE_32(p
);
530 /* do something with extrabufs here? */
533 if (sound_flags
& 0x01)
535 *data_size
+= vmdaudio_loadsound(s
, output_samples
, p
, 1);
538 *data_size
+= vmdaudio_loadsound(s
, output_samples
, p
, 0);
541 output_samples
+= (s
->block_align
* s
->bits
/ 8);
544 } else if (buf
[6] == 3) {
546 *data_size
= vmdaudio_loadsound(s
, output_samples
, p
, 1);
554 * Public Data Structures
557 AVCodec vmdvideo_decoder
= {
561 sizeof(VmdVideoContext
),
562 vmdvideo_decode_init
,
565 vmdvideo_decode_frame
,
569 AVCodec vmdaudio_decoder
= {
573 sizeof(VmdAudioContext
),
574 vmdaudio_decode_init
,
577 vmdaudio_decode_frame
,