]> git.sesse.net Git - x264/commitdiff
Avisynth 2.6 colorspace support
authorSteven Walters <kemuri9@gmail.com>
Sat, 17 Jul 2010 21:43:37 +0000 (17:43 -0400)
committerFiona Glaser <fiona@x264.com>
Sat, 17 Jul 2010 22:16:13 +0000 (15:16 -0700)
Use a customized avisynth_c.h to detect the new planar colorspaces.

configure
extras/avisynth_c.h
input/avs.c

index d8607f8c9f1716d3df56396b8648911221ee4ef6..aeb215afc3b171e0be98220b0207d58b11c82565 100755 (executable)
--- a/configure
+++ b/configure
@@ -648,12 +648,8 @@ if [ "$gpac" = "yes" ] ; then
 fi
 
 if [ "$avs" = "auto" ] ; then
-    avs=no
-    if [ $SYS = MINGW ] && cc_check avisynth_c.h ; then
-        avs="yes"
-        define HAVE_AVS
-        define HAVE_AVISYNTH_C_H
-    elif [ $SYS = MINGW ] && cc_check extras/avisynth_c.h ; then
+    avs="no"
+    if [ $SYS = MINGW ] && cc_check extras/avisynth_c.h ; then
         avs="yes"
         define HAVE_AVS
     fi
index 27e8270808cce56c2545cf9045bf9f4b43605ed3..64deaadfc1c092045f7228691bdc82002318185f 100644 (file)
 // on Avisynth C Interface, such as 3rd-party filters, import and
 // export plugins, or graphical user interfaces.
 
+// NOTE: this is a partial update of the Avisynth C interface to recognize
+// new color spaces added in Avisynth 2.60. By no means is this document
+// completely Avisynth 2.60 compliant.
+
 #ifndef __AVISYNTH_C__
 #define __AVISYNTH_C__
 
 #  define AVSC_CC __stdcall
 #endif
 
-#define AVSC_EXPORT EXTERN_C __declspec(dllexport)
 #define AVSC_INLINE static __inline
+
 #ifdef AVISYNTH_C_EXPORTS
-#  define AVSC_API(ret) EXTERN_C __declspec(dllexport) ret AVSC_CC
+#  define AVSC_EXPORT EXTERN_C
+#  define AVSC_API(ret, name) EXTERN_C __declspec(dllexport) ret AVSC_CC name
 #else
-#  define AVSC_API(ret) EXTERN_C __declspec(dllimport) ret AVSC_CC
+#  define AVSC_EXPORT EXTERN_C __declspec(dllexport)
+#  ifndef AVSC_NO_DECLSPEC
+#    define AVSC_API(ret, name) EXTERN_C __declspec(dllimport) ret AVSC_CC name
+#  else
+#    define AVSC_API(ret, name) typedef ret (AVSC_CC *name##_func)
+#  endif
 #endif
 
 typedef unsigned char BYTE;
@@ -69,7 +79,7 @@ typedef __int64 INT64;
 //
 
 #ifndef __AVISYNTH_H__
-enum { AVISYNTH_INTERFACE_VERSION = 2 };
+enum { AVISYNTH_INTERFACE_VERSION = 4 };
 #endif
 
 enum {AVS_SAMPLE_INT8  = 1<<0,
@@ -84,13 +94,46 @@ enum {AVS_PLANAR_Y=1<<0,
       AVS_PLANAR_ALIGNED=1<<3,
       AVS_PLANAR_Y_ALIGNED=AVS_PLANAR_Y|AVS_PLANAR_ALIGNED,
       AVS_PLANAR_U_ALIGNED=AVS_PLANAR_U|AVS_PLANAR_ALIGNED,
-      AVS_PLANAR_V_ALIGNED=AVS_PLANAR_V|AVS_PLANAR_ALIGNED};
+      AVS_PLANAR_V_ALIGNED=AVS_PLANAR_V|AVS_PLANAR_ALIGNED,
+      AVS_PLANAR_A=1<<4,
+      AVS_PLANAR_R=1<<5,
+      AVS_PLANAR_G=1<<6,
+      AVS_PLANAR_B=1<<7,
+      AVS_PLANAR_A_ALIGNED=AVS_PLANAR_A|AVS_PLANAR_ALIGNED,
+      AVS_PLANAR_R_ALIGNED=AVS_PLANAR_R|AVS_PLANAR_ALIGNED,
+      AVS_PLANAR_G_ALIGNED=AVS_PLANAR_G|AVS_PLANAR_ALIGNED,
+      AVS_PLANAR_B_ALIGNED=AVS_PLANAR_B|AVS_PLANAR_ALIGNED};
 
   // Colorspace properties.
