]> git.sesse.net Git - vlc/blob - modules/video_filter/sharpen.c
Change in copyright and propedit.
[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  *         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 #include <stdlib.h>                                      /* malloc(), free() */
36 #include <string.h>
37
38 #include <vlc/vlc.h>
39 #include <vlc_vout.h>
40
41 #include "vlc_filter.h"
42
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.")
45
46 /*****************************************************************************
47  * Local prototypes
48  *****************************************************************************/
49 static int  Create    ( vlc_object_t * );
50 static void Destroy   ( vlc_object_t * );
51
52 static picture_t *Filter( filter_t *, picture_t * );
53
54 /*****************************************************************************
55  * Module descriptor
56  *****************************************************************************/
57 vlc_module_begin();
58     set_description( _("Augment contrast between contours.") );
59     set_shortname( _("Sharpen video filter") );
60     set_category( CAT_VIDEO );
61     set_subcategory( SUBCAT_VIDEO_VFILTER );
62     set_capability( "video filter2", 0 );
63     add_float_with_range( "sharpen-sigma", 0.05, 0.0, 2.0, NULL,
64         SIG_TEXT, SIG_LONGTEXT, VLC_FALSE );
65     add_shortcut( "sharpen" );
66     set_callbacks( Create, Destroy );
67 vlc_module_end();
68
69 /*****************************************************************************
70  * filter_sys_t: Sharpen video filter descriptor
71  *****************************************************************************
72  * This structure is part of the video output thread descriptor.
73  * It describes the Sharpen specific properties of an output thread.
74  *****************************************************************************/
75
76 struct filter_sys_t
77 {
78     float f_sigma;
79 };
80
81 /*****************************************************************************
82  * clip: avoid negative value and value > 255
83  *****************************************************************************/
84 inline static int32_t clip( int32_t a )
85 {
86     return (a > 255) ? 255 : (a < 0) ? 0 : a;
87 }
88
89 /*****************************************************************************
90  * Create: allocates Sharpen video thread output method
91  *****************************************************************************
92  * This function allocates and initializes a Sharpen vout method.
93  *****************************************************************************/
94 static int Create( vlc_object_t *p_this )
95 {
96     filter_t *p_filter = (filter_t *)p_this;
97
98     /* Allocate structure */
99     p_filter->p_sys = malloc( sizeof( filter_sys_t ) );
100     if( p_filter->p_sys == NULL )
101     {
102         msg_Err( p_filter, "out of memory" );
103         return VLC_ENOMEM;
104     }
105
106     p_filter->pf_video_filter = Filter;
107
108     p_filter->p_sys->f_sigma = var_GetFloat(p_this, "sharpen-sigma");
109
110     return VLC_SUCCESS;
111 }
112
113
114 /*****************************************************************************
115  * Destroy: destroy Sharpen video thread output method
116  *****************************************************************************
117  * Terminate an output method created by SharpenCreateOutputMethod
118  *****************************************************************************/
119 static void Destroy( vlc_object_t *p_this )
120 {
121     filter_t *p_filter = (filter_t *)p_this;
122     free( p_filter->p_sys );
123 }
124
125 /*****************************************************************************
126  * Render: displays previously rendered output
127  *****************************************************************************
128  * This function send the currently rendered image to Invert image, waits
129  * until it is displayed and switch the two rendering buffers, preparing next
130  * frame.
131  *****************************************************************************/
132 static picture_t *Filter( filter_t *p_filter, picture_t *p_pic )
133 {
134     picture_t *p_outpic;
135     int i, j;
136     vlc_value_t val;
137     uint8_t *p_src = NULL;
138     uint8_t *p_out = NULL;
139     int i_src_pitch;
140     int pix;
141     const int v1 = -1;
142     const int v2 = 3; /* 2^3 = 8 */
143
144     if( !p_pic ) return NULL;
145     if( !p_filter ) return NULL;
146     if( !p_filter->p_sys ) return NULL;
147
148     p_outpic = p_filter->pf_vout_buffer_new( p_filter );
149     if( !p_outpic )
150     {
151         msg_Warn( p_filter, "can't get output picture" );
152         if( p_pic->pf_release )
153             p_pic->pf_release( p_pic );
154         return NULL;
155     }
156
157     /* process the Y plane */
158     p_src = p_pic->p[Y_PLANE].p_pixels;
159     p_out = p_outpic->p[Y_PLANE].p_pixels;
160     if( !p_src || !p_out )
161     {
162         msg_Warn( p_filter, "can't get Y plane" );
163         if( p_pic->pf_release ) 
164             p_pic->pf_release( p_pic );
165         return NULL;
166     }
167
168     i_src_pitch = p_pic->p[Y_PLANE].i_visible_pitch;
169
170     /* perform convolution only on Y plane. Avoid border line. */
171     for( i = 0; i < p_pic->p[Y_PLANE].i_visible_lines; i++ )
172     {
173         if( (i == 0) || (i == p_pic->p[Y_PLANE].i_visible_lines - 1) )
174         {
175             for( j = 0; j < p_pic->p[Y_PLANE].i_visible_pitch; j++ )
176                 p_out[i * i_src_pitch + j] = clip( p_src[i * i_src_pitch + j] );
177             continue ;
178         }
179         for( j = 0; j < p_pic->p[Y_PLANE].i_visible_pitch; j++ )
180         {
181             if( (j == 0) || (j == p_pic->p[Y_PLANE].i_visible_pitch - 1) )
182             {
183                 p_out[i * i_src_pitch + j] = p_src[i * i_src_pitch + j];
184                 continue ;
185             }
186
187             pix = (p_src[(i - 1) * i_src_pitch + j - 1] * v1) +
188                   (p_src[(i - 1) * i_src_pitch + j    ] * v1) +
189                   (p_src[(i - 1) * i_src_pitch + j + 1] * v1) +
190                   (p_src[(i    ) * i_src_pitch + j - 1] * v1) +
191                   (p_src[(i    ) * i_src_pitch + j    ] << v2) +
192                   (p_src[(i    ) * i_src_pitch + j + 1] * v1) +
193                   (p_src[(i + 1) * i_src_pitch + j - 1] * v1) +
194                   (p_src[(i + 1) * i_src_pitch + j    ] * v1) +
195                   (p_src[(i + 1) * i_src_pitch + j + 1] * v1);
196
197             p_out[i * i_src_pitch + j] =  clip( p_src[i * i_src_pitch + j] +
198                         (p_filter->p_sys->f_sigma * pix) );
199         }
200     }
201
202
203     p_filter->p_libvlc->pf_memcpy( p_outpic->p[U_PLANE].p_pixels,
204             p_pic->p[U_PLANE].p_pixels,
205             p_outpic->p[U_PLANE].i_lines * p_outpic->p[U_PLANE].i_pitch );
206
207     p_filter->p_libvlc->pf_memcpy( p_outpic->p[V_PLANE].p_pixels,
208             p_pic->p[V_PLANE].p_pixels,
209             p_outpic->p[V_PLANE].i_lines * p_outpic->p[V_PLANE].i_pitch );
210
211
212     p_outpic->date = p_pic->date;
213     p_outpic->b_force = p_pic->b_force;
214     p_outpic->i_nb_fields = p_pic->i_nb_fields;
215     p_outpic->b_progressive = p_pic->b_progressive;
216     p_outpic->b_top_field_first = p_pic->b_top_field_first;
217
218     if( p_pic->pf_release )
219         p_pic->pf_release( p_pic );
220
221     return p_outpic;
222 }