]> git.sesse.net Git - x264/blob - filters/video/resize.c
Bump dates to 2013
[x264] / filters / video / resize.c
1 /*****************************************************************************
2  * resize.c: resize video filter
3  *****************************************************************************
4  * Copyright (C) 2010-2013 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 "video.h"
27 #define NAME "resize"
28 #define FAIL_IF_ERROR( cond, ... ) FAIL_IF_ERR( cond, NAME, __VA_ARGS__ )
29
30 cli_vid_filter_t resize_filter;
31
32 static int full_check( video_info_t *info, x264_param_t *param )
33 {
34     int required = 0;
35     required |= info->csp       != param->i_csp;
36     required |= info->width     != param->i_width;
37     required |= info->height    != param->i_height;
38     required |= info->fullrange != param->vui.b_fullrange;
39     return required;
40 }
41
42 #if HAVE_SWSCALE
43 #undef DECLARE_ALIGNED
44 #include <libswscale/swscale.h>
45 #include <libavutil/opt.h>
46 #include <libavutil/pixdesc.h>
47
48 #ifndef PIX_FMT_BGRA64
49 #define PIX_FMT_BGRA64 PIX_FMT_NONE
50 #endif
51
52 typedef struct
53 {
54     int width;
55     int height;
56     int pix_fmt;
57     int range;
58 } frame_prop_t;
59
60 typedef struct
61 {
62     hnd_t prev_hnd;
63     cli_vid_filter_t prev_filter;
64
65     cli_pic_t buffer;
66     int buffer_allocated;
67     int dst_csp;
68     int input_range;
69     struct SwsContext *ctx;
70     uint32_t ctx_flags;
71     /* state of swapping chroma planes pre and post resize */
72     int pre_swap_chroma;
73     int post_swap_chroma;
74     int variable_input; /* input is capable of changing properties */
75     int working;        /* we have already started working with frames */
76     frame_prop_t dst;   /* desired output properties */
77     frame_prop_t scale; /* properties of the SwsContext input */
78 } resizer_hnd_t;
79
80 static void help( int longhelp )
81 {
82     printf( "      "NAME":[width,height][,sar][,fittobox][,csp][,method]\n" );
83     if( !longhelp )
84         return;
85     printf( "            resizes frames based on the given criteria:\n"
86             "            - resolution only: resizes and adapts sar to avoid stretching\n"
87             "            - sar only: sets the sar and resizes to avoid stretching\n"
88             "            - resolution and sar: resizes to given resolution and sets the sar\n"
89             "            - fittobox: resizes the video based on the desired constraints\n"
90             "               - width, height, both\n"
91             "            - fittobox and sar: same as above except with specified sar\n"
92             "            - csp: convert to the given csp. syntax: [name][:depth]\n"
93             "               - valid csp names [keep current]: " );
94
95     for( int i = X264_CSP_NONE+1; i < X264_CSP_CLI_MAX; i++ )
96     {
97         printf( "%s", x264_cli_csps[i].name );
98         if( i+1 < X264_CSP_CLI_MAX )
99             printf( ", " );
100     }
101     printf( "\n"
102             "               - depth: 8 or 16 bits per pixel [keep current]\n"
103             "            note: not all depths are supported by all csps.\n"
104             "            - method: use resizer method [\"bicubic\"]\n"
105             "               - fastbilinear, bilinear, bicubic, experimental, point,\n"
106             "               - area, bicublin, gauss, sinc, lanczos, spline\n" );
107 }
108
109 static uint32_t convert_method_to_flag( const char *name )
110 {
111     uint32_t flag = 0;
112     if( !strcasecmp( name, "fastbilinear" ) )
113         flag = SWS_FAST_BILINEAR;
114     else if( !strcasecmp( name, "bilinear" ) )
115         flag = SWS_BILINEAR;
116     else if( !strcasecmp( name, "bicubic" ) )
117         flag = SWS_BICUBIC;
118     else if( !strcasecmp( name, "experimental" ) )
119         flag = SWS_X;
120     else if( !strcasecmp( name, "point" ) )
121         flag = SWS_POINT;
122     else if( !strcasecmp( name, "area" ) )
123         flag = SWS_AREA;
124     else if( !strcasecmp( name, "bicublin" ) )
125         flag = SWS_BICUBLIN;
126     else if( !strcasecmp( name, "guass" ) )
127         flag = SWS_GAUSS;
128     else if( !strcasecmp( name, "sinc" ) )
129         flag = SWS_SINC;
130     else if( !strcasecmp( name, "lanczos" ) )
131         flag = SWS_LANCZOS;
132     else if( !strcasecmp( name, "spline" ) )
133         flag = SWS_SPLINE;
134     else // default
135         flag = SWS_BICUBIC;
136     return flag;
137 }
138
139 static int convert_csp_to_pix_fmt( int csp )
140 {
141     if( csp&X264_CSP_OTHER )
142         return csp&X264_CSP_MASK;
143     switch( csp&X264_CSP_MASK )
144     {
145         case X264_CSP_YV12: /* specially handled via swapping chroma */
146         case X264_CSP_I420: return csp&X264_CSP_HIGH_DEPTH ? PIX_FMT_YUV420P16 : PIX_FMT_YUV420P;
147         case X264_CSP_YV16: /* specially handled via swapping chroma */
148         case X264_CSP_I422: return csp&X264_CSP_HIGH_DEPTH ? PIX_FMT_YUV422P16 : PIX_FMT_YUV422P;
149         case X264_CSP_YV24: /* specially handled via swapping chroma */
150         case X264_CSP_I444: return csp&X264_CSP_HIGH_DEPTH ? PIX_FMT_YUV444P16 : PIX_FMT_YUV444P;
151         case X264_CSP_RGB:  return csp&X264_CSP_HIGH_DEPTH ? PIX_FMT_RGB48     : PIX_FMT_RGB24;
152         case X264_CSP_BGR:  return csp&X264_CSP_HIGH_DEPTH ? PIX_FMT_BGR48     : PIX_FMT_BGR24;
153         case X264_CSP_BGRA: return csp&X264_CSP_HIGH_DEPTH ? PIX_FMT_BGRA64    : PIX_FMT_BGRA;
154         /* the next csp has no equivalent 16bit depth in swscale */
155         case X264_CSP_NV12: return csp&X264_CSP_HIGH_DEPTH ? PIX_FMT_NONE      : PIX_FMT_NV12;
156         /* the next csp is no supported by swscale at all */
157         case X264_CSP_NV16:
158         default:            return PIX_FMT_NONE;
159     }
160 }
161
162 static int pix_number_of_planes( const AVPixFmtDescriptor *pix_desc )
163 {
164     int num_planes = 0;
165     for( int i = 0; i < pix_desc->nb_components; i++ )
166     {
167         int plane_plus1 = pix_desc->comp[i].plane + 1;
168         num_planes = X264_MAX( plane_plus1, num_planes );
169     }
170     return num_planes;
171 }
172
173 static int pick_closest_supported_csp( int csp )
174 {
175     int pix_fmt = convert_csp_to_pix_fmt( csp );
176     // first determine the base csp
177     int ret = X264_CSP_NONE;
178     const AVPixFmtDescriptor *pix_desc = av_pix_fmt_descriptors+pix_fmt;
179     if( (unsigned)pix_fmt >= PIX_FMT_NB || !pix_desc->name )
180         return ret;
181
182     const char *pix_fmt_name = pix_desc->name;
183     int is_rgb = pix_desc->flags & (PIX_FMT_RGB | PIX_FMT_PAL);
184     int is_bgr = !!strstr( pix_fmt_name, "bgr" );
185     if( is_bgr || is_rgb )
186     {
187         if( pix_desc->nb_components == 4 ) // has alpha
188             ret = X264_CSP_BGRA;
189         else if( is_bgr )
190             ret = X264_CSP_BGR;
191         else
192             ret = X264_CSP_RGB;
193     }
194     else
195     {
196         // yuv-based
197         if( pix_desc->nb_components == 1 || pix_desc->nb_components == 2 ) // no chroma
198             ret = X264_CSP_I420;
199         else if( pix_desc->log2_chroma_w && pix_desc->log2_chroma_h ) // reduced chroma width & height
200             ret = (pix_number_of_planes( pix_desc ) == 2) ? X264_CSP_NV12 : X264_CSP_I420;
201         else if( pix_desc->log2_chroma_w ) // reduced chroma width only
202             ret = X264_CSP_I422; // X264_CSP_NV16 is not supported by swscale so don't use it
203         else
204             ret = X264_CSP_I444;
205     }
206     // now determine high depth
207     for( int i = 0; i < pix_desc->nb_components; i++ )
208         if( pix_desc->comp[i].depth_minus1 >= 8 )
209             ret |= X264_CSP_HIGH_DEPTH;
210     return ret;
211 }
212
213 static int handle_opts( const char **optlist, char **opts, video_info_t *info, resizer_hnd_t *h )
214 {
215     uint32_t out_sar_w, out_sar_h;
216
217     char *str_width  = x264_get_option( optlist[0], opts );
218     char *str_height = x264_get_option( optlist[1], opts );
219     char *str_sar    = x264_get_option( optlist[2], opts );
220     char *fittobox   = x264_get_option( optlist[3], opts );
221     char *str_csp    = x264_get_option( optlist[4], opts );
222     int width        = x264_otoi( str_width, -1 );
223     int height       = x264_otoi( str_height, -1 );
224
225     int csp_only = 0;
226     uint32_t in_sar_w = info->sar_width;
227     uint32_t in_sar_h = info->sar_height;
228
229     if( str_csp )
230     {
231         /* output csp was specified, first check if optional depth was provided */
232         char *str_depth = strchr( str_csp, ':' );
233         int depth = x264_cli_csp_depth_factor( info->csp ) * 8;
234         if( str_depth )
235         {
236             /* csp bit depth was specified */
237             *str_depth++ = '\0';
238             depth = x264_otoi( str_depth, -1 );
239             FAIL_IF_ERROR( depth != 8 && depth != 16, "unsupported bit depth %d\n", depth );
240         }
241         /* now lookup against the list of valid csps */
242         int csp;
243         if( strlen( str_csp ) == 0 )
244             csp = info->csp & X264_CSP_MASK;
245         else
246             for( csp = X264_CSP_CLI_MAX-1; x264_cli_csps[csp].name && strcasecmp( x264_cli_csps[csp].name, str_csp ); )
247                 csp--;
248         FAIL_IF_ERROR( csp == X264_CSP_NONE, "unsupported colorspace `%s'\n", str_csp );
249         h->dst_csp = csp;
250         if( depth == 16 )
251             h->dst_csp |= X264_CSP_HIGH_DEPTH;
252     }
253
254     /* if the input sar is currently invalid, set it to 1:1 so it can be used in math */
255     if( !in_sar_w || !in_sar_h )
256         in_sar_w = in_sar_h = 1;
257     if( str_sar )
258     {
259         FAIL_IF_ERROR( 2 != sscanf( str_sar, "%u:%u", &out_sar_w, &out_sar_h ) &&
260                        2 != sscanf( str_sar, "%u/%u", &out_sar_w, &out_sar_h ),
261                        "invalid sar `%s'\n", str_sar )
262     }
263     else
264         out_sar_w = out_sar_h = 1;
265     if( fittobox )
266     {
267         /* resize the video to fit the box as much as possible */
268         if( !strcasecmp( fittobox, "both" ) )
269         {
270             FAIL_IF_ERROR( width <= 0 || height <= 0, "invalid box resolution %sx%s\n",
271                            x264_otos( str_width, "<unset>" ), x264_otos( str_height, "<unset>" ) )
272         }
273         else if( !strcasecmp( fittobox, "width" ) )
274         {
275             FAIL_IF_ERROR( width <= 0, "invalid box width `%s'\n", x264_otos( str_width, "<unset>" ) )
276             height = INT_MAX;
277         }
278         else if( !strcasecmp( fittobox, "height" ) )
279         {
280             FAIL_IF_ERROR( height <= 0, "invalid box height `%s'\n", x264_otos( str_height, "<unset>" ) )
281             width = INT_MAX;
282         }
283         else FAIL_IF_ERROR( 1, "invalid fittobox mode `%s'\n", fittobox )
284
285         /* maximally fit the new coded resolution to the box */
286         const x264_cli_csp_t *csp = x264_cli_get_csp( h->dst_csp );
287         double width_units = (double)info->height * in_sar_h * out_sar_w;
288         double height_units = (double)info->width * in_sar_w * out_sar_h;
289         width = width / csp->mod_width * csp->mod_width;
290         height = height / csp->mod_height * csp->mod_height;
291         if( width * width_units > height * height_units )
292         {
293             int new_width = round( height * height_units / (width_units * csp->mod_width) );
294             new_width *= csp->mod_width;
295             width = X264_MIN( new_width, width );
296         }
297         else
298         {
299             int new_height = round( width * width_units / (height_units * csp->mod_height) );
300             new_height *= csp->mod_height;
301             height = X264_MIN( new_height, height );
302         }
303     }
304     else
305     {
306         if( str_width || str_height )
307         {
308             FAIL_IF_ERROR( width <= 0 || height <= 0, "invalid resolution %sx%s\n",
309                            x264_otos( str_width, "<unset>" ), x264_otos( str_height, "<unset>" ) )
310             if( !str_sar ) /* res only -> adjust sar */
311             {
312                 /* new_sar = (new_h * old_w * old_sar_w) / (old_h * new_w * old_sar_h) */
313                 uint64_t num = (uint64_t)info->width  * height;
314                 uint64_t den = (uint64_t)info->height * width;
315                 x264_reduce_fraction64( &num, &den );
316                 out_sar_w = num * in_sar_w;
317                 out_sar_h = den * in_sar_h;
318                 x264_reduce_fraction( &out_sar_w, &out_sar_h );
319             }
320         }
321         else if( str_sar ) /* sar only -> adjust res */
322         {
323              const x264_cli_csp_t *csp = x264_cli_get_csp( h->dst_csp );
324              double width_units = (double)in_sar_h * out_sar_w;
325              double height_units = (double)in_sar_w * out_sar_h;
326              width  = info->width;
327              height = info->height;
328              if( width_units > height_units ) // SAR got wider, decrease width
329              {
330                  width = round( info->width * height_units / (width_units * csp->mod_width) );
331                  width *= csp->mod_width;
332              }
333              else // SAR got thinner, decrease height
334              {
335                  height = round( info->height * width_units / (height_units * csp->mod_height) );
336                  height *= csp->mod_height;
337              }
338         }
339         else /* csp only */
340         {
341             h->dst.width  = info->width;
342             h->dst.height = info->height;
343             csp_only = 1;
344         }
345     }
346     if( !csp_only )
347     {
348         info->sar_width  = out_sar_w;
349         info->sar_height = out_sar_h;
350         h->dst.width  = width;
351         h->dst.height = height;
352     }
353     return 0;
354 }
355
356 static int x264_init_sws_context( resizer_hnd_t *h )
357 {
358     if( h->ctx )
359         sws_freeContext( h->ctx );
360     h->ctx = sws_alloc_context();
361     if( !h->ctx )
362         return -1;
363
364     av_opt_set_int( h->ctx, "sws_flags",  h->ctx_flags,   0 );
365     av_opt_set_int( h->ctx, "dstw",       h->dst.width,   0 );
366     av_opt_set_int( h->ctx, "dsth",       h->dst.height,  0 );
367     av_opt_set_int( h->ctx, "dst_format", h->dst.pix_fmt, 0 );
368     av_opt_set_int( h->ctx, "dst_range",  h->dst.range,   0 );
369
370     av_opt_set_int( h->ctx, "srcw",       h->scale.width,   0 );
371     av_opt_set_int( h->ctx, "srch",       h->scale.height,  0 );
372     av_opt_set_int( h->ctx, "src_format", h->scale.pix_fmt, 0 );
373     av_opt_set_int( h->ctx, "src_range",  h->scale.range,   0 );
374
375     /* FIXME: use the correct matrix coefficients (only YUV -> RGB conversions are supported) */
376     sws_setColorspaceDetails( h->ctx,
377                               sws_getCoefficients( SWS_CS_DEFAULT ), h->scale.range,
378                               sws_getCoefficients( SWS_CS_DEFAULT ), h->dst.range,
379                               0, 1<<16, 1<<16 );
380
381     return sws_init_context( h->ctx, NULL, NULL ) < 0;
382 }
383
384 static int check_resizer( resizer_hnd_t *h, cli_pic_t *in )
385 {
386     frame_prop_t input_prop = { in->img.width, in->img.height, convert_csp_to_pix_fmt( in->img.csp ), h->input_range };
387     if( !memcmp( &input_prop, &h->scale, sizeof(frame_prop_t) ) )
388         return 0;
389     /* also warn if the resizer was initialized after the first frame */
390     if( h->ctx || h->working )
391         x264_cli_log( NAME, X264_LOG_WARNING, "stream properties changed at pts %"PRId64"\n", in->pts );
392     h->scale = input_prop;
393     if( !h->buffer_allocated )
394     {
395         if( x264_cli_pic_alloc( &h->buffer, h->dst_csp, h->dst.width, h->dst.height ) )
396             return -1;
397         h->buffer_allocated = 1;
398     }
399     FAIL_IF_ERROR( x264_init_sws_context( h ), "swscale init failed\n" )
400     return 0;
401 }
402
403 static int init( hnd_t *handle, cli_vid_filter_t *filter, video_info_t *info, x264_param_t *param, char *opt_string )
404 {
405     /* if called for normalizing the csp to known formats and the format is not unknown, exit */
406     if( opt_string && !strcmp( opt_string, "normcsp" ) && !(info->csp&X264_CSP_OTHER) )
407         return 0;
408     /* if called by x264cli and nothing needs to be done, exit */
409     if( !opt_string && !full_check( info, param ) )
410         return 0;
411
412     static const char *optlist[] = { "width", "height", "sar", "fittobox", "csp", "method", NULL };
413     char **opts = x264_split_options( opt_string, optlist );
414     if( !opts && opt_string )
415         return -1;
416
417     resizer_hnd_t *h = calloc( 1, sizeof(resizer_hnd_t) );
418     if( !h )
419         return -1;
420     if( opts )
421     {
422         h->dst_csp    = info->csp;
423         h->dst.width  = info->width;
424         h->dst.height = info->height;
425         h->dst.range  = info->fullrange; // maintain input range
426         if( !strcmp( opt_string, "normcsp" ) )
427         {
428             /* only in normalization scenarios is the input capable of changing properties */
429             h->variable_input = 1;
430             h->dst_csp = pick_closest_supported_csp( info->csp );
431             FAIL_IF_ERROR( h->dst_csp == X264_CSP_NONE,
432                            "filter get invalid input pixel format %d (colorspace %d)\n", convert_csp_to_pix_fmt( info->csp ), info->csp )
433         }
434         else if( handle_opts( optlist, opts, info, h ) )
435             return -1;
436     }
437     else
438     {
439         h->dst_csp    = param->i_csp;
440         h->dst.width  = param->i_width;
441         h->dst.height = param->i_height;
442         h->dst.range  = param->vui.b_fullrange; // change to libx264's range
443     }
444     h->ctx_flags = convert_method_to_flag( x264_otos( x264_get_option( optlist[5], opts ), "" ) );
445     x264_free_string_array( opts );
446
447     if( h->ctx_flags != SWS_FAST_BILINEAR )
448         h->ctx_flags |= SWS_FULL_CHR_H_INT | SWS_FULL_CHR_H_INP | SWS_ACCURATE_RND;
449     h->dst.pix_fmt = convert_csp_to_pix_fmt( h->dst_csp );
450     h->scale = h->dst;
451     h->input_range = info->fullrange;
452
453     /* swap chroma planes if YV12/YV16/YV24 is involved, as libswscale works with I420/I422/I444 */
454     int src_csp = info->csp & (X264_CSP_MASK | X264_CSP_OTHER);
455     int dst_csp = h->dst_csp & (X264_CSP_MASK | X264_CSP_OTHER);
456     h->pre_swap_chroma  = src_csp == X264_CSP_YV12 || src_csp == X264_CSP_YV16 || src_csp == X264_CSP_YV24;
457     h->post_swap_chroma = dst_csp == X264_CSP_YV12 || dst_csp == X264_CSP_YV16 || dst_csp == X264_CSP_YV24;
458
459     int src_pix_fmt = convert_csp_to_pix_fmt( info->csp );
460
461     int src_pix_fmt_inv = convert_csp_to_pix_fmt( info->csp ^ X264_CSP_HIGH_DEPTH );
462     int dst_pix_fmt_inv = convert_csp_to_pix_fmt( h->dst_csp ^ X264_CSP_HIGH_DEPTH );
463
464     /* confirm swscale can support this conversion */
465     FAIL_IF_ERROR( src_pix_fmt == PIX_FMT_NONE && src_pix_fmt_inv != PIX_FMT_NONE,
466                    "input colorspace %s with bit depth %d is not supported\n", av_get_pix_fmt_name( src_pix_fmt_inv ),
467                    info->csp & X264_CSP_HIGH_DEPTH ? 16 : 8 );
468     FAIL_IF_ERROR( !sws_isSupportedInput( src_pix_fmt ), "input colorspace %s is not supported\n", av_get_pix_fmt_name( src_pix_fmt ) )
469     FAIL_IF_ERROR( h->dst.pix_fmt == PIX_FMT_NONE && dst_pix_fmt_inv != PIX_FMT_NONE,
470                    "input colorspace %s with bit depth %d is not supported\n", av_get_pix_fmt_name( dst_pix_fmt_inv ),
471                    h->dst_csp & X264_CSP_HIGH_DEPTH ? 16 : 8 );
472     FAIL_IF_ERROR( !sws_isSupportedOutput( h->dst.pix_fmt ), "output colorspace %s is not supported\n", av_get_pix_fmt_name( h->dst.pix_fmt ) )
473     FAIL_IF_ERROR( h->dst.height != info->height && info->interlaced,
474                    "swscale is not compatible with interlaced vertical resizing\n" )
475     /* confirm that the desired resolution meets the colorspace requirements */
476     const x264_cli_csp_t *csp = x264_cli_get_csp( h->dst_csp );
477     FAIL_IF_ERROR( h->dst.width % csp->mod_width || h->dst.height % csp->mod_height,
478                    "resolution %dx%d is not compliant with colorspace %s\n", h->dst.width, h->dst.height, csp->name )
479
480     if( h->dst.width != info->width || h->dst.height != info->height )
481         x264_cli_log( NAME, X264_LOG_INFO, "resizing to %dx%d\n", h->dst.width, h->dst.height );
482     if( h->dst.pix_fmt != src_pix_fmt )
483         x264_cli_log( NAME, X264_LOG_WARNING, "converting from %s to %s\n",
484                       av_get_pix_fmt_name( src_pix_fmt ), av_get_pix_fmt_name( h->dst.pix_fmt ) );
485     else if( h->dst.range != h->input_range )
486         x264_cli_log( NAME, X264_LOG_WARNING, "converting range from %s to %s\n",
487                       h->input_range ? "PC" : "TV", h->dst.range ? "PC" : "TV" );
488     h->dst_csp |= info->csp & X264_CSP_VFLIP; // preserve vflip
489
490     /* if the input is not variable, initialize the context */
491     if( !h->variable_input )
492     {
493         cli_pic_t input_pic = {{info->csp, info->width, info->height, 0}, 0};
494         if( check_resizer( h, &input_pic ) )
495             return -1;
496     }
497
498     /* finished initing, overwrite values */
499     info->csp       = h->dst_csp;
500     info->width     = h->dst.width;
501     info->height    = h->dst.height;
502     info->fullrange = h->dst.range;
503
504     h->prev_filter = *filter;
505     h->prev_hnd = *handle;
506     *handle = h;
507     *filter = resize_filter;
508
509     return 0;
510 }
511
512 static int get_frame( hnd_t handle, cli_pic_t *output, int frame )
513 {
514     resizer_hnd_t *h = handle;
515     if( h->prev_filter.get_frame( h->prev_hnd, output, frame ) )
516         return -1;
517     if( h->variable_input && check_resizer( h, output ) )
518         return -1;
519     h->working = 1;
520     if( h->pre_swap_chroma )
521         XCHG( uint8_t*, output->img.plane[1], output->img.plane[2] );
522     if( h->ctx )
523     {
524         sws_scale( h->ctx, (const uint8_t* const*)output->img.plane, output->img.stride,
525                    0, output->img.height, h->buffer.img.plane, h->buffer.img.stride );
526         output->img = h->buffer.img; /* copy img data */
527     }
528     else
529         output->img.csp = h->dst_csp;
530     if( h->post_swap_chroma )
531         XCHG( uint8_t*, output->img.plane[1], output->img.plane[2] );
532
533     return 0;
534 }
535
536 static int release_frame( hnd_t handle, cli_pic_t *pic, int frame )
537 {
538     resizer_hnd_t *h = handle;
539     return h->prev_filter.release_frame( h->prev_hnd, pic, frame );
540 }
541
542 static void free_filter( hnd_t handle )
543 {
544     resizer_hnd_t *h = handle;
545     h->prev_filter.free( h->prev_hnd );
546     if( h->ctx )
547         sws_freeContext( h->ctx );
548     if( h->buffer_allocated )
549         x264_cli_pic_clean( &h->buffer );
550     free( h );
551 }
552
553 #else /* no swscale */
554 static int init( hnd_t *handle, cli_vid_filter_t *filter, video_info_t *info, x264_param_t *param, char *opt_string )
555 {
556     int ret = 0;
557
558     if( !opt_string )
559         ret = full_check( info, param );
560     else
561     {
562         if( !strcmp( opt_string, "normcsp" ) )
563             ret = info->csp & X264_CSP_OTHER;
564         else
565             ret = -1;
566     }
567
568     /* pass if nothing needs to be done, otherwise fail */
569     FAIL_IF_ERROR( ret, "not compiled with swscale support\n" )
570     return 0;
571 }
572
573 #define help NULL
574 #define get_frame NULL
575 #define release_frame NULL
576 #define free_filter NULL
577 #define convert_csp_to_pix_fmt(x) (x & X264_CSP_MASK)
578
579 #endif
580
581 cli_vid_filter_t resize_filter = { NAME, help, init, get_frame, release_frame, free_filter, NULL };