风波邪人

幸福,幸福就是心里有那么一个人,不管你走到哪儿,也不管她走到哪儿,心里永远想着她

  博客园 :: 首页 :: 博问 :: 闪存 :: 新随笔 :: 联系 :: 订阅 订阅 :: 管理 ::

最新关于IOS-13818-1标准抓取PCR值计算transport rate的程序,由于我太懒了,最后还是要借助计算器或者Matlab计算最终结果。该程序只能得到

PCR(i)_base 和 PCR(i)_ext的值,以及PCR(i)所在的TS包的位置。

image

//fiel: ts_struct.h

#ifndef _TS_STRUCT_H
#define _TS_STRUCT_H
#include <iostream>

class TS_PACKET
{
private:
    unsigned int    sync_byte;                                    //8
    unsigned int    transport_error_indicator;                 //1
    unsigned int    payload_unit_start_indicator;            //1
    unsigned int    transport_priority;                           //1
    unsigned int    PID;                                              //13
    unsigned int    transport_scrambling_control;             //2
    unsigned int    adaptation_field_control;                   //2
    unsigned int    continuity_counter;                          //4
    unsigned int    adaptation_field_length;                    //8
    unsigned int    discontinuity_indicator;                     //1
    unsigned int    random_access_indicator;                 //1
    unsigned int    elementary_stream_priority_indicator;    //1
    unsigned int    PCR_flag;                                  //1
    unsigned int    OPCR_flag;                                //1
    unsigned int    splicing_point_flag;                      //1
    unsigned int    transport_private_data_flag;             //1
    unsigned int    adaptation_field_extension_flag;        //1
    long            PCR_base[2];                                //33bit
    unsigned int    PCR_reserved;                            //6
    unsigned int    PCR_ext    ;                                //9
    long            OPCR_base[2];                              //33bit
    unsigned int    OPCR_reserved;                          //6
    unsigned int    OPCR_ext;                                 //9
    unsigned int    splice_countdown;                       //8
    unsigned int    transport_private_data_length;      //8
public:
    //解析TS数据包
    int Process_Packet(FILE  * fp) ;
    //显示特定数据包内容
    void Display_Packet_Info(int i);
    void Find_PCR_Code(FILE * fp);
};

extern int PCR_OK;
extern int OPCR_OK;
extern int SP_CD_OK;

#endif

 

//fiel: ts_struct.cpp

#include "ts_struct.h"
#include <iostream.h>
#include <iomanip.h>

int PCR_OK = 0;
int OPCR_OK = 0;
int SP_CD_OK = 0;

//解析TS数据包
int TS_PACKET::Process_Packet(FILE *fp)
{

    long buf[6];
    int z = 0;
    unsigned int ch;
    ch = fgetc(fp);
    sync_byte = ch;                                    //1B

    ch = fgetc(fp);                                            //1B
    transport_error_indicator = (ch & 0x80) >> 7 ;            //transport_error_indicator

    payload_unit_start_indicator = (ch & 0x40) >> 6;        //payload_unit_start_indicator
    transport_priority = (ch & 0x20) >> 5;                    //transport_priority

    PID = ((ch & 0x1f) << 8) | fgetc(fp);                    //PID  , Read 1B

    ch = fgetc(fp);                                            //1B
    transport_scrambling_control = (ch & 0xc0) >> 6;        //transport_scrambling_control
    adaptation_field_control = (ch & 0x30) >> 4;            //adaptation_field_control

    continuity_counter = (ch & 0x0f);                        //continuity_counter

    ch = fgetc(fp);                                            //1B
    adaptation_field_length = ch;                            //adaptation_field_length

    // adatation field flag
    ch = fgetc(fp);                                            //1B
    discontinuity_indicator = ch >> 7;
    random_access_indicator = (ch >> 6) & 0x01;
    elementary_stream_priority_indicator = (ch >> 5) & 0x01;
    PCR_flag = (ch >> 4) & 0x01;
    OPCR_flag = (ch >> 3) & 0x01;
    splicing_point_flag = (ch >> 2) & 0x01;
    transport_private_data_flag = (ch >> 1) & 0x01;
    adaptation_field_extension_flag = ch & 0x01;

    //-------sum 6B

    //adaptation field 有效
    //自适应区长度不为0
    if(( adaptation_field_control == 3 ||  adaptation_field_control == 2 ) &&  adaptation_field_length )
    {
        // 当 PCR_flag 有效时,读 PCR值到缓存  , 6B
        if(  PCR_flag)
        {
            for(z = 0; z < 6; z++)
            {
                buf[z] = 0;
                buf[z] = fgetc(fp);                            //读取PCR数据
            }

            PCR_base[0] = (buf[0] & 0x80) ? 1 : 0;
            PCR_base[1] = (buf[0] << 25) | (buf[1] << 17) | (buf[2] << 9) | (buf[3] << 1) | ((buf[4] & 0x80) >> 7);
            PCR_ext = ((buf[4] & 0x01) << 8) | buf[5];
            PCR_OK = 1;                                        //PCR数据读取完毕标志
        }
        else
            fseek(fp, 6, 1);                                //移动文件指针

        // 当 OPCR_flag 有效时,读 OPCR值到缓存  ,6B
        if( OPCR_flag)
        {
            for(z = 0; z < 6; z++)
            {
                buf[z] = fgetc(fp);
            }
            OPCR_base[0] = (buf[0] & 0x80) ? 1 : 0;
            OPCR_base[1] = (buf[0] << 25) | (buf[1] << 17) | (buf[2] << 9) | (buf[3] << 1) | ((buf[4] & 0x80) >> 7);
            OPCR_ext = ((buf[4] & 0x01) << 8) | buf[5];
            OPCR_OK = 1;
        }
        else
            fseek(fp, 6, 1);

        if( splicing_point_flag )     // 1B
        {
            //读取splice_countdown 数据
            splice_countdown = fgetc(fp);
            SP_CD_OK = 1;
        }
        else
            fseek(fp, 1, 1);

        //文件指针移到下一个TS包起始位置
        fseek(fp, (0xbb - 18), 1);        //0 top position, 1 current position, 2 button position
    }
    else   
        fseek(fp, (0xbb - 5), 1);        //0 top position, 1 current position, 2 button position

    return 1;
}

