1 /* GemRB - Infinity Engine Emulator
2  * Copyright (C) 2003 The GemRB Project
3  *
4  * This program is free software; you can redistribute it and/or
5  * modify it under the terms of the GNU General Public License
6  * as published by the Free Software Foundation; either version 2
7  * of the License, or (at your option) any later version.
8  *
9  * This program is distributed in the hope that it will be useful,
10  * but WITHOUT ANY WARRANTY; without even the implied warranty of
11  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
12  * GNU General Public License for more details.
13  *
14  * You should have received a copy of the GNU General Public License
15  * along with this program; if not, write to the Free Software
16  * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
17  *
18  *
19  */
20 
21 #include "MVEPlayer.h"
22 
23 #include "ie_types.h"
24 
25 #include "Audio.h"
26 #include "Interface.h"
27 #include "Palette.h"
28 #include "Variables.h"
29 #include "Video.h"
30 
31 #include <cassert>
32 #include <cstdio>
33 
34 using namespace GemRB;
35 
36 static const char MVESignature[] = "Interplay MVE File\x1A";
37 static const int MVE_SIGNATURE_LEN = 19;
38 
MVEPlay(void)39 MVEPlay::MVEPlay(void)
40 : decoder(this)
41 {
42 	video = core->GetVideoDriver();
43 	validVideo = false;
44 	vidBuf = NULL;
45 	g_palette = new Palette();
46 
47 	// these colors don't change
48 	g_palette->col[0] = ColorBlack;
49 	//Set color 255 to be our subtitle color
50 	g_palette->col[255] = Color(50,50,50,255);
51 }
52 
~MVEPlay(void)53 MVEPlay::~MVEPlay(void)
54 {
55 }
56 
Open(DataStream * stream)57 bool MVEPlay::Open(DataStream* stream)
58 {
59 	str = stream;
60 	validVideo = false;
61 
62 	char Signature[MVE_SIGNATURE_LEN];
63 	str->Read( Signature, MVE_SIGNATURE_LEN );
64 	if (memcmp( Signature, MVESignature, MVE_SIGNATURE_LEN ) != 0) {
65 		return false;
66 	}
67 
68 	str->Seek( 0, GEM_STREAM_START );
69 
70 	validVideo = decoder.start_playback();
71 	return validVideo;
72 }
73 
DecodeFrame(VideoBuffer & buf)74 bool MVEPlay::DecodeFrame(VideoBuffer& buf)
75 {
76 	vidBuf = &buf;
77 	++framePos;
78 	return (validVideo && decoder.next_frame());
79 }
80 
fileRead(void * buf,unsigned int count)81 unsigned int MVEPlay::fileRead(void* buf, unsigned int count)
82 {
83 	unsigned numread;
84 
85 	numread = str->Read( buf, count );
86 	return ( numread == count );
87 }
88 
showFrame(const unsigned char * buf,unsigned int bufw,unsigned int bufh)89 void MVEPlay::showFrame(const unsigned char* buf, unsigned int bufw, unsigned int bufh)
90 {
91 	if (vidBuf == NULL) {
92 		Log(WARNING, "MVEPlayer", "attempting to decode a frame without a video buffer (most likely during init).");
93 		return;
94 	}
95 	Size s = vidBuf->Size();
96 	int dest_x = unsigned(s.w - bufw) >> 1;
97 	int dest_y = unsigned(s.h - bufh) >> 1;
98 	vidBuf->CopyPixels(Region(dest_x, dest_y, bufw, bufh), buf, NULL, g_palette.get());
99 }
100 
setPalette(unsigned char * p,unsigned start,unsigned count)101 void MVEPlay::setPalette(unsigned char* p, unsigned start, unsigned count)
102 {
103 	p = p + (start * 3);
104 	for (unsigned int i = start; i < start+count; i++) {
105 		g_palette->col[i].r = ( *p++ ) << 2;
106 		g_palette->col[i].g = ( *p++ ) << 2;
107 		g_palette->col[i].b = ( *p++ ) << 2;
108 		g_palette->col[i].a = 0xff;
109 	}
110 }
111 
setAudioStream()112 int MVEPlay::setAudioStream()
113 {
114 	ieDword volume ;
115 	core->GetDictionary()->Lookup( "Volume Movie", volume) ;
116 	int source = core->GetAudioDrv()->SetupNewStream(0, 0, 0, volume, false, false) ;
117 	return source;
118 }
119 
freeAudioStream(int stream)120 void MVEPlay::freeAudioStream(int stream)
121 {
122 	if (stream > -1)
123 		core->GetAudioDrv()->ReleaseStream(stream, true);
124 }
125 
queueBuffer(int stream,unsigned short bits,int channels,short * memory,int size,int samplerate)126 void MVEPlay::queueBuffer(int stream, unsigned short bits,
127 			int channels, short* memory,
128 			int size, int samplerate)
129 {
130 	if (stream > -1)
131 		core->GetAudioDrv()->QueueBuffer(stream, bits, channels, memory, size, samplerate) ;
132 }
133 
134 
135 #include "plugindef.h"
136 
137 GEMRB_PLUGIN(0x218963DC, "MVE Video Player")
138 PLUGIN_IE_RESOURCE(MVEPlay, "mve", (ieWord)IE_MVE_CLASS_ID)
139 END_PLUGIN()
140