r1009: Move the dependencies to newer package names
[cinelerra_cv/mob.git] / quicktime / ffmpeg / libavcodec / vmdav.c
blob4305f81fdad26a9979ed974c4cc00a231513b088
1 /*
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
21 /**
22 * @file vmdvideo.c
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.
41 #include <stdio.h>
42 #include <stdlib.h>
43 #include <string.h>
44 #include <unistd.h>
46 #include "common.h"
47 #include "avcodec.h"
48 #include "dsputil.h"
50 #define VMD_HEADER_SIZE 0x330
51 #define PALETTE_COUNT 256
54 * Video Decoder
57 typedef struct VmdVideoContext {
59 AVCodecContext *avctx;
60 DSPContext dsp;
61 AVFrame frame;
62 AVFrame prev_frame;
64 unsigned char *buf;
65 int size;
67 unsigned char palette[PALETTE_COUNT * 4];
68 unsigned char *unpack_buffer;
70 } VmdVideoContext;
72 #define QUEUE_SIZE 0x1000
73 #define QUEUE_MASK 0x0FFF
75 static void lz_unpack(unsigned char *src, unsigned char *dest)
77 unsigned char *s;
78 unsigned char *d;
79 unsigned char queue[QUEUE_SIZE];
80 unsigned int qpos;
81 unsigned int dataleft;
82 unsigned int chainofs;
83 unsigned int chainlen;
84 unsigned int speclen;
85 unsigned char tag;
86 unsigned int i, j;
88 s = src;
89 d = dest;
90 dataleft = LE_32(s);
91 s += 4;
92 memset(queue, QUEUE_SIZE, 0x20);
93 if (LE_32(s) == 0x56781234) {
94 s += 4;
95 qpos = 0x111;
96 speclen = 0xF + 3;
97 } else {
98 qpos = 0xFEE;
99 speclen = 100; /* no speclen */
102 while (dataleft > 0) {
103 tag = *s++;
104 if ((tag == 0xFF) && (dataleft > 8)) {
105 for (i = 0; i < 8; i++) {
106 queue[qpos++] = *d++ = *s++;
107 qpos &= QUEUE_MASK;
109 dataleft -= 8;
110 } else {
111 for (i = 0; i < 8; i++) {
112 if (dataleft == 0)
113 break;
114 if (tag & 0x01) {
115 queue[qpos++] = *d++ = *s++;
116 qpos &= QUEUE_MASK;
117 dataleft--;
118 } else {
119 chainofs = *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++;
127 qpos &= QUEUE_MASK;
129 dataleft -= chainlen;
131 tag >>= 1;
137 static int rle_unpack(unsigned char *src, unsigned char *dest, int len)
139 unsigned char *ps;
140 unsigned char *pd;
141 int i, l;
143 ps = src;
144 pd = dest;
145 if (len & 1)
146 *pd++ = *ps++;
148 len >>= 1;
149 i = 0;
150 do {
151 l = *ps++;
152 if (l & 0x80) {
153 l = (l & 0x7F) * 2;
154 memcpy(pd, ps, l);
155 ps += l;
156 pd += l;
157 } else {
158 for (i = 0; i < l; i++) {
159 *pd++ = ps[0];
160 *pd++ = ps[1];
162 ps += 2;
164 i += l;
165 } while (i < len);
167 return (ps - src);
170 static void vmd_decode(VmdVideoContext *s)
172 int i;
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;
179 unsigned char *pb;
180 unsigned char meth;
181 unsigned char *dp; /* pointer to current frame */
182 unsigned char *pp; /* pointer to previous frame */
183 unsigned char len;
184 int ofs;
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) {
205 p += 2;
206 palette32 = (unsigned int *)s->palette;
207 for (i = 0; i < PALETTE_COUNT; i++) {
208 r = *p++ * 4;
209 g = *p++ * 4;
210 b = *p++ * 4;
211 palette32[i] = (r << 16) | (g << 8) | (b);
213 s->size -= (256 * 3 + 2);
215 if (s->size >= 0) {
216 /* originally UnpackFrame in VAG's code */
217 pb = p;
218 meth = *pb++;
219 if (meth & 0x80) {
220 lz_unpack(pb, s->unpack_buffer);
221 meth &= 0x7F;
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];
227 switch (meth) {
228 case 1:
229 for (i = 0; i < frame_height; i++) {
230 ofs = 0;
231 do {
232 len = *pb++;
233 if (len & 0x80) {
234 len = (len & 0x7F) + 1;
235 memcpy(&dp[ofs], pb, len);
236 pb += len;
237 ofs += len;
238 } else {
239 /* interframe pixel copy */
240 memcpy(&dp[ofs], &pp[ofs], len + 1);
241 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",
246 ofs, frame_width);
247 break;
249 dp += s->frame.linesize[0];
250 pp += s->prev_frame.linesize[0];
252 break;
254 case 2:
255 for (i = 0; i < frame_height; i++) {
256 memcpy(dp, pb, frame_width);
257 pb += frame_width;
258 dp += s->frame.linesize[0];
259 pp += s->prev_frame.linesize[0];
261 break;
263 case 3:
264 for (i = 0; i < frame_height; i++) {
265 ofs = 0;
266 do {
267 len = *pb++;
268 if (len & 0x80) {
269 len = (len & 0x7F) + 1;
270 if (*pb++ == 0xFF)
271 len = rle_unpack(pb, &dp[ofs], len);
272 else
273 memcpy(&dp[ofs], pb, len);
274 pb += len;
275 ofs += len;
276 } else {
277 /* interframe pixel copy */
278 memcpy(&dp[ofs], &pp[ofs], len + 1);
279 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",
284 ofs, frame_width);
286 dp += s->frame.linesize[0];
287 pp += s->prev_frame.linesize[0];
289 break;
294 static int vmdvideo_decode_init(AVCodecContext *avctx)
296 VmdVideoContext *s = (VmdVideoContext *)avctx->priv_data;
297 int i;
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;
304 s->avctx = avctx;
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",
312 VMD_HEADER_SIZE);
313 return -1;
315 vmd_header = (unsigned char *)avctx->extradata;
317 s->unpack_buffer = av_malloc(LE_32(&vmd_header[800]));
318 if (!s->unpack_buffer)
319 return -1;
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;
333 return 0;
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;
342 s->buf = buf;
343 s->size = buf_size;
345 if (buf_size < 16)
346 return buf_size;
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");
351 return -1;
354 vmd_decode(s);
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);
362 /* shuffle frames */
363 s->prev_frame = s->frame;
365 *data_size = sizeof(AVFrame);
366 *(AVFrame*)data = s->frame;
368 /* report that the buffer was completely consumed */
369 return buf_size;
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);
380 return 0;
385 * Audio Decoder
388 typedef struct VmdAudioContext {
389 AVCodecContext *avctx;
390 int channels;
391 int bits;
392 int block_align;
393 unsigned char steps8[16];
394 unsigned short steps16[16];
395 unsigned short steps128[256];
396 short predictors[2];
397 } VmdAudioContext;
399 static int vmdaudio_decode_init(AVCodecContext *avctx)
401 VmdAudioContext *s = (VmdAudioContext *)avctx->priv_data;
402 int i;
404 s->avctx = avctx;
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++) {
414 if (i < 4)
415 s->steps8[i] = i;
416 else
417 s->steps8[i] = s->steps8[i - 1] + i - 1;
419 if (i == 0)
420 s->steps16[i] = 0;
421 else if (i == 1)
422 s->steps16[i] = 4;
423 else if (i == 2)
424 s->steps16[i] = 16;
425 else
426 s->steps16[i] = 1 << (i + 4);
429 /* set up the step128 table */
430 s->steps128[0] = 0;
431 s->steps128[1] = 8;
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];
453 return 0;
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;
465 int i;
467 if (silence)
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) {
473 if (silence)
474 memset(data, 0, s->block_align * 2);
475 else
476 vmdaudio_decode_audio(s, data, buf, 1);
477 } else {
478 if (silence)
479 memset(data, 0, s->block_align * 2);
480 else
481 vmdaudio_decode_audio(s, data, buf, 1);
483 } else {
485 /* mono handling */
486 if (silence) {
487 if (s->bits == 16) {
488 memset(data, 0, s->block_align * 2);
489 bytes_decoded = s->block_align * 2;
490 } else {
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;
496 } else {
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;
519 if (buf_size < 16)
520 return buf_size;
522 if (buf[6] == 1) {
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);
528 p += 4;
530 /* do something with extrabufs here? */
532 while (p < p_end) {
533 if (sound_flags & 0x01)
534 /* silence */
535 *data_size += vmdaudio_loadsound(s, output_samples, p, 1);
536 else {
537 /* audio */
538 *data_size += vmdaudio_loadsound(s, output_samples, p, 0);
539 p += s->block_align;
541 output_samples += (s->block_align * s->bits / 8);
542 sound_flags >>= 1;
544 } else if (buf[6] == 3) {
545 /* silent chunk */
546 *data_size = vmdaudio_loadsound(s, output_samples, p, 1);
549 return buf_size;
554 * Public Data Structures
557 AVCodec vmdvideo_decoder = {
558 "vmdvideo",
559 CODEC_TYPE_VIDEO,
560 CODEC_ID_VMDVIDEO,
561 sizeof(VmdVideoContext),
562 vmdvideo_decode_init,
563 NULL,
564 vmdvideo_decode_end,
565 vmdvideo_decode_frame,
566 CODEC_CAP_DR1,
569 AVCodec vmdaudio_decoder = {
570 "vmdaudio",
571 CODEC_TYPE_AUDIO,
572 CODEC_ID_VMDAUDIO,
573 sizeof(VmdAudioContext),
574 vmdaudio_decode_init,
575 NULL,
576 NULL,
577 vmdaudio_decode_frame,