#ifndef VMF_FEC_LAYOUT_H
#define VMF_FEC_LAYOUT_H

#include <stdint.h>
#include <vmf/video_source.h>

/* Opaque FEC handle */
typedef struct VMF_FEC_HANDLE_S VMF_FEC_HANDLE_T;
typedef struct VMF_FEC_INFO_S   VMF_FEC_INFO_T;

/* FEC processing method */
typedef enum {
    VMF_FEC_METHOD_GTR = 0,
    VMF_FEC_METHOD_CGE,
    VMF_FEC_METHOD_MAX
} VMF_FEC_METHOD;

/* FEC grid size (real values match SDK: 4/8/16/32) */
typedef enum {
    VMF_FEC_GRID_4X4    =   4,
    VMF_FEC_GRID_8X8    =   8,
    VMF_FEC_GRID_16X16  =  16,
    VMF_FEC_GRID_32X32  =  32,
    VMF_FEC_GRID_64X64  =  64,
    VMF_FEC_GRID_128X128= 128,
} VMF_FEC_GRID_SIZE_TYPE;
typedef VMF_FEC_GRID_SIZE_TYPE VMF_FEC_GRID_SIZE_E;

/* FEC coefficient mode */
typedef enum {
    VMF_FEC_COEF_MODE_ORIG = 0,
    VMF_FEC_COEF_MODE_AREA,
    VMF_FEC_COEF_MODE_PTZ,
    VMF_FEC_COEF_MODE_P360,
    VMF_FEC_COEF_MODE_P180,
    VMF_FEC_COEF_MODE_VR,
    VMF_FEC_COEF_MODE_OBJ,
    VMF_FEC_COEF_MODE_DUAL,
    VMF_FEC_COEF_MODE_PT,
    VMF_FEC_COEF_MODE_NULL,
    VMF_FEC_COEF_MODE_NUM
} VMF_FEC_COEF_MODE;

/* FEC mode type (used by P180 / PTZ config structs) */
typedef enum {
    VMF_FEC_MODE_AREA = 0,
    VMF_FEC_MODE_PTZ_MODE,
    VMF_FEC_MODE_PANO_180_ALL_DIRECTION,
    VMF_FEC_MODE_PANO_180_ONE_DIRECTION,
    VMF_FEC_MODE_PANO_180_TWO_DIRECTION,
    VMF_FEC_MODE_PANO_360_FULL,
    VMF_FEC_MODE_PANO_360_SEPE,
    VMF_FEC_MODE_PANO_360_HALF,
    VMF_FEC_MODE_LDC,
    VMF_FEC_MODE_ORI,
    VMF_FEC_MODE_VR_SPHERE,
    VMF_FEC_MODE_VR_CYLINDER,
    VMF_FEC_MODE_OBJECT,
    VMF_FEC_MODE_PT = 14
} VMF_FEC_MODE_TYPE;

/* FEC app type */
typedef enum {
    VMF_FEC_APP_CEILING = 0,
    VMF_FEC_APP_TABLE   = 1,
    VMF_FEC_APP_WALL    = 2,
    VMF_FEC_APP_LDC,
    VMF_FEC_APP_P720,
    VMF_FEC_APP_DVS,
    VMF_FEC_APP_CGE
} VMF_FEC_APP_TYPE;
typedef VMF_FEC_APP_TYPE VMF_FEC_APP_TYPE_E;

/* FEC lens type */
typedef enum {
    VMF_FEC_LENS_STEREOGRAPHIC  = 0,
    VMF_FEC_LENS_EQUISOLIDANGLE,
    VMF_FEC_LENS_EQUIDISTANT,
    VMF_FEC_LENS_ORTHOGRAPHIC,
    VMF_FEC_LENS_LDC,
    VMF_FEC_LENS_NODISTORT,
    VMF_FEC_LENS_USER_DEF
} VMF_FEC_LENS_TYPE;

/* Extra perspective-transform points (mirrors Extra_PT in fec_api.h) */
typedef struct {
    unsigned int bExtraPtEn;
    unsigned int dwX[4];
    unsigned int dwY[4];
} VMF_FEC_EXTRA_PT_T;

/* Encoder specification */
typedef struct {
    unsigned int bEncH264;
    unsigned int bEncH265;
    unsigned int bEncJPEG;
    unsigned int bOthers;
} VMF_ENC_SPEC_T;

