TS流解析-提取PSI信息

来源:互联网 发布:远视 眼镜 知乎 编辑:程序博客网 时间:2024/06/06 00:20

TS流解析-提取PSI信息等

代码加注释如下:

 

[cpp] view plaincopy
  1. #include <iostream>  
  2. #include <fstream>  
  3. using namespace std;  
  4. struct programs //封装节目信息的结构体  
  5. {  
  6.     int programID;//节目编号  
  7.     int pmtPID;//所属PMT的pid  
  8.     int videoPID;//视频pid  
  9.     int audioPID1;//音频pid  
  10.     int audioPID2;//音频pid  
  11. }myProg[20];  
  12.   
  13. bool FindAndParsePAT(unsigned char *buffer,int pID,int curPack);//传入BUF和PID的值  
  14. bool FindAndParsePMT(unsigned char *buffer,int pID,int curPack);  
  15. int program=0;  
  16. int prog_count=0;  
  17. void main()  
  18. {  
  19.       
  20.     unsigned    char *buffer=new unsigned char[500];  
  21.     int  startPos=0;//第一个TS分组在流中的位置序号  
  22.     int  packageLen=0;//分组长度  
  23.     int  pmtCount=-1;//PMT表序号  
  24.     int  pID=0;  
  25.     int  nullpack=0;  
  26.     //0.以二进制方式打开TS文件  
  27.     ifstream myFile("test.ts",ios::binary|ios::in);  
  28.           
  29.     //1.读入文件的前500个字节,找同步头、确定包长  
  30.     myFile.read((char *)buffer,500);  
  31.     for(int i=0;i<500;i++)  
  32.     {    //判断有无压缩  
  33.         if(buffer[i]==0x47&&buffer[i+188]==0x47)  
  34.         {  
  35.             startPos=i;//第一个TS分组在流中的位置序号  
  36.             packageLen=188;//分组长度  
  37.             break;  
  38.         }  
  39.         else if(buffer[i]==0x47&&buffer[i+204]==0x47)  
  40.         {  
  41.             startPos=i;  
  42.             packageLen=204;  
  43.             break;  
  44.         }  
  45.       
  46.   
  47.     }  
  48.       
  49.       
  50.       
  51.     //2.遍历流中的TS分组,查找PAT  
  52.     myFile.seekg(0,ios::end);//定位到文件尾部  
  53.     int totalBytes=myFile.tellg();//获取尾部距离首部的偏移量,即TS文件字节总数totalBytes  
  54.     int packageCount=(totalBytes-startPos)/packageLen;//确定进行遍历的循环次数 即总TS包数  
  55.   
  56.     int curPack=0;  
  57.     while (curPack<packageCount)//遍历分组  
  58.     {  
  59.         myFile.seekg(startPos+curPack*packageLen);//定位到第curPack个分组的首字节  
  60.         myFile.read((char *)buffer,packageLen);//读出当前分组,保存到缓存buffer中,读一段分组长度188或204  
  61.          pID=((buffer[1]&31)<<8)+buffer[2];//解析出当前分组的pid(13位=第2个字节的后5位+第3个字节全8位)  
  62.         if(pID==0x1fff)  //检查空包数  
  63.         {      
  64.             nullpack++;  
  65.         }  
  66.   
  67.         if(FindAndParsePAT(buffer,pID,curPack))//执行程序:解析PAT 有效  
  68.             break;  //表明只要解析一个PAT就行  
  69.         curPack++;  
  70.           
  71.     }  
  72.     curPack=0;  
  73.   
  74.   
  75.   
  76.   
  77.   
  78.    int a=0;  
  79.     while (curPack<packageCount)  
  80.     {     
  81.         myFile.seekg(startPos+curPack*packageLen);//定位到第curPack个分组的首字节  
  82.         myFile.read((char *)buffer,packageLen);//读出当前分组,保存到缓存buffer中,读一段分组长度188或204  
  83.          pID=((buffer[1]&31)<<8)+buffer[2];//解析出当前分组的pid(13位=第2个字节的后5位+第3个字节全8位)  
  84.         for(int k=0;k<prog_count;k++)  
  85.       
  86.         {  
  87.             if(pID==myProg[k].pmtPID)  
  88.             //根据PAT表内容确定如何查PMT表  
  89.             {    
  90.             cout<<"第"<<k+1<<"套节目:"<<endl;  
  91.             FindAndParsePMT(buffer,pID,curPack);//执行程序:解析PMT  
  92.              a++;  
  93.             }  
  94.         }  
  95.         if(a==prog_count)  
  96.         {     
  97.             break;  
  98.         }  
  99.         curPack++;  
  100.     }  
  101.     cout<<endl;  
  102.     cout<<"TS流相关信息:流中第一个TS分组起始位置"<<startPos<<","<<"TS分组长度"<<packageLen<<","<<"节目数"<<program<<","<<"空包数"<<nullpack<<endl;  
  103.     cout<<"所有节目相关PID信息"<<endl;  
  104.       
  105.     delete[]buffer;  
  106.     myFile.close();   
  107.   
  108.     }  
  109.     //查找并解析PAT  
  110.     bool FindAndParsePAT(unsigned char *buffer,int pID,int curPack)  
  111.     {  
  112.         //3.根据pid值是否为0确认PAT分组,并从中读PMT的PID  
  113.         int adapLen=0;//TS分组适配字段长度  
  114.         int offset=0;//实际净荷在当前分组中的偏移量  
  115.         if(pID==0)  
  116.         {  
  117.             int payload_unit_start = (buffer[1]>>6) & 0X01;//净荷单元起始指示  
  118.             int adaptation_field_control = (buffer[3]>>4) & 0X03;//自适应字段控制  
  119.               
  120.             //3.1 确定净荷起始位置(4字节固定首部+适配字段长度,adaption_field_control)  
  121.             if(adaptation_field_control==0x01)//无调整字段,仅净荷  
  122.             {  
  123.                   
  124.                 adapLen=0;//TS分组适配字段长度为0  
  125.             }  
  126.               
  127.             else if(adaptation_field_control==0x11)//有调整字段和净荷  
  128.             {  
  129.                   
  130.                 adapLen=buffer[4];//自适应字段长度   
  131.             }  
  132.             else//无有效载荷,查找下一个分组  
  133.             {  
  134.                 curPack++;  
  135.                 //continue;  
  136.             }  
  137.               
  138.             offset=4+adapLen;//确定净荷在当前分组中的偏移量,头的字节长度  
  139.               
  140.             //3.2 确定PAT首部在净荷中的偏移量(如payload_unit_start_indicator为1,  
  141.             //则净荷首字节为偏移指针,指示PAT首部与其之间的偏移值)  
  142.             if(payload_unit_start==0x01)//如果净荷单元起始指示为1  
  143.             {  
  144.                     offset+=buffer[offset]+1;//pointer_field字段长为1字节  
  145.             }  
  146.           
  147.             //3.3 开始解析PAT表  
  148.             int tableID=buffer[offset];//从净荷起始  
  149.             if(tableID==0)//进入节目关联表PAT  
  150.             {  
  151.                   
  152.                 int section_len=((buffer[offset+1]&0x0F)<<8)+buffer[offset+2];//code here:初始化  
  153.                 int transport_stream_idd=(buffer[offset+3]<<8)+buffer[offset+4];//code here:初始化  
  154.                 int current_next_indicator=buffer[offset+5]&0x01;//code here:初始化  
  155.                   
  156.                 if (current_next_indicator)//当前PAT有效  
  157.                 {  
  158.                          
  159.                      prog_count=(section_len-9)/4-1;  
  160.                     for(int i=0;i<prog_count;i++)  
  161.                     {  
  162.                         myProg[i].programID=(buffer[offset+12+i*4]<<8)+buffer[offset+12+i*4+1];//用2个字节表示节目号  
  163.                         cout<<"节目号"<<myProg[i].programID<<"  ";  
  164.                         myProg[i].pmtPID=(buffer[offset+14+i*4]&0x1F<<8)+buffer[offset+14+i*4+1];//用13位表示映射表  
  165.                         cout<<"映射表ID"<<myProg[i].pmtPID<<"\n";  
  166.                         program++;  
  167.                     }  
  168.                     //your code here 读出PAT包中存储的有关节目PMT的信息,确定节目数以及每路节目的PMT表pid,存储到myProg中  
  169.                       
  170.                     return true;  
  171.                 }  
  172.             }  
  173.         }  
  174.         return false;  
  175.     }  
  176.   
  177.     //查找并解析PMT  
  178.     bool FindAndParsePMT(unsigned char *buffer,int pID,int curPack)  
  179.     {  
  180.        //PMT 标志位  
  181.     int payload_unit_start_indicator; //1比特标志位,用来指示传送流分组带有PES分组或PSI数据时的情况  
  182.     int adaption_field_length;  //自适应字段长度。  
  183.     int pointer_field;     //  
  184.     int section_length;  //规定此字段之后此分段的字节数,包括CRC  
  185.     int section_number;  
  186.     int last_section_number; //8位字段,值总为0x00  
  187.     int i=0;  
  188.       
  189.         if (((buffer[3])<<2)/64==1)         
  190.             //判断adaption_field_control '01',无调整字段,仅含有有效负载  
  191.             //2位字段。用于指示本传送流分组首部是否跟随有调整字段和/或有效负载。  
  192.                                 /*00  为ISO/IEC未来使用保留 
  193.                                   01  无调整字段,仅含有效负载 
  194.                                   10   仅含调整字段,无有效负载 
  195.                                   11   调整字段后为有效负载 
  196.                                */  
  197.         {  
  198.             adaption_field_length=0;  
  199.         }  
  200.         else if (((buffer[3])<<2)/64==2)    
  201.             //判断adaption_field_control'10',仅含有调整字段,无有效负载  
  202.         {  
  203.             return true;  
  204.         }  
  205.         else if (((buffer[3])<<2)/64==3)    
  206.             //判断adaption_field_control'11',调整字段后为有效负载  
  207.         {  
  208.             adaption_field_length=buffer[4];//获得自适应字段长度  
  209.               
  210.         }  
  211.           
  212.         i=adaption_field_length;//指针指向有效负载部分  
  213.           
  214.         /* 
  215.                 如果传输流分组带有一个 PSI部分的第一个字节,payload_unit_start_indicator值应被置'1',表明传输流分组的第一个字 
  216.            节带有 pointer_field。如果传输流分组不带有一个 PSI部分的第一个字节payload_unit_start_indicator值应被置'0',表明在有 
  217.            效负载中没有pointer_field。空分组的payload_unit_start_indicator应置'0'。 
  218.         */  
  219.         payload_unit_start_indicator=( (buffer[1])<<1 )/128;  
  220.         //1bit,1表示有pointer-field  
  221.         //0表示无  
  222.           
  223.         if(payload_unit_start_indicator)    //判断payload_unit_start_indicator  
  224.         {  
  225.             pointer_field=buffer[i+4];  
  226.             //8位,其值为在此字段之后到传输流分组有效负载的第一个字段的第一个字节之间的字节数。  
  227.         }  
  228.         else   
  229.         {  
  230.             pointer_field=-1;  
  231.         }  
  232.           
  233.         i=i+pointer_field+1+4;//确定净荷在当前分组中的偏移量,头的字节长度  
  234.           
  235.         if(buffer[i]==2)    //判断table_id,PMT==0x02  
  236.         {  
  237.             section_length=buffer[i+1]%16*256+buffer[i+2];  
  238.             //section_length,12bit,表明在section_length字段之后此分段的字节数,包括CRC  
  239.         }  
  240.           
  241.         section_number=buffer[i+6];  
  242.         last_section_number=buffer[i+7];  
  243.           
  244.         for(int m=0;m<(section_length-57)/5;m++)   
  245.             //视频或音频流数目N的计算方法是N=(section_length--57)/5  
  246.             //section_length后面占用9字节+一堆从tsr.exe中看到的字节共57,CRC占用4字节  
  247.         {   
  248.               
  249.             if(buffer[i+23+m*5]==0x02)  
  250.             {  
  251.                 cout<<"视频PID:"<<buffer[i+24+m*5]%32*256+buffer[i+25+m*5]<<"\t";  
  252.             }  
  253.             if(buffer[i+23+m*(5+16)]==0x04)  
  254.             {  
  255.                 cout<<"音频PID:"<<buffer[i+24+m*(5+16)]%32*256+buffer[i+25+m*(5+16)]<<"\t";  
  256.             }  
  257.               
  258.         }  
  259.           
  260.         cout<<endl;  
  261.         //your code here           
  262.         return true;  
  263.     }  


 


0 0
原创粉丝点击