suppressed mpeg demux mess - use now dynamic stream creation api
[libav.git] / libav / wav.c
CommitLineData
de6d9b64
FB
1/*
2 * WAV encoder and decoder
3 * Copyright (c) 2001 Gerard Lantau.
4 *
5 * This program is free software; you can redistribute it and/or modify
6 * it under the terms of the GNU General Public License as published by
7 * the Free Software Foundation; either version 2 of the License, or
8 * (at your option) any later version.
9 *
10 * This program 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
13 * GNU General Public License for more details.
14 *
15 * You should have received a copy of the GNU General Public License
16 * along with this program; if not, write to the Free Software
17 * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
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{
113 WAVContext *wav;
114 ByteIOContext *pb = &s->pb;
115 offset_t fmt;
116
1ea4f593 117 wav = av_malloc(sizeof(WAVContext));
de6d9b64
FB
118 if (!wav)
119 return -1;
120 memset(wav, 0, sizeof(WAVContext));
121 s->priv_data = wav;
122
123 put_tag(pb, "RIFF");
124 put_le32(pb, 0); /* file length */
125 put_tag(pb, "WAVE");
126
127 /* format header */
128 fmt = start_tag(pb, "fmt ");
46a3d068 129 if (put_wav_header(pb, &s->streams[0]->codec) < 0) {
1ea4f593 130 av_free(wav);
46a3d068
FB
131 return -1;
132 }
de6d9b64
FB
133 end_tag(pb, fmt);
134
135 /* data header */
136 wav->data = start_tag(pb, "data");
137
138 put_flush_packet(pb);
139
140 return 0;
141}
142
143static int wav_write_packet(AVFormatContext *s, int stream_index_ptr,
10bb7023 144 UINT8 *buf, int size, int force_pts)
de6d9b64
FB
145{
146 ByteIOContext *pb = &s->pb;
147 put_buffer(pb, buf, size);
148 return 0;
149}
150
151static int wav_write_trailer(AVFormatContext *s)
152{
153 ByteIOContext *pb = &s->pb;
154 WAVContext *wav = s->priv_data;
155 offset_t file_size;
156
157 if (!url_is_streamed(&s->pb)) {
158 end_tag(pb, wav->data);
159
160 /* update file size */
161 file_size = url_ftell(pb);
162 url_fseek(pb, 4, SEEK_SET);
67e9bb0d 163 put_le32(pb, (UINT32)(file_size - 8));
de6d9b64
FB
164 url_fseek(pb, file_size, SEEK_SET);
165
166 put_flush_packet(pb);
167 }
168
1ea4f593 169 av_free(wav);
de6d9b64
FB
170 return 0;
171}
172
173/* return the size of the found tag */
174/* XXX: > 2GB ? */
8be1c656 175static int find_tag(ByteIOContext *pb, UINT32 tag1)
de6d9b64
FB
176{
177 unsigned int tag;
178 int size;
179
180 for(;;) {
181 if (url_feof(pb))
182 return -1;
183 tag = get_le32(pb);
184 size = get_le32(pb);
185 if (tag == tag1)
186 break;
187 url_fseek(pb, size, SEEK_CUR);
188 }
189 if (size < 0)
190 size = 0x7fffffff;
191 return size;
192}
193
194/* wav input */
195static int wav_read_header(AVFormatContext *s,
196 AVFormatParameters *ap)
197{
198 int size;
199 unsigned int tag;
200 ByteIOContext *pb = &s->pb;
5ed8fafc 201 unsigned int id, channels, rate, bit_rate, extra_size, bps;
de6d9b64
FB
202 AVStream *st;
203
204 /* check RIFF header */
205 tag = get_le32(pb);
206
207 if (tag != MKTAG('R', 'I', 'F', 'F'))
208 return -1;
209 get_le32(pb); /* file size */
210 tag = get_le32(pb);
211 if (tag != MKTAG('W', 'A', 'V', 'E'))
212 return -1;
213
214 /* parse fmt header */
215 size = find_tag(pb, MKTAG('f', 'm', 't', ' '));
216 if (size < 0)
217 return -1;
218 id = get_le16(pb);
219 channels = get_le16(pb);
220 rate = get_le32(pb);
221 bit_rate = get_le32(pb) * 8;
222 get_le16(pb); /* block align */
5ed8fafc 223 bps = get_le16(pb); /* bits per sample */
de6d9b64
FB
224 if (size >= 18) {
225 /* wav_extra_size */
226 extra_size = get_le16(pb);
227 /* skip unused data */
228 url_fseek(pb, size - 18, SEEK_CUR);
229 }
230
231 size = find_tag(pb, MKTAG('d', 'a', 't', 'a'));
232 if (size < 0)
233 return -1;
234
235 /* now we are ready: build format streams */
1ea4f593 236 st = av_malloc(sizeof(AVStream));
de6d9b64
FB
237 if (!st)
238 return -1;
239 s->nb_streams = 1;
240 s->streams[0] = st;
241
242 st->id = 0;
243
244 st->codec.codec_type = CODEC_TYPE_AUDIO;
245 st->codec.codec_tag = id;
46a3d068 246 st->codec.codec_id = wav_codec_get_id(id, bps);
de6d9b64
FB
247 st->codec.channels = channels;
248 st->codec.sample_rate = rate;
249 return 0;
250}
251
252#define MAX_SIZE 4096
253
254static int wav_read_packet(AVFormatContext *s,
255 AVPacket *pkt)
256{
257 int packet_size, n, ret;
258
259 if (url_feof(&s->pb))
260 return -EIO;
261 packet_size = url_get_packet_size(&s->pb);
262 n = MAX_SIZE / packet_size;
263 if (n <= 0)
264 return n = 1;
265 if (av_new_packet(pkt, n * packet_size))
266 return -EIO;
267 pkt->stream_index = 0;
268
269 ret = get_buffer(&s->pb, pkt->data, pkt->size);
270 if (ret < 0)
271 av_free_packet(pkt);
5ed8fafc
FB
272 /* note: we need to modify the packet size here to handle the last
273 packet */
274 pkt->size = ret;
de6d9b64
FB
275 return ret;
276}
277
278static int wav_read_close(AVFormatContext *s)
279{
280 return 0;
281}
282
283AVFormat wav_format = {
284 "wav",
285 "wav format",
286 "audio/x-wav",
287 "wav",
5ed8fafc 288 CODEC_ID_PCM_S16LE,
de6d9b64
FB
289 CODEC_ID_NONE,
290 wav_write_header,
291 wav_write_packet,
292 wav_write_trailer,
293
294 wav_read_header,
295 wav_read_packet,
296 wav_read_close,
297};