Statistics
| Branch: | Revision:

ffmpeg / libavformat / wav.c @ c4f8d5d9

History | View | Annotate | Download (10.9 KB)

1 de6d9b64 Fabrice Bellard
/* 
2
 * WAV encoder and decoder
3 e095026a Fabrice Bellard
 * Copyright (c) 2001, 2002 Fabrice Bellard.
4 de6d9b64 Fabrice Bellard
 *
5 19720f15 Fabrice Bellard
 * 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 de6d9b64 Fabrice Bellard
 *
10 19720f15 Fabrice Bellard
 * This library is distributed in the hope that it will be useful,
11 de6d9b64 Fabrice Bellard
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12 19720f15 Fabrice Bellard
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
13
 * Lesser General Public License for more details.
14 de6d9b64 Fabrice Bellard
 *
15 19720f15 Fabrice Bellard
 * 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 de6d9b64 Fabrice Bellard
 */
19
#include "avformat.h"
20
#include "avi.h"
21
22 e8c00089 Zdenek Kabelac
const CodecTag codec_wav_tags[] = {
23 46a3d068 Fabrice Bellard
    { CODEC_ID_MP2, 0x50 },
24 80783dc2 Fabrice Bellard
    { CODEC_ID_MP3, 0x55 },
25 46a3d068 Fabrice Bellard
    { CODEC_ID_AC3, 0x2000 },
26
    { CODEC_ID_PCM_S16LE, 0x01 },
27
    { CODEC_ID_PCM_U8, 0x01 }, /* must come after s16le in this list */
28
    { CODEC_ID_PCM_ALAW, 0x06 },
29
    { CODEC_ID_PCM_MULAW, 0x07 },
30 0147f198 François Revol
    { CODEC_ID_ADPCM_MS, 0x02 },
31
    { CODEC_ID_ADPCM_IMA_WAV, 0x11 },
32 e5966052 Roman Shaposhnik
    { CODEC_ID_ADPCM_G726, 0x45 },
33 048446bf Mike Melanson
    { CODEC_ID_ADPCM_IMA_DK4, 0x61 },  /* rogue format number */
34
    { CODEC_ID_ADPCM_IMA_DK3, 0x62 },  /* rogue format number */
35 e095026a Fabrice Bellard
    { CODEC_ID_WMAV1, 0x160 },
36
    { CODEC_ID_WMAV2, 0x161 },
37 2926db54 Michael Niedermayer
    { CODEC_ID_VORBIS, ('V'<<8)+'o' }, //HACK/FIXME, does vorbis in WAV/AVI have an (in)official id?
38 7251dfaf Alex Beregszaszi
    { CODEC_ID_SONIC, 0x2048 },
39
    { CODEC_ID_SONIC_LS, 0x2048 },
40 46a3d068 Fabrice Bellard
    { 0, 0 },
41
};
42
43 764ef400 Mike Melanson
#ifdef CONFIG_ENCODERS
44 46a3d068 Fabrice Bellard
/* WAVEFORMATEX header */
45 afc25d93 Philip Gladstone
/* returns the size or -1 on error */
46 46a3d068 Fabrice Bellard
int put_wav_header(ByteIOContext *pb, AVCodecContext *enc)
47
{
48 bd5a6020 Michael Niedermayer
    int bps, blkalign, bytespersec;
49 afc25d93 Philip Gladstone
    int hdrsize = 18;
50 46a3d068 Fabrice Bellard
51 bd5a6020 Michael Niedermayer
    if(!enc->codec_tag)
52
       enc->codec_tag = codec_get_tag(codec_wav_tags, enc->codec_id);
53
    if(!enc->codec_tag)
54 46a3d068 Fabrice Bellard
        return -1;
55 bd5a6020 Michael Niedermayer
56
    put_le16(pb, enc->codec_tag);
57 46a3d068 Fabrice Bellard
    put_le16(pb, enc->channels);
58
    put_le32(pb, enc->sample_rate);
59
    if (enc->codec_id == CODEC_ID_PCM_U8 ||
60
        enc->codec_id == CODEC_ID_PCM_ALAW ||
61
        enc->codec_id == CODEC_ID_PCM_MULAW) {
62
        bps = 8;
63 80783dc2 Fabrice Bellard
    } else if (enc->codec_id == CODEC_ID_MP2 || enc->codec_id == CODEC_ID_MP3) {
64 86bec9a1 Juanjo
        bps = 0;
65 889c5224 François Revol
    } else if (enc->codec_id == CODEC_ID_ADPCM_IMA_WAV || enc->codec_id == CODEC_ID_ADPCM_MS) {
66
        bps = 4;
67 46a3d068 Fabrice Bellard
    } else {
68
        bps = 16;
69
    }
70
    
71 80783dc2 Fabrice Bellard
    if (enc->codec_id == CODEC_ID_MP2 || enc->codec_id == CODEC_ID_MP3) {
72 86bec9a1 Juanjo
        blkalign = 1;
73 4b1f4f23 Juanjo
        //blkalign = 144 * enc->bit_rate/enc->sample_rate;
74 0147f198 François Revol
    } else if (enc->block_align != 0) { /* specified by the codec */
75
        blkalign = enc->block_align;
76 4b1f4f23 Juanjo
    } else
77 86bec9a1 Juanjo
        blkalign = enc->channels*bps >> 3;
78
    if (enc->codec_id == CODEC_ID_PCM_U8 ||
79
        enc->codec_id == CODEC_ID_PCM_S16LE) {
80
        bytespersec = enc->sample_rate * blkalign;
81
    } else {
82
        bytespersec = enc->bit_rate / 8;
83
    }
84
    put_le32(pb, bytespersec); /* bytes per second */
85
    put_le16(pb, blkalign); /* block align */
86
    put_le16(pb, bps); /* bits per sample */
87 80783dc2 Fabrice Bellard
    if (enc->codec_id == CODEC_ID_MP3) {
88 86bec9a1 Juanjo
        put_le16(pb, 12); /* wav_extra_size */
89 afc25d93 Philip Gladstone
        hdrsize += 12;
90 86bec9a1 Juanjo
        put_le16(pb, 1); /* wID */
91
        put_le32(pb, 2); /* fdwFlags */
92
        put_le16(pb, 1152); /* nBlockSize */
93
        put_le16(pb, 1); /* nFramesPerBlock */
94
        put_le16(pb, 1393); /* nCodecDelay */
95 4b1f4f23 Juanjo
    } else if (enc->codec_id == CODEC_ID_MP2) {
96
        put_le16(pb, 22); /* wav_extra_size */
97 afc25d93 Philip Gladstone
        hdrsize += 22;
98 4b1f4f23 Juanjo
        put_le16(pb, 2);  /* fwHeadLayer */
99
        put_le32(pb, enc->bit_rate); /* dwHeadBitrate */
100
        put_le16(pb, enc->channels == 2 ? 1 : 8); /* fwHeadMode */
101
        put_le16(pb, 0);  /* fwHeadModeExt */
102
        put_le16(pb, 1);  /* wHeadEmphasis */
103
        put_le16(pb, 16); /* fwHeadFlags */
104
        put_le32(pb, 0);  /* dwPTSLow */
105
        put_le32(pb, 0);  /* dwPTSHigh */
106 889c5224 François Revol
    } else if (enc->codec_id == CODEC_ID_ADPCM_IMA_WAV) {
107
        put_le16(pb, 2); /* wav_extra_size */
108 361b1b86 Brian Becker
        hdrsize += 2;
109 889c5224 François Revol
        put_le16(pb, ((enc->block_align - 4 * enc->channels) / (4 * enc->channels)) * 8 + 1); /* wSamplesPerBlock */
110 c4f8d5d9 Michael Niedermayer
    } else if(enc->extradata_size){
111 6748046e Michael Niedermayer
        put_le16(pb, enc->extradata_size);
112
        put_buffer(pb, enc->extradata, enc->extradata_size);
113
        hdrsize += enc->extradata_size;
114 5062c202 Michael Niedermayer
        if(hdrsize&1){
115
            hdrsize++;
116
            put_byte(pb, 0);
117
        }
118 c4f8d5d9 Michael Niedermayer
    } else {
119
        hdrsize -= 2;
120 6748046e Michael Niedermayer
    }
121 86bec9a1 Juanjo
122 afc25d93 Philip Gladstone
    return hdrsize;
123 46a3d068 Fabrice Bellard
}
124 764ef400 Mike Melanson
#endif //CONFIG_ENCODERS
125 46a3d068 Fabrice Bellard
126 2e7973bb Roman Shaposhnik
/* We could be given one of the three possible structures here:
127
 * WAVEFORMAT, PCMWAVEFORMAT or WAVEFORMATEX. Each structure
128
 * is an expansion of the previous one with the fields added
129
 * at the bottom. PCMWAVEFORMAT adds 'WORD wBitsPerSample' and
130
 * WAVEFORMATEX adds 'WORD  cbSize' and basically makes itself
131
 * an openended structure.
132
 */
