2 * WAV encoder and decoder
3 * Copyright (c) 2001 Fabrice Bellard.
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.
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.
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
22 CodecTag codec_wav_tags[] = {
23 { CODEC_ID_MP2, 0x50 },
24 { CODEC_ID_MP3LAME, 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 },
33 /* WAVEFORMATEX header */
34 /* returns the size or -1 on error */
35 int put_wav_header(ByteIOContext *pb, AVCodecContext *enc)
37 int tag, bps, blkalign, bytespersec;
40 tag = codec_get_tag(codec_wav_tags, enc->codec_id);
44 put_le16(pb, enc->channels);
45 put_le32(pb, enc->sample_rate);
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) {
50 } else if (enc->codec_id == CODEC_ID_MP2 || enc->codec_id == CODEC_ID_MP3LAME) {
56 if (enc->codec_id == CODEC_ID_MP2 || enc->codec_id == CODEC_ID_MP3LAME) {
58 //blkalign = 144 * enc->bit_rate/enc->sample_rate;
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;
65 bytespersec = enc->bit_rate / 8;
67 put_le32(pb, bytespersec); /* bytes per second */
68 put_le16(pb, blkalign); /* block align */
69 put_le16(pb, bps); /* bits per sample */
70 if (enc->codec_id == CODEC_ID_MP3LAME) {
71 put_le16(pb, 12); /* wav_extra_size */
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 */
78 } else if (enc->codec_id == CODEC_ID_MP2) {
79 put_le16(pb, 22); /* wav_extra_size */
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 */
90 put_le16(pb, 0); /* wav_extra_size */
95 int wav_codec_get_id(unsigned int tag, int bps)
98 id = codec_get_id(codec_wav_tags, tag);
101 /* handle specific u8 codec */
102 if (id == CODEC_ID_PCM_S16LE && bps == 8)
103 id = CODEC_ID_PCM_U8;
111 static int wav_write_header(AVFormatContext *s)
113 WAVContext *wav = s->priv_data;
114 ByteIOContext *pb = &s->pb;
118 put_le32(pb, 0); /* file length */
122 fmt = start_tag(pb, "fmt ");
123 if (put_wav_header(pb, &s->streams[0]->codec) < 0) {
130 wav->data = start_tag(pb, "data");
132 put_flush_packet(pb);
137 static int wav_write_packet(AVFormatContext *s, int stream_index_ptr,
138 UINT8 *buf, int size, int force_pts)
140 ByteIOContext *pb = &s->pb;
141 put_buffer(pb, buf, size);
145 static int wav_write_trailer(AVFormatContext *s)
147 ByteIOContext *pb = &s->pb;
148 WAVContext *wav = s->priv_data;
151 if (!url_is_streamed(&s->pb)) {
152 end_tag(pb, wav->data);
154 /* update file size */
155 file_size = url_ftell(pb);
156 url_fseek(pb, 4, SEEK_SET);
157 put_le32(pb, (UINT32)(file_size - 8));
158 url_fseek(pb, file_size, SEEK_SET);
160 put_flush_packet(pb);
165 /* return the size of the found tag */
167 static int find_tag(ByteIOContext *pb, UINT32 tag1)
179 url_fseek(pb, size, SEEK_CUR);
186 static int wav_probe(AVProbeData *p)
188 /* check file header */
189 if (p->buf_size <= 32)
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;
201 static int wav_read_header(AVFormatContext *s,
202 AVFormatParameters *ap)
206 ByteIOContext *pb = &s->pb;
207 unsigned int id, channels, rate, bit_rate, extra_size, bps;
210 /* check RIFF header */
213 if (tag != MKTAG('R', 'I', 'F', 'F'))
215 get_le32(pb); /* file size */
217 if (tag != MKTAG('W', 'A', 'V', 'E'))
220 /* parse fmt header */
221 size = find_tag(pb, MKTAG('f', 'm', 't', ' '));
225 channels = get_le16(pb);
227 bit_rate = get_le32(pb) * 8;
228 get_le16(pb); /* block align */
229 bps = get_le16(pb); /* bits per sample */
232 extra_size = get_le16(pb);
233 /* skip unused data */
234 url_fseek(pb, size - 18, SEEK_CUR);
237 size = find_tag(pb, MKTAG('d', 'a', 't', 'a'));
241 /* now we are ready: build format streams */
242 st = av_new_stream(s, 0);
244 return AVERROR_NOMEM;
246 st->codec.codec_type = CODEC_TYPE_AUDIO;
247 st->codec.codec_tag = id;
248 st->codec.codec_id = wav_codec_get_id(id, bps);
249 st->codec.channels = channels;
250 st->codec.sample_rate = rate;
254 #define MAX_SIZE 4096
256 static int wav_read_packet(AVFormatContext *s,
259 int packet_size, n, ret;
261 if (url_feof(&s->pb))
263 packet_size = url_get_packet_size(&s->pb);
264 n = MAX_SIZE / packet_size;
267 if (av_new_packet(pkt, n * packet_size))
269 pkt->stream_index = 0;
271 ret = get_buffer(&s->pb, pkt->data, pkt->size);
274 /* note: we need to modify the packet size here to handle the last
280 static int wav_read_close(AVFormatContext *s)
285 static AVInputFormat wav_iformat = {
295 static AVOutputFormat wav_oformat = {
310 av_register_input_format(&wav_iformat);
311 av_register_output_format(&wav_oformat);