]> git.xonotic.org Git - xonotic/darkplaces.git/blob - cl_video.c
a9f2fc4df6564d6f817c3b924acec305ea9e08c3
[xonotic/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 = 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_ALWAYSPRECACHE | TEXF_PERSISTENT, 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                 if( developer.integer > 0 ) {
120                         Con_Printf( "CL_OpenVideo: Bad video texture name '%s'!\n", name );
121                 }
122                 return NULL;
123         }
124
125         video = FindUnusedVid();
126         if( !video ) {
127                 Con_Printf( "CL_OpenVideo: unable to open video \"%s\" - video limit reached\n", filename );
128                 return NULL;
129         }
130         video = OpenVideo( video, filename, name, owner );
131         // expand the active range to include the new entry
132         if (video) {
133                 cl_num_videos = max(cl_num_videos, (int)(video - cl_videos) + 1);
134         }
135         return video;
136 }
137
138 static clvideo_t* CL_GetVideoBySlot( int slot )
139 {
140         clvideo_t *video = &cl_videos[ slot ];
141
142         if( video->suspended )
143         {
144                 if( !WakeVideo( video ) )
145                         return NULL;
146                 else if( video->state == CLVIDEO_RESETONWAKEUP )
147                         video->framenum = -1;
148         }
149
150         video->lasttime = realtime;
151
152         return video;
153 }
154
155 clvideo_t *CL_GetVideoByName( const char *name )
156 {
157         int i;
158
159         for( i = 0 ; i < cl_num_videos ; i++ )
160                 if( cl_videos[ i ].state != CLVIDEO_UNUSED
161                         &&      !strcmp( cl_videos[ i ].cpif.name , name ) )
162                         break;
163         if( i != cl_num_videos )
164                 return CL_GetVideoBySlot( i );
165         else
166                 return NULL;
167 }
168
169 void CL_SetVideoState( clvideo_t *video, clvideostate_t state )
170 {
171         if( !video )
172                 return;
173
174         video->lasttime = realtime;
175         video->state = state;
176         if( state == CLVIDEO_FIRSTFRAME )
177                 CL_RestartVideo( video );
178 }
179
180 void CL_RestartVideo( clvideo_t *video )
181 {
182         if( !video )
183                 return;
184
185         video->starttime = video->lasttime = realtime;
186         video->framenum = -1;
187
188         dpvsimpledecode_close( video->stream );
189         if( !OpenStream( video ) )
190                 video->state = CLVIDEO_UNUSED;
191 }
192
193 void CL_CloseVideo( clvideo_t * video )
194 {
195         if( !video || video->state == CLVIDEO_UNUSED )
196                 return;
197
198         if( !video->suspended || video->state != CLVIDEO_FIRSTFRAME )
199                 dpvsimpledecode_close( video->stream );
200         if( !video->suspended ) {
201                 UnlinkVideoTexture( video );
202         }
203
204         video->state = CLVIDEO_UNUSED;
205 }
206
207 static void VideoFrame( clvideo_t *video )
208 {
209         int destframe;
210
211         if( video->state == CLVIDEO_FIRSTFRAME )
212                 destframe = 0;
213         else
214                 destframe = (int)((realtime - video->starttime) * video->framerate);
215         if( destframe < 0 )
216                 destframe = 0;
217         if( video->framenum < destframe ) {
218                 do {
219                         video->framenum++;
220                         if( dpvsimpledecode_video( video->stream, video->imagedata, cl_videormask,
221                                 cl_videogmask, cl_videobmask, cl_videobytesperpixel,
222                                 cl_videobytesperpixel * video->cpif.width )
223                                 ) { // finished?
224                                 CL_RestartVideo( video );
225                                 if( video->state == CLVIDEO_PLAY )
226                                                 video->state = CLVIDEO_FIRSTFRAME;
227                                 return;
228                         }
229                 } while( video->framenum < destframe );
230                 R_MarkDirtyTexture( video->cpif.tex );
231         }
232 }
233
234 void CL_Video_Frame( void ) // update all videos
235 {
236         int i;
237         clvideo_t *video;
238
239         if (!cl_num_videos)
240                 return;
241
242         for( video = cl_videos, i = 0 ; i < cl_num_videos ; video++, i++ )
243                 if( video->state != CLVIDEO_UNUSED && !video->suspended )
244                 {
245                         if( realtime - video->lasttime > CLTHRESHOLD )
246                                 SuspendVideo( video );
247                         else if( video->state == CLVIDEO_PAUSE )
248                                 video->starttime = realtime - video->framenum * video->framerate;
249                         else
250                                 VideoFrame( video );
251                 }
252
253         if( cl_videos->state == CLVIDEO_FIRSTFRAME )
254                 CL_VideoStop();
255
256         // reduce range to exclude unnecessary entries
257         while (cl_num_videos > 0 && cl_videos[cl_num_videos-1].state == CLVIDEO_UNUSED)
258                 cl_num_videos--;
259 }
260
261 void CL_Video_Shutdown( void )
262 {
263         int i;
264         for( i = 0 ; i < cl_num_videos ; i++ )
265                 CL_CloseVideo( &cl_videos[ i ] );
266 }
267
268 void CL_PurgeOwner( int owner )
269 {
270         int i;
271         for( i = 0 ; i < cl_num_videos ; i++ )
272                 if( cl_videos[ i ].ownertag == owner )
273                         CL_CloseVideo( &cl_videos[ i ] );
274 }
275
276 int cl_videoplaying = false; // old, but still supported
277
278 void CL_DrawVideo(void)
279 {
280         if (cl_videoplaying)
281                 DrawQ_Pic(0, 0, &CL_GetVideoBySlot( 0 )->cpif, vid_conwidth.integer, vid_conheight.integer, 1, 1, 1, 1, 0);
282 }
283
284 void CL_VideoStart(char *filename)
285 {
286         Host_StartVideo();
287
288         if( cl_videos->state != CLVIDEO_UNUSED )
289                 CL_CloseVideo( cl_videos );
290         // already contains video/
291         if( !OpenVideo( cl_videos, filename, va( CLDYNTEXTUREPREFIX "%s", filename ), 0 ) )
292                 return;
293         // expand the active range to include the new entry
294         cl_num_videos = max(cl_num_videos, 1);
295
296         cl_videoplaying = true;
297
298         CL_SetVideoState( cl_videos, CLVIDEO_PLAY );
299         CL_RestartVideo( cl_videos );
300 }
301
302 void CL_Video_KeyEvent( int key, int ascii, qboolean down ) 
303 {
304         // only react to up events, to allow the user to delay the abortion point if it suddenly becomes interesting..
305         if( !down ) {
306                 if( key == K_ESCAPE || key == K_ENTER || key == K_SPACE ) {
307                         CL_VideoStop();
308                 }
309         }
310 }
311
312 void CL_VideoStop(void)
313 {
314         cl_videoplaying = false;
315
316         CL_CloseVideo( cl_videos );
317 }
318
319 static void CL_PlayVideo_f(void)
320 {
321         char name[MAX_QPATH];
322
323         Host_StartVideo();
324
325         if (Cmd_Argc() != 2)
326         {
327                 Con_Print("usage: playvideo <videoname>\nplays video named video/<videoname>.dpv\n");
328                 return;
329         }
330
331         sprintf(name, "video/%s.dpv", Cmd_Argv(1));
332         CL_VideoStart(name);
333 }
334
335 static void CL_StopVideo_f(void)
336 {
337         CL_VideoStop();
338 }
339
340 static void cl_video_start( void )
341 {
342         int i;
343         clvideo_t *video;
344
345         cl_videotexturepool = R_AllocTexturePool();
346
347         for( video = cl_videos, i = 0 ; i < cl_num_videos ; i++, video++ )
348                 if( video->state != CLVIDEO_UNUSED && !video->suspended )
349                         LinkVideoTexture( video );
350 }
351
352 static void cl_video_shutdown( void )
353 {
354         // TODO: unlink video textures?
355         R_FreeTexturePool( &cl_videotexturepool );
356 }
357
358 static void cl_video_newmap( void )
359 {
360 }
361
362 void CL_Video_Init( void )
363 {
364         union
365         {
366                 unsigned char b[4];
367                 unsigned int i;
368         }
369         bgra;
370
371         cl_num_videos = 0;
372         cl_videobytesperpixel = 4;
373
374         // set masks in an endian-independent way (as they really represent bytes)
375         bgra.i = 0;bgra.b[0] = 0xFF;cl_videobmask = bgra.i;
376         bgra.i = 0;bgra.b[1] = 0xFF;cl_videogmask = bgra.i;
377         bgra.i = 0;bgra.b[2] = 0xFF;cl_videormask = bgra.i;
378
379         Cmd_AddCommand( "playvideo", CL_PlayVideo_f, "play a .dpv video file" );
380         Cmd_AddCommand( "stopvideo", CL_StopVideo_f, "stop playing a .dpv video file" );
381
382         R_RegisterModule( "CL_Video", cl_video_start, cl_video_shutdown, cl_video_newmap );
383 }
384