1
|
1 // MPG/VOB file parser for DEMUXER v2.5 by A'rpi/ESP-team
|
|
2
|
|
3 static unsigned int read_mpeg_timestamp(stream_t *s,int c){
|
|
4 int d,e;
|
|
5 unsigned int pts;
|
|
6 d=stream_read_word(s);
|
|
7 e=stream_read_word(s);
|
|
8 if( ((c&1)!=1) || ((d&1)!=1) || ((e&1)!=1) ) return 0; // invalid pts
|
|
9 pts=(((c>>1)&7)<<30)|((d>>1)<<15)|(e>>1);
|
|
10 if(verbose>=3) printf("{%d}",pts);
|
|
11 return pts;
|
|
12 }
|
|
13
|
|
14 static char dvdaudio_table[256];
|
|
15 //static unsigned int packet_start_pos=0;
|
|
16
|
|
17 static int demux_mpg_read_packet(demuxer_t *demux,int id){
|
|
18 int d;
|
|
19 int len;
|
492
|
20 #ifdef HAVE_LIBCSS
|
|
21 int css=0;
|
|
22 #endif
|
1
|
23 unsigned char c=0;
|
|
24 unsigned int pts=0;
|
|
25 unsigned int dts=0;
|
|
26 demux_stream_t *ds=NULL;
|
|
27
|
|
28 if(verbose>=3) printf("demux_read_packet: %X\n",id);
|
|
29
|
|
30 // if(id==0x1BA) packet_start_pos=stream_tell(demux->stream);
|
|
31 if(id<0x1BC || id>0x1FF) return -1;
|
|
32 if(id==0x1BE) return -1; // padding stream
|
|
33 if(id==0x1BF) return -1; // private2
|
|
34
|
|
35 len=stream_read_word(demux->stream);
|
|
36 if(verbose>=3) printf("PACKET len=%d",len);
|
494
|
37 if(len==0 || len>4096) return -2; // invalid packet !!!!!!
|
1
|
38
|
|
39 while(len>0){ // Skip stuFFing bytes
|
|
40 c=stream_read_char(demux->stream);--len;
|
|
41 if(c!=0xFF)break;
|
|
42 }
|
|
43 if((c>>6)==1){ // Read (skip) STD scale & size value
|
|
44 // printf(" STD_scale=%d",(c>>5)&1);
|
|
45 d=((c&0x1F)<<8)|stream_read_char(demux->stream);
|
|
46 len-=2;
|
|
47 // printf(" STD_size=%d",d);
|
|
48 c=stream_read_char(demux->stream);
|
|
49 }
|
|
50 // Read System-1 stream timestamps:
|
|
51 if((c>>4)==2){
|
|
52 pts=read_mpeg_timestamp(demux->stream,c);
|
|
53 len-=4;
|
|
54 } else
|
|
55 if((c>>4)==3){
|
|
56 pts=read_mpeg_timestamp(demux->stream,c);
|
|
57 c=stream_read_char(demux->stream);
|
|
58 if((c>>4)!=1) pts=0; //printf("{ERROR4}");
|
|
59 dts=read_mpeg_timestamp(demux->stream,c);
|
|
60 len-=4+1+4;
|
|
61 } else
|
|
62 if((c>>6)==2){
|
|
63 int pts_flags;
|
|
64 int hdrlen;
|
|
65 // System-2 (.VOB) stream:
|
492
|
66 if((c>>4)&3) {
|
|
67 #ifdef HAVE_LIBCSS
|
|
68 css=1;
|
|
69 #else
|
|
70 printf("Encrypted VOB file (not compiled with libcss support)! Read file DOCS/DVD\n");
|
|
71 #endif
|
|
72 }
|
1
|
73 c=stream_read_char(demux->stream); pts_flags=c>>6;
|
|
74 c=stream_read_char(demux->stream); hdrlen=c;
|
|
75 len-=2;
|
|
76 if(verbose>=3) printf(" hdrlen=%d (len=%d)",hdrlen,len);
|
242
|
77 if(hdrlen>len){ printf("demux_mpg: invalid header length \n"); return -1;}
|
1
|
78 if(pts_flags==2){
|
|
79 c=stream_read_char(demux->stream);
|
|
80 pts=read_mpeg_timestamp(demux->stream,c);
|
|
81 len-=5;hdrlen-=5;
|
|
82 } else
|
|
83 if(pts_flags==3){
|
|
84 c=stream_read_char(demux->stream);
|
|
85 pts=read_mpeg_timestamp(demux->stream,c);
|
|
86 c=stream_read_char(demux->stream);
|
|
87 dts=read_mpeg_timestamp(demux->stream,c);
|
|
88 len-=10;hdrlen-=10;
|
|
89 }
|
|
90 len-=hdrlen;
|
|
91 if(hdrlen>0) stream_skip(demux->stream,hdrlen); // skip header bytes
|
|
92
|
|
93 //============== DVD Audio sub-stream ======================
|
|
94 if(id==0x1BD){
|
426
|
95 int aid=128+(stream_read_char(demux->stream)&0x7F);--len;
|
1
|
96 if(len<3) return -1; // invalid audio packet
|
426
|
97
|
|
98 if(!avi_header.a_streams[aid]) new_sh_audio(aid);
|
|
99 if(demux->audio->id==-1) demux->audio->id=aid;
|
|
100
|
|
101 if(demux->audio->id==aid){
|
|
102 ds=demux->audio;
|
|
103 if(!ds->sh) ds->sh=avi_header.a_streams[aid];
|
1
|
104 // READ Packet: Skip additional audio header data:
|
|
105 c=stream_read_char(demux->stream);
|
|
106 c=stream_read_char(demux->stream);
|
|
107 c=stream_read_char(demux->stream);
|
|
108 len-=3;
|
|
109 if(ds->type==-1){
|
|
110 // autodetect type
|
|
111 ds->type=((aid&0x70)==0x20)?2:3;
|
|
112 }
|
|
113 if(ds->type==2 && len>=2){
|
|
114 // read PCM header
|
|
115 int head;
|
|
116 head=stream_read_char(demux->stream); head=c<<8;
|
|
117 c=stream_read_char(demux->stream); head|=c; len-=2;
|
|
118 while(len>0 && head!=0x180){
|
|
119 head=c<<8;
|
|
120 c=stream_read_char(demux->stream);
|
|
121 head|=c;--len;
|
|
122 }
|
|
123 if(!len) printf("End of packet while searching for PCM header\n");
|
|
124 }
|
|
125 }
|
|
126 }
|
|
127
|
|
128 } else {
|
|
129 //if(c!=0x0f) printf(" {ERROR5,c=%d} \n",c);
|
|
130 }
|
242
|
131 if(verbose>=3) printf(" => len=%d\n",len);
|
1
|
132
|
494
|
133 if(len<=0 || len>4096) return -1; // Invalid packet size
|
1
|
134
|
|
135 if(id>=0x1C0 && id<=0x1DF){
|
|
136 // mpeg audio
|
|
137 int aid=id-0x1C0;
|
426
|
138 if(!avi_header.a_streams[aid]) new_sh_audio(aid);
|
1
|
139 if(demux->audio->id==-1) demux->audio->id=aid;
|
|
140 if(demux->audio->id==aid){
|
|
141 ds=demux->audio;
|
426
|
142 if(!ds->sh) ds->sh=avi_header.a_streams[aid];
|
1
|
143 if(ds->type==-1) ds->type=1;
|
|
144 }
|
|
145 } else
|
|
146 if(id>=0x1E0 && id<=0x1EF){
|
|
147 // mpeg video
|
|
148 int aid=id-0x1E0;
|
426
|
149 if(!avi_header.v_streams[aid]) new_sh_video(aid);
|
1
|
150 if(demux->video->id==-1) demux->video->id=aid;
|
426
|
151 if(demux->video->id==aid){
|
|
152 ds=demux->video;
|
|
153 if(!ds->sh) ds->sh=avi_header.v_streams[aid];
|
|
154 }
|
1
|
155 }
|
|
156
|
|
157 if(ds){
|
|
158 if(verbose>=2) printf("DEMUX_MPG: Read %d data bytes from packet %04X\n",len,id);
|
|
159 // printf("packet start = 0x%X \n",stream_tell(demux->stream)-packet_start_pos);
|
492
|
160 #ifdef HAVE_LIBCSS
|
|
161 if (css) CSSDescramble(demux->stream->buffer,key_title);
|
|
162 #endif
|
1
|
163 ds_read_packet(ds,demux->stream,len,pts/90000.0f,0);
|
|
164 return 1;
|
|
165 }
|
|
166 if(verbose>=2) printf("DEMUX_MPG: Skipping %d data bytes from packet %04X\n",len,id);
|
|
167 stream_skip(demux->stream,len);
|
|
168 return 0;
|
|
169 }
|
|
170
|
|
171 static int num_elementary_packets100=0;
|
|
172 static int num_elementary_packets101=0;
|
|
173
|
|
174 int demux_mpg_es_fill_buffer(demuxer_t *demux){
|
|
175 //if(demux->type==DEMUXER_TYPE_MPEG_ES)
|
|
176 // Elementary video stream
|
|
177 if(demux->stream->eof) return 0;
|
|
178 demux->filepos=stream_tell(demux->stream);
|
494
|
179 ds_read_packet(demux->video,demux->stream,4096,0,0);
|
1
|
180 return 1;
|
|
181 }
|
|
182
|
|
183 int demux_mpg_fill_buffer(demuxer_t *demux){
|
|
184 unsigned int head=0;
|
|
185 int skipped=0;
|
|
186 int max_packs=128;
|
|
187 int ret=0;
|
|
188
|
|
189 // System stream
|
|
190 do{
|
|
191 demux->filepos=stream_tell(demux->stream);
|
|
192 head=stream_read_dword(demux->stream);
|
|
193 while((head&0xffffff00)!=0x00000100){
|
|
194 if(stream_eof(demux->stream)) break;
|
|
195 head=(head<<8)|stream_read_char(demux->stream);
|
|
196 ++skipped; ++demux->filepos;
|
|
197 }
|
|
198 if(stream_eof(demux->stream)) break;
|
|
199 // sure: head=0x000001XX
|
|
200 if(demux->synced==0){
|
|
201 if(head==0x1BA) demux->synced=1;
|
|
202 } else
|
|
203 if(demux->synced==1){
|
|
204 if(head==0x1BB || (head>=0x1C0 && head<=0x1EF)){
|
|
205 demux->synced=2;
|
|
206 if(verbose) printf("system stream synced at 0x%X (%d)!\n",demux->filepos,demux->filepos);
|
|
207 } else demux->synced=0;
|
|
208 } // else
|
|
209 if(demux->synced==2){
|
|
210 ret=demux_mpg_read_packet(demux,head);
|
|
211 if(!ret)
|
|
212 if(--max_packs==0){
|
|
213 demux->stream->eof=1;
|
|
214 printf("demux: file doesn't contain the selected audio or video stream\n");
|
|
215 return 0;
|
|
216 }
|
|
217 } else {
|
|
218 if(head>=0x100 && head<0x1B0){
|
|
219 if(head==0x100)
|
|
220 ++num_elementary_packets100;
|
|
221 else
|
|
222 if(head==0x101) ++num_elementary_packets101;
|
|
223 if(verbose>=3) printf("Opps... elementary video packet found: %03X\n",head);
|
|
224 }
|
|
225 #if 1
|
|
226 if(num_elementary_packets100>50 && num_elementary_packets101>50
|
|
227 && skipped>4000000){
|
|
228 if(verbose) printf("sync_mpeg_ps: seems to be ES stream...\n");
|
|
229 demux->stream->eof=1;
|
|
230 break;
|
|
231 }
|
|
232 #endif
|
|
233 }
|
|
234 } while(ret!=1);
|
|
235 if(verbose>=2) printf("demux: %d bad bytes skipped\n",skipped);
|
|
236 if(demux->stream->eof){
|
|
237 if(verbose>=2) printf("MPEG Stream reached EOF\n");
|
|
238 return 0;
|
|
239 }
|
|
240 return 1;
|
|
241 }
|
|
242
|