1 /****************************************************************************** 2 * 3 * Copyright (C) 2020 The Android Open Source Project 4 * 5 * Licensed under the Apache License, Version 2.0 (the "License"); 6 * you may not use this file except in compliance with the License. 7 * You may obtain a copy of the License at: 8 * 9 * http://www.apache.org/licenses/LICENSE-2.0 10 * 11 * Unless required by applicable law or agreed to in writing, software 12 * distributed under the License is distributed on an "AS IS" BASIS, 13 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 14 * See the License for the specific language governing permissions and 15 * limitations under the License. 16 * 17 ***************************************************************************** 18 * Originally developed and contributed by Ittiam Systems Pvt. Ltd, Bangalore 19 */ 20 #ifndef __C2FUZZER_H__ 21 #define __C2FUZZER_H__ 22 23 #include <C2AllocatorIon.h> 24 #include <C2Buffer.h> 25 #include <C2BufferPriv.h> 26 #include <C2Component.h> 27 #include <C2Config.h> 28 #include <C2PlatformSupport.h> 29 30 using namespace std::chrono_literals; 31 32 extern "C" ::C2ComponentFactory* CreateCodec2Factory(); 33 extern "C" void DestroyCodec2Factory(::C2ComponentFactory* factory); 34 35 namespace android { 36 37 #define C2FUZZER_ALIGN(_sz, _align) (((_sz) + ((_align)-1)) & ~((_align)-1)) 38 39 constexpr std::chrono::milliseconds kC2FuzzerTimeOut = 5000ms; 40 constexpr int32_t kNumberOfC2WorkItems = 8; 41 constexpr uint32_t kWidthOfVideo = 3840; 42 constexpr uint32_t kHeightOfVideo = 2160; 43 constexpr uint32_t kSamplingRateOfAudio = 48000; 44 constexpr uint32_t kChannelsOfAudio = 8; 45 46 typedef std::tuple<uint8_t*, size_t, uint32_t> FrameData; 47 48 class Codec2Fuzzer { 49 public: 50 Codec2Fuzzer() = default; ~Codec2Fuzzer()51 ~Codec2Fuzzer() { deInitDecoder(); } 52 bool initDecoder(); 53 void deInitDecoder(); 54 void decodeFrames(const uint8_t* data, size_t size); 55 56 void handleWorkDone(std::weak_ptr<C2Component> comp, 57 std::list<std::unique_ptr<C2Work>>& workItems); 58 59 private: 60 class BufferSource { 61 public: BufferSource(const uint8_t * data,size_t size)62 BufferSource(const uint8_t* data, size_t size) : mData(data), mSize(size) { 63 mReadIndex = (size <= kMarkerSize) ? 0 : (size - kMarkerSize); 64 } ~BufferSource()65 ~BufferSource() { 66 mData = nullptr; 67 mSize = 0; 68 mReadIndex = 0; 69 mFrameList.clear(); 70 } isEos()71 bool isEos() { return mFrameList.empty(); } 72 void parse(); 73 FrameData getFrame(); 74 75 private: isMarker()76 bool isMarker() { 77 if ((kMarkerSize < mSize) && (mReadIndex < mSize - kMarkerSize)) { 78 return (memcmp(&mData[mReadIndex], kMarker, kMarkerSize) == 0); 79 } else { 80 return false; 81 } 82 } 83 isCSDMarker(size_t position)84 bool isCSDMarker(size_t position) { 85 if ((kMarkerSuffixSize < mSize) && (position < mSize - kMarkerSuffixSize)) { 86 return (memcmp(&mData[position], kCsdMarkerSuffix, kMarkerSuffixSize) == 0); 87 } else { 88 return false; 89 } 90 } 91 92 bool searchForMarker(); 93 94 const uint8_t* mData = nullptr; 95 size_t mSize = 0; 96 size_t mReadIndex = 0; 97 std::vector<FrameData> mFrameList; 98 static constexpr uint8_t kMarker[] = "_MARK"; 99 static constexpr uint8_t kCsdMarkerSuffix[] = "_H_"; 100 static constexpr uint8_t kFrameMarkerSuffix[] = "_F_"; 101 // All markers should be 5 bytes long ( sizeof '_MARK' which is 5) 102 static constexpr size_t kMarkerSize = (sizeof(kMarker) - 1); 103 // All marker types should be 3 bytes long ('_H_', '_F_') 104 static constexpr size_t kMarkerSuffixSize = 3; 105 }; 106 107 bool mEos = false; 108 C2BlockPool::local_id_t mBlockPoolId; 109 110 std::shared_ptr<C2BlockPool> mLinearPool; 111 std::shared_ptr<C2Allocator> mLinearAllocator; 112 std::shared_ptr<C2Component::Listener> mListener; 113 std::shared_ptr<C2Component> mComponent; 114 std::shared_ptr<C2ComponentInterface> mInterface; 115 std::mutex mQueueLock; 116 std::condition_variable mQueueCondition; 117 std::list<std::unique_ptr<C2Work>> mWorkQueue; 118 std::mutex mDecodeCompleteMutex; 119 std::condition_variable mConditionalVariable; 120 }; 121 122 } // namespace android 123 124 #endif // __C2FUZZER_H__ 125