DTV 数字电视(二,demux解码TS packet,定义结构体)

1.TS packet结构(默认为188bytes)

关于Syntax中变量的解释,请参考DTV 数字电视(一,预备知识)_其实是个骑士的博客-CSDN博客,也可以参考ISO/IEC 13818-1https://download.csdn.net/download/weixin_43195281/88184762

2.parse code

#ifndef TS_H
#define TS_H

typedef unsigned int       uint32_t;
typedef unsigned short int uint16_t;
typedef unsigned char      uint8_t;
typedef unsigned long      uint64_t;

typedef struct
{
    unsigned int   data_length;
    unsigned char *data_start;
} DATA_BYTE

typedef struct
{
    bool         adaptation_field_header_exist         : 1;//mark whether this header is valid or not
    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;
} ADAPTATION_FIELD_HEADER;

typedef struct
{
    unsigned long program_clock_reference_base          : 33;
    unsigned int  reserved                              : 8 ;
    unsigned int  program_clock_reference_extension     : 9 ;
} PCR;

typedef struct
{
    unsigned long original_program_clock_reference_base        : 33;
    unsigned int  reserved                                     : 8 ;
    unsigned int  original_program_clock_reference_extension   : 9 ;
} OPCR;

typedef struct
{
    unsigned int   transport_private_data_length        : 8 ;
    unsigned char *private_data_byte;
} TRANSPORT_PRIVATE_DATA;

typedef struct
{
    unsigned int af_descr_tag    : 8 ;
    unsigned int af_descr_length : 8 ;
    unsigned int has_timestamp   : 2 ;
    unsigned int has_ntp         : 1 ;
    unsigned int has_ptp         : 1 ;
    unsigned int has_timecode    : 2 ;
    unsigned int force_reload    : 1 ;
    unsigned int paused          : 1 ;
    unsigned int discontinuity   : 1 ;
    unsigned int reserved        : 7 ;
    unsigned int timeline_id     : 8 ;
    unsigned int timescale       : 32;
    union
    {
        unsigned int  midia_timestamp_32bits : 32;
        unsigned long midia_timestamp_64bits : 64;
    }
    unsigned long ntp_timestamp        : 64;
    unsigned long long ptp_timestamp   : 80;
    unsigned int drop                  : 1 ;
    unsigned int frames_per_tc_seconds : 15;
    unsigned int duration              : 16;
    union
    {
        unsigned int  short_time_code : 24;
        unsigned long long_time_code  : 64;
    }
} TIMELINE_DESCRIPTOR;

typedef struct
{
    unsigned int service_type : 8 ;
    DATA_BYTE    mime_data;//mime_data exists when service_type equals to 0.
    DATA_BYTE    url_subpath;
} NB_ADDONS;

typedef struct
{
    unsigned int af_descr_tag           : 8 ;
    unsigned int af_descr_length        : 8 ;
    unsigned int force_reload           : 1 ;
    unsigned int is_announcement        : 1 ;
    unsigned int splicing_flag          : 1 ;
    unsigned int use_base_temi_url      : 1 ;
    unsigned int reserved               : 5 ;
    unsigned int timeline_id            : 7 ;
    unsigned int timescale              : 32;
    unsigned int time_before_activation : 32;
    unsigned int url_scheme             : 8 ;
    unsigned int url_path_length        : 8 ;
    DATA_BYTE    url_path;
    unsigned int nb_addons              : 8 ;
    NB_ADDONS   *nb_addons_data;
} TEMI_LOCATION_DESCRIPTOR;

typedef struct
{
    unsigned int af_descr_tag    : 8 ;
    unsigned int af_descr_length : 8 ;
    unsigned int url_scheme      : 8 ;
    DATA_BYTE    base_url_path;
} BASE_URL_DESCRIPTOR;

typedef struct
{
    
} CETS_BYTE_RANGE_DESCRIPTOR;

typedef struct
{
    unsigned int af_descr_tag        : 8 ;
    unsigned int af_descr_length     : 8 ;
    unsigned int reserved            : 1 ;
    unsigned int auxiliary_stream_id : 7 ;
} AF_MPEG_H_3D_AUDIO_EXTSTREAMID_DESCRIPTOR;

typedef struct
{
    unsigned int mae_groupID       : 7 ;
    unsigned int isInMainStream    : 1 ;
    unsigned int isInTS            : 1 ;
    unsigned int auxiliaryStreamID : 7 ;
} MAE_GROUP;

