History

avfilter/vf_nlmeans: refactor line processing in preparation for x86 SIMD assembly

Paul B Mahol committed 5 years ago

Commit eaca36d1 parent 38c86e72 files · +77 −46Browse files

Unified · Split

2 files
  1. libavfilter/vf_nlmeans.c +63 −46
  2. libavfilter/vf_nlmeans.h +14 −0
libavfilter/vf_nlmeans.c+63−46
@@ -38,11 +38,6 @@ · 37 unmodified lines
38#include "vf_nlmeans.h"38#include "vf_nlmeans.h"
39#include "video.h"39#include "video.h"
40 40
41struct weighted_avg {
42 float total_weight;
43 float sum;
44};
45
46typedef struct NLMeansContext {41typedef struct NLMeansContext {
47 const AVClass *class;42 const AVClass *class;
48 int nb_planes;43 int nb_planes;
@@ -329,6 +324,58 @@ struct thread_data { · 280 unmodified lines
329 int p;324 int p;
330};325};
331 326
327static void compute_weights_line_c(const uint32_t *const iia,
328 const uint32_t *const iib,
329 const uint32_t *const iid,
330 const uint32_t *const iie,
331 const uint8_t *const src,
332 struct weighted_avg *wa,
333 const float *const weight_lut,
334 int max_meaningful_diff,
335 int startx, int endx)
336{
337 for (int x = startx; x < endx; x++) {
338 /*
339 * M is a discrete map where every entry contains the sum of all the entries
340 * in the rectangle from the top-left origin of M to its coordinate. In the
341 * following schema, "i" contains the sum of the whole map:
342 *
343 * M = +----------+-----------------+----+
344 * | | | |
345 * | | | |
346 * | a| b| c|
347 * +----------+-----------------+----+
348 * | | | |
349 * | | | |
350 * | | X | |
351 * | | | |
352 * | d| e| f|
353 * +----------+-----------------+----+
354 * | | | |
355 * | g| h| i|
356 * +----------+-----------------+----+
357 *
358 * The sum of the X box can be calculated with:
359 * X = e-d-b+a
360 *
361 * See https://en.wikipedia.org/wiki/Summed_area_table
362 *
363 * The compute*_ssd functions compute the integral image M where every entry
364 * contains the sum of the squared difference of every corresponding pixels of
365 * two input planes of the same size as M.
366 */
367 const uint32_t a = iia[x];
368 const uint32_t b = iib[x];
369 const uint32_t d = iid[x];
370 const uint32_t e = iie[x];
371 const uint32_t patch_diff_sq = FFMIN(e - d - b + a, max_meaningful_diff);
372 const float weight = weight_lut[patch_diff_sq]; // exp(-patch_diff_sq * s->pdiff_scale)
373
374 wa[x].total_weight += weight;
375 wa[x].sum += weight * src[x];
376 }
377}
378
332static int nlmeans_slice(AVFilterContext *ctx, void *arg, int jobnr, int nb_jobs)379static int nlmeans_slice(AVFilterContext *ctx, void *arg, int jobnr, int nb_jobs)
333{380{
334 NLMeansContext *s = ctx->priv;381 NLMeansContext *s = ctx->priv;
@@ -346,50 +393,19 @@ static int nlmeans_slice(AVFilterContext *ctx, void *arg, int jobnr, int nb_jobs · 11 unmodified lines
346 const int dist_d = dist_b * s->ii_lz_32;393 const int dist_d = dist_b * s->ii_lz_32;
347 const int dist_e = dist_d + dist_b;394 const int dist_e = dist_d + dist_b;
348 const float *const weight_lut = s->weight_lut;395 const float *const weight_lut = s->weight_lut;
396 NLMeansDSPContext *dsp = &s->dsp;
349 397
350 for (int y = starty; y < endy; y++) {398 for (int y = starty; y < endy; y++) {
351 const uint8_t *src = td->src + y*src_linesize;399 const uint8_t *const src = td->src + y*src_linesize;
352 struct weighted_avg *wa = s->wa + y*s->wa_linesize;400 struct weighted_avg *wa = s->wa + y*s->wa_linesize;
353 for (int x = td->startx; x < td->endx; x++) {401 const uint32_t *const iia = ii;
354 /*402 const uint32_t *const iib = ii + dist_b;
355 * M is a discrete map where every entry contains the sum of all the entries403 const uint32_t *const iid = ii + dist_d;
356 * in the rectangle from the top-left origin of M to its coordinate. In the404 const uint32_t *const iie = ii + dist_e;
357 * following schema, "i" contains the sum of the whole map:405
358 *406 dsp->compute_weights_line(iia, iib, iid, iie, src, wa,
359 * M = +----------+-----------------+----+407 weight_lut, max_meaningful_diff,
360 * | | | |408 td->startx, td->endx);
361 * | | | |
362 * | a| b| c|
363 * +----------+-----------------+----+
364 * | | | |
365 * | | | |
366 * | | X | |
367 * | | | |
368 * | d| e| f|
369 * +----------+-----------------+----+
370 * | | | |
371 * | g| h| i|
372 * +----------+-----------------+----+
373 *
374 * The sum of the X box can be calculated with:
375 * X = e-d-b+a
376 *
377 * See https://en.wikipedia.org/wiki/Summed_area_table
378 *
379 * The compute*_ssd functions compute the integral image M where every entry
380 * contains the sum of the squared difference of every corresponding pixels of
381 * two input planes of the same size as M.
382 */
383 const uint32_t a = ii[x];
384 const uint32_t b = ii[x + dist_b];
385 const uint32_t d = ii[x + dist_d];
386 const uint32_t e = ii[x + dist_e];
387 const uint32_t patch_diff_sq = FFMIN(e - d - b + a, max_meaningful_diff);
388 const float weight = weight_lut[patch_diff_sq]; // exp(-patch_diff_sq * s->pdiff_scale)
389
390 wa[x].total_weight += weight;
391 wa[x].sum += weight * src[x];
392 }
393 ii += s->ii_lz_32;409 ii += s->ii_lz_32;
394 }410 }
395 return 0;411 return 0;
@@ -493,6 +509,7 @@ static int filter_frame(AVFilterLink *inlink, AVFrame *in) · 97 unmodified lines
493void ff_nlmeans_init(NLMeansDSPContext *dsp)509void ff_nlmeans_init(NLMeansDSPContext *dsp)
494{510{
495 dsp->compute_safe_ssd_integral_image = compute_safe_ssd_integral_image_c;511 dsp->compute_safe_ssd_integral_image = compute_safe_ssd_integral_image_c;
512 dsp->compute_weights_line = compute_weights_line_c;
496 513
497 if (ARCH_AARCH64)514 if (ARCH_AARCH64)
498 ff_nlmeans_init_aarch64(dsp);515 ff_nlmeans_init_aarch64(dsp);
libavfilter/vf_nlmeans.h+14−0
@@ -22,11 +22,25 @@ · 21 unmodified lines
22#include <stddef.h>22#include <stddef.h>
23#include <stdint.h>23#include <stdint.h>
24 24
25struct weighted_avg {
26 float total_weight;
27 float sum;
28};
29
25typedef struct NLMeansDSPContext {30typedef struct NLMeansDSPContext {
26 void (*compute_safe_ssd_integral_image)(uint32_t *dst, ptrdiff_t dst_linesize_32,31 void (*compute_safe_ssd_integral_image)(uint32_t *dst, ptrdiff_t dst_linesize_32,
27 const uint8_t *s1, ptrdiff_t linesize1,32 const uint8_t *s1, ptrdiff_t linesize1,
28 const uint8_t *s2, ptrdiff_t linesize2,33 const uint8_t *s2, ptrdiff_t linesize2,
29 int w, int h);34 int w, int h);
35 void (*compute_weights_line)(const uint32_t *const iia,
36 const uint32_t *const iib,
37 const uint32_t *const iid,
38 const uint32_t *const iie,
39 const uint8_t *const src,
40 struct weighted_avg *wa,
41 const float *const weight_lut,
42 int max_meaningful_diff,
43 int startx, int endx);
30} NLMeansDSPContext;44} NLMeansDSPContext;
31 45
32void ff_nlmeans_init(NLMeansDSPContext *dsp);46void ff_nlmeans_init(NLMeansDSPContext *dsp);

Keyboard shortcuts

General

Show keyboard shortcuts
?
Focus the current search
/

Issues and pull requests

Open filters
f

Issues, pull requests and Inbox

Select next item
j↓
Select previous item
k↑
Open selected item
Enter
Dismiss filters or clear selection
Esc

Repository code

Go to file
t