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