]> git.sesse.net Git - vlc/blob - modules/video_filter/sharpen.c
Remove most stray semi-colons in module descriptions
[vlc] / modules / video_filter / sharpen.c
1 /*****************************************************************************
2  * sharpen.c: Sharpen video filter
3  *****************************************************************************
4  * Copyright (C) 2003-2007 the VideoLAN team
5  * $Id$
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 #ifdef HAVE_CONFIG_H
37 # include "config.h"
38 #endif
39
40 #include <vlc_common.h>
41 #include <vlc_plugin.h>
42 #include <vlc_vout.h>
43
44 #include "vlc_filter.h"
45 #include "filter_picture.h"
46
47 #define SIG_TEXT N_("Sharpen strength (0-2)")
48 #define SIG_LONGTEXT N_("Set the Sharpen strength, between 0 and 2. Defaults to 0.05.")
49
50 /*****************************************************************************
51  * Local prototypes
52  *****************************************************************************/
53 static int  Create    ( vlc_object_t * );
54 static void Destroy   ( vlc_object_t * );
55
56 static picture_t *Filter( filter_t *, picture_t * );
57 static int SharpenCallback( vlc_object_t *, char const *,
58                             vlc_value_t, vlc_value_t, void * );
59
60 #define FILTER_PREFIX "sharpen-"
61
62 /*****************************************************************************
63  * Module descriptor
64  *****************************************************************************/
65 vlc_module_begin ()
66     set_description( N_("Augment contrast between contours.") )
67     set_shortname( N_("Sharpen video filter") )
68     set_category( CAT_VIDEO )
69     set_subcategory( SUBCAT_VIDEO_VFILTER )
70     set_capability( "video filter2", 0 )
71     add_float_with_range( "sharpen-sigma", 0.05, 0.0, 2.0, NULL,
72         SIG_TEXT, SIG_LONGTEXT, false );
73     add_shortcut( "sharpen" )
74     set_callbacks( Create, Destroy )
75 vlc_module_end ()
76
77 static const char *const ppsz_filter_options[] = {
78     "sigma", NULL
79 };
80
81 /*****************************************************************************
82  * filter_sys_t: Sharpen video filter descriptor
83  *****************************************************************************
84  * This structure is part of the video output thread descriptor.
85  * It describes the Sharpen specific properties of an output thread.
86  *****************************************************************************/
87
88 struct filter_sys_t
89 {
90     float f_sigma;
91     int tab_precalc[512];
92 };
93
94 /*****************************************************************************
95  * clip: avoid negative value and value > 255
96  *****************************************************************************/
97 inline static int32_t clip( int32_t a )
98 {
99     return (a > 255) ? 255 : (a < 0) ? 0 : a;
100 }
101
102 static void init_precalc_table(filter_sys_t *p_filter)
103 {
104     float sigma = p_filter->f_sigma;
105
106     for(int i = 0; i < 512; ++i)
107     {
108         p_filter->tab_precalc[i] = (i - 256) * sigma;
109     }
110 }
111
112 /*****************************************************************************
113  * Create: allocates Sharpen video thread output method
114  *****************************************************************************
115  * This function allocates and initializes a Sharpen vout method.
116  *****************************************************************************/
117 static int Create( vlc_object_t *p_this )
118 {
119     filter_t *p_filter = (filter_t *)p_this;
120
121     /* Allocate structure */
122     p_filter->p_sys = malloc( sizeof( filter_sys_t ) );
123     if( p_filter->p_sys == NULL )
124         return VLC_ENOMEM;
125
126     p_filter->pf_video_filter = Filter;
127
128     config_ChainParse( p_filter, FILTER_PREFIX, ppsz_filter_options,
129                    p_filter->p_cfg );
130
131     p_filter->p_sys->f_sigma =
132         var_CreateGetFloatCommand( p_filter, FILTER_PREFIX "sigma" );
133     var_AddCallback( p_filter, FILTER_PREFIX "sigma",
134                      SharpenCallback, p_filter->p_sys );
135
136     init_precalc_table(p_filter->p_sys);
137
138     return VLC_SUCCESS;
139 }
140
141
142 /*****************************************************************************
143  * Destroy: destroy Sharpen video thread output method
144  *****************************************************************************
145  * Terminate an output method created by SharpenCreateOutputMethod
146  *****************************************************************************/
147 static void Destroy( vlc_object_t *p_this )
148 {
149     filter_t *p_filter = (filter_t *)p_this;
150     free( p_filter->p_sys );
151 }
152
153 /*****************************************************************************
154  * Render: displays previously rendered output
155  *****************************************************************************
156  * This function send the currently rendered image to Invert image, waits
157  * until it is displayed and switch the two rendering buffers, preparing next
158  * frame.
159  *****************************************************************************/
160 static picture_t *Filter( filter_t *p_filter, picture_t *p_pic )
161 {
162     picture_t *p_outpic;
163     int i, j;
164     uint8_t *p_src = NULL;
165     uint8_t *p_out = NULL;
166     int i_src_pitch;
167     int pix;
168     const int v1 = -1;
169     const int v2 = 3; /* 2^3 = 8 */
170
171     if( !p_pic ) return NULL;
172     if( !p_filter ) return NULL;
173     if( !p_filter->p_sys ) return NULL;
174
175     p_outpic = filter_NewPicture( p_filter );
176     if( !p_outpic )
177     {
178         picture_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         picture_Release( p_pic );
189         return NULL;
190     }
191
192     i_src_pitch = p_pic->p[Y_PLANE].i_visible_pitch;
193
194     /* perform convolution only on Y plane. Avoid border line. */
195     for( i = 0; i < p_pic->p[Y_PLANE].i_visible_lines; i++ )
196     {
197         if( (i == 0) || (i == p_pic->p[Y_PLANE].i_visible_lines - 1) )
198         {
199             for( j = 0; j < p_pic->p[Y_PLANE].i_visible_pitch; j++ )
200                 p_out[i * i_src_pitch + j] = clip( p_src[i * i_src_pitch + j] );
201             continue ;
202         }
203         for( j = 0; j < p_pic->p[Y_PLANE].i_visible_pitch; j++ )
204         {
205             if( (j == 0) || (j == p_pic->p[Y_PLANE].i_visible_pitch - 1) )
206             {
207                 p_out[i * i_src_pitch + j] = p_src[i * i_src_pitch + j];
208                 continue ;
209             }
210
211             pix = (p_src[(i - 1) * i_src_pitch + j - 1] * v1) +
212                   (p_src[(i - 1) * i_src_pitch + j    ] * v1) +
213                   (p_src[(i - 1) * i_src_pitch + j + 1] * v1) +
214                   (p_src[(i    ) * i_src_pitch + j - 1] * v1) +
215                   (p_src[(i    ) * i_src_pitch + j    ] << v2) +
216                   (p_src[(i    ) * i_src_pitch + j + 1] * v1) +
217                   (p_src[(i + 1) * i_src_pitch + j - 1] * v1) +
218                   (p_src[(i + 1) * i_src_pitch + j    ] * v1) +
219                   (p_src[(i + 1) * i_src_pitch + j + 1] * v1);
220
221         pix = pix >= 0 ? clip(pix) : -clip(pix * -1);
222         p_out[i * i_src_pitch + j] = clip( p_src[i * i_src_pitch + j] +
223             p_filter->p_sys->tab_precalc[pix + 256] );
224         }
225     }
226
227
228     vlc_memcpy( p_outpic->p[U_PLANE].p_pixels, p_pic->p[U_PLANE].p_pixels,
229                 p_outpic->p[U_PLANE].i_lines * p_outpic->p[U_PLANE].i_pitch );
230
231     vlc_memcpy( p_outpic->p[V_PLANE].p_pixels, p_pic->p[V_PLANE].p_pixels,
232                 p_outpic->p[V_PLANE].i_lines * p_outpic->p[V_PLANE].i_pitch );
233
234
235     return CopyInfoAndRelease( p_outpic, p_pic );
236 }
237
238 static int SharpenCallback( vlc_object_t *p_this, char const *psz_var,
239                             vlc_value_t oldval, vlc_value_t newval,
240                             void *p_data )
241 {
242     VLC_UNUSED(p_this); VLC_UNUSED(oldval);
243     filter_sys_t *p_sys = (filter_sys_t *)p_data;
244     if( !strcmp( psz_var, FILTER_PREFIX "sigma" ) )
245         p_sys->f_sigma = __MIN( 2., __MAX( 0., newval.f_float ) );
246     init_precalc_table(p_sys);
247     return VLC_SUCCESS;
248 }