/*
 *******************************************************************************
 *  Copyright (c) 2010-2022 VATICS(KNERON) Inc. All rights reserved.
 *
 *  +-----------------------------------------------------------------+
 *  | THIS SOFTWARE IS FURNISHED UNDER A LICENSE AND MAY ONLY BE USED |
 *  | AND COPIED IN ACCORDANCE WITH THE TERMS AND CONDITIONS OF SUCH  |
 *  | A LICENSE AND WITH THE INCLUSION OF THE THIS COPY RIGHT NOTICE. |
 *  | THIS SOFTWARE OR ANY OTHER COPIES OF THIS SOFTWARE MAY NOT BE   |
 *  | PROVIDED OR OTHERWISE MADE AVAILABLE TO ANY OTHER PERSON. THE   |
 *  | OWNERSHIP AND TITLE OF THIS SOFTWARE IS NOT TRANSFERRED.        |
 *  |                                                                 |
 *  | THE INFORMATION IN THIS SOFTWARE IS SUBJECT TO CHANGE WITHOUT   |
 *  | ANY PRIOR NOTICE AND SHOULD NOT BE CONSTRUED AS A COMMITMENT BY |
 *  | VATICS(KNERON) INC.                                             |
 *  +-----------------------------------------------------------------+
 *
 *******************************************************************************
 */

/**@addtogroup  HOST_MIPI
 * @{
 * @brief       Kneron application init
 * @copyright   Copyright (C) 2022 Kneron, Inc. All rights reserved.
 */
#ifndef KDP2_APP_HOST_STREAM_H
#define KDP2_APP_HOST_STREAM_H

#include <stdbool.h>
#include <pthread.h>
#include <vmf/video_encoder.h>
#include <vmf/video_encoder_output_srb.h>
#include <vmf/video_encoder_output_scm.h>
#include <vmf/vector_dma.h>
#include <vmf/ssm_info.h>

#define MAX_STREAM                  3
#define MAX_RESULT_BOX              80
#define IMU_CONFIG_PATH             "/sys/bus/iio/devices/iio:device1/buffer/enable"

typedef struct
{
    VMF_VENC_HANDLE_T *ptHandle;
    VMF_VENC_OUT_SRB_T *ptSRB;
    VMF_VENC_OUT_SCM_T *ptSCM;
    unsigned int dwAliveCount;
    VMF_VENC_CODEC_TYPE eCodecType;
    unsigned int dwWidth;
    unsigned int dwHeight;
    unsigned int dwQp;
    unsigned int dwFps;
    unsigned int dwBitrate;
    unsigned int dwGop;
    unsigned int dwVirtIFrameInterval;
    unsigned int dwPiq;
    unsigned int dwKeepRatio;
    unsigned int dwEncodeBufferSize;
    unsigned int dwEncodeBufferAmount;
} VEncoder;

typedef enum
{
	SRB,
	SCM
} MEM_TYPE;

typedef struct
{
    VMF_DMA_HANDLE_T* ptDmaHandle;
    VMF_DMA_DESCRIPTOR_T* ptDmaDesc;
} DMA_INFO_T;

typedef struct
{
    char* pszSensorConfigPath;          //! Path of sensor config file
    char* pszAutoSceneConfigPath;       //! Path of autoscene_config_file config file
    char* pszFusionConfigPath;          //! Path of sensor config file for fusion mode
    char* pszFecCalibratePath;          //! Path of fec calibrate config file
    char* pszFecConfPath;               //! Path of fec config file
    unsigned int dwFecMode;             //! Fec Mode
    unsigned int dwFecAppType;          //! Fec App Type
    unsigned int dwOnlyPerson;          //! Flag of drawing only person box
    unsigned int dwEisEnable;           //! Enable EIS

    //! NNM host settings
    char* pszModelPath;                 //! Path of NN model file
    unsigned int dwJobId;               //e.g. KDP2_INF_ID_APP_YOLO;
    unsigned int dwModelId;
    unsigned int dwInferenceStream;     //! Inference stream index
    float fThreshold;                   //! for post process
    float fFps;                         //! feed image detect fps
    unsigned int dwGetImageBufMode;     //! 0: block mode 1: non-block mode
    unsigned int bRoiEnable;            //! Enable ROI for model detect
    unsigned int dwRoiX;                //! ROI start x
    unsigned int dwRoiY;                //! ROI start y
    unsigned int bDrawBoxEnable;        //! 0: Disable draw box
    unsigned int dwDrawOnResize;        //! Flag of drawing on resize streams

    unsigned int dwNnmSource;        //! 0: run host_stream independently, 1: run with streamer
    char* pszSsmName;                 //! Name of Nnm source SSM name

    //! Video settings
    unsigned int dwEncodeStreamCount;
    MEM_TYPE eMemType;
    VEncoder tVEncoder[MAX_STREAM];

    //! Voc settings
    unsigned int dwVocEnable;           //! Enable Voc
    unsigned int dwVocWidth;            //! Voc width
    unsigned int dwVocHeight;           //! Voc height

    DMA_INFO_T* ptDmaInfo;    //DMAInitOpt

    //! Used for suspend/resume
    pthread_mutex_t tSuspendpMutex;
	pthread_cond_t tSuspendCond;
	unsigned int bSuspend;
} HOST_STREAM_INIT_OPT_T;

/**
 * @struct	DETECT_INFO
 * @brief	The structure for DETECT_INFO
 */
typedef struct
{
    unsigned int dwStartX;
    unsigned int dwStartY;
    unsigned int dwWidth;
    unsigned int dwHeight;
    float fScore;
    unsigned int dwClass;
    bool bDrawFlag;
} DETECT_INFO;

extern DETECT_INFO g_atDrawInfo[MAX_RESULT_BOX];
extern unsigned int g_dwResultCounts;

DMA_INFO_T* dma2d_init();
void dma2d_release(DMA_INFO_T* dma_info);
int dma2d_copy(VMF_DMA_HANDLE_T* dma_handle, VMF_DMA_DESCRIPTOR_T* dma_desc, void* dest, void* source, VMF_VSRC_SSM_OUTPUT_INFO_T* vsrc_ssm_info);

void calculate_bbox_postion(DETECT_INFO *ptResults, float fLeftX, float fTopY, float fRightX, float fBottomY, float fScore, int iClass);
void setup_isp_privacy_mask(unsigned int dwEnable, unsigned int dwStartX, unsigned int dwStartY, unsigned int dwWidth, unsigned int dwHeight);

extern unsigned int g_dwDrawBoxEnable;
extern unsigned int g_dwOnlyPerson;
extern unsigned int g_dwVocEnable;
extern unsigned int g_dwVocPixFmt;

/* STDC segmentation map — shared between result thread and draw-box thread.
 * STDC_SEG_MAP_MAX must match the value in stdc_post_process.h */
#ifndef STDC_SEG_MAP_MAX
#define STDC_SEG_MAP_MAX 8192
#endif
extern uint8_t          g_stdc_seg_map[STDC_SEG_MAP_MAX];
extern uint32_t         g_stdc_seg_w;
extern uint32_t         g_stdc_seg_h;
extern pthread_mutex_t  g_stdc_seg_mutex;

#endif  //KDP2_APP_HOST_STREAM_H
