diff options
Diffstat (limited to 'libavformat/riffdec.c')
-rw-r--r-- | libavformat/riffdec.c | 144 |
1 files changed, 91 insertions, 53 deletions
diff --git a/libavformat/riffdec.c b/libavformat/riffdec.c index db83b32..b448f91 100644 --- a/libavformat/riffdec.c +++ b/libavformat/riffdec.c @@ -2,20 +2,20 @@ * RIFF demuxing functions and data * Copyright (c) 2000 Fabrice Bellard * - * This file is part of Libav. + * This file is part of FFmpeg. * - * Libav is free software; you can redistribute it and/or + * FFmpeg is free software; you can redistribute it and/or * modify it under the terms of the GNU Lesser General Public * License as published by the Free Software Foundation; either * version 2.1 of the License, or (at your option) any later version. * - * Libav is distributed in the hope that it will be useful, + * FFmpeg is distributed in the hope that it will be useful, * but WITHOUT ANY WARRANTY; without even the implied warranty of * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU * Lesser General Public License for more details. * * You should have received a copy of the GNU Lesser General Public - * License along with Libav; if not, write to the Free Software + * License along with FFmpeg; if not, write to the Free Software * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA */ @@ -29,13 +29,17 @@ #include "avio_internal.h" #include "riff.h" -const AVCodecGuid ff_codec_wav_guids[] = { - { AV_CODEC_ID_AC3, { 0x2C, 0x80, 0x6D, 0xE0, 0x46, 0xDB, 0xCF, 0x11, 0xB4, 0xD1, 0x00, 0x80, 0x5F, 0x6C, 0xBB, 0xEA } }, - { AV_CODEC_ID_ATRAC3P, { 0xBF, 0xAA, 0x23, 0xE9, 0x58, 0xCB, 0x71, 0x44, 0xA1, 0x19, 0xFF, 0xFA, 0x01, 0xE4, 0xCE, 0x62 } }, - { AV_CODEC_ID_EAC3, { 0xAF, 0x87, 0xFB, 0xA7, 0x02, 0x2D, 0xFB, 0x42, 0xA4, 0xD4, 0x05, 0xCD, 0x93, 0x84, 0x3B, 0xDD } }, - { AV_CODEC_ID_MP2, { 0x2B, 0x80, 0x6D, 0xE0, 0x46, 0xDB, 0xCF, 0x11, 0xB4, 0xD1, 0x00, 0x80, 0x5F, 0x6C, 0xBB, 0xEA } }, - { AV_CODEC_ID_NONE } -}; +int ff_get_guid(AVIOContext *s, ff_asf_guid *g) +{ + int ret; + av_assert0(sizeof(*g) == 16); //compiler will optimize this out + ret = avio_read(s, *g, sizeof(*g)); + if (ret < (int)sizeof(*g)) { + memset(*g, 0, sizeof(*g)); + return ret < 0 ? ret : AVERROR_INVALIDDATA; + } + return 0; +} enum AVCodecID ff_codec_guid_get_id(const AVCodecGuid *guids, ff_asf_guid guid) { @@ -66,6 +70,10 @@ static void parse_waveformatex(AVIOContext *pb, AVCodecParameters *par) ff_get_guid(pb, &subformat); if (!memcmp(subformat + 4, + (const uint8_t[]){ FF_AMBISONIC_BASE_GUID }, 12) || + !memcmp(subformat + 4, + (const uint8_t[]){ FF_BROKEN_BASE_GUID }, 12) || + !memcmp(subformat + 4, (const uint8_t[]){ FF_MEDIASUBTYPE_BASE_GUID }, 12)) { par->codec_tag = AV_RL32(subformat); par->codec_id = ff_wav_codec_get_id(par->codec_tag, @@ -79,33 +87,56 @@ static void parse_waveformatex(AVIOContext *pb, AVCodecParameters *par) } } +/* "big_endian" values are needed for RIFX file format */ int ff_get_wav_header(AVFormatContext *s, AVIOContext *pb, - AVCodecParameters *par, int size) + AVCodecParameters *par, int size, int big_endian) { int id; - uint64_t bitrate; + uint64_t bitrate = 0; - if (size < 14) + if (size < 14) { + avpriv_request_sample(s, "wav header size < 14"); return AVERROR_INVALIDDATA; + } - id = avio_rl16(pb); - par->codec_type = AVMEDIA_TYPE_AUDIO; - par->channels = avio_rl16(pb); - par->sample_rate = avio_rl32(pb); - bitrate = avio_rl32(pb) * 8; - par->block_align = avio_rl16(pb); + par->codec_type = AVMEDIA_TYPE_AUDIO; + if (!big_endian) { + id = avio_rl16(pb); + if (id != 0x0165) { + par->channels = avio_rl16(pb); + par->sample_rate = avio_rl32(pb); + bitrate = avio_rl32(pb) * 8LL; + par->block_align = avio_rl16(pb); + } + } else { + id = avio_rb16(pb); + par->channels = avio_rb16(pb); + par->sample_rate = avio_rb32(pb); + bitrate = avio_rb32(pb) * 8LL; + par->block_align = avio_rb16(pb); + } if (size == 14) { /* We're dealing with plain vanilla WAVEFORMAT */ par->bits_per_coded_sample = 8; - } else - par->bits_per_coded_sample = avio_rl16(pb); + } else { + if (!big_endian) { + par->bits_per_coded_sample = avio_rl16(pb); + } else { + par->bits_per_coded_sample = avio_rb16(pb); + } + } if (id == 0xFFFE) { par->codec_tag = 0; } else { par->codec_tag = id; - par->codec_id = ff_wav_codec_get_id(id, par->bits_per_coded_sample); + par->codec_id = ff_wav_codec_get_id(id, + par->bits_per_coded_sample); } - if (size >= 18) { /* We're obviously dealing with WAVEFORMATEX */ + if (size >= 18 && id != 0x0165) { /* We're obviously dealing with WAVEFORMATEX */ int cbSize = avio_rl16(pb); /* cbSize */ + if (big_endian) { + avpriv_report_missing_feature(s, "WAVEFORMATEX support for RIFX files"); + return AVERROR_PATCHWELCOME; + } size -= 18; cbSize = FFMIN(size, cbSize); if (cbSize >= 22 && id == 0xfffe) { /* WAVEFORMATEXTENSIBLE */ @@ -113,38 +144,35 @@ int ff_get_wav_header(AVFormatContext *s, AVIOContext *pb, cbSize -= 22; size -= 22; } - par->extradata_size = cbSize; if (cbSize > 0) { - av_free(par->extradata); - par->extradata = av_mallocz(par->extradata_size + - AV_INPUT_BUFFER_PADDING_SIZE); - if (!par->extradata) + av_freep(&par->extradata); + if (ff_get_extradata(s, par, pb, cbSize) < 0) return AVERROR(ENOMEM); - avio_read(pb, par->extradata, par->extradata_size); size -= cbSize; } /* It is possible for the chunk to contain garbage at the end */ if (size > 0) avio_skip(pb, size); - } + } else if (id == 0x0165 && size >= 32) { + int nb_streams, i; - if (bitrate > INT_MAX) { - if (s->error_recognition & AV_EF_EXPLODE) { - av_log(s, AV_LOG_ERROR, - "The bitrate %"PRIu64" is too large.\n", - bitrate); + size -= 4; + av_freep(&par->extradata); + if (ff_get_extradata(s, par, pb, size) < 0) + return AVERROR(ENOMEM); + nb_streams = AV_RL16(par->extradata + 4); + par->sample_rate = AV_RL32(par->extradata + 12); + par->channels = 0; + bitrate = 0; + if (size < 8 + nb_streams * 20) return AVERROR_INVALIDDATA; - } else { - av_log(s, AV_LOG_WARNING, - "The bitrate %"PRIu64" is too large, resetting to 0.", - bitrate); - par->bit_rate = 0; - } - } else { - par->bit_rate = bitrate; + for (i = 0; i < nb_streams; i++) + par->channels += par->extradata[8 + i * 20 + 17]; } + par->bit_rate = bitrate; + if (par->sample_rate <= 0) { av_log(s, AV_LOG_ERROR, "Invalid sample rate: %d\n", par->sample_rate); @@ -157,7 +185,7 @@ int ff_get_wav_header(AVFormatContext *s, AVIOContext *pb, par->sample_rate = 0; } /* override bits_per_coded_sample for G.726 */ - if (par->codec_id == AV_CODEC_ID_ADPCM_G726) + if (par->codec_id == AV_CODEC_ID_ADPCM_G726 && par->sample_rate) par->bits_per_coded_sample = par->bit_rate / par->sample_rate; return 0; @@ -216,12 +244,23 @@ int ff_read_riff_info(AVFormatContext *s, int64_t size) chunk_code = avio_rl32(pb); chunk_size = avio_rl32(pb); - + if (avio_feof(pb)) { + if (chunk_code || chunk_size) { + av_log(s, AV_LOG_WARNING, "INFO subchunk truncated\n"); + return AVERROR_INVALIDDATA; + } + return AVERROR_EOF; + } if (chunk_size > end || end - chunk_size < cur || chunk_size == UINT_MAX) { - av_log(s, AV_LOG_WARNING, "too big INFO subchunk\n"); - break; + avio_seek(pb, -9, SEEK_CUR); + chunk_code = avio_rl32(pb); + chunk_size = avio_rl32(pb); + if (chunk_size > end || end - chunk_size < cur || chunk_size == UINT_MAX) { + av_log(s, AV_LOG_WARNING, "too big INFO subchunk\n"); + return AVERROR_INVALIDDATA; + } } chunk_size += (chunk_size & 1); @@ -236,7 +275,7 @@ int ff_read_riff_info(AVFormatContext *s, int64_t size) continue; } - value = av_malloc(chunk_size + 1); + value = av_mallocz(chunk_size + 1); if (!value) { av_log(s, AV_LOG_ERROR, "out of memory, unable to read INFO tag\n"); @@ -244,16 +283,15 @@ int ff_read_riff_info(AVFormatContext *s, int64_t size) } AV_WL32(key, chunk_code); + // Work around VC++ 2015 Update 1 code-gen bug: + // https://connect.microsoft.com/VisualStudio/feedback/details/2291638 + key[4] = 0; if (avio_read(pb, value, chunk_size) != chunk_size) { - av_free(value); av_log(s, AV_LOG_WARNING, "premature end of file while reading INFO tag\n"); - break; } - value[chunk_size] = 0; - av_dict_set(&s->metadata, key, value, AV_DICT_DONT_STRDUP_VAL); } |