From 7f0ef681aa92c585fcb3534b370c7ac60e4866ec Mon Sep 17 00:00:00 2001
From: Steven Walters <kemuri9@gmail.com>
Date: Thu, 10 Dec 2009 19:48:51 -0800
Subject: [PATCH] Avisynth-MT and 2.6 compatibility fixes Explain to the user
 why YV12 conversion is forced with Avisynth 2.6. Fix encoding with
 Avisynth-MT scripts by inserting the necessary Distributor() call; speeds
 such scripts back up to expected levels.

---
 input/avs.c | 14 +++++++++++++-
 1 file changed, 13 insertions(+), 1 deletion(-)

diff --git a/input/avs.c b/input/avs.c
index b2ddc430..a88b86ef 100644
--- a/input/avs.c
+++ b/input/avs.c
@@ -155,6 +155,17 @@ static int open_file( char *psz_filename, hnd_t *p_handle, x264_param_t *p_param
             fprintf( stderr, "avs [error]: %s\n", avs_as_string( res ) );
             return -1;
         }
+        /* check if the user is using a multi-threaded script and apply distributor if necessary.
+           adapted from avisynth's vfw interface */
+        AVS_Value mt_test = h->func.avs_invoke( h->env, "GetMTMode", avs_new_value_bool( 0 ), NULL );
+        int mt_mode = avs_is_int( mt_test ) ? avs_as_int( mt_test ) : 0;
+        h->func.avs_release_value( mt_test );
+        if( mt_mode > 0 && mt_mode < 5 )
+        {
+            AVS_Value temp = h->func.avs_invoke( h->env, "Distributor", res, NULL );
+            h->func.avs_release_value( res );
+            res = temp;
+        }
     }
     else /* non script file */
     {
@@ -213,7 +224,8 @@ static int open_file( char *psz_filename, hnd_t *p_handle, x264_param_t *p_param
     if( !avs_is_yv12( vi ) || avs_version >= AVS_INTERFACE_OTHER_PLANAR )
     {
         h->func.avs_release_clip( h->clip );
-        fprintf( stderr, "avs [warning]: converting input clip to YV12\n" );
+        fprintf( stderr, "avs %s\n", !avs_is_yv12( vi ) ? "[warning]: converting input clip to YV12"
+               : "[info]: Avisynth 2.6+ detected, forcing conversion to YV12" );
         const char *arg_name[2] = { NULL, "interlaced" };
         AVS_Value arg_arr[2] = { res, avs_new_value_bool( p_param->b_interlaced ) };
         AVS_Value res2 = h->func.avs_invoke( h->env, "ConvertToYV12", avs_new_value_array( arg_arr, 2 ), arg_name );
-- 
2.40.0