⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 psi.c

📁 有线电视系统前端设备复用器原代码。 用语接受卫星信号
💻 C
📖 第 1 页 / 共 5 页
字号:
/*------------------------------------------------------------*/
/*psi.c                                                       */
/*------------------------------------------------------------*/
#include "constant.h"
#include "structure.h"
#include "repid.h"
#include "psi.h"
#include "caproc.h"
#include "psos.h"

SI_SECTION_DATA			g_default_pat_section;
GEN_SI_INFO				g_gen_si_info;
PID_REMAP_TABLE_ITEM	PID_Remap_Table_List[8][256];
unsigned long			PID_Remap_Table_Item_Count[8];

extern NEW_SDT			New_SDT;
extern SELECT_MUL		Select_Mul;
extern NIT_TABLE_TS		Nit;
extern EIT_TABLE_TS		Eit;
extern EIT_ITEM_LIST	EIT_Item_List;
extern PID_INFO			PID_Info[8];
extern System_Data		SystemData;
extern CA_CONFIG		g_ca_config;
extern System_Data		SystemData;
extern CA_ECM_DATA		g_ecm_data[3];
extern CA_ECMG_CHANNEL_STATUS	g_ecmg_channel_status[3];
extern CA_ECMG_STREAM_STATUS_LIST	g_ecmg_stream_status_list[3];
extern void my_Delay(unsigned long int dly_ms);

extern PORT_PID_FILTER PID_Filter[8];


extern unsigned long SST39VF512_Sector_Erase(unsigned long Sector_Off);
extern unsigned long SST39VF512_Write(unsigned long Off,unsigned char *buf,unsigned long len);

/*------------------------------------------------------------*/
/*从32K开始保存PSI信息                                        */
/*------------------------------------------------------------*/
void ClearPSI(void)
{
	int i;
	for(i=0;i<4;i++)
	{
		SST39VF512_Sector_Erase(32*1024 + i*4*1024);
	}
}

