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