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