1 /*****************************************************************************
2 * sharpen.c: Sharpen video filter
3 *****************************************************************************
4 * Copyright (C) 2003-2007 VLC authors and VideoLAN
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,
32 /*****************************************************************************
34 *****************************************************************************/
41 #include <vlc_common.h>
42 #include <vlc_atomic.h>
43 #include <vlc_plugin.h>
44 #include <vlc_filter.h>
45 #include <vlc_picture.h>
46 #include "filter_picture.h"
48 #define SIG_TEXT N_("Sharpen strength (0-2)")
49 #define SIG_LONGTEXT N_("Set the Sharpen strength, between 0 and 2. Defaults to 0.05.")
51 /*****************************************************************************
53 *****************************************************************************/
54 static int Create ( vlc_object_t
* );
55 static void Destroy ( vlc_object_t
* );
57 static picture_t
*Filter( filter_t
*, picture_t
* );
58 static int SharpenCallback( vlc_object_t
*, char const *,
59 vlc_value_t
, vlc_value_t
, void * );
61 #define SHARPEN_HELP N_("Augment contrast between contours.")
62 #define FILTER_PREFIX "sharpen-"
64 /*****************************************************************************
66 *****************************************************************************/
68 set_description( N_("Sharpen video filter") )
69 set_shortname( N_("Sharpen") )
70 set_help(SHARPEN_HELP
)
71 set_category( CAT_VIDEO
)
72 set_subcategory( SUBCAT_VIDEO_VFILTER
)
73 set_capability( "video filter", 0 )
74 add_float_with_range( FILTER_PREFIX
"sigma", 0.05, 0.0, 2.0,
75 SIG_TEXT
, SIG_LONGTEXT
, false )
77 add_shortcut( "sharpen" )
78 set_callbacks( Create
, Destroy
)
81 static const char *const ppsz_filter_options
[] = {
85 /*****************************************************************************
86 * filter_sys_t: Sharpen video filter descriptor
87 *****************************************************************************
88 * This structure is part of the video output thread descriptor.
89 * It describes the Sharpen specific properties of an output thread.
90 *****************************************************************************/
97 /*****************************************************************************
98 * Create: allocates Sharpen video thread output method
99 *****************************************************************************
100 * This function allocates and initializes a Sharpen vout method.
101 *****************************************************************************/
102 static int Create( vlc_object_t
*p_this
)
104 filter_t
*p_filter
= (filter_t
*)p_this
;
106 const vlc_fourcc_t fourcc
= p_filter
->fmt_in
.video
.i_chroma
;
107 const vlc_chroma_description_t
*p_chroma
= vlc_fourcc_GetChromaDescription( fourcc
);
108 if( !p_chroma
|| p_chroma
->plane_count
!= 3 ||
109 (p_chroma
->pixel_size
!= 1 &&
110 p_filter
->fmt_in
.video
.i_chroma
!= VLC_CODEC_I420_10L
&&
111 p_filter
->fmt_in
.video
.i_chroma
!= VLC_CODEC_I420_10B
)) {
112 msg_Dbg( p_filter
, "Unsupported chroma (%4.4s)", (char*)&fourcc
);
116 /* Allocate structure */
117 p_filter
->p_sys
= malloc( sizeof( filter_sys_t
) );
118 if( p_filter
->p_sys
== NULL
)
121 p_filter
->pf_video_filter
= Filter
;
123 config_ChainParse( p_filter
, FILTER_PREFIX
, ppsz_filter_options
,
126 atomic_init(&p_filter
->p_sys
->sigma
,
127 var_CreateGetFloatCommand(p_filter
, FILTER_PREFIX
"sigma")
130 var_AddCallback( p_filter
, FILTER_PREFIX
"sigma",
131 SharpenCallback
, p_filter
->p_sys
);
137 /*****************************************************************************
138 * Destroy: destroy Sharpen video thread output method
139 *****************************************************************************
140 * Terminate an output method created by SharpenCreateOutputMethod
141 *****************************************************************************/
142 static void Destroy( vlc_object_t
*p_this
)
144 filter_t
*p_filter
= (filter_t
*)p_this
;
145 filter_sys_t
*p_sys
= p_filter
->p_sys
;
147 var_DelCallback( p_filter
, FILTER_PREFIX
"sigma", SharpenCallback
, p_sys
);
151 /*****************************************************************************
152 * Render: displays previously rendered output
153 *****************************************************************************
154 * This function send the currently rendered image to Invert image, waits
155 * until it is displayed and switch the two rendering buffers, preparing next
157 *****************************************************************************/
159 #define IS_YUV_420_10BITS(fmt) (fmt == VLC_CODEC_I420_10L || \
160 fmt == VLC_CODEC_I420_10B)
162 #define SHARPEN_FRAME(maxval, data_t) \
165 assert((maxval) >= 0); \
166 data_t *restrict p_src = (data_t *)p_pic->p[Y_PLANE].p_pixels; \
167 data_t *restrict p_out = (data_t *)p_outpic->p[Y_PLANE].p_pixels; \
168 const unsigned data_sz = sizeof(data_t); \
169 const int i_src_line_len = p_outpic->p[Y_PLANE].i_pitch / data_sz; \
170 const int i_out_line_len = p_pic->p[Y_PLANE].i_pitch / data_sz; \
171 const int sigma = atomic_load(&p_filter->p_sys->sigma); \
173 memcpy(p_out, p_src, i_visible_pitch); \
175 for( unsigned i = 1; i < i_visible_lines - 1; i++ ) \
177 p_out[i * i_out_line_len] = p_src[i * i_src_line_len]; \
179 for( unsigned j = data_sz; j < i_visible_pitch - 1; j++ ) \
181 const int line_idx_1 = (i - 1) * i_src_line_len; \
182 const int line_idx_2 = i * i_src_line_len; \
183 const int line_idx_3 = (i + 1) * i_src_line_len; \
185 (p_src[line_idx_1 + j - 1] * v1) + \
186 (p_src[line_idx_1 + j ] * v1) + \
187 (p_src[line_idx_1 + j + 1] * v1) + \
188 (p_src[line_idx_2 + j - 1] * v1) + \
189 (p_src[line_idx_2 + j ] << v2) + \
190 (p_src[line_idx_2 + j + 1] * v1) + \
191 (p_src[line_idx_3 + j - 1] * v1) + \
192 (p_src[line_idx_3 + j ] * v1) + \
193 (p_src[line_idx_3 + j + 1] * v1); \
195 pix = (VLC_CLIP(pix, -(maxval), maxval) * sigma) >> 20; \
196 p_out[i * i_out_line_len + j] = \
197 VLC_CLIP( p_src[line_idx_2 + j] + pix, 0, maxval); \
199 p_out[i * i_out_line_len + i_visible_pitch / 2 - 1] = \
200 p_src[i * i_src_line_len + i_visible_pitch / 2 - 1]; \
202 memcpy(&p_out[(i_visible_lines - 1) * i_out_line_len], \
203 &p_src[(i_visible_lines - 1) * i_src_line_len], \
207 static picture_t
*Filter( filter_t
*p_filter
, picture_t
*p_pic
)
211 const int v2
= 3; /* 2^3 = 8 */
212 const unsigned i_visible_lines
= p_pic
->p
[Y_PLANE
].i_visible_lines
;
213 const unsigned i_visible_pitch
= p_pic
->p
[Y_PLANE
].i_visible_pitch
;
215 p_outpic
= filter_NewPicture( p_filter
);
218 picture_Release( p_pic
);
222 if (!IS_YUV_420_10BITS(p_pic
->format
.i_chroma
))
223 SHARPEN_FRAME(255, uint8_t);
225 SHARPEN_FRAME(1023, uint16_t);
227 plane_CopyPixels( &p_outpic
->p
[U_PLANE
], &p_pic
->p
[U_PLANE
] );
228 plane_CopyPixels( &p_outpic
->p
[V_PLANE
], &p_pic
->p
[V_PLANE
] );
230 return CopyInfoAndRelease( p_outpic
, p_pic
);
233 static int SharpenCallback( vlc_object_t
*p_this
, char const *psz_var
,
234 vlc_value_t oldval
, vlc_value_t newval
,
237 VLC_UNUSED(p_this
); VLC_UNUSED(oldval
); VLC_UNUSED(psz_var
);
238 filter_sys_t
*p_sys
= (filter_sys_t
*)p_data
;
240 atomic_store(&p_sys
->sigma
,
241 VLC_CLIP(newval
.f_float
, 0.f
, 2.f
) * (1 << 20));