X-Git-Url: http://de.git.xonotic.org/?p=xonotic%2Fdarkplaces.git;a=blobdiff_plain;f=cl_video.c;h=42c0496d679026ff9e5229329b32cd27bd6321cc;hp=857d5d8fb85bba8cc477c04cab432d7c21ae6c4c;hb=2965b15fe9f10b90420d1d7185e06eacb203d804;hpb=275526b51e0cc486f423539e2aa9473f91bf2b50 diff --git a/cl_video.c b/cl_video.c index 857d5d8f..42c0496d 100644 --- a/cl_video.c +++ b/cl_video.c @@ -3,204 +3,309 @@ #include "cl_video.h" #include "dpvsimpledecode.h" -mempool_t *clvideomempool; +// constants (and semi-constants) +static int cl_videormask; +static int cl_videobmask; +static int cl_videogmask; +static int cl_videobytesperpixel; -int cl_videoplaying = false; -void *cl_videostream; +static int cl_num_videos; +static clvideo_t cl_videos[ MAXCLVIDEOS ]; +static rtexturepool_t *cl_videotexturepool; -double cl_videostarttime; -int cl_videoframenum; -double cl_videoframerate; - -int cl_videoimagewidth; -int cl_videoimageheight; -int cl_videoimagedata_rmask; -int cl_videoimagedata_gmask; -int cl_videoimagedata_bmask; -int cl_videoimagedata_bytesperpixel; -void *cl_videoimagedata; - -int cl_videosoundrate; -int cl_videosoundlength; -short *cl_videosounddata; -int cl_videosoundresamplelength; -short *cl_videosoundresampledata; +static clvideo_t *FindUnusedVid( void ) +{ + int i; + for( i = 1 ; i < MAXCLVIDEOS ; i++ ) + if( cl_videos[ i ].state == CLVIDEO_UNUSED ) + return &cl_videos[ i ]; + return NULL; +} -rtexture_t *cl_videotexture; -rtexturepool_t *cl_videotexturepool; +static qboolean OpenStream( clvideo_t * video ) +{ + char *errorstring; + video->stream = dpvsimpledecode_open( video->filename, &errorstring); + if (!video->stream ) + { + Con_Printf("unable to open \"%s\", error: %s\n", video->filename, errorstring); + return false; + } + return true; +} -void CL_VideoFrame(void) +static void SuspendVideo( clvideo_t * video ) { - int frames, framenum, samples, s; - if (!cl_videoplaying) + if( video->suspended ) return; - framenum = (realtime - cl_videostarttime) * cl_videoframerate; - //Con_Printf("frame %i\n", framenum); - if (framenum < 0) - framenum = 0; - frames = 0; - while (cl_videoframenum < framenum) - { - frames++; - cl_videoframenum++; - if (dpvsimpledecode_video(cl_videostream, cl_videoimagedata, cl_videoimagedata_rmask, cl_videoimagedata_gmask, cl_videoimagedata_bmask, cl_videoimagedata_bytesperpixel, cl_videoimagewidth * cl_videoimagedata_bytesperpixel)) - { - CL_VideoStop(); - return; + video->suspended = true; + // free the texture + R_FreeTexture( video->cpif.tex ); + // free the image data + Mem_Free( video->imagedata ); + // if we are in firstframe mode, also close the stream + if( video->state == CLVIDEO_FIRSTFRAME ) + dpvsimpledecode_close( video->stream ); +} + +static qboolean WakeVideo( clvideo_t * video ) +{ + if( !video->suspended ) + return true; + video->suspended = false; + + if( video->state == CLVIDEO_FIRSTFRAME ) + if( !OpenStream( video ) ) { + video->state = CLVIDEO_UNUSED; + return false; } + + video->imagedata = Mem_Alloc( cls.permanentmempool, video->cpif.width * video->cpif.height * cl_videobytesperpixel ); + video->cpif.tex = R_LoadTexture2D( cl_videotexturepool, video->cpif.name, + video->cpif.width, video->cpif.height, NULL, TEXTYPE_RGBA, TEXF_ALWAYSPRECACHE, NULL ); + + // update starttime + video->starttime += realtime - video->lasttime; + + return true; +} + +static clvideo_t* OpenVideo( clvideo_t *video, const char *filename, const char *name, int owner ) +{ + strlcpy( video->filename, filename, sizeof(video->filename) ); + video->ownertag = owner; + if( strncmp( name, CLVIDEOPREFIX, sizeof( CLVIDEOPREFIX ) - 1 ) ) + return NULL; + strlcpy( video->cpif.name, name, sizeof(video->cpif.name) ); + + if( !OpenStream( video ) ) + return NULL; + + video->state = CLVIDEO_FIRSTFRAME; + video->framenum = -1; + video->framerate = dpvsimpledecode_getframerate( video->stream ); + video->lasttime = realtime; + + video->cpif.width = dpvsimpledecode_getwidth( video->stream ); + video->cpif.height = dpvsimpledecode_getheight( video->stream ); + video->cpif.tex = R_LoadTexture2D( cl_videotexturepool, video->cpif.name, + video->cpif.width, video->cpif.height, NULL, TEXTYPE_RGBA, TEXF_ALWAYSPRECACHE, NULL ); + + video->imagedata = Mem_Alloc( cls.permanentmempool, video->cpif.width * video->cpif.height * cl_videobytesperpixel ); + + return video; +} + +clvideo_t* CL_OpenVideo( const char *filename, const char *name, int owner ) +{ + clvideo_t *video; + + video = FindUnusedVid(); + if( !video ) { + Con_Printf( "unable to open video \"%s\" - video limit reached\n", filename ); + return NULL; } - if (frames) + video = OpenVideo( video, filename, name, owner ); + // expand the active range to include the new entry + if (video) + cl_num_videos = max(cl_num_videos, (int)(video - cl_videos) + 1); + return video; +} + +static clvideo_t* CL_GetVideoBySlot( int slot ) +{ + clvideo_t *video = &cl_videos[ slot ]; + + if( video->suspended ) { - R_UpdateTexture(cl_videotexture, cl_videoimagedata); - //Draw_NewPic("engine_videoframe", cl_videoimagewidth, cl_videoimageheight, false, cl_videoimagedata); + if( !WakeVideo( video ) ) + return NULL; + else if( video->state == CLVIDEO_RESETONWAKEUP ) + video->framenum = -1; } - if (cl_videosoundrate && (samples = S_RawSamples_QueueWantsMore())) - { - Con_Printf("%i = S_RawSamples_QueueWantsMore()\n", samples); - // calculate how much source data we need to fill the output... - s = samples * cl_videosoundrate / S_RawSamples_SampleRate(); + video->lasttime = realtime; - // reallocate processing buffer if needed - if (cl_videosoundresamplelength < samples) - { - cl_videosoundresamplelength = samples + 100; - if (cl_videosoundresampledata) - Mem_Free(cl_videosoundresampledata); - cl_videosoundresampledata = Mem_Alloc(clvideomempool, cl_videosoundresamplelength * sizeof(short[2])); - } + return video; +} - // reallocate loading buffer if needed - if (cl_videosoundlength < s) - { - cl_videosoundlength = s + 100; - if (cl_videosounddata) - Mem_Free(cl_videosounddata); - cl_videosounddata = Mem_Alloc(clvideomempool, cl_videosoundlength * sizeof(short[2])); - } +clvideo_t *CL_GetVideoByName( const char *name ) +{ + int i; + + for( i = 0 ; i < cl_num_videos ; i++ ) + if( cl_videos[ i ].state != CLVIDEO_UNUSED + && !strcmp( cl_videos[ i ].cpif.name , name ) ) + break; + if( i != cl_num_videos ) + return CL_GetVideoBySlot( i ); + else + return NULL; +} + +void CL_SetVideoState( clvideo_t *video, clvideostate_t state ) +{ + if( !video ) + return; + + video->lasttime = realtime; + video->state = state; + if( state == CLVIDEO_FIRSTFRAME ) + CL_RestartVideo( video ); +} + +void CL_RestartVideo( clvideo_t *video ) +{ + if( !video ) + return; + + video->starttime = video->lasttime = realtime; + video->framenum = -1; + + dpvsimpledecode_close( video->stream ); + if( !OpenStream( video ) ) + video->state = CLVIDEO_UNUSED; +} + +void CL_CloseVideo( clvideo_t * video ) +{ + if( !video || video->state == CLVIDEO_UNUSED ) + return; - dpvsimpledecode_audio(cl_videostream, cl_videosounddata, s); - S_ResampleBuffer16Stereo(cl_videosounddata, s, cl_videosoundresampledata, samples); - S_RawSamples_Enqueue(cl_videosoundresampledata, samples); + if( !video->suspended || video->state != CLVIDEO_FIRSTFRAME ) + dpvsimpledecode_close( video->stream ); + if( !video->suspended ) { + Mem_Free( video->imagedata ); + R_FreeTexture( video->cpif.tex ); } + + video->state = CLVIDEO_UNUSED; } -void CL_DrawVideo(void) +static void VideoFrame( clvideo_t *video ) { - if (cl_videoplaying) - { - drawqueuemesh_t mesh; - int indices[6]; - float vertices[16]; - float texcoords[8]; - float colorsf[16]; - float s1, t1, s2, t2, x1, y1, x2, y2; - indices[0] = 0; - indices[1] = 1; - indices[2] = 2; - indices[3] = 0; - indices[4] = 2; - indices[5] = 3; - x1 = 0; - y1 = 0; - x2 = vid.conwidth; - y2 = vid.conheight; - R_FragmentLocation(cl_videotexture, NULL, NULL, &s1, &t1, &s2, &t2); - texcoords[0] = s1;texcoords[1] = t1; - texcoords[2] = s2;texcoords[3] = t1; - texcoords[4] = s2;texcoords[5] = t2; - texcoords[6] = s1;texcoords[7] = t2; - R_FillColors(colorsf, 4, r_colorscale, r_colorscale, r_colorscale, 1); - vertices[ 0] = x1;vertices[ 1] = y1;vertices[ 2] = 0;vertices[ 3] = 0; - vertices[ 4] = x2;vertices[ 5] = y1;vertices[ 6] = 0;vertices[ 7] = 0; - vertices[ 8] = x2;vertices[ 9] = y2;vertices[10] = 0;vertices[11] = 0; - vertices[12] = x1;vertices[13] = y2;vertices[14] = 0;vertices[15] = 0; - mesh.texture = cl_videotexture; - mesh.numtriangles = 2; - mesh.numvertices = 4; - mesh.indices = indices; - mesh.vertices = vertices; - mesh.texcoords = texcoords; - mesh.colors = colorsf; - DrawQ_Mesh(&mesh, 0); - //DrawQ_Pic(0, 0, "engine_videoframe", vid.conwidth, vid.conheight, 1, 1, 1, 1, 0); + int destframe; + + if( video->state == CLVIDEO_FIRSTFRAME ) + destframe = 0; + else + destframe = (int)((realtime - video->starttime) * video->framerate); + if( destframe < 0 ) + destframe = 0; + if( video->framenum < destframe ) { + do { + video->framenum++; + if( dpvsimpledecode_video( video->stream, video->imagedata, cl_videormask, + cl_videogmask, cl_videobmask, cl_videobytesperpixel, + cl_videobytesperpixel * video->cpif.width ) + ) { // finished? + CL_RestartVideo( video ); + if( video->state == CLVIDEO_PLAY ) + video->state = CLVIDEO_FIRSTFRAME; + return; + } + } while( video->framenum < destframe ); + R_UpdateTexture( video->cpif.tex, (unsigned char *)video->imagedata, 0, 0, video->cpif.width, video->cpif.height ); } } -void CL_VideoStart(char *filename) +void CL_VideoFrame( void ) // update all videos { - char *errorstring; - cl_videostream = dpvsimpledecode_open(filename, &errorstring); - if (!cl_videostream) - { - Con_Printf("unable to open \"%s\", error: %s\n", filename, errorstring); + int i; + clvideo_t *video; + + if (!cl_num_videos) return; - } - cl_videoplaying = true; - cl_videostarttime = realtime; - cl_videoframenum = -1; - cl_videoframerate = dpvsimpledecode_getframerate(cl_videostream); - cl_videoimagewidth = dpvsimpledecode_getwidth(cl_videostream); - cl_videoimageheight = dpvsimpledecode_getheight(cl_videostream); - - // RGBA format - cl_videoimagedata_bytesperpixel = 4; - cl_videoimagedata_rmask = BigLong(0xFF000000); - cl_videoimagedata_gmask = BigLong(0x00FF0000); - cl_videoimagedata_bmask = BigLong(0x0000FF00); - cl_videoimagedata = Mem_Alloc(clvideomempool, cl_videoimagewidth * cl_videoimageheight * cl_videoimagedata_bytesperpixel); - //memset(cl_videoimagedata, 97, cl_videoimagewidth * cl_videoimageheight * cl_videoimagedata_bytesperpixel); - - cl_videosoundrate = dpvsimpledecode_getsoundrate(cl_videostream); - cl_videosoundlength = 0; - cl_videosounddata = NULL; - cl_videosoundresamplelength = 0; - cl_videosoundresampledata = NULL; + for( video = cl_videos, i = 0 ; i < cl_num_videos ; video++, i++ ) + if( video->state != CLVIDEO_UNUSED && !video->suspended ) + { + if( realtime - video->lasttime > CLTHRESHOLD ) + SuspendVideo( video ); + else if( video->state == CLVIDEO_PAUSE ) + video->starttime = realtime - video->framenum * video->framerate; + else + VideoFrame( video ); + } - cl_videotexturepool = R_AllocTexturePool(); - cl_videotexture = R_LoadTexture(cl_videotexturepool, "videotexture", cl_videoimagewidth, cl_videoimageheight, NULL, TEXTYPE_RGBA, TEXF_FRAGMENT); + if( cl_videos->state == CLVIDEO_FIRSTFRAME ) + CL_VideoStop(); + + // reduce range to exclude unnecessary entries + while (cl_num_videos > 0 && cl_videos[cl_num_videos-1].state == CLVIDEO_UNUSED) + cl_num_videos--; } -void CL_VideoStop(void) +void CL_Video_Shutdown( void ) { - cl_videoplaying = false; + int i; + for( i = 0 ; i < cl_num_videos ; i++ ) + CL_CloseVideo( &cl_videos[ i ] ); +} - S_RawSamples_ClearQueue(); +void CL_PurgeOwner( int owner ) +{ + int i; + for( i = 0 ; i < cl_num_videos ; i++ ) + if( cl_videos[ i ].ownertag == owner ) + CL_CloseVideo( &cl_videos[ i ] ); +} - if (cl_videostream) - dpvsimpledecode_close(cl_videostream); - cl_videostream = NULL; +int cl_videoplaying = false; // old, but still supported - if (cl_videoimagedata) - Mem_Free(cl_videoimagedata); - cl_videoimagedata = NULL; +void CL_DrawVideo(void) +{ + if (cl_videoplaying) + DrawQ_Pic(0, 0, &CL_GetVideoBySlot( 0 )->cpif, vid_conwidth.integer, vid_conheight.integer, 1, 1, 1, 1, 0); +} - if (cl_videosounddata) - Mem_Free(cl_videosounddata); - cl_videosounddata = NULL; +void CL_VideoStart(char *filename) +{ + Host_StartVideo(); - if (cl_videosoundresampledata) - Mem_Free(cl_videosoundresampledata); - cl_videosoundresampledata = NULL; + if( cl_videos->state != CLVIDEO_UNUSED ) + CL_CloseVideo( cl_videos ); + if( !OpenVideo( cl_videos, filename, va( CLVIDEOPREFIX "%s", filename ), 0 ) ) + return; + // expand the active range to include the new entry + cl_num_videos = max(cl_num_videos, 1); - cl_videotexture = NULL; - R_FreeTexturePool(&cl_videotexturepool); + cl_videoplaying = true; - Draw_FreePic("engine_videoframe"); + CL_SetVideoState( cl_videos, CLVIDEO_PLAY ); + CL_RestartVideo( cl_videos ); +} + +void CL_Video_KeyEvent( int key, int ascii, qboolean down ) +{ + // only react to up events, to allow the user to delay the abortion point if it suddenly becomes interesting.. + if( !down ) { + if( key == K_ESCAPE || key == K_ENTER || key == K_SPACE ) { + CL_VideoStop(); + } + } +} + +void CL_VideoStop(void) +{ + cl_videoplaying = false; + + CL_CloseVideo( cl_videos ); } static void CL_PlayVideo_f(void) { - char name[1024]; + char name[MAX_QPATH]; + + Host_StartVideo(); if (Cmd_Argc() != 2) { - Con_Printf ("usage: playvideo \nplays video named video/.dpv\n"); + Con_Print("usage: playvideo \nplays video named video/.dpv\n"); return; } - sprintf(name, "%s/video/%s.dpv", com_gamedir, Cmd_Argv(1)); + sprintf(name, "video/%s.dpv", Cmd_Argv(1)); CL_VideoStart(name); } @@ -209,10 +314,39 @@ static void CL_StopVideo_f(void) CL_VideoStop(); } -void CL_Video_Init(void) +static void cl_video_start( void ) { - Cmd_AddCommand("playvideo", CL_PlayVideo_f); - Cmd_AddCommand("stopvideo", CL_StopVideo_f); + int i; + clvideo_t *video; - clvideomempool = Mem_AllocPool("CL_Video"); + cl_videotexturepool = R_AllocTexturePool(); + + for( video = cl_videos, i = 0 ; i < cl_num_videos ; i++, video++ ) + if( video->state != CLVIDEO_UNUSED && !video->suspended ) + video->cpif.tex = R_LoadTexture2D( cl_videotexturepool, video->cpif.name, + video->cpif.width, video->cpif.height, NULL, TEXTYPE_RGBA, TEXF_ALWAYSPRECACHE, NULL ); +} + +static void cl_video_shutdown( void ) +{ + R_FreeTexturePool( &cl_videotexturepool ); +} + +static void cl_video_newmap( void ) +{ } + +void CL_Video_Init( void ) +{ + cl_num_videos = 0; + cl_videobytesperpixel = 4; + cl_videormask = BigLong(0xFF000000); + cl_videogmask = BigLong(0x00FF0000); + cl_videobmask = BigLong(0x0000FF00); + + Cmd_AddCommand( "playvideo", CL_PlayVideo_f, "play a .dpv video file" ); + Cmd_AddCommand( "stopvideo", CL_StopVideo_f, "stop playing a .dpv video file" ); + + R_RegisterModule( "CL_Video", cl_video_start, cl_video_shutdown, cl_video_newmap ); +} +