#ifndef _DTV_H
#define _DTV_H

#include "hx_types.h"
#include "list_head.h"

#define ALLOW_OS_CODE 1

#define AV_FLAG_NONE		0x00
#define AV_FLAG_RESTART		0x01

#define MAX_SECTION_BUFLEN	4096

#define VIDEO_CHAN_MAX		(32)
#define AUDIO_CHAN_MAX		(32)

#define CA_SYSTEM_ID_MAX	(32)
#define PMT_PID_MAX			(128)

#define MAX_ANTENNA			(256)

/////////////////////////////////////////////////////
#define MAX_TUNER_COUNT			4
#define MAX_DTV_COUNT			3
#define DEFAULT_DTV				0
#define DTV_0						0
#define DTV_1						1
#define DTV_2						2

#define SUBTITLE_MAX			(32)
#define TELETEXT_MAX			(32)

#define EPG_NONE    0x00

struct teletext_st {
	HX_UINT16 teletext_pid; //ksko
	HX_INT8 lang[4];

	HX_UINT8 type;
	HX_UINT8 magazine_number; 
	HX_UINT8 page_number;
};

struct subtitling_st {
	HX_UINT16 subtitling_pid; //ksko
	HX_INT8 lang[4];

	HX_UINT8 type;
	HX_UINT16 composition_page_id;
	HX_UINT16 ancillary_page_id;
};

//------------------------------------------
// for DVB_S
#define LNB_FLAG_OFF				0x00
#define LNB_FLAG_ON				0x01
#define LNB_FLAG_SCANNED		0x80

// for combination of diseqc1.1 and diseqc1.2
#define  SIZEOF_BIT_DISEQC_11       4
#define  SIZEOF_BIT_DISEQC_10       2
#define  SIZEOF_BIT_DISEQC_TONE     2

#define  OFFSET_DISEQC_11           0
#define  OFFSET_DISEQC_10           SIZEOF_BIT_DISEQC_11
#define  OFFSET_DISEQC_TONE         ( SIZEOF_BIT_DISEQC_11 + SIZEOF_BIT_DISEQC_10 )

#define  MASK_DISEQC_11             ( 0x0F << OFFSET_DISEQC_11 )
#define  MASK_DISEQC_10             ( 0x03 << OFFSET_DISEQC_10 )
#define  MASK_DISEQC_TONE           ( 0X03 << OFFSET_DISEQC_TONE )

#define  SIZEOF_BIT_DISEQC_MODE_MOTOR		4
#define  SIZEOF_BIT_DISEQC_MODE_COMBI		4

#define  OFFSET_DISEQC_MODE_MOTOR	0
#define  OFFSET_DISEQC_MODE_COMBI	SIZEOF_BIT_DISEQC_MODE_MOTOR

#define  MAKE_DISEQC_MODE(X,Y)      ((X<<OFFSET_DISEQC_MODE_COMBI)|Y)

#define  MASK_DISEQC_MODE_COMBI     ( 0x000F << OFFSET_DISEQC_MODE_COMBI )
#define  MASK_DISEQC_MODE_MOTOR     0x000F

// position control mode
#define LNB_DISEQC_MOTER_MODE_OFF	0x00
#define LNB_DISEQC_MOTER_MODE_12		0x01
#define LNB_DISEQC_MOTER_MODE_USALS	0x02

enum polarisation {
	POLARISATION_HORIZONTAL     = 0x00,
	POLARISATION_VERTICAL       = 0x01,
	POLARISATION_CIRCULAR_LEFT  = 0x02, //HORIZONTAL
	POLARISATION_CIRCULAR_RIGHT = 0x03  //VERTICAL
};

enum annex_type {
	ANNEX_DVBC,
	ANNEX_MCNS
};


//------------------------------------------
//for DVB_T band_width
#if 1
#define INPUTBW_8MHZ        1
#define INPUTBW_7MHZ        2
#define INPUTBW_6MHZ        3
#else //is spec
#define INPUTBW_8MHZ        0	
#define INPUTBW_7MHZ        1
#define INPUTBW_6MHZ        2
#define INPUTBW_5MHZ        3
#endif

//------------------------------------------

/* ATSC Modulation Mode  */
#define DEMOD_ANALOG		 			0x01
#define DEMOD_SCTE_MODE1_64QAM	0x02
#define DEMOD_SCTE_MODE2_256QAM	0x03 
#define DEMOD_ATSC_8VSB				0x04
#define DEMOD_ATSC_16VSB			0x05

/* ATSC Service Types  */
#define ANALOG_TELEVISION 			0x01
#define ATSC_DIGITAL_TELEVISION	0x02
#define ATSC_DIGITAL_AUDIO			0x03
#define ATSC_DATA_ONLY_SERVICE	0x04

