Statistics
| Branch: | Revision:

ffmpeg / libavformat / wav.c @ 6748046e

History | View | Annotate | Download (10.7 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
    { 0, 0 },
39
};
40

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

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

    
54
    put_le16(pb, enc->codec_tag);
55
    put_le16(pb, enc->channels);
56
    put_le32(pb, enc->sample_rate);
57
    if (enc->codec_id == CODEC_ID_PCM_U8 ||
58
        enc->codec_id == CODEC_ID_PCM_ALAW ||
59
        enc->codec_id == CODEC_ID_PCM_MULAW) {
60
        bps = 8;
61
    } else if (enc->codec_id == CODEC_ID_MP2 || enc->codec_id == CODEC_ID_MP3) {
62
        bps = 0;
63
    } else if (enc->codec_id == CODEC_ID_ADPCM_IMA_WAV || enc->codec_id == CODEC_ID_ADPCM_MS) {
64
        bps = 4;
65
    } else {
66
        bps = 16;
67
    }
68
    
69
    if (enc->codec_id == CODEC_ID_MP2 || enc->codec_id == CODEC_ID_MP3) {
70
        blkalign = 1;
71
        //blkalign = 144 * enc->bit_rate/enc->sample_rate;
72
    } else if (enc->block_align != 0) { /* specified by the codec */
73
        blkalign = enc->block_align;
74
    } else
75
        blkalign = enc->channels*bps >> 3;
76
    if (enc->codec_id == CODEC_ID_PCM_U8 ||
77
        enc->codec_id == CODEC_ID_PCM_S16LE) {
78
        bytespersec = enc->sample_rate * blkalign;
79
    } else {
80
        bytespersec = enc->bit_rate / 8;
81
    }
82
    put_le32(pb, bytespersec); /* bytes per second */
83
    put_le16(pb, blkalign); /* block align */
84
    put_le16(pb, bps); /* bits per sample */
85
    if (enc->codec_id == CODEC_ID_MP3) {
86
        put_le16(pb, 12); /* wav_extra_size */
87
        hdrsize += 12;
88
        put_le16(pb, 1); /* wID */
89
        put_le32(pb, 2); /* fdwFlags */
90
        put_le16(pb, 1152); /* nBlockSize */
91
        put_le16(pb, 1); /* nFramesPerBlock */
92
        put_le16(pb, 1393); /* nCodecDelay */
93
    } else if (enc->codec_id == CODEC_ID_MP2) {
94
        put_le16(pb, 22); /* wav_extra_size */
95
        hdrsize += 22;
96
        put_le16(pb, 2);  /* fwHeadLayer */
97
        put_le32(pb, enc->bit_rate); /* dwHeadBitrate */
98
        put_le16(pb, enc->channels == 2 ? 1 : 8); /* fwHeadMode */
99
        put_le16(pb, 0);  /* fwHeadModeExt */
100
        put_le16(pb, 1);  /* wHeadEmphasis */
101
        put_le16(pb, 16); /* fwHeadFlags */
102
        put_le32(pb, 0);  /* dwPTSLow */
103
        put_le32(pb, 0);  /* dwPTSHigh */
104
    } else if (enc->codec_id == CODEC_ID_ADPCM_IMA_WAV) {
105
        put_le16(pb, 2); /* wav_extra_size */
106
        hdrsize += 2;
107
        put_le16(pb, ((enc->block_align - 4 * enc->channels) / (4 * enc->channels)) * 8 + 1); /* wSamplesPerBlock */
108
    } else {
109
        put_le16(pb, enc->extradata_size);
110
        put_buffer(pb, enc->extradata, enc->extradata_size);
111
        hdrsize += enc->extradata_size;
112
    }
113

    
114
    return hdrsize;
115
}
116
#endif //CONFIG_ENCODERS
117

    
118
/* We could be given one of the three possible structures here:
119
 * WAVEFORMAT, PCMWAVEFORMAT or WAVEFORMATEX. Each structure
120
 * is an expansion of the previous one with the fields added
121
 * at the bottom. PCMWAVEFORMAT adds 'WORD wBitsPerSample' and
122
 * WAVEFORMATEX adds 'WORD  cbSize' and basically makes itself
123
 * an openended structure.
124
 */
