2006-01-16 22:02:54 +01:00
// Copyright (c) 2005, 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/
2006-01-16 22:02:54 +01:00
//
2009-07-29 07:43:02 +02:00
// $Id$
/// @file export_framerate.cpp
/// @brief Transform Framerate export filter
/// @ingroup export
///
2006-01-16 22:02:54 +01:00
2009-01-04 07:31:48 +01:00
# include "config.h"
2010-06-24 03:24:26 +02:00
# ifndef AGI_PRE
# include <utility>
# include <wx/button.h>
# include <wx/checkbox.h>
# include <wx/panel.h>
# include <wx/radiobut.h>
# include <wx/sizer.h>
# include <wx/stattext.h>
# include <wx/textctrl.h>
# endif
2006-01-16 22:02:54 +01:00
# include "ass_dialogue.h"
2009-09-10 15:06:40 +02:00
# include "ass_file.h"
2006-01-16 22:02:54 +01:00
# include "ass_override.h"
2009-09-10 15:06:40 +02:00
# include "export_framerate.h"
2011-09-28 21:46:53 +02:00
# include "include/aegisub/context.h"
2010-06-24 03:24:26 +02:00
# include "utils.h"
2010-07-08 06:29:04 +02:00
# include "video_context.h"
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
2011-01-21 07:09:04 +01:00
AssTransformFramerateFilter : : AssTransformFramerateFilter ( )
: AssExportFilter ( _ ( " Transform Framerate " ) , _ ( " Transform subtitle times, including those in override tags, from an input framerate to an output framerate. \n \n This is useful for converting regular time subtitles to VFRaC time subtitles for hardsubbing. \n It can also be used to convert subtitles to a different speed video, such as NTSC to PAL speedup. " ) , 1000 )
2011-09-28 21:46:53 +02:00
, c ( 0 )
2012-10-15 06:37:14 +02:00
, line ( 0 )
, newStart ( 0 )
, newEnd ( 0 )
, newK ( 0 )
, oldK ( 0 )
2011-01-21 07:09:04 +01:00
, Input ( 0 )
, Output ( 0 )
{
2006-01-16 22:02:54 +01:00
}
2011-09-28 21:46:41 +02:00
void AssTransformFramerateFilter : : ProcessSubs ( AssFile * subs , wxWindow * ) {
2010-06-24 03:24:26 +02:00
TransformFrameRate ( subs ) ;
2006-01-16 22:02:54 +01:00
}
2011-09-28 21:46:53 +02:00
wxWindow * AssTransformFramerateFilter : : GetConfigDialogWindow ( wxWindow * parent , agi : : Context * c ) {
2006-01-16 22:02:54 +01:00
wxWindow * base = new wxPanel ( parent , - 1 ) ;
2011-09-28 21:46:53 +02:00
LoadSettings ( true , c ) ;
2010-07-08 06:29:04 +02:00
2006-01-16 22:02:54 +01:00
// Input sizer
wxSizer * InputSizer = new wxBoxSizer ( wxHORIZONTAL ) ;
wxString initialInput ;
2011-11-18 19:49:09 +01:00
wxButton * FromVideo = new wxButton ( base , - 1 , _ ( " From &video " ) ) ;
2011-01-21 07:09:28 +01:00
if ( Input - > IsLoaded ( ) ) {
2011-01-21 07:09:04 +01:00
initialInput = wxString : : Format ( " %2.3f " , Input - > FPS ( ) ) ;
2011-01-21 07:09:28 +01:00
FromVideo - > Bind ( wxEVT_COMMAND_BUTTON_CLICKED , & AssTransformFramerateFilter : : OnFpsFromVideo , this ) ;
}
2006-01-16 22:02:54 +01:00
else {
2011-01-21 07:09:04 +01:00
initialInput = " 23.976 " ;
2006-01-16 22:02:54 +01:00
FromVideo - > Enable ( false ) ;
}
InputFramerate = new wxTextCtrl ( base , - 1 , initialInput , wxDefaultPosition , wxSize ( 60 , 20 ) ) ;
InputSizer - > Add ( InputFramerate , 0 , wxEXPAND | wxLEFT , 5 ) ;
InputSizer - > Add ( FromVideo , 0 , wxEXPAND | wxLEFT , 5 ) ;
InputSizer - > AddStretchSpacer ( 1 ) ;
// Output sizers
wxSizer * OutputSizerTop = new wxBoxSizer ( wxHORIZONTAL ) ;
wxSizer * OutputSizerBottom = new wxBoxSizer ( wxHORIZONTAL ) ;
wxSizer * OutputSizer = new wxBoxSizer ( wxVERTICAL ) ;
// Output top line
2011-11-18 19:49:09 +01:00
RadioOutputVFR = new wxRadioButton ( base , - 1 , _ ( " V&ariable " ) , wxDefaultPosition , wxDefaultSize , wxRB_GROUP ) ;
2006-01-16 22:02:54 +01:00
OutputSizerTop - > Add ( RadioOutputVFR , 0 , wxEXPAND , 0 ) ;
// Output bottom line
2011-11-18 19:49:09 +01:00
RadioOutputCFR = new wxRadioButton ( base , - 1 , _ ( " &Constant: " ) ) ;
2006-01-16 22:02:54 +01:00
wxString initialOutput = initialInput ;
2010-07-08 06:29:04 +02:00
if ( ! Output - > IsVFR ( ) ) {
2006-01-16 22:02:54 +01:00
RadioOutputVFR - > Enable ( false ) ;
RadioOutputCFR - > SetValue ( true ) ;
}
OutputFramerate = new wxTextCtrl ( base , - 1 , initialOutput , wxDefaultPosition , wxSize ( 60 , 20 ) ) ;
OutputSizerBottom - > Add ( RadioOutputCFR , 0 , wxEXPAND , 0 ) ;
OutputSizerBottom - > Add ( OutputFramerate , 0 , wxEXPAND | wxLEFT , 5 ) ;
OutputSizerBottom - > AddStretchSpacer ( 1 ) ;
2008-03-09 06:32:02 +01:00
// Reverse checkbox
2011-11-18 19:49:09 +01:00
Reverse = new wxCheckBox ( base , - 1 , _ ( " &Reverse transformation " ) ) ;
2008-03-09 06:32:02 +01:00
2006-01-16 22:02:54 +01:00
// Output final
OutputSizer - > Add ( OutputSizerTop , 0 , wxLEFT , 5 ) ;
OutputSizer - > Add ( OutputSizerBottom , 0 , wxLEFT , 5 ) ;
// Main window
2009-07-24 04:18:11 +02:00
wxSizer * MainSizer = new wxFlexGridSizer ( 3 , 2 , 5 , 10 ) ;
2006-01-16 22:02:54 +01:00
MainSizer - > Add ( new wxStaticText ( base , - 1 , _ ( " Input framerate: " ) ) , 0 , wxEXPAND | wxALIGN_CENTER_VERTICAL , 0 ) ;
MainSizer - > Add ( InputSizer , 0 , wxEXPAND , 0 ) ;
MainSizer - > Add ( new wxStaticText ( base , - 1 , _ ( " Output: " ) ) , 0 , wxALIGN_CENTER_VERTICAL , 0 ) ;
MainSizer - > Add ( OutputSizer , 0 , wxEXPAND , 0 ) ;
2008-03-09 06:32:02 +01:00
MainSizer - > Add ( Reverse , 0 , wxTOP | wxEXPAND , 5 ) ;
2006-01-16 22:02:54 +01:00
// Window
2007-09-24 19:40:03 +02:00
base - > SetSizerAndFit ( MainSizer ) ;
2006-01-16 22:02:54 +01:00
return base ;
}
2011-01-21 07:09:28 +01:00
void AssTransformFramerateFilter : : OnFpsFromVideo ( wxCommandEvent & ) {
2011-09-28 21:46:53 +02:00
InputFramerate - > SetValue ( wxString : : Format ( " %g " , c - > videoController - > FPS ( ) . FPS ( ) ) ) ;
2011-01-21 07:09:28 +01:00
}
2011-09-28 21:46:53 +02:00
void AssTransformFramerateFilter : : LoadSettings ( bool is_default , agi : : Context * c ) {
this - > c = c ;
if ( is_default ) {
2012-10-09 17:04:38 +02:00
Input = & c - > videoController - > VideoFPS ( ) ;
Output = & c - > videoController - > FPS ( ) ;
2006-01-16 22:02:54 +01:00
}
else {
double temp ;
InputFramerate - > GetValue ( ) . ToDouble ( & temp ) ;
2010-07-08 06:29:04 +02:00
t1 = temp ;
2006-01-16 22:02:54 +01:00
Input = & t1 ;
if ( RadioOutputCFR - > GetValue ( ) ) {
OutputFramerate - > GetValue ( ) . ToDouble ( & temp ) ;
2010-07-08 06:29:04 +02:00
t2 = temp ;
2006-01-16 22:02:54 +01:00
Output = & t2 ;
}
2012-10-09 17:04:38 +02:00
else Output = & c - > videoController - > FPS ( ) ;
2008-03-09 06:32:02 +01:00
if ( Reverse - > IsChecked ( ) ) {
2010-06-24 03:24:26 +02:00
std : : swap ( Input , Output ) ;
2008-03-09 06:32:02 +01:00
}
2006-01-16 22:02:54 +01:00
}
}
2010-06-24 03:24:26 +02:00
/// Truncate a time to centisecond precision
int FORCEINLINE trunc_cs ( int time ) {
return ( time / 10 ) * 10 ;
}
2006-01-16 22:02:54 +01:00
2011-01-21 07:09:04 +01:00
void AssTransformFramerateFilter : : TransformTimeTags ( wxString name , int n , AssOverrideParameter * curParam , void * curData ) {
2008-03-06 20:40:24 +01:00
VariableDataType type = curParam - > GetType ( ) ;
if ( type ! = VARDATA_INT & & type ! = VARDATA_FLOAT ) return ;
2011-01-21 07:09:04 +01:00
AssTransformFramerateFilter * instance = static_cast < AssTransformFramerateFilter * > ( curData ) ;
AssDialogue * curDiag = instance - > line ;
2010-06-24 03:24:26 +02:00
int parVal = curParam - > Get < int > ( ) ;
2006-01-16 22:02:54 +01:00
switch ( curParam - > classification ) {
2010-06-24 03:24:26 +02:00
case PARCLASS_RELATIVE_TIME_START : {
2011-12-22 22:28:51 +01:00
int value = instance - > ConvertTime ( trunc_cs ( curDiag - > Start ) + parVal ) - instance - > newStart ;
2010-06-24 03:24:26 +02:00
// An end time of 0 is actually the end time of the line, so ensure
// nonzero is never converted to 0
// Needed here rather than the end case because start/end here mean
// which end of the line the time is relative to, not whether it's
// the start or end time (compare \move and \fad)
if ( value = = 0 & & parVal ! = 0 ) value = 1 ;
curParam - > Set ( value ) ;
2006-01-16 22:02:54 +01:00
break ;
2010-06-24 03:24:26 +02:00
}
2006-01-16 22:02:54 +01:00
case PARCLASS_RELATIVE_TIME_END :
2011-12-22 22:28:51 +01:00
curParam - > Set ( instance - > newEnd - instance - > ConvertTime ( trunc_cs ( curDiag - > End ) - parVal ) ) ;
2006-01-16 22:02:54 +01:00
break ;
2010-06-24 03:24:26 +02:00
case PARCLASS_KARAOKE : {
2011-12-22 22:28:51 +01:00
int start = curDiag - > Start / 10 + instance - > oldK + parVal ;
2011-01-21 07:09:04 +01:00
int value = ( instance - > ConvertTime ( start * 10 ) - instance - > newStart ) / 10 - instance - > newK ;
instance - > oldK + = parVal ;
instance - > newK + = value ;
2010-06-24 03:24:26 +02:00
curParam - > Set ( value ) ;
2006-01-16 22:02:54 +01:00
break ;
2010-06-24 03:24:26 +02:00
}
2006-01-16 22:02:54 +01:00
default :
return ;
}
}
void AssTransformFramerateFilter : : TransformFrameRate ( AssFile * subs ) {
2010-07-08 06:29:04 +02:00
if ( ! Input - > IsLoaded ( ) | | ! Output - > IsLoaded ( ) ) return ;
2006-01-16 22:02:54 +01:00
for ( entryIter cur = subs - > Line . begin ( ) ; cur ! = subs - > Line . end ( ) ; cur + + ) {
2010-06-24 03:24:26 +02:00
AssDialogue * curDialogue = dynamic_cast < AssDialogue * > ( * cur ) ;
2006-01-16 22:02:54 +01:00
if ( curDialogue ) {
2011-01-21 07:09:04 +01:00
line = curDialogue ;
newK = 0 ;
oldK = 0 ;
2011-12-22 22:28:51 +01:00
newStart = trunc_cs ( ConvertTime ( curDialogue - > Start ) ) ;
newEnd = trunc_cs ( ConvertTime ( curDialogue - > End ) + 9 ) ;
2006-01-16 22:02:54 +01:00
// Process stuff
2012-10-12 03:52:36 +02:00
curDialogue - > ParseAssTags ( ) ;
2011-01-21 07:09:04 +01:00
curDialogue - > ProcessParameters ( TransformTimeTags , this ) ;
2011-12-22 22:28:51 +01:00
curDialogue - > Start = newStart ;
curDialogue - > End = newEnd ;
2006-01-16 22:02:54 +01:00
curDialogue - > UpdateText ( ) ;
2006-02-21 04:13:35 +01:00
curDialogue - > ClearBlocks ( ) ;
2006-01-16 22:02:54 +01:00
}
}
}
2010-06-24 03:24:26 +02:00
int AssTransformFramerateFilter : : ConvertTime ( int time ) {
2010-07-08 06:29:04 +02:00
int frame = Output - > FrameAtTime ( time ) ;
int frameStart = Output - > TimeAtFrame ( frame ) ;
int frameEnd = Output - > TimeAtFrame ( frame + 1 ) ;
2010-06-24 03:24:26 +02:00
int frameDur = frameEnd - frameStart ;
double dist = double ( time - frameStart ) / frameDur ;
2006-01-16 22:02:54 +01:00
2010-07-08 06:29:04 +02:00
int newStart = Input - > TimeAtFrame ( frame ) ;
int newEnd = Input - > TimeAtFrame ( frame + 1 ) ;
2010-06-24 03:24:26 +02:00
int newDur = newEnd - newStart ;
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
2010-06-24 03:24:26 +02:00
return newStart + newDur * dist ;
}