Debian Codegraph @

engine/video/movie.cpp

raw
1 // ____ _ __
2 // / __ )____ _____ | | / /___ ___________
3 // / __ / __ \/ ___/ | | /| / / __ `/ ___/ ___/
4 // / /_/ / /_/ (__ ) | |/ |/ / /_/ / / (__ )
5 // /_____/\____/____/ |__/|__/\__,_/_/ /____/
6 //
7 // A futuristic real-time strategy game.
8 // This file is part of Bos Wars.
9 //
10 /**@name movie.cpp - Movie playback functions. */
11 //
12 // (c) Copyright 2005-2007 by Nehal Mistry and Jimmy Salmon
13 //
14 // This program is free software; you can redistribute it and/or modify
15 // it under the terms of the GNU General Public License as published by
16 // the Free Software Foundation; only version 2 of the License.
17 //
18 // This program is distributed in the hope that it will be useful,
19 // but WITHOUT ANY WARRANTY; without even the implied warranty of
20 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
21 // GNU General Public License for more details.
22 //
23 // You should have received a copy of the GNU General Public License
24 // along with this program; if not, write to the Free Software
25 // Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA
26 // 02111-1307, USA.
27 //
28
29 //@{
30
31 #ifdef USE_THEORA
32
33 /*----------------------------------------------------------------------------
34 -- Includes
35 ----------------------------------------------------------------------------*/
36
37 #include <stdlib.h>
38 #include <stdio.h>
39 #include <string.h>
40
41 #include "stratagus.h"
42 #include "video.h"
43 #include "sound.h"
44 #include "sound_server.h"
45 #include "movie.h"
46 #include "network.h"
47 #include "iocompat.h"
48 #include "iolib.h"
49
50 #include "SDL.h"
51
52 /*----------------------------------------------------------------------------
53 -- Defines
54 ----------------------------------------------------------------------------*/
55
56 /*----------------------------------------------------------------------------
57 -- Variables
58 ----------------------------------------------------------------------------*/
59
60 extern SDL_Surface *TheScreen;
61 static bool MovieStop;
62
63 /*----------------------------------------------------------------------------
64 -- Functions
65 ----------------------------------------------------------------------------*/
66
67 /**
68 ** Callbacks for movie input.
69 */
70
71 static void MovieCallbackButtonPressed(unsigned dummy)
72 {
73 MovieStop = true;
74 }
75
76 static void MovieCallbackButtonReleased(unsigned dummy)
77 {
78 }
79
80 static void MovieCallbackKeyPressed(unsigned dummya, unsigned dummyb)
81 {
82 MovieStop = true;
83 }
84
85
86 static void MovieCallbackKeyReleased(unsigned dummya, unsigned dummyb)
87 {
88 }
89
90 static void MovieCallbackKeyRepeated(unsigned dummya, unsigned dummyb)
91 {
92 }
93
94 static void MovieCallbackMouseMove(int dummya, int dummyb)
95 {
96 }
97
98 static void MovieCallbackMouseExit(void)
99 {
100 }
101
102 /**
103 ** Draw Ogg data to the overlay
104 */
105 static int OutputTheora(OggData *data, SDL_Overlay *yuv_overlay, SDL_Rect *rect)
106 {
107 int i;
108 yuv_buffer yuv;
109 int crop_offset;
110
111 theora_decode_YUVout(&data->tstate, &yuv);
112
113 if (SDL_MUSTLOCK(TheScreen)) {
114 if (SDL_LockSurface(TheScreen) < 0) {
115 return - 1;
116 }
117 }
118
119 if (SDL_LockYUVOverlay(yuv_overlay) < 0) {
120 return -1;
121 }
122
123 crop_offset = data->tinfo.offset_x + yuv.y_stride * data->tinfo.offset_y;
124 for (i = 0; i < yuv_overlay->h; ++i) {
125 memcpy(yuv_overlay->pixels[0] + yuv_overlay->pitches[0] * i,
126 yuv.y + crop_offset + yuv.y_stride * i, yuv_overlay->w);
127 }
128
129 crop_offset = (data->tinfo.offset_x / 2) + (yuv.uv_stride) *
130 (data->tinfo.offset_y / 2);
131 for (i = 0; i < yuv_overlay->h / 2; ++i) {
132 memcpy(yuv_overlay->pixels[1] + yuv_overlay->pitches[1] * i,
133 yuv.v + yuv.uv_stride * i, yuv_overlay->w / 2);
134 memcpy(yuv_overlay->pixels[2] + yuv_overlay->pitches[2] * i,
135 yuv.u + crop_offset + yuv.uv_stride * i, yuv_overlay->w / 2);
136 }
137
138 if (SDL_MUSTLOCK(TheScreen)) {
139 SDL_UnlockSurface(TheScreen);
140 }
141 SDL_UnlockYUVOverlay(yuv_overlay);
142
143 SDL_DisplayYUVOverlay(yuv_overlay, rect);
144
145 return 0;
146 }
147
148 /**
149 ** Process Ogg data
150 */
151 static int TheoraProcessData(OggData *data)
152 {
153 ogg_packet packet;
154
155 while (1) {
156 if (ogg_stream_packetout(&data->vstream, &packet) != 1) {
157 if (OggGetNextPage(&data->page, &data->sync, data->File)) {
158 // EOF
159 return -1;
160 }
161
162 ogg_stream_pagein(&data->vstream, &data->page);
163 } else {
164 theora_decode_packetin(&data->tstate, &packet);
165 return 0;
166 }
167 }
168 }
169
170
171 /**
172 ** Play a video file.
173 **
174 ** @param name Filename of movie file.
175 **
176 ** @return Non-zero if file isn't a supported movie.
177 */
178 int PlayMovie(const std::string &name)
179 {
180 OggData data;
181 CFile f;
182 SDL_Rect rect;
183 SDL_Overlay *yuv_overlay;
184 CSample *sample;
185 const EventCallback *old_callbacks;
186 EventCallback callbacks;
187 unsigned int start_ticks;
188 int need_data;
189 int diff;
190 char buffer[PATH_MAX];
191
192 LibraryFileName(name.c_str(), buffer, sizeof(buffer));
193
194 if (f.open(buffer, CL_OPEN_READ) == -1) {
195 fprintf(stderr, "Can't open file `%s'\n", name.c_str());
196 return -1;
197 }
198
199 memset(&data, 0, sizeof(data));
200 if (OggInit(&f, &data) || !data.video) {
201 OggFree(&data);
202 f.close();
203 return -1;
204 }
205
206 data.File = &f;
207
208 if (data.tinfo.frame_width * 300 / 4 > data.tinfo.frame_height * 100) {
209 rect.w = Video.Width;
210 rect.h = Video.Width * data.tinfo.frame_height / data.tinfo.frame_width;
211 rect.x = 0;
212 rect.y = (Video.Height - rect.h) / 2;
213 } else {
214 rect.w = Video.Height * data.tinfo.frame_width / data.tinfo.frame_height;
215 rect.h = Video.Height;
216 rect.x = (Video.Width - rect.w) / 2;
217 rect.y = 0;
218 }
219
220 yuv_overlay = SDL_CreateYUVOverlay(data.tinfo.frame_width,
221 data.tinfo.frame_height, SDL_YV12_OVERLAY, TheScreen);
222
223 if (yuv_overlay == NULL) {
224 fprintf(stderr, "SDL_CreateYUVOverlay: %s\n", SDL_GetError());
225 OggFree(&data);
226 f.close();
227 return 0;
228 }
229
230 StopMusic();
231 if ((sample = LoadVorbis(buffer, PlayAudioStream))) {
232 if ((sample->Channels != 1 && sample->Channels != 2) ||
233 sample->SampleSize != 16) {
234 fprintf(stderr, "Unsupported sound format in movie\n");
235 delete sample;
236 SDL_FreeYUVOverlay(yuv_overlay);
237 OggFree(&data);
238 f.close();
239 return 0;
240 }
241 PlayMusic(sample);
242 }
243
244 callbacks.ButtonPressed = MovieCallbackButtonPressed;
245 callbacks.ButtonReleased = MovieCallbackButtonReleased;
246 callbacks.MouseMoved = MovieCallbackMouseMove;
247 callbacks.MouseExit = MovieCallbackMouseExit;
248 callbacks.KeyPressed = MovieCallbackKeyPressed;
249 callbacks.KeyReleased = MovieCallbackKeyReleased;
250 callbacks.KeyRepeated = MovieCallbackKeyRepeated;
251 callbacks.NetworkEvent = NetworkEvent;
252
253 old_callbacks = GetCallbacks();
254 SetCallbacks(&callbacks);
255
256 Invalidate();
257 RealizeVideoMemory();
258
259 MovieStop = false;
260 start_ticks = SDL_GetTicks();
261 need_data = 1;
262 while (!MovieStop) {
263 if (need_data) {
264 if (TheoraProcessData(&data)) {
265 break;
266 }
267 need_data = 0;
268 }
269
270 diff = SDL_GetTicks() - start_ticks - static_cast<int>(
271 theora_granule_time(&data.tstate, data.tstate.granulepos) * 1000);
272
273 if (diff > 100) {
274 // too far behind, skip some frames
275 need_data = 1;
276 continue;
277 }
278 if (diff > 0) {
279 OutputTheora(&data, yuv_overlay, &rect);
280 need_data = 1;
281 }
282
283 WaitEventsOneFrame();
284 }
285
286 StopMusic();
287 SDL_FreeYUVOverlay(yuv_overlay);
288
289 OggFree(&data);
290 f.close();
291
292 SetCallbacks(old_callbacks);
293
294 return 0;
295 }
296
297 #else
298
299 #include <string>
300 #include <string.h>
301
302 /**
303 ** Play a video file.
304 **
305 ** @param name Filename of movie file.
306 **
307 ** @return Non-zero if file isn't a supported movie.
308 */
309 int PlayMovie(const std::string &name)
310 {
311 if (strstr(name.c_str(), ".ogg") || strstr(name.c_str(), ".avi")) {
312 return 0;
313 }
314 return -1;
315 }
316
317 #endif
318
319 //@}