对于TS包头的结构体如下:
typedef struct TS_packet_header
{
    unsigned sync_byte                        : 8;
    unsigned transport_error_indicator        : 1;
    unsigned payload_unit_start_indicator    : 1;
    unsigned transport_priority                : 1;
    unsigned PID                            : 13;
    unsigned transport_scrambling_control    : 2;
    unsigned adaption_field_control            : 2;
    unsigned continuity_counter                : 4;
} TS_packet_header; 解析包头的函数为
void adjust_TS_packet_header(TS_packet_header* pheader)
{
    unsigned char buf[4]; 
    memcpy(buf, pheader, 4);
    pheader->transport_error_indicator        = buf[1] >> 7;
    pheader->payload_unit_start_indicator    = buf[1] >> 6 & 0x01;
    pheader->transport_priority                = buf[1] >> 5 & 0x01;
    pheader->PID                            = (buf[1] & 0x1F) << 8 | buf[2];
    pheader->transport_scrambling_control    = buf[3] >> 6;
    pheader->adaption_field_control            = buf[3] >> 4 & 0x03;
    pheader->continuity_counter                = buf[3] & 0x03;
} 看到这里我不明白,为什么要这样进行移位运算,
比如说象:pheader->transport_error_indicator        = buf[1] >> 7;
          pheader->payload_unit_start_indicator    = buf[1] >> 6 & 0x01;
          pheader->transport_priority                = buf[1] >> 5 & 0x01;等等
大家能不能给我详细说说为什么TS流数据头要进行这样的移位,这样为什么能够得到结构体每个成员的值

解决方案 »

  1.   

    typedef struct TS_packet_header 

        unsigned sync_byte                        : 8; 
        unsigned transport_error_indicator        : 1; 
        unsigned payload_unit_start_indicator    : 1; 
        unsigned transport_priority                : 1; 
        unsigned PID                            : 13; 
        unsigned transport_scrambling_control    : 2; 
        unsigned adaption_field_control            : 2; 
        unsigned continuity_counter                : 4; 
    } TS_packet_header; 
    “:”后面表示的是该成员所占的位数,位数为1则表示该成员的取值范围是0~1;位数为2则表示取值范围是0~3;位数为3则是0~7……
    整个结构体占4字节(32位)储存空间,第0~7位是sync_byte,第8位是transport_error_indicator,第9位是payload_unit_start_indicator……
    例如要提取结构体中的adaption_field_control,因为它是第26~27位,也就是第3(基于0)字节的第2~3位(共两位),所以要取第3字节,然后右移2位(把右边多余的2位抛弃),再“与”3(屏蔽左边多余的位)。