1
|
1 #define SAVE_STREAMS
|
|
2
|
|
3 // simple ASF header display program by A'rpi/ESP-team
|
|
4 // .asf fileformat docs from http://divx.euro.ru
|
|
5
|
|
6 #include <stdio.h>
|
|
7 #include <stdlib.h>
|
|
8
|
|
9 typedef struct __attribute__((packed))
|
|
10 {
|
|
11 long biSize; // sizeof(BITMAPINFOHEADER)
|
|
12 long biWidth;
|
|
13 long biHeight;
|
|
14 short biPlanes; // unused
|
|
15 short biBitCount;
|
|
16 long biCompression; // fourcc of image
|
|
17 long biSizeImage; // size of image. For uncompressed images
|
|
18 // ( biCompression 0 or 3 ) can be zero.
|
|
19
|
|
20
|
|
21 long biXPelsPerMeter; // unused
|
|
22 long biYPelsPerMeter; // unused
|
|
23 long biClrUsed; // valid only for palettized images.
|
|
24 // Number of colors in palette.
|
|
25 long biClrImportant;
|
|
26 } BITMAPINFOHEADER;
|
|
27
|
|
28 typedef struct
|
|
29 {
|
|
30 short wFormatTag; // value that identifies compression format
|
|
31 short nChannels;
|
|
32 long nSamplesPerSec;
|
|
33 long nAvgBytesPerSec;
|
|
34 short nBlockAlign; // size of a data sample
|
|
35 short wBitsPerSample;
|
|
36 short cbSize; // size of format-specific data
|
|
37 } WAVEFORMATEX;
|
|
38
|
|
39 typedef struct __attribute__((packed)) {
|
|
40 unsigned char guid[16];
|
|
41 unsigned long long size;
|
|
42 } ASF_obj_header_t;
|
|
43
|
|
44 typedef struct __attribute__((packed)) {
|
|
45 ASF_obj_header_t objh;
|
|
46 unsigned int cno; // number of subchunks
|
|
47 unsigned char v1; // unknown (0x01)
|
|
48 unsigned char v2; // unknown (0x02)
|
|
49 } ASF_header_t;
|
|
50
|
|
51 typedef struct __attribute__((packed)) {
|
|
52 unsigned char client[16]; // Client GUID
|
|
53 unsigned long long file_size;
|
|
54 unsigned long long creat_time; //File creation time FILETIME 8
|
|
55 unsigned long long packets; //Number of packets UINT64 8
|
|
56 unsigned long long end_timestamp; //Timestamp of the end position UINT64 8
|
|
57 unsigned long long duration; //Duration of the playback UINT64 8
|
|
58 unsigned long start_timestamp; //Timestamp of the start position UINT32 4
|
|
59 unsigned long unk1; //Unknown, maybe reserved ( usually contains 0 ) UINT32 4
|
|
60 unsigned long flags; //Unknown, maybe flags ( usually contains 2 ) UINT32 4
|
|
61 unsigned long packetsize; //Size of packet, in bytes UINT32 4
|
|
62 unsigned long packetsize2; //Size of packet ( confirm ) UINT32 4
|
|
63 unsigned long frame_size; //Size of uncompressed video frame UINT32 4
|
|
64 } ASF_file_header_t;
|
|
65
|
|
66 typedef struct __attribute__((packed)) {
|
|
67 unsigned char type[16]; // Stream type (audio/video) GUID 16
|
|
68 unsigned char concealment[16]; // Audio error concealment type GUID 16
|
|
69 unsigned long long unk1; // Unknown, maybe reserved ( usually contains 0 ) UINT64 8
|
|
70 unsigned long type_size; //Total size of type-specific data UINT32 4
|
|
71 unsigned long stream_size; //Size of stream-specific data UINT32 4
|
|
72 unsigned short stream_no; //Stream number UINT16 2
|
|
73 unsigned long unk2; //Unknown UINT32 4
|
|
74 } ASF_stream_header_t;
|
|
75
|
|
76 typedef struct __attribute__((packed)) {
|
|
77 unsigned char streamno;
|
|
78 unsigned char seq;
|
|
79 unsigned long x;
|
|
80 unsigned char flag;
|
|
81 } ASF_segmhdr_t;
|
|
82
|
|
83
|
|
84 ASF_header_t asfh;
|
|
85 ASF_obj_header_t objh;
|
|
86 ASF_file_header_t fileh;
|
|
87 ASF_stream_header_t streamh;
|
|
88 unsigned char buffer[8192];
|
|
89
|
|
90 int i;
|
|
91
|
|
92 char* chunk_type(unsigned char* guid){
|
|
93 switch(*((unsigned int*)guid)){
|
|
94 case 0xF8699E40: return "guid_audio_stream";
|
|
95 case 0xBC19EFC0: return "guid_video_stream";
|
|
96 case 0x49f1a440: return "guid_audio_conceal_none";
|
|
97 case 0xbfc3cd50: return "guid_audio_conceal_interleave";
|
|
98 case 0x75B22630: return "guid_header";
|
|
99 case 0x75b22636: return "guid_data_chunk";
|
|
100 case 0x33000890: return "guid_index_chunk";
|
|
101 case 0xB7DC0791: return "guid_stream_header";
|
|
102 case 0xD6E229D1: return "guid_header_2_0";
|
|
103 case 0x8CABDCA1: return "guid_file_header";
|
|
104 }
|
|
105 return NULL;
|
|
106 }
|
|
107
|
|
108 void print_wave_header(WAVEFORMATEX *h){
|
|
109
|
|
110 printf("======= WAVE Format =======\n");
|
|
111
|
|
112 printf("Format Tag: %d (0x%X)\n",h->wFormatTag,h->wFormatTag);
|
|
113 printf("Channels: %d\n",h->nChannels);
|
|
114 printf("Samplerate: %d\n",h->nSamplesPerSec);
|
|
115 printf("avg byte/sec: %d\n",h->nAvgBytesPerSec);
|
|
116 printf("Block align: %d\n",h->nBlockAlign);
|
|
117 printf("bits/sample: %d\n",h->wBitsPerSample);
|
|
118 printf("cbSize: %d\n",h->cbSize);
|
|
119
|
|
120 switch(h->wFormatTag){
|
|
121 case 0x01: printf("Audio in PCM format\n");break;
|
|
122 case 0x50: printf("Audio in MPEG Layer 1/2 format\n");break;
|
|
123 case 0x55: printf("Audio in MPEG Layer-3 format\n");break; // ACM
|
|
124 case 0x02: printf("Audio in MS ADPCM format\n");break; // ACM
|
|
125 case 0x11: printf("Audio in IMA ADPCM format\n");break; // ACM
|
|
126 case 0x31:
|
|
127 case 0x32: printf("Audio in MS GSM 6.10 format\n");break; // ACM
|
|
128 case 0x160:
|
|
129 case 0x161: printf("Audio in DivX WMA format\n");break; // ACM
|
|
130 default: printf("Audio in UNKNOWN (id=0x%X) format\n",h->wFormatTag);
|
|
131 }
|
|
132
|
|
133 printf("===========================\n");
|
|
134
|
|
135
|
|
136 }
|
|
137
|
|
138 void print_video_header(BITMAPINFOHEADER *h){
|
|
139 printf("======= VIDEO Format ======\n");
|
|
140 printf(" biSize %d\n", h->biSize);
|
|
141 printf(" biWidth %d\n", h->biWidth);
|
|
142 printf(" biHeight %d\n", h->biHeight);
|
|
143 printf(" biPlanes %d\n", h->biPlanes);
|
|
144 printf(" biBitCount %d\n", h->biBitCount);
|
|
145 printf(" biCompression %d='%.4s'\n", h->biCompression, &h->biCompression);
|
|
146 printf(" biSizeImage %d\n", h->biSizeImage);
|
|
147 printf("===========================\n");
|
|
148 }
|
|
149
|
|
150 FILE* streams[128];
|
|
151
|
|
152 int main(int argc,char* argv[]){
|
|
153 FILE *f=fopen(argc>1?argv[1]:"Alice Deejay - Back In My Life.asf","rb");
|
|
154
|
|
155 if(!f){ printf("file not found\n");exit(1);}
|
|
156
|
|
157 //printf("sizeof=%d\n",sizeof(objh));
|
|
158 //printf("sizeof=%d\n",sizeof(asfh));
|
|
159
|
|
160 fread(&asfh,sizeof(asfh),1,f); // header obj
|
|
161 //for(i=0;i<16;i++) printf("%02X ",asfh.objh.guid[i]);
|
|
162 printf("[%s] %d (subchunks: %d)\n",chunk_type(asfh.objh.guid),(int) asfh.objh.size,asfh.cno);
|
|
163
|
|
164 while(fread(&objh,sizeof(objh),1,f)>0){
|
|
165 int pos=ftell(f);
|
|
166 // for(i=0;i<16;i++) printf("%02X ",objh.guid[i]);
|
|
167 printf("0x%08X [%s] %d\n",pos-sizeof(objh), chunk_type(objh.guid),(int) objh.size);
|
|
168 switch(*((unsigned int*)&objh.guid)){
|
|
169 case 0xB7DC0791: // guid_stream_header
|
|
170 fread(&streamh,sizeof(streamh),1,f);
|
|
171 printf("stream type: %s\n",chunk_type(streamh.type));
|
|
172 printf("stream concealment: %s\n",chunk_type(streamh.concealment));
|
|
173 printf("type: %d bytes, stream: %d bytes ID: %d\n",(int)streamh.type_size,(int)streamh.stream_size,(int)streamh.stream_no);
|
|
174 printf("FILEPOS=0x%X\n",ftell(f));
|
|
175 // type-specific data:
|
|
176 fread(buffer,streamh.type_size,1,f);
|
|
177 switch(*((unsigned int*)&streamh.type)){
|
|
178 case 0xF8699E40: // guid_audio_stream
|
|
179 print_wave_header((WAVEFORMATEX*)buffer);
|
|
180 break;
|
|
181 case 0xBC19EFC0: // guid_video_stream
|
|
182 print_video_header((BITMAPINFOHEADER*)&buffer[4+4+1+2]);
|
|
183 break;
|
|
184 }
|
|
185 // stream-specific data:
|
|
186 fread(buffer,streamh.stream_size,1,f);
|
|
187 break;
|
|
188 // case 0xD6E229D1: return "guid_header_2_0";
|
|
189 case 0x8CABDCA1: // guid_file_header
|
|
190 fread(&fileh,sizeof(fileh),1,f);
|
|
191 printf("packets: %d flags: %d pack_size: %d frame_size: %d\n",(int)fileh.packets,(int)fileh.flags,(int)fileh.packetsize,(int)fileh.frame_size);
|
|
192 break;
|
|
193 case 0x75b22636: // guid_data_chunk
|
|
194 { int endp=pos+objh.size-sizeof(objh);
|
|
195 unsigned char* packet=malloc((int)fileh.packetsize);
|
|
196 int fpos;
|
|
197 fseek(f,26,SEEK_CUR);
|
|
198 while((fpos=ftell(f))<endp){
|
|
199 fread(packet,(int)fileh.packetsize,1,f);
|
|
200 if(packet[0]==0x82){
|
|
201 unsigned char flags=packet[3];
|
|
202 unsigned char* p=&packet[5];
|
|
203 unsigned long time;
|
|
204 unsigned short duration;
|
|
205 int segs=1;
|
|
206 int seg;
|
|
207 int padding=0;
|
|
208 if(flags&8){
|
|
209 padding=p[0];++p;
|
|
210 } else
|
|
211 if(flags&16){
|
|
212 padding=p[0]|(p[1]<<8);p+=2;
|
|
213 }
|
|
214 time=*((unsigned long*)p);p+=4;
|
|
215 duration=*((unsigned short*)p);p+=2;
|
|
216 if(flags&1){
|
|
217 segs=p[0]-0x80;++p;
|
|
218 }
|
|
219 printf("%08X: flag=%02X segs=%d pad=%d time=%d dur=%d\n",
|
|
220 fpos,flags,segs,padding,time,duration);
|
|
221 for(seg=0;seg<segs;seg++){
|
|
222 ASF_segmhdr_t* sh=(ASF_segmhdr_t*)p;
|
|
223 int len=0;
|
|
224 p+=sizeof(ASF_segmhdr_t);
|
|
225 if(sh->flag&8) p+=8;// else
|
|
226 if(sh->flag&1) ++p;
|
|
227 if(flags&1){
|
|
228 len=*((unsigned short*)p);p+=2;
|
|
229 }
|
|
230 printf(" seg #%d: streamno=%d seq=%d flag=%02X len=%d\n",seg,sh->streamno&0x7F,sh->seq,sh->flag,len);
|
|
231 #ifdef SAVE_STREAMS
|
|
232 if(!streams[sh->streamno&0x7F]){
|
|
233 char name[256];
|
|
234 sprintf(name,"stream%02X.dat",sh->streamno&0x7F);
|
|
235 streams[sh->streamno&0x7F]=fopen(name,"wb");
|
|
236 }
|
|
237 fwrite(p,len,1,streams[sh->streamno&0x7F]);
|
|
238 #endif
|
|
239 p+=len;
|
|
240 }
|
|
241 } else
|
|
242 printf("%08X: UNKNOWN %02X %02X %02X %02X %02X...\n",fpos,packet[0],packet[1],packet[2],packet[3],packet[4]);
|
|
243 }
|
|
244 }
|
|
245 break;
|
|
246
|
|
247 // case 0x33000890: return "guid_index_chunk";
|
|
248
|
|
249 }
|
|
250 fseek(f,pos+objh.size-sizeof(objh),SEEK_SET);
|
|
251 }
|
|
252
|
|
253
|
|
254 }
|
|
255
|