2007-01-21 07:30:19 +01:00
// Copyright (c) 2005-2007, Rodrigo Braz Monteiro
// All rights reserved.
//
// Redistribution and use in source and binary forms, with or without
// modification, are permitted provided that the following conditions are met:
//
// * Redistributions of source code must retain the above copyright notice,
// this list of conditions and the following disclaimer.
// * Redistributions in binary form must reproduce the above copyright notice,
// this list of conditions and the following disclaimer in the documentation
// and/or other materials provided with the distribution.
// * Neither the name of the Aegisub Group nor the names of its contributors
// may be used to endorse or promote products derived from this software
// without specific prior written permission.
//
// THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
// AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
// IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
// ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE
// LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
// CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
// SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
// INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
// CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
// ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
// POSSIBILITY OF SUCH DAMAGE.
//
2009-07-29 07:43:02 +02:00
// Aegisub Project http://www.aegisub.org/
2007-01-21 07:30:19 +01:00
//
2009-07-29 07:43:02 +02:00
// $Id$
/// @file video_context.cpp
/// @brief Keep track of loaded video and video displays
/// @ingroup video
///
2007-01-21 07:30:19 +01:00
2009-01-04 07:31:48 +01:00
# include "config.h"
2009-09-10 15:06:40 +02:00
# ifndef AGI_PRE
# include <string.h>
# include <wx/clipbrd.h>
# include <wx/config.h>
# include <wx/filename.h>
# include <wx/image.h>
# endif
2007-09-12 01:22:26 +02:00
# ifdef __APPLE__
2007-04-22 04:24:27 +02:00
# include <OpenGL/GL.h>
# include <OpenGL/glu.h>
# else
2007-01-22 23:57:45 +01:00
# include <GL/gl.h>
# include <GL/glu.h>
2007-04-22 04:24:27 +02:00
# endif
2009-09-10 15:06:40 +02:00
2007-01-21 07:30:19 +01:00
# include "ass_dialogue.h"
2009-09-10 15:06:40 +02:00
# include "ass_file.h"
2007-01-21 07:30:19 +01:00
# include "ass_style.h"
2009-09-10 15:06:40 +02:00
# include "ass_time.h"
2007-01-21 07:30:19 +01:00
# include "audio_display.h"
2010-05-21 03:13:36 +02:00
# include "compat.h"
2010-08-02 08:31:38 +02:00
# include "include/aegisub/audio_player.h"
# include "include/aegisub/audio_provider.h"
2010-07-23 07:58:39 +02:00
# include "include/aegisub/video_provider.h"
2010-07-08 06:29:04 +02:00
# include "keyframe.h"
# include <libaegisub/access.h>
2010-05-21 03:13:36 +02:00
# include "main.h"
2010-05-19 02:44:37 +02:00
# include "mkv_wrap.h"
2007-06-21 00:23:55 +02:00
# include "standard_paths.h"
2009-09-10 15:06:40 +02:00
# include "subs_edit_box.h"
# include "subs_grid.h"
2010-07-23 07:58:39 +02:00
# include "threaded_frame_source.h"
2009-09-10 15:06:40 +02:00
# include "utils.h"
# include "video_box.h"
# include "video_context.h"
# include "video_display.h"
2010-08-02 08:32:01 +02:00
# include "video_frame.h"
2007-01-21 07:30:19 +01:00
2010-07-08 06:29:04 +02:00
/// IDs
2007-01-21 07:30:19 +01:00
enum {
VIDEO_PLAY_TIMER = 1300
} ;
BEGIN_EVENT_TABLE ( VideoContext , wxEvtHandler )
EVT_TIMER ( VIDEO_PLAY_TIMER , VideoContext : : OnPlayTimer )
END_EVENT_TABLE ( )
Note: This was done using a script! it's far from perfect but 95% of the work has been done already formatting-wise.
Document all functions, class, struct, union, enum, macro, variable, typedefs. This isn't the actual document in itself but empty documentation using any old documentation if it was there.
This was done using exuberant ctags to get tag info, then a TCL script to parse/remove old comments and convert them into Doxygen-style.
Some notes:
* Anything labeled 'DOCME' needs to be documented, @param and @return have been left blank as it would be annoying to delete the 'DOCME' from every one of those.
* Some multiline comments may have been munged into single line comments
* Leave the /// comments above global variables with a space, if they're harder to read then we'll be less likey to use them.
* Enum comments can go after the enumeration itself '[value] /// comment'
* include/aegisub/*.h haven't been converted yet, this will be done in a later commit
* Some documentation blocks are in the wrong place, in the .h when it should be in the .cpp, or vice versa.
See http://devel.aegisub.org/wiki/Doxygen for some details on Doxygen and a 'style guide'.
Originally committed to SVN as r3312.
2009-07-30 00:59:22 +02:00
/// @brief Constructor
///
2010-05-19 02:44:37 +02:00
VideoContext : : VideoContext ( )
2010-07-18 07:45:52 +02:00
: startFrame ( - 1 )
2010-05-20 10:55:58 +02:00
, endFrame ( - 1 )
, playNextFrame ( - 1 )
, nextFrame ( - 1 )
, isPlaying ( false )
, keepAudioSync ( true )
2010-05-19 02:44:37 +02:00
, frame_n ( 0 )
, length ( 0 )
, arValue ( 1. )
2010-05-20 10:55:58 +02:00
, arType ( 0 )
2010-05-19 02:44:37 +02:00
, hasSubtitles ( false )
2010-06-11 04:24:59 +02:00
, playAudioOnStep ( OPT_GET ( " Audio/Plays When Stepping Video " ) )
2010-07-20 05:11:11 +02:00
, singleFrame ( false )
2010-05-20 10:55:58 +02:00
, grid ( NULL )
, audio ( NULL )
2010-07-08 06:29:04 +02:00
, VFR_Input ( videoFPS )
, VFR_Output ( ovrFPS )
2010-05-19 02:44:37 +02:00
{
2010-07-23 07:58:39 +02:00
Bind ( EVT_VIDEO_ERROR , & VideoContext : : OnVideoError , this ) ;
Bind ( EVT_SUBTITLES_ERROR , & VideoContext : : OnSubtitlesError , this ) ;
2007-01-21 07:30:19 +01:00
}
VideoContext : : ~ VideoContext ( ) {
2010-07-08 06:29:04 +02:00
if ( audio & & audio - > temporary ) {
delete audio - > provider ;
delete audio - > player ;
}
2007-01-21 07:30:19 +01:00
}
VideoContext * VideoContext : : Get ( ) {
2010-07-08 06:29:04 +02:00
static VideoContext instance ;
return & instance ;
2007-01-21 07:30:19 +01:00
}
void VideoContext : : Reset ( ) {
2010-07-08 06:29:04 +02:00
StandardPaths : : SetPathValue ( _T ( " ?video " ) , " " ) ;
2007-06-21 00:23:55 +02:00
2010-07-08 06:29:04 +02:00
keyFrames . clear ( ) ;
videoFPS = agi : : vfr : : Framerate ( ) ;
2007-01-21 07:30:19 +01:00
// Remove temporary audio provider
if ( audio & & audio - > temporary ) {
delete audio - > provider ;
audio - > provider = NULL ;
delete audio - > player ;
audio - > player = NULL ;
audio - > temporary = false ;
}
// Remove video data
frame_n = 0 ;
2009-05-01 00:13:10 +02:00
length = 0 ;
2007-01-21 07:30:19 +01:00
isPlaying = false ;
nextFrame = - 1 ;
// Clean up video data
2010-07-08 06:29:04 +02:00
videoName . clear ( ) ;
2007-01-21 07:30:19 +01:00
// Remove provider
2010-07-23 07:58:39 +02:00
videoProvider . reset ( ) ;
2010-07-08 06:29:04 +02:00
provider . reset ( ) ;
2007-04-08 21:27:46 +02:00
}
2007-01-21 07:30:19 +01:00
void VideoContext : : SetVideo ( const wxString & filename ) {
2010-07-08 06:29:04 +02:00
Stop ( ) ;
2007-01-21 07:30:19 +01:00
Reset ( ) ;
2010-07-08 06:29:04 +02:00
if ( filename . empty ( ) ) return ;
2009-10-06 18:12:23 +02:00
2010-07-08 06:29:04 +02:00
try {
grid - > CommitChanges ( true ) ;
2007-01-21 07:30:19 +01:00
2010-07-23 08:40:12 +02:00
provider . reset ( new ThreadedFrameSource ( filename , this ) , std : : mem_fun ( & ThreadedFrameSource : : End ) ) ;
videoProvider = provider - > GetVideoProvider ( ) ;
2010-07-08 06:29:04 +02:00
2010-07-23 07:58:39 +02:00
keyFrames = videoProvider - > GetKeyFrames ( ) ;
2010-07-08 06:29:04 +02:00
// Set frame rate
2010-07-23 07:58:39 +02:00
videoFPS = videoProvider - > GetFPS ( ) ;
2010-07-08 06:29:04 +02:00
if ( ovrFPS . IsLoaded ( ) ) {
int ovr = wxMessageBox ( _ ( " You already have timecodes loaded. Would you like to replace them with timecodes from the video file? " ) , _ ( " Replace timecodes? " ) , wxYES_NO | wxICON_QUESTION ) ;
if ( ovr = = wxYES ) {
ovrFPS = agi : : vfr : : Framerate ( ) ;
ovrTimecodeFile . clear ( ) ;
2009-06-24 20:16:03 +02:00
}
2010-07-08 06:29:04 +02:00
}
2007-01-21 07:30:19 +01:00
2010-07-08 06:29:04 +02:00
// Gather video parameters
2010-07-23 07:58:39 +02:00
length = videoProvider - > GetFrameCount ( ) ;
2007-01-21 07:30:19 +01:00
2010-07-08 06:29:04 +02:00
// Set filename
videoName = filename ;
config : : mru - > Add ( " Video " , STD_STR ( filename ) ) ;
wxFileName fn ( filename ) ;
StandardPaths : : SetPathValue ( _T ( " ?video " ) , fn . GetPath ( ) ) ;
2007-07-29 11:06:38 +02:00
2010-07-08 06:29:04 +02:00
// Get frame
frame_n = 0 ;
2010-02-14 23:02:33 +01:00
2010-07-08 06:29:04 +02:00
// Show warning
2010-07-23 07:58:39 +02:00
wxString warning = videoProvider - > GetWarning ( ) ;
2010-07-08 06:29:04 +02:00
if ( ! warning . empty ( ) ) wxMessageBox ( warning , _T ( " Warning " ) , wxICON_WARNING | wxOK ) ;
2010-05-19 02:44:37 +02:00
2010-07-08 06:29:04 +02:00
hasSubtitles = false ;
if ( filename . Right ( 4 ) . Lower ( ) = = L " .mkv " ) {
hasSubtitles = MatroskaWrapper : : HasSubtitles ( filename ) ;
2007-01-21 07:30:19 +01:00
}
2010-07-08 06:29:04 +02:00
UpdateDisplays ( true ) ;
}
2010-08-02 08:32:01 +02:00
catch ( agi : : UserCancelException const & ) { }
catch ( agi : : FileNotAccessibleError const & err ) {
config : : mru - > Remove ( " Video " , STD_STR ( filename ) ) ;
wxMessageBox ( lagi_wxString ( err . GetMessage ( ) ) , L " Error setting video " , wxICON_ERROR | wxOK ) ;
2010-07-23 08:40:12 +02:00
}
2010-08-02 08:32:01 +02:00
catch ( VideoProviderError const & err ) {
wxMessageBox ( lagi_wxString ( err . GetMessage ( ) ) , L " Error setting video " , wxICON_ERROR | wxOK ) ;
2007-01-21 07:30:19 +01:00
}
}
void VideoContext : : AddDisplay ( VideoDisplay * display ) {
2010-07-23 07:58:39 +02:00
if ( std : : find ( displayList . begin ( ) , displayList . end ( ) , display ) = = displayList . end ( ) )
displayList . push_back ( display ) ;
2007-01-21 07:30:19 +01:00
}
void VideoContext : : RemoveDisplay ( VideoDisplay * display ) {
displayList . remove ( display ) ;
}
2010-06-28 09:13:15 +02:00
void VideoContext : : UpdateDisplays ( bool full , bool seek ) {
2010-07-08 06:29:04 +02:00
if ( ! IsLoaded ( ) ) return ;
2010-06-28 09:13:08 +02:00
2007-01-21 07:30:19 +01:00
for ( std : : list < VideoDisplay * > : : iterator cur = displayList . begin ( ) ; cur ! = displayList . end ( ) ; cur + + ) {
VideoDisplay * display = * cur ;
2007-01-23 05:42:08 +01:00
2010-07-23 06:15:36 +02:00
if ( ! seek ) {
display - > Refresh ( ) ;
}
2007-01-21 07:30:19 +01:00
if ( full ) {
display - > UpdateSize ( ) ;
2009-10-05 06:22:28 +02:00
display - > SetFrameRange ( 0 , GetLength ( ) - 1 ) ;
2007-01-21 07:30:19 +01:00
}
2010-06-30 08:39:27 +02:00
if ( seek | | full ) {
display - > SetFrame ( GetFrameN ( ) ) ;
}
2007-01-21 07:30:19 +01:00
}
2008-01-14 02:18:24 +01:00
// Update audio display
2008-01-15 23:51:16 +01:00
if ( audio & & audio - > loaded & & audio - > IsShownOnScreen ( ) ) {
2010-06-08 08:09:13 +02:00
static agi : : OptionValue * opt = OPT_GET ( " Audio/Display/Draw/Video Position " ) ;
if ( opt - > GetBool ( ) ) {
2008-01-14 02:18:24 +01:00
audio - > UpdateImage ( false ) ;
}
}
2007-01-21 07:30:19 +01:00
}
2010-07-23 07:58:39 +02:00
void VideoContext : : Refresh ( ) {
if ( ! IsLoaded ( ) ) return ;
2010-07-20 05:11:11 +02:00
2010-07-23 07:58:39 +02:00
provider - > LoadSubtitles ( grid - > ass ) ;
2010-06-16 08:20:39 +02:00
UpdateDisplays ( false ) ;
2007-01-21 07:30:19 +01:00
}
void VideoContext : : JumpToFrame ( int n ) {
2010-07-08 06:29:04 +02:00
if ( ! IsLoaded ( ) ) return ;
2007-01-21 07:30:19 +01:00
// Prevent intervention during playback
if ( isPlaying & & n ! = playNextFrame ) return ;
2009-12-01 01:32:43 +01:00
frame_n = n ;
2007-04-08 01:45:46 +02:00
2010-06-28 09:13:15 +02:00
UpdateDisplays ( false , true ) ;
2007-01-21 07:30:19 +01:00
2010-06-08 08:09:13 +02:00
static agi : : OptionValue * highlight = OPT_GET ( " Subtitle/Grid/Highlight Subtitles in Frame " ) ;
if ( ! isPlaying & & highlight - > GetBool ( ) ) grid - > Refresh ( false ) ;
2007-01-21 07:30:19 +01:00
}
2010-07-08 06:29:04 +02:00
void VideoContext : : JumpToTime ( int ms , agi : : vfr : : Time end ) {
JumpToFrame ( FrameAtTime ( ms , end ) ) ;
2007-01-21 07:30:19 +01:00
}
2010-07-23 07:58:39 +02:00
void VideoContext : : GetFrameAsync ( int n ) {
provider - > RequestFrame ( n , videoFPS . TimeAtFrame ( n ) / 1000.0 ) ;
}
2007-04-03 04:55:43 +02:00
2010-07-23 07:58:39 +02:00
AegiVideoFrame const & VideoContext : : GetFrame ( int n , bool raw ) {
return provider - > GetFrame ( n , videoFPS . TimeAtFrame ( n ) / 1000.0 , raw ) ;
2007-04-03 04:55:43 +02:00
}
2010-07-08 06:29:04 +02:00
int VideoContext : : GetWidth ( ) const {
2010-07-23 07:58:39 +02:00
return videoProvider - > GetWidth ( ) ;
2010-07-08 06:29:04 +02:00
}
int VideoContext : : GetHeight ( ) const {
2010-07-23 07:58:39 +02:00
return videoProvider - > GetHeight ( ) ;
2010-07-08 06:29:04 +02:00
}
2007-04-08 00:03:06 +02:00
void VideoContext : : SaveSnapshot ( bool raw ) {
2007-01-21 07:30:19 +01:00
// Get folder
2010-06-08 08:09:13 +02:00
static agi : : OptionValue * ssPath = OPT_GET ( " Path/Screenshot " ) ;
wxString option = lagi_wxString ( ssPath - > GetString ( ) ) ;
2007-01-21 07:30:19 +01:00
wxFileName videoFile ( videoName ) ;
wxString basepath ;
2008-01-10 23:28:47 +01:00
// Is it a path specifier and not an actual fixed path?
2007-06-21 02:46:50 +02:00
if ( option [ 0 ] = = _T ( ' ? ' ) ) {
2008-01-10 23:28:47 +01:00
// If dummy video is loaded, we can't save to the video location
2008-01-11 00:22:49 +01:00
if ( option . StartsWith ( _T ( " ?video " ) ) & & ( videoName . Find ( _T ( " ?dummy " ) ) ! = wxNOT_FOUND ) ) {
2008-01-10 23:28:47 +01:00
// So try the script location instead
option = _T ( " ?script " ) ;
}
// Find out where the ?specifier points to
2007-06-21 02:46:50 +02:00
basepath = StandardPaths : : DecodePath ( option ) ;
2010-07-08 06:29:04 +02:00
// If where ever that is isn't defined, we can't save there
2008-01-10 23:28:47 +01:00
if ( ( basepath = = _T ( " \\ " ) ) | | ( basepath = = _T ( " / " ) ) ) {
// So save to the current user's home dir instead
basepath = wxGetHomeDir ( ) ;
}
2007-01-21 07:30:19 +01:00
}
2008-01-10 23:28:47 +01:00
// Actual fixed (possibly relative) path, decode it
2007-06-21 02:46:50 +02:00
else basepath = DecodeRelativePath ( option , StandardPaths : : DecodePath ( _T ( " ?user/ " ) ) ) ;
2007-01-21 07:30:19 +01:00
basepath + = _T ( " / " ) + videoFile . GetName ( ) ;
// Get full path
int session_shot_count = 1 ;
wxString path ;
while ( 1 ) {
path = basepath + wxString : : Format ( _T ( " _%03i_%i.png " ) , session_shot_count , frame_n ) ;
+ + session_shot_count ;
wxFileName tryPath ( path ) ;
if ( ! tryPath . FileExists ( ) ) break ;
}
2007-04-08 00:03:06 +02:00
GetFrame ( frame_n , raw ) . GetImage ( ) . SaveFile ( path , wxBITMAP_TYPE_PNG ) ;
2007-01-21 07:30:19 +01:00
}
void VideoContext : : GetScriptSize ( int & sw , int & sh ) {
grid - > ass - > GetResolution ( sw , sh ) ;
}
2010-02-17 20:04:41 +01:00
void VideoContext : : PlayNextFrame ( ) {
if ( isPlaying )
return ;
int thisFrame = frame_n ;
JumpToFrame ( frame_n + 1 ) ;
// Start playing audio
2010-06-08 08:09:13 +02:00
if ( playAudioOnStep - > GetBool ( ) )
2010-07-08 06:29:04 +02:00
audio - > Play ( TimeAtFrame ( thisFrame ) , TimeAtFrame ( thisFrame + 1 ) ) ;
2010-02-17 20:04:41 +01:00
}
void VideoContext : : PlayPrevFrame ( ) {
if ( isPlaying )
return ;
int thisFrame = frame_n ;
JumpToFrame ( frame_n - 1 ) ;
// Start playing audio
2010-06-08 08:09:13 +02:00
if ( playAudioOnStep - > GetBool ( ) )
2010-07-08 06:29:04 +02:00
audio - > Play ( TimeAtFrame ( thisFrame - 1 ) , TimeAtFrame ( thisFrame ) ) ;
2010-02-17 20:04:41 +01:00
}
2007-01-21 07:30:19 +01:00
void VideoContext : : Play ( ) {
if ( isPlaying ) {
Stop ( ) ;
return ;
}
// Set variables
startFrame = frame_n ;
endFrame = - 1 ;
// Start playing audio
2010-07-08 06:29:04 +02:00
audio - > Play ( TimeAtFrame ( startFrame ) , - 1 ) ;
2007-01-21 07:30:19 +01:00
2007-03-18 02:20:25 +01:00
//audio->Play will override this if we put it before, so put it after.
isPlaying = true ;
2007-01-21 07:30:19 +01:00
// Start timer
2008-01-20 07:14:40 +01:00
playTime . Start ( ) ;
2007-01-21 07:30:19 +01:00
playback . SetOwner ( this , VIDEO_PLAY_TIMER ) ;
2008-01-20 07:14:40 +01:00
playback . Start ( 10 ) ;
2007-01-21 07:30:19 +01:00
}
void VideoContext : : PlayLine ( ) {
2010-06-26 13:32:16 +02:00
AssDialogue * curline = grid - > GetActiveLine ( ) ;
2007-01-21 07:30:19 +01:00
if ( ! curline ) return ;
// Start playing audio
audio - > Play ( curline - > Start . GetMS ( ) , curline - > End . GetMS ( ) ) ;
// Set variables
isPlaying = true ;
2010-07-20 05:11:11 +02:00
startFrame = FrameAtTime ( grid - > GetActiveLine ( ) - > Start . GetMS ( ) , agi : : vfr : : START ) ;
endFrame = FrameAtTime ( grid - > GetActiveLine ( ) - > End . GetMS ( ) , agi : : vfr : : END ) ;
2007-01-21 07:30:19 +01:00
// Jump to start
playNextFrame = startFrame ;
JumpToFrame ( startFrame ) ;
// Set other variables
2008-01-20 20:51:48 +01:00
playTime . Start ( ) ;
2007-01-21 07:30:19 +01:00
// Start timer
playback . SetOwner ( this , VIDEO_PLAY_TIMER ) ;
2008-01-20 07:14:40 +01:00
playback . Start ( 10 ) ;
2007-01-21 07:30:19 +01:00
}
void VideoContext : : Stop ( ) {
if ( isPlaying ) {
playback . Stop ( ) ;
isPlaying = false ;
audio - > Stop ( ) ;
}
}
void VideoContext : : OnPlayTimer ( wxTimerEvent & event ) {
// Lock
wxMutexError res = playMutex . TryLock ( ) ;
if ( res = = wxMUTEX_BUSY ) return ;
playMutex . Unlock ( ) ;
wxMutexLocker lock ( playMutex ) ;
// Get time difference
2008-01-20 07:14:40 +01:00
int dif = playTime . Time ( ) ;
2007-01-21 07:30:19 +01:00
// Find next frame
2010-07-08 06:29:04 +02:00
int startMs = TimeAtFrame ( startFrame ) ;
2007-01-21 07:30:19 +01:00
int nextFrame = frame_n ;
int i = 0 ;
for ( i = 0 ; i < 10 ; i + + ) {
if ( nextFrame > = length ) break ;
2010-07-08 06:29:04 +02:00
if ( dif < TimeAtFrame ( nextFrame ) - startMs ) {
2007-01-21 07:30:19 +01:00
break ;
}
nextFrame + + ;
}
// End
if ( nextFrame > = length | | ( endFrame ! = - 1 & & nextFrame > endFrame ) ) {
Stop ( ) ;
return ;
}
// Same frame
if ( nextFrame = = frame_n ) return ;
// Next frame is before or over 2 frames ahead, so force audio resync
2010-07-08 06:29:04 +02:00
if ( audio - > player & & keepAudioSync & & ( nextFrame < frame_n | | nextFrame > frame_n + 2 ) ) audio - > player - > SetCurrentPosition ( audio - > GetSampleAtMS ( TimeAtFrame ( nextFrame ) ) ) ;
2007-01-21 07:30:19 +01:00
// Jump to next frame
playNextFrame = nextFrame ;
frame_n = nextFrame ;
JumpToFrame ( nextFrame ) ;
// Sync audio
2007-07-30 06:05:45 +02:00
if ( keepAudioSync & & nextFrame % 10 = = 0 & & audio & & audio - > provider & & audio - > player ) {
2010-07-08 06:29:04 +02:00
int64_t audPos = audio - > GetSampleAtMS ( TimeAtFrame ( nextFrame ) ) ;
2007-08-31 16:11:35 +02:00
int64_t curPos = audio - > player - > GetCurrentPosition ( ) ;
2007-01-21 07:30:19 +01:00
int delta = int ( audPos - curPos ) ;
if ( delta < 0 ) delta = - delta ;
int maxDelta = audio - > provider - > GetSampleRate ( ) ;
if ( delta > maxDelta ) audio - > player - > SetCurrentPosition ( audPos ) ;
}
}
2010-07-08 06:29:04 +02:00
double VideoContext : : GetARFromType ( int type ) const {
if ( type = = 0 ) return ( double ) VideoContext : : Get ( ) - > GetWidth ( ) / ( double ) VideoContext : : Get ( ) - > GetHeight ( ) ;
if ( type = = 1 ) return 4.0 / 3.0 ;
if ( type = = 2 ) return 16.0 / 9.0 ;
if ( type = = 3 ) return 2.35 ;
return 1.0 ; //error
2007-01-21 07:30:19 +01:00
}
2010-07-08 06:29:04 +02:00
void VideoContext : : SetAspectRatio ( int type , double value ) {
if ( type ! = 4 ) value = GetARFromType ( type ) ;
if ( value < 0.5 ) value = 0.5 ;
if ( value > 5.0 ) value = 5.0 ;
2007-01-21 07:30:19 +01:00
2010-07-08 06:29:04 +02:00
arType = type ;
arValue = value ;
UpdateDisplays ( true ) ;
2007-01-21 07:30:19 +01:00
}
2010-07-08 06:29:04 +02:00
void VideoContext : : LoadKeyframes ( wxString filename ) {
if ( filename = = keyFramesFilename | | filename . empty ( ) ) return ;
2010-08-02 10:18:53 +02:00
try {
keyFrames = KeyFrameFile : : Load ( filename ) ;
keyFramesFilename = filename ;
Refresh ( ) ;
}
catch ( const wchar_t * error ) {
wxMessageBox ( error , _T ( " Error opening keyframes file " ) , wxOK | wxICON_ERROR , NULL ) ;
}
catch ( agi : : acs : : AcsNotFound const & ) {
wxLogError ( L " Could not open file " + filename ) ;
config : : mru - > Remove ( " Keyframes " , STD_STR ( filename ) ) ;
}
catch ( . . . ) {
wxMessageBox ( _T ( " Unknown error " ) , _T ( " Error opening keyframes file " ) , wxOK | wxICON_ERROR , NULL ) ;
}
2007-01-21 07:30:19 +01:00
}
2010-07-08 06:29:04 +02:00
void VideoContext : : SaveKeyframes ( wxString filename ) {
KeyFrameFile : : Save ( filename , GetKeyFrames ( ) ) ;
2007-01-21 07:30:19 +01:00
}
2010-07-08 06:29:04 +02:00
void VideoContext : : CloseKeyframes ( ) {
keyFramesFilename . clear ( ) ;
2010-07-23 07:58:39 +02:00
if ( videoProvider . get ( ) ) {
keyFrames = videoProvider - > GetKeyFrames ( ) ;
2010-07-08 06:29:04 +02:00
}
else {
keyFrames . clear ( ) ;
}
Refresh ( ) ;
2007-01-21 07:30:19 +01:00
}
2010-07-08 06:29:04 +02:00
void VideoContext : : LoadTimecodes ( wxString filename ) {
if ( filename = = ovrTimecodeFile | | filename . empty ( ) ) return ;
try {
ovrFPS = agi : : vfr : : Framerate ( STD_STR ( filename ) ) ;
ovrTimecodeFile = filename ;
config : : mru - > Add ( " Timecodes " , STD_STR ( filename ) ) ;
Refresh ( ) ;
}
catch ( const agi : : acs : : AcsError & ) {
wxLogError ( L " Could not open file " + filename ) ;
2010-08-02 10:18:53 +02:00
config : : mru - > Remove ( " Timecodes " , STD_STR ( filename ) ) ;
2010-07-08 06:29:04 +02:00
}
catch ( const agi : : vfr : : Error & e ) {
wxLogError ( L " Timecode file parse error: %s " , e . GetMessage ( ) . c_str ( ) ) ;
}
2007-01-21 07:30:19 +01:00
}
2010-07-08 06:29:04 +02:00
void VideoContext : : SaveTimecodes ( wxString filename ) {
try {
ovrFPS . Save ( STD_STR ( filename ) , IsLoaded ( ) ? length : - 1 ) ;
config : : mru - > Add ( " Timecodes " , STD_STR ( filename ) ) ;
}
catch ( const agi : : acs : : AcsError & ) {
wxLogError ( L " Could not write to " + filename ) ;
}
}
void VideoContext : : CloseTimecodes ( ) {
ovrFPS = agi : : vfr : : Framerate ( ) ;
ovrTimecodeFile . clear ( ) ;
Refresh ( ) ;
2007-01-23 07:32:16 +01:00
}
2010-07-08 06:29:04 +02:00
int VideoContext : : TimeAtFrame ( int frame , agi : : vfr : : Time type ) const {
if ( ovrFPS . IsLoaded ( ) ) {
return ovrFPS . TimeAtFrame ( frame , type ) ;
}
return videoFPS . TimeAtFrame ( frame , type ) ;
}
int VideoContext : : FrameAtTime ( int time , agi : : vfr : : Time type ) const {
if ( ovrFPS . IsLoaded ( ) ) {
return ovrFPS . FrameAtTime ( time , type ) ;
}
return videoFPS . FrameAtTime ( time , type ) ;
2007-01-23 07:32:16 +01:00
}
2010-07-23 07:58:39 +02:00
void VideoContext : : OnVideoError ( VideoProviderErrorEvent const & err ) {
wxLogError (
L " Failed seeking video. The video file may be corrupt or incomplete. \n "
L " Error message reported: %s " ,
lagi_wxString ( err . GetMessage ( ) ) . c_str ( ) ) ;
}
void VideoContext : : OnSubtitlesError ( SubtitlesProviderErrorEvent const & err ) {
wxLogError (
L " Failed rendering subtitles. Error message reported: %s " ,
lagi_wxString ( err . GetMessage ( ) ) . c_str ( ) ) ;
}