annotate aviwrite.c @ 164:2790235db59f

even more updates :) about time to crack a joke, eh? :))
author gabucino
date Mon, 19 Mar 2001 20:30:03 +0000
parents 3b5f5d1c5041
children 8511095c5283
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
rev   line source
1
3b5f5d1c5041 Initial revision
arpi_esp
parents:
diff changeset
1
3b5f5d1c5041 Initial revision
arpi_esp
parents:
diff changeset
2 void write_avi_chunk(FILE *f,unsigned int id,int len,void* data){
3b5f5d1c5041 Initial revision
arpi_esp
parents:
diff changeset
3
3b5f5d1c5041 Initial revision
arpi_esp
parents:
diff changeset
4 fwrite(&id,4,1,f);
3b5f5d1c5041 Initial revision
arpi_esp
parents:
diff changeset
5 fwrite(&len,4,1,f);
3b5f5d1c5041 Initial revision
arpi_esp
parents:
diff changeset
6 if(len>0){
3b5f5d1c5041 Initial revision
arpi_esp
parents:
diff changeset
7 if(data){
3b5f5d1c5041 Initial revision
arpi_esp
parents:
diff changeset
8 // DATA
3b5f5d1c5041 Initial revision
arpi_esp
parents:
diff changeset
9 fwrite(data,len,1,f);
3b5f5d1c5041 Initial revision
arpi_esp
parents:
diff changeset
10 if(len&1){ // padding
3b5f5d1c5041 Initial revision
arpi_esp
parents:
diff changeset
11 unsigned char zerobyte=0;
3b5f5d1c5041 Initial revision
arpi_esp
parents:
diff changeset
12 fwrite(&zerobyte,1,1,f);
3b5f5d1c5041 Initial revision
arpi_esp
parents:
diff changeset
13 }
3b5f5d1c5041 Initial revision
arpi_esp
parents:
diff changeset
14 } else {
3b5f5d1c5041 Initial revision
arpi_esp
parents:
diff changeset
15 // JUNK
3b5f5d1c5041 Initial revision
arpi_esp
parents:
diff changeset
16 char *avi_junk_data="[= MPlayer junk data! =]";
3b5f5d1c5041 Initial revision
arpi_esp
parents:
diff changeset
17 if(len&1) ++len; // padding
3b5f5d1c5041 Initial revision
arpi_esp
parents:
diff changeset
18 while(len>0){
3b5f5d1c5041 Initial revision
arpi_esp
parents:
diff changeset
19 int l=strlen(avi_junk_data);
3b5f5d1c5041 Initial revision
arpi_esp
parents:
diff changeset
20 if(l>len) l=len;
3b5f5d1c5041 Initial revision
arpi_esp
parents:
diff changeset
21 fwrite(avi_junk_data,l,1,f);
3b5f5d1c5041 Initial revision
arpi_esp
parents:
diff changeset
22 len-=l;
3b5f5d1c5041 Initial revision
arpi_esp
parents:
diff changeset
23 }
3b5f5d1c5041 Initial revision
arpi_esp
parents:
diff changeset
24 }
3b5f5d1c5041 Initial revision
arpi_esp
parents:
diff changeset
25 }
3b5f5d1c5041 Initial revision
arpi_esp
parents:
diff changeset
26
3b5f5d1c5041 Initial revision
arpi_esp
parents:
diff changeset
27 }
3b5f5d1c5041 Initial revision
arpi_esp
parents:
diff changeset
28
3b5f5d1c5041 Initial revision
arpi_esp
parents:
diff changeset
29
3b5f5d1c5041 Initial revision
arpi_esp
parents:
diff changeset
30 void write_avi_list(FILE *f,unsigned int id,int len){
3b5f5d1c5041 Initial revision
arpi_esp
parents:
diff changeset
31 unsigned int list_id=FOURCC_LIST;
3b5f5d1c5041 Initial revision
arpi_esp
parents:
diff changeset
32 len+=4; // list fix
3b5f5d1c5041 Initial revision
arpi_esp
parents:
diff changeset
33 fwrite(&list_id,4,1,f);
3b5f5d1c5041 Initial revision
arpi_esp
parents:
diff changeset
34 fwrite(&len,4,1,f);
3b5f5d1c5041 Initial revision
arpi_esp
parents:
diff changeset
35 fwrite(&id,4,1,f);
3b5f5d1c5041 Initial revision
arpi_esp
parents:
diff changeset
36 }
3b5f5d1c5041 Initial revision
arpi_esp
parents:
diff changeset
37
3b5f5d1c5041 Initial revision
arpi_esp
parents:
diff changeset
38 struct {
3b5f5d1c5041 Initial revision
arpi_esp
parents:
diff changeset
39 MainAVIHeader avih;
3b5f5d1c5041 Initial revision
arpi_esp
parents:
diff changeset
40 AVIStreamHeader video;
3b5f5d1c5041 Initial revision
arpi_esp
parents:
diff changeset
41 BITMAPINFOHEADER bih;
3b5f5d1c5041 Initial revision
arpi_esp
parents:
diff changeset
42 unsigned int movi_start;
3b5f5d1c5041 Initial revision
arpi_esp
parents:
diff changeset
43 unsigned int movi_end;
3b5f5d1c5041 Initial revision
arpi_esp
parents:
diff changeset
44 unsigned int file_end;
3b5f5d1c5041 Initial revision
arpi_esp
parents:
diff changeset
45 } wah;
3b5f5d1c5041 Initial revision
arpi_esp
parents:
diff changeset
46
3b5f5d1c5041 Initial revision
arpi_esp
parents:
diff changeset
47 void write_avi_header(FILE *f){
3b5f5d1c5041 Initial revision
arpi_esp
parents:
diff changeset
48 unsigned int riff[3];
3b5f5d1c5041 Initial revision
arpi_esp
parents:
diff changeset
49 // RIFF header:
3b5f5d1c5041 Initial revision
arpi_esp
parents:
diff changeset
50 riff[0]=mmioFOURCC('R','I','F','F');
3b5f5d1c5041 Initial revision
arpi_esp
parents:
diff changeset
51 riff[1]=wah.file_end; // filesize
3b5f5d1c5041 Initial revision
arpi_esp
parents:
diff changeset
52 riff[2]=formtypeAVI; // 'AVI '
3b5f5d1c5041 Initial revision
arpi_esp
parents:
diff changeset
53 fwrite(&riff,12,1,f);
3b5f5d1c5041 Initial revision
arpi_esp
parents:
diff changeset
54 // AVI header:
3b5f5d1c5041 Initial revision
arpi_esp
parents:
diff changeset
55 write_avi_list(f,listtypeAVIHEADER,sizeof(wah.avih)+8+12+sizeof(wah.video)+8+sizeof(wah.bih)+8);
3b5f5d1c5041 Initial revision
arpi_esp
parents:
diff changeset
56 write_avi_chunk(f,ckidAVIMAINHDR,sizeof(wah.avih),&wah.avih);
3b5f5d1c5041 Initial revision
arpi_esp
parents:
diff changeset
57 // stream header:
3b5f5d1c5041 Initial revision
arpi_esp
parents:
diff changeset
58 write_avi_list(f,listtypeSTREAMHEADER,sizeof(wah.video)+8+sizeof(wah.bih)+8);
3b5f5d1c5041 Initial revision
arpi_esp
parents:
diff changeset
59 write_avi_chunk(f,ckidSTREAMHEADER,sizeof(wah.video),&wah.video);
3b5f5d1c5041 Initial revision
arpi_esp
parents:
diff changeset
60 write_avi_chunk(f,ckidSTREAMFORMAT,sizeof(wah.bih),&wah.bih);
3b5f5d1c5041 Initial revision
arpi_esp
parents:
diff changeset
61 // JUNK:
3b5f5d1c5041 Initial revision
arpi_esp
parents:
diff changeset
62 write_avi_chunk(f,ckidAVIPADDING,2048-(ftell(f)&2047)-8,NULL);
3b5f5d1c5041 Initial revision
arpi_esp
parents:
diff changeset
63 // 'movi' header:
3b5f5d1c5041 Initial revision
arpi_esp
parents:
diff changeset
64 write_avi_list(f,listtypeAVIMOVIE,wah.movi_end-ftell(f)-12);
3b5f5d1c5041 Initial revision
arpi_esp
parents:
diff changeset
65 wah.movi_start=ftell(f);
3b5f5d1c5041 Initial revision
arpi_esp
parents:
diff changeset
66 }
3b5f5d1c5041 Initial revision
arpi_esp
parents:
diff changeset
67
3b5f5d1c5041 Initial revision
arpi_esp
parents:
diff changeset
68 // called _before_ encoding: (write placeholders and video info)
3b5f5d1c5041 Initial revision
arpi_esp
parents:
diff changeset
69 void write_avi_header_1(FILE *f,int fcc,float fps,int width,int height){
3b5f5d1c5041 Initial revision
arpi_esp
parents:
diff changeset
70 int frames=8*3600*fps; // 8 hours
3b5f5d1c5041 Initial revision
arpi_esp
parents:
diff changeset
71
3b5f5d1c5041 Initial revision
arpi_esp
parents:
diff changeset
72 wah.file_end=
3b5f5d1c5041 Initial revision
arpi_esp
parents:
diff changeset
73 wah.movi_end=0x7f000000;
3b5f5d1c5041 Initial revision
arpi_esp
parents:
diff changeset
74
3b5f5d1c5041 Initial revision
arpi_esp
parents:
diff changeset
75 wah.avih.dwMicroSecPerFrame=1000000.0f/fps;
3b5f5d1c5041 Initial revision
arpi_esp
parents:
diff changeset
76 wah.avih.dwMaxBytesPerSec=fps*500000; // ?????
3b5f5d1c5041 Initial revision
arpi_esp
parents:
diff changeset
77 wah.avih.dwPaddingGranularity=1; // padding
3b5f5d1c5041 Initial revision
arpi_esp
parents:
diff changeset
78 wah.avih.dwFlags=AVIF_ISINTERLEAVED;
3b5f5d1c5041 Initial revision
arpi_esp
parents:
diff changeset
79 wah.avih.dwTotalFrames=frames;
3b5f5d1c5041 Initial revision
arpi_esp
parents:
diff changeset
80 wah.avih.dwInitialFrames=0;
3b5f5d1c5041 Initial revision
arpi_esp
parents:
diff changeset
81 wah.avih.dwStreams=1;
3b5f5d1c5041 Initial revision
arpi_esp
parents:
diff changeset
82 wah.avih.dwSuggestedBufferSize=0x10000; // 1MB
3b5f5d1c5041 Initial revision
arpi_esp
parents:
diff changeset
83 wah.avih.dwWidth=width;
3b5f5d1c5041 Initial revision
arpi_esp
parents:
diff changeset
84 wah.avih.dwHeight=height;
3b5f5d1c5041 Initial revision
arpi_esp
parents:
diff changeset
85 wah.avih.dwReserved[0]=
3b5f5d1c5041 Initial revision
arpi_esp
parents:
diff changeset
86 wah.avih.dwReserved[1]=
3b5f5d1c5041 Initial revision
arpi_esp
parents:
diff changeset
87 wah.avih.dwReserved[2]=
3b5f5d1c5041 Initial revision
arpi_esp
parents:
diff changeset
88 wah.avih.dwReserved[3]=0;
3b5f5d1c5041 Initial revision
arpi_esp
parents:
diff changeset
89
3b5f5d1c5041 Initial revision
arpi_esp
parents:
diff changeset
90 wah.video.fccType=streamtypeVIDEO;
3b5f5d1c5041 Initial revision
arpi_esp
parents:
diff changeset
91 wah.video.fccHandler=fcc;
3b5f5d1c5041 Initial revision
arpi_esp
parents:
diff changeset
92 wah.video.dwFlags=0;
3b5f5d1c5041 Initial revision
arpi_esp
parents:
diff changeset
93 wah.video.wPriority=0;
3b5f5d1c5041 Initial revision
arpi_esp
parents:
diff changeset
94 wah.video.wLanguage=0;
3b5f5d1c5041 Initial revision
arpi_esp
parents:
diff changeset
95 wah.video.dwInitialFrames=0;
3b5f5d1c5041 Initial revision
arpi_esp
parents:
diff changeset
96 wah.video.dwScale=10000;
3b5f5d1c5041 Initial revision
arpi_esp
parents:
diff changeset
97 wah.video.dwRate=fps*10000;
3b5f5d1c5041 Initial revision
arpi_esp
parents:
diff changeset
98 wah.video.dwStart=0;
3b5f5d1c5041 Initial revision
arpi_esp
parents:
diff changeset
99 wah.video.dwLength=frames;
3b5f5d1c5041 Initial revision
arpi_esp
parents:
diff changeset
100 wah.video.dwSuggestedBufferSize=0x100000; // 1MB ????
3b5f5d1c5041 Initial revision
arpi_esp
parents:
diff changeset
101 wah.video.dwQuality=10000;
3b5f5d1c5041 Initial revision
arpi_esp
parents:
diff changeset
102 wah.video.dwSampleSize=width*height*3;
3b5f5d1c5041 Initial revision
arpi_esp
parents:
diff changeset
103
3b5f5d1c5041 Initial revision
arpi_esp
parents:
diff changeset
104 wah.bih.biSize=sizeof(wah.bih); // 40 ?
3b5f5d1c5041 Initial revision
arpi_esp
parents:
diff changeset
105 wah.bih.biWidth=width;
3b5f5d1c5041 Initial revision
arpi_esp
parents:
diff changeset
106 wah.bih.biHeight=height;
3b5f5d1c5041 Initial revision
arpi_esp
parents:
diff changeset
107 wah.bih.biPlanes=1;
3b5f5d1c5041 Initial revision
arpi_esp
parents:
diff changeset
108 wah.bih.biBitCount=24;
3b5f5d1c5041 Initial revision
arpi_esp
parents:
diff changeset
109 wah.bih.biCompression=fcc;
3b5f5d1c5041 Initial revision
arpi_esp
parents:
diff changeset
110 wah.bih.biSizeImage=3*width*height;
3b5f5d1c5041 Initial revision
arpi_esp
parents:
diff changeset
111 wah.bih.biXPelsPerMeter=
3b5f5d1c5041 Initial revision
arpi_esp
parents:
diff changeset
112 wah.bih.biYPelsPerMeter=
3b5f5d1c5041 Initial revision
arpi_esp
parents:
diff changeset
113 wah.bih.biClrUsed=
3b5f5d1c5041 Initial revision
arpi_esp
parents:
diff changeset
114 wah.bih.biClrImportant=0;
3b5f5d1c5041 Initial revision
arpi_esp
parents:
diff changeset
115
3b5f5d1c5041 Initial revision
arpi_esp
parents:
diff changeset
116 write_avi_header(f);
3b5f5d1c5041 Initial revision
arpi_esp
parents:
diff changeset
117 }
3b5f5d1c5041 Initial revision
arpi_esp
parents:
diff changeset
118
3b5f5d1c5041 Initial revision
arpi_esp
parents:
diff changeset
119 void avi_fixate(){
3b5f5d1c5041 Initial revision
arpi_esp
parents:
diff changeset
120 // append index and fix avi headers:
3b5f5d1c5041 Initial revision
arpi_esp
parents:
diff changeset
121 FILE *f1=fopen(encode_name,"r+");
3b5f5d1c5041 Initial revision
arpi_esp
parents:
diff changeset
122 FILE *f2;
3b5f5d1c5041 Initial revision
arpi_esp
parents:
diff changeset
123
3b5f5d1c5041 Initial revision
arpi_esp
parents:
diff changeset
124 if(!f1) return; // error
3b5f5d1c5041 Initial revision
arpi_esp
parents:
diff changeset
125
3b5f5d1c5041 Initial revision
arpi_esp
parents:
diff changeset
126 fseek(f1,0,SEEK_END);
3b5f5d1c5041 Initial revision
arpi_esp
parents:
diff changeset
127 wah.file_end=wah.movi_end=ftell(f1);
3b5f5d1c5041 Initial revision
arpi_esp
parents:
diff changeset
128
3b5f5d1c5041 Initial revision
arpi_esp
parents:
diff changeset
129 // index:
3b5f5d1c5041 Initial revision
arpi_esp
parents:
diff changeset
130 if(encode_index_name && (f2=fopen(encode_index_name,"rb"))){
3b5f5d1c5041 Initial revision
arpi_esp
parents:
diff changeset
131 AVIINDEXENTRY idx;
3b5f5d1c5041 Initial revision
arpi_esp
parents:
diff changeset
132 unsigned int pos=0;
3b5f5d1c5041 Initial revision
arpi_esp
parents:
diff changeset
133 int frames=0;
3b5f5d1c5041 Initial revision
arpi_esp
parents:
diff changeset
134 write_avi_chunk(f1,ckidAVINEWINDEX,0,NULL);
3b5f5d1c5041 Initial revision
arpi_esp
parents:
diff changeset
135 while(fread(&idx,sizeof(idx),1,f2)>0){
3b5f5d1c5041 Initial revision
arpi_esp
parents:
diff changeset
136 idx.dwChunkOffset-=wah.movi_start-4;
3b5f5d1c5041 Initial revision
arpi_esp
parents:
diff changeset
137 fwrite(&idx,sizeof(idx),1,f1);
3b5f5d1c5041 Initial revision
arpi_esp
parents:
diff changeset
138 ++frames;
3b5f5d1c5041 Initial revision
arpi_esp
parents:
diff changeset
139 }
3b5f5d1c5041 Initial revision
arpi_esp
parents:
diff changeset
140 fclose(f2);
3b5f5d1c5041 Initial revision
arpi_esp
parents:
diff changeset
141 unlink(encode_index_name);
3b5f5d1c5041 Initial revision
arpi_esp
parents:
diff changeset
142 wah.file_end=ftell(f1);
3b5f5d1c5041 Initial revision
arpi_esp
parents:
diff changeset
143 // re-write idx1 length:
3b5f5d1c5041 Initial revision
arpi_esp
parents:
diff changeset
144 pos=wah.file_end-wah.movi_end-8;
3b5f5d1c5041 Initial revision
arpi_esp
parents:
diff changeset
145 fseek(f1,wah.movi_end+4,SEEK_SET);
3b5f5d1c5041 Initial revision
arpi_esp
parents:
diff changeset
146 fwrite(&pos,4,1,f1);
3b5f5d1c5041 Initial revision
arpi_esp
parents:
diff changeset
147 // fixup frames:
3b5f5d1c5041 Initial revision
arpi_esp
parents:
diff changeset
148 wah.avih.dwTotalFrames=frames;
3b5f5d1c5041 Initial revision
arpi_esp
parents:
diff changeset
149 wah.video.dwLength=frames;
3b5f5d1c5041 Initial revision
arpi_esp
parents:
diff changeset
150 }
3b5f5d1c5041 Initial revision
arpi_esp
parents:
diff changeset
151
3b5f5d1c5041 Initial revision
arpi_esp
parents:
diff changeset
152 // re-write avi header:
3b5f5d1c5041 Initial revision
arpi_esp
parents:
diff changeset
153 fseek(f1,0,SEEK_SET);
3b5f5d1c5041 Initial revision
arpi_esp
parents:
diff changeset
154 write_avi_header(f1);
3b5f5d1c5041 Initial revision
arpi_esp
parents:
diff changeset
155
3b5f5d1c5041 Initial revision
arpi_esp
parents:
diff changeset
156 fclose(f1);
3b5f5d1c5041 Initial revision
arpi_esp
parents:
diff changeset
157
3b5f5d1c5041 Initial revision
arpi_esp
parents:
diff changeset
158 }
3b5f5d1c5041 Initial revision
arpi_esp
parents:
diff changeset
159
3b5f5d1c5041 Initial revision
arpi_esp
parents:
diff changeset
160
3b5f5d1c5041 Initial revision
arpi_esp
parents:
diff changeset
161