Statistics
| Branch: | Revision:

ffmpeg / libavformat / westwood.c @ 20cd83e2

History | View | Annotate | Download (11.7 KB)

1 2fdf638b Mike Melanson
/*
2
 * Westwood Studios Multimedia Formats Demuxer (VQA, AUD)
3
 * Copyright (c) 2003 The ffmpeg Project
4
 *
5 b78e7197 Diego Biurrun
 * This file is part of FFmpeg.
6
 *
7
 * FFmpeg is free software; you can redistribute it and/or
8 2fdf638b Mike Melanson
 * modify it under the terms of the GNU Lesser General Public
9
 * License as published by the Free Software Foundation; either
10 b78e7197 Diego Biurrun
 * version 2.1 of the License, or (at your option) any later version.
11 2fdf638b Mike Melanson
 *
12 b78e7197 Diego Biurrun
 * FFmpeg is distributed in the hope that it will be useful,
13 2fdf638b Mike Melanson
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
14
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
15
 * Lesser General Public License for more details.
16
 *
17
 * You should have received a copy of the GNU Lesser General Public
18 b78e7197 Diego Biurrun
 * License along with FFmpeg; if not, write to the Free Software
19 5509bffa Diego Biurrun
 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
20 2fdf638b Mike Melanson
 */
21
22
/**
23
 * @file westwood.c
24
 * Westwood Studios VQA & AUD file demuxers
25
 * by Mike Melanson (melanson@pcisys.net)
26
 * for more information on the Westwood file formats, visit:
27
 *   http://www.pcisys.net/~melanson/codecs/
28
 *   http://www.geocities.com/SiliconValley/8682/aud3.txt
29
 *
30
 * Implementation note: There is no definite file signature for AUD files.
31
 * The demuxer uses a probabilistic strategy for content detection. This
32
 * entails performing sanity checks on certain header values in order to
33
 * qualify a file. Refer to wsaud_probe() for the precise parameters.
34
 */
35
36
#include "avformat.h"
37
38
#define AUD_HEADER_SIZE 12
39
#define AUD_CHUNK_PREAMBLE_SIZE 8
40
#define AUD_CHUNK_SIGNATURE 0x0000DEAF
41
42 3a278992 Mike Melanson
#define FORM_TAG MKBETAG('F', 'O', 'R', 'M')
43
#define WVQA_TAG MKBETAG('W', 'V', 'Q', 'A')
44
#define VQHD_TAG MKBETAG('V', 'Q', 'H', 'D')
45
#define FINF_TAG MKBETAG('F', 'I', 'N', 'F')
46
#define SND0_TAG MKBETAG('S', 'N', 'D', '0')
47
#define SND2_TAG MKBETAG('S', 'N', 'D', '2')
48
#define VQFR_TAG MKBETAG('V', 'Q', 'F', 'R')
49 2fdf638b Mike Melanson
50 20f269dc Mike Melanson
/* don't know what these tags are for, but acknowledge their existence */
51 3a278992 Mike Melanson
#define CINF_TAG MKBETAG('C', 'I', 'N', 'F')
52
#define CINH_TAG MKBETAG('C', 'I', 'N', 'H')
53
#define CIND_TAG MKBETAG('C', 'I', 'N', 'D')
54
#define PINF_TAG MKBETAG('P', 'I', 'N', 'F')
55
#define PINH_TAG MKBETAG('P', 'I', 'N', 'H')
56
#define PIND_TAG MKBETAG('P', 'I', 'N', 'D')
57 20f269dc Mike Melanson
58 2fdf638b Mike Melanson
#define VQA_HEADER_SIZE 0x2A
59
#define VQA_FRAMERATE 15
60
#define VQA_VIDEO_PTS_INC (90000 / VQA_FRAMERATE)
61
#define VQA_PREAMBLE_SIZE 8
62
63
typedef struct WsAudDemuxContext {
64
    int audio_samplerate;
65
    int audio_channels;
66
    int audio_bits;
67
    int audio_type;
68
    int audio_stream_index;
69
    int64_t audio_frame_counter;
70
} WsAudDemuxContext;
71
72
typedef struct WsVqaDemuxContext {
73
    int audio_samplerate;
74
    int audio_channels;
75
    int audio_bits;
76
77
    int audio_stream_index;
78
    int video_stream_index;
79
80
    int64_t audio_frame_counter;
81
    int64_t video_pts;
82
} WsVqaDemuxContext;
83
84
static int wsaud_probe(AVProbeData *p)
85
{
86
    int field;
87
88
    /* Probabilistic content detection strategy: There is no file signature
89
     * so perform sanity checks on various header parameters:
90
     *   8000 <= sample rate (16 bits) <= 48000  ==> 40001 acceptable numbers
91
     *   compression type (8 bits) = 1 or 99     ==> 2 acceptable numbers
92
     * There is a total of 24 bits. The number space contains 2^24 =
93
     * 16777216 numbers. There are 40001 * 2 = 80002 acceptable combinations
94
     * of numbers. There is a 80002/16777216 = 0.48% chance of a false
95
     * positive.
96
     */
97
98
    if (p->buf_size < AUD_HEADER_SIZE)
99
        return 0;
100
101
    /* check sample rate */
102
    field = LE_16(&p->buf[0]);
103
    if ((field < 8000) || (field > 48000))
104
        return 0;
105
106 115329f1 Diego Biurrun
    /* note: only check for WS IMA (type 99) right now since there is no
107 2fdf638b Mike Melanson
     * support for type 1 */
108
    if (p->buf[11] != 99)
109
        return 0;
110
111
    /* return 1/2 certainty since this file check is a little sketchy */
112
    return AVPROBE_SCORE_MAX / 2;
113
}
114
115
static int wsaud_read_header(AVFormatContext *s,
116
                             AVFormatParameters *ap)
