gradfun.c 7.47 KB
Newer Older
1
/*****************************************************************************
Jean-Baptiste Kempf's avatar
LGPL  
Jean-Baptiste Kempf committed
2
 * gradfun.c: wrapper for the gradfun filter from libav
3 4 5 6 7 8
 *****************************************************************************
 * Copyright (C) 2010 Laurent Aimar
 * $Id$
 *
 * Authors: Laurent Aimar <fenrir _AT_ videolan _DOT_ org>
 *
Jean-Baptiste Kempf's avatar
LGPL  
Jean-Baptiste Kempf committed
9 10 11 12 13
 * Based on the work of: Nolan Lum and Loren Merritt
 *
 * 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
14 15 16 17
 * (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
LGPL  
Jean-Baptiste Kempf committed
18 19
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
 * GNU Lesser General Public License for more details.
20
 *
Jean-Baptiste Kempf's avatar
LGPL  
Jean-Baptiste Kempf committed
21 22 23
 * 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.
24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51
 *****************************************************************************/

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

#ifdef HAVE_CONFIG_H
# include "config.h"
#endif

#include <vlc_common.h>
#include <vlc_plugin.h>
#include <vlc_cpu.h>
#include <vlc_filter.h>

/*****************************************************************************
 * Module descriptor
 *****************************************************************************/
static int  Open (vlc_object_t *);
static void Close(vlc_object_t *);

#define CFG_PREFIX "gradfun-"

#define RADIUS_MIN (4)
#define RADIUS_MAX (32)
#define RADIUS_TEXT N_("Radius")
#define RADIUS_LONGTEXT N_("Radius in pixels")

52
#define STRENGTH_MIN (0.51f)
53 54 55 56 57 58 59
#define STRENGTH_MAX (255)
#define STRENGTH_TEXT N_("Strength")
#define STRENGTH_LONGTEXT N_("Strength used to modify the value of a pixel")

vlc_module_begin()
    set_description(N_("Gradfun video filter"))
    set_shortname(N_("Gradfun"))
60
    set_help(N_("Debanding algorithm"))
61
    set_capability("video filter", 0)
62 63 64
    set_category(CAT_VIDEO)
    set_subcategory(SUBCAT_VIDEO_VFILTER)
    add_integer_with_range(CFG_PREFIX "radius", 16, RADIUS_MIN, RADIUS_MAX,
65
                           RADIUS_TEXT, RADIUS_LONGTEXT, false)
66
    add_float_with_range(CFG_PREFIX "strength", 1.2, STRENGTH_MIN, STRENGTH_MAX,
67
                         STRENGTH_TEXT, STRENGTH_LONGTEXT, false)
68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106

    set_callbacks(Open, Close)
vlc_module_end()

/*****************************************************************************
 * Local prototypes
 *****************************************************************************/
#define FFMAX(a,b) __MAX(a,b)
#ifdef CAN_COMPILE_MMXEXT
#   define HAVE_MMX2 1
#else
#   define HAVE_MMX2 0
#endif
#ifdef CAN_COMPILE_SSE2
#   define HAVE_SSE2 1
#else
#   define HAVE_SSE2 0
#endif
#ifdef CAN_COMPILE_SSSE3
#   define HAVE_SSSE3 1
#else
#   define HAVE_SSSE3 0
#endif
// FIXME too restrictive
#ifdef __x86_64__
#   define HAVE_6REGS 1
#else
#   define HAVE_6REGS 0
#endif
#define av_clip_uint8 clip_uint8_vlc
#include "gradfun.h"

static picture_t *Filter(filter_t *, picture_t *);
static int Callback(vlc_object_t *, char const *, vlc_value_t, vlc_value_t, void *);

struct filter_sys_t {
    vlc_mutex_t      lock;
    float            strength;
    int              radius;
107
    const vlc_chroma_description_t *chroma;
108 109 110 111 112 113
    struct vf_priv_s cfg;
};

