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
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
|
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <sys/types.h>
#include "config.h"
//#include "stream.h"
//#include "demuxer.h"
//#include "stheader.h"
#include "wine/mmreg.h"
#include "wine/avifmt.h"
#include "wine/vfw.h"
#include "aviwrite.h"
aviwrite_stream_t* aviwrite_new_stream(aviwrite_t *muxer,int type){
aviwrite_stream_t* s;
if(muxer->avih.dwStreams>=AVIWRITE_MAX_STREAMS){
printf("Too many streams! increase AVIWRITE_MAX_STREAMS !\n");
return NULL;
}
s=malloc(sizeof(aviwrite_stream_t));
memset(s,0,sizeof(aviwrite_stream_t));
if(!s) return NULL; // no mem!?
muxer->streams[muxer->avih.dwStreams]=s;
s->type=type;
s->id=muxer->avih.dwStreams;
s->timer=0.0;
s->size=0;
switch(type){
case AVIWRITE_TYPE_VIDEO:
s->ckid=mmioFOURCC(('0'+s->id/10),('0'+(s->id%10)),'d','c');
s->h.fccType=streamtypeVIDEO;
if(!muxer->def_v) muxer->def_v=s;
break;
case AVIWRITE_TYPE_AUDIO:
s->ckid=mmioFOURCC(('0'+s->id/10),('0'+(s->id%10)),'w','b');
s->h.fccType=streamtypeAUDIO;
break;
default:
printf("WarninG! unknown stream type: %d\n",type);
return NULL;
}
muxer->avih.dwStreams++;
return s;
}
aviwrite_t* aviwrite_new_muxer(){
aviwrite_t* muxer=malloc(sizeof(aviwrite_t));
memset(muxer,0,sizeof(aviwrite_t));
return muxer;
}
static 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 aviwrite_write_chunk(aviwrite_t *muxer,aviwrite_stream_t *s, FILE *f,int len,unsigned int flags){
// add to the index:
if(muxer->idx_pos>=muxer->idx_size){
muxer->idx_size+=256; // 4kB
muxer->idx=realloc(muxer->idx,16*muxer->idx_size);
}
muxer->idx[muxer->idx_pos].ckid=s->ckid;
muxer->idx[muxer->idx_pos].dwFlags=flags; // keyframe?
muxer->idx[muxer->idx_pos].dwChunkOffset=ftell(f)-(muxer->movi_start-4);
muxer->idx[muxer->idx_pos].dwChunkLength=len;
++muxer->idx_pos;
// write out the chunk:
write_avi_chunk(f,s->ckid,len,s->buffer);
// alter counters:
if(s->h.dwSampleSize){
// CBR
s->h.dwLength+=len/s->h.dwSampleSize;
if(len%s->h.dwSampleSize) printf("Warning! len isn't divisable by samplesize!\n");
} else {
// VBR
s->h.dwLength++;
}
s->timer=(double)s->h.dwLength*s->h.dwScale/s->h.dwRate;
s->size+=len;
if(len>s->h.dwSuggestedBufferSize) s->h.dwSuggestedBufferSize=len;
}
static 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);
}
// muxer->streams[i]->wf->cbSize
#define WFSIZE(wf) (sizeof(WAVEFORMATEX)+(((wf)->cbSize)?((wf)->cbSize-2):0))
void aviwrite_write_header(aviwrite_t *muxer,FILE *f){
unsigned int riff[3];
int i;
unsigned int hdrsize;
// RIFF header:
riff[0]=mmioFOURCC('R','I','F','F');
riff[1]=muxer->file_end; // filesize
riff[2]=formtypeAVI; // 'AVI '
fwrite(&riff,12,1,f);
// update AVI header:
if(muxer->def_v){
muxer->avih.dwMicroSecPerFrame=1000000.0*muxer->def_v->h.dwScale/muxer->def_v->h.dwRate;
// muxer->avih.dwMaxBytesPerSec=1000000; // dummy!!!!! FIXME
// muxer->avih.dwPaddingGranularity=2; // ???
muxer->avih.dwFlags|=AVIF_ISINTERLEAVED|AVIF_TRUSTCKTYPE;
muxer->avih.dwTotalFrames=muxer->def_v->h.dwLength;
// muxer->avih.dwSuggestedBufferSize=muxer->def_v->h.dwSuggestedBufferSize;
muxer->avih.dwWidth=muxer->def_v->bih->biWidth;
muxer->avih.dwHeight=muxer->def_v->bih->biHeight;
}
// AVI header:
hdrsize=sizeof(muxer->avih)+8;
// calc total header size:
for(i=0;i<muxer->avih.dwStreams;i++){
hdrsize+=12; // LIST
hdrsize+=sizeof(muxer->streams[i]->h)+8; // strh
switch(muxer->streams[i]->type){
case AVIWRITE_TYPE_VIDEO:
hdrsize+=muxer->streams[i]->bih->biSize+8; // strf
break;
case AVIWRITE_TYPE_AUDIO:
hdrsize+=WFSIZE(muxer->streams[i]->wf)+8; // strf
break;
}
}
write_avi_list(f,listtypeAVIHEADER,hdrsize);
write_avi_chunk(f,ckidAVIMAINHDR,sizeof(muxer->avih),&muxer->avih);
// stream headers:
for(i=0;i<muxer->avih.dwStreams;i++){
hdrsize=sizeof(muxer->streams[i]->h)+8; // strh
switch(muxer->streams[i]->type){
case AVIWRITE_TYPE_VIDEO:
hdrsize+=muxer->streams[i]->bih->biSize+8; // strf
break;
case AVIWRITE_TYPE_AUDIO:
hdrsize+=WFSIZE(muxer->streams[i]->wf)+8; // strf
break;
}
write_avi_list(f,listtypeSTREAMHEADER,hdrsize);
write_avi_chunk(f,ckidSTREAMHEADER,sizeof(muxer->streams[i]->h),&muxer->streams[i]->h); // strh
switch(muxer->streams[i]->type){
case AVIWRITE_TYPE_VIDEO:
write_avi_chunk(f,ckidSTREAMFORMAT,muxer->streams[i]->bih->biSize,muxer->streams[i]->bih);
break;
case AVIWRITE_TYPE_AUDIO:
write_avi_chunk(f,ckidSTREAMFORMAT,WFSIZE(muxer->streams[i]->wf),muxer->streams[i]->wf);
break;
}
}
// JUNK:
write_avi_chunk(f,ckidAVIPADDING,2048-(ftell(f)&2047)-8,NULL);
// 'movi' header:
write_avi_list(f,listtypeAVIMOVIE,muxer->movi_end-ftell(f)-12);
muxer->movi_start=ftell(f);
}
void aviwrite_write_index(aviwrite_t *muxer,FILE *f){
muxer->movi_end=ftell(f);
if(muxer->idx && muxer->idx_pos>0){
// fixup index entries:
// int i;
// for(i=0;i<muxer->idx_pos;i++) muxer->idx[i].dwChunkOffset-=muxer->movi_start-4;
// write index chunk:
write_avi_chunk(f,ckidAVINEWINDEX,16*muxer->idx_pos,muxer->idx);
muxer->avih.dwFlags|=AVIF_HASINDEX;
}
muxer->file_end=ftell(f);
}
|