Mercurial > mplayer.hg
annotate demux_asf.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 // ASF file parser for DEMUXER v0.3 by A'rpi/ESP-team |
2 | |
587
8511095c5283
stage#1 completed: c files no more included from mplayer.c
arpi_esp
parents:
426
diff
changeset
|
3 #include <stdio.h> |
8511095c5283
stage#1 completed: c files no more included from mplayer.c
arpi_esp
parents:
426
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:
426
diff
changeset
|
6 |
8511095c5283
stage#1 completed: c files no more included from mplayer.c
arpi_esp
parents:
426
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:
426
diff
changeset
|
8 |
8511095c5283
stage#1 completed: c files no more included from mplayer.c
arpi_esp
parents:
426
diff
changeset
|
9 #include "stream.h" |
1342 | 10 #include "asf.h" |
587
8511095c5283
stage#1 completed: c files no more included from mplayer.c
arpi_esp
parents:
426
diff
changeset
|
11 #include "demuxer.h" |
8511095c5283
stage#1 completed: c files no more included from mplayer.c
arpi_esp
parents:
426
diff
changeset
|
12 |
833 | 13 |
1342 | 14 /* |
15 * Load 16/32-bit values in little endian byte order | |
16 * from an unaligned address | |
17 */ | |
18 #ifdef ARCH_X86 | |
19 #define LOAD_LE32(p) (*(unsigned int*)(p)) | |
20 #define LOAD_LE16(p) (*(unsigned short*)(p)) | |
21 #else | |
22 #define LOAD_LE32(p) (((unsigned char*)(p))[0] | \ | |
23 ((unsigned char*)(p))[1]<< 8 | \ | |
24 ((unsigned char*)(p))[2]<<16 | \ | |
25 ((unsigned char*)(p))[3]<<24 ) | |
26 #define LOAD_LE16(p) (((unsigned char*)(p))[0] | \ | |
27 ((unsigned char*)(p))[1]<<8) | |
28 #endif | |
29 | |
587
8511095c5283
stage#1 completed: c files no more included from mplayer.c
arpi_esp
parents:
426
diff
changeset
|
30 // defined at asfheader.c: |
8511095c5283
stage#1 completed: c files no more included from mplayer.c
arpi_esp
parents:
426
diff
changeset
|
31 extern unsigned char* asf_packet; |
8511095c5283
stage#1 completed: c files no more included from mplayer.c
arpi_esp
parents:
426
diff
changeset
|
32 extern int asf_scrambling_h; |
8511095c5283
stage#1 completed: c files no more included from mplayer.c
arpi_esp
parents:
426
diff
changeset
|
33 extern int asf_scrambling_w; |
8511095c5283
stage#1 completed: c files no more included from mplayer.c
arpi_esp
parents:
426
diff
changeset
|
34 extern int asf_scrambling_b; |
8511095c5283
stage#1 completed: c files no more included from mplayer.c
arpi_esp
parents:
426
diff
changeset
|
35 extern int asf_packetsize; |
8511095c5283
stage#1 completed: c files no more included from mplayer.c
arpi_esp
parents:
426
diff
changeset
|
36 |
8511095c5283
stage#1 completed: c files no more included from mplayer.c
arpi_esp
parents:
426
diff
changeset
|
37 |
1 | 38 // based on asf file-format doc by Eugene [http://divx.euro.ru] |
39 | |
40 //static float avi_pts_frametime=1.0f/25.0f; | |
41 //static float avi_audio_pts=0; | |
42 //static float avi_video_pts=0; | |
43 | |
44 //static int skip_video_frames=0; | |
45 | |
46 | |
587
8511095c5283
stage#1 completed: c files no more included from mplayer.c
arpi_esp
parents:
426
diff
changeset
|
47 static void asf_descrambling(unsigned char *src,int len){ |
8511095c5283
stage#1 completed: c files no more included from mplayer.c
arpi_esp
parents:
426
diff
changeset
|
48 unsigned char *dst=malloc(len); |
8511095c5283
stage#1 completed: c files no more included from mplayer.c
arpi_esp
parents:
426
diff
changeset
|
49 unsigned char *s2=src; |
8511095c5283
stage#1 completed: c files no more included from mplayer.c
arpi_esp
parents:
426
diff
changeset
|
50 int i=0,x,y; |
8511095c5283
stage#1 completed: c files no more included from mplayer.c
arpi_esp
parents:
426
diff
changeset
|
51 while(len-i>=asf_scrambling_h*asf_scrambling_w*asf_scrambling_b){ |
8511095c5283
stage#1 completed: c files no more included from mplayer.c
arpi_esp
parents:
426
diff
changeset
|
52 // printf("descrambling! (w=%d b=%d)\n",w,asf_scrambling_b); |
8511095c5283
stage#1 completed: c files no more included from mplayer.c
arpi_esp
parents:
426
diff
changeset
|
53 //i+=asf_scrambling_h*asf_scrambling_w; |
8511095c5283
stage#1 completed: c files no more included from mplayer.c
arpi_esp
parents:
426
diff
changeset
|
54 for(x=0;x<asf_scrambling_w;x++) |
8511095c5283
stage#1 completed: c files no more included from mplayer.c
arpi_esp
parents:
426
diff
changeset
|
55 for(y=0;y<asf_scrambling_h;y++){ |
8511095c5283
stage#1 completed: c files no more included from mplayer.c
arpi_esp
parents:
426
diff
changeset
|
56 memcpy(dst+i,s2+(y*asf_scrambling_w+x)*asf_scrambling_b,asf_scrambling_b); |
8511095c5283
stage#1 completed: c files no more included from mplayer.c
arpi_esp
parents:
426
diff
changeset
|
57 i+=asf_scrambling_b; |
8511095c5283
stage#1 completed: c files no more included from mplayer.c
arpi_esp
parents:
426
diff
changeset
|
58 } |
8511095c5283
stage#1 completed: c files no more included from mplayer.c
arpi_esp
parents:
426
diff
changeset
|
59 s2+=asf_scrambling_h*asf_scrambling_w*asf_scrambling_b; |
8511095c5283
stage#1 completed: c files no more included from mplayer.c
arpi_esp
parents:
426
diff
changeset
|
60 } |
8511095c5283
stage#1 completed: c files no more included from mplayer.c
arpi_esp
parents:
426
diff
changeset
|
61 //if(i<len) memcpy(dst+i,src+i,len-i); |
8511095c5283
stage#1 completed: c files no more included from mplayer.c
arpi_esp
parents:
426
diff
changeset
|
62 memcpy(src,dst,i); |
8511095c5283
stage#1 completed: c files no more included from mplayer.c
arpi_esp
parents:
426
diff
changeset
|
63 free(dst); |
8511095c5283
stage#1 completed: c files no more included from mplayer.c
arpi_esp
parents:
426
diff
changeset
|
64 } |
8511095c5283
stage#1 completed: c files no more included from mplayer.c
arpi_esp
parents:
426
diff
changeset
|
65 |
1 | 66 |
979 | 67 static int demux_asf_read_packet(demuxer_t *demux,unsigned char *data,int len,int id,int seq,unsigned long time,unsigned short dur,int offs,int keyframe){ |
1 | 68 demux_stream_t *ds=NULL; |
69 | |
70 if(verbose>=4) printf("demux_asf.read_packet: id=%d seq=%d len=%d\n",id,seq,len); | |
71 | |
426 | 72 if(demux->video->id==-1) |
587
8511095c5283
stage#1 completed: c files no more included from mplayer.c
arpi_esp
parents:
426
diff
changeset
|
73 if(demux->v_streams[id]) |
426 | 74 demux->video->id=id; |
75 | |
1 | 76 if(demux->audio->id==-1) |
587
8511095c5283
stage#1 completed: c files no more included from mplayer.c
arpi_esp
parents:
426
diff
changeset
|
77 if(demux->a_streams[id]) |
426 | 78 demux->audio->id=id; |
1 | 79 |
80 if(id==demux->audio->id){ | |
81 // audio | |
82 ds=demux->audio; | |
426 | 83 if(!ds->sh){ |
587
8511095c5283
stage#1 completed: c files no more included from mplayer.c
arpi_esp
parents:
426
diff
changeset
|
84 ds->sh=demux->a_streams[id]; |
426 | 85 if(verbose) printf("Auto-selected ASF audio ID = %d\n",ds->id); |
86 } | |
1 | 87 } else |
88 if(id==demux->video->id){ | |
89 // video | |
90 ds=demux->video; | |
426 | 91 if(!ds->sh){ |
587
8511095c5283
stage#1 completed: c files no more included from mplayer.c
arpi_esp
parents:
426
diff
changeset
|
92 ds->sh=demux->v_streams[id]; |
426 | 93 if(verbose) printf("Auto-selected ASF video ID = %d\n",ds->id); |
94 } | |
1 | 95 } |
96 | |
97 if(ds){ | |
98 if(ds->asf_packet){ | |
99 if(ds->asf_seq!=seq){ | |
100 // closed segment, finalize packet: | |
101 if(ds==demux->audio) | |
102 if(asf_scrambling_h>1 && asf_scrambling_w>1) | |
103 asf_descrambling(ds->asf_packet->buffer,ds->asf_packet->len); | |
104 ds_add_packet(ds,ds->asf_packet); | |
105 ds->asf_packet=NULL; | |
106 } else { | |
107 // append data to it! | |
108 demux_packet_t* dp=ds->asf_packet; | |
109 if(dp->len!=offs && offs!=-1) printf("warning! fragment.len=%d BUT next fragment offset=%d \n",dp->len,offs); | |
110 dp->buffer=realloc(dp->buffer,dp->len+len); | |
111 memcpy(dp->buffer+dp->len,data,len); | |
112 if(verbose>=4) printf("data appended! %d+%d\n",dp->len,len); | |
113 dp->len+=len; | |
114 // we are ready now. | |
115 return 1; | |
116 } | |
117 } | |
118 // create new packet: | |
119 { demux_packet_t* dp; | |
120 if(offs>0){ | |
599 | 121 if(verbose) printf("warning! broken fragment, %d bytes missing \n",offs); |
1 | 122 return 0; |
123 } | |
124 dp=new_demux_packet(len); | |
125 memcpy(dp->buffer,data,len); | |
126 dp->pts=time*0.001f; | |
979 | 127 dp->flags=keyframe; |
1 | 128 // if(ds==demux->video) printf("ASF time: %8d dur: %5d \n",time,dur); |
129 dp->pos=demux->filepos; | |
130 ds->asf_packet=dp; | |
131 ds->asf_seq=seq; | |
132 // we are ready now. | |
133 return 1; | |
134 } | |
135 } | |
136 | |
137 return 0; | |
138 } | |
139 | |
140 //static int num_elementary_packets100=0; | |
141 //static int num_elementary_packets101=0; | |
142 | |
143 // return value: | |
144 // 0 = EOF or no stream found | |
145 // 1 = successfully read a packet | |
146 int demux_asf_fill_buffer(demuxer_t *demux){ | |
147 | |
148 demux->filepos=stream_tell(demux->stream); | |
587
8511095c5283
stage#1 completed: c files no more included from mplayer.c
arpi_esp
parents:
426
diff
changeset
|
149 if(demux->filepos>=demux->movi_end){ |
1 | 150 demux->stream->eof=1; |
151 return 0; | |
152 } | |
153 | |
587
8511095c5283
stage#1 completed: c files no more included from mplayer.c
arpi_esp
parents:
426
diff
changeset
|
154 stream_read(demux->stream,asf_packet,asf_packetsize); |
1 | 155 if(demux->stream->eof) return 0; // EOF |
156 | |
157 if(asf_packet[0]==0x82){ | |
158 unsigned char flags=asf_packet[3]; | |
159 unsigned char segtype=asf_packet[4]; | |
160 unsigned char* p=&asf_packet[5]; | |
587
8511095c5283
stage#1 completed: c files no more included from mplayer.c
arpi_esp
parents:
426
diff
changeset
|
161 unsigned char* p_end=p+asf_packetsize; |
1 | 162 unsigned long time; |
163 unsigned short duration; | |
164 int segs=1; | |
165 unsigned char segsizetype=0x80; | |
166 int seg; | |
167 int padding=0; | |
168 int plen; | |
169 | |
170 if(verbose>1){ | |
171 int i; | |
172 for(i=0;i<16;i++) printf(" %02X",asf_packet[i]); | |
173 printf("\n"); | |
174 } | |
175 | |
176 //if(segtype!=0x5d) printf("Warning! packet[4] != 0x5d \n"); | |
177 | |
178 // Calculate packet size (plen): | |
179 if(flags&0x40){ | |
180 // Explicit (absoulte) packet size | |
1342 | 181 plen=LOAD_LE16(p); p+=2; |
1 | 182 if(verbose>1)printf("Explicit packet size specified: %d \n",plen); |
587
8511095c5283
stage#1 completed: c files no more included from mplayer.c
arpi_esp
parents:
426
diff
changeset
|
183 if(plen>asf_packetsize) printf("Warning! plen>packetsize! (%d>%d) \n",plen,asf_packetsize); |
1 | 184 if(flags&(8|16)){ |
185 padding=p[0];p++; | |
186 if(flags&16){ padding|=p[0]<<8; p++;} | |
587
8511095c5283
stage#1 completed: c files no more included from mplayer.c
arpi_esp
parents:
426
diff
changeset
|
187 if(verbose)printf("Warning! explicit=%d padding=%d \n",plen,asf_packetsize-padding); |
1 | 188 } |
189 } else { | |
190 // Padding (relative) size | |
191 if(flags&8){ | |
192 padding=p[0];++p; | |
193 } else | |
194 if(flags&16){ | |
1342 | 195 padding=LOAD_LE16(p);p+=2; |
1 | 196 } |
587
8511095c5283
stage#1 completed: c files no more included from mplayer.c
arpi_esp
parents:
426
diff
changeset
|
197 plen=asf_packetsize-padding; |
1 | 198 } |
199 | |
1342 | 200 time = LOAD_LE32(p); p+=4; |
201 duration = LOAD_LE16(p); p+=2; | |
1 | 202 if(flags&1){ |
203 segsizetype=p[0] & 0xC0; | |
204 segs=p[0] & 0x3F; | |
205 ++p; | |
206 } | |
600 | 207 if(verbose>=4) printf("%08X: flag=%02X segs=%d pad=%d time=%ld dur=%d\n", |
1 | 208 demux->filepos,flags,segs,padding,time,duration); |
209 for(seg=0;seg<segs;seg++){ | |
210 //ASF_segmhdr_t* sh; | |
211 unsigned char streamno; | |
212 unsigned char seq; | |
213 int len; | |
214 unsigned long x; | |
215 unsigned char type; | |
216 unsigned long time2; | |
979 | 217 int keyframe=0; |
1 | 218 |
219 if(p>=p_end) printf("Warning! invalid packet 1, sig11 coming soon...\n"); | |
220 | |
221 if(verbose>1){ | |
222 int i; | |
223 printf("seg %d:",seg); | |
224 for(i=0;i<16;i++) printf(" %02X",p[i]); | |
225 printf("\n"); | |
226 } | |
227 | |
228 streamno=p[0]&0x7F; | |
979 | 229 if(p[0]&0x80) keyframe=1; |
1 | 230 seq=p[1]; |
231 p+=2; | |
232 | |
233 switch(segtype){ | |
234 case 0x55: | |
235 x=*((unsigned char*)p); | |
236 p++; | |
237 break; | |
238 case 0x59: | |
1342 | 239 x=LOAD_LE16(p); |
1 | 240 p+=2; |
241 break; | |
242 case 0x5D: | |
1342 | 243 x=LOAD_LE32(p); |
1 | 244 p+=4; |
245 break; | |
246 default: | |
247 printf("Warning! unknown segtype == 0x%2X \n",segtype); | |
1327
b12e1817bcc2
some cleanup - fixed warnings, removed old stuff, moved audio resync to dec_audio
arpi
parents:
979
diff
changeset
|
248 x=0; |
1 | 249 } |
250 | |
251 type=p[0]; p++; // 0x01: grouping 0x08: single | |
252 | |
253 switch(type){ | |
254 case 0x01: | |
255 //printf("grouping: %02X \n",p[0]); | |
256 ++p; // skip unknown byte | |
257 break; | |
258 case 0x08: | |
259 //printf("!!! obj_length = %d\n",*((unsigned long*)p)); | |
260 p+=4; | |
1342 | 261 time2=LOAD_LE32(p);p+=4; |
1 | 262 break; |
263 default: | |
264 printf("unknown segment type: 0x%02X \n",type); | |
265 } | |
266 | |
267 if(flags&1){ | |
268 // multiple segments | |
269 if(segsizetype==0x40){ | |
1342 | 270 len=*((unsigned char*)p);p++; // 1 byte |
1 | 271 } else { |
1342 | 272 len=LOAD_LE16(p);p+=2; // 2 byte |
1 | 273 } |
274 } else { | |
275 // single segment | |
276 len=plen-(p-asf_packet); | |
277 } | |
278 if(len<0 || (p+len)>=p_end){ | |
279 printf("ASF_parser: warning! segment len=%d\n",len); | |
280 } | |
281 if(verbose>=4) printf(" seg #%d: streamno=%d seq=%d type=%02X len=%d\n",seg,streamno,seq,type,len); | |
282 | |
283 switch(type){ | |
284 case 0x01: | |
285 // GROUPING: | |
286 //printf("ASF_parser: warning! grouping (flag=1) not yet supported!\n",len); | |
287 //printf(" total: %d \n",len); | |
288 while(len>0){ | |
289 int len2=p[0]; | |
290 p++; | |
291 //printf(" group part: %d bytes\n",len2); | |
979 | 292 demux_asf_read_packet(demux,p,len2,streamno,seq,x,duration,-1,keyframe); |
1 | 293 p+=len2; |
294 len-=len2+1; | |
295 } | |
296 if(len!=0){ | |
297 printf("ASF_parser: warning! groups total != len\n"); | |
298 } | |
299 break; | |
300 case 0x08: | |
301 // NO GROUPING: | |
302 //printf("fragment offset: %d \n",sh->x); | |
979 | 303 demux_asf_read_packet(demux,p,len,streamno,seq,time2,duration,x,keyframe); |
1 | 304 p+=len; |
305 break; | |
306 } | |
307 | |
308 } // for segs | |
309 return 1; // success | |
310 } | |
311 | |
312 printf("%08X: UNKNOWN TYPE %02X %02X %02X %02X %02X...\n",demux->filepos,asf_packet[0],asf_packet[1],asf_packet[2],asf_packet[3],asf_packet[4]); | |
313 return 0; | |
314 } |