天天看點

CAT表中EMM PID 和 PMT表中ECM 的解析

直播中播放加擾節目時,常常啟動播放流程(申請裝置鍊,tuner,demux,audio decoder ,video decoder)之後,通常畫面還是不能出來的。此時需要我設定目前節目的EMM PID,ECM PID到CA裡面。解擾的大緻流程是,第三方CA收到EMM PID之後,利用智能卡中的中固化的PDK(中間件層密鑰)從EMM中解出 業務密鑰SK ,然後在利用業務密鑰從解出對應的ECM PID中解出對應的控制字CW ,最後将得到的控制字設定到解碼晶片相應的寄存器即可完整解擾。

另外需要普及一下,ECM 控制加擾的資訊可以是節目級别,也可以是流級别,也就是program level 或者是es level 級别,es 流可以是音頻的,視訊的,或者其他類型,也就是說可以整體加擾控制,也可以分開控制一個節目音頻,視訊等等。

借用網上剪輯的圖加以說明:

CAT表中EMM PID 和 PMT表中ECM 的解析

對于CA廠商來說,通常隻會提供幾個接口給STB廠商來設定EMM PID ,CA system ID, ECM PID ,Service ID,當然這些參數也會根據大廠商定制化的需要發生一些變動。

EMM ,ECM ,CA system ID資料的來源

EMM ,ECM ,CA system ID 存在于CAT,PMT兩個表中,這兩個表一般都是通過設定filter向驅動請求CAT 表和對應PMT表,有個相同點是,這些資訊都存在于CA_descriptor() 的描述字段中。

ca 描述字段的結構如下:

CAT表中EMM PID 和 PMT表中ECM 的解析

解析的代碼如下:

BOOL ca_descriptor_parse(BYTE* buf , int nlength,int *pnCAsysID ,int *pnEcmpid)
{
		int ndescriptor = 0;
		int ndes_length = 0;
		int nCASystemID = 0;
		int nelementPID = 0;
		BYTE *pByte     = NULL;
		
		if(buf == NULL || nlength <=  0)
		{
			printf("param error!\n");
			return FALSE;
		}
		
		pByte = buf;
		while(nlength > 0)
		{
			ndescriptor = pByte[0];
			ndes_length = pByte[1];
			if(ndescriptor == 0x09)
			{
				nCASystemID = (pByte[3]<<8 | pByte[4]);
			//  nelementPID = ((pByte[5]&0x1f)<<8 | pByte[6]);
				nelementPID = (pByte[5]<<8 | pByte[6]) & 0x1fff ;
			}
			*pnCAsysID = nCASystemID;
			*pnEcmpid = nelementPID;
			nlength -= ndes_length;
			printf("CA system ID : %d ,ECM/EMM PID : %d",nCASystemID ,nelementPID);
		}
		return TRUE;
}
           

CA_system_ID :表示适用于相關ECM和EMM流的相關的CA系統類型(CA廠家辨別符)。

CA_PID : PMT表中的該描述資訊為ECM的PID,CAT 表中的該描述資訊為EMM的PID。

ca_descriptor描述符的值是0x09,也即descriptor_tag 的值,如圖:

CAT:

CAT表中EMM PID 和 PMT表中ECM 的解析

PMT:

CAT表中EMM PID 和 PMT表中ECM 的解析

ECM 的解析

CAT表中EMM PID 和 PMT表中ECM 的解析

結合上面兩張圖來看,解析代碼如下:

BOOL ECMParse(BYTE* pBuf , int nLen )
{
	int nTableID 	= 0;
	int nSectionLen = 0;
	int nServiceID	= 0;
	int nProgramLen = 0;
	if(pBuf == NULL || nLen < 12 || nLen > 4096)
	{
		printf("param error!\n");
		return FALSE;
	}
	
	nTableID = pBuf[0];
	if(nTableID != 0x02)
	{
		printf("The Table is not PMT table!\n");
		return FALSE;
	}
	
	nSectionLen = ((pBuf[1]&0x0f) << 8 )| pBuf[2];
//	nSectionLen = ((pBuf[1]<< 8 )| pBuf[2])& 0x0fff;

	if(nLen != nSectionLen + 3 )
	{
		printf("section error!\n");
		return FALSE;
	}
	
	nServiceID  = (pBuf[3] << 8) | pBuf[4];// program number
	
	pBuf 		= pBuf + 10 // move pointer to the second reserved field
	nLen		= nLen - 10;
	
	
	nProgramLen = ((pBuf[0]&0x0f) << 8 |pBuf[1]);// get the program info length 
	if(nProgramLen + 7 >= nSectionLen)
	{
		printf("Bad Data!\n");
		return FALSE;
	}
	
	pBuf		= pBuf	+ nProgramLen;// move the pointer to the CA descriptor field
	nLen		= nLen	- nProgramLen;
	
	// TS level ecm parse
	int nTSCasysID = 0;
	int nTSEcmPID = 0;
	int noffest =  //the offest to the global ca_descriptor field
	ca_descriptor_parse(pBuf+noffest , nLen-noffest ,&nTSCasysID , &nTSEcmPID);
		
	//program level ecm parse 
	while(nLen >5) // make sure the ca_descriptor have data
	{
		int eStreamType = 0;
		int nEsPid 		= 0;
		int nESInfoLen	= 0;
		int nLength		= 0;
		int nCAsysID	= 0;
		int nEcmPid		= 0;
		
		eStreamType = pBuf[0];
		nEsPid		= ((pBuf[1]&0x1f << 8) | pBuf[2]); // es stream pid ,audio pid ,video pid and so on
		nLength		= (((pcData[3] & 0x0F) << 8) | pcData[4]);
		
		if(wDiscriptorLen > nLen -5)
		{
			printf("data error!\n");
			return FALSE;
		}
		
		pBuf		= pBuf + 5; // move the pointer to the ca_descriptor field
		nLen		= nLen - 5;
		
		ca_descriptor_parse(pBuf,nLen,&nCAsysID,&nEcmPid);		
		
	}
	return TRUE;
}
           

CAT表中的EMM解析比較簡單就不作解釋了。

以上解析動作盡量不要在播放過程中去請求,會消耗一定的時間,如果在播放之前解析好緩存起來,對直播速度會有一定的優化作用。