]> git.sesse.net Git - x264/blob - input/avs.c
Use a 16-bit buffer in hpel_filter regardless of bit depth
[x264] / input / avs.c
1 /*****************************************************************************
2  * avs.c: avisynth input
3  *****************************************************************************
4  * Copyright (C) 2009-2010 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_frame );
65         AVSC_DECLARE_FUNC( avs_get_video_info );
66         AVSC_DECLARE_FUNC( avs_function_exists );
67         AVSC_DECLARE_FUNC( avs_invoke );
68         AVSC_DECLARE_FUNC( avs_release_clip );
69         AVSC_DECLARE_FUNC( avs_release_value );
70         AVSC_DECLARE_FUNC( avs_release_video_frame );
71         AVSC_DECLARE_FUNC( avs_take_clip );
72     } func;
73 } avs_hnd_t;
74
75 /* load the library and functions we require from it */
76 static int x264_avs_load_library( avs_hnd_t *h )
77 {
78     h->library = LoadLibrary( "avisynth" );
79     if( !h->library )
80         return -1;
81     LOAD_AVS_FUNC( avs_clip_get_error, 0 );
82     LOAD_AVS_FUNC( avs_create_script_environment, 0 );
83     LOAD_AVS_FUNC( avs_delete_script_environment, 1 );
84     LOAD_AVS_FUNC( avs_get_frame, 0 );
85     LOAD_AVS_FUNC( avs_get_video_info, 0 );
86     LOAD_AVS_FUNC( avs_function_exists, 0 );
87     LOAD_AVS_FUNC( avs_invoke, 0 );
88     LOAD_AVS_FUNC( avs_release_clip, 0 );
89     LOAD_AVS_FUNC( avs_release_value, 0 );
90     LOAD_AVS_FUNC( avs_release_video_frame, 0 );
91     LOAD_AVS_FUNC( avs_take_clip, 0 );
92     return 0;
93 fail:
94     FreeLibrary( h->library );
95     return -1;
96 }
97
98 /* generate a filter sequence to try based on the filename extension */
99 static void avs_build_filter_sequence( char *filename_ext, const char *filter[AVS_MAX_SEQUENCE+1] )
100 {
101     int i = 0;
102     const char *all_purpose[] = { "FFmpegSource2", "DSS2", "DirectShowSource", 0 };
103     if( !strcasecmp( filename_ext, "avi" ) )
104         filter[i++] = "AVISource";
105     if( !strcasecmp( filename_ext, "d2v" ) )
106         filter[i++] = "MPEG2Source";
107     if( !strcasecmp( filename_ext, "dga" ) )
108         filter[i++] = "AVCSource";
109     for( int j = 0; all_purpose[j] && i < AVS_MAX_SEQUENCE; j++ )
110         filter[i++] = all_purpose[j];
111 }
112
113 static AVS_Value update_clip( avs_hnd_t *h, const AVS_VideoInfo **vi, AVS_Value res, AVS_Value release )
114 {
115     h->func.avs_release_clip( h->clip );
116     h->clip = h->func.avs_take_clip( res, h->env );
117     h->func.avs_release_value( release );
118     *vi = h->func.avs_get_video_info( h->clip );
119     return res;
120 }
121
122 static int open_file( char *psz_filename, hnd_t *p_handle, video_info_t *info, cli_input_opt_t *opt )
123 {
124     FILE *fh = fopen( psz_filename, "r" );
125     if( !fh )
126         return -1;
127     FAIL_IF_ERROR( !x264_is_regular_file( fh ), "AVS input is incompatible with non-regular file `%s'\n", psz_filename );
128     fclose( fh );
129
130     avs_hnd_t *h = malloc( sizeof(avs_hnd_t) );
131     if( !h )
132         return -1;
133     FAIL_IF_ERROR( x264_avs_load_library( h ), "failed to load avisynth\n" )
134     h->env = h->func.avs_create_script_environment( AVS_INTERFACE_25 );
135     FAIL_IF_ERROR( !h->env, "failed to initiate avisynth\n" )
136     AVS_Value arg = avs_new_value_string( psz_filename );
137     AVS_Value res;
138     char *filename_ext = get_filename_extension( psz_filename );
139
140     if( !strcasecmp( filename_ext, "avs" ) )
141     {
142         res = h->func.avs_invoke( h->env, "Import", arg, NULL );
143         FAIL_IF_ERROR( avs_is_error( res ), "%s\n", avs_as_string( res ) )
144         /* check if the user is using a multi-threaded script and apply distributor if necessary.
145            adapted from avisynth's vfw interface */
146         AVS_Value mt_test = h->func.avs_invoke( h->env, "GetMTMode", avs_new_value_bool( 0 ), NULL );
147         int mt_mode = avs_is_int( mt_test ) ? avs_as_int( mt_test ) : 0;
148         h->func.avs_release_value( mt_test );
149         if( mt_mode > 0 && mt_mode < 5 )
150         {
151             AVS_Value temp = h->func.avs_invoke( h->env, "Distributor", res, NULL );
152             h->func.avs_release_value( res );
153             res = temp;
154         }
155     }
156     else /* non script file */
157     {
158         /* cycle through known source filters to find one that works */
159         const char *filter[AVS_MAX_SEQUENCE+1] = { 0 };
160         avs_build_filter_sequence( filename_ext, filter );
161         int i;
162         for( i = 0; filter[i]; i++ )
163         {
164             x264_cli_log( "avs", X264_LOG_INFO, "trying %s... ", filter[i] );
165             if( !h->func.avs_function_exists( h->env, filter[i] ) )
166             {
167                 x264_cli_printf( X264_LOG_INFO, "not found\n" );
168                 continue;
169             }
170             if( !strncasecmp( filter[i], "FFmpegSource", 12 ) )
171             {
172                 x264_cli_printf( X264_LOG_INFO, "indexing... " );
173                 fflush( stderr );
174             }
175             res = h->func.avs_invoke( h->env, filter[i], arg, NULL );
176             if( !avs_is_error( res ) )
177             {
178                 x264_cli_printf( X264_LOG_INFO, "succeeded\n" );
179                 break;
180             }
181             x264_cli_printf( X264_LOG_INFO, "failed\n" );
182         }
183         FAIL_IF_ERROR( !filter[i], "unable to find source filter to open `%s'\n", psz_filename )
184     }
185     FAIL_IF_ERROR( !avs_is_clip( res ), "`%s' didn't return a video clip\n", psz_filename )
186     h->clip = h->func.avs_take_clip( res, h->env );
187     const AVS_VideoInfo *vi = h->func.avs_get_video_info( h->clip );
188     FAIL_IF_ERROR( !avs_has_video( vi ), "`%s' has no video data\n", psz_filename )
189     /* if the clip is made of fields instead of frames, call weave to make them frames */
190     if( avs_is_field_based( vi ) )
191     {
192         x264_cli_log( "avs", X264_LOG_WARNING, "detected fieldbased (separated) input, weaving to frames\n" );
193         AVS_Value tmp = h->func.avs_invoke( h->env, "Weave", res, NULL );
194         FAIL_IF_ERROR( avs_is_error( tmp ), "couldn't weave fields into frames\n" )
195         res = update_clip( h, &vi, tmp, res );
196         info->interlaced = 1;
197         info->tff = avs_is_tff( vi );
198     }
199 #if !HAVE_SWSCALE
200     /* if swscale is not available, convert CSPs to yv12 */
201     if( !avs_is_yv12( vi ) )
202     {
203         x264_cli_log( "avs", X264_LOG_WARNING, "converting input clip to YV12\n" );
204         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 )
205         const char *arg_name[2] = { NULL, "interlaced" };
206         AVS_Value arg_arr[2] = { res, avs_new_value_bool( info->interlaced ) };
207         AVS_Value res2 = h->func.avs_invoke( h->env, "ConvertToYV12", avs_new_value_array( arg_arr, 2 ), arg_name );
208         FAIL_IF_ERROR( avs_is_error( res2 ), "couldn't convert input clip to YV12\n" )
209         res = update_clip( h, &vi, res2, res );
210     }
211 #endif
212     h->func.avs_release_value( res );
213
214     info->width   = vi->width;
215     info->height  = vi->height;
216     info->fps_num = vi->fps_numerator;
217     info->fps_den = vi->fps_denominator;
218     h->num_frames = info->num_frames = vi->num_frames;
219     info->thread_safe = 1;
220 #if HAVE_SWSCALE
221     if( avs_is_rgb32( vi ) )
222         info->csp = X264_CSP_BGRA | X264_CSP_VFLIP;
223     else if( avs_is_rgb24( vi ) )
224         info->csp = X264_CSP_BGR | X264_CSP_VFLIP;
225     else if( avs_is_yuy2( vi ) )
226         info->csp = PIX_FMT_YUYV422 | X264_CSP_OTHER;
227     else if( avs_is_yv24( vi ) )
228         info->csp = X264_CSP_I444;
229     else if( avs_is_yv16( vi ) )
230         info->csp = X264_CSP_I422;
231     else if( avs_is_yv12( vi ) )
232          info->csp = X264_CSP_I420;
233     else if( avs_is_yv411( vi ) )
234         info->csp = PIX_FMT_YUV411P | X264_CSP_OTHER;
235     else if( avs_is_y8( vi ) )
236         info->csp = PIX_FMT_GRAY8 | X264_CSP_OTHER;
237     else
238         info->csp = X264_CSP_NONE;
239 #else
240     info->csp = X264_CSP_I420;
241 #endif
242     info->vfr = 0;
243
244     *p_handle = h;
245     return 0;
246 }
247
248 static int picture_alloc( cli_pic_t *pic, int csp, int width, int height )
249 {
250     if( x264_cli_pic_alloc( pic, X264_CSP_NONE, width, height ) )
251         return -1;
252     pic->img.csp = csp;
253     const x264_cli_csp_t *cli_csp = x264_cli_get_csp( csp );
254     if( cli_csp )
255         pic->img.planes = cli_csp->planes;
256 #if HAVE_SWSCALE
257     else if( csp == (PIX_FMT_YUV411P | X264_CSP_OTHER) )
258         pic->img.planes = 3;
259     else
260         pic->img.planes = 1; //y8 and yuy2 are one plane
261 #endif
262     return 0;
263 }
264
265 static int read_frame( cli_pic_t *pic, hnd_t handle, int i_frame )
266 {
267     static const int plane[3] = { AVS_PLANAR_Y, AVS_PLANAR_U, AVS_PLANAR_V };
268     avs_hnd_t *h = handle;
269     if( i_frame >= h->num_frames )
270         return -1;
271     AVS_VideoFrame *frm = pic->opaque = h->func.avs_get_frame( h->clip, i_frame );
272     const char *err = h->func.avs_clip_get_error( h->clip );
273     FAIL_IF_ERROR( err, "%s occurred while reading frame %d\n", err, i_frame )
274     for( int i = 0; i < pic->img.planes; i++ )
275     {
276         /* explicitly cast away the const attribute to avoid a warning */
277         pic->img.plane[i] = (uint8_t*)avs_get_read_ptr_p( frm, plane[i] );
278         pic->img.stride[i] = avs_get_pitch_p( frm, plane[i] );
279     }
280     return 0;
281 }
282
283 static int release_frame( cli_pic_t *pic, hnd_t handle )
284 {
285     avs_hnd_t *h = handle;
286     h->func.avs_release_video_frame( pic->opaque );
287     return 0;
288 }
289
290 static void picture_clean( cli_pic_t *pic )
291 {
292     memset( pic, 0, sizeof(cli_pic_t) );
293 }
294
295 static int close_file( hnd_t handle )
296 {
297     avs_hnd_t *h = handle;
298     h->func.avs_release_clip( h->clip );
299     if( h->func.avs_delete_script_environment )
300         h->func.avs_delete_script_environment( h->env );
301     FreeLibrary( h->library );
302     free( h );
303     return 0;
304 }
305
306 const cli_input_t avs_input = { open_file, picture_alloc, read_frame, release_frame, picture_clean, close_file };