added new fft/mdct code - added fft-test program
[libav.git] / libav / wav.c
CommitLineData
de6d9b64
FB
1/*
2 * WAV encoder and decoder
19720f15 3 * Copyright (c) 2001 Fabrice Bellard.
de6d9b64 4 *
19720f15
FB
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.
de6d9b64 9 *
19720f15 10 * This library is distributed in the hope that it will be useful,
de6d9b64 11 * but WITHOUT ANY WARRANTY; without even the implied warranty of
19720f15
FB
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
13 * Lesser General Public License for more details.
de6d9b64 14 *
19720f15
FB
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
de6d9b64 18 */
de6d9b64
FB
19#include "avformat.h"
20#include "avi.h"
21
46a3d068 22CodecTag codec_wav_tags[] = {
46a3d068 23 { CODEC_ID_MP2, 0x50 },
4b1f4f23 24 { CODEC_ID_MP3LAME, 0x55 },
46a3d068
FB
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 },
0147f198
FR
30 { CODEC_ID_ADPCM_MS, 0x02 },
31 { CODEC_ID_ADPCM_IMA_WAV, 0x11 },
46a3d068
FB
32 { 0, 0 },
33};
34
35/* WAVEFORMATEX header */
afc25d93 36/* returns the size or -1 on error */
46a3d068
FB
37int put_wav_header(ByteIOContext *pb, AVCodecContext *enc)
38{
86bec9a1 39 int tag, bps, blkalign, bytespersec;
afc25d93 40 int hdrsize = 18;
46a3d068
FB
41
42 tag = codec_get_tag(codec_wav_tags, enc->codec_id);
43 if (tag == 0)
44 return -1;
4b1f4f23 45 put_le16(pb, tag);
46a3d068
FB
46 put_le16(pb, enc->channels);
47 put_le32(pb, enc->sample_rate);
46a3d068
FB
48 if (enc->codec_id == CODEC_ID_PCM_U8 ||
49 enc->codec_id == CODEC_ID_PCM_ALAW ||
50 enc->codec_id == CODEC_ID_PCM_MULAW) {
51 bps = 8;
5798368b 52 } else if (enc->codec_id == CODEC_ID_MP2 || enc->codec_id == CODEC_ID_MP3LAME) {
86bec9a1 53 bps = 0;
889c5224
FR
54 } else if (enc->codec_id == CODEC_ID_ADPCM_IMA_WAV || enc->codec_id == CODEC_ID_ADPCM_MS) {
55 bps = 4;
46a3d068
FB
56 } else {
57 bps = 16;
58 }
46a3d068 59
4b1f4f23 60 if (enc->codec_id == CODEC_ID_MP2 || enc->codec_id == CODEC_ID_MP3LAME) {
86bec9a1 61 blkalign = 1;
4b1f4f23 62 //blkalign = 144 * enc->bit_rate/enc->sample_rate;
0147f198
FR
63 } else if (enc->block_align != 0) { /* specified by the codec */
64 blkalign = enc->block_align;
4b1f4f23 65 } else
86bec9a1
J
66 blkalign = enc->channels*bps >> 3;
67 if (enc->codec_id == CODEC_ID_PCM_U8 ||
68 enc->codec_id == CODEC_ID_PCM_S16LE) {
69 bytespersec = enc->sample_rate * blkalign;
70 } else {
71 bytespersec = enc->bit_rate / 8;
72 }
73 put_le32(pb, bytespersec); /* bytes per second */
74 put_le16(pb, blkalign); /* block align */
75 put_le16(pb, bps); /* bits per sample */
4b1f4f23 76 if (enc->codec_id == CODEC_ID_MP3LAME) {
86bec9a1 77 put_le16(pb, 12); /* wav_extra_size */
afc25d93 78 hdrsize += 12;
86bec9a1
J
79 put_le16(pb, 1); /* wID */
80 put_le32(pb, 2); /* fdwFlags */
81 put_le16(pb, 1152); /* nBlockSize */
82 put_le16(pb, 1); /* nFramesPerBlock */
83 put_le16(pb, 1393); /* nCodecDelay */
4b1f4f23
J
84 } else if (enc->codec_id == CODEC_ID_MP2) {
85 put_le16(pb, 22); /* wav_extra_size */
afc25d93 86 hdrsize += 22;
4b1f4f23
J
87 put_le16(pb, 2); /* fwHeadLayer */
88 put_le32(pb, enc->bit_rate); /* dwHeadBitrate */
89 put_le16(pb, enc->channels == 2 ? 1 : 8); /* fwHeadMode */
90 put_le16(pb, 0); /* fwHeadModeExt */
91 put_le16(pb, 1); /* wHeadEmphasis */
92 put_le16(pb, 16); /* fwHeadFlags */
93 put_le32(pb, 0); /* dwPTSLow */
94 put_le32(pb, 0); /* dwPTSHigh */
889c5224
FR
95 } else if (enc->codec_id == CODEC_ID_ADPCM_IMA_WAV) {
96 put_le16(pb, 2); /* wav_extra_size */
97 put_le16(pb, ((enc->block_align - 4 * enc->channels) / (4 * enc->channels)) * 8 + 1); /* wSamplesPerBlock */
86bec9a1
J
98 } else
99 put_le16(pb, 0); /* wav_extra_size */
100
afc25d93 101 return hdrsize;
46a3d068
FB
102}
103
104int wav_codec_get_id(unsigned int tag, int bps)
105{
106 int id;
107 id = codec_get_id(codec_wav_tags, tag);
108 if (id <= 0)
109 return id;
110 /* handle specific u8 codec */
111 if (id == CODEC_ID_PCM_S16LE && bps == 8)
112 id = CODEC_ID_PCM_U8;
113 return id;
114}
115
de6d9b64
FB
116typedef struct {
117 offset_t data;
118} WAVContext;
119
120static int wav_write_header(AVFormatContext *s)
121{
c9a65ca8 122 WAVContext *wav = s->priv_data;
de6d9b64
FB
123 ByteIOContext *pb = &s->pb;
124 offset_t fmt;
125
de6d9b64
FB
126 put_tag(pb, "RIFF");
127 put_le32(pb, 0); /* file length */
128 put_tag(pb, "WAVE");
129
130 /* format header */
131 fmt = start_tag(pb, "fmt ");
46a3d068 132 if (put_wav_header(pb, &s->streams[0]->codec) < 0) {
1ea4f593 133 av_free(wav);
46a3d068
FB
134 return -1;
135 }
de6d9b64
FB
136 end_tag(pb, fmt);
137
138 /* data header */
139 wav->data = start_tag(pb, "data");
140
141 put_flush_packet(pb);
142
143 return 0;
144}
145
146static int wav_write_packet(AVFormatContext *s, int stream_index_ptr,
10bb7023 147 UINT8 *buf, int size, int force_pts)
de6d9b64
FB
148{
149 ByteIOContext *pb = &s->pb;
150 put_buffer(pb, buf, size);
151 return 0;
152}
153
154static int wav_write_trailer(AVFormatContext *s)
155{
156 ByteIOContext *pb = &s->pb;
157 WAVContext *wav = s->priv_data;
158 offset_t file_size;
159
160 if (!url_is_streamed(&s->pb)) {
161 end_tag(pb, wav->data);
162
163 /* update file size */
164 file_size = url_ftell(pb);
165 url_fseek(pb, 4, SEEK_SET);
67e9bb0d 166 put_le32(pb, (UINT32)(file_size - 8));
de6d9b64
FB
167 url_fseek(pb, file_size, SEEK_SET);
168
169 put_flush_packet(pb);
170 }
de6d9b64
FB
171 return 0;
172}
173
174/* return the size of the found tag */
175/* XXX: > 2GB ? */
8be1c656 176static int find_tag(ByteIOContext *pb, UINT32 tag1)
de6d9b64
FB
177{
178 unsigned int tag;
179 int size;
180
181 for(;;) {
182 if (url_feof(pb))
183 return -1;
184 tag = get_le32(pb);
185 size = get_le32(pb);
186 if (tag == tag1)
187 break;
188 url_fseek(pb, size, SEEK_CUR);
189 }
190 if (size < 0)
191 size = 0x7fffffff;
192 return size;
193}
194
c9a65ca8
FB
195static int wav_probe(AVProbeData *p)
196{
197 /* check file header */
198 if (p->buf_size <= 32)
199 return 0;
200 if (p->buf[0] == 'R' && p->buf[1] == 'I' &&
201 p->buf[2] == 'F' && p->buf[3] == 'F' &&
202 p->buf[8] == 'W' && p->buf[9] == 'A' &&
203 p->buf[10] == 'V' && p->buf[11] == 'E')
204 return AVPROBE_SCORE_MAX;
205 else
206 return 0;
207}
208
de6d9b64
FB
209/* wav input */
210static int wav_read_header(AVFormatContext *s,
211 AVFormatParameters *ap)
212{
213 int size;
214 unsigned int tag;
215 ByteIOContext *pb = &s->pb;
5ed8fafc 216 unsigned int id, channels, rate, bit_rate, extra_size, bps;
0147f198 217 unsigned int blkalign;
de6d9b64
FB
218 AVStream *st;
219
220 /* check RIFF header */
221 tag = get_le32(pb);
222
223 if (tag != MKTAG('R', 'I', 'F', 'F'))
224 return -1;
225 get_le32(pb); /* file size */
226 tag = get_le32(pb);
227 if (tag != MKTAG('W', 'A', 'V', 'E'))
228 return -1;
229
230 /* parse fmt header */
231 size = find_tag(pb, MKTAG('f', 'm', 't', ' '));
232 if (size < 0)
233 return -1;
234 id = get_le16(pb);
235 channels = get_le16(pb);
236 rate = get_le32(pb);
237 bit_rate = get_le32(pb) * 8;
0147f198 238 blkalign = get_le16(pb); /* block align */
5ed8fafc 239 bps = get_le16(pb); /* bits per sample */
de6d9b64
FB
240 if (size >= 18) {
241 /* wav_extra_size */
242 extra_size = get_le16(pb);
243 /* skip unused data */
244 url_fseek(pb, size - 18, SEEK_CUR);
245 }
246
247 size = find_tag(pb, MKTAG('d', 'a', 't', 'a'));
248 if (size < 0)
249 return -1;
250
251 /* now we are ready: build format streams */
c9a65ca8 252 st = av_new_stream(s, 0);
de6d9b64 253 if (!st)
c9a65ca8 254 return AVERROR_NOMEM;
de6d9b64 255
de6d9b64
FB
256 st->codec.codec_type = CODEC_TYPE_AUDIO;
257 st->codec.codec_tag = id;
46a3d068 258 st->codec.codec_id = wav_codec_get_id(id, bps);
de6d9b64
FB
259 st->codec.channels = channels;
260 st->codec.sample_rate = rate;
0147f198 261 st->codec.block_align = blkalign;
de6d9b64
FB
262 return 0;
263}
264
265#define MAX_SIZE 4096
266
267static int wav_read_packet(AVFormatContext *s,
268 AVPacket *pkt)
269{
94ef6864 270 int ret;
de6d9b64
FB
271
272 if (url_feof(&s->pb))
273 return -EIO;
94ef6864 274 if (av_new_packet(pkt, MAX_SIZE))
de6d9b64
FB
275 return -EIO;
276 pkt->stream_index = 0;
277
278 ret = get_buffer(&s->pb, pkt->data, pkt->size);
279 if (ret < 0)
280 av_free_packet(pkt);
5ed8fafc
FB
281 /* note: we need to modify the packet size here to handle the last
282 packet */
283 pkt->size = ret;
de6d9b64
FB
284 return ret;
285}
286
287static int wav_read_close(AVFormatContext *s)
288{
289 return 0;
290}
291
c9a65ca8
FB
292static AVInputFormat wav_iformat = {
293 "wav",
294 "wav format",
295 0,
296 wav_probe,
297 wav_read_header,
298 wav_read_packet,
299 wav_read_close,
300};
301
302static AVOutputFormat wav_oformat = {
de6d9b64
FB
303 "wav",
304 "wav format",
305 "audio/x-wav",
306 "wav",
c9a65ca8 307 sizeof(WAVContext),
5ed8fafc 308 CODEC_ID_PCM_S16LE,
de6d9b64
FB
309 CODEC_ID_NONE,
310 wav_write_header,
311 wav_write_packet,
312 wav_write_trailer,
de6d9b64 313};
c9a65ca8
FB
314
315int wav_init(void)
316{
317 av_register_input_format(&wav_iformat);
318 av_register_output_format(&wav_oformat);
319 return 0;
320}