A little more detail regarding using my github copies of the code with where it's...
[vlc/adversarial.git] / modules / video_filter / sharpen.c
blob499358e83fe1dc7dd233fd312019fa75a0f5928e
1 /*****************************************************************************
2 * sharpen.c: Sharpen video filter
3 *****************************************************************************
4 * Copyright (C) 2003-2007 VLC authors and VideoLAN
5 * $Id$
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 it
11 * under the terms of the GNU Lesser General Public License as published by
12 * the Free Software Foundation; either version 2.1 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 Lesser General Public License for more details.
20 * You should have received a copy of the GNU Lesser General Public License
21 * along with this program; if not, write to the Free Software Foundation,
22 * Inc., 51 Franklin Street, Fifth Floor, Boston MA 02110-1301, USA.
23 *****************************************************************************/
25 /* The sharpen filter. */
27 * static int filter[] = { -1, -1, -1,
28 * -1, 8, -1,
29 * -1, -1, -1 };
32 /*****************************************************************************
33 * Preamble
34 *****************************************************************************/
36 #ifdef HAVE_CONFIG_H
37 # include "config.h"
38 #endif
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 /*****************************************************************************
50 * Local prototypes
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 SHARPEN_HELP N_("Augment contrast between contours.")
60 #define FILTER_PREFIX "sharpen-"
62 /*****************************************************************************
63 * Module descriptor
64 *****************************************************************************/
65 vlc_module_begin ()
66 set_description( N_("Sharpen video filter") )
67 set_shortname( N_("Sharpen") )
68 set_help(SHARPEN_HELP)
69 set_category( CAT_VIDEO )
70 set_subcategory( SUBCAT_VIDEO_VFILTER )
71 set_capability( "video filter2", 0 )
72 add_float_with_range( "sharpen-sigma", 0.05, 0.0, 2.0,
73 SIG_TEXT, SIG_LONGTEXT, false )
74 add_shortcut( "sharpen" )
75 set_callbacks( Create, Destroy )
76 vlc_module_end ()
78 static const char *const ppsz_filter_options[] = {
79 "sigma", NULL
82 /*****************************************************************************
83 * filter_sys_t: Sharpen video filter descriptor
84 *****************************************************************************
85 * This structure is part of the video output thread descriptor.
86 * It describes the Sharpen specific properties of an output thread.
87 *****************************************************************************/
89 struct filter_sys_t
91 vlc_mutex_t lock;
92 int tab_precalc[512];
95 /*****************************************************************************
96 * clip: avoid negative value and value > 255
97 *****************************************************************************/
98 inline static int32_t clip( int32_t a )
100 return (a > 255) ? 255 : (a < 0) ? 0 : a;
103 static void init_precalc_table(filter_sys_t *p_filter, float sigma)
105 for(int i = 0; i < 512; ++i)
107 p_filter->tab_precalc[i] = (i - 256) * sigma;
111 /*****************************************************************************
112 * Create: allocates Sharpen video thread output method
113 *****************************************************************************
114 * This function allocates and initializes a Sharpen vout method.
115 *****************************************************************************/
116 static int Create( vlc_object_t *p_this )
118 filter_t *p_filter = (filter_t *)p_this;
120 const vlc_fourcc_t fourcc = p_filter->fmt_in.video.i_chroma;
121 const vlc_chroma_description_t *p_chroma = vlc_fourcc_GetChromaDescription( fourcc );
122 if( !p_chroma || p_chroma->plane_count != 3 || p_chroma->pixel_size != 1 ) {
123 msg_Err( p_filter, "Unsupported chroma (%4.4s)", (char*)&fourcc );
124 return VLC_EGENERIC;
127 /* Allocate structure */
128 p_filter->p_sys = malloc( sizeof( filter_sys_t ) );
129 if( p_filter->p_sys == NULL )
130 return VLC_ENOMEM;
132 p_filter->pf_video_filter = Filter;
134 config_ChainParse( p_filter, FILTER_PREFIX, ppsz_filter_options,
135 p_filter->p_cfg );
137 float sigma = var_CreateGetFloatCommand( p_filter, FILTER_PREFIX "sigma" );
138 init_precalc_table(p_filter->p_sys, sigma);
140 vlc_mutex_init( &p_filter->p_sys->lock );
141 var_AddCallback( p_filter, FILTER_PREFIX "sigma",
142 SharpenCallback, p_filter->p_sys );
144 return VLC_SUCCESS;
148 /*****************************************************************************
149 * Destroy: destroy Sharpen video thread output method
150 *****************************************************************************
151 * Terminate an output method created by SharpenCreateOutputMethod
152 *****************************************************************************/
153 static void Destroy( vlc_object_t *p_this )
155 filter_t *p_filter = (filter_t *)p_this;
156 filter_sys_t *p_sys = p_filter->p_sys;
158 var_DelCallback( p_filter, FILTER_PREFIX "sigma", SharpenCallback, p_sys );
159 vlc_mutex_destroy( &p_sys->lock );
160 free( p_sys );
163 /*****************************************************************************
164 * Render: displays previously rendered output
165 *****************************************************************************
166 * This function send the currently rendered image to Invert image, waits
167 * until it is displayed and switch the two rendering buffers, preparing next
168 * frame.
169 *****************************************************************************/
170 static picture_t *Filter( filter_t *p_filter, picture_t *p_pic )
172 picture_t *p_outpic;
173 int i, j;
174 uint8_t *p_src = NULL;
175 uint8_t *p_out = NULL;
176 int i_src_pitch;
177 int i_out_pitch;
178 int pix;
179 const int v1 = -1;
180 const int v2 = 3; /* 2^3 = 8 */
182 if( !p_pic ) return NULL;
184 p_outpic = filter_NewPicture( p_filter );
185 if( !p_outpic )
187 picture_Release( p_pic );
188 return NULL;
191 /* process the Y plane */
192 p_src = p_pic->p[Y_PLANE].p_pixels;
193 p_out = p_outpic->p[Y_PLANE].p_pixels;
194 i_src_pitch = p_pic->p[Y_PLANE].i_pitch;
195 i_out_pitch = p_outpic->p[Y_PLANE].i_pitch;
197 /* perform convolution only on Y plane. Avoid border line. */
198 vlc_mutex_lock( &p_filter->p_sys->lock );
199 for( i = 0; i < p_pic->p[Y_PLANE].i_visible_lines; i++ )
201 if( (i == 0) || (i == p_pic->p[Y_PLANE].i_visible_lines - 1) )
203 for( j = 0; j < p_pic->p[Y_PLANE].i_visible_pitch; j++ )
204 p_out[i * i_out_pitch + j] = clip( p_src[i * i_src_pitch + j] );
205 continue ;
207 for( j = 0; j < p_pic->p[Y_PLANE].i_visible_pitch; j++ )
209 if( (j == 0) || (j == p_pic->p[Y_PLANE].i_visible_pitch - 1) )
211 p_out[i * i_out_pitch + j] = p_src[i * i_src_pitch + j];
212 continue ;
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);
225 pix = pix >= 0 ? clip(pix) : -clip(pix * -1);
226 p_out[i * i_out_pitch + j] = clip( p_src[i * i_src_pitch + j] +
227 p_filter->p_sys->tab_precalc[pix + 256] );
230 vlc_mutex_unlock( &p_filter->p_sys->lock );
232 plane_CopyPixels( &p_outpic->p[U_PLANE], &p_pic->p[U_PLANE] );
233 plane_CopyPixels( &p_outpic->p[V_PLANE], &p_pic->p[V_PLANE] );
235 return CopyInfoAndRelease( p_outpic, p_pic );
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 )
242 VLC_UNUSED(p_this); VLC_UNUSED(oldval); VLC_UNUSED(psz_var);
243 filter_sys_t *p_sys = (filter_sys_t *)p_data;
245 vlc_mutex_lock( &p_sys->lock );
246 init_precalc_table( p_sys, VLC_CLIP( newval.f_float, 0.f, 2.f ) );
247 vlc_mutex_unlock( &p_sys->lock );
248 return VLC_SUCCESS;