• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright (c) 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 #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 "videoenc_sample.h"
22 #include "native_avcapability.h"
23 using namespace std;
24 using namespace OHOS;
25 using namespace OHOS::Media;
26 #define FUZZ_PROJECT_NAME "encoderconfigure_fuzzer"
27 
RunNormalEncoder()28 void RunNormalEncoder()
29 {
30     VEncFuzzSample *vEncSample = new VEncFuzzSample();
31     vEncSample->inpDir = "/data/test/media/1280_720_nv.yuv";
32     OH_AVCapability *cap = OH_AVCodec_GetCapabilityByCategory("video/avc", true, HARDWARE);
33     string tmpCodecName = OH_AVCapability_GetName(cap);
34     int32_t ret = vEncSample->CreateVideoEncoder(tmpCodecName.c_str());
35     if (ret != 0) {
36         delete vEncSample;
37         return;
38     }
39     vEncSample->SetVideoEncoderCallback();
40     vEncSample->ConfigureVideoEncoder();
41     vEncSample->StartVideoEncoder();
42     vEncSample->WaitForEOS();
43     delete vEncSample;
44 }
45 bool g_needRunNormalEncoder = true;
46 namespace OHOS {
EncoderConfigureFuzzTest(const uint8_t * data,size_t size)47 bool EncoderConfigureFuzzTest(const uint8_t *data, size_t size)
48 {
49     if (size < sizeof(int32_t)) {
50         return false;
51     }
52     if (g_needRunNormalEncoder) {
53         g_needRunNormalEncoder = false;
54         RunNormalEncoder();
55     }
56     bool result = false;
57     int32_t data_ = *reinterpret_cast<const int32_t *>(data);
58     VEncFuzzSample *vEncSample = new VEncFuzzSample();
59     vEncSample->inpDir = "/data/test/media/1280_720_nv.yuv";
60     OH_AVCapability *cap = OH_AVCodec_GetCapabilityByCategory("video/avc", true, HARDWARE);
61     string tmpCodecName = OH_AVCapability_GetName(cap);
62     int32_t ret = vEncSample->CreateVideoEncoder(tmpCodecName.c_str());
63     if (ret != 0) {
64         delete vEncSample;
65         return true;
66     }
67     vEncSample->SetVideoEncoderCallback();
68     vEncSample->fuzzMode = true;
69     vEncSample->ConfigureVideoEncoderFuzz(data_);
70     vEncSample->StartVideoEncoder();
71     vEncSample->WaitForEOS();
72     delete vEncSample;
73     return result;
74 }
75 } // namespace OHOS
76 
77 /* Fuzzer entry point */
LLVMFuzzerTestOneInput(const uint8_t * data,size_t size)78 extern "C" int LLVMFuzzerTestOneInput(const uint8_t *data, size_t size)
79 {
80     /* Run your code on data */
81     OHOS::EncoderConfigureFuzzTest(data, size);
82     return 0;
83 }
84