

#ifndef _VDEC_FRAMING_H_
#define _VDEC_FRAMING_H_

#include <linux/types.h>
//#define ENABLE_REE_PARSE_HEADER

typedef void *VES_PARSER_HANDLE;
#ifndef _EN_VES_PARSER_DV_TYPE_
#define _EN_VES_PARSER_DV_TYPE_
typedef enum {
	E_VES_PARSER_DV_BL = 0,
	E_VES_PARSER_DV_RPU = 1,
	E_VES_PARSER_DV_EL = 2,
} EN_VES_PARSER_DV_TYPE;
#endif
typedef enum {
	E_VES_PARSER_ALIGN_FRAME = 0,
	E_VES_PARSER_ALIGN_SLICE = 1,
} EN_VES_PARSER_ALIGNMENT;

#define I_TYPE                               0x01	///< I picture
#define P_TYPE                               0x02	///< P picture
#define B_TYPE                               0x03	///< B picture
#define UNKNOWN_TYPE                         0xFF

// for mpeg
#define MPEG2_PIC_TYPE_I                     I_TYPE	///< I picture
#define MPEG2_PIC_TYPE_P                     P_TYPE	///< P picture
#define MPEG2_PIC_TYPE_B                     B_TYPE	///< B picture
#define MPEG2_PIC_TYPE_SEQ_START             0x04	///< Sequence start
#define MPEG2_PIC_TYPE_GOP                   0x05	///< GOP
#define MPEG2_PIC_TYPE_SEQ_END               0x06	///< Sequence end

// for mpeg4
#define MPEG4_PIC_TYPE_I                     I_TYPE	///< I picture
#define MPEG4_PIC_TYPE_P                     P_TYPE	///< P picture
#define MPEG4_PIC_TYPE_B                     B_TYPE	///< B picture
#define MPEG4_PIC_TYPE_S                     0x04	///< S picture
#define MPEG4_PIC_TYPE_VOL_START             0x05	///< VOL start
#define MPEG4_PIC_TYPE_VOS_START             0x06	///< VOS start
#define MPEG4_PIC_TYPE_USER_DATA_START       0x07	///< User data
#define MPEG4_PIC_TYPE_OBJ_START             0x08	///< Obj start

// for h264
#define H264_NAL_TYPE_AUD 0x9
#define H264_NAL_TYPE_SPS 0x7
#define H264_NAL_TYPE_PPS 0x8
#define H264_NAL_TYPE_SEI 0x6
#define H264_NAL_TYPE_SLICE 0x1
#define H264_NAL_TYPE_IDR_SLICE 0x05

//NALU type
#define NON_IDR_SLICE 0x01
#define IDR_SLICE 0x05
#define CODED_SLICE_EXT 0x14    // 20

// for hevc
#define HEVC_NAL_TYPE_AUD 0x23
#define HEVC_NAL_TYPE_VPS 0x20
#define HEVC_NAL_TYPE_SPS 0x21
#define HEVC_NAL_TYPE_PPS 0x22
#define HEVC_NAL_TYPE_PRESEI 0x27
#define HEVC_NAL_TYPE_SUFSEI 0x28
#define HEVC_NAL_TYPE_EOB 0x28
#define HEVC_NAL_TYPE_EOS 0x24
#define HEVC_NAL_TYPE_FILT 0x26
#define HEVC_NAL_TYPE_SLICE 0x1

#define     NAL_UNIT_CODED_SLICE_BLA            16   // Current name in the spec: BLA_W_LP
#define     NAL_UNIT_CODED_SLICE_CRA            21
#define     NAL_UNIT_CODED_SLICE_TFD         9// Current name in the spec: RASL_R

// for avs
#define AVS_NAL_TYPE_SEQ 0xB0
#define AVS_NAL_TYPE_SEQEND 0xB1
#define AVS_NAL_TYPE_IFrame 0xB3
#define AVS_NAL_TYPE_PBFRAME 0xB6
#define AVS_NAL_TYPE_VIDEOEDIT 0xB7

#define VES_FLAG_NOT_FIRST_UNIT          (1 << 0)
#define VES_FLAG_IS_PIC                  (1 << 1)
#define VES_FLAG_I_FRAME                 (1 << 2)


#ifndef _VES_PARSER_ES_UNIT_S_
#define _VES_PARSER_ES_UNIT_S_
typedef struct VES_PARSER_ES_UNIT_S {
	uint32_t offset;
	uint32_t size;
	uint8_t unitType;		// NAL byte raw data
	uint8_t unitInfo;		// Flags that contains VES_FLAG_XXXX
	// if mpeg, reserved0: 1 for found pic header, 0 not found
	// if hevc, reserved0, reserved1 store nalu header for hevc parsing
	uint8_t reserve0;
	uint8_t reserve1;
	EN_VES_PARSER_DV_TYPE dvType;	// Dolby vision type, EN_VIDEO_ESPARSER_DV_TYPE
} VES_PARSER_ES_UNIT;

