hqdn3d.c 9.85 KB
Newer Older
1 2 3 4 5 6 7 8
/*****************************************************************************
 * hqdn3d.c : high-quality denoise 3D ported from MPlayer
 *****************************************************************************
 * Copyright (C) 2011 VLC authors and VideoLAN
 * $Id$
 *
 * Authors: Cheng Sun <chengsun9@gmail.com>
 *
Jean-Baptiste Kempf's avatar
Jean-Baptiste Kempf committed
9 10 11
 * 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
12 13 14 15
 * (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
16 17
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
 * GNU Lesser General Public License for more details.
18
 *
Jean-Baptiste Kempf's avatar
Jean-Baptiste Kempf committed
19 20 21
 * 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.
22 23 24 25 26 27 28 29 30 31 32 33 34
 *****************************************************************************/

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

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

#include <vlc_common.h>
#include <vlc_plugin.h>
#include <vlc_filter.h>
35
#include <vlc_picture.h>
36 37 38 39 40 41 42 43 44 45 46
#include "filter_picture.h"


#include "hqdn3d.h"

/*****************************************************************************
 * Local protypes
 *****************************************************************************/
static int  Open         (vlc_object_t *);
static void Close        (vlc_object_t *);
static picture_t *Filter (filter_t *, picture_t *);
47 48 49
static int DenoiseCallback( vlc_object_t *p_this, char const *psz_var,
                            vlc_value_t oldval, vlc_value_t newval,
                            void *p_data );
50 51 52 53 54 55 56 57 58 59 60 61 62 63 64

/*****************************************************************************
 * Module descriptor
 *****************************************************************************/

#define FILTER_PREFIX       "hqdn3d-"

#define LUMA_SPAT_TEXT          N_("Spatial luma strength (0-254)")
#define CHROMA_SPAT_TEXT        N_("Spatial chroma strength (0-254)")
#define LUMA_TEMP_TEXT          N_("Temporal luma strength (0-254)")
#define CHROMA_TEMP_TEXT        N_("Temporal chroma strength (0-254)")

vlc_module_begin()
    set_shortname(N_("HQ Denoiser 3D"))
    set_description(N_("High Quality 3D Denoiser filter"))
65
    set_capability("video filter", 0)
66 67 68 69
    set_category(CAT_VIDEO)
    set_subcategory(SUBCAT_VIDEO_VFILTER)

    add_float_with_range(FILTER_PREFIX "luma-spat", 4.0, 0.0, 254.0,
70
            LUMA_SPAT_TEXT, LUMA_SPAT_TEXT, false)
71
    add_float_with_range(FILTER_PREFIX "chroma-spat", 3.0, 0.0, 254.0,
72
            CHROMA_SPAT_TEXT, CHROMA_SPAT_TEXT, false)
73
    add_float_with_range(FILTER_PREFIX "luma-temp", 6.0, 0.0, 254.0,
74
            LUMA_TEMP_TEXT, LUMA_TEMP_TEXT, false)
75
    add_float_with_range(FILTER_PREFIX "chroma-temp", 4.5, 0.0, 254.0,
76
            CHROMA_TEMP_TEXT, CHROMA_TEMP_TEXT, false)
77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95

    add_shortcut("hqdn3d")

    set_callbacks(Open, Close)
vlc_module_end()

static const char *const filter_options[] = {
    "luma-spat", "chroma-spat", "luma-temp", "chroma-temp", NULL
};

/*****************************************************************************
 * filter_sys_t
 *****************************************************************************/
struct filter_sys_t
{
    const vlc_chroma_description_t *chroma;
    int w[3], h[3];

    struct vf_priv_s cfg;
96 97 98
    bool   b_recalc_coefs;
    vlc_mutex_t coefs_mutex;
    float  luma_spat, luma_temp, chroma_spat, chroma_temp;
99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140
};

/*****************************************************************************
 * Open
 *****************************************************************************/
