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