typedef struct
{
    unsigned int descriptor_tag           : 8 ;
    unsigned int descriptor_length        : 8 ;
    unsigned int extension_descriptor_tag : 8 ;
    unsigned int thisIsMainStream         : 1 ;
    unsigned int thisStreamID             : 7 ;
    unsigned int reserved                 : 1 ;
    unsigned int numAuxiliaryStreams      : 7 ;
    unsigned int reserved                 : 1 ;
    unsigned int mae_numGroups            : 7 ;
    MAE_GROUP   *mae_group;
    DATA_BYTE    reserved;
} AF_MPEG_H_3D_AUDIO_MULTI_STREAM_DESCRIPTOR;

typedef struct
{
    DATA_BYTE data;
} AF_MPEG_H_3D_AUDIO_COMMAND_DESCRIPTOR;

typedef struct
{
    unsigned int partition_id : 3 ;
    unsigned int partition_info_flag : 1 ;
    unsigned int reserved : 4 ;
} PARTITIONS_MINUS_1;

typedef struct
{
    unsigned int af_descr_tag                    : 8 ;
    unsigned int af_descr_length                 : 8 ;
    unsigned int num_partitions_minus_1          : 3 ;
    unsigned int SAP                             : 3 ;
    unsigned int concealment_flag                : 1 ;
    unsigned int reserved                        : 1 ;
    unsigned int sequence_number_length_code_SNL : 2 ;
    unsigned int reserved                        : 6 ;
    DATA_BYTE   *sequence_number;
} BOUNDARY_DESCRIPTOR;

typedef struct
{
    unsigned int af_descr_tag                    : 8 ;
    unsigned int af_descr_length                 : 8 ;
    unsigned int label_length_code               : 3 ;
    unsigned int label_type                      : 13;
    unsigned int label_length : 8 ;
    DATA_BYTE   *label_byte;
} LABELING_DESCRIPTOR;

typedef struct
{
    DATA_BYTE data;
} 13818_1_RESERVED_DESCRIPTOR;

typedef struct
{
    DATA_BYTE data;
} USER_PRIVATE_DESCRIPTOR;

typedef union
{
    TIMELINE_DESCRIPTOR                         timeline_descriptor;
    TEMI_LOCATION_DESCRIPTOR                    temi_location_descriptor;
    TEMI_BASE_URL_DESCRIPTOR                    temi_base_uri_descriptor;
    CETS_BYTE_RANGE_DESCRIPTOR                  cets_byte_range_descriptor;
    AF_MPEG_H_3D_AUDIO_EXTSTREAMID_DESCRIPTOR   af_mpeg_h_3dAudio_extStreamID_descriptor;
    AF_MPEG_H_3D_AUDIO_MULTI_STREAM_DESCRIPTOR  af_mpeg_h_3dAudio_multi_stream_descriptor;
    AF_MPEG_H_3D_AUDIO_COMMAND_DESCRIPTOR       af_mpeg_h_3dAudio_command_descriptor;
    BOUNDARY_DESCRIPTOR                         boundary_descriptor;
    LABELING_DESCRIPTOR                         labeling_descriptor;
    13818_1_RESERVED_DESCRIPTOR                 13818_1_reserved_descriptor;
    USER_PRIVATE_DESCRIPTOR                     user_private_descriptor;
} AF_DESCRIPTOR;

typedef struct
{
    unsigned int ltw_valid_flag      : 1 ;
    unsigned int ltw_offset          : 15;
} LTW;

typedef struct
{
    unsigned int reserved            : 2 ;
    unsigned int piecewise_rate      : 22;
} PIECEWISE_RATE;

typedef struct
{
    unsigned int splice_type         : 4 ;
    unsigned int DTS_next_AU_1       : 3 ;
    unsigned int marker_bit_1        : 1 ;
    unsigned int DTS_next_AU_2       : 15;
    unsigned int marker_bit_2        : 1 ;
    unsigned int DTS_next_AU_3       : 15;
    unsigned int marker_bit_3        : 1 ;
} SEAMLESS_SPLICE;

typedef struct
{
    unsigned int       adaptation_field_extension_length    : 8;
    unsigned int       ltw_flag                             : 1;
    unsigned int       piecewise_rate_flag                  : 1;
    unsigned int       seamless_splice_flag                 : 1;
    unsigned int       af_descriptor_not_present_flag       : 1;
    unsigned int       reserved                             : 4;
    LTW                ltw;
    PIECEWISE_RATE     piecewise_rate;
    SEAMLESS_SPLICE    seamless_splice;
    union
    {
        AF_DESCRIPTOR *af_descriptor;
        DATA_BYTE      reserved;
    }
    DATA_BYTE          stuffing_byte;
} ADAPTATION_FIELD_EXTENSION;

