47685d466a1c07f17ddae738b8d862a66fe15815
[libav.git] / libavformat / wav.c
1 /*
2 * WAV muxer and demuxer
3 * Copyright (c) 2001, 2002 Fabrice Bellard
4 *
5 * Sony Wave64 demuxer
6 * RF64 demuxer
7 * Copyright (c) 2009 Daniel Verkamp
8 *
9 * This file is part of Libav.
10 *
11 * Libav is free software; you can redistribute it and/or
12 * modify it under the terms of the GNU Lesser General Public
13 * License as published by the Free Software Foundation; either
14 * version 2.1 of the License, or (at your option) any later version.
15 *
16 * Libav is distributed in the hope that it will be useful,
17 * but WITHOUT ANY WARRANTY; without even the implied warranty of
18 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
19 * Lesser General Public License for more details.
20 *
21 * You should have received a copy of the GNU Lesser General Public
22 * License along with Libav; if not, write to the Free Software
23 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
24 */
25 #include "avformat.h"
26 #include "avio_internal.h"
27 #include "pcm.h"
28 #include "riff.h"
29
30 typedef struct {
31 int64_t data;
32 int64_t data_end;
33 int64_t minpts;
34 int64_t maxpts;
35 int last_duration;
36 int w64;
37 } WAVContext;
38
39 #if CONFIG_WAV_MUXER
40 static int wav_write_header(AVFormatContext *s)
41 {
42 WAVContext *wav = s->priv_data;
43 AVIOContext *pb = s->pb;
44 int64_t fmt, fact;
45
46 ffio_wfourcc(pb, "RIFF");
47 avio_wl32(pb, 0); /* file length */
48 ffio_wfourcc(pb, "WAVE");
49
50 /* format header */
51 fmt = ff_start_tag(pb, "fmt ");
52 if (ff_put_wav_header(pb, s->streams[0]->codec) < 0) {
53 av_log(s, AV_LOG_ERROR, "%s codec not supported in WAVE format\n",
54 s->streams[0]->codec->codec ? s->streams[0]->codec->codec->name : "NONE");
55 av_free(wav);
56 return -1;
57 }
58 ff_end_tag(pb, fmt);
59
60 if (s->streams[0]->codec->codec_tag != 0x01 /* hence for all other than PCM */
61 && s->pb->seekable) {
62 fact = ff_start_tag(pb, "fact");
63 avio_wl32(pb, 0);
64 ff_end_tag(pb, fact);
65 }
66
67 av_set_pts_info(s->streams[0], 64, 1, s->streams[0]->codec->sample_rate);
68 wav->maxpts = wav->last_duration = 0;
69 wav->minpts = INT64_MAX;
70
71 /* data header */
72 wav->data = ff_start_tag(pb, "data");
73
74 avio_flush(pb);
75
76 return 0;
77 }
78
79 static int wav_write_packet(AVFormatContext *s, AVPacket *pkt)
80 {
81 AVIOContext *pb = s->pb;
82 WAVContext *wav = s->priv_data;
83 avio_write(pb, pkt->data, pkt->size);
84 if(pkt->pts != AV_NOPTS_VALUE) {
85 wav->minpts = FFMIN(wav->minpts, pkt->pts);
86 wav->maxpts = FFMAX(wav->maxpts, pkt->pts);
87 wav->last_duration = pkt->duration;
88 } else
89 av_log(s, AV_LOG_ERROR, "wav_write_packet: NOPTS\n");
90 return 0;
91 }
92
93 static int wav_write_trailer(AVFormatContext *s)
94 {
95 AVIOContext *pb = s->pb;
96 WAVContext *wav = s->priv_data;
97 int64_t file_size;
98
99 avio_flush(pb);
100
101 if (s->pb->seekable) {
102 ff_end_tag(pb, wav->data);
103
104 /* update file size */
105 file_size = avio_tell(pb);
106 avio_seek(pb, 4, SEEK_SET);
107 avio_wl32(pb, (uint32_t)(file_size - 8));
108 avio_seek(pb, file_size, SEEK_SET);
109
110 avio_flush(pb);
111
112 if(s->streams[0]->codec->codec_tag != 0x01) {
113 /* Update num_samps in fact chunk */
114 int number_of_samples;
115 number_of_samples = av_rescale(wav->maxpts - wav->minpts + wav->last_duration,
116 s->streams[0]->codec->sample_rate * (int64_t)s->streams[0]->time_base.num,
117 s->streams[0]->time_base.den);
118 avio_seek(pb, wav->data-12, SEEK_SET);
119 avio_wl32(pb, number_of_samples);
120 avio_seek(pb, file_size, SEEK_SET);
121 avio_flush(pb);
122 }
123 }
124 return 0;
125 }
126
127 AVOutputFormat ff_wav_muxer = {
128 "wav",
129 NULL_IF_CONFIG_SMALL("WAV format"),
130 "audio/x-wav",
131 "wav",
132 sizeof(WAVContext),
133 CODEC_ID_PCM_S16LE,
134 CODEC_ID_NONE,
135 wav_write_header,
136 wav_write_packet,
137 wav_write_trailer,
138 .codec_tag= (const AVCodecTag* const []){ff_codec_wav_tags, 0},
139 };
140 #endif /* CONFIG_WAV_MUXER */
141
142
143 #if CONFIG_WAV_DEMUXER
144
145 static int64_t next_tag(AVIOContext *pb, unsigned int *tag)
146 {
147 *tag = avio_rl32(pb);
148 return avio_rl32(pb);
149 }
150
151 /* return the size of the found tag */
152 static int64_t find_tag(AVIOContext *pb, uint32_t tag1)
153 {
154 unsigned int tag;
155 int64_t size;
156
157 for (;;) {
158 if (pb->eof_reached)
159 return -1;
160 size = next_tag(pb, &tag);
161 if (tag == tag1)
162 break;
163 avio_skip(pb, size);
164 }
165 return size;
166 }
167
168 static int wav_probe(AVProbeData *p)
169 {
170 /* check file header */
171 if (p->buf_size <= 32)
172 return 0;
173 if (!memcmp(p->buf + 8, "WAVE", 4)) {
174 if (!memcmp(p->buf, "RIFF", 4))
175 /*
176 Since ACT demuxer has standard WAV header at top of it's own,
177 returning score is decreased to avoid probe conflict
178 between ACT and WAV.
179 */
180 return AVPROBE_SCORE_MAX - 1;
181 else if (!memcmp(p->buf, "RF64", 4) &&
182 !memcmp(p->buf + 12, "ds64", 4))
183 return AVPROBE_SCORE_MAX;
184 }
185 return 0;
186 }
187
188 /* wav input */
189 static int wav_read_header(AVFormatContext *s,
190 AVFormatParameters *ap)
191 {
192 int64_t size, av_uninit(data_size);
193 int64_t sample_count=0;
194 int rf64;
195 unsigned int tag;
196 AVIOContext *pb = s->pb;
197 AVStream *st;
198 WAVContext *wav = s->priv_data;
199
200 /* check RIFF header */
201 tag = avio_rl32(pb);
202
203 rf64 = tag == MKTAG('R', 'F', '6', '4');
204 if (!rf64 && tag != MKTAG('R', 'I', 'F', 'F'))
205 return -1;
206 avio_rl32(pb); /* file size */
207 tag = avio_rl32(pb);
208 if (tag != MKTAG('W', 'A', 'V', 'E'))
209 return -1;
210
211 if (rf64) {
212 if (avio_rl32(pb) != MKTAG('d', 's', '6', '4'))
213 return -1;
214 size = avio_rl32(pb);
215 if (size < 16)
216 return -1;
217 avio_rl64(pb); /* RIFF size */
218 data_size = avio_rl64(pb);
219 sample_count = avio_rl64(pb);
220 avio_skip(pb, size - 16); /* skip rest of ds64 chunk */
221 }
222
223 /* parse fmt header */
224 size = find_tag(pb, MKTAG('f', 'm', 't', ' '));
225 if (size < 0)
226 return -1;
227 st = av_new_stream(s, 0);
228 if (!st)
229 return AVERROR(ENOMEM);
230
231 ff_get_wav_header(pb, st->codec, size);
232 st->need_parsing = AVSTREAM_PARSE_FULL;
233
234 av_set_pts_info(st, 64, 1, st->codec->sample_rate);
235
236 for (;;) {
237 if (pb->eof_reached)
238 return -1;
239 size = next_tag(pb, &tag);
240 if (tag == MKTAG('d', 'a', 't', 'a')){
241 break;
242 }else if (tag == MKTAG('f','a','c','t') && !sample_count){
243 sample_count = avio_rl32(pb);
244 size -= 4;
245 }
246 avio_skip(pb, size);
247 }
248 if (rf64)
249 size = data_size;
250 if (size < 0)
251 return -1;
252 if (!size) {
253 wav->data_end = INT64_MAX;
254 } else
255 wav->data_end= avio_tell(pb) + size;
256
257 if (!sample_count && st->codec->channels && av_get_bits_per_sample(st->codec->codec_id))
258 sample_count = (size<<3) / (st->codec->channels * (uint64_t)av_get_bits_per_sample(st->codec->codec_id));
259 if (sample_count)
260 st->duration = sample_count;
261 return 0;
262 }
263
264 /** Find chunk with w64 GUID by skipping over other chunks
265 * @return the size of the found chunk
266 */
267 static int64_t find_guid(AVIOContext *pb, const uint8_t guid1[16])
268 {
269 uint8_t guid[16];
270 int64_t size;
271
272 while (!pb->eof_reached) {
273 avio_read(pb, guid, 16);
274 size = avio_rl64(pb);
275 if (size <= 24)
276 return -1;
277 if (!memcmp(guid, guid1, 16))
278 return size;
279 avio_skip(pb, FFALIGN(size, INT64_C(8)) - 24);
280 }
281 return -1;
282 }
283
284 static const uint8_t guid_data[16] = { 'd', 'a', 't', 'a',
285 0xF3, 0xAC, 0xD3, 0x11, 0x8C, 0xD1, 0x00, 0xC0, 0x4F, 0x8E, 0xDB, 0x8A };
286
287 #define MAX_SIZE 4096
288
289 static int wav_read_packet(AVFormatContext *s,
290 AVPacket *pkt)
291 {
292 int ret, size;
293 int64_t left;
294 AVStream *st;
295 WAVContext *wav = s->priv_data;
296
297 st = s->streams[0];
298
299 left = wav->data_end - avio_tell(s->pb);
300 if (left <= 0){
301 if (CONFIG_W64_DEMUXER && wav->w64)
302 left = find_guid(s->pb, guid_data) - 24;
303 else
304 left = find_tag(s->pb, MKTAG('d', 'a', 't', 'a'));
305 if (left < 0)
306 return AVERROR_EOF;
307 wav->data_end= avio_tell(s->pb) + left;
308 }
309
310 size = MAX_SIZE;
311 if (st->codec->block_align > 1) {
312 if (size < st->codec->block_align)
313 size = st->codec->block_align;
314 size = (size / st->codec->block_align) * st->codec->block_align;
315 }
316 size = FFMIN(size, left);
317 ret = av_get_packet(s->pb, pkt, size);
318 if (ret < 0)
319 return ret;
320 pkt->stream_index = 0;
321
322 return ret;
323 }
324
325 static int wav_read_seek(AVFormatContext *s,
326 int stream_index, int64_t timestamp, int flags)
327 {
328 AVStream *st;
329
330 st = s->streams[0];
331 switch (st->codec->codec_id) {
332 case CODEC_ID_MP2:
333 case CODEC_ID_MP3:
334 case CODEC_ID_AC3:
335 case CODEC_ID_DTS:
336 /* use generic seeking with dynamically generated indexes */
337 return -1;
338 default:
339 break;
340 }
341 return pcm_read_seek(s, stream_index, timestamp, flags);
342 }
343
344 AVInputFormat ff_wav_demuxer = {
345 "wav",
346 NULL_IF_CONFIG_SMALL("WAV format"),
347 sizeof(WAVContext),
348 wav_probe,
349 wav_read_header,
350 wav_read_packet,
351 NULL,
352 wav_read_seek,
353 .flags= AVFMT_GENERIC_INDEX,
354 .codec_tag= (const AVCodecTag* const []){ff_codec_wav_tags, 0},
355 };
356 #endif /* CONFIG_WAV_DEMUXER */
357
358
359 #if CONFIG_W64_DEMUXER
360 static const uint8_t guid_riff[16] = { 'r', 'i', 'f', 'f',
361 0x2E, 0x91, 0xCF, 0x11, 0xA5, 0xD6, 0x28, 0xDB, 0x04, 0xC1, 0x00, 0x00 };
362
363 static const uint8_t guid_wave[16] = { 'w', 'a', 'v', 'e',
364 0xF3, 0xAC, 0xD3, 0x11, 0x8C, 0xD1, 0x00, 0xC0, 0x4F, 0x8E, 0xDB, 0x8A };
365
366 static const uint8_t guid_fmt [16] = { 'f', 'm', 't', ' ',
367 0xF3, 0xAC, 0xD3, 0x11, 0x8C, 0xD1, 0x00, 0xC0, 0x4F, 0x8E, 0xDB, 0x8A };
368
369 static int w64_probe(AVProbeData *p)
370 {
371 if (p->buf_size <= 40)
372 return 0;
373 if (!memcmp(p->buf, guid_riff, 16) &&
374 !memcmp(p->buf + 24, guid_wave, 16))
375 return AVPROBE_SCORE_MAX;
376 else
377 return 0;
378 }
379
380 static int w64_read_header(AVFormatContext *s, AVFormatParameters *ap)
381 {
382 int64_t size;
383 AVIOContext *pb = s->pb;
384 WAVContext *wav = s->priv_data;
385 AVStream *st;
386 uint8_t guid[16];
387
388 avio_read(pb, guid, 16);
389 if (memcmp(guid, guid_riff, 16))
390 return -1;
391
392 if (avio_rl64(pb) < 16 + 8 + 16 + 8 + 16 + 8) /* riff + wave + fmt + sizes */
393 return -1;
394
395 avio_read(pb, guid, 16);
396 if (memcmp(guid, guid_wave, 16)) {
397 av_log(s, AV_LOG_ERROR, "could not find wave guid\n");
398 return -1;
399 }
400
401 size = find_guid(pb, guid_fmt);
402 if (size < 0) {
403 av_log(s, AV_LOG_ERROR, "could not find fmt guid\n");
404 return -1;
405 }
406
407 st = av_new_stream(s, 0);
408 if (!st)
409 return AVERROR(ENOMEM);
410
411 /* subtract chunk header size - normal wav file doesn't count it */
412 ff_get_wav_header(pb, st->codec, size - 24);
413 avio_skip(pb, FFALIGN(size, INT64_C(8)) - size);
414
415 st->need_parsing = AVSTREAM_PARSE_FULL;
416
417 av_set_pts_info(st, 64, 1, st->codec->sample_rate);
418
419 size = find_guid(pb, guid_data);
420 if (size < 0) {
421 av_log(s, AV_LOG_ERROR, "could not find data guid\n");
422 return -1;
423 }
424 wav->data_end = avio_tell(pb) + size - 24;
425 wav->w64 = 1;
426
427 return 0;
428 }
429
430 AVInputFormat ff_w64_demuxer = {
431 "w64",
432 NULL_IF_CONFIG_SMALL("Sony Wave64 format"),
433 sizeof(WAVContext),
434 w64_probe,
435 w64_read_header,
436 wav_read_packet,
437 NULL,
438 wav_read_seek,
439 .flags = AVFMT_GENERIC_INDEX,
440 .codec_tag = (const AVCodecTag* const []){ff_codec_wav_tags, 0},
441 };
442 #endif /* CONFIG_W64_DEMUXER */