sharpen: remove precalc_table, replace locks with atomic accesses
[vlc.git] / modules / video_filter / sharpen.c
blob65268cc6a411962604d81f39da5fe9563c3d0d26
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_atomic.h>
42 #include <vlc_plugin.h>
43 #include <vlc_filter.h>
44 #include <vlc_picture.h>
45 #include "filter_picture.h"
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.")
50 /*****************************************************************************
51 * Local prototypes
52 *****************************************************************************/
53 static int Create ( vlc_object_t * );
54 static void Destroy ( vlc_object_t * );
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 * );
60 #define SHARPEN_HELP N_("Augment contrast between contours.")
61 #define FILTER_PREFIX "sharpen-"
63 /*****************************************************************************
64 * Module descriptor
65 *****************************************************************************/
66 vlc_module_begin ()
67 set_description( N_("Sharpen video filter") )
68 set_shortname( N_("Sharpen") )
69 set_help(SHARPEN_HELP)
70 set_category( CAT_VIDEO )
71 set_subcategory( SUBCAT_VIDEO_VFILTER )
72 set_capability( "video filter", 0 )
73 add_float_with_range( FILTER_PREFIX "sigma", 0.05, 0.0, 2.0,
74 SIG_TEXT, SIG_LONGTEXT, false )
75 change_safe()
76 add_shortcut( "sharpen" )
77 set_callbacks( Create, Destroy )
78 vlc_module_end ()
80 static const char *const ppsz_filter_options[] = {
81 "sigma", NULL
84 /*****************************************************************************
85 * filter_sys_t: Sharpen video filter descriptor
86 *****************************************************************************
87 * This structure is part of the video output thread descriptor.
88 * It describes the Sharpen specific properties of an output thread.
89 *****************************************************************************/
91 struct filter_sys_t
93 atomic_int sigma;
96 /*****************************************************************************
97 * Create: allocates Sharpen video thread output method
98 *****************************************************************************
99 * This function allocates and initializes a Sharpen vout method.
100 *****************************************************************************/
101 static int Create( vlc_object_t *p_this )
103 filter_t *p_filter = (filter_t *)p_this;
105 const vlc_fourcc_t fourcc = p_filter->fmt_in.video.i_chroma;
106 const vlc_chroma_description_t *p_chroma = vlc_fourcc_GetChromaDescription( fourcc );
107 if( !p_chroma || p_chroma->plane_count != 3 ||
108 (p_chroma->pixel_size != 1 &&
109 p_filter->fmt_in.video.i_chroma != VLC_CODEC_I420_10L &&
110 p_filter->fmt_in.video.i_chroma != VLC_CODEC_I420_10B)) {
111 msg_Err( p_filter, "Unsupported chroma (%4.4s)", (char*)&fourcc );
112 return VLC_EGENERIC;
115 /* Allocate structure */
116 p_filter->p_sys = malloc( sizeof( filter_sys_t ) );
117 if( p_filter->p_sys == NULL )
118 return VLC_ENOMEM;
120 p_filter->pf_video_filter = Filter;
122 config_ChainParse( p_filter, FILTER_PREFIX, ppsz_filter_options,
123 p_filter->p_cfg );
125 atomic_init(&p_filter->p_sys->sigma,
126 var_CreateGetFloatCommand(p_filter, FILTER_PREFIX "sigma")
127 * (1 << 20));
129 var_AddCallback( p_filter, FILTER_PREFIX "sigma",
130 SharpenCallback, p_filter->p_sys );
132 return VLC_SUCCESS;
136 /*****************************************************************************
137 * Destroy: destroy Sharpen video thread output method
138 *****************************************************************************
139 * Terminate an output method created by SharpenCreateOutputMethod
140 *****************************************************************************/
141 static void Destroy( vlc_object_t *p_this )
143 filter_t *p_filter = (filter_t *)p_this;
144 filter_sys_t *p_sys = p_filter->p_sys;
146 var_DelCallback( p_filter, FILTER_PREFIX "sigma", SharpenCallback, p_sys );
147 free( p_sys );
150 /*****************************************************************************
151 * Render: displays previously rendered output
152 *****************************************************************************
153 * This function send the currently rendered image to Invert image, waits
154 * until it is displayed and switch the two rendering buffers, preparing next
155 * frame.
156 *****************************************************************************/
158 #define IS_YUV_420_10BITS(fmt) (fmt == VLC_CODEC_I420_10L || fmt == VLC_CODEC_I420_10B)
160 #define SHARPEN_FRAME(maxval) \
161 do \
163 data_t *restrict p_src = (data_t *)p_pic->p[Y_PLANE].p_pixels; \
164 data_t *restrict p_out = (data_t *)p_outpic->p[Y_PLANE].p_pixels; \
165 const unsigned data_sz = sizeof(data_t); \
166 const int i_src_line_len = p_outpic->p[Y_PLANE].i_pitch / data_sz; \
167 const int i_out_line_len = p_pic->p[Y_PLANE].i_pitch / data_sz; \
169 memcpy(p_out, p_src, i_visible_pitch); \
171 for( unsigned i = 1; i < i_visible_lines - 1; i++ ) \
173 p_out[i * i_out_line_len] = p_src[i * i_src_line_len]; \
175 for( unsigned j = data_sz; j < i_visible_pitch - 1; j++ ) \
177 pix = (p_src[(i - 1) * i_src_line_len + j - 1] * v[0]) + \
178 (p_src[(i - 1) * i_src_line_len + j ] * v[0]) + \
179 (p_src[(i - 1) * i_src_line_len + j + 1] * v[0]) + \
180 (p_src[(i ) * i_src_line_len + j - 1] * v[0]) + \
181 (p_src[(i ) * i_src_line_len + j ] << v[1]) + \
182 (p_src[(i ) * i_src_line_len + j + 1] * v[0]) + \
183 (p_src[(i + 1) * i_src_line_len + j - 1] * v[0]) + \
184 (p_src[(i + 1) * i_src_line_len + j ] * v[0]) + \
185 (p_src[(i + 1) * i_src_line_len + j + 1] * v[0]); \
187 pix = pix >= 0 ? VLC_CLIP(pix, 0, maxval) : -VLC_CLIP(pix * -1, 0, maxval); \
188 p_out[i * i_out_line_len + j] = VLC_CLIP( p_src[i * i_src_line_len + j] + ((pix * atomic_load(&p_filter->p_sys->sigma)) >> 20), 0, maxval); \
190 p_out[i * i_out_line_len + i_visible_pitch / 2 - 1] = \
191 p_src[i * i_src_line_len + i_visible_pitch / 2 - 1]; \
193 memcpy(&p_out[(i_visible_lines - 1) * i_out_line_len], \
194 &p_src[(i_visible_lines - 1) * i_src_line_len], i_visible_pitch); \
195 } while (0);
197 static picture_t *Filter( filter_t *p_filter, picture_t *p_pic )
199 picture_t *p_outpic;
200 int pix;
201 const int v[2] = { -1, 3 /* 2^3 = 8 */ };
202 const unsigned i_visible_lines = p_pic->p[Y_PLANE].i_visible_lines;
203 const unsigned i_visible_pitch = p_pic->p[Y_PLANE].i_visible_pitch;
205 p_outpic = filter_NewPicture( p_filter );
206 if( !p_outpic )
208 picture_Release( p_pic );
209 return NULL;
212 if (!IS_YUV_420_10BITS(p_pic->format.i_chroma))
214 typedef uint8_t data_t;
216 SHARPEN_FRAME(255);
218 else
220 typedef uint16_t data_t;
222 SHARPEN_FRAME(1023);
225 plane_CopyPixels( &p_outpic->p[U_PLANE], &p_pic->p[U_PLANE] );
226 plane_CopyPixels( &p_outpic->p[V_PLANE], &p_pic->p[V_PLANE] );
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,
233 void *p_data )
235 VLC_UNUSED(p_this); VLC_UNUSED(oldval); VLC_UNUSED(psz_var);
236 filter_sys_t *p_sys = (filter_sys_t *)p_data;
238 atomic_store(&p_sys->sigma,
239 VLC_CLIP(newval.f_float, 0.f, 2.f) * (1 << 20));
241 return VLC_SUCCESS;