static int Open(vlc_object_t *object)
{
    filter_t *filter = (filter_t *)object;
114
    const vlc_fourcc_t fourcc = filter->fmt_in.video.i_chroma;
115

116
    const vlc_chroma_description_t *chroma = vlc_fourcc_GetChromaDescription(fourcc);
117
    if (!chroma || chroma->plane_count < 3 || chroma->pixel_size != 1) {
118
        msg_Err(filter, "Unsupported chroma (%4.4s)", (char*)&fourcc);
119 120 121 122 123 124 125 126
        return VLC_EGENERIC;
    }

    filter_sys_t *sys = malloc(sizeof(*sys));
    if (!sys)
        return VLC_ENOMEM;

    vlc_mutex_init(&sys->lock);
127
    sys->chroma   = chroma;
128 129 130 131
    sys->strength = var_CreateGetFloatCommand(filter,   CFG_PREFIX "strength");
    sys->radius   = var_CreateGetIntegerCommand(filter, CFG_PREFIX "radius");
    var_AddCallback(filter, CFG_PREFIX "strength", Callback, NULL);
    var_AddCallback(filter, CFG_PREFIX "radius",   Callback, NULL);
132
    sys->cfg.buf = NULL;
133 134 135 136 137 138 139

    struct vf_priv_s *cfg = &sys->cfg;
    cfg->thresh      = 0.0;
    cfg->radius      = 0;
    cfg->buf         = NULL;

#if HAVE_SSE2 && HAVE_6REGS
140
    if (vlc_CPU_SSE2())
141
        cfg->blur_line = blur_line_sse2;
142
    else
143
#endif
144
        cfg->blur_line   = blur_line_c;
145
#if HAVE_SSSE3
146
    if (vlc_CPU_SSSE3())
147
        cfg->filter_line = filter_line_ssse3;
148 149 150 151 152 153
    else
#endif
#if HAVE_MMX2
    if (vlc_CPU_MMXEXT())
        cfg->filter_line = filter_line_mmx2;
    else
154
#endif
155
        cfg->filter_line = filter_line_c;
156 157 158 159 160 161 162 163 164 165 166

    filter->p_sys           = sys;
    filter->pf_video_filter = Filter;
    return VLC_SUCCESS;
}

static void Close(vlc_object_t *object)
{
    filter_t     *filter = (filter_t *)object;
    filter_sys_t *sys = filter->p_sys;

167 168
    var_DelCallback(filter, CFG_PREFIX "radius",   Callback, NULL);
    var_DelCallback(filter, CFG_PREFIX "strength", Callback, NULL);
169
    vlc_free(sys->cfg.buf);
170 171 172 173 174 175 176 177 178 179 180 181 182 183 184
    vlc_mutex_destroy(&sys->lock);
    free(sys);
}

static picture_t *Filter(filter_t *filter, picture_t *src)
{
    filter_sys_t *sys = filter->p_sys;

    picture_t *dst = filter_NewPicture(filter);
    if (!dst) {
        picture_Release(src);
        return NULL;
    }

    vlc_mutex_lock(&sys->lock);
185 186
    float strength = VLC_CLIP(sys->strength, STRENGTH_MIN, STRENGTH_MAX);
    int   radius   = VLC_CLIP((sys->radius + 1) & ~1, RADIUS_MIN, RADIUS_MAX);
187 188 189 190 191 192 193 194
    vlc_mutex_unlock(&sys->lock);

    const video_format_t *fmt = &filter->fmt_in.video;
    struct vf_priv_s *cfg = &sys->cfg;

    cfg->thresh = (1 << 15) / strength;
    if (cfg->radius != radius) {
        cfg->radius = radius;
195
        cfg->buf    = vlc_memalign(16,
196 197 198 199 200 201 202
                                   (((fmt->i_width + 15) & ~15) * (cfg->radius + 1) / 2 + 32) * sizeof(*cfg->buf));
    }

    for (int i = 0; i < dst->i_planes; i++) {
        const plane_t *srcp = &src->p[i];
        plane_t       *dstp = &dst->p[i];

203 204 205 206 207
        const vlc_chroma_description_t *chroma = sys->chroma;
        int w = fmt->i_width  * chroma->p[i].w.num / chroma->p[i].w.den;
        int h = fmt->i_height * chroma->p[i].h.num / chroma->p[i].h.den;
        int r = (cfg->radius  * chroma->p[i].w.num / chroma->p[i].w.den +
                 cfg->radius  * chroma->p[i].h.num / chroma->p[i].h.den) / 2;
208
        r = VLC_CLIP((r + 1) & ~1, RADIUS_MIN, RADIUS_MAX);
209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238
        if (__MIN(w, h) > 2 * r && cfg->buf) {
            filter_plane(cfg, dstp->p_pixels, srcp->p_pixels,
                         w, h, dstp->i_pitch, srcp->i_pitch, r);
        } else {
            plane_CopyPixels(dstp, srcp);
        }
    }

    picture_CopyProperties(dst, src);
    picture_Release(src);
    return dst;
}

static int Callback(vlc_object_t *object, char const *cmd,
                    vlc_value_t oldval, vlc_value_t newval, void *data)
{
    filter_t     *filter = (filter_t *)object;
    filter_sys_t *sys = filter->p_sys;
    VLC_UNUSED(oldval); VLC_UNUSED(data);

    vlc_mutex_lock(&sys->lock);
    if (!strcmp(cmd, CFG_PREFIX "strength"))
        sys->strength = newval.f_float;
    else
        sys->radius    = newval.i_int;
    vlc_mutex_unlock(&sys->lock);

    return VLC_SUCCESS;
}