1
|
1 // AVI Parser tool v0.1 (C) 2000. by A'rpi/ESP-team
|
|
2
|
|
3 #include <stdio.h>
|
|
4 #include <stdlib.h>
|
|
5
|
|
6 #include <signal.h>
|
|
7
|
|
8 #include <sys/ioctl.h>
|
|
9 #include <unistd.h>
|
|
10 #include <sys/mman.h>
|
|
11
|
|
12 #include <sys/types.h>
|
|
13 #include <sys/wait.h>
|
|
14 #include <sys/time.h>
|
|
15 #include <sys/stat.h>
|
|
16 #include <fcntl.h>
|
|
17 #include <linux/cdrom.h>
|
|
18
|
|
19 #include "config.h"
|
|
20
|
|
21 #include "loader.h"
|
|
22 #include "wine/avifmt.h"
|
|
23 //#include "libvo/video_out.h"
|
|
24
|
|
25 #include "linux/timer.h"
|
|
26 #include "linux/shmem.h"
|
|
27
|
|
28 #include "help_avp.h"
|
|
29
|
|
30 #define DEBUG if(0)
|
|
31
|
|
32 //static int show_packets=0;
|
|
33
|
|
34 typedef struct {
|
|
35 // file:
|
|
36 MainAVIHeader avih;
|
|
37 unsigned int movi_start;
|
|
38 unsigned int movi_end;
|
|
39 // index:
|
|
40 AVIINDEXENTRY* idx;
|
|
41 int idx_size;
|
|
42 int idx_pos;
|
|
43 // int a_idx;
|
|
44 // int v_idx;
|
|
45 // video:
|
|
46 AVIStreamHeader video;
|
|
47 char *video_codec;
|
|
48 BITMAPINFOHEADER bih; // in format
|
|
49 BITMAPINFOHEADER o_bih; // out format
|
|
50 HIC hic;
|
|
51 void *our_out_buffer;
|
|
52 char yuv_supported; // 1 if codec support YUY2 output format
|
|
53 char yuv_hack_needed; // requires for divx & mpeg4
|
|
54 // audio:
|
|
55 AVIStreamHeader audio;
|
|
56 char *audio_codec;
|
|
57 char wf_ext[64]; // in format
|
|
58 WAVEFORMATEX wf; // out format
|
|
59 HACMSTREAM srcstream;
|
|
60 int audio_minsize;
|
|
61 } avi_header_t;
|
|
62
|
|
63 avi_header_t avi_header;
|
|
64
|
|
65 #include "aviprint.c"
|
|
66 //#include "codecs.c"
|
|
67
|
|
68 //**************************************************************************//
|
|
69 #include "stream.c"
|
|
70 //#include "demuxer.c"
|
|
71 //#include "demux_avi.c"
|
|
72
|
|
73 static stream_t* stream=NULL;
|
|
74
|
|
75 //**************************************************************************//
|
|
76
|
|
77 extern int errno;
|
|
78 static int play_in_bg=0;
|
|
79
|
|
80 void exit_player(){
|
|
81 // int tmp;
|
|
82 // restore terminal:
|
|
83 getch2_disable();
|
|
84 printf("\n\n");
|
|
85 if(play_in_bg) system("xsetroot -solid \\#000000");
|
|
86 exit(1);
|
|
87 }
|
|
88
|
|
89 void exit_sighandler(int x){
|
|
90 printf("\nmpgplay2 interrupted by signal %d\n",x);
|
|
91 exit_player();
|
|
92 }
|
|
93
|
|
94
|
|
95 int main(int argc,char* argv[]){
|
|
96 char* filename=NULL; //"MI2-Trailer.avi";
|
|
97 int i;
|
|
98 //int seek_to_sec=0;
|
|
99 int seek_to_byte=0;
|
|
100 int f; // filedes
|
|
101 int has_audio=1;
|
|
102 //int audio_format=0;
|
|
103 //int alsa=0;
|
|
104 //int audio_buffer_size=-1;
|
|
105 int audio_id=-1;
|
|
106 //int video_id=-1;
|
|
107 //float default_max_pts_correction=0.01f;
|
|
108 //int delay_corrected=0;
|
|
109 //float force_fps=0;
|
|
110 //float default_fps=25;
|
|
111 //float audio_delay=0;
|
|
112 int stream_type;
|
|
113 //int elementary_stream=0;
|
|
114 int vcd_track=0;
|
|
115 #ifdef VCD_CACHE
|
|
116 int vcd_cache_size=128;
|
|
117 #endif
|
|
118 //char* video_driver="mga"; // default
|
|
119 //int out_fmt=0;
|
|
120 int idx_filepos=0;
|
|
121 FILE *audiofile=NULL;
|
|
122 FILE *videofile=NULL;
|
|
123 char *audiofile_name=NULL;
|
|
124 char *videofile_name=NULL;
|
|
125
|
|
126 printf("%s",banner_text);
|
|
127
|
|
128 for(i=1;i<argc;i++){
|
|
129 if(strcmp(argv[i],"-afile")==0) audiofile_name=argv[++i]; else
|
|
130 if(strcmp(argv[i],"-vfile")==0) videofile_name=argv[++i]; else
|
|
131 // if(strcmp(argv[i],"-sb")==0) seek_to_byte=strtol(argv[++i],NULL,0); else
|
|
132 if(strcmp(argv[i],"-aid")==0) audio_id=strtol(argv[++i],NULL,0); else
|
|
133 // if(strcmp(argv[i],"-vid")==0) video_id=strtol(argv[++i],NULL,0); else
|
|
134 // if(strcmp(argv[i],"-afm")==0) audio_format=strtol(argv[++i],NULL,0); else
|
|
135 // if(strcmp(argv[i],"-vcd")==0) vcd_track=strtol(argv[++i],NULL,0); else
|
|
136 if(strcmp(argv[i],"-h")==0) break; else
|
|
137 if(strcmp(argv[i],"--help")==0) break; else
|
|
138 { if(filename){ printf("invalid option: %s\n",filename);exit(1);}
|
|
139 filename=argv[i];
|
|
140 }
|
|
141 }
|
|
142
|
|
143 if(!filename){
|
|
144 if(vcd_track) filename="/dev/cdrom";
|
|
145 // else
|
|
146 // filename="/4/Film/Joan of Arc [Hun DivX]/Joan of Arc - CD2.avi";
|
|
147 { printf("%s",help_text); exit(0);}
|
|
148 }
|
|
149
|
|
150
|
|
151 if(vcd_track){
|
|
152 //============ Open VideoCD track ==============
|
|
153 f=open(filename,O_RDONLY);
|
|
154 if(f<0){ printf("Device not found!\n");return 1; }
|
|
155 vcd_read_toc(f);
|
|
156 if(!vcd_seek_to_track(f,vcd_track)){ printf("Error selecting VCD track!\n");return 1;}
|
|
157 seek_to_byte+=VCD_SECTOR_DATA*vcd_get_msf();
|
|
158 stream_type=STREAMTYPE_VCD;
|
|
159 #ifdef VCD_CACHE
|
|
160 vcd_cache_init(vcd_cache_size);
|
|
161 #endif
|
|
162 } else {
|
|
163 //============ Open plain FILE ============
|
|
164 f=open(filename,O_RDONLY);
|
|
165 if(f<0){ printf("File not found!\n");return 1; }
|
|
166 stream_type=STREAMTYPE_FILE;
|
|
167 }
|
|
168
|
|
169 //============ Open & Sync stream and detect file format ===============
|
|
170
|
|
171 stream=new_stream(f,stream_type);
|
|
172 //=============== Read AVI header:
|
|
173 { //---- RIFF header:
|
|
174 int id=stream_read_dword_le(stream); // "RIFF"
|
|
175 if(id!=mmioFOURCC('R','I','F','F')){ printf("Not RIFF format file!\n");return 1; }
|
|
176 stream_read_dword_le(stream); //filesize
|
|
177 id=stream_read_dword_le(stream); // "AVI "
|
|
178 if(id!=formtypeAVI){ printf("Not AVI file!\n");return 1; }
|
|
179 }
|
|
180 //---- AVI header:
|
|
181 avi_header.idx_size=0;
|
|
182 while(1){
|
|
183 int id=stream_read_dword_le(stream);
|
|
184 int chunksize,size2;
|
|
185 static int last_fccType=0;
|
|
186 //
|
|
187 if(stream_eof(stream)) break;
|
|
188 //
|
|
189 if(id==mmioFOURCC('L','I','S','T')){
|
|
190 int len=stream_read_dword_le(stream)-4; // list size
|
|
191 id=stream_read_dword_le(stream); // list type
|
|
192 printf("LIST %.4s len=%d\n",&id,len);
|
|
193 if(id==listtypeAVIMOVIE){
|
|
194 // found MOVI header
|
|
195 avi_header.movi_start=stream_tell(stream);
|
|
196 avi_header.movi_end=avi_header.movi_start+len;
|
|
197 // printf("Found movie at 0x%X - 0x%X\n",avi_header.movi_start,avi_header.movi_end);
|
|
198 len=(len+1)&(~1);
|
|
199 stream_skip(stream,len);
|
|
200 }
|
|
201 continue;
|
|
202 }
|
|
203 size2=stream_read_dword_le(stream);
|
|
204 printf("CHUNK %.4s len=%d\n",&id,size2);
|
|
205 chunksize=(size2+1)&(~1);
|
|
206 switch(id){
|
|
207 case ckidAVIMAINHDR: // read 'avih'
|
|
208 stream_read(stream,(char*) &avi_header.avih,sizeof(avi_header.avih));
|
|
209 chunksize-=sizeof(avi_header.avih);
|
|
210 print_avih(&avi_header.avih);
|
|
211 break;
|
|
212 case ckidSTREAMHEADER: { // read 'strh'
|
|
213 AVIStreamHeader h;
|
|
214 stream_read(stream,(char*) &h,sizeof(h));
|
|
215 chunksize-=sizeof(h);
|
|
216 if(h.fccType==streamtypeVIDEO) memcpy(&avi_header.video,&h,sizeof(h));else
|
|
217 if(h.fccType==streamtypeAUDIO) memcpy(&avi_header.audio,&h,sizeof(h));
|
|
218 last_fccType=h.fccType;
|
|
219 print_strh(&h);
|
|
220 break; }
|
|
221 case ckidSTREAMFORMAT: { // read 'strf'
|
|
222 if(last_fccType==streamtypeVIDEO){
|
|
223 stream_read(stream,(char*) &avi_header.bih,sizeof(avi_header.bih));
|
|
224 chunksize-=sizeof(avi_header.bih);
|
|
225 // init_video_codec();
|
|
226 // init_video_out();
|
|
227 } else
|
|
228 if(last_fccType==streamtypeAUDIO){
|
|
229 int z=(chunksize<64)?chunksize:64;
|
|
230 printf("found 'wf', %d bytes of %d\n",chunksize,sizeof(WAVEFORMATEX));
|
|
231 stream_read(stream,(char*) &avi_header.wf_ext,z);
|
|
232 chunksize-=z;
|
|
233 print_wave_header((WAVEFORMATEX*)&avi_header.wf_ext);
|
|
234 // init_audio_codec();
|
|
235 // init_audio_out();
|
|
236 }
|
|
237 break;
|
|
238 }
|
|
239 case ckidAVINEWINDEX: {
|
|
240 avi_header.idx_size=size2>>4;
|
|
241 // printf("Reading INDEX block, %d chunks for %d frames\n",
|
|
242 // avi_header.idx_size,avi_header.avih.dwTotalFrames);
|
|
243 avi_header.idx=malloc(avi_header.idx_size<<4);
|
|
244 idx_filepos=stream_tell(stream);
|
|
245 stream_read(stream,(char*)avi_header.idx,avi_header.idx_size<<4);
|
|
246 chunksize-=avi_header.idx_size<<4;
|
|
247 print_index();
|
|
248 break;
|
|
249 }
|
|
250 }
|
|
251 if(chunksize>0) stream_skip(stream,chunksize); else
|
|
252 if(chunksize<0) printf("WARNING!!! chunksize=%d (id=%.4s)\n",chunksize,&id);
|
|
253
|
|
254 }
|
|
255
|
|
256 printf("----------------------------------------------------------------------\n");
|
|
257 printf("Found movie at 0x%X - 0x%X\n",avi_header.movi_start,avi_header.movi_end);
|
|
258 if(avi_header.idx_size<=0){ printf("No index block found!\n");return 0;}
|
|
259 printf("Index block at 0x%X, %d entries for %d frames\n",idx_filepos,
|
|
260 avi_header.idx_size,avi_header.avih.dwTotalFrames );
|
|
261
|
|
262 stream_reset(stream);
|
|
263 stream_seek(stream,avi_header.movi_start);
|
|
264 avi_header.idx_pos=0;
|
|
265
|
|
266 if(audiofile_name) audiofile=fopen(audiofile_name,"wb");
|
|
267 if(videofile_name) videofile=fopen(videofile_name,"wb");
|
|
268
|
|
269 for(i=0;i<avi_header.idx_size;i++){
|
|
270 #if 0
|
|
271 printf("%.4s %4X %08X %d ",
|
|
272 &avi_header.idx[i].ckid,
|
|
273 avi_header.idx[i].dwFlags,
|
|
274 avi_header.idx[i].dwChunkOffset,
|
|
275 avi_header.idx[i].dwChunkLength
|
|
276 );fflush(stdout);
|
|
277 #endif
|
|
278 if(avi_header.idx[i].ckid&AVIIF_LIST){
|
|
279 // printf("LIST\n");
|
|
280 } else {
|
|
281 int id,size;
|
|
282 stream_seek(stream,avi_header.movi_start+avi_header.idx[i].dwChunkOffset-4);
|
|
283 id=stream_read_dword_le(stream);
|
|
284 size=stream_read_dword_le(stream);
|
|
285 if(id!=avi_header.idx[i].ckid){
|
|
286 printf("ChunkID mismatch! raw=%.4s (0x%X) idx=%.4s (0x%X)\n",
|
|
287 &id,avi_header.movi_start+avi_header.idx[i].dwChunkOffset-4,
|
|
288 &avi_header.idx[i].ckid,idx_filepos+16*i
|
|
289 );
|
|
290 continue;
|
|
291 }
|
|
292 if(size!=avi_header.idx[i].dwChunkLength){
|
|
293 printf("ChunkSize mismatch! raw=%d (0x%X) idx=%d (0x%X)\n",
|
|
294 size,avi_header.movi_start+avi_header.idx[i].dwChunkOffset-4,
|
|
295 avi_header.idx[i].dwChunkLength,idx_filepos+16*i
|
|
296 );
|
|
297 continue;
|
|
298 }
|
|
299
|
|
300 if(id!=mmioFOURCC('J','U','N','K'))
|
|
301 if(TWOCCFromFOURCC(id)==cktypeWAVEbytes){
|
|
302 // audio
|
|
303 int aid=StreamFromFOURCC(id);
|
|
304 if(audio_id==-1) audio_id=aid;
|
|
305 if(audio_id==aid){
|
|
306 if(audiofile){
|
|
307 void* mem=malloc(size);
|
|
308 stream_read(stream,mem,size);
|
|
309 fwrite(mem,size,1,audiofile);
|
|
310 free(mem);
|
|
311 }
|
|
312 } else {
|
|
313 printf("Invalid audio stream id: %d (%.4s)\n",aid,&id);
|
|
314 }
|
|
315 } else
|
|
316 if(LOWORD(id)==aviTWOCC('0','0')){
|
|
317 // video
|
|
318 if(videofile){
|
|
319 void* mem=malloc(size);
|
|
320 stream_read(stream,mem,size);
|
|
321 fwrite(&size,4,1,videofile);
|
|
322 fwrite(mem,size,1,videofile);
|
|
323 free(mem);
|
|
324 }
|
|
325 } else {
|
|
326 // unknown
|
|
327 printf("Unknown chunk: %.4s (%X)\n",&id,id);
|
|
328 }
|
|
329
|
|
330 } // LIST or CHUNK
|
|
331
|
|
332 }
|
|
333
|
|
334 return 0;
|
|
335 }
|
|
336
|