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