Statistics
| Branch: | Revision:

ffmpeg / libavformat / wav.c @ 27d5f18f

History | View | Annotate | Download (11.1 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 ca377212 Måns Rullgård
    { CODEC_ID_AAC, 0x706d },
38 2926db54 Michael Niedermayer
    { CODEC_ID_VORBIS, ('V'<<8)+'o' }, //HACK/FIXME, does vorbis in WAV/AVI have an (in)official id?
39 7251dfaf Alex Beregszaszi
    { CODEC_ID_SONIC, 0x2048 },
40
    { CODEC_ID_SONIC_LS, 0x2048 },
41 b3bfb299 Mike Melanson
    { CODEC_ID_ADPCM_CT, 0x200 },
42 46a3d068 Fabrice Bellard
    { 0, 0 },
43
};
44
45 764ef400 Mike Melanson
#ifdef CONFIG_ENCODERS
46 46a3d068 Fabrice Bellard
/* WAVEFORMATEX header */
47 afc25d93 Philip Gladstone
/* returns the size or -1 on error */
48 46a3d068 Fabrice Bellard
int put_wav_header(ByteIOContext *pb, AVCodecContext *enc)
49
{
50 bd5a6020 Michael Niedermayer
    int bps, blkalign, bytespersec;
51 afc25d93 Philip Gladstone
    int hdrsize = 18;
52 46a3d068 Fabrice Bellard
53 bd5a6020 Michael Niedermayer
    if(!enc->codec_tag)
54
       enc->codec_tag = codec_get_tag(codec_wav_tags, enc->codec_id);
55
    if(!enc->codec_tag)
56 46a3d068 Fabrice Bellard
        return -1;
57 bd5a6020 Michael Niedermayer
58
    put_le16(pb, enc->codec_tag);
59 46a3d068 Fabrice Bellard
    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 80783dc2 Fabrice Bellard
    } else if (enc->codec_id == CODEC_ID_MP2 || enc->codec_id == CODEC_ID_MP3) {
66 86bec9a1 Juanjo
        bps = 0;
67 889c5224 François Revol
    } else if (enc->codec_id == CODEC_ID_ADPCM_IMA_WAV || enc->codec_id == CODEC_ID_ADPCM_MS) {
68
        bps = 4;
69 46a3d068 Fabrice Bellard
    } else {
70
        bps = 16;
71
    }
72
    
73 80783dc2 Fabrice Bellard
    if (enc->codec_id == CODEC_ID_MP2 || enc->codec_id == CODEC_ID_MP3) {
74 359fa0fe Michael Niedermayer
        blkalign = enc->frame_size; //this is wrong, but seems many demuxers dont work if this is set correctly
75 4b1f4f23 Juanjo
        //blkalign = 144 * enc->bit_rate/enc->sample_rate;
76 0147f198 François Revol
    } else if (enc->block_align != 0) { /* specified by the codec */
77
        blkalign = enc->block_align;
78 4b1f4f23 Juanjo
    } else
79 86bec9a1 Juanjo
        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 80783dc2 Fabrice Bellard
    if (enc->codec_id == CODEC_ID_MP3) {
90 86bec9a1 Juanjo
        put_le16(pb, 12); /* wav_extra_size */
91 afc25d93 Philip Gladstone
        hdrsize += 12;
92 86bec9a1 Juanjo
        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 4b1f4f23 Juanjo
    } else if (enc->codec_id == CODEC_ID_MP2) {
98
        put_le16(pb, 22); /* wav_extra_size */
99 afc25d93 Philip Gladstone
        hdrsize += 22;
100 4b1f4f23 Juanjo
        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 889c5224 François Revol
    } else if (enc->codec_id == CODEC_ID_ADPCM_IMA_WAV) {
109
        put_le16(pb, 2); /* wav_extra_size */
110 361b1b86 Brian Becker
        hdrsize += 2;
111 889c5224 François Revol
        put_le16(pb, ((enc->block_align - 4 * enc->channels) / (4 * enc->channels)) * 8 + 1); /* wSamplesPerBlock */
112 c4f8d5d9 Michael Niedermayer
    } else if(enc->extradata_size){
113 6748046e Michael Niedermayer
        put_le16(pb, enc->extradata_size);
114
        put_buffer(pb, enc->extradata, enc->extradata_size);
115
        hdrsize += enc->extradata_size;
116 5062c202 Michael Niedermayer
        if(hdrsize&1){
117
            hdrsize++;
118
            put_byte(pb, 0);
119
        }
120 c4f8d5d9 Michael Niedermayer
    } else {
121
        hdrsize -= 2;
122 6748046e Michael Niedermayer
    }
