-
Notifications
You must be signed in to change notification settings - Fork 25
/
avi.c
487 lines (412 loc) · 16.2 KB
/
avi.c
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
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
/*
** Oricutron
** Copyright (C) 2009-2014 Peter Gordon
**
** This program is free software; you can redistribute it and/or
** modify it under the terms of the GNU General Public License
** as published by the Free Software Foundation, version 2
** of the License.
**
** This program is distributed in the hope that it will be useful,
** but WITHOUT ANY WARRANTY; without even the implied warranty of
** MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
** GNU General Public License for more details.
**
** You should have received a copy of the GNU General Public License
** along with this program; if not, write to the Free Software
** Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
**
** AVI capturing
**
*/
#include <stdio.h>
#include <stdlib.h>
#include <math.h>
#include <string.h>
#include "system.h"
#include "avi.h"
extern SDL_AudioSpec obtained;
// Write a 32bit value to a stream in little-endian format
static SDL_bool write32l( SDL_bool stillok, struct avi_handle *ah, Uint32 val, Uint32 *rem )
{
if( !stillok ) return SDL_FALSE; // Something failed earlier, so we're aborting
if( rem ) (*rem) = ah->csize;
ah->csize += 4;
_MAKE_LE32(val);
return (fwrite( &val, 4, 1, ah->f ) == 1);
}
// Write a 16bit value to a stream in little-endian format
static SDL_bool write16l( SDL_bool stillok, struct avi_handle *ah, Uint16 val, Uint32 *rem )
{
if( !stillok ) return SDL_FALSE; // Something failed earlier, so we're aborting
if( rem ) (*rem) = ah->csize;
ah->csize += 2;
_MAKE_LE16(val);
return (fwrite( &val, 2, 1, ah->f ) == 1);
}
// Write a string to a stream
static SDL_bool writestr( SDL_bool stillok, struct avi_handle *ah, char *str, Uint32 *rem )
{
Sint32 i = (int)strlen( str );
if( !stillok ) return SDL_FALSE; // Something failed earlier, so we're aborting
if( rem ) (*rem) = ah->csize;
ah->csize += i;
return (fwrite( str, i, 1, ah->f ) == 1);
}
// Write a 32bit value to a stream in little-endian format at a specific offset
static SDL_bool seek_write32l( SDL_bool stillok, struct avi_handle *ah, Uint32 offs, Uint32 val )
{
if( !stillok ) return SDL_FALSE; // Something failed earlier, so we're aborting
if( offs == 0 ) return SDL_TRUE; // Don't overwrite RIFF!
fseek( ah->f, offs, SEEK_SET );
_MAKE_LE32(val);
return (fwrite( &val, 4, 1, ah->f ) == 1);
}
// Write a block of memory
static SDL_bool writeblk( SDL_bool stillok, struct avi_handle *ah, Uint8 *blk, Uint32 blklen )
{
if( !stillok ) return SDL_FALSE;
ah->csize += blklen;
return (fwrite( blk, blklen, 1, ah->f ) == 1);
}
// Write an 8bit value to a stream
static SDL_bool writebyt( SDL_bool stillok, struct avi_handle *ah, Uint8 val )
{
if( !stillok ) return SDL_FALSE; // Something failed earlier, so we're aborting
ah->csize += 1;
return (fwrite( &val, 1, 1, ah->f ) == 1);
}
#define AVIFLAGS (AVIF_ISINTERLEAVED|AVIF_WASCAPTUREFILE)
struct avi_handle *avi_open( char *filename, Uint8 *pal, SDL_bool dosound, int is50hz )
{
struct avi_handle *ah;
SDL_bool ok;
Sint32 i;
ah = malloc( sizeof( struct avi_handle ) );
if( !ah ) return NULL;
memset( ah, 0, sizeof( struct avi_handle ) );
ah->f = fopen( filename, "wb" );
if( !ah->f )
{
free( ah );
return NULL;
}
ah->dosnd = dosound;
ah->csize = 0;
ah->is50hz = is50hz;
ok = SDL_TRUE;
ok &= writestr( ok, ah, "RIFF" , NULL ); // RIFF header
ok &= write32l( ok, ah, 0, &ah->offs_riffsize ); // RIFF size
ok &= writestr( ok, ah, "AVI " , NULL ); // RIFF type
ok &= writestr( ok, ah, "LIST" , NULL ); // Header list
ok &= write32l( ok, ah, 0, &ah->offs_hdrlsize ); // Header list size
ok &= writestr( ok, ah, "hdrl" , NULL ); // Header list identifier
ok &= writestr( ok, ah, "avih" , NULL ); // MainAVIHeader chunk
ok &= write32l( ok, ah, 56, NULL ); // Chunk size
ok &= write32l( ok, ah,is50hz?20000:16667,&ah->offs_usperfrm); // Microseconds per frame
ok &= write32l( ok, ah, 0, NULL ); // Max bytes per second
ok &= write32l( ok, ah, 0, NULL ); // Padding granularity
ok &= write32l( ok, ah, AVIFLAGS, NULL ); // Flags
ok &= write32l( ok, ah, 0, &ah->offs_frames ); // Number of frames
ok &= write32l( ok, ah, 0, NULL ); // Initial frames
ok &= write32l( ok, ah, ah->dosnd?2:1, NULL ); // Stream count
ok &= write32l( ok, ah, 0, NULL ); // Suggested buffer size
ok &= write32l( ok, ah, 240, NULL ); // Width
ok &= write32l( ok, ah, 224, NULL ); // Height
ok &= write32l( ok, ah, 0, NULL ); // Reserved
ok &= write32l( ok, ah, 0, NULL ); // Reserved
ok &= write32l( ok, ah, 0, NULL ); // Reserved
ok &= write32l( ok, ah, 0, NULL ); // Reserved
ok &= writestr( ok, ah, "LIST" , NULL ); // Stream header list
ok &= write32l( ok, ah,4+8+56+8+40+8*4, NULL ); // Stream header list size
ok &= writestr( ok, ah, "strl" , NULL ); // Stream header list identifier
ok &= writestr( ok, ah, "strh" , NULL ); // Video stream header
ok &= write32l( ok, ah, 56, NULL ); // Chunk size
ok &= writestr( ok, ah, "vids" , NULL ); // Type
ok &= writestr( ok, ah, "MRLE" , NULL ); // Microsoft Run Length Encoding
ok &= write32l( ok, ah, 0, NULL ); // Flags
ok &= write32l( ok, ah, 0, NULL ); // Reserved
ok &= write32l( ok, ah, 0, NULL ); // Initial frames
ok &= write32l( ok, ah, 1000000, NULL ); // Scale
ok &= write32l( ok, ah,is50hz?50000000:60000000,&ah->offs_frmrate); // Rate
ok &= write32l( ok, ah, 0, NULL ); // Start
ok &= write32l( ok, ah, 0, &ah->offs_frames2 ); // Length
ok &= write32l( ok, ah, 0, NULL ); // Suggested buffer size
ok &= write32l( ok, ah, 10000, NULL ); // Quality
ok &= write32l( ok, ah, 0, NULL ); // Sample size
ok &= write32l( ok, ah, 0, NULL ); // Frame
ok &= write32l( ok, ah, 0, NULL ); // Frame
ok &= writestr( ok, ah, "strf" , NULL ); // Video stream format
ok &= write32l( ok, ah, 40+8*4, NULL ); // Chunk size
ok &= write32l( ok, ah, 40, NULL ); // Structure size
ok &= write32l( ok, ah, 240, NULL ); // Width
ok &= write32l( ok, ah, 224, NULL ); // Height
ok &= write16l( ok, ah, 1, NULL ); // Planes
ok &= write16l( ok, ah, 8, NULL ); // Bit count
ok &= write32l( ok, ah, 1, NULL ); // Compression
ok &= write32l( ok, ah, 240*244, NULL ); // Image size
ok &= write32l( ok, ah, 0, NULL ); // XPelsPerMeter
ok &= write32l( ok, ah, 0, NULL ); // YPelsPerMeter
ok &= write32l( ok, ah, 8, NULL ); // Colours used
ok &= write32l( ok, ah, 8, NULL ); // Colours important
for( i=0; i<8; i++ )
{
ok &= writebyt( ok, ah, pal[i*3+2] );
ok &= writebyt( ok, ah, pal[i*3+1] );
ok &= writebyt( ok, ah, pal[i*3+0] );
ok &= writebyt( ok, ah, 0 );
}
if( ah->dosnd )
{
ok &= writestr( ok, ah, "LIST" , NULL ); // Stream header list
ok &= write32l( ok, ah, 4+8+56+8+16, NULL ); // Stream header list size
ok &= writestr( ok, ah, "strl" , NULL ); // Stream header identifier
ok &= writestr( ok, ah, "strh" , NULL ); // Audio stream header
ok &= write32l( ok, ah, 56, NULL ); // Chunk size
ok &= writestr( ok, ah, "auds" , NULL ); // Type
ok &= write32l( ok, ah, 0, NULL ); // Codec
ok &= write32l( ok, ah, 0, NULL ); // Flags
ok &= write32l( ok, ah, 0, NULL ); // Reserved
ok &= write32l( ok, ah, 0, NULL ); // Initial frames
ok &= write32l( ok, ah, 1, NULL ); // Scale
ok &= write32l( ok, ah,obtained.freq, NULL ); // Rate
ok &= write32l( ok, ah, 0, NULL ); // Start
ok &= write32l( ok, ah, 0, &ah->offs_audiolen ); // Length
ok &= write32l( ok, ah, 0, NULL ); // Suggested buffer size
ok &= write32l( ok, ah, 10000, NULL ); // Quality
ok &= write32l( ok, ah, 2, NULL ); // Sample size
ok &= write32l( ok, ah, 0, NULL ); // Frame
ok &= write32l( ok, ah, 0, NULL ); // Frame
ok &= writestr( ok, ah, "strf" , NULL ); // Audio stream format
ok &= write32l( ok, ah, 16, NULL ); // Chunk size
ok &= write16l( ok, ah, 1, NULL ); // Format (PCM)
ok &= write16l( ok, ah, 1, NULL ); // Channels
ok &= write32l( ok, ah,obtained.freq, NULL ); // Samples per second
ok &= write32l( ok, ah,obtained.freq*2, NULL ); // Bytes per second
ok &= write16l( ok, ah, 2, NULL ); // BlockAlign
ok &= write16l( ok, ah, 16, NULL ); // BitsPerSample
}
ah->hdrlsize = ah->csize - ah->offs_hdrlsize - 4;
ok &= writestr( ok, ah, "LIST" , NULL ); // movi list
ok &= write32l( ok, ah, 0, &ah->offs_movisize ); // Size
ok &= writestr( ok, ah, "movi" , NULL ); // identifier
if( !ok )
{
free( ah );
return NULL;
}
ah->frames = 0;
ah->frameadjust = 0;
ah->audiolen = 0;
ah->movisize = 0;
ah->lastframevalid = SDL_FALSE;
ah->time_start = 0;
return ah;
}
static Uint32 rle_putpixels( struct avi_handle *ah, Uint8 *srcdata, Uint32 rlec, Uint32 srcc, Uint32 length )
{
Uint32 c, chunker;
if( length == 0 ) return rlec;
while( length > 254 )
{
// Try not to leave an annoying dangly 1,2 or 3 byte section
chunker = (length<258) ? 250 : 254;
rlec = rle_putpixels( ah, srcdata, rlec, srcc, chunker );
srcc += chunker;
length -= chunker;
}
// Somehow "chunker" failed :-(
if( length < 3 )
{
while( length > 0 )
{
ah->rledata[rlec++] = 0x01;
ah->rledata[rlec++] = srcdata[srcc++];
length--;
}
return rlec;
}
c = length;
ah->rledata[rlec++] = 0x00;
ah->rledata[rlec++] = length;
while( c > 0 )
{
ah->rledata[rlec++] = srcdata[srcc++];
c--;
}
if( length & 1 )
ah->rledata[rlec++] = 0;
return rlec;
}
// RLE encode the frame
#define NOABS 0xffffffff
static Uint32 rle_encode( struct avi_handle *ah, Uint8 *srcdata, Uint32 rlec, Uint32 eol )
{
Uint32 srcc, runc, abspos, chunker;
Uint8 thiscol;
if( eol == 0 )
{
ah->rledata[rlec++] = 0x00;
ah->rledata[rlec++] = 0x00;
return rlec;
}
srcc = 0; // Source count
abspos = NOABS;
while( srcc < eol )
{
// Look for a run
thiscol = srcdata[srcc];
// At the last pixel?
if( srcc == eol-1 )
{
// Need to dump an absolute section?
if( abspos != NOABS ) rlec = rle_putpixels( ah, srcdata, rlec, abspos, srcc-abspos );
// Just encode it
ah->rledata[rlec++] = 0x01;
ah->rledata[rlec++] = thiscol;
ah->rledata[rlec++] = 0x00; // The end!
ah->rledata[rlec++] = 0x00;
return rlec;
}
runc = 1; // Set the run count to 1
while( srcdata[srcc+runc] == thiscol )
{
runc++;
if( (srcc+runc) >= eol ) break;
}
// Need an absolute mode section?
if( runc < 2 )
{
// Starting a new absolute mode section?
if( abspos == NOABS ) abspos = srcc;
// Next pixel!
srcc++;
continue;
}
// Need to dump an absolute section?
if( abspos != NOABS )
{
rlec = rle_putpixels( ah, srcdata, rlec, abspos, srcc-abspos );
abspos = NOABS;
}
// Encode that run!
srcc += runc; // Skip past the run
while( runc > 255 ) // Do any 255 byte chunks until runc < 255
{
chunker = (runc<258) ? 250 : 255;
ah->rledata[rlec++] = chunker;
ah->rledata[rlec++] = thiscol;
runc -= chunker;
}
ah->rledata[rlec++] = runc; // Encode the bastard!
ah->rledata[rlec++] = thiscol;
}
if( abspos != NOABS ) rlec = rle_putpixels( ah, srcdata, rlec, abspos, srcc-abspos );
ah->rledata[rlec++] = 0x00; // The end!
ah->rledata[rlec++] = 0x00;
return rlec;
}
SDL_bool avi_addframe( struct avi_handle **ah, Uint8 *srcdata )
{
SDL_bool ok;
Sint32 i, x, lastline, lastx;
Uint32 size;
lastline = 0;
if( (*ah)->lastframevalid )
{
// Find the last line that differs
for( i=0; i<224; i++ )
{
for( x=0; x<240; x++ )
{
if( (*ah)->lastframe[i*240+x] != srcdata[i*240+x] )
break;
}
if( x<240 ) break;
}
lastline = i;
}
if( lastline == 224 )
{
size = 0;
(*ah)->rledata[size++] = 0x00;
(*ah)->rledata[size++] = 0x01;
} else {
for( i=223,size=0; i>=lastline; i-- )
{
lastx = 240;
if( (*ah)->lastframevalid )
{
// Find the last pixel we need to encode
for( x=239; x>=0; x-- )
{
if( (*ah)->lastframe[i*240+x] != srcdata[i*240+x] )
break;
}
lastx = x+1;
}
size = rle_encode( *ah, &srcdata[i*240], size, lastx );
}
(*ah)->rledata[size-1] = 0x01;
}
memcpy( (*ah)->lastframe, srcdata, 240*224 );
(*ah)->lastframevalid = SDL_TRUE;
ok = SDL_TRUE;
ok &= writestr( ok, *ah, "00dc", NULL ); // Chunk header
ok &= write32l( ok, *ah, size, NULL ); // Chunk size
ok &= writeblk( ok, *ah, (*ah)->rledata, size ); // Chunk data
(*ah)->movisize += size + 8;
(*ah)->frames++;
if( !ok )
{
avi_close( ah );
return SDL_FALSE;
}
if( !(*ah)->time_start )
{
(*ah)->time_start = SDL_GetTicks();
}
return SDL_TRUE;
}
SDL_bool avi_addaudio( struct avi_handle **ah, Sint16 *audiodata, Uint32 audiosize )
{
SDL_bool ok;
if( !(*ah)->dosnd ) return SDL_TRUE;
ok = SDL_TRUE;
ok &= writestr( ok, *ah, "01wb", NULL ); // Chunk header
ok &= write32l( ok, *ah, audiosize, NULL ); // Chunk size
ok &= writeblk( ok, *ah, (Uint8 *)audiodata, audiosize ); // Chunk data
(*ah)->movisize += audiosize+8;
(*ah)->audiolen += audiosize;
if( !ok )
{
avi_close( ah );
return SDL_FALSE;
}
return SDL_TRUE;
}
void avi_close( struct avi_handle **ah )
{
SDL_bool ok;
Uint32 time_end = SDL_GetTicks();
double time_ms, rate, usperfrm;
if( ( !ah ) || (!(*ah)) ) return;
if( (*ah)->f )
{
ok = SDL_TRUE;
ok &= seek_write32l( ok, *ah, (*ah)->offs_riffsize, (*ah)->csize-8 );
ok &= seek_write32l( ok, *ah, (*ah)->offs_hdrlsize, (*ah)->hdrlsize );
ok &= seek_write32l( ok, *ah, (*ah)->offs_frames, (*ah)->frames );
ok &= seek_write32l( ok, *ah, (*ah)->offs_frames2, (*ah)->frames );
ok &= seek_write32l( ok, *ah, (*ah)->offs_movisize, (*ah)->movisize );
ok &= seek_write32l( ok, *ah, (*ah)->offs_audiolen, (*ah)->audiolen );
time_ms = (double)(time_end - (*ah)->time_start); // Time in milliseconds
rate = ((double)((*ah)->frames) * 1000000000.0f) / time_ms;
usperfrm = (time_ms*1000.0f) / ((double)(*ah)->frames);
ok &= seek_write32l( ok, *ah, (*ah)->offs_frmrate, (Uint32)(rate + .5) );
ok &= seek_write32l( ok, *ah, (*ah)->offs_usperfrm, (Uint32)(usperfrm + .5) );
fclose( (*ah)->f );
}
free( (*ah) );
*ah = NULL;
}