133
void get_wav_header(ByteIOContext *pb, AVCodecContext *codec, int size) 
134 e095026a Fabrice Bellard
{
135 e8c00089 Zdenek Kabelac
    int id;
136 e095026a Fabrice Bellard
137
    id = get_le16(pb);
138
    codec->codec_type = CODEC_TYPE_AUDIO;
139
    codec->codec_tag = id;
140
    codec->channels = get_le16(pb);
141
    codec->sample_rate = get_le32(pb);
142
    codec->bit_rate = get_le32(pb) * 8;
143
    codec->block_align = get_le16(pb);
144 2e7973bb Roman Shaposhnik
    if (size == 14) {  /* We're dealing with plain vanilla WAVEFORMAT */
145
        codec->bits_per_sample = 8;
146 e0ece99a Michael Niedermayer
    }else
147
        codec->bits_per_sample = get_le16(pb);
148
    codec->codec_id = wav_codec_get_id(id, codec->bits_per_sample);
149 f9b5459e Fabrice Bellard
    
150 2e7973bb Roman Shaposhnik
    if (size > 16) {  /* We're obviously dealing with WAVEFORMATEX */
151 cb146faf Zdenek Kabelac
        codec->extradata_size = get_le16(pb);
152
        if (codec->extradata_size > 0) {
153 2e7973bb Roman Shaposhnik
            if (codec->extradata_size > size - 18)
154
                codec->extradata_size = size - 18;
155 cb146faf Zdenek Kabelac
            codec->extradata = av_mallocz(codec->extradata_size);
156
            get_buffer(pb, codec->extradata, codec->extradata_size);
157 2e7973bb Roman Shaposhnik
        } else
158
            codec->extradata_size = 0;
159
        
160
        /* It is possible for the chunk to contain garbage at the end */
161
        if (size - codec->extradata_size - 18 > 0)
162
            url_fskip(pb, size - codec->extradata_size - 18);
163 e095026a Fabrice Bellard
    }
164
}
165
166
167 46a3d068 Fabrice Bellard
int wav_codec_get_id(unsigned int tag, int bps)
168
{
169
    int id;
170
    id = codec_get_id(codec_wav_tags, tag);
171
    if (id <= 0)
172
        return id;
173
    /* handle specific u8 codec */
174
    if (id == CODEC_ID_PCM_S16LE && bps == 8)
175
        id = CODEC_ID_PCM_U8;
176
    return id;
177
}
178
179 764ef400 Mike Melanson
#ifdef CONFIG_ENCODERS
180 de6d9b64 Fabrice Bellard
typedef struct {
181
    offset_t data;
182
} WAVContext;
183
184
static int wav_write_header(AVFormatContext *s)
185
{
186 c9a65ca8 Fabrice Bellard
    WAVContext *wav = s->priv_data;
187 de6d9b64 Fabrice Bellard
    ByteIOContext *pb = &s->pb;
188
    offset_t fmt;
189
190
    put_tag(pb, "RIFF");
191
    put_le32(pb, 0); /* file length */
192
    put_tag(pb, "WAVE");
193
194
    /* format header */
195
    fmt = start_tag(pb, "fmt ");
196 46a3d068 Fabrice Bellard
    if (put_wav_header(pb, &s->streams[0]->codec) < 0) {
197 1ea4f593 Fabrice Bellard
        av_free(wav);
198 46a3d068 Fabrice Bellard
        return -1;
199
    }
200 de6d9b64 Fabrice Bellard
    end_tag(pb, fmt);
201
202
    /* data header */
203
    wav->data = start_tag(pb, "data");
204
    
205
    put_flush_packet(pb);
206
207
    return 0;
208
}
209
210 e928649b Michael Niedermayer
static int wav_write_packet(AVFormatContext *s, AVPacket *pkt)
211 de6d9b64 Fabrice Bellard
{
212
    ByteIOContext *pb = &s->pb;
213 e928649b Michael Niedermayer
    put_buffer(pb, pkt->data, pkt->size);
214 de6d9b64 Fabrice Bellard
    return 0;
215
}
216
217
static int wav_write_trailer(AVFormatContext *s)
218
{
219
    ByteIOContext *pb = &s->pb;
220
    WAVContext *wav = s->priv_data;
221
    offset_t file_size;
222
223
    if (!url_is_streamed(&s->pb)) {
224
        end_tag(pb, wav->data);
225
226
        /* update file size */
227
        file_size = url_ftell(pb);
228
        url_fseek(pb, 4, SEEK_SET);
229 0c1a9eda Zdenek Kabelac
        put_le32(pb, (uint32_t)(file_size - 8));
230 de6d9b64 Fabrice Bellard
        url_fseek(pb, file_size, SEEK_SET);
231
232
        put_flush_packet(pb);
233
    }
234
    return 0;
235
}
236 764ef400 Mike Melanson
#endif //CONFIG_ENCODERS
237 de6d9b64 Fabrice Bellard
238
/* return the size of the found tag */
239
/* XXX: > 2GB ? */
240 0c1a9eda Zdenek Kabelac
static int find_tag(ByteIOContext *pb, uint32_t tag1)
241 de6d9b64 Fabrice Bellard
{
242
    unsigned int tag;
243
    int size;
244
245
    for(;;) {
246
        if (url_feof(pb))
247
            return -1;
248
        tag = get_le32(pb);
249
        size = get_le32(pb);
250
        if (tag == tag1)
251
            break;
252
        url_fseek(pb, size, SEEK_CUR);
253
    }
254
    if (size < 0)
255
        size = 0x7fffffff;
256
    return size;
257
}
258
259 c9a65ca8 Fabrice Bellard
static int wav_probe(AVProbeData *p)
260
{
261
    /* check file header */
262
    if (p->buf_size <= 32)
263
        return 0;
264
    if (p->buf[0] == 'R' && p->buf[1] == 'I' &&
265
        p->buf[2] == 'F' && p->buf[3] == 'F' &&
266
        p->buf[8] == 'W' && p->buf[9] == 'A' &&
267
        p->buf[10] == 'V' && p->buf[11] == 'E')
268
        return AVPROBE_SCORE_MAX;
269
    else
270
        return 0;
271
}
272
273 de6d9b64 Fabrice Bellard
/* wav input */
274
static int wav_read_header(AVFormatContext *s,
275
                           AVFormatParameters *ap)
