1
|
1 //=================== VIDEO PARSER =========================
|
|
2
|
|
3 #define MAX_VIDEO_PACKET_SIZE (224*1024+4)
|
|
4 #define VIDEOBUFFER_SIZE 0x100000
|
|
5
|
|
6 //static unsigned char videobuffer[MAX_VIDEO_PACKET_SIZE];
|
|
7 static unsigned char* videobuffer=NULL;
|
|
8 static int videobuf_len=0;
|
|
9
|
|
10 static unsigned char videobuf_code[4];
|
|
11 static int videobuf_code_len=0;
|
|
12
|
|
13 // sync video stream, and returns next packet code
|
|
14 int sync_video_packet(demux_stream_t *ds){
|
|
15 int skipped=0;
|
|
16 // we need enough bytes in the buffer:
|
|
17 while(videobuf_code_len<4){
|
|
18 #if 0
|
|
19 int c;
|
|
20 c=demux_getc(ds);if(c<0){ return 0;} // EOF
|
|
21 videobuf_code[videobuf_code_len++]=c;
|
|
22 #else
|
|
23 videobuf_code[videobuf_code_len++]=demux_getc(ds);
|
|
24 #endif
|
|
25 }
|
|
26 // sync packet:
|
|
27 while(1){
|
|
28 int c;
|
|
29 if(videobuf_code[0]==0 &&
|
|
30 videobuf_code[1]==0 &&
|
|
31 videobuf_code[2]==1) break; // synced
|
|
32 // shift buffer, drop first byte
|
|
33 ++skipped;
|
|
34 videobuf_code[0]=videobuf_code[1];
|
|
35 videobuf_code[1]=videobuf_code[2];
|
|
36 videobuf_code[2]=videobuf_code[3];
|
|
37 c=demux_getc(ds);if(c<0){ return 0;} // EOF
|
|
38 videobuf_code[3]=c;
|
|
39 }
|
400
|
40 if(verbose>=2) if(skipped) printf("videobuf: %d bytes skipped (next: 0x1%02X)\n",skipped,videobuf_code[3]);
|
1
|
41 return 0x100|videobuf_code[3];
|
|
42 }
|
|
43
|
|
44 // return: packet length
|
|
45 int read_video_packet(demux_stream_t *ds){
|
|
46 int packet_start;
|
|
47
|
|
48 // SYNC STREAM
|
|
49 // if(!sync_video_packet(ds)) return 0; // cannot sync (EOF)
|
|
50
|
|
51 // COPY STARTCODE:
|
|
52 packet_start=videobuf_len;
|
|
53 videobuffer[videobuf_len+0]=videobuf_code[0];
|
|
54 videobuffer[videobuf_len+1]=videobuf_code[1];
|
|
55 videobuffer[videobuf_len+2]=videobuf_code[2];
|
|
56 videobuffer[videobuf_len+3]=videobuf_code[3];
|
|
57 videobuf_len+=4;
|
|
58
|
|
59 // READ PACKET:
|
|
60 { unsigned int head=-1;
|
|
61 while(videobuf_len<VIDEOBUFFER_SIZE){
|
|
62 int c=demux_getc(ds);
|
|
63 if(c<0) break; // EOF
|
|
64 videobuffer[videobuf_len++]=c;
|
|
65 #if 1
|
|
66 head<<=8;
|
|
67 if(head==0x100) break; // synced
|
|
68 head|=c;
|
|
69 #else
|
|
70 if(videobuffer[videobuf_len-4]==0 &&
|
|
71 videobuffer[videobuf_len-3]==0 &&
|
|
72 videobuffer[videobuf_len-2]==1) break; // synced
|
|
73 #endif
|
|
74 }
|
|
75 }
|
|
76
|
|
77 if(ds->eof){
|
|
78 videobuf_code_len=0; // EOF, no next code
|
|
79 return videobuf_len-packet_start;
|
|
80 }
|
|
81
|
|
82 videobuf_len-=4;
|
|
83
|
400
|
84 if(verbose>=2) printf("videobuf: packet 0x1%02X len=%d (total=%d)\n",videobuffer[packet_start+3],videobuf_len-packet_start,videobuf_len);
|
1
|
85
|
|
86 // Save next packet code:
|
|
87 videobuf_code[0]=videobuffer[videobuf_len];
|
|
88 videobuf_code[1]=videobuffer[videobuf_len+1];
|
|
89 videobuf_code[2]=videobuffer[videobuf_len+2];
|
|
90 videobuf_code[3]=videobuffer[videobuf_len+3];
|
|
91 videobuf_code_len=4;
|
|
92
|
|
93 return videobuf_len-packet_start;
|
|
94 }
|
|
95
|
|
96 // return: next packet code
|
|
97 int skip_video_packet(demux_stream_t *ds){
|
|
98
|
|
99 // SYNC STREAM
|
|
100 // if(!sync_video_packet(ds)) return 0; // cannot sync (EOF)
|
|
101
|
|
102 videobuf_code_len=0; // force resync
|
|
103
|
|
104 // SYNC AGAIN:
|
|
105 return sync_video_packet(ds);
|
|
106 }
|