2007-01-08 22:11:06 +01:00
// Copyright (c) 2005-2007, Rodrigo Braz Monteiro
2006-01-16 22:02:54 +01:00
// 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.
//
// -----------------------------------------------------------------------------
//
// AEGISUB
//
// Website: http://aegisub.cellosoft.com
// Contact: mailto:zeratul@cellosoft.com
//
////////////
// Includes
2006-12-23 04:55:00 +01:00
# include "setup.h"
2007-01-06 22:07:51 +01:00
# include <wx/image.h>
# include <string.h>
# include <wx/clipbrd.h>
# include <wx/filename.h>
# include <wx/config.h>
# include "utils.h"
2006-01-16 22:02:54 +01:00
# include "video_display.h"
2007-01-08 22:11:06 +01:00
# include "video_display_visual.h"
2006-02-24 09:30:08 +01:00
# include "video_provider.h"
2006-01-16 22:02:54 +01:00
# include "vfr.h"
# include "ass_file.h"
# include "ass_time.h"
# include "ass_dialogue.h"
2007-01-08 22:11:06 +01:00
# include "ass_style.h"
2006-01-16 22:02:54 +01:00
# include "subs_grid.h"
# include "vfw_wrap.h"
2006-02-23 02:44:48 +01:00
# include "mkv_wrap.h"
2006-01-16 22:02:54 +01:00
# include "options.h"
# include "subs_edit_box.h"
# include "audio_display.h"
# include "main.h"
# include "video_slider.h"
2006-12-23 04:55:00 +01:00
# if USE_FEXTRACKER == 1
2006-03-05 20:14:42 +01:00
# include "../FexTrackerSource/FexTracker.h"
# include "../FexTrackerSource/FexTrackingFeature.h"
# include "../FexTrackerSource/FexMovement.h"
2006-02-26 03:49:38 +01:00
# endif
2006-01-16 22:02:54 +01:00
///////
// IDs
enum {
VIDEO_MENU_COPY_TO_CLIPBOARD = 1230 ,
VIDEO_MENU_COPY_COORDS ,
VIDEO_MENU_SAVE_SNAPSHOT ,
VIDEO_PLAY_TIMER
} ;
///////////////
// Event table
BEGIN_EVENT_TABLE ( VideoDisplay , wxWindow )
EVT_MOUSE_EVENTS ( VideoDisplay : : OnMouseEvent )
2007-01-09 02:52:30 +01:00
EVT_KEY_DOWN ( VideoDisplay : : OnKey )
2006-01-16 22:02:54 +01:00
EVT_LEAVE_WINDOW ( VideoDisplay : : OnMouseLeave )
EVT_PAINT ( VideoDisplay : : OnPaint )
EVT_TIMER ( VIDEO_PLAY_TIMER , VideoDisplay : : OnPlayTimer )
EVT_MENU ( VIDEO_MENU_COPY_TO_CLIPBOARD , VideoDisplay : : OnCopyToClipboard )
EVT_MENU ( VIDEO_MENU_SAVE_SNAPSHOT , VideoDisplay : : OnSaveSnapshot )
EVT_MENU ( VIDEO_MENU_COPY_COORDS , VideoDisplay : : OnCopyCoords )
END_EVENT_TABLE ( )
///////////////
// Constructor
VideoDisplay : : VideoDisplay ( wxWindow * parent , wxWindowID id , const wxPoint & pos , const wxSize & size , long style , const wxString & name )
: wxWindow ( parent , id , pos , size , style , name )
{
2006-07-01 08:32:11 +02:00
audio = NULL ;
2006-01-22 13:44:53 +01:00
provider = NULL ;
2006-01-16 22:02:54 +01:00
curLine = NULL ;
ControlSlider = NULL ;
PositionDisplay = NULL ;
loaded = false ;
2006-12-18 03:44:38 +01:00
keyFramesLoaded = false ;
2006-12-18 05:53:40 +01:00
overKeyFramesLoaded = false ;
2006-01-16 22:02:54 +01:00
frame_n = 0 ;
origSize = size ;
arType = 0 ;
IsPlaying = false ;
threaded = Options . AsBool ( _T ( " Threaded Video " ) ) ;
nextFrame = - 1 ;
2006-02-19 03:31:25 +01:00
zoomValue = 0.5 ;
2007-01-08 22:11:06 +01:00
visual = new VideoDisplayVisual ( this ) ;
2006-01-16 22:02:54 +01:00
}
//////////////
// Destructor
VideoDisplay : : ~ VideoDisplay ( ) {
2006-02-22 07:49:20 +01:00
wxRemoveFile ( tempfile ) ;
tempfile = _T ( " " ) ;
2006-01-22 13:44:53 +01:00
SetVideo ( _T ( " " ) ) ;
2007-01-08 22:11:06 +01:00
delete visual ;
2006-01-16 22:02:54 +01:00
}
2006-01-22 13:44:53 +01:00
void VideoDisplay : : UpdateSize ( ) {
if ( provider ) {
w = provider - > GetWidth ( ) ;
h = provider - > GetHeight ( ) ;
2006-01-16 22:02:54 +01:00
2006-01-22 13:44:53 +01:00
// Set the size for this control
2006-10-24 00:17:27 +02:00
SetSizeHints ( w , h , w , h ) ;
2006-01-22 13:44:53 +01:00
SetClientSize ( w , h ) ;
int _w , _h ;
GetSize ( & _w , & _h ) ;
SetSizeHints ( _w , _h , _w , _h ) ;
2006-01-16 22:02:54 +01:00
}
}
2006-04-19 23:55:02 +02:00
2007-01-02 21:07:52 +01:00
//////////
// Resets
void VideoDisplay : : Reset ( ) {
w = origSize . GetX ( ) ;
h = origSize . GetY ( ) ;
SetClientSize ( w , h ) ;
int _w , _h ;
GetSize ( & _w , & _h ) ;
SetSizeHints ( _w , _h , _w , _h ) ;
KeyFrames . Clear ( ) ;
keyFramesLoaded = false ;
// Remove temporary audio provider
if ( audio & & audio - > temporary ) {
delete audio - > provider ;
audio - > provider = NULL ;
delete audio - > player ;
audio - > player = NULL ;
audio - > temporary = false ;
}
}
2006-01-16 22:02:54 +01:00
///////////////////////
2006-01-22 13:44:53 +01:00
// Sets video filename
void VideoDisplay : : SetVideo ( const wxString & filename ) {
2006-02-24 09:30:08 +01:00
// Unload video
2006-02-25 21:48:32 +01:00
delete provider ;
provider = NULL ;
2006-04-04 15:22:45 +02:00
//if (VFR_Output.GetFrameRateType() == VFR) VFR_Output.Unload();
//VFR_Input.Unload();
2006-02-25 21:48:32 +01:00
videoName = _T ( " " ) ;
loaded = false ;
frame_n = 0 ;
Reset ( ) ;
2006-02-24 09:30:08 +01:00
// Load video
2006-02-25 21:48:32 +01:00
if ( ! filename . IsEmpty ( ) ) {
2006-01-16 22:02:54 +01:00
try {
grid - > CommitChanges ( true ) ;
2007-01-01 04:29:20 +01:00
bool isVfr = false ;
double overFps = 0 ;
2007-01-02 21:07:52 +01:00
FrameRate temp ;
// Unload timecodes
//int unload = wxYES;
//if (VFR_Output.IsLoaded()) unload = wxMessageBox(_("Do you want to unload timecodes, too?"),_("Unload timecodes?"),wxYES_NO | wxICON_QUESTION);
//if (unload == wxYES) VFR_Output.Unload();
2006-01-16 22:02:54 +01:00
2006-02-25 21:48:32 +01:00
// Read extra data from file
bool mkvOpen = MatroskaWrapper : : wrapper . IsOpen ( ) ;
2006-02-23 02:44:48 +01:00
wxString ext = filename . Right ( 4 ) . Lower ( ) ;
2006-02-25 21:48:32 +01:00
KeyFrames . Clear ( ) ;
if ( ext = = _T ( " .mkv " ) | | mkvOpen ) {
2006-02-23 02:44:48 +01:00
// Parse mkv
2006-02-25 21:48:32 +01:00
if ( ! mkvOpen ) MatroskaWrapper : : wrapper . Open ( filename ) ;
2006-02-23 02:44:48 +01:00
// Get keyframes
2006-02-25 21:48:32 +01:00
KeyFrames = MatroskaWrapper : : wrapper . GetKeyFrames ( ) ;
2006-12-18 03:44:38 +01:00
keyFramesLoaded = true ;
2006-02-23 02:44:48 +01:00
// Ask to override timecodes
int override = wxYES ;
2006-06-22 01:07:03 +02:00
if ( VFR_Output . IsLoaded ( ) ) override = wxMessageBox ( _ ( " You already have timecodes loaded. Replace them with the timecodes from the Matroska file? " ) , _ ( " Replace timecodes? " ) , wxYES_NO | wxICON_QUESTION ) ;
2007-01-01 04:29:20 +01:00
if ( override = = wxYES ) {
2007-01-02 21:07:52 +01:00
MatroskaWrapper : : wrapper . SetToTimecodes ( temp ) ;
isVfr = temp . GetFrameRateType ( ) = = VFR ;
if ( isVfr ) {
overFps = temp . GetCommonFPS ( ) ;
MatroskaWrapper : : wrapper . SetToTimecodes ( VFR_Input ) ;
MatroskaWrapper : : wrapper . SetToTimecodes ( VFR_Output ) ;
}
2007-01-01 04:29:20 +01:00
}
2006-02-23 02:44:48 +01:00
// Close mkv
2006-02-25 21:48:32 +01:00
MatroskaWrapper : : wrapper . Close ( ) ;
2006-02-23 02:44:48 +01:00
}
2006-05-10 02:33:44 +02:00
# ifdef __WINDOWS__
2006-12-18 03:44:38 +01:00
else if ( ext = = _T ( " .avi " ) ) {
KeyFrames = VFWWrapper : : GetKeyFrames ( filename ) ;
keyFramesLoaded = true ;
}
2006-02-26 03:53:45 +01:00
# endif
2006-01-16 22:02:54 +01:00
2007-01-01 04:29:20 +01:00
// Choose a provider
provider = VideoProvider : : GetProvider ( filename , GetTempWorkFile ( ) , overFps ) ;
2007-01-02 21:07:52 +01:00
if ( isVfr ) provider - > OverrideFrameTimeList ( temp . GetFrameTimeList ( ) ) ;
2007-01-01 04:29:20 +01:00
provider - > SetZoom ( zoomValue ) ;
if ( arType ! = 4 ) arValue = GetARFromType ( arType ) ; // 4 = custom
provider - > SetDAR ( arValue ) ;
2006-02-25 21:48:32 +01:00
// Update size
2006-01-22 13:44:53 +01:00
UpdateSize ( ) ;
2006-01-16 22:02:54 +01:00
2006-01-22 13:44:53 +01:00
//Gather video parameters
length = provider - > GetFrameCount ( ) ;
fps = provider - > GetFPS ( ) ;
2007-01-02 21:07:52 +01:00
if ( ! isVfr ) {
VFR_Input . SetCFR ( fps ) ;
if ( VFR_Output . GetFrameRateType ( ) ! = VFR ) VFR_Output . SetCFR ( fps ) ;
}
2006-01-16 22:02:54 +01:00
2006-01-22 13:44:53 +01:00
// Set range of slider
ControlSlider - > SetRange ( 0 , length - 1 ) ;
ControlSlider - > SetValue ( 0 ) ;
2006-01-16 22:02:54 +01:00
2006-01-22 13:44:53 +01:00
videoName = filename ;
2006-01-16 22:02:54 +01:00
2006-01-22 13:44:53 +01:00
// Add to recent
Options . AddToRecentList ( filename , _T ( " Recent vid " ) ) ;
2006-01-16 22:02:54 +01:00
2006-01-22 13:44:53 +01:00
RefreshVideo ( ) ;
2006-02-25 00:07:30 +01:00
UpdatePositionDisplay ( ) ;
2006-04-19 23:55:02 +02:00
}
catch ( wxString & e ) {
2006-01-22 13:44:53 +01:00
wxMessageBox ( e , _T ( " Error setting video " ) , wxICON_ERROR | wxOK ) ;
2006-01-16 22:02:54 +01:00
}
}
2006-01-22 13:44:53 +01:00
loaded = provider ! = NULL ;
}
2006-01-16 22:02:54 +01:00
2007-01-02 21:07:52 +01:00
/////////////////////
// Refresh subtitles
2006-01-22 13:44:53 +01:00
void VideoDisplay : : RefreshSubtitles ( ) {
provider - > RefreshSubtitles ( ) ;
RefreshVideo ( ) ;
2006-01-16 22:02:54 +01:00
}
/////////////////
// OnPaint event
void VideoDisplay : : OnPaint ( wxPaintEvent & event ) {
wxPaintDC dc ( this ) ;
// Draw frame
2006-12-19 03:59:03 +01:00
if ( provider ) dc . DrawBitmap ( GetFrame ( frame_n ) , 0 , 0 ) ;
2006-01-16 22:02:54 +01:00
}
///////////////
// Mouse stuff
void VideoDisplay : : OnMouseEvent ( wxMouseEvent & event ) {
// Disable when playing
if ( IsPlaying ) return ;
2006-02-01 23:15:21 +01:00
if ( event . Leaving ( ) ) {
// OnMouseLeave isn't called as long as we have an OnMouseEvent
// Just check for it and call it manually instead
OnMouseLeave ( event ) ;
event . Skip ( true ) ;
return ;
}
2006-01-16 22:02:54 +01:00
// Right click
if ( event . ButtonUp ( wxMOUSE_BTN_RIGHT ) ) {
wxMenu menu ;
menu . Append ( VIDEO_MENU_SAVE_SNAPSHOT , _ ( " Save PNG snapshot " ) ) ;
menu . Append ( VIDEO_MENU_COPY_TO_CLIPBOARD , _ ( " Copy image to Clipboard " ) ) ;
menu . Append ( VIDEO_MENU_COPY_COORDS , _ ( " Copy coordinates to Clipboard " ) ) ;
PopupMenu ( & menu ) ;
return ;
}
2007-01-09 02:52:30 +01:00
// Click?
if ( event . ButtonDown ( wxMOUSE_BTN_ANY ) ) {
SetFocus ( ) ;
}
2007-01-08 22:11:06 +01:00
// Send to visual
visual - > OnMouseEvent ( event ) ;
2006-01-16 22:02:54 +01:00
}
2007-01-09 02:52:30 +01:00
/////////////
// Key event
void VideoDisplay : : OnKey ( wxKeyEvent & event ) {
visual - > OnKeyEvent ( event ) ;
}
2006-01-16 22:02:54 +01:00
//////////////////////
// Mouse left display
void VideoDisplay : : OnMouseLeave ( wxMouseEvent & event ) {
if ( IsPlaying ) return ;
2006-01-28 22:47:46 +01:00
bTrackerEditing = 0 ;
2006-01-22 13:44:53 +01:00
RefreshVideo ( ) ;
2006-01-16 22:02:54 +01:00
}
///////////////////////////////////////
// Jumps to a frame and update display
void VideoDisplay : : JumpToFrame ( int n ) {
// Loaded?
if ( ! loaded ) return ;
// Prevent intervention during playback
if ( IsPlaying & & n ! = PlayNextFrame ) return ;
// Set frame
2006-01-22 13:44:53 +01:00
GetFrame ( n ) ;
// Display
RefreshVideo ( ) ;
UpdatePositionDisplay ( ) ;
2006-01-16 22:02:54 +01:00
// Update slider
2006-01-22 13:44:53 +01:00
ControlSlider - > SetValue ( n ) ;
2006-01-16 22:02:54 +01:00
// Update grid
2006-02-22 05:59:39 +01:00
if ( ! IsPlaying & & Options . AsBool ( _T ( " Highlight subs in frame " ) ) ) grid - > Refresh ( false ) ;
2006-01-16 22:02:54 +01:00
}
////////////////////////////
// Jumps to a specific time
void VideoDisplay : : JumpToTime ( int ms ) {
2006-02-24 17:45:10 +01:00
JumpToFrame ( VFR_Output . GetFrameAtTime ( ms ) ) ;
2006-01-16 22:02:54 +01:00
}
///////////////////
// Sets zoom level
void VideoDisplay : : SetZoom ( double value ) {
2006-02-19 03:31:25 +01:00
zoomValue = value ;
2006-01-22 13:44:53 +01:00
if ( provider ) {
provider - > SetZoom ( value ) ;
UpdateSize ( ) ;
RefreshVideo ( ) ;
GetParent ( ) - > Layout ( ) ;
2006-01-16 22:02:54 +01:00
}
}
//////////////////////
// Sets zoom position
void VideoDisplay : : SetZoomPos ( int value ) {
if ( value < 0 ) value = 0 ;
if ( value > 15 ) value = 15 ;
SetZoom ( double ( value + 1 ) / 8.0 ) ;
if ( zoomBox - > GetSelection ( ) ! = value ) zoomBox - > SetSelection ( value ) ;
}
2006-02-24 09:43:44 +01:00
//////////////////////////
// Calculate aspect ratio
double VideoDisplay : : GetARFromType ( int type ) {
if ( type = = 0 ) return ( double ) provider - > GetSourceWidth ( ) / ( double ) provider - > GetSourceHeight ( ) ;
if ( type = = 1 ) return 4.0 / 3.0 ;
if ( type = = 2 ) return 16.0 / 9.0 ;
2006-05-16 01:44:53 +02:00
if ( type = = 3 ) return 2.35 ;
return 1.0 ; //error
2006-02-24 09:43:44 +01:00
}
/////////////////////
// Sets aspect ratio
2006-05-16 01:44:53 +02:00
void VideoDisplay : : SetAspectRatio ( int _type , double value ) {
2006-01-22 13:44:53 +01:00
if ( provider ) {
2006-05-16 01:44:53 +02:00
// Get value
if ( _type ! = 4 ) value = GetARFromType ( _type ) ;
if ( value < 0.5 ) value = 0.5 ;
if ( value > 5.0 ) value = 5.0 ;
// Set
provider - > SetDAR ( value ) ;
arType = _type ;
arValue = value ;
2006-01-22 13:44:53 +01:00
UpdateSize ( ) ;
RefreshVideo ( ) ;
GetParent ( ) - > Layout ( ) ;
2006-01-16 22:02:54 +01:00
}
}
////////////////////////////
// Updates position display
void VideoDisplay : : UpdatePositionDisplay ( ) {
// Update position display control
if ( ! PositionDisplay ) {
throw _T ( " Position Display not set! " ) ;
}
// Get time
2006-02-25 00:07:30 +01:00
int time = VFR_Output . GetTimeAtFrame ( frame_n , true , true ) ;
2006-01-16 22:02:54 +01:00
int temp = time ;
int h = 0 , m = 0 , s = 0 , ms = 0 ;
while ( temp > = 3600000 ) {
temp - = 3600000 ;
h + + ;
}
while ( temp > = 60000 ) {
temp - = 60000 ;
m + + ;
}
while ( temp > = 1000 ) {
temp - = 1000 ;
s + + ;
}
ms = temp ;
// Position display update
PositionDisplay - > SetValue ( wxString : : Format ( _T ( " %01i:%02i:%02i.%03i - %i " ) , h , m , s , ms , frame_n ) ) ;
2006-12-18 05:53:40 +01:00
if ( GetKeyFrames ( ) . Index ( frame_n ) ! = wxNOT_FOUND ) {
2006-01-16 22:02:54 +01:00
PositionDisplay - > SetBackgroundColour ( Options . AsColour ( _T ( " Grid selection background " ) ) ) ;
PositionDisplay - > SetForegroundColour ( Options . AsColour ( _T ( " Grid selection foreground " ) ) ) ;
}
else {
PositionDisplay - > SetBackgroundColour ( wxNullColour ) ;
PositionDisplay - > SetForegroundColour ( wxNullColour ) ;
}
// Subs position display update
UpdateSubsRelativeTime ( ) ;
}
////////////////////////////////////////////////////
// Updates box with subs position relative to frame
void VideoDisplay : : UpdateSubsRelativeTime ( ) {
// Set variables
wxString startSign ;
wxString endSign ;
int startOff , endOff ;
// Set start/end
if ( curLine ) {
2006-02-25 00:07:30 +01:00
int time = VFR_Output . GetTimeAtFrame ( frame_n , true , true ) ;
2006-01-16 22:02:54 +01:00
startOff = time - curLine - > Start . GetMS ( ) ;
endOff = time - curLine - > End . GetMS ( ) ;
}
// Fallback to zero
else {
startOff = 0 ;
endOff = 0 ;
}
// Positive signs
if ( startOff > 0 ) startSign = _T ( " + " ) ;
if ( endOff > 0 ) endSign = _T ( " + " ) ;
// Update line
SubsPosition - > SetValue ( wxString : : Format ( _T ( " %s%ims; %s%ims " ) , startSign . c_str ( ) , startOff , endSign . c_str ( ) , endOff ) ) ;
}
/////////////////////
// Copy to clipboard
void VideoDisplay : : OnCopyToClipboard ( wxCommandEvent & event ) {
if ( wxTheClipboard - > Open ( ) ) {
2006-01-22 13:44:53 +01:00
wxTheClipboard - > SetData ( new wxBitmapDataObject ( GetFrame ( frame_n ) ) ) ;
2006-01-16 22:02:54 +01:00
wxTheClipboard - > Close ( ) ;
}
}
/////////////////
// Save snapshot
void VideoDisplay : : OnSaveSnapshot ( wxCommandEvent & event ) {
SaveSnapshot ( ) ;
}
void VideoDisplay : : SaveSnapshot ( ) {
2007-01-06 22:07:51 +01:00
// Get folder
wxString option = Options . AsText ( _ ( " Video Screenshot Path " ) ) ;
wxFileName videoFile ( videoName ) ;
wxString basepath ;
if ( option = = _T ( " ?video " ) ) {
basepath = videoFile . GetPath ( ) ;
}
else if ( option = = _T ( " ?script " ) ) {
if ( grid - > ass - > filename . IsEmpty ( ) ) basepath = videoFile . GetPath ( ) ;
else {
wxFileName file2 ( grid - > ass - > filename ) ;
basepath = file2 . GetPath ( ) ;
}
}
else basepath = DecodeRelativePath ( option , ( ( AegisubApp * ) wxTheApp ) - > folderName ) ;
basepath + = _T ( " / " ) + videoFile . GetName ( ) ;
// Get full path
int session_shot_count = 1 ;
2006-01-16 22:02:54 +01:00
wxString path ;
2006-11-23 18:26:53 +01:00
while ( 1 ) {
path = basepath + wxString : : Format ( _T ( " _%03i_%i.png " ) , session_shot_count , frame_n ) ;
+ + session_shot_count ;
2006-01-16 22:02:54 +01:00
wxFileName tryPath ( path ) ;
if ( ! tryPath . FileExists ( ) ) break ;
}
// Save
2006-01-22 13:44:53 +01:00
GetFrame ( frame_n ) . ConvertToImage ( ) . SaveFile ( path , wxBITMAP_TYPE_PNG ) ;
2006-01-16 22:02:54 +01:00
}
/////////////////////
// Copy coordinates
void VideoDisplay : : OnCopyCoords ( wxCommandEvent & event ) {
if ( wxTheClipboard - > Open ( ) ) {
int sw , sh ;
GetScriptSize ( sw , sh ) ;
2007-01-08 22:11:06 +01:00
int vx = ( sw * visual - > mouse_x + w / 2 ) / w ;
int vy = ( sh * visual - > mouse_y + h / 2 ) / h ;
2006-01-16 22:02:54 +01:00
wxTheClipboard - > SetData ( new wxTextDataObject ( wxString : : Format ( _T ( " %i,%i " ) , vx , vy ) ) ) ;
wxTheClipboard - > Close ( ) ;
}
}
//////////////////
// Refresh screen
2006-01-22 13:44:53 +01:00
void VideoDisplay : : RefreshVideo ( ) {
// Draw frame
wxClientDC dc ( this ) ;
dc . DrawBitmap ( GetFrame ( ) , 0 , 0 ) ;
2006-01-28 20:48:35 +01:00
// Draw the control points for FexTracker
2007-01-08 22:11:06 +01:00
visual - > DrawTrackingOverlay ( dc ) ;
2006-01-28 20:48:35 +01:00
}
//////////////////
// DrawVideoWithOverlay
void VideoDisplay : : DrawText ( wxPoint Pos , wxString text ) {
// Draw frame
wxClientDC dc ( this ) ;
dc . SetBrush ( wxBrush ( wxColour ( 128 , 128 , 128 ) , wxSOLID ) ) ;
dc . DrawRectangle ( 0 , 0 , provider - > GetWidth ( ) , provider - > GetHeight ( ) ) ;
dc . SetTextForeground ( wxColour ( 64 , 64 , 64 ) ) ;
dc . DrawText ( text , Pos . x + 1 , Pos . y - 1 ) ;
dc . DrawText ( text , Pos . x + 1 , Pos . y + 1 ) ;
dc . DrawText ( text , Pos . x - 1 , Pos . y - 1 ) ;
dc . DrawText ( text , Pos . x - 1 , Pos . y + 1 ) ;
dc . SetTextForeground ( wxColour ( 255 , 255 , 255 ) ) ;
dc . DrawText ( text , Pos . x , Pos . y ) ;
2006-01-16 22:02:54 +01:00
}
////////////////////////
// Requests a new frame
2006-01-22 13:44:53 +01:00
wxBitmap VideoDisplay : : GetFrame ( int n ) {
frame_n = n ;
return provider - > GetFrame ( n ) ;
RefreshVideo ( ) ;
2006-01-16 22:02:54 +01:00
}
////////////////////////////
// Get dimensions of script
void VideoDisplay : : GetScriptSize ( int & sw , int & sh ) {
2006-07-01 09:22:57 +02:00
grid - > ass - > GetResolution ( sw , sh ) ;
2006-01-16 22:02:54 +01:00
}
////////
// Play
void VideoDisplay : : Play ( ) {
// Stop if already playing
if ( IsPlaying ) {
Stop ( ) ;
return ;
}
// Set variables
IsPlaying = true ;
StartFrame = frame_n ;
EndFrame = - 1 ;
// Start playing audio
audio - > Play ( VFR_Output . GetTimeAtFrame ( StartFrame ) , - 1 ) ;
// Start timer
2006-07-01 08:32:11 +02:00
StartTime = clock ( ) ;
PlayTime = StartTime ;
2006-01-16 22:02:54 +01:00
Playback . SetOwner ( this , VIDEO_PLAY_TIMER ) ;
Playback . Start ( 1 ) ;
}
/////////////
// Play line
void VideoDisplay : : PlayLine ( ) {
// Get line
AssDialogue * curline = grid - > GetDialogue ( grid - > editBox - > linen ) ;
if ( ! curline ) return ;
2006-07-01 08:32:11 +02:00
// Start playing audio
audio - > Play ( curline - > Start . GetMS ( ) , curline - > End . GetMS ( ) ) ;
2006-01-16 22:02:54 +01:00
// Set variables
IsPlaying = true ;
2006-02-24 17:45:10 +01:00
StartFrame = VFR_Output . GetFrameAtTime ( curline - > Start . GetMS ( ) , true ) ;
EndFrame = VFR_Output . GetFrameAtTime ( curline - > End . GetMS ( ) , false ) ;
2006-01-16 22:02:54 +01:00
// Jump to start
PlayNextFrame = StartFrame ;
JumpToFrame ( StartFrame ) ;
// Set other variables
StartTime = clock ( ) ;
PlayTime = StartTime ;
// Start timer
Playback . SetOwner ( this , VIDEO_PLAY_TIMER ) ;
Playback . Start ( 1 ) ;
}
////////
// Stop
void VideoDisplay : : Stop ( ) {
2006-07-02 01:31:15 +02:00
if ( IsPlaying ) {
Playback . Stop ( ) ;
IsPlaying = false ;
audio - > Stop ( ) ;
}
2006-01-16 22:02:54 +01:00
}
//////////////
// Play timer
void VideoDisplay : : OnPlayTimer ( wxTimerEvent & event ) {
// Get time difference
clock_t cur = clock ( ) ;
int dif = ( clock ( ) - StartTime ) * 1000 / CLOCKS_PER_SEC ;
if ( ! dif ) return ;
PlayTime = cur ;
// Find next frame
int startMs = VFR_Output . GetTimeAtFrame ( StartFrame ) ;
int nextFrame = frame_n ;
for ( int i = 0 ; i < 10 ; i + + ) {
if ( nextFrame > = length ) break ;
if ( dif < VFR_Output . GetTimeAtFrame ( nextFrame ) - startMs ) {
break ;
}
nextFrame + + ;
}
// Same frame
if ( nextFrame = = frame_n ) return ;
// End
if ( nextFrame > = length | | ( EndFrame ! = - 1 & & nextFrame > EndFrame ) ) {
Stop ( ) ;
return ;
}
2006-07-01 08:32:11 +02:00
// Next frame is before or over 2 frames ahead, so force audio resync
if ( nextFrame < frame_n | | nextFrame > frame_n + 2 ) audio - > player - > SetCurrentPosition ( audio - > GetSampleAtMS ( VFR_Output . GetTimeAtFrame ( nextFrame ) ) ) ;
2006-01-16 22:02:54 +01:00
// Jump to next frame
PlayNextFrame = nextFrame ;
JumpToFrame ( nextFrame ) ;
2006-02-20 23:24:25 +01:00
// Sync audio
2006-02-25 08:14:39 +01:00
if ( nextFrame % 10 = = 0 ) {
2006-02-20 23:24:25 +01:00
__int64 audPos = audio - > GetSampleAtMS ( VFR_Output . GetTimeAtFrame ( nextFrame ) ) ;
2006-02-25 08:41:18 +01:00
__int64 curPos = audio - > player - > GetCurrentPosition ( ) ;
2006-07-01 08:32:11 +02:00
int delta = int ( audPos - curPos ) ;
if ( delta < 0 ) delta = - delta ;
int maxDelta = audio - > provider - > GetSampleRate ( ) ;
if ( delta > maxDelta ) audio - > player - > SetCurrentPosition ( audPos ) ;
2006-02-20 23:24:25 +01:00
}
2006-01-16 22:02:54 +01:00
}
2006-02-22 07:49:20 +01:00
//////////////////////////////
// Get name of temp work file
wxString VideoDisplay : : GetTempWorkFile ( ) {
if ( tempfile . IsEmpty ( ) ) {
tempfile = wxFileName : : CreateTempFileName ( _T ( " aegisub " ) ) ;
2006-03-31 11:44:48 +02:00
wxRemoveFile ( tempfile ) ;
2006-02-22 07:49:20 +01:00
tempfile + = _T ( " .ass " ) ;
}
return tempfile ;
}
2006-12-18 05:53:40 +01:00
/////////////////
// Get keyframes
wxArrayInt VideoDisplay : : GetKeyFrames ( ) {
if ( OverKeyFramesLoaded ( ) ) return overKeyFrames ;
return KeyFrames ;
}
/////////////////
// Set keyframes
void VideoDisplay : : SetKeyFrames ( wxArrayInt frames ) {
KeyFrames = frames ;
}
/////////////////////////
// Set keyframe override
void VideoDisplay : : SetOverKeyFrames ( wxArrayInt frames ) {
overKeyFrames = frames ;
2006-12-18 17:08:58 +01:00
overKeyFramesLoaded = true ;
2006-12-18 05:53:40 +01:00
}
///////////////////
// Close keyframes
void VideoDisplay : : CloseOverKeyFrames ( ) {
overKeyFrames . Clear ( ) ;
overKeyFramesLoaded = false ;
}
//////////////////////////////////////////
// Check if override keyframes are loaded
bool VideoDisplay : : OverKeyFramesLoaded ( ) {
return overKeyFramesLoaded ;
}
2006-12-18 17:08:58 +01:00
/////////////////////////////////
// Check if keyframes are loaded
bool VideoDisplay : : KeyFramesLoaded ( ) {
return overKeyFramesLoaded | | keyFramesLoaded ;
}