276
{
277
    int size;
278
    unsigned int tag;
279
    ByteIOContext *pb = &s->pb;
280
    AVStream *st;
281
282
    /* check RIFF header */
283
    tag = get_le32(pb);
284
285
    if (tag != MKTAG('R', 'I', 'F', 'F'))
286
        return -1;
287
    get_le32(pb); /* file size */
288
    tag = get_le32(pb);
289
    if (tag != MKTAG('W', 'A', 'V', 'E'))
290
        return -1;
291
    
292
    /* parse fmt header */
293
    size = find_tag(pb, MKTAG('f', 'm', 't', ' '));
294
    if (size < 0)
295
        return -1;
296 c9a65ca8 Fabrice Bellard
    st = av_new_stream(s, 0);
297 de6d9b64 Fabrice Bellard
    if (!st)
298 c9a65ca8 Fabrice Bellard
        return AVERROR_NOMEM;
299 de6d9b64 Fabrice Bellard
300 2e7973bb Roman Shaposhnik
    get_wav_header(pb, &st->codec, size);
301 f9b5459e Fabrice Bellard
    st->need_parsing = 1;
302 e095026a Fabrice Bellard
    
303
    size = find_tag(pb, MKTAG('d', 'a', 't', 'a'));
304
    if (size < 0)
305
        return -1;
306 de6d9b64 Fabrice Bellard
    return 0;
307
}
308
309
#define MAX_SIZE 4096
310
311
static int wav_read_packet(AVFormatContext *s,
312
                           AVPacket *pkt)
