Mercurial > mplayer.hg
annotate demux_mpg.c @ 1552:ce8e8c342004
changes made for vo_aa
author | folke |
---|---|
date | Thu, 16 Aug 2001 15:18:46 +0000 |
parents | ad4d402b3d29 |
children | 5c7760aa4f94 |
rev | line source |
---|---|
1 | 1 // MPG/VOB file parser for DEMUXER v2.5 by A'rpi/ESP-team |
2 | |
587
8511095c5283
stage#1 completed: c files no more included from mplayer.c
arpi_esp
parents:
554
diff
changeset
|
3 #include <stdio.h> |
8511095c5283
stage#1 completed: c files no more included from mplayer.c
arpi_esp
parents:
554
diff
changeset
|
4 #include <stdlib.h> |
1430 | 5 #include <unistd.h> |
587
8511095c5283
stage#1 completed: c files no more included from mplayer.c
arpi_esp
parents:
554
diff
changeset
|
6 |
8511095c5283
stage#1 completed: c files no more included from mplayer.c
arpi_esp
parents:
554
diff
changeset
|
7 extern int verbose; // defined in mplayer.c |
8511095c5283
stage#1 completed: c files no more included from mplayer.c
arpi_esp
parents:
554
diff
changeset
|
8 |
594 | 9 #include "config.h" |
10 #include "dvdauth.h" | |
587
8511095c5283
stage#1 completed: c files no more included from mplayer.c
arpi_esp
parents:
554
diff
changeset
|
11 #include "stream.h" |
8511095c5283
stage#1 completed: c files no more included from mplayer.c
arpi_esp
parents:
554
diff
changeset
|
12 #include "demuxer.h" |
1466 | 13 #include "parse_es.h" |
587
8511095c5283
stage#1 completed: c files no more included from mplayer.c
arpi_esp
parents:
554
diff
changeset
|
14 |
524
9105fc95636c
A fast'n'ugly hack to correct DVD VOB playback problems
lgb
parents:
501
diff
changeset
|
15 //#define MAX_PS_PACKETSIZE 2048 |
501 | 16 #define MAX_PS_PACKETSIZE (224*1024) |
17 | |
547 | 18 static int mpeg_pts_error=0; |
19 | |
1 | 20 static unsigned int read_mpeg_timestamp(stream_t *s,int c){ |
21 int d,e; | |
22 unsigned int pts; | |
23 d=stream_read_word(s); | |
24 e=stream_read_word(s); | |
547 | 25 if( ((c&1)!=1) || ((d&1)!=1) || ((e&1)!=1) ){ |
26 ++mpeg_pts_error; | |
27 return 0; // invalid pts | |
28 } | |
1 | 29 pts=(((c>>1)&7)<<30)|((d>>1)<<15)|(e>>1); |
30 if(verbose>=3) printf("{%d}",pts); | |
31 return pts; | |
32 } | |
33 | |
34 //static unsigned int packet_start_pos=0; | |
35 | |
1466 | 36 //extern void *new_sh_audio(demuxer_t *demux,int id); |
37 //extern void *new_sh_video(demuxer_t *demux,int id); | |
38 #include "wine/mmreg.h" | |
39 #include "wine/avifmt.h" | |
40 #include "wine/vfw.h" | |
41 | |
42 #include "codec-cfg.h" | |
43 #include "stheader.h" | |
605 | 44 |
1 | 45 static int demux_mpg_read_packet(demuxer_t *demux,int id){ |
46 int d; | |
47 int len; | |
492 | 48 #ifdef HAVE_LIBCSS |
49 int css=0; | |
50 #endif | |
1 | 51 unsigned char c=0; |
52 unsigned int pts=0; | |
53 unsigned int dts=0; | |
54 demux_stream_t *ds=NULL; | |
55 | |
56 if(verbose>=3) printf("demux_read_packet: %X\n",id); | |
57 | |
536 | 58 // if(id==0x1F0){ |
59 // demux->synced=0; // force resync after 0x1F0 | |
60 // return -1; | |
61 //} | |
501 | 62 |
1 | 63 // if(id==0x1BA) packet_start_pos=stream_tell(demux->stream); |
501 | 64 if(id<0x1BC || id>=0x1F0) return -1; |
1 | 65 if(id==0x1BE) return -1; // padding stream |
66 if(id==0x1BF) return -1; // private2 | |
67 | |
68 len=stream_read_word(demux->stream); | |
69 if(verbose>=3) printf("PACKET len=%d",len); | |
536 | 70 // if(len==62480){ demux->synced=0;return -1;} /* :) */ |
501 | 71 if(len==0 || len>MAX_PS_PACKETSIZE){ |
72 if(verbose>=2) printf("Invalid PS packet len: %d\n",len); | |
73 return -2; // invalid packet !!!!!! | |
74 } | |
1 | 75 |
547 | 76 mpeg_pts_error=0; |
77 | |
1 | 78 while(len>0){ // Skip stuFFing bytes |
79 c=stream_read_char(demux->stream);--len; | |
80 if(c!=0xFF)break; | |
81 } | |
82 if((c>>6)==1){ // Read (skip) STD scale & size value | |
83 // printf(" STD_scale=%d",(c>>5)&1); | |
84 d=((c&0x1F)<<8)|stream_read_char(demux->stream); | |
85 len-=2; | |
86 // printf(" STD_size=%d",d); | |
87 c=stream_read_char(demux->stream); | |
88 } | |
89 // Read System-1 stream timestamps: | |
90 if((c>>4)==2){ | |
91 pts=read_mpeg_timestamp(demux->stream,c); | |
92 len-=4; | |
93 } else | |
94 if((c>>4)==3){ | |
95 pts=read_mpeg_timestamp(demux->stream,c); | |
96 c=stream_read_char(demux->stream); | |
97 if((c>>4)!=1) pts=0; //printf("{ERROR4}"); | |
98 dts=read_mpeg_timestamp(demux->stream,c); | |
99 len-=4+1+4; | |
100 } else | |
101 if((c>>6)==2){ | |
102 int pts_flags; | |
103 int hdrlen; | |
104 // System-2 (.VOB) stream: | |
492 | 105 if((c>>4)&3) { |
106 #ifdef HAVE_LIBCSS | |
107 css=1; | |
108 #else | |
109 printf("Encrypted VOB file (not compiled with libcss support)! Read file DOCS/DVD\n"); | |
110 #endif | |
111 } | |
1 | 112 c=stream_read_char(demux->stream); pts_flags=c>>6; |
113 c=stream_read_char(demux->stream); hdrlen=c; | |
114 len-=2; | |
115 if(verbose>=3) printf(" hdrlen=%d (len=%d)",hdrlen,len); | |
242 | 116 if(hdrlen>len){ printf("demux_mpg: invalid header length \n"); return -1;} |
1 | 117 if(pts_flags==2){ |
118 c=stream_read_char(demux->stream); | |
119 pts=read_mpeg_timestamp(demux->stream,c); | |
120 len-=5;hdrlen-=5; | |
121 } else | |
122 if(pts_flags==3){ | |
123 c=stream_read_char(demux->stream); | |
124 pts=read_mpeg_timestamp(demux->stream,c); | |
125 c=stream_read_char(demux->stream); | |
126 dts=read_mpeg_timestamp(demux->stream,c); | |
127 len-=10;hdrlen-=10; | |
128 } | |
129 len-=hdrlen; | |
130 if(hdrlen>0) stream_skip(demux->stream,hdrlen); // skip header bytes | |
131 | |
132 //============== DVD Audio sub-stream ====================== | |
133 if(id==0x1BD){ | |
552 | 134 int aid=stream_read_char(demux->stream);--len; |
1 | 135 if(len<3) return -1; // invalid audio packet |
552 | 136 |
137 // AID: | |
138 // 0x20..0x3F subtitle | |
139 // 0x80..0x9F AC3 audio | |
140 // 0xA0..0xBF PCM audio | |
141 | |
142 if((aid & 0xE0) == 0x20){ | |
143 // subtitle: | |
144 aid&=0x1F; | |
426 | 145 |
587
8511095c5283
stage#1 completed: c files no more included from mplayer.c
arpi_esp
parents:
554
diff
changeset
|
146 if(!demux->s_streams[aid]){ |
552 | 147 printf("==> Found subtitle: %d\n",aid); |
587
8511095c5283
stage#1 completed: c files no more included from mplayer.c
arpi_esp
parents:
554
diff
changeset
|
148 demux->s_streams[aid]=1; |
552 | 149 } |
150 | |
151 if(demux->sub->id==aid){ | |
152 ds=demux->sub; | |
153 } | |
154 | |
155 } else if((aid & 0xC0) == 0x80) { | |
156 | |
157 // aid=128+(aid&0x7F); | |
158 // aid=0x80..0xBF | |
159 | |
1289 | 160 if(!demux->a_streams[aid]) new_sh_audio(demux,aid); |
552 | 161 if(demux->audio->id==-1) demux->audio->id=aid; |
426 | 162 |
163 if(demux->audio->id==aid){ | |
536 | 164 // int type; |
426 | 165 ds=demux->audio; |
587
8511095c5283
stage#1 completed: c files no more included from mplayer.c
arpi_esp
parents:
554
diff
changeset
|
166 if(!ds->sh) ds->sh=demux->a_streams[aid]; |
1 | 167 // READ Packet: Skip additional audio header data: |
536 | 168 c=stream_read_char(demux->stream);//type=c; |
169 c=stream_read_char(demux->stream);//type|=c<<8; | |
170 c=stream_read_char(demux->stream);//type|=c<<16; | |
171 // printf("[%06X]",type); | |
1 | 172 len-=3; |
1331
b4457de47804
ds->type removed - using id-based audio format detection in mplayer.c
arpi
parents:
1289
diff
changeset
|
173 if((aid&0xE0)==0xA0 && len>=2){ |
1 | 174 // read PCM header |
175 int head; | |
176 head=stream_read_char(demux->stream); head=c<<8; | |
177 c=stream_read_char(demux->stream); head|=c; len-=2; | |
178 while(len>0 && head!=0x180){ | |
179 head=c<<8; | |
180 c=stream_read_char(demux->stream); | |
181 head|=c;--len; | |
182 } | |
183 if(!len) printf("End of packet while searching for PCM header\n"); | |
184 } | |
552 | 185 } // if(demux->audio->id==aid) |
186 | |
187 } else printf("Unknown 0x1BD substream: 0x%02X \n",aid); | |
188 | |
189 } //if(id==0x1BD) | |
1 | 190 |
191 } else { | |
539 | 192 if(c!=0x0f){ |
541
b382156bff40
Embarrassing messages while playing DVD are moved to verbose level 1
lgb
parents:
539
diff
changeset
|
193 if (verbose>=1) printf(" {ERROR5,c=%d} \n",c); |
539 | 194 return -1; // invalid packet !!!!!! |
195 } | |
1 | 196 } |
547 | 197 if(mpeg_pts_error) printf(" {PTS_err:%d} \n",mpeg_pts_error); |
242 | 198 if(verbose>=3) printf(" => len=%d\n",len); |
1 | 199 |
501 | 200 // if(len<=0 || len>MAX_PS_PACKETSIZE) return -1; // Invalid packet size |
201 if(len<=0 || len>MAX_PS_PACKETSIZE){ | |
202 if(verbose>=2) printf("Invalid PS data len: %d\n",len); | |
203 return -1; // invalid packet !!!!!! | |
204 } | |
1 | 205 |
206 if(id>=0x1C0 && id<=0x1DF){ | |
207 // mpeg audio | |
208 int aid=id-0x1C0; | |
1289 | 209 if(!demux->a_streams[aid]) new_sh_audio(demux,aid); |
1 | 210 if(demux->audio->id==-1) demux->audio->id=aid; |
211 if(demux->audio->id==aid){ | |
212 ds=demux->audio; | |
587
8511095c5283
stage#1 completed: c files no more included from mplayer.c
arpi_esp
parents:
554
diff
changeset
|
213 if(!ds->sh) ds->sh=demux->a_streams[aid]; |
1 | 214 } |
215 } else | |
216 if(id>=0x1E0 && id<=0x1EF){ | |
217 // mpeg video | |
218 int aid=id-0x1E0; | |
1289 | 219 if(!demux->v_streams[aid]) new_sh_video(demux,aid); |
1 | 220 if(demux->video->id==-1) demux->video->id=aid; |
426 | 221 if(demux->video->id==aid){ |
222 ds=demux->video; | |
587
8511095c5283
stage#1 completed: c files no more included from mplayer.c
arpi_esp
parents:
554
diff
changeset
|
223 if(!ds->sh) ds->sh=demux->v_streams[aid]; |
426 | 224 } |
1 | 225 } |
226 | |
227 if(ds){ | |
228 if(verbose>=2) printf("DEMUX_MPG: Read %d data bytes from packet %04X\n",len,id); | |
229 // printf("packet start = 0x%X \n",stream_tell(demux->stream)-packet_start_pos); | |
492 | 230 #ifdef HAVE_LIBCSS |
546 | 231 if (css) { |
232 if (descrambling) CSSDescramble(demux->stream->buffer,key_title); else | |
233 printf("Encrypted stream but authentication was not requested by you!!\n"); | |
234 } | |
492 | 235 #endif |
979 | 236 ds_read_packet(ds,demux->stream,len,pts/90000.0f,0,0); |
554 | 237 // if(ds==demux->sub) parse_dvdsub(ds->last->buffer,ds->last->len); |
1 | 238 return 1; |
239 } | |
240 if(verbose>=2) printf("DEMUX_MPG: Skipping %d data bytes from packet %04X\n",len,id); | |
536 | 241 if(len<=2356) stream_skip(demux->stream,len); |
1 | 242 return 0; |
243 } | |
244 | |
587
8511095c5283
stage#1 completed: c files no more included from mplayer.c
arpi_esp
parents:
554
diff
changeset
|
245 int num_elementary_packets100=0; |
8511095c5283
stage#1 completed: c files no more included from mplayer.c
arpi_esp
parents:
554
diff
changeset
|
246 int num_elementary_packets101=0; |
1162 | 247 int num_elementary_packets1B6=0; |
1338
345585097137
mpeg PES steram support (only 1E0 & 1C0 packets, no 1BA/1BB headers)
arpi
parents:
1331
diff
changeset
|
248 int num_elementary_packetsPES=0; |
1 | 249 |
250 int demux_mpg_es_fill_buffer(demuxer_t *demux){ | |
251 // Elementary video stream | |
252 if(demux->stream->eof) return 0; | |
253 demux->filepos=stream_tell(demux->stream); | |
979 | 254 ds_read_packet(demux->video,demux->stream,STREAM_BUFFER_SIZE,0,0,0); |
1 | 255 return 1; |
256 } | |
257 | |
258 int demux_mpg_fill_buffer(demuxer_t *demux){ | |
259 unsigned int head=0; | |
260 int skipped=0; | |
261 int max_packs=128; | |
262 int ret=0; | |
263 | |
264 // System stream | |
265 do{ | |
266 demux->filepos=stream_tell(demux->stream); | |
267 head=stream_read_dword(demux->stream); | |
547 | 268 if((head&0xFFFFFF00)!=0x100){ |
269 // sync... | |
270 demux->filepos-=skipped; | |
271 while(1){ | |
536 | 272 int c=stream_read_char(demux->stream); |
273 if(c<0) break; //EOF | |
274 head<<=8; | |
275 if(head!=0x100){ | |
276 head|=c; | |
277 ++skipped; //++demux->filepos; | |
278 continue; | |
279 } | |
280 head|=c; | |
281 break; | |
547 | 282 } |
283 demux->filepos+=skipped; | |
1 | 284 } |
285 if(stream_eof(demux->stream)) break; | |
286 // sure: head=0x000001XX | |
524
9105fc95636c
A fast'n'ugly hack to correct DVD VOB playback problems
lgb
parents:
501
diff
changeset
|
287 if(verbose>=4) printf("*** head=0x%X\n",head); |
1 | 288 if(demux->synced==0){ |
289 if(head==0x1BA) demux->synced=1; | |
1338
345585097137
mpeg PES steram support (only 1E0 & 1C0 packets, no 1BA/1BB headers)
arpi
parents:
1331
diff
changeset
|
290 #if 0 |
345585097137
mpeg PES steram support (only 1E0 & 1C0 packets, no 1BA/1BB headers)
arpi
parents:
1331
diff
changeset
|
291 else if(head>=0x1C0 && head<=0x1EF){ |
345585097137
mpeg PES steram support (only 1E0 & 1C0 packets, no 1BA/1BB headers)
arpi
parents:
1331
diff
changeset
|
292 demux->synced=2; |
345585097137
mpeg PES steram support (only 1E0 & 1C0 packets, no 1BA/1BB headers)
arpi
parents:
1331
diff
changeset
|
293 if(verbose) printf("Mpeg PES stream synced at 0x%X (%d)!\n",demux->filepos,demux->filepos); |
345585097137
mpeg PES steram support (only 1E0 & 1C0 packets, no 1BA/1BB headers)
arpi
parents:
1331
diff
changeset
|
294 num_elementary_packets100=0; // requires for re-sync! |
345585097137
mpeg PES steram support (only 1E0 & 1C0 packets, no 1BA/1BB headers)
arpi
parents:
1331
diff
changeset
|
295 num_elementary_packets101=0; // requires for re-sync! |
345585097137
mpeg PES steram support (only 1E0 & 1C0 packets, no 1BA/1BB headers)
arpi
parents:
1331
diff
changeset
|
296 } |
345585097137
mpeg PES steram support (only 1E0 & 1C0 packets, no 1BA/1BB headers)
arpi
parents:
1331
diff
changeset
|
297 #endif |
1 | 298 } else |
299 if(demux->synced==1){ | |
929 | 300 if(head==0x1BB || head==0x1BD || (head>=0x1C0 && head<=0x1EF)){ |
1 | 301 demux->synced=2; |
302 if(verbose) printf("system stream synced at 0x%X (%d)!\n",demux->filepos,demux->filepos); | |
536 | 303 num_elementary_packets100=0; // requires for re-sync! |
304 num_elementary_packets101=0; // requires for re-sync! | |
1 | 305 } else demux->synced=0; |
306 } // else | |
307 if(demux->synced==2){ | |
308 ret=demux_mpg_read_packet(demux,head); | |
309 if(!ret) | |
310 if(--max_packs==0){ | |
311 demux->stream->eof=1; | |
312 printf("demux: file doesn't contain the selected audio or video stream\n"); | |
313 return 0; | |
314 } | |
315 } else { | |
316 if(head>=0x100 && head<0x1B0){ | |
1162 | 317 if(head==0x100) ++num_elementary_packets100; else |
318 if(head==0x101) ++num_elementary_packets101; | |
1 | 319 if(verbose>=3) printf("Opps... elementary video packet found: %03X\n",head); |
1162 | 320 } else |
1338
345585097137
mpeg PES steram support (only 1E0 & 1C0 packets, no 1BA/1BB headers)
arpi
parents:
1331
diff
changeset
|
321 if(head>=0x1C0 && head<0x1F0){ |
345585097137
mpeg PES steram support (only 1E0 & 1C0 packets, no 1BA/1BB headers)
arpi
parents:
1331
diff
changeset
|
322 ++num_elementary_packetsPES; |
345585097137
mpeg PES steram support (only 1E0 & 1C0 packets, no 1BA/1BB headers)
arpi
parents:
1331
diff
changeset
|
323 if(verbose>=3) printf("Opps... PES packet found: %03X\n",head); |
345585097137
mpeg PES steram support (only 1E0 & 1C0 packets, no 1BA/1BB headers)
arpi
parents:
1331
diff
changeset
|
324 } else |
1162 | 325 if(head==0x1B6) ++num_elementary_packets1B6; |
1 | 326 #if 1 |
1338
345585097137
mpeg PES steram support (only 1E0 & 1C0 packets, no 1BA/1BB headers)
arpi
parents:
1331
diff
changeset
|
327 if( ( (num_elementary_packets100>50 && num_elementary_packets101>50) || |
345585097137
mpeg PES steram support (only 1E0 & 1C0 packets, no 1BA/1BB headers)
arpi
parents:
1331
diff
changeset
|
328 (num_elementary_packetsPES>50) ) && skipped>4000000){ |
345585097137
mpeg PES steram support (only 1E0 & 1C0 packets, no 1BA/1BB headers)
arpi
parents:
1331
diff
changeset
|
329 if(verbose) printf("sync_mpeg_ps: seems to be ES/PES stream...\n"); |
1 | 330 demux->stream->eof=1; |
331 break; | |
332 } | |
333 #endif | |
334 } | |
335 } while(ret!=1); | |
336 if(verbose>=2) printf("demux: %d bad bytes skipped\n",skipped); | |
337 if(demux->stream->eof){ | |
338 if(verbose>=2) printf("MPEG Stream reached EOF\n"); | |
339 return 0; | |
340 } | |
341 return 1; | |
342 } | |
343 | |
1497
ad4d402b3d29
seek.c moved to demuxer.c, stream_reset in new_demuxer()
arpi
parents:
1466
diff
changeset
|
344 //extern off_t seek_to_byte; |
1466 | 345 |
346 void demux_seek_mpg(demuxer_t *demuxer,float rel_seek_secs,int flags){ | |
347 demux_stream_t *d_audio=demuxer->audio; | |
348 demux_stream_t *d_video=demuxer->video; | |
349 sh_audio_t *sh_audio=d_audio->sh; | |
350 sh_video_t *sh_video=d_video->sh; | |
351 | |
352 //================= seek in MPEG ========================== | |
353 off_t newpos; | |
354 if(!sh_video->i_bps) // unspecified? | |
355 newpos=demuxer->filepos+2324*75*rel_seek_secs; // 174.3 kbyte/sec | |
356 else | |
357 newpos=demuxer->filepos+(sh_video->i_bps)*rel_seek_secs; | |
358 | |
1497
ad4d402b3d29
seek.c moved to demuxer.c, stream_reset in new_demuxer()
arpi
parents:
1466
diff
changeset
|
359 if(newpos<demuxer->stream->start_pos) newpos=demuxer->stream->start_pos; // for VCD |
1466 | 360 #ifdef _LARGEFILE_SOURCE |
361 newpos&=~((long long)STREAM_BUFFER_SIZE-1); /* sector boundary */ | |
362 #else | |
363 newpos&=~(STREAM_BUFFER_SIZE-1); /* sector boundary */ | |
364 #endif | |
365 stream_seek(demuxer->stream,newpos); | |
366 | |
367 // re-sync video: | |
368 videobuf_code_len=0; // reset ES stream buffer | |
369 | |
370 ds_fill_buffer(d_video); | |
371 if(sh_audio){ | |
372 ds_fill_buffer(d_audio); | |
373 resync_audio_stream(sh_audio); | |
374 } | |
375 | |
376 while(1){ | |
377 int i; | |
378 if(sh_audio && !d_audio->eof && d_video->pts && d_audio->pts){ | |
379 float a_pts=d_audio->pts; | |
380 a_pts+=(ds_tell_pts(d_audio)-sh_audio->a_in_buffer_len)/(float)sh_audio->i_bps; | |
381 if(d_video->pts>a_pts){ | |
382 skip_audio_frame(sh_audio); // sync audio | |
383 continue; | |
384 } | |
385 } | |
386 i=sync_video_packet(d_video); | |
387 if(i==0x1B3 || i==0x1B8) break; // found it! | |
388 if(!i || !skip_video_packet(d_video)) break; // EOF? | |
389 } | |
390 | |
391 | |
392 } | |
393 |