Mercurial > mplayer.hg
annotate demux_mpg.c @ 540:4d0ad0f6b7dc
some updates, not finished yet
author | arpi_esp |
---|---|
date | Fri, 20 Apr 2001 02:45:09 +0000 |
parents | 48f0c6f0519f |
children | b382156bff40 |
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 | |
536 | 33 // if(id==0x1F0){ |
34 // demux->synced=0; // force resync after 0x1F0 | |
35 // return -1; | |
36 //} | |
501 | 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); | |
536 | 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){ | |
536 | 115 // int type; |
426 | 116 ds=demux->audio; |
117 if(!ds->sh) ds->sh=avi_header.a_streams[aid]; | |
1 | 118 // READ Packet: Skip additional audio header data: |
536 | 119 c=stream_read_char(demux->stream);//type=c; |
120 c=stream_read_char(demux->stream);//type|=c<<8; | |
121 c=stream_read_char(demux->stream);//type|=c<<16; | |
122 // printf("[%06X]",type); | |
1 | 123 len-=3; |
124 if(ds->type==-1){ | |
125 // autodetect type | |
126 ds->type=((aid&0x70)==0x20)?2:3; | |
127 } | |
128 if(ds->type==2 && len>=2){ | |
129 // read PCM header | |
130 int head; | |
131 head=stream_read_char(demux->stream); head=c<<8; | |
132 c=stream_read_char(demux->stream); head|=c; len-=2; | |
133 while(len>0 && head!=0x180){ | |
134 head=c<<8; | |
135 c=stream_read_char(demux->stream); | |
136 head|=c;--len; | |
137 } | |
138 if(!len) printf("End of packet while searching for PCM header\n"); | |
139 } | |
140 } | |
141 } | |
142 | |
143 } else { | |
539 | 144 if(c!=0x0f){ |
145 printf(" {ERROR5,c=%d} \n",c); | |
146 return -1; // invalid packet !!!!!! | |
147 } | |
1 | 148 } |
242 | 149 if(verbose>=3) printf(" => len=%d\n",len); |
1 | 150 |
501 | 151 // if(len<=0 || len>MAX_PS_PACKETSIZE) return -1; // Invalid packet size |
152 if(len<=0 || len>MAX_PS_PACKETSIZE){ | |
153 if(verbose>=2) printf("Invalid PS data len: %d\n",len); | |
154 return -1; // invalid packet !!!!!! | |
155 } | |
1 | 156 |
157 if(id>=0x1C0 && id<=0x1DF){ | |
158 // mpeg audio | |
159 int aid=id-0x1C0; | |
426 | 160 if(!avi_header.a_streams[aid]) new_sh_audio(aid); |
1 | 161 if(demux->audio->id==-1) demux->audio->id=aid; |
162 if(demux->audio->id==aid){ | |
163 ds=demux->audio; | |
426 | 164 if(!ds->sh) ds->sh=avi_header.a_streams[aid]; |
1 | 165 if(ds->type==-1) ds->type=1; |
166 } | |
167 } else | |
168 if(id>=0x1E0 && id<=0x1EF){ | |
169 // mpeg video | |
170 int aid=id-0x1E0; | |
426 | 171 if(!avi_header.v_streams[aid]) new_sh_video(aid); |
1 | 172 if(demux->video->id==-1) demux->video->id=aid; |
426 | 173 if(demux->video->id==aid){ |
174 ds=demux->video; | |
175 if(!ds->sh) ds->sh=avi_header.v_streams[aid]; | |
176 } | |
1 | 177 } |
178 | |
179 if(ds){ | |
180 if(verbose>=2) printf("DEMUX_MPG: Read %d data bytes from packet %04X\n",len,id); | |
181 // printf("packet start = 0x%X \n",stream_tell(demux->stream)-packet_start_pos); | |
492 | 182 #ifdef HAVE_LIBCSS |
183 if (css) CSSDescramble(demux->stream->buffer,key_title); | |
184 #endif | |
1 | 185 ds_read_packet(ds,demux->stream,len,pts/90000.0f,0); |
186 return 1; | |
187 } | |
188 if(verbose>=2) printf("DEMUX_MPG: Skipping %d data bytes from packet %04X\n",len,id); | |
536 | 189 if(len<=2356) stream_skip(demux->stream,len); |
1 | 190 return 0; |
191 } | |
192 | |
193 static int num_elementary_packets100=0; | |
194 static int num_elementary_packets101=0; | |
195 | |
196 int demux_mpg_es_fill_buffer(demuxer_t *demux){ | |
197 //if(demux->type==DEMUXER_TYPE_MPEG_ES) | |
198 // Elementary video stream | |
199 if(demux->stream->eof) return 0; | |
200 demux->filepos=stream_tell(demux->stream); | |
501 | 201 ds_read_packet(demux->video,demux->stream,STREAM_BUFFER_SIZE,0,0); |
1 | 202 return 1; |
203 } | |
204 | |
205 int demux_mpg_fill_buffer(demuxer_t *demux){ | |
206 unsigned int head=0; | |
207 int skipped=0; | |
208 int max_packs=128; | |
209 int ret=0; | |
210 | |
211 // System stream | |
212 do{ | |
213 demux->filepos=stream_tell(demux->stream); | |
214 head=stream_read_dword(demux->stream); | |
536 | 215 demux->filepos-=skipped; |
216 while(1){ | |
217 int c=stream_read_char(demux->stream); | |
218 if(c<0) break; //EOF | |
219 head<<=8; | |
220 if(head!=0x100){ | |
221 head|=c; | |
222 ++skipped; //++demux->filepos; | |
223 continue; | |
224 } | |
225 head|=c; | |
226 break; | |
1 | 227 } |
536 | 228 demux->filepos+=skipped; |
1 | 229 if(stream_eof(demux->stream)) break; |
230 // sure: head=0x000001XX | |
524
9105fc95636c
A fast'n'ugly hack to correct DVD VOB playback problems
lgb
parents:
501
diff
changeset
|
231 if(verbose>=4) printf("*** head=0x%X\n",head); |
1 | 232 if(demux->synced==0){ |
233 if(head==0x1BA) demux->synced=1; | |
234 } else | |
235 if(demux->synced==1){ | |
236 if(head==0x1BB || (head>=0x1C0 && head<=0x1EF)){ | |
237 demux->synced=2; | |
238 if(verbose) printf("system stream synced at 0x%X (%d)!\n",demux->filepos,demux->filepos); | |
536 | 239 num_elementary_packets100=0; // requires for re-sync! |
240 num_elementary_packets101=0; // requires for re-sync! | |
1 | 241 } else demux->synced=0; |
242 } // else | |
243 if(demux->synced==2){ | |
244 ret=demux_mpg_read_packet(demux,head); | |
245 if(!ret) | |
246 if(--max_packs==0){ | |
247 demux->stream->eof=1; | |
248 printf("demux: file doesn't contain the selected audio or video stream\n"); | |
249 return 0; | |
250 } | |
251 } else { | |
252 if(head>=0x100 && head<0x1B0){ | |
253 if(head==0x100) | |
254 ++num_elementary_packets100; | |
255 else | |
256 if(head==0x101) ++num_elementary_packets101; | |
257 if(verbose>=3) printf("Opps... elementary video packet found: %03X\n",head); | |
258 } | |
259 #if 1 | |
260 if(num_elementary_packets100>50 && num_elementary_packets101>50 | |
261 && skipped>4000000){ | |
262 if(verbose) printf("sync_mpeg_ps: seems to be ES stream...\n"); | |
263 demux->stream->eof=1; | |
264 break; | |
265 } | |
266 #endif | |
267 } | |
268 } while(ret!=1); | |
269 if(verbose>=2) printf("demux: %d bad bytes skipped\n",skipped); | |
270 if(demux->stream->eof){ | |
271 if(verbose>=2) printf("MPEG Stream reached EOF\n"); | |
272 return 0; | |
273 } | |
274 return 1; | |
275 } | |
276 |