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