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