3 #include "cl_dyntexture.h"
7 cvar_t cl_video_subtitles = {CVAR_SAVE, "cl_video_subtitles", "0", "show subtitles for videos (if they are present)"};
8 cvar_t cl_video_subtitles_lines = {CVAR_SAVE, "cl_video_subtitles_lines", "4", "how many lines to occupy for subtitles"};
9 cvar_t cl_video_subtitles_textsize = {CVAR_SAVE, "cl_video_subtitles_textsize", "16", "textsize for subtitles"};
10 cvar_t cl_video_scale = {CVAR_SAVE, "cl_video_scale", "1", "scale of video, 1 = fullscreen, 0.75 - 3/4 of screen etc."};
11 cvar_t cl_video_scale_vpos = {CVAR_SAVE, "cl_video_scale_vpos", "0", "vertical align of scaled video, -1 is top, 1 is bottom"};
12 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."};
13 cvar_t cl_video_brightness = {CVAR_SAVE, "cl_video_brightness", "1", "brightness of video, 1 = fullbright, 0.75 - 3/4 etc."};
14 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"};
15 cvar_t cl_video_fadein = {CVAR_SAVE, "cl_video_fadein", "0", "fading-from-black effect once video is started, in seconds"};
16 cvar_t cl_video_fadeout = {CVAR_SAVE, "cl_video_fadeout", "0", "fading-to-black effect once video is ended, in seconds"};
18 cvar_t v_glslgamma_video = {CVAR_SAVE, "v_glslgamma_video", "1", "applies GLSL gamma to played video, could be a fraction, requires r_glslgamma_2d 1."};
21 #include "dpvsimpledecode.h"
23 // VorteX: libavcodec implementation
24 #include "cl_video_libavw.c"
26 // JAM video decoder used by Blood Omnicide
28 #include "cl_video_jamdecode.c"
31 // constants (and semi-constants)
32 static int cl_videormask;
33 static int cl_videobmask;
34 static int cl_videogmask;
35 static int cl_videobytesperpixel;
37 static int cl_num_videos;
38 static clvideo_t cl_videos[ MAXCLVIDEOS ];
39 static rtexturepool_t *cl_videotexturepool;
41 static clvideo_t *FindUnusedVid( void )
44 for( i = 1 ; i < MAXCLVIDEOS ; i++ )
45 if( cl_videos[ i ].state == CLVIDEO_UNUSED )
46 return &cl_videos[ i ];
50 static qboolean OpenStream( clvideo_t * video )
52 const char *errorstring;
54 video->stream = dpvsimpledecode_open( video, video->filename, &errorstring);
59 video->stream = jam_open( video, video->filename, &errorstring);
64 video->stream = LibAvW_OpenVideo( video, video->filename, &errorstring);
68 Con_Printf("unable to open \"%s\", error: %s\n", video->filename, errorstring);
72 static void VideoUpdateCallback(rtexture_t *rt, void *data)
74 clvideo_t *video = (clvideo_t *) data;
75 R_UpdateTexture( video->cpif.tex, (unsigned char *)video->imagedata, 0, 0, 0, video->cpif.width, video->cpif.height, 1 );
78 static void LinkVideoTexture( clvideo_t *video )
80 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 );
81 R_MakeTextureDynamic( video->cpif.tex, VideoUpdateCallback, video );
82 CL_LinkDynTexture( video->cpif.name, video->cpif.tex );
85 static void UnlinkVideoTexture( clvideo_t *video )
87 CL_UnlinkDynTexture( video->cpif.name );
89 R_FreeTexture( video->cpif.tex );
90 video->cpif.tex = NULL;
91 // free the image data
92 Mem_Free( video->imagedata );
95 static void SuspendVideo( clvideo_t * video )
99 video->suspended = true;
100 UnlinkVideoTexture(video);
101 // if we are in firstframe mode, also close the stream
102 if (video->state == CLVIDEO_FIRSTFRAME)
105 video->close(video->stream);
106 video->stream = NULL;
110 static qboolean WakeVideo( clvideo_t * video )
112 if( !video->suspended )
114 video->suspended = false;
116 if( video->state == CLVIDEO_FIRSTFRAME )
117 if( !OpenStream( video ) ) {
118 video->state = CLVIDEO_UNUSED;
122 video->imagedata = Mem_Alloc( cls.permanentmempool, video->cpif.width * video->cpif.height * cl_videobytesperpixel );
123 LinkVideoTexture( video );
126 video->starttime += realtime - video->lasttime;
131 static void LoadSubtitles( clvideo_t *video, const char *subtitlesfile )
135 float subtime, sublen;
138 if (gamemode == GAME_BLOODOMNICIDE)
140 char overridename[MAX_QPATH];
143 langcvar = Cvar_FindVar("language");
144 subtitle_text = NULL;
147 dpsnprintf(overridename, sizeof(overridename), "locale/%s/%s", langcvar->string, subtitlesfile);
148 subtitle_text = (char *)FS_LoadFile(overridename, cls.permanentmempool, false, NULL);
151 subtitle_text = (char *)FS_LoadFile(subtitlesfile, cls.permanentmempool, false, NULL);
155 subtitle_text = (char *)FS_LoadFile(subtitlesfile, cls.permanentmempool, false, NULL);
159 Con_DPrintf( "LoadSubtitles: can't open subtitle file '%s'!\n", subtitlesfile );
163 // parse subtitle_text
164 // line is: x y "text" where
166 // y - seconds last (if 0 - last thru next sub, if negative - last to next sub - this amount of seconds)
168 data = subtitle_text;
171 if (!COM_ParseToken_QuakeC(&data, false))
173 subtime = atof( com_token );
174 if (!COM_ParseToken_QuakeC(&data, false))
176 sublen = atof( com_token );
177 if (!COM_ParseToken_QuakeC(&data, false))
182 if (video->subtitles == CLVIDEO_MAX_SUBTITLES)
184 Con_Printf("WARNING: CLVIDEO_MAX_SUBTITLES = %i reached when reading subtitles from '%s'\n", CLVIDEO_MAX_SUBTITLES, subtitlesfile);
188 video->subtitle_text[numsubs] = (char *) Mem_Alloc(cls.permanentmempool, strlen(com_token) + 1);
189 memcpy(video->subtitle_text[numsubs], com_token, strlen(com_token) + 1);
190 video->subtitle_start[numsubs] = subtime;
191 video->subtitle_end[numsubs] = sublen;
192 if (numsubs > 0) // make true len for prev sub, autofix overlapping subtitles
194 if (video->subtitle_end[numsubs-1] <= 0)
195 video->subtitle_end[numsubs-1] = max(video->subtitle_start[numsubs-1], video->subtitle_start[numsubs] + video->subtitle_end[numsubs-1]);
197 video->subtitle_end[numsubs-1] = min(video->subtitle_start[numsubs-1] + video->subtitle_end[numsubs-1], video->subtitle_start[numsubs]);
200 // todo: check timing for consistency?
202 if (numsubs > 0) // make true len for prev sub, autofix overlapping subtitles
204 if (video->subtitle_end[numsubs-1] <= 0)
205 video->subtitle_end[numsubs-1] = 99999999; // fixme: make it end when video ends?
207 video->subtitle_end[numsubs-1] = video->subtitle_start[numsubs-1] + video->subtitle_end[numsubs-1];
209 Z_Free( subtitle_text );
210 video->subtitles = numsubs;
212 Con_Printf( "video->subtitles: %i\n", video->subtitles );
213 for (numsubs = 0; numsubs < video->subtitles; numsubs++)
214 Con_Printf( " %03.2f %03.2f : %s\n", video->subtitle_start[numsubs], video->subtitle_end[numsubs], video->subtitle_text[numsubs] );
218 static clvideo_t* OpenVideo( clvideo_t *video, const char *filename, const char *name, int owner, const char *subtitlesfile )
220 strlcpy( video->filename, filename, sizeof(video->filename) );
221 video->ownertag = owner;
222 if( strncmp( name, CLVIDEOPREFIX, sizeof( CLVIDEOPREFIX ) - 1 ) )
224 strlcpy( video->cpif.name, name, sizeof(video->cpif.name) );
226 if( !OpenStream( video ) )
229 video->state = CLVIDEO_FIRSTFRAME;
230 video->framenum = -1;
231 video->framerate = video->getframerate( video->stream );
232 video->lasttime = realtime;
233 video->subtitles = 0;
235 video->cpif.width = video->getwidth( video->stream );
236 video->cpif.height = video->getheight( video->stream );
237 video->imagedata = Mem_Alloc( cls.permanentmempool, video->cpif.width * video->cpif.height * cl_videobytesperpixel );
238 LinkVideoTexture( video );
240 // VorteX: load simple subtitle_text file
241 if (subtitlesfile[0])
242 LoadSubtitles( video, subtitlesfile );
247 clvideo_t* CL_OpenVideo( const char *filename, const char *name, int owner, const char *subtitlesfile )
251 if( !name || !*name || strncmp( name, CLVIDEOPREFIX, sizeof( CLVIDEOPREFIX ) - 1 ) != 0 ) {
252 Con_DPrintf( "CL_OpenVideo: Bad video texture name '%s'!\n", name );
256 video = FindUnusedVid();
258 Con_Printf( "CL_OpenVideo: unable to open video \"%s\" - video limit reached\n", filename );
261 video = OpenVideo( video, filename, name, owner, subtitlesfile );
262 // expand the active range to include the new entry
264 cl_num_videos = max(cl_num_videos, (int)(video - cl_videos) + 1);
269 static clvideo_t* CL_GetVideoBySlot( int slot )
271 clvideo_t *video = &cl_videos[ slot ];
273 if( video->suspended )
275 if( !WakeVideo( video ) )
277 else if( video->state == CLVIDEO_RESETONWAKEUP )
278 video->framenum = -1;
281 video->lasttime = realtime;
286 clvideo_t *CL_GetVideoByName( const char *name )
290 for( i = 0 ; i < cl_num_videos ; i++ )
291 if( cl_videos[ i ].state != CLVIDEO_UNUSED
292 && !strcmp( cl_videos[ i ].cpif.name , name ) )
294 if( i != cl_num_videos )
295 return CL_GetVideoBySlot( i );
300 void CL_SetVideoState(clvideo_t *video, clvideostate_t state)
305 video->lasttime = realtime;
306 video->state = state;
307 if (state == CLVIDEO_FIRSTFRAME)
308 CL_RestartVideo(video);
311 void CL_RestartVideo(clvideo_t *video)
317 video->starttime = video->lasttime = realtime;
318 video->framenum = -1;
322 video->close(video->stream);
323 video->stream = NULL;
324 if (!OpenStream(video))
325 video->state = CLVIDEO_UNUSED;
329 void CL_CloseVideo(clvideo_t * video)
333 if (!video || video->state == CLVIDEO_UNUSED)
337 if (!video->suspended || video->state != CLVIDEO_FIRSTFRAME)
340 video->close(video->stream);
341 video->stream = NULL;
344 if (!video->suspended)
345 UnlinkVideoTexture(video);
347 if (video->subtitles)
349 for (i = 0; i < video->subtitles; i++)
350 Z_Free( video->subtitle_text[i] );
351 video->subtitles = 0;
353 video->state = CLVIDEO_UNUSED;
357 void CL_Video_Frame(void)
365 for (video = cl_videos, i = 0 ; i < cl_num_videos ; video++, i++)
367 if (video->state != CLVIDEO_UNUSED && !video->suspended)
369 if (realtime - video->lasttime > CLTHRESHOLD)
374 if (video->state == CLVIDEO_PAUSE)
376 video->starttime = realtime - video->framenum * video->framerate;
379 // read video frame from stream if time has come
380 if (video->state == CLVIDEO_FIRSTFRAME )
383 destframe = (int)((realtime - video->starttime) * video->framerate);
386 if (video->framenum < destframe)
390 if (video->decodeframe(video->stream, video->imagedata, cl_videormask, cl_videogmask, cl_videobmask, cl_videobytesperpixel, cl_videobytesperpixel * video->cpif.width))
393 CL_RestartVideo(video);
394 if (video->state == CLVIDEO_PLAY)
395 video->state = CLVIDEO_FIRSTFRAME;
398 } while(video->framenum < destframe);
399 R_MarkDirtyTexture(video->cpif.tex);
405 if (cl_videos->state == CLVIDEO_FIRSTFRAME)
408 // reduce range to exclude unnecessary entries
409 while(cl_num_videos > 0 && cl_videos[cl_num_videos-1].state == CLVIDEO_UNUSED)
413 void CL_PurgeOwner( int owner )
417 for (i = 0 ; i < cl_num_videos ; i++)
418 if (cl_videos[i].ownertag == owner)
419 CL_CloseVideo(&cl_videos[i]);
429 float alignment; // 0 = left, 0.5 = center, 1 = right
433 cl_video_subtitle_info_t;
435 static float CL_DrawVideo_WordWidthFunc(void *passthrough, const char *w, size_t *length, float maxWidth)
437 cl_video_subtitle_info_t *si = (cl_video_subtitle_info_t *) passthrough;
440 return si->fontsize * si->font->maxwidth;
442 return DrawQ_TextWidth_UntilWidth(w, length, si->fontsize, si->fontsize, false, si->font, -maxWidth); // -maxWidth: we want at least one char
443 else if(maxWidth == -1)
444 return DrawQ_TextWidth(w, *length, si->fontsize, si->fontsize, false, si->font);
449 static int CL_DrawVideo_DisplaySubtitleLine(void *passthrough, const char *line, size_t length, float width, qboolean isContinuation)
451 cl_video_subtitle_info_t *si = (cl_video_subtitle_info_t *) passthrough;
453 int x = (int) (si->x + (si->width - width) * si->alignment);
455 DrawQ_String(x, si->y, line, length, si->fontsize, si->fontsize, 1.0, 1.0, 1.0, si->textalpha, 0, NULL, false, si->font);
456 si->y += si->fontsize;
460 int cl_videoplaying = false; // old, but still supported
462 void CL_DrawVideo(void)
465 float videotime, px, py, sx, sy, st[8], b;
466 cl_video_subtitle_info_t si;
469 if (!cl_videoplaying)
472 video = CL_GetVideoBySlot( 0 );
475 if (cl_video_scale.value <= 0 || cl_video_scale.value > 1)
476 Cvar_SetValueQuick( &cl_video_scale, 1);
477 if (cl_video_brightness.value <= 0 || cl_video_brightness.value > 10)
478 Cvar_SetValueQuick( &cl_video_brightness, 1);
480 // calc video proportions
483 sx = vid_conwidth.integer;
484 sy = vid_conheight.integer;
485 st[0] = 0.0; st[1] = 0.0;
486 st[2] = 1.0; st[3] = 0.0;
487 st[4] = 0.0; st[5] = 1.0;
488 st[6] = 1.0; st[7] = 1.0;
489 if (cl_video_keepaspectratio.integer)
491 float a = video->getaspectratio(video->stream) / ((float)vid.width / (float)vid.height);
492 if (cl_video_keepaspectratio.integer >= 2)
494 // clip instead of scale
495 if (a < 1.0) // clip horizontally
497 st[1] = st[3] = (1 - a)*0.5;
498 st[5] = st[7] = 1 - (1 - a)*0.5;
500 else if (a > 1.0) // clip vertically
502 st[0] = st[4] = (1 - 1/a)*0.5;
503 st[2] = st[6] = (1/a)*0.5;
506 else if (a < 1.0) // scale horizontally
508 px += sx * (1 - a) * 0.5;
511 else if (a > 1.0) // scale vertically
519 if (cl_video_scale.value != 1)
521 px += sx * (1 - cl_video_scale.value) * 0.5;
522 py += sy * (1 - cl_video_scale.value) * ((bound(-1, cl_video_scale_vpos.value, 1) + 1) / 2);
523 sx *= cl_video_scale.value;
524 sy *= cl_video_scale.value;
527 // calc brightness for fadein and fadeout effects
528 b = cl_video_brightness.value;
529 if (cl_video_fadein.value && (realtime - video->starttime) < cl_video_fadein.value)
530 b = pow((realtime - video->starttime)/cl_video_fadein.value, 2);
531 else if (cl_video_fadeout.value && ((video->starttime + video->framenum * video->framerate) - realtime) < cl_video_fadeout.value)
532 b = pow(((video->starttime + video->framenum * video->framerate) - realtime)/cl_video_fadeout.value, 2);
534 // draw black bg in case stipple is active or video is scaled
535 if (cl_video_stipple.integer || px != 0 || py != 0 || sx != vid_conwidth.integer || sy != vid_conheight.integer)
536 DrawQ_Fill(0, 0, vid_conwidth.integer, vid_conheight.integer, 0, 0, 0, 1, 0);
539 // enable video-only polygon stipple (of global stipple is not active)
540 if (qglPolygonStipple && !scr_stipple.integer && cl_video_stipple.integer)
542 GLubyte stipple[128];
545 s = cl_video_stipple.integer;
547 width = (s & 070) >> 3;
548 qglEnable(GL_POLYGON_STIPPLE);CHECKGLERROR // 0x0B42
549 for(i = 0; i < 128; ++i)
552 stipple[i] = ((line >> width) & ((1 << parts) - 1)) ? 0x00 : 0xFF;
554 qglPolygonStipple(stipple);CHECKGLERROR
559 if (v_glslgamma_video.value >= 1)
560 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);
563 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, DRAWFLAG_NOGAMMA);
564 if (v_glslgamma_video.value > 0.0)
565 DrawQ_SuperPic(px, py, &video->cpif, 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);
569 // disable video-only stipple
570 if (qglPolygonStipple && !scr_stipple.integer && cl_video_stipple.integer)
572 qglDisable(GL_POLYGON_STIPPLE);CHECKGLERROR
576 // VorteX: draw subtitle_text
577 if (!video->subtitles || !cl_video_subtitles.integer)
580 // find current subtitle
581 videotime = realtime - video->starttime;
582 for (i = 0; i < video->subtitles; i++)
584 if (videotime >= video->subtitle_start[i] && videotime <= video->subtitle_end[i])
587 si.font = FONT_NOTIFY;
588 si.x = vid_conwidth.integer * 0.1;
589 si.y = vid_conheight.integer - (max(1, cl_video_subtitles_lines.value) * cl_video_subtitles_textsize.value);
590 si.width = vid_conwidth.integer * 0.8;
591 si.height = max(1, cl_video_subtitles_lines.integer) * cl_video_subtitles_textsize.value;
593 si.fontsize = cl_video_subtitles_textsize.value;
594 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
595 COM_Wordwrap(video->subtitle_text[i], strlen(video->subtitle_text[i]), 0, si.width, CL_DrawVideo_WordWidthFunc, &si, CL_DrawVideo_DisplaySubtitleLine, &si);
601 void CL_VideoStart(char *filename, const char *subtitlesfile)
606 if( cl_videos->state != CLVIDEO_UNUSED )
607 CL_CloseVideo( cl_videos );
608 // already contains video/
609 if( !OpenVideo( cl_videos, filename, va(vabuf, sizeof(vabuf), CLDYNTEXTUREPREFIX "%s", filename ), 0, subtitlesfile ) )
611 // expand the active range to include the new entry
612 cl_num_videos = max(cl_num_videos, 1);
614 cl_videoplaying = true;
616 CL_SetVideoState( cl_videos, CLVIDEO_PLAY );
617 CL_RestartVideo( cl_videos );
620 void CL_Video_KeyEvent( int key, int ascii, qboolean down )
622 // only react to up events, to allow the user to delay the abortion point if it suddenly becomes interesting..
624 if( key == K_ESCAPE || key == K_ENTER || key == K_SPACE ) {
630 void CL_VideoStop(void)
632 cl_videoplaying = false;
634 CL_CloseVideo( cl_videos );
637 static void CL_PlayVideo_f(void)
639 char name[MAX_QPATH], subtitlesfile[MAX_QPATH];
640 const char *extension;
644 if (COM_CheckParm("-benchmark"))
649 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");
653 extension = FS_FileExtension(Cmd_Argv(1));
655 dpsnprintf(name, sizeof(name), "video/%s", Cmd_Argv(1));
657 dpsnprintf(name, sizeof(name), "video/%s.dpv", Cmd_Argv(1));
659 CL_VideoStart(name, Cmd_Argv(2));
662 dpsnprintf(subtitlesfile, sizeof(subtitlesfile), "video/%s.dpsubs", Cmd_Argv(1));
663 CL_VideoStart(name, subtitlesfile);
667 static void CL_StopVideo_f(void)
672 static void cl_video_start( void )
677 cl_videotexturepool = R_AllocTexturePool();
679 for( video = cl_videos, i = 0 ; i < cl_num_videos ; i++, video++ )
680 if( video->state != CLVIDEO_UNUSED && !video->suspended )
681 LinkVideoTexture( video );
684 static void cl_video_shutdown( void )
689 for( video = cl_videos, i = 0 ; i < cl_num_videos ; i++, video++ )
690 if( video->state != CLVIDEO_UNUSED && !video->suspended )
691 SuspendVideo( video );
692 R_FreeTexturePool( &cl_videotexturepool );
695 static void cl_video_newmap( void )
699 void CL_Video_Init( void )
709 cl_videobytesperpixel = 4;
711 // set masks in an endian-independent way (as they really represent bytes)
712 bgra.i = 0;bgra.b[0] = 0xFF;cl_videobmask = bgra.i;
713 bgra.i = 0;bgra.b[1] = 0xFF;cl_videogmask = bgra.i;
714 bgra.i = 0;bgra.b[2] = 0xFF;cl_videormask = bgra.i;
716 Cmd_AddCommand( "playvideo", CL_PlayVideo_f, "play a .dpv video file" );
717 Cmd_AddCommand( "stopvideo", CL_StopVideo_f, "stop playing a .dpv video file" );
719 Cvar_RegisterVariable(&cl_video_subtitles);
720 Cvar_RegisterVariable(&cl_video_subtitles_lines);
721 Cvar_RegisterVariable(&cl_video_subtitles_textsize);
722 Cvar_RegisterVariable(&cl_video_scale);
723 Cvar_RegisterVariable(&cl_video_scale_vpos);
724 Cvar_RegisterVariable(&cl_video_brightness);
725 Cvar_RegisterVariable(&cl_video_stipple);
726 Cvar_RegisterVariable(&cl_video_keepaspectratio);
727 Cvar_RegisterVariable(&cl_video_fadein);
728 Cvar_RegisterVariable(&cl_video_fadeout);
730 Cvar_RegisterVariable(&v_glslgamma_video);
732 R_RegisterModule( "CL_Video", cl_video_start, cl_video_shutdown, cl_video_newmap, NULL, NULL );
734 LibAvW_OpenLibrary();
737 void CL_Video_Shutdown( void )
741 for (i = 0 ; i < cl_num_videos ; i++)
742 CL_CloseVideo(&cl_videos[ i ]);
744 LibAvW_CloseLibrary();