annotate aviparse.c @ 672:c716b964679e

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