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