typedef struct
{
    ADAPTATION_FIELD_HEADER    adaptation_field_header   ;
    PCR                        pcr                       ;
    OPCR                       opcr                      ;
    unsigned int               splice_countdown       : 8;
    TRANSPORT_PRIVATE_DATA     transport_private_data    ;
    ADAPTATION_FIELD_EXTENSION adaptation_field_extension;
} ADAPTATION_FIELD;

typedef struct
{
    bool         ts_header_exist;//a flag Used to mark whether this tsHeader is valid or not
    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 ;
} TRANSPORT_PACKET_HEADER;

typedef struct
{
    TRANSPORT_PACKET_HEADER ts_header;
    ADAPTATION_FIELD        adaptation_field;
    DATA_BYTE               data_byte_payload;
} TRANSPORT_PACKET;

#endif

3.code annotation(TS_PACKET_HEADER)

typedef struct
{
    bool         ts_header_exist;//a flag Used to mark whether this tsHeader is valid or not
    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 ;
} TRANSPORT_PACKET_HEADER;

typedef struct
{
    TRANSPORT_PACKET_HEADER ts_header;
    ADAPTATION_FIELD        adaptation_field;
    DATA_BYTE               data_byte_payload;
} TRANSPORT_PACKET;

代码注释参考自spec ISO/IEC 13818-1-2018,免费下载地址如下,也可以私信我

https://download.csdn.net/download/weixin_43195281/88184762

TRANSPORT_PACKET:由TS包头、adapation_field()、data_byte组成。TS包头是每个TS PACKET都会包含的,固定占每个TS PACKET最开始的4个字节。adapation_field()和data_byte是依据每个TS包头里面的控制位来标识是否包含。

sync_byte:一个固定的8bits字段,其值为0x01000111(0x47)。

transport_error_indicator:1bit标志,当其置为1时,表示相关的TS中至少存在1bit不可纠正的错误。其可以被传输层外部的实体置为1。如果被置为1,其不能被重置为0,直到错误被纠正。

payload_unit_start_indicator:1bit标志。对于携带PES包或TS section data有具体的意义。其实就是表明是不是PES或section的开头。

如果TS的payload data包含PES,该位置为1表示payload包含PES的第一个字节,并且该TS有且只会包含一个PES;如果置为0表示没有没有PES从这个TS开始传输。也适用于private stream的stream_type = 6的情况。

如果TS的payload data包含TS section data,该位置1表示TS packet包含section的第一个byte,也意味着该TS包含point_field,该位置0表示不包含section的第一个byte,也意味着该TS不包含point_field。也适用于private stream的stream_type = 5的情况。

transport_priority:是一个1bit指示器。其置为1是,它相关的TS packet比其他相同PID的没有置为1的TS packet优先级更高。其可以被application修改,不要求PID相同。特定的编解码器也可以修改该值。

PID:是一个13bit字段,用来表示TS packet payload里面存储data的类型。部分值固定分配,例如PAT--0x0000、null packet--0x1FFF。具体如下图:

transport_scrambling_control:2bit字段,用于表示描述TS packet payload的scrambling mode。null packet一定要置为0x00。

adaptation_field_control:2bit字段,用于表示TS packet header之后是否携带adaptation_field与payload。

continuity_counter:4bit字段,其值随着每个具有相同PID的TS packet而递增。到达最大值之后重置为0。当TS packet的adaptation_field为'00'或'10‘时,其不得增加。

data_byte:字节应是来自PES包(参见2.4.3.6)、TS section(参见2.4.4)、TS section后的包填充字节或非PID指示的这些结构中的private data的连续字节。在PID值为Ox1FFF的空包的情况下,可以分配数据字节值。data_bytes数N由184减去adaptation_field()中的字节数指定,如2.4.3.4所述。

其余的struct具体变量含义都能够类似上文参考spec ISO/IEC 13818-1-2018,以后会在具体parse TS的过程中挨个介绍,免费下载地址如下,也可以私信我

https://download.csdn.net/download/weixin_43195281/88184762

4.下一次直接开始分享parse TS的函数实现

  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值