6 cvar_t cl_video_subtitles = {CVAR_CLIENT | CVAR_SAVE, "cl_video_subtitles", "0", "show subtitles for videos (if they are present)"};
7 cvar_t cl_video_subtitles_lines = {CVAR_CLIENT | CVAR_SAVE, "cl_video_subtitles_lines", "4", "how many lines to occupy for subtitles"};
8 cvar_t cl_video_subtitles_textsize = {CVAR_CLIENT | CVAR_SAVE, "cl_video_subtitles_textsize", "16", "textsize for subtitles"};
9 cvar_t cl_video_scale = {CVAR_CLIENT | CVAR_SAVE, "cl_video_scale", "1", "scale of video, 1 = fullscreen, 0.75 - 3/4 of screen etc."};
10 cvar_t cl_video_scale_vpos = {CVAR_CLIENT | CVAR_SAVE, "cl_video_scale_vpos", "0", "vertical align of scaled video, -1 is top, 1 is bottom"};
11 cvar_t cl_video_stipple = {CVAR_CLIENT | CVAR_SAVE, "cl_video_stipple", "0", "draw interlacing-like effect on videos, similar to scr_stipple but static and used only with video playing."};
12 cvar_t cl_video_brightness = {CVAR_CLIENT | CVAR_SAVE, "cl_video_brightness", "1", "brightness of video, 1 = fullbright, 0.75 - 3/4 etc."};
13 cvar_t cl_video_keepaspectratio = {CVAR_CLIENT | CVAR_SAVE, "cl_video_keepaspectratio", "0", "keeps aspect ratio of fullscreen videos, leaving black color on unfilled areas, a value of 2 let video to be stretched horizontally with top & bottom being sliced out"};
14 cvar_t cl_video_fadein = {CVAR_CLIENT | CVAR_SAVE, "cl_video_fadein", "0", "fading-from-black effect once video is started, in seconds"};
15 cvar_t cl_video_fadeout = {CVAR_CLIENT | CVAR_SAVE, "cl_video_fadeout", "0", "fading-to-black effect once video is ended, in seconds"};
17 cvar_t v_glslgamma_video = {CVAR_CLIENT | CVAR_SAVE, "v_glslgamma_video", "1", "applies GLSL gamma to played video, could be a fraction, requires r_glslgamma_2d 1."};
20 #include "dpvsimpledecode.h"
22 // VorteX: libavcodec implementation
23 #include "cl_video_libavw.c"
25 // JAM video decoder used by Blood Omnicide
27 #include "cl_video_jamdecode.c"
30 // constants (and semi-constants)
31 static int cl_videormask;
32 static int cl_videobmask;
33 static int cl_videogmask;
34 static int cl_videobytesperpixel;
36 static int cl_num_videos;
37 static clvideo_t cl_videos[ MAXCLVIDEOS ];
38 static rtexturepool_t *cl_videotexturepool;
40 static clvideo_t *FindUnusedVid( void )
43 for( i = 1 ; i < MAXCLVIDEOS ; i++ )
44 if( cl_videos[ i ].state == CLVIDEO_UNUSED )
45 return &cl_videos[ i ];
49 static qboolean OpenStream( clvideo_t * video )
51 const char *errorstring;
53 video->stream = dpvsimpledecode_open( video, video->filename, &errorstring);
58 video->stream = jam_open( video, video->filename, &errorstring);
63 video->stream = LibAvW_OpenVideo( video, video->filename, &errorstring);
67 Con_Printf(CON_ERROR "unable to open \"%s\", error: %s\n", video->filename, errorstring);
71 static void VideoUpdateCallback(rtexture_t *rt, void *data)
73 clvideo_t *video = (clvideo_t *) data;
74 Draw_NewPic(video->name, video->width, video->height, (unsigned char *)video->imagedata, TEXTYPE_BGRA, TEXF_CLAMP);
77 static void LinkVideoTexture( clvideo_t *video )
79 video->cachepic = Draw_NewPic(video->name, video->width, video->height, NULL, TEXTYPE_BGRA, TEXF_CLAMP);
80 // make R_GetTexture() call our VideoUpdateCallback
81 R_MakeTextureDynamic(Draw_GetPicTexture(video->cachepic), VideoUpdateCallback, video);
84 static void UnlinkVideoTexture( clvideo_t *video )
86 // free the texture (this does not destroy the cachepic_t, which is eternal)
87 Draw_FreePic(video->name);
88 // free the image data
89 Mem_Free( video->imagedata );
92 static void SuspendVideo( clvideo_t * video )
96 video->suspended = true;
97 UnlinkVideoTexture(video);
98 // if we are in firstframe mode, also close the stream
99 if (video->state == CLVIDEO_FIRSTFRAME)
102 video->close(video->stream);
103 video->stream = NULL;
107 static qboolean WakeVideo( clvideo_t * video )
109 if( !video->suspended )
111 video->suspended = false;
113 if( video->state == CLVIDEO_FIRSTFRAME )
114 if( !OpenStream( video ) ) {
115 video->state = CLVIDEO_UNUSED;
119 video->imagedata = Mem_Alloc( cls.permanentmempool, video->width * video->height * cl_videobytesperpixel );
120 LinkVideoTexture( video );
123 video->starttime += host.realtime - video->lasttime;
128 static void LoadSubtitles( clvideo_t *video, const char *subtitlesfile )
132 float subtime, sublen;
135 if (gamemode == GAME_BLOODOMNICIDE)
137 char overridename[MAX_QPATH];
140 langcvar = Cvar_FindVar(&cvars_all, "language", CVAR_CLIENT | CVAR_SERVER);
141 subtitle_text = NULL;
144 dpsnprintf(overridename, sizeof(overridename), "locale/%s/%s", langcvar->string, subtitlesfile);
145 subtitle_text = (char *)FS_LoadFile(overridename, cls.permanentmempool, false, NULL);
148 subtitle_text = (char *)FS_LoadFile(subtitlesfile, cls.permanentmempool, false, NULL);
152 subtitle_text = (char *)FS_LoadFile(subtitlesfile, cls.permanentmempool, false, NULL);
156 Con_DPrintf( "LoadSubtitles: can't open subtitle file '%s'!\n", subtitlesfile );
160 // parse subtitle_text
161 // line is: x y "text" where
163 // y - seconds last (if 0 - last thru next sub, if negative - last to next sub - this amount of seconds)
165 data = subtitle_text;
168 if (!COM_ParseToken_QuakeC(&data, false))
170 subtime = atof( com_token );
171 if (!COM_ParseToken_QuakeC(&data, false))
173 sublen = atof( com_token );
174 if (!COM_ParseToken_QuakeC(&data, false))
179 if (video->subtitles == CLVIDEO_MAX_SUBTITLES)
181 Con_Printf(CON_WARN "WARNING: CLVIDEO_MAX_SUBTITLES = %i reached when reading subtitles from '%s'\n", CLVIDEO_MAX_SUBTITLES, subtitlesfile);
185 video->subtitle_text[numsubs] = (char *) Mem_Alloc(cls.permanentmempool, strlen(com_token) + 1);
186 memcpy(video->subtitle_text[numsubs], com_token, strlen(com_token) + 1);
187 video->subtitle_start[numsubs] = subtime;
188 video->subtitle_end[numsubs] = sublen;
189 if (numsubs > 0) // make true len for prev sub, autofix overlapping subtitles
191 if (video->subtitle_end[numsubs-1] <= 0)
192 video->subtitle_end[numsubs-1] = max(video->subtitle_start[numsubs-1], video->subtitle_start[numsubs] + video->subtitle_end[numsubs-1]);
194 video->subtitle_end[numsubs-1] = min(video->subtitle_start[numsubs-1] + video->subtitle_end[numsubs-1], video->subtitle_start[numsubs]);
197 // todo: check timing for consistency?
199 if (numsubs > 0) // make true len for prev sub, autofix overlapping subtitles
201 if (video->subtitle_end[numsubs-1] <= 0)
202 video->subtitle_end[numsubs-1] = 99999999; // fixme: make it end when video ends?
204 video->subtitle_end[numsubs-1] = video->subtitle_start[numsubs-1] + video->subtitle_end[numsubs-1];
206 Z_Free( subtitle_text );
207 video->subtitles = numsubs;
209 Con_Printf( "video->subtitles: %i\n", video->subtitles );
210 for (numsubs = 0; numsubs < video->subtitles; numsubs++)
211 Con_Printf( " %03.2f %03.2f : %s\n", video->subtitle_start[numsubs], video->subtitle_end[numsubs], video->subtitle_text[numsubs] );
215 static clvideo_t* OpenVideo( clvideo_t *video, const char *filename, const char *name, int owner, const char *subtitlesfile )
217 strlcpy(video->filename, filename, sizeof(video->filename));
218 dpsnprintf(video->name, sizeof(video->name), CLVIDEOPREFIX "%s", name);
219 video->ownertag = owner;
220 if( strncmp( name, CLVIDEOPREFIX, sizeof( CLVIDEOPREFIX ) - 1 ) )
222 video->cachepic = Draw_CachePic_Flags(name, CACHEPICFLAG_NOTPERSISTENT | CACHEPICFLAG_QUIET);
224 if( !OpenStream( video ) )
227 video->state = CLVIDEO_FIRSTFRAME;
228 video->framenum = -1;
229 video->framerate = video->getframerate( video->stream );
230 video->lasttime = host.realtime;
231 video->subtitles = 0;
233 video->width = video->getwidth( video->stream );
234 video->height = video->getheight( video->stream );
235 video->imagedata = Mem_Alloc( cls.permanentmempool, video->width * video->height * cl_videobytesperpixel );
236 LinkVideoTexture( video );
238 // VorteX: load simple subtitle_text file
239 if (subtitlesfile[0])
240 LoadSubtitles( video, subtitlesfile );
245 clvideo_t* CL_OpenVideo( const char *filename, const char *name, int owner, const char *subtitlesfile )
249 if( !name || !*name || strncmp( name, CLVIDEOPREFIX, sizeof( CLVIDEOPREFIX ) - 1 ) != 0 ) {
250 Con_DPrintf( "CL_OpenVideo: Bad video texture name '%s'!\n", name );
254 video = FindUnusedVid();
256 Con_Printf(CON_ERROR "CL_OpenVideo: unable to open video \"%s\" - video limit reached\n", filename );
259 video = OpenVideo( video, filename, name, owner, subtitlesfile );
260 // expand the active range to include the new entry
262 cl_num_videos = max(cl_num_videos, (int)(video - cl_videos) + 1);
267 static clvideo_t* CL_GetVideoBySlot( int slot )
269 clvideo_t *video = &cl_videos[ slot ];
271 if( video->suspended )
273 if( !WakeVideo( video ) )
275 else if( video->state == CLVIDEO_RESETONWAKEUP )
276 video->framenum = -1;
279 video->lasttime = host.realtime;
284 clvideo_t *CL_GetVideoByName( const char *name )
288 for( i = 0 ; i < cl_num_videos ; i++ )
289 if( cl_videos[ i ].state != CLVIDEO_UNUSED
290 && !strcmp( cl_videos[ i ].name , name ) )
292 if( i != cl_num_videos )
293 return CL_GetVideoBySlot( i );
298 void CL_SetVideoState(clvideo_t *video, clvideostate_t state)
303 video->lasttime = host.realtime;
304 video->state = state;
305 if (state == CLVIDEO_FIRSTFRAME)
306 CL_RestartVideo(video);
309 void CL_RestartVideo(clvideo_t *video)
315 video->starttime = video->lasttime = host.realtime;
316 video->framenum = -1;
320 video->close(video->stream);
321 video->stream = NULL;
322 if (!OpenStream(video))
323 video->state = CLVIDEO_UNUSED;
327 void CL_CloseVideo(clvideo_t * video)
331 if (!video || video->state == CLVIDEO_UNUSED)
335 if (!video->suspended || video->state != CLVIDEO_FIRSTFRAME)
338 video->close(video->stream);
339 video->stream = NULL;
342 if (!video->suspended)
343 UnlinkVideoTexture(video);
345 if (video->subtitles)
347 for (i = 0; i < video->subtitles; i++)
348 Z_Free( video->subtitle_text[i] );
349 video->subtitles = 0;
351 video->state = CLVIDEO_UNUSED;
355 void CL_Video_Frame(void)
363 for (video = cl_videos, i = 0 ; i < cl_num_videos ; video++, i++)
365 if (video->state != CLVIDEO_UNUSED && !video->suspended)
367 if (host.realtime - video->lasttime > CLTHRESHOLD)
372 if (video->state == CLVIDEO_PAUSE)
374 video->starttime = host.realtime - video->framenum * video->framerate;
377 // read video frame from stream if time has come
378 if (video->state == CLVIDEO_FIRSTFRAME )
381 destframe = (int)((host.realtime - video->starttime) * video->framerate);
384 if (video->framenum < destframe)
388 if (video->decodeframe(video->stream, video->imagedata, cl_videormask, cl_videogmask, cl_videobmask, cl_videobytesperpixel, cl_videobytesperpixel * video->width))
391 CL_RestartVideo(video);
392 if (video->state == CLVIDEO_PLAY)
393 video->state = CLVIDEO_FIRSTFRAME;
396 } while(video->framenum < destframe);
397 R_MarkDirtyTexture(Draw_GetPicTexture(video->cachepic));
403 if (cl_videos->state == CLVIDEO_FIRSTFRAME)
406 // reduce range to exclude unnecessary entries
407 while(cl_num_videos > 0 && cl_videos[cl_num_videos-1].state == CLVIDEO_UNUSED)
411 void CL_PurgeOwner( int owner )
415 for (i = 0 ; i < cl_num_videos ; i++)
416 if (cl_videos[i].ownertag == owner)
417 CL_CloseVideo(&cl_videos[i]);
427 float alignment; // 0 = left, 0.5 = center, 1 = right
431 cl_video_subtitle_info_t;
433 static float CL_DrawVideo_WordWidthFunc(void *passthrough, const char *w, size_t *length, float maxWidth)
435 cl_video_subtitle_info_t *si = (cl_video_subtitle_info_t *) passthrough;
438 return si->fontsize * si->font->maxwidth;
440 return DrawQ_TextWidth_UntilWidth(w, length, si->fontsize, si->fontsize, false, si->font, -maxWidth); // -maxWidth: we want at least one char
441 else if(maxWidth == -1)
442 return DrawQ_TextWidth(w, *length, si->fontsize, si->fontsize, false, si->font);
447 static int CL_DrawVideo_DisplaySubtitleLine(void *passthrough, const char *line, size_t length, float width, qboolean isContinuation)
449 cl_video_subtitle_info_t *si = (cl_video_subtitle_info_t *) passthrough;
451 int x = (int) (si->x + (si->width - width) * si->alignment);
453 DrawQ_String(x, si->y, line, length, si->fontsize, si->fontsize, 1.0, 1.0, 1.0, si->textalpha, 0, NULL, false, si->font);
454 si->y += si->fontsize;
458 int cl_videoplaying = false; // old, but still supported
460 void CL_DrawVideo(void)
463 float videotime, px, py, sx, sy, st[8], b;
464 cl_video_subtitle_info_t si;
467 if (!cl_videoplaying)
470 video = CL_GetVideoBySlot( 0 );
473 if (cl_video_scale.value <= 0 || cl_video_scale.value > 1)
474 Cvar_SetValueQuick( &cl_video_scale, 1);
475 if (cl_video_brightness.value <= 0 || cl_video_brightness.value > 10)
476 Cvar_SetValueQuick( &cl_video_brightness, 1);
478 // calc video proportions
481 sx = vid_conwidth.integer;
482 sy = vid_conheight.integer;
483 st[0] = 0.0; st[1] = 0.0;
484 st[2] = 1.0; st[3] = 0.0;
485 st[4] = 0.0; st[5] = 1.0;
486 st[6] = 1.0; st[7] = 1.0;
487 if (cl_video_keepaspectratio.integer)
489 float a = video->getaspectratio(video->stream) / ((float)vid.width / (float)vid.height);
490 if (cl_video_keepaspectratio.integer >= 2)
492 // clip instead of scale
493 if (a < 1.0) // clip horizontally
495 st[1] = st[3] = (1 - a)*0.5;
496 st[5] = st[7] = 1 - (1 - a)*0.5;
498 else if (a > 1.0) // clip vertically
500 st[0] = st[4] = (1 - 1/a)*0.5;
501 st[2] = st[6] = (1/a)*0.5;
504 else if (a < 1.0) // scale horizontally
506 px += sx * (1 - a) * 0.5;
509 else if (a > 1.0) // scale vertically
517 if (cl_video_scale.value != 1)
519 px += sx * (1 - cl_video_scale.value) * 0.5;
520 py += sy * (1 - cl_video_scale.value) * ((bound(-1, cl_video_scale_vpos.value, 1) + 1) / 2);
521 sx *= cl_video_scale.value;
522 sy *= cl_video_scale.value;
525 // calc brightness for fadein and fadeout effects
526 b = cl_video_brightness.value;
527 if (cl_video_fadein.value && (host.realtime - video->starttime) < cl_video_fadein.value)
528 b = pow((host.realtime - video->starttime)/cl_video_fadein.value, 2);
529 else if (cl_video_fadeout.value && ((video->starttime + video->framenum * video->framerate) - host.realtime) < cl_video_fadeout.value)
530 b = pow(((video->starttime + video->framenum * video->framerate) - host.realtime)/cl_video_fadeout.value, 2);
532 // draw black bg in case stipple is active or video is scaled
533 if (cl_video_stipple.integer || px != 0 || py != 0 || sx != vid_conwidth.integer || sy != vid_conheight.integer)
534 DrawQ_Fill(0, 0, vid_conwidth.integer, vid_conheight.integer, 0, 0, 0, 1, 0);
536 // enable video-only polygon stipple (of global stipple is not active)
537 if (!scr_stipple.integer && cl_video_stipple.integer)
539 Con_Print("FIXME: cl_video_stipple not implemented\n");
540 Cvar_SetValueQuick(&cl_video_stipple, 0);
544 if (v_glslgamma_video.value >= 1)
545 DrawQ_SuperPic(px, py, video->cachepic, sx, sy, st[0], st[1], b, b, b, 1, st[2], st[3], b, b, b, 1, st[4], st[5], b, b, b, 1, st[6], st[7], b, b, b, 1, 0);
548 DrawQ_SuperPic(px, py, video->cachepic, sx, sy, st[0], st[1], b, b, b, 1, st[2], st[3], b, b, b, 1, st[4], st[5], b, b, b, 1, st[6], st[7], b, b, b, 1, DRAWFLAG_NOGAMMA);
549 if (v_glslgamma_video.value > 0.0)
550 DrawQ_SuperPic(px, py, video->cachepic, sx, sy, st[0], st[1], b, b, b, v_glslgamma_video.value, st[2], st[3], b, b, b, v_glslgamma_video.value, st[4], st[5], b, b, b, v_glslgamma_video.value, st[6], st[7], b, b, b, v_glslgamma_video.value, 0);
553 // VorteX: draw subtitle_text
554 if (!video->subtitles || !cl_video_subtitles.integer)
557 // find current subtitle
558 videotime = host.realtime - video->starttime;
559 for (i = 0; i < video->subtitles; i++)
561 if (videotime >= video->subtitle_start[i] && videotime <= video->subtitle_end[i])
564 si.font = FONT_NOTIFY;
565 si.x = vid_conwidth.integer * 0.1;
566 si.y = vid_conheight.integer - (max(1, cl_video_subtitles_lines.value) * cl_video_subtitles_textsize.value);
567 si.width = vid_conwidth.integer * 0.8;
568 si.height = max(1, cl_video_subtitles_lines.integer) * cl_video_subtitles_textsize.value;
570 si.fontsize = cl_video_subtitles_textsize.value;
571 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
572 COM_Wordwrap(video->subtitle_text[i], strlen(video->subtitle_text[i]), 0, si.width, CL_DrawVideo_WordWidthFunc, &si, CL_DrawVideo_DisplaySubtitleLine, &si);
578 void CL_VideoStart(char *filename, const char *subtitlesfile)
582 if( cl_videos->state != CLVIDEO_UNUSED )
583 CL_CloseVideo( cl_videos );
584 // already contains video/
585 if( !OpenVideo( cl_videos, filename, filename, 0, subtitlesfile ) )
587 // expand the active range to include the new entry
588 cl_num_videos = max(cl_num_videos, 1);
590 cl_videoplaying = true;
592 CL_SetVideoState( cl_videos, CLVIDEO_PLAY );
593 CL_RestartVideo( cl_videos );
596 void CL_Video_KeyEvent( int key, int ascii, qboolean down )
598 // only react to up events, to allow the user to delay the abortion point if it suddenly becomes interesting..
600 if( key == K_ESCAPE || key == K_ENTER || key == K_SPACE ) {
606 void CL_VideoStop(void)
608 cl_videoplaying = false;
610 CL_CloseVideo( cl_videos );
613 static void CL_PlayVideo_f(cmd_state_t *cmd)
615 char name[MAX_QPATH], subtitlesfile[MAX_QPATH];
616 const char *extension;
620 if (COM_CheckParm("-benchmark"))
623 if (Cmd_Argc(cmd) < 2)
625 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");
629 extension = FS_FileExtension(Cmd_Argv(cmd, 1));
631 dpsnprintf(name, sizeof(name), "video/%s", Cmd_Argv(cmd, 1));
633 dpsnprintf(name, sizeof(name), "video/%s.dpv", Cmd_Argv(cmd, 1));
634 if ( Cmd_Argc(cmd) > 2)
635 CL_VideoStart(name, Cmd_Argv(cmd, 2));
638 dpsnprintf(subtitlesfile, sizeof(subtitlesfile), "video/%s.dpsubs", Cmd_Argv(cmd, 1));
639 CL_VideoStart(name, subtitlesfile);
643 static void CL_StopVideo_f(cmd_state_t *cmd)
648 static void cl_video_start( void )
653 cl_videotexturepool = R_AllocTexturePool();
655 for( video = cl_videos, i = 0 ; i < cl_num_videos ; i++, video++ )
656 if( video->state != CLVIDEO_UNUSED && !video->suspended )
657 LinkVideoTexture( video );
660 static void cl_video_shutdown( void )
665 for( video = cl_videos, i = 0 ; i < cl_num_videos ; i++, video++ )
666 if( video->state != CLVIDEO_UNUSED && !video->suspended )
667 SuspendVideo( video );
668 R_FreeTexturePool( &cl_videotexturepool );
671 static void cl_video_newmap( void )
675 void CL_Video_Init( void )
685 cl_videobytesperpixel = 4;
687 // set masks in an endian-independent way (as they really represent bytes)
688 bgra.i = 0;bgra.b[0] = 0xFF;cl_videobmask = bgra.i;
689 bgra.i = 0;bgra.b[1] = 0xFF;cl_videogmask = bgra.i;
690 bgra.i = 0;bgra.b[2] = 0xFF;cl_videormask = bgra.i;
692 Cmd_AddCommand(CMD_CLIENT, "playvideo", CL_PlayVideo_f, "play a .dpv video file" );
693 Cmd_AddCommand(CMD_CLIENT, "stopvideo", CL_StopVideo_f, "stop playing a .dpv video file" );
695 Cvar_RegisterVariable(&cl_video_subtitles);
696 Cvar_RegisterVariable(&cl_video_subtitles_lines);
697 Cvar_RegisterVariable(&cl_video_subtitles_textsize);
698 Cvar_RegisterVariable(&cl_video_scale);
699 Cvar_RegisterVariable(&cl_video_scale_vpos);
700 Cvar_RegisterVariable(&cl_video_brightness);
701 Cvar_RegisterVariable(&cl_video_stipple);
702 Cvar_RegisterVariable(&cl_video_keepaspectratio);
703 Cvar_RegisterVariable(&cl_video_fadein);
704 Cvar_RegisterVariable(&cl_video_fadeout);
706 Cvar_RegisterVariable(&v_glslgamma_video);
708 R_RegisterModule( "CL_Video", cl_video_start, cl_video_shutdown, cl_video_newmap, NULL, NULL );
710 LibAvW_OpenLibrary();
713 void CL_Video_Shutdown( void )
717 for (i = 0 ; i < cl_num_videos ; i++)
718 CL_CloseVideo(&cl_videos[ i ]);
720 LibAvW_CloseLibrary();