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

Showing the changes to libavfilter (2 of 2 files). Show all

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
3838 #include "vf_nlmeans.h"
3939 #include "video.h"
4040
41−struct weighted_avg {
42− float total_weight;
43− float sum;
44−};
45−
4641 typedef struct NLMeansContext {
4742 const AVClass *class;
4843 int nb_planes;
@@ -329,6 +324,58 @@ struct thread_data { · 280 unmodified lines
329324 int p;
330325 };
331326
327+static 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+
332379 static int nlmeans_slice(AVFilterContext *ctx, void *arg, int jobnr, int nb_jobs)
333380 {
334381 NLMeansContext *s = ctx->priv;
@@ -346,50 +393,19 @@ static int nlmeans_slice(AVFilterContext *ctx, void *arg, int jobnr, int nb_jobs · 11 unmodified lines
346393 const int dist_d = dist_b * s->ii_lz_32;
347394 const int dist_e = dist_d + dist_b;
348395 const float *const weight_lut = s->weight_lut;
396+ NLMeansDSPContext *dsp = &s->dsp;
349397
350398 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;
352400 struct weighted_avg *wa = s->wa + y*s->wa_linesize;
353− for (int x = td->startx; x < td->endx; x++) {
354− /*
355− * M is a discrete map where every entry contains the sum of all the entries
356− * in the rectangle from the top-left origin of M to its coordinate. In the
357− * following schema, "i" contains the sum of the whole map:
358− *
359− * M = +----------+-----------------+----+
360− * | | | |
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− }
401+ const uint32_t *const iia = ii;
402+ const uint32_t *const iib = ii + dist_b;
403+ const uint32_t *const iid = ii + dist_d;
404+ const uint32_t *const iie = ii + dist_e;
405+
406+ dsp->compute_weights_line(iia, iib, iid, iie, src, wa,
407+ weight_lut, max_meaningful_diff,
408+ td->startx, td->endx);
393409 ii += s->ii_lz_32;
394410 }
395411 return 0;
@@ -493,6 +509,7 @@ static int filter_frame(AVFilterLink *inlink, AVFrame *in) · 97 unmodified lines
493509 void ff_nlmeans_init(NLMeansDSPContext *dsp)
494510 {
495511 dsp->compute_safe_ssd_integral_image = compute_safe_ssd_integral_image_c;
512+ dsp->compute_weights_line = compute_weights_line_c;
496513
497514 if (ARCH_AARCH64)
498515 ff_nlmeans_init_aarch64(dsp);
libavfilter/vf_nlmeans.h+14−0
@@ -22,11 +22,25 @@ · 21 unmodified lines
2222 #include <stddef.h>
2323 #include <stdint.h>
2424
25+struct weighted_avg {
26+ float total_weight;
27+ float sum;
28+};
29+
2530 typedef struct NLMeansDSPContext {
2631 void (*compute_safe_ssd_integral_image)(uint32_t *dst, ptrdiff_t dst_linesize_32,
2732 const uint8_t *s1, ptrdiff_t linesize1,
2833 const uint8_t *s2, ptrdiff_t linesize2,
2934 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);
3044 } NLMeansDSPContext;
3145
3246 void 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