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