sharpen.c 10.4 KB
Newer Older
1 2 3
/*****************************************************************************
 * sharpen.c: Sharpen video filter
 *****************************************************************************
Jean-Baptiste Kempf's avatar
Jean-Baptiste Kempf committed
4
 * Copyright (C) 2003-2007 VLC authors and VideoLAN
Pierre's avatar
Pierre committed
5
 * $Id$
6
 *
7 8
 * Author: Jérémy DEMEULE <dj_mulder at djduron dot no-ip dot org>
 *         Jean-Baptiste Kempf <jb at videolan dot org>
9
 *
Jean-Baptiste Kempf's avatar
Jean-Baptiste Kempf committed
10 11 12
 * This program is free software; you can redistribute it and/or modify it
 * under the terms of the GNU Lesser General Public License as published by
 * the Free Software Foundation; either version 2.1 of the License, or
13 14 15 16
 * (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
Jean-Baptiste Kempf's avatar
Jean-Baptiste Kempf committed
17 18
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
 * GNU Lesser General Public License for more details.
19
 *
Jean-Baptiste Kempf's avatar
Jean-Baptiste Kempf committed
20 21 22
 * You should have received a copy of the GNU Lesser General Public License
 * along with this program; if not, write to the Free Software Foundation,
 * Inc., 51 Franklin Street, Fifth Floor, Boston MA 02110-1301, USA.
23 24 25 26 27 28 29 30 31 32 33 34 35
 *****************************************************************************/

/* The sharpen filter. */
/*
 * static int filter[] = { -1, -1, -1,
 *                         -1,  8, -1,
 *                         -1, -1, -1 };
 */

/*****************************************************************************
 * Preamble
 *****************************************************************************/

36 37 38 39
#ifdef HAVE_CONFIG_H
# include "config.h"
#endif

40
#include <assert.h>
41
#include <vlc_common.h>
42
#include <vlc_atomic.h>
43
#include <vlc_plugin.h>
Rémi Duraffort's avatar
Rémi Duraffort committed
44
#include <vlc_filter.h>
45
#include <vlc_picture.h>
46
#include "filter_picture.h"
47 48 49 50 51 52 53 54 55 56 57

#define SIG_TEXT N_("Sharpen strength (0-2)")
#define SIG_LONGTEXT N_("Set the Sharpen strength, between 0 and 2. Defaults to 0.05.")

/*****************************************************************************
 * Local prototypes
 *****************************************************************************/
static int  Create    ( vlc_object_t * );
static void Destroy   ( vlc_object_t * );

static picture_t *Filter( filter_t *, picture_t * );
58 59
static int SharpenCallback( vlc_object_t *, char const *,
                            vlc_value_t, vlc_value_t, void * );
60

61
#define SHARPEN_HELP N_("Augment contrast between contours.")
62 63
#define FILTER_PREFIX "sharpen-"

64 65 66
/*****************************************************************************
 * Module descriptor
 *****************************************************************************/
67
vlc_module_begin ()
68 69 70
    set_description( N_("Sharpen video filter") )
    set_shortname( N_("Sharpen") )
    set_help(SHARPEN_HELP)
71 72
    set_category( CAT_VIDEO )
    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,
Rémi Denis-Courmont's avatar
Rémi Denis-Courmont committed
75
        SIG_TEXT, SIG_LONGTEXT, false )
76
    change_safe()
77 78 79
    add_shortcut( "sharpen" )
    set_callbacks( Create, Destroy )
vlc_module_end ()
80

81
static const char *const ppsz_filter_options[] = {
82 83 84
    "sigma", NULL
};

85 86 87 88 89 90 91 92 93
/*****************************************************************************
 * filter_sys_t: Sharpen video filter descriptor
 *****************************************************************************
 * This structure is part of the video output thread descriptor.
 * It describes the Sharpen specific properties of an output thread.
 *****************************************************************************/

struct filter_sys_t
{
94
    atomic_int sigma;
95 96 97 98 99 100 101 102 103 104 105
};

/*****************************************************************************
 * Create: allocates Sharpen video thread output method
 *****************************************************************************
 * This function allocates and initializes a Sharpen vout method.
 *****************************************************************************/
static int Create( vlc_object_t *p_this )
{
    filter_t *p_filter = (filter_t *)p_this;

106 107
    const vlc_fourcc_t fourcc = p_filter->fmt_in.video.i_chroma;
    const vlc_chroma_description_t *p_chroma = vlc_fourcc_GetChromaDescription( fourcc );
108 109 110 111
    if( !p_chroma || p_chroma->plane_count != 3 ||
        (p_chroma->pixel_size != 1 &&
         p_filter->fmt_in.video.i_chroma != VLC_CODEC_I420_10L &&
         p_filter->fmt_in.video.i_chroma != VLC_CODEC_I420_10B)) {
112
        msg_Dbg( p_filter, "Unsupported chroma (%4.4s)", (char*)&fourcc );
113 114 115
        return VLC_EGENERIC;
    }

116 117 118 119 120 121 122
    /* Allocate structure */
    p_filter->p_sys = malloc( sizeof( filter_sys_t ) );
    if( p_filter->p_sys == NULL )
        return VLC_ENOMEM;

    p_filter->pf_video_filter = Filter;

123 124 125
    config_ChainParse( p_filter, FILTER_PREFIX, ppsz_filter_options,
                   p_filter->p_cfg );

126 127 128
    atomic_init(&p_filter->p_sys->sigma,
                var_CreateGetFloatCommand(p_filter, FILTER_PREFIX "sigma")
                * (1 << 20));
129

130 131
    var_AddCallback( p_filter, FILTER_PREFIX "sigma",
                     SharpenCallback, p_filter->p_sys );
132 133 134 135 136 137 138 139 140 141 142 143 144

    return VLC_SUCCESS;
}


