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

轉載自:https://blog.csdn.net/kehyuanyu/article/details/22402497

直播中播放加擾節目時,常常啓動播放流程(申請設備鏈,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 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解析比較簡單就不作解釋了。


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


--------------------- 
作者:Tony_ke 
來源:CSDN 
原文:https://blog.csdn.net/kehyuanyu/article/details/22402497 
版權聲明:本文爲博主原創文章,轉載請附上博文鏈接!

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章