1 /*****************************************************************************
2 * sharpen.c: Sharpen video filter
3 *****************************************************************************
4 * Copyright (C) 2003 the VideoLAN team
5 * $Id: sharpen.c 18062 2006-11-26 14:20:34Z zorglub $
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 *****************************************************************************/
35 #include <stdlib.h> /* malloc(), free() */
41 #include "vlc_filter.h"
43 #define SIG_TEXT N_("Sharpen strength (0-2)")
44 #define SIG_LONGTEXT N_("Set the Sharpen strength, between 0 and 2. Defaults to 0.05.")
46 /*****************************************************************************
48 *****************************************************************************/
49 static int Create ( vlc_object_t * );
50 static void Destroy ( vlc_object_t * );
52 static picture_t *Filter( filter_t *, picture_t * );
54 #define FILTER_PREFIX "sharpen-"
56 /*****************************************************************************
58 *****************************************************************************/
60 set_description( _("Augment contrast between contours.") );
61 set_shortname( _("Sharpen video filter") );
62 set_category( CAT_VIDEO );
63 set_subcategory( SUBCAT_VIDEO_VFILTER );
64 set_capability( "video filter2", 0 );
65 add_float_with_range( "sharpen-sigma", 0.05, 0.0, 2.0, NULL,
66 SIG_TEXT, SIG_LONGTEXT, VLC_FALSE );
67 add_shortcut( "sharpen" );
68 set_callbacks( Create, Destroy );
71 static const char *ppsz_filter_options[] = {
75 /*****************************************************************************
76 * filter_sys_t: Sharpen video filter descriptor
77 *****************************************************************************
78 * This structure is part of the video output thread descriptor.
79 * It describes the Sharpen specific properties of an output thread.
80 *****************************************************************************/
87 /*****************************************************************************
88 * clip: avoid negative value and value > 255
89 *****************************************************************************/
90 inline static int32_t clip( int32_t a )
92 return (a > 255) ? 255 : (a < 0) ? 0 : a;
95 /*****************************************************************************
96 * Create: allocates Sharpen video thread output method
97 *****************************************************************************
98 * This function allocates and initializes a Sharpen vout method.
99 *****************************************************************************/
100 static int Create( vlc_object_t *p_this )
102 filter_t *p_filter = (filter_t *)p_this;
104 /* Allocate structure */
105 p_filter->p_sys = malloc( sizeof( filter_sys_t ) );
106 if( p_filter->p_sys == NULL )
108 msg_Err( p_filter, "out of memory" );
112 p_filter->pf_video_filter = Filter;
114 config_ChainParse( p_filter, FILTER_PREFIX, ppsz_filter_options,
117 var_Create( p_filter, FILTER_PREFIX "sigma",
118 VLC_VAR_FLOAT | VLC_VAR_DOINHERIT );
120 p_filter->p_sys->f_sigma = var_GetFloat(p_this, FILTER_PREFIX "sigma");
126 /*****************************************************************************
127 * Destroy: destroy Sharpen video thread output method
128 *****************************************************************************
129 * Terminate an output method created by SharpenCreateOutputMethod
130 *****************************************************************************/
131 static void Destroy( vlc_object_t *p_this )
133 filter_t *p_filter = (filter_t *)p_this;
134 free( p_filter->p_sys );
137 /*****************************************************************************
138 * Render: displays previously rendered output
139 *****************************************************************************
140 * This function send the currently rendered image to Invert image, waits
141 * until it is displayed and switch the two rendering buffers, preparing next
143 *****************************************************************************/
144 static picture_t *Filter( filter_t *p_filter, picture_t *p_pic )
149 uint8_t *p_src = NULL;
150 uint8_t *p_out = NULL;
154 const int v2 = 3; /* 2^3 = 8 */
156 if( !p_pic ) return NULL;
157 if( !p_filter ) return NULL;
158 if( !p_filter->p_sys ) return NULL;
160 p_outpic = p_filter->pf_vout_buffer_new( p_filter );
163 msg_Warn( p_filter, "can't get output picture" );
164 if( p_pic->pf_release )
165 p_pic->pf_release( p_pic );
169 /* process the Y plane */
170 p_src = p_pic->p[Y_PLANE].p_pixels;
171 p_out = p_outpic->p[Y_PLANE].p_pixels;
172 if( !p_src || !p_out )
174 msg_Warn( p_filter, "can't get Y plane" );
175 if( p_pic->pf_release )
176 p_pic->pf_release( p_pic );
180 i_src_pitch = p_pic->p[Y_PLANE].i_visible_pitch;
182 /* perform convolution only on Y plane. Avoid border line. */
183 for( i = 0; i < p_pic->p[Y_PLANE].i_visible_lines; i++ )
185 if( (i == 0) || (i == p_pic->p[Y_PLANE].i_visible_lines - 1) )
187 for( j = 0; j < p_pic->p[Y_PLANE].i_visible_pitch; j++ )
188 p_out[i * i_src_pitch + j] = clip( p_src[i * i_src_pitch + j] );
191 for( j = 0; j < p_pic->p[Y_PLANE].i_visible_pitch; j++ )
193 if( (j == 0) || (j == p_pic->p[Y_PLANE].i_visible_pitch - 1) )
195 p_out[i * i_src_pitch + j] = p_src[i * i_src_pitch + j];
199 pix = (p_src[(i - 1) * i_src_pitch + j - 1] * v1) +
200 (p_src[(i - 1) * i_src_pitch + j ] * v1) +
201 (p_src[(i - 1) * i_src_pitch + j + 1] * v1) +
202 (p_src[(i ) * i_src_pitch + j - 1] * v1) +
203 (p_src[(i ) * i_src_pitch + j ] << v2) +
204 (p_src[(i ) * i_src_pitch + j + 1] * v1) +
205 (p_src[(i + 1) * i_src_pitch + j - 1] * v1) +
206 (p_src[(i + 1) * i_src_pitch + j ] * v1) +
207 (p_src[(i + 1) * i_src_pitch + j + 1] * v1);
209 p_out[i * i_src_pitch + j] = clip( p_src[i * i_src_pitch + j] +
210 (p_filter->p_sys->f_sigma * pix) );
215 p_filter->p_libvlc->pf_memcpy( p_outpic->p[U_PLANE].p_pixels,
216 p_pic->p[U_PLANE].p_pixels,
217 p_outpic->p[U_PLANE].i_lines * p_outpic->p[U_PLANE].i_pitch );
219 p_filter->p_libvlc->pf_memcpy( p_outpic->p[V_PLANE].p_pixels,
220 p_pic->p[V_PLANE].p_pixels,
221 p_outpic->p[V_PLANE].i_lines * p_outpic->p[V_PLANE].i_pitch );
224 p_outpic->date = p_pic->date;
225 p_outpic->b_force = p_pic->b_force;
226 p_outpic->i_nb_fields = p_pic->i_nb_fields;
227 p_outpic->b_progressive = p_pic->b_progressive;
228 p_outpic->b_top_field_first = p_pic->b_top_field_first;
230 if( p_pic->pf_release )
231 p_pic->pf_release( p_pic );