http://blog.youkuaiyun.com/nine_locks/article/details/46965885
版权声明:本文为博主原创文章,未经博主允许不得转载。
- int H2642PES()
- {
- Ts_Adaptation_field ts_adaptation_field_Head ;
- Ts_Adaptation_field ts_adaptation_field_Tail ;
- ts_video_pes->packet_start_code_prefix = 0x000001;
- ts_video_pes->stream_id = TS_H264_STREAM_ID; //E0~EF表示是视频的,C0~DF是音频,H264-- E0
- ts_video_pes->PES_packet_length = 0 ;// OneFrameLen_H264 + 8; //一帧数据的长度 不包含 PES包头 ,这个8 是 自适应的长度,填0 可以自动查找
- ts_video_pes->Pes_Packet_Length_Beyond = OneFrameLen_H264;
- if (OneFrameLen_H264 > 0xFFFF) //如果一帧数据的大小超出界限
- {
- ts_video_pes->PES_packet_length = 0x00;
- ts_video_pes->Pes_Packet_Length_Beyond = OneFrameLen_H264;
- }
- ts_video_pes->marker_bit = 0x02;
- ts_video_pes->PES_scrambling_control = 0x00; //人选字段 存在,不加扰
- ts_video_pes->PES_priority = 0x00;
- ts_video_pes->data_alignment_indicator = 0x00;
- ts_video_pes->copyright = 0x00;
- ts_video_pes->original_or_copy = 0x00;
- ts_video_pes->PTS_DTS_flags = 0x03;
- ts_video_pes->ESCR_flag = 0x00;
- ts_video_pes->ES_rate_flag = 0x00;
- ts_video_pes->DSM_trick_mode_flag = 0x00;
- ts_video_pes->additional_copy_info_flag = 0x00;
- ts_video_pes->PES_CRC_flag = 0x00;
- ts_video_pes->PES_extension_flag = 0x00;
- ts_video_pes->PES_header_data_length = 0x0a; //后面的数据 包括了 PTS和 DTS所占的字节数
- //清 0
- ts_video_pes->tsptsdts.pts_32_30 = 0;
- ts_video_pes->tsptsdts.pts_29_15 = 0;
- ts_video_pes->tsptsdts.pts_14_0 = 0;
- ts_video_pes->tsptsdts.dts_32_30 = 0;
- ts_video_pes->tsptsdts.dts_29_15 = 0;
- ts_video_pes->tsptsdts.dts_14_0 = 0;
- ts_video_pes->tsptsdts.reserved_1 = 0x0003; //填写 pts信息
- // Videopts大于30bit,使用最高三位
- if(Videopts > 0x7FFFFFFF)
- {
- ts_video_pes->tsptsdts.pts_32_30 = (Videopts >> 30) & 0x07;
- ts_video_pes->tsptsdts.marker_bit1 = 0x01;
- }
- else
- {
- ts_video_pes->tsptsdts.marker_bit1 = 0;
- }
- // Videopts大于15bit,使用更多的位来存储
- if(Videopts > 0x7FFF)
- {
- ts_video_pes->tsptsdts.pts_29_15 = (Videopts >> 15) & 0x007FFF ;
- ts_video_pes->tsptsdts.marker_bit2 = 0x01;
- }
- else
- {
- ts_video_pes->tsptsdts.marker_bit2 = 0;
- }
- //使用最后15位
- ts_video_pes->tsptsdts.pts_14_0 = Videopts & 0x007FFF;
- ts_video_pes->tsptsdts.marker_bit3 = 0x01;
- ts_video_pes->tsptsdts.reserved_2 = 0x0001; //填写 dts信息
- // Videopts大于30bit,使用最高三位
- if(Videodts > 0x7FFFFFFF)
- {
- ts_video_pes->tsptsdts.dts_32_30 = (Videodts >> 30) & 0x07;
- ts_video_pes->tsptsdts.marker_bit4 = 0x01;
- }
- else
- {
- ts_video_pes->tsptsdts.marker_bit4 = 0;
- }
- // Videopts大于15bit,使用更多的位来存储
- if(Videodts > 0x7FFF)
- {
- ts_video_pes->tsptsdts.dts_29_15 = (Videodts >> 15) & 0x007FFF ;
- ts_video_pes->tsptsdts.marker_bit5 = 0x01;
- }
- else
- {
- ts_video_pes->tsptsdts.marker_bit5 = 0;
- }
- //使用最后15位
- ts_video_pes->tsptsdts.dts_14_0 = Videodts & 0x007FFF;
- ts_video_pes->tsptsdts.marker_bit6 = 0x01;
- memcpy(ts_video_pes->Es,OneFrameBuf_H264,OneFrameLen_H264);
- //将数据存入新的H264 文件检测 中间步骤是否正确
- fwrite(ts_video_pes->Es,OneFrameLen_H264,1,FOutNEWH264);
- WriteAdaptive_flags_Head(&ts_adaptation_field_Head); //填写自适应段标志帧头
- WriteAdaptive_flags_Tail(&ts_adaptation_field_Tail); //填写自适应段标志帧尾
- EnterCriticalSection(&Cs);
- PES2TS(ts_video_pes,TS_H264_PID,&ts_adaptation_field_Head,&ts_adaptation_field_Tail);
- LeaveCriticalSection(&Cs);
- return 1;
- }