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/log.h"
27 #include "libavutil/mathematics.h"
28 #include "libavutil/opt.h"
30 #include "avio_internal.h"
34 #include "avio_internal.h"
48 static inline void bwf_write_bext_string(AVFormatContext *s, const char *key, int maxlen)
50 AVDictionaryEntry *tag;
53 if (tag = av_dict_get(s->metadata, key, NULL, 0)) {
54 len = strlen(tag->value);
55 len = FFMIN(len, maxlen);
56 avio_write(s->pb, tag->value, len);
59 ffio_fill(s->pb, 0, maxlen - len);
62 static void bwf_write_bext_chunk(AVFormatContext *s)
64 AVDictionaryEntry *tmp_tag;
65 uint64_t time_reference = 0;
66 int64_t bext = ff_start_tag(s->pb, "bext");
68 bwf_write_bext_string(s, "description", 256);
69 bwf_write_bext_string(s, "originator", 32);
70 bwf_write_bext_string(s, "originator_reference", 32);
71 bwf_write_bext_string(s, "origination_date", 10);
72 bwf_write_bext_string(s, "origination_time", 8);
74 if (tmp_tag = av_dict_get(s->metadata, "time_reference", NULL, 0))
75 time_reference = strtoll(tmp_tag->value, NULL, 10);
76 avio_wl64(s->pb, time_reference);
77 avio_wl16(s->pb, 1); // set version to 1
79 if (tmp_tag = av_dict_get(s->metadata, "umid", NULL, 0)) {
80 unsigned char umidpart_str[17] = {0};
83 int len = strlen(tmp_tag->value+2);
85 for (i = 0; i < len/16; i++) {
86 memcpy(umidpart_str, tmp_tag->value + 2 + (i*16), 16);
87 umidpart = strtoll(umidpart_str, NULL, 16);
88 avio_wb64(s->pb, umidpart);
90 ffio_fill(s->pb, 0, 64 - i*8);
92 ffio_fill(s->pb, 0, 64); // zero UMID
94 ffio_fill(s->pb, 0, 190); // Reserved
96 if (tmp_tag = av_dict_get(s->metadata, "coding_history", NULL, 0))
97 avio_put_str(s->pb, tmp_tag->value);
99 ff_end_tag(s->pb, bext);
102 static int wav_write_header(AVFormatContext *s)
104 WAVContext *wav = s->priv_data;
105 AVIOContext *pb = s->pb;
108 ffio_wfourcc(pb, "RIFF");
109 avio_wl32(pb, 0); /* file length */
110 ffio_wfourcc(pb, "WAVE");
113 fmt = ff_start_tag(pb, "fmt ");
114 if (ff_put_wav_header(pb, s->streams[0]->codec) < 0) {
115 av_log(s, AV_LOG_ERROR, "%s codec not supported in WAVE format\n",
116 s->streams[0]->codec->codec ? s->streams[0]->codec->codec->name : "NONE");
121 if (s->streams[0]->codec->codec_tag != 0x01 /* hence for all other than PCM */
122 && s->pb->seekable) {
123 fact = ff_start_tag(pb, "fact");
125 ff_end_tag(pb, fact);
129 bwf_write_bext_chunk(s);
131 av_set_pts_info(s->streams[0], 64, 1, s->streams[0]->codec->sample_rate);
132 wav->maxpts = wav->last_duration = 0;
133 wav->minpts = INT64_MAX;
136 wav->data = ff_start_tag(pb, "data");
143 static int wav_write_packet(AVFormatContext *s, AVPacket *pkt)
145 AVIOContext *pb = s->pb;
146 WAVContext *wav = s->priv_data;
147 avio_write(pb, pkt->data, pkt->size);
148 if(pkt->pts != AV_NOPTS_VALUE) {
149 wav->minpts = FFMIN(wav->minpts, pkt->pts);
150 wav->maxpts = FFMAX(wav->maxpts, pkt->pts);
151 wav->last_duration = pkt->duration;
153 av_log(s, AV_LOG_ERROR, "wav_write_packet: NOPTS\n");
157 static int wav_write_trailer(AVFormatContext *s)
159 AVIOContext *pb = s->pb;
160 WAVContext *wav = s->priv_data;
165 if (s->pb->seekable) {
166 ff_end_tag(pb, wav->data);
168 /* update file size */
169 file_size = avio_tell(pb);
170 avio_seek(pb, 4, SEEK_SET);
171 avio_wl32(pb, (uint32_t)(file_size - 8));
172 avio_seek(pb, file_size, SEEK_SET);
176 if(s->streams[0]->codec->codec_tag != 0x01) {
177 /* Update num_samps in fact chunk */
178 int number_of_samples;
179 number_of_samples = av_rescale(wav->maxpts - wav->minpts + wav->last_duration,
180 s->streams[0]->codec->sample_rate * (int64_t)s->streams[0]->time_base.num,
181 s->streams[0]->time_base.den);
182 avio_seek(pb, wav->data-12, SEEK_SET);
183 avio_wl32(pb, number_of_samples);
184 avio_seek(pb, file_size, SEEK_SET);
191 #define OFFSET(x) offsetof(WAVContext, x)
192 #define ENC AV_OPT_FLAG_ENCODING_PARAM
193 static const AVOption options[] = {
194 { "write_bext", "Write BEXT chunk.", OFFSET(write_bext), FF_OPT_TYPE_INT, { 0 }, 0, 1, ENC },
198 static const AVClass wav_muxer_class = {
199 .class_name = "WAV muxer",
200 .item_name = av_default_item_name,
202 .version = LIBAVUTIL_VERSION_INT,
205 AVOutputFormat ff_wav_muxer = {
207 NULL_IF_CONFIG_SMALL("WAV format"),
216 .codec_tag= (const AVCodecTag* const []){ff_codec_wav_tags, 0},
217 .priv_class = &wav_muxer_class,
219 #endif /* CONFIG_WAV_MUXER */
222 #if CONFIG_WAV_DEMUXER
224 static int64_t next_tag(AVIOContext *pb, unsigned int *tag)
226 *tag = avio_rl32(pb);
227 return avio_rl32(pb);
230 /* return the size of the found tag */
231 static int64_t find_tag(AVIOContext *pb, uint32_t tag1)
239 size = next_tag(pb, &tag);
247 static int wav_probe(AVProbeData *p)
249 /* check file header */
250 if (p->buf_size <= 32)
252 if (!memcmp(p->buf + 8, "WAVE", 4)) {
253 if (!memcmp(p->buf, "RIFF", 4))
255 Since ACT demuxer has standard WAV header at top of it's own,
256 returning score is decreased to avoid probe conflict
259 return AVPROBE_SCORE_MAX - 1;
260 else if (!memcmp(p->buf, "RF64", 4) &&
261 !memcmp(p->buf + 12, "ds64", 4))
262 return AVPROBE_SCORE_MAX;
267 static int wav_parse_fmt_tag(AVFormatContext *s, int64_t size, AVStream **st)
269 AVIOContext *pb = s->pb;
272 /* parse fmt header */
273 *st = av_new_stream(s, 0);
275 return AVERROR(ENOMEM);
277 ret = ff_get_wav_header(pb, (*st)->codec, size);
280 (*st)->need_parsing = AVSTREAM_PARSE_FULL;
282 av_set_pts_info(*st, 64, 1, (*st)->codec->sample_rate);
288 static int wav_read_header(AVFormatContext *s,
289 AVFormatParameters *ap)
291 int64_t size, av_uninit(data_size);
292 int64_t sample_count=0;
295 AVIOContext *pb = s->pb;
297 WAVContext *wav = s->priv_data;
298 int ret, got_fmt = 0;
299 int64_t next_tag_ofs;
301 /* check RIFF header */
304 rf64 = tag == MKTAG('R', 'F', '6', '4');
305 if (!rf64 && tag != MKTAG('R', 'I', 'F', 'F'))
307 avio_rl32(pb); /* file size */
309 if (tag != MKTAG('W', 'A', 'V', 'E'))
313 if (avio_rl32(pb) != MKTAG('d', 's', '6', '4'))
315 size = avio_rl32(pb);
318 avio_rl64(pb); /* RIFF size */
319 data_size = avio_rl64(pb);
320 sample_count = avio_rl64(pb);
321 if (data_size < 0 || sample_count < 0) {
322 av_log(s, AV_LOG_ERROR, "negative data_size and/or sample_count in "
323 "ds64: data_size = %"PRId64", sample_count = %"PRId64"\n",
324 data_size, sample_count);
325 return AVERROR_INVALIDDATA;
327 avio_skip(pb, size - 16); /* skip rest of ds64 chunk */
334 size = next_tag(pb, &tag);
335 next_tag_ofs = avio_tell(pb) + size;
337 if (tag == MKTAG('f', 'm', 't', ' ')) {
338 /* only parse the first 'fmt ' tag found */
339 if (!got_fmt && (ret = wav_parse_fmt_tag(s, size, &st) < 0)) {
342 av_log(s, AV_LOG_WARNING, "found more than one 'fmt ' tag\n");
345 } else if (tag == MKTAG('d', 'a', 't', 'a')) {
347 av_log(s, AV_LOG_ERROR, "found no 'fmt ' tag before the 'data' tag\n");
348 return AVERROR_INVALIDDATA;
352 }else if (tag == MKTAG('f','a','c','t') && !sample_count){
353 sample_count = avio_rl32(pb);
355 avio_seek(pb, next_tag_ofs, SEEK_SET);
362 wav->data_end = INT64_MAX;
364 wav->data_end= avio_tell(pb) + size;
366 if (!sample_count && st->codec->channels && av_get_bits_per_sample(st->codec->codec_id))
367 sample_count = (size<<3) / (st->codec->channels * (uint64_t)av_get_bits_per_sample(st->codec->codec_id));
369 st->duration = sample_count;
373 /** Find chunk with w64 GUID by skipping over other chunks
374 * @return the size of the found chunk
376 static int64_t find_guid(AVIOContext *pb, const uint8_t guid1[16])
381 while (!pb->eof_reached) {
382 avio_read(pb, guid, 16);
383 size = avio_rl64(pb);
386 if (!memcmp(guid, guid1, 16))
388 avio_skip(pb, FFALIGN(size, INT64_C(8)) - 24);
393 static const uint8_t guid_data[16] = { 'd', 'a', 't', 'a',
394 0xF3, 0xAC, 0xD3, 0x11, 0x8C, 0xD1, 0x00, 0xC0, 0x4F, 0x8E, 0xDB, 0x8A };
396 #define MAX_SIZE 4096
398 static int wav_read_packet(AVFormatContext *s,
404 WAVContext *wav = s->priv_data;
408 left = wav->data_end - avio_tell(s->pb);
410 if (CONFIG_W64_DEMUXER && wav->w64)
411 left = find_guid(s->pb, guid_data) - 24;
413 left = find_tag(s->pb, MKTAG('d', 'a', 't', 'a'));
416 wav->data_end= avio_tell(s->pb) + left;
420 if (st->codec->block_align > 1) {
421 if (size < st->codec->block_align)
422 size = st->codec->block_align;
423 size = (size / st->codec->block_align) * st->codec->block_align;
425 size = FFMIN(size, left);
426 ret = av_get_packet(s->pb, pkt, size);
429 pkt->stream_index = 0;
434 static int wav_read_seek(AVFormatContext *s,
435 int stream_index, int64_t timestamp, int flags)
440 switch (st->codec->codec_id) {
445 /* use generic seeking with dynamically generated indexes */
450 return pcm_read_seek(s, stream_index, timestamp, flags);
453 AVInputFormat ff_wav_demuxer = {
455 NULL_IF_CONFIG_SMALL("WAV format"),
462 .flags= AVFMT_GENERIC_INDEX,
463 .codec_tag= (const AVCodecTag* const []){ff_codec_wav_tags, 0},
465 #endif /* CONFIG_WAV_DEMUXER */
468 #if CONFIG_W64_DEMUXER
469 static const uint8_t guid_riff[16] = { 'r', 'i', 'f', 'f',
470 0x2E, 0x91, 0xCF, 0x11, 0xA5, 0xD6, 0x28, 0xDB, 0x04, 0xC1, 0x00, 0x00 };
472 static const uint8_t guid_wave[16] = { 'w', 'a', 'v', 'e',
473 0xF3, 0xAC, 0xD3, 0x11, 0x8C, 0xD1, 0x00, 0xC0, 0x4F, 0x8E, 0xDB, 0x8A };
475 static const uint8_t guid_fmt [16] = { 'f', 'm', 't', ' ',
476 0xF3, 0xAC, 0xD3, 0x11, 0x8C, 0xD1, 0x00, 0xC0, 0x4F, 0x8E, 0xDB, 0x8A };
478 static int w64_probe(AVProbeData *p)
480 if (p->buf_size <= 40)
482 if (!memcmp(p->buf, guid_riff, 16) &&
483 !memcmp(p->buf + 24, guid_wave, 16))
484 return AVPROBE_SCORE_MAX;
489 static int w64_read_header(AVFormatContext *s, AVFormatParameters *ap)
492 AVIOContext *pb = s->pb;
493 WAVContext *wav = s->priv_data;
498 avio_read(pb, guid, 16);
499 if (memcmp(guid, guid_riff, 16))
502 if (avio_rl64(pb) < 16 + 8 + 16 + 8 + 16 + 8) /* riff + wave + fmt + sizes */
505 avio_read(pb, guid, 16);
506 if (memcmp(guid, guid_wave, 16)) {
507 av_log(s, AV_LOG_ERROR, "could not find wave guid\n");
511 size = find_guid(pb, guid_fmt);
513 av_log(s, AV_LOG_ERROR, "could not find fmt guid\n");
517 st = av_new_stream(s, 0);
519 return AVERROR(ENOMEM);
521 /* subtract chunk header size - normal wav file doesn't count it */
522 ret = ff_get_wav_header(pb, st->codec, size - 24);
525 avio_skip(pb, FFALIGN(size, INT64_C(8)) - size);
527 st->need_parsing = AVSTREAM_PARSE_FULL;
529 av_set_pts_info(st, 64, 1, st->codec->sample_rate);
531 size = find_guid(pb, guid_data);
533 av_log(s, AV_LOG_ERROR, "could not find data guid\n");
536 wav->data_end = avio_tell(pb) + size - 24;
542 AVInputFormat ff_w64_demuxer = {
544 NULL_IF_CONFIG_SMALL("Sony Wave64 format"),
551 .flags = AVFMT_GENERIC_INDEX,
552 .codec_tag = (const AVCodecTag* const []){ff_codec_wav_tags, 0},
554 #endif /* CONFIG_W64_DEMUXER */