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 kMaxIterations = 100;
41 constexpr int32_t kNumberOfC2WorkItems = 8;
42 constexpr uint32_t kWidthOfVideo = 3840;
43 constexpr uint32_t kHeightOfVideo = 2160;
44 constexpr uint32_t kSamplingRateOfAudio = 48000;
45 constexpr uint32_t kChannelsOfAudio = 8;
46 
47 typedef std::tuple<uint8_t*, size_t, uint32_t> FrameData;
48 
49 class Codec2Fuzzer {
50  public:
51   Codec2Fuzzer() = default;
~Codec2Fuzzer()52   ~Codec2Fuzzer() { deInitDecoder(); }
53   bool initDecoder();
54   void deInitDecoder();
55   void decodeFrames(const uint8_t* data, size_t size);
56 
57   void handleWorkDone(std::weak_ptr<C2Component> comp,
58                       std::list<std::unique_ptr<C2Work>>& workItems);
59 
60  private:
61   class BufferSource {
62    public:
BufferSource(const uint8_t * data,size_t size)63     BufferSource(const uint8_t* data, size_t size) : mData(data), mSize(size) {
64       mReadIndex = (size <= kMarkerSize) ? 0 : (size - kMarkerSize);
65     }
~BufferSource()66     ~BufferSource() {
67       mData = nullptr;
68       mSize = 0;
69       mReadIndex = 0;
70       mFrameList.clear();
71     }
isEos()72     bool isEos() { return mFrameList.empty(); }
73     void parse();
74     FrameData getFrame();
75 
76    private:
isMarker()77     bool isMarker() {
78       if ((kMarkerSize < mSize) && (mReadIndex < mSize - kMarkerSize)) {
79         return (memcmp(&mData[mReadIndex], kMarker, kMarkerSize) == 0);
80       } else {
81         return false;
82       }
83     }
84 
isCSDMarker(size_t position)85     bool isCSDMarker(size_t position) {
86       if ((kMarkerSuffixSize < mSize) && (position < mSize - kMarkerSuffixSize)) {
87         return (memcmp(&mData[position], kCsdMarkerSuffix, kMarkerSuffixSize) == 0);
88       } else {
89         return false;
90       }
91     }
92 
93     bool searchForMarker();
94 
95     const uint8_t* mData = nullptr;
96     size_t mSize = 0;
97     size_t mReadIndex = 0;
98     std::vector<FrameData> mFrameList;
99     static constexpr uint8_t kMarker[] = "_MARK";
100     static constexpr uint8_t kCsdMarkerSuffix[] = "_H_";
101     static constexpr uint8_t kFrameMarkerSuffix[] = "_F_";
102     // All markers should be 5 bytes long ( sizeof '_MARK' which is 5)
103     static constexpr size_t kMarkerSize = (sizeof(kMarker) - 1);
104     // All marker types should be 3 bytes long ('_H_', '_F_')
105     static constexpr size_t kMarkerSuffixSize = 3;
106   };
107 
108   bool mEos = false;
109   C2BlockPool::local_id_t mBlockPoolId;
110 
111   std::shared_ptr<C2BlockPool> mLinearPool;
112   std::shared_ptr<C2Allocator> mLinearAllocator;
113   std::shared_ptr<C2Component::Listener> mListener;
114   std::shared_ptr<C2Component> mComponent;
115   std::shared_ptr<C2ComponentInterface> mInterface;
116   std::mutex mQueueLock;
117   std::condition_variable mQueueCondition;
118   std::list<std::unique_ptr<C2Work>> mWorkQueue;
119   std::mutex mDecodeCompleteMutex;
120   std::condition_variable mConditionalVariable;
121 };
122 
123 }  // namespace android
124 
125 #endif  // __C2FUZZER_H__
126