1
|
1 // ASF file parser for DEMUXER v0.3 by A'rpi/ESP-team
|
|
2
|
|
3 // based on asf file-format doc by Eugene [http://divx.euro.ru]
|
|
4
|
|
5 //static float avi_pts_frametime=1.0f/25.0f;
|
|
6 //static float avi_audio_pts=0;
|
|
7 //static float avi_video_pts=0;
|
|
8
|
|
9 //static int skip_video_frames=0;
|
|
10
|
|
11 typedef struct __attribute__((packed)) {
|
|
12 unsigned char streamno;
|
|
13 unsigned char seq;
|
|
14 unsigned long x;
|
|
15 unsigned char flag;
|
|
16 } ASF_segmhdr_t;
|
|
17
|
|
18
|
|
19 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){
|
|
20 demux_stream_t *ds=NULL;
|
|
21
|
|
22 if(verbose>=4) printf("demux_asf.read_packet: id=%d seq=%d len=%d\n",id,seq,len);
|
|
23
|
|
24 #if 0
|
|
25 if(demux->video->id==-1) {
|
|
26 demux->video->id=asf_video_id;
|
|
27 if(verbose) printf("ASF video ID = %d\n",demux->video->id);
|
|
28 }
|
|
29 if(demux->audio->id==-1)
|
|
30 if(id!=asf_video_id && id!=demux->video->id){
|
|
31 demux->audio->id=id;
|
|
32 if(verbose) printf("ASF audio ID = %d\n",demux->audio->id);
|
|
33 }
|
|
34 #endif
|
|
35
|
|
36 if(id==demux->audio->id){
|
|
37 // audio
|
|
38 ds=demux->audio;
|
|
39 } else
|
|
40 if(id==demux->video->id){
|
|
41 // video
|
|
42 ds=demux->video;
|
|
43 }
|
|
44
|
|
45 if(ds){
|
|
46 if(ds->asf_packet){
|
|
47 if(ds->asf_seq!=seq){
|
|
48 // closed segment, finalize packet:
|
|
49 if(ds==demux->audio)
|
|
50 if(asf_scrambling_h>1 && asf_scrambling_w>1)
|
|
51 asf_descrambling(ds->asf_packet->buffer,ds->asf_packet->len);
|
|
52 ds_add_packet(ds,ds->asf_packet);
|
|
53 ds->asf_packet=NULL;
|
|
54 } else {
|
|
55 // append data to it!
|
|
56 demux_packet_t* dp=ds->asf_packet;
|
|
57 if(dp->len!=offs && offs!=-1) printf("warning! fragment.len=%d BUT next fragment offset=%d \n",dp->len,offs);
|
|
58 dp->buffer=realloc(dp->buffer,dp->len+len);
|
|
59 memcpy(dp->buffer+dp->len,data,len);
|
|
60 if(verbose>=4) printf("data appended! %d+%d\n",dp->len,len);
|
|
61 dp->len+=len;
|
|
62 // we are ready now.
|
|
63 return 1;
|
|
64 }
|
|
65 }
|
|
66 // create new packet:
|
|
67 { demux_packet_t* dp;
|
|
68 if(offs>0){
|
|
69 printf("warning! broken fragment, %d bytes missing \n",offs);
|
|
70 return 0;
|
|
71 }
|
|
72 dp=new_demux_packet(len);
|
|
73 memcpy(dp->buffer,data,len);
|
|
74 dp->pts=time*0.001f;
|
|
75 // if(ds==demux->video) printf("ASF time: %8d dur: %5d \n",time,dur);
|
|
76 dp->pos=demux->filepos;
|
|
77 ds->asf_packet=dp;
|
|
78 ds->asf_seq=seq;
|
|
79 // we are ready now.
|
|
80 return 1;
|
|
81 }
|
|
82 }
|
|
83
|
|
84 return 0;
|
|
85 }
|
|
86
|
|
87 //static int num_elementary_packets100=0;
|
|
88 //static int num_elementary_packets101=0;
|
|
89
|
|
90 // return value:
|
|
91 // 0 = EOF or no stream found
|
|
92 // 1 = successfully read a packet
|
|
93 int demux_asf_fill_buffer(demuxer_t *demux){
|
|
94
|
|
95 demux->filepos=stream_tell(demux->stream);
|
|
96 if(demux->filepos>=demux->endpos){
|
|
97 demux->stream->eof=1;
|
|
98 return 0;
|
|
99 }
|
|
100
|
|
101 stream_read(demux->stream,asf_packet,(int)fileh.packetsize);
|
|
102 if(demux->stream->eof) return 0; // EOF
|
|
103
|
|
104 if(asf_packet[0]==0x82){
|
|
105 unsigned char flags=asf_packet[3];
|
|
106 unsigned char segtype=asf_packet[4];
|
|
107 unsigned char* p=&asf_packet[5];
|
|
108 unsigned char* p_end=p+(int)fileh.packetsize;
|
|
109 unsigned long time;
|
|
110 unsigned short duration;
|
|
111 int segs=1;
|
|
112 unsigned char segsizetype=0x80;
|
|
113 int seg;
|
|
114 int padding=0;
|
|
115 int plen;
|
|
116
|
|
117 if(verbose>1){
|
|
118 int i;
|
|
119 for(i=0;i<16;i++) printf(" %02X",asf_packet[i]);
|
|
120 printf("\n");
|
|
121 }
|
|
122
|
|
123 //if(segtype!=0x5d) printf("Warning! packet[4] != 0x5d \n");
|
|
124
|
|
125 // Calculate packet size (plen):
|
|
126 if(flags&0x40){
|
|
127 // Explicit (absoulte) packet size
|
|
128 plen=p[0]|(p[1]<<8); p+=2;
|
|
129 if(verbose>1)printf("Explicit packet size specified: %d \n",plen);
|
|
130 if(plen>fileh.packetsize) printf("Warning! plen>packetsize! (%d>%d) \n",plen,(int)fileh.packetsize);
|
|
131 if(flags&(8|16)){
|
|
132 padding=p[0];p++;
|
|
133 if(flags&16){ padding|=p[0]<<8; p++;}
|
|
134 if(verbose)printf("Warning! explicit=%d padding=%d \n",plen,fileh.packetsize-padding);
|
|
135 }
|
|
136 } else {
|
|
137 // Padding (relative) size
|
|
138 if(flags&8){
|
|
139 padding=p[0];++p;
|
|
140 } else
|
|
141 if(flags&16){
|
|
142 padding=p[0]|(p[1]<<8);p+=2;
|
|
143 }
|
|
144 plen=fileh.packetsize-padding;
|
|
145 }
|
|
146
|
|
147 time=*((unsigned long*)p);p+=4;
|
|
148 duration=*((unsigned short*)p);p+=2;
|
|
149 if(flags&1){
|
|
150 segsizetype=p[0] & 0xC0;
|
|
151 segs=p[0] & 0x3F;
|
|
152 ++p;
|
|
153 }
|
|
154 if(verbose>=4) printf("%08X: flag=%02X segs=%d pad=%d time=%d dur=%d\n",
|
|
155 demux->filepos,flags,segs,padding,time,duration);
|
|
156 for(seg=0;seg<segs;seg++){
|
|
157 //ASF_segmhdr_t* sh;
|
|
158 unsigned char streamno;
|
|
159 unsigned char seq;
|
|
160 int len;
|
|
161 unsigned long x;
|
|
162 unsigned char type;
|
|
163 unsigned long time2;
|
|
164
|
|
165 if(p>=p_end) printf("Warning! invalid packet 1, sig11 coming soon...\n");
|
|
166
|
|
167 if(verbose>1){
|
|
168 int i;
|
|
169 printf("seg %d:",seg);
|
|
170 for(i=0;i<16;i++) printf(" %02X",p[i]);
|
|
171 printf("\n");
|
|
172 }
|
|
173
|
|
174 streamno=p[0]&0x7F;
|
|
175 seq=p[1];
|
|
176 p+=2;
|
|
177
|
|
178 switch(segtype){
|
|
179 case 0x55:
|
|
180 x=*((unsigned char*)p);
|
|
181 p++;
|
|
182 break;
|
|
183 case 0x59:
|
|
184 x=*((unsigned short*)p);
|
|
185 p+=2;
|
|
186 break;
|
|
187 case 0x5D:
|
|
188 x=*((unsigned long*)p);
|
|
189 p+=4;
|
|
190 break;
|
|
191 default:
|
|
192 printf("Warning! unknown segtype == 0x%2X \n",segtype);
|
|
193 }
|
|
194
|
|
195 type=p[0]; p++; // 0x01: grouping 0x08: single
|
|
196
|
|
197 switch(type){
|
|
198 case 0x01:
|
|
199 //printf("grouping: %02X \n",p[0]);
|
|
200 ++p; // skip unknown byte
|
|
201 break;
|
|
202 case 0x08:
|
|
203 //printf("!!! obj_length = %d\n",*((unsigned long*)p));
|
|
204 p+=4;
|
|
205 time2=*((unsigned long*)p);p+=4;
|
|
206 break;
|
|
207 default:
|
|
208 printf("unknown segment type: 0x%02X \n",type);
|
|
209 }
|
|
210
|
|
211 if(flags&1){
|
|
212 // multiple segments
|
|
213 if(segsizetype==0x40){
|
|
214 len=*((unsigned char*)p);p++; // 1 byte
|
|
215 } else {
|
|
216 len=*((unsigned short*)p);p+=2; // 2 byte
|
|
217 }
|
|
218 } else {
|
|
219 // single segment
|
|
220 len=plen-(p-asf_packet);
|
|
221 }
|
|
222 if(len<0 || (p+len)>=p_end){
|
|
223 printf("ASF_parser: warning! segment len=%d\n",len);
|
|
224 }
|
|
225 if(verbose>=4) printf(" seg #%d: streamno=%d seq=%d type=%02X len=%d\n",seg,streamno,seq,type,len);
|
|
226
|
|
227 switch(type){
|
|
228 case 0x01:
|
|
229 // GROUPING:
|
|
230 //printf("ASF_parser: warning! grouping (flag=1) not yet supported!\n",len);
|
|
231 //printf(" total: %d \n",len);
|
|
232 while(len>0){
|
|
233 int len2=p[0];
|
|
234 p++;
|
|
235 //printf(" group part: %d bytes\n",len2);
|
|
236 demux_asf_read_packet(demux,p,len2,streamno,seq,x,duration,-1);
|
|
237 p+=len2;
|
|
238 len-=len2+1;
|
|
239 }
|
|
240 if(len!=0){
|
|
241 printf("ASF_parser: warning! groups total != len\n");
|
|
242 }
|
|
243 break;
|
|
244 case 0x08:
|
|
245 // NO GROUPING:
|
|
246 //printf("fragment offset: %d \n",sh->x);
|
|
247 demux_asf_read_packet(demux,p,len,streamno,seq,time2,duration,x);
|
|
248 p+=len;
|
|
249 break;
|
|
250 }
|
|
251
|
|
252 } // for segs
|
|
253 return 1; // success
|
|
254 }
|
|
255
|
|
256 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]);
|
|
257 return 0;
|
|
258 }
|