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#include "native_avcodec_base.h"
18#include "native_avformat.h"
19#include "videodec_sample.h"
20#define FUZZ_PROJECT_NAME "swdecodersetparameter_fuzzer"
21using namespace std;
22using namespace OHOS;
23using namespace OHOS::Media;
24static VDecFuzzSample *vDecSample = nullptr;
25constexpr uint32_t DEFAULT_WIDTH = 1920;
26constexpr uint32_t DEFAULT_HEIGHT = 1080;
27constexpr double DEFAULT_FRAME_RATE = 30.0;
28namespace OHOS {
29bool DoSomethingInterestingWithMyAPI(const uint8_t *data, size_t size)
30{
31    if (size < sizeof(int64_t)) {
32        return false;
33    }
34    if (!vDecSample) {
35        vDecSample = new VDecFuzzSample();
36        vDecSample->defaultWidth = DEFAULT_WIDTH;
37        vDecSample->defaultHeight = DEFAULT_HEIGHT;
38        vDecSample->defaultFrameRate = DEFAULT_FRAME_RATE;
39        vDecSample->isSurfMode = true;
40        vDecSample->CreateVideoDecoder("OH.Media.Codec.Decoder.Video.AVC");
41        vDecSample->ConfigureVideoDecoder();
42        vDecSample->SetVideoDecoderCallback();
43        vDecSample->Start();
44    }
45    OH_AVFormat *format = OH_AVFormat_CreateVideoFormat("video/avc", DEFAULT_WIDTH, DEFAULT_HEIGHT);
46    int32_t intData = *reinterpret_cast<const int32_t *>(data);
47    int64_t longData = *reinterpret_cast<const int64_t *>(data);
48    double doubleData = *reinterpret_cast<const double *>(data);
49    OH_AVFormat_SetIntValue(format, OH_MD_KEY_BITRATE, intData);
50    OH_AVFormat_SetIntValue(format, OH_MD_KEY_MAX_INPUT_SIZE, intData);
51    OH_AVFormat_SetIntValue(format, OH_MD_KEY_WIDTH, intData);
52    OH_AVFormat_SetIntValue(format, OH_MD_KEY_HEIGHT, intData);
53    OH_AVFormat_SetIntValue(format, OH_MD_KEY_PIXEL_FORMAT, intData);
54    OH_AVFormat_SetIntValue(format, OH_MD_KEY_VIDEO_ENCODE_BITRATE_MODE, intData);
55    OH_AVFormat_SetIntValue(format, OH_MD_KEY_PROFILE, intData);
56    OH_AVFormat_SetIntValue(format, OH_MD_KEY_I_FRAME_INTERVAL, intData);
57    OH_AVFormat_SetIntValue(format, OH_MD_KEY_ROTATION, intData);
58    OH_AVFormat_SetLongValue(format, OH_MD_KEY_DURATION, longData);
59    OH_AVFormat_SetDoubleValue(format, OH_MD_KEY_FRAME_RATE, doubleData);
60
61    vDecSample->SetParameter(format);
62
63    OH_AVFormat_Destroy(format);
64    return true;
65}
66} // namespace OHOS
67
68/* Fuzzer entry point */
69extern "C" int LLVMFuzzerTestOneInput(const uint8_t *data, size_t size)
70{
71    /* Run your code on data */
72    OHOS::DoSomethingInterestingWithMyAPI(data, size);
73    return 0;
74}
75