3 #include "cl_dyntexture.h"
5 #include "dpvsimpledecode.h"
7 // VorteX: JAM video module used by Blood Omnicide
10 #include "cl_video_jamdecode.c"
14 cvar_t cl_video_subtitles = {CVAR_SAVE, "cl_video_subtitles", "0", "show subtitles for videos (if they are present)"};
15 cvar_t cl_video_subtitles_lines = {CVAR_SAVE, "cl_video_subtitles_lines", "4", "how many lines to occupy for subtitles"};
16 cvar_t cl_video_subtitles_textsize = {CVAR_SAVE, "cl_video_subtitles_textsize", "16", "textsize for subtitles"};
17 cvar_t cl_video_scale = {CVAR_SAVE, "cl_video_scale", "1", "scale of video, 1 = fullscreen, 0.75 - 3/4 of screen etc."};
18 cvar_t cl_video_scale_vpos = {CVAR_SAVE, "cl_video_scale_vpos", "0", "vertical align of scaled video, -1 is top, 1 is bottom"};
19 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."};
20 cvar_t cl_video_brightness = {CVAR_SAVE, "cl_video_brightness", "1", "brightness of video, 1 = fullbright, 0.75 - 3/4 etc."};
21 cvar_t cl_video_keepaspectratio = {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"};
22 cvar_t cl_video_fadein = {CVAR_SAVE, "cl_video_fadein", "0", "fading-from-black effect once video is started, in seconds"};
23 cvar_t cl_video_fadeout = {CVAR_SAVE, "cl_video_fadeout", "0", "fading-to-black effect once video is ended, in seconds"};
25 // constants (and semi-constants)
26 static int cl_videormask;
27 static int cl_videobmask;
28 static int cl_videogmask;
29 static int cl_videobytesperpixel;
31 static int cl_num_videos;
32 static clvideo_t cl_videos[ MAXCLVIDEOS ];
33 static rtexturepool_t *cl_videotexturepool;
35 static clvideo_t *FindUnusedVid( void )
38 for( i = 1 ; i < MAXCLVIDEOS ; i++ )
39 if( cl_videos[ i ].state == CLVIDEO_UNUSED )
40 return &cl_videos[ i ];
44 static qboolean OpenStream( clvideo_t * video )
46 const char *errorstring;
47 video->stream = dpvsimpledecode_open( video, video->filename, &errorstring);
51 video->stream = jam_open( video, video->filename, &errorstring);
55 Con_Printf("unable to open \"%s\", error: %s\n", video->filename, errorstring);
61 static void VideoUpdateCallback(rtexture_t *rt, void *data)
63 clvideo_t *video = (clvideo_t *) data;
64 R_UpdateTexture( video->cpif.tex, (unsigned char *)video->imagedata, 0, 0, 0, video->cpif.width, video->cpif.height, 1 );
67 static void LinkVideoTexture( clvideo_t *video )
69 video->cpif.tex = R_LoadTexture2D( cl_videotexturepool, video->cpif.name, video->cpif.width, video->cpif.height, NULL, TEXTYPE_BGRA, TEXF_PERSISTENT | TEXF_CLAMP, -1, NULL );
70 R_MakeTextureDynamic( video->cpif.tex, VideoUpdateCallback, video );
71 CL_LinkDynTexture( video->cpif.name, video->cpif.tex );
74 static void UnlinkVideoTexture( clvideo_t *video )
76 CL_UnlinkDynTexture( video->cpif.name );
78 R_FreeTexture( video->cpif.tex );
79 video->cpif.tex = NULL;
80 // free the image data
81 Mem_Free( video->imagedata );
84 static void SuspendVideo( clvideo_t * video )
88 video->suspended = true;
89 UnlinkVideoTexture(video);
90 // if we are in firstframe mode, also close the stream
91 if (video->state == CLVIDEO_FIRSTFRAME)
92 video->close(video->stream);
95 static qboolean WakeVideo( clvideo_t * video )
97 if( !video->suspended )
99 video->suspended = false;
101 if( video->state == CLVIDEO_FIRSTFRAME )
102 if( !OpenStream( video ) ) {
103 video->state = CLVIDEO_UNUSED;
107 video->imagedata = Mem_Alloc( cls.permanentmempool, video->cpif.width * video->cpif.height * cl_videobytesperpixel );
108 LinkVideoTexture( video );
111 video->starttime += realtime - video->lasttime;
116 static void LoadSubtitles( clvideo_t *video, const char *subtitlesfile )
120 float subtime, sublen;
123 if (gamemode == GAME_BLOODOMNICIDE)
125 char overridename[MAX_QPATH];
128 langcvar = Cvar_FindVar("language");
129 subtitle_text = NULL;
132 dpsnprintf(overridename, sizeof(overridename), "script/locale/%s/%s", langcvar->string, subtitlesfile);
133 subtitle_text = (char *)FS_LoadFile(overridename, cls.permanentmempool, false, NULL);
136 subtitle_text = (char *)FS_LoadFile(subtitlesfile, cls.permanentmempool, false, NULL);
140 subtitle_text = (char *)FS_LoadFile(subtitlesfile, cls.permanentmempool, false, NULL);
144 Con_DPrintf( "LoadSubtitles: can't open subtitle file '%s'!\n", subtitlesfile );
148 // parse subtitle_text
149 // line is: x y "text" where
151 // y - seconds last (if 0 - last thru next sub, if negative - last to next sub - this amount of seconds)
153 data = subtitle_text;
156 if (!COM_ParseToken_QuakeC(&data, false))
158 subtime = atof( com_token );
159 if (!COM_ParseToken_QuakeC(&data, false))
161 sublen = atof( com_token );
162 if (!COM_ParseToken_QuakeC(&data, false))
167 if (video->subtitles == CLVIDEO_MAX_SUBTITLES)
169 Con_Printf("WARNING: CLVIDEO_MAX_SUBTITLES = %i reached when reading subtitles from '%s'\n", CLVIDEO_MAX_SUBTITLES, subtitlesfile);
173 video->subtitle_text[numsubs] = (char *) Mem_Alloc(cls.permanentmempool, strlen(com_token) + 1);
174 memcpy(video->subtitle_text[numsubs], com_token, strlen(com_token) + 1);
175 video->subtitle_start[numsubs] = subtime;
176 video->subtitle_end[numsubs] = sublen;
177 if (numsubs > 0) // make true len for prev sub, autofix overlapping subtitles
179 if (video->subtitle_end[numsubs-1] <= 0)
180 video->subtitle_end[numsubs-1] = max(video->subtitle_start[numsubs-1], video->subtitle_start[numsubs] + video->subtitle_end[numsubs-1]);
182 video->subtitle_end[numsubs-1] = min(video->subtitle_start[numsubs-1] + video->subtitle_end[numsubs-1], video->subtitle_start[numsubs]);
185 // todo: check timing for consistency?
187 if (numsubs > 0) // make true len for prev sub, autofix overlapping subtitles
189 if (video->subtitle_end[numsubs-1] <= 0)
190 video->subtitle_end[numsubs-1] = 99999999; // fixme: make it end when video ends?
192 video->subtitle_end[numsubs-1] = video->subtitle_start[numsubs-1] + video->subtitle_end[numsubs-1];
194 Z_Free( subtitle_text );
195 video->subtitles = numsubs;
197 Con_Printf( "video->subtitles: %i\n", video->subtitles );
198 for (numsubs = 0; numsubs < video->subtitles; numsubs++)
199 Con_Printf( " %03.2f %03.2f : %s\n", video->subtitle_start[numsubs], video->subtitle_end[numsubs], video->subtitle_text[numsubs] );
203 static clvideo_t* OpenVideo( clvideo_t *video, const char *filename, const char *name, int owner, const char *subtitlesfile )
205 strlcpy( video->filename, filename, sizeof(video->filename) );
206 video->ownertag = owner;
207 if( strncmp( name, CLVIDEOPREFIX, sizeof( CLVIDEOPREFIX ) - 1 ) )
209 strlcpy( video->cpif.name, name, sizeof(video->cpif.name) );
211 if( !OpenStream( video ) )
214 video->state = CLVIDEO_FIRSTFRAME;
215 video->framenum = -1;
216 video->framerate = video->getframerate( video->stream );
217 video->lasttime = realtime;
218 video->subtitles = 0;
220 video->cpif.width = video->getwidth( video->stream );
221 video->cpif.height = video->getheight( video->stream );
222 video->imagedata = Mem_Alloc( cls.permanentmempool, video->cpif.width * video->cpif.height * cl_videobytesperpixel );
223 LinkVideoTexture( video );
225 // VorteX: load simple subtitle_text file
226 if (subtitlesfile[0])
227 LoadSubtitles( video, subtitlesfile );
232 clvideo_t* CL_OpenVideo( const char *filename, const char *name, int owner, const char *subtitlesfile )
236 if( !name || !*name || strncmp( name, CLVIDEOPREFIX, sizeof( CLVIDEOPREFIX ) - 1 ) != 0 ) {
237 Con_DPrintf( "CL_OpenVideo: Bad video texture name '%s'!\n", name );
241 video = FindUnusedVid();
243 Con_Printf( "CL_OpenVideo: unable to open video \"%s\" - video limit reached\n", filename );
246 video = OpenVideo( video, filename, name, owner, subtitlesfile );
247 // expand the active range to include the new entry
249 cl_num_videos = max(cl_num_videos, (int)(video - cl_videos) + 1);
254 static clvideo_t* CL_GetVideoBySlot( int slot )
256 clvideo_t *video = &cl_videos[ slot ];
258 if( video->suspended )
260 if( !WakeVideo( video ) )
262 else if( video->state == CLVIDEO_RESETONWAKEUP )
263 video->framenum = -1;
266 video->lasttime = realtime;
271 clvideo_t *CL_GetVideoByName( const char *name )
275 for( i = 0 ; i < cl_num_videos ; i++ )
276 if( cl_videos[ i ].state != CLVIDEO_UNUSED
277 && !strcmp( cl_videos[ i ].cpif.name , name ) )
279 if( i != cl_num_videos )
280 return CL_GetVideoBySlot( i );
285 void CL_SetVideoState(clvideo_t *video, clvideostate_t state)
290 video->lasttime = realtime;
291 video->state = state;
292 if (state == CLVIDEO_FIRSTFRAME)
293 CL_RestartVideo(video);
296 void CL_RestartVideo(clvideo_t *video)
302 video->starttime = video->lasttime = realtime;
303 video->framenum = -1;
306 video->close(video->stream);
307 if (!OpenStream(video))
308 video->state = CLVIDEO_UNUSED;
312 void CL_CloseVideo(clvideo_t * video)
316 if (!video || video->state == CLVIDEO_UNUSED)
320 if (!video->suspended || video->state != CLVIDEO_FIRSTFRAME)
321 video->close(video->stream);
323 if (!video->suspended)
324 UnlinkVideoTexture(video);
326 if (video->subtitles)
328 for (i = 0; i < video->subtitles; i++)
329 Z_Free( video->subtitle_text[i] );
330 video->subtitles = 0;
332 video->state = CLVIDEO_UNUSED;
336 void CL_Video_Frame(void)
344 for (video = cl_videos, i = 0 ; i < cl_num_videos ; video++, i++)
346 if (video->state != CLVIDEO_UNUSED && !video->suspended)
348 if (realtime - video->lasttime > CLTHRESHOLD)
353 if (video->state == CLVIDEO_PAUSE)
355 video->starttime = realtime - video->framenum * video->framerate;
358 // read video frame from stream if time has come
359 if (video->state == CLVIDEO_FIRSTFRAME )
362 destframe = (int)((realtime - video->starttime) * video->framerate);
365 if (video->framenum < destframe)
369 if (video->decodeframe(video->stream, video->imagedata, cl_videormask, cl_videogmask, cl_videobmask, cl_videobytesperpixel, cl_videobytesperpixel * video->cpif.width))
372 CL_RestartVideo(video);
373 if (video->state == CLVIDEO_PLAY)
374 video->state = CLVIDEO_FIRSTFRAME;
377 } while(video->framenum < destframe);
378 R_MarkDirtyTexture(video->cpif.tex);
384 if (cl_videos->state == CLVIDEO_FIRSTFRAME)
387 // reduce range to exclude unnecessary entries
388 while(cl_num_videos > 0 && cl_videos[cl_num_videos-1].state == CLVIDEO_UNUSED)
392 void CL_Video_Shutdown( void )
395 for (i = 0 ; i < cl_num_videos ; i++)
396 CL_CloseVideo(&cl_videos[ i ]);
399 void CL_PurgeOwner( int owner )
403 for (i = 0 ; i < cl_num_videos ; i++)
404 if (cl_videos[i].ownertag == owner)
405 CL_CloseVideo(&cl_videos[i]);
415 float alignment; // 0 = left, 0.5 = center, 1 = right
419 cl_video_subtitle_info_t;
421 float CL_DrawVideo_WordWidthFunc(void *passthrough, const char *w, size_t *length, float maxWidth)
423 cl_video_subtitle_info_t *si = (cl_video_subtitle_info_t *) passthrough;
426 return si->fontsize * si->font->maxwidth;
428 return DrawQ_TextWidth_UntilWidth(w, length, si->fontsize, si->fontsize, false, si->font, -maxWidth); // -maxWidth: we want at least one char
429 else if(maxWidth == -1)
430 return DrawQ_TextWidth(w, *length, si->fontsize, si->fontsize, false, si->font);
435 int CL_DrawVideo_DisplaySubtitleLine(void *passthrough, const char *line, size_t length, float width, qboolean isContinuation)
437 cl_video_subtitle_info_t *si = (cl_video_subtitle_info_t *) passthrough;
439 int x = (int) (si->x + (si->width - width) * si->alignment);
441 DrawQ_String(x, si->y, line, length, si->fontsize, si->fontsize, 1.0, 1.0, 1.0, si->textalpha, 0, NULL, false, si->font);
442 si->y += si->fontsize;
446 int cl_videoplaying = false; // old, but still supported
448 void CL_DrawVideo(void)
451 float videotime, px, py, sx, sy, st[8], b;
452 cl_video_subtitle_info_t si;
455 if (!cl_videoplaying)
458 video = CL_GetVideoBySlot( 0 );
461 if (cl_video_scale.value <= 0 || cl_video_scale.value > 1)
462 Cvar_SetValueQuick( &cl_video_scale, 1);
463 if (cl_video_brightness.value <= 0 || cl_video_brightness.value > 10)
464 Cvar_SetValueQuick( &cl_video_brightness, 1);
466 // calc video proportions
469 sx = vid_conwidth.integer;
470 sy = vid_conheight.integer;
471 st[0] = 0.0; st[1] = 0.0;
472 st[2] = 1.0; st[3] = 0.0;
473 st[4] = 0.0; st[5] = 1.0;
474 st[6] = 1.0; st[7] = 1.0;
475 if (cl_video_keepaspectratio.integer)
477 float a = ((float)video->cpif.width / (float)video->cpif.height) / ((float)vid.width / (float)vid.height);
478 if (cl_video_keepaspectratio.integer >= 2)
480 // clip instead of scale
481 if (a < 1.0) // clip horizontally
483 st[1] = st[3] = (1 - a)*0.5;
484 st[5] = st[7] = 1 - (1 - a)*0.5;
486 else if (a > 1.0) // clip vertically
488 st[0] = st[4] = (1 - 1/a)*0.5;
489 st[2] = st[6] = (1/a)*0.5;
492 else if (a < 1.0) // scale horizontally
494 px += sx * (1 - a) * 0.5;
497 else if (a > 1.0) // scale vertically
505 if (cl_video_scale.value != 1)
507 px += sx * (1 - cl_video_scale.value) * 0.5;
508 py += sy * (1 - cl_video_scale.value) * ((bound(-1, cl_video_scale_vpos.value, 1) + 1) / 2);
509 sx *= cl_video_scale.value;
510 sy *= cl_video_scale.value;
513 // calc brightness for fadein and fadeout effects
514 b = cl_video_brightness.value;
515 if (cl_video_fadein.value && (realtime - video->starttime) < cl_video_fadein.value)
516 b = pow((realtime - video->starttime)/cl_video_fadein.value, 2);
517 else if (cl_video_fadeout.value && ((video->starttime + video->framenum * video->framerate) - realtime) < cl_video_fadeout.value)
518 b = pow(((video->starttime + video->framenum * video->framerate) - realtime)/cl_video_fadeout.value, 2);
520 // draw black bg in case stipple is active or video is scaled
521 if (cl_video_stipple.integer || px != 0 || py != 0 || sx != vid_conwidth.integer || sy != vid_conheight.integer)
522 DrawQ_Fill(0, 0, vid_conwidth.integer, vid_conheight.integer, 0, 0, 0, 1, 0);
525 // enable video-only polygon stipple (of global stipple is not active)
526 if (qglPolygonStipple && !scr_stipple.integer && cl_video_stipple.integer)
528 GLubyte stipple[128];
529 int i, s, width, parts;
531 s = cl_video_stipple.integer;
533 width = (s & 070) >> 3;
534 qglEnable(GL_POLYGON_STIPPLE);CHECKGLERROR // 0x0B42
535 for(i = 0; i < 128; ++i)
538 stipple[i] = ((line >> width) & ((1 << parts) - 1)) ? 0x00 : 0xFF;
540 qglPolygonStipple(stipple);CHECKGLERROR
545 DrawQ_SuperPic(px, py, &video->cpif, 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 // disable video-only stipple
549 if (qglPolygonStipple && !scr_stipple.integer && cl_video_stipple.integer)
550 qglDisable(GL_POLYGON_STIPPLE);CHECKGLERROR
553 // VorteX: draw subtitle_text
554 if (!video->subtitles || !cl_video_subtitles.integer)
557 // find current subtitle
558 videotime = 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, va( CLDYNTEXTUREPREFIX "%s", 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(void)
615 char name[MAX_QPATH], subtitlesfile[MAX_QPATH];
616 const char *extension;
620 if (COM_CheckParm("-benchmark"))
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(1));
631 dpsnprintf(name, sizeof(name), "video/%s", Cmd_Argv(1));
633 dpsnprintf(name, sizeof(name), "video/%s.dpv", Cmd_Argv(1));
635 CL_VideoStart(name, Cmd_Argv(2));
638 dpsnprintf(subtitlesfile, sizeof(subtitlesfile), "video/%s.dpsubs", Cmd_Argv(1));
639 CL_VideoStart(name, subtitlesfile);
643 static void CL_StopVideo_f(void)
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( "playvideo", CL_PlayVideo_f, "play a .dpv video file" );
693 Cmd_AddCommand( "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 R_RegisterModule( "CL_Video", cl_video_start, cl_video_shutdown, cl_video_newmap, NULL, NULL );