2 * WAV muxer and demuxer
3 * Copyright (c) 2001, 2002 Fabrice Bellard
7 * Copyright (c) 2009 Daniel Verkamp
9 * This file is part of Libav.
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.
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.
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
26 #include "libavutil/mathematics.h"
28 #include "avio_internal.h"
42 static int wav_write_header(AVFormatContext *s)
44 WAVContext *wav = s->priv_data;
45 AVIOContext *pb = s->pb;
48 ffio_wfourcc(pb, "RIFF");
49 avio_wl32(pb, 0); /* file length */
50 ffio_wfourcc(pb, "WAVE");
53 fmt = ff_start_tag(pb, "fmt ");
54 if (ff_put_wav_header(pb, s->streams[0]->codec) < 0) {
55 av_log(s, AV_LOG_ERROR, "%s codec not supported in WAVE format\n",
56 s->streams[0]->codec->codec ? s->streams[0]->codec->codec->name : "NONE");
61 if (s->streams[0]->codec->codec_tag != 0x01 /* hence for all other than PCM */
63 fact = ff_start_tag(pb, "fact");
68 av_set_pts_info(s->streams[0], 64, 1, s->streams[0]->codec->sample_rate);
69 wav->maxpts = wav->last_duration = 0;
70 wav->minpts = INT64_MAX;
73 wav->data = ff_start_tag(pb, "data");
80 static int wav_write_packet(AVFormatContext *s, AVPacket *pkt)
82 AVIOContext *pb = s->pb;
83 WAVContext *wav = s->priv_data;
84 avio_write(pb, pkt->data, pkt->size);
85 if(pkt->pts != AV_NOPTS_VALUE) {
86 wav->minpts = FFMIN(wav->minpts, pkt->pts);
87 wav->maxpts = FFMAX(wav->maxpts, pkt->pts);
88 wav->last_duration = pkt->duration;
90 av_log(s, AV_LOG_ERROR, "wav_write_packet: NOPTS\n");
94 static int wav_write_trailer(AVFormatContext *s)
96 AVIOContext *pb = s->pb;
97 WAVContext *wav = s->priv_data;
102 if (s->pb->seekable) {
103 ff_end_tag(pb, wav->data);
105 /* update file size */
106 file_size = avio_tell(pb);
107 avio_seek(pb, 4, SEEK_SET);
108 avio_wl32(pb, (uint32_t)(file_size - 8));
109 avio_seek(pb, file_size, SEEK_SET);
113 if(s->streams[0]->codec->codec_tag != 0x01) {
114 /* Update num_samps in fact chunk */
115 int number_of_samples;
116 number_of_samples = av_rescale(wav->maxpts - wav->minpts + wav->last_duration,
117 s->streams[0]->codec->sample_rate * (int64_t)s->streams[0]->time_base.num,
118 s->streams[0]->time_base.den);
119 avio_seek(pb, wav->data-12, SEEK_SET);
120 avio_wl32(pb, number_of_samples);
121 avio_seek(pb, file_size, SEEK_SET);
128 AVOutputFormat ff_wav_muxer = {
130 NULL_IF_CONFIG_SMALL("WAV format"),
139 .codec_tag= (const AVCodecTag* const []){ff_codec_wav_tags, 0},
141 #endif /* CONFIG_WAV_MUXER */
144 #if CONFIG_WAV_DEMUXER
146 static int64_t next_tag(AVIOContext *pb, unsigned int *tag)
148 *tag = avio_rl32(pb);
149 return avio_rl32(pb);
152 /* return the size of the found tag */
153 static int64_t find_tag(AVIOContext *pb, uint32_t tag1)
161 size = next_tag(pb, &tag);
169 static int wav_probe(AVProbeData *p)
171 /* check file header */
172 if (p->buf_size <= 32)
174 if (!memcmp(p->buf + 8, "WAVE", 4)) {
175 if (!memcmp(p->buf, "RIFF", 4))
177 Since ACT demuxer has standard WAV header at top of it's own,
178 returning score is decreased to avoid probe conflict
181 return AVPROBE_SCORE_MAX - 1;
182 else if (!memcmp(p->buf, "RF64", 4) &&
183 !memcmp(p->buf + 12, "ds64", 4))
184 return AVPROBE_SCORE_MAX;
190 static int wav_read_header(AVFormatContext *s,
191 AVFormatParameters *ap)
193 int64_t size, av_uninit(data_size);
194 int64_t sample_count=0;
197 AVIOContext *pb = s->pb;
199 WAVContext *wav = s->priv_data;
202 /* check RIFF header */
205 rf64 = tag == MKTAG('R', 'F', '6', '4');
206 if (!rf64 && tag != MKTAG('R', 'I', 'F', 'F'))
208 avio_rl32(pb); /* file size */
210 if (tag != MKTAG('W', 'A', 'V', 'E'))
214 if (avio_rl32(pb) != MKTAG('d', 's', '6', '4'))
216 size = avio_rl32(pb);
219 avio_rl64(pb); /* RIFF size */
220 data_size = avio_rl64(pb);
221 sample_count = avio_rl64(pb);
222 avio_skip(pb, size - 16); /* skip rest of ds64 chunk */
225 /* parse fmt header */
226 size = find_tag(pb, MKTAG('f', 'm', 't', ' '));
229 st = av_new_stream(s, 0);
231 return AVERROR(ENOMEM);
233 ret = ff_get_wav_header(pb, st->codec, size);
236 st->need_parsing = AVSTREAM_PARSE_FULL;
238 av_set_pts_info(st, 64, 1, st->codec->sample_rate);
243 size = next_tag(pb, &tag);
244 if (tag == MKTAG('d', 'a', 't', 'a')){
246 }else if (tag == MKTAG('f','a','c','t') && !sample_count){
247 sample_count = avio_rl32(pb);
257 wav->data_end = INT64_MAX;
259 wav->data_end= avio_tell(pb) + size;
261 if (!sample_count && st->codec->channels && av_get_bits_per_sample(st->codec->codec_id))
262 sample_count = (size<<3) / (st->codec->channels * (uint64_t)av_get_bits_per_sample(st->codec->codec_id));
264 st->duration = sample_count;
268 /** Find chunk with w64 GUID by skipping over other chunks
269 * @return the size of the found chunk
271 static int64_t find_guid(AVIOContext *pb, const uint8_t guid1[16])
276 while (!pb->eof_reached) {
277 avio_read(pb, guid, 16);
278 size = avio_rl64(pb);
281 if (!memcmp(guid, guid1, 16))
283 avio_skip(pb, FFALIGN(size, INT64_C(8)) - 24);
288 static const uint8_t guid_data[16] = { 'd', 'a', 't', 'a',
289 0xF3, 0xAC, 0xD3, 0x11, 0x8C, 0xD1, 0x00, 0xC0, 0x4F, 0x8E, 0xDB, 0x8A };
291 #define MAX_SIZE 4096
293 static int wav_read_packet(AVFormatContext *s,
299 WAVContext *wav = s->priv_data;
303 left = wav->data_end - avio_tell(s->pb);
305 if (CONFIG_W64_DEMUXER && wav->w64)
306 left = find_guid(s->pb, guid_data) - 24;
308 left = find_tag(s->pb, MKTAG('d', 'a', 't', 'a'));
311 wav->data_end= avio_tell(s->pb) + left;
315 if (st->codec->block_align > 1) {
316 if (size < st->codec->block_align)
317 size = st->codec->block_align;
318 size = (size / st->codec->block_align) * st->codec->block_align;
320 size = FFMIN(size, left);
321 ret = av_get_packet(s->pb, pkt, size);
324 pkt->stream_index = 0;
329 static int wav_read_seek(AVFormatContext *s,
330 int stream_index, int64_t timestamp, int flags)
335 switch (st->codec->codec_id) {
340 /* use generic seeking with dynamically generated indexes */
345 return pcm_read_seek(s, stream_index, timestamp, flags);
348 AVInputFormat ff_wav_demuxer = {
350 NULL_IF_CONFIG_SMALL("WAV format"),
357 .flags= AVFMT_GENERIC_INDEX,
358 .codec_tag= (const AVCodecTag* const []){ff_codec_wav_tags, 0},
360 #endif /* CONFIG_WAV_DEMUXER */
363 #if CONFIG_W64_DEMUXER
364 static const uint8_t guid_riff[16] = { 'r', 'i', 'f', 'f',
365 0x2E, 0x91, 0xCF, 0x11, 0xA5, 0xD6, 0x28, 0xDB, 0x04, 0xC1, 0x00, 0x00 };
367 static const uint8_t guid_wave[16] = { 'w', 'a', 'v', 'e',
368 0xF3, 0xAC, 0xD3, 0x11, 0x8C, 0xD1, 0x00, 0xC0, 0x4F, 0x8E, 0xDB, 0x8A };
370 static const uint8_t guid_fmt [16] = { 'f', 'm', 't', ' ',
371 0xF3, 0xAC, 0xD3, 0x11, 0x8C, 0xD1, 0x00, 0xC0, 0x4F, 0x8E, 0xDB, 0x8A };
373 static int w64_probe(AVProbeData *p)
375 if (p->buf_size <= 40)
377 if (!memcmp(p->buf, guid_riff, 16) &&
378 !memcmp(p->buf + 24, guid_wave, 16))
379 return AVPROBE_SCORE_MAX;
384 static int w64_read_header(AVFormatContext *s, AVFormatParameters *ap)
387 AVIOContext *pb = s->pb;
388 WAVContext *wav = s->priv_data;
393 avio_read(pb, guid, 16);
394 if (memcmp(guid, guid_riff, 16))
397 if (avio_rl64(pb) < 16 + 8 + 16 + 8 + 16 + 8) /* riff + wave + fmt + sizes */
400 avio_read(pb, guid, 16);
401 if (memcmp(guid, guid_wave, 16)) {
402 av_log(s, AV_LOG_ERROR, "could not find wave guid\n");
406 size = find_guid(pb, guid_fmt);
408 av_log(s, AV_LOG_ERROR, "could not find fmt guid\n");
412 st = av_new_stream(s, 0);
414 return AVERROR(ENOMEM);
416 /* subtract chunk header size - normal wav file doesn't count it */
417 ret = ff_get_wav_header(pb, st->codec, size - 24);
420 avio_skip(pb, FFALIGN(size, INT64_C(8)) - size);
422 st->need_parsing = AVSTREAM_PARSE_FULL;
424 av_set_pts_info(st, 64, 1, st->codec->sample_rate);
426 size = find_guid(pb, guid_data);
428 av_log(s, AV_LOG_ERROR, "could not find data guid\n");
431 wav->data_end = avio_tell(pb) + size - 24;
437 AVInputFormat ff_w64_demuxer = {
439 NULL_IF_CONFIG_SMALL("Sony Wave64 format"),
446 .flags = AVFMT_GENERIC_INDEX,
447 .codec_tag = (const AVCodecTag* const []){ff_codec_wav_tags, 0},
449 #endif /* CONFIG_W64_DEMUXER */