3 #include "cl_dyntexture.h"
5 #include "dpvsimpledecode.h"
8 cvar_t cl_video_subtitles = {CVAR_SAVE, "cl_video_subtitles", "0", "show subtitles for videos (if they are presented)"};
9 cvar_t cl_video_subtitles_lines = {CVAR_SAVE, "cl_video_subtitles_lines", "4", "how many lines to occupy for subtitles"};
10 cvar_t cl_video_subtitles_textsize = {CVAR_SAVE, "cl_video_subtitles_textsize", "16", "textsize for subtitles"};
11 cvar_t cl_video_scale = {CVAR_SAVE, "cl_video_scale", "1", "scale of video, 1 = fullscreen, 0.75 - 3/4 of screen etc."};
12 cvar_t cl_video_scale_vpos = {CVAR_SAVE, "cl_video_scale_vpos", "0", "vertial align of scaled video, -1 is top, 1 is bottom"};
13 cvar_t cl_video_stipple = {CVAR_SAVE, "cl_video_stipple", "0", "draw interlacing-like effect on videos, similar to scr_stipple but static and used only with video playing."};
14 cvar_t cl_video_brightness = {CVAR_SAVE, "cl_video_brightness", "1", "brightness of video, 1 = fullbright, 0.75 - 3/4 etc."};
16 // constants (and semi-constants)
17 static int cl_videormask;
18 static int cl_videobmask;
19 static int cl_videogmask;
20 static int cl_videobytesperpixel;
22 static int cl_num_videos;
23 static clvideo_t cl_videos[ MAXCLVIDEOS ];
24 static rtexturepool_t *cl_videotexturepool;
26 static clvideo_t *FindUnusedVid( void )
29 for( i = 1 ; i < MAXCLVIDEOS ; i++ )
30 if( cl_videos[ i ].state == CLVIDEO_UNUSED )
31 return &cl_videos[ i ];
35 static qboolean OpenStream( clvideo_t * video )
37 const char *errorstring;
38 video->stream = dpvsimpledecode_open( video->filename, &errorstring);
41 Con_Printf("unable to open \"%s\", error: %s\n", video->filename, errorstring);
47 static void VideoUpdateCallback(rtexture_t *rt, void *data) {
48 clvideo_t *video = (clvideo_t *) data;
49 R_UpdateTexture( video->cpif.tex, (unsigned char *)video->imagedata, 0, 0, video->cpif.width, video->cpif.height );
52 static void LinkVideoTexture( clvideo_t *video ) {
53 video->cpif.tex = R_LoadTexture2D( cl_videotexturepool, video->cpif.name,
54 video->cpif.width, video->cpif.height, NULL, TEXTYPE_BGRA, TEXF_PERSISTENT, -1, NULL );
55 R_MakeTextureDynamic( video->cpif.tex, VideoUpdateCallback, video );
56 CL_LinkDynTexture( video->cpif.name, video->cpif.tex );
59 static void UnlinkVideoTexture( clvideo_t *video ) {
60 CL_UnlinkDynTexture( video->cpif.name );
62 R_FreeTexture( video->cpif.tex );
63 // free the image data
64 Mem_Free( video->imagedata );
67 static void SuspendVideo( clvideo_t * video )
69 if( video->suspended )
71 video->suspended = true;
72 UnlinkVideoTexture( video );
73 // if we are in firstframe mode, also close the stream
74 if( video->state == CLVIDEO_FIRSTFRAME )
75 dpvsimpledecode_close( video->stream );
78 static qboolean WakeVideo( clvideo_t * video )
80 if( !video->suspended )
82 video->suspended = false;
84 if( video->state == CLVIDEO_FIRSTFRAME )
85 if( !OpenStream( video ) ) {
86 video->state = CLVIDEO_UNUSED;
90 video->imagedata = Mem_Alloc( cls.permanentmempool, video->cpif.width * video->cpif.height * cl_videobytesperpixel );
91 LinkVideoTexture( video );
94 video->starttime += realtime - video->lasttime;
99 static void LoadSubtitles( clvideo_t *video, const char *subtitlesfile )
103 float subtime, sublen;
106 if (gamemode == GAME_BLOODOMNICIDE)
108 char overridename[MAX_QPATH];
111 langcvar = Cvar_FindVar("language");
112 subtitle_text = NULL;
115 dpsnprintf(overridename, sizeof(overridename), "script/locale/%s/%s", langcvar->string, subtitlesfile);
116 subtitle_text = (char *)FS_LoadFile(overridename, cls.permanentmempool, false, NULL);
119 subtitle_text = (char *)FS_LoadFile(subtitlesfile, cls.permanentmempool, false, NULL);
123 subtitle_text = (char *)FS_LoadFile(subtitlesfile, cls.permanentmempool, false, NULL);
127 Con_DPrintf( "LoadSubtitles: can't open subtitle file '%s'!\n", subtitlesfile );
131 // parse subtitle_text
132 // line is: x y "text" where
134 // y - seconds last (if 0 - last thru next sub, if negative - last to next sub - this amount of seconds)
136 data = subtitle_text;
139 if (!COM_ParseToken_QuakeC(&data, false))
141 subtime = atof( com_token );
142 if (!COM_ParseToken_QuakeC(&data, false))
144 sublen = atof( com_token );
145 if (!COM_ParseToken_QuakeC(&data, false))
150 if (video->subtitles == CLVIDEO_MAX_SUBTITLES)
152 Con_Printf("WARNING: CLVIDEO_MAX_SUBTITLES = %i reached when reading subtitles from '%s'\n", CLVIDEO_MAX_SUBTITLES, subtitlesfile);
156 video->subtitle_text[numsubs] = (char *) Mem_Alloc(cls.permanentmempool, strlen(com_token) + 1);
157 memcpy(video->subtitle_text[numsubs], com_token, strlen(com_token) + 1);
158 video->subtitle_start[numsubs] = subtime;
159 video->subtitle_end[numsubs] = sublen;
160 if (numsubs > 0) // make true len for prev sub, autofix overlapping subtitles
162 if (video->subtitle_end[numsubs-1] <= 0)
163 video->subtitle_end[numsubs-1] = max(video->subtitle_start[numsubs-1], video->subtitle_start[numsubs] + video->subtitle_end[numsubs-1]);
165 video->subtitle_end[numsubs-1] = min(video->subtitle_start[numsubs-1] + video->subtitle_end[numsubs-1], video->subtitle_start[numsubs]);
168 // todo: check timing for consistency?
170 if (numsubs > 0) // make true len for prev sub, autofix overlapping subtitles
172 if (video->subtitle_end[numsubs-1] <= 0)
173 video->subtitle_end[numsubs-1] = 99999999; // fixme: make it end when video ends?
175 video->subtitle_end[numsubs-1] = video->subtitle_start[numsubs-1] + video->subtitle_end[numsubs-1];
177 Z_Free( subtitle_text );
178 video->subtitles = numsubs;
180 Con_Printf( "video->subtitles: %i\n", video->subtitles );
181 for (numsubs = 0; numsubs < video->subtitles; numsubs++)
182 Con_Printf( " %03.2f %03.2f : %s\n", video->subtitle_start[numsubs], video->subtitle_end[numsubs], video->subtitle_text[numsubs] );
186 static clvideo_t* OpenVideo( clvideo_t *video, const char *filename, const char *name, int owner, const char *subtitlesfile )
188 strlcpy( video->filename, filename, sizeof(video->filename) );
189 video->ownertag = owner;
190 if( strncmp( name, CLVIDEOPREFIX, sizeof( CLVIDEOPREFIX ) - 1 ) )
192 strlcpy( video->cpif.name, name, sizeof(video->cpif.name) );
194 if( !OpenStream( video ) )
197 video->state = CLVIDEO_FIRSTFRAME;
198 video->framenum = -1;
199 video->framerate = dpvsimpledecode_getframerate( video->stream );
200 video->lasttime = realtime;
201 video->subtitles = 0;
203 video->cpif.width = dpvsimpledecode_getwidth( video->stream );
204 video->cpif.height = dpvsimpledecode_getheight( video->stream );
205 video->imagedata = Mem_Alloc( cls.permanentmempool, video->cpif.width * video->cpif.height * cl_videobytesperpixel );
206 LinkVideoTexture( video );
208 // VorteX: load simple subtitle_text file
209 if (subtitlesfile[0])
210 LoadSubtitles( video, subtitlesfile );
215 clvideo_t* CL_OpenVideo( const char *filename, const char *name, int owner, const char *subtitlesfile )
219 if( !name || !*name || strncmp( name, CLVIDEOPREFIX, sizeof( CLVIDEOPREFIX ) - 1 ) != 0 ) {
220 Con_DPrintf( "CL_OpenVideo: Bad video texture name '%s'!\n", name );
224 video = FindUnusedVid();
226 Con_Printf( "CL_OpenVideo: unable to open video \"%s\" - video limit reached\n", filename );
229 video = OpenVideo( video, filename, name, owner, subtitlesfile );
230 // expand the active range to include the new entry
232 cl_num_videos = max(cl_num_videos, (int)(video - cl_videos) + 1);
237 static clvideo_t* CL_GetVideoBySlot( int slot )
239 clvideo_t *video = &cl_videos[ slot ];
241 if( video->suspended )
243 if( !WakeVideo( video ) )
245 else if( video->state == CLVIDEO_RESETONWAKEUP )
246 video->framenum = -1;
249 video->lasttime = realtime;
254 clvideo_t *CL_GetVideoByName( const char *name )
258 for( i = 0 ; i < cl_num_videos ; i++ )
259 if( cl_videos[ i ].state != CLVIDEO_UNUSED
260 && !strcmp( cl_videos[ i ].cpif.name , name ) )
262 if( i != cl_num_videos )
263 return CL_GetVideoBySlot( i );
268 void CL_SetVideoState( clvideo_t *video, clvideostate_t state )
273 video->lasttime = realtime;
274 video->state = state;
275 if( state == CLVIDEO_FIRSTFRAME )
276 CL_RestartVideo( video );
279 void CL_RestartVideo( clvideo_t *video )
284 video->starttime = video->lasttime = realtime;
285 video->framenum = -1;
287 dpvsimpledecode_close( video->stream );
288 if( !OpenStream( video ) )
289 video->state = CLVIDEO_UNUSED;
292 void CL_CloseVideo( clvideo_t * video )
296 if( !video || video->state == CLVIDEO_UNUSED )
299 if( !video->suspended || video->state != CLVIDEO_FIRSTFRAME )
300 dpvsimpledecode_close( video->stream );
301 if( !video->suspended )
302 UnlinkVideoTexture( video );
303 if (video->subtitles)
305 for (i = 0; i < video->subtitles; i++)
306 Z_Free( video->subtitle_text[i] );
307 video->subtitles = 0;
310 video->state = CLVIDEO_UNUSED;
313 static void VideoFrame( clvideo_t *video )
317 if( video->state == CLVIDEO_FIRSTFRAME )
320 destframe = (int)((realtime - video->starttime) * video->framerate);
323 if( video->framenum < destframe ) {
326 if( dpvsimpledecode_video( video->stream, video->imagedata, cl_videormask,
327 cl_videogmask, cl_videobmask, cl_videobytesperpixel,
328 cl_videobytesperpixel * video->cpif.width )
330 CL_RestartVideo( video );
331 if( video->state == CLVIDEO_PLAY )
332 video->state = CLVIDEO_FIRSTFRAME;
335 } while( video->framenum < destframe );
336 R_MarkDirtyTexture( video->cpif.tex );
340 void CL_Video_Frame( void ) // update all videos
348 for( video = cl_videos, i = 0 ; i < cl_num_videos ; video++, i++ )
349 if( video->state != CLVIDEO_UNUSED && !video->suspended )
351 if( realtime - video->lasttime > CLTHRESHOLD )
352 SuspendVideo( video );
353 else if( video->state == CLVIDEO_PAUSE )
354 video->starttime = realtime - video->framenum * video->framerate;
359 if( cl_videos->state == CLVIDEO_FIRSTFRAME )
362 // reduce range to exclude unnecessary entries
363 while (cl_num_videos > 0 && cl_videos[cl_num_videos-1].state == CLVIDEO_UNUSED)
367 void CL_Video_Shutdown( void )
370 for( i = 0 ; i < cl_num_videos ; i++ )
371 CL_CloseVideo( &cl_videos[ i ] );
374 void CL_PurgeOwner( int owner )
377 for( i = 0 ; i < cl_num_videos ; i++ )
378 if( cl_videos[ i ].ownertag == owner )
379 CL_CloseVideo( &cl_videos[ i ] );
389 float alignment; // 0 = left, 0.5 = center, 1 = right
393 cl_video_subtitle_info_t;
395 float CL_DrawVideo_WordWidthFunc(void *passthrough, const char *w, size_t *length, float maxWidth)
397 cl_video_subtitle_info_t *si = (cl_video_subtitle_info_t *) passthrough;
400 return si->fontsize * si->font->maxwidth;
402 return DrawQ_TextWidth_UntilWidth(w, length, si->fontsize, si->fontsize, false, si->font, -maxWidth); // -maxWidth: we want at least one char
403 else if(maxWidth == -1)
404 return DrawQ_TextWidth(w, *length, si->fontsize, si->fontsize, false, si->font);
409 int CL_DrawVideo_DisplaySubtitleLine(void *passthrough, const char *line, size_t length, float width, qboolean isContinuation)
411 cl_video_subtitle_info_t *si = (cl_video_subtitle_info_t *) passthrough;
413 int x = (int) (si->x + (si->width - width) * si->alignment);
415 DrawQ_String(x, si->y, line, length, si->fontsize, si->fontsize, 1.0, 1.0, 1.0, si->textalpha, 0, NULL, false, si->font);
416 si->y += si->fontsize;
420 int cl_videoplaying = false; // old, but still supported
422 void CL_DrawVideo(void)
426 cl_video_subtitle_info_t si;
429 if (!cl_videoplaying)
432 video = CL_GetVideoBySlot( 0 );
435 if (cl_video_scale.value <= 0 || cl_video_scale.value > 1)
436 Cvar_SetValueQuick( &cl_video_scale, 1);
437 if (cl_video_brightness.value <= 0 || cl_video_brightness.value > 10)
438 Cvar_SetValueQuick( &cl_video_brightness, 1);
440 // draw black bg in case stipple is active or video is scaled
441 if (cl_video_stipple.integer || cl_video_scale.value != 1)
442 DrawQ_Fill(0, 0, vid_conwidth.integer, vid_conheight.integer, 0, 0, 0, 1, 0);
444 // enable video-only polygon stipple (of global stipple is not active)
445 if (qglPolygonStipple && !scr_stipple.integer && cl_video_stipple.integer)
447 GLubyte stipple[128];
448 int i, s, width, parts;
450 s = cl_video_stipple.integer;
452 width = (s & 070) >> 3;
453 qglEnable(GL_POLYGON_STIPPLE);CHECKGLERROR // 0x0B42
454 for(i = 0; i < 128; ++i)
457 stipple[i] = ((line >> width) & ((1 << parts) - 1)) ? 0x00 : 0xFF;
459 qglPolygonStipple(stipple);CHECKGLERROR
463 if (cl_video_scale.value == 1)
464 DrawQ_Pic(0, 0, &video->cpif, vid_conwidth.integer, vid_conheight.integer, cl_video_brightness.value, cl_video_brightness.value, cl_video_brightness.value, 1, 0);
467 int px = (int)(vid_conwidth.integer * (1 - cl_video_scale.value) * 0.5);
468 int py = (int)(vid_conheight.integer * (1 - cl_video_scale.value) * ((bound(-1, cl_video_scale_vpos.value, 1) + 1) / 2));
469 int sx = (int)(vid_conwidth.integer * cl_video_scale.value);
470 int sy = (int)(vid_conheight.integer * cl_video_scale.value);
471 DrawQ_Pic(px, py, &video->cpif, sx , sy, cl_video_brightness.value, cl_video_brightness.value, cl_video_brightness.value, 1, 0);
474 // disable video-only stipple
475 if (qglPolygonStipple && !scr_stipple.integer && cl_video_stipple.integer)
476 qglDisable(GL_POLYGON_STIPPLE);CHECKGLERROR
478 // VorteX: draw subtitle_text
479 if (!video->subtitles || !cl_video_subtitles.integer)
482 // find current subtitle
483 videotime = realtime - video->starttime;
484 for (i = 0; i < video->subtitles; i++)
486 if (videotime >= video->subtitle_start[i] && videotime <= video->subtitle_end[i])
489 si.font = FONT_NOTIFY;
490 si.x = vid_conwidth.integer * 0.1;
491 si.y = vid_conheight.integer - (max(1, cl_video_subtitles_lines.value) * cl_video_subtitles_textsize.value);
492 si.width = vid_conwidth.integer * 0.8;
493 si.height = max(1, cl_video_subtitles_lines.integer) * cl_video_subtitles_textsize.value;
495 si.fontsize = cl_video_subtitles_textsize.value;
496 si.textalpha = min(1, (videotime - video->subtitle_start[i])/0.5) * min(1, ((video->subtitle_end[i] - videotime)/0.3)); // fade in and fade out
497 COM_Wordwrap(video->subtitle_text[i], strlen(video->subtitle_text[i]), 0, si.width, CL_DrawVideo_WordWidthFunc, &si, CL_DrawVideo_DisplaySubtitleLine, &si);
503 void CL_VideoStart(char *filename, const char *subtitlesfile)
507 if( cl_videos->state != CLVIDEO_UNUSED )
508 CL_CloseVideo( cl_videos );
509 // already contains video/
510 if( !OpenVideo( cl_videos, filename, va( CLDYNTEXTUREPREFIX "%s", filename ), 0, subtitlesfile ) )
512 // expand the active range to include the new entry
513 cl_num_videos = max(cl_num_videos, 1);
515 cl_videoplaying = true;
517 CL_SetVideoState( cl_videos, CLVIDEO_PLAY );
518 CL_RestartVideo( cl_videos );
521 void CL_Video_KeyEvent( int key, int ascii, qboolean down )
523 // only react to up events, to allow the user to delay the abortion point if it suddenly becomes interesting..
525 if( key == K_ESCAPE || key == K_ENTER || key == K_SPACE ) {
531 void CL_VideoStop(void)
533 cl_videoplaying = false;
535 CL_CloseVideo( cl_videos );
538 static void CL_PlayVideo_f(void)
540 char name[MAX_QPATH], subtitlesfile[MAX_QPATH];
546 Con_Print("usage: playvideo <videoname> [custom_subtitles_file]\nplays video named video/<videoname>.dpv\nif custom subtitles file is not presented\nit tries video/<videoname>.sub");
550 dpsnprintf(name, sizeof(name), "video/%s.dpv", Cmd_Argv(1));
552 CL_VideoStart(name, Cmd_Argv(2));
555 dpsnprintf(subtitlesfile, sizeof(subtitlesfile), "video/%s.dpsubs", Cmd_Argv(1));
556 CL_VideoStart(name, subtitlesfile);
560 static void CL_StopVideo_f(void)
565 static void cl_video_start( void )
570 cl_videotexturepool = R_AllocTexturePool();
572 for( video = cl_videos, i = 0 ; i < cl_num_videos ; i++, video++ )
573 if( video->state != CLVIDEO_UNUSED && !video->suspended )
574 LinkVideoTexture( video );
577 static void cl_video_shutdown( void )
579 // TODO: unlink video textures?
580 R_FreeTexturePool( &cl_videotexturepool );
583 static void cl_video_newmap( void )
587 void CL_Video_Init( void )
597 cl_videobytesperpixel = 4;
599 // set masks in an endian-independent way (as they really represent bytes)
600 bgra.i = 0;bgra.b[0] = 0xFF;cl_videobmask = bgra.i;
601 bgra.i = 0;bgra.b[1] = 0xFF;cl_videogmask = bgra.i;
602 bgra.i = 0;bgra.b[2] = 0xFF;cl_videormask = bgra.i;
604 Cmd_AddCommand( "playvideo", CL_PlayVideo_f, "play a .dpv video file" );
605 Cmd_AddCommand( "stopvideo", CL_StopVideo_f, "stop playing a .dpv video file" );
607 Cvar_RegisterVariable(&cl_video_subtitles);
608 Cvar_RegisterVariable(&cl_video_subtitles_lines);
609 Cvar_RegisterVariable(&cl_video_subtitles_textsize);
610 Cvar_RegisterVariable(&cl_video_scale);
611 Cvar_RegisterVariable(&cl_video_scale_vpos);
612 Cvar_RegisterVariable(&cl_video_brightness);
613 Cvar_RegisterVariable(&cl_video_stipple);
615 R_RegisterModule( "CL_Video", cl_video_start, cl_video_shutdown, cl_video_newmap, NULL, NULL );