forked from nillerusr/source-engine
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathquicktime_material.cpp
962 lines (735 loc) · 26.7 KB
/
quicktime_material.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
//========= Copyright Valve Corporation, All rights reserved. ============//
//
// Purpose:
//
//=============================================================================
#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"
#include "platform.h"
#include "quicktime_material.h"
#if defined ( WIN32 )
#include <WinDef.h>
#include <../dx9sdk/include/dsound.h>
#endif
#include "tier0/memdbgon.h"
// ===========================================================================
// CQuicktimeMaterialRGBTextureRegenerator - Inherited from ITextureRegenerator
// Copies and converts the buffer bits to texture bits
// Currently only supports 32-bit BGRA
// ===========================================================================
CQuicktimeMaterialRGBTextureRegenerator::CQuicktimeMaterialRGBTextureRegenerator() :
m_SrcGWorld( nullptr ),
m_nSourceWidth( 0 ),
m_nSourceHeight( 0 )
{
}
CQuicktimeMaterialRGBTextureRegenerator::~CQuicktimeMaterialRGBTextureRegenerator()
{
// nothing to do
}
void CQuicktimeMaterialRGBTextureRegenerator::SetSourceGWorld( GWorldPtr theGWorld, int nWidth, int nHeight )
{
m_SrcGWorld = theGWorld;
m_nSourceWidth = nWidth;
m_nSourceHeight = nHeight;
}
void CQuicktimeMaterialRGBTextureRegenerator::RegenerateTextureBits( ITexture *pTexture, IVTFTexture *pVTFTexture, Rect_t *pRect )
{
AssertExit( pVTFTexture != nullptr );
// Error condition, should only have 1 frame, 1 face, 1 mip level
if ( ( pVTFTexture->FrameCount() > 1 ) || ( pVTFTexture->FaceCount() > 1 ) || ( pVTFTexture->MipCount() > 1 ) || ( pVTFTexture->Depth() > 1 ) )
{
WarningAssert( "Texture Properties Incorrect ");
memset( pVTFTexture->ImageData(), 0xAA, pVTFTexture->ComputeTotalSize() );
return;
}
// Make sure we have a valid video image source
if ( m_SrcGWorld == nullptr )
{
WarningAssert( "Video texture source not set" );
memset( pVTFTexture->ImageData(), 0xCC, pVTFTexture->ComputeTotalSize() );
return;
}
// Verify the destination texture is set up correctly
Assert( pVTFTexture->Format() == IMAGE_FORMAT_BGRA8888 );
Assert( pVTFTexture->RowSizeInBytes( 0 ) >= pVTFTexture->Width() * 4 );
Assert( pVTFTexture->Width() >= m_nSourceWidth );
Assert( pVTFTexture->Height() >= m_nSourceHeight );
// Copy directly from the Quicktime GWorld
PixMapHandle thePixMap = GetGWorldPixMap( m_SrcGWorld );
if ( LockPixels( thePixMap ) )
{
BYTE *pImageData = pVTFTexture->ImageData();
int dstStride = pVTFTexture->RowSizeInBytes( 0 );
BYTE *pSrcData = (BYTE*) GetPixBaseAddr( thePixMap );
long srcStride = QTGetPixMapHandleRowBytes( thePixMap );
int rowSize = m_nSourceWidth * 4;
for (int y = 0; y < m_nSourceHeight; y++ )
{
memcpy( pImageData, pSrcData, rowSize );
pImageData+= dstStride;
pSrcData+= srcStride;
}
UnlockPixels( thePixMap );
}
else
{
WarningAssert( "LockPixels Failed" );
}
}
void CQuicktimeMaterialRGBTextureRegenerator::Release()
{
// we don't invoke the destructor here, we're not using the no-release extensions
}
// ===========================================================================
// CQuickTimeMaterial class - creates a material, opens a QuickTime movie
// and plays the movie onto the material
// ===========================================================================
//-----------------------------------------------------------------------------
// CQuickTimeMaterial Constructor
//-----------------------------------------------------------------------------
CQuickTimeMaterial::CQuickTimeMaterial() :
m_pFileName( nullptr ),
m_MovieGWorld( nullptr ),
m_QTMovie( nullptr ),
m_AudioContext( nullptr ),
m_bInitCalled( false )
{
Reset();
}
//-----------------------------------------------------------------------------
// CQuickTimeMaterial Destructor
//-----------------------------------------------------------------------------
CQuickTimeMaterial::~CQuickTimeMaterial()
{
Reset();
}
void CQuickTimeMaterial::Reset()
{
SetQTFileName( nullptr );
DestroyProceduralTexture();
DestroyProceduralMaterial();
m_TexCordU = 0.0f;
m_TexCordV = 0.0f;
m_VideoFrameWidth = 0;
m_VideoFrameHeight = 0;
m_PlaybackFlags = VideoPlaybackFlags::NO_PLAYBACK_OPTIONS;
m_bMovieInitialized = false;
m_bMoviePlaying = false;
m_bMovieFinishedPlaying = false;
m_bMoviePaused = false;
m_bLoopMovie = false;
m_bHasAudio = false;
m_bMuted = false;
m_CurrentVolume = 0.0f;
m_QTMovieTimeScale = 0;
m_QTMovieDuration = 0;
m_QTMovieDurationinSec = 0.0f;
m_QTMovieFrameRate.SetFPS( 0, false );
SAFE_RELEASE_AUDIOCONTEXT( m_AudioContext );
SAFE_DISPOSE_GWORLD( m_MovieGWorld );
SAFE_DISPOSE_MOVIE( m_QTMovie );
m_LastResult = VideoResult::SUCCESS;
}
void CQuickTimeMaterial::SetQTFileName( const char *theQTMovieFileName )
{
SAFE_DELETE_ARRAY( m_pFileName );
if ( theQTMovieFileName != nullptr )
{
AssertMsg( V_strlen( theQTMovieFileName ) <= MAX_QT_FILENAME_LEN, "Bad Quicktime Movie Filename" );
m_pFileName = COPY_STRING( theQTMovieFileName );
}
}
VideoResult_t CQuickTimeMaterial::SetResult( VideoResult_t status )
{
m_LastResult = status;
return status;
}
//-----------------------------------------------------------------------------
// Video information functions
//-----------------------------------------------------------------------------
//-----------------------------------------------------------------------------
// Returns the resolved filename of the video, as it might differ from
// what the user supplied, (also with absolute path)
//-----------------------------------------------------------------------------
const char *CQuickTimeMaterial::GetVideoFileName()
{
return m_pFileName;
}
VideoFrameRate_t &CQuickTimeMaterial::GetVideoFrameRate()
{
return m_QTMovieFrameRate;
}
VideoResult_t CQuickTimeMaterial::GetLastResult()
{
return m_LastResult;
}
//-----------------------------------------------------------------------------
// Audio Functions
//-----------------------------------------------------------------------------
bool CQuickTimeMaterial::HasAudio()
{
return m_bHasAudio;
}
bool CQuickTimeMaterial::SetVolume( float fVolume )
{
clamp( fVolume, 0.0f, 1.0f );
m_CurrentVolume = fVolume;
if ( m_AudioContext != nullptr && m_bHasAudio )
{
short movieVolume = (short) ( m_CurrentVolume * 256.0f );
SetMovieVolume( m_QTMovie, movieVolume );
SetResult( VideoResult::SUCCESS );
return true;
}
SetResult( VideoResult::AUDIO_ERROR_OCCURED );
return false;
}
float CQuickTimeMaterial::GetVolume()
{
return m_CurrentVolume;
}
void CQuickTimeMaterial::SetMuted( bool bMuteState )
{
AssertExitFunc( m_bMoviePlaying, SetResult( VideoResult::OPERATION_OUT_OF_SEQUENCE) );
SetResult( VideoResult::SUCCESS );
if ( bMuteState == m_bMuted ) // no change?
{
return;
}
m_bMuted = bMuteState;
if ( m_bHasAudio )
{
OSStatus result = SetMovieAudioMute( m_QTMovie, m_bMuted, 0 );
AssertExitFunc( result == noErr, SetResult( VideoResult::AUDIO_ERROR_OCCURED) );
}
SetResult( VideoResult::SUCCESS );
}
bool CQuickTimeMaterial::IsMuted()
{
return m_bMuted;
}
VideoResult_t CQuickTimeMaterial::SoundDeviceCommand( VideoSoundDeviceOperation_t operation, void *pDevice, void *pData )
{
AssertExitV( m_bMovieInitialized || m_bMoviePlaying, VideoResult::OPERATION_OUT_OF_SEQUENCE );
switch( operation )
{
// On win32, we try and create an audio context from a GUID
case VideoSoundDeviceOperation::SET_DIRECT_SOUND_DEVICE:
{
#if defined ( WIN32 )
SAFE_RELEASE_AUDIOCONTEXT( m_AudioContext );
return ( CreateMovieAudioContext( m_bHasAudio, m_QTMovie, &m_AudioContext ) ? SetResult( VideoResult::SUCCESS ) : SetResult( VideoResult::AUDIO_ERROR_OCCURED ) );
#else
// On any other OS, we don't support this operation
return SetResult( VideoResult::OPERATION_NOT_SUPPORTED );
#endif
}
case VideoSoundDeviceOperation::SET_SOUND_MANAGER_DEVICE:
{
#if defined ( OSX )
SAFE_RELEASE_AUDIOCONTEXT( m_AudioContext );
return ( CreateMovieAudioContext( m_bHasAudio, m_QTMovie, &m_AudioContext ) ? SetResult( VideoResult::SUCCESS ) : SetResult( VideoResult::AUDIO_ERROR_OCCURED ) );
#else
// On any other OS, we don't support this operation
return SetResult( VideoResult::OPERATION_NOT_SUPPORTED );
#endif
}
case VideoSoundDeviceOperation::SET_LIB_AUDIO_DEVICE:
case VideoSoundDeviceOperation::HOOK_X_AUDIO:
case VideoSoundDeviceOperation::SET_MILES_SOUND_DEVICE:
{
return SetResult( VideoResult::OPERATION_NOT_SUPPORTED );
}
default:
{
return SetResult( VideoResult::BAD_INPUT_PARAMETERS );
}
}
}
//-----------------------------------------------------------------------------
// Initializes the video material
//-----------------------------------------------------------------------------
bool CQuickTimeMaterial::Init( const char *pMaterialName, const char *pFileName, VideoPlaybackFlags_t flags )
{
SetResult( VideoResult::BAD_INPUT_PARAMETERS );
AssertExitF( IS_NOT_EMPTY( pFileName ) );
AssertExitF( m_bInitCalled == false );
m_PlaybackFlags = flags;
OpenQTMovie( pFileName ); // Open up the Quicktime file
if ( !m_bMovieInitialized )
{
return false; // Something bad happened when we went to open
}
// Now we can properly setup our regenerators
m_TextureRegen.SetSourceGWorld( m_MovieGWorld, m_VideoFrameWidth, m_VideoFrameHeight );
CreateProceduralTexture( pMaterialName );
CreateProceduralMaterial( pMaterialName );
// Start movie playback
if ( !BITFLAGS_SET( m_PlaybackFlags, VideoPlaybackFlags::DONT_AUTO_START_VIDEO ) )
{
StartVideo();
}
m_bInitCalled = true; // Look, if you only got one shot...
return true;
}
void CQuickTimeMaterial::Shutdown( void )
{
StopVideo();
Reset();
}
//-----------------------------------------------------------------------------
// Video playback state functions
//-----------------------------------------------------------------------------
bool CQuickTimeMaterial::IsVideoReadyToPlay()
{
return m_bMovieInitialized;
}
bool CQuickTimeMaterial::IsVideoPlaying()
{
return m_bMoviePlaying;
}
//-----------------------------------------------------------------------------
// Checks to see if the video has a new frame ready to be rendered and
// downloaded into the texture and eventually display
//-----------------------------------------------------------------------------
bool CQuickTimeMaterial::IsNewFrameReady( void )
{
// Are we waiting to start playing the first frame? if so, tell them we are ready!
if ( m_bMovieInitialized == true )
{
return true;
}
// We better be playing the movie
AssertExitF( m_bMoviePlaying );
// paused?
if ( m_bMoviePaused )
{
return false;
}
TimeValue curMovieTime = GetMovieTime( m_QTMovie, nullptr );
if ( curMovieTime >= m_QTMovieDuration || m_NextInterestingTimeToPlay == NO_MORE_INTERESTING_TIMES )
{
// if we are looping, we have another frame, otherwise no
return m_bLoopMovie;
}
// Enough time passed to get to next frame??
if ( curMovieTime < m_NextInterestingTimeToPlay )
{
// nope.. use the previous frame
return false;
}
// we have a new frame we want then..
return true;
}
bool CQuickTimeMaterial::IsFinishedPlaying()
{
return m_bMovieFinishedPlaying;
}
void CQuickTimeMaterial::SetLooping( bool bLoopVideo )
{
m_bLoopMovie = bLoopVideo;
}
bool CQuickTimeMaterial::IsLooping()
{
return m_bLoopMovie;
}
void CQuickTimeMaterial::SetPaused( bool bPauseState )
{
if ( !m_bMoviePlaying || m_bMoviePaused == bPauseState )
{
Assert( m_bMoviePlaying );
return;
}
if ( bPauseState ) // Pausing the movie?
{
// Save off current time and set paused state
m_MoviePauseTime = GetMovieTime( m_QTMovie, nullptr );
StopMovie( m_QTMovie );
}
else // unpausing the movie
{
// Reset the movie to the paused time
SetMovieTimeValue( m_QTMovie, m_MoviePauseTime );
StartMovie( m_QTMovie );
Assert( GetMoviesError() == noErr );
}
m_bMoviePaused = bPauseState;
}
bool CQuickTimeMaterial::IsPaused()
{
return ( m_bMoviePlaying ) ? m_bMoviePaused : false;
}
// Begins playback of the movie
bool CQuickTimeMaterial::StartVideo()
{
if ( !m_bMovieInitialized )
{
Assert( false );
SetResult( VideoResult::OPERATION_ALREADY_PERFORMED );
return false;
}
// Start the movie playing at the first frame
SetMovieTimeValue( m_QTMovie, m_MovieFirstFrameTime );
Assert( GetMoviesError() == noErr );
StartMovie( m_QTMovie );
Assert( GetMoviesError() == noErr );
// Transition to playing state
m_bMovieInitialized = false;
m_bMoviePlaying = true;
// Deliberately set the next interesting time to the current time to
// insure that the ::update() call causes the textures to be downloaded
m_NextInterestingTimeToPlay = m_MovieFirstFrameTime;
Update();
return true;
}
// stops movie for good, frees resources, but retains texture & material of last frame rendered
bool CQuickTimeMaterial::StopVideo()
{
if ( !m_bMoviePlaying )
{
SetResult( VideoResult::OPERATION_OUT_OF_SEQUENCE );
return false;
}
StopMovie( m_QTMovie );
m_bMoviePlaying = false;
m_bMoviePaused = false;
m_bMovieFinishedPlaying = true;
// free resources
CloseQTFile();
SetResult( VideoResult::SUCCESS );
return true;
}
//-----------------------------------------------------------------------------
// Purpose: Updates our scene
// Output : true = movie playing ok, false = time to end movie
// supposed to be: Returns true on a new frame of video being downloaded into the texture
//-----------------------------------------------------------------------------
bool CQuickTimeMaterial::Update( void )
{
AssertExitF( m_bMoviePlaying );
OSType qTypes[1] = { VisualMediaCharacteristic };
// are we paused? can't update if so...
if ( m_bMoviePaused )
{
return true; // reuse the last frame
}
// Get current time in the movie
TimeValue curMovieTime = GetMovieTime( m_QTMovie, nullptr );
// Did we hit the end of the movie?
if ( curMovieTime >= m_QTMovieDuration )
{
// If we're not looping, then report that we are done updating
if ( m_bLoopMovie == false )
{
StopVideo();
return false;
}
// Reset the movie to the start time
SetMovieTimeValue( m_QTMovie, m_MovieFirstFrameTime );
AssertExitF( GetMoviesError() == noErr );
// Assure fall through to render a new frame
m_NextInterestingTimeToPlay = m_MovieFirstFrameTime;
}
// Are we on the last frame of the movie? (but not past the end of any audio?)
if ( m_NextInterestingTimeToPlay == NO_MORE_INTERESTING_TIMES )
{
return true; // reuse last frame
}
// Enough time passed to get to next frame?
if ( curMovieTime < m_NextInterestingTimeToPlay )
{
// nope.. use the previous frame
return true;
}
// move the movie along
UpdateMovie( m_QTMovie );
AssertExitF( GetMoviesError() == noErr );
// Let QuickTime render the frame
MoviesTask( m_QTMovie, 0L );
AssertExitF( GetMoviesError() == noErr );
// Get the next frame after the current time (the movie may have advanced a bit during UpdateMovie() and MovieTasks()
GetMovieNextInterestingTime( m_QTMovie, nextTimeStep | nextTimeEdgeOK, 1, qTypes, GetMovieTime( m_QTMovie, nullptr ), fixed1, &m_NextInterestingTimeToPlay, nullptr );
// hit the end of the movie?
if ( GetMoviesError() == invalidTime || m_NextInterestingTimeToPlay == END_OF_QUICKTIME_MOVIE )
{
m_NextInterestingTimeToPlay = NO_MORE_INTERESTING_TIMES;
}
// Regenerate our texture, it'll grab from the GWorld Directly
m_Texture->Download();
SetResult( VideoResult::SUCCESS );
return true;
}
//-----------------------------------------------------------------------------
// Returns the material
//-----------------------------------------------------------------------------
IMaterial *CQuickTimeMaterial::GetMaterial()
{
return m_Material;
}
//-----------------------------------------------------------------------------
// Returns the texcoord range
//-----------------------------------------------------------------------------
void CQuickTimeMaterial::GetVideoTexCoordRange( float *pMaxU, float *pMaxV )
{
AssertExit( pMaxU != nullptr && pMaxV != nullptr );
if ( m_Texture == nullptr ) // no texture?
{
*pMaxU = *pMaxV = 1.0f;
return;
}
*pMaxU = m_TexCordU;
*pMaxV = m_TexCordV;
}
//-----------------------------------------------------------------------------
// Returns the frame size of the QuickTime Video in pixels
//-----------------------------------------------------------------------------
void CQuickTimeMaterial::GetVideoImageSize( int *pWidth, int *pHeight )
{
Assert( pWidth != nullptr && pHeight != nullptr );
*pWidth = m_VideoFrameWidth;
*pHeight = m_VideoFrameHeight;
}
float CQuickTimeMaterial::GetVideoDuration()
{
return m_QTMovieDurationinSec;
}
int CQuickTimeMaterial::GetFrameCount()
{
return m_QTMovieFrameCount;
}
//-----------------------------------------------------------------------------
// Sets the frame for an QuickTime Material (use instead of SetTime)
//-----------------------------------------------------------------------------
bool CQuickTimeMaterial::SetFrame( int FrameNum )
{
if ( !m_bMoviePlaying )
{
Assert( false );
SetResult( VideoResult::OPERATION_OUT_OF_SEQUENCE );
return false;
}
float theTime = (float) FrameNum * m_QTMovieFrameRate.GetFPS();
return SetTime( theTime );
}
int CQuickTimeMaterial::GetCurrentFrame()
{
AssertExitV( m_bMoviePlaying, -1 );
TimeValue curTime = m_bMoviePaused ? m_MoviePauseTime : GetMovieTime( m_QTMovie, nullptr );
return curTime / m_QTMovieFrameRate.GetUnitsPerFrame();
}
float CQuickTimeMaterial::GetCurrentVideoTime()
{
AssertExitV( m_bMoviePlaying, -1.0f );
TimeValue curTime = m_bMoviePaused ? m_MoviePauseTime : GetMovieTime( m_QTMovie, nullptr );
return curTime / m_QTMovieFrameRate.GetUnitsPerSecond();
}
bool CQuickTimeMaterial::SetTime( float flTime )
{
AssertExitF( m_bMoviePlaying );
AssertExitF( flTime >= 0 && flTime < m_QTMovieDurationinSec );
TimeValue newTime = (TimeValue) ( flTime * m_QTMovieFrameRate.GetUnitsPerSecond() + 0.5f) ;
clamp( newTime, m_MovieFirstFrameTime, m_QTMovieDuration );
// Are we paused?
if ( m_bMoviePaused )
{
m_MoviePauseTime = newTime;
return true;
}
TimeValue curMovieTime = GetMovieTime( m_QTMovie, nullptr );
// Don't stop and reset movie if we are within 1 frame of the requested time
if ( newTime <= curMovieTime - m_QTMovieFrameRate.GetUnitsPerFrame() || newTime >= curMovieTime + m_QTMovieFrameRate.GetUnitsPerFrame() )
{
// Reset the movie to the requested time
StopMovie( m_QTMovie );
SetMovieTimeValue( m_QTMovie, newTime );
StartMovie( m_QTMovie );
Assert( GetMoviesError() == noErr );
}
return true;
}
//-----------------------------------------------------------------------------
// Initializes, shuts down the procedural texture
//-----------------------------------------------------------------------------
void CQuickTimeMaterial::CreateProceduralTexture( const char *pTextureName )
{
AssertIncRange( m_VideoFrameWidth, cMinVideoFrameWidth, cMaxVideoFrameWidth );
AssertIncRange( m_VideoFrameHeight, cMinVideoFrameHeight, cMaxVideoFrameHeight );
AssertStr( pTextureName );
// Either make the texture the same dimensions as the video,
// or choose power-of-two textures which are at least as big as the video
bool actualSizeTexture = BITFLAGS_SET( m_PlaybackFlags, VideoPlaybackFlags::TEXTURES_ACTUAL_SIZE );
int nWidth = ( actualSizeTexture ) ? ALIGN_VALUE( m_VideoFrameWidth, TEXTURE_SIZE_ALIGNMENT ) : ComputeGreaterPowerOfTwo( m_VideoFrameWidth );
int nHeight = ( actualSizeTexture ) ? ALIGN_VALUE( m_VideoFrameHeight, TEXTURE_SIZE_ALIGNMENT ) : ComputeGreaterPowerOfTwo( m_VideoFrameHeight );
// initialize the procedural texture as 32-it RGBA, w/o mipmaps
m_Texture.InitProceduralTexture( pTextureName, "VideoCacheTextures", nWidth, nHeight,
IMAGE_FORMAT_BGRA8888, TEXTUREFLAGS_CLAMPS | TEXTUREFLAGS_CLAMPT | TEXTUREFLAGS_NOMIP |
TEXTUREFLAGS_PROCEDURAL | TEXTUREFLAGS_SINGLECOPY | TEXTUREFLAGS_NOLOD );
// Use this to get the updated frame from the remote connection
m_Texture->SetTextureRegenerator( &m_TextureRegen /* , false */ );
// compute the texcoords
int nTextureWidth = m_Texture->GetActualWidth();
int nTextureHeight = m_Texture->GetActualHeight();
m_TexCordU = ( nTextureWidth > 0 ) ? (float) m_VideoFrameWidth / (float) nTextureWidth : 0.0f;
m_TexCordV = ( nTextureHeight > 0 ) ? (float) m_VideoFrameHeight / (float) nTextureHeight : 0.0f;
}
void CQuickTimeMaterial::DestroyProceduralTexture()
{
if ( m_Texture != nullptr )
{
// DO NOT Call release on the Texture Regenerator, as it will destroy this object! bad bad bad
// instead we tell it to assign a NULL regenerator and flag it to not call release
m_Texture->SetTextureRegenerator( nullptr /*, false */ );
// Texture, texture go away...
m_Texture.Shutdown( true );
}
}
//-----------------------------------------------------------------------------
// Initializes, shuts down the procedural material
//-----------------------------------------------------------------------------
void CQuickTimeMaterial::CreateProceduralMaterial( const char *pMaterialName )
{
// create keyvalues if necessary
KeyValues *pVMTKeyValues = new KeyValues( "UnlitGeneric" );
{
pVMTKeyValues->SetString( "$basetexture", m_Texture->GetName() );
pVMTKeyValues->SetInt( "$nobasetexture", 1 );
pVMTKeyValues->SetInt( "$nofog", 1 );
pVMTKeyValues->SetInt( "$spriteorientation", 3 );
pVMTKeyValues->SetInt( "$translucent", 1 );
pVMTKeyValues->SetInt( "$nolod", 1 );
pVMTKeyValues->SetInt( "$nomip", 1 );
pVMTKeyValues->SetInt( "$gammacolorread", 0 );
}
// FIXME: gak, this is backwards. Why doesn't the material just see that it has a funky basetexture?
m_Material.Init( pMaterialName, pVMTKeyValues );
m_Material->Refresh();
}
void CQuickTimeMaterial::DestroyProceduralMaterial()
{
// Store the internal material pointer for later use
IMaterial *pMaterial = m_Material;
m_Material.Shutdown();
materials->UncacheUnusedMaterials();
// Now be sure to free that material because we don't want to reference it again later, we'll recreate it!
if ( pMaterial != nullptr )
{
pMaterial->DeleteIfUnreferenced();
}
}
//-----------------------------------------------------------------------------
// Opens a movie file using quicktime
//-----------------------------------------------------------------------------
void CQuickTimeMaterial::OpenQTMovie( const char *theQTMovieFileName )
{
AssertExit( IS_NOT_EMPTY( theQTMovieFileName ) );
// Set graphics port
#if defined ( WIN32 )
SetGWorld ( (CGrafPtr) GetNativeWindowPort( nil ), nil );
#elif defined ( OSX )
SetGWorld( nil, nil );
#endif
SetQTFileName( theQTMovieFileName );
Handle MovieFileDataRef = nullptr;
OSType MovieFileDataRefType = 0;
CFStringRef imageStrRef = CFStringCreateWithCString ( NULL, theQTMovieFileName, 0 );
AssertExitFunc( imageStrRef != nullptr, SetResult( VideoResult::SYSTEM_ERROR_OCCURED ) );
OSErr status = QTNewDataReferenceFromFullPathCFString( imageStrRef, (QTPathStyle) kQTNativeDefaultPathStyle, 0, &MovieFileDataRef, &MovieFileDataRefType );
AssertExitFunc( status == noErr, SetResult( VideoResult::FILE_ERROR_OCCURED ) );
CFRelease( imageStrRef );
status = NewMovieFromDataRef( &m_QTMovie, newMovieActive, nil, MovieFileDataRef, MovieFileDataRefType );
SAFE_DISPOSE_HANDLE( MovieFileDataRef );
if ( status != noErr )
{
Assert( false );
Reset();
SetResult( VideoResult::VIDEO_ERROR_OCCURED );
return;
}
// disabling audio?
if ( BITFLAGS_SET( m_PlaybackFlags, VideoPlaybackFlags::NO_AUDIO ) )
{
m_bHasAudio = false;
}
else
{
// does movie have audio?
Track audioTrack = GetMovieIndTrackType( m_QTMovie, 1, SoundMediaType, movieTrackMediaType );
m_bHasAudio = ( audioTrack != nullptr );
}
// Now we need to extract the time info from the QT Movie
m_QTMovieTimeScale = GetMovieTimeScale( m_QTMovie );
m_QTMovieDuration = GetMovieDuration( m_QTMovie );
// compute movie duration
m_QTMovieDurationinSec = float ( double( m_QTMovieDuration ) / double( m_QTMovieTimeScale ) );
if ( !MovieGetStaticFrameRate( m_QTMovie, m_QTMovieFrameRate ) )
{
WarningAssert( "Couldn't Get Frame Rate" );
}
// and get an estimated frame count
m_QTMovieFrameCount = m_QTMovieDuration / m_QTMovieTimeScale;
if ( m_QTMovieFrameRate.GetUnitsPerSecond() == m_QTMovieTimeScale )
{
m_QTMovieFrameCount = m_QTMovieDuration / m_QTMovieFrameRate.GetUnitsPerFrame();
}
else
{
m_QTMovieFrameCount = (int) ( (float) m_QTMovieDurationinSec * m_QTMovieFrameRate.GetFPS() + 0.5f );
}
// what size do we set the output rect to?
GetMovieNaturalBoundsRect(m_QTMovie, &m_QTMovieRect);
m_VideoFrameWidth = m_QTMovieRect.right;
m_VideoFrameHeight = m_QTMovieRect.bottom;
// Sanity check...
AssertExitFunc( m_QTMovieRect.top == 0 && m_QTMovieRect.left == 0 &&
m_QTMovieRect.right >= cMinVideoFrameWidth && m_QTMovieRect.right <= cMaxVideoFrameWidth &&
m_QTMovieRect.bottom >= cMinVideoFrameHeight && m_QTMovieRect.bottom <= cMaxVideoFrameHeight &&
m_QTMovieRect.right % 4 == 0,
SetResult( VideoResult::VIDEO_ERROR_OCCURED ) );
// Setup the QuiuckTime Graphics World for the Movie
status = QTNewGWorld( &m_MovieGWorld, k32BGRAPixelFormat, &m_QTMovieRect, nil, nil, 0 );
AssertExit( status == noErr );
// Setup the playback gamma according to the convar
SetGWorldDecodeGamma( m_MovieGWorld, VideoPlaybackGamma::USE_GAMMA_CONVAR );
// Assign the GWorld to this movie
SetMovieGWorld( m_QTMovie, m_MovieGWorld, nil );
// Setup Movie Audio, unless suppressed
if ( !CreateMovieAudioContext( m_bHasAudio, m_QTMovie, &m_AudioContext, true, &m_CurrentVolume ) )
{
SetResult( VideoResult::AUDIO_ERROR_OCCURED );
WarningAssert( "Couldn't Set Audio" );
}
// Get the time of the first frame
OSType qTypes[1] = { VisualMediaCharacteristic };
short qFlags = nextTimeStep | nextTimeEdgeOK; // use nextTimeStep instead of nextTimeMediaSample for MPEG 1-2 compatibility
GetMovieNextInterestingTime( m_QTMovie, qFlags, 1, qTypes, (TimeValue) 0, fixed1, &m_MovieFirstFrameTime, NULL );
AssertExitFunc( GetMoviesError() == noErr, SetResult( VideoResult::VIDEO_ERROR_OCCURED ) );
// Preroll the movie
if ( BITFLAGS_SET( m_PlaybackFlags, VideoPlaybackFlags::PRELOAD_VIDEO ) )
{
Fixed playRate = GetMoviePreferredRate( m_QTMovie );
status = PrerollMovie( m_QTMovie, m_MovieFirstFrameTime, playRate );
AssertExitFunc( status == noErr, SetResult( VideoResult::VIDEO_ERROR_OCCURED ) );
}
m_bMovieInitialized = true;
}
void CQuickTimeMaterial::CloseQTFile()
{
if ( m_QTMovie == nullptr )
{
return;
}
SAFE_RELEASE_AUDIOCONTEXT( m_AudioContext );
SAFE_DISPOSE_GWORLD( m_MovieGWorld );
SAFE_DISPOSE_MOVIE( m_QTMovie );
SetQTFileName( nullptr );
}