123 86bec9a1 Juanjo
124 afc25d93 Philip Gladstone
    return hdrsize;
125 46a3d068 Fabrice Bellard
}
126 764ef400 Mike Melanson
#endif //CONFIG_ENCODERS
127 46a3d068 Fabrice Bellard
128 2e7973bb Roman Shaposhnik
/* 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 e095026a Fabrice Bellard
{
137 e8c00089 Zdenek Kabelac
    int id;
138 e095026a Fabrice Bellard
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 2e7973bb Roman Shaposhnik
    if (size == 14) {  /* We're dealing with plain vanilla WAVEFORMAT */
147
        codec->bits_per_sample = 8;
148 e0ece99a Michael Niedermayer
    }else
149
        codec->bits_per_sample = get_le16(pb);
150
    codec->codec_id = wav_codec_get_id(id, codec->bits_per_sample);
151 f9b5459e Fabrice Bellard
    
152 2e7973bb Roman Shaposhnik
    if (size > 16) {  /* We're obviously dealing with WAVEFORMATEX */
153 cb146faf Zdenek Kabelac
        codec->extradata_size = get_le16(pb);
154
        if (codec->extradata_size > 0) {
155 2e7973bb Roman Shaposhnik
            if (codec->extradata_size > size - 18)
156
                codec->extradata_size = size - 18;
157 3129cd01 Michael Niedermayer
            codec->extradata = av_mallocz(codec->extradata_size + FF_INPUT_BUFFER_PADDING_SIZE);
158 cb146faf Zdenek Kabelac
            get_buffer(pb, codec->extradata, codec->extradata_size);
159 2e7973bb Roman Shaposhnik
        } 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 e095026a Fabrice Bellard
    }
