Statistics
| Branch: | Revision:

ffmpeg / libavcodec / vmdav.c @ 650cec0c

History | View | Annotate | Download (16 KB)

1 fafa0b75 Mike Melanson
/*
2
 * Sierra VMD Audio & Video Decoders
3
 * Copyright (C) 2004 the ffmpeg project
4
 *
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.
9
 *
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.
14
 *
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
18
 *
19
 */
20
21
/**
22
 * @file vmdvideo.c
23
 * Sierra VMD audio & video decoders
24
 * by Vladimir "VAG" Gneushev (vagsoft at mail.ru)
25 23fe14bb Mike Melanson
 * for more information on the Sierra VMD format, visit:
26
 *   http://www.pcisys.net/~melanson/codecs/
27 fafa0b75 Mike Melanson
 *
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.
33
 *
34
 * The audio decoder, like the video decoder, expects each encoded data
35 23fe14bb Mike Melanson
 * chunk to be prepended with the appropriate 16-byte frame information
36 fafa0b75 Mike Melanson
 * 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.
39
 */
40
41
#include <stdio.h>
42
#include <stdlib.h>
43
#include <string.h>
44
#include <unistd.h>
45
46
#include "common.h"
47
#include "avcodec.h"
48
#include "dsputil.h"
49
50
#define VMD_HEADER_SIZE 0x330
51
#define PALETTE_COUNT 256
52
53
/*
54
 * Video Decoder
55
 */
56
57
typedef struct VmdVideoContext {
58
59
    AVCodecContext *avctx;
60
    DSPContext dsp;
61
    AVFrame frame;
62
    AVFrame prev_frame;
63
64
    unsigned char *buf;
65
    int size;
66
67
    unsigned char palette[PALETTE_COUNT * 4];
68
    unsigned char *unpack_buffer;
69
70
} VmdVideoContext;
71
72
#define QUEUE_SIZE 0x1000
73
#define QUEUE_MASK 0x0FFF
74
75
static void lz_unpack(unsigned char *src, unsigned char *dest)
76
{
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;
87
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 */
100
    }
101
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;
108
            }
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;
128
                    }
129
                    dataleft -= chainlen;
130
                }
131
                tag >>= 1;
132
            }
133
        }
134
    }
135
}
136
137
static int rle_unpack(unsigned char *src, unsigned char *dest, int len)
138
{
139
    unsigned char *ps;
140
    unsigned char *pd;
141
    int i, l;
142
143
    ps = src;
144
    pd = dest;
145
    if (len & 1)
146
        *pd++ = *ps++;
147
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];
161
            }
162
            ps += 2;
163
        }
164
        i += l;
165
    } while (i < len);
166
167
    return (ps - src);
168
}
169
170
static void vmd_decode(VmdVideoContext *s)
171
{
172
    int i;
173
    unsigned int *palette32;
174
    unsigned char r, g, b;
175
176
    /* point to the start of the encoded data */
177
    unsigned char *p = s->buf + 16;
178
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;
185
186
    int frame_x, frame_y;
187
    int frame_width, frame_height;
188
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;
193
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)) {
198
199
        memcpy(s->frame.data[0], s->prev_frame.data[0], 
200
            s->avctx->height * s->frame.linesize[0]);
201
    }
202
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);
212
        }
213
        s->size -= (256 * 3 + 2);
214
    }
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;
223
        }
224
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;
242
                    }
243
                } while (ofs < frame_width);
244
                if (ofs > frame_width) {
245 fd146758 Alex Beregszaszi
                    av_log(s->avctx, AV_LOG_ERROR, "VMD video: offset > width (%d > %d)\n",
246 fafa0b75 Mike Melanson
                        ofs, frame_width);
247
                    break;
248
                }
249
                dp += s->frame.linesize[0];
250
                pp += s->prev_frame.linesize[0];
251
            }
252
            break;
253
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];
260
            }
261
            break;
262
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 23fe14bb Mike Melanson
                            len = rle_unpack(pb, &dp[ofs], len);
272 fafa0b75 Mike Melanson
                        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;
280
                    }
281
                } while (ofs < frame_width);
282
                if (ofs > frame_width) {
283 fd146758 Alex Beregszaszi
                    av_log(s->avctx, AV_LOG_ERROR, "VMD video: offset > width (%d > %d)\n",
284 fafa0b75 Mike Melanson
                        ofs, frame_width);
285
                }
286
                dp += s->frame.linesize[0];
287
                pp += s->prev_frame.linesize[0];
288
            }
289
            break;
290
        }
291
    }
292
}
293
294
static int vmdvideo_decode_init(AVCodecContext *avctx)
295
{
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;
303
304
    s->avctx = avctx;
305
    avctx->pix_fmt = PIX_FMT_PAL8;
306
    avctx->has_b_frames = 0;
307
    dsputil_init(&s->dsp, avctx);
308
309
    /* make sure the VMD header made it */
310
    if (s->avctx->extradata_size != VMD_HEADER_SIZE) {
311 fd146758 Alex Beregszaszi
        av_log(s->avctx, AV_LOG_ERROR, "VMD video: expected extradata size of %d\n", 
312 fafa0b75 Mike Melanson
            VMD_HEADER_SIZE);
313
        return -1;
314
    }
315
    vmd_header = (unsigned char *)avctx->extradata;
316
317
    s->unpack_buffer = av_malloc(LE_32(&vmd_header[800]));
318
    if (!s->unpack_buffer)
319
        return -1;
320
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);
329
    }
330
331
    s->frame.data[0] = s->prev_frame.data[0] = NULL;
332
333
    return 0;
334
}
335
336
static int vmdvideo_decode_frame(AVCodecContext *avctx,
337
                                 void *data, int *data_size,
338
                                 uint8_t *buf, int buf_size)
