Debian Codegraph @

code/client/cl_avi.c

raw symbols SCIP
1 /*
2 ===========================================================================
3 Copyright (C) 2005-2006 Tim Angus
4
5 This file is part of Quake III Arena source code.
6
7 Quake III Arena source code is free software; you can redistribute it
8 and/or modify it under the terms of the GNU General Public License as
9 published by the Free Software Foundation; either version 2 of the License,
10 or (at your option) any later version.
11
12 Quake III Arena source code is distributed in the hope that it will be
13 useful, but WITHOUT ANY WARRANTY; without even the implied warranty of
14 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 GNU General Public License for more details.
16
17 You should have received a copy of the GNU General Public License
18 along with Quake III Arena source code; if not, write to the Free Software
19 Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
20 ===========================================================================
21 */
22
23 #include "client.h"
24 #include "snd_local.h"
25
26 #define INDEX_FILE_EXTENSION ".index.dat"
27
28 #define MAX_RIFF_CHUNKS 16
29
30 typedef struct audioFormat_s
31 {
32 int rate;
33 int format;
34 int channels;
35 int bits;
36
37 int sampleSize;
38 int totalBytes;
39 } audioFormat_t;
40
41 typedef struct aviFileData_s
42 {
43 qboolean fileOpen;
44 fileHandle_t f;
45 char fileName[ MAX_QPATH ];
46 int fileSize;
47 int moviOffset;
48 int moviSize;
49
50 fileHandle_t idxF;
51 int numIndices;
52
53 int frameRate;
54 int framePeriod;
55 int width, height;
56 int numVideoFrames;
57 int maxRecordSize;
58 qboolean motionJpeg;
59
60 qboolean audio;
61 audioFormat_t a;
62 int numAudioFrames;
63
64 int chunkStack[ MAX_RIFF_CHUNKS ];
65 int chunkStackTop;
66
67 byte *cBuffer, *eBuffer;
68 } aviFileData_t;
69
70 static aviFileData_t afd;
71
72 #define MAX_AVI_BUFFER 2048
73
74 static byte buffer[ MAX_AVI_BUFFER ];
75 static int bufIndex;
76
77 /*
78 ===============
79 SafeFS_Write
80 ===============
81 */
82 static ID_INLINE void SafeFS_Write( const void *buf, int len, fileHandle_t f )
83 {
84 if( FS_Write( buf, len, f ) < len )
85 Com_Error( ERR_DROP, "Failed to write avi file" );
86 }
87
88 /*
89 ===============
90 WRITE_STRING
91 ===============
92 */
93 static ID_INLINE void WRITE_STRING( const char *s )
94 {
95 Com_Memcpy( &buffer[ bufIndex ], s, strlen( s ) );
96 bufIndex += strlen( s );
97 }
98
99 /*
100 ===============
101 WRITE_4BYTES
102 ===============
103 */
104 static ID_INLINE void WRITE_4BYTES( int x )
105 {
106 buffer[ bufIndex + 0 ] = (byte)( ( x >> 0 ) & 0xFF );
107 buffer[ bufIndex + 1 ] = (byte)( ( x >> 8 ) & 0xFF );
108 buffer[ bufIndex + 2 ] = (byte)( ( x >> 16 ) & 0xFF );
109 buffer[ bufIndex + 3 ] = (byte)( ( x >> 24 ) & 0xFF );
110 bufIndex += 4;
111 }
112
113 /*
114 ===============
115 WRITE_2BYTES
116 ===============
117 */
118 static ID_INLINE void WRITE_2BYTES( int x )
119 {
120 buffer[ bufIndex + 0 ] = (byte)( ( x >> 0 ) & 0xFF );
121 buffer[ bufIndex + 1 ] = (byte)( ( x >> 8 ) & 0xFF );
122 bufIndex += 2;
123 }
124
125 /*
126 ===============
127 START_CHUNK
128 ===============
129 */
130 static ID_INLINE void START_CHUNK( const char *s )
131 {
132 if( afd.chunkStackTop == MAX_RIFF_CHUNKS )
133 {
134 Com_Error( ERR_DROP, "ERROR: Top of chunkstack breached" );
135 }
136
137 afd.chunkStack[ afd.chunkStackTop ] = bufIndex;
138 afd.chunkStackTop++;
139 WRITE_STRING( s );
140 WRITE_4BYTES( 0 );
141 }
142
143 /*
144 ===============
145 END_CHUNK
146 ===============
147 */
148 static ID_INLINE void END_CHUNK( void )
149 {
150 int endIndex = bufIndex;
151
152 if( afd.chunkStackTop <= 0 )
153 {
154 Com_Error( ERR_DROP, "ERROR: Bottom of chunkstack breached" );
155 }
156
157 afd.chunkStackTop--;
158 bufIndex = afd.chunkStack[ afd.chunkStackTop ];
159 bufIndex += 4;
160 WRITE_4BYTES( endIndex - bufIndex - 4 );
161 bufIndex = endIndex;
162 bufIndex = PAD( bufIndex, 2 );
163 }
164
165 /*
166 ===============
167 CL_WriteAVIHeader
168 ===============
169 */
170 void CL_WriteAVIHeader( void )
171 {
172 bufIndex = 0;
173 afd.chunkStackTop = 0;
174
175 START_CHUNK( "RIFF" );
176 {
177 WRITE_STRING( "AVI " );
178 {
179 START_CHUNK( "LIST" );
180 {
181 WRITE_STRING( "hdrl" );
182 WRITE_STRING( "avih" );
183 WRITE_4BYTES( 56 ); //"avih" "chunk" size
184 WRITE_4BYTES( afd.framePeriod ); //dwMicroSecPerFrame
185 WRITE_4BYTES( afd.maxRecordSize *
186 afd.frameRate ); //dwMaxBytesPerSec
187 WRITE_4BYTES( 0 ); //dwReserved1
188 WRITE_4BYTES( 0x110 ); //dwFlags bits HAS_INDEX and IS_INTERLEAVED
189 WRITE_4BYTES( afd.numVideoFrames ); //dwTotalFrames
190 WRITE_4BYTES( 0 ); //dwInitialFrame
191
192 if( afd.audio ) //dwStreams
193 WRITE_4BYTES( 2 );
194 else
195 WRITE_4BYTES( 1 );
196
197 WRITE_4BYTES( afd.maxRecordSize ); //dwSuggestedBufferSize
198 WRITE_4BYTES( afd.width ); //dwWidth
199 WRITE_4BYTES( afd.height ); //dwHeight
200 WRITE_4BYTES( 0 ); //dwReserved[ 0 ]
201 WRITE_4BYTES( 0 ); //dwReserved[ 1 ]
202 WRITE_4BYTES( 0 ); //dwReserved[ 2 ]
203 WRITE_4BYTES( 0 ); //dwReserved[ 3 ]
204
205 START_CHUNK( "LIST" );
206 {
207 WRITE_STRING( "strl" );
208 WRITE_STRING( "strh" );
209 WRITE_4BYTES( 56 ); //"strh" "chunk" size
210 WRITE_STRING( "vids" );
211
212 if( afd.motionJpeg )
213 WRITE_STRING( "MJPG" );
214 else
215 WRITE_4BYTES( 0 ); // BI_RGB
216
217 WRITE_4BYTES( 0 ); //dwFlags
218 WRITE_4BYTES( 0 ); //dwPriority
219 WRITE_4BYTES( 0 ); //dwInitialFrame
220
221 WRITE_4BYTES( 1 ); //dwTimescale
222 WRITE_4BYTES( afd.frameRate ); //dwDataRate
223 WRITE_4BYTES( 0 ); //dwStartTime
224 WRITE_4BYTES( afd.numVideoFrames ); //dwDataLength
225
226 WRITE_4BYTES( afd.maxRecordSize ); //dwSuggestedBufferSize
227 WRITE_4BYTES( -1 ); //dwQuality
228 WRITE_4BYTES( 0 ); //dwSampleSize
229 WRITE_2BYTES( 0 ); //rcFrame
230 WRITE_2BYTES( 0 ); //rcFrame
231 WRITE_2BYTES( afd.width ); //rcFrame
232 WRITE_2BYTES( afd.height ); //rcFrame
233
234 WRITE_STRING( "strf" );
235 WRITE_4BYTES( 40 ); //"strf" "chunk" size
236 WRITE_4BYTES( 40 ); //biSize
237 WRITE_4BYTES( afd.width ); //biWidth
238 WRITE_4BYTES( afd.height ); //biHeight
239 WRITE_2BYTES( 1 ); //biPlanes
240 WRITE_2BYTES( 24 ); //biBitCount
241
242 if( afd.motionJpeg ) //biCompression
243 {
244 WRITE_STRING( "MJPG" );
245 WRITE_4BYTES( afd.width *
246 afd.height ); //biSizeImage
247 }
248 else
249 {
250 WRITE_4BYTES( 0 ); // BI_RGB
251 WRITE_4BYTES( afd.width *
252 afd.height * 3 ); //biSizeImage
253 }
254
255 WRITE_4BYTES( 0 ); //biXPelsPetMeter
256 WRITE_4BYTES( 0 ); //biYPelsPetMeter
257 WRITE_4BYTES( 0 ); //biClrUsed
258 WRITE_4BYTES( 0 ); //biClrImportant
259 }
260 END_CHUNK( );
261
262 if( afd.audio )
263 {
264 START_CHUNK( "LIST" );
265 {
266 WRITE_STRING( "strl" );
267 WRITE_STRING( "strh" );
268 WRITE_4BYTES( 56 ); //"strh" "chunk" size
269 WRITE_STRING( "auds" );
270 WRITE_4BYTES( 0 ); //FCC
271 WRITE_4BYTES( 0 ); //dwFlags
272 WRITE_4BYTES( 0 ); //dwPriority
273 WRITE_4BYTES( 0 ); //dwInitialFrame
274
275 WRITE_4BYTES( afd.a.sampleSize ); //dwTimescale
276 WRITE_4BYTES( afd.a.sampleSize *
277 afd.a.rate ); //dwDataRate
278 WRITE_4BYTES( 0 ); //dwStartTime
279 WRITE_4BYTES( afd.a.totalBytes /
280 afd.a.sampleSize ); //dwDataLength
281
282 WRITE_4BYTES( 0 ); //dwSuggestedBufferSize
283 WRITE_4BYTES( -1 ); //dwQuality
284 WRITE_4BYTES( afd.a.sampleSize ); //dwSampleSize
285 WRITE_2BYTES( 0 ); //rcFrame
286 WRITE_2BYTES( 0 ); //rcFrame
287 WRITE_2BYTES( 0 ); //rcFrame
288 WRITE_2BYTES( 0 ); //rcFrame
289
290 WRITE_STRING( "strf" );
291 WRITE_4BYTES( 18 ); //"strf" "chunk" size
292 WRITE_2BYTES( afd.a.format ); //wFormatTag
293 WRITE_2BYTES( afd.a.channels ); //nChannels
294 WRITE_4BYTES( afd.a.rate ); //nSamplesPerSec
295 WRITE_4BYTES( afd.a.sampleSize *
296 afd.a.rate ); //nAvgBytesPerSec
297 WRITE_2BYTES( afd.a.sampleSize ); //nBlockAlign
298 WRITE_2BYTES( afd.a.bits ); //wBitsPerSample
299 WRITE_2BYTES( 0 ); //cbSize
300 }
301 END_CHUNK( );
302 }
303 }
304 END_CHUNK( );
305
306 afd.moviOffset = bufIndex;
307
308 START_CHUNK( "LIST" );
309 {
310 WRITE_STRING( "movi" );
311 }
312 }
313 }
314 }
315
316 /*
317 ===============
318 CL_OpenAVIForWriting
319
320 Creates an AVI file and gets it into a state where
321 writing the actual data can begin
322 ===============
323 */
324 qboolean CL_OpenAVIForWriting( const char *fileName )
325 {
326 if( afd.fileOpen )
327 return qfalse;
328
329 Com_Memset( &afd, 0, sizeof( aviFileData_t ) );
330
331 // Don't start if a framerate has not been chosen
332 if( cl_aviFrameRate->integer <= 0 )
333 {
334 Com_Printf( S_COLOR_RED "cl_aviFrameRate must be >= 1\n" );
335 return qfalse;
336 }
337
338 if( ( afd.f = FS_FOpenFileWrite_HomeData( fileName ) ) <= 0 )
339 return qfalse;
340
341 if( ( afd.idxF = FS_FOpenFileWrite_HomeData(
342 va( "%s" INDEX_FILE_EXTENSION, fileName ) ) ) <= 0 )
343 {
344 FS_FCloseFile( afd.f );
345 return qfalse;
346 }
347
348 Q_strncpyz( afd.fileName, fileName, MAX_QPATH );
349
350 afd.frameRate = cl_aviFrameRate->integer;
351 afd.framePeriod = (int)( 1000000.0f / afd.frameRate );
352 afd.width = cls.glconfig.vidWidth;
353 afd.height = cls.glconfig.vidHeight;
354
355 if( cl_aviMotionJpeg->integer )
356 afd.motionJpeg = qtrue;
357 else
358 afd.motionJpeg = qfalse;
359
360 // Capture buffer stores RGB pixels but OpenGL ES reads RGBA and converts to RGB in-place.
361 // Encode buffer only needs to store RGB pixels.
362 // Allocate a bit more space for the capture buffer to account for possible
363 // padding at the end of pixel lines, and padding for alignment
364 #define MAX_PACK_LEN 16
365 afd.cBuffer = Z_Malloc((afd.width * 4 + MAX_PACK_LEN - 1) * afd.height + MAX_PACK_LEN - 1);
366 // raw avi files have pixel lines start on 4-byte boundaries
367 afd.eBuffer = Z_Malloc(PAD(afd.width * 3, AVI_LINE_PADDING) * afd.height);
368
369 afd.a.rate = dma.speed;
370 afd.a.format = WAV_FORMAT_PCM;
371 afd.a.channels = dma.channels;
372 /* !!! FIXME: if CL_WriteAVIAudioFrame() is ever called from somewhere other
373 !!! FIXME: than S_TransferStereo16(), we will need to handle/convert
374 !!! FIXME: float32 samples for AVI writing. */
375 afd.a.bits = dma.samplebits;
376 afd.a.sampleSize = ( afd.a.bits / 8 ) * afd.a.channels;
377
378 if( afd.a.rate % afd.frameRate )
379 {
380 int suggestRate = afd.frameRate;
381
382 while( ( afd.a.rate % suggestRate ) && suggestRate >= 1 )
383 suggestRate--;
384
385 Com_Printf( S_COLOR_YELLOW "WARNING: cl_aviFrameRate is not a divisor "
386 "of the audio rate, suggest %d\n", suggestRate );
387 }
388
389 if( !Cvar_VariableIntegerValue( "s_initsound" ) )
390 {
391 afd.audio = qfalse;
392 }
393 else if( Q_stricmp( Cvar_VariableString( "s_backend" ), "OpenAL" ) )
394 {
395 if( afd.a.bits != 16 || afd.a.channels != 2 )
396 {
397 Com_Printf( S_COLOR_YELLOW "WARNING: Audio format of %d bit/%d channels not supported",
398 afd.a.bits, afd.a.channels );
399 afd.audio = qfalse;
400 }
401 else
402 afd.audio = qtrue;
403 }
404 else
405 {
406 afd.audio = qfalse;
407 Com_Printf( S_COLOR_YELLOW "WARNING: Audio capture is not supported "
408 "with OpenAL. Set s_useOpenAL to 0 for audio capture\n" );
409 }
410
411 // This doesn't write a real header, but allocates the
412 // correct amount of space at the beginning of the file
413 CL_WriteAVIHeader( );
414
415 SafeFS_Write( buffer, bufIndex, afd.f );
416 afd.fileSize = bufIndex;
417
418 bufIndex = 0;
419 START_CHUNK( "idx1" );
420 SafeFS_Write( buffer, bufIndex, afd.idxF );
421
422 afd.moviSize = 4; // For the "movi"
423 afd.fileOpen = qtrue;
424
425 return qtrue;
426 }
427
428 /*
429 ===============
430 CL_CheckFileSize
431 ===============
432 */
433 static qboolean CL_CheckFileSize( int bytesToAdd )
434 {
435 unsigned int newFileSize;
436
437 newFileSize =
438 afd.fileSize + // Current file size
439 bytesToAdd + // What we want to add
440 ( afd.numIndices * 16 ) + // The index
441 4; // The index size
442
443 // I assume all the operating systems
444 // we target can handle a 2Gb file
445 if( newFileSize > INT_MAX )
446 {
447 // Close the current file...
448 CL_CloseAVI( );
449
450 // ...And open a new one
451 CL_OpenAVIForWriting( va( "%s_", afd.fileName ) );
452
453 return qtrue;
454 }
455
456 return qfalse;
457 }
458
459 /*
460 ===============
461 CL_WriteAVIVideoFrame
462 ===============
463 */
464 void CL_WriteAVIVideoFrame( const byte *imageBuffer, int size )
465 {
466 int chunkOffset = afd.fileSize - afd.moviOffset - 8;
467 int chunkSize = 8 + size;
468 int paddingSize = PADLEN(size, 2);
469 byte padding[ 4 ] = { 0 };
470
471 if( !afd.fileOpen )
472 return;
473
474 // Chunk header + contents + padding
475 if( CL_CheckFileSize( 8 + size + 2 ) )
476 return;
477
478 bufIndex = 0;
479 WRITE_STRING( "00dc" );
480 WRITE_4BYTES( size );
481
482 SafeFS_Write( buffer, 8, afd.f );
483 SafeFS_Write( imageBuffer, size, afd.f );
484 SafeFS_Write( padding, paddingSize, afd.f );
485 afd.fileSize += ( chunkSize + paddingSize );
486
487 afd.numVideoFrames++;
488 afd.moviSize += ( chunkSize + paddingSize );
489
490 if( size > afd.maxRecordSize )
491 afd.maxRecordSize = size;
492
493 // Index
494 bufIndex = 0;
495 WRITE_STRING( "00dc" ); //dwIdentifier
496 WRITE_4BYTES( 0x00000010 ); //dwFlags (all frames are KeyFrames)
497 WRITE_4BYTES( chunkOffset ); //dwOffset
498 WRITE_4BYTES( size ); //dwLength
499 SafeFS_Write( buffer, 16, afd.idxF );
500
501 afd.numIndices++;
502 }
503
504 #define PCM_BUFFER_SIZE 44100
505
506 /*
507 ===============
508 CL_WriteAVIAudioFrame
509 ===============
510 */
511 void CL_WriteAVIAudioFrame( const byte *pcmBuffer, int size )
512 {
513 static byte pcmCaptureBuffer[ PCM_BUFFER_SIZE ] = { 0 };
514 static int bytesInBuffer = 0;
515
516 if( !afd.audio )
517 return;
518
519 if( !afd.fileOpen )
520 return;
521
522 // Chunk header + contents + padding
523 if( CL_CheckFileSize( 8 + bytesInBuffer + size + 2 ) )
524 return;
525
526 if( bytesInBuffer + size > PCM_BUFFER_SIZE )
527 {
528 Com_Printf( S_COLOR_YELLOW
529 "WARNING: Audio capture buffer overflow -- truncating\n" );
530 size = PCM_BUFFER_SIZE - bytesInBuffer;
531 }
532
533 Com_Memcpy( &pcmCaptureBuffer[ bytesInBuffer ], pcmBuffer, size );
534 bytesInBuffer += size;
535
536 // Only write if we have a frame's worth of audio
537 if( bytesInBuffer >= (int)ceil( (float)afd.a.rate / (float)afd.frameRate ) *
538 afd.a.sampleSize )
539 {
540 int chunkOffset = afd.fileSize - afd.moviOffset - 8;
541 int chunkSize = 8 + bytesInBuffer;
542 int paddingSize = PADLEN(bytesInBuffer, 2);
543 byte padding[ 4 ] = { 0 };
544
545 bufIndex = 0;
546 WRITE_STRING( "01wb" );
547 WRITE_4BYTES( bytesInBuffer );
548
549 SafeFS_Write( buffer, 8, afd.f );
550 SafeFS_Write( pcmCaptureBuffer, bytesInBuffer, afd.f );
551 SafeFS_Write( padding, paddingSize, afd.f );
552 afd.fileSize += ( chunkSize + paddingSize );
553
554 afd.numAudioFrames++;
555 afd.moviSize += ( chunkSize + paddingSize );
556 afd.a.totalBytes += bytesInBuffer;
557
558 // Index
559 bufIndex = 0;
560 WRITE_STRING( "01wb" ); //dwIdentifier
561 WRITE_4BYTES( 0 ); //dwFlags
562 WRITE_4BYTES( chunkOffset ); //dwOffset
563 WRITE_4BYTES( bytesInBuffer ); //dwLength
564 SafeFS_Write( buffer, 16, afd.idxF );
565
566 afd.numIndices++;
567
568 bytesInBuffer = 0;
569 }
570 }
571
572 /*
573 ===============
574 CL_TakeVideoFrame
575 ===============
576 */
577 void CL_TakeVideoFrame( void )
578 {
579 // AVI file isn't open
580 if( !afd.fileOpen )
581 return;
582
583 re.TakeVideoFrame( afd.width, afd.height,
584 afd.cBuffer, afd.eBuffer, afd.motionJpeg );
585 }
586
587 /*
588 ===============
589 CL_CloseAVI
590
591 Closes the AVI file and writes an index chunk
592 ===============
593 */
594 qboolean CL_CloseAVI( void )
595 {
596 int indexRemainder;
597 int indexSize = afd.numIndices * 16;
598 const char *idxFileName = va( "%s" INDEX_FILE_EXTENSION, afd.fileName );
599
600 // AVI file isn't open
601 if( !afd.fileOpen )
602 return qfalse;
603
604 afd.fileOpen = qfalse;
605
606 FS_Seek( afd.idxF, 4, FS_SEEK_SET );
607 bufIndex = 0;
608 WRITE_4BYTES( indexSize );
609 SafeFS_Write( buffer, bufIndex, afd.idxF );
610 FS_FCloseFile( afd.idxF );
611
612 // Write index
613
614 // Open the temp index file
615 if( ( indexSize = FS_FOpenFileRead( idxFileName,
616 &afd.idxF, qtrue ) ) <= 0 )
617 {
618 FS_FCloseFile( afd.f );
619 return qfalse;
620 }
621
622 indexRemainder = indexSize;
623
624 // Append index to end of avi file
625 while( indexRemainder > MAX_AVI_BUFFER )
626 {
627 FS_Read( buffer, MAX_AVI_BUFFER, afd.idxF );
628 SafeFS_Write( buffer, MAX_AVI_BUFFER, afd.f );
629 afd.fileSize += MAX_AVI_BUFFER;
630 indexRemainder -= MAX_AVI_BUFFER;
631 }
632 FS_Read( buffer, indexRemainder, afd.idxF );
633 SafeFS_Write( buffer, indexRemainder, afd.f );
634 afd.fileSize += indexRemainder;
635 FS_FCloseFile( afd.idxF );
636
637 // Remove temp index file
638 FS_Remove_HomeData( idxFileName );
639
640 // Write the real header
641 FS_Seek( afd.f, 0, FS_SEEK_SET );
642 CL_WriteAVIHeader( );
643
644 bufIndex = 4;
645 WRITE_4BYTES( afd.fileSize - 8 ); // "RIFF" size
646
647 bufIndex = afd.moviOffset + 4; // Skip "LIST"
648 WRITE_4BYTES( afd.moviSize );
649
650 SafeFS_Write( buffer, bufIndex, afd.f );
651
652 Z_Free( afd.cBuffer );
653 Z_Free( afd.eBuffer );
654 FS_FCloseFile( afd.f );
655
656 Com_Printf( "Wrote %d:%d frames to %s\n", afd.numVideoFrames, afd.numAudioFrames, afd.fileName );
657
658 return qtrue;
659 }
660
661 /*
662 ===============
663 CL_VideoRecording
664 ===============
665 */
666 qboolean CL_VideoRecording( void )
667 {
668 return afd.fileOpen;
669 }