patch by Blub and me:
[divverent/darkplaces.git] / cl_video.c
1
2 #include "quakedef.h"
3 #include "cl_dyntexture.h"
4 #include "cl_video.h"
5 #include "dpvsimpledecode.h"
6
7 // constants (and semi-constants)
8 static int  cl_videormask;
9 static int  cl_videobmask;
10 static int  cl_videogmask;
11 static int      cl_videobytesperpixel;
12
13 static int cl_num_videos;
14 static clvideo_t cl_videos[ MAXCLVIDEOS ];
15 static rtexturepool_t *cl_videotexturepool;
16
17 static clvideo_t *FindUnusedVid( void )
18 {
19         int i;
20         for( i = 1 ; i < MAXCLVIDEOS ; i++ )
21                 if( cl_videos[ i ].state == CLVIDEO_UNUSED )
22                         return &cl_videos[ i ];
23         return NULL;
24 }
25
26 static qboolean OpenStream( clvideo_t * video )
27 {
28         char *errorstring;
29         video->stream = dpvsimpledecode_open( video->filename, &errorstring);
30         if (!video->stream )
31         {
32                 Con_Printf("unable to open \"%s\", error: %s\n", video->filename, errorstring);
33                 return false;
34         }
35         return true;
36 }
37
38 static void VideoUpdateCallback(rtexture_t *rt, void *data) {
39         clvideo_t *video = (clvideo_t *) data;
40         R_UpdateTexture( video->cpif.tex, (unsigned char *)video->imagedata, 0, 0, video->cpif.width, video->cpif.height );
41 }
42
43 static void LinkVideoTexture( clvideo_t *video ) {
44         video->cpif.tex = R_LoadTexture2D( cl_videotexturepool, video->cpif.name,
45                 video->cpif.width, video->cpif.height, NULL, TEXTYPE_BGRA, TEXF_PERSISTENT | TEXF_ALLOWUPDATES, NULL );
46         R_MakeTextureDynamic( video->cpif.tex, VideoUpdateCallback, video );
47         CL_LinkDynTexture( video->cpif.name, video->cpif.tex );
48 }
49
50 static void UnlinkVideoTexture( clvideo_t *video ) {
51         CL_UnlinkDynTexture( video->cpif.name );
52         // free the texture
53         R_FreeTexture( video->cpif.tex );
54         // free the image data
55         Mem_Free( video->imagedata );
56 }
57
58 static void SuspendVideo( clvideo_t * video )
59 {
60         if( video->suspended )
61                 return;
62         video->suspended = true;
63         UnlinkVideoTexture( video );
64         // if we are in firstframe mode, also close the stream
65         if( video->state == CLVIDEO_FIRSTFRAME )
66                 dpvsimpledecode_close( video->stream );
67 }
68
69 static qboolean WakeVideo( clvideo_t * video )
70 {
71         if( !video->suspended )
72                 return true;
73         video->suspended = false;
74
75         if( video->state == CLVIDEO_FIRSTFRAME )
76                 if( !OpenStream( video ) ) {
77                         video->state = CLVIDEO_UNUSED;
78                         return false;
79                 }
80
81         video->imagedata = Mem_Alloc( cls.permanentmempool, video->cpif.width * video->cpif.height * cl_videobytesperpixel );
82         LinkVideoTexture( video );
83
84         // update starttime
85         video->starttime += realtime - video->lasttime;
86
87         return true;
88 }
89
90 static clvideo_t* OpenVideo( clvideo_t *video, const char *filename, const char *name, int owner )
91 {
92         strlcpy( video->filename, filename, sizeof(video->filename) );
93         video->ownertag = owner;
94         if( strncmp( name, CLVIDEOPREFIX, sizeof( CLVIDEOPREFIX ) - 1 ) )
95                 return NULL;
96         strlcpy( video->cpif.name, name, sizeof(video->cpif.name) );
97
98         if( !OpenStream( video ) )
99                 return NULL;
100
101         video->state = CLVIDEO_FIRSTFRAME;
102         video->framenum = -1;
103         video->framerate = dpvsimpledecode_getframerate( video->stream );
104         video->lasttime = realtime;
105
106         video->cpif.width = dpvsimpledecode_getwidth( video->stream );
107         video->cpif.height = dpvsimpledecode_getheight( video->stream );
108         video->imagedata = Mem_Alloc( cls.permanentmempool, video->cpif.width * video->cpif.height * cl_videobytesperpixel );
109         LinkVideoTexture( video );
110
111         return video;
112 }
113
114 clvideo_t* CL_OpenVideo( const char *filename, const char *name, int owner )
115 {
116         clvideo_t *video;
117         // sanity check
118         if( !name || !*name || strncmp( name, CLVIDEOPREFIX, sizeof( CLVIDEOPREFIX ) - 1 ) != 0 ) {
119                 Con_DPrintf( "CL_OpenVideo: Bad video texture name '%s'!\n", name );
120                 return NULL;
121         }
122
123         video = FindUnusedVid();
124         if( !video ) {
125                 Con_Printf( "CL_OpenVideo: unable to open video \"%s\" - video limit reached\n", filename );
126                 return NULL;
127         }
128         video = OpenVideo( video, filename, name, owner );
129         // expand the active range to include the new entry
130         if (video) {
131                 cl_num_videos = max(cl_num_videos, (int)(video - cl_videos) + 1);
132         }
133         return video;
134 }
135
136 static clvideo_t* CL_GetVideoBySlot( int slot )
137 {
138         clvideo_t *video = &cl_videos[ slot ];
139
140         if( video->suspended )
141         {
142                 if( !WakeVideo( video ) )
143                         return NULL;
144                 else if( video->state == CLVIDEO_RESETONWAKEUP )
145                         video->framenum = -1;
146         }
147
148         video->lasttime = realtime;
149
150         return video;
151 }
152
153 clvideo_t *CL_GetVideoByName( const char *name )
154 {
155         int i;
156
157         for( i = 0 ; i < cl_num_videos ; i++ )
158                 if( cl_videos[ i ].state != CLVIDEO_UNUSED
159                         &&      !strcmp( cl_videos[ i ].cpif.name , name ) )
160                         break;
161         if( i != cl_num_videos )
162                 return CL_GetVideoBySlot( i );
163         else
164                 return NULL;
165 }
166
167 void CL_SetVideoState( clvideo_t *video, clvideostate_t state )
168 {
169         if( !video )
170                 return;
171
172         video->lasttime = realtime;
173         video->state = state;
174         if( state == CLVIDEO_FIRSTFRAME )
175                 CL_RestartVideo( video );
176 }
177
178 void CL_RestartVideo( clvideo_t *video )
179 {
180         if( !video )
181                 return;
182
183         video->starttime = video->lasttime = realtime;
184         video->framenum = -1;
185
186         dpvsimpledecode_close( video->stream );
187         if( !OpenStream( video ) )
188                 video->state = CLVIDEO_UNUSED;
189 }
190
191 void CL_CloseVideo( clvideo_t * video )
192 {
193         if( !video || video->state == CLVIDEO_UNUSED )
194                 return;
195
196         if( !video->suspended || video->state != CLVIDEO_FIRSTFRAME )
197                 dpvsimpledecode_close( video->stream );
198         if( !video->suspended ) {
199                 UnlinkVideoTexture( video );
200         }
201
202         video->state = CLVIDEO_UNUSED;
203 }
204
205 static void VideoFrame( clvideo_t *video )
206 {
207         int destframe;
208
209         if( video->state == CLVIDEO_FIRSTFRAME )
210                 destframe = 0;
211         else
212                 destframe = (int)((realtime - video->starttime) * video->framerate);
213         if( destframe < 0 )
214                 destframe = 0;
215         if( video->framenum < destframe ) {
216                 do {
217                         video->framenum++;
218                         if( dpvsimpledecode_video( video->stream, video->imagedata, cl_videormask,
219                                 cl_videogmask, cl_videobmask, cl_videobytesperpixel,
220                                 cl_videobytesperpixel * video->cpif.width )
221                                 ) { // finished?
222                                 CL_RestartVideo( video );
223                                 if( video->state == CLVIDEO_PLAY )
224                                                 video->state = CLVIDEO_FIRSTFRAME;
225                                 return;
226                         }
227                 } while( video->framenum < destframe );
228                 R_MarkDirtyTexture( video->cpif.tex );
229         }
230 }
231
232 void CL_Video_Frame( void ) // update all videos
233 {
234         int i;
235         clvideo_t *video;
236
237         if (!cl_num_videos)
238                 return;
239
240         for( video = cl_videos, i = 0 ; i < cl_num_videos ; video++, i++ )
241                 if( video->state != CLVIDEO_UNUSED && !video->suspended )
242                 {
243                         if( realtime - video->lasttime > CLTHRESHOLD )
244                                 SuspendVideo( video );
245                         else if( video->state == CLVIDEO_PAUSE )
246                                 video->starttime = realtime - video->framenum * video->framerate;
247                         else
248                                 VideoFrame( video );
249                 }
250
251         if( cl_videos->state == CLVIDEO_FIRSTFRAME )
252                 CL_VideoStop();
253
254         // reduce range to exclude unnecessary entries
255         while (cl_num_videos > 0 && cl_videos[cl_num_videos-1].state == CLVIDEO_UNUSED)
256                 cl_num_videos--;
257 }
258
259 void CL_Video_Shutdown( void )
260 {
261         int i;
262         for( i = 0 ; i < cl_num_videos ; i++ )
263                 CL_CloseVideo( &cl_videos[ i ] );
264 }
265
266 void CL_PurgeOwner( int owner )
267 {
268         int i;
269         for( i = 0 ; i < cl_num_videos ; i++ )
270                 if( cl_videos[ i ].ownertag == owner )
271                         CL_CloseVideo( &cl_videos[ i ] );
272 }
273
274 int cl_videoplaying = false; // old, but still supported
275
276 void CL_DrawVideo(void)
277 {
278         if (cl_videoplaying)
279                 DrawQ_Pic(0, 0, &CL_GetVideoBySlot( 0 )->cpif, vid_conwidth.integer, vid_conheight.integer, 1, 1, 1, 1, 0);
280 }
281
282 void CL_VideoStart(char *filename)
283 {
284         Host_StartVideo();
285
286         if( cl_videos->state != CLVIDEO_UNUSED )
287                 CL_CloseVideo( cl_videos );
288         // already contains video/
289         if( !OpenVideo( cl_videos, filename, va( CLDYNTEXTUREPREFIX "%s", filename ), 0 ) )
290                 return;
291         // expand the active range to include the new entry
292         cl_num_videos = max(cl_num_videos, 1);
293
294         cl_videoplaying = true;
295
296         CL_SetVideoState( cl_videos, CLVIDEO_PLAY );
297         CL_RestartVideo( cl_videos );
298 }
299
300 void CL_Video_KeyEvent( int key, int ascii, qboolean down ) 
301 {
302         // only react to up events, to allow the user to delay the abortion point if it suddenly becomes interesting..
303         if( !down ) {
304                 if( key == K_ESCAPE || key == K_ENTER || key == K_SPACE ) {
305                         CL_VideoStop();
306                 }
307         }
308 }
309
310 void CL_VideoStop(void)
311 {
312         cl_videoplaying = false;
313
314         CL_CloseVideo( cl_videos );
315 }
316
317 static void CL_PlayVideo_f(void)
318 {
319         char name[MAX_QPATH];
320
321         Host_StartVideo();
322
323         if (Cmd_Argc() != 2)
324         {
325                 Con_Print("usage: playvideo <videoname>\nplays video named video/<videoname>.dpv\n");
326                 return;
327         }
328
329         dpsnprintf(name, sizeof(name), "video/%s.dpv", Cmd_Argv(1));
330         CL_VideoStart(name);
331 }
332
333 static void CL_StopVideo_f(void)
334 {
335         CL_VideoStop();
336 }
337
338 static void cl_video_start( void )
339 {
340         int i;
341         clvideo_t *video;
342
343         cl_videotexturepool = R_AllocTexturePool();
344
345         for( video = cl_videos, i = 0 ; i < cl_num_videos ; i++, video++ )
346                 if( video->state != CLVIDEO_UNUSED && !video->suspended )
347                         LinkVideoTexture( video );
348 }
349
350 static void cl_video_shutdown( void )
351 {
352         // TODO: unlink video textures?
353         R_FreeTexturePool( &cl_videotexturepool );
354 }
355
356 static void cl_video_newmap( void )
357 {
358 }
359
360 void CL_Video_Init( void )
361 {
362         union
363         {
364                 unsigned char b[4];
365                 unsigned int i;
366         }
367         bgra;
368
369         cl_num_videos = 0;
370         cl_videobytesperpixel = 4;
371
372         // set masks in an endian-independent way (as they really represent bytes)
373         bgra.i = 0;bgra.b[0] = 0xFF;cl_videobmask = bgra.i;
374         bgra.i = 0;bgra.b[1] = 0xFF;cl_videogmask = bgra.i;
375         bgra.i = 0;bgra.b[2] = 0xFF;cl_videormask = bgra.i;
376
377         Cmd_AddCommand( "playvideo", CL_PlayVideo_f, "play a .dpv video file" );
378         Cmd_AddCommand( "stopvideo", CL_StopVideo_f, "stop playing a .dpv video file" );
379
380         R_RegisterModule( "CL_Video", cl_video_start, cl_video_shutdown, cl_video_newmap );
381 }
382