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"
39 typedef struct WAVDemuxContext {
45 int smv_frames_per_jpeg;
54 #if CONFIG_WAV_DEMUXER
56 static int64_t next_tag(AVIOContext *pb, uint32_t *tag)
62 /* return the size of the found tag */
63 static int64_t find_tag(AVIOContext *pb, uint32_t tag1)
71 size = next_tag(pb, &tag);
79 static int wav_probe(AVProbeData *p)
81 /* check file header */
82 if (p->buf_size <= 32)
84 if (!memcmp(p->buf + 8, "WAVE", 4)) {
85 if (!memcmp(p->buf, "RIFF", 4))
87 Since ACT demuxer has standard WAV header at top of it's own,
88 returning score is decreased to avoid probe conflict
91 return AVPROBE_SCORE_MAX - 1;
92 else if (!memcmp(p->buf, "RF64", 4) &&
93 !memcmp(p->buf + 12, "ds64", 4))
94 return AVPROBE_SCORE_MAX;
99 static void handle_stream_probing(AVStream *st)
101 if (st->codec->codec_id == AV_CODEC_ID_PCM_S16LE) {
102 st->request_probe = AVPROBE_SCORE_MAX/2;
103 st->probe_packets = FFMIN(st->probe_packets, 4);
107 static int wav_parse_fmt_tag(AVFormatContext *s, int64_t size, AVStream **st)
109 AVIOContext *pb = s->pb;
112 /* parse fmt header */
113 *st = avformat_new_stream(s, NULL);
115 return AVERROR(ENOMEM);
117 ret = ff_get_wav_header(pb, (*st)->codec, size);
120 handle_stream_probing(*st);
122 (*st)->need_parsing = AVSTREAM_PARSE_FULL_RAW;
124 avpriv_set_pts_info(*st, 64, 1, (*st)->codec->sample_rate);
129 static inline int wav_parse_bext_string(AVFormatContext *s, const char *key,
135 av_assert0(length <= sizeof(temp));
136 if ((ret = avio_read(s->pb, temp, length)) < 0)
142 return av_dict_set(&s->metadata, key, temp, 0);
147 static int wav_parse_bext_tag(AVFormatContext *s, int64_t size)
149 char temp[131], *coding_history;
151 uint64_t time_reference;
152 int64_t umid_parts[8], umid_mask = 0;
154 if ((ret = wav_parse_bext_string(s, "description", 256)) < 0 ||
155 (ret = wav_parse_bext_string(s, "originator", 32)) < 0 ||
156 (ret = wav_parse_bext_string(s, "originator_reference", 32)) < 0 ||
157 (ret = wav_parse_bext_string(s, "origination_date", 10)) < 0 ||
158 (ret = wav_parse_bext_string(s, "origination_time", 8)) < 0)
161 time_reference = avio_rl64(s->pb);
162 snprintf(temp, sizeof(temp), "%"PRIu64, time_reference);
163 if ((ret = av_dict_set(&s->metadata, "time_reference", temp, 0)) < 0)
166 /* check if version is >= 1, in which case an UMID may be present */
167 if (avio_rl16(s->pb) >= 1) {
168 for (x = 0; x < 8; x++)
169 umid_mask |= umid_parts[x] = avio_rb64(s->pb);
172 /* the string formatting below is per SMPTE 330M-2004 Annex C */
173 if (umid_parts[4] == 0 && umid_parts[5] == 0 && umid_parts[6] == 0 && umid_parts[7] == 0) {
175 snprintf(temp, sizeof(temp), "0x%016"PRIX64"%016"PRIX64"%016"PRIX64"%016"PRIX64,
176 umid_parts[0], umid_parts[1], umid_parts[2], umid_parts[3]);
179 snprintf(temp, sizeof(temp), "0x%016"PRIX64"%016"PRIX64"%016"PRIX64"%016"PRIX64
180 "%016"PRIX64"%016"PRIX64"%016"PRIX64"%016"PRIX64,
181 umid_parts[0], umid_parts[1], umid_parts[2], umid_parts[3],
182 umid_parts[4], umid_parts[5], umid_parts[6], umid_parts[7]);
185 if ((ret = av_dict_set(&s->metadata, "umid", temp, 0)) < 0)
189 avio_skip(s->pb, 190);
191 avio_skip(s->pb, 254);
194 /* CodingHistory present */
197 if (!(coding_history = av_malloc(size+1)))
198 return AVERROR(ENOMEM);
200 if ((ret = avio_read(s->pb, coding_history, size)) < 0)
203 coding_history[size] = 0;
204 if ((ret = av_dict_set(&s->metadata, "coding_history", coding_history,
205 AV_DICT_DONT_STRDUP_VAL)) < 0)
212 static const AVMetadataConv wav_metadata_conv[] = {
213 {"description", "comment" },
214 {"originator", "encoded_by" },
215 {"origination_date", "date" },
216 {"origination_time", "creation_time"},
221 static int wav_read_header(AVFormatContext *s)
223 int64_t size, av_uninit(data_size);
224 int64_t sample_count=0;
227 AVIOContext *pb = s->pb;
229 WAVDemuxContext *wav = s->priv_data;
230 int ret, got_fmt = 0;
231 int64_t next_tag_ofs, data_ofs = -1;
233 wav->smv_data_ofs = -1;
235 /* check RIFF header */
238 rf64 = tag == MKTAG('R', 'F', '6', '4');
239 if (!rf64 && tag != MKTAG('R', 'I', 'F', 'F'))
241 avio_rl32(pb); /* file size */
243 if (tag != MKTAG('W', 'A', 'V', 'E'))
247 if (avio_rl32(pb) != MKTAG('d', 's', '6', '4'))
249 size = avio_rl32(pb);
252 avio_rl64(pb); /* RIFF size */
253 data_size = avio_rl64(pb);
254 sample_count = avio_rl64(pb);
255 if (data_size < 0 || sample_count < 0) {
256 av_log(s, AV_LOG_ERROR, "negative data_size and/or sample_count in "
257 "ds64: data_size = %"PRId64", sample_count = %"PRId64"\n",
258 data_size, sample_count);
259 return AVERROR_INVALIDDATA;
261 avio_skip(pb, size - 24); /* skip rest of ds64 chunk */
267 size = next_tag(pb, &tag);
268 next_tag_ofs = avio_tell(pb) + size;
274 case MKTAG('f', 'm', 't', ' '):
275 /* only parse the first 'fmt ' tag found */
276 if (!got_fmt && (ret = wav_parse_fmt_tag(s, size, &st)) < 0) {
279 av_log(s, AV_LOG_WARNING, "found more than one 'fmt ' tag\n");
283 case MKTAG('d', 'a', 't', 'a'):
285 av_log(s, AV_LOG_ERROR, "found no 'fmt ' tag before the 'data' tag\n");
286 return AVERROR_INVALIDDATA;
290 next_tag_ofs = wav->data_end = avio_tell(pb) + data_size;
293 next_tag_ofs = wav->data_end = size ? next_tag_ofs : INT64_MAX;
296 data_ofs = avio_tell(pb);
298 /* don't look for footer metadata if we can't seek or if we don't
299 * know where the data tag ends
301 if (!pb->seekable || (!rf64 && !size))
304 case MKTAG('f','a','c','t'):
306 sample_count = avio_rl32(pb);
308 case MKTAG('b','e','x','t'):
309 if ((ret = wav_parse_bext_tag(s, size)) < 0)
312 case MKTAG('S','M','V','0'):
314 av_log(s, AV_LOG_ERROR, "found no 'fmt ' tag before the 'SMV0' tag\n");
315 return AVERROR_INVALIDDATA;
317 // SMV file, a wav file with video appended.
318 if (size != MKTAG('0','2','0','0')) {
319 av_log(s, AV_LOG_ERROR, "Unknown SMV version found\n");
322 av_log(s, AV_LOG_DEBUG, "Found SMV data\n");
323 vst = avformat_new_stream(s, NULL);
325 return AVERROR(ENOMEM);
328 vst->codec->codec_type = AVMEDIA_TYPE_VIDEO;
329 vst->codec->codec_id = AV_CODEC_ID_MJPEG;
330 vst->codec->width = avio_rl24(pb);
331 vst->codec->height = avio_rl24(pb);
332 size = avio_rl24(pb);
333 wav->smv_data_ofs = avio_tell(pb) + (size - 5) * 3;
335 wav->smv_block_size = avio_rl24(pb);
336 avpriv_set_pts_info(vst, 32, 1, avio_rl24(pb));
337 vst->duration = avio_rl24(pb);
340 wav->smv_frames_per_jpeg = avio_rl24(pb);
342 case MKTAG('L', 'I', 'S', 'T'):
344 av_log(s, AV_LOG_ERROR, "too short LIST tag\n");
345 return AVERROR_INVALIDDATA;
347 switch (avio_rl32(pb)) {
348 case MKTAG('I', 'N', 'F', 'O'):
349 ff_read_riff_info(s, size - 4);
354 /* seek to next tag unless we know that we'll run into EOF */
355 if ((avio_size(pb) > 0 && next_tag_ofs >= avio_size(pb)) ||
356 avio_seek(pb, next_tag_ofs, SEEK_SET) < 0) {
362 av_log(s, AV_LOG_ERROR, "no 'data' tag found\n");
363 return AVERROR_INVALIDDATA;
366 avio_seek(pb, data_ofs, SEEK_SET);
368 if (!sample_count && st->codec->channels && av_get_bits_per_sample(st->codec->codec_id))
369 sample_count = (data_size<<3) / (st->codec->channels * (uint64_t)av_get_bits_per_sample(st->codec->codec_id));
371 st->duration = sample_count;
373 ff_metadata_conv_ctx(s, NULL, wav_metadata_conv);
374 ff_metadata_conv_ctx(s, NULL, ff_riff_info_conv);
379 /** Find chunk with w64 GUID by skipping over other chunks
380 * @return the size of the found chunk
382 static int64_t find_guid(AVIOContext *pb, const uint8_t guid1[16])
387 while (!url_feof(pb)) {
388 avio_read(pb, guid, 16);
389 size = avio_rl64(pb);
392 if (!memcmp(guid, guid1, 16))
394 avio_skip(pb, FFALIGN(size, INT64_C(8)) - 24);
399 static const uint8_t guid_data[16] = { 'd', 'a', 't', 'a',
400 0xF3, 0xAC, 0xD3, 0x11, 0x8C, 0xD1, 0x00, 0xC0, 0x4F, 0x8E, 0xDB, 0x8A };
402 #define MAX_SIZE 4096
404 static int wav_read_packet(AVFormatContext *s,
410 WAVDemuxContext *wav = s->priv_data;
412 if (wav->smv_data_ofs > 0) {
413 int64_t audio_dts, video_dts;
415 audio_dts = s->streams[0]->cur_dts;
416 video_dts = s->streams[1]->cur_dts;
417 if (audio_dts != AV_NOPTS_VALUE && video_dts != AV_NOPTS_VALUE) {
418 audio_dts = av_rescale_q(audio_dts, s->streams[0]->time_base, AV_TIME_BASE_Q);
419 video_dts = av_rescale_q(video_dts, s->streams[1]->time_base, AV_TIME_BASE_Q);
420 wav->smv_last_stream = video_dts >= audio_dts;
422 wav->smv_last_stream = !wav->smv_last_stream;
423 wav->smv_last_stream |= wav->audio_eof;
424 wav->smv_last_stream &= !wav->smv_eof;
425 if (wav->smv_last_stream) {
426 uint64_t old_pos = avio_tell(s->pb);
427 uint64_t new_pos = wav->smv_data_ofs +
428 wav->smv_block * wav->smv_block_size;
429 if (avio_seek(s->pb, new_pos, SEEK_SET) < 0) {
433 size = avio_rl24(s->pb);
434 ret = av_get_packet(s->pb, pkt, size);
438 pkt->pts = wav->smv_block * wav->smv_frames_per_jpeg;
440 pkt->stream_index = 1;
442 avio_seek(s->pb, old_pos, SEEK_SET);
443 if (ret == AVERROR_EOF) {
453 left = wav->data_end - avio_tell(s->pb);
454 if (wav->ignore_length)
457 if (CONFIG_W64_DEMUXER && wav->w64)
458 left = find_guid(s->pb, guid_data) - 24;
460 left = find_tag(s->pb, MKTAG('d', 'a', 't', 'a'));
463 if (wav->smv_data_ofs > 0 && !wav->smv_eof)
467 wav->data_end= avio_tell(s->pb) + left;
471 if (st->codec->block_align > 1) {
472 if (size < st->codec->block_align)
473 size = st->codec->block_align;
474 size = (size / st->codec->block_align) * st->codec->block_align;
476 size = FFMIN(size, left);
477 ret = av_get_packet(s->pb, pkt, size);
480 pkt->stream_index = 0;
485 static int wav_read_seek(AVFormatContext *s,
486 int stream_index, int64_t timestamp, int flags)
488 WAVDemuxContext *wav = s->priv_data;
492 if (wav->smv_data_ofs > 0) {
493 int64_t smv_timestamp = timestamp;
494 if (stream_index == 0)
495 smv_timestamp = av_rescale_q(timestamp, s->streams[0]->time_base, s->streams[1]->time_base);
497 timestamp = av_rescale_q(smv_timestamp, s->streams[1]->time_base, s->streams[0]->time_base);
498 wav->smv_block = smv_timestamp / wav->smv_frames_per_jpeg;
502 switch (st->codec->codec_id) {
503 case AV_CODEC_ID_MP2:
504 case AV_CODEC_ID_MP3:
505 case AV_CODEC_ID_AC3:
506 case AV_CODEC_ID_DTS:
507 /* use generic seeking with dynamically generated indexes */
512 return ff_pcm_read_seek(s, stream_index, timestamp, flags);
515 #define OFFSET(x) offsetof(WAVDemuxContext, x)
516 #define DEC AV_OPT_FLAG_DECODING_PARAM
517 static const AVOption demux_options[] = {
518 { "ignore_length", "Ignore length", OFFSET(ignore_length), AV_OPT_TYPE_INT, { .i64 = 0 }, 0, 1, DEC },
522 static const AVClass wav_demuxer_class = {
523 .class_name = "WAV demuxer",
524 .item_name = av_default_item_name,
525 .option = demux_options,
526 .version = LIBAVUTIL_VERSION_INT,
528 AVInputFormat ff_wav_demuxer = {
530 .long_name = NULL_IF_CONFIG_SMALL("WAV / WAVE (Waveform Audio)"),
531 .priv_data_size = sizeof(WAVDemuxContext),
532 .read_probe = wav_probe,
533 .read_header = wav_read_header,
534 .read_packet = wav_read_packet,
535 .read_seek = wav_read_seek,
536 .flags = AVFMT_GENERIC_INDEX,
537 .codec_tag = (const AVCodecTag* const []){ ff_codec_wav_tags, 0 },
538 .priv_class = &wav_demuxer_class,
540 #endif /* CONFIG_WAV_DEMUXER */
543 #if CONFIG_W64_DEMUXER
544 static const uint8_t guid_riff[16] = { 'r', 'i', 'f', 'f',
545 0x2E, 0x91, 0xCF, 0x11, 0xA5, 0xD6, 0x28, 0xDB, 0x04, 0xC1, 0x00, 0x00 };
547 static const uint8_t guid_wave[16] = { 'w', 'a', 'v', 'e',
548 0xF3, 0xAC, 0xD3, 0x11, 0x8C, 0xD1, 0x00, 0xC0, 0x4F, 0x8E, 0xDB, 0x8A };
550 static const uint8_t guid_fmt [16] = { 'f', 'm', 't', ' ',
551 0xF3, 0xAC, 0xD3, 0x11, 0x8C, 0xD1, 0x00, 0xC0, 0x4F, 0x8E, 0xDB, 0x8A };
553 static int w64_probe(AVProbeData *p)
555 if (p->buf_size <= 40)
557 if (!memcmp(p->buf, guid_riff, 16) &&
558 !memcmp(p->buf + 24, guid_wave, 16))
559 return AVPROBE_SCORE_MAX;
564 static int w64_read_header(AVFormatContext *s)
567 AVIOContext *pb = s->pb;
568 WAVDemuxContext *wav = s->priv_data;
573 avio_read(pb, guid, 16);
574 if (memcmp(guid, guid_riff, 16))
577 if (avio_rl64(pb) < 16 + 8 + 16 + 8 + 16 + 8) /* riff + wave + fmt + sizes */
580 avio_read(pb, guid, 16);
581 if (memcmp(guid, guid_wave, 16)) {
582 av_log(s, AV_LOG_ERROR, "could not find wave guid\n");
586 size = find_guid(pb, guid_fmt);
588 av_log(s, AV_LOG_ERROR, "could not find fmt guid\n");
592 st = avformat_new_stream(s, NULL);
594 return AVERROR(ENOMEM);
596 /* subtract chunk header size - normal wav file doesn't count it */
597 ret = ff_get_wav_header(pb, st->codec, size - 24);
600 avio_skip(pb, FFALIGN(size, INT64_C(8)) - size);
602 handle_stream_probing(st);
603 st->need_parsing = AVSTREAM_PARSE_FULL_RAW;
605 avpriv_set_pts_info(st, 64, 1, st->codec->sample_rate);
607 size = find_guid(pb, guid_data);
609 av_log(s, AV_LOG_ERROR, "could not find data guid\n");
612 wav->data_end = avio_tell(pb) + size - 24;
618 AVInputFormat ff_w64_demuxer = {
620 .long_name = NULL_IF_CONFIG_SMALL("Sony Wave64"),
621 .priv_data_size = sizeof(WAVDemuxContext),
622 .read_probe = w64_probe,
623 .read_header = w64_read_header,
624 .read_packet = wav_read_packet,
625 .read_seek = wav_read_seek,
626 .flags = AVFMT_GENERIC_INDEX,
627 .codec_tag = (const AVCodecTag* const []){ ff_codec_wav_tags, 0 },
629 #endif /* CONFIG_W64_DEMUXER */