1 /*
2 * Copyright (c) 2003 Daniel Moreno <comac AT comac DOT darktech DOT org>
3 * Copyright (c) 2010 Baptiste Coudurier
4 * Copyright (c) 2012 Loren Merritt
5 *
6 * This file is part of FFmpeg, ported from MPlayer.
7 *
8 * FFmpeg is free software; you can redistribute it and/or modify
9 * it under the terms of the GNU General Public License as published by
10 * the Free Software Foundation; either version 2 of the License, or
11 * (at your option) any later version.
12 *
13 * FFmpeg is distributed in the hope that it will be useful,
14 * but WITHOUT ANY WARRANTY; without even the implied warranty of
15 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16 * GNU General Public License for more details.
17 *
18 * You should have received a copy of the GNU General Public License along
19 * with FFmpeg; if not, write to the Free Software Foundation, Inc.,
20 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
21 */
22
23 /**
24 * @file
25 * high quality 3d video denoiser, ported from MPlayer
26 * libmpcodecs/vf_hqdn3d.c.
27 */
28
29 #include <float.h>
30
31 #include "config.h"
37
43
44 #define LUT_BITS (depth==16 ? 8 : 4)
45 #define LOAD(x) (((depth == 8 ? src[x] : AV_RN16A(src + (x) * 2)) << (16 - depth))\
46 + (((1 << (16 - depth)) - 1) >> 1))
47 #define STORE(x,val) (depth == 8 ? dst[x] = (val) >> (16 - depth) : \
48 AV_WN16A(dst + (x) * 2, (val) >> (16 - depth)))
49
51 static uint32_t
lowpass(
int prev,
int cur, int16_t *coef,
int depth)
52 {
53 int d = (prev - cur) >> (8 -
LUT_BITS);
54 return cur + coef[d];
55 }
56
59 uint16_t *frame_ant,
60 int w, int h, int sstride, int dstride,
61 int16_t *temporal,
int depth)
62 {
64 uint32_t tmp;
65
67
68 for (y = 0; y < h; y++) {
69 for (x = 0; x < w; x++) {
70 frame_ant[x] = tmp =
lowpass(frame_ant[x],
LOAD(x), temporal, depth);
72 }
73 src += sstride;
74 dst += dstride;
75 frame_ant += w;
76 }
77 }
78
82 uint16_t *line_ant, uint16_t *frame_ant,
83 int w, int h, int sstride, int dstride,
84 int16_t *spatial, int16_t *temporal,
int depth)
85 {
87 uint32_t pixel_ant;
88 uint32_t tmp;
89
92
93 /* First line has no top neighbor. Only left one for each tmp and
94 * last frame */
96 for (x = 0; x < w; x++) {
97 line_ant[x] = tmp = pixel_ant =
lowpass(pixel_ant,
LOAD(x), spatial, depth);
98 frame_ant[x] = tmp =
lowpass(frame_ant[x], tmp, temporal, depth);
100 }
101
102 for (y = 1; y < h; y++) {
103 src += sstride;
104 dst += dstride;
105 frame_ant += w;
108 continue;
109 }
111 for (x = 0; x < w-1; x++) {
112 line_ant[x] = tmp =
lowpass(line_ant[x], pixel_ant, spatial, depth);
113 pixel_ant =
lowpass(pixel_ant,
LOAD(x+1), spatial, depth);
114 frame_ant[x] = tmp =
lowpass(frame_ant[x], tmp, temporal, depth);
116 }
117 line_ant[x] = tmp =
lowpass(line_ant[x], pixel_ant, spatial, depth);
118 frame_ant[x] = tmp =
lowpass(frame_ant[x], tmp, temporal, depth);
120 }
121 }
122
126 uint16_t *line_ant, uint16_t **frame_ant_ptr,
127 int w, int h, int sstride, int dstride,
128 int16_t *spatial, int16_t *temporal,
int depth)
129 {
130 // FIXME: For 16bit depth, frame_ant could be a pointer to the previous
131 // filtered frame rather than a separate buffer.
133 uint16_t *frame_ant = *frame_ant_ptr;
134 if (!frame_ant) {
136 *frame_ant_ptr = frame_ant =
av_malloc(w*h*
sizeof(uint16_t));
137 for (y = 0; y < h; y++, src += sstride, frame_ant += w)
138 for (x = 0; x < w; x++)
139 frame_ant[x] =
LOAD(x);
140 src = frame_src;
141 frame_ant = *frame_ant_ptr;
142 }
143
144 if (spatial[0])
146 w, h, sstride, dstride, spatial, temporal, depth);
147 else
149 w, h, sstride, dstride, temporal, depth);
150 }
151
152 #define denoise(...) \
153 switch (s->depth) {\
154 case 8: denoise_depth(__VA_ARGS__, 8); break;\
155 case 9: denoise_depth(__VA_ARGS__, 9); break;\
156 case 10: denoise_depth(__VA_ARGS__, 10); break;\
157 case 16: denoise_depth(__VA_ARGS__, 16); break;\
158 }
159
161 {
162 int i;
163 double gamma, simil,
C;
165 if (!ct)
166 return NULL;
167
168 gamma = log(0.25) / log(1.0 -
FFMIN(dist25,252.0)/255.0 - 0.00001);
169
171 double f = ((i<<(9-
LUT_BITS)) + (1<<(8-
LUT_BITS)) - 1) / 512.0;
// midpoint of the bin
172 simil = 1.0 -
FFABS(f) / 255.0;
173 C = pow(simil, gamma) * 256.0 * f;
175 }
176
177 ct[0] = !!dist25;
178 return ct;
179 }
180
181 #define PARAM1_DEFAULT 4.0
182 #define PARAM2_DEFAULT 3.0
183 #define PARAM3_DEFAULT 6.0
184
186 {
188
197
201
202 return 0;
203 }
204
206 {
208
217 }
218
220 {
242 };
243
245
246 return 0;
247 }
248
250 {
253 int i;
254
256
260
264
265 for (i = 0; i < 4; i++) {
269 }
270
271 if (ARCH_X86)
273
274 return 0;
275 }
276
278 {
282
285
287 direct = 1;
289 } else {
290 direct = 0;
292 if (!out) {
295 }
296
298 }
299
300 for (c = 0; c < 3; c++) {
308 }
309
313 }
314
315 if (!direct)
317
319 }
320
321 #define OFFSET(x) offsetof(HQDN3DContext, x)
322 #define FLAGS AV_OPT_FLAG_VIDEO_PARAM | AV_OPT_FLAG_FILTERING_PARAM
328 { NULL },
329 };
330
332
334 {
339 },
340 { NULL }
341 };
342
343
345 {
348 },
349 { NULL }
350 };
351
355
357 .priv_class = &hqdn3d_class,
361
362 .
inputs = avfilter_vf_hqdn3d_inputs,
363 .
outputs = avfilter_vf_hqdn3d_outputs,
365 };