forked from SwagSoftware/Kisak-Strike
-
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathavi.cpp
1184 lines (999 loc) · 32 KB
/
avi.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
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
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
//====== Copyright © 1996-2005, Valve Corporation, All rights reserved. =======
//
// Purpose:
//
//=============================================================================
#include "avi/iavi.h"
#include "avi.h"
#include "filesystem.h"
#include "tier1/strtools.h"
#include "tier1/utllinkedlist.h"
#include "tier1/keyvalues.h"
#include "materialsystem/imaterial.h"
#include "materialsystem/imaterialsystem.h"
#include "materialsystem/materialsystemutil.h"
#include "materialsystem/itexture.h"
#include "vtf/vtf.h"
#include "pixelwriter.h"
#include "tier3/tier3.h"
#pragma warning( disable : 4201 )
#define WIN32_LEAN_AND_MEAN
#include <windows.h>
#include <vfw.h>
#pragma warning( default : 4201 )
DWORD g_dwLastValidCodec = 0;
//-----------------------------------------------------------------------------
//
// Class used to write out AVI files
//
//-----------------------------------------------------------------------------
class CAviFile
{
public:
CAviFile();
void Init( const AVIParams_t& params, void *hWnd );
void Shutdown();
void AppendMovieSound( short *buf, size_t bufsize );
void AppendMovieFrame( const BGR888_t *pRGBData );
private:
void Reset();
void CreateVideoStreams( const AVIParams_t& params, void *hWnd );
void CreateAudioStream();
bool m_bValid;
int m_nWidth;
int m_nHeight;
IAVIFile *m_pAVIFile;
WAVEFORMATEX m_wFormat;
int m_nFrameRate;
int m_nFrameScale;
IAVIStream *m_pAudioStream;
IAVIStream *m_pVideoStream;
IAVIStream *m_pCompressedStream;
int m_nFrame;
int m_nSample;
HDC m_memdc;
HBITMAP m_DIBSection;
BITMAPINFO m_bi;
BITMAPINFOHEADER *m_bih;
};
//-----------------------------------------------------------------------------
// Constructor
//-----------------------------------------------------------------------------
CAviFile::CAviFile()
{
Reset();
}
//-----------------------------------------------------------------------------
// Reset the avi file
//-----------------------------------------------------------------------------
void CAviFile::Reset()
{
Q_memset( &m_wFormat, 0, sizeof( m_wFormat ) );
Q_memset( &m_bi, 0, sizeof( m_bi ) );
m_bValid = false;
m_nWidth = 0;
m_nHeight = 0;
m_pAVIFile = NULL;
m_nFrameRate = 0;
m_nFrameScale = 1;
m_pAudioStream = NULL;
m_pVideoStream = NULL;
m_pCompressedStream = NULL;
m_nFrame = 0;
m_nSample = 0;
m_memdc = ( HDC )0;
m_DIBSection = ( HBITMAP )0;
m_bih = &m_bi.bmiHeader;
m_bih->biSize = sizeof( *m_bih );
//m_bih->biWidth = xxx
//m_bih->biHeight = xxx
m_bih->biPlanes = 1;
m_bih->biBitCount = 24;
m_bih->biCompression = BI_RGB;
//m_bih->biSizeImage = ( ( m_bih->biWidth * m_bih->biBitCount/8 + 3 )& 0xFFFFFFFC ) * m_bih->biHeight;
m_bih->biXPelsPerMeter = 10000;
m_bih->biYPelsPerMeter = 10000;
m_bih->biClrUsed = 0;
m_bih->biClrImportant = 0;
}
//-----------------------------------------------------------------------------
// Start recording an AVI
//-----------------------------------------------------------------------------
void CAviFile::Init( const AVIParams_t& params, void *hWnd )
{
Reset();
char avifilename[ 512 ];
char fullavifilename[ 512 ];
Q_snprintf( avifilename, sizeof( avifilename ), "%s", params.m_pFileName );
Q_SetExtension( avifilename, ".avi", sizeof( avifilename ) );
g_pFullFileSystem->RelativePathToFullPath( avifilename, params.m_pPathID, fullavifilename, sizeof( fullavifilename ) );
if ( g_pFullFileSystem->FileExists( fullavifilename, params.m_pPathID ) )
{
g_pFullFileSystem->RemoveFile( fullavifilename, params.m_pPathID );
}
HRESULT hr = AVIFileOpen( &m_pAVIFile, fullavifilename, OF_WRITE | OF_CREATE, NULL );
if ( hr != AVIERR_OK )
return;
m_wFormat.cbSize = sizeof( m_wFormat );
m_wFormat.wFormatTag = WAVE_FORMAT_PCM;
m_wFormat.nChannels = params.m_nNumChannels;
m_wFormat.nSamplesPerSec = params.m_nSampleRate;
m_wFormat.nBlockAlign = params.m_nNumChannels * ( params.m_nSampleBits == 8 ? 1 : 2 );
m_wFormat.nAvgBytesPerSec = m_wFormat.nBlockAlign * params.m_nSampleRate;
m_wFormat.wBitsPerSample = params.m_nSampleBits;
m_nFrameRate = params.m_nFrameRate;
m_nFrameScale = params.m_nFrameScale;
m_bValid = true;
m_nHeight = params.m_nHeight;
m_nWidth = params.m_nWidth;
CreateVideoStreams( params, hWnd );
CreateAudioStream();
}
void CAviFile::Shutdown()
{
if ( m_pAudioStream )
{
AVIStreamRelease( m_pAudioStream );
m_pAudioStream = NULL;
}
if ( m_pVideoStream )
{
AVIStreamRelease( m_pVideoStream );
m_pVideoStream = NULL;
}
if ( m_pCompressedStream )
{
AVIStreamRelease( m_pCompressedStream );
m_pCompressedStream = NULL;
}
if ( m_pAVIFile )
{
AVIFileRelease( m_pAVIFile );
m_pAVIFile = NULL;
}
if ( m_DIBSection != 0 )
{
DeleteObject( m_DIBSection );
}
if ( m_memdc != 0 )
{
// Release the compatible DC
DeleteDC( m_memdc );
}
Reset();
}
static unsigned int FormatAviMessage( HRESULT code, char *buf, unsigned int len)
{
const char *msg="unknown avi result code";
switch (code)
{
case S_OK: msg="Success"; break;
case AVIERR_BADFORMAT: msg="AVIERR_BADFORMAT: corrupt file or unrecognized format"; break;
case AVIERR_MEMORY: msg="AVIERR_MEMORY: insufficient memory"; break;
case AVIERR_FILEREAD: msg="AVIERR_FILEREAD: disk error while reading file"; break;
case AVIERR_FILEOPEN: msg="AVIERR_FILEOPEN: disk error while opening file"; break;
case REGDB_E_CLASSNOTREG: msg="REGDB_E_CLASSNOTREG: file type not recognised"; break;
case AVIERR_READONLY: msg="AVIERR_READONLY: file is read-only"; break;
case AVIERR_NOCOMPRESSOR: msg="AVIERR_NOCOMPRESSOR: a suitable compressor could not be found"; break;
case AVIERR_UNSUPPORTED: msg="AVIERR_UNSUPPORTED: compression is not supported for this type of data"; break;
case AVIERR_INTERNAL: msg="AVIERR_INTERNAL: internal error"; break;
case AVIERR_BADFLAGS: msg="AVIERR_BADFLAGS"; break;
case AVIERR_BADPARAM: msg="AVIERR_BADPARAM"; break;
case AVIERR_BADSIZE: msg="AVIERR_BADSIZE"; break;
case AVIERR_BADHANDLE: msg="AVIERR_BADHANDLE"; break;
case AVIERR_FILEWRITE: msg="AVIERR_FILEWRITE: disk error while writing file"; break;
case AVIERR_COMPRESSOR: msg="AVIERR_COMPRESSOR"; break;
case AVIERR_NODATA: msg="AVIERR_READONLY"; break;
case AVIERR_BUFFERTOOSMALL: msg="AVIERR_BUFFERTOOSMALL"; break;
case AVIERR_CANTCOMPRESS: msg="AVIERR_CANTCOMPRESS"; break;
case AVIERR_USERABORT: msg="AVIERR_USERABORT"; break;
case AVIERR_ERROR: msg="AVIERR_ERROR"; break;
}
unsigned int mlen = (unsigned int)Q_strlen( msg );
if ( buf==0 || len==0 )
return mlen;
unsigned int n=mlen;
if (n+1>len)
{
n=len-1;
}
strncpy(buf,msg,n);
buf[n]=0;
return mlen;
}
static void ReportError( HRESULT hr )
{
char buf[ 512 ];
FormatAviMessage( hr, buf, sizeof( buf ) );
Warning( "%s\n", buf );
}
void CAviFile::CreateVideoStreams( const AVIParams_t& params, void *hWnd )
{
AVISTREAMINFO streaminfo;
Q_memset( &streaminfo, 0, sizeof( streaminfo ) ) ;
streaminfo.fccType = streamtypeVIDEO;
streaminfo.fccHandler = 0;
streaminfo.dwScale = params.m_nFrameScale;
streaminfo.dwRate = params.m_nFrameRate;
streaminfo.dwSuggestedBufferSize = params.m_nWidth * params.m_nHeight * 3;
SetRect( &streaminfo.rcFrame, 0, 0, params.m_nWidth, params.m_nHeight );
HRESULT hr = AVIFileCreateStream( m_pAVIFile, &m_pVideoStream, &streaminfo );
if ( hr != AVIERR_OK )
{
m_bValid = false;
ReportError( hr );
return;
}
AVICOMPRESSOPTIONS compression;
Q_memset( &compression, 0, sizeof( compression ) );
AVICOMPRESSOPTIONS *aopts[1];
aopts[ 0 ] = &compression;
// Choose DIVX compressor for now
Warning( "FIXME: DIVX only for now\n" );
if ( params.m_bGetCodecFromUser )
{
// FIXME: This won't work so well in full screen!!!
if ( !AVISaveOptions( (HWND)hWnd, 0, 1, &m_pVideoStream, aopts ) )
{
m_bValid = false;
return;
}
// Cache for next time
g_dwLastValidCodec = compression.fccHandler;
}
else
{
compression.fccHandler = g_dwLastValidCodec ? g_dwLastValidCodec : mmioFOURCC( 'd', 'i', 'b', ' ' );
}
hr = AVIMakeCompressedStream( &m_pCompressedStream, m_pVideoStream, &compression, NULL );
if ( hr != AVIERR_OK )
{
m_bValid = false;
ReportError( hr );
return;
}
// Create a compatible DC
HDC hdcscreen = GetDC( GetDesktopWindow() );
m_memdc = CreateCompatibleDC(hdcscreen);
ReleaseDC( GetDesktopWindow(), hdcscreen );
// Set up a DIBSection for the screen
m_bih->biWidth = params.m_nWidth;
m_bih->biHeight = params.m_nHeight;
m_bih->biSizeImage = ( ( m_bih->biWidth * m_bih->biBitCount / 8 + 3 )& 0xFFFFFFFC ) * m_bih->biHeight;
// Create the DIBSection
void *bits;
m_DIBSection = CreateDIBSection
(
m_memdc,
( BITMAPINFO *)m_bih,
DIB_RGB_COLORS,
&bits,
NULL,
NULL
);
// Get at the DIBSection object
DIBSECTION dibs;
GetObject( m_DIBSection, sizeof( dibs ), &dibs );
// Set the stream format
hr = AVIStreamSetFormat(
m_pCompressedStream,
0,
&dibs.dsBmih,
dibs.dsBmih.biSize + dibs.dsBmih.biClrUsed *sizeof( RGBQUAD )
);
if ( hr != AVIERR_OK )
{
m_bValid = false;
ReportError( hr );
return;
}
}
void CAviFile::CreateAudioStream()
{
AVISTREAMINFO audiostream;
Q_memset( &audiostream, 0, sizeof( audiostream ) );
audiostream.fccType = streamtypeAUDIO;
audiostream.dwScale = m_wFormat.nBlockAlign;
audiostream.dwRate = m_wFormat.nSamplesPerSec * m_wFormat.nBlockAlign;
audiostream.dwSampleSize = m_wFormat.nBlockAlign;
audiostream.dwQuality = (DWORD)-1;
HRESULT hr = AVIFileCreateStream( m_pAVIFile, &m_pAudioStream, &audiostream );
if ( hr != AVIERR_OK )
{
m_bValid = false;
ReportError( hr );
return;
}
hr = AVIStreamSetFormat( m_pAudioStream, 0, &m_wFormat, sizeof( m_wFormat ) );
if ( hr != AVIERR_OK )
{
m_bValid = false;
ReportError( hr );
return;
}
}
void CAviFile::AppendMovieSound( short *buf, size_t bufsize )
{
if ( !m_bValid )
return;
unsigned long numsamps = bufsize / sizeof( short ); // numbytes*8 / au->wfx.wBitsPerSample;
//
// now we can write the data
HRESULT hr = AVIStreamWrite
(
m_pAudioStream,
m_nSample,
numsamps,
buf,
bufsize,
0,
NULL,
NULL
);
if ( hr != AVIERR_OK )
{
m_bValid = false;
ReportError( hr );
return;
}
m_nSample += numsamps;
}
//-----------------------------------------------------------------------------
// Adds a frame of the movie to the AVI
//-----------------------------------------------------------------------------
void CAviFile::AppendMovieFrame( const BGR888_t *pRGBData )
{
if ( !m_bValid )
return;
DIBSECTION dibs;
HGDIOBJ hOldObject = SelectObject( m_memdc, m_DIBSection );
// Update the DIBSection bits
// FIXME: Have to invert this vertically since passing in negative
// biHeights in the m_bih field doesn't make the system know it's a top-down AVI
int scanlines = 0;
for ( int i = 0; i < m_nHeight; ++i )
{
scanlines += SetDIBits( m_memdc, m_DIBSection, m_nHeight - i - 1, 1, pRGBData,
( CONST BITMAPINFO * )m_bih, DIB_RGB_COLORS );
pRGBData += m_nWidth;
}
int objectSize = GetObject( m_DIBSection, sizeof( dibs ), &dibs );
if ( scanlines != m_nHeight || objectSize != sizeof( DIBSECTION ))
{
SelectObject( m_memdc, hOldObject );
m_bValid = false;
return;
}
// Now we can add the frame
HRESULT hr = AVIStreamWrite(
m_pCompressedStream,
m_nFrame,
1,
dibs.dsBm.bmBits,
dibs.dsBmih.biSizeImage,
AVIIF_KEYFRAME,
NULL,
NULL );
SelectObject( m_memdc, hOldObject );
if ( hr != AVIERR_OK )
{
m_bValid = false;
ReportError( hr );
return;
}
++m_nFrame;
}
//-----------------------------------------------------------------------------
//
// Class used to associated AVI files with IMaterials
//
//-----------------------------------------------------------------------------
class CAVIMaterial : public ITextureRegenerator
{
public:
CAVIMaterial();
// Initializes, shuts down the material
bool Init( const char *pMaterialName, const char *pFileName, const char *pPathID );
void Shutdown();
// Inherited from ITextureRegenerator
virtual void RegenerateTextureBits( ITexture *pTexture, IVTFTexture *pVTFTexture, Rect_t *pRect );
virtual void Release();
// Returns the material
IMaterial *GetMaterial();
// Returns the texcoord range
void GetTexCoordRange( float *pMaxU, float *pMaxV );
// Returns the frame size of the AVI (stored in a subrect of the material itself)
void GetFrameSize( int *pWidth, int *pHeight );
// Sets the current time
void SetTime( float flTime );
// Returns the frame rate/count of the AVI
int GetFrameRate( );
int GetFrameCount( );
// Sets the frame for an AVI material (use instead of SetTime)
void SetFrame( float flFrame );
private:
// Initializes, shuts down the procedural texture
void CreateProceduralTexture( const char *pTextureName );
void DestroyProceduralTexture();
// Initializes, shuts down the procedural material
void CreateProceduralMaterial( const char *pMaterialName );
void DestroyProceduralMaterial();
// Initializes, shuts down the video stream
void CreateVideoStream( );
void DestroyVideoStream( );
CMaterialReference m_Material;
CTextureReference m_Texture;
IAVIFile *m_pAVIFile;
IAVIStream *m_pAVIStream;
IGetFrame *m_pGetFrame;
int m_nAVIWidth;
int m_nAVIHeight;
int m_nFrameRate;
int m_nFrameCount;
int m_nCurrentSample;
HDC m_memdc;
HBITMAP m_DIBSection;
BITMAPINFO m_bi;
BITMAPINFOHEADER *m_bih;
};
//-----------------------------------------------------------------------------
// Constructor
//-----------------------------------------------------------------------------
CAVIMaterial::CAVIMaterial()
{
Q_memset( &m_bi, 0, sizeof( m_bi ) );
m_memdc = ( HDC )0;
m_DIBSection = ( HBITMAP )0;
m_pAVIStream = NULL;
m_pAVIFile = NULL;
m_pGetFrame = NULL;
}
//-----------------------------------------------------------------------------
// Initializes the material
//-----------------------------------------------------------------------------
bool CAVIMaterial::Init( const char *pMaterialName, const char *pFileName, const char *pPathID )
{
// Determine the full path name of the AVI
char pAVIFileName[ 512 ];
char pFullAVIFileName[ 512 ];
Q_snprintf( pAVIFileName, sizeof( pAVIFileName ), "%s", pFileName );
Q_DefaultExtension( pAVIFileName, ".avi", sizeof( pAVIFileName ) );
g_pFullFileSystem->RelativePathToFullPath( pAVIFileName, pPathID, pFullAVIFileName, sizeof( pFullAVIFileName ) );
HRESULT hr = AVIFileOpen( &m_pAVIFile, pFullAVIFileName, OF_READ, NULL );
if ( hr != AVIERR_OK )
{
Warning( "AVI '%s' not found\n", pFullAVIFileName );
m_nAVIWidth = 64;
m_nAVIHeight = 64;
m_nFrameRate = 1;
m_nFrameCount = 1;
m_Material.Init( "debug/debugempty", TEXTURE_GROUP_OTHER );
return false;
}
// Get AVI size
AVIFILEINFO info;
AVIFileInfo( m_pAVIFile, &info, sizeof(info) );
m_nAVIWidth = info.dwWidth;
m_nAVIHeight = info.dwHeight;
m_nFrameRate = (int)( (float)info.dwRate / (float)info.dwScale + 0.5f );
CreateProceduralTexture( pMaterialName );
CreateProceduralMaterial( pMaterialName );
CreateVideoStream();
// Get frame count
m_nFrameCount = MAX( 0, AVIStreamLength( m_pAVIStream ) );
m_Texture->Download();
return true;
}
void CAVIMaterial::Shutdown()
{
DestroyVideoStream();
DestroyProceduralMaterial( );
DestroyProceduralTexture( );
if ( m_pAVIFile )
{
AVIFileRelease( m_pAVIFile );
m_pAVIFile = NULL;
}
}
//-----------------------------------------------------------------------------
// Returns the material
//-----------------------------------------------------------------------------
IMaterial *CAVIMaterial::GetMaterial()
{
return m_Material;
}
//-----------------------------------------------------------------------------
// Returns the texcoord range
//-----------------------------------------------------------------------------
void CAVIMaterial::GetTexCoordRange( float *pMaxU, float *pMaxV )
{
if ( !m_Texture )
{
*pMaxU = *pMaxV = 1.0f;
return;
}
int nTextureWidth = m_Texture->GetActualWidth();
int nTextureHeight = m_Texture->GetActualHeight();
if ( nTextureWidth )
*pMaxU = (float)m_nAVIWidth / (float)nTextureWidth;
else
*pMaxU = 0.0f;
if ( nTextureHeight )
*pMaxV = (float)m_nAVIHeight / (float)nTextureHeight;
else
*pMaxV = 0.0f;
}
//-----------------------------------------------------------------------------
// Returns the frame size of the AVI (stored in a subrect of the material itself)
//-----------------------------------------------------------------------------
void CAVIMaterial::GetFrameSize( int *pWidth, int *pHeight )
{
*pWidth = m_nAVIWidth;
*pHeight = m_nAVIHeight;
}
//-----------------------------------------------------------------------------
// Computes a power of two at least as big as the passed-in number
//-----------------------------------------------------------------------------
static inline int ComputeGreaterPowerOfTwo( int n )
{
int i = 1;
while ( i < n )
{
i <<= 1;
}
return i;
}
//-----------------------------------------------------------------------------
// Initializes, shuts down the procedural texture
//-----------------------------------------------------------------------------
void CAVIMaterial::CreateProceduralTexture( const char *pTextureName )
{
// Choose power-of-two textures which are at least as big as the AVI
int nWidth = ComputeGreaterPowerOfTwo( m_nAVIWidth );
int nHeight = ComputeGreaterPowerOfTwo( m_nAVIHeight );
m_Texture.InitProceduralTexture( pTextureName, "avi", nWidth, nHeight,
IMAGE_FORMAT_RGBA8888, TEXTUREFLAGS_CLAMPS | TEXTUREFLAGS_CLAMPT | TEXTUREFLAGS_NOMIP |
TEXTUREFLAGS_PROCEDURAL | TEXTUREFLAGS_SINGLECOPY );
m_Texture->SetTextureRegenerator( this );
}
void CAVIMaterial::DestroyProceduralTexture()
{
if (m_Texture)
{
m_Texture->SetTextureRegenerator( NULL );
m_Texture.Shutdown();
}
}
//-----------------------------------------------------------------------------
// Initializes, shuts down the procedural material
//-----------------------------------------------------------------------------
void CAVIMaterial::CreateProceduralMaterial( const char *pMaterialName )
{
// FIXME: gak, this is backwards. Why doesn't the material just see that it has a funky basetexture?
char vmtfilename[ 512 ];
Q_strcpy( vmtfilename, pMaterialName );
Q_SetExtension( vmtfilename, ".vmt", sizeof( vmtfilename ) );
KeyValues *pVMTKeyValues = new KeyValues( "UnlitGeneric" );
if (!pVMTKeyValues->LoadFromFile( g_pFullFileSystem , vmtfilename, "GAME" ))
{
pVMTKeyValues->SetString( "$basetexture", m_Texture->GetName() );
pVMTKeyValues->SetInt( "$nofog", 1 );
pVMTKeyValues->SetInt( "$spriteorientation", 3 );
pVMTKeyValues->SetInt( "$translucent", 1 );
}
m_Material.Init( pMaterialName, pVMTKeyValues );
m_Material->Refresh();
}
void CAVIMaterial::DestroyProceduralMaterial()
{
m_Material.Shutdown();
}
//-----------------------------------------------------------------------------
// Sets the current time
//-----------------------------------------------------------------------------
void CAVIMaterial::SetTime( float flTime )
{
if ( m_pAVIStream )
{
// Round to the nearest frame
// FIXME: Strangely, AVIStreamTimeToSample gets off by several frames if you're a ways down the stream
// int nCurrentSample = AVIStreamTimeToSample( m_pAVIStream, ( flTime + 0.5f / m_nFrameRate )* 1000.0f );
int nCurrentSample = (int)( flTime * m_nFrameRate + 0.5f );
if ( m_nCurrentSample != nCurrentSample )
{
m_nCurrentSample = nCurrentSample;
m_Texture->Download();
}
}
}
//-----------------------------------------------------------------------------
// Returns the frame rate of the AVI
//-----------------------------------------------------------------------------
int CAVIMaterial::GetFrameRate( )
{
return m_nFrameRate;
}
int CAVIMaterial::GetFrameCount( )
{
return m_nFrameCount;
}
//-----------------------------------------------------------------------------
// Sets the frame for an AVI material (use instead of SetTime)
//-----------------------------------------------------------------------------
void CAVIMaterial::SetFrame( float flFrame )
{
if ( m_pAVIStream )
{
int nCurrentSample = (int)( flFrame + 0.5f );
if ( m_nCurrentSample != nCurrentSample )
{
m_nCurrentSample = nCurrentSample;
m_Texture->Download();
}
}
}
//-----------------------------------------------------------------------------
// Initializes, shuts down the video stream
//-----------------------------------------------------------------------------
void CAVIMaterial::CreateVideoStream( )
{
HRESULT hr = AVIFileGetStream( m_pAVIFile, &m_pAVIStream, streamtypeVIDEO, 0 );
if ( hr != AVIERR_OK )
{
ReportError( hr );
return;
}
m_nCurrentSample = AVIStreamStart( m_pAVIStream );
// Create a compatible DC
HDC hdcscreen = GetDC( GetDesktopWindow() );
m_memdc = CreateCompatibleDC( hdcscreen );
ReleaseDC( GetDesktopWindow(), hdcscreen );
// Set up a DIBSection for the screen
m_bih = &m_bi.bmiHeader;
m_bih->biSize = sizeof( *m_bih );
m_bih->biWidth = m_nAVIWidth;
m_bih->biHeight = m_nAVIHeight;
m_bih->biPlanes = 1;
m_bih->biBitCount = 32;
m_bih->biCompression = BI_RGB;
m_bih->biSizeImage = ( ( m_bih->biWidth * m_bih->biBitCount / 8 + 3 )& 0xFFFFFFFC ) * m_bih->biHeight;
m_bih->biXPelsPerMeter = 10000;
m_bih->biYPelsPerMeter = 10000;
m_bih->biClrUsed = 0;
m_bih->biClrImportant = 0;
// Create the DIBSection
void *bits;
m_DIBSection = CreateDIBSection( m_memdc, ( BITMAPINFO *)m_bih, DIB_RGB_COLORS, &bits, NULL, NULL );
// Get at the DIBSection object
DIBSECTION dibs;
GetObject( m_DIBSection, sizeof( dibs ), &dibs );
m_pGetFrame = AVIStreamGetFrameOpen( m_pAVIStream, &dibs.dsBmih );
}
void CAVIMaterial::DestroyVideoStream( )
{
if ( m_pGetFrame )
{
AVIStreamGetFrameClose( m_pGetFrame );
m_pGetFrame = NULL;
}
if ( m_DIBSection != 0 )
{
DeleteObject( m_DIBSection );
m_DIBSection = (HBITMAP)0;
}
if ( m_memdc != 0 )
{
// Release the compatible DC
DeleteDC( m_memdc );
m_memdc = (HDC)0;
}
if ( m_pAVIStream )
{
AVIStreamRelease( m_pAVIStream );
m_pAVIStream = NULL;
}
}
//-----------------------------------------------------------------------------
// Inherited from ITextureRegenerator
//-----------------------------------------------------------------------------
void CAVIMaterial::RegenerateTextureBits( ITexture *pTexture, IVTFTexture *pVTFTexture, Rect_t *pRect )
{
CPixelWriter pixelWriter;
LPBITMAPINFOHEADER lpbih;
unsigned char *pData;
int i, y, nIncY;
// Error condition
if ( !m_pAVIStream || !m_pGetFrame || (pVTFTexture->FrameCount() > 1) ||
(pVTFTexture->FaceCount() > 1) || (pVTFTexture->MipCount() > 1) || (pVTFTexture->Depth() > 1) )
{
goto AVIMaterialError;
}
lpbih = (LPBITMAPINFOHEADER)AVIStreamGetFrame( m_pGetFrame, m_nCurrentSample );
if ( !lpbih )
goto AVIMaterialError;
// Set up the pixel writer to write into the VTF texture
pixelWriter.SetPixelMemory( pVTFTexture->Format(),
pVTFTexture->ImageData( ), pVTFTexture->RowSizeInBytes( 0 ) );
int nWidth = pVTFTexture->Width();
int nHeight = pVTFTexture->Height();
int nBihHeight = abs( lpbih->biHeight );
if ( lpbih->biWidth > nWidth || nBihHeight > nHeight )
goto AVIMaterialError;
pData = (unsigned char *)lpbih + lpbih->biSize;
if ( lpbih->biBitCount == 8 )
{
// This is the palette
pData += 256 * sizeof(RGBQUAD);
}
if ( (( lpbih->biBitCount == 16 ) || ( lpbih->biBitCount == 32 )) && ( lpbih->biCompression == BI_BITFIELDS ) )
{
pData += 3 * sizeof(DWORD);
// MASKS NOT IMPLEMENTED YET
Assert( 0 );
}
int nStride = ( lpbih->biWidth * lpbih->biBitCount / 8 + 3 ) & 0xFFFFFFFC;
if ( lpbih->biHeight > 0 )
{
y = nBihHeight - 1;
nIncY = -1;
}
else
{
y = 0;
nIncY = 1;
}
if ( lpbih->biBitCount == 24)
{
for ( i = 0; i < nBihHeight; ++i, pData += nStride, y += nIncY )
{
pixelWriter.Seek( 0, y );
BGR888_t *pAVIPixel = (BGR888_t*)pData;
for (int x = 0; x < lpbih->biWidth; ++x, ++pAVIPixel)
{
pixelWriter.WritePixel( pAVIPixel->r, pAVIPixel->g, pAVIPixel->b, 255 );
}
}
}
else if (lpbih->biBitCount == 32)
{
for ( i = 0; i < nBihHeight; ++i, pData += nStride, y += nIncY )
{
pixelWriter.Seek( 0, y );
BGRA8888_t *pAVIPixel = (BGRA8888_t*)pData;
for (int x = 0; x < lpbih->biWidth; ++x, ++pAVIPixel)
{
pixelWriter.WritePixel( pAVIPixel->r, pAVIPixel->g, pAVIPixel->b, pAVIPixel->a );
}
}
}
return;
AVIMaterialError:
int nBytes = pVTFTexture->ComputeTotalSize();
memset( pVTFTexture->ImageData(), 0xFF, nBytes );
return;
}
void CAVIMaterial::Release()
{
}
//-----------------------------------------------------------------------------
//
// Implementation of IAvi
//
//-----------------------------------------------------------------------------
class CAvi : public CTier3AppSystem< IAvi >
{
typedef CTier3AppSystem< IAvi > BaseClass;
public:
CAvi();
// Inherited from IAppSystem
virtual bool Connect( CreateInterfaceFn factory );
virtual void *QueryInterface( const char *pInterfaceName );
virtual InitReturnVal_t Init();
virtual void Shutdown();
// Inherited from IAvi
virtual void SetMainWindow( void* hWnd );
virtual AVIHandle_t StartAVI( const AVIParams_t& params );
virtual void FinishAVI( AVIHandle_t h );
virtual void AppendMovieSound( AVIHandle_t h, short *buf, size_t bufsize );
virtual void AppendMovieFrame( AVIHandle_t h, const BGR888_t *pRGBData );
virtual AVIMaterial_t CreateAVIMaterial( const char *pMaterialName, const char *pFileName, const char *pPathID );
virtual void DestroyAVIMaterial( AVIMaterial_t hMaterial );
virtual void SetTime( AVIMaterial_t hMaterial, float flTime );
virtual IMaterial* GetMaterial( AVIMaterial_t hMaterial );
virtual void GetTexCoordRange( AVIMaterial_t hMaterial, float *pMaxU, float *pMaxV );
virtual void GetFrameSize( AVIMaterial_t hMaterial, int *pWidth, int *pHeight );
virtual int GetFrameRate( AVIMaterial_t hMaterial );
virtual void SetFrame( AVIMaterial_t hMaterial, float flFrame );
virtual int GetFrameCount( AVIMaterial_t hMaterial );
private:
HWND m_hWnd;
CUtlLinkedList< CAviFile, AVIHandle_t > m_AVIFiles;
// NOTE: Have to use pointers here since AVIMaterials inherit from ITextureRegenerator
// The realloc screws up the pointers held to ITextureRegenerators in the material system.
CUtlLinkedList< CAVIMaterial*, AVIMaterial_t > m_AVIMaterials;
};
//-----------------------------------------------------------------------------
// Singleton
//-----------------------------------------------------------------------------
static CAvi g_AVI;
EXPOSE_SINGLE_INTERFACE_GLOBALVAR( CAvi, IAvi, AVI_INTERFACE_VERSION, g_AVI );
//-----------------------------------------------------------------------------
// Constructor/destructor
//-----------------------------------------------------------------------------
CAvi::CAvi()
{
m_hWnd = NULL;
}
//-----------------------------------------------------------------------------
// Connect/disconnect
//-----------------------------------------------------------------------------
bool CAvi::Connect( CreateInterfaceFn factory )
{
if ( !BaseClass::Connect( factory ) )
return false;
if ( !( g_pFullFileSystem && materials ) )
{
Msg( "Avi failed to connect to a required system\n" );
}
return ( g_pFullFileSystem && materials );
}
//-----------------------------------------------------------------------------
// Query Interface
//-----------------------------------------------------------------------------
void *CAvi::QueryInterface( const char *pInterfaceName )
{
if (!Q_strncmp( pInterfaceName, AVI_INTERFACE_VERSION, Q_strlen(AVI_INTERFACE_VERSION) + 1))