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