]> git.sesse.net Git - vlc/blob - modules/video_filter/sharpen.c
input options whitelisting, step 2 (refs #1371)
[vlc] / modules / video_filter / sharpen.c
1 /*****************************************************************************
2  * sharpen.c: Sharpen video filter
3  *****************************************************************************
4  * Copyright (C) 2003-2007 the VideoLAN team
5  * $Id: sharpen.c 18062 2006-11-26 14:20:34Z zorglub $
6  *
7  * Author: Jérémy DEMEULE <dj_mulder at djduron dot no-ip dot org>
8  *         Jean-Baptiste Kempf <jb at videolan dot org>
9  *
10  * This program is free software; you can redistribute it and/or modify
11  * it under the terms of the GNU General Public License as published by
12  * the Free Software Foundation; either version 2 of the License, or
13  * (at your option) any later version.
14  *
15  * This program is distributed in the hope that it will be useful,
16  * but WITHOUT ANY WARRANTY; without even the implied warranty of
17  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
18  * GNU General Public License for more details.
19  *
20  * You should have received a copy of the GNU General Public License
21  * along with this program; if not, write to the Free Software
22  * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston MA 02110-1301, USA.
23  *****************************************************************************/
24
25 /* The sharpen filter. */
26 /*
27  * static int filter[] = { -1, -1, -1,
28  *                         -1,  8, -1,
29  *                         -1, -1, -1 };
30  */
31
32 /*****************************************************************************
33  * Preamble
34  *****************************************************************************/
35
36 #include <vlc/vlc.h>
37 #include <vlc_vout.h>
38
39 #include "vlc_filter.h"
40
41 #define SIG_TEXT N_("Sharpen strength (0-2)")
42 #define SIG_LONGTEXT N_("Set the Sharpen strength, between 0 and 2. Defaults to 0.05.")
43
44 /*****************************************************************************
45  * Local prototypes
46  *****************************************************************************/
47 static int  Create    ( vlc_object_t * );
48 static void Destroy   ( vlc_object_t * );
49
50 static picture_t *Filter( filter_t *, picture_t * );
51 static int SharpenCallback( vlc_object_t *, char const *,
52                             vlc_value_t, vlc_value_t, void * );
53
54 #define FILTER_PREFIX "sharpen-"
55
56 /*****************************************************************************
57  * Module descriptor
58  *****************************************************************************/
59 vlc_module_begin();
60     set_description( _("Augment contrast between contours.") );
61     set_shortname( _("Sharpen video filter") );
62     set_category( CAT_VIDEO );
63     set_subcategory( SUBCAT_VIDEO_VFILTER );
64     set_capability( "video filter2", 0 );
65     add_float_with_range( "sharpen-sigma", 0.05, 0.0, 2.0, NULL,
66         SIG_TEXT, SIG_LONGTEXT, VLC_FALSE );
67         change_safe();
68     add_shortcut( "sharpen" );
69     set_callbacks( Create, Destroy );
70 vlc_module_end();
71
72 static const char *ppsz_filter_options[] = {
73     "sigma", NULL
74 };
75
76 /*****************************************************************************
77  * filter_sys_t: Sharpen video filter descriptor
78  *****************************************************************************
79  * This structure is part of the video output thread descriptor.
80  * It describes the Sharpen specific properties of an output thread.
81  *****************************************************************************/
82
83 struct filter_sys_t
84 {
85     float f_sigma;
86     int tab_precalc[512];
87 };
88
89 /*****************************************************************************
90  * clip: avoid negative value and value > 255
91  *****************************************************************************/
92 inline static int32_t clip( int32_t a )
93 {
94     return (a > 255) ? 255 : (a < 0) ? 0 : a;
95 }
96
97 static void init_precalc_table(filter_sys_t *p_filter)
98 {
99     float sigma = p_filter->f_sigma;
100  
101     for(int i = 0; i < 512; ++i)
102     {
103         p_filter->tab_precalc[i] = (i - 256) * sigma;
104     }
105 }
106
107 /*****************************************************************************
108  * Create: allocates Sharpen video thread output method
109  *****************************************************************************
110  * This function allocates and initializes a Sharpen vout method.
111  *****************************************************************************/
112 static int Create( vlc_object_t *p_this )
113 {
114     filter_t *p_filter = (filter_t *)p_this;
115
116     /* Allocate structure */
117     p_filter->p_sys = malloc( sizeof( filter_sys_t ) );
118     if( p_filter->p_sys == NULL )
119     {
120         msg_Err( p_filter, "out of memory" );
121         return VLC_ENOMEM;
122     }
123
124     p_filter->pf_video_filter = Filter;
125
126     config_ChainParse( p_filter, FILTER_PREFIX, ppsz_filter_options,
127                    p_filter->p_cfg );
128
129     p_filter->p_sys->f_sigma =
130         var_CreateGetFloatCommand( p_filter, FILTER_PREFIX "sigma" );
131     var_AddCallback( p_filter, FILTER_PREFIX "sigma",
132                      SharpenCallback, p_filter->p_sys );
133
134     init_precalc_table(p_filter->p_sys);
135
136     return VLC_SUCCESS;
137 }
138
139
140 /*****************************************************************************
141  * Destroy: destroy Sharpen video thread output method
142  *****************************************************************************
143  * Terminate an output method created by SharpenCreateOutputMethod
144  *****************************************************************************/
145 static void Destroy( vlc_object_t *p_this )
146 {
147     filter_t *p_filter = (filter_t *)p_this;
148     free( p_filter->p_sys );
149 }
150
151 /*****************************************************************************
152  * Render: displays previously rendered output
153  *****************************************************************************
154  * This function send the currently rendered image to Invert image, waits
155  * until it is displayed and switch the two rendering buffers, preparing next
156  * frame.
157  *****************************************************************************/
158 static picture_t *Filter( filter_t *p_filter, picture_t *p_pic )
159 {
160     picture_t *p_outpic;
161     int i, j;
162     uint8_t *p_src = NULL;
163     uint8_t *p_out = NULL;
164     int i_src_pitch;
165     int pix;
166     const int v1 = -1;
167     const int v2 = 3; /* 2^3 = 8 */
168
169     if( !p_pic ) return NULL;
170     if( !p_filter ) return NULL;
171     if( !p_filter->p_sys ) return NULL;
172
173     p_outpic = p_filter->pf_vout_buffer_new( p_filter );
174     if( !p_outpic )
175     {
176         msg_Warn( p_filter, "can't get output picture" );
177         if( p_pic->pf_release )
178             p_pic->pf_release( p_pic );
179         return NULL;
180     }
181
182     /* process the Y plane */
183     p_src = p_pic->p[Y_PLANE].p_pixels;
184     p_out = p_outpic->p[Y_PLANE].p_pixels;
185     if( !p_src || !p_out )
186     {
187         msg_Warn( p_filter, "can't get Y plane" );
188         if( p_pic->pf_release )
189             p_pic->pf_release( p_pic );
190         return NULL;
191     }
192
193     i_src_pitch = p_pic->p[Y_PLANE].i_visible_pitch;
194
195     /* perform convolution only on Y plane. Avoid border line. */
196     for( i = 0; i < p_pic->p[Y_PLANE].i_visible_lines; i++ )
197     {
198         if( (i == 0) || (i == p_pic->p[Y_PLANE].i_visible_lines - 1) )
199         {
200             for( j = 0; j < p_pic->p[Y_PLANE].i_visible_pitch; j++ )
201                 p_out[i * i_src_pitch + j] = clip( p_src[i * i_src_pitch + j] );
202             continue ;
203         }
204         for( j = 0; j < p_pic->p[Y_PLANE].i_visible_pitch; j++ )
205         {
206             if( (j == 0) || (j == p_pic->p[Y_PLANE].i_visible_pitch - 1) )
207             {
208                 p_out[i * i_src_pitch + j] = p_src[i * i_src_pitch + j];
209                 continue ;
210             }
211
212             pix = (p_src[(i - 1) * i_src_pitch + j - 1] * v1) +
213                   (p_src[(i - 1) * i_src_pitch + j    ] * v1) +
214                   (p_src[(i - 1) * i_src_pitch + j + 1] * v1) +
215                   (p_src[(i    ) * i_src_pitch + j - 1] * v1) +
216                   (p_src[(i    ) * i_src_pitch + j    ] << v2) +
217                   (p_src[(i    ) * i_src_pitch + j + 1] * v1) +
218                   (p_src[(i + 1) * i_src_pitch + j - 1] * v1) +
219                   (p_src[(i + 1) * i_src_pitch + j    ] * v1) +
220                   (p_src[(i + 1) * i_src_pitch + j + 1] * v1);
221
222         pix = pix >= 0 ? clip(pix) : -clip(pix * -1);
223         p_out[i * i_src_pitch + j] = clip( p_src[i * i_src_pitch + j] +
224             p_filter->p_sys->tab_precalc[pix + 256] );
225         }
226     }
227
228
229     p_filter->p_libvlc->pf_memcpy( p_outpic->p[U_PLANE].p_pixels,
230             p_pic->p[U_PLANE].p_pixels,
231             p_outpic->p[U_PLANE].i_lines * p_outpic->p[U_PLANE].i_pitch );
232
233     p_filter->p_libvlc->pf_memcpy( p_outpic->p[V_PLANE].p_pixels,
234             p_pic->p[V_PLANE].p_pixels,
235             p_outpic->p[V_PLANE].i_lines * p_outpic->p[V_PLANE].i_pitch );
236
237
238     p_outpic->date = p_pic->date;
239     p_outpic->b_force = p_pic->b_force;
240     p_outpic->i_nb_fields = p_pic->i_nb_fields;
241     p_outpic->b_progressive = p_pic->b_progressive;
242     p_outpic->b_top_field_first = p_pic->b_top_field_first;
243
244     if( p_pic->pf_release )
245         p_pic->pf_release( p_pic );
246
247     return p_outpic;
248 }
249
250 static int SharpenCallback( vlc_object_t *p_this, char const *psz_var,
251                             vlc_value_t oldval, vlc_value_t newval,
252                             void *p_data )
253 {
254     filter_sys_t *p_sys = (filter_sys_t *)p_data;
255     if( !strcmp( psz_var, FILTER_PREFIX "sigma" ) )
256         p_sys->f_sigma = __MIN( 2., __MAX( 0., newval.f_float ) );
257     init_precalc_table(p_sys);
258     return VLC_SUCCESS;
259 }