diff --git a/include/media/ExtractorUtils.h b/include/media/ExtractorUtils.h new file mode 120000 index 0000000000..e2dd082066 --- /dev/null +++ b/include/media/ExtractorUtils.h @@ -0,0 +1 @@ +../../media/libmediaextractor/include/media/ExtractorUtils.h \ No newline at end of file diff --git a/media/extractors/mkv/MatroskaExtractor.cpp b/media/extractors/mkv/MatroskaExtractor.cpp index 3f832bc47f..fc60fd4577 100644 --- a/media/extractors/mkv/MatroskaExtractor.cpp +++ b/media/extractors/mkv/MatroskaExtractor.cpp @@ -22,6 +22,7 @@ #include "MatroskaExtractor.h" #include +#include #include #include #include @@ -1108,7 +1109,7 @@ static status_t addFlacMetadata( meta.setData(kKeyFlacMetadata, 0, codecPrivate, codecPrivateSize); int32_t maxInputSize = 64 << 10; - sp flacDecoder = FLACDecoder::Create(); + FLACDecoder *flacDecoder = FLACDecoder::Create(); if (flacDecoder != NULL && flacDecoder->parseMetadata((const uint8_t*)codecPrivate, codecPrivateSize) == OK) { FLAC__StreamMetadata_StreamInfo streamInfo = flacDecoder->getStreamInfo(); @@ -1120,6 +1121,7 @@ static status_t addFlacMetadata( && streamInfo.channels != 0 && ((streamInfo.bits_per_sample + 7) / 8) > INT32_MAX / streamInfo.max_blocksize / streamInfo.channels) { + delete flacDecoder; return ERROR_MALFORMED; } maxInputSize = ((streamInfo.bits_per_sample + 7) / 8) @@ -1128,6 +1130,7 @@ static status_t addFlacMetadata( } meta.setInt32(kKeyMaxInputSize, maxInputSize); + delete flacDecoder; return OK; } @@ -1143,13 +1146,13 @@ status_t MatroskaExtractor::synthesizeAVCC(TrackInfo *trackInfo, size_t index) { } const mkvparser::Block::Frame &frame = block->GetFrame(0); - sp abuf = new ABuffer(frame.len); - long n = frame.Read(mReader, abuf->data()); + auto tmpData = heapbuffer(frame.len); + long n = frame.Read(mReader, tmpData.get()); if (n != 0) { return ERROR_MALFORMED; } - if (!MakeAVCCodecSpecificData(trackInfo->mMeta, abuf)) { + if (!MakeAVCCodecSpecificData(trackInfo->mMeta, tmpData.get(), frame.len)) { return ERROR_MALFORMED; } diff --git a/media/extractors/ogg/OggExtractor.cpp b/media/extractors/ogg/OggExtractor.cpp index 4d49013ad0..b2fe69cd07 100644 --- a/media/extractors/ogg/OggExtractor.cpp +++ b/media/extractors/ogg/OggExtractor.cpp @@ -22,6 +22,7 @@ #include #include +#include #include #include #include @@ -990,21 +991,11 @@ status_t MyOpusExtractor::verifyOpusHeader(MediaBufferBase *buffer) { return OK; } -struct TmpData { - uint8_t *data; - TmpData(size_t size) { - data = (uint8_t*) malloc(size); - } - ~TmpData() { - free(data); - } -}; - status_t MyOpusExtractor::verifyOpusComments(MediaBufferBase *buffer) { // add artificial framing bit so we can reuse _vorbis_unpack_comment int32_t commentSize = buffer->range_length() + 1; - TmpData commentDataHolder(commentSize); - uint8_t *commentData = commentDataHolder.data; + auto tmp = heapbuffer(commentSize); + uint8_t *commentData = tmp.get(); if (commentData == nullptr) { return ERROR_MALFORMED; } diff --git a/media/libmediaextractor/include/media/ExtractorUtils.h b/media/libmediaextractor/include/media/ExtractorUtils.h new file mode 100644 index 0000000000..22f9349272 --- /dev/null +++ b/media/libmediaextractor/include/media/ExtractorUtils.h @@ -0,0 +1,32 @@ +/* + * Copyright (C) 2018 The Android Open Source Project + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +#ifndef EXTRACTOR_UTILS_H_ + +#define EXTRACTOR_UTILS_H_ + +#include + +namespace android { + +template +std::unique_ptr heapbuffer(size_t size) { + return std::unique_ptr(new (std::nothrow) T[size]); +} + +} // namespace android + +#endif // UTILS_H_ diff --git a/media/libstagefright/MetaDataUtils.cpp b/media/libstagefright/MetaDataUtils.cpp index af8f539e35..04f6adea4d 100644 --- a/media/libstagefright/MetaDataUtils.cpp +++ b/media/libstagefright/MetaDataUtils.cpp @@ -24,11 +24,12 @@ namespace android { -bool MakeAVCCodecSpecificData(MetaDataBase &meta, const sp &accessUnit) { +bool MakeAVCCodecSpecificData(MetaDataBase &meta, const uint8_t *data, size_t size) { int32_t width; int32_t height; int32_t sarWidth; int32_t sarHeight; + sp accessUnit = new ABuffer((void*)data, size); sp csd = MakeAVCCodecSpecificData(accessUnit, &width, &height, &sarWidth, &sarHeight); if (csd == nullptr) { return false; diff --git a/media/libstagefright/codecs/flac/dec/C2SoftFlacDecoder.cpp b/media/libstagefright/codecs/flac/dec/C2SoftFlacDecoder.cpp index ce40d6b8f6..ba8ef6d102 100644 --- a/media/libstagefright/codecs/flac/dec/C2SoftFlacDecoder.cpp +++ b/media/libstagefright/codecs/flac/dec/C2SoftFlacDecoder.cpp @@ -48,6 +48,7 @@ C2SoftFlacDecoder::C2SoftFlacDecoder(const char *name, c2_node_id_t id) } C2SoftFlacDecoder::~C2SoftFlacDecoder() { + delete mFLACDecoder; } c2_status_t C2SoftFlacDecoder::onInit() { @@ -77,6 +78,9 @@ c2_status_t C2SoftFlacDecoder::onFlush_sm() { } status_t C2SoftFlacDecoder::initDecoder() { + if (mFLACDecoder) { + delete mFLACDecoder; + } mFLACDecoder = FLACDecoder::Create(); if (!mFLACDecoder) { ALOGE("initDecoder: failed to create FLACDecoder"); diff --git a/media/libstagefright/codecs/flac/dec/C2SoftFlacDecoder.h b/media/libstagefright/codecs/flac/dec/C2SoftFlacDecoder.h index a5c01a9ef5..43d913b83c 100644 --- a/media/libstagefright/codecs/flac/dec/C2SoftFlacDecoder.h +++ b/media/libstagefright/codecs/flac/dec/C2SoftFlacDecoder.h @@ -46,7 +46,7 @@ private: kMaxBlockSize = 4096 }; - sp mFLACDecoder; + FLACDecoder *mFLACDecoder; FLAC__StreamMetadata_StreamInfo mStreamInfo; bool mSignalledError; bool mSignalledOutputEos; diff --git a/media/libstagefright/codecs/flac/dec/SoftFlacDecoder.cpp b/media/libstagefright/codecs/flac/dec/SoftFlacDecoder.cpp index 4ab1ab2b5a..d0b72b77d2 100644 --- a/media/libstagefright/codecs/flac/dec/SoftFlacDecoder.cpp +++ b/media/libstagefright/codecs/flac/dec/SoftFlacDecoder.cpp @@ -57,6 +57,7 @@ SoftFlacDecoder::SoftFlacDecoder( SoftFlacDecoder::~SoftFlacDecoder() { ALOGV("dtor:"); + delete mFLACDecoder; } void SoftFlacDecoder::initPorts() { diff --git a/media/libstagefright/codecs/flac/dec/SoftFlacDecoder.h b/media/libstagefright/codecs/flac/dec/SoftFlacDecoder.h index 4a21c3429a..0f17ed819b 100644 --- a/media/libstagefright/codecs/flac/dec/SoftFlacDecoder.h +++ b/media/libstagefright/codecs/flac/dec/SoftFlacDecoder.h @@ -50,7 +50,7 @@ private: kNumOutputBuffers = 4, }; - sp mFLACDecoder; + FLACDecoder *mFLACDecoder; FLAC__StreamMetadata_StreamInfo mStreamInfo; bool mHasStreamInfo; size_t mInputBufferCount; diff --git a/media/libstagefright/flac/dec/FLACDecoder.cpp b/media/libstagefright/flac/dec/FLACDecoder.cpp index 8c7137c3be..e0e9211629 100644 --- a/media/libstagefright/flac/dec/FLACDecoder.cpp +++ b/media/libstagefright/flac/dec/FLACDecoder.cpp @@ -220,9 +220,10 @@ static void copyMultiCh24( } // static -sp FLACDecoder::Create() { - sp decoder = new FLACDecoder(); - if (decoder->init() != OK) { +FLACDecoder *FLACDecoder::Create() { + FLACDecoder *decoder = new (std::nothrow) FLACDecoder(); + if (decoder == NULL || decoder->init() != OK) { + delete decoder; return NULL; } return decoder; diff --git a/media/libstagefright/flac/dec/FLACDecoder.h b/media/libstagefright/flac/dec/FLACDecoder.h index 36282a8c66..1a33caee44 100644 --- a/media/libstagefright/flac/dec/FLACDecoder.h +++ b/media/libstagefright/flac/dec/FLACDecoder.h @@ -26,14 +26,14 @@ namespace android { // packet based FLAC decoder, wrapps libFLAC stream decoder. -class FLACDecoder : public RefBase { +class FLACDecoder { public: enum { kMaxChannels = 8, }; - static sp Create(); + static FLACDecoder *Create(); FLAC__StreamMetadata_StreamInfo getStreamInfo() const { return mStreamInfo; @@ -43,10 +43,10 @@ public: status_t decodeOneFrame(const uint8_t *inBuffer, size_t inBufferLen, short *outBuffer, size_t *outBufferLen); void flush(); + virtual ~FLACDecoder(); protected: FLACDecoder(); - virtual ~FLACDecoder() override; private: // stream properties diff --git a/media/libstagefright/include/media/stagefright/MetaDataUtils.h b/media/libstagefright/include/media/stagefright/MetaDataUtils.h index 3af2218663..d5a8080e63 100644 --- a/media/libstagefright/include/media/stagefright/MetaDataUtils.h +++ b/media/libstagefright/include/media/stagefright/MetaDataUtils.h @@ -23,7 +23,7 @@ namespace android { struct ABuffer; -bool MakeAVCCodecSpecificData(MetaDataBase &meta, const sp &accessUnit); +bool MakeAVCCodecSpecificData(MetaDataBase &meta, const uint8_t *data, size_t size); bool MakeAACCodecSpecificData(MetaDataBase &meta, unsigned profile, unsigned sampling_freq_index, unsigned channel_configuration); diff --git a/media/libstagefright/mpeg2ts/ESQueue.cpp b/media/libstagefright/mpeg2ts/ESQueue.cpp index 0b7bd2600b..50b1bea8ee 100644 --- a/media/libstagefright/mpeg2ts/ESQueue.cpp +++ b/media/libstagefright/mpeg2ts/ESQueue.cpp @@ -634,7 +634,7 @@ sp ElementaryStreamQueue::dequeueAccessUnit() { if (mFormat == NULL) { mFormat = new MetaData; - if (!MakeAVCCodecSpecificData(*mFormat, accessUnit)) { + if (!MakeAVCCodecSpecificData(*mFormat, accessUnit->data(), accessUnit->size())) { mFormat.clear(); } } @@ -1010,7 +1010,7 @@ sp ElementaryStreamQueue::dequeueAccessUnitH264() { } if (mFormat == NULL) { mFormat = new MetaData; - if (!MakeAVCCodecSpecificData(*mFormat, mBuffer)) { + if (!MakeAVCCodecSpecificData(*mFormat, mBuffer->data(), mBuffer->size())) { ALOGW("Creating dummy AVC format for scrambled content"); mFormat = new MetaData; mFormat->setCString(kKeyMIMEType, MEDIA_MIMETYPE_VIDEO_AVC); @@ -1172,7 +1172,9 @@ sp ElementaryStreamQueue::dequeueAccessUnitH264() { if (mFormat == NULL) { mFormat = new MetaData; - if (!MakeAVCCodecSpecificData(*mFormat, accessUnit)) { + if (!MakeAVCCodecSpecificData(*mFormat, + accessUnit->data(), + accessUnit->size())) { mFormat.clear(); } }