aboutsummaryrefslogtreecommitdiff
path: root/engines/wintermute/video/VidTheoraPlayer.cpp
blob: fd1b7cea2b44d4e7c9c867214af6a27c60c1cbd1 (plain)
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
// Copyright 2009, 2010 Jan Nedoma
//
// This file is part of Wintermute Engine.
//
// Wintermute Engine is free software: you can redistribute it and/or modify
// it under the terms of the GNU Lesser General Public License as published by
// the Free Software Foundation, either version 3 of the License, or
// (at your option) any later version.
//
// Wintermute Engine 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 Lesser General Public License for more details.
//
// You should have received a copy of the GNU Lesser General Public License
// along with Wintermute Engine.  If not, see <http://www.gnu.org/licenses/>.
//////////////////////////////////////////////////////////////////////////


#include "engines/wintermute/dcgf.h"
#include "engines/wintermute/video/VidTheoraPlayer.h"
#include "engines/wintermute/Base/BGame.h"
#include "engines/wintermute/Base/BFileManager.h"
#include "engines/wintermute/Base/BSurfaceSDL.h"
#include "engines/wintermute/Base/BSoundMgr.h"
#include "engines/wintermute/utils/utils.h"
#include "engines/wintermute/PlatformSDL.h"
#include "engines/wintermute/video/decoders/theora_decoder.h"
#include "common/system.h"
//#pragma comment(lib, "libtheora.lib")

