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"
41 static int wav_write_header(AVFormatContext *s)
43 WAVContext *wav = s->priv_data;
44 AVIOContext *pb = s->pb;
47 ffio_wfourcc(pb, "RIFF");
48 avio_wl32(pb, 0); /* file length */
49 ffio_wfourcc(pb, "WAVE");
52 fmt = ff_start_tag(pb, "fmt ");
53 if (ff_put_wav_header(pb, s->streams[0]->codec) < 0) {
54 av_log(s, AV_LOG_ERROR, "%s codec not supported in WAVE format\n",
55 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 */
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 (s->pb->seekable) {
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;
188 static int wav_parse_fmt_tag(AVFormatContext *s, int64_t size, AVStream **st)
190 AVIOContext *pb = s->pb;
193 /* parse fmt header */
194 *st = av_new_stream(s, 0);
196 return AVERROR(ENOMEM);
198 ret = ff_get_wav_header(pb, (*st)->codec, size);
201 (*st)->need_parsing = AVSTREAM_PARSE_FULL;
203 av_set_pts_info(*st, 64, 1, (*st)->codec->sample_rate);
208 static inline int wav_parse_bext_string(AVFormatContext *s, const char *key, int length)
213 if ((ret = avio_read(s->pb, temp, length)) < 0)
219 return av_metadata_set2(&s->metadata, key, temp, 0);
224 static int wav_parse_bext_tag(AVFormatContext *s, int64_t size)
226 char temp[131], *coding_history;
228 uint64_t time_reference;
229 int64_t umid_parts[8], umid_mask = 0;
231 if ((ret = wav_parse_bext_string(s, "description", 256)) < 0 ||
232 (ret = wav_parse_bext_string(s, "originator", 32)) < 0 ||
233 (ret = wav_parse_bext_string(s, "originator_reference", 32)) < 0 ||
234 (ret = wav_parse_bext_string(s, "origination_date", 10)) < 0 ||
235 (ret = wav_parse_bext_string(s, "origination_time", 8)) < 0)
238 time_reference = avio_rl64(s->pb);
239 snprintf(temp, sizeof(temp), "%"PRIu64, time_reference);
240 if ((ret = av_metadata_set2(&s->metadata, "time_reference", temp, 0)) < 0)
243 /* check if version is >= 1, in which case an UMID may be present */
244 if (avio_rl16(s->pb) >= 1) {
245 for (x = 0; x < 8; x++)
246 umid_mask |= umid_parts[x] = avio_rb64(s->pb);
249 /* the string formatting below is per SMPTE 330M-2004 Annex C */
250 if (umid_parts[4] == 0 && umid_parts[5] == 0 && umid_parts[6] == 0 && umid_parts[7] == 0) {
252 snprintf(temp, sizeof(temp), "0x%016"PRIX64"%016"PRIX64"%016"PRIX64"%016"PRIX64,
253 umid_parts[0], umid_parts[1], umid_parts[2], umid_parts[3]);
256 snprintf(temp, sizeof(temp), "0x%016"PRIX64"%016"PRIX64"%016"PRIX64"%016"PRIX64
257 "0x%016"PRIX64"%016"PRIX64"%016"PRIX64"%016"PRIX64,
258 umid_parts[0], umid_parts[1], umid_parts[2], umid_parts[3],
259 umid_parts[4], umid_parts[5], umid_parts[6], umid_parts[7]);
262 if ((ret = av_metadata_set2(&s->metadata, "umid", temp, 0)) < 0)
266 avio_skip(s->pb, 190);
268 avio_skip(s->pb, 254);
271 /* CodingHistory present */
274 if (!(coding_history = av_malloc(size+1)))
275 return AVERROR(ENOMEM);
277 if ((ret = avio_read(s->pb, coding_history, size)) < 0)
280 coding_history[size] = 0;
281 if ((ret = av_metadata_set2(&s->metadata, "coding_history", coding_history,
282 AV_METADATA_DONT_STRDUP_VAL)) < 0)
289 static const AVMetadataConv wav_metadata_conv[] = {
290 {"description", "comment" },
291 {"originator", "encoded_by" },
292 {"origination_date", "date" },
293 {"origination_time", "creation_time"},
298 static int wav_read_header(AVFormatContext *s,
299 AVFormatParameters *ap)
301 int64_t size, av_uninit(data_size);
302 int64_t sample_count=0;
305 AVIOContext *pb = s->pb;
307 WAVContext *wav = s->priv_data;
308 int ret, got_fmt = 0;
309 int64_t next_tag_ofs, data_ofs = -1;
311 /* check RIFF header */
314 rf64 = tag == MKTAG('R', 'F', '6', '4');
315 if (!rf64 && tag != MKTAG('R', 'I', 'F', 'F'))
317 avio_rl32(pb); /* file size */
319 if (tag != MKTAG('W', 'A', 'V', 'E'))
323 if (avio_rl32(pb) != MKTAG('d', 's', '6', '4'))
325 size = avio_rl32(pb);
328 avio_rl64(pb); /* RIFF size */
329 data_size = avio_rl64(pb);
330 sample_count = avio_rl64(pb);
331 if (data_size < 0 || sample_count < 0) {
332 av_log(s, AV_LOG_ERROR, "negative data_size and/or sample_count in "
333 "ds64: data_size = %"PRId64", sample_count = %"PRId64"\n",
334 data_size, sample_count);
335 return AVERROR_INVALIDDATA;
337 avio_skip(pb, size - 24); /* skip rest of ds64 chunk */
341 size = next_tag(pb, &tag);
342 next_tag_ofs = avio_tell(pb) + size;
348 case MKTAG('f', 'm', 't', ' '):
349 /* only parse the first 'fmt ' tag found */
350 if (!got_fmt && (ret = wav_parse_fmt_tag(s, size, &st) < 0)) {
353 av_log(s, AV_LOG_WARNING, "found more than one 'fmt ' tag\n");
357 case MKTAG('d', 'a', 't', 'a'):
359 av_log(s, AV_LOG_ERROR, "found no 'fmt ' tag before the 'data' tag\n");
360 return AVERROR_INVALIDDATA;
364 next_tag_ofs = wav->data_end = avio_tell(pb) + data_size;
367 next_tag_ofs = wav->data_end = size ? next_tag_ofs : INT64_MAX;
370 data_ofs = avio_tell(pb);
372 /* don't look for footer metadata if we can't seek or if we don't
373 * know where the data tag ends
375 if (!pb->seekable || (!rf64 && !size))
378 case MKTAG('f','a','c','t'):
380 sample_count = avio_rl32(pb);
382 case MKTAG('b','e','x','t'):
383 if ((ret = wav_parse_bext_tag(s, size)) < 0)
388 /* seek to next tag unless we know that we'll run into EOF */
389 if ((avio_size(pb) > 0 && next_tag_ofs >= avio_size(pb)) ||
390 avio_seek(pb, next_tag_ofs, SEEK_SET) < 0) {
396 av_log(s, AV_LOG_ERROR, "no 'data' tag found\n");
397 return AVERROR_INVALIDDATA;
400 avio_seek(pb, data_ofs, SEEK_SET);
402 if (!sample_count && st->codec->channels && av_get_bits_per_sample(st->codec->codec_id))
403 sample_count = (data_size<<3) / (st->codec->channels * (uint64_t)av_get_bits_per_sample(st->codec->codec_id));
405 st->duration = sample_count;
407 ff_metadata_conv_ctx(s, NULL, wav_metadata_conv);
412 /** Find chunk with w64 GUID by skipping over other chunks
413 * @return the size of the found chunk
415 static int64_t find_guid(AVIOContext *pb, const uint8_t guid1[16])
420 while (!url_feof(pb)) {
421 avio_read(pb, guid, 16);
422 size = avio_rl64(pb);
425 if (!memcmp(guid, guid1, 16))
427 avio_skip(pb, FFALIGN(size, INT64_C(8)) - 24);
432 static const uint8_t guid_data[16] = { 'd', 'a', 't', 'a',
433 0xF3, 0xAC, 0xD3, 0x11, 0x8C, 0xD1, 0x00, 0xC0, 0x4F, 0x8E, 0xDB, 0x8A };
435 #define MAX_SIZE 4096
437 static int wav_read_packet(AVFormatContext *s,
443 WAVContext *wav = s->priv_data;
447 left = wav->data_end - avio_tell(s->pb);
449 if (CONFIG_W64_DEMUXER && wav->w64)
450 left = find_guid(s->pb, guid_data) - 24;
452 left = find_tag(s->pb, MKTAG('d', 'a', 't', 'a'));
455 wav->data_end= avio_tell(s->pb) + left;
459 if (st->codec->block_align > 1) {
460 if (size < st->codec->block_align)
461 size = st->codec->block_align;
462 size = (size / st->codec->block_align) * st->codec->block_align;
464 size = FFMIN(size, left);
465 ret = av_get_packet(s->pb, pkt, size);
468 pkt->stream_index = 0;
473 static int wav_read_seek(AVFormatContext *s,
474 int stream_index, int64_t timestamp, int flags)
479 switch (st->codec->codec_id) {
484 /* use generic seeking with dynamically generated indexes */
489 return pcm_read_seek(s, stream_index, timestamp, flags);
492 AVInputFormat ff_wav_demuxer = {
494 NULL_IF_CONFIG_SMALL("WAV format"),
501 .flags= AVFMT_GENERIC_INDEX,
502 .codec_tag= (const AVCodecTag* const []){ff_codec_wav_tags, 0},
504 #endif /* CONFIG_WAV_DEMUXER */
507 #if CONFIG_W64_DEMUXER
508 static const uint8_t guid_riff[16] = { 'r', 'i', 'f', 'f',
509 0x2E, 0x91, 0xCF, 0x11, 0xA5, 0xD6, 0x28, 0xDB, 0x04, 0xC1, 0x00, 0x00 };
511 static const uint8_t guid_wave[16] = { 'w', 'a', 'v', 'e',
512 0xF3, 0xAC, 0xD3, 0x11, 0x8C, 0xD1, 0x00, 0xC0, 0x4F, 0x8E, 0xDB, 0x8A };
514 static const uint8_t guid_fmt [16] = { 'f', 'm', 't', ' ',
515 0xF3, 0xAC, 0xD3, 0x11, 0x8C, 0xD1, 0x00, 0xC0, 0x4F, 0x8E, 0xDB, 0x8A };
517 static int w64_probe(AVProbeData *p)
519 if (p->buf_size <= 40)
521 if (!memcmp(p->buf, guid_riff, 16) &&
522 !memcmp(p->buf + 24, guid_wave, 16))
523 return AVPROBE_SCORE_MAX;
528 static int w64_read_header(AVFormatContext *s, AVFormatParameters *ap)
531 AVIOContext *pb = s->pb;
532 WAVContext *wav = s->priv_data;
537 avio_read(pb, guid, 16);
538 if (memcmp(guid, guid_riff, 16))
541 if (avio_rl64(pb) < 16 + 8 + 16 + 8 + 16 + 8) /* riff + wave + fmt + sizes */
544 avio_read(pb, guid, 16);
545 if (memcmp(guid, guid_wave, 16)) {
546 av_log(s, AV_LOG_ERROR, "could not find wave guid\n");
550 size = find_guid(pb, guid_fmt);
552 av_log(s, AV_LOG_ERROR, "could not find fmt guid\n");
556 st = av_new_stream(s, 0);
558 return AVERROR(ENOMEM);
560 /* subtract chunk header size - normal wav file doesn't count it */
561 ret = ff_get_wav_header(pb, st->codec, size - 24);
564 avio_skip(pb, FFALIGN(size, INT64_C(8)) - size);
566 st->need_parsing = AVSTREAM_PARSE_FULL;
568 av_set_pts_info(st, 64, 1, st->codec->sample_rate);
570 size = find_guid(pb, guid_data);
572 av_log(s, AV_LOG_ERROR, "could not find data guid\n");
575 wav->data_end = avio_tell(pb) + size - 24;
581 AVInputFormat ff_w64_demuxer = {
583 NULL_IF_CONFIG_SMALL("Sony Wave64 format"),
590 .flags = AVFMT_GENERIC_INDEX,
591 .codec_tag = (const AVCodecTag* const []){ff_codec_wav_tags, 0},
593 #endif /* CONFIG_W64_DEMUXER */