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