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