-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathavi.cpp
More file actions
315 lines (261 loc) · 7.82 KB
/
avi.cpp
File metadata and controls
315 lines (261 loc) · 7.82 KB
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
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
//
// Created by Pkuism on 2022/2/9.
//
#include <fstream>
#include <vector>
#include "avi.h"
typedef unsigned char byte;
// AVI atoms
#define AVIIF_KEYFRAME 0x00000010
// Tried to output directly through outFile.put ms ,file_ptr Will instead store header in file and then
// read and output it
void fwrite_DWORD(std::ofstream& outFile, DWORD word)
{
unsigned char *p;
p = (unsigned char *)&word;
int i;
for (i = 0; i < 4; i++) {
outFile.put(p[i]);
}
}
void fwrite_WORD(std::ofstream& outFile, WORD word)
{
unsigned char *p;
p = (unsigned char *)&word;
int i;
for (i = 0; i < 2; i++) {
outFile.put(p[i]);
}
}
void writeFrames(std::ofstream& outFile, std::vector<byte>& buff)
{
CHUNK data;
data.dwFourCC = '00db';
outFile.put('0');
outFile.put('0');
outFile.put('d');
outFile.put('b');
data.dwSize = buff.size();
fwrite_DWORD(outFile, data.dwSize);
for (auto i : buff)
outFile.put(i);
if (buff.size() % 2){
outFile.put('\0');
buff.push_back('\0');
}
}
void writeEnd(std::ofstream& outFile, std::vector<int>& frameSizes){
outFile.put('i');
outFile.put('d');
outFile.put('x');
outFile.put('1');
unsigned long index_length = 4 * 4 * frameSizes.size();
fwrite_DWORD(outFile, index_length);
unsigned long AVI_KEYFRAME = 16;
unsigned long offset_count = 4;
for (auto i : frameSizes){
outFile.put('0');
outFile.put('0');
outFile.put('d');
outFile.put('b');
fwrite_DWORD(outFile, AVI_KEYFRAME);
fwrite_DWORD(outFile, offset_count);
fwrite_DWORD(outFile, i);
offset_count += i + 8;
}
}
void writeAVI(std::ofstream& outFile, int jpgs_width, int jpgs_height, unsigned long fps, int nbr_of_jpgs, unsigned int len)
{
RIFF RIFF_LIST;
RIFF_LIST.dwRIFF = 'RIFF';
outFile.put('R');
outFile.put('I');
outFile.put('F');
outFile.put('F');
RIFF_LIST.dwSize = 150 + 12 + len + 8 * nbr_of_jpgs + 8 + 4 * 4 * nbr_of_jpgs;
fwrite_DWORD(outFile, RIFF_LIST.dwSize);
RIFF_LIST.dwFourCC = 'AVI ';
outFile.put('A');
outFile.put('V');
outFile.put('I');
outFile.put(' ');
// RIFF_LIST.data = WAIT WITH THIS
LIST hdrl;
hdrl.dwList = 'LIST';
outFile.put('L');
outFile.put('I');
outFile.put('S');
outFile.put('T');
hdrl.dwSize = 208;
fwrite_DWORD(outFile, hdrl.dwSize);
hdrl.dwFourCC = 'hdrl';
outFile.put('h');
outFile.put('d');
outFile.put('r');
outFile.put('l');
MainAVIHeader avih;
avih.dwFourCC = 'avih';
outFile.put('a');
outFile.put('v');
outFile.put('i');
outFile.put('h');
avih.dwSize = 56;
fwrite_DWORD(outFile, avih.dwSize);
avih.dwMicroSecPerFrame = 1000000 / fps;
fwrite_DWORD(outFile, avih.dwMicroSecPerFrame);
avih.dwMaxBytesPerSec = 7000;
fwrite_DWORD(outFile, avih.dwMaxBytesPerSec);
avih.dwPaddingGranularity = 0;
fwrite_DWORD(outFile, avih.dwPaddingGranularity);
// dwFlags set to 16, do not know why!
avih.dwFlags = 16;
fwrite_DWORD(outFile, avih.dwFlags);
avih.dwTotalFrames = nbr_of_jpgs;
fwrite_DWORD(outFile, avih.dwTotalFrames);
avih.dwInitialFrames = 0;
fwrite_DWORD(outFile, avih.dwInitialFrames);
avih.dwStreams = 1;
fwrite_DWORD(outFile, avih.dwStreams);
avih.dwSuggestedBufferSize = 0;
fwrite_DWORD(outFile, avih.dwSuggestedBufferSize);
avih.dwWidth = jpgs_width;
fwrite_DWORD(outFile, avih.dwWidth);
avih.dwHeight = jpgs_height;
fwrite_DWORD(outFile, avih.dwHeight);
avih.dwReserved[0] = 0;
fwrite_DWORD(outFile, avih.dwReserved[0]);
avih.dwReserved[1] = 0;
fwrite_DWORD(outFile, avih.dwReserved[1]);
avih.dwReserved[2] = 0;
fwrite_DWORD(outFile, avih.dwReserved[2]);
avih.dwReserved[3] = 0;
fwrite_DWORD(outFile, avih.dwReserved[3]);
LIST strl;
strl.dwList = 'LIST';
outFile.put('L');
outFile.put('I');
outFile.put('S');
outFile.put('T');
strl.dwSize = 132;
fwrite_DWORD(outFile, strl.dwSize);
strl.dwFourCC = 'strl';
outFile.put('s');
outFile.put('t');
outFile.put('r');
outFile.put('l');
AVIStreamHeader strh;
strh.dwFourCC = 'strh';
outFile.put('s');
outFile.put('t');
outFile.put('r');
outFile.put('h');
strh.dwSize = 48;
fwrite_DWORD(outFile, strh.dwSize);
strh.fccType = 'vids';
outFile.put('v');
outFile.put('i');
outFile.put('d');
outFile.put('s');
strh.fccHandler = 'MJPG';
outFile.put('M');
outFile.put('J');
outFile.put('P');
outFile.put('G');
strh.dwFlags = 0;
fwrite_DWORD(outFile, strh.dwFlags);
strh.wPriority = 0; // +2 = 14
fwrite_WORD(outFile, strh.wPriority);
strh.wLanguage = 0; // +2 = 16
fwrite_WORD(outFile, strh.wLanguage);
strh.dwInitialFrames = 0; // +4 = 20
fwrite_DWORD(outFile, strh.dwInitialFrames);
strh.dwScale = 1; // +4 = 24
fwrite_DWORD(outFile, strh.dwScale);
// insert FPS
strh.dwRate = fps; // +4 = 28
fwrite_DWORD(outFile, strh.dwRate);
strh.dwStart = 0; // +4 = 32
fwrite_DWORD(outFile, strh.dwStart);
// insert nbr of jpegs
strh.dwLength = nbr_of_jpgs; // +4 = 36
fwrite_DWORD(outFile, strh.dwLength);
strh.dwSuggestedBufferSize = 0; // +4 = 40
fwrite_DWORD(outFile, strh.dwSuggestedBufferSize);
strh.dwQuality = 0; // +4 = 44
fwrite_DWORD(outFile, strh.dwQuality);
// Specifies the size of a single sample of data.
// This is set to zero if the samples can vary in size.
// If this number is nonzero, then multiple samples of data
// can be grouped into a single chunk within the file.
// If it is zero, each sample of data (such as a video frame) must be in a separate chunk.
// For video streams, this number is typically zero, although
// it can be nonzero if all video frames are the same size.
//
strh.dwSampleSize = 0; // +4 = 48
fwrite_DWORD(outFile, strh.dwSampleSize);
EXBMINFOHEADER strf;
strf.dwFourCC = 'strf';
outFile.put('s');
outFile.put('t');
outFile.put('r');
outFile.put('f');
strf.dwSize = 40;
fwrite_DWORD(outFile, strf.dwSize);
strf.biSize = 40;
fwrite_DWORD(outFile, strf.biSize);
strf.biWidth = jpgs_width;
fwrite_DWORD(outFile, strf.biWidth);
strf.biHeight = jpgs_height;
fwrite_DWORD(outFile, strf.biHeight);
strf.biPlanes = 1;
fwrite_WORD(outFile, strf.biPlanes);
strf.biBitCount = 24;
fwrite_WORD(outFile, strf.biBitCount);
strf.biCompression = 'MJPG';
outFile.put('M');
outFile.put('J');
outFile.put('P');
outFile.put('G');
strf.biSizeImage = ((strf.biWidth * strf.biBitCount / 8 + 3) & 0xFFFFFFFC) * strf.biHeight;
fwrite_DWORD(outFile, strf.biSizeImage);
strf.biXPelsPerMeter = 0;
fwrite_DWORD(outFile, strf.biXPelsPerMeter);
strf.biYPelsPerMeter = 0;
fwrite_DWORD(outFile, strf.biYPelsPerMeter);
strf.biClrUsed = 0;
fwrite_DWORD(outFile, strf.biClrUsed);
strf.biClrImportant = 0;
fwrite_DWORD(outFile, strf.biClrImportant);
outFile.put('L');
outFile.put('I');
outFile.put('S');
outFile.put('T');
DWORD ddww = 16;
fwrite_DWORD(outFile, ddww);
outFile.put('o');
outFile.put('d');
outFile.put('m');
outFile.put('l');
outFile.put('d');
outFile.put('m');
outFile.put('l');
outFile.put('h');
DWORD szs = 4;
fwrite_DWORD(outFile, szs);
// nbr of jpgs
DWORD totalframes = nbr_of_jpgs;
fwrite_DWORD(outFile, totalframes);
LIST movi;
movi.dwList = 'LIST';
outFile.put('L');
outFile.put('I');
outFile.put('S');
outFile.put('T');
movi.dwSize = len + 4 + 8 * nbr_of_jpgs;
fwrite_DWORD(outFile, movi.dwSize);
movi.dwFourCC = 'movi';
outFile.put('m');
outFile.put('o');
outFile.put('v');
outFile.put('i');
}