FFmpeg coverage


Directory: ../../../ffmpeg/
File: src/libavfilter/vf_nlmeans_init.h
Date: 2024-04-27 00:58:15
Exec Total Coverage
Lines: 26 37 70.3%
Functions: 2 3 66.7%
Branches: 4 6 66.7%

Line Branch Exec Source
1 /*
2 * Copyright (c) 2016 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 #ifndef AVFILTER_NLMEANS_INIT_H
22 #define AVFILTER_NLMEANS_INIT_H
23
24 #include <stddef.h>
25 #include <stdint.h>
26
27 #include "config.h"
28 #include "libavutil/avassert.h"
29 #include "libavutil/macros.h"
30 #include "vf_nlmeans.h"
31
32 /**
33 * Compute squared difference of the safe area (the zone where s1 and s2
34 * overlap). It is likely the largest integral zone, so it is interesting to do
35 * as little checks as possible; contrary to the unsafe version of this
36 * function, we do not need any clipping here.
37 *
38 * The line above dst and the column to its left are always readable.
39 */
40 48 static void compute_safe_ssd_integral_image_c(uint32_t *dst, ptrdiff_t dst_linesize_32,
41 const uint8_t *s1, ptrdiff_t linesize1,
42 const uint8_t *s2, ptrdiff_t linesize2,
43 int w, int h)
44 {
45 48 const uint32_t *dst_top = dst - dst_linesize_32;
46
47 /* SIMD-friendly assumptions allowed here */
48 av_assert2(!(w & 0xf) && w >= 16 && h >= 1);
49
50
2/2
✓ Branch 0 taken 2100 times.
✓ Branch 1 taken 48 times.
2148 for (int y = 0; y < h; y++) {
51
2/2
✓ Branch 0 taken 58800 times.
✓ Branch 1 taken 2100 times.
60900 for (int x = 0; x < w; x += 4) {
52 58800 const int d0 = s1[x ] - s2[x ];
53 58800 const int d1 = s1[x + 1] - s2[x + 1];
54 58800 const int d2 = s1[x + 2] - s2[x + 2];
55 58800 const int d3 = s1[x + 3] - s2[x + 3];
56
57 58800 dst[x ] = dst_top[x ] - dst_top[x - 1] + d0*d0;
58 58800 dst[x + 1] = dst_top[x + 1] - dst_top[x ] + d1*d1;
59 58800 dst[x + 2] = dst_top[x + 2] - dst_top[x + 1] + d2*d2;
60 58800 dst[x + 3] = dst_top[x + 3] - dst_top[x + 2] + d3*d3;
61
62 58800 dst[x ] += dst[x - 1];
63 58800 dst[x + 1] += dst[x ];
64 58800 dst[x + 2] += dst[x + 1];
65 58800 dst[x + 3] += dst[x + 2];
66 }
67 2100 s1 += linesize1;
68 2100 s2 += linesize2;
69 2100 dst += dst_linesize_32;
70 2100 dst_top += dst_linesize_32;
71 }
72 48 }
73
74 static void compute_weights_line_c(const uint32_t *const iia,
75 const uint32_t *const iib,
76 const uint32_t *const iid,
77 const uint32_t *const iie,
78 const uint8_t *const src,
79 float *total_weight,
80 float *sum,
81 const float *const weight_lut,
82 int max_meaningful_diff,
83 int startx, int endx)
84 {
85 for (int x = startx; x < endx; x++) {
86 /*
87 * M is a discrete map where every entry contains the sum of all the entries
88 * in the rectangle from the top-left origin of M to its coordinate. In the
89 * following schema, "i" contains the sum of the whole map:
90 *
91 * M = +----------+-----------------+----+
92 * | | | |
93 * | | | |
94 * | a| b| c|
95 * +----------+-----------------+----+
96 * | | | |
97 * | | | |
98 * | | X | |
99 * | | | |
100 * | d| e| f|
101 * +----------+-----------------+----+
102 * | | | |
103 * | g| h| i|
104 * +----------+-----------------+----+
105 *
106 * The sum of the X box can be calculated with:
107 * X = e-d-b+a
108 *
109 * See https://en.wikipedia.org/wiki/Summed_area_table
110 *
111 * The compute*_ssd functions compute the integral image M where every entry
112 * contains the sum of the squared difference of every corresponding pixels of
113 * two input planes of the same size as M.
114 */
115 const uint32_t a = iia[x];
116 const uint32_t b = iib[x];
117 const uint32_t d = iid[x];
118 const uint32_t e = iie[x];
119 const uint32_t patch_diff_sq = FFMIN(e - d - b + a, max_meaningful_diff);
120 const float weight = weight_lut[patch_diff_sq]; // exp(-patch_diff_sq * s->pdiff_scale)
121
122 total_weight[x] += weight;
123 sum[x] += weight * src[x];
124 }
125 }
126
127 13 static av_unused void ff_nlmeans_init(NLMeansDSPContext *dsp)
128 {
129 13 dsp->compute_safe_ssd_integral_image = compute_safe_ssd_integral_image_c;
130 13 dsp->compute_weights_line = compute_weights_line_c;
131
132 #if ARCH_AARCH64
133 ff_nlmeans_init_aarch64(dsp);
134 #elif ARCH_X86
135 13 ff_nlmeans_init_x86(dsp);
136 #endif
137 13 }
138
139 #endif /* AVFILTER_NLMEANS_INIT_H */
140