//------------------------------------------
#define DTV_NONE		0x00
#define DTV_ATSC		0x01
#define DTV_DVBS		0x02
#define DTV_DVBT		0x04
#define DTV_CIPTV		0x08
#define DTV_ARIB		0x10
#define DTV_ISDBT    		0x20
#define DTV_DVBC		0x40

#define ATSC_NETWORK_NAME "/DISK2/ATSC.dat"
#define DVBT_NETWORK_NAME "/DISK2/DVBTa.dat"
#define DVBS_NETWORK_NAME "/DISK2/DVBS.dat"
#define ISDBT_NETWORK_NAME "/DISK2/ISDBT.dat"
#define DVBC_NETWORK_NAME "/DISK2/DVBC.dat"		//link file
#define DVBC_NETWORK_NAME_STD "/DISK2/DVBCa.dat"		//standard
#define DVBC_NETWORK_NAME_GERMAN "/DISK2/DVBC_ger.dat"	//germany
#define DVBC_NETWORK_NAME_GERMAN_A "/DISK2/DVBC_ger_a.dat"	//germany
#define DVBC_NETWORK_NAME_BRASIL "/DISK2/DVBC_bra.dat"	//brasil

/* 
 ****	NETWRORK INDEX	*** 
 * DVBS 	: 0 ~ 240
 * DVBC 	: 241
 * DVBT 	: 242
 * ISDBT	: 243
 * ATSC 	: 244 ~ 251
 * RESERVED : 246 ~ 250
 * UDP_CHANNEL 	: 252
 * CIPTV 		: 253
 * RESERVED 	: 254 ~ 255
 */

#define DVBS_NET_INDEX_MIN 0
#define DVBS_NET_INDEX_MAX 240

#define DVBC_NET_INDEX 241

#define DVBT_NET_INDEX 242

#define ISDBT_NET_INDEX 243

#define ATSC_C_NET_INDEX 244
#define ATSC_T_NET_INDEX 245

#define ATSC_STD_NET_INDEX 246
#define ATSC_HRC_NET_INDEX 247
#define ATSC_IRC_NET_INDEX 248

#define UDP_CHANNEL_NET_INDEX	252
#define FILE_CHANNEL_NET_INDEX	253

#define NET_INDEX_MAX 255

#define NET_VERSION 3

typedef struct atsc_tp_st {
	HX_UINT16 channel_number;
	HX_UINT16 demod_type;
} stATSCTP;

typedef struct dvbt_tp_st {
	HX_UINT16 channel_number;
	HX_UINT8 band_width;
} stDVBTTP;

typedef struct dvbs_tp_st {
	HX_UINT32 symbol_rate; /* symbol rate in Symbols per second */
	HX_INT8 	 fec_inner; /* forward error correction */
	HX_UINT8	 polarisation;	
	HX_UINT8  we_flag;
	HX_INT8	 pilot;
	HX_INT32  orbital_pos;						
	HX_UINT8  roll_off;
	HX_UINT8  modulation_system;
	HX_UINT8  modulation_type;
} stDVBSTP;

typedef struct isdbt_tp_st {
	HX_UINT16 channel;
} stIsDVBTTP;

typedef struct dvbc_tp_st {
	HX_UINT16 channel_number;
	HX_UINT32 symbol_rate;
	HX_UINT8 qam_mode;
	HX_UINT8 input_band;
} stDVBCTP;

typedef union type_info_un {
	struct dvbs_tp_st dvb_s;
	struct dvbt_tp_st dvb_t;
	struct atsc_tp_st atsc;
	struct isdbt_tp_st isdbt;
	struct dvbc_tp_st dvb_c;
} unTypeInfo;

typedef struct multiplex_st {
	struct list_head list;
	HX_UINT32 frequency;	/* intermedate frequency in Hz for 8VSB */
	HX_UINT16 original_network_id;
	HX_UINT16 transport_stream_id;
	HX_UINT32 type;
	unTypeInfo info;
} stMultiplex;

typedef struct network_st {
	struct list_head list;
	HX_UINT8	index; // -> uid
	HX_INT8 name[MAX_SAT_NAME_LEN];
	HX_INT16 unused; //angle*10 move -> orbital_pos
	HX_UINT16 mp_num;
	struct multiplex_st mp;
	HX_INT16 version; //orbital_pos;						
	HX_INT16  reserved1; //orbital_pos;						
	HX_INT32 reserved2;
}stNetwork;

typedef struct networks_st {
	HX_UINT16 number;
	struct network_st net;
} stNetwroks;

typedef struct all_networks_st {
	HX_UINT dvbs_number;
	HX_UINT dvbc_number;
	HX_UINT dvbt_number;
	HX_UINT isdbt_number;
	HX_UINT atsc_number;
	HX_UINT number;
	struct network_st net;
} stAllNetwork;

