Mercurial > mplayer.hg
annotate demux_mpg.c @ 549:c97496b240c1
new options: -dumpaudio and -dumpvideo
author | arpi_esp |
---|---|
date | Fri, 20 Apr 2001 18:28:13 +0000 |
parents | 3e46eab9f782 |
children | c2a449f90087 |
rev | line source |
---|---|
1 | 1 // MPG/VOB file parser for DEMUXER v2.5 by A'rpi/ESP-team |
2 | |
524
9105fc95636c
A fast'n'ugly hack to correct DVD VOB playback problems
lgb
parents:
501
diff
changeset
|
3 //#define MAX_PS_PACKETSIZE 2048 |
501 | 4 #define MAX_PS_PACKETSIZE (224*1024) |
5 | |
547 | 6 static int mpeg_pts_error=0; |
7 | |
1 | 8 static unsigned int read_mpeg_timestamp(stream_t *s,int c){ |
9 int d,e; | |
10 unsigned int pts; | |
11 d=stream_read_word(s); | |
12 e=stream_read_word(s); | |
547 | 13 if( ((c&1)!=1) || ((d&1)!=1) || ((e&1)!=1) ){ |
14 ++mpeg_pts_error; | |
15 return 0; // invalid pts | |
16 } | |
1 | 17 pts=(((c>>1)&7)<<30)|((d>>1)<<15)|(e>>1); |
18 if(verbose>=3) printf("{%d}",pts); | |
19 return pts; | |
20 } | |
21 | |
501 | 22 //static char dvdaudio_table[256]; |
1 | 23 //static unsigned int packet_start_pos=0; |
24 | |
25 static int demux_mpg_read_packet(demuxer_t *demux,int id){ | |
26 int d; | |
27 int len; | |
492 | 28 #ifdef HAVE_LIBCSS |
29 int css=0; | |
30 #endif | |
1 | 31 unsigned char c=0; |
32 unsigned int pts=0; | |
33 unsigned int dts=0; | |
34 demux_stream_t *ds=NULL; | |
35 | |
36 if(verbose>=3) printf("demux_read_packet: %X\n",id); | |
37 | |
536 | 38 // if(id==0x1F0){ |
39 // demux->synced=0; // force resync after 0x1F0 | |
40 // return -1; | |
41 //} | |
501 | 42 |
1 | 43 // if(id==0x1BA) packet_start_pos=stream_tell(demux->stream); |
501 | 44 if(id<0x1BC || id>=0x1F0) return -1; |
1 | 45 if(id==0x1BE) return -1; // padding stream |
46 if(id==0x1BF) return -1; // private2 | |
47 | |
48 len=stream_read_word(demux->stream); | |
49 if(verbose>=3) printf("PACKET len=%d",len); | |
536 | 50 // if(len==62480){ demux->synced=0;return -1;} /* :) */ |
501 | 51 // if(len==0 || len>MAX_PS_PACKETSIZE) return -2; // invalid packet !!!!!! |
52 if(len==0 || len>MAX_PS_PACKETSIZE){ | |
53 if(verbose>=2) printf("Invalid PS packet len: %d\n",len); | |
54 return -2; // invalid packet !!!!!! | |
55 } | |
1 | 56 |
547 | 57 mpeg_pts_error=0; |
58 | |
1 | 59 while(len>0){ // Skip stuFFing bytes |
60 c=stream_read_char(demux->stream);--len; | |
61 if(c!=0xFF)break; | |
62 } | |
63 if((c>>6)==1){ // Read (skip) STD scale & size value | |
64 // printf(" STD_scale=%d",(c>>5)&1); | |
65 d=((c&0x1F)<<8)|stream_read_char(demux->stream); | |
66 len-=2; | |
67 // printf(" STD_size=%d",d); | |
68 c=stream_read_char(demux->stream); | |
69 } | |
70 // Read System-1 stream timestamps: | |
71 if((c>>4)==2){ | |
72 pts=read_mpeg_timestamp(demux->stream,c); | |
73 len-=4; | |
74 } else | |
75 if((c>>4)==3){ | |
76 pts=read_mpeg_timestamp(demux->stream,c); | |
77 c=stream_read_char(demux->stream); | |
78 if((c>>4)!=1) pts=0; //printf("{ERROR4}"); | |
79 dts=read_mpeg_timestamp(demux->stream,c); | |
80 len-=4+1+4; | |
81 } else | |
82 if((c>>6)==2){ | |
83 int pts_flags; | |
84 int hdrlen; | |
85 // System-2 (.VOB) stream: | |
492 | 86 if((c>>4)&3) { |
87 #ifdef HAVE_LIBCSS | |
88 css=1; | |
89 #else | |
90 printf("Encrypted VOB file (not compiled with libcss support)! Read file DOCS/DVD\n"); | |
91 #endif | |
92 } | |
1 | 93 c=stream_read_char(demux->stream); pts_flags=c>>6; |
94 c=stream_read_char(demux->stream); hdrlen=c; | |
95 len-=2; | |
96 if(verbose>=3) printf(" hdrlen=%d (len=%d)",hdrlen,len); | |
242 | 97 if(hdrlen>len){ printf("demux_mpg: invalid header length \n"); return -1;} |
1 | 98 if(pts_flags==2){ |
99 c=stream_read_char(demux->stream); | |
100 pts=read_mpeg_timestamp(demux->stream,c); | |
101 len-=5;hdrlen-=5; | |
102 } else | |
103 if(pts_flags==3){ | |
104 c=stream_read_char(demux->stream); | |
105 pts=read_mpeg_timestamp(demux->stream,c); | |
106 c=stream_read_char(demux->stream); | |
107 dts=read_mpeg_timestamp(demux->stream,c); | |
108 len-=10;hdrlen-=10; | |
109 } | |
110 len-=hdrlen; | |
111 if(hdrlen>0) stream_skip(demux->stream,hdrlen); // skip header bytes | |
112 | |
113 //============== DVD Audio sub-stream ====================== | |
114 if(id==0x1BD){ | |
426 | 115 int aid=128+(stream_read_char(demux->stream)&0x7F);--len; |
1 | 116 if(len<3) return -1; // invalid audio packet |
426 | 117 |
118 if(!avi_header.a_streams[aid]) new_sh_audio(aid); | |
119 if(demux->audio->id==-1) demux->audio->id=aid; | |
120 | |
121 if(demux->audio->id==aid){ | |
536 | 122 // int type; |
426 | 123 ds=demux->audio; |
124 if(!ds->sh) ds->sh=avi_header.a_streams[aid]; | |
1 | 125 // READ Packet: Skip additional audio header data: |
536 | 126 c=stream_read_char(demux->stream);//type=c; |
127 c=stream_read_char(demux->stream);//type|=c<<8; | |
128 c=stream_read_char(demux->stream);//type|=c<<16; | |
129 // printf("[%06X]",type); | |
1 | 130 len-=3; |
131 if(ds->type==-1){ | |
132 // autodetect type | |
133 ds->type=((aid&0x70)==0x20)?2:3; | |
134 } | |
135 if(ds->type==2 && len>=2){ | |
136 // read PCM header | |
137 int head; | |
138 head=stream_read_char(demux->stream); head=c<<8; | |
139 c=stream_read_char(demux->stream); head|=c; len-=2; | |
140 while(len>0 && head!=0x180){ | |
141 head=c<<8; | |
142 c=stream_read_char(demux->stream); | |
143 head|=c;--len; | |
144 } | |
145 if(!len) printf("End of packet while searching for PCM header\n"); | |
146 } | |
147 } | |
148 } | |
149 | |
150 } else { | |
539 | 151 if(c!=0x0f){ |
541
b382156bff40
Embarrassing messages while playing DVD are moved to verbose level 1
lgb
parents:
539
diff
changeset
|
152 if (verbose>=1) printf(" {ERROR5,c=%d} \n",c); |
539 | 153 return -1; // invalid packet !!!!!! |
154 } | |
1 | 155 } |
547 | 156 if(mpeg_pts_error) printf(" {PTS_err:%d} \n",mpeg_pts_error); |
242 | 157 if(verbose>=3) printf(" => len=%d\n",len); |
1 | 158 |
501 | 159 // if(len<=0 || len>MAX_PS_PACKETSIZE) return -1; // Invalid packet size |
160 if(len<=0 || len>MAX_PS_PACKETSIZE){ | |
161 if(verbose>=2) printf("Invalid PS data len: %d\n",len); | |
162 return -1; // invalid packet !!!!!! | |
163 } | |
1 | 164 |
165 if(id>=0x1C0 && id<=0x1DF){ | |
166 // mpeg audio | |
167 int aid=id-0x1C0; | |
426 | 168 if(!avi_header.a_streams[aid]) new_sh_audio(aid); |
1 | 169 if(demux->audio->id==-1) demux->audio->id=aid; |
170 if(demux->audio->id==aid){ | |
171 ds=demux->audio; | |
426 | 172 if(!ds->sh) ds->sh=avi_header.a_streams[aid]; |
1 | 173 if(ds->type==-1) ds->type=1; |
174 } | |
175 } else | |
176 if(id>=0x1E0 && id<=0x1EF){ | |
177 // mpeg video | |
178 int aid=id-0x1E0; | |
426 | 179 if(!avi_header.v_streams[aid]) new_sh_video(aid); |
1 | 180 if(demux->video->id==-1) demux->video->id=aid; |
426 | 181 if(demux->video->id==aid){ |
182 ds=demux->video; | |
183 if(!ds->sh) ds->sh=avi_header.v_streams[aid]; | |
184 } | |
1 | 185 } |
186 | |
187 if(ds){ | |
188 if(verbose>=2) printf("DEMUX_MPG: Read %d data bytes from packet %04X\n",len,id); | |
189 // printf("packet start = 0x%X \n",stream_tell(demux->stream)-packet_start_pos); | |
492 | 190 #ifdef HAVE_LIBCSS |
546 | 191 if (css) { |
192 if (descrambling) CSSDescramble(demux->stream->buffer,key_title); else | |
193 printf("Encrypted stream but authentication was not requested by you!!\n"); | |
194 } | |
492 | 195 #endif |
1 | 196 ds_read_packet(ds,demux->stream,len,pts/90000.0f,0); |
197 return 1; | |
198 } | |
199 if(verbose>=2) printf("DEMUX_MPG: Skipping %d data bytes from packet %04X\n",len,id); | |
536 | 200 if(len<=2356) stream_skip(demux->stream,len); |
1 | 201 return 0; |
202 } | |
203 | |
204 static int num_elementary_packets100=0; | |
205 static int num_elementary_packets101=0; | |
206 | |
207 int demux_mpg_es_fill_buffer(demuxer_t *demux){ | |
208 //if(demux->type==DEMUXER_TYPE_MPEG_ES) | |
209 // Elementary video stream | |
210 if(demux->stream->eof) return 0; | |
211 demux->filepos=stream_tell(demux->stream); | |
501 | 212 ds_read_packet(demux->video,demux->stream,STREAM_BUFFER_SIZE,0,0); |
1 | 213 return 1; |
214 } | |
215 | |
216 int demux_mpg_fill_buffer(demuxer_t *demux){ | |
217 unsigned int head=0; | |
218 int skipped=0; | |
219 int max_packs=128; | |
220 int ret=0; | |
221 | |
222 // System stream | |
223 do{ | |
224 demux->filepos=stream_tell(demux->stream); | |
225 head=stream_read_dword(demux->stream); | |
547 | 226 if((head&0xFFFFFF00)!=0x100){ |
227 // sync... | |
228 demux->filepos-=skipped; | |
229 while(1){ | |
536 | 230 int c=stream_read_char(demux->stream); |
231 if(c<0) break; //EOF | |
232 head<<=8; | |
233 if(head!=0x100){ | |
234 head|=c; | |
235 ++skipped; //++demux->filepos; | |
236 continue; | |
237 } | |
238 head|=c; | |
239 break; | |
547 | 240 } |
241 demux->filepos+=skipped; | |
1 | 242 } |
243 if(stream_eof(demux->stream)) break; | |
244 // sure: head=0x000001XX | |
524
9105fc95636c
A fast'n'ugly hack to correct DVD VOB playback problems
lgb
parents:
501
diff
changeset
|
245 if(verbose>=4) printf("*** head=0x%X\n",head); |
1 | 246 if(demux->synced==0){ |
247 if(head==0x1BA) demux->synced=1; | |
248 } else | |
249 if(demux->synced==1){ | |
250 if(head==0x1BB || (head>=0x1C0 && head<=0x1EF)){ | |
251 demux->synced=2; | |
252 if(verbose) printf("system stream synced at 0x%X (%d)!\n",demux->filepos,demux->filepos); | |
536 | 253 num_elementary_packets100=0; // requires for re-sync! |
254 num_elementary_packets101=0; // requires for re-sync! | |
1 | 255 } else demux->synced=0; |
256 } // else | |
257 if(demux->synced==2){ | |
258 ret=demux_mpg_read_packet(demux,head); | |
259 if(!ret) | |
260 if(--max_packs==0){ | |
261 demux->stream->eof=1; | |
262 printf("demux: file doesn't contain the selected audio or video stream\n"); | |
263 return 0; | |
264 } | |
265 } else { | |
266 if(head>=0x100 && head<0x1B0){ | |
267 if(head==0x100) | |
268 ++num_elementary_packets100; | |
269 else | |
270 if(head==0x101) ++num_elementary_packets101; | |
271 if(verbose>=3) printf("Opps... elementary video packet found: %03X\n",head); | |
272 } | |
273 #if 1 | |
274 if(num_elementary_packets100>50 && num_elementary_packets101>50 | |
275 && skipped>4000000){ | |
276 if(verbose) printf("sync_mpeg_ps: seems to be ES stream...\n"); | |
277 demux->stream->eof=1; | |
278 break; | |
279 } | |
280 #endif | |
281 } | |
282 } while(ret!=1); | |
283 if(verbose>=2) printf("demux: %d bad bytes skipped\n",skipped); | |
284 if(demux->stream->eof){ | |
285 if(verbose>=2) printf("MPEG Stream reached EOF\n"); | |
286 return 0; | |
287 } | |
288 return 1; | |
289 } | |
290 |