2 * WAV muxer and demuxer
3 * Copyright (c) 2001, 2002 Fabrice Bellard
7 * Copyright (c) 2009 Daniel Verkamp
9 * This file is part of FFmpeg.
11 * FFmpeg 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 * FFmpeg 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 FFmpeg; if not, write to the Free Software
23 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
26 #include "avio_internal.h"
40 static int wav_write_header(AVFormatContext *s)
42 WAVContext *wav = s->priv_data;
43 AVIOContext *pb = s->pb;
46 ffio_wfourcc(pb, "RIFF");
47 avio_wl32(pb, 0); /* file length */
48 ffio_wfourcc(pb, "WAVE");
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");
59 if (s->streams[0]->codec->codec_tag != 0x01 /* hence for all other than PCM */
61 fact = ff_start_tag(pb, "fact");
66 av_set_pts_info(s->streams[0], 64, 1, s->streams[0]->codec->sample_rate);
67 wav->maxpts = wav->last_duration = 0;
68 wav->minpts = INT64_MAX;
71 wav->data = ff_start_tag(pb, "data");
78 static int wav_write_packet(AVFormatContext *s, AVPacket *pkt)
80 AVIOContext *pb = s->pb;
81 WAVContext *wav = s->priv_data;
82 avio_write(pb, pkt->data, pkt->size);
83 if(pkt->pts != AV_NOPTS_VALUE) {
84 wav->minpts = FFMIN(wav->minpts, pkt->pts);
85 wav->maxpts = FFMAX(wav->maxpts, pkt->pts);
86 wav->last_duration = pkt->duration;
88 av_log(s, AV_LOG_ERROR, "wav_write_packet: NOPTS\n");
92 static int wav_write_trailer(AVFormatContext *s)
94 AVIOContext *pb = s->pb;
95 WAVContext *wav = s->priv_data;
100 if (s->pb->seekable) {
101 ff_end_tag(pb, wav->data);
103 /* update file size */
104 file_size = avio_tell(pb);
105 avio_seek(pb, 4, SEEK_SET);
106 avio_wl32(pb, (uint32_t)(file_size - 8));
107 avio_seek(pb, file_size, SEEK_SET);
111 if(s->streams[0]->codec->codec_tag != 0x01) {
112 /* Update num_samps in fact chunk */
113 int number_of_samples;
114 number_of_samples = av_rescale(wav->maxpts - wav->minpts + wav->last_duration,
115 s->streams[0]->codec->sample_rate * (int64_t)s->streams[0]->time_base.num,
116 s->streams[0]->time_base.den);
117 avio_seek(pb, wav->data-12, SEEK_SET);
118 avio_wl32(pb, number_of_samples);
119 avio_seek(pb, file_size, SEEK_SET);
126 AVOutputFormat ff_wav_muxer = {
128 NULL_IF_CONFIG_SMALL("WAV format"),
137 .codec_tag= (const AVCodecTag* const []){ff_codec_wav_tags, 0},
139 #endif /* CONFIG_WAV_MUXER */
142 #if CONFIG_WAV_DEMUXER
144 static int64_t next_tag(AVIOContext *pb, unsigned int *tag)
146 *tag = avio_rl32(pb);
147 return avio_rl32(pb);
150 /* return the size of the found tag */
151 static int64_t find_tag(AVIOContext *pb, uint32_t tag1)
159 size = next_tag(pb, &tag);
167 static int wav_probe(AVProbeData *p)
169 /* check file header */
170 if (p->buf_size <= 32)
172 if (!memcmp(p->buf + 8, "WAVE", 4)) {
173 if (!memcmp(p->buf, "RIFF", 4))
175 Since ACT demuxer has standard WAV header at top of it's own,
176 returning score is decreased to avoid probe conflict
179 return AVPROBE_SCORE_MAX - 1;
180 else if (!memcmp(p->buf, "RF64", 4) &&
181 !memcmp(p->buf + 12, "ds64", 4))
182 return AVPROBE_SCORE_MAX;
187 static int wav_parse_fmt_tag(AVFormatContext *s, int64_t size, AVStream **st)
189 AVIOContext *pb = s->pb;
192 /* parse fmt header */
193 *st = av_new_stream(s, 0);
195 return AVERROR(ENOMEM);
197 ff_get_wav_header(pb, (*st)->codec, size);
200 (*st)->need_parsing = AVSTREAM_PARSE_FULL;
202 av_set_pts_info(*st, 64, 1, (*st)->codec->sample_rate);
208 static int wav_read_header(AVFormatContext *s,
209 AVFormatParameters *ap)
211 int64_t size, av_uninit(data_size);
212 int64_t sample_count=0;
215 AVIOContext *pb = s->pb;
217 WAVContext *wav = s->priv_data;
218 int ret, got_fmt = 0;
219 int64_t next_tag_ofs;
221 /* check RIFF header */
224 rf64 = tag == MKTAG('R', 'F', '6', '4');
225 if (!rf64 && tag != MKTAG('R', 'I', 'F', 'F'))
227 avio_rl32(pb); /* file size */
229 if (tag != MKTAG('W', 'A', 'V', 'E'))
233 if (avio_rl32(pb) != MKTAG('d', 's', '6', '4'))
235 size = avio_rl32(pb);
238 avio_rl64(pb); /* RIFF size */
239 data_size = avio_rl64(pb);
240 sample_count = avio_rl64(pb);
241 if (data_size < 0 || sample_count < 0) {
242 av_log(s, AV_LOG_ERROR, "negative data_size and/or sample_count in "
243 "ds64: data_size = %li, sample_count = %li\n",
244 data_size, sample_count);
245 return AVERROR_INVALIDDATA;
247 avio_skip(pb, size - 16); /* skip rest of ds64 chunk */
254 size = next_tag(pb, &tag);
255 next_tag_ofs = avio_tell(pb) + size;
257 if (tag == MKTAG('f', 'm', 't', ' ')) {
258 /* only parse the first 'fmt ' tag found */
259 if (!got_fmt && (ret = wav_parse_fmt_tag(s, size, &st) < 0)) {
262 av_log(s, AV_LOG_WARNING, "found more than one 'fmt ' tag\n");
265 } else if (tag == MKTAG('d', 'a', 't', 'a')) {
267 av_log(s, AV_LOG_ERROR, "found no 'fmt ' tag before the 'data' tag\n");
268 return AVERROR_INVALIDDATA;
272 }else if (tag == MKTAG('f','a','c','t') && !sample_count){
273 sample_count = avio_rl32(pb);
275 avio_seek(pb, next_tag_ofs, SEEK_SET);
282 wav->data_end = INT64_MAX;
284 wav->data_end= avio_tell(pb) + size;
286 if (!sample_count && st->codec->channels && av_get_bits_per_sample(st->codec->codec_id))
287 sample_count = (size<<3) / (st->codec->channels * (uint64_t)av_get_bits_per_sample(st->codec->codec_id));
289 st->duration = sample_count;
293 /** Find chunk with w64 GUID by skipping over other chunks
294 * @return the size of the found chunk
296 static int64_t find_guid(AVIOContext *pb, const uint8_t guid1[16])
301 while (!url_feof(pb)) {
302 avio_read(pb, guid, 16);
303 size = avio_rl64(pb);
306 if (!memcmp(guid, guid1, 16))
308 avio_skip(pb, FFALIGN(size, INT64_C(8)) - 24);
313 static const uint8_t guid_data[16] = { 'd', 'a', 't', 'a',
314 0xF3, 0xAC, 0xD3, 0x11, 0x8C, 0xD1, 0x00, 0xC0, 0x4F, 0x8E, 0xDB, 0x8A };
316 #define MAX_SIZE 4096
318 static int wav_read_packet(AVFormatContext *s,
324 WAVContext *wav = s->priv_data;
328 left = wav->data_end - avio_tell(s->pb);
330 if (CONFIG_W64_DEMUXER && wav->w64)
331 left = find_guid(s->pb, guid_data) - 24;
333 left = find_tag(s->pb, MKTAG('d', 'a', 't', 'a'));
336 wav->data_end= avio_tell(s->pb) + left;
340 if (st->codec->block_align > 1) {
341 if (size < st->codec->block_align)
342 size = st->codec->block_align;
343 size = (size / st->codec->block_align) * st->codec->block_align;
345 size = FFMIN(size, left);
346 ret = av_get_packet(s->pb, pkt, size);
349 pkt->stream_index = 0;
354 static int wav_read_seek(AVFormatContext *s,
355 int stream_index, int64_t timestamp, int flags)
360 switch (st->codec->codec_id) {
365 /* use generic seeking with dynamically generated indexes */
370 return pcm_read_seek(s, stream_index, timestamp, flags);
373 AVInputFormat ff_wav_demuxer = {
375 NULL_IF_CONFIG_SMALL("WAV format"),
382 .flags= AVFMT_GENERIC_INDEX,
383 .codec_tag= (const AVCodecTag* const []){ff_codec_wav_tags, 0},
385 #endif /* CONFIG_WAV_DEMUXER */
388 #if CONFIG_W64_DEMUXER
389 static const uint8_t guid_riff[16] = { 'r', 'i', 'f', 'f',
390 0x2E, 0x91, 0xCF, 0x11, 0xA5, 0xD6, 0x28, 0xDB, 0x04, 0xC1, 0x00, 0x00 };
392 static const uint8_t guid_wave[16] = { 'w', 'a', 'v', 'e',
393 0xF3, 0xAC, 0xD3, 0x11, 0x8C, 0xD1, 0x00, 0xC0, 0x4F, 0x8E, 0xDB, 0x8A };
395 static const uint8_t guid_fmt [16] = { 'f', 'm', 't', ' ',
396 0xF3, 0xAC, 0xD3, 0x11, 0x8C, 0xD1, 0x00, 0xC0, 0x4F, 0x8E, 0xDB, 0x8A };
398 static int w64_probe(AVProbeData *p)
400 if (p->buf_size <= 40)
402 if (!memcmp(p->buf, guid_riff, 16) &&
403 !memcmp(p->buf + 24, guid_wave, 16))
404 return AVPROBE_SCORE_MAX;
409 static int w64_read_header(AVFormatContext *s, AVFormatParameters *ap)
412 AVIOContext *pb = s->pb;
413 WAVContext *wav = s->priv_data;
418 avio_read(pb, guid, 16);
419 if (memcmp(guid, guid_riff, 16))
422 if (avio_rl64(pb) < 16 + 8 + 16 + 8 + 16 + 8) /* riff + wave + fmt + sizes */
425 avio_read(pb, guid, 16);
426 if (memcmp(guid, guid_wave, 16)) {
427 av_log(s, AV_LOG_ERROR, "could not find wave guid\n");
431 size = find_guid(pb, guid_fmt);
433 av_log(s, AV_LOG_ERROR, "could not find fmt guid\n");
437 st = av_new_stream(s, 0);
439 return AVERROR(ENOMEM);
441 /* subtract chunk header size - normal wav file doesn't count it */
442 ret = ff_get_wav_header(pb, st->codec, size - 24);
445 avio_skip(pb, FFALIGN(size, INT64_C(8)) - size);
447 st->need_parsing = AVSTREAM_PARSE_FULL;
449 av_set_pts_info(st, 64, 1, st->codec->sample_rate);
451 size = find_guid(pb, guid_data);
453 av_log(s, AV_LOG_ERROR, "could not find data guid\n");
456 wav->data_end = avio_tell(pb) + size - 24;
462 AVInputFormat ff_w64_demuxer = {
464 NULL_IF_CONFIG_SMALL("Sony Wave64 format"),
471 .flags = AVFMT_GENERIC_INDEX,
472 .codec_tag = (const AVCodecTag* const []){ff_codec_wav_tags, 0},
474 #endif /* CONFIG_W64_DEMUXER */