]> git.sesse.net Git - x264/blob - filters/video/crop.c
cli: Refactor filter option parsing
[x264] / filters / video / crop.c
1 /*****************************************************************************
2  * crop.c: crop video filter
3  *****************************************************************************
4  * Copyright (C) 2010-2016 x264 project
5  *
6  * Authors: Steven Walters <kemuri9@gmail.com>
7  *          James Darnley <james.darnley@gmail.com>
8  *
9  * This program is free software; you can redistribute it and/or modify
10  * it under the terms of the GNU General Public License as published by
11  * the Free Software Foundation; either version 2 of the License, or
12  * (at your option) any later version.
13  *
14  * This program is distributed in the hope that it will be useful,
15  * but WITHOUT ANY WARRANTY; without even the implied warranty of
16  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
17  * GNU General Public License for more details.
18  *
19  * You should have received a copy of the GNU General Public License
20  * along with this program; if not, write to the Free Software
21  * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02111, USA.
22  *
23  * This program is also available under a commercial proprietary license.
24  * For more information, contact us at licensing@x264.com.
25  *****************************************************************************/
26
27 #include "video.h"
28 #define NAME "crop"
29 #define FAIL_IF_ERROR( cond, ... ) FAIL_IF_ERR( cond, NAME, __VA_ARGS__ )
30
31 cli_vid_filter_t crop_filter;
32
33 typedef struct
34 {
35     hnd_t prev_hnd;
36     cli_vid_filter_t prev_filter;
37
38     int dims[4]; /* left, top, width, height */
39     const x264_cli_csp_t *csp;
40 } crop_hnd_t;
41
42 static void help( int longhelp )
43 {
44     printf( "      "NAME":left,top,right,bottom\n" );
45     if( !longhelp )
46         return;
47     printf( "            removes pixels from the edges of the frame\n" );
48 }
49
50 static int handle_opts( crop_hnd_t *h, video_info_t *info, char **opts, const char * const *optlist )
51 {
52     for( int i = 0; i < 4; i++ )
53     {
54         char *opt = x264_get_option( optlist[i], opts );
55         FAIL_IF_ERROR( !opt, "%s crop value not specified\n", optlist[i] )
56         h->dims[i] = x264_otoi( opt, -1 );
57         FAIL_IF_ERROR( h->dims[i] < 0, "%s crop value `%s' is less than 0\n", optlist[i], opt )
58         int dim_mod = i&1 ? (h->csp->mod_height << info->interlaced) : h->csp->mod_width;
59         FAIL_IF_ERROR( h->dims[i] % dim_mod, "%s crop value `%s' is not a multiple of %d\n", optlist[i], opt, dim_mod )
60     }
61     return 0;
62 }
63
64 static int init( hnd_t *handle, cli_vid_filter_t *filter, video_info_t *info, x264_param_t *param, char *opt_string )
65 {
66     FAIL_IF_ERROR( x264_cli_csp_is_invalid( info->csp ), "invalid csp %d\n", info->csp )
67     crop_hnd_t *h = calloc( 1, sizeof(crop_hnd_t) );
68     if( !h )
69         return -1;
70
71     h->csp = x264_cli_get_csp( info->csp );
72     static const char * const optlist[] = { "left", "top", "right", "bottom", NULL };
73     char **opts = x264_split_options( opt_string, optlist );
74     if( !opts )
75         return -1;
76
77     int err = handle_opts( h, info, opts, optlist );
78     free( opts );
79     if( err )
80         return -1;
81
82     h->dims[2] = info->width  - h->dims[0] - h->dims[2];
83     h->dims[3] = info->height - h->dims[1] - h->dims[3];
84     FAIL_IF_ERROR( h->dims[2] <= 0 || h->dims[3] <= 0, "invalid output resolution %dx%d\n", h->dims[2], h->dims[3] )
85
86     if( info->width != h->dims[2] || info->height != h->dims[3] )
87         x264_cli_log( NAME, X264_LOG_INFO, "cropping to %dx%d\n", h->dims[2], h->dims[3] );
88     else
89     {
90         /* do nothing as the user supplied 0s for all the values */
91         free( h );
92         return 0;
93     }
94     /* done initializing, overwrite values */
95     info->width  = h->dims[2];
96     info->height = h->dims[3];
97
98     h->prev_filter = *filter;
99     h->prev_hnd = *handle;
100     *handle = h;
101     *filter = crop_filter;
102
103     return 0;
104 }
105
106 static int get_frame( hnd_t handle, cli_pic_t *output, int frame )
107 {
108     crop_hnd_t *h = handle;
109     if( h->prev_filter.get_frame( h->prev_hnd, output, frame ) )
110         return -1;
111     output->img.width  = h->dims[2];
112     output->img.height = h->dims[3];
113     /* shift the plane pointers down 'top' rows and right 'left' columns. */
114     for( int i = 0; i < output->img.planes; i++ )
115     {
116         intptr_t offset = output->img.stride[i] * h->dims[1] * h->csp->height[i];
117         offset += h->dims[0] * h->csp->width[i] * x264_cli_csp_depth_factor( output->img.csp );
118         output->img.plane[i] += offset;
119     }
120     return 0;
121 }
122
123 static int release_frame( hnd_t handle, cli_pic_t *pic, int frame )
124 {
125     crop_hnd_t *h = handle;
126     /* NO filter should ever have a dependent release based on the plane pointers,
127      * so avoid unnecessary unshifting */
128     return h->prev_filter.release_frame( h->prev_hnd, pic, frame );
129 }
130
131 static void free_filter( hnd_t handle )
132 {
133     crop_hnd_t *h = handle;
134     h->prev_filter.free( h->prev_hnd );
135     free( h );
136 }
137
138 cli_vid_filter_t crop_filter = { NAME, help, init, get_frame, release_frame, free_filter, NULL };