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

来源:互联网 发布:data science 知乎 编辑:程序博客网 时间:2024/06/14 16:18

直播中播放加扰节目时,常常启动播放流程(申请设备链,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 流可以是音频的,视频的,或者其他类型,也就是说可以整体加扰控制,也可以分开控制一个节目音频,视频等等。

借用网上剪辑的图加以说明:

                                       

对于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 描述字段的结构如下:

                                                  

解析的代码如下:

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:

                      


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 numberpBuf = pBuf + 10 // move pointer to the second reserved fieldnLen= 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 fieldnLen= nLen- nProgramLen;// TS level ecm parseint nTSCasysID = 0;int nTSEcmPID = 0;int noffest =  //the offest to the global ca_descriptor fieldca_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 onnLength= (((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 fieldnLen= nLen - 5;ca_descriptor_parse(pBuf,nLen,&nCAsysID,&nEcmPid);}return TRUE;}

CAT表中的EMM解析比较简单就不作解释了。


以上解析动作尽量不要在播放过程中去请求,会消耗一定的时间,如果在播放之前解析好缓存起来,对直播速度会有一定的优化作用。


0 0