-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathAviWriter.cpp
229 lines (191 loc) · 5.8 KB
/
AviWriter.cpp
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
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
//
// AVI File Writer
//
#include "aviwriter.h"
#include "macro.h"
#pragma comment(lib, "vfw32.lib")
CAVIWriter::CAVIWriter() :
m_pAviFile(NULL),
m_pVideoStream(NULL),
m_pAudioStream(NULL),
m_pVideoStreamCompress(NULL),
m_pVideoFormat(NULL),
m_FrameNumber(0),
m_SampleNumber(0)
{
::AVIFileInit();
ZEROMEMORY( &m_VideoStreamInfo, sizeof(m_VideoStreamInfo) );
m_VideoStreamInfo.fccType = streamtypeVIDEO;
m_VideoStreamInfo.fccHandler = comptypeDIB;
m_VideoStreamInfo.dwQuality = (DWORD)-1;
// default formats
SetAudioFormat( 44100, 16, 1 );
SetFrameRate( 100, 60*100 );
ZEROMEMORY( &m_VideoOption, sizeof(m_VideoOption) );
m_VideoOption.fccType = streamtypeVIDEO;
m_VideoOption.fccHandler = comptypeDIB;
ZEROMEMORY( &m_Compvers, sizeof(m_Compvers) );
m_Compvers.cbSize = sizeof(COMPVARS);
m_Compvers.dwFlags = ICMF_COMPVARS_VALID;
m_Compvers.fccHandler = comptypeDIB;
m_Compvers.lQ = ICQUALITY_DEFAULT;
m_Compvers.lKey = 15;
}
CAVIWriter::~CAVIWriter()
{
Close();
::AVIFileExit();
}
bool CAVIWriter::Open( const char* fname )
{
if( !m_pVideoFormat )
return false;
::DeleteFile( fname );
if( ::AVIFileOpen( &m_pAviFile, fname, OF_WRITE | OF_CREATE, NULL ) ) {
return false;
}
// Video Stream
if( ::AVIFileCreateStream( m_pAviFile, &m_pVideoStream, &m_VideoStreamInfo ) ) {
Close();
return false;
}
if( ::AVIMakeCompressedStream( &m_pVideoStreamCompress, m_pVideoStream, &m_VideoOption, NULL ) != AVIERR_OK ) {
Close();
return false;
}
if( ::AVIStreamSetFormat( m_pVideoStreamCompress, 0, m_pVideoFormat, m_pVideoFormat->biSize + m_pVideoFormat->biClrUsed * sizeof(RGBQUAD) ) ) {
Close();
return false;
}
// Audio Stream
if( ::AVIFileCreateStream( m_pAviFile, &m_pAudioStream, &m_AudioStreamInfo ) ) {
Close();
return false;
}
if( ::AVIStreamSetFormat( m_pAudioStream, 0, &m_AudioFormat, sizeof(WAVEFORMATEX) ) ) {
Close();
return false;
}
m_FrameNumber = 0;
m_SampleNumber = 0;
return true;
}
void CAVIWriter::Close()
{
if( m_pAudioStream ) {
::AVIStreamClose( m_pAudioStream );
m_pAudioStream = NULL;
}
if( m_pVideoStreamCompress ) {
::AVIStreamClose( m_pVideoStreamCompress );
m_pVideoStreamCompress = NULL;
}
if( m_pVideoStream ) {
::AVIStreamClose( m_pVideoStream );
m_pVideoStream = NULL;
}
if( m_pAviFile ) {
::AVIFileClose( m_pAviFile );
m_pAviFile = NULL;
}
}
bool CAVIWriter::SelectVideoFormat( HWND hWnd )
{
m_Compvers.dwFlags = ICMF_COMPVARS_VALID;
m_Compvers.lpState = NULL;
m_Compvers.cbState = 0;
if( !::ICCompressorChoose( hWnd, ICMF_CHOOSE_DATARATE | ICMF_CHOOSE_KEYFRAME,
m_pVideoFormat, NULL, &m_Compvers, NULL) )
return false;
m_VideoStreamInfo.fccHandler = m_Compvers.fccHandler;
ZEROMEMORY( &m_VideoOption, sizeof(m_VideoOption) );
m_VideoOption.fccType = streamtypeVIDEO;
m_VideoOption.fccHandler = m_Compvers.fccHandler;
m_VideoOption.dwKeyFrameEvery = m_Compvers.lKey;
m_VideoOption.dwQuality = m_Compvers.lQ;
m_VideoOption.dwBytesPerSecond = m_Compvers.lDataRate;
m_VideoOption.dwFlags = ((m_Compvers.lDataRate > 0) ? AVICOMPRESSF_DATARATE : 0)
|((m_Compvers.lKey > 0 ) ? AVICOMPRESSF_KEYFRAMES : 0);
::ICCompressorFree( &m_Compvers );
return true;
}
void CAVIWriter::SetVideoFormat( BITMAPINFOHEADER* bih )
{
m_pVideoFormat = bih;
}
void CAVIWriter::SetAudioFormat( const DWORD rate, const WORD bits, const INT channels )
{
m_AudioFormat.wFormatTag = WAVE_FORMAT_PCM;
m_AudioFormat.nSamplesPerSec = rate;
m_AudioFormat.wBitsPerSample = bits;
m_AudioFormat.nChannels = channels;
m_AudioFormat.nBlockAlign = m_AudioFormat.nChannels * m_AudioFormat.wBitsPerSample / 8;
m_AudioFormat.nAvgBytesPerSec = m_AudioFormat.nSamplesPerSec * m_AudioFormat.nBlockAlign;
m_AudioFormat.cbSize = 0;
ZEROMEMORY( &m_AudioStreamInfo, sizeof(m_AudioStreamInfo) );
m_AudioStreamInfo.fccType = streamtypeAUDIO;
// m_AudioStreamInfo.fccHandler = comptypeDIB;
m_AudioStreamInfo.fccHandler = m_AudioFormat.wFormatTag;
m_AudioStreamInfo.dwScale = m_AudioFormat.nBlockAlign;
m_AudioStreamInfo.dwInitialFrames = 1;
m_AudioStreamInfo.dwRate = m_AudioFormat.nAvgBytesPerSec;
m_AudioStreamInfo.dwQuality = (DWORD)-1;
m_AudioStreamInfo.dwSampleSize = m_AudioFormat.nBlockAlign;
}
void CAVIWriter::SetFrameRate( const DWORD scale, const DWORD rate )
{
m_FrameRate = rate;
// ZEROMEMORY( &m_VideoStreamInfo, sizeof(m_VideoStreamInfo) );
// m_VideoStreamInfo.fccType = streamtypeVIDEO;
// m_VideoStreamInfo.fccHandler = comptypeDIB;
m_VideoStreamInfo.dwScale = scale;
m_VideoStreamInfo.dwRate = rate;
// m_VideoStreamInfo.dwQuality = (DWORD)-1;
}
bool CAVIWriter::WriteVideo( const char* video )
{
if( !m_pAviFile )
return false;
#if 0
DWORD dwFlags = 0;
if( m_VideoOption.dwFlags & AVICOMPRESSF_KEYFRAMES ) {
if( (m_FrameNumber % m_VideoOption.dwKeyFrameEvery) == 0 ) {
dwFlags = AVIIF_KEYFRAME;
}
}
if( ::AVIStreamWrite( m_pVideoStreamCompress, m_FrameNumber, 1, (void*)video,
m_pVideoFormat->biSizeImage, dwFlags, NULL, NULL ) ) {
Close();
return false;
}
#else
if( ::AVIStreamWrite( m_pVideoStreamCompress, m_FrameNumber, 1, (void*)video,
m_pVideoFormat->biSizeImage, AVIIF_KEYFRAME, NULL, NULL ) ) {
Close();
return false;
}
#endif
m_FrameNumber++;
return true;
}
bool CAVIWriter::WriteAudio( const char* audio, const int length )
{
if( !m_pAviFile )
return false;
int samples = length / m_AudioFormat.nBlockAlign;
if( ::AVIStreamWrite( m_pAudioStream, m_SampleNumber, samples, (void*)audio,
length, 0, NULL, NULL ) ) {
Close();
return false;
}
m_SampleNumber += samples;
return true;
}
bool CAVIWriter::WriteFrame( const char* video, const char* audio, const int length )
{
if( !WriteVideo( video ) )
return false;
if( !WriteAudio( audio, length ) )
return false;
return true;
}