/* FEC layout config — full field layout from SDK fec_layout.h */
typedef struct {
    unsigned int         dwOutputId;
    VMF_LAYOUT_T         tLayout;
    unsigned int         bCoeffOnly;
    VMF_FEC_GRID_SIZE_E  eGridSize;
    unsigned int         dwClearBackColor;
    VMF_FEC_METHOD       eLayoutMethod;
    VMF_ENC_SPEC_T       tEncSpec;
    unsigned int         bDuplexMode;
    unsigned int         bEisMode;
} VMF_FEC_LYT_CONFIG_T;

/* FEC cell config — full field layout from SDK fec_layout.h */
typedef struct {
    VMF_FEC_COEF_MODE    eFecMode;
    void                *pFecConfig;
    unsigned int         dwFlag;
    unsigned int         dwIspLinePixels;
    unsigned int         bEisMode;
} VMF_FEC_CELL_CONFIG_T;

/* FEC original (passthrough) config */
typedef struct {
    float                fZoom;
    float                fDstOffsetX;
    float                fDstOffsetY;
    float                fDstXYRatio;
    VMF_FEC_APP_TYPE     eAppType;
    void                *ptRoi;
    unsigned int         dwOutRadius;
    unsigned int         eFECRotateType;
    VMF_FEC_EXTRA_PT_T   tExtraPt;
    unsigned int         dwSrcRadius;
} VMF_FEC_ORIG_CONFIG_T;

/* FEC PTZ (pan-tilt-zoom) config */
typedef struct {
    float                fPan;
    float                fTilt;
    float                fZoom;
    float                fFocalLength;
    VMF_FEC_APP_TYPE     eAppType;
    VMF_FEC_MODE_TYPE    eModeType;
    VMF_FEC_LENS_TYPE    eLensType;
    void                *ptRoi;
    unsigned int         dwOutRadius;
    float                fDstOffsetX;
    float                fDstOffsetY;
    VMF_FEC_EXTRA_PT_T   tExtraPt;
    float                fDstRotate;
    unsigned int         eFECRotateType;
} VMF_FEC_PTZ_CONFIG_T;

/* FEC panorama-180 config */
typedef struct {
    float                fTilt;
    float                fZoom;
    float                fSpin;
    float                fFocalLength;
    float                fRectCurvature;
    float                fRectSlope;
    float                fDstOffsetX;
    float                fDstOffsetY;
    float                fDstXYRatio;
    unsigned int         eFECRotateType;
    VMF_FEC_MODE_TYPE    eModeType;
    VMF_FEC_LENS_TYPE    eLensType;
    void                *ptRoi;
    unsigned int         dwOutRadius;
    unsigned int         dwOptionFlags;
    VMF_FEC_EXTRA_PT_T   tExtraPt;
} VMF_FEC_P180_CONFIG_T;

/* FEC projection-transform (PT) config */
typedef struct {
    void                *pCoeffData;
    unsigned int         dwCoeffDataSize;
    void                *pComplexCoeffData;
    unsigned int         dwComplexCoeffDataSize;
    void                *pMrfCoeffData;
    unsigned int         dwMrfCoeffDataSize;
} VMF_FEC_PT_CONFIG_T;

/* FEC APIs (real implementations are in libvmf.so on device) */
int VMF_FEC_Open(VMF_FEC_HANDLE_T **handle, const char *cal_path, const char *conf_path);
int VMF_FEC_Close(VMF_FEC_HANDLE_T *handle);
int VMF_FEC_SetMode(VMF_FEC_HANDLE_T *handle, unsigned int mode, unsigned int app_type);
int VMF_FEC_LYT_Single(VMF_VSRC_HANDLE_T *vsrc, VMF_FEC_LYT_CONFIG_T *lyt, VMF_FEC_CELL_CONFIG_T *cell);
int VMF_FEC_LYT_Single_Ext(VMF_VSRC_HANDLE_T *vsrc, VMF_FEC_LYT_CONFIG_T *lyt, VMF_FEC_CELL_CONFIG_T *cell,
                            unsigned int adwRectangles[2], unsigned int adwOffsets[2]);

VMF_FEC_INFO_T *VMF_LYT_FEC_Info_Init(VMF_VSRC_HANDLE_T *vsrc, VMF_FEC_LYT_CONFIG_T *lyt, VMF_FEC_CELL_CONFIG_T *cell);
int             VMF_LYT_FEC_Info_Release(VMF_FEC_INFO_T *info);

#endif /* VMF_FEC_LAYOUT_H */