/*保存PSI信息*/
void SavePSI(void)
{
	int            i,j;
	unsigned char  *buf,*pbuf;
	volatile       PID_REMAP_TABLE_ITEM *pItem;
	unsigned long  PSILength=0;
	unsigned short Offset;

	unsigned long  VerifyFlag=0;

	PSILength+=sizeof(unsigned long);                  /*save the PSI data verify flag    */
	PSILength+=sizeof(unsigned long);                  /*save the PSI data length         */	
	
	PSILength+=sizeof(unsigned short);                 /*save the PAT section count       */
	for(i=0;i<1;i++)
	{
		PSILength+=sizeof(unsigned short);             /*save the PAT section length     */
		PSILength+=g_gen_si_info.pat_section.section_length;                 
	}

	PSILength+=sizeof(unsigned short);                 /*save the CAT section count       */

	if(g_gen_si_info.cat_section.section_length>0)
	{
		for(i=0;i<1;i++)
		{
			PSILength+=sizeof(unsigned short);             /*save the CAT section length     */
			PSILength+=g_gen_si_info.cat_section.section_length;                 
		}
	}

	PSILength+=sizeof(unsigned short);                /*save the PMT count               */
	/*for(i=0;i<Select_Mul.Sels;i++)*/
	for(i=0;i<g_gen_si_info.pmt_section_number;i++)
	{
		PSILength+=sizeof(unsigned short);            /*save the section count           */
		for(j=0;j<1;j++)
		{
			PSILength+=sizeof(unsigned short);       /*save the PMT section length */
			PSILength+=sizeof(unsigned short);       /*save the pmt pid            */
			PSILength+=g_gen_si_info.pmt_sectoin_list[i].section_length;   /*save the pmt section data   */
		}
	}

	PSILength+=sizeof(unsigned short);                           /*save the section count          */
	for(i=0;i<New_SDT.SectionCount;i++)
	{
		PSILength+=sizeof(unsigned short);                       /*save the section length         */
		PSILength+=New_SDT.SectionList[i].SectionLength;/*save the section data           */
	}
	/*--------------------------------------------------------*/
	/*NIT                                                     */
	/*--------------------------------------------------------*/
	PSILength+=sizeof(unsigned short);                           
	PSILength+=sizeof(unsigned long); /* stream_id  */                           
	PSILength+=sizeof(unsigned long); /* network id */                          
	for(i=0;i<Nit.TS_Count;i++)
	{
		PSILength+=sizeof(unsigned short);                      
		PSILength+=188;
	}

#if 0
	/*--------------------------------------------------------*/
	/*EIT                                                     */
	/*--------------------------------------------------------*/
	PSILength+=sizeof(unsigned short);    
	PSILength+=sizeof(unsigned char);    
	PSILength+=sizeof(unsigned char);    
	
	for(i=0;i<Eit.TS_Count;i++)
	{
		PSILength+=sizeof(unsigned short);                      
		PSILength+=188;
	}
#endif

	/*--------------------------------------------------------*/
	/*remap table                                             */
	/*--------------------------------------------------------*/
	for(i=0;i<8;i++)
	{
		PSILength+=sizeof(unsigned short);                          /* save the remap table item count */
		PSILength+=PID_Remap_Table_Item_Count[i]*sizeof(PID_REMAP_TABLE_ITEM);
	}
	/*--------------------------------------------------------*/
	/* 计算PSI信息的总长度                                    */
	/*--------------------------------------------------------*/
	buf=(unsigned char *)MemAlloc(RNG2ID,"SavePSI",PSILength);

	if(buf==NULL)
	{
		Display("can allocate memory to save psi!");
		return;
	}

	pbuf=buf;
	/*-------------------------------------------------------*/
	/* start to fill the buf                                 */
	/* first set the verify flag=0                           */
	/*-------------------------------------------------------*/
	Offset=0;
	*(unsigned long  *)(pbuf + Offset)=0;
	Offset+=sizeof(unsigned long);

	*(unsigned long  *)(pbuf + Offset)=PSILength - 4;
	Offset+=sizeof(unsigned long);

	/*-------------------------------------------------------*/
	/*save PAT                                               */
	/*-------------------------------------------------------*/
	/*-------------------------------------------------------*/
	/*PAT section count                                      */
	/*-------------------------------------------------------*/
	*(unsigned short *)(pbuf + Offset)=1;
	Offset+=sizeof(unsigned short);

	for(i=0;i<1;i++)
	{
		*(unsigned short *)(pbuf + Offset)=g_gen_si_info.pat_section.section_length;
		Offset+=sizeof(unsigned short);

		MemCopy(pbuf + Offset,g_gen_si_info.pat_section.section,g_gen_si_info.pat_section.section_length);
		Offset+=g_gen_si_info.pat_section.section_length;
	}	
	/*--------------------------------------------------------*/
	/*save CAT                                                */
	/*--------------------------------------------------------*/
	if(g_gen_si_info.cat_section.section_length>0)
	{
		*(unsigned short *)(pbuf + Offset)=1;
		Offset+=sizeof(unsigned short);

		for(i=0;i<1;i++)
		{
			*(unsigned short *)(pbuf + Offset)=g_gen_si_info.cat_section.section_length;
			Offset+=sizeof(unsigned short);

			MemCopy(pbuf + Offset,g_gen_si_info.cat_section.section,g_gen_si_info.cat_section.section_length);
			Offset+=g_gen_si_info.cat_section.section_length;
		}
	}
	else
	{
		*(unsigned short *)(pbuf + Offset)=0;
		Offset+=sizeof(unsigned short);
	}

	/*--------------------------------------------------------*/
	/*save PMT                                                */
	/*--------------------------------------------------------*/
	*(unsigned short *)(pbuf + Offset)=g_gen_si_info.pmt_section_number;
	Offset+=sizeof(unsigned short);

	for(i=0;i<g_gen_si_info.pmt_section_number;i++)
	{
		*(unsigned short *)(pbuf + Offset)=1;
		Offset+=sizeof(unsigned short);

		for(j=0;j<1;j++)
		{
			*(unsigned short *)(pbuf + Offset)=g_gen_si_info.pmt_sectoin_list[i].section_length;
			Offset+=sizeof(unsigned short);

			*(unsigned short *)(pbuf + Offset)=g_gen_si_info.pmt_sectoin_list[i].pid;
			Offset+=sizeof(unsigned short);

			MemCopy(pbuf + Offset,g_gen_si_info.pmt_sectoin_list[i].section,g_gen_si_info.pmt_sectoin_list[i].section_length);
			Offset+=g_gen_si_info.pmt_sectoin_list[i].section_length;
		}
	}
	/*--------------------------------------------------------*/
	/*save SDT                                                */
	/*--------------------------------------------------------*/
	*(unsigned short *)(pbuf + Offset)=New_SDT.SectionCount;
	Offset+=sizeof(unsigned short);

	for(i=0;i<New_SDT.SectionCount;i++)
	{
		*(unsigned short *)(pbuf + Offset)=New_SDT.SectionList[i].SectionLength;
		Offset+=sizeof(unsigned short);
		
		MemCopy(pbuf + Offset, New_SDT.SectionList[i].Section,New_SDT.SectionList[i].SectionLength);
		Offset+=New_SDT.SectionList[i].SectionLength;
	}

	/*--------------------------------------------------------*/
	/*save NIT                                                */
	/*--------------------------------------------------------*/
	*(unsigned short *)(pbuf + Offset)=Nit.TS_Count;
	Offset+=sizeof(unsigned short);

	*(unsigned long *)(pbuf + Offset)=Nit.Transport_Stream_ID;
	Offset+=sizeof(unsigned long);

	*(unsigned long *)(pbuf + Offset)=Nit.Network_ID;
	Offset+=sizeof(unsigned long);

	for(i=0;i<Nit.TS_Count;i++)
	{
		*(unsigned short *)(pbuf + Offset)=188;
		Offset+=sizeof(unsigned short);
		
		MemCopy(pbuf + Offset, Nit.NIT_TS[i],188);
		Offset+=188;
	}

#if 0
	/*--------------------------------------------------------*/
	/*save EIT                                                */
	/*--------------------------------------------------------*/
	*(unsigned short *)(pbuf + Offset)=Eit.TS_Count;
	Offset+=sizeof(unsigned short);

	*(unsigned char *)(pbuf + Offset)=Eit.Present_Flowing_Flag;
	Offset+=sizeof(unsigned char);

	*(unsigned char *)(pbuf + Offset)=Eit.Schedule_Flag;
	Offset+=sizeof(unsigned char);

	for(i=0;i<Eit.TS_Count;i++)
	{
		*(unsigned short *)(pbuf + Offset)=188;
		Offset+=sizeof(unsigned short);
		
		MemCopy(pbuf + Offset, Eit.EIT_TS[i],188);
		Offset+=188;
	}
#endif

	/*--------------------------------------------------------*/
	/*save the pid remap table                                */
	/*--------------------------------------------------------*/
	for(i=0;i<8;i++)
	{
		*(unsigned short *)(pbuf + Offset)=PID_Remap_Table_Item_Count[i];
		Offset+=sizeof(unsigned short);

		for(j=0;j<PID_Remap_Table_Item_Count[i];j++)
		{
			pItem=(PID_REMAP_TABLE_ITEM *)(pbuf + Offset);
			
			pItem->OldPID=PID_Remap_Table_List[i][j].OldPID;
			pItem->NewPID=PID_Remap_Table_List[i][j].NewPID;
			pItem->Output=PID_Remap_Table_List[i][j].Output;

			Offset+=sizeof(PID_REMAP_TABLE_ITEM);	
		}
	}

	VerifyFlag=0;
	for(i=0;i<PSILength-4;i++)
		VerifyFlag+=*(unsigned char *)(buf + 4 + i);

	*(unsigned long *)buf=VerifyFlag;
	/*-------------------------------------------------------*/
	/* save buf to flash                                     */
	/*-------------------------------------------------------*/
	for(i=0;i<4;i++)
	{
		SST39VF512_Sector_Erase(32*1024 + i*4*1024);
	}
	SST39VF512_Write(32*1024,buf,PSILength);

	MemFree(buf);

}
void InitDefPATSection(void)
{
	PAT_SECTION_TAG   *PAT_Section_Head;
	/*---------------------------------------------------------------------*/
	/* PAT section                                                         */
	/*---------------------------------------------------------------------*/
	PAT_Section_Head=(PAT_SECTION_TAG *)(g_default_pat_section.section);

	PAT_Section_Head->TableID=0x00;
	PAT_Section_Head->Section_syntax_indicator=1;
	PAT_Section_Head->Zero=0;
	PAT_Section_Head->Reserved1 =3;
	
	PAT_Section_Head->SectionLength=sizeof(PAT_SECTION_TAG) -3 + 4;   /* 初始值,需要根据节目的个数修改 */

	PAT_Section_Head->Transport_stream_id =0;

	PAT_Section_Head->Reserved2 =3;
	PAT_Section_Head->Version_number =0;
	PAT_Section_Head->Current_next_indicator = 1;
	PAT_Section_Head->Section_number=0;
	PAT_Section_Head->Last_section_number=0;

	*(unsigned long *)((unsigned char*)PAT_Section_Head + PAT_Section_Head->SectionLength + 3 - 4)=GetCRC((unsigned char *)PAT_Section_Head,PAT_Section_Head->SectionLength + 3 -4);

	g_default_pat_section.continue_counter=0;
	g_default_pat_section.pid=0;
	g_default_pat_section.section_length=PAT_Section_Head->SectionLength + 3;
}