117
{
118
    WsAudDemuxContext *wsaud = (WsAudDemuxContext *)s->priv_data;
119
    ByteIOContext *pb = &s->pb;
120
    AVStream *st;
121
    unsigned char header[AUD_HEADER_SIZE];
122
123
    if (get_buffer(pb, header, AUD_HEADER_SIZE) != AUD_HEADER_SIZE)
124 0bd586c5 Mike Melanson
        return AVERROR_IO;
125 2fdf638b Mike Melanson
    wsaud->audio_samplerate = LE_16(&header[0]);
126
    if (header[11] == 99)
127
        wsaud->audio_type = CODEC_ID_ADPCM_IMA_WS;
128
    else
129
        return AVERROR_INVALIDDATA;
130
131
    /* flag 0 indicates stereo */
132
    wsaud->audio_channels = (header[10] & 0x1) + 1;
133
    /* flag 1 indicates 16 bit audio */
134
    wsaud->audio_bits = (((header[10] & 0x2) >> 1) + 1) * 8;
135
136
    /* initialize the audio decoder stream */
137
    st = av_new_stream(s, 0);
138
    if (!st)
139
        return AVERROR_NOMEM;
140 9ee91c2f Michael Niedermayer
    av_set_pts_info(st, 33, 1, wsaud->audio_samplerate);
141 01f4895c Michael Niedermayer
    st->codec->codec_type = CODEC_TYPE_AUDIO;
142
    st->codec->codec_id = wsaud->audio_type;
143
    st->codec->codec_tag = 0;  /* no tag */
144
    st->codec->channels = wsaud->audio_channels;
145
    st->codec->sample_rate = wsaud->audio_samplerate;
146
    st->codec->bits_per_sample = wsaud->audio_bits;
147
    st->codec->bit_rate = st->codec->channels * st->codec->sample_rate *
148
        st->codec->bits_per_sample / 4;
149
    st->codec->block_align = st->codec->channels * st->codec->bits_per_sample;
150 2fdf638b Mike Melanson
151
    wsaud->audio_stream_index = st->index;
152
    wsaud->audio_frame_counter = 0;
153
154
    return 0;
155
}
156
157
static int wsaud_read_packet(AVFormatContext *s,
158
                             AVPacket *pkt)
159
{
160
    WsAudDemuxContext *wsaud = (WsAudDemuxContext *)s->priv_data;
161
    ByteIOContext *pb = &s->pb;
162
    unsigned char preamble[AUD_CHUNK_PREAMBLE_SIZE];
163
    unsigned int chunk_size;
164
    int ret = 0;
165
166
    if (get_buffer(pb, preamble, AUD_CHUNK_PREAMBLE_SIZE) !=
167
        AUD_CHUNK_PREAMBLE_SIZE)
168 0bd586c5 Mike Melanson
        return AVERROR_IO;
169 2fdf638b Mike Melanson
170
    /* validate the chunk */
171
    if (LE_32(&preamble[4]) != AUD_CHUNK_SIGNATURE)
172
        return AVERROR_INVALIDDATA;
173
174
    chunk_size = LE_16(&preamble[0]);
175 2692067a Michael Niedermayer
    ret= av_get_packet(pb, pkt, chunk_size);
176
    if (ret != chunk_size)
177 0bd586c5 Mike Melanson
        return AVERROR_IO;
178 2fdf638b Mike Melanson
    pkt->stream_index = wsaud->audio_stream_index;
179
    pkt->pts = wsaud->audio_frame_counter;
180
    pkt->pts /= wsaud->audio_samplerate;
181
182
    /* 2 samples/byte, 1 or 2 samples per frame depending on stereo */
183
    wsaud->audio_frame_counter += (chunk_size * 2) / wsaud->audio_channels;
184
185
    return ret;
186
}
187
188
static int wsaud_read_close(AVFormatContext *s)
189
{
190
//    WsAudDemuxContext *wsaud = (WsAudDemuxContext *)s->priv_data;
191
192
    return 0;
193
}
194
195
196
static int wsvqa_probe(AVProbeData *p)
197
{
198
    /* need 12 bytes to qualify */
199
    if (p->buf_size < 12)
200
        return 0;
201
202
    /* check for the VQA signatures */
203
    if ((BE_32(&p->buf[0]) != FORM_TAG) ||
204
        (BE_32(&p->buf[8]) != WVQA_TAG))
205
        return 0;
206
207
    return AVPROBE_SCORE_MAX;
208
}
209
210
static int wsvqa_read_header(AVFormatContext *s,
211
                             AVFormatParameters *ap)