166
}
167
168
169 46a3d068 Fabrice Bellard
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 764ef400 Mike Melanson
#ifdef CONFIG_ENCODERS
182 de6d9b64 Fabrice Bellard
typedef struct {
183
    offset_t data;
184
} WAVContext;
185
186
static int wav_write_header(AVFormatContext *s)
187
{
188 c9a65ca8 Fabrice Bellard
    WAVContext *wav = s->priv_data;
189 de6d9b64 Fabrice Bellard
    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 46a3d068 Fabrice Bellard
    if (put_wav_header(pb, &s->streams[0]->codec) < 0) {
199 1ea4f593 Fabrice Bellard
        av_free(wav);
200 46a3d068 Fabrice Bellard
        return -1;
201
    }
202 de6d9b64 Fabrice Bellard
    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 e928649b Michael Niedermayer
static int wav_write_packet(AVFormatContext *s, AVPacket *pkt)
213 de6d9b64 Fabrice Bellard
{
214
    ByteIOContext *pb = &s->pb;
215 e928649b Michael Niedermayer
    put_buffer(pb, pkt->data, pkt->size);
216 de6d9b64 Fabrice Bellard
    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 0c1a9eda Zdenek Kabelac
        put_le32(pb, (uint32_t)(file_size - 8));
232 de6d9b64 Fabrice Bellard
        url_fseek(pb, file_size, SEEK_SET);
233
234
        put_flush_packet(pb);
235
    }
236
    return 0;
237
}
238 764ef400 Mike Melanson
#endif //CONFIG_ENCODERS
239 de6d9b64 Fabrice Bellard
240
/* return the size of the found tag */
241
/* XXX: > 2GB ? */
242 0c1a9eda Zdenek Kabelac
static int find_tag(ByteIOContext *pb, uint32_t tag1)
243 de6d9b64 Fabrice Bellard
{
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 c9a65ca8 Fabrice Bellard
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 de6d9b64 Fabrice Bellard
/* 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 c9a65ca8 Fabrice Bellard
    st = av_new_stream(s, 0);
299 de6d9b64 Fabrice Bellard
    if (!st)
300 c9a65ca8 Fabrice Bellard
        return AVERROR_NOMEM;
301 de6d9b64 Fabrice Bellard
302 2e7973bb Roman Shaposhnik
    get_wav_header(pb, &st->codec, size);
303 f9b5459e Fabrice Bellard
    st->need_parsing = 1;
304 595bf4ef Michael Niedermayer
305
    av_set_pts_info(st, 64, 1, st->codec.sample_rate);
306
307 e095026a Fabrice Bellard
    size = find_tag(pb, MKTAG('d', 'a', 't', 'a'));
308
    if (size < 0)
309
        return -1;
310 de6d9b64 Fabrice Bellard
    return 0;
311
}
312
313
#define MAX_SIZE 4096
314
315
static int wav_read_packet(AVFormatContext *s,
316
                           AVPacket *pkt)
317
{
318 f9b5459e Fabrice Bellard
    int ret, size;
319
    AVStream *st;
320 de6d9b64 Fabrice Bellard
321
    if (url_feof(&s->pb))
322 0bd586c5 Mike Melanson
        return AVERROR_IO;
323 f9b5459e Fabrice Bellard
    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 0bd586c5 Mike Melanson
        return AVERROR_IO;
333 de6d9b64 Fabrice Bellard
    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 5ed8fafc Fabrice Bellard
    /* note: we need to modify the packet size here to handle the last
339
       packet */
340
    pkt->size = ret;
341 de6d9b64 Fabrice Bellard
    return ret;
342
}
343
344
static int wav_read_close(AVFormatContext *s)
345
{
346
    return 0;
347
}
348
349 f9b5459e Fabrice Bellard
static int wav_read_seek(AVFormatContext *s, 
350 7b3c1382 Michael Niedermayer
                         int stream_index, int64_t timestamp, int flags)
351 f9b5459e Fabrice Bellard
{
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 23c99253 Michael Niedermayer
    case CODEC_ID_DTS:
360 f9b5459e Fabrice Bellard
        /* use generic seeking with dynamically generated indexes */
361
        return -1;
362
    default:
363
        break;
364
    }
365 7b3c1382 Michael Niedermayer
    return pcm_read_seek(s, stream_index, timestamp, flags);
366 f9b5459e Fabrice Bellard
}
367
368
369 c9a65ca8 Fabrice Bellard
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 f9b5459e Fabrice Bellard
    wav_read_seek,
378 c9a65ca8 Fabrice Bellard
};
379
380 764ef400 Mike Melanson
#ifdef CONFIG_ENCODERS
381 c9a65ca8 Fabrice Bellard
static AVOutputFormat wav_oformat = {
382 de6d9b64 Fabrice Bellard
    "wav",
383
    "wav format",
384
    "audio/x-wav",
385
    "wav",
386 c9a65ca8 Fabrice Bellard
    sizeof(WAVContext),
387 5ed8fafc Fabrice Bellard
    CODEC_ID_PCM_S16LE,
388 de6d9b64 Fabrice Bellard
    CODEC_ID_NONE,
389
    wav_write_header,
390
    wav_write_packet,
391
    wav_write_trailer,
392
};
393 764ef400 Mike Melanson
#endif //CONFIG_ENCODERS
394 c9a65ca8 Fabrice Bellard
395 c3775e54 Ronald S. Bultje
int ff_wav_init(void)
396 c9a65ca8 Fabrice Bellard
{
397
    av_register_input_format(&wav_iformat);
398 764ef400 Mike Melanson
#ifdef CONFIG_ENCODERS
399 c9a65ca8 Fabrice Bellard
    av_register_output_format(&wav_oformat);
400 764ef400 Mike Melanson
#endif //CONFIG_ENCODERS
401 c9a65ca8 Fabrice Bellard
    return 0;
402
}