/////////////////////////////////////////////////////////////////

//------------------------------------------

enum running_mode {
	RM_NOT_RUNNING = 0x01,
	RM_STARTS_SOON = 0x02,
	RM_PAUSING     = 0x03,
	RM_RUNNING     = 0x04
};

// tag 0x03, 0x04, 0x0f, 0x81, 0x82, 0x11
struct audio_info_st {
	HX_UINT8 tag;
	HX_UINT8 component_tag;
	HX_UINT16 pid;
	HX_UINT16 ecm_pid;
	HX_INT8 lang[4];
}stAudioInfo;

// tag 0x01 0x02 0x10 0x1b 0xea
struct video_info_st {
	HX_UINT8 tag;
	HX_UINT8 component_tag;
	HX_UINT16 pid;
	HX_UINT16 ecm_pid;
}stVideoInfo;

struct ca_info_st {
	HX_UINT16 ca_system_id[CA_SYSTEM_ID_MAX];
	HX_UINT16 ca_pid[CA_SYSTEM_ID_MAX];
	HX_UINT8 ca_num;
}stCAInfo;

struct pat_info_st {
	HX_UINT16 pmt_pid[PMT_PID_MAX];
	HX_UINT16 pmt_sid[PMT_PID_MAX];
	HX_UINT8 pmt_num;
}stPATInfo;

struct pmt_info_st {
	HX_UINT8 buf[MAX_SECTION_BUFLEN];
	HX_UINT32 buflen;
}stPMTInfo;

struct cat_info_st {
	HX_UINT8 buf[MAX_SECTION_BUFLEN];
	HX_UINT32 buflen;
}stCATInfo;

//------------------------------------------


#define NAME_TYPE_NONE		0x00
#define NAME_TYPE_JAPAN		0x01
#define NAME_TYPE_ENGLISH	0x02
#define NAME_TYPE_KOREA		0x03

#define LOGICAL_CELL_MAX	16
#define ELEMENT_CELL_MAX	16
#define GENRE_MAX	10

typedef struct elementary_cell_st{
	HX_UINT8 elementary_cell_id;
}stElementaryCall;

struct logical_cell_st{
	HX_UINT8 logical_cell_id;
	HX_UINT8 logical_cell_info; //1:video 2:still picture 3:graphics/text

	struct elementary_cell_st elementary_cell[ELEMENT_CELL_MAX];
	HX_UINT8 elementary_cell_num;

	HX_UINT8 cell_linkage_info;
	HX_UINT16 original_network_id;
	HX_UINT16 transport_stream_id;
	HX_UINT16 service_id;
}stLogicalCell;


#define MAX_DISCRIPTOR_COUNT			2
//#define MAX_DISCRIPTOR_COUNT			16

#define MAX_DISCRIPTOR_LENGTH	253

typedef struct _data_component_st {
	HX_UINT16	id;
	//HX_INT8 add_data_component_info //not yet
} data_component_st;

// tag 0x56(ttx), 0x59(subt), 0xfd(data component)

struct service_st {
	struct list_head list;

	HX_UINT16 major_number;
	HX_UINT16 minor_number;
	HX_UINT8	 lock;
	HX_UINT8	 hd;
	HX_INT8	 tuner_id;

	HX_UINT32 freq;
	HX_UINT16 transport_stream_id;

	HX_UINT16 original_network_id;
	HX_UINT32 index;
	HX_UINT16 service_id;
	HX_UINT16 source_id; //use ATSC

	HX_UINT8	 polarisation;	

	HX_UINT16 reference_service_id;

	HX_INT32 version_number;
	HX_INT32 current_next_indicator;

	HX_INT8 provider_name[MAX_PROVIDER_NAME_LEN+1];
	HX_INT16 pv_name_len;	

	name_list_st name_list;

	HX_UINT8 net_index;

	HX_INT16 pat_version_number;
	HX_INT16 pmt_version_number;

	HX_UINT16 pmt_pid;
	HX_UINT16 pcr_pid;
	
	HX_UINT16 ecm_pid; // program level

	struct video_info_st video_info[VIDEO_CHAN_MAX];
	HX_INT16 video_num;

	struct audio_info_st audio_info[AUDIO_CHAN_MAX];
	HX_INT16 audio_num;

	HX_UINT16 ca_system_id[CA_SYSTEM_ID_MAX];
	HX_UINT16 ca_pid[CA_SYSTEM_ID_MAX];
	HX_INT16 ca_num;

	private_data_st pdata[PRIVATE_DATA_MAX];
	HX_INT16 pdata_num;

	struct teletext_st teletext[TELETEXT_MAX];
	HX_INT16 teletext_num;

	struct subtitling_st subtitling[SUBTITLE_MAX];
	HX_INT16 subtitling_num;

