Statistics
| Branch: | Revision:

ffmpeg / libavformat / wav.c @ c4f8d5d9

History | View | Annotate | Download (10.9 KB)

1
/* 
2
 * WAV encoder and decoder
3
 * Copyright (c) 2001, 2002 Fabrice Bellard.
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
#include "avformat.h"
20
#include "avi.h"
21

    
22
const CodecTag codec_wav_tags[] = {
23
    { CODEC_ID_MP2, 0x50 },
24
    { CODEC_ID_MP3, 0x55 },
25
    { 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
    { CODEC_ID_ADPCM_MS, 0x02 },
31
    { CODEC_ID_ADPCM_IMA_WAV, 0x11 },
32
    { CODEC_ID_ADPCM_G726, 0x45 },
33
    { CODEC_ID_ADPCM_IMA_DK4, 0x61 },  /* rogue format number */
34
    { CODEC_ID_ADPCM_IMA_DK3, 0x62 },  /* rogue format number */
35
    { CODEC_ID_WMAV1, 0x160 },
36
    { CODEC_ID_WMAV2, 0x161 },
37
    { CODEC_ID_VORBIS, ('V'<<8)+'o' }, //HACK/FIXME, does vorbis in WAV/AVI have an (in)official id?
38
    { CODEC_ID_SONIC, 0x2048 },
39
    { CODEC_ID_SONIC_LS, 0x2048 },
40
    { 0, 0 },
41
};
42

    
43
#ifdef CONFIG_ENCODERS
44
/* WAVEFORMATEX header */
45
/* returns the size or -1 on error */
46
int put_wav_header(ByteIOContext *pb, AVCodecContext *enc)
47
{
48
    int bps, blkalign, bytespersec;
49
    int hdrsize = 18;
50

    
51
    if(!enc->codec_tag)
52
       enc->codec_tag = codec_get_tag(codec_wav_tags, enc->codec_id);
53
    if(!enc->codec_tag)
54
        return -1;
55

    
56
    put_le16(pb, enc->codec_tag);
57
    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
    } else if (enc->codec_id == CODEC_ID_MP2 || enc->codec_id == CODEC_ID_MP3) {
64
        bps = 0;
65
    } else if (enc->codec_id == CODEC_ID_ADPCM_IMA_WAV || enc->codec_id == CODEC_ID_ADPCM_MS) {
66
        bps = 4;
67
    } else {
68
        bps = 16;
69
    }
70
    
71
    if (enc->codec_id == CODEC_ID_MP2 || enc->codec_id == CODEC_ID_MP3) {
72
        blkalign = 1;
73
        //blkalign = 144 * enc->bit_rate/enc->sample_rate;
74
    } else if (enc->block_align != 0) { /* specified by the codec */
75
        blkalign = enc->block_align;
76
    } else
77
        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
    if (enc->codec_id == CODEC_ID_MP3) {
88
        put_le16(pb, 12); /* wav_extra_size */
89
        hdrsize += 12;
90
        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
    } else if (enc->codec_id == CODEC_ID_MP2) {
96
        put_le16(pb, 22); /* wav_extra_size */
97
        hdrsize += 22;
98
        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
    } else if (enc->codec_id == CODEC_ID_ADPCM_IMA_WAV) {
107
        put_le16(pb, 2); /* wav_extra_size */
108
        hdrsize += 2;
109
        put_le16(pb, ((enc->block_align - 4 * enc->channels) / (4 * enc->channels)) * 8 + 1); /* wSamplesPerBlock */
110
    } else if(enc->extradata_size){
111
        put_le16(pb, enc->extradata_size);
112
        put_buffer(pb, enc->extradata, enc->extradata_size);
113
        hdrsize += enc->extradata_size;
114
        if(hdrsize&1){
115
            hdrsize++;
116
            put_byte(pb, 0);
117
        }
118
    } else {
119
        hdrsize -= 2;
120
    }
