S Vasudev Prasad | d064e17 | 2020-05-11 13:10:16 +0530 | [diff] [blame] | 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; |
| 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: |
| 62 | BufferSource(const uint8_t* data, size_t size) |
| 63 | : mData(data), mSize(size), mReadIndex(size - kMarkerSize) {} |
| 64 | ~BufferSource() { |
| 65 | mData = nullptr; |
| 66 | mSize = 0; |
| 67 | mReadIndex = 0; |
| 68 | mFrameList.clear(); |
| 69 | } |
| 70 | bool isEos() { return mFrameList.empty(); } |
| 71 | void parse(); |
| 72 | FrameData getFrame(); |
| 73 | |
| 74 | private: |
| 75 | bool isMarker() { return (memcmp(&mData[mReadIndex], kMarker, kMarkerSize) == 0); } |
| 76 | |
| 77 | bool isCSDMarker(size_t position) { |
| 78 | return (memcmp(&mData[position], kCsdMarkerSuffix, kMarkerSuffixSize) == 0); |
| 79 | } |
| 80 | |
| 81 | bool searchForMarker(); |
| 82 | |
| 83 | const uint8_t* mData = nullptr; |
| 84 | size_t mSize = 0; |
| 85 | size_t mReadIndex = 0; |
| 86 | std::vector<FrameData> mFrameList; |
| 87 | static constexpr uint8_t kMarker[] = "_MARK"; |
| 88 | static constexpr uint8_t kCsdMarkerSuffix[] = "_H_"; |
| 89 | static constexpr uint8_t kFrameMarkerSuffix[] = "_F_"; |
| 90 | // All markers should be 5 bytes long ( sizeof '_MARK' which is 5) |
| 91 | static constexpr size_t kMarkerSize = (sizeof(kMarker) - 1); |
| 92 | // All marker types should be 3 bytes long ('_H_', '_F_') |
| 93 | static constexpr size_t kMarkerSuffixSize = 3; |
| 94 | }; |
| 95 | |
| 96 | BufferSource* mBufferSource; |
| 97 | bool mEos = false; |
| 98 | C2BlockPool::local_id_t mBlockPoolId; |
| 99 | |
| 100 | std::shared_ptr<C2BlockPool> mLinearPool; |
| 101 | std::shared_ptr<C2Allocator> mLinearAllocator; |
| 102 | std::shared_ptr<C2Component::Listener> mListener; |
| 103 | std::shared_ptr<C2Component> mComponent; |
| 104 | std::shared_ptr<C2ComponentInterface> mInterface; |
| 105 | std::mutex mQueueLock; |
| 106 | std::condition_variable mQueueCondition; |
| 107 | std::list<std::unique_ptr<C2Work>> mWorkQueue; |
| 108 | std::mutex mDecodeCompleteMutex; |
| 109 | std::condition_variable mConditionalVariable; |
| 110 | }; |
| 111 | |
| 112 | } // namespace android |
| 113 | |
| 114 | #endif // __C2FUZZER_H__ |