Statistics
| Branch: | Revision:

ffmpeg / libavformat / wav.c @ 27d5f18f

History | View | Annotate | Download (11.1 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_AAC, 0x706d },
38
    { CODEC_ID_VORBIS, ('V'<<8)+'o' }, //HACK/FIXME, does vorbis in WAV/AVI have an (in)official id?
39
    { CODEC_ID_SONIC, 0x2048 },
40
    { CODEC_ID_SONIC_LS, 0x2048 },
41
    { CODEC_ID_ADPCM_CT, 0x200 },
42
    { 0, 0 },
43
};
44

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

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

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

    
124
    return hdrsize;
125
}
126
#endif //CONFIG_ENCODERS
127

    
128
/* We could be given one of the three possible structures here:
129
 * WAVEFORMAT, PCMWAVEFORMAT or WAVEFORMATEX. Each structure
130
 * is an expansion of the previous one with the fields added
131
 * at the bottom. PCMWAVEFORMAT adds 'WORD wBitsPerSample' and
132
 * WAVEFORMATEX adds 'WORD  cbSize' and basically makes itself
133
 * an openended structure.
134
 */
135
void get_wav_header(ByteIOContext *pb, AVCodecContext *codec, int size) 
136
{
137
    int id;
138

    
139
    id = get_le16(pb);
140
    codec->codec_type = CODEC_TYPE_AUDIO;
141
    codec->codec_tag = id;
142
    codec->channels = get_le16(pb);
143
    codec->sample_rate = get_le32(pb);
144
    codec->bit_rate = get_le32(pb) * 8;
145
    codec->block_align = get_le16(pb);
146
    if (size == 14) {  /* We're dealing with plain vanilla WAVEFORMAT */
147
        codec->bits_per_sample = 8;
148
    }else
149
        codec->bits_per_sample = get_le16(pb);
150
    codec->codec_id = wav_codec_get_id(id, codec->bits_per_sample);
151
    
152
    if (size > 16) {  /* We're obviously dealing with WAVEFORMATEX */
153
        codec->extradata_size = get_le16(pb);
154
        if (codec->extradata_size > 0) {
155
            if (codec->extradata_size > size - 18)
156
                codec->extradata_size = size - 18;
157
            codec->extradata = av_mallocz(codec->extradata_size + FF_INPUT_BUFFER_PADDING_SIZE);
158
            get_buffer(pb, codec->extradata, codec->extradata_size);
159
        } else
160
            codec->extradata_size = 0;
161
        
162
        /* It is possible for the chunk to contain garbage at the end */
163
        if (size - codec->extradata_size - 18 > 0)
164
            url_fskip(pb, size - codec->extradata_size - 18);
165
    }
166
}
167

    
168

    
169
int wav_codec_get_id(unsigned int tag, int bps)
170
{
171
    int id;
172
    id = codec_get_id(codec_wav_tags, tag);
173
    if (id <= 0)
174
        return id;
175
    /* handle specific u8 codec */
176
    if (id == CODEC_ID_PCM_S16LE && bps == 8)
177
        id = CODEC_ID_PCM_U8;
178
    return id;
179
}
180

    
181
#ifdef CONFIG_ENCODERS
182
typedef struct {
183
    offset_t data;
184
} WAVContext;
185

    
186
static int wav_write_header(AVFormatContext *s)
187
{
188
    WAVContext *wav = s->priv_data;
189
    ByteIOContext *pb = &s->pb;
190
    offset_t fmt;
191

    
192
    put_tag(pb, "RIFF");
193
    put_le32(pb, 0); /* file length */
194
    put_tag(pb, "WAVE");
195

    
196
    /* format header */
197
    fmt = start_tag(pb, "fmt ");
198
    if (put_wav_header(pb, &s->streams[0]->codec) < 0) {
199
        av_free(wav);
200
        return -1;
201
    }
202
    end_tag(pb, fmt);
203

    
204
    /* data header */
205
    wav->data = start_tag(pb, "data");
206
    
207
    put_flush_packet(pb);
208

    
209
    return 0;
210
}
211

    
212
static int wav_write_packet(AVFormatContext *s, AVPacket *pkt)
213
{
214
    ByteIOContext *pb = &s->pb;
215
    put_buffer(pb, pkt->data, pkt->size);
216
    return 0;
217
}
218

    
219
static int wav_write_trailer(AVFormatContext *s)
220
{
221
    ByteIOContext *pb = &s->pb;
222
    WAVContext *wav = s->priv_data;
223
    offset_t file_size;
224

    
225
    if (!url_is_streamed(&s->pb)) {
226
        end_tag(pb, wav->data);
227

    
228
        /* update file size */
229
        file_size = url_ftell(pb);
230
        url_fseek(pb, 4, SEEK_SET);
231
        put_le32(pb, (uint32_t)(file_size - 8));
232
        url_fseek(pb, file_size, SEEK_SET);
233

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

    
240
/* return the size of the found tag */
241
/* XXX: > 2GB ? */
242
static int find_tag(ByteIOContext *pb, uint32_t tag1)
243
{
244
    unsigned int tag;
245
    int size;
246

    
247
    for(;;) {
248
        if (url_feof(pb))
249
            return -1;
250
        tag = get_le32(pb);
251
        size = get_le32(pb);
252
        if (tag == tag1)
253
            break;
254
        url_fseek(pb, size, SEEK_CUR);
255
    }
256
    if (size < 0)
257
        size = 0x7fffffff;
258
    return size;
259
}
260

    
261
static int wav_probe(AVProbeData *p)
262
{
263
    /* check file header */
264
    if (p->buf_size <= 32)
265
        return 0;
266
    if (p->buf[0] == 'R' && p->buf[1] == 'I' &&
267
        p->buf[2] == 'F' && p->buf[3] == 'F' &&
268
        p->buf[8] == 'W' && p->buf[9] == 'A' &&
269
        p->buf[10] == 'V' && p->buf[11] == 'E')
270
        return AVPROBE_SCORE_MAX;
271
    else
272
        return 0;
273
}
274

    
275
/* wav input */
276
static int wav_read_header(AVFormatContext *s,
277
                           AVFormatParameters *ap)
278
{
279
    int size;
280
    unsigned int tag;
281
    ByteIOContext *pb = &s->pb;
282
    AVStream *st;
283

    
284
    /* check RIFF header */
285
    tag = get_le32(pb);
286

    
287
    if (tag != MKTAG('R', 'I', 'F', 'F'))
288
        return -1;
289
    get_le32(pb); /* file size */
290
    tag = get_le32(pb);
291
    if (tag != MKTAG('W', 'A', 'V', 'E'))
292
        return -1;
293
    
294
    /* parse fmt header */
295
    size = find_tag(pb, MKTAG('f', 'm', 't', ' '));
296
    if (size < 0)
297
        return -1;
298
    st = av_new_stream(s, 0);
299
    if (!st)
300
        return AVERROR_NOMEM;
301

    
302
    get_wav_header(pb, &st->codec, size);
303
    st->need_parsing = 1;
304

    
305
    av_set_pts_info(st, 64, 1, st->codec.sample_rate);
306

    
307
    size = find_tag(pb, MKTAG('d', 'a', 't', 'a'));
308
    if (size < 0)
309
        return -1;
310
    return 0;
311
}
312

    
313
#define MAX_SIZE 4096
314

    
315
static int wav_read_packet(AVFormatContext *s,
316
                           AVPacket *pkt)
317
{
318
    int ret, size;
319
    AVStream *st;
320

    
321
    if (url_feof(&s->pb))
322
        return AVERROR_IO;
323
    st = s->streams[0];
324

    
325
    size = MAX_SIZE;
326
    if (st->codec.block_align > 1) {
327
        if (size < st->codec.block_align)
328
            size = st->codec.block_align;
329
        size = (size / st->codec.block_align) * st->codec.block_align;
330
    }
331
    if (av_new_packet(pkt, size))
332
        return AVERROR_IO;
333
    pkt->stream_index = 0;
334

    
335
    ret = get_buffer(&s->pb, pkt->data, pkt->size);
336
    if (ret < 0)
337
        av_free_packet(pkt);
338
    /* note: we need to modify the packet size here to handle the last
339
       packet */
340
    pkt->size = ret;
341
    return ret;
342
}
343

    
344
static int wav_read_close(AVFormatContext *s)
345
{
346
    return 0;
347
}
348

    
349
static int wav_read_seek(AVFormatContext *s, 
350
                         int stream_index, int64_t timestamp, int flags)
351
{
352
    AVStream *st;
353

    
354
    st = s->streams[0];
355
    switch(st->codec.codec_id) {
356
    case CODEC_ID_MP2:
357
    case CODEC_ID_MP3:
358
    case CODEC_ID_AC3:
359
    case CODEC_ID_DTS:
360
        /* use generic seeking with dynamically generated indexes */
361
        return -1;
362
    default:
363
        break;
364
    }
365
    return pcm_read_seek(s, stream_index, timestamp, flags);
366
}
367

    
368

    
369
static AVInputFormat wav_iformat = {
370
    "wav",
371
    "wav format",
372
    0,
373
    wav_probe,
374
    wav_read_header,
375
    wav_read_packet,
376
    wav_read_close,
377
    wav_read_seek,
378
};
379

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

    
395
int ff_wav_init(void)
396
{
397
    av_register_input_format(&wav_iformat);
398
#ifdef CONFIG_ENCODERS
399
    av_register_output_format(&wav_oformat);
400
#endif //CONFIG_ENCODERS
401
    return 0;
402
}