/*****************************************************************************
 * Destroy: destroy Sharpen video thread output method
 *****************************************************************************
 * Terminate an output method created by SharpenCreateOutputMethod
 *****************************************************************************/
static void Destroy( vlc_object_t *p_this )
{
    filter_t *p_filter = (filter_t *)p_this;
145 146 147 148
    filter_sys_t *p_sys = p_filter->p_sys;

    var_DelCallback( p_filter, FILTER_PREFIX "sigma", SharpenCallback, p_sys );
    free( p_sys );
149 150 151 152 153 154 155 156 157
}

/*****************************************************************************
 * Render: displays previously rendered output
 *****************************************************************************
 * This function send the currently rendered image to Invert image, waits
 * until it is displayed and switch the two rendering buffers, preparing next
 * frame.
 *****************************************************************************/
158

159 160
#define IS_YUV_420_10BITS(fmt) (fmt == VLC_CODEC_I420_10L ||    \
                                fmt == VLC_CODEC_I420_10B)
161

162
#define SHARPEN_FRAME(maxval, data_t)                                   \
163 164
    do                                                                  \
    {                                                                   \
165
        assert((maxval) >= 0);                                          \
166 167 168 169 170
        data_t *restrict p_src = (data_t *)p_pic->p[Y_PLANE].p_pixels;  \
        data_t *restrict p_out = (data_t *)p_outpic->p[Y_PLANE].p_pixels; \
        const unsigned data_sz = sizeof(data_t);                        \
        const int i_src_line_len = p_outpic->p[Y_PLANE].i_pitch / data_sz; \
        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);         \
172 173 174 175 176 177 178 179 180
                                                                        \
        memcpy(p_out, p_src, i_visible_pitch);                          \
                                                                        \
        for( unsigned i = 1; i < i_visible_lines - 1; i++ )             \
        {                                                               \
            p_out[i * i_out_line_len] = p_src[i * i_src_line_len];      \
                                                                        \
            for( unsigned j = data_sz; j < i_visible_pitch - 1; j++ )   \
            {                                                           \
181 182 183 184 185 186 187 188 189 190 191 192 193
                const int line_idx_1 = (i - 1) * i_src_line_len;        \
                const int line_idx_2 = i * i_src_line_len;              \
                const int line_idx_3 = (i + 1) * i_src_line_len;        \
                int pix =                                               \
                    (p_src[line_idx_1 + j - 1] * v1) +                  \
                    (p_src[line_idx_1 + j    ] * v1) +                  \
                    (p_src[line_idx_1 + j + 1] * v1) +                  \
                    (p_src[line_idx_2 + j - 1] * v1) +                  \
                    (p_src[line_idx_2 + j    ] << v2) +                 \
                    (p_src[line_idx_2 + j + 1] * v1) +                  \
                    (p_src[line_idx_3 + j - 1] * v1) +                  \
                    (p_src[line_idx_3 + j    ] * v1) +                  \
                    (p_src[line_idx_3 + j + 1] * v1);                   \
194
                                                                        \
195 196 197
                pix = (VLC_CLIP(pix, -(maxval), maxval) * sigma) >> 20; \
                p_out[i * i_out_line_len + j] =                         \
                    VLC_CLIP( p_src[line_idx_2 + j] + pix, 0, maxval);  \
198
            }                                                           \
199 200
            p_out[i * i_out_line_len + i_visible_pitch / data_sz - 1] = \
                p_src[i * i_src_line_len + i_visible_pitch / data_sz - 1];  \
201 202
        }                                                               \
        memcpy(&p_out[(i_visible_lines - 1) * i_out_line_len],          \
203 204 205
               &p_src[(i_visible_lines - 1) * i_src_line_len],          \
               i_visible_pitch);                                        \
    } while (0)
206

207 208 209
static picture_t *Filter( filter_t *p_filter, picture_t *p_pic )
{
    picture_t *p_outpic;
210 211
    const int v1 = -1;
    const int v2 = 3; /* 2^3 = 8 */
212 213
    const unsigned i_visible_lines = p_pic->p[Y_PLANE].i_visible_lines;
    const unsigned i_visible_pitch = p_pic->p[Y_PLANE].i_visible_pitch;
214

Laurent Aimar's avatar
Laurent Aimar committed
215
    p_outpic = filter_NewPicture( p_filter );
216 217
    if( !p_outpic )
    {
218
        picture_Release( p_pic );
219 220 221
        return NULL;
    }

222
    if (!IS_YUV_420_10BITS(p_pic->format.i_chroma))
223
        SHARPEN_FRAME(255, uint8_t);
224
    else
225
        SHARPEN_FRAME(1023, uint16_t);
226

227 228
    plane_CopyPixels( &p_outpic->p[U_PLANE], &p_pic->p[U_PLANE] );
    plane_CopyPixels( &p_outpic->p[V_PLANE], &p_pic->p[V_PLANE] );
229

230
    return CopyInfoAndRelease( p_outpic, p_pic );
231
}
232 233 234 235 236

static int SharpenCallback( vlc_object_t *p_this, char const *psz_var,
                            vlc_value_t oldval, vlc_value_t newval,
                            void *p_data )
{
237
    VLC_UNUSED(p_this); VLC_UNUSED(oldval); VLC_UNUSED(psz_var);
238
    filter_sys_t *p_sys = (filter_sys_t *)p_data;
239

240 241 242
    atomic_store(&p_sys->sigma,
                 VLC_CLIP(newval.f_float, 0.f, 2.f) * (1 << 20));

243 244
    return VLC_SUCCESS;
}