125
void get_wav_header(ByteIOContext *pb, AVCodecContext *codec, int size) 
126
{
127
    int id;
128

    
129
    id = get_le16(pb);
130
    codec->codec_type = CODEC_TYPE_AUDIO;
131
    codec->codec_tag = id;
132
    codec->channels = get_le16(pb);
133
    codec->sample_rate = get_le32(pb);
134
    codec->bit_rate = get_le32(pb) * 8;
135
    codec->block_align = get_le16(pb);
136
    if (size == 14) {  /* We're dealing with plain vanilla WAVEFORMAT */
137
        codec->bits_per_sample = 8;
138
    }else
139
        codec->bits_per_sample = get_le16(pb);
140
    codec->codec_id = wav_codec_get_id(id, codec->bits_per_sample);
141
    
142
    if (size > 16) {  /* We're obviously dealing with WAVEFORMATEX */
143
        codec->extradata_size = get_le16(pb);
144
        if (codec->extradata_size > 0) {
145
            if (codec->extradata_size > size - 18)
146
                codec->extradata_size = size - 18;
147
            codec->extradata = av_mallocz(codec->extradata_size);
148
            get_buffer(pb, codec->extradata, codec->extradata_size);
149
        } else
150
            codec->extradata_size = 0;
151
        
152
        /* It is possible for the chunk to contain garbage at the end */
153
        if (size - codec->extradata_size - 18 > 0)
154
            url_fskip(pb, size - codec->extradata_size - 18);
155
    }
156
}
157

    
158

    
159
int wav_codec_get_id(unsigned int tag, int bps)
160
{
161
    int id;
162
    id = codec_get_id(codec_wav_tags, tag);
163
    if (id <= 0)
164
        return id;
165
    /* handle specific u8 codec */
166
    if (id == CODEC_ID_PCM_S16LE && bps == 8)
167
        id = CODEC_ID_PCM_U8;
168
    return id;
169
}
170

    
171
#ifdef CONFIG_ENCODERS
172
typedef struct {
173
    offset_t data;
174
} WAVContext;
175

    
176
static int wav_write_header(AVFormatContext *s)
177
{
178
    WAVContext *wav = s->priv_data;
179
    ByteIOContext *pb = &s->pb;
180
    offset_t fmt;
181

    
182
    put_tag(pb, "RIFF");
183
    put_le32(pb, 0); /* file length */
184
    put_tag(pb, "WAVE");
185

    
186
    /* format header */
187
    fmt = start_tag(pb, "fmt ");
188
    if (put_wav_header(pb, &s->streams[0]->codec) < 0) {
189
        av_free(wav);
190
        return -1;
191
    }
192
    end_tag(pb, fmt);
193

    
194
    /* data header */
195
    wav->data = start_tag(pb, "data");
196
    
197
    put_flush_packet(pb);
198

    
199
    return 0;
200
}
201

    
202
static int wav_write_packet(AVFormatContext *s, int stream_index_ptr,
203
                            const uint8_t *buf, int size, int64_t pts)
204
{
205
    ByteIOContext *pb = &s->pb;
206
    put_buffer(pb, buf, size);
207
    return 0;
208
}
209

    
210
static int wav_write_trailer(AVFormatContext *s)
211
{
212
    ByteIOContext *pb = &s->pb;
213
    WAVContext *wav = s->priv_data;
214
    offset_t file_size;
215

    
216
    if (!url_is_streamed(&s->pb)) {
217
        end_tag(pb, wav->data);
218

    
219
        /* update file size */
220
        file_size = url_ftell(pb);
221
        url_fseek(pb, 4, SEEK_SET);
222
        put_le32(pb, (uint32_t)(file_size - 8));
223
        url_fseek(pb, file_size, SEEK_SET);
224

    
225
        put_flush_packet(pb);
226
    }
227
    return 0;
228
}
229
#endif //CONFIG_ENCODERS
230

    
231
/* return the size of the found tag */
232
/* XXX: > 2GB ? */
233
static int find_tag(ByteIOContext *pb, uint32_t tag1)
234
{
235
    unsigned int tag;
236
    int size;
237

    
238
    for(;;) {
239
        if (url_feof(pb))
240
            return -1;
241
        tag = get_le32(pb);
242
        size = get_le32(pb);
243
        if (tag == tag1)
244
            break;
245
        url_fseek(pb, size, SEEK_CUR);
246
    }
247
    if (size < 0)
248
        size = 0x7fffffff;
249
    return size;
250
}
251

    
252
static int wav_probe(AVProbeData *p)
253
{
254
    /* check file header */
255
    if (p->buf_size <= 32)
256
        return 0;
257
    if (p->buf[0] == 'R' && p->buf[1] == 'I' &&
258
        p->buf[2] == 'F' && p->buf[3] == 'F' &&
259
        p->buf[8] == 'W' && p->buf[9] == 'A' &&
260
        p->buf[10] == 'V' && p->buf[11] == 'E')
261
        return AVPROBE_SCORE_MAX;
262
    else
263
        return 0;
264
}
265

    
266
/* wav input */
267
static int wav_read_header(AVFormatContext *s,
268
                           AVFormatParameters *ap)
269
{
270
    int size;
271
    unsigned int tag;
272
    ByteIOContext *pb = &s->pb;
273
    AVStream *st;
274

    
275
    /* check RIFF header */
276
    tag = get_le32(pb);
277

    
278
    if (tag != MKTAG('R', 'I', 'F', 'F'))
279
        return -1;
280
    get_le32(pb); /* file size */
281
    tag = get_le32(pb);
282
    if (tag != MKTAG('W', 'A', 'V', 'E'))
283
        return -1;
284
    
285
    /* parse fmt header */
286
    size = find_tag(pb, MKTAG('f', 'm', 't', ' '));
287
    if (size < 0)
288
        return -1;
289
    st = av_new_stream(s, 0);
290
    if (!st)
291
        return AVERROR_NOMEM;
292

    
293
    get_wav_header(pb, &st->codec, size);
294
    st->need_parsing = 1;
295
    
296
    size = find_tag(pb, MKTAG('d', 'a', 't', 'a'));
297
    if (size < 0)
298
        return -1;
299
    return 0;
300
}
301

    
302
#define MAX_SIZE 4096
303

    
304
static int wav_read_packet(AVFormatContext *s,
305
                           AVPacket *pkt)
306
{
307
    int ret, size;
308
    AVStream *st;
309

    
310
    if (url_feof(&s->pb))
311
        return -EIO;
312
    st = s->streams[0];
313

    
314
    size = MAX_SIZE;
315
    if (st->codec.block_align > 1) {
316
        if (size < st->codec.block_align)
317
            size = st->codec.block_align;
318
        size = (size / st->codec.block_align) * st->codec.block_align;
319
    }
320
    if (av_new_packet(pkt, size))
321
        return -EIO;
322
    pkt->stream_index = 0;
323

    
324
    ret = get_buffer(&s->pb, pkt->data, pkt->size);
325
    if (ret < 0)
326
        av_free_packet(pkt);
327
    /* note: we need to modify the packet size here to handle the last
328
       packet */
329
    pkt->size = ret;
330
    return ret;
331
}
332

    
333
static int wav_read_close(AVFormatContext *s)
334
{
335
    return 0;
336
}
337

    
338
static int wav_read_seek(AVFormatContext *s, 
339
                         int stream_index, int64_t timestamp)
340
{
341
    AVStream *st;
342

    
343
    st = s->streams[0];
344
    switch(st->codec.codec_id) {
345
    case CODEC_ID_MP2:
346
    case CODEC_ID_MP3:
347
    case CODEC_ID_AC3:
348
        /* use generic seeking with dynamically generated indexes */
349
        return -1;
350
    default:
351
        break;
352
    }
353
    return pcm_read_seek(s, stream_index, timestamp);
354
}
355

    
356

    
357
static AVInputFormat wav_iformat = {
358
    "wav",
359
    "wav format",
360
    0,
361
    wav_probe,
362
    wav_read_header,
363
    wav_read_packet,
364
    wav_read_close,
365
    wav_read_seek,
366
};
367

    
368
#ifdef CONFIG_ENCODERS
369
static AVOutputFormat wav_oformat = {
370
    "wav",
371
    "wav format",
372
    "audio/x-wav",
373
    "wav",
374
    sizeof(WAVContext),
375
    CODEC_ID_PCM_S16LE,
376
    CODEC_ID_NONE,
377
    wav_write_header,
378
    wav_write_packet,
379
    wav_write_trailer,
380
};
381
#endif //CONFIG_ENCODERS
382

    
383
int ff_wav_init(void)
384
{
385
    av_register_input_format(&wav_iformat);
386
#ifdef CONFIG_ENCODERS
387
    av_register_output_format(&wav_oformat);
388
#endif //CONFIG_ENCODERS
389
    return 0;
390
}