/*从FLASH获取PSI信息*/
void LoadPSI(void)
{
	unsigned long  VerifyFlag,tmp;
	unsigned long  PSILength;
	unsigned long  Offset=0;
	unsigned char  *buf;
	unsigned short PMTs,Sections,SectionLength;
	unsigned short pid;
	volatile       PID_REMAP_TABLE_ITEM *pItem;

	int            i,j;
	
	VerifyFlag=*(unsigned long *)(BD_FLASH_ADDR + 32*1024);
	PSILength  =*(unsigned long *)(BD_FLASH_ADDR + 32*1024 + 4);

	if(PSILength>32*1024)
	{
		InitDefPATSection();				
		MemCopy((unsigned char *)&g_gen_si_info.pat_section,(unsigned char *)&g_default_pat_section,sizeof(SI_SECTION_DATA));
		g_gen_si_info.pmt_section_number=0;
		g_gen_si_info.cat_section.section_length=0;
		g_gen_si_info.flag=1;

		New_SDT.SectionCount=0;
		Nit.TS_Count        =0;
		Eit.TS_Count        =0;

		return;
	}

	buf=(unsigned char *)MemAlloc(RNG2ID,"LoadPSI",PSILength);

	if(buf==NULL)
	{/*内存分配失败*/
		InitDefPATSection();		

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -