From a4ad9438600ea20f4ef459f3f5f397755d48c58b Mon Sep 17 00:00:00 2001 From: Benjamin Larsson Date: Fri, 18 Mar 2011 00:00:15 +0000 Subject: wav: add an option for writing BEXT chunk Signed-off-by: Anton Khirnov --- libavformat/wav.c | 78 +++++++++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 78 insertions(+) (limited to 'libavformat/wav.c') diff --git a/libavformat/wav.c b/libavformat/wav.c index 391461044b..c9086196ca 100644 --- a/libavformat/wav.c +++ b/libavformat/wav.c @@ -23,22 +23,82 @@ * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA */ +#include "libavutil/log.h" #include "libavutil/mathematics.h" +#include "libavutil/opt.h" #include "avformat.h" #include "avio_internal.h" #include "pcm.h" #include "riff.h" +#include "avio.h" +#include "avio_internal.h" typedef struct { + const AVClass *class; int64_t data; int64_t data_end; int64_t minpts; int64_t maxpts; int last_duration; int w64; + int write_bext; } WAVContext; #if CONFIG_WAV_MUXER +static inline void bwf_write_bext_string(AVFormatContext *s, const char *key, int maxlen) +{ + AVDictionaryEntry *tag; + int len = 0; + + if (tag = av_dict_get(s->metadata, key, NULL, 0)) { + len = strlen(tag->value); + len = FFMIN(len, maxlen); + avio_write(s->pb, tag->value, len); + } + + ffio_fill(s->pb, 0, maxlen - len); +} + +static void bwf_write_bext_chunk(AVFormatContext *s) +{ + AVDictionaryEntry *tmp_tag; + uint64_t time_reference = 0; + int64_t bext = ff_start_tag(s->pb, "bext"); + + bwf_write_bext_string(s, "description", 256); + bwf_write_bext_string(s, "originator", 32); + bwf_write_bext_string(s, "originator_reference", 32); + bwf_write_bext_string(s, "origination_date", 10); + bwf_write_bext_string(s, "origination_time", 8); + + if (tmp_tag = av_dict_get(s->metadata, "time_reference", NULL, 0)) + time_reference = strtoll(tmp_tag->value, NULL, 10); + avio_wl64(s->pb, time_reference); + avio_wl16(s->pb, 1); // set version to 1 + + if (tmp_tag = av_dict_get(s->metadata, "umid", NULL, 0)) { + unsigned char umidpart_str[17] = {0}; + int i; + uint64_t umidpart; + int len = strlen(tmp_tag->value+2); + + for (i = 0; i < len/16; i++) { + memcpy(umidpart_str, tmp_tag->value + 2 + (i*16), 16); + umidpart = strtoll(umidpart_str, NULL, 16); + avio_wb64(s->pb, umidpart); + } + ffio_fill(s->pb, 0, 64 - i*8); + } else + ffio_fill(s->pb, 0, 64); // zero UMID + + ffio_fill(s->pb, 0, 190); // Reserved + + if (tmp_tag = av_dict_get(s->metadata, "coding_history", NULL, 0)) + avio_put_str(s->pb, tmp_tag->value); + + ff_end_tag(s->pb, bext); +} + static int wav_write_header(AVFormatContext *s) { WAVContext *wav = s->priv_data; @@ -65,6 +125,9 @@ static int wav_write_header(AVFormatContext *s) ff_end_tag(pb, fact); } + if (wav->write_bext) + bwf_write_bext_chunk(s); + av_set_pts_info(s->streams[0], 64, 1, s->streams[0]->codec->sample_rate); wav->maxpts = wav->last_duration = 0; wav->minpts = INT64_MAX; @@ -125,6 +188,20 @@ static int wav_write_trailer(AVFormatContext *s) return 0; } +#define OFFSET(x) offsetof(WAVContext, x) +#define ENC AV_OPT_FLAG_ENCODING_PARAM +static const AVOption options[] = { + { "write_bext", "Write BEXT chunk.", OFFSET(write_bext), FF_OPT_TYPE_INT, { 0 }, 0, 1, ENC }, + { NULL }, +}; + +static const AVClass wav_muxer_class = { + .class_name = "WAV muxer", + .item_name = av_default_item_name, + .option = options, + .version = LIBAVUTIL_VERSION_INT, +}; + AVOutputFormat ff_wav_muxer = { "wav", NULL_IF_CONFIG_SMALL("WAV format"), @@ -137,6 +214,7 @@ AVOutputFormat ff_wav_muxer = { wav_write_packet, wav_write_trailer, .codec_tag= (const AVCodecTag* const []){ff_codec_wav_tags, 0}, + .priv_class = &wav_muxer_class, }; #endif /* CONFIG_WAV_MUXER */ -- cgit v1.2.3 From 7f84055e2d3814972a61515da7f1d2659b185ea0 Mon Sep 17 00:00:00 2001 From: Tomas Härdin Date: Tue, 17 May 2011 19:46:08 +0200 Subject: wav: refactor the 'fmt ' tag search and parsing. Moving the search and parsing of the 'fmt ' info the main loop of wav_read_header() allows tags that precede it to be parsed. Creating wav_parse_fmt_tag() makes wav_read_header() easier to read. Signed-off-by: Anton Khirnov --- libavformat/wav.c | 57 +++++++++++++++++++++++++++++++++++++------------------ 1 file changed, 39 insertions(+), 18 deletions(-) (limited to 'libavformat/wav.c') diff --git a/libavformat/wav.c b/libavformat/wav.c index c9086196ca..3e4e0c4ea4 100644 --- a/libavformat/wav.c +++ b/libavformat/wav.c @@ -264,6 +264,26 @@ static int wav_probe(AVProbeData *p) return 0; } +static int wav_parse_fmt_tag(AVFormatContext *s, int64_t size, AVStream **st) +{ + AVIOContext *pb = s->pb; + int ret; + + /* parse fmt header */ + *st = av_new_stream(s, 0); + if (!*st) + return AVERROR(ENOMEM); + + ret = ff_get_wav_header(pb, (*st)->codec, size); + if (ret < 0) + return ret; + (*st)->need_parsing = AVSTREAM_PARSE_FULL; + + av_set_pts_info(*st, 64, 1, (*st)->codec->sample_rate); + + return 0; +} + /* wav input */ static int wav_read_header(AVFormatContext *s, AVFormatParameters *ap) @@ -275,7 +295,8 @@ static int wav_read_header(AVFormatContext *s, AVIOContext *pb = s->pb; AVStream *st; WAVContext *wav = s->priv_data; - int ret; + int ret, got_fmt = 0; + int64_t next_tag_ofs; /* check RIFF header */ tag = avio_rl32(pb); @@ -300,32 +321,32 @@ static int wav_read_header(AVFormatContext *s, avio_skip(pb, size - 16); /* skip rest of ds64 chunk */ } - /* parse fmt header */ - size = find_tag(pb, MKTAG('f', 'm', 't', ' ')); - if (size < 0) - return -1; - st = av_new_stream(s, 0); - if (!st) - return AVERROR(ENOMEM); - - ret = ff_get_wav_header(pb, st->codec, size); - if (ret < 0) - return ret; - st->need_parsing = AVSTREAM_PARSE_FULL; - - av_set_pts_info(st, 64, 1, st->codec->sample_rate); for (;;) { if (pb->eof_reached) return -1; size = next_tag(pb, &tag); - if (tag == MKTAG('d', 'a', 't', 'a')){ + next_tag_ofs = avio_tell(pb) + size; + + if (tag == MKTAG('f', 'm', 't', ' ')) { + /* only parse the first 'fmt ' tag found */ + if (!got_fmt && (ret = wav_parse_fmt_tag(s, size, &st) < 0)) { + return ret; + } else if (got_fmt) + av_log(s, AV_LOG_WARNING, "found more than one 'fmt ' tag\n"); + + got_fmt = 1; + } else if (tag == MKTAG('d', 'a', 't', 'a')) { + if (!got_fmt) { + av_log(s, AV_LOG_ERROR, "found no 'fmt ' tag before the 'data' tag\n"); + return AVERROR_INVALIDDATA; + } + break; }else if (tag == MKTAG('f','a','c','t') && !sample_count){ sample_count = avio_rl32(pb); - size -= 4; } - avio_skip(pb, size); + avio_seek(pb, next_tag_ofs, SEEK_SET); } if (rf64) size = data_size; -- cgit v1.2.3 From 1cf18de982a0a62fa9214a6c4a5becc1a32d9caf Mon Sep 17 00:00:00 2001 From: Tomas Härdin Date: Tue, 17 May 2011 19:52:36 +0200 Subject: wav: make sure neither data_size nor sample_count is negative. Signed-off-by: Anton Khirnov --- libavformat/wav.c | 6 ++++++ 1 file changed, 6 insertions(+) (limited to 'libavformat/wav.c') diff --git a/libavformat/wav.c b/libavformat/wav.c index 3e4e0c4ea4..9f902be818 100644 --- a/libavformat/wav.c +++ b/libavformat/wav.c @@ -318,6 +318,12 @@ static int wav_read_header(AVFormatContext *s, avio_rl64(pb); /* RIFF size */ data_size = avio_rl64(pb); sample_count = avio_rl64(pb); + if (data_size < 0 || sample_count < 0) { + av_log(s, AV_LOG_ERROR, "negative data_size and/or sample_count in " + "ds64: data_size = %"PRId64", sample_count = %"PRId64"\n", + data_size, sample_count); + return AVERROR_INVALIDDATA; + } avio_skip(pb, size - 16); /* skip rest of ds64 chunk */ } -- cgit v1.2.3 From 90f2ee8cb4d2b135dd33128a94ad6cf126821472 Mon Sep 17 00:00:00 2001 From: Tomas Härdin Date: Thu, 17 Feb 2011 15:58:10 +0100 Subject: wav: Refactor the tag checking into a switch statement Signed-off-by: Anton Khirnov --- libavformat/wav.c | 13 +++++++++---- 1 file changed, 9 insertions(+), 4 deletions(-) (limited to 'libavformat/wav.c') diff --git a/libavformat/wav.c b/libavformat/wav.c index 9f902be818..cf815ef3df 100644 --- a/libavformat/wav.c +++ b/libavformat/wav.c @@ -334,7 +334,8 @@ static int wav_read_header(AVFormatContext *s, size = next_tag(pb, &tag); next_tag_ofs = avio_tell(pb) + size; - if (tag == MKTAG('f', 'm', 't', ' ')) { + switch (tag) { + case MKTAG('f', 'm', 't', ' '): /* only parse the first 'fmt ' tag found */ if (!got_fmt && (ret = wav_parse_fmt_tag(s, size, &st) < 0)) { return ret; @@ -342,18 +343,22 @@ static int wav_read_header(AVFormatContext *s, av_log(s, AV_LOG_WARNING, "found more than one 'fmt ' tag\n"); got_fmt = 1; - } else if (tag == MKTAG('d', 'a', 't', 'a')) { + break; + case MKTAG('d', 'a', 't', 'a'): if (!got_fmt) { av_log(s, AV_LOG_ERROR, "found no 'fmt ' tag before the 'data' tag\n"); return AVERROR_INVALIDDATA; } + goto break_loop; + case MKTAG('f','a','c','t'): + if (!sample_count) + sample_count = avio_rl32(pb); break; - }else if (tag == MKTAG('f','a','c','t') && !sample_count){ - sample_count = avio_rl32(pb); } avio_seek(pb, next_tag_ofs, SEEK_SET); } +break_loop: if (rf64) size = data_size; if (size < 0) -- cgit v1.2.3 From 67b1761fcbd8ddcfe7bdb47ace5f46435d8313fa Mon Sep 17 00:00:00 2001 From: Tomas Härdin Date: Wed, 18 May 2011 13:48:02 +0200 Subject: wav: keep parsing until EOF if the input is seekable and we know the size of the data tag Signed-off-by: Anton Khirnov --- libavformat/wav.c | 47 ++++++++++++++++++++++++++++++++--------------- 1 file changed, 32 insertions(+), 15 deletions(-) (limited to 'libavformat/wav.c') diff --git a/libavformat/wav.c b/libavformat/wav.c index cf815ef3df..a8312e254e 100644 --- a/libavformat/wav.c +++ b/libavformat/wav.c @@ -296,7 +296,7 @@ static int wav_read_header(AVFormatContext *s, AVStream *st; WAVContext *wav = s->priv_data; int ret, got_fmt = 0; - int64_t next_tag_ofs; + int64_t next_tag_ofs, data_ofs = -1; /* check RIFF header */ tag = avio_rl32(pb); @@ -327,13 +327,13 @@ static int wav_read_header(AVFormatContext *s, avio_skip(pb, size - 16); /* skip rest of ds64 chunk */ } - for (;;) { - if (pb->eof_reached) - return -1; size = next_tag(pb, &tag); next_tag_ofs = avio_tell(pb) + size; + if (pb->eof_reached) + break; + switch (tag) { case MKTAG('f', 'm', 't', ' '): /* only parse the first 'fmt ' tag found */ @@ -350,26 +350,43 @@ static int wav_read_header(AVFormatContext *s, return AVERROR_INVALIDDATA; } - goto break_loop; + if (rf64) { + next_tag_ofs = wav->data_end = avio_tell(pb) + data_size; + } else { + data_size = size; + next_tag_ofs = wav->data_end = size ? next_tag_ofs : INT64_MAX; + } + + data_ofs = avio_tell(pb); + + /* don't look for footer metadata if we can't seek or if we don't + * know where the data tag ends + */ + if (!pb->seekable || (!rf64 && !size)) + goto break_loop; + break; case MKTAG('f','a','c','t'): if (!sample_count) sample_count = avio_rl32(pb); break; } - avio_seek(pb, next_tag_ofs, SEEK_SET); + + /* seek to next tag unless we know that we'll run into EOF */ + if ((avio_size(pb) > 0 && next_tag_ofs >= avio_size(pb)) || + avio_seek(pb, next_tag_ofs, SEEK_SET) < 0) { + break; + } } break_loop: - if (rf64) - size = data_size; - if (size < 0) - return -1; - if (!size) { - wav->data_end = INT64_MAX; - } else - wav->data_end= avio_tell(pb) + size; + if (data_ofs < 0) { + av_log(s, AV_LOG_ERROR, "no 'data' tag found\n"); + return AVERROR_INVALIDDATA; + } + + avio_seek(pb, data_ofs, SEEK_SET); if (!sample_count && st->codec->channels && av_get_bits_per_sample(st->codec->codec_id)) - sample_count = (size<<3) / (st->codec->channels * (uint64_t)av_get_bits_per_sample(st->codec->codec_id)); + sample_count = (data_size<<3) / (st->codec->channels * (uint64_t)av_get_bits_per_sample(st->codec->codec_id)); if (sample_count) st->duration = sample_count; return 0; -- cgit v1.2.3 From b21e6b707fbc80511f4886d3b272a77b86157a2d Mon Sep 17 00:00:00 2001 From: Tomas Härdin Date: Wed, 18 May 2011 13:48:39 +0200 Subject: wav: parse 'bext' metadata Signed-off-by: Anton Khirnov --- doc/general.texi | 2 +- libavformat/wav.c | 101 ++++++++++++++++++++++++++++++++++++++++++++++++++++++ 2 files changed, 102 insertions(+), 1 deletion(-) (limited to 'libavformat/wav.c') diff --git a/doc/general.texi b/doc/general.texi index 77a54a3fd0..fe314481a3 100644 --- a/doc/general.texi +++ b/doc/general.texi @@ -66,7 +66,7 @@ library: @tab Used in Z and Z95 games. @item Brute Force & Ignorance @tab @tab X @tab Used in the game Flash Traffic: City of Angels. -@item BWF @tab X @tab +@item BWF @tab X @tab X @item Interplay C93 @tab @tab X @tab Used in the game Cyberia from Interplay. @item Delphine Software International CIN @tab @tab X diff --git a/libavformat/wav.c b/libavformat/wav.c index a8312e254e..5b9f6e4480 100644 --- a/libavformat/wav.c +++ b/libavformat/wav.c @@ -23,6 +23,8 @@ * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA */ +#include "libavutil/avassert.h" +#include "libavutil/dict.h" #include "libavutil/log.h" #include "libavutil/mathematics.h" #include "libavutil/opt.h" @@ -32,6 +34,7 @@ #include "riff.h" #include "avio.h" #include "avio_internal.h" +#include "metadata.h" typedef struct { const AVClass *class; @@ -284,6 +287,97 @@ static int wav_parse_fmt_tag(AVFormatContext *s, int64_t size, AVStream **st) return 0; } +static inline int wav_parse_bext_string(AVFormatContext *s, const char *key, + int length) +{ + char temp[257]; + int ret; + + av_assert0(length <= sizeof(temp)); + if ((ret = avio_read(s->pb, temp, length)) < 0) + return ret; + + temp[length] = 0; + + if (strlen(temp)) + return av_dict_set(&s->metadata, key, temp, 0); + + return 0; +} + +static int wav_parse_bext_tag(AVFormatContext *s, int64_t size) +{ + char temp[131], *coding_history; + int ret, x; + uint64_t time_reference; + int64_t umid_parts[8], umid_mask = 0; + + if ((ret = wav_parse_bext_string(s, "description", 256)) < 0 || + (ret = wav_parse_bext_string(s, "originator", 32)) < 0 || + (ret = wav_parse_bext_string(s, "originator_reference", 32)) < 0 || + (ret = wav_parse_bext_string(s, "origination_date", 10)) < 0 || + (ret = wav_parse_bext_string(s, "origination_time", 8)) < 0) + return ret; + + time_reference = avio_rl64(s->pb); + snprintf(temp, sizeof(temp), "%"PRIu64, time_reference); + if ((ret = av_dict_set(&s->metadata, "time_reference", temp, 0)) < 0) + return ret; + + /* check if version is >= 1, in which case an UMID may be present */ + if (avio_rl16(s->pb) >= 1) { + for (x = 0; x < 8; x++) + umid_mask |= umid_parts[x] = avio_rb64(s->pb); + + if (umid_mask) { + /* the string formatting below is per SMPTE 330M-2004 Annex C */ + if (umid_parts[4] == 0 && umid_parts[5] == 0 && umid_parts[6] == 0 && umid_parts[7] == 0) { + /* basic UMID */ + snprintf(temp, sizeof(temp), "0x%016"PRIX64"%016"PRIX64"%016"PRIX64"%016"PRIX64, + umid_parts[0], umid_parts[1], umid_parts[2], umid_parts[3]); + } else { + /* extended UMID */ + snprintf(temp, sizeof(temp), "0x%016"PRIX64"%016"PRIX64"%016"PRIX64"%016"PRIX64 + "0x%016"PRIX64"%016"PRIX64"%016"PRIX64"%016"PRIX64, + umid_parts[0], umid_parts[1], umid_parts[2], umid_parts[3], + umid_parts[4], umid_parts[5], umid_parts[6], umid_parts[7]); + } + + if ((ret = av_dict_set(&s->metadata, "umid", temp, 0)) < 0) + return ret; + } + + avio_skip(s->pb, 190); + } else + avio_skip(s->pb, 254); + + if (size > 602) { + /* CodingHistory present */ + size -= 602; + + if (!(coding_history = av_malloc(size+1))) + return AVERROR(ENOMEM); + + if ((ret = avio_read(s->pb, coding_history, size)) < 0) + return ret; + + coding_history[size] = 0; + if ((ret = av_dict_set(&s->metadata, "coding_history", coding_history, + AV_METADATA_DONT_STRDUP_VAL)) < 0) + return ret; + } + + return 0; +} + +static const AVMetadataConv wav_metadata_conv[] = { + {"description", "comment" }, + {"originator", "encoded_by" }, + {"origination_date", "date" }, + {"origination_time", "creation_time"}, + {0}, +}; + /* wav input */ static int wav_read_header(AVFormatContext *s, AVFormatParameters *ap) @@ -369,6 +463,10 @@ static int wav_read_header(AVFormatContext *s, if (!sample_count) sample_count = avio_rl32(pb); break; + case MKTAG('b','e','x','t'): + if ((ret = wav_parse_bext_tag(s, size)) < 0) + return ret; + break; } /* seek to next tag unless we know that we'll run into EOF */ @@ -389,6 +487,9 @@ break_loop: sample_count = (data_size<<3) / (st->codec->channels * (uint64_t)av_get_bits_per_sample(st->codec->codec_id)); if (sample_count) st->duration = sample_count; + + ff_metadata_conv_ctx(s, NULL, wav_metadata_conv); + return 0; } -- cgit v1.2.3