• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright (c) 2025 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 #include <cstddef>
16 #include <cstdint>
17 
18 #include "native_avcodec_videoencoder.h"
19 #include "native_averrors.h"
20 #include "native_avcodec_base.h"
21 #include "native_avcapability.h"
22 #include "syncvideoenc_sample.h"
23 #include <fuzzer/FuzzedDataProvider.h>
24 #include <fstream>
25 using namespace std;
26 using namespace OHOS;
27 using namespace OHOS::Media;
28 #define FUZZ_PROJECT_NAME "syncencoder_fuzzer"
29 
30 
31 OH_AVCapability *cap = nullptr;
32 string g_codeName = "";
33 VEncSyncSample *g_vEncSample = nullptr;
34 constexpr int32_t ONE = 1;
35 constexpr int32_t TWO = 2;
36 
37 
SaveCorpus(const uint8_t * data,size_t size,const std::string & filename)38 void SaveCorpus(const uint8_t *data, size_t size, const std::string& filename)
39 {
40     std::ofstream file(filename, std::ios::out | std::ios::binary);
41     if (file.is_open()) {
42         file.write(reinterpret_cast<const char*>(data), size);
43         file.close();
44     }
45 }
46 
GetCodeName(const char * mimeName,OH_AVCodecCategory category)47 string GetCodeName(const char* mimeName, OH_AVCodecCategory category)
48 {
49     cap = OH_AVCodec_GetCapabilityByCategory(mimeName, true, category);
50     if (cap == nullptr) {
51         return "";
52     }
53     return OH_AVCapability_GetName(cap);
54 }
55 
ReleaseSample()56 bool ReleaseSample()
57 {
58     delete g_vEncSample;
59     g_vEncSample = nullptr;
60     return false;
61 }
62 
CodeType()63 void CodeType()
64 {
65     if (g_vEncSample->codecType == ONE) {
66         g_codeName = GetCodeName(OH_AVCODEC_MIMETYPE_VIDEO_AVC, HARDWARE);
67     } else if (g_vEncSample->codecType == TWO) {
68         g_codeName = GetCodeName(OH_AVCODEC_MIMETYPE_VIDEO_HEVC, HARDWARE);
69     }
70 }
71 
72 namespace OHOS {
EncoderSyncFuzzTest(const uint8_t * data,size_t size)73 bool EncoderSyncFuzzTest(const uint8_t *data, size_t size)
74 {
75     if (size < sizeof(int32_t)) {
76         return false;
77     }
78     std::string filename = "/data/test/corpus-EncoderSyncFuzzTest";
79     SaveCorpus(data, size, filename);
80     FuzzedDataProvider fdp(data, size);
81     int data1 = fdp.ConsumeIntegral<int32_t>();
82     g_vEncSample = new VEncSyncSample();
83     g_vEncSample->codecType = fdp.ConsumeIntegralInRange<int32_t>(ONE, TWO);
84     CodeType();
85     bool isRgba1010102 = fdp.ConsumeBool();
86     if (isRgba1010102) {
87         g_vEncSample->DEFAULT_PIX_FMT = AV_PIXEL_FORMAT_RGBA1010102;
88     }
89     g_vEncSample->fuzzData = data;
90     g_vEncSample->fuzzSize = size;
91     g_vEncSample->surfInput = fdp.ConsumeBool();
92     g_vEncSample->fuzzMode = true;
93     g_vEncSample->enbleBFrameMode = fdp.ConsumeIntegral<int32_t>();
94     g_vEncSample->enbleSyncMode = 1;
95     g_vEncSample->syncInputWaitTime = fdp.ConsumeIntegral<int64_t>();
96     g_vEncSample->syncOutputWaitTime = 1;
97     g_vEncSample->enableRepeat = fdp.ConsumeBool();
98     g_vEncSample->setMaxCount = fdp.ConsumeBool();
99     g_vEncSample->defaultKeyFrameInterval = fdp.ConsumeIntegral<uint32_t>();
100     g_vEncSample->DEFAULT_BITRATE_MODE = fdp.ConsumeIntegral<uint32_t>();
101     g_vEncSample->defaultQuality = fdp.ConsumeIntegral<uint32_t>();
102     if (g_vEncSample->CreateVideoEncoder(g_codeName.c_str()) != AV_ERR_OK) {
103         return ReleaseSample();
104     }
105     if (g_vEncSample->ConfigureVideoEncoder() != AV_ERR_OK) {
106         return ReleaseSample();
107     }
108     if (g_vEncSample->surfInput) {
109         g_vEncSample->CreateSurface();
110     }
111     if (g_vEncSample->Start() != AV_ERR_OK) {
112         return ReleaseSample();
113     }
114     if (g_vEncSample->surfInput) {
115         g_vEncSample->InputFuncSurfaceFuzz();
116     } else {
117         g_vEncSample->SyncInputFuncFuzz();
118     }
119     g_vEncSample->SyncOutputFuncFuzz();
120     g_vEncSample->SetParameter(data1);
121     return ReleaseSample();
122 }
123 } // namespace OHOS
124 
125 /* Fuzzer entry point */
LLVMFuzzerTestOneInput(const uint8_t * data,size_t size)126 extern "C" int LLVMFuzzerTestOneInput(const uint8_t *data, size_t size)
127 {
128     /* Run your code on data */
129     OHOS::EncoderSyncFuzzTest(data, size);
130     return 0;
131 }
132