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 *****************************************************************************/
40 #include <vlc_common.h>
41 #include <vlc_atomic.h>
42 #include <vlc_plugin.h>
43 #include <vlc_filter.h>
44 #include <vlc_picture.h>
45 #include "filter_picture.h"
47 #define SIG_TEXT N_("Sharpen strength (0-2)")
48 #define SIG_LONGTEXT N_("Set the Sharpen strength, between 0 and 2. Defaults to 0.05.")
50 /*****************************************************************************
52 *****************************************************************************/
53 static int Create ( vlc_object_t
* );
54 static void Destroy ( vlc_object_t
* );
56 static picture_t
*Filter( filter_t
*, picture_t
* );
57 static int SharpenCallback( vlc_object_t
*, char const *,
58 vlc_value_t
, vlc_value_t
, void * );
60 #define SHARPEN_HELP N_("Augment contrast between contours.")
61 #define FILTER_PREFIX "sharpen-"
63 /*****************************************************************************
65 *****************************************************************************/
67 set_description( N_("Sharpen video filter") )
68 set_shortname( N_("Sharpen") )
69 set_help(SHARPEN_HELP
)
70 set_category( CAT_VIDEO
)
71 set_subcategory( SUBCAT_VIDEO_VFILTER
)
72 set_capability( "video filter", 0 )
73 add_float_with_range( FILTER_PREFIX
"sigma", 0.05, 0.0, 2.0,
74 SIG_TEXT
, SIG_LONGTEXT
, false )
76 add_shortcut( "sharpen" )
77 set_callbacks( Create
, Destroy
)
80 static const char *const ppsz_filter_options
[] = {
84 /*****************************************************************************
85 * filter_sys_t: Sharpen video filter descriptor
86 *****************************************************************************
87 * This structure is part of the video output thread descriptor.
88 * It describes the Sharpen specific properties of an output thread.
89 *****************************************************************************/
96 /*****************************************************************************
97 * Create: allocates Sharpen video thread output method
98 *****************************************************************************
99 * This function allocates and initializes a Sharpen vout method.
100 *****************************************************************************/
101 static int Create( vlc_object_t
*p_this
)
103 filter_t
*p_filter
= (filter_t
*)p_this
;
105 const vlc_fourcc_t fourcc
= p_filter
->fmt_in
.video
.i_chroma
;
106 const vlc_chroma_description_t
*p_chroma
= vlc_fourcc_GetChromaDescription( fourcc
);
107 if( !p_chroma
|| p_chroma
->plane_count
!= 3 ||
108 (p_chroma
->pixel_size
!= 1 &&
109 p_filter
->fmt_in
.video
.i_chroma
!= VLC_CODEC_I420_10L
&&
110 p_filter
->fmt_in
.video
.i_chroma
!= VLC_CODEC_I420_10B
)) {
111 msg_Err( p_filter
, "Unsupported chroma (%4.4s)", (char*)&fourcc
);
115 /* Allocate structure */
116 p_filter
->p_sys
= malloc( sizeof( filter_sys_t
) );
117 if( p_filter
->p_sys
== NULL
)
120 p_filter
->pf_video_filter
= Filter
;
122 config_ChainParse( p_filter
, FILTER_PREFIX
, ppsz_filter_options
,
125 atomic_init(&p_filter
->p_sys
->sigma
,
126 var_CreateGetFloatCommand(p_filter
, FILTER_PREFIX
"sigma")
129 var_AddCallback( p_filter
, FILTER_PREFIX
"sigma",
130 SharpenCallback
, p_filter
->p_sys
);
136 /*****************************************************************************
137 * Destroy: destroy Sharpen video thread output method
138 *****************************************************************************
139 * Terminate an output method created by SharpenCreateOutputMethod
140 *****************************************************************************/
141 static void Destroy( vlc_object_t
*p_this
)
143 filter_t
*p_filter
= (filter_t
*)p_this
;
144 filter_sys_t
*p_sys
= p_filter
->p_sys
;
146 var_DelCallback( p_filter
, FILTER_PREFIX
"sigma", SharpenCallback
, p_sys
);
150 /*****************************************************************************
151 * Render: displays previously rendered output
152 *****************************************************************************
153 * This function send the currently rendered image to Invert image, waits
154 * until it is displayed and switch the two rendering buffers, preparing next
156 *****************************************************************************/
158 #define IS_YUV_420_10BITS(fmt) (fmt == VLC_CODEC_I420_10L || fmt == VLC_CODEC_I420_10B)
160 #define SHARPEN_FRAME(maxval) \
163 data_t *restrict p_src = (data_t *)p_pic->p[Y_PLANE].p_pixels; \
164 data_t *restrict p_out = (data_t *)p_outpic->p[Y_PLANE].p_pixels; \
165 const unsigned data_sz = sizeof(data_t); \
166 const int i_src_line_len = p_outpic->p[Y_PLANE].i_pitch / data_sz; \
167 const int i_out_line_len = p_pic->p[Y_PLANE].i_pitch / data_sz; \
169 memcpy(p_out, p_src, i_visible_pitch); \
171 for( unsigned i = 1; i < i_visible_lines - 1; i++ ) \
173 p_out[i * i_out_line_len] = p_src[i * i_src_line_len]; \
175 for( unsigned j = data_sz; j < i_visible_pitch - 1; j++ ) \
177 pix = (p_src[(i - 1) * i_src_line_len + j - 1] * v[0]) + \
178 (p_src[(i - 1) * i_src_line_len + j ] * v[0]) + \
179 (p_src[(i - 1) * i_src_line_len + j + 1] * v[0]) + \
180 (p_src[(i ) * i_src_line_len + j - 1] * v[0]) + \
181 (p_src[(i ) * i_src_line_len + j ] << v[1]) + \
182 (p_src[(i ) * i_src_line_len + j + 1] * v[0]) + \
183 (p_src[(i + 1) * i_src_line_len + j - 1] * v[0]) + \
184 (p_src[(i + 1) * i_src_line_len + j ] * v[0]) + \
185 (p_src[(i + 1) * i_src_line_len + j + 1] * v[0]); \
187 pix = pix >= 0 ? VLC_CLIP(pix, 0, maxval) : -VLC_CLIP(pix * -1, 0, maxval); \
188 p_out[i * i_out_line_len + j] = VLC_CLIP( p_src[i * i_src_line_len + j] + ((pix * atomic_load(&p_filter->p_sys->sigma)) >> 20), 0, maxval); \
190 p_out[i * i_out_line_len + i_visible_pitch / 2 - 1] = \
191 p_src[i * i_src_line_len + i_visible_pitch / 2 - 1]; \
193 memcpy(&p_out[(i_visible_lines - 1) * i_out_line_len], \
194 &p_src[(i_visible_lines - 1) * i_src_line_len], i_visible_pitch); \
197 static picture_t
*Filter( filter_t
*p_filter
, picture_t
*p_pic
)
201 const int v
[2] = { -1, 3 /* 2^3 = 8 */ };
202 const unsigned i_visible_lines
= p_pic
->p
[Y_PLANE
].i_visible_lines
;
203 const unsigned i_visible_pitch
= p_pic
->p
[Y_PLANE
].i_visible_pitch
;
205 p_outpic
= filter_NewPicture( p_filter
);
208 picture_Release( p_pic
);
212 if (!IS_YUV_420_10BITS(p_pic
->format
.i_chroma
))
214 typedef uint8_t data_t
;
220 typedef uint16_t data_t
;
225 plane_CopyPixels( &p_outpic
->p
[U_PLANE
], &p_pic
->p
[U_PLANE
] );
226 plane_CopyPixels( &p_outpic
->p
[V_PLANE
], &p_pic
->p
[V_PLANE
] );
228 return CopyInfoAndRelease( p_outpic
, p_pic
);
231 static int SharpenCallback( vlc_object_t
*p_this
, char const *psz_var
,
232 vlc_value_t oldval
, vlc_value_t newval
,
235 VLC_UNUSED(p_this
); VLC_UNUSED(oldval
); VLC_UNUSED(psz_var
);
236 filter_sys_t
*p_sys
= (filter_sys_t
*)p_data
;
238 atomic_store(&p_sys
->sigma
,
239 VLC_CLIP(newval
.f_float
, 0.f
, 2.f
) * (1 << 20));