]> git.sesse.net Git - vlc/blob - modules/video_filter/hqdn3d.c
decoder: fix data race in input_DecoderChangePause()
[vlc] / modules / video_filter / hqdn3d.c
1 /*****************************************************************************
2  * hqdn3d.c : high-quality denoise 3D ported from MPlayer
3  *****************************************************************************
4  * Copyright (C) 2011 VLC authors and VideoLAN
5  * $Id$
6  *
7  * Authors: Cheng Sun <chengsun9@gmail.com>
8  *
9  * This program is free software; you can redistribute it and/or modify it
10  * under the terms of the GNU Lesser General Public License as published by
11  * the Free Software Foundation; either version 2.1 of the License, or
12  * (at your option) any later version.
13  *
14  * This program is distributed in the hope that it will be useful,
15  * but WITHOUT ANY WARRANTY; without even the implied warranty of
16  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
17  * GNU Lesser General Public License for more details.
18  *
19  * You should have received a copy of the GNU Lesser General Public License
20  * along with this program; if not, write to the Free Software Foundation,
21  * Inc., 51 Franklin Street, Fifth Floor, Boston MA 02110-1301, USA.
22  *****************************************************************************/
23
24 /*****************************************************************************
25  * Preamble
26  *****************************************************************************/
27
28 #ifdef HAVE_CONFIG_H
29 # include "config.h"
30 #endif
31
32 #include <vlc_common.h>
33 #include <vlc_plugin.h>
34 #include <vlc_filter.h>
35 #include "filter_picture.h"
36
37
38 #include "hqdn3d.h"
39
40 /*****************************************************************************
41  * Local protypes
42  *****************************************************************************/
43 static int  Open         (vlc_object_t *);
44 static void Close        (vlc_object_t *);
45 static picture_t *Filter (filter_t *, picture_t *);
46 static int DenoiseCallback( vlc_object_t *p_this, char const *psz_var,
47                             vlc_value_t oldval, vlc_value_t newval,
48                             void *p_data );
49
50 /*****************************************************************************
51  * Module descriptor
52  *****************************************************************************/
53
54 #define FILTER_PREFIX       "hqdn3d-"
55
56 #define LUMA_SPAT_TEXT          N_("Spatial luma strength (0-254)")
57 #define CHROMA_SPAT_TEXT        N_("Spatial chroma strength (0-254)")
58 #define LUMA_TEMP_TEXT          N_("Temporal luma strength (0-254)")
59 #define CHROMA_TEMP_TEXT        N_("Temporal chroma strength (0-254)")
60
61 vlc_module_begin()
62     set_shortname(N_("HQ Denoiser 3D"))
63     set_description(N_("High Quality 3D Denoiser filter"))
64     set_capability("video filter2", 0)
65     set_category(CAT_VIDEO)
66     set_subcategory(SUBCAT_VIDEO_VFILTER)
67
68     add_float_with_range(FILTER_PREFIX "luma-spat", 4.0, 0.0, 254.0,
69             LUMA_SPAT_TEXT, LUMA_SPAT_TEXT, false)
70     add_float_with_range(FILTER_PREFIX "chroma-spat", 3.0, 0.0, 254.0,
71             CHROMA_SPAT_TEXT, CHROMA_SPAT_TEXT, false)
72     add_float_with_range(FILTER_PREFIX "luma-temp", 6.0, 0.0, 254.0,
73             LUMA_TEMP_TEXT, LUMA_TEMP_TEXT, false)
74     add_float_with_range(FILTER_PREFIX "chroma-temp", 4.5, 0.0, 254.0,
75             CHROMA_TEMP_TEXT, CHROMA_TEMP_TEXT, false)
76
77     add_shortcut("hqdn3d")
78
79     set_callbacks(Open, Close)
80 vlc_module_end()
81
82 static const char *const filter_options[] = {
83     "luma-spat", "chroma-spat", "luma-temp", "chroma-temp", NULL
84 };
85
86 /*****************************************************************************
87  * filter_sys_t
88  *****************************************************************************/
89 struct filter_sys_t
90 {
91     const vlc_chroma_description_t *chroma;
92     int w[3], h[3];
93
94     struct vf_priv_s cfg;
95     bool   b_recalc_coefs;
96     vlc_mutex_t coefs_mutex;
97     float  luma_spat, luma_temp, chroma_spat, chroma_temp;
98 };
99
100 /*****************************************************************************
101  * Open
102  *****************************************************************************/
103 static int Open(vlc_object_t *this)
104 {
105     filter_t *filter = (filter_t *)this;
106     filter_sys_t *sys;
107     struct vf_priv_s *cfg;
108     const video_format_t *fmt_in  = &filter->fmt_in.video;
109     const video_format_t *fmt_out = &filter->fmt_out.video;
110     const vlc_fourcc_t fourcc_in  = fmt_in->i_chroma;
111     const vlc_fourcc_t fourcc_out = fmt_out->i_chroma;
112     int wmax = 0;
113
114     const vlc_chroma_description_t *chroma =
115             vlc_fourcc_GetChromaDescription(fourcc_in);
116     if (!chroma || chroma->plane_count != 3 || chroma->pixel_size != 1) {
117         msg_Err(filter, "Unsupported chroma (%4.4s)", (char*)&fourcc_in);
118         return VLC_EGENERIC;
119     }
120
121     if (fourcc_in != fourcc_out) {
122         msg_Err(filter, "Input and output chromas don't match");
123         return VLC_EGENERIC;
124     }
125
126     /* Allocate structure */
127     sys = calloc(1, sizeof(filter_sys_t));
128     if (!sys) {
129         return VLC_ENOMEM;
130     }
131     cfg = &sys->cfg;
132
133     sys->chroma = chroma;
134
135     for (int i = 0; i < 3; ++i) {
136         sys->w[i] = fmt_in->i_width  * chroma->p[i].w.num / chroma->p[i].w.den;
137         if (sys->w[i] > wmax) wmax = sys->w[i];
138         sys->h[i] = fmt_out->i_height * chroma->p[i].h.num / chroma->p[i].h.den;
139     }
140     cfg->Line = malloc(wmax*sizeof(unsigned int));
141     if (!cfg->Line) {
142         free(sys);
143         return VLC_ENOMEM;
144     }
145
146     config_ChainParse(filter, FILTER_PREFIX, filter_options,
147                       filter->p_cfg);
148
149
150     vlc_mutex_init( &sys->coefs_mutex );
151     sys->b_recalc_coefs = true;
152     sys->luma_spat = var_CreateGetFloatCommand(filter, FILTER_PREFIX "luma-spat");
153     sys->chroma_spat = var_CreateGetFloatCommand(filter, FILTER_PREFIX "chroma-spat");
154     sys->luma_temp = var_CreateGetFloatCommand(filter, FILTER_PREFIX "luma-temp");
155     sys->chroma_temp = var_CreateGetFloatCommand(filter, FILTER_PREFIX "chroma-temp");
156
157     filter->p_sys = sys;
158     filter->pf_video_filter = Filter;
159
160     var_AddCallback( filter, FILTER_PREFIX "luma-spat", DenoiseCallback, sys );
161     var_AddCallback( filter, FILTER_PREFIX "chroma-spat", DenoiseCallback, sys );
162     var_AddCallback( filter, FILTER_PREFIX "luma-temp", DenoiseCallback, sys );
163     var_AddCallback( filter, FILTER_PREFIX "chroma-temp", DenoiseCallback, sys );
164
165     return VLC_SUCCESS;
166 }
167
168 /*****************************************************************************
169  * Close
170  *****************************************************************************/
171 static void Close(vlc_object_t *this)
172 {
173     filter_t *filter = (filter_t *)this;
174     filter_sys_t *sys = filter->p_sys;
175     struct vf_priv_s *cfg = &sys->cfg;
176
177     var_DelCallback( filter, FILTER_PREFIX "luma-spat", DenoiseCallback, sys );
178     var_DelCallback( filter, FILTER_PREFIX "chroma-spat", DenoiseCallback, sys );
179     var_DelCallback( filter, FILTER_PREFIX "luma-temp", DenoiseCallback, sys );
180     var_DelCallback( filter, FILTER_PREFIX "chroma-temp", DenoiseCallback, sys );
181
182     vlc_mutex_destroy( &sys->coefs_mutex );
183
184     for (int i = 0; i < 3; ++i) {
185         free(cfg->Frame[i]);
186     }
187     free(cfg->Line);
188     free(sys);
189 }
190
191 /*****************************************************************************
192  * Filter
193  *****************************************************************************/
194 static picture_t *Filter(filter_t *filter, picture_t *src)
195 {
196     picture_t *dst;
197     filter_sys_t *sys = filter->p_sys;
198     struct vf_priv_s *cfg = &sys->cfg;
199     bool recalc = false;
200
201     if (!src) return NULL;
202
203     dst = filter_NewPicture(filter);
204     if ( unlikely(!dst) ) {
205         picture_Release(src);
206         return NULL;
207     }
208     vlc_mutex_lock( &sys->coefs_mutex );
209     recalc = sys->b_recalc_coefs;
210     sys->b_recalc_coefs = false;
211
212     if( unlikely( recalc ) )
213     {
214         msg_Dbg( filter, "Changing coefs to %.2f %.2f %.2f %.2f",
215                             sys->luma_spat, sys->luma_temp, sys->chroma_spat, sys->chroma_temp );
216         PrecalcCoefs(cfg->Coefs[0], sys->luma_spat);
217         PrecalcCoefs(cfg->Coefs[1], sys->luma_temp);
218         PrecalcCoefs(cfg->Coefs[2], sys->chroma_spat);
219         PrecalcCoefs(cfg->Coefs[3], sys->chroma_temp);
220     }
221     vlc_mutex_unlock( &sys->coefs_mutex );
222
223     deNoise(src->p[0].p_pixels, dst->p[0].p_pixels,
224             cfg->Line, &cfg->Frame[0], sys->w[0], sys->h[0],
225             src->p[0].i_pitch, dst->p[0].i_pitch,
226             cfg->Coefs[0],
227             cfg->Coefs[0],
228             cfg->Coefs[1]);
229     deNoise(src->p[1].p_pixels, dst->p[1].p_pixels,
230             cfg->Line, &cfg->Frame[1], sys->w[1], sys->h[1],
231             src->p[1].i_pitch, dst->p[1].i_pitch,
232             cfg->Coefs[2],
233             cfg->Coefs[2],
234             cfg->Coefs[3]);
235     deNoise(src->p[2].p_pixels, dst->p[2].p_pixels,
236             cfg->Line, &cfg->Frame[2], sys->w[2], sys->h[2],
237             src->p[2].i_pitch, dst->p[2].i_pitch,
238             cfg->Coefs[2],
239             cfg->Coefs[2],
240             cfg->Coefs[3]);
241
242     return CopyInfoAndRelease(dst, src);
243 }
244
245
246 static int DenoiseCallback( vlc_object_t *p_this, char const *psz_var,
247                             vlc_value_t oldval, vlc_value_t newval,
248                             void *p_data )
249 {
250     VLC_UNUSED(p_this); VLC_UNUSED(oldval);
251
252     filter_sys_t *sys = (filter_sys_t*)p_data;
253
254     /* Just take values and flag for recalc so we don't block UI thread calling this
255      * and don't right thread safety calcing coefs in here without mutex*/
256     vlc_mutex_lock( &sys->coefs_mutex );
257     if( !strcmp( psz_var, FILTER_PREFIX "luma-spat") )
258         sys->luma_spat = newval.f_float;
259     else if( !strcmp( psz_var, FILTER_PREFIX "luma-temp") )
260         sys->luma_temp = newval.f_float;
261     else if( !strcmp( psz_var, FILTER_PREFIX "chroma-temp") )
262         sys->chroma_spat = newval.f_float;
263     else if( !strcmp( psz_var, FILTER_PREFIX "chroma-spat") )
264         sys->chroma_temp = newval.f_float;
265     sys->b_recalc_coefs = true;
266     vlc_mutex_unlock( &sys->coefs_mutex );
267
268     return VLC_SUCCESS;
269 }