comparison demux_mpg.c @ 1:3b5f5d1c5041

Initial revision
author arpi_esp
date Sat, 24 Feb 2001 20:28:24 +0000
parents
children 18e5c0f63947
comparison
equal deleted inserted replaced
0:c1bb2c071d63 1:3b5f5d1c5041
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);
34 if(len==0 || len>4096) return -2; // invalid packet !!!!!!
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);
68 if(hdrlen>len) return -1; // invalid header length
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){
86 int aid=stream_read_char(demux->stream)&0x7F;--len;
87 ds=demux->audio;
88 if(ds->id==-1) ds->id=aid;
89 if(!dvdaudio_table[aid]){
90 dvdaudio_table[aid]=1;
91 printf("DVD Audio format: %s ID=%d%s\n",
92 ((aid&0x70)==0x20)?"PCM":"AC3",aid,(ds->id==aid)?" CURRENT":"");
93 }
94 if(len<3) return -1; // invalid audio packet
95 if(ds->id!=aid){
96 // drop packet (not selected channel)
97 ds=NULL;
98 } else {
99 // READ Packet: Skip additional audio header data:
100 c=stream_read_char(demux->stream);
101 c=stream_read_char(demux->stream);
102 c=stream_read_char(demux->stream);
103 len-=3;
104 if(ds->type==-1){
105 // autodetect type
106 ds->type=((aid&0x70)==0x20)?2:3;
107 }
108 if(ds->type==2 && len>=2){
109 // read PCM header
110 int head;
111 head=stream_read_char(demux->stream); head=c<<8;
112 c=stream_read_char(demux->stream); head|=c; len-=2;
113 while(len>0 && head!=0x180){
114 head=c<<8;
115 c=stream_read_char(demux->stream);
116 head|=c;--len;
117 }
118 if(!len) printf("End of packet while searching for PCM header\n");
119 }
120 }
121 }
122
123 } else {
124 //if(c!=0x0f) printf(" {ERROR5,c=%d} \n",c);
125 }
126 if(verbose>=3) printf("\n");
127
128 if(len<=0 || len>4096) return -1; // Invalid packet size
129
130 if(id>=0x1C0 && id<=0x1DF){
131 // mpeg audio
132 int aid=id-0x1C0;
133 if(demux->audio->id==-1) demux->audio->id=aid;
134 if(demux->audio->id==aid){
135 ds=demux->audio;
136 if(ds->type==-1) ds->type=1;
137 }
138 } else
139 if(id>=0x1E0 && id<=0x1EF){
140 // mpeg video
141 int aid=id-0x1E0;
142 if(demux->video->id==-1) demux->video->id=aid;
143 if(demux->video->id==aid) ds=demux->video;
144 }
145
146 if(ds){
147 if(verbose>=2) printf("DEMUX_MPG: Read %d data bytes from packet %04X\n",len,id);
148 // printf("packet start = 0x%X \n",stream_tell(demux->stream)-packet_start_pos);
149 ds_read_packet(ds,demux->stream,len,pts/90000.0f,0);
150 return 1;
151 }
152 if(verbose>=2) printf("DEMUX_MPG: Skipping %d data bytes from packet %04X\n",len,id);
153 stream_skip(demux->stream,len);
154 return 0;
155 }
156
157 static int num_elementary_packets100=0;
158 static int num_elementary_packets101=0;
159
160 int demux_mpg_es_fill_buffer(demuxer_t *demux){
161 //if(demux->type==DEMUXER_TYPE_MPEG_ES)
162 // Elementary video stream
163 if(demux->stream->eof) return 0;
164 demux->filepos=stream_tell(demux->stream);
165 ds_read_packet(demux->video,demux->stream,4096,0,0);
166 return 1;
167 }
168
169 int demux_mpg_fill_buffer(demuxer_t *demux){
170 unsigned int head=0;
171 int skipped=0;
172 int max_packs=128;
173 int ret=0;
174
175 // System stream
176 do{
177 demux->filepos=stream_tell(demux->stream);
178 head=stream_read_dword(demux->stream);
179 while((head&0xffffff00)!=0x00000100){
180 if(stream_eof(demux->stream)) break;
181 head=(head<<8)|stream_read_char(demux->stream);
182 ++skipped; ++demux->filepos;
183 }
184 if(stream_eof(demux->stream)) break;
185 // sure: head=0x000001XX
186 if(demux->synced==0){
187 if(head==0x1BA) demux->synced=1;
188 } else
189 if(demux->synced==1){
190 if(head==0x1BB || (head>=0x1C0 && head<=0x1EF)){
191 demux->synced=2;
192 if(verbose) printf("system stream synced at 0x%X (%d)!\n",demux->filepos,demux->filepos);
193 } else demux->synced=0;
194 } // else
195 if(demux->synced==2){
196 ret=demux_mpg_read_packet(demux,head);
197 if(!ret)
198 if(--max_packs==0){
199 demux->stream->eof=1;
200 printf("demux: file doesn't contain the selected audio or video stream\n");
201 return 0;
202 }
203 } else {
204 if(head>=0x100 && head<0x1B0){
205 if(head==0x100)
206 ++num_elementary_packets100;
207 else
208 if(head==0x101) ++num_elementary_packets101;
209 if(verbose>=3) printf("Opps... elementary video packet found: %03X\n",head);
210 }
211 #if 1
212 if(num_elementary_packets100>50 && num_elementary_packets101>50
213 && skipped>4000000){
214 if(verbose) printf("sync_mpeg_ps: seems to be ES stream...\n");
215 demux->stream->eof=1;
216 break;
217 }
218 #endif
219 }
220 } while(ret!=1);
221 if(verbose>=2) printf("demux: %d bad bytes skipped\n",skipped);
222 if(demux->stream->eof){
223 if(verbose>=2) printf("MPEG Stream reached EOF\n");
224 return 0;
225 }
226 return 1;
227 }
228