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."};
15 cvar_t cl_video_keepaspectratio = {CVAR_SAVE, "cl_video_keepaspectratio", "0", "keeps aspect ratio of fullscreen videos, leaving black color on unfilled areas"};
17 // constants (and semi-constants)
18 static int cl_videormask;
19 static int cl_videobmask;
20 static int cl_videogmask;
21 static int cl_videobytesperpixel;
23 static int cl_num_videos;
24 static clvideo_t cl_videos[ MAXCLVIDEOS ];
25 static rtexturepool_t *cl_videotexturepool;
27 static clvideo_t *FindUnusedVid( void )
30 for( i = 1 ; i < MAXCLVIDEOS ; i++ )
31 if( cl_videos[ i ].state == CLVIDEO_UNUSED )
32 return &cl_videos[ i ];
36 static qboolean OpenStream( clvideo_t * video )
39 video->stream = dpvsimpledecode_open( video->filename, &errorstring);
42 Con_Printf("unable to open \"%s\", error: %s\n", video->filename, errorstring);
48 static void VideoUpdateCallback(rtexture_t *rt, void *data) {
49 clvideo_t *video = (clvideo_t *) data;
50 R_UpdateTexture( video->cpif.tex, (unsigned char *)video->imagedata, 0, 0, video->cpif.width, video->cpif.height );
53 static void LinkVideoTexture( clvideo_t *video )
55 video->cpif.tex = R_LoadTexture2D( cl_videotexturepool, video->cpif.name,
56 video->cpif.width, video->cpif.height, NULL, TEXTYPE_BGRA, TEXF_PERSISTENT | TEXF_CLAMP, -1, NULL );
57 R_MakeTextureDynamic( video->cpif.tex, VideoUpdateCallback, video );
58 CL_LinkDynTexture( video->cpif.name, video->cpif.tex );
61 static void UnlinkVideoTexture( clvideo_t *video ) {
62 CL_UnlinkDynTexture( video->cpif.name );
64 R_FreeTexture( video->cpif.tex );
65 video->cpif.tex = NULL;
66 // free the image data
67 Mem_Free( video->imagedata );
70 static void SuspendVideo( clvideo_t * video )
72 if( video->suspended )
74 video->suspended = true;
75 UnlinkVideoTexture( video );
76 // if we are in firstframe mode, also close the stream
77 if( video->state == CLVIDEO_FIRSTFRAME )
78 dpvsimpledecode_close( video->stream );
81 static qboolean WakeVideo( clvideo_t * video )
83 if( !video->suspended )
85 video->suspended = false;
87 if( video->state == CLVIDEO_FIRSTFRAME )
88 if( !OpenStream( video ) ) {
89 video->state = CLVIDEO_UNUSED;
93 video->imagedata = Mem_Alloc( cls.permanentmempool, video->cpif.width * video->cpif.height * cl_videobytesperpixel );
94 LinkVideoTexture( video );
97 video->starttime += realtime - video->lasttime;
102 static void LoadSubtitles( clvideo_t *video, const char *subtitlesfile )
106 float subtime, sublen;
109 if (gamemode == GAME_BLOODOMNICIDE)
111 char overridename[MAX_QPATH];
114 langcvar = Cvar_FindVar("language");
115 subtitle_text = NULL;
118 dpsnprintf(overridename, sizeof(overridename), "script/locale/%s/%s", langcvar->string, subtitlesfile);
119 subtitle_text = (char *)FS_LoadFile(overridename, cls.permanentmempool, false, NULL);
122 subtitle_text = (char *)FS_LoadFile(subtitlesfile, cls.permanentmempool, false, NULL);
126 subtitle_text = (char *)FS_LoadFile(subtitlesfile, cls.permanentmempool, false, NULL);
130 Con_DPrintf( "LoadSubtitles: can't open subtitle file '%s'!\n", subtitlesfile );
134 // parse subtitle_text
135 // line is: x y "text" where
137 // y - seconds last (if 0 - last thru next sub, if negative - last to next sub - this amount of seconds)
139 data = subtitle_text;
142 if (!COM_ParseToken_QuakeC(&data, false))
144 subtime = atof( com_token );
145 if (!COM_ParseToken_QuakeC(&data, false))
147 sublen = atof( com_token );
148 if (!COM_ParseToken_QuakeC(&data, false))
153 if (video->subtitles == CLVIDEO_MAX_SUBTITLES)
155 Con_Printf("WARNING: CLVIDEO_MAX_SUBTITLES = %i reached when reading subtitles from '%s'\n", CLVIDEO_MAX_SUBTITLES, subtitlesfile);
159 video->subtitle_text[numsubs] = (char *) Mem_Alloc(cls.permanentmempool, strlen(com_token) + 1);
160 memcpy(video->subtitle_text[numsubs], com_token, strlen(com_token) + 1);
161 video->subtitle_start[numsubs] = subtime;
162 video->subtitle_end[numsubs] = sublen;
163 if (numsubs > 0) // make true len for prev sub, autofix overlapping subtitles
165 if (video->subtitle_end[numsubs-1] <= 0)
166 video->subtitle_end[numsubs-1] = max(video->subtitle_start[numsubs-1], video->subtitle_start[numsubs] + video->subtitle_end[numsubs-1]);
168 video->subtitle_end[numsubs-1] = min(video->subtitle_start[numsubs-1] + video->subtitle_end[numsubs-1], video->subtitle_start[numsubs]);
171 // todo: check timing for consistency?
173 if (numsubs > 0) // make true len for prev sub, autofix overlapping subtitles
175 if (video->subtitle_end[numsubs-1] <= 0)
176 video->subtitle_end[numsubs-1] = 99999999; // fixme: make it end when video ends?
178 video->subtitle_end[numsubs-1] = video->subtitle_start[numsubs-1] + video->subtitle_end[numsubs-1];
180 Z_Free( subtitle_text );
181 video->subtitles = numsubs;
183 Con_Printf( "video->subtitles: %i\n", video->subtitles );
184 for (numsubs = 0; numsubs < video->subtitles; numsubs++)
185 Con_Printf( " %03.2f %03.2f : %s\n", video->subtitle_start[numsubs], video->subtitle_end[numsubs], video->subtitle_text[numsubs] );
189 static clvideo_t* OpenVideo( clvideo_t *video, const char *filename, const char *name, int owner, const char *subtitlesfile )
191 strlcpy( video->filename, filename, sizeof(video->filename) );
192 video->ownertag = owner;
193 if( strncmp( name, CLVIDEOPREFIX, sizeof( CLVIDEOPREFIX ) - 1 ) )
195 strlcpy( video->cpif.name, name, sizeof(video->cpif.name) );
197 if( !OpenStream( video ) )
200 video->state = CLVIDEO_FIRSTFRAME;
201 video->framenum = -1;
202 video->framerate = dpvsimpledecode_getframerate( video->stream );
203 video->lasttime = realtime;
204 video->subtitles = 0;
206 video->cpif.width = dpvsimpledecode_getwidth( video->stream );
207 video->cpif.height = dpvsimpledecode_getheight( video->stream );
208 video->imagedata = Mem_Alloc( cls.permanentmempool, video->cpif.width * video->cpif.height * cl_videobytesperpixel );
209 LinkVideoTexture( video );
211 // VorteX: load simple subtitle_text file
212 if (subtitlesfile[0])
213 LoadSubtitles( video, subtitlesfile );
218 clvideo_t* CL_OpenVideo( const char *filename, const char *name, int owner, const char *subtitlesfile )
222 if( !name || !*name || strncmp( name, CLVIDEOPREFIX, sizeof( CLVIDEOPREFIX ) - 1 ) != 0 ) {
223 Con_DPrintf( "CL_OpenVideo: Bad video texture name '%s'!\n", name );
227 video = FindUnusedVid();
229 Con_Printf( "CL_OpenVideo: unable to open video \"%s\" - video limit reached\n", filename );
232 video = OpenVideo( video, filename, name, owner, subtitlesfile );
233 // expand the active range to include the new entry
235 cl_num_videos = max(cl_num_videos, (int)(video - cl_videos) + 1);
240 static clvideo_t* CL_GetVideoBySlot( int slot )
242 clvideo_t *video = &cl_videos[ slot ];
244 if( video->suspended )
246 if( !WakeVideo( video ) )
248 else if( video->state == CLVIDEO_RESETONWAKEUP )
249 video->framenum = -1;
252 video->lasttime = realtime;
257 clvideo_t *CL_GetVideoByName( const char *name )
261 for( i = 0 ; i < cl_num_videos ; i++ )
262 if( cl_videos[ i ].state != CLVIDEO_UNUSED
263 && !strcmp( cl_videos[ i ].cpif.name , name ) )
265 if( i != cl_num_videos )
266 return CL_GetVideoBySlot( i );
271 void CL_SetVideoState( clvideo_t *video, clvideostate_t state )
276 video->lasttime = realtime;
277 video->state = state;
278 if( state == CLVIDEO_FIRSTFRAME )
279 CL_RestartVideo( video );
282 void CL_RestartVideo( clvideo_t *video )
287 video->starttime = video->lasttime = realtime;
288 video->framenum = -1;
290 dpvsimpledecode_close( video->stream );
291 if( !OpenStream( video ) )
292 video->state = CLVIDEO_UNUSED;
295 void CL_CloseVideo( clvideo_t * video )
299 if( !video || video->state == CLVIDEO_UNUSED )
302 if( !video->suspended || video->state != CLVIDEO_FIRSTFRAME )
303 dpvsimpledecode_close( video->stream );
304 if( !video->suspended )
305 UnlinkVideoTexture( video );
306 if (video->subtitles)
308 for (i = 0; i < video->subtitles; i++)
309 Z_Free( video->subtitle_text[i] );
310 video->subtitles = 0;
313 video->state = CLVIDEO_UNUSED;
316 static void VideoFrame( clvideo_t *video )
320 if( video->state == CLVIDEO_FIRSTFRAME )
323 destframe = (int)((realtime - video->starttime) * video->framerate);
326 if( video->framenum < destframe ) {
329 if( dpvsimpledecode_video( video->stream, video->imagedata, cl_videormask,
330 cl_videogmask, cl_videobmask, cl_videobytesperpixel,
331 cl_videobytesperpixel * video->cpif.width )
333 CL_RestartVideo( video );
334 if( video->state == CLVIDEO_PLAY )
335 video->state = CLVIDEO_FIRSTFRAME;
338 } while( video->framenum < destframe );
339 R_MarkDirtyTexture( video->cpif.tex );
343 void CL_Video_Frame( void ) // update all videos
351 for( video = cl_videos, i = 0 ; i < cl_num_videos ; video++, i++ )
352 if( video->state != CLVIDEO_UNUSED && !video->suspended )
354 if( realtime - video->lasttime > CLTHRESHOLD )
355 SuspendVideo( video );
356 else if( video->state == CLVIDEO_PAUSE )
357 video->starttime = realtime - video->framenum * video->framerate;
362 if( cl_videos->state == CLVIDEO_FIRSTFRAME )
365 // reduce range to exclude unnecessary entries
366 while (cl_num_videos > 0 && cl_videos[cl_num_videos-1].state == CLVIDEO_UNUSED)
370 void CL_Video_Shutdown( void )
373 for( i = 0 ; i < cl_num_videos ; i++ )
374 CL_CloseVideo( &cl_videos[ i ] );
377 void CL_PurgeOwner( int owner )
380 for( i = 0 ; i < cl_num_videos ; i++ )
381 if( cl_videos[ i ].ownertag == owner )
382 CL_CloseVideo( &cl_videos[ i ] );
392 float alignment; // 0 = left, 0.5 = center, 1 = right
396 cl_video_subtitle_info_t;
398 float CL_DrawVideo_WordWidthFunc(void *passthrough, const char *w, size_t *length, float maxWidth)
400 cl_video_subtitle_info_t *si = (cl_video_subtitle_info_t *) passthrough;
403 return si->fontsize * si->font->maxwidth;
405 return DrawQ_TextWidth_UntilWidth(w, length, si->fontsize, si->fontsize, false, si->font, -maxWidth); // -maxWidth: we want at least one char
406 else if(maxWidth == -1)
407 return DrawQ_TextWidth(w, *length, si->fontsize, si->fontsize, false, si->font);
412 int CL_DrawVideo_DisplaySubtitleLine(void *passthrough, const char *line, size_t length, float width, qboolean isContinuation)
414 cl_video_subtitle_info_t *si = (cl_video_subtitle_info_t *) passthrough;
416 int x = (int) (si->x + (si->width - width) * si->alignment);
418 DrawQ_String(x, si->y, line, length, si->fontsize, si->fontsize, 1.0, 1.0, 1.0, si->textalpha, 0, NULL, false, si->font);
419 si->y += si->fontsize;
423 int cl_videoplaying = false; // old, but still supported
425 void CL_DrawVideo(void)
428 float videotime, px, py, sx, sy;
429 cl_video_subtitle_info_t si;
432 if (!cl_videoplaying)
435 video = CL_GetVideoBySlot( 0 );
438 if (cl_video_scale.value <= 0 || cl_video_scale.value > 1)
439 Cvar_SetValueQuick( &cl_video_scale, 1);
440 if (cl_video_brightness.value <= 0 || cl_video_brightness.value > 10)
441 Cvar_SetValueQuick( &cl_video_brightness, 1);
443 // calc video proportions
446 sx = vid_conwidth.integer;
447 sy = vid_conheight.integer;
448 if (cl_video_keepaspectratio.integer)
450 float a = ((float)video->cpif.width / (float)video->cpif.height) / ((float)vid.width / (float)vid.height);
451 Con_Printf("%f\n", a);
452 if (a < 1.0) // scale horizontally
454 px += sx * (1 - a) * 0.5;
457 else if (a > 1.0) // scale vertically
464 if (cl_video_scale.value != 1)
466 px += sx * (1 - cl_video_scale.value) * 0.5;
467 py += sy * (1 - cl_video_scale.value) * ((bound(-1, cl_video_scale_vpos.value, 1) + 1) / 2);
468 sx *= cl_video_scale.value;
469 sy *= cl_video_scale.value;
472 // draw black bg in case stipple is active or video is scaled
473 if (cl_video_stipple.integer || px != 0 || py != 0 || sx != vid_conwidth.integer || sy != vid_conheight.integer)
474 DrawQ_Fill(0, 0, vid_conwidth.integer, vid_conheight.integer, 0, 0, 0, 1, 0);
476 // enable video-only polygon stipple (of global stipple is not active)
477 if (qglPolygonStipple && !scr_stipple.integer && cl_video_stipple.integer)
479 GLubyte stipple[128];
480 int i, s, width, parts;
482 s = cl_video_stipple.integer;
484 width = (s & 070) >> 3;
485 qglEnable(GL_POLYGON_STIPPLE);CHECKGLERROR // 0x0B42
486 for(i = 0; i < 128; ++i)
489 stipple[i] = ((line >> width) & ((1 << parts) - 1)) ? 0x00 : 0xFF;
491 qglPolygonStipple(stipple);CHECKGLERROR
495 DrawQ_Pic(px, py, &video->cpif, sx, sy, cl_video_brightness.value, cl_video_brightness.value, cl_video_brightness.value, 1, 0);
497 // disable video-only stipple
498 if (qglPolygonStipple && !scr_stipple.integer && cl_video_stipple.integer)
499 qglDisable(GL_POLYGON_STIPPLE);CHECKGLERROR
501 // VorteX: draw subtitle_text
502 if (!video->subtitles || !cl_video_subtitles.integer)
505 // find current subtitle
506 videotime = realtime - video->starttime;
507 for (i = 0; i < video->subtitles; i++)
509 if (videotime >= video->subtitle_start[i] && videotime <= video->subtitle_end[i])
512 si.font = FONT_NOTIFY;
513 si.x = vid_conwidth.integer * 0.1;
514 si.y = vid_conheight.integer - (max(1, cl_video_subtitles_lines.value) * cl_video_subtitles_textsize.value);
515 si.width = vid_conwidth.integer * 0.8;
516 si.height = max(1, cl_video_subtitles_lines.integer) * cl_video_subtitles_textsize.value;
518 si.fontsize = cl_video_subtitles_textsize.value;
519 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
520 COM_Wordwrap(video->subtitle_text[i], strlen(video->subtitle_text[i]), 0, si.width, CL_DrawVideo_WordWidthFunc, &si, CL_DrawVideo_DisplaySubtitleLine, &si);
526 void CL_VideoStart(char *filename, const char *subtitlesfile)
530 if( cl_videos->state != CLVIDEO_UNUSED )
531 CL_CloseVideo( cl_videos );
532 // already contains video/
533 if( !OpenVideo( cl_videos, filename, va( CLDYNTEXTUREPREFIX "%s", filename ), 0, subtitlesfile ) )
535 // expand the active range to include the new entry
536 cl_num_videos = max(cl_num_videos, 1);
538 cl_videoplaying = true;
540 CL_SetVideoState( cl_videos, CLVIDEO_PLAY );
541 CL_RestartVideo( cl_videos );
544 void CL_Video_KeyEvent( int key, int ascii, qboolean down )
546 // only react to up events, to allow the user to delay the abortion point if it suddenly becomes interesting..
548 if( key == K_ESCAPE || key == K_ENTER || key == K_SPACE ) {
554 void CL_VideoStop(void)
556 cl_videoplaying = false;
558 CL_CloseVideo( cl_videos );
561 static void CL_PlayVideo_f(void)
563 char name[MAX_QPATH], subtitlesfile[MAX_QPATH];
569 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");
573 dpsnprintf(name, sizeof(name), "video/%s.dpv", Cmd_Argv(1));
575 CL_VideoStart(name, Cmd_Argv(2));
578 dpsnprintf(subtitlesfile, sizeof(subtitlesfile), "video/%s.dpsubs", Cmd_Argv(1));
579 CL_VideoStart(name, subtitlesfile);
583 static void CL_StopVideo_f(void)
588 static void cl_video_start( void )
593 cl_videotexturepool = R_AllocTexturePool();
595 for( video = cl_videos, i = 0 ; i < cl_num_videos ; i++, video++ )
596 if( video->state != CLVIDEO_UNUSED && !video->suspended )
597 LinkVideoTexture( video );
600 static void cl_video_shutdown( void )
605 for( video = cl_videos, i = 0 ; i < cl_num_videos ; i++, video++ )
606 if( video->state != CLVIDEO_UNUSED && !video->suspended )
607 SuspendVideo( video );
608 R_FreeTexturePool( &cl_videotexturepool );
611 static void cl_video_newmap( void )
615 void CL_Video_Init( void )
625 cl_videobytesperpixel = 4;
627 // set masks in an endian-independent way (as they really represent bytes)
628 bgra.i = 0;bgra.b[0] = 0xFF;cl_videobmask = bgra.i;
629 bgra.i = 0;bgra.b[1] = 0xFF;cl_videogmask = bgra.i;
630 bgra.i = 0;bgra.b[2] = 0xFF;cl_videormask = bgra.i;
632 Cmd_AddCommand( "playvideo", CL_PlayVideo_f, "play a .dpv video file" );
633 Cmd_AddCommand( "stopvideo", CL_StopVideo_f, "stop playing a .dpv video file" );
635 Cvar_RegisterVariable(&cl_video_subtitles);
636 Cvar_RegisterVariable(&cl_video_subtitles_lines);
637 Cvar_RegisterVariable(&cl_video_subtitles_textsize);
638 Cvar_RegisterVariable(&cl_video_scale);
639 Cvar_RegisterVariable(&cl_video_scale_vpos);
640 Cvar_RegisterVariable(&cl_video_brightness);
641 Cvar_RegisterVariable(&cl_video_stipple);
642 Cvar_RegisterVariable(&cl_video_keepaspectratio);
644 R_RegisterModule( "CL_Video", cl_video_start, cl_video_shutdown, cl_video_newmap, NULL, NULL );