	data_component_st data_comp[PRIVATE_DATA_MAX];
	HX_INT16 data_comp_num;

	HX_UINT16 s_data_pid;
	HX_UINT16 s_cmd_pid;

	HX_UINT16 type;
	HX_UINT8 scrambled;
	HX_UINT8 dsmcc;
	
	enum running_mode running;

	//DM_logo_bitmap_list_st logo_bitmap_list;
	void *priv;

	struct text_info_st text_info;

	HX_UINT8 is_mosaic;
	HX_UINT8 h_element_cell_num;
	HX_UINT8 v_element_cell_num;
	struct logical_cell_st logical_cell[LOGICAL_CELL_MAX];
	HX_UINT8 logical_cell_num;
#if 0
	struct service_list_st service_list[SERVICE_LIST_MAX];
	HX_UINT16 service_list_num;
#endif

	HX_UINT8 general_last_table_id;
	HX_UINT8 general_last_section_number;
	HX_UINT8 detail_last_table_id;
	HX_UINT8 detail_last_section_number;

	unsigned short elementary_PID;
	HX_UINT8 SIDtype;
}stService;

//------------------------------------------

#define MAX_TS_NUM	64

struct scanning_per_ts_st {
#ifdef _TD115S_P
	HX_UINT16 sat_index;
#endif
	HX_UINT16 transport_id;
	HX_UINT32 frequency;
	
	HX_INT16 service_index;
	struct list_head services;

	HX_BOOL done;
}stScanningPerTS;

struct scanner_info_st {
	HX_BOOL created;
	HX_BOOL vendor_specific_scanning;

	HX_INT16 getoutof_scanner;
	HX_INT16 done;

	HX_INT8 tuner_id;

//	HX_UINT8 network_name[MAX_SAT_NAME_LEN];
//	HX_UINT8 net_name_len;

	HX_UINT type;			// (SCAN_ALL, SCAN_SAT, SCAN_TP, SCAN_PID)

	HX_UINT ant_cnt;	// append(hskim)
	HX_UINT ant_index[MAX_ANTENNA];	// append(hskim)
	HX_UINT scanned_ant_num;

//	HX_UINT tuner_spec;

	struct network_st *net;
	struct multiplex_st *mp;
	
	HX_BOOL NIT_mps_is_used;
	
	struct multiplex_st NIT_mps;		// MPs got from NIT
	HX_UINT16 NIT_mps_num;

	struct multiplex_st scanned_mps;	// Already scanned MPs
	HX_UINT16 scanned_mps_num;
	HX_UINT16 scanned_mp_index;

	HX_BOOL mp_is_updated;

	HX_UINT16 total_mp_num;
	HX_UINT 	mp_freq[256];	// append(qwer)
	HX_UINT 	mp_polar[256];	// append(qwer)
	
	HX_UINT16 video_pid;
	HX_UINT16 audio_pid;
	HX_UINT16 pcr_pid;
	HX_UINT16 service_id;
	
	HX_UINT8 get_other_nits;
	HX_UINT8 use_nit_search;
	HX_UINT8 filter; // first bit is TV, 2 is Radio, 3 is FTA, 4 is scam

	HX_UINT16 transport_stream_id;

	struct list_head * mp_pos; // append(hskim)
	struct list_head * sv_pos; // append(hskim)

	HX_INT16 scanning_ts_num;
	HX_INT16 scanning_ts_done_num;
	struct scanning_per_ts_st scanning[MAX_TS_NUM];

	HX_INT16 scanned_service_index;
	struct list_head scanned_services;
	
	HX_INT16 semid;

	HX_UINT8 seg_select; // select ONE SEG or FULL SEG
	HX_UINT8 SIDtype;		/* 0:normal type 1: DiskNetwork 2 : Bell Vu */
	HX_BOOL SamePmtPidScan; //2k90429 son1127 all sam pmt pid in one tp
} stScannerInfo;

enum {
	AUDIO_FORMAT_NONE = 0,
	AUDIO_FORMAT_AC3,
	AUDIO_FORMAT_MPEG,
	AUDIO_FORMAT_DTS,
	AUDIO_FORMAT_AAC,
	AUDIO_FORMAT_MAX
};

enum MEM_ID{
	DEFAULT_EIT = 0,
	USER_EIT,
	MAX_EIT_ID,
};

typedef struct memory_status_st {
	HX_INT32 max_eit_memory;
	HX_INT32 totalmemory[MAX_EIT_ID]; 
	HX_INT32 usedmemory[MAX_EIT_ID];
}stMemoryStatus;

#define MAX_USER_EIT_MEMORY (1024*1024*20)
#define DEFAULT_USER_EIT_MEMORY (1024*1024*8)

#endif // _DTV_H