//显示特定数据包内容
void TS_PACKET::Display_Packet_Info(int i)
{
    cout << "-----------------------------------------------------------------------" << endl << endl;
    cout << "TS Packet : " << dec << i << endl;
    cout << "Item                                 --- Data" << endl;

    cout << "sync_byte                            : " << hex << sync_byte << endl;
    //cout << "transport_error_indicator            : " << transport_error_indicator << endl;
    //cout << "payload_unit_start_indicator         : " << payload_unit_start_indicator << endl;
    //cout << "transport_priority                   : " << transport_priority << endl;
    cout << "PID                                  : " << PID << endl;
    //cout << "transport_scrambling_control         : " << transport_scrambling_control << endl;
    //cout << "adaptation_field_control             : " << adaptation_field_control << endl;
    //cout << "continuity_counter                   : " << continuity_counter << endl;
    //cout << "adaptation_field_length              : " << adaptation_field_length << endl;

    //cout << "discontinuity_indicator              : " << discontinuity_indicator << endl;
    //cout << "random_access_indicator              : " << random_access_indicator << endl;
    //cout << "elementary_stream_priority_indicator : " << elementary_stream_priority_indicator << endl;
    cout << "PCR_flag                             : " << PCR_flag << endl;
    cout << "OPCR_flag                            : " << OPCR_flag << endl;
    //cout << "splicing_point_flag                  : " << splicing_point_flag << endl;
    //cout << "transport_private_data_flag          : " << transport_private_data_flag << endl;
    //cout << "adaptation_field_extension_flag      : " << adaptation_field_extension_flag << endl;

    if(PCR_OK)
    {
        PCR_OK = 0;
        //output PCR
        cout.setf(ios::showbase);
        cout << "PCR_base                         : " << hex<< PCR_base[0];
        cout.unsetf(ios::showbase);
        cout<<PCR_base[1] << endl;
        cout << "PCR_reseverd                     : " << hex<< PCR_reserved << endl;
        cout << "PCR_ext                          : " << hex<< PCR_ext << endl;
    }

    if(OPCR_OK)
    {
        OPCR_OK = 0;
        //output OPCR
        cout.setf(ios::showbase);
        cout << "OPCR_base                        : " << hex<< OPCR_base[0];
        cout.unsetf(ios::showbase);
        cout << OPCR_base[1]<< endl;
        cout << "OPCR_reseverd                    : " << hex<< OPCR_reserved << endl;
        cout << "OPCR_ext                         : " << hex<< OPCR_ext << endl;
    }

    //output splice_countdown
    if(SP_CD_OK)
    {
        SP_CD_OK = 0;
        cout << "splice_countdown: " << splice_countdown << endl;
    }

}

void TS_PACKET::Find_PCR_Code(FILE * fp)
{
    int i = 0, count = 0;
    do{

        if(Process_Packet(fp))
        {
            if(PCR_OK || OPCR_OK)
            {
                count ++;
                if(count == 20)
                    break;
                Display_Packet_Info(i);
            }
        }
        else
        {
            cout<<"Red Transport Packet Fail!"<<endl;
            return;
        }
        i++;
    }while(!feof(fp));
}

 

//fiel: main.cpp

#include <iostream>
#include "ts_struct.h"
using namespace std;

int main(){

    TS_PACKET ts;
    FILE * fp;
    fp = fopen("ysgq.trp","rb");
    if(fp  != NULL)
        cout<<"File open success!"<<endl;
    else{
        cout<<"File open fail!"<<endl;
        exit(0);
    }
    ts.Find_PCR_Code(fp);
    fclose(fp);
    return 0;
}

posted on 2011-12-27 13:05  风波邪人  阅读(1072)  评论(0编辑  收藏  举报