313
{
314 f9b5459e Fabrice Bellard
    int ret, size;
315
    AVStream *st;
316 de6d9b64 Fabrice Bellard
317
    if (url_feof(&s->pb))
318 0bd586c5 Mike Melanson
        return AVERROR_IO;
319 f9b5459e Fabrice Bellard
    st = s->streams[0];
320
321
    size = MAX_SIZE;
322
    if (st->codec.block_align > 1) {
323
        if (size < st->codec.block_align)
324
            size = st->codec.block_align;
325
        size = (size / st->codec.block_align) * st->codec.block_align;
326
    }
327
    if (av_new_packet(pkt, size))
328 0bd586c5 Mike Melanson
        return AVERROR_IO;
329 de6d9b64 Fabrice Bellard
    pkt->stream_index = 0;
330
331
    ret = get_buffer(&s->pb, pkt->data, pkt->size);
332
    if (ret < 0)
333
        av_free_packet(pkt);
334 5ed8fafc Fabrice Bellard
    /* note: we need to modify the packet size here to handle the last
335
       packet */
336
    pkt->size = ret;
337 de6d9b64 Fabrice Bellard
    return ret;
338
}
339
340
static int wav_read_close(AVFormatContext *s)
341
{
342
    return 0;
343
}
344
345 f9b5459e Fabrice Bellard
static int wav_read_seek(AVFormatContext *s, 
346
                         int stream_index, int64_t timestamp)
