+ if( !WakeVideo( video ) )
+ return NULL;
+ else if( video->state == CLVIDEO_RESETONWAKEUP )
+ video->framenum = -1;
+ }
+
+ video->lasttime = realtime;
+
+ return video;
+}
+
+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;
+
+ 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;
+}
+
+static void VideoFrame( clvideo_t *video )
+{
+ 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 );