]> git.sesse.net Git - ffmpeg/blob - libav/wav.c
put codec registering in another file so that the user can install the codecs he...
[ffmpeg] / libav / wav.c
1 /* 
2  * WAV encoder and decoder
3  * Copyright (c) 2001 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 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 },
30     { 0, 0 },
31 };
32
33 /* WAVEFORMATEX header */
34 /* returns the size or -1 on error */
35 int put_wav_header(ByteIOContext *pb, AVCodecContext *enc)
36 {
37     int tag, bps, blkalign, bytespersec;
38     int hdrsize = 18;
39
40     tag = codec_get_tag(codec_wav_tags, enc->codec_id);
41     if (tag == 0)
42         return -1;
43     put_le16(pb, tag);
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) {
49         bps = 8;
50     } else if (enc->codec_id == CODEC_ID_MP2 || enc->codec_id == CODEC_ID_MP3LAME) {
51         bps = 0;
52     } else {
53         bps = 16;
54     }
55     
56     if (enc->codec_id == CODEC_ID_MP2 || enc->codec_id == CODEC_ID_MP3LAME) {
57         blkalign = 1;
58         //blkalign = 144 * enc->bit_rate/enc->sample_rate;
59     } else
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 */
70     if (enc->codec_id == CODEC_ID_MP3LAME) {
71         put_le16(pb, 12); /* wav_extra_size */
72         hdrsize += 12;
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 */
80         hdrsize += 22;
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 */
89     } else
90         put_le16(pb, 0); /* wav_extra_size */
91
92     return hdrsize;
93 }
94
95 int 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
107 typedef struct {
108     offset_t data;
109 } WAVContext;
110
111 static int wav_write_header(AVFormatContext *s)
112 {
113     WAVContext *wav = s->priv_data;
114     ByteIOContext *pb = &s->pb;
115     offset_t fmt;
116
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 ");
123     if (put_wav_header(pb, &s->streams[0]->codec) < 0) {
124         av_free(wav);
125         return -1;
126     }
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
137 static int wav_write_packet(AVFormatContext *s, int stream_index_ptr,
138                            UINT8 *buf, int size, int force_pts)
139 {
140     ByteIOContext *pb = &s->pb;
141     put_buffer(pb, buf, size);
142     return 0;
143 }
144
145 static 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);
157         put_le32(pb, (UINT32)(file_size - 8));
158         url_fseek(pb, file_size, SEEK_SET);
159
160         put_flush_packet(pb);
161     }
162     return 0;
163 }
164
165 /* return the size of the found tag */
166 /* XXX: > 2GB ? */
167 static int find_tag(ByteIOContext *pb, UINT32 tag1)
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
186 static 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
200 /* wav input */
201 static int wav_read_header(AVFormatContext *s,
202                            AVFormatParameters *ap)
203 {
204     int size;
205     unsigned int tag;
206     ByteIOContext *pb = &s->pb;
207     unsigned int id, channels, rate, bit_rate, extra_size, bps;
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 */
229     bps = get_le16(pb); /* bits per sample */
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 */
242     st = av_new_stream(s, 0);
243     if (!st)
244         return AVERROR_NOMEM;
245
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;
251     return 0;
252 }
253
254 #define MAX_SIZE 4096
255
256 static 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);
274     /* note: we need to modify the packet size here to handle the last
275        packet */
276     pkt->size = ret;
277     return ret;
278 }
279
280 static int wav_read_close(AVFormatContext *s)
281 {
282     return 0;
283 }
284
285 static 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
295 static AVOutputFormat wav_oformat = {
296     "wav",
297     "wav format",
298     "audio/x-wav",
299     "wav",
300     sizeof(WAVContext),
301     CODEC_ID_PCM_S16LE,
302     CODEC_ID_NONE,
303     wav_write_header,
304     wav_write_packet,
305     wav_write_trailer,
306 };
307
308 int wav_init(void)
309 {
310     av_register_input_format(&wav_iformat);
311     av_register_output_format(&wav_oformat);
312     return 0;
313 }