| Line | Branch | Exec | Source |
|---|---|---|---|
| 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 "checkasm.h" | ||
| 23 | #include "libavfilter/vf_nlmeans_init.h" | ||
| 24 | #include "libavutil/avassert.h" | ||
| 25 | #include "libavutil/mem.h" | ||
| 26 | #include "libavutil/mem_internal.h" | ||
| 27 | |||
| 28 | #define randomize_buffer(buf, size) do { \ | ||
| 29 | int i; \ | ||
| 30 | for (i = 0; i < size / 4; i++) \ | ||
| 31 | ((uint32_t *)buf)[i] = rnd(); \ | ||
| 32 | } while (0) | ||
| 33 | |||
| 34 | 14 | void checkasm_check_nlmeans(void) | |
| 35 | { | ||
| 36 | 14 | NLMeansDSPContext dsp = {0}; | |
| 37 | |||
| 38 | 14 | const int w = 123; // source width | |
| 39 | 14 | const int h = 45; // source height | |
| 40 | 14 | const int p = 3; // patch half size | |
| 41 | 14 | const int r = 2; // research window half size | |
| 42 | |||
| 43 | 14 | ff_nlmeans_init(&dsp); | |
| 44 | |||
| 45 | /* See the filter's code for the explanations on the variables */ | ||
| 46 |
2/2✓ Branch 3 taken 1 times.
✓ Branch 4 taken 13 times.
|
14 | if (check_func(dsp.compute_safe_ssd_integral_image, "ssd_integral_image")) { |
| 47 | int offx, offy; | ||
| 48 | 1 | const int e = p + r; | |
| 49 | 1 | const int ii_w = w + e*2; | |
| 50 | 1 | const int ii_h = h + e*2; | |
| 51 | 1 | const int ii_lz_32 = FFALIGN(ii_w + 1, 4); | |
| 52 | 1 | uint32_t *ii_orig_ref = av_calloc(ii_h + 1, ii_lz_32 * sizeof(*ii_orig_ref)); | |
| 53 | 1 | uint32_t *ii_ref = ii_orig_ref + ii_lz_32 + 1; | |
| 54 | 1 | uint32_t *ii_orig_new = av_calloc(ii_h + 1, ii_lz_32 * sizeof(*ii_orig_new)); | |
| 55 | 1 | uint32_t *ii_new = ii_orig_new + ii_lz_32 + 1; | |
| 56 | 1 | const int src_lz = FFALIGN(w, 16); | |
| 57 | 1 | uint8_t *src = av_calloc(h, src_lz); | |
| 58 | |||
| 59 | 1 | declare_func(void, uint32_t *dst, ptrdiff_t dst_linesize_32, | |
| 60 | const uint8_t *s1, ptrdiff_t linesize1, | ||
| 61 | const uint8_t *s2, ptrdiff_t linesize2, | ||
| 62 | int w, int h); | ||
| 63 | |||
| 64 |
2/2✓ Branch 1 taken 1440 times.
✓ Branch 2 taken 1 times.
|
1441 | randomize_buffer(src, h * src_lz); |
| 65 | |||
| 66 |
2/2✓ Branch 0 taken 5 times.
✓ Branch 1 taken 1 times.
|
6 | for (offy = -r; offy <= r; offy++) { |
| 67 |
2/2✓ Branch 0 taken 25 times.
✓ Branch 1 taken 5 times.
|
30 | for (offx = -r; offx <= r; offx++) { |
| 68 |
4/4✓ Branch 0 taken 5 times.
✓ Branch 1 taken 20 times.
✓ Branch 2 taken 4 times.
✓ Branch 3 taken 1 times.
|
25 | if (offx || offy) { |
| 69 | 24 | const int s1x = e; | |
| 70 | 24 | const int s1y = e; | |
| 71 | 24 | const int s2x = e + offx; | |
| 72 | 24 | const int s2y = e + offy; | |
| 73 | 24 | const int startx_safe = FFMAX(s1x, s2x); | |
| 74 | 24 | const int starty_safe = FFMAX(s1y, s2y); | |
| 75 |
2/2✓ Branch 0 taken 10 times.
✓ Branch 1 taken 14 times.
|
24 | const int u_endx_safe = FFMIN(s1x + w, s2x + w); |
| 76 |
2/2✓ Branch 0 taken 10 times.
✓ Branch 1 taken 14 times.
|
24 | const int endy_safe = FFMIN(s1y + h, s2y + h); |
| 77 | 24 | const int safe_pw = (u_endx_safe - startx_safe) & ~0xf; | |
| 78 | 24 | const int safe_ph = endy_safe - starty_safe; | |
| 79 | |||
| 80 |
2/4✓ Branch 0 taken 24 times.
✗ Branch 1 not taken.
✗ Branch 2 not taken.
✓ Branch 3 taken 24 times.
|
24 | av_assert0(safe_pw && safe_ph); |
| 81 |
2/4✗ Branch 0 not taken.
✓ Branch 1 taken 24 times.
✗ Branch 4 not taken.
✓ Branch 5 taken 24 times.
|
24 | av_assert0(startx_safe - s1x >= 0); av_assert0(startx_safe - s1x < w); |
| 82 |
2/4✗ Branch 0 not taken.
✓ Branch 1 taken 24 times.
✗ Branch 4 not taken.
✓ Branch 5 taken 24 times.
|
24 | av_assert0(starty_safe - s1y >= 0); av_assert0(starty_safe - s1y < h); |
| 83 |
2/4✗ Branch 0 not taken.
✓ Branch 1 taken 24 times.
✗ Branch 4 not taken.
✓ Branch 5 taken 24 times.
|
24 | av_assert0(startx_safe - s2x >= 0); av_assert0(startx_safe - s2x < w); |
| 84 |
2/4✗ Branch 0 not taken.
✓ Branch 1 taken 24 times.
✗ Branch 4 not taken.
✓ Branch 5 taken 24 times.
|
24 | av_assert0(starty_safe - s2y >= 0); av_assert0(starty_safe - s2y < h); |
| 85 | |||
| 86 | 24 | memset(ii_ref, 0, (ii_lz_32 * ii_h - 1) * sizeof(*ii_ref)); | |
| 87 | 24 | memset(ii_new, 0, (ii_lz_32 * ii_h - 1) * sizeof(*ii_new)); | |
| 88 | |||
| 89 | 24 | call_ref(ii_ref + starty_safe*ii_lz_32 + startx_safe, ii_lz_32, | |
| 90 | src + (starty_safe - s1y) * src_lz + (startx_safe - s1x), src_lz, | ||
| 91 | src + (starty_safe - s2y) * src_lz + (startx_safe - s2x), src_lz, | ||
| 92 | safe_pw, safe_ph); | ||
| 93 | 24 | call_new(ii_new + starty_safe*ii_lz_32 + startx_safe, ii_lz_32, | |
| 94 | src + (starty_safe - s1y) * src_lz + (startx_safe - s1x), src_lz, | ||
| 95 | src + (starty_safe - s2y) * src_lz + (startx_safe - s2x), src_lz, | ||
| 96 | safe_pw, safe_ph); | ||
| 97 | |||
| 98 |
1/2✗ Branch 0 not taken.
✓ Branch 1 taken 24 times.
|
24 | if (memcmp(ii_ref, ii_new, (ii_lz_32 * ii_h - 1) * sizeof(*ii_ref))) |
| 99 | ✗ | fail(); | |
| 100 | |||
| 101 | 24 | memset(ii_new, 0, (ii_lz_32 * ii_h - 1) * sizeof(*ii_new)); | |
| 102 |
1/8✗ Branch 1 not taken.
✓ Branch 2 taken 24 times.
✗ Branch 39 not taken.
✗ Branch 40 not taken.
✗ Branch 41 not taken.
✗ Branch 42 not taken.
✗ Branch 43 not taken.
✗ Branch 44 not taken.
|
24 | bench_new(ii_new + starty_safe*ii_lz_32 + startx_safe, ii_lz_32, |
| 103 | src + (starty_safe - s1y) * src_lz + (startx_safe - s1x), src_lz, | ||
| 104 | src + (starty_safe - s2y) * src_lz + (startx_safe - s2x), src_lz, | ||
| 105 | safe_pw, safe_ph); | ||
| 106 | } | ||
| 107 | } | ||
| 108 | } | ||
| 109 | |||
| 110 | 1 | av_freep(&ii_orig_ref); | |
| 111 | 1 | av_freep(&ii_orig_new); | |
| 112 | 1 | av_freep(&src); | |
| 113 | } | ||
| 114 | |||
| 115 |
2/2✓ Branch 3 taken 2 times.
✓ Branch 4 taken 12 times.
|
14 | if (check_func(dsp.compute_weights_line, "compute_weights_line")) { |
| 116 | #define TEST_W 256 | ||
| 117 | #define MAX_MEANINGFUL_DIFF 255 | ||
| 118 | 2 | const int startx = 10; | |
| 119 | 2 | const int endx = 200; | |
| 120 | |||
| 121 | // Allocate aligned buffers on stack | ||
| 122 | 2 | LOCAL_ALIGNED_32(uint32_t, iia, [TEST_W + 16]); | |
| 123 | 2 | LOCAL_ALIGNED_32(uint32_t, iib, [TEST_W + 16]); | |
| 124 | 2 | LOCAL_ALIGNED_32(uint32_t, iid, [TEST_W + 16]); | |
| 125 | 2 | LOCAL_ALIGNED_32(uint32_t, iie, [TEST_W + 16]); | |
| 126 | 2 | LOCAL_ALIGNED_32(uint8_t, src, [TEST_W + 16]); | |
| 127 | 2 | LOCAL_ALIGNED_32(float, tw_ref, [TEST_W + 16]); | |
| 128 | 2 | LOCAL_ALIGNED_32(float, tw_new, [TEST_W + 16]); | |
| 129 | 2 | LOCAL_ALIGNED_32(float, sum_ref, [TEST_W + 16]); | |
| 130 | 2 | LOCAL_ALIGNED_32(float, sum_new, [TEST_W + 16]); | |
| 131 | 2 | LOCAL_ALIGNED_32(float, lut, [MAX_MEANINGFUL_DIFF + 1]); | |
| 132 | |||
| 133 | 2 | declare_func(void, const uint32_t *const iia, | |
| 134 | const uint32_t *const iib, | ||
| 135 | const uint32_t *const iid, | ||
| 136 | const uint32_t *const iie, | ||
| 137 | const uint8_t *const src, | ||
| 138 | float *total_weight, | ||
| 139 | float *sum, | ||
| 140 | const float *const weight_lut, | ||
| 141 | ptrdiff_t max_meaningful_diff, | ||
| 142 | ptrdiff_t startx, ptrdiff_t endx); | ||
| 143 | |||
| 144 | // Initialize LUT: weight = exp(-diff * scale) | ||
| 145 | // Using scale = 0.01 for testing | ||
| 146 |
2/2✓ Branch 0 taken 512 times.
✓ Branch 1 taken 2 times.
|
514 | for (int i = 0; i <= MAX_MEANINGFUL_DIFF; i++) |
| 147 | 512 | lut[i] = expf(-i * 0.01f); | |
| 148 | |||
| 149 | // Initialize source pixels | ||
| 150 |
2/2✓ Branch 0 taken 512 times.
✓ Branch 1 taken 2 times.
|
514 | for (int i = 0; i < TEST_W; i++) |
| 151 | 512 | src[i] = rnd() & 0xff; | |
| 152 | |||
| 153 | // Initialize integral images | ||
| 154 | // We need to ensure diff = e - d - b + a is non-negative and within range | ||
| 155 | // Set up as if computing real integral image values | ||
| 156 |
2/2✓ Branch 0 taken 512 times.
✓ Branch 1 taken 2 times.
|
514 | for (int i = 0; i < TEST_W; i++) { |
| 157 | 512 | uint32_t base = rnd() % 1000; | |
| 158 | 512 | iia[i] = base; | |
| 159 | 512 | iib[i] = base + (rnd() % 100); | |
| 160 | 512 | iid[i] = base + (rnd() % 100); | |
| 161 | // e = a + (b - a) + (d - a) + diff | ||
| 162 | // So diff = e - d - b + a will be in range [0, max_meaningful_diff] | ||
| 163 | 512 | uint32_t diff = rnd() % (MAX_MEANINGFUL_DIFF + 1); | |
| 164 | 512 | iie[i] = iia[i] + (iib[i] - iia[i]) + (iid[i] - iia[i]) + diff; | |
| 165 | } | ||
| 166 | |||
| 167 | // Clear output buffers | ||
| 168 | 2 | memset(tw_ref, 0, (TEST_W + 16) * sizeof(float)); | |
| 169 | 2 | memset(tw_new, 0, (TEST_W + 16) * sizeof(float)); | |
| 170 | 2 | memset(sum_ref, 0, (TEST_W + 16) * sizeof(float)); | |
| 171 | 2 | memset(sum_new, 0, (TEST_W + 16) * sizeof(float)); | |
| 172 | |||
| 173 | 2 | call_ref(iia, iib, iid, iie, src, tw_ref, sum_ref, lut, | |
| 174 | MAX_MEANINGFUL_DIFF, startx, endx); | ||
| 175 | 2 | call_new(iia, iib, iid, iie, src, tw_new, sum_new, lut, | |
| 176 | MAX_MEANINGFUL_DIFF, startx, endx); | ||
| 177 | |||
| 178 | // Compare results with small tolerance for floating point | ||
| 179 |
1/2✗ Branch 1 not taken.
✓ Branch 2 taken 2 times.
|
2 | if (!float_near_abs_eps_array(tw_ref + startx, tw_new + startx, 1e-5f, endx - startx)) |
| 180 | ✗ | fail(); | |
| 181 |
1/2✗ Branch 1 not taken.
✓ Branch 2 taken 2 times.
|
2 | if (!float_near_abs_eps_array(sum_ref + startx, sum_new + startx, 1e-4f, endx - startx)) |
| 182 | ✗ | fail(); | |
| 183 | |||
| 184 | // Benchmark | ||
| 185 | 2 | memset(tw_new, 0, (TEST_W + 16) * sizeof(float)); | |
| 186 | 2 | memset(sum_new, 0, (TEST_W + 16) * sizeof(float)); | |
| 187 |
1/8✗ Branch 1 not taken.
✓ Branch 2 taken 2 times.
✗ Branch 39 not taken.
✗ Branch 40 not taken.
✗ Branch 41 not taken.
✗ Branch 42 not taken.
✗ Branch 43 not taken.
✗ Branch 44 not taken.
|
2 | bench_new(iia, iib, iid, iie, src, tw_new, sum_new, lut, |
| 188 | MAX_MEANINGFUL_DIFF, startx, endx); | ||
| 189 | } | ||
| 190 | |||
| 191 | 14 | report("dsp"); | |
| 192 | 14 | } | |
| 193 |