121

    
122
    return hdrsize;
123
}
124
#endif //CONFIG_ENCODERS
125

    
126
/* 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
{
135
    int id;
136

    
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
    if (size == 14) {  /* We're dealing with plain vanilla WAVEFORMAT */
145
        codec->bits_per_sample = 8;
146
    }else
147
        codec->bits_per_sample = get_le16(pb);
148
    codec->codec_id = wav_codec_get_id(id, codec->bits_per_sample);
149
    
150
    if (size > 16) {  /* We're obviously dealing with WAVEFORMATEX */
151
        codec->extradata_size = get_le16(pb);
152
        if (codec->extradata_size > 0) {
153
            if (codec->extradata_size > size - 18)
154
                codec->extradata_size = size - 18;
155
            codec->extradata = av_mallocz(codec->extradata_size);
156
            get_buffer(pb, codec->extradata, codec->extradata_size);
157
        } 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
    }
164
}
165

    
166

    
167
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
#ifdef CONFIG_ENCODERS
180
typedef struct {
181
    offset_t data;
182
} WAVContext;
183

    
184
static int wav_write_header(AVFormatContext *s)
185
{
186
    WAVContext *wav = s->priv_data;
187
    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
    if (put_wav_header(pb, &s->streams[0]->codec) < 0) {
197
        av_free(wav);
198
        return -1;
199
    }
200
    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
static int wav_write_packet(AVFormatContext *s, AVPacket *pkt)
211
{
212
    ByteIOContext *pb = &s->pb;
213
    put_buffer(pb, pkt->data, pkt->size);
214
    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
        put_le32(pb, (uint32_t)(file_size - 8));
230
        url_fseek(pb, file_size, SEEK_SET);
231

    
232
        put_flush_packet(pb);
233
    }
234
    return 0;
235
}
236
#endif //CONFIG_ENCODERS
237

    
238
/* return the size of the found tag */
239
/* XXX: > 2GB ? */
240
static int find_tag(ByteIOContext *pb, uint32_t tag1)
241
{
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
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
/* 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
    st = av_new_stream(s, 0);
297
    if (!st)
298
        return AVERROR_NOMEM;
299

    
300
    get_wav_header(pb, &st->codec, size);
301
    st->need_parsing = 1;
302
    
303
    size = find_tag(pb, MKTAG('d', 'a', 't', 'a'));
304
    if (size < 0)
305
        return -1;
306
    return 0;
307
}
308

    
309
#define MAX_SIZE 4096
310

    
311
static int wav_read_packet(AVFormatContext *s,
312
                           AVPacket *pkt)
313
{
314
    int ret, size;
315
    AVStream *st;
316

    
317
    if (url_feof(&s->pb))
318
        return AVERROR_IO;
319
    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
        return AVERROR_IO;
329
    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
    /* note: we need to modify the packet size here to handle the last
335
       packet */
336
    pkt->size = ret;
337
    return ret;
338
}
339

    
340
static int wav_read_close(AVFormatContext *s)
341
{
342
    return 0;
343
}
344

    
345
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
    case CODEC_ID_DTS:
356
        /* 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
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
    wav_read_seek,
374
};
375

    
376
#ifdef CONFIG_ENCODERS
377
static AVOutputFormat wav_oformat = {
378
    "wav",
379
    "wav format",
380
    "audio/x-wav",
381
    "wav",
382
    sizeof(WAVContext),
383
    CODEC_ID_PCM_S16LE,
384
    CODEC_ID_NONE,
385
    wav_write_header,
386
    wav_write_packet,
387
    wav_write_trailer,
388
};
389
#endif //CONFIG_ENCODERS
390

    
391
int ff_wav_init(void)
392
{
393
    av_register_input_format(&wav_iformat);
394
#ifdef CONFIG_ENCODERS
395
    av_register_output_format(&wav_oformat);
396
#endif //CONFIG_ENCODERS
397
    return 0;
398
}