212
{
213
    WsVqaDemuxContext *wsvqa = (WsVqaDemuxContext *)s->priv_data;
214
    ByteIOContext *pb = &s->pb;
215
    AVStream *st;
216
    unsigned char *header;
217
    unsigned char scratch[VQA_PREAMBLE_SIZE];
218 20f269dc Mike Melanson
    unsigned int chunk_tag;
219
    unsigned int chunk_size;
220 2fdf638b Mike Melanson
221
    /* initialize the video decoder stream */
222
    st = av_new_stream(s, 0);
223
    if (!st)
224
        return AVERROR_NOMEM;
225 9ee91c2f Michael Niedermayer
    av_set_pts_info(st, 33, 1, 90000);
226 2fdf638b Mike Melanson
    wsvqa->video_stream_index = st->index;
227 01f4895c Michael Niedermayer
    st->codec->codec_type = CODEC_TYPE_VIDEO;
228
    st->codec->codec_id = CODEC_ID_WS_VQA;
229
    st->codec->codec_tag = 0;  /* no fourcc */
230 2fdf638b Mike Melanson
231
    /* skip to the start of the VQA header */
232
    url_fseek(pb, 20, SEEK_SET);
233
234
    /* the VQA header needs to go to the decoder */
235 01f4895c Michael Niedermayer
    st->codec->extradata_size = VQA_HEADER_SIZE;
236 d76319b1 Jindřich Makovička
    st->codec->extradata = av_mallocz(VQA_HEADER_SIZE + FF_INPUT_BUFFER_PADDING_SIZE);
237 01f4895c Michael Niedermayer
    header = (unsigned char *)st->codec->extradata;
238
    if (get_buffer(pb, st->codec->extradata, VQA_HEADER_SIZE) !=
239 2fdf638b Mike Melanson
        VQA_HEADER_SIZE) {
240 01f4895c Michael Niedermayer
        av_free(st->codec->extradata);
241 0bd586c5 Mike Melanson
        return AVERROR_IO;
242 2fdf638b Mike Melanson
    }
243 01f4895c Michael Niedermayer
    st->codec->width = LE_16(&header[6]);
244
    st->codec->height = LE_16(&header[8]);
245 2fdf638b Mike Melanson
246 20f269dc Mike Melanson
    /* initialize the audio decoder stream is sample rate is non-zero */
247
    if (LE_16(&header[24])) {
248
        st = av_new_stream(s, 0);
249
        if (!st)
250
            return AVERROR_NOMEM;
251 9ee91c2f Michael Niedermayer
        av_set_pts_info(st, 33, 1, 90000);
252 01f4895c Michael Niedermayer
        st->codec->codec_type = CODEC_TYPE_AUDIO;
253
        st->codec->codec_id = CODEC_ID_ADPCM_IMA_WS;
254
        st->codec->codec_tag = 0;  /* no tag */
255
        st->codec->sample_rate = LE_16(&header[24]);
256
        st->codec->channels = header[26];
257
        st->codec->bits_per_sample = 16;
258
        st->codec->bit_rate = st->codec->channels * st->codec->sample_rate *
259
            st->codec->bits_per_sample / 4;
260
        st->codec->block_align = st->codec->channels * st->codec->bits_per_sample;
261 20f269dc Mike Melanson
262
        wsvqa->audio_stream_index = st->index;
263 01f4895c Michael Niedermayer
        wsvqa->audio_samplerate = st->codec->sample_rate;
264
        wsvqa->audio_channels = st->codec->channels;
265 20f269dc Mike Melanson
        wsvqa->audio_frame_counter = 0;
266
    }
267 2fdf638b Mike Melanson
268 20f269dc Mike Melanson
    /* there are 0 or more chunks before the FINF chunk; iterate until
269
     * FINF has been skipped and the file will be ready to be demuxed */
270
    do {
271
        if (get_buffer(pb, scratch, VQA_PREAMBLE_SIZE) != VQA_PREAMBLE_SIZE) {
272 01f4895c Michael Niedermayer
            av_free(st->codec->extradata);
273 0bd586c5 Mike Melanson
            return AVERROR_IO;
274 20f269dc Mike Melanson
        }
275
        chunk_tag = BE_32(&scratch[0]);
276
        chunk_size = BE_32(&scratch[4]);
277
278
        /* catch any unknown header tags, for curiousity */
279
        switch (chunk_tag) {
280
        case CINF_TAG:
281
        case CINH_TAG:
282
        case CIND_TAG:
283
        case PINF_TAG:
284
        case PINH_TAG:
285
        case PIND_TAG:
286
        case FINF_TAG:
287
            break;
288
289
        default:
290 bc874dae Michel Bardiaux
            av_log (s, AV_LOG_ERROR, " note: unknown chunk seen (%c%c%c%c)\n",
291 20f269dc Mike Melanson
                scratch[0], scratch[1],
292
                scratch[2], scratch[3]);
293
            break;
294
        }
295
296
        url_fseek(pb, chunk_size, SEEK_CUR);
297
    } while (chunk_tag != FINF_TAG);
298 2fdf638b Mike Melanson
299
    wsvqa->video_pts = wsvqa->audio_frame_counter = 0;
300
301
    return 0;
302
}
303
304
static int wsvqa_read_packet(AVFormatContext *s,
305
                             AVPacket *pkt)
