fixing warning
[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
FB
22CodecTag codec_wav_tags[] = {
23 { CODEC_ID_MP2, 0x55 },
24 { CODEC_ID_MP2, 0x50 },
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 */
34int put_wav_header(ByteIOContext *pb, AVCodecContext *enc)
35{
36 int tag, bps;
37
38 tag = codec_get_tag(codec_wav_tags, enc->codec_id);
39 if (tag == 0)
40 return -1;
41 put_le16(pb, tag);
42 put_le16(pb, enc->channels);
43 put_le32(pb, enc->sample_rate);
44 put_le32(pb, enc->bit_rate / 8);
45 put_le16(pb, 1); /* block align */
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 {
51 bps = 16;
52 }
53 put_le16(pb, bps); /* bits per sample */
54
55 put_le16(pb, 0); /* wav_extra_size */
56 return 0;
57}
58
59int wav_codec_get_id(unsigned int tag, int bps)
60{
61 int id;
62 id = codec_get_id(codec_wav_tags, tag);
63 if (id <= 0)
64 return id;
65 /* handle specific u8 codec */
66 if (id == CODEC_ID_PCM_S16LE && bps == 8)
67 id = CODEC_ID_PCM_U8;
68 return id;
69}
70
de6d9b64
FB
71typedef struct {
72 offset_t data;
73} WAVContext;
74
75static int wav_write_header(AVFormatContext *s)
76{
77 WAVContext *wav;
78 ByteIOContext *pb = &s->pb;
79 offset_t fmt;
80
81 wav = malloc(sizeof(WAVContext));
82 if (!wav)
83 return -1;
84 memset(wav, 0, sizeof(WAVContext));
85 s->priv_data = wav;
86
87 put_tag(pb, "RIFF");
88 put_le32(pb, 0); /* file length */
89 put_tag(pb, "WAVE");
90
91 /* format header */
92 fmt = start_tag(pb, "fmt ");
46a3d068
FB
93 if (put_wav_header(pb, &s->streams[0]->codec) < 0) {
94 free(wav);
95 return -1;
96 }
de6d9b64
FB
97 end_tag(pb, fmt);
98
99 /* data header */
100 wav->data = start_tag(pb, "data");
101
102 put_flush_packet(pb);
103
104 return 0;
105}
106
107static int wav_write_packet(AVFormatContext *s, int stream_index_ptr,
108 UINT8 *buf, int size)
109{
110 ByteIOContext *pb = &s->pb;
111 put_buffer(pb, buf, size);
112 return 0;
113}
114
115static int wav_write_trailer(AVFormatContext *s)
116{
117 ByteIOContext *pb = &s->pb;
118 WAVContext *wav = s->priv_data;
119 offset_t file_size;
120
121 if (!url_is_streamed(&s->pb)) {
122 end_tag(pb, wav->data);
123
124 /* update file size */
125 file_size = url_ftell(pb);
126 url_fseek(pb, 4, SEEK_SET);
67e9bb0d 127 put_le32(pb, (UINT32)(file_size - 8));
de6d9b64
FB
128 url_fseek(pb, file_size, SEEK_SET);
129
130 put_flush_packet(pb);
131 }
132
133 free(wav);
134 return 0;
135}
136
137/* return the size of the found tag */
138/* XXX: > 2GB ? */
8be1c656 139static int find_tag(ByteIOContext *pb, UINT32 tag1)
de6d9b64
FB
140{
141 unsigned int tag;
142 int size;
143
144 for(;;) {
145 if (url_feof(pb))
146 return -1;
147 tag = get_le32(pb);
148 size = get_le32(pb);
149 if (tag == tag1)
150 break;
151 url_fseek(pb, size, SEEK_CUR);
152 }
153 if (size < 0)
154 size = 0x7fffffff;
155 return size;
156}
157
158/* wav input */
159static int wav_read_header(AVFormatContext *s,
160 AVFormatParameters *ap)
161{
162 int size;
163 unsigned int tag;
164 ByteIOContext *pb = &s->pb;
5ed8fafc 165 unsigned int id, channels, rate, bit_rate, extra_size, bps;
de6d9b64
FB
166 AVStream *st;
167
168 /* check RIFF header */
169 tag = get_le32(pb);
170
171 if (tag != MKTAG('R', 'I', 'F', 'F'))
172 return -1;
173 get_le32(pb); /* file size */
174 tag = get_le32(pb);
175 if (tag != MKTAG('W', 'A', 'V', 'E'))
176 return -1;
177
178 /* parse fmt header */
179 size = find_tag(pb, MKTAG('f', 'm', 't', ' '));
180 if (size < 0)
181 return -1;
182 id = get_le16(pb);
183 channels = get_le16(pb);
184 rate = get_le32(pb);
185 bit_rate = get_le32(pb) * 8;
186 get_le16(pb); /* block align */
5ed8fafc 187 bps = get_le16(pb); /* bits per sample */
de6d9b64
FB
188 if (size >= 18) {
189 /* wav_extra_size */
190 extra_size = get_le16(pb);
191 /* skip unused data */
192 url_fseek(pb, size - 18, SEEK_CUR);
193 }
194
195 size = find_tag(pb, MKTAG('d', 'a', 't', 'a'));
196 if (size < 0)
197 return -1;
198
199 /* now we are ready: build format streams */
200 st = malloc(sizeof(AVStream));
201 if (!st)
202 return -1;
203 s->nb_streams = 1;
204 s->streams[0] = st;
205
206 st->id = 0;
207
208 st->codec.codec_type = CODEC_TYPE_AUDIO;
209 st->codec.codec_tag = id;
46a3d068 210 st->codec.codec_id = wav_codec_get_id(id, bps);
de6d9b64
FB
211 st->codec.channels = channels;
212 st->codec.sample_rate = rate;
213 return 0;
214}
215
216#define MAX_SIZE 4096
217
218static int wav_read_packet(AVFormatContext *s,
219 AVPacket *pkt)
220{
221 int packet_size, n, ret;
222
223 if (url_feof(&s->pb))
224 return -EIO;
225 packet_size = url_get_packet_size(&s->pb);
226 n = MAX_SIZE / packet_size;
227 if (n <= 0)
228 return n = 1;
229 if (av_new_packet(pkt, n * packet_size))
230 return -EIO;
231 pkt->stream_index = 0;
232
233 ret = get_buffer(&s->pb, pkt->data, pkt->size);
234 if (ret < 0)
235 av_free_packet(pkt);
5ed8fafc
FB
236 /* note: we need to modify the packet size here to handle the last
237 packet */
238 pkt->size = ret;
de6d9b64
FB
239 return ret;
240}
241
242static int wav_read_close(AVFormatContext *s)
243{
244 return 0;
245}
246
247AVFormat wav_format = {
248 "wav",
249 "wav format",
250 "audio/x-wav",
251 "wav",
5ed8fafc 252 CODEC_ID_PCM_S16LE,
de6d9b64
FB
253 CODEC_ID_NONE,
254 wav_write_header,
255 wav_write_packet,
256 wav_write_trailer,
257
258 wav_read_header,
259 wav_read_packet,
260 wav_read_close,
261};