Skip to content

Instantly share code, notes, and snippets.

@ItEndsWithTens
Last active December 22, 2015 02:19
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save ItEndsWithTens/6402453 to your computer and use it in GitHub Desktop.
Save ItEndsWithTens/6402453 to your computer and use it in GitHub Desktop.
My initial attempt at an Avisynth plugin that supports both Avisynth 2.5 and 2.6 in one binary. Based on the InvertNeg sample at http://avisynth.nl/index.php/Filter_SDK/InvertNeg See http://forum.doom9.org/showthread.php?p=1641233#post1641233 and the following posts for more discussion of the topic. This seems to work from what testing I've done…
// Avisynth v2.5. Copyright 2002, 2005 Ben Rudiak-Gould et al.
// Avisynth v2.6. Copyright 2006 Klaus Post.
// Avisynth v2.6. Copyright 2009 Ian Brabham.
// http://www.avisynth.org
// This program is free software; you can redistribute it and/or modify
// it under the terms of the GNU General Public License as published by
// the Free Software Foundation; either version 2 of the License, or
// (at your option) any later version.
//
// This program is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
// GNU General Public License for more details.
//
// You should have received a copy of the GNU General Public License
// along with this program; if not, write to the Free Software
// Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA, or visit
// http://www.gnu.org/copyleft/gpl.html .
//
// Linking Avisynth statically or dynamically with other modules is making a
// combined work based on Avisynth. Thus, the terms and conditions of the GNU
// General Public License cover the whole combination.
/*
Please NOTE! This version of avisynth.h DOES NOT have any special exemption!
While this version is under development you are fully
constrained by the terms of the GNU General Public License.
Any derivative software you may publish MUST include the full source code.
Normal licence conditions will be reapplied in a future version.
*/
#ifndef __AVISYNTH_H__
#define __AVISYNTH_H__
enum { AVISYNTH_INTERFACE_VERSION = 5 };
/* Define all types necessary for interfacing with avisynth.dll
Moved from internal.h */
// Win32 API macros, notably the types BYTE, DWORD, ULONG, etc.
#include <windef.h>
// Raster types used by VirtualDub & Avisynth
#define in64 (__int64)(unsigned short)
typedef unsigned long Pixel; // this will break on 64-bit machines!
typedef unsigned long Pixel32;
typedef unsigned char Pixel8;
typedef long PixCoord;
typedef long PixDim;
typedef long PixOffset;
/* Compiler-specific crap */
// Tell MSVC to stop precompiling here
#ifdef _MSC_VER
#pragma hdrstop
#endif
// Set up debugging macros for MS compilers; for others, step down to the
// standard <assert.h> interface
#ifdef _MSC_VER
#include <crtdbg.h>
#else
#define _RPT0(a,b) ((void)0)
#define _RPT1(a,b,c) ((void)0)
#define _RPT2(a,b,c,d) ((void)0)
#define _RPT3(a,b,c,d,e) ((void)0)
#define _RPT4(a,b,c,d,e,f) ((void)0)
#define _ASSERTE(x) assert(x)
#include <assert.h>
#endif
// I had problems with Premiere wanting 1-byte alignment for its structures,
// so I now set the Avisynth struct alignment explicitly here.
#pragma pack(push,8)
#define FRAME_ALIGN 16
// Default frame alignment is 16 bytes, to help P4, when using SSE2
// The VideoInfo struct holds global information about a clip (i.e.
// information that does not depend on the frame number). The GetVideoInfo
// method in IClip returns this struct.
// Audio Sample information
typedef float SFLOAT;
enum {SAMPLE_INT8 = 1<<0,
SAMPLE_INT16 = 1<<1,
SAMPLE_INT24 = 1<<2, // Int24 is a very stupid thing to code, but it's supported by some hardware.
SAMPLE_INT32 = 1<<3,
SAMPLE_FLOAT = 1<<4};
enum {
PLANAR_Y=1<<0,
PLANAR_U=1<<1,
PLANAR_V=1<<2,
PLANAR_ALIGNED=1<<3,
PLANAR_Y_ALIGNED=PLANAR_Y|PLANAR_ALIGNED,
PLANAR_U_ALIGNED=PLANAR_U|PLANAR_ALIGNED,
PLANAR_V_ALIGNED=PLANAR_V|PLANAR_ALIGNED,
PLANAR_A=1<<4,
PLANAR_R=1<<5,
PLANAR_G=1<<6,
PLANAR_B=1<<7,
PLANAR_A_ALIGNED=PLANAR_A|PLANAR_ALIGNED,
PLANAR_R_ALIGNED=PLANAR_R|PLANAR_ALIGNED,
PLANAR_G_ALIGNED=PLANAR_G|PLANAR_ALIGNED,
PLANAR_B_ALIGNED=PLANAR_B|PLANAR_ALIGNED,
};
class AvisynthError /* exception */ {
public:
const char* const msg;
AvisynthError(const char* _msg) : msg(_msg) {}
}; // end class AvisynthError
/* Forward references */
struct __single_inheritance VideoInfo;
class __single_inheritance VideoFrameBuffer;
class __single_inheritance VideoFrame;
class IClip;
class __single_inheritance PClip;
class __single_inheritance PVideoFrame;
class IScriptEnvironment;
class __single_inheritance AVSValue;
/*
* Avisynth C++ plugin API code function pointers.
*
* In order to maintain binary compatibility with
* future version do not change the order of the
* existing function pointers. It will be baked
* into all existing plugins.
*
* Add new function pointers to the end of the
* structure. The linkage macros generate some
* protection code to ensure newer plugin do not
* call non-existing functions in an older host.
*/
struct AVS_Linkage {
int Size;
/**********************************************************************/
// struct VideoInfo
bool (VideoInfo::*HasVideo)() const;
bool (VideoInfo::*HasAudio)() const;
bool (VideoInfo::*IsRGB)() const;
bool (VideoInfo::*IsRGB24)() const;
bool (VideoInfo::*IsRGB32)() const;
bool (VideoInfo::*IsYUV)() const;
bool (VideoInfo::*IsYUY2)() const;
bool (VideoInfo::*IsYV24)() const;
bool (VideoInfo::*IsYV16)() const;
bool (VideoInfo::*IsYV12)() const;
bool (VideoInfo::*IsYV411)() const;
bool (VideoInfo::*IsY8)() const;
bool (VideoInfo::*IsColorSpace)(int c_space) const;
bool (VideoInfo::*Is)(int property) const;
bool (VideoInfo::*IsPlanar)() const;
bool (VideoInfo::*IsFieldBased)() const;
bool (VideoInfo::*IsParityKnown)() const;
bool (VideoInfo::*IsBFF)() const;
bool (VideoInfo::*IsTFF)() const;
bool (VideoInfo::*IsVPlaneFirst)() const;
int (VideoInfo::*BytesFromPixels)(int pixels) const;
int (VideoInfo::*RowSize)(int plane) const;
int (VideoInfo::*BMPSize)() const;
__int64 (VideoInfo::*AudioSamplesFromFrames)(int frames) const;
int (VideoInfo::*FramesFromAudioSamples)(__int64 samples) const;
__int64 (VideoInfo::*AudioSamplesFromBytes)(__int64 bytes) const;
__int64 (VideoInfo::*BytesFromAudioSamples)(__int64 samples) const;
int (VideoInfo::*AudioChannels)() const;
int (VideoInfo::*SampleType)() const;
bool (VideoInfo::*IsSampleType)(int testtype) const;
int (VideoInfo::*SamplesPerSecond)() const;
int (VideoInfo::*BytesPerAudioSample)() const;
void (VideoInfo::*SetFieldBased)(bool isfieldbased);
void (VideoInfo::*Set)(int property);
void (VideoInfo::*Clear)(int property);
int (VideoInfo::*GetPlaneWidthSubsampling)(int plane) const;
int (VideoInfo::*GetPlaneHeightSubsampling)(int plane) const;
int (VideoInfo::*BitsPerPixel)() const;
int (VideoInfo::*BytesPerChannelSample)() const;
void (VideoInfo::*SetFPS)(unsigned numerator, unsigned denominator);
void (VideoInfo::*MulDivFPS)(unsigned multiplier, unsigned divisor);
bool (VideoInfo::*IsSameColorspace)(const VideoInfo& vi) const;
// end struct VideoInfo
/**********************************************************************/
// class VideoFrameBuffer
const BYTE* (VideoFrameBuffer::*VFBGetReadPtr)() const;
BYTE* (VideoFrameBuffer::*VFBGetWritePtr)();
int (VideoFrameBuffer::*GetDataSize)() const;
int (VideoFrameBuffer::*GetSequenceNumber)() const;
int (VideoFrameBuffer::*GetRefcount)() const;
// end class VideoFrameBuffer
/**********************************************************************/
// class VideoFrame
int (VideoFrame::*GetPitch)(int plane) const;
int (VideoFrame::*GetRowSize)(int plane) const;
int (VideoFrame::*GetHeight)(int plane) const;
VideoFrameBuffer* (VideoFrame::*GetFrameBuffer)() const;
int (VideoFrame::*GetOffset)(int plane) const;
const BYTE* (VideoFrame::*VFGetReadPtr)(int plane) const;
bool (VideoFrame::*IsWritable)() const;
BYTE* (VideoFrame::*VFGetWritePtr)(int plane) const;
void (VideoFrame::*VideoFrame_DESTRUCTOR)();
// end class VideoFrame
/**********************************************************************/
// class IClip
/* nothing */
// end class IClip
/**********************************************************************/
// class PClip
void (PClip::*PClip_CONSTRUCTOR0)();
void (PClip::*PClip_CONSTRUCTOR1)(const PClip& x);
void (PClip::*PClip_CONSTRUCTOR2)(IClip* x);
void (PClip::*PClip_OPERATOR_ASSIGN0)(IClip* x);
void (PClip::*PClip_OPERATOR_ASSIGN1)(const PClip& x);
void (PClip::*PClip_DESTRUCTOR)();
// end class PClip
/**********************************************************************/
// class PVideoFrame
void (PVideoFrame::*PVideoFrame_CONSTRUCTOR0)();
void (PVideoFrame::*PVideoFrame_CONSTRUCTOR1)(const PVideoFrame& x);
void (PVideoFrame::*PVideoFrame_CONSTRUCTOR2)(VideoFrame* x);
void (PVideoFrame::*PVideoFrame_OPERATOR_ASSIGN0)(VideoFrame* x);
void (PVideoFrame::*PVideoFrame_OPERATOR_ASSIGN1)(const PVideoFrame& x);
void (PVideoFrame::*PVideoFrame_DESTRUCTOR)();
// end class PVideoFrame
/**********************************************************************/
// class AVSValue
void (AVSValue::*AVSValue_CONSTRUCTOR0)();
void (AVSValue::*AVSValue_CONSTRUCTOR1)(IClip* c);
void (AVSValue::*AVSValue_CONSTRUCTOR2)(const PClip& c);
void (AVSValue::*AVSValue_CONSTRUCTOR3)(bool b);
void (AVSValue::*AVSValue_CONSTRUCTOR4)(int i);
void (AVSValue::*AVSValue_CONSTRUCTOR5)(float f);
void (AVSValue::*AVSValue_CONSTRUCTOR6)(double f);
void (AVSValue::*AVSValue_CONSTRUCTOR7)(const char* s);
void (AVSValue::*AVSValue_CONSTRUCTOR8)(const AVSValue* a, int size);
void (AVSValue::*AVSValue_CONSTRUCTOR9)(const AVSValue& v);
void (AVSValue::*AVSValue_DESTRUCTOR)();
AVSValue& (AVSValue::*AVSValue_OPERATOR_ASSIGN)(const AVSValue& v);
const AVSValue& (AVSValue::*AVSValue_OPERATOR_INDEX)(int index) const;
bool (AVSValue::*Defined)() const;
bool (AVSValue::*IsClip)() const;
bool (AVSValue::*IsBool)() const;
bool (AVSValue::*IsInt)() const;
bool (AVSValue::*IsFloat)() const;
bool (AVSValue::*IsString)() const;
bool (AVSValue::*IsArray)() const;
PClip (AVSValue::*AsClip)() const;
bool (AVSValue::*AsBool1)() const;
int (AVSValue::*AsInt1)() const;
const char* (AVSValue::*AsString1)() const;
double (AVSValue::*AsFloat1)() const;
bool (AVSValue::*AsBool2)(bool def) const;
int (AVSValue::*AsInt2)(int def) const;
double (AVSValue::*AsDblDef)(double def) const;
double (AVSValue::*AsFloat2)(float def) const;
const char* (AVSValue::*AsString2)(const char* def) const;
int (AVSValue::*ArraySize)() const;
// end class AVSValue
/**********************************************************************/
};
#ifdef AVISYNTH_CORE
/* Macro resolution for code inside Avisynth.dll */
# define AVS_BakedCode(arg) ;
# define AVS_LinkCall(arg)
# define AVS_LinkCallV(arg)
#else
/* Macro resolution for code inside user plugin */
# ifdef AVS_LINKAGE_DLLIMPORT
extern __declspec(dllimport) const AVS_Linkage* const AVS_linkage;
# else
extern const AVS_Linkage* AVS_linkage;
# endif
# ifndef offsetof
# include <stddef.h>
# endif
# define AVS_BakedCode(arg) { arg ; }
# define AVS_LinkCall(arg) !AVS_linkage || offsetof(AVS_Linkage, arg) >= AVS_linkage->Size ? 0 : (this->*(AVS_linkage->arg))
# define AVS_LinkCallV(arg) !AVS_linkage || offsetof(AVS_Linkage, arg) >= AVS_linkage->Size ? *this : (this->*(AVS_linkage->arg))
#endif
struct VideoInfo {
int width, height; // width=0 means no video
unsigned fps_numerator, fps_denominator;
int num_frames;
// This is more extensible than previous versions. More properties can be added seeminglesly.
// Colorspace properties.
/*
7<<0 Planar Width Subsampling bits
Use (X+1) & 3 for GetPlaneWidthSubsampling
000 => 1 YV12, YV16
001 => 2 YV411, YUV9
010 => reserved
011 => 0 YV24
1xx => reserved
1<<3 VPlaneFirst YV12, YV16, YV24, YV411, YUV9
1<<4 UPlaneFirst I420
7<<8 Planar Height Subsampling bits
Use ((X>>8)+1) & 3 for GetPlaneHeightSubsampling
000 => 1 YV12
001 => 2 YUV9
010 => reserved
011 => 0 YV16, YV24, YV411
1xx => reserved
7<<16 Sample resolution bits
000 => 8
001 => 16
010 => 32
011 => reserved
1xx => reserved
Planar match mask 1111.0000.0000.0111.0000.0111.0000.0111
Planar signature 10xx.0000.0000.00xx.0000.00xx.00xx.00xx
Planar filter mask 1111.1111.1111.1111.1111.1111.1100.1111
*/
enum {
CS_BGR = 1<<28,
CS_YUV = 1<<29,
CS_INTERLEAVED = 1<<30,
CS_PLANAR = 1<<31,
CS_Shift_Sub_Width = 0,
CS_Shift_Sub_Height = 8,
CS_Shift_Sample_Bits = 16,
CS_Sub_Width_Mask = 7 << CS_Shift_Sub_Width,
CS_Sub_Width_1 = 3 << CS_Shift_Sub_Width, // YV24
CS_Sub_Width_2 = 0 << CS_Shift_Sub_Width, // YV12, I420, YV16
CS_Sub_Width_4 = 1 << CS_Shift_Sub_Width, // YUV9, YV411
CS_VPlaneFirst = 1 << 3, // YV12, YV16, YV24, YV411, YUV9
CS_UPlaneFirst = 1 << 4, // I420
CS_Sub_Height_Mask = 7 << CS_Shift_Sub_Height,
CS_Sub_Height_1 = 3 << CS_Shift_Sub_Height, // YV16, YV24, YV411
CS_Sub_Height_2 = 0 << CS_Shift_Sub_Height, // YV12, I420
CS_Sub_Height_4 = 1 << CS_Shift_Sub_Height, // YUV9
CS_Sample_Bits_Mask = 7 << CS_Shift_Sample_Bits,
CS_Sample_Bits_8 = 0 << CS_Shift_Sample_Bits,
CS_Sample_Bits_16 = 1 << CS_Shift_Sample_Bits,
CS_Sample_Bits_32 = 2 << CS_Shift_Sample_Bits,
CS_PLANAR_MASK = CS_PLANAR | CS_INTERLEAVED | CS_YUV | CS_BGR | CS_Sample_Bits_Mask | CS_Sub_Height_Mask | CS_Sub_Width_Mask,
CS_PLANAR_FILTER = ~( CS_VPlaneFirst | CS_UPlaneFirst ),
// Specific colorformats
CS_UNKNOWN = 0,
CS_BGR24 = 1<<0 | CS_BGR | CS_INTERLEAVED,
CS_BGR32 = 1<<1 | CS_BGR | CS_INTERLEAVED,
CS_YUY2 = 1<<2 | CS_YUV | CS_INTERLEAVED,
// CS_YV12 = 1<<3 Reserved
// CS_I420 = 1<<4 Reserved
CS_RAW32 = 1<<5 | CS_INTERLEAVED,
// YV12 must be 0xA000008 2.5 Baked API will see all new planar as YV12
// I420 must be 0xA000010
CS_YV24 = CS_PLANAR | CS_YUV | CS_Sample_Bits_8 | CS_VPlaneFirst | CS_Sub_Height_1 | CS_Sub_Width_1, // YUV 4:4:4 planar
CS_YV16 = CS_PLANAR | CS_YUV | CS_Sample_Bits_8 | CS_VPlaneFirst | CS_Sub_Height_1 | CS_Sub_Width_2, // YUV 4:2:2 planar
CS_YV12 = CS_PLANAR | CS_YUV | CS_Sample_Bits_8 | CS_VPlaneFirst | CS_Sub_Height_2 | CS_Sub_Width_2, // y-v-u, 4:2:0 planar
CS_I420 = CS_PLANAR | CS_YUV | CS_Sample_Bits_8 | CS_UPlaneFirst | CS_Sub_Height_2 | CS_Sub_Width_2, // y-u-v, 4:2:0 planar
CS_IYUV = CS_I420,
CS_YUV9 = CS_PLANAR | CS_YUV | CS_Sample_Bits_8 | CS_VPlaneFirst | CS_Sub_Height_4 | CS_Sub_Width_4, // YUV 4:1:0 planar
CS_YV411 = CS_PLANAR | CS_YUV | CS_Sample_Bits_8 | CS_VPlaneFirst | CS_Sub_Height_1 | CS_Sub_Width_4, // YUV 4:1:1 planar
CS_Y8 = CS_PLANAR | CS_INTERLEAVED | CS_YUV | CS_Sample_Bits_8, // Y 4:0:0 planar
/*
CS_YV48 = CS_PLANAR | CS_YUV | CS_Sample_Bits_16 | CS_VPlaneFirst | CS_Sub_Height_1 | CS_Sub_Width_1, // YUV 4:4:4 16bit samples
CS_Y16 = CS_PLANAR | CS_INTERLEAVED | CS_YUV | CS_Sample_Bits_16, // Y 4:0:0 16bit samples
CS_YV96 = CS_PLANAR | CS_YUV | CS_Sample_Bits_32 | CS_VPlaneFirst | CS_Sub_Height_1 | CS_Sub_Width_1, // YUV 4:4:4 32bit samples
CS_Y32 = CS_PLANAR | CS_INTERLEAVED | CS_YUV | CS_Sample_Bits_32, // Y 4:0:0 32bit samples
CS_PRGB = CS_PLANAR | CS_RGB | CS_Sample_Bits_8, // Planar RGB
CS_RGB48 = CS_PLANAR | CS_RGB | CS_Sample_Bits_16, // Planar RGB 16bit samples
CS_RGB96 = CS_PLANAR | CS_RGB | CS_Sample_Bits_32, // Planar RGB 32bit samples
*/
};
int pixel_type; // changed to int as of 2.5
int audio_samples_per_second; // 0 means no audio
int sample_type; // as of 2.5
__int64 num_audio_samples; // changed as of 2.5
int nchannels; // as of 2.5
// Imagetype properties
int image_type;
enum {
IT_BFF = 1<<0,
IT_TFF = 1<<1,
IT_FIELDBASED = 1<<2
};
// Chroma placement bits 20 -> 23 ::FIXME:: Really want a Class to support this
enum {
CS_UNKNOWN_CHROMA_PLACEMENT = 0 << 20,
CS_MPEG1_CHROMA_PLACEMENT = 1 << 20,
CS_MPEG2_CHROMA_PLACEMENT = 2 << 20,
CS_YUY2_CHROMA_PLACEMENT = 3 << 20,
CS_TOPLEFT_CHROMA_PLACEMENT = 4 << 20
};
// useful functions of the above
bool HasVideo() const AVS_BakedCode( return AVS_LinkCall(HasVideo)() )
bool HasAudio() const AVS_BakedCode( return AVS_LinkCall(HasAudio)() )
bool IsRGB() const AVS_BakedCode( return AVS_LinkCall(IsRGB)() )
bool IsRGB24() const AVS_BakedCode( return AVS_LinkCall(IsRGB24)() )
bool IsRGB32() const AVS_BakedCode( return AVS_LinkCall(IsRGB32)() )
bool IsYUV() const AVS_BakedCode( return AVS_LinkCall(IsYUV)() )
bool IsYUY2() const AVS_BakedCode( return AVS_LinkCall(IsYUY2)() )
bool IsYV24() const AVS_BakedCode( return AVS_LinkCall(IsYV24)() )
bool IsYV16() const AVS_BakedCode( return AVS_LinkCall(IsYV16)() )
bool IsYV12() const AVS_BakedCode( return AVS_LinkCall(IsYV12)() )
bool IsYV411() const AVS_BakedCode( return AVS_LinkCall(IsYV411)() )
//bool IsYUV9() const;
bool IsY8() const AVS_BakedCode( return AVS_LinkCall(IsY8)() )
bool IsColorSpace(int c_space) const AVS_BakedCode( return AVS_LinkCall(IsColorSpace)(c_space) )
bool Is(int property) const AVS_BakedCode( return AVS_LinkCall(Is)(property) )
bool IsPlanar() const AVS_BakedCode( return AVS_LinkCall(IsPlanar)() )
bool IsFieldBased() const AVS_BakedCode( return AVS_LinkCall(IsFieldBased)() )
bool IsParityKnown() const AVS_BakedCode( return AVS_LinkCall(IsParityKnown)() )
bool IsBFF() const AVS_BakedCode( return AVS_LinkCall(IsBFF)() )
bool IsTFF() const AVS_BakedCode( return AVS_LinkCall(IsTFF)() )
bool IsVPlaneFirst() const AVS_BakedCode( return AVS_LinkCall(IsVPlaneFirst)() ) // Don't use this
int BytesFromPixels(int pixels) const AVS_BakedCode( return AVS_LinkCall(BytesFromPixels)(pixels) ) // Will not work on planar images, but will return only luma planes
int RowSize(int plane=0) const AVS_BakedCode( return AVS_LinkCall(RowSize)(plane) )
int BMPSize() const AVS_BakedCode( return AVS_LinkCall(BMPSize)() )
__int64 AudioSamplesFromFrames(int frames) const AVS_BakedCode( return AVS_LinkCall(AudioSamplesFromFrames)(frames) )
int FramesFromAudioSamples(__int64 samples) const AVS_BakedCode( return AVS_LinkCall(FramesFromAudioSamples)(samples) )
__int64 AudioSamplesFromBytes(__int64 bytes) const AVS_BakedCode( return AVS_LinkCall(AudioSamplesFromBytes)(bytes) )
__int64 BytesFromAudioSamples(__int64 samples) const AVS_BakedCode( return AVS_LinkCall(BytesFromAudioSamples)(samples) )
int AudioChannels() const AVS_BakedCode( return AVS_LinkCall(AudioChannels)() )
int SampleType() const AVS_BakedCode( return AVS_LinkCall(SampleType)() )
bool IsSampleType(int testtype) const AVS_BakedCode( return AVS_LinkCall(IsSampleType)(testtype) )
int SamplesPerSecond() const AVS_BakedCode( return AVS_LinkCall(SamplesPerSecond)() )
int BytesPerAudioSample() const AVS_BakedCode( return AVS_LinkCall(BytesPerAudioSample)() )
void SetFieldBased(bool isfieldbased) AVS_BakedCode( AVS_LinkCall(SetFieldBased)(isfieldbased) )
void Set(int property) AVS_BakedCode( AVS_LinkCall(Set)(property) )
void Clear(int property) AVS_BakedCode( AVS_LinkCall(Clear)(property) )
int GetPlaneWidthSubsampling(int plane) const AVS_BakedCode( return AVS_LinkCall(GetPlaneWidthSubsampling)(plane) ) // Subsampling in bitshifts!
int GetPlaneHeightSubsampling(int plane) const AVS_BakedCode( return AVS_LinkCall(GetPlaneHeightSubsampling)(plane) ) // Subsampling in bitshifts!
int BitsPerPixel() const AVS_BakedCode( return AVS_LinkCall(BitsPerPixel)() )
int BytesPerChannelSample() const AVS_BakedCode( return AVS_LinkCall(BytesPerChannelSample)() )
// useful mutator
void SetFPS(unsigned numerator, unsigned denominator) AVS_BakedCode( AVS_LinkCall(SetFPS)(numerator, denominator) )
// Range protected multiply-divide of FPS
void MulDivFPS(unsigned multiplier, unsigned divisor) AVS_BakedCode( AVS_LinkCall(MulDivFPS)(multiplier, divisor) )
// Test for same colorspace
bool IsSameColorspace(const VideoInfo& vi) const AVS_BakedCode( return AVS_LinkCall(IsSameColorspace)(vi) )
}; // end struct VideoInfo
// VideoFrameBuffer holds information about a memory block which is used
// for video data. For efficiency, instances of this class are not deleted
// when the refcount reaches zero; instead they're stored in a linked list
// to be reused. The instances are deleted when the corresponding AVS
// file is closed.
class VideoFrameBuffer {
BYTE* const data;
const int data_size;
// sequence_number is incremented every time the buffer is changed, so
// that stale views can tell they're no longer valid.
volatile long sequence_number;
friend class VideoFrame;
friend class Cache;
friend class ScriptEnvironment;
volatile long refcount;
protected:
VideoFrameBuffer(int size);
VideoFrameBuffer();
~VideoFrameBuffer();
public:
const BYTE* GetReadPtr() const AVS_BakedCode( return AVS_LinkCall(VFBGetReadPtr)() )
BYTE* GetWritePtr() AVS_BakedCode( return AVS_LinkCall(VFBGetWritePtr)() )
int GetDataSize() const AVS_BakedCode( return AVS_LinkCall(GetDataSize)() )
int GetSequenceNumber() const AVS_BakedCode( return AVS_LinkCall(GetSequenceNumber)() )
int GetRefcount() const AVS_BakedCode( return AVS_LinkCall(GetRefcount)() )
}; // end class VideoFrameBuffer
// VideoFrame holds a "window" into a VideoFrameBuffer. Operator new
// is overloaded to recycle class instances.
class VideoFrame {
volatile long refcount;
VideoFrameBuffer* const vfb;
const int offset, pitch, row_size, height, offsetU, offsetV, pitchUV; // U&V offsets are from top of picture.
const int row_sizeUV, heightUV;
friend class PVideoFrame;
void AddRef();
void Release();
friend class ScriptEnvironment;
friend class Cache;
VideoFrame(VideoFrameBuffer* _vfb, int _offset, int _pitch, int _row_size, int _height);
VideoFrame(VideoFrameBuffer* _vfb, int _offset, int _pitch, int _row_size, int _height, int _offsetU, int _offsetV, int _pitchUV, int _row_sizeUV, int _heightUV);
void* operator new(unsigned size);
// TESTME: OFFSET U/V may be switched to what could be expected from AVI standard!
public:
int GetPitch(int plane=0) const AVS_BakedCode( return AVS_LinkCall(GetPitch)(plane) )
int GetRowSize(int plane=0) const AVS_BakedCode( return AVS_LinkCall(GetRowSize)(plane) )
int GetHeight(int plane=0) const AVS_BakedCode( return AVS_LinkCall(GetHeight)(plane) )
// generally you shouldn't use these three
VideoFrameBuffer* GetFrameBuffer() const AVS_BakedCode( return AVS_LinkCall(GetFrameBuffer)() )
int GetOffset(int plane=0) const AVS_BakedCode( return AVS_LinkCall(GetOffset)(plane) )
// in plugins use env->SubFrame() -- because implementation code is only available inside avisynth.dll. Doh!
VideoFrame* Subframe(int rel_offset, int new_pitch, int new_row_size, int new_height) const;
VideoFrame* Subframe(int rel_offset, int new_pitch, int new_row_size, int new_height, int rel_offsetU, int rel_offsetV, int pitchUV) const;
const BYTE* GetReadPtr(int plane=0) const AVS_BakedCode( return AVS_LinkCall(VFGetReadPtr)(plane) )
bool IsWritable() const AVS_BakedCode( return AVS_LinkCall(IsWritable)() )
BYTE* GetWritePtr(int plane=0) const AVS_BakedCode( return AVS_LinkCall(VFGetWritePtr)(plane) )
~VideoFrame() AVS_BakedCode( AVS_LinkCall(VideoFrame_DESTRUCTOR)() )
#ifdef AVISYNTH_CORE
public:
void DESTRUCTOR(); /* Damn compiler won't allow taking the address of reserved constructs, make a dummy interlude */
#endif
}; // end class VideoFrame
enum {
// Old 2.5 poorly defined cache hints.
// Reserve values used by 2.5 API
// Do not use in new filters
CACHE_25_NOTHING=0,
CACHE_25_RANGE=1,
CACHE_25_ALL=2,
CACHE_25_AUDIO=3,
CACHE_25_AUDIO_NONE=4,
CACHE_25_AUDIO_AUTO=5,
// New 2.6 explicitly defined cache hints.
CACHE_NOTHING=10, // Do not cache video.
CACHE_WINDOW=11, // Hard protect upto X frames within a range of X from the current frame N.
CACHE_GENERIC=12, // LRU cache upto X frames.
CACHE_FORCE_GENERIC=13, // LRU cache upto X frames, override any previous CACHE_WINDOW.
CACHE_GET_POLICY=30, // Get the current policy.
CACHE_GET_WINDOW=31, // Get the current window h_span.
CACHE_GET_RANGE=32, // Get the current generic frame range.
CACHE_AUDIO=50, // Explicitly do cache audio, X byte cache.
CACHE_AUDIO_NOTHING=51, // Explicitly do not cache audio.
CACHE_AUDIO_NONE=52, // Audio cache off (auto mode), X byte intial cache.
CACHE_AUDIO_AUTO=53, // Audio cache on (auto mode), X byte intial cache.
CACHE_GET_AUDIO_POLICY=70, // Get the current audio policy.
CACHE_GET_AUDIO_SIZE=71, // Get the current audio cache size.
CACHE_PREFETCH_FRAME=100, // Queue request to prefetch frame N.
CACHE_PREFETCH_GO=101, // Action video prefetches.
CACHE_PREFETCH_AUDIO_BEGIN=120, // Begin queue request transaction to prefetch audio (take critical section).
CACHE_PREFETCH_AUDIO_STARTLO=121, // Set low 32 bits of start.
CACHE_PREFETCH_AUDIO_STARTHI=122, // Set high 32 bits of start.
CACHE_PREFETCH_AUDIO_COUNT=123, // Set low 32 bits of length.
CACHE_PREFETCH_AUDIO_COMMIT=124, // Enqueue request transaction to prefetch audio (release critical section).
CACHE_PREFETCH_AUDIO_GO=125, // Action audio prefetches.
CACHE_GETCHILD_CACHE_MODE=200, // Cache ask Child for desired video cache mode.
CACHE_GETCHILD_CACHE_SIZE=201, // Cache ask Child for desired video cache size.
CACHE_GETCHILD_AUDIO_MODE=202, // Cache ask Child for desired audio cache mode.
CACHE_GETCHILD_AUDIO_SIZE=203, // Cache ask Child for desired audio cache size.
CACHE_GETCHILD_COST=220, // Cache ask Child for estimated processing cost.
CACHE_COST_ZERO=221, // Child response of zero cost (ptr arithmetic only).
CACHE_COST_UNIT=222, // Child response of unit cost (less than or equal 1 full frame blit).
CACHE_COST_LOW=223, // Child response of light cost. (Fast)
CACHE_COST_MED=224, // Child response of medium cost. (Real time)
CACHE_COST_HI=225, // Child response of heavy cost. (Slow)
CACHE_GETCHILD_THREAD_MODE=240, // Cache ask Child for thread safetyness.
CACHE_THREAD_UNSAFE=241, // Only 1 thread allowed for all instances. 2.5 filters default!
CACHE_THREAD_CLASS=242, // Only 1 thread allowed for each instance. 2.6 filters default!
CACHE_THREAD_SAFE=243, // Allow all threads in any instance.
CACHE_THREAD_OWN=244, // Safe but limit to 1 thread, internally threaded.
CACHE_GETCHILD_ACCESS_COST=260, // Cache ask Child for preferred access pattern.
CACHE_ACCESS_RAND=261, // Filter is access order agnostic.
CACHE_ACCESS_SEQ0=262, // Filter prefers sequential access (low cost)
CACHE_ACCESS_SEQ1=263, // Filter needs sequential access (high cost)
};
// Base class for all filters.
class IClip {
friend class PClip;
friend class AVSValue;
volatile long refcnt;
void AddRef();
void Release();
public:
IClip() : refcnt(0) {}
virtual int __stdcall GetVersion() { return AVISYNTH_INTERFACE_VERSION; }
virtual PVideoFrame __stdcall GetFrame(int n, IScriptEnvironment* env) = 0;
virtual bool __stdcall GetParity(int n) = 0; // return field parity if field_based, else parity of first field in frame
virtual void __stdcall GetAudio(void* buf, __int64 start, __int64 count, IScriptEnvironment* env) = 0; // start and count are in samples
/* Need to check GetVersion first, pre v5 will return random crap from EAX reg. */
virtual int __stdcall SetCacheHints(int cachehints,int frame_range) = 0 ; // We do not pass cache requests upwards, only to the next filter.
virtual const VideoInfo& __stdcall GetVideoInfo() = 0;
virtual __stdcall ~IClip() {}
}; // end class IClip
// smart pointer to IClip
class PClip {
IClip* p;
IClip* GetPointerWithAddRef() const;
friend class AVSValue;
friend class VideoFrame;
void Init(IClip* x);
void Set(IClip* x);
public:
PClip() AVS_BakedCode( AVS_LinkCall(PClip_CONSTRUCTOR0)() )
PClip(const PClip& x) AVS_BakedCode( AVS_LinkCall(PClip_CONSTRUCTOR1)(x) )
PClip(IClip* x) AVS_BakedCode( AVS_LinkCall(PClip_CONSTRUCTOR2)(x) )
void operator=(IClip* x) AVS_BakedCode( AVS_LinkCall(PClip_OPERATOR_ASSIGN0)(x) )
void operator=(const PClip& x) AVS_BakedCode( AVS_LinkCall(PClip_OPERATOR_ASSIGN1)(x) )
IClip* operator->() const { return p; }
// useful in conditional expressions
operator void*() const { return p; }
bool operator!() const { return !p; }
~PClip() AVS_BakedCode( AVS_LinkCall(PClip_DESTRUCTOR)() )
#ifdef AVISYNTH_CORE
public:
void CONSTRUCTOR0(); /* Damn compiler won't allow taking the address of reserved constructs, make a dummy interlude */
void CONSTRUCTOR1(const PClip& x);
void CONSTRUCTOR2(IClip* x);
void OPERATOR_ASSIGN0(IClip* x);
void OPERATOR_ASSIGN1(const PClip& x);
void DESTRUCTOR();
#endif
}; // end class PClip
// smart pointer to VideoFrame
class PVideoFrame {
VideoFrame* p;
void Init(VideoFrame* x);
void Set(VideoFrame* x);
public:
PVideoFrame() AVS_BakedCode( AVS_LinkCall(PVideoFrame_CONSTRUCTOR0)() )
PVideoFrame(const PVideoFrame& x) AVS_BakedCode( AVS_LinkCall(PVideoFrame_CONSTRUCTOR1)(x) )
PVideoFrame(VideoFrame* x) AVS_BakedCode( AVS_LinkCall(PVideoFrame_CONSTRUCTOR2)(x) )
void operator=(VideoFrame* x) AVS_BakedCode( AVS_LinkCall(PVideoFrame_OPERATOR_ASSIGN0)(x) )
void operator=(const PVideoFrame& x) AVS_BakedCode( AVS_LinkCall(PVideoFrame_OPERATOR_ASSIGN1)(x) )
VideoFrame* operator->() const { return p; }
// for conditional expressions
operator void*() const { return p; }
bool operator!() const { return !p; }
~PVideoFrame() AVS_BakedCode( AVS_LinkCall(PVideoFrame_DESTRUCTOR)() )
#ifdef AVISYNTH_CORE
public:
void CONSTRUCTOR0(); /* Damn compiler won't allow taking the address of reserved constructs, make a dummy interlude */
void CONSTRUCTOR1(const PVideoFrame& x);
void CONSTRUCTOR2(VideoFrame* x);
void OPERATOR_ASSIGN0(VideoFrame* x);
void OPERATOR_ASSIGN1(const PVideoFrame& x);
void DESTRUCTOR();
#endif
}; // end class PVideoFrame
class AVSValue {
public:
AVSValue() AVS_BakedCode( AVS_LinkCall(AVSValue_CONSTRUCTOR0)() )
AVSValue(IClip* c) AVS_BakedCode( AVS_LinkCall(AVSValue_CONSTRUCTOR1)(c) )
AVSValue(const PClip& c) AVS_BakedCode( AVS_LinkCall(AVSValue_CONSTRUCTOR2)(c) )
AVSValue(bool b) AVS_BakedCode( AVS_LinkCall(AVSValue_CONSTRUCTOR3)(b) )
AVSValue(int i) AVS_BakedCode( AVS_LinkCall(AVSValue_CONSTRUCTOR4)(i) )
// AVSValue(__int64 l);
AVSValue(float f) AVS_BakedCode( AVS_LinkCall(AVSValue_CONSTRUCTOR5)(f) )
AVSValue(double f) AVS_BakedCode( AVS_LinkCall(AVSValue_CONSTRUCTOR6)(f) )
AVSValue(const char* s) AVS_BakedCode( AVS_LinkCall(AVSValue_CONSTRUCTOR7)(s) )
AVSValue(const AVSValue* a, int size) AVS_BakedCode( AVS_LinkCall(AVSValue_CONSTRUCTOR8)(a, size) )
AVSValue(const AVSValue& v) AVS_BakedCode( AVS_LinkCall(AVSValue_CONSTRUCTOR9)(v) )
~AVSValue() AVS_BakedCode( AVS_LinkCall(AVSValue_DESTRUCTOR)() )
AVSValue& operator=(const AVSValue& v) AVS_BakedCode( return AVS_LinkCallV(AVSValue_OPERATOR_ASSIGN)(v) )
// Note that we transparently allow 'int' to be treated as 'float'.
// There are no int<->bool conversions, though.
bool Defined() const AVS_BakedCode( return AVS_LinkCall(Defined)() )
bool IsClip() const AVS_BakedCode( return AVS_LinkCall(IsClip)() )
bool IsBool() const AVS_BakedCode( return AVS_LinkCall(IsBool)() )
bool IsInt() const AVS_BakedCode( return AVS_LinkCall(IsInt)() )
// bool IsLong() const;
bool IsFloat() const AVS_BakedCode( return AVS_LinkCall(IsFloat)() )
bool IsString() const AVS_BakedCode( return AVS_LinkCall(IsString)() )
bool IsArray() const AVS_BakedCode( return AVS_LinkCall(IsArray)() )
PClip AsClip() const AVS_BakedCode( return AVS_LinkCall(AsClip)() )
bool AsBool() const AVS_BakedCode( return AVS_LinkCall(AsBool1)() )
int AsInt() const AVS_BakedCode( return AVS_LinkCall(AsInt1)() )
// int AsLong() const;
const char* AsString() const AVS_BakedCode( return AVS_LinkCall(AsString1)() )
double AsFloat() const AVS_BakedCode( return AVS_LinkCall(AsFloat1)() )
bool AsBool(bool def) const AVS_BakedCode( return AVS_LinkCall(AsBool2)(def) )
int AsInt(int def) const AVS_BakedCode( return AVS_LinkCall(AsInt2)(def) )
double AsDblDef(double def) const AVS_BakedCode( return AVS_LinkCall(AsDblDef)(def) ) // Value is still a float
//float AsFloat(double def) const; // def demoted to a float
double AsFloat(float def) const AVS_BakedCode( return AVS_LinkCall(AsFloat2)(def) )
const char* AsString(const char* def) const AVS_BakedCode( return AVS_LinkCall(AsString2)(def) )
int ArraySize() const AVS_BakedCode( return AVS_LinkCall(ArraySize)() )
const AVSValue& operator[](int index) const AVS_BakedCode( return AVS_LinkCallV(AVSValue_OPERATOR_INDEX)(index) )
private:
short type; // 'a'rray, 'c'lip, 'b'ool, 'i'nt, 'f'loat, 's'tring, 'v'oid, or 'l'ong
short array_size;
union {
IClip* clip;
bool boolean;
int integer;
float floating_pt;
const char* string;
const AVSValue* array;
// __int64 longlong;
};
void Assign(const AVSValue* src, bool init);
#ifdef AVISYNTH_CORE
public:
void CONSTRUCTOR0(); /* Damn compiler won't allow taking the address of reserved constructs, make a dummy interlude */
void CONSTRUCTOR1(IClip* c);
void CONSTRUCTOR2(const PClip& c);
void CONSTRUCTOR3(bool b);
void CONSTRUCTOR4(int i);
void CONSTRUCTOR5(float f);
void CONSTRUCTOR6(double f);
void CONSTRUCTOR7(const char* s);
void CONSTRUCTOR8(const AVSValue* a, int size);
void CONSTRUCTOR9(const AVSValue& v);
void DESTRUCTOR();
AVSValue& OPERATOR_ASSIGN(const AVSValue& v);
const AVSValue& OPERATOR_INDEX(int index) const;
bool AsBool1() const;
int AsInt1() const;
const char* AsString1() const;
double AsFloat1() const;
bool AsBool2(bool def) const;
int AsInt2(int def) const;
double AsFloat2(float def) const;
const char* AsString2(const char* def) const;
#endif
}; // end class AVSValue
#undef AVS_LinkCallV
#undef AVS_LinkCall
#undef AVS_BakedCode
// instantiable null filter
class GenericVideoFilter : public IClip {
protected:
PClip child;
VideoInfo vi;
public:
GenericVideoFilter(PClip _child) : child(_child) { vi = child->GetVideoInfo(); }
PVideoFrame __stdcall GetFrame(int n, IScriptEnvironment* env) { return child->GetFrame(n, env); }
void __stdcall GetAudio(void* buf, __int64 start, __int64 count, IScriptEnvironment* env) { child->GetAudio(buf, start, count, env); }
const VideoInfo& __stdcall GetVideoInfo() { return vi; }
bool __stdcall GetParity(int n) { return child->GetParity(n); }
int __stdcall SetCacheHints(int cachehints,int frame_range) { return 0; } ; // We do not pass cache requests upwards, only to the next filter.
};
#if 0
/* Helper classes useful to plugin authors */ // But we don't export the entry points, Doh!
class AlignPlanar : public GenericVideoFilter
{
public:
AlignPlanar(PClip _clip);
static PClip Create(PClip clip);
PVideoFrame __stdcall GetFrame(int n, IScriptEnvironment* env);
};
class FillBorder : public GenericVideoFilter
{
public:
FillBorder(PClip _clip);
static PClip Create(PClip clip);
PVideoFrame __stdcall GetFrame(int n, IScriptEnvironment* env);
};
class ConvertAudio : public GenericVideoFilter
/**
* Helper class to convert audio to any format
**/
{
public:
ConvertAudio(PClip _clip, int prefered_format);
void __stdcall GetAudio(void* buf, __int64 start, __int64 count, IScriptEnvironment* env);
int __stdcall SetCacheHints(int cachehints,int frame_range); // We do pass cache requests upwards, to the cache!
static PClip Create(PClip clip, int sample_type, int prefered_type);
static AVSValue __cdecl Create_float(AVSValue args, void*, IScriptEnvironment*);
static AVSValue __cdecl Create_32bit(AVSValue args, void*, IScriptEnvironment*);
static AVSValue __cdecl Create_24bit(AVSValue args, void*, IScriptEnvironment*);
static AVSValue __cdecl Create_16bit(AVSValue args, void*, IScriptEnvironment*);
static AVSValue __cdecl Create_8bit (AVSValue args, void*, IScriptEnvironment*);
static AVSValue __cdecl Create_Any (AVSValue args, void*, IScriptEnvironment*);
virtual ~ConvertAudio();
private:
void convertToFloat(char* inbuf, float* outbuf, char sample_type, int count);
void convertToFloat_3DN(char* inbuf, float* outbuf, char sample_type, int count);
void convertToFloat_SSE(char* inbuf, float* outbuf, char sample_type, int count);
void convertToFloat_SSE2(char* inbuf, float* outbuf, char sample_type, int count);
void convertFromFloat(float* inbuf, void* outbuf, char sample_type, int count);
void convertFromFloat_3DN(float* inbuf, void* outbuf, char sample_type, int count);
void convertFromFloat_SSE(float* inbuf, void* outbuf, char sample_type, int count);
void convertFromFloat_SSE2(float* inbuf, void* outbuf, char sample_type, int count);
__inline int Saturate_int8(float n);
__inline short Saturate_int16(float n);
__inline int Saturate_int24(float n);
__inline int Saturate_int32(float n);
char src_format;
char dst_format;
int src_bps;
char *tempbuffer;
SFLOAT *floatbuffer;
int tempbuffer_size;
};
#endif
// For GetCPUFlags. These are backwards-compatible with those in VirtualDub.
enum {
/* oldest CPU to support extension */
CPUF_FORCE = 0x01, // N/A
CPUF_FPU = 0x02, // 386/486DX
CPUF_MMX = 0x04, // P55C, K6, PII
CPUF_INTEGER_SSE = 0x08, // PIII, Athlon
CPUF_SSE = 0x10, // PIII, Athlon XP/MP
CPUF_SSE2 = 0x20, // PIV, K8
CPUF_3DNOW = 0x40, // K6-2
CPUF_3DNOW_EXT = 0x80, // Athlon
CPUF_X86_64 = 0xA0, // Hammer (note: equiv. to 3DNow + SSE2, which
// only Hammer will have anyway)
CPUF_SSE3 = 0x100, // PIV+, K8 Venice
CPUF_SSSE3 = 0x200, // Core 2
CPUF_SSE4 = 0x400, // Penryn, Wolfdale, Yorkfield
CPUF_SSE4_1 = 0x400,
CPUF_SSE4_2 = 0x800, // Nehalem
};
#if 0
#define MAX_INT 0x7fffffff
#define MIN_INT -0x7fffffff // ::FIXME:: research why this is not 0x80000000
#endif
class IScriptEnvironment {
public:
virtual __stdcall ~IScriptEnvironment() {}
virtual /*static*/ long __stdcall GetCPUFlags() = 0;
virtual char* __stdcall SaveString(const char* s, int length = -1) = 0;
virtual char* __stdcall Sprintf(const char* fmt, ...) = 0;
// note: val is really a va_list; I hope everyone typedefs va_list to a pointer
virtual char* __stdcall VSprintf(const char* fmt, void* val) = 0;
__declspec(noreturn) virtual void __stdcall ThrowError(const char* fmt, ...) = 0;
class NotFound /*exception*/ {}; // thrown by Invoke and GetVar
typedef AVSValue (__cdecl *ApplyFunc)(AVSValue args, void* user_data, IScriptEnvironment* env);
virtual void __stdcall AddFunction(const char* name, const char* params, ApplyFunc apply, void* user_data) = 0;
virtual bool __stdcall FunctionExists(const char* name) = 0;
virtual AVSValue __stdcall Invoke(const char* name, const AVSValue args, const char* const* arg_names=0) = 0;
virtual AVSValue __stdcall GetVar(const char* name) = 0;
virtual bool __stdcall SetVar(const char* name, const AVSValue& val) = 0;
virtual bool __stdcall SetGlobalVar(const char* name, const AVSValue& val) = 0;
virtual void __stdcall PushContext(int level=0) = 0;
virtual void __stdcall PopContext() = 0;
// align should be 4 or 8
virtual PVideoFrame __stdcall NewVideoFrame(const VideoInfo& vi, int align=FRAME_ALIGN) = 0;
virtual bool __stdcall MakeWritable(PVideoFrame* pvf) = 0;
virtual /*static*/ void __stdcall BitBlt(BYTE* dstp, int dst_pitch, const BYTE* srcp, int src_pitch, int row_size, int height) = 0;
typedef void (__cdecl *ShutdownFunc)(void* user_data, IScriptEnvironment* env);
virtual void __stdcall AtExit(ShutdownFunc function, void* user_data) = 0;
virtual void __stdcall CheckVersion(int version = AVISYNTH_INTERFACE_VERSION) = 0;
virtual PVideoFrame __stdcall Subframe(PVideoFrame src, int rel_offset, int new_pitch, int new_row_size, int new_height) = 0;
virtual int __stdcall SetMemoryMax(int mem) = 0;
virtual int __stdcall SetWorkingDir(const char * newdir) = 0;
virtual void* __stdcall ManageCache(int key, void* data) = 0;
enum PlanarChromaAlignmentMode {
PlanarChromaAlignmentOff,
PlanarChromaAlignmentOn,
PlanarChromaAlignmentTest };
virtual bool __stdcall PlanarChromaAlignment(PlanarChromaAlignmentMode key) = 0;
virtual PVideoFrame __stdcall SubframePlanar(PVideoFrame src, int rel_offset, int new_pitch, int new_row_size, int new_height, int rel_offsetU, int rel_offsetV, int new_pitchUV) = 0;
virtual void __stdcall DeleteScriptEnvironment() = 0;
virtual void _stdcall ApplyMessage(PVideoFrame* frame, const VideoInfo& vi, const char* message, int size, int textcolor, int halocolor, int bgcolor) = 0;
virtual const AVS_Linkage* const __stdcall GetAVSLinkage() = 0;
}; // end class IScriptEnvironment
// avisynth.dll exports this; it's a way to use it as a library, without
// writing an AVS script or without going through AVIFile.
IScriptEnvironment* __stdcall CreateScriptEnvironment(int version = AVISYNTH_INTERFACE_VERSION);
#pragma pack(pop)
#endif //__AVISYNTH_H__
// Modified copy of interface.cpp version 1.13, from Avisynth 2.6:
// http://avisynth2.cvs.sourceforge.net/avisynth2/avisynth/src/core/interface.cpp?revision=1.13
//
// See http://forum.doom9.org/showthread.php?p=1641233#post1641233 and the
// following posts for discussion.
//
// Changes from revision 1.13:
//
// -Inclusion of stdafx.h eliminated, we're not using precompiled headers.
//
// -AVISYNTH_CORE macro defined before avisynth.h is included, to disable
// the header's function definitions and allow us to use our own.
//
// -Debug macro block lifted from the Avisynth header, since some versions of
// MS headers (namely those of VS 2010) expect certain things to be available
// in the global namespace. _RPT macros commented out to avoid warnings about
// macro redefinition.
//
// -AVSInterface25 namespace wrapped around all function definitions to keep
// them distinct from those found in invertneg.cpp when including Avisynth
// header normally (without AVISYNTH_CORE defined). Inclusion of Windows.h
// kept outside namespace to avoid problems with sourceannotations.h, present
// in some later Microsoft SDKs.
//
// -GetRowSize and GetHeight reverted to the 2.5 design, as the new approach
// depends on VideoInfo members that don't exist (and won't be initialized)
// in that version of Avisynth. The two overloads were not restored, since
// the header declares a default value of zero for the plane argument. Other
// 2.6 functions were left alone, as I believe they depend only on things
// declared, defined and initialized in this file and avisynth.h.
//
// -AVS_Linkage instance 'avs_linkage' made non-static and non-const, to make
// it visible from invertneg.cpp.
//
// I make no guarantees that this is the best, or even a valid, way to enable
// a dual 2.5/2.6 use plugin, but it seems to work, and is the best I've come up
// with so far. Questions and corrections welcome.
//
// September 2013, Robert Martens <robert.martens@gmail.com>
// Avisynth v2.5. Copyright 2002, 2005 Ben Rudiak-Gould et al.
// Avisynth v2.6. Copyright 2006 Klaus Post.
// Avisynth v2.6. Copyright 2009 Ian Brabham.
// http://www.avisynth.org
// This program is free software; you can redistribute it and/or modify
// it under the terms of the GNU General Public License as published by
// the Free Software Foundation; either version 2 of the License, or
// (at your option) any later version.
//
// This program is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
// GNU General Public License for more details.
//
// You should have received a copy of the GNU General Public License
// along with this program; if not, write to the Free Software
// Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA, or visit
// http://www.gnu.org/copyleft/gpl.html .
//
// Linking Avisynth statically or dynamically with other modules is making a
// combined work based on Avisynth. Thus, the terms and conditions of the GNU
// General Public License cover the whole combination.
//
// As a special exception, the copyright holders of Avisynth give you
// permission to link Avisynth with independent modules that communicate with
// Avisynth solely through the interfaces defined in avisynth.h, regardless of the license
// terms of these independent modules, and to copy and distribute the
// resulting combined work under terms of your choice, provided that
// every copy of the combined work is accompanied by a complete copy of
// the source code of Avisynth (the version of Avisynth used to produce the
// combined work), being distributed under the terms of the GNU General
// Public License plus this exception. An independent module is a module
// which is not derived from or based on Avisynth, such as 3rd-party filters,
// import and export plugins, or graphical user interfaces.
/* Maintenance notes :-
*
* All the code here was formally baked into the avisynth.h interface.
*
* Whenever you modify any code here please keep and mark the original
* code with a block comment beginning with the word "Baked"
*
* Be mindful with changes you do make that previous 2.5 plugins will
* still have the original code active. This may require other defensive
* or mitigating code elsewhere.
*/
/*2.6
// #include "stdafx.h"
2.6 */
#include <windows.h>
#ifdef _MSC_VER
#include <crtdbg.h>
#else
//#define _RPT0(a,b) ((void)0)
//#define _RPT1(a,b,c) ((void)0)
//#define _RPT2(a,b,c,d) ((void)0)
//#define _RPT3(a,b,c,d,e) ((void)0)
//#define _RPT4(a,b,c,d,e,f) ((void)0)
//#define _ASSERTE(x) assert(x)
#include <assert.h>
#endif
namespace AVSInterface25 {
#define AVISYNTH_CORE
#include "avisynth.h"
/**********************************************************************/
// struct VideoInfo
// useful functions of the above
bool VideoInfo::HasVideo() const { return (width!=0); }
bool VideoInfo::HasAudio() const { return (audio_samples_per_second!=0); }
bool VideoInfo::IsRGB() const { return !!(pixel_type&CS_BGR); }
bool VideoInfo::IsRGB24() const { return (pixel_type&CS_BGR24)==CS_BGR24; } // Clear out additional properties
bool VideoInfo::IsRGB32() const { return (pixel_type & CS_BGR32) == CS_BGR32 ; }
bool VideoInfo::IsYUV() const { return !!(pixel_type&CS_YUV ); }
bool VideoInfo::IsYUY2() const { return (pixel_type & CS_YUY2) == CS_YUY2; }
bool VideoInfo::IsYV24() const { return (pixel_type & CS_PLANAR_MASK) == (CS_YV24 & CS_PLANAR_FILTER); }
bool VideoInfo::IsYV16() const { return (pixel_type & CS_PLANAR_MASK) == (CS_YV16 & CS_PLANAR_FILTER); }
bool VideoInfo::IsYV12() const { return (pixel_type & CS_PLANAR_MASK) == (CS_YV12 & CS_PLANAR_FILTER); }
bool VideoInfo::IsY8() const { return (pixel_type & CS_PLANAR_MASK) == (CS_Y8 & CS_PLANAR_FILTER); }
bool VideoInfo::IsYV411() const { return (pixel_type & CS_PLANAR_MASK) == (CS_YV411 & CS_PLANAR_FILTER); }
//bool VideoInfo::IsYUV9() const { return (pixel_type & CS_PLANAR_MASK) == (CS_YUV9 & CS_PLANAR_FILTER); }
/* Baked ********************
bool VideoInfo::IsColorSpace(int c_space) const { return ((pixel_type & c_space) == c_space); }
Baked ********************/
bool VideoInfo::IsColorSpace(int c_space) const {
return IsPlanar() ? ((pixel_type & CS_PLANAR_MASK) == (c_space & CS_PLANAR_FILTER)) : ((pixel_type & c_space) == c_space);
}
bool VideoInfo::Is(int property) const { return ((pixel_type & property)==property ); }
bool VideoInfo::IsPlanar() const { return !!(pixel_type & CS_PLANAR); }
bool VideoInfo::IsFieldBased() const { return !!(image_type & IT_FIELDBASED); }
bool VideoInfo::IsParityKnown() const { return ((image_type & IT_FIELDBASED)&&(image_type & (IT_BFF|IT_TFF))); }
bool VideoInfo::IsBFF() const { return !!(image_type & IT_BFF); }
bool VideoInfo::IsTFF() const { return !!(image_type & IT_TFF); }
/* Baked ********************
bool VideoInfo::IsVPlaneFirst() const {return ((pixel_type & CS_YV12) == CS_YV12); } // Don't use this
int VideoInfo::BytesFromPixels(int pixels) const { return pixels * (BitsPerPixel()>>3); } // Will not work on planar images, but will return only luma planes
int VideoInfo::RowSize() const { return BytesFromPixels(width); } // Also only returns first plane on planar images
int VideoInfo::BMPSize() const { if (IsPlanar()) {int p = height * ((RowSize()+3) & ~3); p+=p>>1; return p; } return height * ((RowSize()+3) & ~3); }
__int64 VideoInfo::AudioSamplesFromFrames(__int64 frames) const { return (fps_numerator && HasVideo()) ? ((__int64)(frames) * audio_samples_per_second * fps_denominator / fps_numerator) : 0; }
Baked ********************/
__int64 VideoInfo::AudioSamplesFromFrames(int frames) const { return (fps_numerator && HasVideo()) ? ((__int64)(frames) * audio_samples_per_second * fps_denominator / fps_numerator) : 0; }
int VideoInfo::FramesFromAudioSamples(__int64 samples) const { return (fps_denominator && HasAudio()) ? (int)((samples * fps_numerator)/((__int64)fps_denominator * audio_samples_per_second)) : 0; }
__int64 VideoInfo::AudioSamplesFromBytes(__int64 bytes) const { return HasAudio() ? bytes / BytesPerAudioSample() : 0; }
__int64 VideoInfo::BytesFromAudioSamples(__int64 samples) const { return samples * BytesPerAudioSample(); }
int VideoInfo::AudioChannels() const { return HasAudio() ? nchannels : 0; }
int VideoInfo::SampleType() const{ return sample_type;}
bool VideoInfo::IsSampleType(int testtype) const{ return !!(sample_type&testtype);}
int VideoInfo::SamplesPerSecond() const { return audio_samples_per_second; }
int VideoInfo::BytesPerAudioSample() const { return nchannels*BytesPerChannelSample();}
void VideoInfo::SetFieldBased(bool isfieldbased) { if (isfieldbased) image_type|=IT_FIELDBASED; else image_type&=~IT_FIELDBASED; }
void VideoInfo::Set(int property) { image_type|=property; }
void VideoInfo::Clear(int property) { image_type&=~property; }
/* Baked ********************
int VideoInfo::BitsPerPixel() const {
switch (pixel_type) {
case CS_BGR24:
return 24;
case CS_BGR32:
return 32;
case CS_YUY2:
return 16;
case CS_YV12:
case CS_I420:
return 12;
default:
return 0;
}
}
Baked ********************/
int VideoInfo::BytesPerChannelSample() const {
switch (sample_type) {
case SAMPLE_INT8:
return sizeof(unsigned char);
case SAMPLE_INT16:
return sizeof(signed short);
case SAMPLE_INT24:
return 3;
case SAMPLE_INT32:
return sizeof(signed int);
case SAMPLE_FLOAT:
return sizeof(SFLOAT);
default:
_ASSERTE("Sample type not recognized!");
return 0;
}
}
bool VideoInfo::IsVPlaneFirst() const {
return !IsY8() && IsPlanar() && (pixel_type & (CS_VPlaneFirst | CS_UPlaneFirst)) == CS_VPlaneFirst; // Shouldn't use this
}
int VideoInfo::BytesFromPixels(int pixels) const {
return !IsY8() && IsPlanar() ? pixels << ((pixel_type>>CS_Shift_Sample_Bits) & 3) : pixels * (BitsPerPixel()>>3); // For planar images, will return luma plane
}
int VideoInfo::RowSize(int plane) const {
const int rowsize = BytesFromPixels(width);
switch (plane) {
case PLANAR_U: case PLANAR_V:
return (!IsY8() && IsPlanar()) ? rowsize>>GetPlaneWidthSubsampling(plane) : 0;
case PLANAR_U_ALIGNED: case PLANAR_V_ALIGNED:
return (!IsY8() && IsPlanar()) ? ((rowsize>>GetPlaneWidthSubsampling(plane))+FRAME_ALIGN-1)&(~(FRAME_ALIGN-1)) : 0; // Aligned rowsize
case PLANAR_Y_ALIGNED:
return (rowsize+FRAME_ALIGN-1)&(~(FRAME_ALIGN-1)); // Aligned rowsize
}
return rowsize;
}
int VideoInfo::BMPSize() const {
if (!IsY8() && IsPlanar()) {
// Y plane
const int Ybytes = ((RowSize(PLANAR_Y)+3) & ~3) * height;
const int UVbytes = ((RowSize(PLANAR_U)+3) & ~3) * height >> GetPlaneHeightSubsampling(PLANAR_U);
return Ybytes + UVbytes*2;
}
return height * ((RowSize()+3) & ~3);
}
int VideoInfo::GetPlaneWidthSubsampling(int plane) const { // Subsampling in bitshifts!
if (plane == PLANAR_Y) // No subsampling
return 0;
if (IsY8())
throw AvisynthError("Filter error: GetPlaneWidthSubsampling not available on Y8 pixel type.");
if (plane == PLANAR_U || plane == PLANAR_V) {
if (IsYUY2())
return 1;
else if (IsPlanar())
return ((pixel_type>>CS_Shift_Sub_Width)+1) & 3;
else
throw AvisynthError("Filter error: GetPlaneWidthSubsampling called with unsupported pixel type.");
}
throw AvisynthError("Filter error: GetPlaneWidthSubsampling called with unsupported plane.");
}
int VideoInfo::GetPlaneHeightSubsampling(int plane) const { // Subsampling in bitshifts!
if (plane == PLANAR_Y) // No subsampling
return 0;
if (IsY8())
throw AvisynthError("Filter error: GetPlaneHeightSubsampling not available on Y8 pixel type.");
if (plane == PLANAR_U || plane == PLANAR_V) {
if (IsYUY2())
return 0;
else if (IsPlanar())
return ((pixel_type>>CS_Shift_Sub_Height)+1) & 3;
else
throw AvisynthError("Filter error: GetPlaneHeightSubsampling called with unsupported pixel type.");
}
throw AvisynthError("Filter error: GetPlaneHeightSubsampling called with supported plane.");
}
int VideoInfo::BitsPerPixel() const {
// Lookup Interleaved, calculate PLANAR's
switch (pixel_type) {
case CS_BGR24:
return 24;
case CS_BGR32:
return 32;
case CS_YUY2:
return 16;
case CS_Y8:
return 8;
// case CS_Y16:
// return 16;
// case CS_Y32:
// return 32;
}
if (IsPlanar()) {
const int S = IsYUV() ? GetPlaneWidthSubsampling(PLANAR_U) + GetPlaneHeightSubsampling(PLANAR_U) : 0;
return ( ((1<<S)+2) * (8<<((pixel_type>>CS_Shift_Sample_Bits) & 3)) ) >> S;
}
return 0;
}
// useful mutator
void VideoInfo::SetFPS(unsigned numerator, unsigned denominator) {
if ((numerator == 0) || (denominator == 0)) {
fps_numerator = 0;
fps_denominator = 1;
}
else {
unsigned x=numerator, y=denominator;
while (y) { // find gcd
unsigned t = x%y; x = y; y = t;
}
fps_numerator = numerator/x;
fps_denominator = denominator/x;
}
}
// Range protected multiply-divide of FPS
void VideoInfo::MulDivFPS(unsigned multiplier, unsigned divisor) {
unsigned __int64 numerator = UInt32x32To64(fps_numerator, multiplier);
unsigned __int64 denominator = UInt32x32To64(fps_denominator, divisor);
unsigned __int64 x=numerator, y=denominator;
while (y) { // find gcd
unsigned __int64 t = x%y; x = y; y = t;
}
numerator /= x; // normalize
denominator /= x;
unsigned __int64 temp = numerator | denominator; // Just looking top bit
unsigned u = 0;
while (temp & 0xffffffff80000000) { // or perhaps > 16777216*2
temp = Int64ShrlMod32(temp, 1);
u++;
}
if (u) { // Scale to fit
const unsigned round = 1 << (u-1);
SetFPS( (unsigned)Int64ShrlMod32(numerator + round, u),
(unsigned)Int64ShrlMod32(denominator + round, u) );
}
else {
fps_numerator = (unsigned)numerator;
fps_denominator = (unsigned)denominator;
}
}
// Test for same colorspace
bool VideoInfo::IsSameColorspace(const VideoInfo& vi) const {
if (vi.pixel_type == pixel_type) return TRUE;
if (IsYV12() && vi.IsYV12()) return TRUE;
return FALSE;
}
// end struct VideoInfo
/**********************************************************************/
// class VideoFrameBuffer
const BYTE* VideoFrameBuffer::GetReadPtr() const { return data; }
/* Baked ********************
BYTE* VideoFrameBuffer::GetWritePtr() { ++sequence_number; return data; }
Baked ********************/
BYTE* VideoFrameBuffer::GetWritePtr() { InterlockedIncrement(&sequence_number); return data; }
int VideoFrameBuffer::GetDataSize() const { return data_size; }
int VideoFrameBuffer::GetSequenceNumber() const { return sequence_number; }
int VideoFrameBuffer::GetRefcount() const { return refcount; }
// end class VideoFrameBuffer
/**********************************************************************/
// class VideoFrame
/* Baked ********************
void VideoFrame::AddRef() { InterlockedIncrement((long *)&refcount); }
void VideoFrame::Release() { if (refcount==1) InterlockedDecrement(&vfb->refcount); InterlockedDecrement((long *)&refcount); }
int VideoFrame::GetPitch() const { return pitch; }
int VideoFrame::GetPitch(int plane) const { switch (plane) {case PLANAR_U: case PLANAR_V: return pitchUV;} return pitch; }
int VideoFrame::GetRowSize() const { return row_size; }
Baked ********************/
int VideoFrame::GetRowSize(int plane) const {
switch (plane) {
case PLANAR_U: case PLANAR_V: if (pitchUV) return row_size>>1; else return 0;
case PLANAR_U_ALIGNED: case PLANAR_V_ALIGNED:
if (pitchUV) {
int r = ((row_size+FRAME_ALIGN-1)&(~(FRAME_ALIGN-1)) )>>1; // Aligned rowsize
if (r<=pitchUV)
return r;
return row_size>>1;
} else return 0;
case PLANAR_Y_ALIGNED:
int r = (row_size+FRAME_ALIGN-1)&(~(FRAME_ALIGN-1)); // Aligned rowsize
if (r<=pitch)
return r;
return row_size;
}
return row_size; }
/* Baked ********************
int VideoFrame::GetHeight() const { return height; }
Baked ********************/
int VideoFrame::GetHeight(int plane) const { switch (plane) {case PLANAR_U: case PLANAR_V: if (pitchUV) return height>>1; return 0;} return height; }
/* Baked ********************
// generally you shouldn't use these three
VideoFrameBuffer* VideoFrame::GetFrameBuffer() const { return vfb; }
int VideoFrame::GetOffset() const { return offset; }
int VideoFrame::GetOffset(int plane) const { switch (plane) {case PLANAR_U: return offsetU;case PLANAR_V: return offsetV;default: return offset;}; }
const BYTE* VideoFrame::GetReadPtr() const { return vfb->GetReadPtr() + offset; }
const BYTE* VideoFrame::GetReadPtr(int plane) const { return vfb->GetReadPtr() + GetOffset(plane); }
bool VideoFrame::IsWritable() const { return (refcount == 1 && vfb->refcount == 1); }
BYTE* VideoFrame::GetWritePtr() const {
if (vfb->GetRefcount()>1) {
_ASSERT(FALSE);
//throw AvisynthError("Internal Error - refcount was more than one!");
}
return IsWritable() ? (vfb->GetWritePtr() + offset) : 0;
}
Baked ********************/
void VideoFrame::AddRef() { InterlockedIncrement(&refcount); }
void VideoFrame::Release() {
VideoFrameBuffer* _vfb = vfb;
if (!InterlockedDecrement(&refcount))
InterlockedDecrement(&_vfb->refcount);
}
int VideoFrame::GetPitch(int plane) const { switch (plane) {case PLANAR_U: case PLANAR_V: return pitchUV;} return pitch; }
/* 2.6 ********************
int VideoFrame::GetRowSize(int plane) const {
switch (plane) {
case PLANAR_U: case PLANAR_V: if (pitchUV) return row_sizeUV; else return 0;
case PLANAR_U_ALIGNED: case PLANAR_V_ALIGNED:
if (pitchUV) {
const int r = (row_sizeUV+FRAME_ALIGN-1)&(~(FRAME_ALIGN-1)); // Aligned rowsize
if (r<=pitchUV)
return r;
return row_sizeUV;
}
else return 0;
case PLANAR_ALIGNED: case PLANAR_Y_ALIGNED:
const int r = (row_size+FRAME_ALIGN-1)&(~(FRAME_ALIGN-1)); // Aligned rowsize
if (r<=pitch)
return r;
return row_size;
}
return row_size; }
int VideoFrame::GetHeight(int plane) const { switch (plane) {case PLANAR_U: case PLANAR_V: if (pitchUV) return heightUV; return 0;} return height; }
2.6 ********************/
// Generally you should not be using these two
VideoFrameBuffer* VideoFrame::GetFrameBuffer() const { return vfb; }
int VideoFrame::GetOffset(int plane) const { switch (plane) {case PLANAR_U: return offsetU;case PLANAR_V: return offsetV;default: return offset;}; }
const BYTE* VideoFrame::GetReadPtr(int plane) const { return vfb->GetReadPtr() + GetOffset(plane); }
bool VideoFrame::IsWritable() const {
if (refcount == 1 && vfb->refcount == 1) {
vfb->GetWritePtr(); // Bump sequence number
return true;
}
return false;
}
BYTE* VideoFrame::GetWritePtr(int plane) const {
if (!plane || plane == PLANAR_Y) {
if (vfb->GetRefcount()>1) {
_ASSERT(FALSE);
// throw AvisynthError("Internal Error - refcount was more than one!");
}
return (refcount == 1 && vfb->refcount == 1) ? vfb->GetWritePtr() + GetOffset(plane) : 0;
}
return vfb->data + GetOffset(plane);
}
/* Baked ********************
VideoFrame::~VideoFrame() { InterlockedDecrement(&vfb->refcount); }
Baked ********************/
VideoFrame::~VideoFrame() { DESTRUCTOR(); }
void VideoFrame::DESTRUCTOR() { Release(); }
// end class VideoFrame
/**********************************************************************/
// class IClip
/* Baked ********************
void IClip::AddRef() { InterlockedIncrement((long *)&refcnt); }
void IClip::Release() { InterlockedDecrement((long *)&refcnt); if (!refcnt) delete this; }
Baked ********************/
void IClip::AddRef() { InterlockedIncrement(&refcnt); }
void IClip::Release() { if (!InterlockedDecrement(&refcnt)) delete this; }
// end class IClip
/**********************************************************************/
// class PClip
IClip* PClip::GetPointerWithAddRef() const { if (p) p->AddRef(); return p; }
void PClip::Init(IClip* x) {
if (x) x->AddRef();
p=x;
}
void PClip::Set(IClip* x) {
if (x) x->AddRef();
if (p) p->Release();
p=x;
}
PClip::PClip() { CONSTRUCTOR0(); }
void PClip::CONSTRUCTOR0() { p = 0; }
PClip::PClip(const PClip& x) { CONSTRUCTOR1(x); }
void PClip::CONSTRUCTOR1(const PClip& x) { Init(x.p); }
PClip::PClip(IClip* x) { CONSTRUCTOR2(x); }
void PClip::CONSTRUCTOR2(IClip* x) { Init(x); }
void PClip::operator=(IClip* x) { OPERATOR_ASSIGN0(x); }
void PClip::OPERATOR_ASSIGN0(IClip* x) { Set(x); }
void PClip::operator=(const PClip& x) { OPERATOR_ASSIGN1(x); }
void PClip::OPERATOR_ASSIGN1(const PClip& x) { Set(x.p); }
PClip::~PClip() { DESTRUCTOR(); }
void PClip::DESTRUCTOR() { if (p) p->Release(); }
// end class PClip
/**********************************************************************/
// class PVideoFrame
void PVideoFrame::Init(VideoFrame* x) {
if (x) x->AddRef();
p=x;
}
void PVideoFrame::Set(VideoFrame* x) {
if (x) x->AddRef();
if (p) p->Release();
p=x;
}
PVideoFrame::PVideoFrame() { CONSTRUCTOR0(); }
void PVideoFrame::CONSTRUCTOR0() { p = 0; }
PVideoFrame::PVideoFrame(const PVideoFrame& x) { CONSTRUCTOR1(x); }
void PVideoFrame::CONSTRUCTOR1(const PVideoFrame& x) { Init(x.p); }
PVideoFrame::PVideoFrame(VideoFrame* x) { CONSTRUCTOR2(x); }
void PVideoFrame::CONSTRUCTOR2(VideoFrame* x) { Init(x); }
void PVideoFrame::operator=(VideoFrame* x) { OPERATOR_ASSIGN0(x); }
void PVideoFrame::OPERATOR_ASSIGN0(VideoFrame* x) { Set(x); }
void PVideoFrame::operator=(const PVideoFrame& x) { OPERATOR_ASSIGN1(x); }
void PVideoFrame::OPERATOR_ASSIGN1(const PVideoFrame& x) { Set(x.p); }
PVideoFrame::~PVideoFrame() { DESTRUCTOR(); }
void PVideoFrame::DESTRUCTOR() { if (p) p->Release(); }
// end class PVideoFrame
/**********************************************************************/
// class AVSValue
AVSValue::AVSValue() { CONSTRUCTOR0(); }
void AVSValue::CONSTRUCTOR0() { type = 'v'; }
AVSValue::AVSValue(IClip* c) { CONSTRUCTOR1(c); }
void AVSValue::CONSTRUCTOR1(IClip* c) { type = 'c'; clip = c; if (c) c->AddRef(); }
AVSValue::AVSValue(const PClip& c) { CONSTRUCTOR2(c); }
void AVSValue::CONSTRUCTOR2(const PClip& c) { type = 'c'; clip = c.GetPointerWithAddRef(); }
AVSValue::AVSValue(bool b) { CONSTRUCTOR3(b); }
void AVSValue::CONSTRUCTOR3(bool b) { type = 'b'; boolean = b; }
AVSValue::AVSValue(int i) { CONSTRUCTOR4(i); }
void AVSValue::CONSTRUCTOR4(int i) { type = 'i'; integer = i; }
AVSValue::AVSValue(float f) { CONSTRUCTOR5(f); }
void AVSValue::CONSTRUCTOR5(float f) { type = 'f'; floating_pt = f; }
AVSValue::AVSValue(double f) { CONSTRUCTOR6(f); }
void AVSValue::CONSTRUCTOR6(double f) { type = 'f'; floating_pt = float(f); }
AVSValue::AVSValue(const char* s) { CONSTRUCTOR7(s); }
void AVSValue::CONSTRUCTOR7(const char* s) { type = 's'; string = s; }
/* Baked ********************
AVSValue::AVSValue(const AVSValue* a, int size) { type = 'a'; array = a; array_size = size; }
Baked ********************/
AVSValue::AVSValue(const AVSValue* a, int size) { CONSTRUCTOR8(a, size); }
void AVSValue::CONSTRUCTOR8(const AVSValue* a, int size) { type = 'a'; array = a; array_size = (short)size; }
AVSValue::AVSValue(const AVSValue& v) { CONSTRUCTOR9(v); }
void AVSValue::CONSTRUCTOR9(const AVSValue& v) { Assign(&v, true); }
AVSValue::~AVSValue() { DESTRUCTOR(); }
void AVSValue::DESTRUCTOR() { if (IsClip() && clip) clip->Release(); }
AVSValue& AVSValue::operator=(const AVSValue& v) { return OPERATOR_ASSIGN(v); }
AVSValue& AVSValue::OPERATOR_ASSIGN(const AVSValue& v) { Assign(&v, false); return *this; }
// Note that we transparently allow 'int' to be treated as 'float'.
// There are no int<->bool conversions, though.
bool AVSValue::Defined() const { return type != 'v'; }
bool AVSValue::IsClip() const { return type == 'c'; }
bool AVSValue::IsBool() const { return type == 'b'; }
bool AVSValue::IsInt() const { return type == 'i'; }
// bool IsLong() const { return (type == 'l'|| type == 'i'); }
bool AVSValue::IsFloat() const { return type == 'f' || type == 'i'; }
bool AVSValue::IsString() const { return type == 's'; }
bool AVSValue::IsArray() const { return type == 'a'; }
PClip AVSValue::AsClip() const { _ASSERTE(IsClip()); return IsClip()?clip:0; }
bool AVSValue::AsBool1() const { _ASSERTE(IsBool()); return boolean; }
bool AVSValue::AsBool() const { return AsBool1(); }
int AVSValue::AsInt1() const { _ASSERTE(IsInt()); return integer; }
int AVSValue::AsInt() const { return AsInt1(); }
// int AsLong() const { _ASSERTE(IsLong()); return IsInt()?integer:longlong; }
const char* AVSValue::AsString1() const { _ASSERTE(IsString()); return IsString()?string:0; }
const char* AVSValue::AsString() const { return AVSValue::AsString1(); }
/* Baked ********************
double AVSValue::AsFloat() const { _ASSERTE(IsFloat()); return IsInt()?integer:floating_pt; }
Baked ********************/
double AVSValue::AsFloat1() const { _ASSERTE(IsFloat()); return IsInt()?integer:floating_pt; }
double AVSValue::AsFloat() const { return AsFloat1(); }
bool AVSValue::AsBool2(bool def) const { _ASSERTE(IsBool()||!Defined()); return IsBool() ? boolean : def; }
bool AVSValue::AsBool(bool def) const { return AsBool2(def); }
int AVSValue::AsInt2(int def) const { _ASSERTE(IsInt()||!Defined()); return IsInt() ? integer : def; }
int AVSValue::AsInt(int def) const { return AsInt2(def); }
/* Baked ********************
double AVSValue::AsFloat(double def) const { _ASSERTE(IsFloat()||!Defined()); return IsInt() ? integer : type=='f' ? floating_pt : def; }
Baked ********************/
double AVSValue::AsDblDef(double def) const { _ASSERTE(IsFloat()||!Defined()); return IsInt() ? integer : type=='f' ? floating_pt : def; }
//float AVSValue::AsFloat(double def) const { _ASSERTE(IsFloat()||!Defined()); return IsInt() ? integer : type=='f' ? floating_pt : (float)def; }
double AVSValue::AsFloat2(float def) const { _ASSERTE(IsFloat()||!Defined()); return IsInt() ? integer : type=='f' ? floating_pt : def; }
double AVSValue::AsFloat(float def) const { return AsFloat2(def); }
const char* AVSValue::AsString2(const char* def) const { _ASSERTE(IsString()||!Defined()); return IsString() ? string : def; }
const char* AVSValue::AsString(const char* def) const { return AVSValue::AsString2(def); }
int AVSValue::ArraySize() const { _ASSERTE(IsArray()); return IsArray()?array_size:1; }
const AVSValue& AVSValue::operator[](int index) const { return OPERATOR_INDEX(index); }
const AVSValue& AVSValue::OPERATOR_INDEX(int index) const {
_ASSERTE(IsArray() && index>=0 && index<array_size);
return (IsArray() && index>=0 && index<array_size) ? array[index] : *this;
}
void AVSValue::Assign(const AVSValue* src, bool init) {
if (src->IsClip() && src->clip)
src->clip->AddRef();
if (!init && IsClip() && clip)
clip->Release();
// make sure this copies the whole struct!
((__int32*)this)[0] = ((__int32*)src)[0];
((__int32*)this)[1] = ((__int32*)src)[1];
}
// end class AVSValue
/**********************************************************************/
AVS_Linkage avs_linkage = { // struct AVS_Linkage {
sizeof(AVS_Linkage), // int Size;
/***************************************************************************************************************/
// struct VideoInfo
&VideoInfo::HasVideo, // bool (VideoInfo::*HasVideo)() const;
&VideoInfo::HasAudio, // bool (VideoInfo::*HasAudio)() const;
&VideoInfo::IsRGB, // bool (VideoInfo::*IsRGB)() const;
&VideoInfo::IsRGB24, // bool (VideoInfo::*IsRGB24)() const;
&VideoInfo::IsRGB32, // bool (VideoInfo::*IsRGB32)() const;
&VideoInfo::IsYUV, // bool (VideoInfo::*IsYUV)() const;
&VideoInfo::IsYUY2, // bool (VideoInfo::*IsYUY2)() const;
&VideoInfo::IsYV24, // bool (VideoInfo::*IsYV24)() const;
&VideoInfo::IsYV16, // bool (VideoInfo::*IsYV16)() const;
&VideoInfo::IsYV12, // bool (VideoInfo::*IsYV12)() const;
&VideoInfo::IsYV411, // bool (VideoInfo::*IsYV411)() const;
&VideoInfo::IsY8, // bool (VideoInfo::*IsY8)() const;
&VideoInfo::IsColorSpace, // bool (VideoInfo::*IsColorSpace)(int c_space) const;
&VideoInfo::Is, // bool (VideoInfo::*Is)(int property) const;
&VideoInfo::IsPlanar, // bool (VideoInfo::*IsPlanar)() const;
&VideoInfo::IsFieldBased, // bool (VideoInfo::*IsFieldBased)() const;
&VideoInfo::IsParityKnown, // bool (VideoInfo::*IsParityKnown)() const;
&VideoInfo::IsBFF, // bool (VideoInfo::*IsBFF)() const;
&VideoInfo::IsTFF, // bool (VideoInfo::*IsTFF)() const;
&VideoInfo::IsVPlaneFirst, // bool (VideoInfo::*IsVPlaneFirst)() const;
&VideoInfo::BytesFromPixels, // int (VideoInfo::*BytesFromPixels)(int pixels) const;
&VideoInfo::RowSize, // int (VideoInfo::*RowSize)(int plane) const;
&VideoInfo::BMPSize, // int (VideoInfo::*BMPSize)() const;
&VideoInfo::AudioSamplesFromFrames, // __int64 (VideoInfo::*AudioSamplesFromFrames)(int frames) const;
&VideoInfo::FramesFromAudioSamples, // int (VideoInfo::*FramesFromAudioSamples)(__int64 samples) const;
&VideoInfo::AudioSamplesFromBytes, // __int64 (VideoInfo::*AudioSamplesFromBytes)(__int64 bytes) const;
&VideoInfo::BytesFromAudioSamples, // __int64 (VideoInfo::*BytesFromAudioSamples)(__int64 samples) const;
&VideoInfo::AudioChannels, // int (VideoInfo::*AudioChannels)() const;
&VideoInfo::SampleType, // int (VideoInfo::*SampleType)() const;
&VideoInfo::IsSampleType, // bool (VideoInfo::*IsSampleType)(int testtype) const;
&VideoInfo::SamplesPerSecond, // int (VideoInfo::*SamplesPerSecond)() const;
&VideoInfo::BytesPerAudioSample, // int (VideoInfo::*BytesPerAudioSample)() const;
&VideoInfo::SetFieldBased, // void (VideoInfo::*SetFieldBased)(bool isfieldbased);
&VideoInfo::Set, // void (VideoInfo::*Set)(int property);
&VideoInfo::Clear, // void (VideoInfo::*Clear)(int property);
&VideoInfo::GetPlaneWidthSubsampling, // int (VideoInfo::*GetPlaneWidthSubsampling)(int plane) const;
&VideoInfo::GetPlaneHeightSubsampling, // int (VideoInfo::*GetPlaneHeightSubsampling)(int plane) const;
&VideoInfo::BitsPerPixel, // int (VideoInfo::*BitsPerPixel)() const;
&VideoInfo::BytesPerChannelSample, // int (VideoInfo::*BytesPerChannelSample)() const;
&VideoInfo::SetFPS, // void (VideoInfo::*SetFPS)(unsigned numerator, unsigned denominator)
&VideoInfo::MulDivFPS, // void (VideoInfo::*MulDivFPS)(unsigned multiplier, unsigned divisor)
&VideoInfo::IsSameColorspace, // bool (VideoInfo::*IsSameColorspace)(const VideoInfo& vi) const;
// end struct VideoInfo
/***************************************************************************************************************/
// class VideoFrameBuffer
&VideoFrameBuffer::GetReadPtr, // const BYTE* (VideoFrameBuffer::*VFBGetReadPtr)() const;
&VideoFrameBuffer::GetWritePtr, // BYTE* (VideoFrameBuffer::*VFBGetWritePtr)();
&VideoFrameBuffer::GetDataSize, // int (VideoFrameBuffer::*GetDataSize)() const;
&VideoFrameBuffer::GetSequenceNumber, // int (VideoFrameBuffer::*GetSequenceNumber)() const;
&VideoFrameBuffer::GetRefcount, // int (VideoFrameBuffer::*GetRefcount)() const;
// end class VideoFrameBuffer
/***************************************************************************************************************/
// class VideoFrame
&VideoFrame::GetPitch, // int (VideoFrame::*GetPitch)(int plane) const;
&VideoFrame::GetRowSize, // int (VideoFrame::*GetRowSize)(int plane) const;
&VideoFrame::GetHeight, // int (VideoFrame::*GetHeight)(int plane) const;
&VideoFrame::GetFrameBuffer, // VideoFrameBuffer* (VideoFrame::*GetFrameBuffer)() const;
&VideoFrame::GetOffset, // int (VideoFrame::*GetOffset)(int plane) const;
&VideoFrame::GetReadPtr, // const BYTE* (VideoFrame::*VFGetReadPtr)(int plane) const;
&VideoFrame::IsWritable, // bool (VideoFrame::*IsWritable)() const;
&VideoFrame::GetWritePtr, // BYTE* (VideoFrame::*VFGetWritePtr)(int plane) const;
&VideoFrame::DESTRUCTOR, // void (VideoFrame::*VideoFrame_DESTRUCTOR)();
// end class VideoFrame
/***************************************************************************************************************/
// class IClip
// /* nothing */
// end class IClip
/***************************************************************************************************************/
// class PClip
&PClip::CONSTRUCTOR0, // void (PClip::*PClip_CONSTRUCTOR0)();
&PClip::CONSTRUCTOR1, // void (PClip::*PClip_CONSTRUCTOR1)(const PClip& x);
&PClip::CONSTRUCTOR2, // void (PClip::*PClip_CONSTRUCTOR2)(IClip* x);
&PClip::OPERATOR_ASSIGN0, // void (PClip::*PClip_OPERATOR_ASSIGN0)(IClip* x);
&PClip::OPERATOR_ASSIGN1, // void (PClip::*PClip_OPERATOR_ASSIGN1)(const PClip& x);
&PClip::DESTRUCTOR, // void (PClip::*PClip_DESTRUCTOR)();
// end class PClip
/***************************************************************************************************************/
// class PVideoFrame
&PVideoFrame::CONSTRUCTOR0, // void (PVideoFrame::*PVideoFrame_CONSTRUCTOR0)();
&PVideoFrame::CONSTRUCTOR1, // void (PVideoFrame::*PVideoFrame_CONSTRUCTOR1)(const PVideoFrame& x);
&PVideoFrame::CONSTRUCTOR2, // void (PVideoFrame::*PVideoFrame_CONSTRUCTOR2)(VideoFrame* x);
&PVideoFrame::OPERATOR_ASSIGN0, // void (PVideoFrame::*PVideoFrame_OPERATOR_ASSIGN0(VideoFrame* x);
&PVideoFrame::OPERATOR_ASSIGN1, // void (PVideoFrame::*PVideoFrame_OPERATOR_ASSIGN1(const PVideoFrame& x);
&PVideoFrame::DESTRUCTOR, // void (PVideoFrame::*PVideoFrame_DESTRUCTOR)();
// end class PVideoFrame
/***************************************************************************************************************/
// class AVSValue
&AVSValue::CONSTRUCTOR0, // void (AVSValue::*AVSValue_CONSTRUCTOR0)();
&AVSValue::CONSTRUCTOR1, // void (AVSValue::*AVSValue_CONSTRUCTOR1)(IClip* c);
&AVSValue::CONSTRUCTOR2, // void (AVSValue::*AVSValue_CONSTRUCTOR2)(const PClip& c);
&AVSValue::CONSTRUCTOR3, // void (AVSValue::*AVSValue_CONSTRUCTOR3)(bool b);
&AVSValue::CONSTRUCTOR4, // void (AVSValue::*AVSValue_CONSTRUCTOR4)(int i);
&AVSValue::CONSTRUCTOR5, // void (AVSValue::*AVSValue_CONSTRUCTOR5)(float f);
&AVSValue::CONSTRUCTOR6, // void (AVSValue::*AVSValue_CONSTRUCTOR6)(double f);
&AVSValue::CONSTRUCTOR7, // void (AVSValue::*AVSValue_CONSTRUCTOR7)(const char* s);
&AVSValue::CONSTRUCTOR8, // void (AVSValue::*AVSValue_CONSTRUCTOR8)(const AVSValue* a, int
&AVSValue::CONSTRUCTOR9, // void (AVSValue::*AVSValue_CONSTRUCTOR9)(const AVSValue& v);
&AVSValue::DESTRUCTOR, // void (AVSValue::*AVSValue_DESTRUCTOR)();
&AVSValue::OPERATOR_ASSIGN, // AVSValue& (AVSValue::*AVSValue_OPERATOR_ASSIGN)(const AVSValue& v);
&AVSValue::OPERATOR_INDEX, // const AVSValue& (AVSValue::*AVSValue_OPERATOR_INDEX)(int index) const;
&AVSValue::Defined, // bool (AVSValue::*Defined)() const;
&AVSValue::IsClip, // bool (AVSValue::*IsClip)() const;
&AVSValue::IsBool, // bool (AVSValue::*IsBool)() const;
&AVSValue::IsInt, // bool (AVSValue::*IsInt)() const;
&AVSValue::IsFloat, // bool (AVSValue::*IsFloat)() const;
&AVSValue::IsString, // bool (AVSValue::*IsString)() const;
&AVSValue::IsArray, // bool (AVSValue::*IsArray)() const;
&AVSValue::AsClip, // PClip (AVSValue::*AsClip)() const;
&AVSValue::AsBool1, // bool (AVSValue::*AsBool1)() const;
&AVSValue::AsInt1, // int (AVSValue::*AsInt1)() const;
&AVSValue::AsString1, // const char* (AVSValue::*AsString1)() const;
&AVSValue::AsFloat1, // double (AVSValue::*AsFloat1)() const;
&AVSValue::AsBool2, // bool (AVSValue::*AsBool2)(bool def) const;
&AVSValue::AsInt2, // int (AVSValue::*AsInt2)(int def) const;
&AVSValue::AsDblDef, // double (AVSValue::*AsDblDef)(double def) const;
&AVSValue::AsFloat2, // double (AVSValue::*AsFloat2)(float def) const;
&AVSValue::AsString2, // const char* (AVSValue::*AsString2)(const char* def) const;
&AVSValue::ArraySize, // int (AVSValue::*ArraySize)() const;
// end class AVSValue
}; // }
//extern __declspec(dllexport) const AVS_Linkage* const AVS_linkage = &avs_linkage;
}; // namespace AVSInterface25
/**********************************************************************/
// in UserPlugin.cpp
#if 0
#include "avisynth.h"
/* New 2.6 requirment!!! */
// Declare and initialise server pointers static storage.
const AVS_Linkage *AVS_linkage = 0;
/* New 2.6 requirment!!! */
// DLL entry point called from LoadPlugin() to setup a user plugin.
extern "C" __declspec(dllexport) const char* __stdcall AvisynthPluginInit3(IScriptEnvironment* env, const AVS_Linkage* const vectors) {
/* New 2.6 requirment!!! */
// Save the server pointers.
AVS_linkage = vectors;
// Add the name of our function
env->AddFunction("Plugin", "c", Create_Plugin, 0);
// Return plugin text identifier.
return "Plugin";
}
#endif
/**********************************************************************/
#include <windows.h>
#include "avisynth.h"
class InvertNeg : public GenericVideoFilter
{
public:
InvertNeg(PClip _child, IScriptEnvironment* env);
PVideoFrame __stdcall GetFrame(int n, IScriptEnvironment* env);
};
InvertNeg::InvertNeg(PClip _child, IScriptEnvironment* env) :
GenericVideoFilter(_child) {
if (!vi.IsPlanar() || !vi.IsYUV()) {
env->ThrowError("InvertNeg: planar YUV data only!");
}
}
PVideoFrame __stdcall InvertNeg::GetFrame(int n, IScriptEnvironment* env) {
PVideoFrame src = child->GetFrame(n, env);
PVideoFrame dst = env->NewVideoFrame(vi);
const unsigned char* srcp;
unsigned char* dstp;
int src_pitch;
int dst_pitch;
int row_size;
int height;
int p, x, y;
if ((vi.IsPlanar()) && (vi.IsYUV())) {
int planes[] = {PLANAR_Y, PLANAR_V, PLANAR_U};
for (p=0; p<3; p++) {
srcp = src->GetReadPtr(planes[p]);
dstp = dst->GetWritePtr(planes[p]);
src_pitch = src->GetPitch(planes[p]);
dst_pitch = dst->GetPitch(planes[p]);
row_size = dst->GetRowSize(planes[p]);
height = dst->GetHeight(planes[p]);
for (y = 0; y < height; y++) {
for (x = 0; x < row_size; x++) {
dstp[x] = srcp[x] ^ 255;
}
srcp += src_pitch;
dstp += dst_pitch;
}
}
return dst;
} else {
return src;
}
}
AVSValue __cdecl Create_InvertNeg(AVSValue args, void* user_data, IScriptEnvironment* env) {
return new InvertNeg(args[0].AsClip(), env);
}
namespace AVSInterface25 {
struct AVS_Linkage;
extern AVS_Linkage avs_linkage;
}
const AVS_Linkage* AVS_linkage = reinterpret_cast<AVS_Linkage*>(&AVSInterface25::avs_linkage);
extern "C" __declspec(dllexport) const char* __stdcall AvisynthPluginInit3(IScriptEnvironment* env, const AVS_Linkage* const vectors) {
AVS_linkage = vectors;
env->AddFunction("InvertNeg", "c", Create_InvertNeg, 0);
return "`InvertNeg' sample plugin";
}
extern "C" __declspec(dllexport) const char* __stdcall AvisynthPluginInit2(IScriptEnvironment* env) {
env->AddFunction("InvertNeg", "c", Create_InvertNeg, 0);
return "`InvertNeg' sample plugin";
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment