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