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/avassert.h"
27 #include "libavutil/dict.h"
28 #include "libavutil/log.h"
29 #include "libavutil/mathematics.h"
30 #include "libavutil/opt.h"
33 #include "avio_internal.h"
39 typedef struct WAVDemuxContext {
44 #if CONFIG_WAV_DEMUXER
46 static int64_t next_tag(AVIOContext *pb, uint32_t *tag)
52 /* return the size of the found tag */
53 static int64_t find_tag(AVIOContext *pb, uint32_t tag1)
61 size = next_tag(pb, &tag);
69 static int wav_probe(AVProbeData *p)
71 /* check file header */
72 if (p->buf_size <= 32)
74 if (!memcmp(p->buf + 8, "WAVE", 4)) {
75 if (!memcmp(p->buf, "RIFF", 4))
76 /* Since the ACT demuxer has a standard WAV header at the top of
77 * its own, the returned score is decreased to avoid a probe
78 * conflict between ACT and WAV. */
79 return AVPROBE_SCORE_MAX - 1;
80 else if (!memcmp(p->buf, "RF64", 4) &&
81 !memcmp(p->buf + 12, "ds64", 4))
82 return AVPROBE_SCORE_MAX;
87 static int wav_parse_fmt_tag(AVFormatContext *s, int64_t size, AVStream **st)
89 AVIOContext *pb = s->pb;
92 /* parse fmt header */
93 *st = avformat_new_stream(s, NULL);
95 return AVERROR(ENOMEM);
97 ret = ff_get_wav_header(pb, (*st)->codec, size);
100 (*st)->need_parsing = AVSTREAM_PARSE_FULL;
102 avpriv_set_pts_info(*st, 64, 1, (*st)->codec->sample_rate);
107 static inline int wav_parse_bext_string(AVFormatContext *s, const char *key,
113 av_assert0(length <= sizeof(temp));
114 if ((ret = avio_read(s->pb, temp, length)) < 0)
120 return av_dict_set(&s->metadata, key, temp, 0);
125 static int wav_parse_bext_tag(AVFormatContext *s, int64_t size)
127 char temp[131], *coding_history;
129 uint64_t time_reference;
130 int64_t umid_parts[8], umid_mask = 0;
132 if ((ret = wav_parse_bext_string(s, "description", 256)) < 0 ||
133 (ret = wav_parse_bext_string(s, "originator", 32)) < 0 ||
134 (ret = wav_parse_bext_string(s, "originator_reference", 32)) < 0 ||
135 (ret = wav_parse_bext_string(s, "origination_date", 10)) < 0 ||
136 (ret = wav_parse_bext_string(s, "origination_time", 8)) < 0)
139 time_reference = avio_rl64(s->pb);
140 snprintf(temp, sizeof(temp), "%"PRIu64, time_reference);
141 if ((ret = av_dict_set(&s->metadata, "time_reference", temp, 0)) < 0)
144 /* check if version is >= 1, in which case an UMID may be present */
145 if (avio_rl16(s->pb) >= 1) {
146 for (x = 0; x < 8; x++)
147 umid_mask |= umid_parts[x] = avio_rb64(s->pb);
150 /* the string formatting below is per SMPTE 330M-2004 Annex C */
151 if (umid_parts[4] == 0 && umid_parts[5] == 0 &&
152 umid_parts[6] == 0 && umid_parts[7] == 0) {
154 snprintf(temp, sizeof(temp),
155 "0x%016"PRIX64"%016"PRIX64"%016"PRIX64"%016"PRIX64,
156 umid_parts[0], umid_parts[1],
157 umid_parts[2], umid_parts[3]);
160 snprintf(temp, sizeof(temp),
161 "0x%016"PRIX64"%016"PRIX64"%016"PRIX64"%016"PRIX64
162 "0x%016"PRIX64"%016"PRIX64"%016"PRIX64"%016"PRIX64,
163 umid_parts[0], umid_parts[1],
164 umid_parts[2], umid_parts[3],
165 umid_parts[4], umid_parts[5],
166 umid_parts[6], umid_parts[7]);
169 if ((ret = av_dict_set(&s->metadata, "umid", temp, 0)) < 0)
173 avio_skip(s->pb, 190);
175 avio_skip(s->pb, 254);
178 /* CodingHistory present */
181 if (!(coding_history = av_malloc(size + 1)))
182 return AVERROR(ENOMEM);
184 if ((ret = avio_read(s->pb, coding_history, size)) < 0)
187 coding_history[size] = 0;
188 if ((ret = av_dict_set(&s->metadata, "coding_history", coding_history,
189 AV_DICT_DONT_STRDUP_VAL)) < 0)
196 static const AVMetadataConv wav_metadata_conv[] = {
197 { "description", "comment" },
198 { "originator", "encoded_by" },
199 { "origination_date", "date" },
200 { "origination_time", "creation_time" },
205 static int wav_read_header(AVFormatContext *s)
207 int64_t size, av_uninit(data_size);
208 int64_t sample_count = 0;
211 AVIOContext *pb = s->pb;
213 WAVDemuxContext *wav = s->priv_data;
214 int ret, got_fmt = 0;
215 int64_t next_tag_ofs, data_ofs = -1;
217 /* check RIFF header */
220 rf64 = tag == MKTAG('R', 'F', '6', '4');
221 if (!rf64 && tag != MKTAG('R', 'I', 'F', 'F'))
223 avio_rl32(pb); /* file size */
225 if (tag != MKTAG('W', 'A', 'V', 'E'))
229 if (avio_rl32(pb) != MKTAG('d', 's', '6', '4'))
231 size = avio_rl32(pb);
234 avio_rl64(pb); /* RIFF size */
236 data_size = avio_rl64(pb);
237 sample_count = avio_rl64(pb);
239 if (data_size < 0 || sample_count < 0) {
240 av_log(s, AV_LOG_ERROR, "negative data_size and/or sample_count in "
241 "ds64: data_size = %"PRId64", sample_count = %"PRId64"\n",
242 data_size, sample_count);
243 return AVERROR_INVALIDDATA;
245 avio_skip(pb, size - 16); /* skip rest of ds64 chunk */
249 size = next_tag(pb, &tag);
250 next_tag_ofs = avio_tell(pb) + size;
256 case MKTAG('f', 'm', 't', ' '):
257 /* only parse the first 'fmt ' tag found */
258 if (!got_fmt && (ret = wav_parse_fmt_tag(s, size, &st) < 0)) {
261 av_log(s, AV_LOG_WARNING, "found more than one 'fmt ' tag\n");
265 case MKTAG('d', 'a', 't', 'a'):
267 av_log(s, AV_LOG_ERROR,
268 "found no 'fmt ' tag before the 'data' tag\n");
269 return AVERROR_INVALIDDATA;
273 next_tag_ofs = wav->data_end = avio_tell(pb) + data_size;
276 next_tag_ofs = wav->data_end = size ? next_tag_ofs : INT64_MAX;
279 data_ofs = avio_tell(pb);
281 /* don't look for footer metadata if we can't seek or if we don't
282 * know where the data tag ends
284 if (!pb->seekable || (!rf64 && !size))
287 case MKTAG('f', 'a', 'c', 't'):
289 sample_count = avio_rl32(pb);
291 case MKTAG('b', 'e', 'x', 't'):
292 if ((ret = wav_parse_bext_tag(s, size)) < 0)
295 case MKTAG('L', 'I', 'S', 'T'):
297 av_log(s, AV_LOG_ERROR, "too short LIST");
298 return AVERROR_INVALIDDATA;
300 switch (avio_rl32(pb)) {
301 case MKTAG('I', 'N', 'F', 'O'):
302 if ((ret = ff_read_riff_info(s, size - 4)) < 0)
308 /* seek to next tag unless we know that we'll run into EOF */
309 if ((avio_size(pb) > 0 && next_tag_ofs >= avio_size(pb)) ||
310 avio_seek(pb, next_tag_ofs, SEEK_SET) < 0) {
317 av_log(s, AV_LOG_ERROR, "no 'data' tag found\n");
318 return AVERROR_INVALIDDATA;
321 avio_seek(pb, data_ofs, SEEK_SET);
323 if (!sample_count && st->codec->channels &&
324 av_get_bits_per_sample(st->codec->codec_id))
325 sample_count = (data_size << 3) /
326 (st->codec->channels *
327 (uint64_t)av_get_bits_per_sample(st->codec->codec_id));
329 st->duration = sample_count;
331 ff_metadata_conv_ctx(s, NULL, wav_metadata_conv);
332 ff_metadata_conv_ctx(s, NULL, ff_riff_info_conv);
338 * Find chunk with w64 GUID by skipping over other chunks.
339 * @return the size of the found chunk
341 static int64_t find_guid(AVIOContext *pb, const uint8_t guid1[16])
346 while (!pb->eof_reached) {
347 avio_read(pb, guid, 16);
348 size = avio_rl64(pb);
351 if (!memcmp(guid, guid1, 16))
353 avio_skip(pb, FFALIGN(size, INT64_C(8)) - 24);
358 static const uint8_t guid_data[16] = {
360 0xF3, 0xAC, 0xD3, 0x11,0x8C, 0xD1, 0x00, 0xC0, 0x4F, 0x8E, 0xDB, 0x8A
363 #define MAX_SIZE 4096
365 static int wav_read_packet(AVFormatContext *s, AVPacket *pkt)
370 WAVDemuxContext *wav = s->priv_data;
374 left = wav->data_end - avio_tell(s->pb);
376 if (CONFIG_W64_DEMUXER && wav->w64)
377 left = find_guid(s->pb, guid_data) - 24;
379 left = find_tag(s->pb, MKTAG('d', 'a', 't', 'a'));
382 wav->data_end = avio_tell(s->pb) + left;
386 if (st->codec->block_align > 1) {
387 if (size < st->codec->block_align)
388 size = st->codec->block_align;
389 size = (size / st->codec->block_align) * st->codec->block_align;
391 size = FFMIN(size, left);
392 ret = av_get_packet(s->pb, pkt, size);
395 pkt->stream_index = 0;
400 static int wav_read_seek(AVFormatContext *s,
401 int stream_index, int64_t timestamp, int flags)
406 switch (st->codec->codec_id) {
407 case AV_CODEC_ID_MP2:
408 case AV_CODEC_ID_MP3:
409 case AV_CODEC_ID_AC3:
410 case AV_CODEC_ID_DTS:
411 /* use generic seeking with dynamically generated indexes */
416 return ff_pcm_read_seek(s, stream_index, timestamp, flags);
419 AVInputFormat ff_wav_demuxer = {
421 .long_name = NULL_IF_CONFIG_SMALL("WAV / WAVE (Waveform Audio)"),
422 .priv_data_size = sizeof(WAVDemuxContext),
423 .read_probe = wav_probe,
424 .read_header = wav_read_header,
425 .read_packet = wav_read_packet,
426 .read_seek = wav_read_seek,
427 .flags = AVFMT_GENERIC_INDEX,
428 .codec_tag = (const AVCodecTag * const []) { ff_codec_wav_tags, 0 },
430 #endif /* CONFIG_WAV_DEMUXER */
432 #if CONFIG_W64_DEMUXER
433 static const uint8_t guid_riff[16] = {
435 0x2E, 0x91, 0xCF, 0x11,0xA5, 0xD6, 0x28, 0xDB, 0x04, 0xC1, 0x00, 0x00
438 static const uint8_t guid_wave[16] = {
440 0xF3, 0xAC, 0xD3, 0x11,0x8C, 0xD1, 0x00, 0xC0, 0x4F, 0x8E, 0xDB, 0x8A
443 static const uint8_t guid_fmt[16] = {
445 0xF3, 0xAC, 0xD3, 0x11,0x8C, 0xD1, 0x00, 0xC0, 0x4F, 0x8E, 0xDB, 0x8A
448 static int w64_probe(AVProbeData *p)
450 if (p->buf_size <= 40)
452 if (!memcmp(p->buf, guid_riff, 16) &&
453 !memcmp(p->buf + 24, guid_wave, 16))
454 return AVPROBE_SCORE_MAX;
459 static int w64_read_header(AVFormatContext *s)
462 AVIOContext *pb = s->pb;
463 WAVDemuxContext *wav = s->priv_data;
468 avio_read(pb, guid, 16);
469 if (memcmp(guid, guid_riff, 16))
472 /* riff + wave + fmt + sizes */
473 if (avio_rl64(pb) < 16 + 8 + 16 + 8 + 16 + 8)
476 avio_read(pb, guid, 16);
477 if (memcmp(guid, guid_wave, 16)) {
478 av_log(s, AV_LOG_ERROR, "could not find wave guid\n");
482 size = find_guid(pb, guid_fmt);
484 av_log(s, AV_LOG_ERROR, "could not find fmt guid\n");
488 st = avformat_new_stream(s, NULL);
490 return AVERROR(ENOMEM);
492 /* subtract chunk header size - normal wav file doesn't count it */
493 ret = ff_get_wav_header(pb, st->codec, size - 24);
496 avio_skip(pb, FFALIGN(size, INT64_C(8)) - size);
498 st->need_parsing = AVSTREAM_PARSE_FULL;
500 avpriv_set_pts_info(st, 64, 1, st->codec->sample_rate);
502 size = find_guid(pb, guid_data);
504 av_log(s, AV_LOG_ERROR, "could not find data guid\n");
507 wav->data_end = avio_tell(pb) + size - 24;
513 AVInputFormat ff_w64_demuxer = {
515 .long_name = NULL_IF_CONFIG_SMALL("Sony Wave64"),
516 .priv_data_size = sizeof(WAVDemuxContext),
517 .read_probe = w64_probe,
518 .read_header = w64_read_header,
519 .read_packet = wav_read_packet,
520 .read_seek = wav_read_seek,
521 .flags = AVFMT_GENERIC_INDEX,
522 .codec_tag = (const AVCodecTag * const []) { ff_codec_wav_tags, 0 },
524 #endif /* CONFIG_W64_DEMUXER */