X-Git-Url: https://git.sesse.net/?a=blobdiff_plain;f=libavformat%2Fwav.c;h=c01121f9dbd9d3a01dedd15d6126cf71b85facfb;hb=fd4872184945ff4275f04f7b95ee7aba49407f3c;hp=cf815ef3df3ee2dda86136518c122820e4da448b;hpb=90f2ee8cb4d2b135dd33128a94ad6cf126821472;p=ffmpeg diff --git a/libavformat/wav.c b/libavformat/wav.c index cf815ef3df3..c01121f9dbd 100644 --- a/libavformat/wav.c +++ b/libavformat/wav.c @@ -23,15 +23,18 @@ * 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" #include "avformat.h" +#include "internal.h" #include "avio_internal.h" #include "pcm.h" #include "riff.h" #include "avio.h" -#include "avio_internal.h" +#include "metadata.h" typedef struct { const AVClass *class; @@ -128,7 +131,7 @@ static int wav_write_header(AVFormatContext *s) if (wav->write_bext) bwf_write_bext_chunk(s); - av_set_pts_info(s->streams[0], 64, 1, s->streams[0]->codec->sample_rate); + avpriv_set_pts_info(s->streams[0], 64, 1, s->streams[0]->codec->sample_rate); wav->maxpts = wav->last_duration = 0; wav->minpts = INT64_MAX; @@ -191,7 +194,7 @@ static int wav_write_trailer(AVFormatContext *s) #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 }, + { "write_bext", "Write BEXT chunk.", OFFSET(write_bext), AV_OPT_TYPE_INT, { 0 }, 0, 1, ENC }, { NULL }, }; @@ -203,25 +206,26 @@ static const AVClass wav_muxer_class = { }; AVOutputFormat ff_wav_muxer = { - "wav", - NULL_IF_CONFIG_SMALL("WAV format"), - "audio/x-wav", - "wav", - sizeof(WAVContext), - CODEC_ID_PCM_S16LE, - CODEC_ID_NONE, - wav_write_header, - wav_write_packet, - wav_write_trailer, - .codec_tag= (const AVCodecTag* const []){ff_codec_wav_tags, 0}, - .priv_class = &wav_muxer_class, + .name = "wav", + .long_name = NULL_IF_CONFIG_SMALL("WAV format"), + .mime_type = "audio/x-wav", + .extensions = "wav", + .priv_data_size = sizeof(WAVContext), + .audio_codec = CODEC_ID_PCM_S16LE, + .video_codec = CODEC_ID_NONE, + .write_header = wav_write_header, + .write_packet = wav_write_packet, + .write_trailer = wav_write_trailer, + .flags = AVFMT_TS_NONSTRICT, + .codec_tag = (const AVCodecTag* const []){ ff_codec_wav_tags, 0 }, + .priv_class = &wav_muxer_class, }; #endif /* CONFIG_WAV_MUXER */ #if CONFIG_WAV_DEMUXER -static int64_t next_tag(AVIOContext *pb, unsigned int *tag) +static int64_t next_tag(AVIOContext *pb, uint32_t *tag) { *tag = avio_rl32(pb); return avio_rl32(pb); @@ -270,7 +274,7 @@ static int wav_parse_fmt_tag(AVFormatContext *s, int64_t size, AVStream **st) int ret; /* parse fmt header */ - *st = av_new_stream(s, 0); + *st = avformat_new_stream(s, NULL); if (!*st) return AVERROR(ENOMEM); @@ -279,24 +283,114 @@ static int wav_parse_fmt_tag(AVFormatContext *s, int64_t size, AVStream **st) return ret; (*st)->need_parsing = AVSTREAM_PARSE_FULL; - av_set_pts_info(*st, 64, 1, (*st)->codec->sample_rate); + avpriv_set_pts_info(*st, 64, 1, (*st)->codec->sample_rate); 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_DICT_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) +static int wav_read_header(AVFormatContext *s) { int64_t size, av_uninit(data_size); int64_t sample_count=0; int rf64; - unsigned int tag; + uint32_t tag, list_type; AVIOContext *pb = s->pb; 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 +421,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,28 +444,65 @@ 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; + case MKTAG('b','e','x','t'): + if ((ret = wav_parse_bext_tag(s, size)) < 0) + return ret; + break; + case MKTAG('L', 'I', 'S', 'T'): + list_type = avio_rl32(pb); + if (size <= 4) { + av_log(s, AV_LOG_ERROR, "too short LIST"); + return AVERROR_INVALIDDATA; + } + switch (list_type) { + case MKTAG('I', 'N', 'F', 'O'): + if ((ret = ff_read_riff_info(s, size - 4)) < 0) + return ret; + } + break; + } + + /* 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; } - avio_seek(pb, next_tag_ofs, SEEK_SET); } 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; + + ff_metadata_conv_ctx(s, NULL, wav_metadata_conv); + ff_metadata_conv_ctx(s, NULL, ff_riff_info_conv); + return 0; } @@ -452,20 +583,19 @@ static int wav_read_seek(AVFormatContext *s, default: break; } - return pcm_read_seek(s, stream_index, timestamp, flags); + return ff_pcm_read_seek(s, stream_index, timestamp, flags); } AVInputFormat ff_wav_demuxer = { - "wav", - NULL_IF_CONFIG_SMALL("WAV format"), - sizeof(WAVContext), - wav_probe, - wav_read_header, - wav_read_packet, - NULL, - wav_read_seek, - .flags= AVFMT_GENERIC_INDEX, - .codec_tag= (const AVCodecTag* const []){ff_codec_wav_tags, 0}, + .name = "wav", + .long_name = NULL_IF_CONFIG_SMALL("WAV format"), + .priv_data_size = sizeof(WAVContext), + .read_probe = wav_probe, + .read_header = wav_read_header, + .read_packet = wav_read_packet, + .read_seek = wav_read_seek, + .flags = AVFMT_GENERIC_INDEX, + .codec_tag = (const AVCodecTag* const []){ ff_codec_wav_tags, 0 }, }; #endif /* CONFIG_WAV_DEMUXER */ @@ -491,7 +621,7 @@ static int w64_probe(AVProbeData *p) return 0; } -static int w64_read_header(AVFormatContext *s, AVFormatParameters *ap) +static int w64_read_header(AVFormatContext *s) { int64_t size; AVIOContext *pb = s->pb; @@ -519,7 +649,7 @@ static int w64_read_header(AVFormatContext *s, AVFormatParameters *ap) return -1; } - st = av_new_stream(s, 0); + st = avformat_new_stream(s, NULL); if (!st) return AVERROR(ENOMEM); @@ -531,7 +661,7 @@ static int w64_read_header(AVFormatContext *s, AVFormatParameters *ap) st->need_parsing = AVSTREAM_PARSE_FULL; - av_set_pts_info(st, 64, 1, st->codec->sample_rate); + avpriv_set_pts_info(st, 64, 1, st->codec->sample_rate); size = find_guid(pb, guid_data); if (size < 0) { @@ -545,15 +675,14 @@ static int w64_read_header(AVFormatContext *s, AVFormatParameters *ap) } AVInputFormat ff_w64_demuxer = { - "w64", - NULL_IF_CONFIG_SMALL("Sony Wave64 format"), - sizeof(WAVContext), - w64_probe, - w64_read_header, - wav_read_packet, - NULL, - wav_read_seek, - .flags = AVFMT_GENERIC_INDEX, - .codec_tag = (const AVCodecTag* const []){ff_codec_wav_tags, 0}, + .name = "w64", + .long_name = NULL_IF_CONFIG_SMALL("Sony Wave64 format"), + .priv_data_size = sizeof(WAVContext), + .read_probe = w64_probe, + .read_header = w64_read_header, + .read_packet = wav_read_packet, + .read_seek = wav_read_seek, + .flags = AVFMT_GENERIC_INDEX, + .codec_tag = (const AVCodecTag* const []){ ff_codec_wav_tags, 0 }, }; #endif /* CONFIG_W64_DEMUXER */