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