339
{
340
    VmdVideoContext *s = (VmdVideoContext *)avctx->priv_data;
341
342
    s->buf = buf;
343
    s->size = buf_size;
344
345 23fe14bb Mike Melanson
    if (buf_size < 16)
346
        return buf_size;
347
348 fafa0b75 Mike Melanson
    s->frame.reference = 1;
349
    if (avctx->get_buffer(avctx, &s->frame)) {
350 fd146758 Alex Beregszaszi
        av_log(s->avctx, AV_LOG_ERROR, "VMD Video: get_buffer() failed\n");
351 fafa0b75 Mike Melanson
        return -1;
352
    }
353
354
    vmd_decode(s);
355
356
    /* make the palette available on the way out */
357
    memcpy(s->frame.data[1], s->palette, PALETTE_COUNT * 4);
358
359
    if (s->prev_frame.data[0])
360
        avctx->release_buffer(avctx, &s->prev_frame);
361
362
    /* shuffle frames */
363
    s->prev_frame = s->frame;
364
365
    *data_size = sizeof(AVFrame);
366
    *(AVFrame*)data = s->frame;
367
368
    /* report that the buffer was completely consumed */
369
    return buf_size;
370
}
371
372
static int vmdvideo_decode_end(AVCodecContext *avctx)
373
{
374
    VmdVideoContext *s = (VmdVideoContext *)avctx->priv_data;
375
376
    if (s->prev_frame.data[0])
377
        avctx->release_buffer(avctx, &s->prev_frame);
378
    av_free(s->unpack_buffer);
379
380
    return 0;
381
}
382
383
384
/*
385
 * Audio Decoder
386
 */
387
388
typedef struct VmdAudioContext {
389 fd146758 Alex Beregszaszi
    AVCodecContext *avctx;
390 fafa0b75 Mike Melanson
    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;
398
399
static int vmdaudio_decode_init(AVCodecContext *avctx)
400
{
401
    VmdAudioContext *s = (VmdAudioContext *)avctx->priv_data;
402
    int i;
403
404 fd146758 Alex Beregszaszi
    s->avctx = avctx;
405 fafa0b75 Mike Melanson
    s->channels = avctx->channels;
406
    s->bits = avctx->bits_per_sample;
407
    s->block_align = avctx->block_align;
408
409 fd146758 Alex Beregszaszi
    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);
411 fafa0b75 Mike Melanson
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;
418
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);
427
    }
428
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;
444
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];
449
    }
450
    for (i = 0; i < 128; i++)
451
      s->steps128[i + 128] = -s->steps128[i];
452
453
    return 0;
454
}
455
456
static void vmdaudio_decode_audio(VmdAudioContext *s, unsigned char *data,
457
    uint8_t *buf, int ratio) {
458
459
}
460
461 23fe14bb Mike Melanson
static int vmdaudio_loadsound(VmdAudioContext *s, unsigned char *data,
462 fafa0b75 Mike Melanson
    uint8_t *buf, int silence)
463
{
464 23fe14bb Mike Melanson
    int bytes_decoded = 0;
465
    int i;
466
467 fd146758 Alex Beregszaszi
    if (silence)
468
        av_log(s->avctx, AV_LOG_INFO, "silent block!\n");
469 fafa0b75 Mike Melanson
    if (s->channels == 2) {
470 23fe14bb Mike Melanson
471
        /* stereo handling */
472 fafa0b75 Mike Melanson
        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 23fe14bb Mike Melanson
            else
481
                vmdaudio_decode_audio(s, data, buf, 1);
482 fafa0b75 Mike Melanson
        }
483
    } else {
484 23fe14bb Mike Melanson
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;
495
            }
496
        } else {
497 98772cbf Mike Melanson
            /* 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;
501 23fe14bb Mike Melanson
        }
502 fafa0b75 Mike Melanson
    }
503 23fe14bb Mike Melanson
504
    return bytes_decoded;
505 fafa0b75 Mike Melanson
}
506
507
static int vmdaudio_decode_frame(AVCodecContext *avctx,
508
                                 void *data, int *data_size,
509
                                 uint8_t *buf, int buf_size)
510
{
511
    VmdAudioContext *s = (VmdAudioContext *)avctx->priv_data;
512
    unsigned int sound_flags;
513
    unsigned char *output_samples = (unsigned char *)data;
514
515
    /* point to the start of the encoded data */
516
    unsigned char *p = buf + 16;
517
    unsigned char *p_end = buf + buf_size;
518
519 23fe14bb Mike Melanson
    if (buf_size < 16)
520
        return buf_size;
521
522 fafa0b75 Mike Melanson
    if (buf[6] == 1) {
523
        /* the chunk contains audio */
524 23fe14bb Mike Melanson
        *data_size = vmdaudio_loadsound(s, output_samples, p, 0);
525 fafa0b75 Mike Melanson
    } else if (buf[6] == 2) {
526
        /* the chunk contains audio and silence mixed together */
527
        sound_flags = LE_32(p);
528
        p += 4;
529
530
        /* do something with extrabufs here? */
531
532
        while (p < p_end) {
533
            if (sound_flags & 0x01)
534
                /* silence */
535 23fe14bb Mike Melanson
                *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;
540
            }
541 fafa0b75 Mike Melanson
            output_samples += (s->block_align * s->bits / 8);
542
            sound_flags >>= 1;
543
        }
544
    } else if (buf[6] == 3) {
545
        /* silent chunk */
546 23fe14bb Mike Melanson
        *data_size = vmdaudio_loadsound(s, output_samples, p, 1);
547 fafa0b75 Mike Melanson
    }
548
549
    return buf_size;
550
}
551
552
553
/*
554
 * Public Data Structures
555
 */
556
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,
567
};
568
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,
578
};