Configure all of the CL_MeshEntities* meshes the same, the material flags decide...
[xonotic/darkplaces.git] / cl_video.c
1
2 #include "quakedef.h"
3 #include "cl_dyntexture.h"
4 #include "cl_video.h"
5
6 // cvars
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"};
17
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."};
19
20 // DPV stream decoder
21 #include "dpvsimpledecode.h"
22
23 // VorteX: libavcodec implementation
24 #include "cl_video_libavw.c"
25
26 // JAM video decoder used by Blood Omnicide
27 #ifdef JAMVIDEO
28 #include "cl_video_jamdecode.c"
29 #endif
30
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;
36
37 static int cl_num_videos;
38 static clvideo_t cl_videos[ MAXCLVIDEOS ];
39 static rtexturepool_t *cl_videotexturepool;
40
41 static clvideo_t *FindUnusedVid( void )
42 {
43         int i;
44         for( i = 1 ; i < MAXCLVIDEOS ; i++ )
45                 if( cl_videos[ i ].state == CLVIDEO_UNUSED )
46                         return &cl_videos[ i ];
47         return NULL;
48 }
49
50 static qboolean OpenStream( clvideo_t * video )
51 {
52         const char *errorstring;
53
54         video->stream = dpvsimpledecode_open( video, video->filename, &errorstring);
55         if (video->stream)
56                 return true;
57
58 #ifdef JAMVIDEO
59         video->stream = jam_open( video, video->filename, &errorstring);
60         if (video->stream)
61                 return true;
62 #endif
63
64         video->stream = LibAvW_OpenVideo( video, video->filename, &errorstring);
65         if (video->stream)
66                 return true;
67
68         Con_Printf("unable to open \"%s\", error: %s\n", video->filename, errorstring);
69         return false;
70 }
71
72 static void VideoUpdateCallback(rtexture_t *rt, void *data)
73 {
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 );
76 }
77
78 static void LinkVideoTexture( clvideo_t *video )
79 {
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 );
83 }
84
85 static void UnlinkVideoTexture( clvideo_t *video )
86 {
87         CL_UnlinkDynTexture( video->cpif.name );
88         // free the texture
89         R_FreeTexture( video->cpif.tex );
90         video->cpif.tex = NULL;
91         // free the image data
92         Mem_Free( video->imagedata );
93 }
94
95 static void SuspendVideo( clvideo_t * video )
96 {
97         if (video->suspended)
98                 return;
99         video->suspended = true;
100         UnlinkVideoTexture(video);
101         // if we are in firstframe mode, also close the stream
102         if (video->state == CLVIDEO_FIRSTFRAME)
103         {
104                 if (video->stream)
105                         video->close(video->stream);
106                 video->stream = NULL;
107         }
108 }
109
110 static qboolean WakeVideo( clvideo_t * video )
111 {
112         if( !video->suspended )
113                 return true;
114         video->suspended = false;
115
116         if( video->state == CLVIDEO_FIRSTFRAME )
117                 if( !OpenStream( video ) ) {
118                         video->state = CLVIDEO_UNUSED;
119                         return false;
120                 }
121
122         video->imagedata = Mem_Alloc( cls.permanentmempool, video->cpif.width * video->cpif.height * cl_videobytesperpixel );
123         LinkVideoTexture( video );
124
125         // update starttime
126         video->starttime += realtime - video->lasttime;
127
128         return true;
129 }
130
131 static void LoadSubtitles( clvideo_t *video, const char *subtitlesfile )
132 {
133         char *subtitle_text;
134         const char *data;
135         float subtime, sublen;
136         int numsubs = 0;
137
138         if (gamemode == GAME_BLOODOMNICIDE)
139         {
140                 char overridename[MAX_QPATH];
141                 cvar_t *langcvar;
142
143                 langcvar = Cvar_FindVar("language");
144                 subtitle_text = NULL;
145                 if (langcvar)
146                 {
147                         dpsnprintf(overridename, sizeof(overridename), "locale/%s/%s", langcvar->string, subtitlesfile);
148                         subtitle_text = (char *)FS_LoadFile(overridename, cls.permanentmempool, false, NULL);
149                 }
150                 if (!subtitle_text)
151                         subtitle_text = (char *)FS_LoadFile(subtitlesfile, cls.permanentmempool, false, NULL);
152         }
153         else
154         {
155                 subtitle_text = (char *)FS_LoadFile(subtitlesfile, cls.permanentmempool, false, NULL);
156         }
157         if (!subtitle_text)
158         {
159                 Con_DPrintf( "LoadSubtitles: can't open subtitle file '%s'!\n", subtitlesfile );
160                 return;
161         }
162
163         // parse subtitle_text
164         // line is: x y "text" where
165         //    x - start time
166         //    y - seconds last (if 0 - last thru next sub, if negative - last to next sub - this amount of seconds)
167
168         data = subtitle_text;
169         for (;;)
170         {
171                 if (!COM_ParseToken_QuakeC(&data, false))
172                         break;
173                 subtime = atof( com_token );
174                 if (!COM_ParseToken_QuakeC(&data, false))
175                         break;
176                 sublen = atof( com_token );
177                 if (!COM_ParseToken_QuakeC(&data, false))
178                         break;
179                 if (!com_token[0])
180                         continue;
181                 // check limits
182                 if (video->subtitles == CLVIDEO_MAX_SUBTITLES)
183                 {
184                         Con_Printf("WARNING: CLVIDEO_MAX_SUBTITLES = %i reached when reading subtitles from '%s'\n", CLVIDEO_MAX_SUBTITLES, subtitlesfile);
185                         break;  
186                 }
187                 // add a sub
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
193                 {
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]);
196                         else
197                                 video->subtitle_end[numsubs-1] = min(video->subtitle_start[numsubs-1] + video->subtitle_end[numsubs-1], video->subtitle_start[numsubs]);
198                 }
199                 numsubs++;
200                 // todo: check timing for consistency?
201         }
202         if (numsubs > 0) // make true len for prev sub, autofix overlapping subtitles
203         {
204                 if (video->subtitle_end[numsubs-1] <= 0)
205                         video->subtitle_end[numsubs-1] = 99999999; // fixme: make it end when video ends?
206                 else
207                         video->subtitle_end[numsubs-1] = video->subtitle_start[numsubs-1] + video->subtitle_end[numsubs-1];
208         }
209         Z_Free( subtitle_text );
210         video->subtitles = numsubs;
211 /*
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] );
215 */
216 }
217
218 static clvideo_t* OpenVideo( clvideo_t *video, const char *filename, const char *name, int owner, const char *subtitlesfile )
219 {
220         strlcpy( video->filename, filename, sizeof(video->filename) );
221         video->ownertag = owner;
222         if( strncmp( name, CLVIDEOPREFIX, sizeof( CLVIDEOPREFIX ) - 1 ) )
223                 return NULL;
224         strlcpy( video->cpif.name, name, sizeof(video->cpif.name) );
225
226         if( !OpenStream( video ) )
227                 return NULL;
228
229         video->state = CLVIDEO_FIRSTFRAME;
230         video->framenum = -1;
231         video->framerate = video->getframerate( video->stream );
232         video->lasttime = realtime;
233         video->subtitles = 0;
234
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 );
239
240         // VorteX: load simple subtitle_text file
241         if (subtitlesfile[0])
242                 LoadSubtitles( video, subtitlesfile );
243
244         return video;
245 }
246
247 clvideo_t* CL_OpenVideo( const char *filename, const char *name, int owner, const char *subtitlesfile )
248 {
249         clvideo_t *video;
250         // sanity check
251         if( !name || !*name || strncmp( name, CLVIDEOPREFIX, sizeof( CLVIDEOPREFIX ) - 1 ) != 0 ) {
252                 Con_DPrintf( "CL_OpenVideo: Bad video texture name '%s'!\n", name );
253                 return NULL;
254         }
255
256         video = FindUnusedVid();
257         if( !video ) {
258                 Con_Printf( "CL_OpenVideo: unable to open video \"%s\" - video limit reached\n", filename );
259                 return NULL;
260         }
261         video = OpenVideo( video, filename, name, owner, subtitlesfile );
262         // expand the active range to include the new entry
263         if (video) {
264                 cl_num_videos = max(cl_num_videos, (int)(video - cl_videos) + 1);
265         }
266         return video;
267 }
268
269 static clvideo_t* CL_GetVideoBySlot( int slot )
270 {
271         clvideo_t *video = &cl_videos[ slot ];
272
273         if( video->suspended )
274         {
275                 if( !WakeVideo( video ) )
276                         return NULL;
277                 else if( video->state == CLVIDEO_RESETONWAKEUP )
278                         video->framenum = -1;
279         }
280
281         video->lasttime = realtime;
282
283         return video;
284 }
285
286 clvideo_t *CL_GetVideoByName( const char *name )
287 {
288         int i;
289
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 ) )
293                         break;
294         if( i != cl_num_videos )
295                 return CL_GetVideoBySlot( i );
296         else
297                 return NULL;
298 }
299
300 void CL_SetVideoState(clvideo_t *video, clvideostate_t state)
301 {
302         if (!video)
303                 return;
304
305         video->lasttime = realtime;
306         video->state = state;
307         if (state == CLVIDEO_FIRSTFRAME)
308                 CL_RestartVideo(video);
309 }
310
311 void CL_RestartVideo(clvideo_t *video)
312 {
313         if (!video)
314                 return;
315
316         // reset time
317         video->starttime = video->lasttime = realtime;
318         video->framenum = -1;
319
320         // reopen stream
321         if (video->stream)
322                 video->close(video->stream);
323         video->stream = NULL;
324         if (!OpenStream(video))
325                 video->state = CLVIDEO_UNUSED;
326 }
327
328 // close video
329 void CL_CloseVideo(clvideo_t * video)
330 {
331         int i;
332
333         if (!video || video->state == CLVIDEO_UNUSED)
334                 return;
335
336         // close stream
337         if (!video->suspended || video->state != CLVIDEO_FIRSTFRAME)
338         {
339                 if (video->stream)
340                         video->close(video->stream);
341                 video->stream = NULL;
342         }
343         // unlink texture
344         if (!video->suspended)
345                 UnlinkVideoTexture(video);
346         // purge subtitles
347         if (video->subtitles)
348         {
349                 for (i = 0; i < video->subtitles; i++)
350                         Z_Free( video->subtitle_text[i] );
351                 video->subtitles = 0;
352         }
353         video->state = CLVIDEO_UNUSED;
354 }
355
356 // update all videos
357 void CL_Video_Frame(void) 
358 {
359         clvideo_t *video;
360         int destframe;
361         int i;
362
363         if (!cl_num_videos)
364                 return;
365         for (video = cl_videos, i = 0 ; i < cl_num_videos ; video++, i++)
366         {
367                 if (video->state != CLVIDEO_UNUSED && !video->suspended)
368                 {
369                         if (realtime - video->lasttime > CLTHRESHOLD)
370                         {
371                                 SuspendVideo(video);
372                                 continue;
373                         }
374                         if (video->state == CLVIDEO_PAUSE)
375                         {
376                                 video->starttime = realtime - video->framenum * video->framerate;
377                                 continue;
378                         }
379                         // read video frame from stream if time has come
380                         if (video->state == CLVIDEO_FIRSTFRAME )
381                                 destframe = 0;
382                         else
383                                 destframe = (int)((realtime - video->starttime) * video->framerate);
384                         if (destframe < 0)
385                                 destframe = 0;
386                         if (video->framenum < destframe)
387                         {
388                                 do {
389                                         video->framenum++;
390                                         if (video->decodeframe(video->stream, video->imagedata, cl_videormask, cl_videogmask, cl_videobmask, cl_videobytesperpixel, cl_videobytesperpixel * video->cpif.width))
391                                         { 
392                                                 // finished?
393                                                 CL_RestartVideo(video);
394                                                 if (video->state == CLVIDEO_PLAY)
395                                                         video->state = CLVIDEO_FIRSTFRAME;
396                                                 return;
397                                         }
398                                 } while(video->framenum < destframe);
399                                 R_MarkDirtyTexture(video->cpif.tex);
400                         }
401                 }
402         }
403
404         // stop main video
405         if (cl_videos->state == CLVIDEO_FIRSTFRAME)
406                 CL_VideoStop();
407
408         // reduce range to exclude unnecessary entries
409         while(cl_num_videos > 0 && cl_videos[cl_num_videos-1].state == CLVIDEO_UNUSED)
410                 cl_num_videos--;
411 }
412
413 void CL_PurgeOwner( int owner )
414 {
415         int i;
416
417         for (i = 0 ; i < cl_num_videos ; i++)
418                 if (cl_videos[i].ownertag == owner)
419                         CL_CloseVideo(&cl_videos[i]);
420 }
421
422 typedef struct
423 {
424         dp_font_t *font;
425         float x;
426         float y;
427         float width;
428         float height;
429         float alignment; // 0 = left, 0.5 = center, 1 = right
430         float fontsize;
431         float textalpha;
432 }
433 cl_video_subtitle_info_t;
434
435 static float CL_DrawVideo_WordWidthFunc(void *passthrough, const char *w, size_t *length, float maxWidth)
436 {
437         cl_video_subtitle_info_t *si = (cl_video_subtitle_info_t *) passthrough;
438
439         if(w == NULL)
440                 return si->fontsize * si->font->maxwidth;
441         if(maxWidth >= 0)
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);
445         else
446                 return 0;
447 }
448
449 static int CL_DrawVideo_DisplaySubtitleLine(void *passthrough, const char *line, size_t length, float width, qboolean isContinuation)
450 {
451         cl_video_subtitle_info_t *si = (cl_video_subtitle_info_t *) passthrough;
452
453         int x = (int) (si->x + (si->width - width) * si->alignment);
454         if (length > 0)
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;
457         return 1;
458 }
459
460 int cl_videoplaying = false; // old, but still supported
461
462 void CL_DrawVideo(void)
463 {
464         clvideo_t *video;
465         float videotime, px, py, sx, sy, st[8], b;
466         cl_video_subtitle_info_t si;
467         int i;
468
469         if (!cl_videoplaying)
470                 return;
471
472         video = CL_GetVideoBySlot( 0 );
473
474         // fix cvars
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);
479
480         // calc video proportions
481         px = 0;
482         py = 0;
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)
490         {
491                 float a = video->getaspectratio(video->stream) / ((float)vid.width / (float)vid.height);
492                 if (cl_video_keepaspectratio.integer >= 2)
493                 {
494                         // clip instead of scale
495                         if (a < 1.0) // clip horizontally
496                         {
497                                 st[1] = st[3] = (1 - a)*0.5;
498                                 st[5] = st[7] = 1 - (1 - a)*0.5;
499                         }
500                         else if (a > 1.0) // clip vertically
501                         {
502                                 st[0] = st[4] = (1 - 1/a)*0.5;
503                                 st[2] = st[6] = (1/a)*0.5;
504                         }
505                 }
506                 else if (a < 1.0) // scale horizontally
507                 {
508                         px += sx * (1 - a) * 0.5;
509                         sx *= a;
510                 }
511                 else if (a > 1.0) // scale vertically
512                 {
513                         a = 1 / a;
514                         py += sy * (1 - a);
515                         sy *= a;
516                 }
517         }
518
519         if (cl_video_scale.value != 1)
520         {
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;
525         }
526
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);
533
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);
537
538 #ifndef USE_GLES2
539         // enable video-only polygon stipple (of global stipple is not active)
540         if (qglPolygonStipple && !scr_stipple.integer && cl_video_stipple.integer)
541         {
542                 GLubyte stipple[128];
543                 int s, width, parts;
544         
545                 s = cl_video_stipple.integer;
546                 parts = (s & 007);
547                 width = (s & 070) >> 3;
548                 qglEnable(GL_POLYGON_STIPPLE);CHECKGLERROR // 0x0B42
549                 for(i = 0; i < 128; ++i)
550                 {
551                         int line = i/4;
552                         stipple[i] = ((line >> width) & ((1 << parts) - 1)) ? 0x00 : 0xFF;
553                 }
554                 qglPolygonStipple(stipple);CHECKGLERROR
555         }
556 #endif
557
558         // draw video
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);
561         else
562         {
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);
566         }
567
568 #ifndef USE_GLES2
569         // disable video-only stipple
570         if (qglPolygonStipple && !scr_stipple.integer && cl_video_stipple.integer)
571         {
572                 qglDisable(GL_POLYGON_STIPPLE);CHECKGLERROR
573         }
574 #endif
575
576         // VorteX: draw subtitle_text
577         if (!video->subtitles || !cl_video_subtitles.integer)
578                 return;
579
580         // find current subtitle
581         videotime = realtime - video->starttime;
582         for (i = 0; i < video->subtitles; i++)
583         {
584                 if (videotime >= video->subtitle_start[i] && videotime <= video->subtitle_end[i])
585                 {
586                         // found, draw it
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;
592                         si.alignment = 0.5;
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);
596                         break;
597                 }
598         }
599 }
600
601 void CL_VideoStart(char *filename, const char *subtitlesfile)
602 {
603         char vabuf[1024];
604         Host_StartVideo();
605
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 ) )
610                 return;
611         // expand the active range to include the new entry
612         cl_num_videos = max(cl_num_videos, 1);
613
614         cl_videoplaying = true;
615
616         CL_SetVideoState( cl_videos, CLVIDEO_PLAY );
617         CL_RestartVideo( cl_videos );
618 }
619
620 void CL_Video_KeyEvent( int key, int ascii, qboolean down ) 
621 {
622         // only react to up events, to allow the user to delay the abortion point if it suddenly becomes interesting..
623         if( !down ) {
624                 if( key == K_ESCAPE || key == K_ENTER || key == K_SPACE ) {
625                         CL_VideoStop();
626                 }
627         }
628 }
629
630 void CL_VideoStop(void)
631 {
632         cl_videoplaying = false;
633
634         CL_CloseVideo( cl_videos );
635 }
636
637 static void CL_PlayVideo_f(void)
638 {
639         char name[MAX_QPATH], subtitlesfile[MAX_QPATH];
640         const char *extension;
641
642         Host_StartVideo();
643
644         if (COM_CheckParm("-benchmark"))
645                 return;
646
647         if (Cmd_Argc() < 2)
648         {
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");
650                 return;
651         }
652
653         extension = FS_FileExtension(Cmd_Argv(1));
654         if (extension[0])
655                 dpsnprintf(name, sizeof(name), "video/%s", Cmd_Argv(1));
656         else
657                 dpsnprintf(name, sizeof(name), "video/%s.dpv", Cmd_Argv(1));
658         if ( Cmd_Argc() > 2)
659                 CL_VideoStart(name, Cmd_Argv(2));
660         else
661         {
662                 dpsnprintf(subtitlesfile, sizeof(subtitlesfile), "video/%s.dpsubs", Cmd_Argv(1));
663                 CL_VideoStart(name, subtitlesfile);
664         }
665 }
666
667 static void CL_StopVideo_f(void)
668 {
669         CL_VideoStop();
670 }
671
672 static void cl_video_start( void )
673 {
674         int i;
675         clvideo_t *video;
676
677         cl_videotexturepool = R_AllocTexturePool();
678
679         for( video = cl_videos, i = 0 ; i < cl_num_videos ; i++, video++ )
680                 if( video->state != CLVIDEO_UNUSED && !video->suspended )
681                         LinkVideoTexture( video );
682 }
683
684 static void cl_video_shutdown( void )
685 {
686         int i;
687         clvideo_t *video;
688
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 );
693 }
694
695 static void cl_video_newmap( void )
696 {
697 }
698
699 void CL_Video_Init( void )
700 {
701         union
702         {
703                 unsigned char b[4];
704                 unsigned int i;
705         }
706         bgra;
707
708         cl_num_videos = 0;
709         cl_videobytesperpixel = 4;
710
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;
715
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" );
718
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);
729
730         Cvar_RegisterVariable(&v_glslgamma_video);
731
732         R_RegisterModule( "CL_Video", cl_video_start, cl_video_shutdown, cl_video_newmap, NULL, NULL );
733
734         LibAvW_OpenLibrary();
735 }
736
737 void CL_Video_Shutdown( void )
738 {
739         int i;
740
741         for (i = 0 ; i < cl_num_videos ; i++)
742                 CL_CloseVideo(&cl_videos[ i ]);
743
744         LibAvW_CloseLibrary();
745 }