blob: d5ac81a1186993961af387c8f3ce5bfe7cb3b2dd [file] [log] [blame]
S Vasudev Prasadd064e172020-05-11 13:10:16 +05301/******************************************************************************
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
30using namespace std::chrono_literals;
31
32extern "C" ::C2ComponentFactory* CreateCodec2Factory();
33extern "C" void DestroyCodec2Factory(::C2ComponentFactory* factory);
34
35namespace android {
36
37#define C2FUZZER_ALIGN(_sz, _align) (((_sz) + ((_align)-1)) & ~((_align)-1))
38
39constexpr std::chrono::milliseconds kC2FuzzerTimeOut = 5000ms;
40constexpr int32_t kNumberOfC2WorkItems = 8;
41constexpr uint32_t kWidthOfVideo = 3840;
42constexpr uint32_t kHeightOfVideo = 2160;
43constexpr uint32_t kSamplingRateOfAudio = 48000;
44constexpr uint32_t kChannelsOfAudio = 8;
45
46typedef std::tuple<uint8_t*, size_t, uint32_t> FrameData;
47
48class 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:
S Vasudev Prasade8161a12021-01-27 17:06:55 +053062 BufferSource(const uint8_t* data, size_t size) : mData(data), mSize(size) {
63 mReadIndex = (size <= kMarkerSize) ? 0 : (size - kMarkerSize);
64 }
S Vasudev Prasadd064e172020-05-11 13:10:16 +053065 ~BufferSource() {
66 mData = nullptr;
67 mSize = 0;
68 mReadIndex = 0;
69 mFrameList.clear();
70 }
71 bool isEos() { return mFrameList.empty(); }
72 void parse();
73 FrameData getFrame();
74
75 private:
S Vasudev Prasade8161a12021-01-27 17:06:55 +053076 bool isMarker() {
77 if ((kMarkerSize < mSize) && (mReadIndex < mSize - kMarkerSize)) {
78 return (memcmp(&mData[mReadIndex], kMarker, kMarkerSize) == 0);
79 } else {
80 return false;
81 }
82 }
S Vasudev Prasadd064e172020-05-11 13:10:16 +053083
84 bool isCSDMarker(size_t position) {
S Vasudev Prasade8161a12021-01-27 17:06:55 +053085 if ((kMarkerSuffixSize < mSize) && (position < mSize - kMarkerSuffixSize)) {
86 return (memcmp(&mData[position], kCsdMarkerSuffix, kMarkerSuffixSize) == 0);
87 } else {
88 return false;
89 }
S Vasudev Prasadd064e172020-05-11 13:10:16 +053090 }
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 BufferSource* mBufferSource;
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__