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
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) {
137 if (!frame_ant)
139 for (y = 0; y < h; y++, src += sstride, frame_ant += w)
140 for (x = 0; x < w; x++)
141 frame_ant[x] =
LOAD(x);
142 src = frame_src;
143 frame_ant = *frame_ant_ptr;
144 }
145
146 if (spatial[0])
148 w, h, sstride, dstride, spatial, temporal, depth);
149 else
151 w, h, sstride, dstride, temporal, depth);
152 emms_c();
153 return 0;
154 }
155
156 #define denoise(...) \
157 do { \
158 int ret = AVERROR_BUG; \
159 switch (s->depth) { \
160 case 8: ret = denoise_depth(__VA_ARGS__, 8); break; \
161 case 9: ret = denoise_depth(__VA_ARGS__, 9); break; \
162 case 10: ret = denoise_depth(__VA_ARGS__, 10); break; \
163 case 16: ret = denoise_depth(__VA_ARGS__, 16); break; \
164 } \
165 if (ret < 0) { \
166 av_frame_free(&out); \
167 if (!direct) \
168 av_frame_free(&in); \
169 return ret; \
170 } \
171 } while (0)
172
174 {
175 int i;
176 double gamma, simil,
C;
178 if (!ct)
180
181 gamma = log(0.25) / log(1.0 -
FFMIN(dist25,252.0)/255.0 - 0.00001);
182
184 double f = ((i<<(9-
LUT_BITS)) + (1<<(8-
LUT_BITS)) - 1) / 512.0;
// midpoint of the bin
185 simil = 1.0 -
FFABS(f) / 255.0;
186 C = pow(simil, gamma) * 256.0 * f;
188 }
189
190 ct[0] = !!dist25;
191 return ct;
192 }
193
194 #define PARAM1_DEFAULT 4.0
195 #define PARAM2_DEFAULT 3.0
196 #define PARAM3_DEFAULT 6.0
197
199 {
201
210
214
215 return 0;
216 }
217
219 {
221
230 }
231
233 {
255 };
256
258
259 return 0;
260 }
261
263 {
266 int i;
267
269
273
277
278 for (i = 0; i < 4; i++) {
282 }
283
284 if (ARCH_X86)
286
287 return 0;
288 }
289
291 {
295
298
299 if (direct) {
301 } else {
303 if (!out) {
306 }
307
309 }
310
311 for (c = 0; c < 3; c++) {
319 }
320
324 }
325
326 if (!direct)
328
330 }
331
332 #define OFFSET(x) offsetof(HQDN3DContext, x)
333 #define FLAGS AV_OPT_FLAG_VIDEO_PARAM | AV_OPT_FLAG_FILTERING_PARAM
340 };
341
343
345 {
350 },
352 };
353
354
356 {
359 },
361 };
362
367 .priv_class = &hqdn3d_class,
371 .
inputs = avfilter_vf_hqdn3d_inputs,
372 .
outputs = avfilter_vf_hqdn3d_outputs,
374 };