]> git.sesse.net Git - vlc/blob - modules/video_filter/sharpen.c
Sharpen Video-Filter.
[vlc] / modules / video_filter / sharpen.c
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 $
6  *
7  * Author: Jérémy DEMEULE <dj_mulder at djduron dot no-ip dot org>
8  *
9  * This program is free software; you can redistribute it and/or modify
10  * it under the terms of the GNU General Public License as published by
11  * the Free Software Foundation; either version 2 of the License, or
12  * (at your option) any later version.
13  *
14  * This program is distributed in the hope that it will be useful,
15  * but WITHOUT ANY WARRANTY; without even the implied warranty of
16  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
17  * GNU General Public License for more details.
18  *
19  * You should have received a copy of the GNU General Public License
20  * along with this program; if not, write to the Free Software
21  * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston MA 02110-1301, USA.
22  *****************************************************************************/
23
24 /* The sharpen filter. */
25 /*
26  * static int filter[] = { -1, -1, -1,
27  *                         -1,  8, -1,
28  *                         -1, -1, -1 };
29  */
30
31 /*****************************************************************************
32  * Preamble
33  *****************************************************************************/
34 #include <stdlib.h>                                      /* malloc(), free() */
35 #include <string.h>
36
37 #include <vlc/vlc.h>
38 #include <vlc_vout.h>
39
40 #include "vlc_filter.h"
41
42 #define SIG_TEXT N_("Sharpen strength (0-2)")
43 #define SIG_LONGTEXT N_("Set the Sharpen strength, between 0 and 2. Defaults to 0.05.")
44
45 /*****************************************************************************
46  * Local prototypes
47  *****************************************************************************/
48 static int  Create    ( vlc_object_t * );
49 static void Destroy   ( vlc_object_t * );
50
51 static picture_t *Filter( filter_t *, picture_t * );
52
53 /*****************************************************************************
54  * Module descriptor
55  *****************************************************************************/
56 vlc_module_begin();
57     set_description( _("Augment contrast between contours.") );
58     set_shortname( _("Sharpen video filter") );
59     set_category( CAT_VIDEO );
60     set_subcategory( SUBCAT_VIDEO_VFILTER );
61     set_capability( "video filter2", 0 );
62     add_float_with_range( "sharpen-sigma", 0.05, 0.0, 2.0, NULL,
63         SIG_TEXT, SIG_LONGTEXT, VLC_FALSE );
64     add_shortcut( "sharpen" );
65     set_callbacks( Create, Destroy );
66 vlc_module_end();
67
68 /*****************************************************************************
69  * filter_sys_t: Sharpen video filter descriptor
70  *****************************************************************************
71  * This structure is part of the video output thread descriptor.
72  * It describes the Sharpen specific properties of an output thread.
73  *****************************************************************************/
74
75 struct filter_sys_t
76 {
77     float f_sigma;
78 };
79
80 /*****************************************************************************
81  * clip: avoid negative value and value > 255
82  *****************************************************************************/
83 inline static int32_t clip( int32_t a )
84 {
85     return (a > 255) ? 255 : (a < 0) ? 0 : a;
86 }
87
88 /*****************************************************************************
89  * Create: allocates Sharpen video thread output method
90  *****************************************************************************
91  * This function allocates and initializes a Sharpen vout method.
92  *****************************************************************************/
93 static int Create( vlc_object_t *p_this )
94 {
95     filter_t *p_filter = (filter_t *)p_this;
96
97     /* Allocate structure */
98     p_filter->p_sys = malloc( sizeof( filter_sys_t ) );
99     if( p_filter->p_sys == NULL )
100     {
101         msg_Err( p_filter, "out of memory" );
102         return VLC_ENOMEM;
103     }
104
105     p_filter->pf_video_filter = Filter;
106
107     p_filter->p_sys->f_sigma = var_GetFloat(p_this, "sharpen-sigma");
108
109     return VLC_SUCCESS;
110 }
111
112
113 /*****************************************************************************
114  * Destroy: destroy Sharpen video thread output method
115  *****************************************************************************
116  * Terminate an output method created by SharpenCreateOutputMethod
117  *****************************************************************************/
118 static void Destroy( vlc_object_t *p_this )
119 {
120     filter_t *p_filter = (filter_t *)p_this;
121     free( p_filter->p_sys );
122 }
123
124 /*****************************************************************************
125  * Render: displays previously rendered output
126  *****************************************************************************
127  * This function send the currently rendered image to Invert image, waits
128  * until it is displayed and switch the two rendering buffers, preparing next
129  * frame.
130  *****************************************************************************/
131 static picture_t *Filter( filter_t *p_filter, picture_t *p_pic )
132 {
133     picture_t *p_outpic;
134     int i, j;
135     vlc_value_t val;
136     uint8_t *p_src = NULL;
137     uint8_t *p_out = NULL;
138     int i_src_pitch;
139     int pix;
140     const int v1 = -1;
141     const int v2 = 3; /* 2^3 = 8 */
142
143     if( !p_pic ) return NULL;
144     if( !p_filter ) return NULL;
145     if( !p_filter->p_sys ) return NULL;
146
147     p_outpic = p_filter->pf_vout_buffer_new( p_filter );
148     if( !p_outpic )
149     {
150         msg_Warn( p_filter, "can't get output picture" );
151         if( p_pic->pf_release )
152             p_pic->pf_release( p_pic );
153         return NULL;
154     }
155
156     /* process the Y plane */
157     p_src = p_pic->p[Y_PLANE].p_pixels;
158     p_out = p_outpic->p[Y_PLANE].p_pixels;
159     if( !p_src || !p_out )
160     {
161         msg_Warn( p_filter, "can't get Y plane" );
162         if( p_pic->pf_release ) 
163             p_pic->pf_release( p_pic );
164         return NULL;
165     }
166
167     i_src_pitch = p_pic->p[Y_PLANE].i_visible_pitch;
168
169     /* perform convolution only on Y plane. Avoid border line. */
170     for( i = 0; i < p_pic->p[Y_PLANE].i_visible_lines; i++ )
171     {
172         if( (i == 0) || (i == p_pic->p[Y_PLANE].i_visible_lines - 1) )
173         {
174             for( j = 0; j < p_pic->p[Y_PLANE].i_visible_pitch; j++ )
175                 p_out[i * i_src_pitch + j] = clip( p_src[i * i_src_pitch + j] );
176             continue ;
177         }
178         for( j = 0; j < p_pic->p[Y_PLANE].i_visible_pitch; j++ )
179         {
180             if( (j == 0) || (j == p_pic->p[Y_PLANE].i_visible_pitch - 1) )
181             {
182                 p_out[i * i_src_pitch + j] = p_src[i * i_src_pitch + j];
183                 continue ;
184             }
185
186             pix = (p_src[(i - 1) * i_src_pitch + j - 1] * v1) +
187                   (p_src[(i - 1) * i_src_pitch + j    ] * v1) +
188                   (p_src[(i - 1) * i_src_pitch + j + 1] * v1) +
189                   (p_src[(i    ) * i_src_pitch + j - 1] * v1) +
190                   (p_src[(i    ) * i_src_pitch + j    ] << v2) +
191                   (p_src[(i    ) * i_src_pitch + j + 1] * v1) +
192                   (p_src[(i + 1) * i_src_pitch + j - 1] * v1) +
193                   (p_src[(i + 1) * i_src_pitch + j    ] * v1) +
194                   (p_src[(i + 1) * i_src_pitch + j + 1] * v1);
195
196             p_out[i * i_src_pitch + j] =  clip( p_src[i * i_src_pitch + j] +
197                         (p_filter->p_sys->f_sigma * pix) );
198         }
199     }
200
201
202     p_filter->p_libvlc->pf_memcpy( p_outpic->p[U_PLANE].p_pixels,
203             p_pic->p[U_PLANE].p_pixels,
204             p_outpic->p[U_PLANE].i_lines * p_outpic->p[U_PLANE].i_pitch );
205
206     p_filter->p_libvlc->pf_memcpy( p_outpic->p[V_PLANE].p_pixels,
207             p_pic->p[V_PLANE].p_pixels,
208             p_outpic->p[V_PLANE].i_lines * p_outpic->p[V_PLANE].i_pitch );
209
210
211     p_outpic->date = p_pic->date;
212     p_outpic->b_force = p_pic->b_force;
213     p_outpic->i_nb_fields = p_pic->i_nb_fields;
214     p_outpic->b_progressive = p_pic->b_progressive;
215     p_outpic->b_top_field_first = p_pic->b_top_field_first;
216
217     if( p_pic->pf_release )
218         p_pic->pf_release( p_pic );
219
220     return p_outpic;
221 }