1 /*****************************************************************************
2 * sharpen.c: Sharpen video filter
3 *****************************************************************************
4 * Copyright (C) 2003-2007 the VideoLAN team
7 * Author: Jérémy DEMEULE <dj_mulder at djduron dot no-ip dot org>
8 * Jean-Baptiste Kempf <jb at videolan dot org>
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.
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.
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 *****************************************************************************/
25 /* The sharpen filter. */
27 * static int filter[] = { -1, -1, -1,
32 /*****************************************************************************
34 *****************************************************************************/
40 #include <vlc_common.h>
41 #include <vlc_plugin.h>
43 #include "vlc_filter.h"
44 #include "filter_picture.h"
46 #define SIG_TEXT N_("Sharpen strength (0-2)")
47 #define SIG_LONGTEXT N_("Set the Sharpen strength, between 0 and 2. Defaults to 0.05.")
49 /*****************************************************************************
51 *****************************************************************************/
52 static int Create ( vlc_object_t * );
53 static void Destroy ( vlc_object_t * );
55 static picture_t *Filter( filter_t *, picture_t * );
56 static int SharpenCallback( vlc_object_t *, char const *,
57 vlc_value_t, vlc_value_t, void * );
59 #define FILTER_PREFIX "sharpen-"
61 /*****************************************************************************
63 *****************************************************************************/
65 set_description( N_("Augment contrast between contours.") )
66 set_shortname( N_("Sharpen video filter") )
67 set_category( CAT_VIDEO )
68 set_subcategory( SUBCAT_VIDEO_VFILTER )
69 set_capability( "video filter2", 0 )
70 add_float_with_range( "sharpen-sigma", 0.05, 0.0, 2.0, NULL,
71 SIG_TEXT, SIG_LONGTEXT, false )
72 add_shortcut( "sharpen" )
73 set_callbacks( Create, Destroy )
76 static const char *const ppsz_filter_options[] = {
80 /*****************************************************************************
81 * filter_sys_t: Sharpen video filter descriptor
82 *****************************************************************************
83 * This structure is part of the video output thread descriptor.
84 * It describes the Sharpen specific properties of an output thread.
85 *****************************************************************************/
93 /*****************************************************************************
94 * clip: avoid negative value and value > 255
95 *****************************************************************************/
96 inline static int32_t clip( int32_t a )
98 return (a > 255) ? 255 : (a < 0) ? 0 : a;
101 static void init_precalc_table(filter_sys_t *p_filter, float sigma)
103 for(int i = 0; i < 512; ++i)
105 p_filter->tab_precalc[i] = (i - 256) * sigma;
109 /*****************************************************************************
110 * Create: allocates Sharpen video thread output method
111 *****************************************************************************
112 * This function allocates and initializes a Sharpen vout method.
113 *****************************************************************************/
114 static int Create( vlc_object_t *p_this )
116 filter_t *p_filter = (filter_t *)p_this;
118 /* Allocate structure */
119 p_filter->p_sys = malloc( sizeof( filter_sys_t ) );
120 if( p_filter->p_sys == NULL )
123 p_filter->pf_video_filter = Filter;
125 config_ChainParse( p_filter, FILTER_PREFIX, ppsz_filter_options,
128 float sigma = var_CreateGetFloatCommand( p_filter, FILTER_PREFIX "sigma" );
129 init_precalc_table(p_filter->p_sys, sigma);
131 vlc_mutex_init( &p_filter->p_sys->lock );
132 var_AddCallback( p_filter, FILTER_PREFIX "sigma",
133 SharpenCallback, p_filter->p_sys );
139 /*****************************************************************************
140 * Destroy: destroy Sharpen video thread output method
141 *****************************************************************************
142 * Terminate an output method created by SharpenCreateOutputMethod
143 *****************************************************************************/
144 static void Destroy( vlc_object_t *p_this )
146 filter_t *p_filter = (filter_t *)p_this;
147 filter_sys_t *p_sys = p_filter->p_sys;
149 var_DelCallback( p_filter, FILTER_PREFIX "sigma", SharpenCallback, p_sys );
150 vlc_mutex_destroy( &p_sys->lock );
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
160 *****************************************************************************/
161 static picture_t *Filter( filter_t *p_filter, picture_t *p_pic )
165 uint8_t *p_src = NULL;
166 uint8_t *p_out = NULL;
170 const int v2 = 3; /* 2^3 = 8 */
172 if( !p_pic ) return NULL;
174 p_outpic = filter_NewPicture( p_filter );
177 picture_Release( p_pic );
181 /* process the Y plane */
182 p_src = p_pic->p[Y_PLANE].p_pixels;
183 p_out = p_outpic->p[Y_PLANE].p_pixels;
184 i_src_pitch = p_pic->p[Y_PLANE].i_visible_pitch;
186 /* perform convolution only on Y plane. Avoid border line. */
187 vlc_mutex_lock( &p_filter->p_sys->lock );
188 for( i = 0; i < p_pic->p[Y_PLANE].i_visible_lines; i++ )
190 if( (i == 0) || (i == p_pic->p[Y_PLANE].i_visible_lines - 1) )
192 for( j = 0; j < p_pic->p[Y_PLANE].i_visible_pitch; j++ )
193 p_out[i * i_src_pitch + j] = clip( p_src[i * i_src_pitch + j] );
196 for( j = 0; j < p_pic->p[Y_PLANE].i_visible_pitch; j++ )
198 if( (j == 0) || (j == p_pic->p[Y_PLANE].i_visible_pitch - 1) )
200 p_out[i * i_src_pitch + j] = p_src[i * i_src_pitch + j];
204 pix = (p_src[(i - 1) * i_src_pitch + j - 1] * v1) +
205 (p_src[(i - 1) * i_src_pitch + j ] * v1) +
206 (p_src[(i - 1) * i_src_pitch + j + 1] * v1) +
207 (p_src[(i ) * i_src_pitch + j - 1] * v1) +
208 (p_src[(i ) * i_src_pitch + j ] << v2) +
209 (p_src[(i ) * i_src_pitch + j + 1] * v1) +
210 (p_src[(i + 1) * i_src_pitch + j - 1] * v1) +
211 (p_src[(i + 1) * i_src_pitch + j ] * v1) +
212 (p_src[(i + 1) * i_src_pitch + j + 1] * v1);
214 pix = pix >= 0 ? clip(pix) : -clip(pix * -1);
215 p_out[i * i_src_pitch + j] = clip( p_src[i * i_src_pitch + j] +
216 p_filter->p_sys->tab_precalc[pix + 256] );
219 vlc_mutex_unlock( &p_filter->p_sys->lock );
221 vlc_memcpy( p_outpic->p[U_PLANE].p_pixels, p_pic->p[U_PLANE].p_pixels,
222 p_outpic->p[U_PLANE].i_lines * p_outpic->p[U_PLANE].i_pitch );
224 vlc_memcpy( p_outpic->p[V_PLANE].p_pixels, p_pic->p[V_PLANE].p_pixels,
225 p_outpic->p[V_PLANE].i_lines * p_outpic->p[V_PLANE].i_pitch );
228 return CopyInfoAndRelease( p_outpic, p_pic );
231 static int SharpenCallback( vlc_object_t *p_this, char const *psz_var,
232 vlc_value_t oldval, vlc_value_t newval,
235 VLC_UNUSED(p_this); VLC_UNUSED(oldval); VLC_UNUSED(psz_var);
236 filter_sys_t *p_sys = (filter_sys_t *)p_data;
238 vlc_mutex_lock( &p_sys->lock );
239 init_precalc_table(p_sys, __MIN( 2., __MAX( 0., newval.f_float ) ));
240 vlc_mutex_unlock( &p_sys->lock );