/*
 * Copyright (c) 2025 Huawei Device Co., Ltd.
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *     http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
#include <cstddef>
#include <cstdint>
#include "native_avcodec_base.h"
#include "native_avformat.h"
#include "videodec_sample.h"
#include <fuzzer/FuzzedDataProvider.h>

#define FUZZ_PROJECT_NAME "msvideo1decodersetparameter_fuzzer"

using namespace std;
using namespace OHOS;
using namespace OHOS::Media;

static VDecFuzzSample *vDecSample = nullptr;
constexpr uint32_t DEFAULT_WIDTH = 1920;
constexpr uint32_t DEFAULT_HEIGHT = 1080;
constexpr double DEFAULT_FRAME_RATE = 30.0;

OH_AVFormat *format = nullptr;

void SetFormatValue(const uint8_t *data, size_t size)
{
    format = OH_AVFormat_CreateVideoFormat("video/msvideo1", DEFAULT_WIDTH, DEFAULT_HEIGHT);
    FuzzedDataProvider fdp(data, size);

    int intData0 = fdp.ConsumeIntegral<int32_t>();
    int intData1 = fdp.ConsumeIntegral<int32_t>();
    int intData2 = fdp.ConsumeIntegral<int32_t>();
    int intData3 = fdp.ConsumeIntegral<int32_t>();
    int intData4 = fdp.ConsumeIntegral<int32_t>();
    int intData5 = fdp.ConsumeIntegral<int32_t>();
    int intData6 = fdp.ConsumeIntegral<int32_t>();
    int intData7 = fdp.ConsumeIntegral<int32_t>();
    int intData8 = fdp.ConsumeIntegral<int32_t>();
    int longData = fdp.ConsumeIntegral<int64_t>();
    double doubleData = fdp.ConsumeFloatingPoint<double>();
    
    OH_AVFormat_SetIntValue(format, OH_MD_KEY_BITRATE, intData0);
    OH_AVFormat_SetIntValue(format, OH_MD_KEY_MAX_INPUT_SIZE, intData1);
    OH_AVFormat_SetIntValue(format, OH_MD_KEY_WIDTH, intData2);
    OH_AVFormat_SetIntValue(format, OH_MD_KEY_HEIGHT, intData3);
    OH_AVFormat_SetIntValue(format, OH_MD_KEY_PIXEL_FORMAT, intData4);
    OH_AVFormat_SetIntValue(format, OH_MD_KEY_VIDEO_ENCODE_BITRATE_MODE, intData5);
    OH_AVFormat_SetIntValue(format, OH_MD_KEY_PROFILE, intData6);
    OH_AVFormat_SetIntValue(format, OH_MD_KEY_I_FRAME_INTERVAL, intData7);
    OH_AVFormat_SetIntValue(format, OH_MD_KEY_ROTATION, intData8);
    OH_AVFormat_SetLongValue(format, OH_MD_KEY_DURATION, longData);
    OH_AVFormat_SetDoubleValue(format, OH_MD_KEY_FRAME_RATE, doubleData);
}

namespace OHOS {
bool MsVideo1decoderSetParameterFuzzTest(const uint8_t *data, size_t size)
{
    if (size < sizeof(int64_t)) {
        return false;
    }
    if (!vDecSample) {
        vDecSample = new VDecFuzzSample();
        vDecSample->defaultWidth = DEFAULT_WIDTH;
        vDecSample->defaultHeight = DEFAULT_HEIGHT;
        vDecSample->defaultFrameRate = DEFAULT_FRAME_RATE;
        vDecSample->isSurfMode = true;
        
        if (vDecSample->CreateVideoDecoder("OH.Media.Codec.Decoder.Video.MSVIDEO1") != AV_ERR_OK) {
            delete vDecSample;
            vDecSample = nullptr;
            return false;
        }
        if (vDecSample->ConfigureVideoDecoder() != AV_ERR_OK) {
            delete vDecSample;
            vDecSample = nullptr;
            return false;
        }
        if (vDecSample->SetVideoDecoderCallback() != AV_ERR_OK) {
            delete vDecSample;
            vDecSample = nullptr;
            return false;
        }
        if (vDecSample->Start() != AV_ERR_OK) {
            delete vDecSample;
            vDecSample = nullptr;
            return false;
        }
    }

    SetFormatValue(data, size);
    vDecSample->SetParameter(format);
    OH_AVFormat_Destroy(format);
    delete vDecSample;
    vDecSample = nullptr;
    return true;
}
} // namespace OHOS

/* Fuzzer entry point */
extern "C" int LLVMFuzzerTestOneInput(const uint8_t *data, size_t size)
{
    /* Run your code on data */
    OHOS::MsVideo1decoderSetParameterFuzzTest(data, size);
    return 0;
}