+static int init_vid_filters( char *sequence, hnd_t *handle, video_info_t *info, x264_param_t *param, int output_csp )
+{
+ x264_register_vid_filters();
+
+ /* intialize baseline filters */
+ if( x264_init_vid_filter( "source", handle, &filter, info, param, NULL ) ) /* wrap demuxer into a filter */
+ return -1;
+ if( x264_init_vid_filter( "resize", handle, &filter, info, param, "normcsp" ) ) /* normalize csps to be of a known/supported format */
+ return -1;
+ if( x264_init_vid_filter( "fix_vfr_pts", handle, &filter, info, param, NULL ) ) /* fix vfr pts */
+ return -1;
+
+ /* parse filter chain */
+ for( char *p = sequence; p && *p; )
+ {
+ int tok_len = strcspn( p, "/" );
+ int p_len = strlen( p );
+ p[tok_len] = 0;
+ int name_len = strcspn( p, ":" );
+ p[name_len] = 0;
+ name_len += name_len != tok_len;
+ if( x264_init_vid_filter( p, handle, &filter, info, param, p + name_len ) )
+ return -1;
+ p += X264_MIN( tok_len+1, p_len );
+ }
+
+ /* force end result resolution */
+ if( !param->i_width && !param->i_height )
+ {
+ param->i_height = info->height;
+ param->i_width = info->width;
+ }
+ /* force the output csp to what the user specified (or the default) */
+ param->i_csp = info->csp;
+ int csp = info->csp & X264_CSP_MASK;
+ if( output_csp == X264_CSP_I420 && (csp < X264_CSP_I420 || csp >= X264_CSP_I422) )
+ param->i_csp = X264_CSP_I420;
+ else if( output_csp == X264_CSP_I422 && (csp < X264_CSP_I422 || csp >= X264_CSP_I444) )
+ param->i_csp = X264_CSP_I422;
+ else if( output_csp == X264_CSP_I444 && (csp < X264_CSP_I444 || csp >= X264_CSP_BGR) )
+ param->i_csp = X264_CSP_I444;
+ else if( output_csp == X264_CSP_RGB && (csp < X264_CSP_BGR || csp > X264_CSP_RGB) )
+ param->i_csp = X264_CSP_RGB;
+ param->i_csp |= info->csp & X264_CSP_HIGH_DEPTH;
+ /* if the output range is not forced, assign it to the input one now */
+ if( param->vui.b_fullrange == RANGE_AUTO )
+ param->vui.b_fullrange = info->fullrange;
+
+ if( x264_init_vid_filter( "resize", handle, &filter, info, param, NULL ) )
+ return -1;
+
+ char args[20];
+ sprintf( args, "bit_depth=%d", x264_bit_depth );
+
+ if( x264_init_vid_filter( "depth", handle, &filter, info, param, args ) )
+ return -1;
+
+ return 0;
+}
+