FFmpeg
Loading...
Searching...
No Matches
vf_nlmeans.c
Go to the documentation of this file.
1/*
2 * Copyright (c) 2018 Clément Bœsch <u pkh me>
3 *
4 * This file is part of FFmpeg.
5 *
6 * FFmpeg is free software; you can redistribute it and/or
7 * modify it under the terms of the GNU Lesser General Public
8 * License as published by the Free Software Foundation; either
9 * version 2.1 of the License, or (at your option) any later version.
10 *
11 * FFmpeg is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
14 * Lesser General Public License for more details.
15 *
16 * You should have received a copy of the GNU Lesser General Public
17 * License along with FFmpeg; if not, write to the Free Software
18 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
19 */
20
21#include <math.h>
22#include <string.h>
23
24#include "checkasm.h"
26#include "libavutil/avassert.h"
27#include "libavutil/mem.h"
29
30#define randomize_buffer(buf, size) do { \
31 int i; \
32 for (i = 0; i < size / 4; i++) \
33 ((uint32_t *)buf)[i] = rnd(); \
34} while (0)
35
37{
38 NLMeansDSPContext dsp = {0};
39
40 const int w = 123; // source width
41 const int h = 45; // source height
42 const int p = 3; // patch half size
43 const int r = 2; // research window half size
44
45 ff_nlmeans_init(&dsp);
46
47 /* See the filter's code for the explanations on the variables */
48 if (check_func(dsp.compute_safe_ssd_integral_image, "ssd_integral_image")) {
49 int offx, offy;
50 const int e = p + r;
51 const int ii_w = w + e*2;
52 const int ii_h = h + e*2;
53 const int ii_lz_32 = FFALIGN(ii_w + 1, 4);
54 uint32_t *ii_orig_ref = av_calloc(ii_h + 1, ii_lz_32 * sizeof(*ii_orig_ref));
55 uint32_t *ii_ref = ii_orig_ref + ii_lz_32 + 1;
56 uint32_t *ii_orig_new = av_calloc(ii_h + 1, ii_lz_32 * sizeof(*ii_orig_new));
57 uint32_t *ii_new = ii_orig_new + ii_lz_32 + 1;
58 const int src_lz = FFALIGN(w, 16);
59 uint8_t *src = av_calloc(h, src_lz);
60
61 declare_func(void, uint32_t *dst, ptrdiff_t dst_linesize_32,
62 const uint8_t *s1, ptrdiff_t linesize1,
63 const uint8_t *s2, ptrdiff_t linesize2,
64 int w, int h);
65
66 randomize_buffer(src, h * src_lz);
67
68 for (offy = -r; offy <= r; offy++) {
69 for (offx = -r; offx <= r; offx++) {
70 if (offx || offy) {
71 const int s1x = e;
72 const int s1y = e;
73 const int s2x = e + offx;
74 const int s2y = e + offy;
75 const int startx_safe = FFMAX(s1x, s2x);
76 const int starty_safe = FFMAX(s1y, s2y);
77 const int u_endx_safe = FFMIN(s1x + w, s2x + w);
78 const int endy_safe = FFMIN(s1y + h, s2y + h);
79 const int safe_pw = (u_endx_safe - startx_safe) & ~0xf;
80 const int safe_ph = endy_safe - starty_safe;
81
82 av_assert0(safe_pw && safe_ph);
83 av_assert0(startx_safe - s1x >= 0); av_assert0(startx_safe - s1x < w);
84 av_assert0(starty_safe - s1y >= 0); av_assert0(starty_safe - s1y < h);
85 av_assert0(startx_safe - s2x >= 0); av_assert0(startx_safe - s2x < w);
86 av_assert0(starty_safe - s2y >= 0); av_assert0(starty_safe - s2y < h);
87
88 memset(ii_ref, 0, (ii_lz_32 * ii_h - 1) * sizeof(*ii_ref));
89 memset(ii_new, 0, (ii_lz_32 * ii_h - 1) * sizeof(*ii_new));
90
91 call_ref(ii_ref + starty_safe*ii_lz_32 + startx_safe, ii_lz_32,
92 src + (starty_safe - s1y) * src_lz + (startx_safe - s1x), src_lz,
93 src + (starty_safe - s2y) * src_lz + (startx_safe - s2x), src_lz,
94 safe_pw, safe_ph);
95 call_new(ii_new + starty_safe*ii_lz_32 + startx_safe, ii_lz_32,
96 src + (starty_safe - s1y) * src_lz + (startx_safe - s1x), src_lz,
97 src + (starty_safe - s2y) * src_lz + (startx_safe - s2x), src_lz,
98 safe_pw, safe_ph);
99
100 if (memcmp(ii_ref, ii_new, (ii_lz_32 * ii_h - 1) * sizeof(*ii_ref)))
101 fail();
102
103 memset(ii_new, 0, (ii_lz_32 * ii_h - 1) * sizeof(*ii_new));
104 bench_new(ii_new + starty_safe*ii_lz_32 + startx_safe, ii_lz_32,
105 src + (starty_safe - s1y) * src_lz + (startx_safe - s1x), src_lz,
106 src + (starty_safe - s2y) * src_lz + (startx_safe - s2x), src_lz,
107 safe_pw, safe_ph);
108 }
109 }
110 }
111
112 av_freep(&ii_orig_ref);
113 av_freep(&ii_orig_new);
114 av_freep(&src);
115 }
116
117 if (check_func(dsp.compute_weights_line, "compute_weights_line")) {
118#define TEST_W 256
119#define MAX_MEANINGFUL_DIFF 255
120 const int startx = 10;
121 const int endx = 200;
122
123 // Allocate aligned buffers on stack
124 LOCAL_ALIGNED_32(uint32_t, iia, [TEST_W + 16]);
125 LOCAL_ALIGNED_32(uint32_t, iib, [TEST_W + 16]);
126 LOCAL_ALIGNED_32(uint32_t, iid, [TEST_W + 16]);
127 LOCAL_ALIGNED_32(uint32_t, iie, [TEST_W + 16]);
128 LOCAL_ALIGNED_32(uint8_t, src, [TEST_W + 16]);
129 LOCAL_ALIGNED_32(float, tw_ref, [TEST_W + 16]);
130 LOCAL_ALIGNED_32(float, tw_new, [TEST_W + 16]);
131 LOCAL_ALIGNED_32(float, sum_ref, [TEST_W + 16]);
132 LOCAL_ALIGNED_32(float, sum_new, [TEST_W + 16]);
133 LOCAL_ALIGNED_32(float, lut, [MAX_MEANINGFUL_DIFF + 1]);
134
135 declare_func(void, const uint32_t *const iia,
136 const uint32_t *const iib,
137 const uint32_t *const iid,
138 const uint32_t *const iie,
139 const uint8_t *const src,
140 float *total_weight,
141 float *sum,
142 const float *const weight_lut,
143 ptrdiff_t max_meaningful_diff,
144 ptrdiff_t startx, ptrdiff_t endx);
145
146 // Initialize LUT: weight = exp(-diff * scale)
147 // Using scale = 0.01 for testing
148 for (int i = 0; i <= MAX_MEANINGFUL_DIFF; i++)
149 lut[i] = expf(-i * 0.01f);
150
151 // Initialize source pixels
152 for (int i = 0; i < TEST_W; i++)
153 src[i] = rnd() & 0xff;
154
155 // Initialize integral images
156 // We need to ensure diff = e - d - b + a is non-negative and within range
157 // Set up as if computing real integral image values
158 for (int i = 0; i < TEST_W; i++) {
159 uint32_t base = rnd() % 1000;
160 iia[i] = base;
161 iib[i] = base + (rnd() % 100);
162 iid[i] = base + (rnd() % 100);
163 // e = a + (b - a) + (d - a) + diff
164 // So diff = e - d - b + a will be in range [0, max_meaningful_diff]
165 uint32_t diff = rnd() % (MAX_MEANINGFUL_DIFF + 1);
166 iie[i] = iia[i] + (iib[i] - iia[i]) + (iid[i] - iia[i]) + diff;
167 }
168
169 // Clear output buffers
170 memset(tw_ref, 0, (TEST_W + 16) * sizeof(float));
171 memset(tw_new, 0, (TEST_W + 16) * sizeof(float));
172 memset(sum_ref, 0, (TEST_W + 16) * sizeof(float));
173 memset(sum_new, 0, (TEST_W + 16) * sizeof(float));
174
175 call_ref(iia, iib, iid, iie, src, tw_ref, sum_ref, lut,
176 MAX_MEANINGFUL_DIFF, startx, endx);
177 call_new(iia, iib, iid, iie, src, tw_new, sum_new, lut,
178 MAX_MEANINGFUL_DIFF, startx, endx);
179
180 // Compare results with small tolerance for floating point
181 if (!float_near_abs_eps_array(tw_ref + startx, tw_new + startx, 1e-5f, endx - startx))
182 fail();
183 if (!float_near_abs_eps_array(sum_ref + startx, sum_new + startx, 1e-4f, endx - startx))
184 fail();
185
186 // Benchmark
187 memset(tw_new, 0, (TEST_W + 16) * sizeof(float));
188 memset(sum_new, 0, (TEST_W + 16) * sizeof(float));
189 bench_new(iia, iib, iid, iie, src, tw_new, sum_new, lut,
190 MAX_MEANINGFUL_DIFF, startx, endx);
191 }
192
193 report("dsp");
194}
uint8_t ptrdiff_t const uint8_t ptrdiff_t int intptr_t intptr_t int int16_t * dst
Definition dsp.h:87
simple assert() macros that are a bit more flexible than ISO C assert().
#define av_assert0(cond)
assert() equivalent, that is always enabled.
Definition avassert.h:42
#define i(width, name, range_min, range_max)
Definition cbs_h264.c:63
#define rnd
Definition checkasm.h:136
#define declare_func
Definition test.h:489
#define fail
Definition test.h:479
#define bench_new
Definition test.h:487
#define float_near_abs_eps_array
Definition utils.h:452
#define check_func
Definition test.h:481
#define call_new
Definition test.h:486
#define call_ref
Definition test.h:485
#define report
Definition test.h:480
#define r
Definition input.c:42
#define expf(x)
Definition libm.h:285
uint8_t w
Definition llvidencdsp.c:39
#define FFMIN(a, b)
Definition macros.h:49
#define FFMAX(a, b)
Definition macros.h:47
#define FFALIGN(x, a)
Definition macros.h:78
void * av_calloc(size_t nmemb, size_t size)
Definition mem.c:264
Memory handling functions.
#define LOCAL_ALIGNED_32(t, v,...)
void(* compute_safe_ssd_integral_image)(uint32_t *dst, ptrdiff_t dst_linesize_32, const uint8_t *s1, ptrdiff_t linesize1, const uint8_t *s2, ptrdiff_t linesize2, int w, int h)
Definition vf_nlmeans.h:26
void(* compute_weights_line)(const uint32_t *const iia, const uint32_t *const iib, const uint32_t *const iid, const uint32_t *const iie, const uint8_t *const src, float *total_weight, float *sum, const float *const weight_lut, ptrdiff_t max_meaningful_diff, ptrdiff_t startx, ptrdiff_t endx)
Definition vf_nlmeans.h:30
#define av_freep(p)
#define randomize_buffer(buf, size)
Definition vf_nlmeans.c:30
void checkasm_check_nlmeans(void)
Definition vf_nlmeans.c:36
#define MAX_MEANINGFUL_DIFF
#define TEST_W
#define src
Definition vp8dsp.c:248
static av_unused void ff_nlmeans_init(NLMeansDSPContext *dsp)
static av_always_inline int diff(const struct color_info *a, const struct color_info *b, const int trans_thresh)
uint8_t base
Definition vp3data.h:128