namespace WinterMute {

//IMPLEMENT_PERSISTENT(CVidTheoraPlayer, false);

//////////////////////////////////////////////////////////////////////////
CVidTheoraPlayer::CVidTheoraPlayer(CBGame *inGame): CBBase(inGame) {
	SetDefaults();
}

//////////////////////////////////////////////////////////////////////////
void CVidTheoraPlayer::SetDefaults() {

	_file = NULL;
	_filename = "";
	_startTime = 0;
	_looping = false;

	_freezeGame = false;
	_currentTime = 0;

	_state = THEORA_STATE_NONE;

	_videoFrameReady = false;
	_audioFrameReady = false;
	_videobufTime = 0;

	_playbackStarted = false;
	_dontDropFrames = false;

	_texture = NULL;
	_alphaImage = NULL;
	_alphaFilename = NULL;

	_frameRendered = false;

	_seekingKeyframe = false;
	_timeOffset = 0.0f;
	
	_posX = _posY = 0;
	_playbackType = VID_PLAY_CENTER;
	_playZoom = 0.0f;
	
	_savedState = THEORA_STATE_NONE;
	_savedPos = 0;
	_volume = 100;
#if 0
	_vorbisStreams = _theoraStreams = 0;

	GenLookupTables();

	_subtitler = NULL;
#endif
}

//////////////////////////////////////////////////////////////////////////
CVidTheoraPlayer::~CVidTheoraPlayer(void) {
	cleanup();

/*	SAFE_DELETE_ARRAY(_filename);
	SAFE_DELETE_ARRAY(_alphaFilename);
	SAFE_DELETE(_texture);
	SAFE_DELETE(_alphaImage);*/
//	SAFE_DELETE(_subtitler);
}

//////////////////////////////////////////////////////////////////////////
void CVidTheoraPlayer::cleanup() {
#if 0
	if (m_Sound) {
		Game->m_SoundMgr->RemoveSound(m_Sound);
		m_Sound = NULL;
	}

	SAFE_DELETE_ARRAY(m_AudioBuf);
	m_AudioBufFill = 0;
	m_AudioBufSize = 0;
#endif
}

//////////////////////////////////////////////////////////////////////////
HRESULT CVidTheoraPlayer::initialize(const Common::String &filename, const Common::String &subtitleFile) {
	cleanup();
	
	_file = Game->_fileManager->OpenFile(filename, true, false);
	if (!_file) return E_FAIL;
	
	//if (Filename != _filename) CBUtils::SetString(&_filename, Filename);
#if defined (USE_THEORADEC)
	_theoraDecoder = new TheoraDecoder();
#else
	return E_FAIL;
#endif
	_theoraDecoder->loadStream(_file);
	
	if (!_theoraDecoder->isVideoLoaded())
		return E_FAIL;
	
	_state = THEORA_STATE_PAUSED;

	// Additional setup.
	_surface.create(_theoraDecoder->getWidth(), _theoraDecoder->getHeight(), _theoraDecoder->getPixelFormat());
	_texture = new CBSurfaceSDL(Game);
	_texture->create(_theoraDecoder->getWidth(), _theoraDecoder->getHeight());
	_state = THEORA_STATE_PLAYING;
	_playZoom = 100;
	
	return S_OK;
#if 0
	Cleanup();

	_file = Game->_fileManager->OpenFile(Filename);
	if (!_file) return E_FAIL;

	if (Filename != _filename) CBUtils::SetString(&_filename, Filename);

	// start up Ogg stream synchronization layer
	ogg_sync_init(&m_OggSyncState);

	// init supporting Vorbis structures needed in header parsing
	vorbis_comment_init(&m_VorbisComment);
	vorbis_info_init(&m_VorbisInfo);

	// init supporting Theora structures needed in header parsing
	theora_comment_init(&m_TheoraComment);
	theora_info_init(&m_TheoraInfo);



	// Ogg file open; parse the headers
	// Only interested in Vorbis/Theora streams
	ogg_packet TempOggPacket;
	bool IsDone = false;
	while (!IsDone) {
		int BytesRead = BufferData(&m_OggSyncState);
		if (BytesRead == 0) break;

		while (ogg_sync_pageout(&m_OggSyncState, &m_OggPage) > 0) {
			ogg_stream_state OggStateTest;

			// is this a mandated initial header? If not, stop parsing
			if (!ogg_page_bos(&m_OggPage)) {
				// don't leak the page; get it into the appropriate stream
				if (m_TheoraStreams)
					ogg_stream_pagein(&m_TheoraStreamState, &m_OggPage);
				if (m_VorbisStreams)
					ogg_stream_pagein(&m_VorbisStreamState, &m_OggPage);

				IsDone = true;
				break;
			}

			ogg_stream_init(&OggStateTest, ogg_page_serialno(&m_OggPage));
			ogg_stream_pagein(&OggStateTest, &m_OggPage);
			ogg_stream_packetout(&OggStateTest, &TempOggPacket);

			// identify the codec: try theora
			if (!m_TheoraStreams && theora_decode_header(&m_TheoraInfo, &m_TheoraComment, &TempOggPacket) >= 0) {
				// it is theora
				memcpy(&m_TheoraStreamState, &OggStateTest, sizeof(OggStateTest));
				m_TheoraStreams = 1;
			} else if (!m_VorbisStreams && vorbis_synthesis_headerin(&m_VorbisInfo, &m_VorbisComment, &TempOggPacket) >= 0) {
				// it is vorbis
				memcpy(&m_VorbisStreamState, &OggStateTest, sizeof(OggStateTest));
				m_VorbisStreams = 1;
			} else {
				// whatever it is, we don't care about it
				ogg_stream_clear(&OggStateTest);
			}
		}
	}

	// we're expecting more header packets
	while ((m_TheoraStreams && m_TheoraStreams < 3) || (m_VorbisStreams && m_VorbisStreams < 3)) {
		int Ret;

		// look for further theora headers
		while (m_TheoraStreams && (m_TheoraStreams < 3) && (Ret = ogg_stream_packetout(&m_TheoraStreamState, &TempOggPacket))) {
			if (Ret < 0) {
				Game->LOG(0, "Error parsing Theora stream headers; corrupt stream?");
				return E_FAIL;
			}
			if (theora_decode_header(&m_TheoraInfo, &m_TheoraComment, &TempOggPacket)) {
				Game->LOG(0, "Error parsing Theora stream headers; corrupt stream?");
				return E_FAIL;
			}
			m_TheoraStreams++;
			if (m_TheoraStreams == 3) break;
		}

		/* look for more vorbis header packets */
		while (m_VorbisStreams && (m_VorbisStreams < 3) && (Ret = ogg_stream_packetout(&m_VorbisStreamState, &TempOggPacket))) {
			if (Ret < 0) {
				Game->LOG(0, "Error parsing Vorbis stream headers; corrupt stream?");
				return E_FAIL;
			}
			if (vorbis_synthesis_headerin(&m_VorbisInfo, &m_VorbisComment, &TempOggPacket)) {
				Game->LOG(0, "Error parsing Vorbis stream headers; corrupt stream?");
				return E_FAIL;
			}
			m_VorbisStreams++;
			if (m_VorbisStreams == 3) break;
		}

		// The header pages/packets will arrive before anything else we
		// care about, or the stream is not obeying spec
		if (ogg_sync_pageout(&m_OggSyncState, &m_OggPage) > 0) {
			if (m_TheoraStreams)
				ogg_stream_pagein(&m_TheoraStreamState, &m_OggPage);
			if (m_VorbisStreams)
				ogg_stream_pagein(&m_VorbisStreamState, &m_OggPage);
		} else {
			int Ret = BufferData(&m_OggSyncState); // someone needs more data
			if (Ret == 0) {
				Game->LOG(0, "End of file while searching for codec headers");
				return E_FAIL;
			}
		}
	}



	// and now we have it all.  initialize decoders
	if (m_TheoraStreams) {
		theora_decode_init(&m_TheoraState, &m_TheoraInfo);
	} else {
		// tear down the partial theora setup
		theora_info_clear(&m_TheoraInfo);
		theora_comment_clear(&m_TheoraComment);
	}

	if (m_VorbisStreams) {
		vorbis_synthesis_init(&m_VorbisDSPState, &m_VorbisInfo);
		vorbis_block_init(&m_VorbisDSPState, &m_VorbisBlock);

	} else {
		// tear down the partial vorbis setup
		vorbis_info_clear(&m_VorbisInfo);
		vorbis_comment_clear(&m_VorbisComment);
	}

	HRESULT Res = S_OK;

	// create sound buffer
	if (m_VorbisStreams && Game->m_SoundMgr->m_SoundAvailable) {
		m_Sound = new CBSoundTheora(Game);
		Game->m_SoundMgr->AddSound(m_Sound);
		if (FAILED(Res = m_Sound->InitializeBuffer(this))) {
			Game->m_SoundMgr->RemoveSound(m_Sound);
			m_Sound = NULL;
			Game->LOG(Res, "Error initializing sound buffer for Theora file '%s'", Filename);
		} else {
			SAFE_DELETE_ARRAY(m_AudioBuf);
			m_AudioBufSize = m_Sound->m_StreamBlockSize;
			m_AudioBuf = new ogg_int16_t[m_AudioBufSize];
		}
	}

	// create texture
	if (m_TheoraStreams && !m_Texture) {
		if (Game->m_UseD3D)
			m_Texture = new CBSurfaceD3D(Game);
		else
			m_Texture = new CBSurfaceDD(Game);

		if (!m_Texture || FAILED(Res = m_Texture->Create(m_TheoraInfo.width, m_TheoraInfo.height))) {
			SAFE_DELETE(m_Texture);
		}
	}


	if (!m_Subtitler) m_Subtitler = new CVidSubtitler(Game);
	if (m_Subtitler && Game->m_VideoSubtitles) m_Subtitler->LoadSubtitles(Filename, SubtitleFile);

	return Res;
#endif
	return E_FAIL;
}


//////////////////////////////////////////////////////////////////////////
HRESULT CVidTheoraPlayer::resetStream() {
#if 0
	if (_sound) _sound->Stop();

	m_TimeOffset = 0.0f;
	Initialize(m_Filename);
	Play(m_PlaybackType, m_PosX, m_PosY, false, false, m_Looping, 0, m_PlayZoom);
#endif
	return S_OK;
}

//////////////////////////////////////////////////////////////////////////
HRESULT CVidTheoraPlayer::play(TVideoPlayback Type, int X, int Y, bool FreezeGame, bool FreezeMusic, bool Looping, uint32 StartTime, float ForceZoom, int Volume) {
	if (ForceZoom < 0.0f) 
		ForceZoom = 100.0f;
	if (Volume < 0) 
		_volume = Game->_soundMgr->getVolumePercent(SOUND_SFX);
	else _volume = Volume;
	
	_freezeGame = FreezeGame;
	
	if (!_playbackStarted && _freezeGame) 
		Game->Freeze(FreezeMusic);
	
	_playbackStarted = false;
	float Width, Height;
	if (_theoraDecoder) {
		_surface.copyFrom(*_theoraDecoder->decodeNextFrame());
		_state = THEORA_STATE_PLAYING;
		_looping = Looping;
		_playbackType = Type;

		_startTime = StartTime;
		_volume = Volume;
		_posX = X;
		_posY = Y;
		_playZoom = ForceZoom;
		
		Width = (float)_theoraDecoder->getWidth();
		Height = (float)_theoraDecoder->getHeight();
	} else {
		Width = (float)Game->_renderer->_width;
		Height = (float)Game->_renderer->_height;
	}

	switch (Type) {
		case VID_PLAY_POS:
			_playZoom = ForceZoom;
			_posX = X;
			_posY = Y;
			break;
			
		case VID_PLAY_STRETCH: {
			float ZoomX = (float)((float)Game->_renderer->_width / Width * 100);
			float ZoomY = (float)((float)Game->_renderer->_height / Height * 100);
			_playZoom = MIN(ZoomX, ZoomY);
			_posX = (Game->_renderer->_width - Width * (_playZoom / 100)) / 2;
			_posY = (Game->_renderer->_height - Height * (_playZoom / 100)) / 2;
		}
			break;
			
		case VID_PLAY_CENTER:
			_playZoom = 100.0f;
			_posX = (Game->_renderer->_width - Width) / 2;
			_posY = (Game->_renderer->_height - Height) / 2;
			break;
	}
	return S_OK;
#if 0

	m_State = THEORA_STATE_PLAYING;

	m_Looping = Looping;
	m_PlaybackType = Type;

	float Width, Height;
	if (m_TheoraStreams) {
		Width = (float)m_TheoraInfo.width;
		Height = (float)m_TheoraInfo.height;
	} else {
		Width = (float)Game->m_Renderer->m_Width;
		Height = (float)Game->m_Renderer->m_Height;
	}

	switch (Type) {
	case VID_PLAY_POS:
		m_PlayZoom = ForceZoom;
		m_PosX = X;
		m_PosY = Y;
		break;

	case VID_PLAY_STRETCH: {
		float ZoomX = (float)((float)Game->m_Renderer->m_Width / Width * 100);
		float ZoomY = (float)((float)Game->m_Renderer->m_Height / Height * 100);
		m_PlayZoom = min(ZoomX, ZoomY);
		m_PosX = (Game->m_Renderer->m_Width - Width * (m_PlayZoom / 100)) / 2;
		m_PosY = (Game->m_Renderer->m_Height - Height * (m_PlayZoom / 100)) / 2;
	}
	break;

	case VID_PLAY_CENTER:
		m_PlayZoom = 100.0f;
		m_PosX = (Game->m_Renderer->m_Width - Width) / 2;
		m_PosY = (Game->m_Renderer->m_Height - Height) / 2;
		break;
	}


	if (StartTime) SeekToTime(StartTime);

	Update();
#endif
	return E_FAIL;
}

//////////////////////////////////////////////////////////////////////////
HRESULT CVidTheoraPlayer::stop() {
#if 0
	if (m_Sound) m_Sound->Stop();
	m_State = THEORA_STATE_FINISHED;
	if (m_FreezeGame) Game->Unfreeze();
#endif
	return S_OK;
}

//////////////////////////////////////////////////////////////////////////
HRESULT CVidTheoraPlayer::update() {
	_currentTime = _freezeGame ? Game->_liveTimer : Game->_timer;
	
	if (!isPlaying()) return S_OK;
	
	if (_playbackStarted /*&& m_Sound && !m_Sound->IsPlaying()*/) return S_OK;
	
	if (_playbackStarted && !_freezeGame && Game->_state == GAME_FROZEN) return S_OK;
	
	int Counter = 0;
	if (_theoraDecoder) {
		if (_theoraDecoder->endOfVideo() && _looping) {
			warning("Should loop movie");
			_theoraDecoder->rewind();
		} else if (_theoraDecoder->endOfVideo() && !_looping) {
			warning("Finished movie");
			_state = THEORA_STATE_FINISHED;
			_playbackStarted = false;
			if (_freezeGame) Game->Unfreeze();
		}
		if (_state == THEORA_STATE_PLAYING) {
			if (_theoraDecoder->getTimeToNextFrame() == 0) {
				_surface.copyFrom(*_theoraDecoder->decodeNextFrame());
				_videoFrameReady = true;
			}
			if (_texture && _videoFrameReady) {
				WriteVideo();
			}
			return S_OK;
		}
	}
	// Skip the busy-loop?
	if ((!_texture || !_videoFrameReady) && !_theoraDecoder->endOfVideo()) {
		// end playback
		if (!_looping) {
			_state = THEORA_STATE_FINISHED;
			if (_freezeGame) Game->Unfreeze();
			return S_OK;
		} else {
			resetStream();
			return S_OK;
		}
	}

#if 0
	m_CurrentTime = m_FreezeGame ? Game->m_LiveTimer : Game->m_Timer;

	if (!IsPlaying()) return S_OK;

	if (m_PlaybackStarted && m_Sound && !m_Sound->IsPlaying()) return S_OK;

	if (m_PlaybackStarted && !m_FreezeGame && Game->m_State == GAME_FROZEN) return S_OK;

	int Counter = 0;
	while (true) {
		if (m_Sound) DecodeVorbis();
		else m_AudioFrameReady = true;

		if (m_Texture) DecodeTheora();
		else m_VideoFrameReady = true;

		if ((!m_Sound || !m_AudioFrameReady) && (!m_Texture || !m_VideoFrameReady) && m_File->IsEOF()) {
			// end playback
			if (!m_Looping) {
				m_State = THEORA_STATE_FINISHED;
				if (m_Sound) m_Sound->Stop();
				if (m_FreezeGame) Game->Unfreeze();
				break;
			} else {
				ResetStream();
				return S_OK;
			}
		}


		if (!m_VideoFrameReady || !m_AudioFrameReady) {
			Counter++;
			if (StreamInData() == 0) break;
		} else break;
	}


	// If playback has begun, top audio buffer off immediately.
	//if(m_Sound) WriteAudio();

	// are we at or past time for this video frame?
	if (m_PlaybackStarted && m_VideoFrameReady && (!m_FrameRendered || m_VideobufTime <= GetMovieTime())) {
		//Game->LOG(0, "%f %f", m_VideobufTime, GetMovieTime());
		if (m_Texture) WriteVideo();
		m_VideoFrameReady = false;

		if (m_SavedState == THEORA_STATE_PAUSED) {
			Pause();
			m_SavedState = THEORA_STATE_NONE;
		}
	}

	// if our buffers either don't exist or are ready to go,
	// we can begin playback
	bool StartNow = false;
	if ((!m_TheoraStreams || m_VideoFrameReady) &&
	        (!m_VorbisStreams || m_AudioFrameReady)) StartNow = true;
	// same if we've run out of input
	if (m_File->IsEOF()) StartNow = true;


	if (m_Sound) WriteAudio();


	if (!m_PlaybackStarted && StartNow && !m_SeekingKeyframe) {
		//m_StartTime = timeGetTime();
		m_StartTime = m_CurrentTime;
		if (m_Sound) {
			m_Sound->SetPrivateVolume(m_Volume);
			m_Sound->Play();
		}
		m_PlaybackStarted = true;
	}

	if (m_Subtitler && Game->m_VideoSubtitles) m_Subtitler->Update(GetMovieFrame());
#endif
	return S_OK;
}

//////////////////////////////////////////////////////////////////////////
float CVidTheoraPlayer::getMovieTime() {
	if (!_playbackStarted) {
		return 0.0f;
	} else {
		return _theoraDecoder->getTime();
	}
}


//////////////////////////////////////////////////////////////////////////
int CVidTheoraPlayer::getMovieFrame() {
#if 0
	if (!m_TheoraStreams) return 0;
	float Time = GetMovieTime();

	return Time / ((double)m_TheoraInfo.fps_denominator / m_TheoraInfo.fps_numerator);
#endif
	if (_theoraDecoder) {
		return _theoraDecoder->getTime();
	} else {
		return 0;
	}
}

//////////////////////////////////////////////////////////////////////////
HRESULT CVidTheoraPlayer::WriteVideo() {
	if (!_texture) return E_FAIL;

	_texture->startPixelOp();
	//RenderFrame(_texture, &yuv);
	_texture->putSurface(_surface);
	_texture->endPixelOp();

	return S_OK;
}

//////////////////////////////////////////////////////////////////////////
HRESULT CVidTheoraPlayer::display(uint32 Alpha) {
	RECT rc;
	HRESULT Res;

	if (_texture) {
		CBPlatform::SetRect(&rc, 0, 0, _texture->getWidth(), _texture->getHeight());
		if (_playZoom == 100.0f) Res = _texture->displayTrans(_posX, _posY, rc, Alpha);
		else Res = _texture->displayTransZoom(_posX, _posY, rc, _playZoom, _playZoom, Alpha);
	} else Res = E_FAIL;
#if 0
	if (m_Subtitler && Game->m_VideoSubtitles) m_Subtitler->Display();
#endif
	return Res;
}

//////////////////////////////////////////////////////////////////////////
HRESULT CVidTheoraPlayer::setAlphaImage(const char *filename) {
	warning("CVidTheoraPlayer::SetAlphaImage(%s) - Not implemented", filename);
#if 0
	SAFE_DELETE(m_AlphaImage);
	m_AlphaImage = new CBImage(Game);
	if (!m_AlphaImage || FAILED(m_AlphaImage->LoadFile(Filename))) {
		SAFE_DELETE(m_AlphaImage);
		SAFE_DELETE_ARRAY(m_AlphaFilename);
		return E_FAIL;
	}
	if (m_AlphaFilename != Filename) CBUtils::SetString(&m_AlphaFilename, Filename);
	m_AlphaImage->Convert(IMG_TRUECOLOR);
#endif
	return S_OK;
}

//////////////////////////////////////////////////////////////////////////
byte CVidTheoraPlayer::getAlphaAt(int X, int Y) {
#if 0
	if (_alphaImage) return _alphaImage->GetAlphaAt(X, Y);
	else return 0xFF;
#endif
	return 0;
}


//////////////////////////////////////////////////////////////////////////
inline int intlog(int num) {
	int r = 0;
	while (num > 0) {
		num = num / 2;
		r = r + 1;
	}

	return r;
}

//////////////////////////////////////////////////////////////////////////
HRESULT CVidTheoraPlayer::SeekToTime(uint32 time) {
	warning("CVidTheoraPlayer::SeekToTime(%d) - not supported", time);
#if 0
	if (!m_TheoraStreams) return E_FAIL;


	float TargetTime = Time / 1000.0f;


	ogg_page page;
	int read = 1;
	ogg_int64_t gran;
	float movieLength = 0;
	DWORD LastPos = 0;

	int keyframe_granule_shift = intlog(m_TheoraInfo.keyframe_frequency_force - 1);

	while (!m_File->IsEOF() && read != 0) {
		read = BufferData(&m_OggSyncState);

		while (ogg_sync_pageout(&m_OggSyncState, &page) > 0) {
			int serno = ogg_page_serialno(&page);
			//This is theora stream we were searching for
			if (m_TheoraStreamState.serialno == serno) {
				//Calculate a rough time estimate
				gran = ogg_page_granulepos(&page);
				if (gran >= 0) {
					ogg_int64_t iframe = gran >> keyframe_granule_shift;
					ogg_int64_t pframe = gran   - (iframe << keyframe_granule_shift);
					movieLength = (iframe + pframe) *
					              ((double)m_TheoraInfo.fps_denominator / m_TheoraInfo.fps_numerator);

					if (movieLength >= TargetTime) {
						m_TimeOffset = movieLength;
						//m_TimeOffset = TargetTime;
						//m_File->Seek(LastPos);

						goto finish;
					}
					LastPos = m_File->GetPos();
				}
			}
		}
	}

finish:
	ogg_sync_reset(&m_OggSyncState);

	ogg_stream_reset(&m_TheoraStreamState);
	ogg_stream_reset(&m_VorbisStreamState);

	theora_clear(&m_TheoraState);
	theora_decode_init(&m_TheoraState, &m_TheoraInfo);
	vorbis_synthesis_restart(&m_VorbisDSPState);

	m_SeekingKeyframe = true;

	//theora_packet_iskeyframe

#endif
	return S_OK;
}

//////////////////////////////////////////////////////////////////////////
HRESULT CVidTheoraPlayer::pause() {
	if (_state == THEORA_STATE_PLAYING) {
		_state = THEORA_STATE_PAUSED;
		_theoraDecoder->pauseVideo(true);
		return S_OK;
	} else {
		return E_FAIL;
	}
}

//////////////////////////////////////////////////////////////////////////
HRESULT CVidTheoraPlayer::resume() {
	if (_state == THEORA_STATE_PAUSED) {
		_state = THEORA_STATE_PLAYING;
		_theoraDecoder->pauseVideo(false);
		return S_OK;
	} else {
		return E_FAIL;
	}
}

//////////////////////////////////////////////////////////////////////////
/*HRESULT CVidTheoraPlayer::Persist(CBPersistMgr *PersistMgr) {
	//CBBase::Persist(PersistMgr);
#if 0
	if (PersistMgr->m_Saving) {
		m_SavedPos = GetMovieTime() * 1000;
		m_SavedState = m_State;
	} else {
		SetDefaults();
	}

	PersistMgr->Transfer(TMEMBER(Game));
	PersistMgr->Transfer(TMEMBER(m_SavedPos));
	PersistMgr->Transfer(TMEMBER(m_SavedState));
	PersistMgr->Transfer(TMEMBER(m_Filename));
	PersistMgr->Transfer(TMEMBER(m_AlphaFilename));
	PersistMgr->Transfer(TMEMBER(m_PosX));
	PersistMgr->Transfer(TMEMBER(m_PosY));
	PersistMgr->Transfer(TMEMBER(m_PlayZoom));
	PersistMgr->Transfer(TMEMBER_INT(m_PlaybackType));
	PersistMgr->Transfer(TMEMBER(m_Looping));

	if (PersistMgr->CheckVersion(1, 7, 3)) {
		PersistMgr->Transfer(TMEMBER(m_Volume));
	} else {
		m_Volume = 100;
	}
#endif
	return S_OK;
}
*/
//////////////////////////////////////////////////////////////////////////
HRESULT CVidTheoraPlayer::initializeSimple() {
	if (SUCCEEDED(initialize(_filename))) {
		if (_alphaFilename) setAlphaImage(_alphaFilename);
		play(_playbackType, _posX, _posY, false, false, _looping, _savedPos, _playZoom);
	} else _state = THEORA_STATE_FINISHED;

	return S_OK;
}

//////////////////////////////////////////////////////////////////////////
CBSurface *CVidTheoraPlayer::getTexture() {
	return _texture;
}

} // end of namespace WinterMute