2006-01-22 13:44:53 +01:00
// Copyright (c) 2006, Fredrik Mellbin
// 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-22 13:44:53 +01:00
//
2009-07-29 07:43:02 +02:00
// $Id$
/// @file video_provider_avs.cpp
/// @brief Avisynth-based video provider
/// @ingroup video_input
///
2006-01-22 13:44:53 +01:00
2009-01-04 07:31:48 +01:00
# include "config.h"
2008-01-22 03:54:16 +01:00
# ifdef WITH_AVISYNTH
2009-09-10 15:06:40 +02:00
# ifndef AGI_PRE
2006-01-22 13:44:53 +01:00
# include <wx/filename.h>
# include <wx/msw/registry.h>
2009-09-10 15:06:40 +02:00
# endif
# include "charset_conv.h"
2010-08-02 08:32:01 +02:00
# include "compat.h"
2009-09-10 15:06:40 +02:00
# include "gl_wrap.h"
2010-07-23 22:34:23 +02:00
# include <libaegisub/log.h>
2009-09-10 15:06:40 +02:00
# include "mkv_wrap.h"
2007-06-21 02:46:50 +02:00
# include "standard_paths.h"
2008-07-15 02:08:05 +02:00
# include "vfw_wrap.h"
2009-09-10 15:06:40 +02:00
# include "video_context.h"
# include "video_provider_avs.h"
2006-01-22 13:44:53 +01:00
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
/// @param _filename
///
2010-08-02 08:32:01 +02:00
AvisynthVideoProvider : : AvisynthVideoProvider ( wxString filename ) try
: usedDirectShow ( false )
, decoderName ( _ ( " Unknown " ) )
, num_frames ( 0 )
, last_fnum ( - 1 )
, RGB32Video ( NULL )
{
RGB32Video = OpenVideo ( filename ) ;
2006-01-22 13:44:53 +01:00
2009-05-15 14:44:36 +02:00
vi = RGB32Video - > GetVideoInfo ( ) ;
2006-01-22 13:44:53 +01:00
}
2010-08-02 08:32:01 +02:00
catch ( AvisynthError const & err ) {
throw VideoOpenError ( " Avisynth error: " + std : : string ( err . msg ) ) ;
}
2006-01-22 13:44:53 +01:00
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 Destructor
2006-02-23 04:41:29 +01:00
AvisynthVideoProvider : : ~ AvisynthVideoProvider ( ) {
2007-01-21 07:30:19 +01:00
iframe . Clear ( ) ;
2006-01-22 13:44:53 +01:00
}
2010-08-02 08:32:01 +02:00
AVSValue AvisynthVideoProvider : : Open ( wxFileName const & fname , wxString const & extension ) {
char * videoFilename = env - > SaveString ( fname . GetShortPath ( ) . mb_str ( csConvLocal ) ) ;
2006-01-22 13:44:53 +01:00
2010-08-02 08:32:01 +02:00
// Avisynth file, just import it
if ( extension = = L " .avs " ) {
LOG_I ( " avisynth/video " ) < < " Opening .avs file with Import " ;
decoderName = L " Import " ;
return env - > Invoke ( " Import " , videoFilename ) ;
}
2006-01-22 13:44:53 +01:00
2010-08-02 08:32:01 +02:00
// Open avi file with AviSource
if ( extension = = L " .avi " ) {
LOG_I ( " avisynth/video " ) < < " Opening .avi file with AviSource " ;
try {
const char * argnames [ 2 ] = { 0 , " audio " } ;
AVSValue args [ 2 ] = { videoFilename , false } ;
decoderName = L " AviSource " ;
return env - > Invoke ( " AviSource " , AVSValue ( args , 2 ) , argnames ) ;
2006-12-17 05:58:10 +01:00
}
2006-12-19 18:30:25 +01:00
2010-08-02 08:32:01 +02:00
// On Failure, fallback to DSS
catch ( AvisynthError & ) {
LOG_I ( " avisynth/video " ) < < " Failed to open .avi file with AviSource, switching to DirectShowSource " ;
2006-01-22 13:44:53 +01:00
}
2010-08-02 08:32:01 +02:00
}
// Open d2v with mpeg2dec3
if ( extension = = L " .d2v " & & env - > FunctionExists ( " Mpeg2Dec3_Mpeg2Source " ) ) {
LOG_I ( " avisynth/video " ) < < " Opening .d2v file with Mpeg2Dec3_Mpeg2Source " ;
AVSValue script = env - > Invoke ( " Mpeg2Dec3_Mpeg2Source " , videoFilename ) ;
decoderName = L " Mpeg2Dec3_Mpeg2Source " ;
2006-12-19 18:30:25 +01:00
2010-08-02 08:32:01 +02:00
//if avisynth is 2.5.7 beta 2 or newer old mpeg2decs will crash without this
if ( env - > FunctionExists ( " SetPlanarLegacyAlignment " ) ) {
AVSValue args [ 2 ] = { script , true } ;
script = env - > Invoke ( " SetPlanarLegacyAlignment " , AVSValue ( args , 2 ) ) ;
2007-01-11 20:49:37 +01:00
}
2010-08-02 08:32:01 +02:00
return script ;
}
2007-01-11 20:49:37 +01:00
2010-08-02 08:32:01 +02:00
// If that fails, try opening it with DGDecode
if ( extension = = L " .d2v " & & env - > FunctionExists ( " DGDecode_Mpeg2Source " ) ) {
LOG_I ( " avisynth/video " ) < < " Opening .d2v file with DGDecode_Mpeg2Source " ;
decoderName = L " DGDecode_Mpeg2Source " ;
return env - > Invoke ( " Mpeg2Source " , videoFilename ) ;
2007-01-11 20:49:37 +01:00
2010-08-02 08:32:01 +02:00
//note that DGDecode will also have issues like if the version is too ancient but no sane person
//would use that anyway
}
2006-12-19 18:30:25 +01:00
2010-08-02 08:32:01 +02:00
if ( extension = = L " .d2v " & & env - > FunctionExists ( " Mpeg2Source " ) ) {
LOG_I ( " avisynth/video " ) < < " Opening .d2v file with other Mpeg2Source " ;
AVSValue script = env - > Invoke ( " Mpeg2Source " , videoFilename ) ;
decoderName = L " Mpeg2Source " ;
2007-01-11 20:49:37 +01:00
2010-08-02 08:32:01 +02:00
//if avisynth is 2.5.7 beta 2 or newer old mpeg2decs will crash without this
if ( env - > FunctionExists ( " SetPlanarLegacyAlignment " ) )
script = env - > Invoke ( " SetPlanarLegacyAlignment " , script ) ;
return script ;
}
2006-12-19 18:30:25 +01:00
2010-08-02 08:32:01 +02:00
// Try loading DirectShowSource2
if ( ! env - > FunctionExists ( " dss2 " ) ) {
wxFileName dss2path ( StandardPaths : : DecodePath ( _T ( " ?data/avss.dll " ) ) ) ;
if ( dss2path . FileExists ( ) ) {
env - > Invoke ( " LoadPlugin " , env - > SaveString ( dss2path . GetFullPath ( ) . mb_str ( csConvLocal ) ) ) ;
2006-12-19 18:30:25 +01:00
}
}
2010-08-02 08:32:01 +02:00
// If DSS2 loaded properly, try using it
if ( env - > FunctionExists ( " dss2 " ) ) {
LOG_I ( " avisynth/video " ) < < " Opening file with DSS2 " ;
decoderName = L " DSS2 " ;
return env - > Invoke ( " DSS2 " , videoFilename ) ;
}
// Try DirectShowSource
// Load DirectShowSource.dll from app dir if it exists
wxFileName dsspath ( StandardPaths : : DecodePath ( _T ( " ?data/DirectShowSource.dll " ) ) ) ;
if ( dsspath . FileExists ( ) ) {
env - > Invoke ( " LoadPlugin " , env - > SaveString ( dsspath . GetFullPath ( ) . mb_str ( csConvLocal ) ) ) ;
}
// Then try using DSS
if ( env - > FunctionExists ( " DirectShowSource " ) ) {
const char * argnames [ 3 ] = { 0 , " video " , " audio " } ;
AVSValue args [ 3 ] = { videoFilename , true , false } ;
usedDirectShow = true ;
decoderName = L " DirectShowSource " ;
LOG_I ( " avisynth/video " ) < < " Opening file with DirectShowSource " ;
return env - > Invoke ( " DirectShowSource " , AVSValue ( args , 3 ) , argnames ) ;
}
// Failed to find a suitable function
LOG_E ( " avisynth/video " ) < < " DSS function not found " ;
throw VideoNotSupported ( " No function suitable for opening the video found " ) ;
}
/// @brief Actually open the video into Avisynth
/// @param _filename
/// @return
///
PClip AvisynthVideoProvider : : OpenVideo ( wxString filename ) {
wxMutexLocker lock ( AviSynthMutex ) ;
wxFileName fname ( filename ) ;
if ( ! fname . FileExists ( ) )
throw agi : : FileNotFoundError ( STD_STR ( filename ) ) ;
AVSValue script ;
wxString extension = filename . Right ( 4 ) . Lower ( ) ;
try {
script = Open ( fname , extension ) ;
}
catch ( AvisynthError const & err ) {
throw VideoOpenError ( " Avisynth error: " + std : : string ( err . msg ) ) ;
2006-01-22 13:44:53 +01:00
}
2006-12-19 18:30:25 +01:00
// Check if video was loaded properly
2008-03-06 22:38:40 +01:00
if ( ! script . IsClip ( ) | | ! script . AsClip ( ) - > GetVideoInfo ( ) . HasVideo ( ) ) {
2010-08-02 08:32:01 +02:00
throw VideoNotSupported ( " No usable video found " ) ;
2006-12-17 05:58:10 +01:00
}
2006-01-22 13:44:53 +01:00
2008-07-15 02:08:05 +02:00
// Read keyframes and timecodes from MKV file
bool mkvOpen = MatroskaWrapper : : wrapper . IsOpen ( ) ;
2010-07-08 06:29:04 +02:00
KeyFrames . clear ( ) ;
2010-08-02 08:32:01 +02:00
if ( extension = = L " .mkv " | | mkvOpen ) {
2008-07-15 02:08:05 +02:00
// Parse mkv
2010-08-02 08:32:01 +02:00
if ( ! mkvOpen ) MatroskaWrapper : : wrapper . Open ( filename ) ;
2008-07-15 02:08:05 +02:00
// Get keyframes
KeyFrames = MatroskaWrapper : : wrapper . GetKeyFrames ( ) ;
2010-07-08 06:29:04 +02:00
MatroskaWrapper : : wrapper . SetToTimecodes ( vfr_fps ) ;
2008-07-15 02:08:05 +02:00
// Close mkv
MatroskaWrapper : : wrapper . Close ( ) ;
}
// check if we have windows, if so we can load keyframes from AVI files using VFW
# ifdef __WINDOWS__
2010-08-02 08:32:01 +02:00
else if ( extension = = L " .avi " ) {
2010-07-08 06:29:04 +02:00
KeyFrames . clear ( ) ;
2010-08-02 08:32:01 +02:00
KeyFrames = VFWWrapper : : GetKeyFrames ( filename ) ;
2008-07-15 02:08:05 +02:00
}
# endif /* __WINDOWS__ */
// Check if the file is all keyframes
bool isAllKeyFrames = true ;
2010-07-08 06:29:04 +02:00
for ( unsigned int i = 1 ; i < KeyFrames . size ( ) ; i + + ) {
2008-07-15 02:08:05 +02:00
// Is the last keyframe not this keyframe -1?
if ( KeyFrames [ i - 1 ] ! = ( int ) ( i - 1 ) ) {
// It's not all keyframes, go ahead
isAllKeyFrames = false ;
break ;
}
}
// If it is all keyframes, discard the keyframe info as it is useless
if ( isAllKeyFrames ) {
2010-07-08 06:29:04 +02:00
KeyFrames . clear ( ) ;
2008-07-15 02:08:05 +02:00
}
2010-07-08 06:29:04 +02:00
real_fps = ( double ) vi . fps_numerator / vi . fps_denominator ;
2006-01-22 13:44:53 +01:00
// Convert to RGB32
2009-07-20 03:32:01 +02:00
script = env - > Invoke ( " ConvertToRGB32 " , script ) ;
2006-01-22 13:44:53 +01:00
// Cache
2006-02-22 20:47:44 +01:00
return ( env - > Invoke ( " Cache " , script ) ) . AsClip ( ) ;
2006-01-22 13:44:53 +01:00
}
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 Actually get a frame
/// @param _n
/// @return
///
2010-07-08 06:29:04 +02:00
const AegiVideoFrame AvisynthVideoProvider : : GetFrame ( int n ) {
if ( vfr_fps . IsLoaded ( ) ) {
n = real_fps . FrameAtTime ( vfr_fps . TimeAtFrame ( n ) ) ;
2007-01-21 07:30:19 +01:00
}
// Get avs frame
wxMutexLocker lock ( AviSynthMutex ) ;
2009-05-15 14:44:36 +02:00
PVideoFrame frame = RGB32Video - > GetFrame ( n , env ) ;
2007-01-21 07:30:19 +01:00
int Bpp = vi . BitsPerPixel ( ) / 8 ;
// Aegisub's video frame
AegiVideoFrame & final = iframe ;
2009-07-20 05:50:25 +02:00
final . format = FORMAT_RGB32 ;
final . flipped = true ;
final . invertChannels = true ;
2007-01-21 07:30:19 +01:00
// Set size properties
final . pitch [ 0 ] = frame - > GetPitch ( ) ;
final . w = frame - > GetRowSize ( ) / Bpp ;
final . h = frame - > GetHeight ( ) ;
// Allocate
final . Allocate ( ) ;
// Copy
memcpy ( final . data [ 0 ] , frame - > GetReadPtr ( ) , final . pitch [ 0 ] * final . h ) ;
// Set last number
last_fnum = n ;
return final ;
}
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 Get warning
///
2010-07-08 06:29:04 +02:00
wxString AvisynthVideoProvider : : GetWarning ( ) const {
2008-03-07 22:24:14 +01:00
if ( usedDirectShow ) return L " Warning! The file is being opened using Avisynth's DirectShowSource, which has unreliable seeking. Frame numbers might not match the real number. PROCEED AT YOUR OWN RISK! " ;
else return L " " ;
2007-07-29 11:06:38 +02:00
}
2007-01-23 05:42:08 +01:00
# endif