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