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
2011-08-27 08:30:03 +02:00
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
2011-08-27 08:30:03 +02:00
# ifdef _WIN32
# include <vfw.h>
# endif
2009-09-10 15:06:40 +02:00
# include "charset_conv.h"
2010-08-02 08:32:01 +02:00
# include "compat.h"
2010-07-23 22:34:23 +02:00
# include <libaegisub/log.h>
2007-06-21 02:46:50 +02:00
# include "standard_paths.h"
2009-09-10 15:06:40 +02:00
# include "video_provider_avs.h"
2006-01-22 13:44:53 +01:00
2010-08-02 08:32:01 +02:00
AvisynthVideoProvider : : AvisynthVideoProvider ( wxString filename ) try
2011-08-27 08:30:03 +02:00
: last_fnum ( - 1 )
2010-08-02 08:32:01 +02:00
{
2011-08-27 08:30:03 +02:00
iframe . flipped = true ;
iframe . invertChannels = true ;
wxMutexLocker lock ( AviSynthMutex ) ;
wxFileName fname ( filename ) ;
if ( ! fname . FileExists ( ) )
throw agi : : FileNotFoundError ( STD_STR ( filename ) ) ;
wxString extension = filename . Right ( 4 ) . Lower ( ) ;
# ifdef _WIN32
if ( extension = = " .avi " ) {
// Try to read the keyframes before actually opening the file as trying
// to open the file while it's already open can cause problems with
// badly written VFW decoders
AVIFileInit ( ) ;
PAVIFILE pfile ;
long hr = AVIFileOpen ( & pfile , filename . wc_str ( ) , OF_SHARE_DENY_WRITE , 0 ) ;
if ( hr ) {
warning = " Unable to open AVI file for reading keyframes: \n " ;
switch ( hr ) {
case AVIERR_BADFORMAT :
warning + = " The file is corrupted, incomplete or has an otherwise bad format. " ;
break ;
case AVIERR_MEMORY :
warning + = " The file could not be opened because of insufficient memory. " ;
break ;
case AVIERR_FILEREAD :
warning + = " An error occurred reading the file. There might be a problem with the storage media. " ;
break ;
case AVIERR_FILEOPEN :
warning + = " The file could not be opened. It might be in use by another application, or you do not have permission to access it. " ;
break ;
case REGDB_E_CLASSNOTREG :
warning + = " There is no handler installed for the file extension. This might indicate a fundameltal problem in your Video for Windows installation, and can be caused by extremely stripped Windows installations. " ;
break ;
default :
warning + = " Unknown error. " ;
break ;
}
goto file_exit ;
}
PAVISTREAM ppavi ;
if ( hr = AVIFileGetStream ( pfile , & ppavi , streamtypeVIDEO , 0 ) ) {
warning = " Unable to open AVI video stream for reading keyframes: \n " ;
switch ( hr ) {
case AVIERR_NODATA :
warning + = " The file does not contain a usable video stream. " ;
break ;
case AVIERR_MEMORY :
warning + = " Not enough memory. " ;
break ;
default :
warning + = " Unknown error. " ;
break ;
}
goto file_release ;
}
AVISTREAMINFO avis ;
if ( AVIStreamInfo ( ppavi , & avis , sizeof ( avis ) ) ) {
warning = " Unable to read keyframes from AVI file: \n Could not get stream information. " ;
goto stream_release ;
}
bool all_keyframes = true ;
for ( size_t i = 0 ; i < avis . dwLength ; i + + ) {
if ( AVIStreamIsKeyFrame ( ppavi , i ) )
KeyFrames . push_back ( i ) ;
else
all_keyframes = false ;
}
2006-01-22 13:44:53 +01:00
2011-08-27 08:30:03 +02:00
// If every frame is a keyframe then just discard the keyframe data as it's useless
if ( all_keyframes ) KeyFrames . clear ( ) ;
// Clean up
stream_release :
AVIStreamRelease ( ppavi ) ;
file_release :
AVIFileRelease ( pfile ) ;
file_exit :
AVIFileExit ( ) ;
}
# endif
AVSValue script = Open ( fname , extension ) ;
// Check if video was loaded properly
if ( ! script . IsClip ( ) | | ! script . AsClip ( ) - > GetVideoInfo ( ) . HasVideo ( ) )
throw VideoNotSupported ( " No usable video found " ) ;
RGB32Video = ( env - > Invoke ( " Cache " , env - > Invoke ( " ConvertToRGB32 " , script ) ) ) . AsClip ( ) ;
2009-05-15 14:44:36 +02:00
vi = RGB32Video - > GetVideoInfo ( ) ;
2011-08-27 08:30:03 +02:00
fps = ( double ) vi . fps_numerator / vi . fps_denominator ;
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
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
2011-08-27 08:30:03 +02:00
if ( extension = = " .avs " ) {
2010-08-02 08:32:01 +02:00
LOG_I ( " avisynth/video " ) < < " Opening .avs file with Import " ;
2011-08-27 08:30:03 +02:00
decoderName = " Avisynth/Import " ;
2010-08-02 08:32:01 +02:00
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
2011-08-27 08:30:03 +02:00
if ( extension = = " .avi " ) {
2010-08-02 08:32:01 +02:00
LOG_I ( " avisynth/video " ) < < " Opening .avi file with AviSource " ;
try {
const char * argnames [ 2 ] = { 0 , " audio " } ;
AVSValue args [ 2 ] = { videoFilename , false } ;
2011-08-27 08:30:03 +02:00
decoderName = " Avisynth/AviSource " ;
2010-08-02 08:32:01 +02:00
return env - > Invoke ( " AviSource " , AVSValue ( args , 2 ) , argnames ) ;
2006-12-17 05:58:10 +01:00
}
2010-08-02 08:32:01 +02:00
// On Failure, fallback to DSS
2011-08-27 08:30:03 +02:00
catch ( AvisynthError & err ) {
LOG_E ( " avisynth/video " ) < < err . msg ;
LOG_I ( " avisynth/video " ) < < " Failed to open .avi file with AviSource, trying DirectShowSource " ;
2006-01-22 13:44:53 +01:00
}
2010-08-02 08:32:01 +02:00
}
// Open d2v with mpeg2dec3
2011-08-27 08:30:03 +02:00
if ( extension = = " .d2v " & & env - > FunctionExists ( " Mpeg2Dec3_Mpeg2Source " ) ) {
2010-08-02 08:32:01 +02:00
LOG_I ( " avisynth/video " ) < < " Opening .d2v file with Mpeg2Dec3_Mpeg2Source " ;
AVSValue script = env - > Invoke ( " Mpeg2Dec3_Mpeg2Source " , videoFilename ) ;
2011-08-27 08:30:03 +02:00
decoderName = " Avisynth/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
2011-08-27 08:30:03 +02:00
if ( extension = = " .d2v " & & env - > FunctionExists ( " DGDecode_Mpeg2Source " ) ) {
2010-08-02 08:32:01 +02:00
LOG_I ( " avisynth/video " ) < < " Opening .d2v file with DGDecode_Mpeg2Source " ;
2011-08-27 08:30:03 +02:00
decoderName = " DGDecode_Mpeg2Source " ;
return env - > Invoke ( " Avisynth/Mpeg2Source " , videoFilename ) ;
2007-01-11 20:49:37 +01:00
2011-08-27 08:30:03 +02:00
//note that DGDecode will also have issues like if the version is too
// ancient but no sane person would use that anyway
2010-08-02 08:32:01 +02:00
}
2006-12-19 18:30:25 +01:00
2011-08-27 08:30:03 +02:00
if ( extension = = " .d2v " & & env - > FunctionExists ( " Mpeg2Source " ) ) {
2010-08-02 08:32:01 +02:00
LOG_I ( " avisynth/video " ) < < " Opening .d2v file with other Mpeg2Source " ;
AVSValue script = env - > Invoke ( " Mpeg2Source " , videoFilename ) ;
2011-08-27 08:30:03 +02:00
decoderName = " Avisynth/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 " ) ) {
2011-08-27 08:30:03 +02:00
wxFileName dss2path ( StandardPaths : : DecodePath ( " ?data/avss.dll " ) ) ;
2010-08-02 08:32:01 +02:00
if ( dss2path . FileExists ( ) ) {
2011-08-27 08:30:03 +02:00
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 " ;
2011-08-27 08:30:03 +02:00
decoderName = " Avisynth/DSS2 " ;
2010-08-02 08:32:01 +02:00
return env - > Invoke ( " DSS2 " , videoFilename ) ;
}
// Try DirectShowSource
// Load DirectShowSource.dll from app dir if it exists
2011-08-27 08:30:03 +02:00
wxFileName dsspath ( StandardPaths : : DecodePath ( " ?data/DirectShowSource.dll " ) ) ;
2010-08-02 08:32:01 +02:00
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 } ;
2011-08-27 08:30:03 +02:00
decoderName = " Avisynth/DirectShowSource " ;
warning = " 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! " ;
2010-08-02 08:32:01 +02:00
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 " ) ;
}
2010-07-08 06:29:04 +02:00
const AegiVideoFrame AvisynthVideoProvider : : GetFrame ( int n ) {
2011-08-27 08:30:03 +02:00
if ( n = = last_fnum ) return iframe ;
2007-01-21 07:30:19 +01:00
2011-08-27 08:30:03 +02:00
wxMutexLocker lock ( AviSynthMutex ) ;
2007-01-21 07:30:19 +01:00
2011-08-27 08:30:03 +02:00
PVideoFrame frame = RGB32Video - > GetFrame ( n , env ) ;
iframe . pitch = frame - > GetPitch ( ) ;
iframe . w = frame - > GetRowSize ( ) / ( vi . BitsPerPixel ( ) / 8 ) ;
iframe . h = frame - > GetHeight ( ) ;
2007-01-21 07:30:19 +01:00
2011-08-27 08:30:03 +02:00
iframe . Allocate ( ) ;
2007-01-21 07:30:19 +01:00
2011-08-27 08:30:03 +02:00
memcpy ( iframe . data , frame - > GetReadPtr ( ) , iframe . pitch * iframe . h ) ;
2007-01-21 07:30:19 +01:00
last_fnum = n ;
2011-08-27 08:30:03 +02:00
return iframe ;
2007-07-29 11:06:38 +02:00
}
2011-08-27 08:30:03 +02:00
# endif // HAVE_AVISYNTH