#include <stdio.h>
#define byte unsigned char
byte buf[1024];
int pos = 0;
FILE *oldfp, *videofp,*audiofp;
byte *p_buf = buf;
int i,n;
#define program_stream_map 0xBC
#define padding_stream 0xBE
#define private_stream_2 0xBF
#define ECM 0xF0
#define EMM 0xF1
#define program_stream_directory 0xFF
#define DSMCC_stream 0xF2
#define ITU_T_REC_type_E_stream 0xF8
/*#define fast_forward 0x000
#define slow_motion 0x001
#define freeze_frame 0x010
#define fast_reverse 0x011
#define slow_reverse 0x100*/
int PES_packet_length;
byte stream_id;
byte PTS_DTS_flags=0;
byte ESCR_flag=0;
byte ES_rate_flag=0;
byte DSM_trick_mode_flag=0;
byte additional_copy_info_flag=0;
byte PES_CRC_flag=0;
byte PES_extension_flag=0;
//byte trick_mode_control;
byte PES_private_data_flag=0;
byte pack_header_field_flag=0;
byte program_packet_sequence_counter_flag=0;
byte PSTD_buffer_flag=0;
byte PES_extension_flag_2=0;
int original_stuff_length=0;
byte PES_extension_field_length;
int N1,N2;
int PES_header_data_length;
int choicenum;
void read_next_1024()
{
if(pos == 1021) {
buf[0] = p_buf[0]; buf[1] = p_buf[1]; buf[2] = p_buf[2];
fread(buf+3, 1, 1021, oldfp);
p_buf = buf;
pos = 0;
}
else if(pos == 1022){
buf[0] = p_buf[0]; buf[1] = p_buf[1];
fread(buf+2, 1, 1022, oldfp);
p_buf = buf;
pos = 0;
}
else if(pos == 1023){
buf[0] = p_buf[0];
fread(buf+1, 1, 1023, oldfp);
p_buf = buf;
pos = 0;
}
else if(pos == 1024){
fread(buf+0, 1, 1024, oldfp);
p_buf = buf;
pos = 0;
}
}
void pack_header(FILE *fp)
{
original_stuff_length=0;
choicenum=0;
n=0;
p_buf+=3;
pos+=3;
read_next_1024();
stream_id=p_buf[0];
PES_packet_length=(p_buf[1]<<8)+p_buf[2];
p_buf+=3;
pos+=3;
read_next_1024();
/*if((stream_id != program_stream_map)
&& (stream_id !=padding_stream)
&& (stream_id !=private_stream_2)
&& (stream_id !=ECM)
&& (stream_id !=EMM)
&& (stream_id !=program_stream_directory)
&& (stream_id !=DSMCC_stream)
&& (stream_id !=ITU_T_REC_type_E_stream))*/
//{// start stream_id
PTS_DTS_flags=(p_buf[1]>>6)&0x03;
ESCR_flag=p_buf[1]&(0x01<<5);
ES_rate_flag=p_buf[1]&(0x01<<4);
DSM_trick_mode_flag=p_buf[1]&(0x01<<3);
additional_copy_info_flag=p_buf[1]&(0x01<<2);
PES_CRC_flag=p_buf[1]&(0x01<<1);
PES_extension_flag=p_buf[1]&0x01;
PES_header_data_length=p_buf[2];
p_buf+=3;
pos+=3;
n+=3;
read_next_1024();
//printf("%0x\t%0x\t%0x\n",p_buf[0],p_buf[1],p_buf[2]);
if(PTS_DTS_flags ==0x02){
for(i=0;i<5;i++)
{p_buf++;pos++;n++;choicenum++;read_next_1024();}
}
if(PTS_DTS_flags ==0x03){
for(i=0;i<10;i++)
{p_buf++;pos++;n++;choicenum++;read_next_1024();}
}
if(ESCR_flag!=0){
for(i=0;i<6;i++)
{p_buf++;pos++;n++;choicenum++;read_next_1024();}
}
if(ES_rate_flag!=0){
for(i=0;i<3;i++)
{p_buf++;pos++;n++;choicenum++;read_next_1024();}
}
if (DSM_trick_mode_flag!=0)
{p_buf++;pos++;n++;choicenum++;read_next_1024();}
if ( additional_copy_info_flag!=0)
{p_buf++;pos++;n++;choicenum++;read_next_1024();}
if (PES_CRC_flag!=0)
{p_buf+=2;pos+=2;n++;choicenum++;read_next_1024();}
if ( PES_extension_flag!=0)
{
PES_private_data_flag=p_buf[0]&(0x01<<7);
pack_header_field_flag=p_buf[0]&(0x01<<6);
program_packet_sequence_counter_flag=p_buf[0]&(0x01<<5);
PSTD_buffer_flag=p_buf[0]&(0x01<<4);
PES_extension_flag_2=p_buf[0]&0x01;
p_buf++;pos++;n++;choicenum++;read_next_1024();
if(PES_private_data_flag !=0)
{
for(i=0;i<16;i++)
{p_buf++;pos++;n++;choicenum++;read_next_1024();}
}
if (pack_header_field_flag!=0)
{
p_buf++;pos++;n++;choicenum++;read_next_1024();
return;
}
if (program_packet_sequence_counter_flag !=0)
{
original_stuff_length=p_buf[1]&0x3f;
p_buf+=2;pos+=2;n+=2;choicenum+=2;read_next_1024();
}
if (PSTD_buffer_flag!=0)
{
p_buf+=2;pos+=2;n+=2;choicenum+=2;read_next_1024();
}
if (PES_extension_flag_2!=0)
{
PES_extension_field_length=p_buf[0]&0x7f;
p_buf++;pos++;n++;choicenum++;read_next_1024();
for(i=0;i<PES_extension_field_length;i++)
{p_buf++;pos++;n++;choicenum++;read_next_1024();}
}
}
N1=PES_header_data_length-choicenum;
for(i=0;i<N1;i++)
{p_buf++;pos++;n++;read_next_1024();}
N2=PES_packet_length-n;
for(i=0;i<N2;i++)
{fwrite(p_buf,1,1,fp);p_buf++;pos++;read_next_1024();}
}
void jumpt_system()
{
p_buf+=4;
pos+=4;
read_next_1024();
while(!(p_buf[0]==0&&p_buf[1]==0&&p_buf[2]==1&&(p_buf[3]>=0xb8||p_buf[3]==0xb1)))
{
p_buf++;
pos++;
read_next_1024();
}
}
void main()
{
if ((oldfp =fopen("part00.spts","rb"))==NULL)
printf("can't open input file test.avs\n");
if ((videofp=fopen("video.avs","wb"))==NULL)
printf("can't open output file videoes.avs\n");
if ((audiofp=fopen("audioes.avs","wb"))==NULL)
printf("can't open output file audioes.avs\n");
fread(p_buf, 1, 1024, oldfp);
while(1)
{
loop:
if((p_buf[0]==0 && p_buf[1]==0 && p_buf[2]==1 && p_buf[3]==0xB1)) {fwrite(p_buf,1,4,videofp);return;}
else if(p_buf[0]==0&&p_buf[1]==0&&p_buf[2]==1&&p_buf[3]!=0xE0&&p_buf[3]!=0xc0)
{
jumpt_system();
goto loop;
}
else if(p_buf[0]==0&&p_buf[1]==0&&p_buf[2]==1&&p_buf[3]==0xE0)//video 求出填充位后面,分组包中的数据
{
pack_header(videofp);
goto loop;
}// end else if 001e0
else if(p_buf[0]==0&&p_buf[1]==0&&p_buf[2]==1&&p_buf[3]==0xc0)//audio 求出填充位后面,分组包中的数据
{
pack_header(audiofp);
goto loop;
}// end else if 001c0
else {
printf("\n error \n");printf("\n%d\t%d\t%d\n",p_buf[0],p_buf[1],p_buf[2]);
}
}// end while
fclose(oldfp);
fclose(videofp);
printf("press any key to continue");
}
h264,ps流转es流
4星 · 超过85%的资源 需积分: 13 35 浏览量
2012-08-22
14:02:51
上传
评论 5
收藏 254KB RAR 举报
UnkownState
- 粉丝: 61
- 资源: 13
- 1
- 2
- 3
- 4
- 5
- 6
前往页