-enum {AVS_CS_BGR = 1<<28,  
+enum {AVS_CS_BGR = 1<<28,
       AVS_CS_YUV = 1<<29,
       AVS_CS_INTERLEAVED = 1<<30,
-      AVS_CS_PLANAR = 1<<31};
+      AVS_CS_PLANAR = 1<<31,
+    
+      AVS_CS_SHIFT_SUB_WIDTH   = 0,
+      AVS_CS_SHIFT_SUB_HEIGHT  = 1 << 3,
+      AVS_CS_SHIFT_SAMPLE_BITS = 1 << 4,
+
+      AVS_CS_SUB_WIDTH_MASK    = 7 << AVS_CS_SHIFT_SUB_WIDTH,
+      AVS_CS_SUB_WIDTH_1       = 3 << AVS_CS_SHIFT_SUB_WIDTH, // YV24
+      AVS_CS_SUB_WIDTH_2       = 0 << AVS_CS_SHIFT_SUB_WIDTH, // YV12, I420, YV16
+      AVS_CS_SUB_WIDTH_4       = 1 << AVS_CS_SHIFT_SUB_WIDTH, // YUV9, YV411
+
+      AVS_CS_VPLANEFIRST       = 1 << 3, // YV12, YV16, YV24, YV411, YUV9
+      AVS_CS_UPLANEFIRST       = 1 << 4, // I420
+
+      AVS_CS_SUB_HEIGHT_MASK   = 7 << AVS_CS_SHIFT_SUB_HEIGHT,
+      AVS_CS_SUB_HEIGHT_1      = 3 << AVS_CS_SHIFT_SUB_HEIGHT, // YV16, YV24, YV411
+      AVS_CS_SUB_HEIGHT_2      = 0 << AVS_CS_SHIFT_SUB_HEIGHT, // YV12, I420
+      AVS_CS_SUB_HEIGHT_4      = 1 << AVS_CS_SHIFT_SUB_HEIGHT, // YUV9
+
+      AVS_CS_SAMPLE_BITS_MASK  = 7 << AVS_CS_SHIFT_SAMPLE_BITS,
+      AVS_CS_SAMPLE_BITS_8     = 0 << AVS_CS_SHIFT_SAMPLE_BITS,
+      AVS_CS_SAMPLE_BITS_16    = 1 << AVS_CS_SHIFT_SAMPLE_BITS,
+      AVS_CS_SAMPLE_BITS_32    = 2 << AVS_CS_SHIFT_SAMPLE_BITS,
+
+      AVS_CS_PLANAR_MASK       = AVS_CS_PLANAR | AVS_CS_INTERLEAVED | AVS_CS_YUV | AVS_CS_BGR | AVS_CS_SAMPLE_BITS_MASK | AVS_CS_SUB_HEIGHT_MASK | AVS_CS_SUB_WIDTH_MASK,
+      AVS_CS_PLANAR_FILTER     = ~( AVS_CS_VPLANEFIRST | AVS_CS_UPLANEFIRST )};
 
   // Specific colorformats
 enum {
@@ -98,9 +141,18 @@ enum {
   AVS_CS_BGR24 = 1<<0 | AVS_CS_BGR | AVS_CS_INTERLEAVED,
   AVS_CS_BGR32 = 1<<1 | AVS_CS_BGR | AVS_CS_INTERLEAVED,
   AVS_CS_YUY2 = 1<<2 | AVS_CS_YUV | AVS_CS_INTERLEAVED,
-  AVS_CS_YV12 = 1<<3 | AVS_CS_YUV | AVS_CS_PLANAR,  // y-v-u, planar
-  AVS_CS_I420 = 1<<4 | AVS_CS_YUV | AVS_CS_PLANAR,  // y-u-v, planar
-  AVS_CS_IYUV = 1<<4 | AVS_CS_YUV | AVS_CS_PLANAR  // same as above
+  //  AVS_CS_YV12  = 1<<3  Reserved
+  //  AVS_CS_I420  = 1<<4  Reserved
+  AVS_CS_RAW32 = 1<<5 | AVS_CS_INTERLEAVED,
+
+  AVS_CS_YV24  = AVS_CS_PLANAR | AVS_CS_YUV | AVS_CS_SAMPLE_BITS_8 | AVS_CS_VPLANEFIRST | AVS_CS_SUB_HEIGHT_1 | AVS_CS_SUB_WIDTH_1,  // YVU 4:4:4 planar
+  AVS_CS_YV16  = AVS_CS_PLANAR | AVS_CS_YUV | AVS_CS_SAMPLE_BITS_8 | AVS_CS_VPLANEFIRST | AVS_CS_SUB_HEIGHT_1 | AVS_CS_SUB_WIDTH_2,  // YVU 4:2:2 planar
+  AVS_CS_YV12  = AVS_CS_PLANAR | AVS_CS_YUV | AVS_CS_SAMPLE_BITS_8 | AVS_CS_VPLANEFIRST | AVS_CS_SUB_HEIGHT_2 | AVS_CS_SUB_WIDTH_2,  // YVU 4:2:0 planar
+  AVS_CS_I420  = AVS_CS_PLANAR | AVS_CS_YUV | AVS_CS_SAMPLE_BITS_8 | AVS_CS_UPLANEFIRST | AVS_CS_SUB_HEIGHT_2 | AVS_CS_SUB_WIDTH_2,  // YUV 4:2:0 planar
+  AVS_CS_IYUV  = AVS_CS_I420,
+  AVS_CS_YV411 = AVS_CS_PLANAR | AVS_CS_YUV | AVS_CS_SAMPLE_BITS_8 | AVS_CS_VPLANEFIRST | AVS_CS_SUB_HEIGHT_1 | AVS_CS_SUB_WIDTH_4,  // YVU 4:1:1 planar
+  AVS_CS_YUV9  = AVS_CS_PLANAR | AVS_CS_YUV | AVS_CS_SAMPLE_BITS_8 | AVS_CS_VPLANEFIRST | AVS_CS_SUB_HEIGHT_4 | AVS_CS_SUB_WIDTH_4,  // YVU 4:1:0 planar
+  AVS_CS_Y8    = AVS_CS_PLANAR | AVS_CS_INTERLEAVED | AVS_CS_YUV | AVS_CS_SAMPLE_BITS_8                                              // Y   4:0:0 planar
 };
 
 enum {
@@ -128,7 +180,12 @@ enum {  //SUBTYPES
 
 enum {
   AVS_CACHE_NOTHING=0,
-  AVS_CACHE_RANGE=1 };
+  AVS_CACHE_RANGE=1,
+  AVS_CACHE_ALL=2,
+  AVS_CACHE_AUDIO=3,
+  AVS_CACHE_AUDIO_NONE=4,
+  AVS_CACHE_AUDIO_AUTO=5
+  };
 
 #define AVS_FRAME_ALIGN 16 
 
@@ -180,17 +237,29 @@ AVSC_INLINE int avs_is_yuv(const AVS_VideoInfo * p)
 AVSC_INLINE int avs_is_yuy2(const AVS_VideoInfo * p) 
         { return (p->pixel_type & AVS_CS_YUY2) == AVS_CS_YUY2; }  
 
+AVSC_INLINE int avs_is_yv24(const AVS_VideoInfo * p)
+        { return (p->pixel_type & AVS_CS_PLANAR_MASK) == (AVS_CS_YV24  & AVS_CS_PLANAR_FILTER); }
+
+AVSC_INLINE int avs_is_yv16(const AVS_VideoInfo * p)
+        { return (p->pixel_type & AVS_CS_PLANAR_MASK) == (AVS_CS_YV16  & AVS_CS_PLANAR_FILTER); }
+
 AVSC_INLINE int avs_is_yv12(const AVS_VideoInfo * p) 
-        { return ((p->pixel_type & AVS_CS_YV12) == AVS_CS_YV12)||((p->pixel_type & AVS_CS_I420) == AVS_CS_I420); }
+        { return (p->pixel_type & AVS_CS_PLANAR_MASK) == (AVS_CS_YV12  & AVS_CS_PLANAR_FILTER); }
 
-AVSC_INLINE int avs_is_color_space(const AVS_VideoInfo * p, int c_space) 
-        { return ((p->pixel_type & c_space) == c_space); }
+AVSC_INLINE int avs_is_yv411(const AVS_VideoInfo * p)
+        { return (p->pixel_type & AVS_CS_PLANAR_MASK) == (AVS_CS_YV411 & AVS_CS_PLANAR_FILTER); }
+       
+AVSC_INLINE int avs_is_y8(const AVS_VideoInfo * p)
+        { return (p->pixel_type & AVS_CS_PLANAR_MASK) == (AVS_CS_Y8    & AVS_CS_PLANAR_FILTER); }
 
 AVSC_INLINE int avs_is_property(const AVS_VideoInfo * p, int property) 
         { return ((p->pixel_type & property)==property ); }
 
 AVSC_INLINE int avs_is_planar(const AVS_VideoInfo * p) 
         { return !!(p->pixel_type & AVS_CS_PLANAR); }
+
+AVSC_INLINE int avs_is_color_space(const AVS_VideoInfo * p, int c_space)
+        { return avs_is_planar(p) ? ((p->pixel_type & AVS_CS_PLANAR_MASK) == (c_space & AVS_CS_PLANAR_FILTER)) : ((p->pixel_type & c_space) == c_space); }
         
 AVSC_INLINE int avs_is_field_based(const AVS_VideoInfo * p) 
         { return !!(p->image_type & AVS_IT_FIELDBASED); }
@@ -304,9 +373,9 @@ typedef struct AVS_VideoFrameBuffer {
   int data_size;
   // sequence_number is incremented every time the buffer is changed, so
   // that stale views can tell they're no longer valid.
-  long sequence_number;
+  volatile long sequence_number;
 
-  long refcount;
+  volatile long refcount;
 } AVS_VideoFrameBuffer;
 
 // VideoFrame holds a "window" into a VideoFrameBuffer.
@@ -314,9 +383,10 @@ typedef struct AVS_VideoFrameBuffer {
 // AVS_VideoFrame is layed out identicly to IVideoFrame
 // DO NOT USE THIS STRUCTURE DIRECTLY
 typedef struct AVS_VideoFrame {
-  int refcount;
+  volatile long refcount;
   AVS_VideoFrameBuffer * vfb;
   int offset, pitch, row_size, height, offsetU, offsetV, pitchUV;  // U&V offsets are from top of picture.
+  int row_sizeUV, heightUV;
 } AVS_VideoFrame;
 
 // Access functions for AVS_VideoFrame
@@ -335,14 +405,14 @@ AVSC_INLINE int avs_get_row_size_p(const AVS_VideoFrame * p, int plane) {
         int r;
     switch (plane) {
     case AVS_PLANAR_U: case AVS_PLANAR_V: 
-                if (p->pitchUV) return p->row_size>>1
+                if (p->pitchUV) return p->row_sizeUV
                 else            return 0;
     case AVS_PLANAR_U_ALIGNED: case AVS_PLANAR_V_ALIGNED: 
                 if (p->pitchUV) { 
-                        int r = ((p->row_size+AVS_FRAME_ALIGN-1)&(~(AVS_FRAME_ALIGN-1)) )>>1; // Aligned rowsize
+                        r = (p->row_sizeUV+AVS_FRAME_ALIGN-1)&(~(AVS_FRAME_ALIGN-1)); // Aligned rowsize
                         if (r < p->pitchUV) 
                                 return r; 
-                        return p->row_size>>1; 
+                        return p->row_sizeUV;
                 } else return 0;
     case AVS_PLANAR_Y_ALIGNED:
                 r = (p->row_size+AVS_FRAME_ALIGN-1)&(~(AVS_FRAME_ALIGN-1)); // Aligned rowsize
@@ -359,7 +429,7 @@ AVSC_INLINE int avs_get_height(const AVS_VideoFrame * p) {
 AVSC_INLINE int avs_get_height_p(const AVS_VideoFrame * p, int plane) {
         switch (plane) {
                 case AVS_PLANAR_U: case AVS_PLANAR_V: 
-                        if (p->pitchUV) return p->height>>1;
+                        if (p->pitchUV) return p->heightUV;
                         return 0;
         }
         return p->height;}
@@ -404,16 +474,16 @@ AVSC_INLINE BYTE* avs_get_write_ptr_p(const AVS_VideoFrame * p, int plane)
 }
 
 
-AVSC_API(void) avs_release_video_frame(AVS_VideoFrame *);
+AVSC_API(void, avs_release_video_frame)(AVS_VideoFrame *);
 // makes a shallow copy of a video frame
-AVSC_API(AVS_VideoFrame *) avs_copy_video_frame(AVS_VideoFrame *);
+AVSC_API(AVS_VideoFrame *, avs_copy_video_frame)(AVS_VideoFrame *);
 
+#ifndef AVSC_NO_DECLSPEC
 AVSC_INLINE void avs_release_frame(AVS_VideoFrame * f)
   {avs_release_video_frame(f);}
 AVSC_INLINE AVS_VideoFrame * avs_copy_frame(AVS_VideoFrame * f)
   {return avs_copy_video_frame(f);}
-
-
+#endif
 
 /////////////////////////////////////////////////////////////////////
 //
@@ -450,8 +520,8 @@ struct AVS_Value {
 // a pointer to NULL
 static const AVS_Value avs_void = {'v'};
 
-AVSC_API(void) avs_copy_value(AVS_Value * dest, AVS_Value src);
-AVSC_API(void) avs_release_value(AVS_Value);
+AVSC_API(void, avs_copy_value)(AVS_Value * dest, AVS_Value src);
+AVSC_API(void, avs_release_value)(AVS_Value);
 
 AVSC_INLINE int avs_defined(AVS_Value v) { return v.type != 'v'; }
 AVSC_INLINE int avs_is_clip(AVS_Value v) { return v.type == 'c'; }
@@ -462,8 +532,8 @@ AVSC_INLINE int avs_is_string(AVS_Value v) { return v.type == 's'; }
 AVSC_INLINE int avs_is_array(AVS_Value v) { return v.type == 'a'; }
 AVSC_INLINE int avs_is_error(AVS_Value v) { return v.type == 'e'; }
 
-AVSC_API(AVS_Clip *) avs_take_clip(AVS_Value, AVS_ScriptEnvironment *);
-AVSC_API(void) avs_set_to_clip(AVS_Value *, AVS_Clip *);
+AVSC_API(AVS_Clip *, avs_take_clip)(AVS_Value, AVS_ScriptEnvironment *);
+AVSC_API(void, avs_set_to_clip)(AVS_Value *, AVS_Clip *);
 
 AVSC_INLINE int avs_as_bool(AVS_Value v) 
         { return v.d.boolean; }   
@@ -494,8 +564,10 @@ AVSC_INLINE AVS_Value avs_new_value_float(float v0)
         { AVS_Value v; v.type = 'f'; v.d.floating_pt = v0; return v;}
 AVSC_INLINE AVS_Value avs_new_value_error(const char * v0) 
         { AVS_Value v; v.type = 'e'; v.d.string = v0; return v; }
+#ifndef AVSC_NO_DECLSPEC
 AVSC_INLINE AVS_Value avs_new_value_clip(AVS_Clip * v0)
         { AVS_Value v; avs_set_to_clip(&v, v0); return v; }
+#endif
 AVSC_INLINE AVS_Value avs_new_value_array(AVS_Value * v0, int size)
         { AVS_Value v; v.type = 'a'; v.d.array = v0; v.array_size = size; return v; }
 
@@ -504,27 +576,27 @@ AVSC_INLINE AVS_Value avs_new_value_array(AVS_Value * v0, int size)
 // AVS_Clip
 //
 
-AVSC_API(void) avs_release_clip(AVS_Clip *);
-AVSC_API(AVS_Clip *) avs_copy_clip(AVS_Clip *);
+AVSC_API(void, avs_release_clip)(AVS_Clip *);
+AVSC_API(AVS_Clip *, avs_copy_clip)(AVS_Clip *);
 
-AVSC_API(const char *) avs_clip_get_error(AVS_Clip *); // return 0 if no error
+AVSC_API(const char *, avs_clip_get_error)(AVS_Clip *); // return 0 if no error
 
-AVSC_API(const AVS_VideoInfo *) avs_get_video_info(AVS_Clip *);
+AVSC_API(const AVS_VideoInfo *, avs_get_video_info)(AVS_Clip *);
 
-AVSC_API(int) avs_get_version(AVS_Clip *);
+AVSC_API(int, avs_get_version)(AVS_Clip *);
  
-AVSC_API(AVS_VideoFrame *) avs_get_frame(AVS_Clip *, int n);
+AVSC_API(AVS_VideoFrame *, avs_get_frame)(AVS_Clip *, int n);
 // The returned video frame must be released with avs_release_video_frame
 
-AVSC_API(int) avs_get_parity(AVS_Clip *, int n); 
+AVSC_API(int, avs_get_parity)(AVS_Clip *, int n); 
 // return field parity if field_based, else parity of first field in frame
 
-AVSC_API(int) avs_get_audio(AVS_Clip *, void * buf, 
-                                  INT64 start, INT64 count); 
+AVSC_API(int, avs_get_audio)(AVS_Clip *, void * buf, 
+                             INT64 start, INT64 count); 
 // start and count are in samples
 
-AVSC_API(int) avs_set_cache_hints(AVS_Clip *, 
-                                        int cachehints, int frame_range);
+AVSC_API(int, avs_set_cache_hints)(AVS_Clip *, 
+                                   int cachehints, int frame_range);
 
 // This is the callback type used by avs_add_function
 typedef AVS_Value (AVSC_CC * AVS_ApplyFunc)
@@ -540,9 +612,9 @@ struct AVS_FilterInfo
   AVS_VideoFrame * (AVSC_CC * get_frame)(AVS_FilterInfo *, int n);
   int (AVSC_CC * get_parity)(AVS_FilterInfo *, int n);
   int (AVSC_CC * get_audio)(AVS_FilterInfo *, void * buf, 
-                                 INT64 start, INT64 count);
+                                  INT64 start, INT64 count);
   int (AVSC_CC * set_cache_hints)(AVS_FilterInfo *, int cachehints, 
-                                       int frame_range);
+                                        int frame_range);
   void (AVSC_CC * free_filter)(AVS_FilterInfo *);
   
   // Should be set when ever there is an error to report.
@@ -559,9 +631,9 @@ struct AVS_FilterInfo
 //    set than ALL methods (the function pointers) must be defined
 // If it is set than you do not need to worry about freeing the child
 //    clip.
-AVSC_API(AVS_Clip *) avs_new_c_filter(AVS_ScriptEnvironment * e,
-                                            AVS_FilterInfo * * fi,
-                                            AVS_Value child, int store_child);
+AVSC_API(AVS_Clip *, avs_new_c_filter)(AVS_ScriptEnvironment * e,
+                                       AVS_FilterInfo * * fi,
+                                       AVS_Value child, int store_child);
 
 /////////////////////////////////////////////////////////////////////
 //
@@ -581,42 +653,48 @@ enum {
   AVS_CPU_3DNOW_EXT    = 0x80,   // Athlon
   AVS_CPU_X86_64       = 0xA0,   // Hammer (note: equiv. to 3DNow + SSE2, 
                                  // which only Hammer will have anyway)
+  AVS_CPUF_SSE3       = 0x100,   //  PIV+, K8 Venice
+  AVS_CPUF_SSSE3      = 0x200,   //  Core 2
+  AVS_CPUF_SSE4       = 0x400,   //  Penryn, Wolfdale, Yorkfield
+  AVS_CPUF_SSE4_1     = 0x400,
+  AVS_CPUF_SSE4_2     = 0x800,   //  Nehalem
 };
 
 
-AVSC_API(long) avs_get_cpu_flags(AVS_ScriptEnvironment *);
-AVSC_API(int) avs_check_version(AVS_ScriptEnvironment *, int version);
+AVSC_API(long, avs_get_cpu_flags)(AVS_ScriptEnvironment *);
+AVSC_API(int, avs_check_version)(AVS_ScriptEnvironment *, int version);
 
-AVSC_API(char *) avs_save_string(AVS_ScriptEnvironment *, const char* s, int length);
-AVSC_API(char *) avs_sprintf(AVS_ScriptEnvironment *, const char * fmt, ...);
+AVSC_API(char *, avs_save_string)(AVS_ScriptEnvironment *, const char* s, int length);
+AVSC_API(char *, avs_sprintf)(AVS_ScriptEnvironment *, const char * fmt, ...);
 
-AVSC_API(char *) avs_vsprintf(AVS_ScriptEnvironment *, const char * fmt, void* val);
+AVSC_API(char *, avs_vsprintf)(AVS_ScriptEnvironment *, const char * fmt, void* val);
  // note: val is really a va_list; I hope everyone typedefs va_list to a pointer
 
-AVSC_API(int) avs_add_function(AVS_ScriptEnvironment *, 
-                                    const char * name, const char * params, 
-                                    AVS_ApplyFunc apply, void * user_data);
+AVSC_API(int, avs_add_function)(AVS_ScriptEnvironment *, 
+                                const char * name, const char * params, 
+                                AVS_ApplyFunc apply, void * user_data);
 
-AVSC_API(int) avs_function_exists(AVS_ScriptEnvironment *, const char * name);
+AVSC_API(int, avs_function_exists)(AVS_ScriptEnvironment *, const char * name);
 
-AVSC_API(AVS_Value) avs_invoke(AVS_ScriptEnvironment *, const char * name, 
+AVSC_API(AVS_Value, avs_invoke)(AVS_ScriptEnvironment *, const char * name, 
                                AVS_Value args, const char** arg_names);
 // The returned value must be be released with avs_release_value
 
-AVSC_API(AVS_Value) avs_get_var(AVS_ScriptEnvironment *, const char* name);
+AVSC_API(AVS_Value, avs_get_var)(AVS_ScriptEnvironment *, const char* name);
 // The returned value must be be released with avs_release_value
 
-AVSC_API(int) avs_set_var(AVS_ScriptEnvironment *, const char* name, AVS_Value val);
+AVSC_API(int, avs_set_var)(AVS_ScriptEnvironment *, const char* name, AVS_Value val);
 
-AVSC_API(int) avs_set_global_var(AVS_ScriptEnvironment *, const char* name, const AVS_Value val);
+AVSC_API(int, avs_set_global_var)(AVS_ScriptEnvironment *, const char* name, const AVS_Value val);
 
 //void avs_push_context(AVS_ScriptEnvironment *, int level=0);
 //void avs_pop_context(AVS_ScriptEnvironment *);
 
-AVSC_API(AVS_VideoFrame *) avs_new_video_frame_a(AVS_ScriptEnvironment *, 
+AVSC_API(AVS_VideoFrame *, avs_new_video_frame_a)(AVS_ScriptEnvironment *, 
                                           const AVS_VideoInfo * vi, int align);
 // align should be at least 16
 
+#ifndef AVSC_NO_DECLSPEC
 AVSC_INLINE 
 AVS_VideoFrame * avs_new_video_frame(AVS_ScriptEnvironment * env, 
                                      const AVS_VideoInfo * vi)
@@ -626,25 +704,26 @@ AVSC_INLINE
 AVS_VideoFrame * avs_new_frame(AVS_ScriptEnvironment * env, 
                                const AVS_VideoInfo * vi)
   {return avs_new_video_frame_a(env,vi,AVS_FRAME_ALIGN);}
+#endif
 
 
-AVSC_API(int) avs_make_writable(AVS_ScriptEnvironment *, AVS_VideoFrame * * pvf);
+AVSC_API(int, avs_make_writable)(AVS_ScriptEnvironment *, AVS_VideoFrame * * pvf);
 
-AVSC_API(void) avs_bit_blt(AVS_ScriptEnvironment *, BYTE* dstp, int dst_pitch, const BYTE* srcp, int src_pitch, int row_size, int height);
+AVSC_API(void, avs_bit_blt)(AVS_ScriptEnvironment *, BYTE* dstp, int dst_pitch, const BYTE* srcp, int src_pitch, int row_size, int height);
 
 typedef void (AVSC_CC *AVS_ShutdownFunc)(void* user_data, AVS_ScriptEnvironment * env);
-AVSC_API(void) avs_at_exit(AVS_ScriptEnvironment *, AVS_ShutdownFunc function, void * user_data);
+AVSC_API(void, avs_at_exit)(AVS_ScriptEnvironment *, AVS_ShutdownFunc function, void * user_data);
 
-AVSC_API(AVS_VideoFrame *) avs_subframe(AVS_ScriptEnvironment *, AVS_VideoFrame * src, int rel_offset, int new_pitch, int new_row_size, int new_height);
+AVSC_API(AVS_VideoFrame *, avs_subframe)(AVS_ScriptEnvironment *, AVS_VideoFrame * src, int rel_offset, int new_pitch, int new_row_size, int new_height);
 // The returned video frame must be be released
 
-AVSC_API(int) avs_set_memory_max(AVS_ScriptEnvironment *, int mem);
+AVSC_API(int, avs_set_memory_max)(AVS_ScriptEnvironment *, int mem);
 
-AVSC_API(int) avs_set_working_dir(AVS_ScriptEnvironment *, const char * newdir);
+AVSC_API(int, avs_set_working_dir)(AVS_ScriptEnvironment *, const char * newdir);
 
 // avisynth.dll exports this; it's a way to use it as a library, without
 // writing an AVS script or without going through AVIFile.
-AVSC_API(AVS_ScriptEnvironment *) avs_create_script_environment(int version);
+AVSC_API(AVS_ScriptEnvironment *, avs_create_script_environment)(int version);
 
 // this symbol is the entry point for the plugin and must
 // be defined
@@ -652,10 +731,146 @@ AVSC_EXPORT
 const char * AVSC_CC avisynth_c_plugin_init(AVS_ScriptEnvironment* env);
 
 
-AVSC_API(void) avs_delete_script_environment(AVS_ScriptEnvironment *);
+AVSC_API(void, avs_delete_script_environment)(AVS_ScriptEnvironment *);
 
 
-AVSC_API(AVS_VideoFrame *) avs_subframe_planar(AVS_ScriptEnvironment *, AVS_VideoFrame * src, int rel_offset, int new_pitch, int new_row_size, int new_height, int rel_offsetU, int rel_offsetV, int new_pitchUV);
+AVSC_API(AVS_VideoFrame *, avs_subframe_planar)(AVS_ScriptEnvironment *, AVS_VideoFrame * src, int rel_offset, int new_pitch, int new_row_size, int new_height, int rel_offsetU, int rel_offsetV, int new_pitchUV);
 // The returned video frame must be be released
 
+#ifdef AVSC_NO_DECLSPEC
+// use LoadLibrary and related functions to dynamically load Avisynth instead of declspec(dllimport)
+/*
+  The following functions needs to have been declared, probably from windows.h
+
+  void* malloc(size_t)
+  void free(void*);
+
+  HMODULE LoadLibrary(const char*);
+  void* GetProcAddress(HMODULE, const char*);
+  FreeLibrary(HMODULE);
+*/
+
+
+typedef struct AVS_Library AVS_Library;
+
+#define AVSC_DECLARE_FUNC(name) name##_func name
+
+struct AVS_Library {
+  HMODULE handle;
+
+  AVSC_DECLARE_FUNC(avs_add_function);
+  AVSC_DECLARE_FUNC(avs_at_exit);
+  AVSC_DECLARE_FUNC(avs_bit_blt);
+  AVSC_DECLARE_FUNC(avs_check_version);
+  AVSC_DECLARE_FUNC(avs_clip_get_error);
+  AVSC_DECLARE_FUNC(avs_copy_clip);
+  AVSC_DECLARE_FUNC(avs_copy_value);
+  AVSC_DECLARE_FUNC(avs_copy_video_frame);
+  AVSC_DECLARE_FUNC(avs_create_script_environment);
+  AVSC_DECLARE_FUNC(avs_delete_script_environment);
+  AVSC_DECLARE_FUNC(avs_function_exists);
+  AVSC_DECLARE_FUNC(avs_get_audio);
+  AVSC_DECLARE_FUNC(avs_get_cpu_flags);
+  AVSC_DECLARE_FUNC(avs_get_frame);
+  AVSC_DECLARE_FUNC(avs_get_parity);
+  AVSC_DECLARE_FUNC(avs_get_var);
+  AVSC_DECLARE_FUNC(avs_get_version);
+  AVSC_DECLARE_FUNC(avs_get_video_info);
+  AVSC_DECLARE_FUNC(avs_invoke);
+  AVSC_DECLARE_FUNC(avs_make_writable);
+  AVSC_DECLARE_FUNC(avs_new_c_filter);
+  AVSC_DECLARE_FUNC(avs_new_video_frame_a);
+  AVSC_DECLARE_FUNC(avs_release_clip);
+  AVSC_DECLARE_FUNC(avs_release_value);
+  AVSC_DECLARE_FUNC(avs_release_video_frame);
+  AVSC_DECLARE_FUNC(avs_save_string);
+  AVSC_DECLARE_FUNC(avs_set_cache_hints);
+  AVSC_DECLARE_FUNC(avs_set_global_var);
+  AVSC_DECLARE_FUNC(avs_set_memory_max);
+  AVSC_DECLARE_FUNC(avs_set_to_clip);
+  AVSC_DECLARE_FUNC(avs_set_var);
+  AVSC_DECLARE_FUNC(avs_set_working_dir);
+  AVSC_DECLARE_FUNC(avs_sprintf);
+  AVSC_DECLARE_FUNC(avs_subframe);
+  AVSC_DECLARE_FUNC(avs_subframe_planar);
+  AVSC_DECLARE_FUNC(avs_take_clip);
+  AVSC_DECLARE_FUNC(avs_vsprintf);
+};
+
+#undef AVSC_DECLARE_FUNC
+
+
+AVSC_INLINE AVS_Library * avs_load_library() {
+  AVS_Library *library = (AVS_Library *)malloc(sizeof(AVS_Library));
+  if (library == NULL)
+    return NULL;
+  library->handle = LoadLibrary("avisynth");
+  if (library->handle == NULL)
+    goto fail;
+
+#define __AVSC_STRINGIFY(x) #x
+#define AVSC_STRINGIFY(x) __AVSC_STRINGIFY(x)
+#define AVSC_LOAD_FUNC(name) {\
+  library->name = (name##_func) GetProcAddress(library->handle, AVSC_STRINGIFY(name));\
+  if (library->name == NULL)\
+    goto fail;\
+}
+
+  AVSC_LOAD_FUNC(avs_add_function);
+  AVSC_LOAD_FUNC(avs_at_exit);
+  AVSC_LOAD_FUNC(avs_bit_blt);
+  AVSC_LOAD_FUNC(avs_check_version);
+  AVSC_LOAD_FUNC(avs_clip_get_error);
+  AVSC_LOAD_FUNC(avs_copy_clip);
+  AVSC_LOAD_FUNC(avs_copy_value);
+  AVSC_LOAD_FUNC(avs_copy_video_frame);
+  AVSC_LOAD_FUNC(avs_create_script_environment);
+  AVSC_LOAD_FUNC(avs_delete_script_environment);
+  AVSC_LOAD_FUNC(avs_function_exists);
+  AVSC_LOAD_FUNC(avs_get_audio);
+  AVSC_LOAD_FUNC(avs_get_cpu_flags);
+  AVSC_LOAD_FUNC(avs_get_frame);
+  AVSC_LOAD_FUNC(avs_get_parity);
+  AVSC_LOAD_FUNC(avs_get_var);
+  AVSC_LOAD_FUNC(avs_get_version);
+  AVSC_LOAD_FUNC(avs_get_video_info);
+  AVSC_LOAD_FUNC(avs_invoke);
+  AVSC_LOAD_FUNC(avs_make_writable);
+  AVSC_LOAD_FUNC(avs_new_c_filter);
+  AVSC_LOAD_FUNC(avs_new_video_frame_a);
+  AVSC_LOAD_FUNC(avs_release_clip);
+  AVSC_LOAD_FUNC(avs_release_value);
+  AVSC_LOAD_FUNC(avs_release_video_frame);
+  AVSC_LOAD_FUNC(avs_save_string);
+  AVSC_LOAD_FUNC(avs_set_cache_hints);
+  AVSC_LOAD_FUNC(avs_set_global_var);
+  AVSC_LOAD_FUNC(avs_set_memory_max);
+  AVSC_LOAD_FUNC(avs_set_to_clip);
+  AVSC_LOAD_FUNC(avs_set_var);
+  AVSC_LOAD_FUNC(avs_set_working_dir);
+  AVSC_LOAD_FUNC(avs_sprintf);
+  AVSC_LOAD_FUNC(avs_subframe);
+  AVSC_LOAD_FUNC(avs_subframe_planar);
+  AVSC_LOAD_FUNC(avs_take_clip);
+  AVSC_LOAD_FUNC(avs_vsprintf);
+
+#undef __AVSC_STRINGIFY
+#undef AVSC_STRINGIFY
+#undef AVSC_LOAD_FUNC
+
+  return library;
+
+fail:
+  free(library);
+  return NULL;
+}
+
+AVSC_INLINE void avs_free_library(AVS_Library *library) {
+  if (library == NULL)
+    return;
+  FreeLibrary(library->handle);
+  free(library);
+}
+#endif
+
 #endif
index 0737b277c076f761a4df0f706e6dd38c4c9d9b37..d511b0d0b997bebefd7d4666caea48308553e139 100644 (file)
 #include <windows.h>
 #define FAIL_IF_ERROR( cond, ... ) FAIL_IF_ERR( cond, "avs", __VA_ARGS__ )
 
-/* the AVS interface currently uses __declspec to link function declarations to their definitions in the dll.
-   this has a side effect of preventing program execution if the avisynth dll is not found,
-   so define __declspec(dllimport) to nothing and work around this */
-#undef __declspec
-#define __declspec(i)
+#define AVSC_NO_DECLSPEC
 #undef EXTERN_C
-
-#if HAVE_AVISYNTH_C_H
-#include <avisynth_c.h>
-#else
 #include "extras/avisynth_c.h"
-#endif
+#define AVSC_DECLARE_FUNC(name) name##_func name
 
 /* AVS uses a versioned interface to control backwards compatibility */
-/* YV12 support is required */
-#define AVS_INTERFACE_YV12 2
-/* when AVS supports other planar colorspaces, a workaround is required */
-#define AVS_INTERFACE_OTHER_PLANAR 5
+/* YV12 support is required, which was added in 2.5 */
+#define AVS_INTERFACE_25 2
 
 #if HAVE_SWSCALE
 #include <libavutil/pixfmt.h>
@@ -63,28 +53,24 @@ typedef struct
     AVS_ScriptEnvironment *env;
     HMODULE library;
     int num_frames;
-    /* declare function pointers for the utilized functions to be loaded without __declspec,
-       as the avisynth header does not compensate for this type of usage */
     struct
     {
-        const char *(__stdcall *avs_clip_get_error)( AVS_Clip *clip );
-        AVS_ScriptEnvironment *(__stdcall *avs_create_script_environment)( int version );
-        void (__stdcall *avs_delete_script_environment)( AVS_ScriptEnvironment *env );
-        AVS_VideoFrame *(__stdcall *avs_get_frame)( AVS_Clip *clip, int n );
-        int (__stdcall *avs_get_version)( AVS_Clip *clip );
-        const AVS_VideoInfo *(__stdcall *avs_get_video_info)( AVS_Clip *clip );
-        int (__stdcall *avs_function_exists)( AVS_ScriptEnvironment *env, const char *name );
-        AVS_Value (__stdcall *avs_invoke)( AVS_ScriptEnvironment *env, const char *name,
-            AVS_Value args, const char **arg_names );
-        void (__stdcall *avs_release_clip)( AVS_Clip *clip );
-        void (__stdcall *avs_release_value)( AVS_Value value );
-        void (__stdcall *avs_release_video_frame)( AVS_VideoFrame *frame );
-        AVS_Clip *(__stdcall *avs_take_clip)( AVS_Value, AVS_ScriptEnvironment *env );
+        AVSC_DECLARE_FUNC( avs_clip_get_error );
+        AVSC_DECLARE_FUNC( avs_create_script_environment );
+        AVSC_DECLARE_FUNC( avs_delete_script_environment );
+        AVSC_DECLARE_FUNC( avs_get_frame );
+        AVSC_DECLARE_FUNC( avs_get_video_info );
+        AVSC_DECLARE_FUNC( avs_function_exists );
+        AVSC_DECLARE_FUNC( avs_invoke );
+        AVSC_DECLARE_FUNC( avs_release_clip );
+        AVSC_DECLARE_FUNC( avs_release_value );
+        AVSC_DECLARE_FUNC( avs_release_video_frame );
+        AVSC_DECLARE_FUNC( avs_take_clip );
     } func;
 } avs_hnd_t;
 
 /* load the library and functions we require from it */
-static int avs_load_library( avs_hnd_t *h )
+static int x264_avs_load_library( avs_hnd_t *h )
 {
     h->library = LoadLibrary( "avisynth" );
     if( !h->library )
@@ -93,7 +79,6 @@ static int avs_load_library( avs_hnd_t *h )
     LOAD_AVS_FUNC( avs_create_script_environment, 0 );
     LOAD_AVS_FUNC( avs_delete_script_environment, 1 );
     LOAD_AVS_FUNC( avs_get_frame, 0 );
-    LOAD_AVS_FUNC( avs_get_version, 0 );
     LOAD_AVS_FUNC( avs_get_video_info, 0 );
     LOAD_AVS_FUNC( avs_function_exists, 0 );
     LOAD_AVS_FUNC( avs_invoke, 0 );
@@ -142,8 +127,8 @@ static int open_file( char *psz_filename, hnd_t *p_handle, video_info_t *info, c
     avs_hnd_t *h = malloc( sizeof(avs_hnd_t) );
     if( !h )
         return -1;
-    FAIL_IF_ERROR( avs_load_library( h ), "failed to load avisynth\n" )
-    h->env = h->func.avs_create_script_environment( AVS_INTERFACE_YV12 );
+    FAIL_IF_ERROR( x264_avs_load_library( h ), "failed to load avisynth\n" )
+    h->env = h->func.avs_create_script_environment( AVS_INTERFACE_25 );
     FAIL_IF_ERROR( !h->env, "failed to initiate avisynth\n" )
     AVS_Value arg = avs_new_value_string( psz_filename );
     AVS_Value res;
@@ -196,7 +181,6 @@ static int open_file( char *psz_filename, hnd_t *p_handle, video_info_t *info, c
     }
     FAIL_IF_ERROR( !avs_is_clip( res ), "`%s' didn't return a video clip\n", psz_filename )
     h->clip = h->func.avs_take_clip( res, h->env );
-    int avs_version = h->func.avs_get_version( h->clip );
     const AVS_VideoInfo *vi = h->func.avs_get_video_info( h->clip );
     FAIL_IF_ERROR( !avs_has_video( vi ), "`%s' has no video data\n", psz_filename )
     /* if the clip is made of fields instead of frames, call weave to make them frames */
@@ -209,20 +193,11 @@ static int open_file( char *psz_filename, hnd_t *p_handle, video_info_t *info, c
         info->interlaced = 1;
         info->tff = avs_is_tff( vi );
     }
-    /* if swscale is available, convert CSPs with it rather than with avisynth. */
-#if HAVE_SWSCALE
-    int convert_to_yv12 = 0;
-#else
-    int convert_to_yv12 = !avs_is_yv12( vi );
-#endif
-    /* always call ConvertToYV12 to convert non YV12 planar colorspaces to YV12 when user's AVS supports them,
-       as all planar colorspaces are flagged as YV12. If it is already YV12 in this case, the call does nothing */
-    if( convert_to_yv12 || (avs_version >= AVS_INTERFACE_OTHER_PLANAR && avs_is_yv12( vi )) )
+#if !HAVE_SWSCALE
+    /* if swscale is not available, convert CSPs to yv12 */
+    if( !avs_is_yv12( vi ) )
     {
-        if( convert_to_yv12 )
-            x264_cli_log( "avs", X264_LOG_WARNING, "converting input clip to YV12" );
-        else
-            x264_cli_log( "avs", X264_LOG_INFO, "avisynth 2.6+ detected, forcing conversion to YV12" );
+        x264_cli_log( "avs", X264_LOG_WARNING, "converting input clip to YV12\n" );
         FAIL_IF_ERROR( vi->width&1 || vi->height&1, "input clip width or height not divisible by 2 (%dx%d)\n", vi->width, vi->height )
         const char *arg_name[2] = { NULL, "interlaced" };
         AVS_Value arg_arr[2] = { res, avs_new_value_bool( info->interlaced ) };
@@ -230,6 +205,7 @@ static int open_file( char *psz_filename, hnd_t *p_handle, video_info_t *info, c
         FAIL_IF_ERROR( avs_is_error( res2 ), "couldn't convert input clip to YV12\n" )
         res = update_clip( h, &vi, res2, res );
     }
+#endif
     h->func.avs_release_value( res );
 
     info->width   = vi->width;
@@ -245,8 +221,18 @@ static int open_file( char *psz_filename, hnd_t *p_handle, video_info_t *info, c
         info->csp = X264_CSP_BGR | X264_CSP_VFLIP;
     else if( avs_is_yuy2( vi ) )
         info->csp = PIX_FMT_YUYV422 | X264_CSP_OTHER;
-    else /* yv12 */
-        info->csp = X264_CSP_I420;
+    else if( avs_is_yv24( vi ) )
+        info->csp = X264_CSP_I444;
+    else if( avs_is_yv16( vi ) )
+        info->csp = X264_CSP_I422;
+    else if( avs_is_yv12( vi ) )
+         info->csp = X264_CSP_I420;
+    else if( avs_is_yv411( vi ) )
+        info->csp = PIX_FMT_YUV411P | X264_CSP_OTHER;
+    else if( avs_is_y8( vi ) )
+        info->csp = PIX_FMT_GRAY8 | X264_CSP_OTHER;
+    else
+        info->csp = X264_CSP_NONE;
 #else
     info->csp = X264_CSP_I420;
 #endif
@@ -262,7 +248,14 @@ static int picture_alloc( cli_pic_t *pic, int csp, int width, int height )
         return -1;
     pic->img.csp = csp;
     const x264_cli_csp_t *cli_csp = x264_cli_get_csp( csp );
-    pic->img.planes = cli_csp ? cli_csp->planes : 1;
+    if( cli_csp )
+        pic->img.planes = cli_csp->planes;
+#if HAVE_SWSCALE
+    else if( csp == (PIX_FMT_YUV411P | X264_CSP_OTHER) )
+        pic->img.planes = 3;
+    else
+        pic->img.planes = 1; //y8 and yuy2 are one plane
+#endif
     return 0;
 }