FFmpeg coverage


Directory: ../../../ffmpeg/
File: src/libavfilter/vf_hqdn3d.c
Date: 2024-03-28 14:59:00
Exec Total Coverage
Lines: 117 148 79.1%
Functions: 10 12 83.3%
Branches: 60 135 44.4%

Line Branch Exec Source
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"
32 #include "libavutil/attributes.h"
33 #include "libavutil/common.h"
34 #include "libavutil/emms.h"
35 #include "libavutil/pixdesc.h"
36 #include "libavutil/intreadwrite.h"
37 #include "libavutil/opt.h"
38
39 #include "avfilter.h"
40 #include "internal.h"
41 #include "video.h"
42 #include "vf_hqdn3d.h"
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
50 av_always_inline
51 163840 static uint32_t lowpass(int prev, int cur, int16_t *coef, int depth)
52 {
53
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 163840 times.
163840 int d = (prev - cur) >> (8 - LUT_BITS);
54 163840 return cur + coef[d];
55 }
56
57 av_always_inline
58 static void denoise_temporal(uint8_t *src, uint8_t *dst,
59 uint16_t *frame_ant,
60 int w, int h, int sstride, int dstride,
61 int16_t *temporal, int depth)
62 {
63 long x, y;
64 uint32_t tmp;
65
66 temporal += 256 << LUT_BITS;
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);
71 STORE(x, tmp);
72 }
73 src += sstride;
74 dst += dstride;
75 frame_ant += w;
76 }
77 }
78
79 av_always_inline
80 369 static void denoise_spatial(HQDN3DContext *s,
81 uint8_t *src, uint8_t *dst,
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 {
86 long x, y;
87 uint32_t pixel_ant;
88 uint32_t tmp;
89
90
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 369 times.
369 spatial += 256 << LUT_BITS;
91
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 369 times.
369 temporal += 256 << LUT_BITS;
92
93 /* First line has no top neighbor. Only left one for each tmp and
94 * last frame */
95
1/2
✓ Branch 0 taken 369 times.
✗ Branch 1 not taken.
369 pixel_ant = LOAD(0);
96
2/2
✓ Branch 0 taken 81920 times.
✓ Branch 1 taken 369 times.
82289 for (x = 0; x < w; x++) {
97
1/2
✓ Branch 0 taken 81920 times.
✗ Branch 1 not taken.
81920 line_ant[x] = tmp = pixel_ant = lowpass(pixel_ant, LOAD(x), spatial, depth);
98 81920 frame_ant[x] = tmp = lowpass(frame_ant[x], tmp, temporal, depth);
99
1/2
✓ Branch 0 taken 81920 times.
✗ Branch 1 not taken.
81920 STORE(x, tmp);
100 }
101
102
2/2
✓ Branch 0 taken 63471 times.
✓ Branch 1 taken 369 times.
63840 for (y = 1; y < h; y++) {
103 63471 src += sstride;
104 63471 dst += dstride;
105 63471 frame_ant += w;
106
1/2
✓ Branch 0 taken 63471 times.
✗ Branch 1 not taken.
63471 if (s->denoise_row[depth]) {
107 63471 s->denoise_row[depth](src, dst, line_ant, frame_ant, w, spatial, temporal);
108 63471 continue;
109 }
110 pixel_ant = LOAD(0);
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);
115 STORE(x, tmp);
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);
119 STORE(x, tmp);
120 }
121 369 }
122
123 av_always_inline
124 369 static int denoise_depth(HQDN3DContext *s,
125 uint8_t *src, uint8_t *dst,
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 16-bit depth, frame_ant could be a pointer to the previous
131 // filtered frame rather than a separate buffer.
132 long x, y;
133 369 uint16_t *frame_ant = *frame_ant_ptr;
134
2/2
✓ Branch 0 taken 6 times.
✓ Branch 1 taken 363 times.
369 if (!frame_ant) {
135 6 uint8_t *frame_src = src;
136 6 *frame_ant_ptr = frame_ant = av_malloc_array(w, h*sizeof(uint16_t));
137
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 6 times.
6 if (!frame_ant)
138 return AVERROR(ENOMEM);
139
2/2
✓ Branch 0 taken 1056 times.
✓ Branch 1 taken 6 times.
1062 for (y = 0; y < h; y++, src += sstride, frame_ant += w)
140
2/2
✓ Branch 0 taken 267264 times.
✓ Branch 1 taken 1056 times.
268320 for (x = 0; x < w; x++)
141
1/2
✓ Branch 0 taken 267264 times.
✗ Branch 1 not taken.
267264 frame_ant[x] = LOAD(x);
142 6 src = frame_src;
143 6 frame_ant = *frame_ant_ptr;
144 }
145
146
1/2
✓ Branch 0 taken 369 times.
✗ Branch 1 not taken.
369 if (spatial[0])
147 369 denoise_spatial(s, src, dst, line_ant, frame_ant,
148 w, h, sstride, dstride, spatial, temporal, depth);
149 else
150 denoise_temporal(src, dst, frame_ant,
151 w, h, sstride, dstride, temporal, depth);
152 369 emms_c();
153 369 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 12: ret = denoise_depth(__VA_ARGS__, 12); break; \
164 case 14: ret = denoise_depth(__VA_ARGS__, 14); break; \
165 case 16: ret = denoise_depth(__VA_ARGS__, 16); break; \
166 } \
167 if (ret < 0) { \
168 av_frame_free(&out); \
169 if (!direct) \
170 av_frame_free(&in); \
171 return ret; \
172 } \
173 } while (0)
174
175 8 static void precalc_coefs(double dist25, int depth, int16_t *ct)
176 {
177 int i;
178 double gamma, simil, C;
179
180
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 8 times.
8 gamma = log(0.25) / log(1.0 - FFMIN(dist25,252.0)/255.0 - 0.00001);
181
182
4/6
✗ Branch 0 not taken.
✓ Branch 1 taken 8 times.
✗ Branch 2 not taken.
✓ Branch 3 taken 65544 times.
✓ Branch 4 taken 65536 times.
✓ Branch 5 taken 8 times.
65544 for (i = -(256<<LUT_BITS); i < 256<<LUT_BITS; i++) {
183
2/4
✗ Branch 0 not taken.
✓ Branch 1 taken 65536 times.
✗ Branch 2 not taken.
✓ Branch 3 taken 65536 times.
65536 double f = (i * (1 << (9-LUT_BITS)) + (1<<(8-LUT_BITS)) - 1) / 512.0; // midpoint of the bin
184
2/2
✓ Branch 0 taken 256 times.
✓ Branch 1 taken 65280 times.
65536 simil = FFMAX(0, 1.0 - fabs(f) / 255.0);
185 65536 C = pow(simil, gamma) * 256.0 * f;
186
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 65536 times.
65536 ct[(256<<LUT_BITS)+i] = lrint(C);
187 }
188
189 8 ct[0] = !!dist25;
190 8 }
191
192 #define PARAM1_DEFAULT 4.0
193 #define PARAM2_DEFAULT 3.0
194 #define PARAM3_DEFAULT 6.0
195
196 4 static av_cold int init(AVFilterContext *ctx)
197 {
198 4 HQDN3DContext *s = ctx->priv;
199
200
1/2
✓ Branch 0 taken 4 times.
✗ Branch 1 not taken.
4 if (!s->strength[LUMA_SPATIAL])
201 4 s->strength[LUMA_SPATIAL] = PARAM1_DEFAULT;
202
1/2
✓ Branch 0 taken 4 times.
✗ Branch 1 not taken.
4 if (!s->strength[CHROMA_SPATIAL])
203 4 s->strength[CHROMA_SPATIAL] = PARAM2_DEFAULT * s->strength[LUMA_SPATIAL] / PARAM1_DEFAULT;
204
1/2
✓ Branch 0 taken 4 times.
✗ Branch 1 not taken.
4 if (!s->strength[LUMA_TMP])
205 4 s->strength[LUMA_TMP] = PARAM3_DEFAULT * s->strength[LUMA_SPATIAL] / PARAM1_DEFAULT;
206
1/2
✓ Branch 0 taken 4 times.
✗ Branch 1 not taken.
4 if (!s->strength[CHROMA_TMP])
207 4 s->strength[CHROMA_TMP] = s->strength[LUMA_TMP] * s->strength[CHROMA_SPATIAL] / s->strength[LUMA_SPATIAL];
208
209 4 av_log(ctx, AV_LOG_VERBOSE, "ls:%f cs:%f lt:%f ct:%f\n",
210 s->strength[LUMA_SPATIAL], s->strength[CHROMA_SPATIAL],
211 s->strength[LUMA_TMP], s->strength[CHROMA_TMP]);
212
213 4 return 0;
214 }
215
216 6 static av_cold void uninit(AVFilterContext *ctx)
217 {
218 6 HQDN3DContext *s = ctx->priv;
219
220 6 av_freep(&s->coefs[0]);
221 6 av_freep(&s->coefs[1]);
222 6 av_freep(&s->coefs[2]);
223 6 av_freep(&s->coefs[3]);
224 6 av_freep(&s->line[0]);
225 6 av_freep(&s->line[1]);
226 6 av_freep(&s->line[2]);
227 6 av_freep(&s->frame_prev[0]);
228 6 av_freep(&s->frame_prev[1]);
229 6 av_freep(&s->frame_prev[2]);
230 6 }
231
232 static const enum AVPixelFormat pix_fmts[] = {
233 AV_PIX_FMT_YUV420P, AV_PIX_FMT_YUV422P, AV_PIX_FMT_YUV444P,
234 AV_PIX_FMT_YUV410P, AV_PIX_FMT_YUV411P, AV_PIX_FMT_YUV440P,
235 AV_PIX_FMT_YUVJ420P, AV_PIX_FMT_YUVJ422P, AV_PIX_FMT_YUVJ444P, AV_PIX_FMT_YUVJ440P,
236 AV_PIX_FMT_YUV420P9, AV_PIX_FMT_YUV422P9, AV_PIX_FMT_YUV444P9,
237 AV_PIX_FMT_YUV420P10, AV_PIX_FMT_YUV422P10, AV_PIX_FMT_YUV444P10,
238 AV_PIX_FMT_YUV440P10,
239 AV_PIX_FMT_YUV444P12, AV_PIX_FMT_YUV422P12, AV_PIX_FMT_YUV420P12,
240 AV_PIX_FMT_YUV440P12,
241 AV_PIX_FMT_YUV444P14, AV_PIX_FMT_YUV422P14, AV_PIX_FMT_YUV420P14,
242 AV_PIX_FMT_YUV420P16, AV_PIX_FMT_YUV422P16, AV_PIX_FMT_YUV444P16,
243 AV_PIX_FMT_NONE
244 };
245
246 2 static void calc_coefs(AVFilterContext *ctx)
247 {
248 2 HQDN3DContext *s = ctx->priv;
249
250
2/2
✓ Branch 0 taken 8 times.
✓ Branch 1 taken 2 times.
10 for (int i = 0; i < 4; i++)
251 8 precalc_coefs(s->strength[i], s->depth, s->coefs[i]);
252 2 }
253
254 2 static int config_input(AVFilterLink *inlink)
255 {
256 2 AVFilterContext *ctx = inlink->dst;
257 2 HQDN3DContext *s = inlink->dst->priv;
258 2 const AVPixFmtDescriptor *desc = av_pix_fmt_desc_get(inlink->format);
259 int i, depth;
260
261 2 uninit(inlink->dst);
262
263 2 s->hsub = desc->log2_chroma_w;
264 2 s->vsub = desc->log2_chroma_h;
265 2 s->depth = depth = desc->comp[0].depth;
266
267
2/2
✓ Branch 0 taken 6 times.
✓ Branch 1 taken 2 times.
8 for (i = 0; i < 3; i++) {
268 6 s->line[i] = av_malloc_array(inlink->w, sizeof(*s->line[i]));
269
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 6 times.
6 if (!s->line[i])
270 return AVERROR(ENOMEM);
271 }
272
273
2/2
✓ Branch 0 taken 8 times.
✓ Branch 1 taken 2 times.
10 for (i = 0; i < 4; i++) {
274
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 8 times.
8 s->coefs[i] = av_malloc((512<<LUT_BITS) * sizeof(int16_t));
275
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 8 times.
8 if (!s->coefs[i])
276 return AVERROR(ENOMEM);
277 }
278
279 2 calc_coefs(ctx);
280
281 #if ARCH_X86
282 2 ff_hqdn3d_init_x86(s);
283 #endif
284
285 2 return 0;
286 }
287
288 typedef struct ThreadData {
289 AVFrame *in, *out;
290 int direct;
291 } ThreadData;
292
293 369 static int do_denoise(AVFilterContext *ctx, void *data, int job_nr, int n_jobs)
294 {
295 369 HQDN3DContext *s = ctx->priv;
296 369 const ThreadData *td = data;
297 369 AVFrame *out = td->out;
298 369 AVFrame *in = td->in;
299 369 int direct = td->direct;
300
301
6/35
✓ Branch 0 taken 369 times.
✗ Branch 1 not taken.
✗ Branch 2 not taken.
✗ Branch 3 not taken.
✗ Branch 4 not taken.
✗ Branch 5 not taken.
✗ Branch 6 not taken.
✓ Branch 7 taken 246 times.
✓ Branch 8 taken 123 times.
✓ Branch 9 taken 246 times.
✓ Branch 10 taken 123 times.
✗ Branch 12 not taken.
✗ Branch 13 not taken.
✗ Branch 14 not taken.
✗ Branch 15 not taken.
✗ Branch 17 not taken.
✗ Branch 18 not taken.
✗ Branch 19 not taken.
✗ Branch 20 not taken.
✗ Branch 22 not taken.
✗ Branch 23 not taken.
✗ Branch 24 not taken.
✗ Branch 25 not taken.
✗ Branch 27 not taken.
✗ Branch 28 not taken.
✗ Branch 29 not taken.
✗ Branch 30 not taken.
✗ Branch 32 not taken.
✗ Branch 33 not taken.
✗ Branch 34 not taken.
✗ Branch 35 not taken.
✗ Branch 37 not taken.
✓ Branch 38 taken 369 times.
✗ Branch 40 not taken.
✗ Branch 41 not taken.
369 denoise(s, in->data[job_nr], out->data[job_nr],
302 s->line[job_nr], &s->frame_prev[job_nr],
303 AV_CEIL_RSHIFT(in->width, (!!job_nr * s->hsub)),
304 AV_CEIL_RSHIFT(in->height, (!!job_nr * s->vsub)),
305 in->linesize[job_nr], out->linesize[job_nr],
306 s->coefs[job_nr ? CHROMA_SPATIAL : LUMA_SPATIAL],
307 s->coefs[job_nr ? CHROMA_TMP : LUMA_TMP]);
308
309 369 return 0;
310 }
311
312 123 static int filter_frame(AVFilterLink *inlink, AVFrame *in)
313 {
314 123 AVFilterContext *ctx = inlink->dst;
315 123 AVFilterLink *outlink = ctx->outputs[0];
316
317 AVFrame *out;
318
4/4
✓ Branch 1 taken 91 times.
✓ Branch 2 taken 32 times.
✓ Branch 3 taken 84 times.
✓ Branch 4 taken 7 times.
123 int direct = av_frame_is_writable(in) && !ctx->is_disabled;
319 ThreadData td;
320
321
2/2
✓ Branch 0 taken 84 times.
✓ Branch 1 taken 39 times.
123 if (direct) {
322 84 out = in;
323 } else {
324 39 out = ff_get_video_buffer(outlink, outlink->w, outlink->h);
325
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 39 times.
39 if (!out) {
326 av_frame_free(&in);
327 return AVERROR(ENOMEM);
328 }
329
330 39 av_frame_copy_props(out, in);
331 }
332
333 123 td.in = in;
334 123 td.out = out;
335 123 td.direct = direct;
336 /* one thread per plane */
337 123 ff_filter_execute(ctx, do_denoise, &td, NULL, 3);
338
339
2/2
✓ Branch 0 taken 10 times.
✓ Branch 1 taken 113 times.
123 if (ctx->is_disabled) {
340 10 av_frame_free(&out);
341 10 return ff_filter_frame(outlink, in);
342 }
343
344
2/2
✓ Branch 0 taken 29 times.
✓ Branch 1 taken 84 times.
113 if (!direct)
345 29 av_frame_free(&in);
346
347 113 return ff_filter_frame(outlink, out);
348 }
349
350 static int process_command(AVFilterContext *ctx, const char *cmd, const char *args,
351 char *res, int res_len, int flags)
352 {
353 int ret;
354
355 ret = ff_filter_process_command(ctx, cmd, args, res, res_len, flags);
356 if (ret < 0)
357 return ret;
358
359 calc_coefs(ctx);
360
361 return 0;
362 }
363
364 #define OFFSET(x) offsetof(HQDN3DContext, x)
365 #define FLAGS AV_OPT_FLAG_VIDEO_PARAM | AV_OPT_FLAG_FILTERING_PARAM | AV_OPT_FLAG_RUNTIME_PARAM
366 static const AVOption hqdn3d_options[] = {
367 { "luma_spatial", "spatial luma strength", OFFSET(strength[LUMA_SPATIAL]), AV_OPT_TYPE_DOUBLE, { .dbl = 0.0 }, 0, DBL_MAX, FLAGS },
368 { "chroma_spatial", "spatial chroma strength", OFFSET(strength[CHROMA_SPATIAL]), AV_OPT_TYPE_DOUBLE, { .dbl = 0.0 }, 0, DBL_MAX, FLAGS },
369 { "luma_tmp", "temporal luma strength", OFFSET(strength[LUMA_TMP]), AV_OPT_TYPE_DOUBLE, { .dbl = 0.0 }, 0, DBL_MAX, FLAGS },
370 { "chroma_tmp", "temporal chroma strength", OFFSET(strength[CHROMA_TMP]), AV_OPT_TYPE_DOUBLE, { .dbl = 0.0 }, 0, DBL_MAX, FLAGS },
371 { NULL }
372 };
373
374 AVFILTER_DEFINE_CLASS(hqdn3d);
375
376 static const AVFilterPad avfilter_vf_hqdn3d_inputs[] = {
377 {
378 .name = "default",
379 .type = AVMEDIA_TYPE_VIDEO,
380 .config_props = config_input,
381 .filter_frame = filter_frame,
382 },
383 };
384
385
386 const AVFilter ff_vf_hqdn3d = {
387 .name = "hqdn3d",
388 .description = NULL_IF_CONFIG_SMALL("Apply a High Quality 3D Denoiser."),
389 .priv_size = sizeof(HQDN3DContext),
390 .priv_class = &hqdn3d_class,
391 .init = init,
392 .uninit = uninit,
393 FILTER_INPUTS(avfilter_vf_hqdn3d_inputs),
394 FILTER_OUTPUTS(ff_video_default_filterpad),
395 FILTER_PIXFMTS_ARRAY(pix_fmts),
396 .flags = AVFILTER_FLAG_SUPPORT_TIMELINE_INTERNAL | AVFILTER_FLAG_SLICE_THREADS,
397 .process_command = process_command,
398 };
399