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");
60 if (s->streams[0]->codec->codec_tag != 0x01 /* hence for all other than PCM */
61 && !url_is_streamed(s->pb)) {
62 fact = ff_start_tag(pb, "fact");
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;
72 wav->data = ff_start_tag(pb, "data");
79 static int wav_write_packet(AVFormatContext *s, AVPacket *pkt)
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;
89 av_log(s, AV_LOG_ERROR, "wav_write_packet: NOPTS\n");
93 static int wav_write_trailer(AVFormatContext *s)
95 AVIOContext *pb = s->pb;
96 WAVContext *wav = s->priv_data;
101 if (!url_is_streamed(s->pb)) {
102 ff_end_tag(pb, wav->data);
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);
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);
127 AVOutputFormat ff_wav_muxer = {
129 NULL_IF_CONFIG_SMALL("WAV format"),
138 .codec_tag= (const AVCodecTag* const []){ff_codec_wav_tags, 0},
140 #endif /* CONFIG_WAV_MUXER */
143 #if CONFIG_WAV_DEMUXER
145 static int64_t next_tag(AVIOContext *pb, unsigned int *tag)
147 *tag = avio_rl32(pb);
148 return avio_rl32(pb);
151 /* return the size of the found tag */
152 static int64_t find_tag(AVIOContext *pb, uint32_t tag1)
160 size = next_tag(pb, &tag);
168 static int wav_probe(AVProbeData *p)
170 /* check file header */
171 if (p->buf_size <= 32)
173 if (!memcmp(p->buf + 8, "WAVE", 4)) {
174 if (!memcmp(p->buf, "RIFF", 4))
176 Since ACT demuxer has standard WAV header at top of it's own,
177 returning score is decreased to avoid probe conflict
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;
189 static int wav_read_header(AVFormatContext *s,
190 AVFormatParameters *ap)
192 int64_t size, av_uninit(data_size);
193 int64_t sample_count=0;
196 AVIOContext *pb = s->pb;
198 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 ff_get_wav_header(pb, st->codec, size);
232 st->need_parsing = AVSTREAM_PARSE_FULL;
234 av_set_pts_info(st, 64, 1, st->codec->sample_rate);
239 size = next_tag(pb, &tag);
240 if (tag == MKTAG('d', 'a', 't', 'a')){
242 }else if (tag == MKTAG('f','a','c','t') && !sample_count){
243 sample_count = avio_rl32(pb);
253 wav->data_end = INT64_MAX;
255 wav->data_end= avio_tell(pb) + size;
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));
260 st->duration = sample_count;
264 /** Find chunk with w64 GUID by skipping over other chunks
265 * @return the size of the found chunk
267 static int64_t find_guid(AVIOContext *pb, const uint8_t guid1[16])
272 while (!url_feof(pb)) {
273 avio_read(pb, guid, 16);
274 size = avio_rl64(pb);
277 if (!memcmp(guid, guid1, 16))
279 avio_skip(pb, FFALIGN(size, INT64_C(8)) - 24);
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 };
287 #define MAX_SIZE 4096
289 static int wav_read_packet(AVFormatContext *s,
295 WAVContext *wav = s->priv_data;
299 left = wav->data_end - avio_tell(s->pb);
301 if (CONFIG_W64_DEMUXER && wav->w64)
302 left = find_guid(s->pb, guid_data) - 24;
304 left = find_tag(s->pb, MKTAG('d', 'a', 't', 'a'));
307 wav->data_end= avio_tell(s->pb) + left;
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;
316 size = FFMIN(size, left);
317 ret = av_get_packet(s->pb, pkt, size);
320 pkt->stream_index = 0;
325 static int wav_read_seek(AVFormatContext *s,
326 int stream_index, int64_t timestamp, int flags)
331 switch (st->codec->codec_id) {
336 /* use generic seeking with dynamically generated indexes */
341 return pcm_read_seek(s, stream_index, timestamp, flags);
344 AVInputFormat ff_wav_demuxer = {
346 NULL_IF_CONFIG_SMALL("WAV format"),
353 .flags= AVFMT_GENERIC_INDEX,
354 .codec_tag= (const AVCodecTag* const []){ff_codec_wav_tags, 0},
356 #endif /* CONFIG_WAV_DEMUXER */
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 };
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 };
366 static const uint8_t guid_fmt [16] = { 'f', 'm', 't', ' ',
367 0xF3, 0xAC, 0xD3, 0x11, 0x8C, 0xD1, 0x00, 0xC0, 0x4F, 0x8E, 0xDB, 0x8A };
369 static int w64_probe(AVProbeData *p)
371 if (p->buf_size <= 40)
373 if (!memcmp(p->buf, guid_riff, 16) &&
374 !memcmp(p->buf + 24, guid_wave, 16))
375 return AVPROBE_SCORE_MAX;
380 static int w64_read_header(AVFormatContext *s, AVFormatParameters *ap)
383 AVIOContext *pb = s->pb;
384 WAVContext *wav = s->priv_data;
388 avio_read(pb, guid, 16);
389 if (memcmp(guid, guid_riff, 16))
392 if (avio_rl64(pb) < 16 + 8 + 16 + 8 + 16 + 8) /* riff + wave + fmt + sizes */
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");
401 size = find_guid(pb, guid_fmt);
403 av_log(s, AV_LOG_ERROR, "could not find fmt guid\n");
407 st = av_new_stream(s, 0);
409 return AVERROR(ENOMEM);
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);
415 st->need_parsing = AVSTREAM_PARSE_FULL;
417 av_set_pts_info(st, 64, 1, st->codec->sample_rate);
419 size = find_guid(pb, guid_data);
421 av_log(s, AV_LOG_ERROR, "could not find data guid\n");
424 wav->data_end = avio_tell(pb) + size - 24;
430 AVInputFormat ff_w64_demuxer = {
432 NULL_IF_CONFIG_SMALL("Sony Wave64 format"),
439 .flags = AVFMT_GENERIC_INDEX,
440 .codec_tag = (const AVCodecTag* const []){ff_codec_wav_tags, 0},
442 #endif /* CONFIG_W64_DEMUXER */