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;
188 static int wav_read_header(AVFormatContext *s,
189 AVFormatParameters *ap)
191 int64_t size, av_uninit(data_size);
192 int64_t sample_count=0;
195 AVIOContext *pb = s->pb;
197 WAVContext *wav = s->priv_data;
200 /* check RIFF header */
203 rf64 = tag == MKTAG('R', 'F', '6', '4');
204 if (!rf64 && tag != MKTAG('R', 'I', 'F', 'F'))
206 avio_rl32(pb); /* file size */
208 if (tag != MKTAG('W', 'A', 'V', 'E'))
212 if (avio_rl32(pb) != MKTAG('d', 's', '6', '4'))
214 size = avio_rl32(pb);
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 */
223 /* parse fmt header */
224 size = find_tag(pb, MKTAG('f', 'm', 't', ' '));
227 st = av_new_stream(s, 0);
229 return AVERROR(ENOMEM);
231 ret = ff_get_wav_header(pb, st->codec, size);
234 st->need_parsing = AVSTREAM_PARSE_FULL;
236 av_set_pts_info(st, 64, 1, st->codec->sample_rate);
241 size = next_tag(pb, &tag);
242 if (tag == MKTAG('d', 'a', 't', 'a')){
244 }else if (tag == MKTAG('f','a','c','t') && !sample_count){
245 sample_count = avio_rl32(pb);
255 wav->data_end = INT64_MAX;
257 wav->data_end= avio_tell(pb) + size;
259 if (!sample_count && st->codec->channels && av_get_bits_per_sample(st->codec->codec_id))
260 sample_count = (size<<3) / (st->codec->channels * (uint64_t)av_get_bits_per_sample(st->codec->codec_id));
262 st->duration = sample_count;
266 /** Find chunk with w64 GUID by skipping over other chunks
267 * @return the size of the found chunk
269 static int64_t find_guid(AVIOContext *pb, const uint8_t guid1[16])
274 while (!url_feof(pb)) {
275 avio_read(pb, guid, 16);
276 size = avio_rl64(pb);
279 if (!memcmp(guid, guid1, 16))
281 avio_skip(pb, FFALIGN(size, INT64_C(8)) - 24);
286 static const uint8_t guid_data[16] = { 'd', 'a', 't', 'a',
287 0xF3, 0xAC, 0xD3, 0x11, 0x8C, 0xD1, 0x00, 0xC0, 0x4F, 0x8E, 0xDB, 0x8A };
289 #define MAX_SIZE 4096
291 static int wav_read_packet(AVFormatContext *s,
297 WAVContext *wav = s->priv_data;
301 left = wav->data_end - avio_tell(s->pb);
303 if (CONFIG_W64_DEMUXER && wav->w64)
304 left = find_guid(s->pb, guid_data) - 24;
306 left = find_tag(s->pb, MKTAG('d', 'a', 't', 'a'));
309 wav->data_end= avio_tell(s->pb) + left;
313 if (st->codec->block_align > 1) {
314 if (size < st->codec->block_align)
315 size = st->codec->block_align;
316 size = (size / st->codec->block_align) * st->codec->block_align;
318 size = FFMIN(size, left);
319 ret = av_get_packet(s->pb, pkt, size);
322 pkt->stream_index = 0;
327 static int wav_read_seek(AVFormatContext *s,
328 int stream_index, int64_t timestamp, int flags)
333 switch (st->codec->codec_id) {
338 /* use generic seeking with dynamically generated indexes */
343 return pcm_read_seek(s, stream_index, timestamp, flags);
346 AVInputFormat ff_wav_demuxer = {
348 NULL_IF_CONFIG_SMALL("WAV format"),
355 .flags= AVFMT_GENERIC_INDEX,
356 .codec_tag= (const AVCodecTag* const []){ff_codec_wav_tags, 0},
358 #endif /* CONFIG_WAV_DEMUXER */
361 #if CONFIG_W64_DEMUXER
362 static const uint8_t guid_riff[16] = { 'r', 'i', 'f', 'f',
363 0x2E, 0x91, 0xCF, 0x11, 0xA5, 0xD6, 0x28, 0xDB, 0x04, 0xC1, 0x00, 0x00 };
365 static const uint8_t guid_wave[16] = { 'w', 'a', 'v', 'e',
366 0xF3, 0xAC, 0xD3, 0x11, 0x8C, 0xD1, 0x00, 0xC0, 0x4F, 0x8E, 0xDB, 0x8A };
368 static const uint8_t guid_fmt [16] = { 'f', 'm', 't', ' ',
369 0xF3, 0xAC, 0xD3, 0x11, 0x8C, 0xD1, 0x00, 0xC0, 0x4F, 0x8E, 0xDB, 0x8A };
371 static int w64_probe(AVProbeData *p)
373 if (p->buf_size <= 40)
375 if (!memcmp(p->buf, guid_riff, 16) &&
376 !memcmp(p->buf + 24, guid_wave, 16))
377 return AVPROBE_SCORE_MAX;
382 static int w64_read_header(AVFormatContext *s, AVFormatParameters *ap)
385 AVIOContext *pb = s->pb;
386 WAVContext *wav = s->priv_data;
391 avio_read(pb, guid, 16);
392 if (memcmp(guid, guid_riff, 16))
395 if (avio_rl64(pb) < 16 + 8 + 16 + 8 + 16 + 8) /* riff + wave + fmt + sizes */
398 avio_read(pb, guid, 16);
399 if (memcmp(guid, guid_wave, 16)) {
400 av_log(s, AV_LOG_ERROR, "could not find wave guid\n");
404 size = find_guid(pb, guid_fmt);
406 av_log(s, AV_LOG_ERROR, "could not find fmt guid\n");
410 st = av_new_stream(s, 0);
412 return AVERROR(ENOMEM);
414 /* subtract chunk header size - normal wav file doesn't count it */
415 ret = ff_get_wav_header(pb, st->codec, size - 24);
418 avio_skip(pb, FFALIGN(size, INT64_C(8)) - size);
420 st->need_parsing = AVSTREAM_PARSE_FULL;
422 av_set_pts_info(st, 64, 1, st->codec->sample_rate);
424 size = find_guid(pb, guid_data);
426 av_log(s, AV_LOG_ERROR, "could not find data guid\n");
429 wav->data_end = avio_tell(pb) + size - 24;
435 AVInputFormat ff_w64_demuxer = {
437 NULL_IF_CONFIG_SMALL("Sony Wave64 format"),
444 .flags = AVFMT_GENERIC_INDEX,
445 .codec_tag = (const AVCodecTag* const []){ff_codec_wav_tags, 0},
447 #endif /* CONFIG_W64_DEMUXER */