some random id for vorbis so we can do some experiments with vorbis in various contai...
[libav.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., 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, 0); /* wav_extra_size */
110
111 return hdrsize;
112 }
113 #endif //CONFIG_ENCODERS
114
115 /* We could be given one of the three possible structures here:
116 * WAVEFORMAT, PCMWAVEFORMAT or WAVEFORMATEX. Each structure
117 * is an expansion of the previous one with the fields added
118 * at the bottom. PCMWAVEFORMAT adds 'WORD wBitsPerSample' and
119 * WAVEFORMATEX adds 'WORD cbSize' and basically makes itself
120 * an openended structure.
121 */
122 void get_wav_header(ByteIOContext *pb, AVCodecContext *codec, int size)
123 {
124 int id;
125
126 id = get_le16(pb);
127 codec->codec_type = CODEC_TYPE_AUDIO;
128 codec->codec_tag = id;
129 codec->channels = get_le16(pb);
130 codec->sample_rate = get_le32(pb);
131 codec->bit_rate = get_le32(pb) * 8;
132 codec->block_align = get_le16(pb);
133 if (size == 14) { /* We're dealing with plain vanilla WAVEFORMAT */
134 codec->bits_per_sample = 8;
135 }else
136 codec->bits_per_sample = get_le16(pb);
137 codec->codec_id = wav_codec_get_id(id, codec->bits_per_sample);
138
139 if (size > 16) { /* We're obviously dealing with WAVEFORMATEX */
140 codec->extradata_size = get_le16(pb);
141 if (codec->extradata_size > 0) {
142 if (codec->extradata_size > size - 18)
143 codec->extradata_size = size - 18;
144 codec->extradata = av_mallocz(codec->extradata_size);
145 get_buffer(pb, codec->extradata, codec->extradata_size);
146 } else
147 codec->extradata_size = 0;
148
149 /* It is possible for the chunk to contain garbage at the end */
150 if (size - codec->extradata_size - 18 > 0)
151 url_fskip(pb, size - codec->extradata_size - 18);
152 }
153 }
154
155
156 int wav_codec_get_id(unsigned int tag, int bps)
157 {
158 int id;
159 id = codec_get_id(codec_wav_tags, tag);
160 if (id <= 0)
161 return id;
162 /* handle specific u8 codec */
163 if (id == CODEC_ID_PCM_S16LE && bps == 8)
164 id = CODEC_ID_PCM_U8;
165 return id;
166 }
167
168 #ifdef CONFIG_ENCODERS
169 typedef struct {
170 offset_t data;
171 } WAVContext;
172
173 static int wav_write_header(AVFormatContext *s)
174 {
175 WAVContext *wav = s->priv_data;
176 ByteIOContext *pb = &s->pb;
177 offset_t fmt;
178
179 put_tag(pb, "RIFF");
180 put_le32(pb, 0); /* file length */
181 put_tag(pb, "WAVE");
182
183 /* format header */
184 fmt = start_tag(pb, "fmt ");
185 if (put_wav_header(pb, &s->streams[0]->codec) < 0) {
186 av_free(wav);
187 return -1;
188 }
189 end_tag(pb, fmt);
190
191 /* data header */
192 wav->data = start_tag(pb, "data");
193
194 put_flush_packet(pb);
195
196 return 0;
197 }
198
199 static int wav_write_packet(AVFormatContext *s, int stream_index_ptr,
200 const uint8_t *buf, int size, int64_t pts)
201 {
202 ByteIOContext *pb = &s->pb;
203 put_buffer(pb, buf, size);
204 return 0;
205 }
206
207 static int wav_write_trailer(AVFormatContext *s)
208 {
209 ByteIOContext *pb = &s->pb;
210 WAVContext *wav = s->priv_data;
211 offset_t file_size;
212
213 if (!url_is_streamed(&s->pb)) {
214 end_tag(pb, wav->data);
215
216 /* update file size */
217 file_size = url_ftell(pb);
218 url_fseek(pb, 4, SEEK_SET);
219 put_le32(pb, (uint32_t)(file_size - 8));
220 url_fseek(pb, file_size, SEEK_SET);
221
222 put_flush_packet(pb);
223 }
224 return 0;
225 }
226 #endif //CONFIG_ENCODERS
227
228 /* return the size of the found tag */
229 /* XXX: > 2GB ? */
230 static int find_tag(ByteIOContext *pb, uint32_t tag1)
231 {
232 unsigned int tag;
233 int size;
234
235 for(;;) {
236 if (url_feof(pb))
237 return -1;
238 tag = get_le32(pb);
239 size = get_le32(pb);
240 if (tag == tag1)
241 break;
242 url_fseek(pb, size, SEEK_CUR);
243 }
244 if (size < 0)
245 size = 0x7fffffff;
246 return size;
247 }
248
249 static int wav_probe(AVProbeData *p)
250 {
251 /* check file header */
252 if (p->buf_size <= 32)
253 return 0;
254 if (p->buf[0] == 'R' && p->buf[1] == 'I' &&
255 p->buf[2] == 'F' && p->buf[3] == 'F' &&
256 p->buf[8] == 'W' && p->buf[9] == 'A' &&
257 p->buf[10] == 'V' && p->buf[11] == 'E')
258 return AVPROBE_SCORE_MAX;
259 else
260 return 0;
261 }
262
263 /* wav input */
264 static int wav_read_header(AVFormatContext *s,
265 AVFormatParameters *ap)
266 {
267 int size;
268 unsigned int tag;
269 ByteIOContext *pb = &s->pb;
270 AVStream *st;
271
272 /* check RIFF header */
273 tag = get_le32(pb);
274
275 if (tag != MKTAG('R', 'I', 'F', 'F'))
276 return -1;
277 get_le32(pb); /* file size */
278 tag = get_le32(pb);
279 if (tag != MKTAG('W', 'A', 'V', 'E'))
280 return -1;
281
282 /* parse fmt header */
283 size = find_tag(pb, MKTAG('f', 'm', 't', ' '));
284 if (size < 0)
285 return -1;
286 st = av_new_stream(s, 0);
287 if (!st)
288 return AVERROR_NOMEM;
289
290 get_wav_header(pb, &st->codec, size);
291 st->need_parsing = 1;
292
293 size = find_tag(pb, MKTAG('d', 'a', 't', 'a'));
294 if (size < 0)
295 return -1;
296 return 0;
297 }
298
299 #define MAX_SIZE 4096
300
301 static int wav_read_packet(AVFormatContext *s,
302 AVPacket *pkt)
303 {
304 int ret, size;
305 AVStream *st;
306
307 if (url_feof(&s->pb))
308 return -EIO;
309 st = s->streams[0];
310
311 size = MAX_SIZE;
312 if (st->codec.block_align > 1) {
313 if (size < st->codec.block_align)
314 size = st->codec.block_align;
315 size = (size / st->codec.block_align) * st->codec.block_align;
316 }
317 if (av_new_packet(pkt, size))
318 return -EIO;
319 pkt->stream_index = 0;
320
321 ret = get_buffer(&s->pb, pkt->data, pkt->size);
322 if (ret < 0)
323 av_free_packet(pkt);
324 /* note: we need to modify the packet size here to handle the last
325 packet */
326 pkt->size = ret;
327 return ret;
328 }
329
330 static int wav_read_close(AVFormatContext *s)
331 {
332 return 0;
333 }
334
335 static int wav_read_seek(AVFormatContext *s,
336 int stream_index, int64_t timestamp)
337 {
338 AVStream *st;
339
340 st = s->streams[0];
341 switch(st->codec.codec_id) {
342 case CODEC_ID_MP2:
343 case CODEC_ID_MP3:
344 case CODEC_ID_AC3:
345 /* use generic seeking with dynamically generated indexes */
346 return -1;
347 default:
348 break;
349 }
350 return pcm_read_seek(s, stream_index, timestamp);
351 }
352
353
354 static AVInputFormat wav_iformat = {
355 "wav",
356 "wav format",
357 0,
358 wav_probe,
359 wav_read_header,
360 wav_read_packet,
361 wav_read_close,
362 wav_read_seek,
363 };
364
365 #ifdef CONFIG_ENCODERS
366 static AVOutputFormat wav_oformat = {
367 "wav",
368 "wav format",
369 "audio/x-wav",
370 "wav",
371 sizeof(WAVContext),
372 CODEC_ID_PCM_S16LE,
373 CODEC_ID_NONE,
374 wav_write_header,
375 wav_write_packet,
376 wav_write_trailer,
377 };
378 #endif //CONFIG_ENCODERS
379
380 int ff_wav_init(void)
381 {
382 av_register_input_format(&wav_iformat);
383 #ifdef CONFIG_ENCODERS
384 av_register_output_format(&wav_oformat);
385 #endif //CONFIG_ENCODERS
386 return 0;
387 }