struct MPEG_ES_UNIT {
	uint32_t offset;
	uint32_t size;
	uint8_t unitType;		// NAL byte raw data
	uint8_t unitInfo;		// Flags that contains VES_FLAG_XXXX
	uint8_t bFoundPicHeader;	// if mpeg, reserved0: 1 for found pic header, 0 not found
	uint8_t pic_struct;	// 1 top field, 2 bottom field, 3 frame
	EN_VES_PARSER_DV_TYPE dvType;	// Dolby vision type, EN_VIDEO_ESPARSER_DV_TYPE
};

struct avc_custom_flag {
	unsigned char  bIFrame:    1;
	unsigned char  field_pic_flag:    1;
	unsigned char  bottom_field_flag:    1;
	unsigned char  is_ref:    1;
	unsigned char  reserved:    4;
};

union avc_custom_union {
	unsigned char data;
	struct avc_custom_flag f;
};

enum hevc_pic_struct {
	HEVC_BOTTOM_FIELD,
	HEVC_TOP_FIELD,
	HEVC_FRAME
};
struct AVC_ES_UNIT {
	uint32_t offset;
	uint32_t size;
	uint8_t unitType;		// NAL byte raw data
	union avc_custom_union unitInfo;
	uint16_t frame_num;
	EN_VES_PARSER_DV_TYPE dvType;	// Dolby vision type, EN_VIDEO_ESPARSER_DV_TYPE
};

struct hevc_custom_flag {
	unsigned char  bIFrame:    1;
	unsigned char  field_flag:    2;
	unsigned char  reserved:    5;
};

union hevc_custom_union {
	unsigned char data;
	struct hevc_custom_flag f;
};

struct HEVC_ES_UNIT {
	uint32_t offset;
	uint32_t size;
	uint8_t unitType;		// NAL byte raw data
	union hevc_custom_union unitInfo;
	uint16_t frame_num;
	EN_VES_PARSER_DV_TYPE dvType;	// Dolby vision type, EN_VIDEO_ESPARSER_DV_TYPE
};
#endif

typedef enum {
	E_VES_PARSER_CODEC_AVC = 0,
	E_VES_PARSER_CODEC_AVC_DV,
	E_VES_PARSER_CODEC_HEVC,
	E_VES_PARSER_CODEC_HEVC_DV,
	E_VES_PARSER_CODEC_MPEG2,
	E_VES_PARSER_CODEC_AVS,
	E_VES_PARSER_CODEC_AVS2,
	E_VES_PARSER_CODEC_VC1,
	E_VES_PARSER_CODEC_BY_PASS,
	E_VES_PARSER_CODEC_VP9,
	E_VES_PARSER_CODEC_MPEG4,
	E_VES_PARSER_CODEC_VP8,
	E_VES_PARSER_CODEC_H263,
	E_VES_PARSER_CODEC_S263,
	E_VES_PARSER_CODEC_VVC,
	E_VES_PARSER_CODEC_AVS3,
	E_VES_PARSER_CODEC_NUM,
} EN_VES_PARSER_CODEC_TYPE;

typedef enum {
	E_VES_PARSER_RET_OK = 0,
	E_VES_PARSER_RET_FAIL = 1,
	E_VES_PARSER_RET_TABLE_NOT_ENOUGH = 2,
} EN_VES_PARSER_RETURN;

EN_VES_PARSER_RETURN apiVEsParserCreateHandle(EN_VES_PARSER_CODEC_TYPE eCodecType,
					      uint64_t u64FifoStartAddr, uint64_t u64FifoEndAddr,
					      uint64_t u64FifoStartPhyAddr, uint64_t u64FifoEndPhyAddr,
					      VES_PARSER_HANDLE *pVEsParserHandle,
					      uint8_t bEnableParseSlice);
EN_VES_PARSER_RETURN apiVEsParserDeleteHandle(VES_PARSER_HANDLE VEsParserHandle);
EN_VES_PARSER_RETURN apiVEsParserAddEsData(VES_PARSER_HANDLE VEsParserHandle, uint64_t u64StartAddr,
					   uint32_t u32Size);
EN_VES_PARSER_RETURN apiVEsParserReset(VES_PARSER_HANDLE VEsParserHandle);
EN_VES_PARSER_RETURN apiVEsParserFindEsUnits(VES_PARSER_HANDLE VEsParserHandle,
					     uint64_t *pu64UnitBaseAddr,
					     VES_PARSER_ES_UNIT *pUnitTable,
					     uint32_t u32UnitTableLength,
					     uint32_t *pu32FoundUnitNum,
					     uint8_t *pbFindLastUnit);


#endif				// _VDEC_FRAMING_H_