static int Open(vlc_object_t *this)
{
    filter_t *filter = (filter_t *)this;
    filter_sys_t *sys;
    struct vf_priv_s *cfg;
    const video_format_t *fmt_in  = &filter->fmt_in.video;
    const video_format_t *fmt_out = &filter->fmt_out.video;
    const vlc_fourcc_t fourcc_in  = fmt_in->i_chroma;
    const vlc_fourcc_t fourcc_out = fmt_out->i_chroma;
    int wmax = 0;

    const vlc_chroma_description_t *chroma =
            vlc_fourcc_GetChromaDescription(fourcc_in);
    if (!chroma || chroma->plane_count != 3 || chroma->pixel_size != 1) {
        msg_Err(filter, "Unsupported chroma (%4.4s)", (char*)&fourcc_in);
        return VLC_EGENERIC;
    }

    if (fourcc_in != fourcc_out) {
        msg_Err(filter, "Input and output chromas don't match");
        return VLC_EGENERIC;
    }

    /* Allocate structure */
    sys = calloc(1, sizeof(filter_sys_t));
    if (!sys) {
        return VLC_ENOMEM;
    }
    cfg = &sys->cfg;

    sys->chroma = chroma;

    for (int i = 0; i < 3; ++i) {
        sys->w[i] = fmt_in->i_width  * chroma->p[i].w.num / chroma->p[i].w.den;
        if (sys->w[i] > wmax) wmax = sys->w[i];
        sys->h[i] = fmt_out->i_height * chroma->p[i].h.num / chroma->p[i].h.den;
    }
141
    cfg->Line = malloc(wmax*sizeof(unsigned int));
142 143 144 145 146 147 148 149
    if (!cfg->Line) {
        free(sys);
        return VLC_ENOMEM;
    }

    config_ChainParse(filter, FILTER_PREFIX, filter_options,
                      filter->p_cfg);

150 151 152 153 154 155 156 157 158 159 160 161 162 163 164

    vlc_mutex_init( &sys->coefs_mutex );
    sys->b_recalc_coefs = true;
    sys->luma_spat = var_CreateGetFloatCommand(filter, FILTER_PREFIX "luma-spat");
    sys->chroma_spat = var_CreateGetFloatCommand(filter, FILTER_PREFIX "chroma-spat");
    sys->luma_temp = var_CreateGetFloatCommand(filter, FILTER_PREFIX "luma-temp");
    sys->chroma_temp = var_CreateGetFloatCommand(filter, FILTER_PREFIX "chroma-temp");

    filter->p_sys = sys;
    filter->pf_video_filter = Filter;

    var_AddCallback( filter, FILTER_PREFIX "luma-spat", DenoiseCallback, sys );
    var_AddCallback( filter, FILTER_PREFIX "chroma-spat", DenoiseCallback, sys );
    var_AddCallback( filter, FILTER_PREFIX "luma-temp", DenoiseCallback, sys );
    var_AddCallback( filter, FILTER_PREFIX "chroma-temp", DenoiseCallback, sys );
165

166 167 168 169 170 171 172 173 174 175 176 177
    return VLC_SUCCESS;
}

/*****************************************************************************
 * Close
 *****************************************************************************/
static void Close(vlc_object_t *this)
{
    filter_t *filter = (filter_t *)this;
    filter_sys_t *sys = filter->p_sys;
    struct vf_priv_s *cfg = &sys->cfg;

178 179 180 181 182 183 184
    var_DelCallback( filter, FILTER_PREFIX "luma-spat", DenoiseCallback, sys );
    var_DelCallback( filter, FILTER_PREFIX "chroma-spat", DenoiseCallback, sys );
    var_DelCallback( filter, FILTER_PREFIX "luma-temp", DenoiseCallback, sys );
    var_DelCallback( filter, FILTER_PREFIX "chroma-temp", DenoiseCallback, sys );

    vlc_mutex_destroy( &sys->coefs_mutex );

185 186 187 188 189 190 191 192 193 194 195 196 197 198 199
    for (int i = 0; i < 3; ++i) {
        free(cfg->Frame[i]);
    }
    free(cfg->Line);
    free(sys);
}

/*****************************************************************************
 * Filter
 *****************************************************************************/