306
{
307
    WsVqaDemuxContext *wsvqa = (WsVqaDemuxContext *)s->priv_data;
308
    ByteIOContext *pb = &s->pb;
309
    int ret = 0;
310
    unsigned char preamble[VQA_PREAMBLE_SIZE];
311
    unsigned int chunk_type;
312
    unsigned int chunk_size;
313
    int skip_byte;
314
315
    if (get_buffer(pb, preamble, VQA_PREAMBLE_SIZE) != VQA_PREAMBLE_SIZE)
316 0bd586c5 Mike Melanson
        return AVERROR_IO;
317 2fdf638b Mike Melanson
318
    chunk_type = BE_32(&preamble[0]);
319
    chunk_size = BE_32(&preamble[4]);
320
    skip_byte = chunk_size & 0x01;
321
322
    if ((chunk_type == SND2_TAG) || (chunk_type == VQFR_TAG)) {
323
324 2692067a Michael Niedermayer
        av_get_packet(pb, pkt, chunk_size);
325 2fdf638b Mike Melanson
        if (ret != chunk_size) {
326 0bd586c5 Mike Melanson
            ret = AVERROR_IO;
327 2fdf638b Mike Melanson
        }
328
329
        if (chunk_type == SND2_TAG) {
330
            pkt->stream_index = wsvqa->audio_stream_index;
331
332
            pkt->pts = 90000;
333
            pkt->pts *= wsvqa->audio_frame_counter;
334
            pkt->pts /= wsvqa->audio_samplerate;
335
336
            /* 2 samples/byte, 1 or 2 samples per frame depending on stereo */
337 115329f1 Diego Biurrun
            wsvqa->audio_frame_counter += (chunk_size * 2) /
338 2fdf638b Mike Melanson
                wsvqa->audio_channels;
339
        } else {
340
            pkt->stream_index = wsvqa->video_stream_index;
341
            pkt->pts = wsvqa->video_pts;
342
            wsvqa->video_pts += VQA_VIDEO_PTS_INC;
343
        }
344
345
    } else
346
        return AVERROR_INVALIDDATA;
347
348
    /* stay on 16-bit alignment */
349
    if (skip_byte)
350
        url_fseek(pb, 1, SEEK_CUR);
351
352
    return ret;
353
}
354
355
static int wsvqa_read_close(AVFormatContext *s)
356
{
357
//    WsVqaDemuxContext *wsvqa = (WsVqaDemuxContext *)s->priv_data;
358
359
    return 0;
360
}
361
362 ff70e601 Måns Rullgård
#ifdef CONFIG_WSAUD_DEMUXER
363
AVInputFormat wsaud_demuxer = {
364 2fdf638b Mike Melanson
    "wsaud",
365
    "Westwood Studios audio format",
366
    sizeof(WsAudDemuxContext),
367
    wsaud_probe,
368
    wsaud_read_header,
369
    wsaud_read_packet,
370
    wsaud_read_close,
371
};
372 ff70e601 Måns Rullgård
#endif
373
#ifdef CONFIG_WSVQA_DEMUXER
374
AVInputFormat wsvqa_demuxer = {
375 2fdf638b Mike Melanson
    "wsvqa",
376
    "Westwood Studios VQA format",
377
    sizeof(WsVqaDemuxContext),
378
    wsvqa_probe,
379
    wsvqa_read_header,
380
    wsvqa_read_packet,
381
    wsvqa_read_close,
382
};
383 ff70e601 Måns Rullgård
#endif