/*
 * Copyright (C) 2023 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.
 */

#ifndef VIDEOENC_API11_SAMPLE_H
#define VIDEOENC_API11_SAMPLE_H

#include <iostream>
#include <cstdio>
#include <unistd.h>
#include <atomic>
#include <fstream>
#include <thread>
#include <mutex>
#include <queue>
#include <string>
#include <unordered_map>
#include "securec.h"
#include "native_avcodec_videoencoder.h"
#include "nocopyable.h"
#include "native_avbuffer.h"
#include "native_avformat.h"
#include "native_averrors.h"
#include "window.h"
#include "media_description.h"
#include "av_common.h"
#include "external_window.h"
#include "native_buffer.h"
#include "native_avcapability.h"
namespace OHOS {
namespace Media {
class VEncAPI11Signal {
public:
    std::mutex inMutex_;
    std::mutex outMutex_;
    std::condition_variable inCond_;
    std::condition_variable outCond_;
    std::queue<uint32_t> inIdxQueue_;
    std::queue<uint32_t> outIdxQueue_;
    std::queue<OH_AVBuffer *> inBufferQueue_;
    std::queue<OH_AVBuffer *> outBufferQueue_;
    std::queue<bool> isPrimaryQueue_;
};
struct fileInfo {
    std::string fileDir;
    OH_NativeBuffer_Format format;
    uint32_t width;
    uint32_t height;
};

typedef struct LtrTestParameter {
    uint32_t ltrInterval = 0;
    bool enableUseLtr = false;
    bool useBadLtr = false;
    bool useLtrOnce = false;
    uint32_t useLtrIndex = 0;
    bool markAndUseSelf = false;
    int32_t ltrCount = 0;
}LtrTestParameter;

typedef struct PreProcessParameter {
    bool configEnable = false;
    bool setParameterEnable = false;
    bool enableCrop = false;
    bool enableDFrame = false;
    bool enableDownsampling = false;
    int32_t cropLeft = 0;
    int32_t cropRight = 0;
    int32_t cropTop = 0;
    int32_t cropBottom = 0;
    int32_t downsamplingWidth = 0;
    int32_t downsamplingHeight = 0;
    double dropFrameRate = 0.00;
    int32_t cropNum = 4;
    int32_t downsamplingNum = 2;
    bool dropFramePtsSame = false;
}PreProcessParameter;

typedef struct Parameter {
    bool enableSwitchPrimary = false;
    bool enableSwitchSecondary = false;
    bool enableSwitchMode = false;
    bool enableSwitchParame = false;
    bool enableSwitchBitParame = false;
    std::vector<uint32_t> bitrateMode = {};
    int32_t sqrFactor = 0;
    int64_t maxBitrate = 0;
    uint32_t cbrBitrate = 0;
    uint32_t vbrBitrate = 0;
    uint32_t cbrHighQuality = 0;
    OH_HEVCProfile hevcProfile = HEVC_PROFILE_MAIN;
}Parameter;

class VEncAPI11Sample : public NoCopyable {
public:
    VEncAPI11Sample() = default;
    ~VEncAPI11Sample();
    const char *INP_DIR = "/data/test/media/1280_720_nv.yuv";
    const char *OUT_DIR = "/data/test/media/VEncTest.h264";
    const char *OUT_DIR_SEC = "/data/test/media/VEncTest2.h264";
    const char *roiInfo = "0,0-128,64=-4;100,200-300,400=3";
    uint32_t DEFAULT_WIDTH = 1280;
    uint32_t DEFAULT_HEIGHT = 720;
    uint32_t DEFAULT_BITRATE = 5000000;
    uint32_t DEFAULT_QUALITY = 30;
    double DEFAULT_FRAME_RATE = 30.0;
    bool isAVCEncoder = true;
    OH_HEVCProfile hevcProfile = HEVC_PROFILE_MAIN;
    OH_AVCProfile avcProfile = AVC_PROFILE_BASELINE;
    int32_t DEFAULT_QP = 20;
    uint32_t DEFAULT_BITRATE_MODE = CBR;
    int32_t DEFAULT_PROFILE = HEVC_PROFILE_MAIN;
    OH_AVPixelFormat DEFAULT_PIX_FMT = AV_PIXEL_FORMAT_NV12;
    uint32_t DEFAULT_KEY_FRAME_INTERVAL = 1000;
    uint32_t DEFAULT_RANGE_FLAG = 0;
    uint32_t DEFAULT_COLOR_PRIMARIES = COLOR_PRIMARY_BT709;
    uint32_t DEFAULT_TRANSFER_CHARACTERISTICS = TRANSFER_CHARACTERISTIC_BT709;
    uint32_t DEFAULT_MATRIX_COEFFICIENTS = MATRIX_COEFFICIENT_BT709;
    uint32_t GOP_REFERENCE_MODE = UNIFORMLY_SCALED_REFERENCE;
    uint32_t repeat_time = 0;
    bool runDualEncoder = false;
    PreProcessParameter preProcessParameter;
    Parameter parameter;
    int32_t CreatePrimaryEncoder(const char *codecMime);
    int32_t CreateSecondaryEncoder();
    int32_t SwitchPrimaryEncoder();
    int32_t SwitchSecondaryEncoder();
    int32_t StartSecondaryEncoder();
    int32_t ReleaseSecondary();
    int32_t NotifyEndOfStream();
    int32_t FlushSecondary();
    int32_t ResetSecondary();
    int32_t NotifyEndOfStreamSecondary();
    int32_t StopSecondary();
    int32_t FlushVideoEncode();
    int32_t SetDualCallback();
    int32_t SetCallback();
    void SwitchParame(size_t index, OH_AVCodec *venc);
    void SetPreProcessConfigure(OH_AVFormat *format);
    void SetPreProcessParameter();
    int32_t CreateVideoEncoder(const char *codecName);
    int32_t ConfigureVideoEncoder();
    int32_t ConfigureVideoEncoder_Temporal(int32_t temporal_gop_size);
    int32_t ConfigureVideoEncoder_fuzz(int32_t data);
    int32_t SetVideoEncoderCallback();
    int32_t CreateSurface();
    int32_t StartVideoEncoder();
    int32_t VideoEncoder();
    int32_t SetParameter(OH_AVFormat *format);
    void SetBufferParameter(OH_AVBuffer *buffer);
    void SetLTRParameter(OH_AVBuffer *buffer);
    void SetForceIDR();
    void GetStride();
    void testApi();
    void WaitForEOS();
    int32_t SyncOutputFuncEos(uint32_t &last_index, uint32_t &outFrames, uint32_t &index,
        OH_AVBuffer *buffer, OH_AVCodecBufferAttr &attr);
    int32_t OpenFile();
    uint32_t ReturnZeroIfEOS(uint32_t expectedSize);
    int64_t GetSystemTimeUs();
    int32_t Start();
    int32_t Flush();
    int32_t Reset();
    int32_t Stop();
    int32_t Release();
    int32_t ReadFile(uint32_t index, OH_AVBuffer *buffer);
    void Flush_buffer();
    void AutoSwitchParam();
    void RepeatStartBeforeEOS();
    bool RandomEOS(uint32_t index);
    void SetEOS(uint32_t index, OH_AVBuffer *buffer);
    int32_t PushData(OH_AVBuffer *buffer, uint32_t index, int32_t &result);
    int32_t CheckResult(bool isRandomEosSuccess, int32_t pushResult);
    void InputFunc();
    void SyncInputFunc();
    int32_t state_EOS();
    void InputFuncSurface();
    uint32_t ReadOneFrameYUV420SP(uint8_t *dst);
    uint32_t ReadOneFrameRGBA8888(uint8_t *dst);
    uint32_t ReadOneFrameYUVP010(uint8_t *dst);
    uint32_t ReadOneFrameFromList(uint8_t *dst, int32_t &fileIndex);
    uint32_t ReadOneFrameByType(uint8_t *dst, OH_NativeBuffer_Format format);
    int32_t OpenFileFail();
    int32_t CheckAttrFlag(OH_AVCodecBufferAttr attr);
    void OutputFuncFail();
    void OutputFunc();
    void SyncOutputFunc();
    uint32_t FlushSurf(OHNativeWindowBuffer *ohNativeWindowBuffer, OH_NativeBuffer *nativeBuffer);
    int32_t RequestWindowBuffer(uint32_t time);
    void ReleaseSignal();
    void ReleaseInFile();
    void StopInloop();
    void StopOutloop();
    void DumpLtrInfo(OH_AVBuffer *buffer);
    void DumpQPInfo(OH_AVBuffer *buffer);
    void DumpInfo(OH_AVCodecBufferAttr attr, OH_AVBuffer *buffer);
    void readMultiFilesFunc();
    int32_t InitBuffer(OHNativeWindowBuffer *&ohNativeWindowBuffer, OH_NativeBuffer *&nativeBuffer, uint8_t *&dst);
    void InputEnableRepeatSleep();
    int32_t QueryInputBuffer(uint32_t index, int64_t timeoutUs);
    int32_t QueryOutputBuffer(uint32_t index, int64_t timeoutUs);
    OH_AVBuffer *GetInputBuffer(uint32_t index);
    OH_AVBuffer *GetOutputBuffer(uint32_t index);
    void GetInputDescription();
    int32_t PushInputParameter(uint32_t index);
    void SetConfigureEnc(OH_AVFormat *format);
    void FlushStatus();
    void GetVideoSupportedPixelFormats();
    void GetFormatKey();
    int32_t PushInputBuffer(uint32_t index);
    int32_t SetRoi(OH_NativeBuffer *nativeBuffer);
    int32_t ReadFileData(uint8_t *dst, OHNativeWindowBuffer *ohNativeWindowBuffer, int32_t &readFileIndex);
    void FreeOutputBuffer(FILE *outFile, FILE *outFileSec, OH_AVBuffer *buffer, uint32_t index, int size,
        bool isPrimary);
    void SetParame(OH_AVCodec *venc);
    uint32_t SetNativeWindowFormat(int32_t &index);
    bool isGetVideoSupportedPixelFormats = false;
    bool isGetFormatKey = false;
    int isGetVideoSupportedPixelFormatsNum = 0;
    int isGetFormatKeyNum = 0;
    const char *avcodecMimeType = nullptr;
    bool isEncoder = true;
    const OH_NativeBuffer_Format *pixlFormats = nullptr;
    uint32_t pixlFormatNum = 0;
    int firstCallBackKey = 0;
    int onStreamChangedKey = 0;
    VEncAPI11Signal *signal_;
    uint32_t errCount = 0;
    bool enableForceIDR = false;
    uint32_t outCount = 0;
    uint32_t frameCount = 0;
    uint32_t switchParamsTimeSec = 3;
    uint32_t temporalGopSize = 4;
    bool sleepOnFPS = false;
    bool SURF_INPUT = false;
    bool enableAutoSwitchParam = false;
    bool enableColorSpaceParams = false;
    bool enableAutoSwitchBufferParam = false;
    bool needResetBitrate = false;
    bool needResetFrameRate = false;
    bool needResetQP = false;
    bool enableQP = false;
    bool repeatRun = false;
    bool showLog = false;
    bool enableLTR = false;
    bool enableLowLatency = false;
    bool getInputBufferIndexRepeat = false;
    bool abnormalIndexValue = false;
    bool getOutputBufferIndexNoExisted = false;
    int64_t encode_count = 0;
    bool enable_random_eos = false;
    uint32_t REPEAT_START_STOP_BEFORE_EOS = 0;  // 1200 测试用例
    uint32_t REPEAT_START_FLUSH_BEFORE_EOS = 0; // 1300 测试用例
    int64_t start_time = 0;
    int64_t end_time = 0;
    LtrTestParameter ltrParam;
    bool TEMPORAL_CONFIG = false;
    bool TEMPORAL_ENABLE = false;
    bool TEMPORAL_JUMP_MODE = false;
    bool TEMPORAL_DEFAULT = false;
    bool TEMPORAL_UNIFORMLY = false;
    bool ENABLE_GOP_SIZE = false;
    bool TEMPORAL_ENABLE_RUN = false;
    bool getQpMse = false;
    std::vector<fileInfo> fileInfos;
    bool readMultiFiles = false;
    bool setFormatRbgx = false;
    bool configMain = false;
    bool configMain10 = false;
    bool setFormat8Bit = false;
    bool setFormat10Bit = false;
    bool enableRepeat = false;
    bool enableSeekEos = false;
    bool setMaxCount = false;
    uint32_t repeatHeaderXPS = 0;
    int32_t maxFrameDelayCount = 5;
    int32_t DEFAULT_FRAME_AFTER = 1;
    int32_t DEFAULT_MAX_COUNT = 1;
    uint32_t inCount = 0;
    int32_t enbleSyncMode = 0;
    int32_t enableBFrameMode = 0;
    int32_t layerId = 0;
    int64_t syncInputWaitTime = -1;
    int64_t syncOutputWaitTime = -1;
    bool queryOutputBufferEOS = false;
    bool queryInputBufferEOS = false;
    bool getOutputBufferIndexRepeated = false;
    bool noDestroy = false;
    std::atomic<bool> isRunning_ { false };
    bool isSurface = false;
    OH_AVCodec *vencPrimary;
    OH_AVCodec *vencSecondary;
    int32_t DEFAULT_SQR_FACTOR = 30;
    int64_t DEFAULT_MAX_BITERATE = 4000000;
    bool enableRoi = false;
    bool setParameterFail = false;
    bool dualEos = false;
    bool repeatCreateSecondary = false;
    bool needStopInLoop = false;
    bool noStop = false;
    bool enableDualRoi = false;
private:
    std::unique_ptr<std::ifstream> inFile_;
    std::unique_ptr<std::thread> inputLoop_;
    std::unique_ptr<std::thread> outputLoop_;
    std::unordered_map<uint32_t, OH_AVBuffer *> inBufferMap_;
    std::unordered_map<uint32_t, OH_AVBuffer *> outBufferMap_;
    OH_AVCodec *venc_;
    OH_AVCodecCallback cb_;
    int64_t timeStamp_ { 0 };
    int64_t lastRenderedTimeUs_ { 0 };
    bool isFirstFrame_ = true;
    OHNativeWindow *nativeWindow;
    int stride_;
    static constexpr uint32_t SAMPLE_RATIO = 2;
};
} // namespace Media
} // namespace OHOS

#endif // VIDEOENC_API11_SAMPLE_H