347
{
348
    AVStream *st;
349
350
    st = s->streams[0];
351
    switch(st->codec.codec_id) {
352
    case CODEC_ID_MP2:
353
    case CODEC_ID_MP3:
354
    case CODEC_ID_AC3:
355 23c99253 Michael Niedermayer
    case CODEC_ID_DTS:
356 f9b5459e Fabrice Bellard
        /* use generic seeking with dynamically generated indexes */
357
        return -1;
358
    default:
359
        break;
360
    }
361
    return pcm_read_seek(s, stream_index, timestamp);
362
}
363
364
365 c9a65ca8 Fabrice Bellard
static AVInputFormat wav_iformat = {
366
    "wav",
367
    "wav format",
368
    0,
369
    wav_probe,
370
    wav_read_header,
371
    wav_read_packet,
372
    wav_read_close,
373 f9b5459e Fabrice Bellard
    wav_read_seek,
374 c9a65ca8 Fabrice Bellard
};
375
376 764ef400 Mike Melanson
#ifdef CONFIG_ENCODERS
377 c9a65ca8 Fabrice Bellard
static AVOutputFormat wav_oformat = {
378 de6d9b64 Fabrice Bellard
    "wav",
379
    "wav format",
380
    "audio/x-wav",
381
    "wav",
382 c9a65ca8 Fabrice Bellard
    sizeof(WAVContext),
383 5ed8fafc Fabrice Bellard
    CODEC_ID_PCM_S16LE,
384 de6d9b64 Fabrice Bellard
    CODEC_ID_NONE,
385
    wav_write_header,
386
    wav_write_packet,
387
    wav_write_trailer,
388
};
389 764ef400 Mike Melanson
#endif //CONFIG_ENCODERS
390 c9a65ca8 Fabrice Bellard
391 c3775e54 Ronald S. Bultje
int ff_wav_init(void)
392 c9a65ca8 Fabrice Bellard
{
393
    av_register_input_format(&wav_iformat);
394 764ef400 Mike Melanson
#ifdef CONFIG_ENCODERS
395 c9a65ca8 Fabrice Bellard
    av_register_output_format(&wav_oformat);
396 764ef400 Mike Melanson
#endif //CONFIG_ENCODERS
397 c9a65ca8 Fabrice Bellard
    return 0;
398
}