Mercurial > mplayer.hg
annotate demux_asf.c @ 1534:12b9354cd320
AAlib section updated
author | folke |
---|---|
date | Wed, 15 Aug 2001 19:21:25 +0000 |
parents | b895f95e7657 |
children | 5c7760aa4f94 |
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 | |
587
8511095c5283
stage#1 completed: c files no more included from mplayer.c
arpi_esp
parents:
426
diff
changeset
|
40 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
|
41 unsigned char *dst=malloc(len); |
8511095c5283
stage#1 completed: c files no more included from mplayer.c
arpi_esp
parents:
426
diff
changeset
|
42 unsigned char *s2=src; |
8511095c5283
stage#1 completed: c files no more included from mplayer.c
arpi_esp
parents:
426
diff
changeset
|
43 int i=0,x,y; |
8511095c5283
stage#1 completed: c files no more included from mplayer.c
arpi_esp
parents:
426
diff
changeset
|
44 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
|
45 // 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
|
46 //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
|
47 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
|
48 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
|
49 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
|
50 i+=asf_scrambling_b; |
8511095c5283
stage#1 completed: c files no more included from mplayer.c
arpi_esp
parents:
426
diff
changeset
|
51 } |
8511095c5283
stage#1 completed: c files no more included from mplayer.c
arpi_esp
parents:
426
diff
changeset
|
52 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
|
53 } |
8511095c5283
stage#1 completed: c files no more included from mplayer.c
arpi_esp
parents:
426
diff
changeset
|
54 //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
|
55 memcpy(src,dst,i); |
8511095c5283
stage#1 completed: c files no more included from mplayer.c
arpi_esp
parents:
426
diff
changeset
|
56 free(dst); |
8511095c5283
stage#1 completed: c files no more included from mplayer.c
arpi_esp
parents:
426
diff
changeset
|
57 } |
8511095c5283
stage#1 completed: c files no more included from mplayer.c
arpi_esp
parents:
426
diff
changeset
|
58 |
1 | 59 |
979 | 60 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 | 61 demux_stream_t *ds=NULL; |
62 | |
63 if(verbose>=4) printf("demux_asf.read_packet: id=%d seq=%d len=%d\n",id,seq,len); | |
64 | |
426 | 65 if(demux->video->id==-1) |
587
8511095c5283
stage#1 completed: c files no more included from mplayer.c
arpi_esp
parents:
426
diff
changeset
|
66 if(demux->v_streams[id]) |
426 | 67 demux->video->id=id; |
68 | |
1 | 69 if(demux->audio->id==-1) |
587
8511095c5283
stage#1 completed: c files no more included from mplayer.c
arpi_esp
parents:
426
diff
changeset
|
70 if(demux->a_streams[id]) |
426 | 71 demux->audio->id=id; |
1 | 72 |
73 if(id==demux->audio->id){ | |
74 // audio | |
75 ds=demux->audio; | |
426 | 76 if(!ds->sh){ |
587
8511095c5283
stage#1 completed: c files no more included from mplayer.c
arpi_esp
parents:
426
diff
changeset
|
77 ds->sh=demux->a_streams[id]; |
426 | 78 if(verbose) printf("Auto-selected ASF audio ID = %d\n",ds->id); |
79 } | |
1 | 80 } else |
81 if(id==demux->video->id){ | |
82 // video | |
83 ds=demux->video; | |
426 | 84 if(!ds->sh){ |
587
8511095c5283
stage#1 completed: c files no more included from mplayer.c
arpi_esp
parents:
426
diff
changeset
|
85 ds->sh=demux->v_streams[id]; |
426 | 86 if(verbose) printf("Auto-selected ASF video ID = %d\n",ds->id); |
87 } | |
1 | 88 } |
89 | |
90 if(ds){ | |
91 if(ds->asf_packet){ | |
92 if(ds->asf_seq!=seq){ | |
93 // closed segment, finalize packet: | |
94 if(ds==demux->audio) | |
95 if(asf_scrambling_h>1 && asf_scrambling_w>1) | |
96 asf_descrambling(ds->asf_packet->buffer,ds->asf_packet->len); | |
97 ds_add_packet(ds,ds->asf_packet); | |
98 ds->asf_packet=NULL; | |
99 } else { | |
100 // append data to it! | |
101 demux_packet_t* dp=ds->asf_packet; | |
102 if(dp->len!=offs && offs!=-1) printf("warning! fragment.len=%d BUT next fragment offset=%d \n",dp->len,offs); | |
103 dp->buffer=realloc(dp->buffer,dp->len+len); | |
104 memcpy(dp->buffer+dp->len,data,len); | |
105 if(verbose>=4) printf("data appended! %d+%d\n",dp->len,len); | |
106 dp->len+=len; | |
107 // we are ready now. | |
108 return 1; | |
109 } | |
110 } | |
111 // create new packet: | |
112 { demux_packet_t* dp; | |
113 if(offs>0){ | |
599 | 114 if(verbose) printf("warning! broken fragment, %d bytes missing \n",offs); |
1 | 115 return 0; |
116 } | |
117 dp=new_demux_packet(len); | |
118 memcpy(dp->buffer,data,len); | |
119 dp->pts=time*0.001f; | |
979 | 120 dp->flags=keyframe; |
1 | 121 // if(ds==demux->video) printf("ASF time: %8d dur: %5d \n",time,dur); |
122 dp->pos=demux->filepos; | |
123 ds->asf_packet=dp; | |
124 ds->asf_seq=seq; | |
125 // we are ready now. | |
126 return 1; | |
127 } | |
128 } | |
129 | |
130 return 0; | |
131 } | |
132 | |
133 //static int num_elementary_packets100=0; | |
134 //static int num_elementary_packets101=0; | |
135 | |
136 // return value: | |
137 // 0 = EOF or no stream found | |
138 // 1 = successfully read a packet | |
139 int demux_asf_fill_buffer(demuxer_t *demux){ | |
140 | |
141 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
|
142 if(demux->filepos>=demux->movi_end){ |
1 | 143 demux->stream->eof=1; |
144 return 0; | |
145 } | |
146 | |
587
8511095c5283
stage#1 completed: c files no more included from mplayer.c
arpi_esp
parents:
426
diff
changeset
|
147 stream_read(demux->stream,asf_packet,asf_packetsize); |
1 | 148 if(demux->stream->eof) return 0; // EOF |
149 | |
150 if(asf_packet[0]==0x82){ | |
151 unsigned char flags=asf_packet[3]; | |
152 unsigned char segtype=asf_packet[4]; | |
153 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
|
154 unsigned char* p_end=p+asf_packetsize; |
1 | 155 unsigned long time; |
156 unsigned short duration; | |
157 int segs=1; | |
158 unsigned char segsizetype=0x80; | |
159 int seg; | |
160 int padding=0; | |
161 int plen; | |
162 | |
163 if(verbose>1){ | |
164 int i; | |
165 for(i=0;i<16;i++) printf(" %02X",asf_packet[i]); | |
166 printf("\n"); | |
167 } | |
168 | |
169 //if(segtype!=0x5d) printf("Warning! packet[4] != 0x5d \n"); | |
170 | |
171 // Calculate packet size (plen): | |
172 if(flags&0x40){ | |
173 // Explicit (absoulte) packet size | |
1342 | 174 plen=LOAD_LE16(p); p+=2; |
1 | 175 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
|
176 if(plen>asf_packetsize) printf("Warning! plen>packetsize! (%d>%d) \n",plen,asf_packetsize); |
1 | 177 if(flags&(8|16)){ |
178 padding=p[0];p++; | |
179 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
|
180 if(verbose)printf("Warning! explicit=%d padding=%d \n",plen,asf_packetsize-padding); |
1 | 181 } |
182 } else { | |
183 // Padding (relative) size | |
184 if(flags&8){ | |
185 padding=p[0];++p; | |
186 } else | |
187 if(flags&16){ | |
1342 | 188 padding=LOAD_LE16(p);p+=2; |
1 | 189 } |
587
8511095c5283
stage#1 completed: c files no more included from mplayer.c
arpi_esp
parents:
426
diff
changeset
|
190 plen=asf_packetsize-padding; |
1 | 191 } |
192 | |
1342 | 193 time = LOAD_LE32(p); p+=4; |
194 duration = LOAD_LE16(p); p+=2; | |
1 | 195 if(flags&1){ |
196 segsizetype=p[0] & 0xC0; | |
197 segs=p[0] & 0x3F; | |
198 ++p; | |
199 } | |
600 | 200 if(verbose>=4) printf("%08X: flag=%02X segs=%d pad=%d time=%ld dur=%d\n", |
1 | 201 demux->filepos,flags,segs,padding,time,duration); |
202 for(seg=0;seg<segs;seg++){ | |
203 //ASF_segmhdr_t* sh; | |
204 unsigned char streamno; | |
205 unsigned char seq; | |
206 int len; | |
207 unsigned long x; | |
208 unsigned char type; | |
209 unsigned long time2; | |
979 | 210 int keyframe=0; |
1 | 211 |
212 if(p>=p_end) printf("Warning! invalid packet 1, sig11 coming soon...\n"); | |
213 | |
214 if(verbose>1){ | |
215 int i; | |
216 printf("seg %d:",seg); | |
217 for(i=0;i<16;i++) printf(" %02X",p[i]); | |
218 printf("\n"); | |
219 } | |
220 | |
221 streamno=p[0]&0x7F; | |
979 | 222 if(p[0]&0x80) keyframe=1; |
1 | 223 seq=p[1]; |
224 p+=2; | |
225 | |
226 switch(segtype){ | |
227 case 0x55: | |
228 x=*((unsigned char*)p); | |
229 p++; | |
230 break; | |
231 case 0x59: | |
1342 | 232 x=LOAD_LE16(p); |
1 | 233 p+=2; |
234 break; | |
235 case 0x5D: | |
1342 | 236 x=LOAD_LE32(p); |
1 | 237 p+=4; |
238 break; | |
239 default: | |
240 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
|
241 x=0; |
1 | 242 } |
243 | |
244 type=p[0]; p++; // 0x01: grouping 0x08: single | |
245 | |
246 switch(type){ | |
247 case 0x01: | |
248 //printf("grouping: %02X \n",p[0]); | |
249 ++p; // skip unknown byte | |
250 break; | |
251 case 0x08: | |
252 //printf("!!! obj_length = %d\n",*((unsigned long*)p)); | |
253 p+=4; | |
1342 | 254 time2=LOAD_LE32(p);p+=4; |
1 | 255 break; |
256 default: | |
257 printf("unknown segment type: 0x%02X \n",type); | |
258 } | |
259 | |
260 if(flags&1){ | |
261 // multiple segments | |
262 if(segsizetype==0x40){ | |
1342 | 263 len=*((unsigned char*)p);p++; // 1 byte |
1 | 264 } else { |
1342 | 265 len=LOAD_LE16(p);p+=2; // 2 byte |
1 | 266 } |
267 } else { | |
268 // single segment | |
269 len=plen-(p-asf_packet); | |
270 } | |
271 if(len<0 || (p+len)>=p_end){ | |
272 printf("ASF_parser: warning! segment len=%d\n",len); | |
273 } | |
274 if(verbose>=4) printf(" seg #%d: streamno=%d seq=%d type=%02X len=%d\n",seg,streamno,seq,type,len); | |
275 | |
276 switch(type){ | |
277 case 0x01: | |
278 // GROUPING: | |
279 //printf("ASF_parser: warning! grouping (flag=1) not yet supported!\n",len); | |
280 //printf(" total: %d \n",len); | |
281 while(len>0){ | |
282 int len2=p[0]; | |
283 p++; | |
284 //printf(" group part: %d bytes\n",len2); | |
979 | 285 demux_asf_read_packet(demux,p,len2,streamno,seq,x,duration,-1,keyframe); |
1 | 286 p+=len2; |
287 len-=len2+1; | |
288 } | |
289 if(len!=0){ | |
290 printf("ASF_parser: warning! groups total != len\n"); | |
291 } | |
292 break; | |
293 case 0x08: | |
294 // NO GROUPING: | |
295 //printf("fragment offset: %d \n",sh->x); | |
979 | 296 demux_asf_read_packet(demux,p,len,streamno,seq,time2,duration,x,keyframe); |
1 | 297 p+=len; |
298 break; | |
299 } | |
300 | |
301 } // for segs | |
302 return 1; // success | |
303 } | |
304 | |
305 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]); | |
306 return 0; | |
307 } | |
1466 | 308 |
309 | |
310 #include "wine/mmreg.h" | |
311 #include "wine/avifmt.h" | |
312 #include "wine/vfw.h" | |
313 | |
314 #include "codec-cfg.h" | |
315 #include "stheader.h" | |
316 | |
317 | |
318 void demux_seek_asf(demuxer_t *demuxer,float rel_seek_secs,int flags){ | |
319 demux_stream_t *d_audio=demuxer->audio; | |
320 demux_stream_t *d_video=demuxer->video; | |
321 sh_audio_t *sh_audio=d_audio->sh; | |
322 // sh_video_t *sh_video=d_video->sh; | |
323 | |
324 //FIXME: OFF_T - didn't test ASF case yet (don't have a large asf...) | |
325 //FIXME: reports good or bad to steve@daviesfam.org please | |
326 | |
327 //================= seek in ASF ========================== | |
328 float p_rate=10; // packets / sec | |
329 off_t rel_seek_packs=rel_seek_secs*p_rate; // FIXME: int may be enough? | |
330 off_t rel_seek_bytes=rel_seek_packs*asf_packetsize; | |
331 off_t newpos; | |
332 //printf("ASF: packs: %d duration: %d \n",(int)fileh.packets,*((int*)&fileh.duration)); | |
333 // printf("ASF_seek: %d secs -> %d packs -> %d bytes \n", | |
334 // rel_seek_secs,rel_seek_packs,rel_seek_bytes); | |
335 newpos=demuxer->filepos+rel_seek_bytes; | |
336 if(newpos<0 || newpos<demuxer->movi_start) newpos=demuxer->movi_start; | |
337 // printf("\r -- asf: newpos=%d -- \n",newpos); | |
338 stream_seek(demuxer->stream,newpos); | |
339 | |
340 ds_fill_buffer(d_video); | |
341 if(sh_audio){ | |
342 ds_fill_buffer(d_audio); | |
343 resync_audio_stream(sh_audio); | |
344 } | |
345 | |
346 while(1){ | |
347 if(sh_audio && !d_audio->eof){ | |
348 float a_pts=d_audio->pts; | |
349 a_pts+=(ds_tell_pts(d_audio)-sh_audio->a_in_buffer_len)/(float)sh_audio->i_bps; | |
350 // sync audio: | |
351 if (d_video->pts > a_pts){ | |
352 skip_audio_frame(sh_audio); | |
353 // if(!ds_fill_buffer(d_audio)) sh_audio=NULL; // skip audio. EOF? | |
354 continue; | |
355 } | |
356 } | |
357 if(d_video->flags&1) break; // found a keyframe! | |
358 if(!ds_fill_buffer(d_video)) break; // skip frame. EOF? | |
359 } | |
360 | |
361 | |
362 } | |
363 |