]> git.sesse.net Git - x264/blob - input/avs.c
Bump dates to 2012
[x264] / input / avs.c
1 /*****************************************************************************
2  * avs.c: avisynth input
3  *****************************************************************************
4  * Copyright (C) 2009-2012 x264 project
5  *
6  * Authors: Steven Walters <kemuri9@gmail.com>
7  *
8  * This program is free software; you can redistribute it and/or modify
9  * it under the terms of the GNU General Public License as published by
10  * the Free Software Foundation; either version 2 of the License, or
11  * (at your option) any later version.
12  *
13  * This program is distributed in the hope that it will be useful,
14  * but WITHOUT ANY WARRANTY; without even the implied warranty of
15  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
16  * GNU General Public License for more details.
17  *
18  * You should have received a copy of the GNU General Public License
19  * along with this program; if not, write to the Free Software
20  * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02111, USA.
21  *
22  * This program is also available under a commercial proprietary license.
23  * For more information, contact us at licensing@x264.com.
24  *****************************************************************************/
25
26 #include "input.h"
27 #include <windows.h>
28 #define FAIL_IF_ERROR( cond, ... ) FAIL_IF_ERR( cond, "avs", __VA_ARGS__ )
29
30 #define AVSC_NO_DECLSPEC
31 #undef EXTERN_C
32 #include "extras/avisynth_c.h"
33 #define AVSC_DECLARE_FUNC(name) name##_func name
34
35 /* AVS uses a versioned interface to control backwards compatibility */
36 /* YV12 support is required, which was added in 2.5 */
37 #define AVS_INTERFACE_25 2
38
39 #if HAVE_SWSCALE
40 #include <libavutil/pixfmt.h>
41 #endif
42
43 /* maximum size of the sequence of filters to try on non script files */
44 #define AVS_MAX_SEQUENCE 5
45
46 #define LOAD_AVS_FUNC(name, continue_on_fail)\
47 {\
48     h->func.name = (void*)GetProcAddress( h->library, #name );\
49     if( !continue_on_fail && !h->func.name )\
50         goto fail;\
51 }
52
53 typedef struct
54 {
55     AVS_Clip *clip;
56     AVS_ScriptEnvironment *env;
57     HMODULE library;
58     int num_frames;
59     struct
60     {
61         AVSC_DECLARE_FUNC( avs_clip_get_error );
62         AVSC_DECLARE_FUNC( avs_create_script_environment );
63         AVSC_DECLARE_FUNC( avs_delete_script_environment );
64         AVSC_DECLARE_FUNC( avs_get_error );
65         AVSC_DECLARE_FUNC( avs_get_frame );
66         AVSC_DECLARE_FUNC( avs_get_video_info );
67         AVSC_DECLARE_FUNC( avs_function_exists );
68         AVSC_DECLARE_FUNC( avs_invoke );
69         AVSC_DECLARE_FUNC( avs_release_clip );
70         AVSC_DECLARE_FUNC( avs_release_value );
71         AVSC_DECLARE_FUNC( avs_release_video_frame );
72         AVSC_DECLARE_FUNC( avs_take_clip );
73     } func;
74 } avs_hnd_t;
75
76 /* load the library and functions we require from it */
77 static int x264_avs_load_library( avs_hnd_t *h )
78 {
79     h->library = LoadLibrary( "avisynth" );
80     if( !h->library )
81         return -1;
82     LOAD_AVS_FUNC( avs_clip_get_error, 0 );
83     LOAD_AVS_FUNC( avs_create_script_environment, 0 );
84     LOAD_AVS_FUNC( avs_delete_script_environment, 1 );
85     LOAD_AVS_FUNC( avs_get_error, 1 );
86     LOAD_AVS_FUNC( avs_get_frame, 0 );
87     LOAD_AVS_FUNC( avs_get_video_info, 0 );
88     LOAD_AVS_FUNC( avs_function_exists, 0 );
89     LOAD_AVS_FUNC( avs_invoke, 0 );
90     LOAD_AVS_FUNC( avs_release_clip, 0 );
91     LOAD_AVS_FUNC( avs_release_value, 0 );
92     LOAD_AVS_FUNC( avs_release_video_frame, 0 );
93     LOAD_AVS_FUNC( avs_take_clip, 0 );
94     return 0;
95 fail:
96     FreeLibrary( h->library );
97     return -1;
98 }
99
100 /* generate a filter sequence to try based on the filename extension */
101 static void avs_build_filter_sequence( char *filename_ext, const char *filter[AVS_MAX_SEQUENCE+1] )
102 {
103     int i = 0;
104     const char *all_purpose[] = { "FFmpegSource2", "DSS2", "DirectShowSource", 0 };
105     if( !strcasecmp( filename_ext, "avi" ) )
106         filter[i++] = "AVISource";
107     if( !strcasecmp( filename_ext, "d2v" ) )
108         filter[i++] = "MPEG2Source";
109     if( !strcasecmp( filename_ext, "dga" ) )
110         filter[i++] = "AVCSource";
111     for( int j = 0; all_purpose[j] && i < AVS_MAX_SEQUENCE; j++ )
112         filter[i++] = all_purpose[j];
113 }
114
115 static AVS_Value update_clip( avs_hnd_t *h, const AVS_VideoInfo **vi, AVS_Value res, AVS_Value release )
116 {
117     h->func.avs_release_clip( h->clip );
118     h->clip = h->func.avs_take_clip( res, h->env );
119     h->func.avs_release_value( release );
120     *vi = h->func.avs_get_video_info( h->clip );
121     return res;
122 }
123
124 static float get_avs_version( avs_hnd_t *h )
125 {
126     FAIL_IF_ERROR( !h->func.avs_function_exists( h->env, "VersionNumber" ), "VersionNumber does not exist\n" )
127     AVS_Value ver = h->func.avs_invoke( h->env, "VersionNumber", avs_new_value_array( NULL, 0 ), NULL );
128     FAIL_IF_ERROR( avs_is_error( ver ), "unable to determine avisynth version: %s\n", avs_as_error( ver ) )
129     FAIL_IF_ERROR( !avs_is_float( ver ), "VersionNumber did not return a float value\n" );
130     float ret = avs_as_float( ver );
131     h->func.avs_release_value( ver );
132     return ret;
133 }
134
135 static int open_file( char *psz_filename, hnd_t *p_handle, video_info_t *info, cli_input_opt_t *opt )
136 {
137     FILE *fh = fopen( psz_filename, "r" );
138     if( !fh )
139         return -1;
140     FAIL_IF_ERROR( !x264_is_regular_file( fh ), "AVS input is incompatible with non-regular file `%s'\n", psz_filename );
141     fclose( fh );
142
143     avs_hnd_t *h = malloc( sizeof(avs_hnd_t) );
144     if( !h )
145         return -1;
146     FAIL_IF_ERROR( x264_avs_load_library( h ), "failed to load avisynth\n" )
147     h->env = h->func.avs_create_script_environment( AVS_INTERFACE_25 );
148     if( h->func.avs_get_error )
149     {
150         const char *error = h->func.avs_get_error( h->env );
151         FAIL_IF_ERROR( error, "%s\n", error );
152     }
153     float avs_version = get_avs_version( h );
154     if( avs_version <= 0 )
155         return -1;
156     x264_cli_log( "avs", X264_LOG_DEBUG, "using avisynth version %.2f\n", avs_version );
157     AVS_Value arg = avs_new_value_string( psz_filename );
158     AVS_Value res;
159     char *filename_ext = get_filename_extension( psz_filename );
160
161     if( !strcasecmp( filename_ext, "avs" ) )
162     {
163         res = h->func.avs_invoke( h->env, "Import", arg, NULL );
164         FAIL_IF_ERROR( avs_is_error( res ), "%s\n", avs_as_string( res ) )
165         /* check if the user is using a multi-threaded script and apply distributor if necessary.
166            adapted from avisynth's vfw interface */
167         AVS_Value mt_test = h->func.avs_invoke( h->env, "GetMTMode", avs_new_value_bool( 0 ), NULL );
168         int mt_mode = avs_is_int( mt_test ) ? avs_as_int( mt_test ) : 0;
169         h->func.avs_release_value( mt_test );
170         if( mt_mode > 0 && mt_mode < 5 )
171         {
172             AVS_Value temp = h->func.avs_invoke( h->env, "Distributor", res, NULL );
173             h->func.avs_release_value( res );
174             res = temp;
175         }
176     }
177     else /* non script file */
178     {
179         /* cycle through known source filters to find one that works */
180         const char *filter[AVS_MAX_SEQUENCE+1] = { 0 };
181         avs_build_filter_sequence( filename_ext, filter );
182         int i;
183         for( i = 0; filter[i]; i++ )
184         {
185             x264_cli_log( "avs", X264_LOG_INFO, "trying %s... ", filter[i] );
186             if( !h->func.avs_function_exists( h->env, filter[i] ) )
187             {
188                 x264_cli_printf( X264_LOG_INFO, "not found\n" );
189                 continue;
190             }
191             if( !strncasecmp( filter[i], "FFmpegSource", 12 ) )
192             {
193                 x264_cli_printf( X264_LOG_INFO, "indexing... " );
194                 fflush( stderr );
195             }
196             res = h->func.avs_invoke( h->env, filter[i], arg, NULL );
197             if( !avs_is_error( res ) )
198             {
199                 x264_cli_printf( X264_LOG_INFO, "succeeded\n" );
200                 break;
201             }
202             x264_cli_printf( X264_LOG_INFO, "failed\n" );
203         }
204         FAIL_IF_ERROR( !filter[i], "unable to find source filter to open `%s'\n", psz_filename )
205     }
206     FAIL_IF_ERROR( !avs_is_clip( res ), "`%s' didn't return a video clip\n", psz_filename )
207     h->clip = h->func.avs_take_clip( res, h->env );
208     const AVS_VideoInfo *vi = h->func.avs_get_video_info( h->clip );
209     FAIL_IF_ERROR( !avs_has_video( vi ), "`%s' has no video data\n", psz_filename )
210     /* if the clip is made of fields instead of frames, call weave to make them frames */
211     if( avs_is_field_based( vi ) )
212     {
213         x264_cli_log( "avs", X264_LOG_WARNING, "detected fieldbased (separated) input, weaving to frames\n" );
214         AVS_Value tmp = h->func.avs_invoke( h->env, "Weave", res, NULL );
215         FAIL_IF_ERROR( avs_is_error( tmp ), "couldn't weave fields into frames\n" )
216         res = update_clip( h, &vi, tmp, res );
217         info->interlaced = 1;
218         info->tff = avs_is_tff( vi );
219     }
220 #if !HAVE_SWSCALE
221     /* if swscale is not available, convert the CSP if necessary */
222     if( (opt->output_csp == X264_CSP_I420 && !avs_is_yv12( vi )) || (opt->output_csp == X264_CSP_I422 && !avs_is_yv16( vi )) ||
223         (opt->output_csp == X264_CSP_I444 && !avs_is_yv24( vi )) || (opt->output_csp == X264_CSP_RGB && !avs_is_rgb( vi )) )
224     {
225         FAIL_IF_ERROR( avs_version < 2.6f && (opt->output_csp == X264_CSP_I422 || opt->output_csp == X264_CSP_I444),
226                        "avisynth >= 2.6 is required for i422/i444 output\n" )
227
228         const char *csp = opt->output_csp == X264_CSP_I420 ? "YV12" :
229                           opt->output_csp == X264_CSP_I422 ? "YV16" :
230                           opt->output_csp == X264_CSP_I444 ? "YV24" : "RGB";
231         x264_cli_log( "avs", X264_LOG_WARNING, "converting input clip to %s\n", csp );
232         FAIL_IF_ERROR( opt->output_csp < X264_CSP_I444 && (vi->width&1),
233                        "input clip width not divisible by 2 (%dx%d)\n", vi->width, vi->height )
234         FAIL_IF_ERROR( opt->output_csp == X264_CSP_I420 && info->interlaced && (vi->height&3),
235                        "input clip height not divisible by 4 (%dx%d)\n", vi->width, vi->height )
236         FAIL_IF_ERROR( (opt->output_csp == X264_CSP_I420 || info->interlaced) && (vi->height&1),
237                        "input clip height not divisible by 2 (%dx%d)\n", vi->width, vi->height )
238         char conv_func[14] = { "ConvertTo" };
239         strcat( conv_func, csp );
240         char matrix[7] = "";
241         int arg_count = 2;
242         /* if doing a rgb <-> yuv conversion then range is handled via 'matrix'. though it's only supported in 2.56+ */
243         if( avs_version >= 2.56f && ((opt->output_csp == X264_CSP_RGB && avs_is_yuv( vi )) || (opt->output_csp != X264_CSP_RGB && avs_is_rgb( vi ))) )
244         {
245             // if converting from yuv, then we specify the matrix for the input, otherwise use the output's.
246             int use_pc_matrix = avs_is_yuv( vi ) ? opt->input_range == RANGE_PC : opt->output_range == RANGE_PC;
247             strcpy( matrix, use_pc_matrix ? "PC." : "Rec" );
248             strcat( matrix, "601" ); /* FIXME: use correct coefficients */
249             arg_count++;
250             // notification that the input range has changed to the desired one
251             opt->input_range = opt->output_range;
252         }
253         const char *arg_name[] = { NULL, "interlaced", "matrix" };
254         AVS_Value arg_arr[] = { res, avs_new_value_bool( info->interlaced ), avs_new_value_string( matrix ) };
255         AVS_Value res2 = h->func.avs_invoke( h->env, conv_func, avs_new_value_array( arg_arr, arg_count ), arg_name );
256         FAIL_IF_ERROR( avs_is_error( res2 ), "couldn't convert input clip to %s\n", csp )
257         res = update_clip( h, &vi, res2, res );
258     }
259     /* if swscale is not available, change the range if necessary. This only applies to YUV-based CSPs however */
260     if( avs_is_yuv( vi ) && opt->output_range != RANGE_AUTO && ((opt->input_range == RANGE_PC) != opt->output_range) )
261     {
262         const char *levels = opt->output_range ? "TV->PC" : "PC->TV";
263         x264_cli_log( "avs", X264_LOG_WARNING, "performing %s conversion\n", levels );
264         AVS_Value arg_arr[] = { res, avs_new_value_string( levels ) };
265         const char *arg_name[] = { NULL, "levels" };
266         AVS_Value res2 = h->func.avs_invoke( h->env, "ColorYUV", avs_new_value_array( arg_arr, 2 ), arg_name );
267         FAIL_IF_ERROR( avs_is_error( res2 ), "couldn't convert range: %s\n", avs_as_error( res2 ) )
268         res = update_clip( h, &vi, res2, res );
269         // notification that the input range has changed to the desired one
270         opt->input_range = opt->output_range;
271     }
272 #endif
273     h->func.avs_release_value( res );
274
275     info->width   = vi->width;
276     info->height  = vi->height;
277     info->fps_num = vi->fps_numerator;
278     info->fps_den = vi->fps_denominator;
279     h->num_frames = info->num_frames = vi->num_frames;
280     info->thread_safe = 1;
281     if( avs_is_rgb32( vi ) )
282         info->csp = X264_CSP_BGRA | X264_CSP_VFLIP;
283     else if( avs_is_rgb24( vi ) )
284         info->csp = X264_CSP_BGR | X264_CSP_VFLIP;
285     else if( avs_is_yv24( vi ) )
286         info->csp = X264_CSP_I444;
287     else if( avs_is_yv16( vi ) )
288         info->csp = X264_CSP_I422;
289     else if( avs_is_yv12( vi ) )
290         info->csp = X264_CSP_I420;
291 #if HAVE_SWSCALE
292     else if( avs_is_yuy2( vi ) )
293         info->csp = PIX_FMT_YUYV422 | X264_CSP_OTHER;
294     else if( avs_is_yv411( vi ) )
295         info->csp = PIX_FMT_YUV411P | X264_CSP_OTHER;
296     else if( avs_is_y8( vi ) )
297         info->csp = PIX_FMT_GRAY8 | X264_CSP_OTHER;
298 #endif
299     else
300         info->csp = X264_CSP_NONE;
301     info->vfr = 0;
302
303     *p_handle = h;
304     return 0;
305 }
306
307 static int picture_alloc( cli_pic_t *pic, int csp, int width, int height )
308 {
309     if( x264_cli_pic_alloc( pic, X264_CSP_NONE, width, height ) )
310         return -1;
311     pic->img.csp = csp;
312     const x264_cli_csp_t *cli_csp = x264_cli_get_csp( csp );
313     if( cli_csp )
314         pic->img.planes = cli_csp->planes;
315 #if HAVE_SWSCALE
316     else if( csp == (PIX_FMT_YUV411P | X264_CSP_OTHER) )
317         pic->img.planes = 3;
318     else
319         pic->img.planes = 1; //y8 and yuy2 are one plane
320 #endif
321     return 0;
322 }
323
324 static int read_frame( cli_pic_t *pic, hnd_t handle, int i_frame )
325 {
326     static const int plane[3] = { AVS_PLANAR_Y, AVS_PLANAR_U, AVS_PLANAR_V };
327     avs_hnd_t *h = handle;
328     if( i_frame >= h->num_frames )
329         return -1;
330     AVS_VideoFrame *frm = pic->opaque = h->func.avs_get_frame( h->clip, i_frame );
331     const char *err = h->func.avs_clip_get_error( h->clip );
332     FAIL_IF_ERROR( err, "%s occurred while reading frame %d\n", err, i_frame )
333     for( int i = 0; i < pic->img.planes; i++ )
334     {
335         /* explicitly cast away the const attribute to avoid a warning */
336         pic->img.plane[i] = (uint8_t*)avs_get_read_ptr_p( frm, plane[i] );
337         pic->img.stride[i] = avs_get_pitch_p( frm, plane[i] );
338     }
339     return 0;
340 }
341
342 static int release_frame( cli_pic_t *pic, hnd_t handle )
343 {
344     avs_hnd_t *h = handle;
345     h->func.avs_release_video_frame( pic->opaque );
346     return 0;
347 }
348
349 static void picture_clean( cli_pic_t *pic )
350 {
351     memset( pic, 0, sizeof(cli_pic_t) );
352 }
353
354 static int close_file( hnd_t handle )
355 {
356     avs_hnd_t *h = handle;
357     h->func.avs_release_clip( h->clip );
358     if( h->func.avs_delete_script_environment )
359         h->func.avs_delete_script_environment( h->env );
360     FreeLibrary( h->library );
361     free( h );
362     return 0;
363 }
364
365 const cli_input_t avs_input = { open_file, picture_alloc, read_frame, release_frame, picture_clean, close_file };