Mercurial > libavformat.hg
annotate wav.c @ 204:61f9cad50ff3 libavformat
* making resolution abbreviations right
* introducing sntsc/spal for square pixel ntsc/pal
author | romansh |
---|---|
date | Fri, 29 Aug 2003 06:58:55 +0000 |
parents | 01bec1059bdf |
children | d55e4b761e24 |
rev | line source |
---|---|
0 | 1 /* |
2 * WAV encoder and decoder | |
3 * Copyright (c) 2001, 2002 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 const 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 { CODEC_ID_ADPCM_MS, 0x02 }, | |
31 { CODEC_ID_ADPCM_IMA_WAV, 0x11 }, | |
32 { CODEC_ID_WMAV1, 0x160 }, | |
33 { CODEC_ID_WMAV2, 0x161 }, | |
34 { 0, 0 }, | |
35 }; | |
36 | |
37 /* WAVEFORMATEX header */ | |
38 /* returns the size or -1 on error */ | |
39 int put_wav_header(ByteIOContext *pb, AVCodecContext *enc) | |
40 { | |
196 | 41 int bps, blkalign, bytespersec; |
0 | 42 int hdrsize = 18; |
43 | |
196 | 44 if(!enc->codec_tag) |
45 enc->codec_tag = codec_get_tag(codec_wav_tags, enc->codec_id); | |
46 if(!enc->codec_tag) | |
0 | 47 return -1; |
196 | 48 |
49 put_le16(pb, enc->codec_tag); | |
0 | 50 put_le16(pb, enc->channels); |
51 put_le32(pb, enc->sample_rate); | |
52 if (enc->codec_id == CODEC_ID_PCM_U8 || | |
53 enc->codec_id == CODEC_ID_PCM_ALAW || | |
54 enc->codec_id == CODEC_ID_PCM_MULAW) { | |
55 bps = 8; | |
56 } else if (enc->codec_id == CODEC_ID_MP2 || enc->codec_id == CODEC_ID_MP3LAME) { | |
57 bps = 0; | |
58 } else if (enc->codec_id == CODEC_ID_ADPCM_IMA_WAV || enc->codec_id == CODEC_ID_ADPCM_MS) { | |
59 bps = 4; | |
60 } else { | |
61 bps = 16; | |
62 } | |
63 | |
64 if (enc->codec_id == CODEC_ID_MP2 || enc->codec_id == CODEC_ID_MP3LAME) { | |
65 blkalign = 1; | |
66 //blkalign = 144 * enc->bit_rate/enc->sample_rate; | |
67 } else if (enc->block_align != 0) { /* specified by the codec */ | |
68 blkalign = enc->block_align; | |
69 } else | |
70 blkalign = enc->channels*bps >> 3; | |
71 if (enc->codec_id == CODEC_ID_PCM_U8 || | |
72 enc->codec_id == CODEC_ID_PCM_S16LE) { | |
73 bytespersec = enc->sample_rate * blkalign; | |
74 } else { | |
75 bytespersec = enc->bit_rate / 8; | |
76 } | |
77 put_le32(pb, bytespersec); /* bytes per second */ | |
78 put_le16(pb, blkalign); /* block align */ | |
79 put_le16(pb, bps); /* bits per sample */ | |
80 if (enc->codec_id == CODEC_ID_MP3LAME) { | |
81 put_le16(pb, 12); /* wav_extra_size */ | |
82 hdrsize += 12; | |
83 put_le16(pb, 1); /* wID */ | |
84 put_le32(pb, 2); /* fdwFlags */ | |
85 put_le16(pb, 1152); /* nBlockSize */ | |
86 put_le16(pb, 1); /* nFramesPerBlock */ | |
87 put_le16(pb, 1393); /* nCodecDelay */ | |
88 } else if (enc->codec_id == CODEC_ID_MP2) { | |
89 put_le16(pb, 22); /* wav_extra_size */ | |
90 hdrsize += 22; | |
91 put_le16(pb, 2); /* fwHeadLayer */ | |
92 put_le32(pb, enc->bit_rate); /* dwHeadBitrate */ | |
93 put_le16(pb, enc->channels == 2 ? 1 : 8); /* fwHeadMode */ | |
94 put_le16(pb, 0); /* fwHeadModeExt */ | |
95 put_le16(pb, 1); /* wHeadEmphasis */ | |
96 put_le16(pb, 16); /* fwHeadFlags */ | |
97 put_le32(pb, 0); /* dwPTSLow */ | |
98 put_le32(pb, 0); /* dwPTSHigh */ | |
99 } else if (enc->codec_id == CODEC_ID_ADPCM_IMA_WAV) { | |
100 put_le16(pb, 2); /* wav_extra_size */ | |
101 put_le16(pb, ((enc->block_align - 4 * enc->channels) / (4 * enc->channels)) * 8 + 1); /* wSamplesPerBlock */ | |
102 } else | |
103 put_le16(pb, 0); /* wav_extra_size */ | |
104 | |
105 return hdrsize; | |
106 } | |
107 | |
84
0068a6902911
correct AUDIO strf parsing patch by (Roman Shaposhnick <rvs at sun dot com>)
michaelni
parents:
74
diff
changeset
|
108 /* We could be given one of the three possible structures here: |
0068a6902911
correct AUDIO strf parsing patch by (Roman Shaposhnick <rvs at sun dot com>)
michaelni
parents:
74
diff
changeset
|
109 * WAVEFORMAT, PCMWAVEFORMAT or WAVEFORMATEX. Each structure |
0068a6902911
correct AUDIO strf parsing patch by (Roman Shaposhnick <rvs at sun dot com>)
michaelni
parents:
74
diff
changeset
|
110 * is an expansion of the previous one with the fields added |
0068a6902911
correct AUDIO strf parsing patch by (Roman Shaposhnick <rvs at sun dot com>)
michaelni
parents:
74
diff
changeset
|
111 * at the bottom. PCMWAVEFORMAT adds 'WORD wBitsPerSample' and |
0068a6902911
correct AUDIO strf parsing patch by (Roman Shaposhnick <rvs at sun dot com>)
michaelni
parents:
74
diff
changeset
|
112 * WAVEFORMATEX adds 'WORD cbSize' and basically makes itself |
0068a6902911
correct AUDIO strf parsing patch by (Roman Shaposhnick <rvs at sun dot com>)
michaelni
parents:
74
diff
changeset
|
113 * an openended structure. |
0068a6902911
correct AUDIO strf parsing patch by (Roman Shaposhnick <rvs at sun dot com>)
michaelni
parents:
74
diff
changeset
|
114 */ |
0068a6902911
correct AUDIO strf parsing patch by (Roman Shaposhnick <rvs at sun dot com>)
michaelni
parents:
74
diff
changeset
|
115 void get_wav_header(ByteIOContext *pb, AVCodecContext *codec, int size) |
0 | 116 { |
117 int id; | |
118 | |
119 id = get_le16(pb); | |
120 codec->codec_type = CODEC_TYPE_AUDIO; | |
121 codec->codec_tag = id; | |
122 codec->channels = get_le16(pb); | |
123 codec->sample_rate = get_le32(pb); | |
124 codec->bit_rate = get_le32(pb) * 8; | |
125 codec->block_align = get_le16(pb); | |
84
0068a6902911
correct AUDIO strf parsing patch by (Roman Shaposhnick <rvs at sun dot com>)
michaelni
parents:
74
diff
changeset
|
126 if (size == 14) { /* We're dealing with plain vanilla WAVEFORMAT */ |
0068a6902911
correct AUDIO strf parsing patch by (Roman Shaposhnick <rvs at sun dot com>)
michaelni
parents:
74
diff
changeset
|
127 codec->bits_per_sample = 8; |
87 | 128 }else |
129 codec->bits_per_sample = get_le16(pb); | |
130 codec->codec_id = wav_codec_get_id(id, codec->bits_per_sample); | |
131 | |
84
0068a6902911
correct AUDIO strf parsing patch by (Roman Shaposhnick <rvs at sun dot com>)
michaelni
parents:
74
diff
changeset
|
132 if (size > 16) { /* We're obviously dealing with WAVEFORMATEX */ |
0 | 133 codec->extradata_size = get_le16(pb); |
134 if (codec->extradata_size > 0) { | |
84
0068a6902911
correct AUDIO strf parsing patch by (Roman Shaposhnick <rvs at sun dot com>)
michaelni
parents:
74
diff
changeset
|
135 if (codec->extradata_size > size - 18) |
0068a6902911
correct AUDIO strf parsing patch by (Roman Shaposhnick <rvs at sun dot com>)
michaelni
parents:
74
diff
changeset
|
136 codec->extradata_size = size - 18; |
0 | 137 codec->extradata = av_mallocz(codec->extradata_size); |
138 get_buffer(pb, codec->extradata, codec->extradata_size); | |
84
0068a6902911
correct AUDIO strf parsing patch by (Roman Shaposhnick <rvs at sun dot com>)
michaelni
parents:
74
diff
changeset
|
139 } else |
0068a6902911
correct AUDIO strf parsing patch by (Roman Shaposhnick <rvs at sun dot com>)
michaelni
parents:
74
diff
changeset
|
140 codec->extradata_size = 0; |
0068a6902911
correct AUDIO strf parsing patch by (Roman Shaposhnick <rvs at sun dot com>)
michaelni
parents:
74
diff
changeset
|
141 |
0068a6902911
correct AUDIO strf parsing patch by (Roman Shaposhnick <rvs at sun dot com>)
michaelni
parents:
74
diff
changeset
|
142 /* It is possible for the chunk to contain garbage at the end */ |
0068a6902911
correct AUDIO strf parsing patch by (Roman Shaposhnick <rvs at sun dot com>)
michaelni
parents:
74
diff
changeset
|
143 if (size - codec->extradata_size - 18 > 0) |
0068a6902911
correct AUDIO strf parsing patch by (Roman Shaposhnick <rvs at sun dot com>)
michaelni
parents:
74
diff
changeset
|
144 url_fskip(pb, size - codec->extradata_size - 18); |
0 | 145 } |
146 } | |
147 | |
148 | |
149 int wav_codec_get_id(unsigned int tag, int bps) | |
150 { | |
151 int id; | |
152 id = codec_get_id(codec_wav_tags, tag); | |
153 if (id <= 0) | |
154 return id; | |
155 /* handle specific u8 codec */ | |
156 if (id == CODEC_ID_PCM_S16LE && bps == 8) | |
157 id = CODEC_ID_PCM_U8; | |
158 return id; | |
159 } | |
160 | |
161 typedef struct { | |
162 offset_t data; | |
163 } WAVContext; | |
164 | |
165 static int wav_write_header(AVFormatContext *s) | |
166 { | |
167 WAVContext *wav = s->priv_data; | |
168 ByteIOContext *pb = &s->pb; | |
169 offset_t fmt; | |
170 | |
171 put_tag(pb, "RIFF"); | |
172 put_le32(pb, 0); /* file length */ | |
173 put_tag(pb, "WAVE"); | |
174 | |
175 /* format header */ | |
176 fmt = start_tag(pb, "fmt "); | |
177 if (put_wav_header(pb, &s->streams[0]->codec) < 0) { | |
178 av_free(wav); | |
179 return -1; | |
180 } | |
181 end_tag(pb, fmt); | |
182 | |
183 /* data header */ | |
184 wav->data = start_tag(pb, "data"); | |
185 | |
186 put_flush_packet(pb); | |
187 | |
188 return 0; | |
189 } | |
190 | |
191 static int wav_write_packet(AVFormatContext *s, int stream_index_ptr, | |
65 | 192 uint8_t *buf, int size, int force_pts) |
0 | 193 { |
194 ByteIOContext *pb = &s->pb; | |
195 put_buffer(pb, buf, size); | |
196 return 0; | |
197 } | |
198 | |
199 static int wav_write_trailer(AVFormatContext *s) | |
200 { | |
201 ByteIOContext *pb = &s->pb; | |
202 WAVContext *wav = s->priv_data; | |
203 offset_t file_size; | |
204 | |
205 if (!url_is_streamed(&s->pb)) { | |
206 end_tag(pb, wav->data); | |
207 | |
208 /* update file size */ | |
209 file_size = url_ftell(pb); | |
210 url_fseek(pb, 4, SEEK_SET); | |
65 | 211 put_le32(pb, (uint32_t)(file_size - 8)); |
0 | 212 url_fseek(pb, file_size, SEEK_SET); |
213 | |
214 put_flush_packet(pb); | |
215 } | |
216 return 0; | |
217 } | |
218 | |
219 /* return the size of the found tag */ | |
220 /* XXX: > 2GB ? */ | |
65 | 221 static int find_tag(ByteIOContext *pb, uint32_t tag1) |
0 | 222 { |
223 unsigned int tag; | |
224 int size; | |
225 | |
226 for(;;) { | |
227 if (url_feof(pb)) | |
228 return -1; | |
229 tag = get_le32(pb); | |
230 size = get_le32(pb); | |
231 if (tag == tag1) | |
232 break; | |
233 url_fseek(pb, size, SEEK_CUR); | |
234 } | |
235 if (size < 0) | |
236 size = 0x7fffffff; | |
237 return size; | |
238 } | |
239 | |
240 static int wav_probe(AVProbeData *p) | |
241 { | |
242 /* check file header */ | |
243 if (p->buf_size <= 32) | |
244 return 0; | |
245 if (p->buf[0] == 'R' && p->buf[1] == 'I' && | |
246 p->buf[2] == 'F' && p->buf[3] == 'F' && | |
247 p->buf[8] == 'W' && p->buf[9] == 'A' && | |
248 p->buf[10] == 'V' && p->buf[11] == 'E') | |
249 return AVPROBE_SCORE_MAX; | |
250 else | |
251 return 0; | |
252 } | |
253 | |
254 /* wav input */ | |
255 static int wav_read_header(AVFormatContext *s, | |
256 AVFormatParameters *ap) | |
257 { | |
258 int size; | |
259 unsigned int tag; | |
260 ByteIOContext *pb = &s->pb; | |
261 AVStream *st; | |
262 | |
263 /* check RIFF header */ | |
264 tag = get_le32(pb); | |
265 | |
266 if (tag != MKTAG('R', 'I', 'F', 'F')) | |
267 return -1; | |
268 get_le32(pb); /* file size */ | |
269 tag = get_le32(pb); | |
270 if (tag != MKTAG('W', 'A', 'V', 'E')) | |
271 return -1; | |
272 | |
273 /* parse fmt header */ | |
274 size = find_tag(pb, MKTAG('f', 'm', 't', ' ')); | |
275 if (size < 0) | |
276 return -1; | |
277 st = av_new_stream(s, 0); | |
278 if (!st) | |
279 return AVERROR_NOMEM; | |
280 | |
84
0068a6902911
correct AUDIO strf parsing patch by (Roman Shaposhnick <rvs at sun dot com>)
michaelni
parents:
74
diff
changeset
|
281 get_wav_header(pb, &st->codec, size); |
0 | 282 |
283 size = find_tag(pb, MKTAG('d', 'a', 't', 'a')); | |
284 if (size < 0) | |
285 return -1; | |
286 return 0; | |
287 } | |
288 | |
289 #define MAX_SIZE 4096 | |
290 | |
291 static int wav_read_packet(AVFormatContext *s, | |
292 AVPacket *pkt) | |
293 { | |
294 int ret; | |
295 | |
296 if (url_feof(&s->pb)) | |
297 return -EIO; | |
298 if (av_new_packet(pkt, MAX_SIZE)) | |
299 return -EIO; | |
300 pkt->stream_index = 0; | |
301 | |
302 ret = get_buffer(&s->pb, pkt->data, pkt->size); | |
303 if (ret < 0) | |
304 av_free_packet(pkt); | |
305 /* note: we need to modify the packet size here to handle the last | |
306 packet */ | |
307 pkt->size = ret; | |
308 return ret; | |
309 } | |
310 | |
311 static int wav_read_close(AVFormatContext *s) | |
312 { | |
313 return 0; | |
314 } | |
315 | |
316 static AVInputFormat wav_iformat = { | |
317 "wav", | |
318 "wav format", | |
319 0, | |
320 wav_probe, | |
321 wav_read_header, | |
322 wav_read_packet, | |
323 wav_read_close, | |
324 }; | |
325 | |
326 static AVOutputFormat wav_oformat = { | |
327 "wav", | |
328 "wav format", | |
329 "audio/x-wav", | |
330 "wav", | |
331 sizeof(WAVContext), | |
332 CODEC_ID_PCM_S16LE, | |
333 CODEC_ID_NONE, | |
334 wav_write_header, | |
335 wav_write_packet, | |
336 wav_write_trailer, | |
337 }; | |
338 | |
339 int wav_init(void) | |
340 { | |
341 av_register_input_format(&wav_iformat); | |
342 av_register_output_format(&wav_oformat); | |
343 return 0; | |
344 } |