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