static picture_t *Filter(filter_t *filter, picture_t *src)
{
    picture_t *dst;
    filter_sys_t *sys = filter->p_sys;
    struct vf_priv_s *cfg = &sys->cfg;
200
    bool recalc = false;
201 202 203 204

    if (!src) return NULL;

    dst = filter_NewPicture(filter);
Ilkka Ollakka's avatar
Ilkka Ollakka committed
205
    if ( unlikely(!dst) ) {
206 207 208
        picture_Release(src);
        return NULL;
    }
209 210 211 212 213 214 215 216 217 218 219 220 221 222
    vlc_mutex_lock( &sys->coefs_mutex );
    recalc = sys->b_recalc_coefs;
    sys->b_recalc_coefs = false;

    if( unlikely( recalc ) )
    {
        msg_Dbg( filter, "Changing coefs to %.2f %.2f %.2f %.2f",
                            sys->luma_spat, sys->luma_temp, sys->chroma_spat, sys->chroma_temp );
        PrecalcCoefs(cfg->Coefs[0], sys->luma_spat);
        PrecalcCoefs(cfg->Coefs[1], sys->luma_temp);
        PrecalcCoefs(cfg->Coefs[2], sys->chroma_spat);
        PrecalcCoefs(cfg->Coefs[3], sys->chroma_temp);
    }
    vlc_mutex_unlock( &sys->coefs_mutex );
223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242

    deNoise(src->p[0].p_pixels, dst->p[0].p_pixels,
            cfg->Line, &cfg->Frame[0], sys->w[0], sys->h[0],
            src->p[0].i_pitch, dst->p[0].i_pitch,
            cfg->Coefs[0],
            cfg->Coefs[0],
            cfg->Coefs[1]);
    deNoise(src->p[1].p_pixels, dst->p[1].p_pixels,
            cfg->Line, &cfg->Frame[1], sys->w[1], sys->h[1],
            src->p[1].i_pitch, dst->p[1].i_pitch,
            cfg->Coefs[2],
            cfg->Coefs[2],
            cfg->Coefs[3]);
    deNoise(src->p[2].p_pixels, dst->p[2].p_pixels,
            cfg->Line, &cfg->Frame[2], sys->w[2], sys->h[2],
            src->p[2].i_pitch, dst->p[2].i_pitch,
            cfg->Coefs[2],
            cfg->Coefs[2],
            cfg->Coefs[3]);

243 244 245 246 247 248 249
    if(unlikely(!cfg->Frame[0] || !cfg->Frame[1] || !cfg->Frame[2]))
    {
        picture_Release( src );
        picture_Release( dst );
        return NULL;
    }

250 251
    return CopyInfoAndRelease(dst, src);
}
252 253 254 255 256 257 258 259


static int DenoiseCallback( vlc_object_t *p_this, char const *psz_var,
                            vlc_value_t oldval, vlc_value_t newval,
                            void *p_data )
{
    VLC_UNUSED(p_this); VLC_UNUSED(oldval);

260
    filter_sys_t *sys = (filter_sys_t*)p_data;
261

262 263 264 265 266 267 268
    /* Just take values and flag for recalc so we don't block UI thread calling this
     * and don't right thread safety calcing coefs in here without mutex*/
    vlc_mutex_lock( &sys->coefs_mutex );
    if( !strcmp( psz_var, FILTER_PREFIX "luma-spat") )
        sys->luma_spat = newval.f_float;
    else if( !strcmp( psz_var, FILTER_PREFIX "luma-temp") )
        sys->luma_temp = newval.f_float;
269
    else if( !strcmp( psz_var, FILTER_PREFIX "chroma-temp") )
270 271 272 273 274
        sys->chroma_spat = newval.f_float;
    else if( !strcmp( psz_var, FILTER_PREFIX "chroma-spat") )
        sys->chroma_temp = newval.f_float;
    sys->b_recalc_coefs = true;
    vlc_mutex_unlock( &sys->coefs_mutex );
275 276 277

    return VLC_SUCCESS;
}