• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright (c) 2022-2023 Huawei Device Co., Ltd.
3  * Licensed under the Apache License, Version 2.0 (the "License");
4  * you may not use this file except in compliance with the License.
5  * You may obtain a copy of the License at
6  *
7  *     http://www.apache.org/licenses/LICENSE-2.0
8  *
9  * Unless required by applicable law or agreed to in writing, software
10  * distributed under the License is distributed on an "AS IS" BASIS,
11  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12  * See the License for the specific language governing permissions and
13  * limitations under the License.
14  */
15 
16 #include "codeccommon_fuzzer.h"
17 #include "codec_omx_ext.h"
18 namespace OHOS {
19 namespace Codec {
20     static const int32_t DATA_BUFFERID = 10;
21     static const int32_t DATA_SIZE = 20;
22     static const int32_t DATA_VERSION_NVERSION = 30;
23     static const int32_t DATA_BUFFERTYPE = 40;
24     static const int32_t DATA_BUFFERLEN = 50;
25     static const int32_t DATA_ALLOCLEN = 60;
26     static const int32_t DATA_FILLEDLEN = 70;
27     static const int32_t DATA_OFFSET = 80;
28     static const int32_t DATA_FENCEFD = 90;
29     static const int32_t DATA_TYPE = 100;
30     static const int32_t DATA_PTS = 200;
31     static const int32_t DATA_FLAG = 300;
32     static const int32_t TESTING_APP_DATA = 33;
33 
34     CodecComponentManager *g_manager = nullptr;
35     CodecComponentType *g_component = nullptr;
36     CodecCallbackType *g_callback = nullptr;
37     uint32_t g_componentId = 0;
38     static int32_t g_appData = TESTING_APP_DATA;
39 
Convert2Uint32(const uint8_t * ptr)40     uint32_t Convert2Uint32(const uint8_t* ptr)
41     {
42         if (ptr == nullptr) {
43             return 0;
44         }
45         /*
46          * Move the 0th digit 24 to the left, the first digit 16 to the left, the second digit 8 to the left,
47          * and the third digit no left
48          */
49         return (ptr[0] << 24) | (ptr[1] << 16) | (ptr[2] << 8) | (ptr[3]);
50     }
51 
FillDataOmxCodecBuffer(struct OmxCodecBuffer * dataFuzz)52     void FillDataOmxCodecBuffer(struct OmxCodecBuffer *dataFuzz)
53     {
54         dataFuzz->bufferId = DATA_BUFFERID;
55         dataFuzz->size = DATA_SIZE;
56         dataFuzz->version.nVersion = DATA_VERSION_NVERSION;
57         dataFuzz->bufferType = static_cast<enum CodecBufferType>(DATA_BUFFERTYPE);
58         dataFuzz->buffer = reinterpret_cast<uint8_t *>(OsalMemAlloc(DATA_BUFFERLEN));
59         if (dataFuzz->buffer == nullptr) {
60             HDF_LOGE("%{public}s: dataFuzz->buffer is nullptr", __func__);
61             return;
62         }
63         dataFuzz->bufferLen = DATA_BUFFERLEN;
64         dataFuzz->allocLen = DATA_ALLOCLEN;
65         dataFuzz->filledLen = DATA_FILLEDLEN;
66         dataFuzz->offset = DATA_OFFSET;
67         dataFuzz->fenceFd = DATA_FENCEFD;
68         dataFuzz->type = static_cast<enum ShareMemTypes>(DATA_TYPE);
69         dataFuzz->pts = DATA_PTS;
70         dataFuzz->flag = DATA_FLAG;
71     }
72 
Preconditions()73     bool Preconditions()
74     {
75         g_manager = GetCodecComponentManager();
76         if (g_manager == nullptr) {
77             HDF_LOGE("%{public}s: GetCodecComponentManager failed\n", __func__);
78             return false;
79         }
80 
81         g_callback = CodecCallbackTypeStubGetInstance();
82         if (g_callback == nullptr) {
83             HDF_LOGE("%{public}s: CodecCallbackTypeStubGetInstance failed\n", __func__);
84             return false;
85         }
86 
87         std::string compName("OMX.rk.video_encoder.avc");
88         int32_t ret = g_manager->CreateComponent(&g_component, &g_componentId, compName.data(), g_appData, g_callback);
89         if (ret != HDF_SUCCESS) {
90             CodecCallbackTypeRelease(g_callback);
91             HDF_LOGE("%{public}s: CreateComponent failed\n", __func__);
92             return false;
93         }
94 
95         return true;
96     }
97 
Destroy()98     bool Destroy()
99     {
100         int32_t ret = g_manager->DestroyComponent(g_componentId);
101         if (ret != HDF_SUCCESS) {
102             HDF_LOGE("%{public}s: DestroyComponent failed\n", __func__);
103             return false;
104         }
105         CodecCallbackTypeRelease(g_callback);
106         CodecComponentTypeRelease(g_component);
107         CodecComponentManagerRelease();
108         return true;
109     }
110 } // namespace codec
111 } // namespace OHOS
112