]> rtime.felk.cvut.cz Git - frescor/ffmpeg.git/blob - libavformat/wav.c
dont read over the end of a data chunk and at the end search for the next
[frescor/ffmpeg.git] / libavformat / wav.c
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., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 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_DTS, 0x2001 },
27     { CODEC_ID_PCM_S16LE, 0x01 },
28     { CODEC_ID_PCM_U8, 0x01 }, /* must come after s16le in this list */
29     { CODEC_ID_PCM_S24LE, 0x01 },
30     { CODEC_ID_PCM_S32LE, 0x01 },
31     { CODEC_ID_PCM_ALAW, 0x06 },
32     { CODEC_ID_PCM_MULAW, 0x07 },
33     { CODEC_ID_ADPCM_MS, 0x02 },
34     { CODEC_ID_ADPCM_IMA_WAV, 0x11 },
35     { CODEC_ID_ADPCM_YAMAHA, 0x20 },
36     { CODEC_ID_ADPCM_G726, 0x45 },
37     { CODEC_ID_ADPCM_IMA_DK4, 0x61 },  /* rogue format number */
38     { CODEC_ID_ADPCM_IMA_DK3, 0x62 },  /* rogue format number */
39     { CODEC_ID_WMAV1, 0x160 },
40     { CODEC_ID_WMAV2, 0x161 },
41     { CODEC_ID_AAC, 0x706d },
42     { CODEC_ID_VORBIS, ('V'<<8)+'o' }, //HACK/FIXME, does vorbis in WAV/AVI have an (in)official id?
43     { CODEC_ID_SONIC, 0x2048 },
44     { CODEC_ID_SONIC_LS, 0x2048 },
45     { CODEC_ID_ADPCM_CT, 0x200 },
46     { CODEC_ID_ADPCM_SWF, ('S'<<8)+'F' },
47     { CODEC_ID_TRUESPEECH, 0x22 },
48
49     // for NuppelVideo (nuv.c)
50     { CODEC_ID_PCM_S16LE, MKTAG('R', 'A', 'W', 'A') },
51     { CODEC_ID_MP3, MKTAG('L', 'A', 'M', 'E') },
52     { 0, 0 },
53 };
54
55 #ifdef CONFIG_MUXERS
56 /* WAVEFORMATEX header */
57 /* returns the size or -1 on error */
58 int put_wav_header(ByteIOContext *pb, AVCodecContext *enc)
59 {
60     int bps, blkalign, bytespersec;
61     int hdrsize = 18;
62
63     if(!enc->codec_tag || enc->codec_tag > 0xffff)
64        enc->codec_tag = codec_get_tag(codec_wav_tags, enc->codec_id);
65     if(!enc->codec_tag)
66         return -1;
67
68     put_le16(pb, enc->codec_tag);
69     put_le16(pb, enc->channels);
70     put_le32(pb, enc->sample_rate);
71     if (enc->codec_id == CODEC_ID_PCM_U8 ||
72         enc->codec_id == CODEC_ID_PCM_ALAW ||
73         enc->codec_id == CODEC_ID_PCM_MULAW) {
74         bps = 8;
75     } else if (enc->codec_id == CODEC_ID_MP2 || enc->codec_id == CODEC_ID_MP3) {
76         bps = 0;
77     } else if (enc->codec_id == CODEC_ID_ADPCM_IMA_WAV || enc->codec_id == CODEC_ID_ADPCM_MS || enc->codec_id == CODEC_ID_ADPCM_G726 || enc->codec_id == CODEC_ID_ADPCM_YAMAHA) { //
78         bps = 4;
79     } else if (enc->codec_id == CODEC_ID_PCM_S24LE) {
80         bps = 24;
81     } else if (enc->codec_id == CODEC_ID_PCM_S32LE) {
82         bps = 32;
83     } else {
84         bps = 16;
85     }
86
87     if (enc->codec_id == CODEC_ID_MP2 || enc->codec_id == CODEC_ID_MP3) {
88         blkalign = enc->frame_size; //this is wrong, but seems many demuxers dont work if this is set correctly
89         //blkalign = 144 * enc->bit_rate/enc->sample_rate;
90     } else if (enc->codec_id == CODEC_ID_ADPCM_G726) { //
91         blkalign = 1;
92     } else if (enc->block_align != 0) { /* specified by the codec */
93         blkalign = enc->block_align;
94     } else
95         blkalign = enc->channels*bps >> 3;
96     if (enc->codec_id == CODEC_ID_PCM_U8 ||
97         enc->codec_id == CODEC_ID_PCM_S24LE ||
98         enc->codec_id == CODEC_ID_PCM_S32LE ||
99         enc->codec_id == CODEC_ID_PCM_S16LE) {
100         bytespersec = enc->sample_rate * blkalign;
101     } else {
102         bytespersec = enc->bit_rate / 8;
103     }
104     put_le32(pb, bytespersec); /* bytes per second */
105     put_le16(pb, blkalign); /* block align */
106     put_le16(pb, bps); /* bits per sample */
107     if (enc->codec_id == CODEC_ID_MP3) {
108         put_le16(pb, 12); /* wav_extra_size */
109         hdrsize += 12;
110         put_le16(pb, 1); /* wID */
111         put_le32(pb, 2); /* fdwFlags */
112         put_le16(pb, 1152); /* nBlockSize */
113         put_le16(pb, 1); /* nFramesPerBlock */
114         put_le16(pb, 1393); /* nCodecDelay */
115     } else if (enc->codec_id == CODEC_ID_MP2) {
116         put_le16(pb, 22); /* wav_extra_size */
117         hdrsize += 22;
118         put_le16(pb, 2);  /* fwHeadLayer */
119         put_le32(pb, enc->bit_rate); /* dwHeadBitrate */
120         put_le16(pb, enc->channels == 2 ? 1 : 8); /* fwHeadMode */
121         put_le16(pb, 0);  /* fwHeadModeExt */
122         put_le16(pb, 1);  /* wHeadEmphasis */
123         put_le16(pb, 16); /* fwHeadFlags */
124         put_le32(pb, 0);  /* dwPTSLow */
125         put_le32(pb, 0);  /* dwPTSHigh */
126     } else if (enc->codec_id == CODEC_ID_ADPCM_IMA_WAV) {
127         put_le16(pb, 2); /* wav_extra_size */
128         hdrsize += 2;
129         put_le16(pb, ((enc->block_align - 4 * enc->channels) / (4 * enc->channels)) * 8 + 1); /* wSamplesPerBlock */
130     } else if(enc->extradata_size){
131         put_le16(pb, enc->extradata_size);
132         put_buffer(pb, enc->extradata, enc->extradata_size);
133         hdrsize += enc->extradata_size;
134         if(hdrsize&1){
135             hdrsize++;
136             put_byte(pb, 0);
137         }
138     } else {
139         hdrsize -= 2;
140     }
141
142     return hdrsize;
143 }
144 #endif //CONFIG_MUXERS
145
146 /* We could be given one of the three possible structures here:
147  * WAVEFORMAT, PCMWAVEFORMAT or WAVEFORMATEX. Each structure
148  * is an expansion of the previous one with the fields added
149  * at the bottom. PCMWAVEFORMAT adds 'WORD wBitsPerSample' and
150  * WAVEFORMATEX adds 'WORD  cbSize' and basically makes itself
151  * an openended structure.
152  */
153 void get_wav_header(ByteIOContext *pb, AVCodecContext *codec, int size)
154 {
155     int id;
156
157     id = get_le16(pb);
158     codec->codec_type = CODEC_TYPE_AUDIO;
159     codec->codec_tag = id;
160     codec->channels = get_le16(pb);
161     codec->sample_rate = get_le32(pb);
162     codec->bit_rate = get_le32(pb) * 8;
163     codec->block_align = get_le16(pb);
164     if (size == 14) {  /* We're dealing with plain vanilla WAVEFORMAT */
165         codec->bits_per_sample = 8;
166     }else
167         codec->bits_per_sample = get_le16(pb);
168     codec->codec_id = wav_codec_get_id(id, codec->bits_per_sample);
169
170     if (size > 16) {  /* We're obviously dealing with WAVEFORMATEX */
171         codec->extradata_size = get_le16(pb);
172         if (codec->extradata_size > 0) {
173             if (codec->extradata_size > size - 18)
174                 codec->extradata_size = size - 18;
175             codec->extradata = av_mallocz(codec->extradata_size + FF_INPUT_BUFFER_PADDING_SIZE);
176             get_buffer(pb, codec->extradata, codec->extradata_size);
177         } else
178             codec->extradata_size = 0;
179
180         /* It is possible for the chunk to contain garbage at the end */
181         if (size - codec->extradata_size - 18 > 0)
182             url_fskip(pb, size - codec->extradata_size - 18);
183     }
184 }
185
186
187 int wav_codec_get_id(unsigned int tag, int bps)
188 {
189     int id;
190     id = codec_get_id(codec_wav_tags, tag);
191     if (id <= 0)
192         return id;
193     /* handle specific u8 codec */
194     if (id == CODEC_ID_PCM_S16LE && bps == 8)
195         id = CODEC_ID_PCM_U8;
196     if (id == CODEC_ID_PCM_S16LE && bps == 24)
197         id = CODEC_ID_PCM_S24LE;
198     if (id == CODEC_ID_PCM_S16LE && bps == 32)
199         id = CODEC_ID_PCM_S32LE;
200     return id;
201 }
202
203 #ifdef CONFIG_MUXERS
204 typedef struct {
205     offset_t data;
206     offset_t data_end;
207 } WAVContext;
208
209 static int wav_write_header(AVFormatContext *s)
210 {
211     WAVContext *wav = s->priv_data;
212     ByteIOContext *pb = &s->pb;
213     offset_t fmt;
214
215     put_tag(pb, "RIFF");
216     put_le32(pb, 0); /* file length */
217     put_tag(pb, "WAVE");
218
219     /* format header */
220     fmt = start_tag(pb, "fmt ");
221     if (put_wav_header(pb, s->streams[0]->codec) < 0) {
222         av_free(wav);
223         return -1;
224     }
225     end_tag(pb, fmt);
226
227     av_set_pts_info(s->streams[0], 64, 1, s->streams[0]->codec->sample_rate);
228
229     /* data header */
230     wav->data = start_tag(pb, "data");
231
232     put_flush_packet(pb);
233
234     return 0;
235 }
236
237 static int wav_write_packet(AVFormatContext *s, AVPacket *pkt)
238 {
239     ByteIOContext *pb = &s->pb;
240     put_buffer(pb, pkt->data, pkt->size);
241     return 0;
242 }
243
244 static int wav_write_trailer(AVFormatContext *s)
245 {
246     ByteIOContext *pb = &s->pb;
247     WAVContext *wav = s->priv_data;
248     offset_t file_size;
249
250     if (!url_is_streamed(&s->pb)) {
251         end_tag(pb, wav->data);
252
253         /* update file size */
254         file_size = url_ftell(pb);
255         url_fseek(pb, 4, SEEK_SET);
256         put_le32(pb, (uint32_t)(file_size - 8));
257         url_fseek(pb, file_size, SEEK_SET);
258
259         put_flush_packet(pb);
260     }
261     return 0;
262 }
263 #endif //CONFIG_MUXERS
264
265 /* return the size of the found tag */
266 /* XXX: > 2GB ? */
267 static int find_tag(ByteIOContext *pb, uint32_t tag1)
268 {
269     unsigned int tag;
270     int size;
271
272     for(;;) {
273         if (url_feof(pb))
274             return -1;
275         tag = get_le32(pb);
276         size = get_le32(pb);
277         if (tag == tag1)
278             break;
279         url_fseek(pb, size, SEEK_CUR);
280     }
281     if (size < 0)
282         size = 0x7fffffff;
283     return size;
284 }
285
286 static int wav_probe(AVProbeData *p)
287 {
288     /* check file header */
289     if (p->buf_size <= 32)
290         return 0;
291     if (p->buf[0] == 'R' && p->buf[1] == 'I' &&
292         p->buf[2] == 'F' && p->buf[3] == 'F' &&
293         p->buf[8] == 'W' && p->buf[9] == 'A' &&
294         p->buf[10] == 'V' && p->buf[11] == 'E')
295         return AVPROBE_SCORE_MAX;
296     else
297         return 0;
298 }
299
300 /* wav input */
301 static int wav_read_header(AVFormatContext *s,
302                            AVFormatParameters *ap)
303 {
304     int size;
305     unsigned int tag;
306     ByteIOContext *pb = &s->pb;
307     AVStream *st;
308     WAVContext *wav = s->priv_data;
309
310     /* check RIFF header */
311     tag = get_le32(pb);
312
313     if (tag != MKTAG('R', 'I', 'F', 'F'))
314         return -1;
315     get_le32(pb); /* file size */
316     tag = get_le32(pb);
317     if (tag != MKTAG('W', 'A', 'V', 'E'))
318         return -1;
319
320     /* parse fmt header */
321     size = find_tag(pb, MKTAG('f', 'm', 't', ' '));
322     if (size < 0)
323         return -1;
324     st = av_new_stream(s, 0);
325     if (!st)
326         return AVERROR_NOMEM;
327
328     get_wav_header(pb, st->codec, size);
329     st->need_parsing = 1;
330
331     av_set_pts_info(st, 64, 1, st->codec->sample_rate);
332
333     size = find_tag(pb, MKTAG('d', 'a', 't', 'a'));
334     if (size < 0)
335         return -1;
336     wav->data_end= url_ftell(pb) + size;
337     return 0;
338 }
339
340 #define MAX_SIZE 4096
341
342 static int wav_read_packet(AVFormatContext *s,
343                            AVPacket *pkt)
344 {
345     int ret, size, left;
346     AVStream *st;
347     WAVContext *wav = s->priv_data;
348
349     if (url_feof(&s->pb))
350         return AVERROR_IO;
351     st = s->streams[0];
352
353     left= wav->data_end - url_ftell(&s->pb);
354     if(left <= 0){
355         left = find_tag(&(s->pb), MKTAG('d', 'a', 't', 'a'));
356         if (left < 0) {
357             return AVERROR_IO;
358         }
359         wav->data_end= url_ftell(&s->pb) + left;
360     }
361
362     size = MAX_SIZE;
363     if (st->codec->block_align > 1) {
364         if (size < st->codec->block_align)
365             size = st->codec->block_align;
366         size = (size / st->codec->block_align) * st->codec->block_align;
367     }
368     size= FFMIN(size, left);
369     if (av_new_packet(pkt, size))
370         return AVERROR_IO;
371     pkt->stream_index = 0;
372
373     ret = get_buffer(&s->pb, pkt->data, pkt->size);
374     if (ret < 0)
375         av_free_packet(pkt);
376     /* note: we need to modify the packet size here to handle the last
377        packet */
378     pkt->size = ret;
379     return ret;
380 }
381
382 static int wav_read_close(AVFormatContext *s)
383 {
384     return 0;
385 }
386
387 static int wav_read_seek(AVFormatContext *s,
388                          int stream_index, int64_t timestamp, int flags)
389 {
390     AVStream *st;
391
392     st = s->streams[0];
393     switch(st->codec->codec_id) {
394     case CODEC_ID_MP2:
395     case CODEC_ID_MP3:
396     case CODEC_ID_AC3:
397     case CODEC_ID_DTS:
398         /* use generic seeking with dynamically generated indexes */
399         return -1;
400     default:
401         break;
402     }
403     return pcm_read_seek(s, stream_index, timestamp, flags);
404 }
405
406
407 static AVInputFormat wav_iformat = {
408     "wav",
409     "wav format",
410     sizeof(WAVContext),
411     wav_probe,
412     wav_read_header,
413     wav_read_packet,
414     wav_read_close,
415     wav_read_seek,
416 };
417
418 #ifdef CONFIG_MUXERS
419 static AVOutputFormat wav_oformat = {
420     "wav",
421     "wav format",
422     "audio/x-wav",
423     "wav",
424     sizeof(WAVContext),
425     CODEC_ID_PCM_S16LE,
426     CODEC_ID_NONE,
427     wav_write_header,
428     wav_write_packet,
429     wav_write_trailer,
430 };
431 #endif //CONFIG_MUXERS
432
433 int ff_wav_init(void)
434 {
435     av_register_input_format(&wav_iformat);
436 #ifdef CONFIG_MUXERS
437     av_register_output_format(&wav_oformat);
438 #endif //CONFIG_MUXERS
439     return 0;
440 }