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 "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"
40 typedef struct WAVDemuxContext {
46 int smv_frames_per_jpeg;
55 #if CONFIG_WAV_DEMUXER
57 static int64_t next_tag(AVIOContext *pb, uint32_t *tag)
63 /* return the size of the found tag */
64 static int64_t find_tag(AVIOContext *pb, uint32_t tag1)
72 size = next_tag(pb, &tag);
80 static int wav_probe(AVProbeData *p)
82 /* check file header */
83 if (p->buf_size <= 32)
85 if (!memcmp(p->buf + 8, "WAVE", 4)) {
86 if (!memcmp(p->buf, "RIFF", 4))
88 Since ACT demuxer has standard WAV header at top of it's own,
89 returning score is decreased to avoid probe conflict
92 return AVPROBE_SCORE_MAX - 1;
93 else if (!memcmp(p->buf, "RF64", 4) &&
94 !memcmp(p->buf + 12, "ds64", 4))
95 return AVPROBE_SCORE_MAX;
100 static void handle_stream_probing(AVStream *st)
102 if (st->codec->codec_id == AV_CODEC_ID_PCM_S16LE) {
103 st->request_probe = AVPROBE_SCORE_MAX/2;
104 st->probe_packets = FFMIN(st->probe_packets, 4);
108 static int wav_parse_fmt_tag(AVFormatContext *s, int64_t size, AVStream **st)
110 AVIOContext *pb = s->pb;
113 /* parse fmt header */
114 *st = avformat_new_stream(s, NULL);
116 return AVERROR(ENOMEM);
118 ret = ff_get_wav_header(pb, (*st)->codec, size);
121 handle_stream_probing(*st);
123 (*st)->need_parsing = AVSTREAM_PARSE_FULL_RAW;
125 avpriv_set_pts_info(*st, 64, 1, (*st)->codec->sample_rate);
130 static inline int wav_parse_bext_string(AVFormatContext *s, const char *key,
136 av_assert0(length <= sizeof(temp));
137 if ((ret = avio_read(s->pb, temp, length)) < 0)
143 return av_dict_set(&s->metadata, key, temp, 0);
148 static int wav_parse_bext_tag(AVFormatContext *s, int64_t size)
150 char temp[131], *coding_history;
152 uint64_t time_reference;
153 int64_t umid_parts[8], umid_mask = 0;
155 if ((ret = wav_parse_bext_string(s, "description", 256)) < 0 ||
156 (ret = wav_parse_bext_string(s, "originator", 32)) < 0 ||
157 (ret = wav_parse_bext_string(s, "originator_reference", 32)) < 0 ||
158 (ret = wav_parse_bext_string(s, "origination_date", 10)) < 0 ||
159 (ret = wav_parse_bext_string(s, "origination_time", 8)) < 0)
162 time_reference = avio_rl64(s->pb);
163 snprintf(temp, sizeof(temp), "%"PRIu64, time_reference);
164 if ((ret = av_dict_set(&s->metadata, "time_reference", temp, 0)) < 0)
167 /* check if version is >= 1, in which case an UMID may be present */
168 if (avio_rl16(s->pb) >= 1) {
169 for (x = 0; x < 8; x++)
170 umid_mask |= umid_parts[x] = avio_rb64(s->pb);
173 /* the string formatting below is per SMPTE 330M-2004 Annex C */
174 if (umid_parts[4] == 0 && umid_parts[5] == 0 && umid_parts[6] == 0 && umid_parts[7] == 0) {
176 snprintf(temp, sizeof(temp), "0x%016"PRIX64"%016"PRIX64"%016"PRIX64"%016"PRIX64,
177 umid_parts[0], umid_parts[1], umid_parts[2], umid_parts[3]);
180 snprintf(temp, sizeof(temp), "0x%016"PRIX64"%016"PRIX64"%016"PRIX64"%016"PRIX64
181 "%016"PRIX64"%016"PRIX64"%016"PRIX64"%016"PRIX64,
182 umid_parts[0], umid_parts[1], umid_parts[2], umid_parts[3],
183 umid_parts[4], umid_parts[5], umid_parts[6], umid_parts[7]);
186 if ((ret = av_dict_set(&s->metadata, "umid", temp, 0)) < 0)
190 avio_skip(s->pb, 190);
192 avio_skip(s->pb, 254);
195 /* CodingHistory present */
198 if (!(coding_history = av_malloc(size+1)))
199 return AVERROR(ENOMEM);
201 if ((ret = avio_read(s->pb, coding_history, size)) < 0)
204 coding_history[size] = 0;
205 if ((ret = av_dict_set(&s->metadata, "coding_history", coding_history,
206 AV_DICT_DONT_STRDUP_VAL)) < 0)
213 static const AVMetadataConv wav_metadata_conv[] = {
214 {"description", "comment" },
215 {"originator", "encoded_by" },
216 {"origination_date", "date" },
217 {"origination_time", "creation_time"},
222 static int wav_read_header(AVFormatContext *s)
224 int64_t size, av_uninit(data_size);
225 int64_t sample_count=0;
228 AVIOContext *pb = s->pb;
230 WAVDemuxContext *wav = s->priv_data;
231 int ret, got_fmt = 0;
232 int64_t next_tag_ofs, data_ofs = -1;
234 wav->smv_data_ofs = -1;
236 /* check RIFF header */
239 rf64 = tag == MKTAG('R', 'F', '6', '4');
240 if (!rf64 && tag != MKTAG('R', 'I', 'F', 'F'))
242 avio_rl32(pb); /* file size */
244 if (tag != MKTAG('W', 'A', 'V', 'E'))
248 if (avio_rl32(pb) != MKTAG('d', 's', '6', '4'))
250 size = avio_rl32(pb);
253 avio_rl64(pb); /* RIFF size */
254 data_size = avio_rl64(pb);
255 sample_count = avio_rl64(pb);
256 if (data_size < 0 || sample_count < 0) {
257 av_log(s, AV_LOG_ERROR, "negative data_size and/or sample_count in "
258 "ds64: data_size = %"PRId64", sample_count = %"PRId64"\n",
259 data_size, sample_count);
260 return AVERROR_INVALIDDATA;
262 avio_skip(pb, size - 24); /* skip rest of ds64 chunk */
268 size = next_tag(pb, &tag);
269 next_tag_ofs = avio_tell(pb) + size;
275 case MKTAG('f', 'm', 't', ' '):
276 /* only parse the first 'fmt ' tag found */
277 if (!got_fmt && (ret = wav_parse_fmt_tag(s, size, &st)) < 0) {
280 av_log(s, AV_LOG_WARNING, "found more than one 'fmt ' tag\n");
284 case MKTAG('d', 'a', 't', 'a'):
286 av_log(s, AV_LOG_ERROR, "found no 'fmt ' tag before the 'data' tag\n");
287 return AVERROR_INVALIDDATA;
291 next_tag_ofs = wav->data_end = avio_tell(pb) + data_size;
294 next_tag_ofs = wav->data_end = size ? next_tag_ofs : INT64_MAX;
297 data_ofs = avio_tell(pb);
299 /* don't look for footer metadata if we can't seek or if we don't
300 * know where the data tag ends
302 if (!pb->seekable || (!rf64 && !size))
305 case MKTAG('f','a','c','t'):
307 sample_count = avio_rl32(pb);
309 case MKTAG('b','e','x','t'):
310 if ((ret = wav_parse_bext_tag(s, size)) < 0)
313 case MKTAG('S','M','V','0'):
315 av_log(s, AV_LOG_ERROR, "found no 'fmt ' tag before the 'SMV0' tag\n");
316 return AVERROR_INVALIDDATA;
318 // SMV file, a wav file with video appended.
319 if (size != MKTAG('0','2','0','0')) {
320 av_log(s, AV_LOG_ERROR, "Unknown SMV version found\n");
323 av_log(s, AV_LOG_DEBUG, "Found SMV data\n");
324 vst = avformat_new_stream(s, NULL);
326 return AVERROR(ENOMEM);
329 vst->codec->codec_type = AVMEDIA_TYPE_VIDEO;
330 vst->codec->codec_id = AV_CODEC_ID_MJPEG;
331 vst->codec->width = avio_rl24(pb);
332 vst->codec->height = avio_rl24(pb);
333 size = avio_rl24(pb);
334 wav->smv_data_ofs = avio_tell(pb) + (size - 5) * 3;
336 wav->smv_block_size = avio_rl24(pb);
337 avpriv_set_pts_info(vst, 32, 1, avio_rl24(pb));
338 vst->duration = avio_rl24(pb);
341 wav->smv_frames_per_jpeg = avio_rl24(pb);
343 case MKTAG('L', 'I', 'S', 'T'):
345 av_log(s, AV_LOG_ERROR, "too short LIST tag\n");
346 return AVERROR_INVALIDDATA;
348 switch (avio_rl32(pb)) {
349 case MKTAG('I', 'N', 'F', 'O'):
350 ff_read_riff_info(s, size - 4);
355 /* seek to next tag unless we know that we'll run into EOF */
356 if ((avio_size(pb) > 0 && next_tag_ofs >= avio_size(pb)) ||
357 avio_seek(pb, next_tag_ofs, SEEK_SET) < 0) {
363 av_log(s, AV_LOG_ERROR, "no 'data' tag found\n");
364 return AVERROR_INVALIDDATA;
367 avio_seek(pb, data_ofs, SEEK_SET);
369 if (!sample_count && st->codec->channels && av_get_bits_per_sample(st->codec->codec_id) && wav->data_end <= avio_size(pb))
370 sample_count = (data_size<<3) / (st->codec->channels * (uint64_t)av_get_bits_per_sample(st->codec->codec_id));
372 st->duration = sample_count;
374 ff_metadata_conv_ctx(s, NULL, wav_metadata_conv);
375 ff_metadata_conv_ctx(s, NULL, ff_riff_info_conv);
380 /** Find chunk with w64 GUID by skipping over other chunks
381 * @return the size of the found chunk
383 static int64_t find_guid(AVIOContext *pb, const uint8_t guid1[16])
388 while (!url_feof(pb)) {
389 avio_read(pb, guid, 16);
390 size = avio_rl64(pb);
393 if (!memcmp(guid, guid1, 16))
395 avio_skip(pb, FFALIGN(size, INT64_C(8)) - 24);
400 #define MAX_SIZE 4096
402 static int wav_read_packet(AVFormatContext *s,
408 WAVDemuxContext *wav = s->priv_data;
410 if (wav->smv_data_ofs > 0) {
411 int64_t audio_dts, video_dts;
413 audio_dts = s->streams[0]->cur_dts;
414 video_dts = s->streams[1]->cur_dts;
415 if (audio_dts != AV_NOPTS_VALUE && video_dts != AV_NOPTS_VALUE) {
416 audio_dts = av_rescale_q(audio_dts, s->streams[0]->time_base, AV_TIME_BASE_Q);
417 video_dts = av_rescale_q(video_dts, s->streams[1]->time_base, AV_TIME_BASE_Q);
418 wav->smv_last_stream = video_dts >= audio_dts;
420 wav->smv_last_stream = !wav->smv_last_stream;
421 wav->smv_last_stream |= wav->audio_eof;
422 wav->smv_last_stream &= !wav->smv_eof;
423 if (wav->smv_last_stream) {
424 uint64_t old_pos = avio_tell(s->pb);
425 uint64_t new_pos = wav->smv_data_ofs +
426 wav->smv_block * wav->smv_block_size;
427 if (avio_seek(s->pb, new_pos, SEEK_SET) < 0) {
431 size = avio_rl24(s->pb);
432 ret = av_get_packet(s->pb, pkt, size);
436 pkt->pts = wav->smv_block * wav->smv_frames_per_jpeg;
438 pkt->stream_index = 1;
440 avio_seek(s->pb, old_pos, SEEK_SET);
441 if (ret == AVERROR_EOF) {
451 left = wav->data_end - avio_tell(s->pb);
452 if (wav->ignore_length)
455 if (CONFIG_W64_DEMUXER && wav->w64)
456 left = find_guid(s->pb, ff_w64_guid_data) - 24;
458 left = find_tag(s->pb, MKTAG('d', 'a', 't', 'a'));
461 if (wav->smv_data_ofs > 0 && !wav->smv_eof)
465 wav->data_end= avio_tell(s->pb) + left;
469 if (st->codec->block_align > 1) {
470 if (size < st->codec->block_align)
471 size = st->codec->block_align;
472 size = (size / st->codec->block_align) * st->codec->block_align;
474 size = FFMIN(size, left);
475 ret = av_get_packet(s->pb, pkt, size);
478 pkt->stream_index = 0;
483 static int wav_read_seek(AVFormatContext *s,
484 int stream_index, int64_t timestamp, int flags)
486 WAVDemuxContext *wav = s->priv_data;
490 if (wav->smv_data_ofs > 0) {
491 int64_t smv_timestamp = timestamp;
492 if (stream_index == 0)
493 smv_timestamp = av_rescale_q(timestamp, s->streams[0]->time_base, s->streams[1]->time_base);
495 timestamp = av_rescale_q(smv_timestamp, s->streams[1]->time_base, s->streams[0]->time_base);
496 wav->smv_block = smv_timestamp / wav->smv_frames_per_jpeg;
500 switch (st->codec->codec_id) {
501 case AV_CODEC_ID_MP2:
502 case AV_CODEC_ID_MP3:
503 case AV_CODEC_ID_AC3:
504 case AV_CODEC_ID_DTS:
505 /* use generic seeking with dynamically generated indexes */
510 return ff_pcm_read_seek(s, stream_index, timestamp, flags);
513 #define OFFSET(x) offsetof(WAVDemuxContext, x)
514 #define DEC AV_OPT_FLAG_DECODING_PARAM
515 static const AVOption demux_options[] = {
516 { "ignore_length", "Ignore length", OFFSET(ignore_length), AV_OPT_TYPE_INT, { .i64 = 0 }, 0, 1, DEC },
520 static const AVClass wav_demuxer_class = {
521 .class_name = "WAV demuxer",
522 .item_name = av_default_item_name,
523 .option = demux_options,
524 .version = LIBAVUTIL_VERSION_INT,
526 AVInputFormat ff_wav_demuxer = {
528 .long_name = NULL_IF_CONFIG_SMALL("WAV / WAVE (Waveform Audio)"),
529 .priv_data_size = sizeof(WAVDemuxContext),
530 .read_probe = wav_probe,
531 .read_header = wav_read_header,
532 .read_packet = wav_read_packet,
533 .read_seek = wav_read_seek,
534 .flags = AVFMT_GENERIC_INDEX,
535 .codec_tag = (const AVCodecTag* const []){ ff_codec_wav_tags, 0 },
536 .priv_class = &wav_demuxer_class,
538 #endif /* CONFIG_WAV_DEMUXER */
541 #if CONFIG_W64_DEMUXER
542 static int w64_probe(AVProbeData *p)
544 if (p->buf_size <= 40)
546 if (!memcmp(p->buf, ff_w64_guid_riff, 16) &&
547 !memcmp(p->buf + 24, ff_w64_guid_wave, 16))
548 return AVPROBE_SCORE_MAX;
553 static int w64_read_header(AVFormatContext *s)
555 int64_t size, data_ofs = 0;
556 AVIOContext *pb = s->pb;
557 WAVDemuxContext *wav = s->priv_data;
562 avio_read(pb, guid, 16);
563 if (memcmp(guid, ff_w64_guid_riff, 16))
566 if (avio_rl64(pb) < 16 + 8 + 16 + 8 + 16 + 8) /* riff + wave + fmt + sizes */
569 avio_read(pb, guid, 16);
570 if (memcmp(guid, ff_w64_guid_wave, 16)) {
571 av_log(s, AV_LOG_ERROR, "could not find wave guid\n");
577 st = avformat_new_stream(s, NULL);
579 return AVERROR(ENOMEM);
581 while (!url_feof(pb)) {
582 if (avio_read(pb, guid, 16) != 16)
584 size = avio_rl64(pb);
585 if (size <= 24 || INT64_MAX - size < avio_tell(pb))
586 return AVERROR_INVALIDDATA;
588 if (!memcmp(guid, ff_w64_guid_fmt, 16)) {
589 /* subtract chunk header size - normal wav file doesn't count it */
590 ret = ff_get_wav_header(pb, st->codec, size - 24);
593 avio_skip(pb, FFALIGN(size, INT64_C(8)) - size);
595 avpriv_set_pts_info(st, 64, 1, st->codec->sample_rate);
596 } else if (!memcmp(guid, ff_w64_guid_data, 16)) {
597 wav->data_end = avio_tell(pb) + size - 24;
599 data_ofs = avio_tell(pb);
603 avio_skip(pb, size - 24);
604 } else if (!memcmp(guid, ff_w64_guid_summarylist, 16)) {
605 int64_t start, end, cur;
606 uint32_t count, chunk_size, i;
608 start = avio_tell(pb);
610 count = avio_rl32(pb);
612 for (i = 0; i < count; i++) {
613 char chunk_key[5], *value;
615 if (url_feof(pb) || (cur = avio_tell(pb)) < 0 || cur > end - 8 /* = tag + size */)
619 avio_read(pb, chunk_key, 4);
620 chunk_size = avio_rl32(pb);
622 value = av_mallocz(chunk_size + 1);
624 return AVERROR(ENOMEM);
626 ret = avio_get_str16le(pb, chunk_size, value, chunk_size);
627 avio_skip(pb, chunk_size - ret);
629 av_dict_set(&s->metadata, chunk_key, value, AV_DICT_DONT_STRDUP_VAL);
632 avio_skip(pb, end - avio_tell(pb));
634 av_log(s, AV_LOG_DEBUG, "unknown guid: "FF_PRI_GUID"\n", FF_ARG_GUID(guid));
635 avio_skip(pb, size - 24);
642 ff_metadata_conv_ctx(s, NULL, wav_metadata_conv);
643 ff_metadata_conv_ctx(s, NULL, ff_riff_info_conv);
645 handle_stream_probing(st);
646 st->need_parsing = AVSTREAM_PARSE_FULL_RAW;
648 avio_seek(pb, data_ofs, SEEK_SET);
653 AVInputFormat ff_w64_demuxer = {
655 .long_name = NULL_IF_CONFIG_SMALL("Sony Wave64"),
656 .priv_data_size = sizeof(WAVDemuxContext),
657 .read_probe = w64_probe,
658 .read_header = w64_read_header,
659 .read_packet = wav_read_packet,
660 .read_seek = wav_read_seek,
661 .flags = AVFMT_GENERIC_INDEX,
662 .codec_tag = (const AVCodecTag* const []){ ff_codec_wav_tags, 0 },
664 #endif /* CONFIG_W64_DEMUXER */