[FFmpeg-cvslog] avfilter/vf_chromanr: add also euclidean distance for calculations
Paul B Mahol
git at videolan.org
Sat Jul 17 16:24:24 EEST 2021
ffmpeg | branch: master | Paul B Mahol <onemda at gmail.com> | Sat Jul 17 15:18:30 2021 +0200| [075157ec867e0bf8d500bbbd23fee86f37eff45a] | committer: Paul B Mahol
avfilter/vf_chromanr: add also euclidean distance for calculations
> http://git.videolan.org/gitweb.cgi/ffmpeg.git/?a=commit;h=075157ec867e0bf8d500bbbd23fee86f37eff45a
---
doc/filters.texi | 20 +++++++++++++----
libavfilter/vf_chromanr.c | 56 +++++++++++++++++++++++++++++++----------------
2 files changed, 53 insertions(+), 23 deletions(-)
diff --git a/doc/filters.texi b/doc/filters.texi
index d991c06628..232d81ae3e 100644
--- a/doc/filters.texi
+++ b/doc/filters.texi
@@ -7857,7 +7857,7 @@ Set threshold for averaging chrominance values.
Sum of absolute difference of Y, U and V pixel components of current
pixel and neighbour pixels lower than this threshold will be used in
averaging. Luma component is left unchanged and is copied to output.
-Default value is 30. Allowed range is from 1 to 200.
+Default value is 30. Allowed range is from 1 to 5000.
@item sizew
Set horizontal radius of rectangle used for averaging.
@@ -7881,19 +7881,31 @@ Mostly useful to speed-up filtering.
Set Y threshold for averaging chrominance values.
Set finer control for max allowed difference between Y components
of current pixel and neigbour pixels.
-Default value is 200. Allowed range is from 1 to 200.
+Default value is 5000. Allowed range is from 1 to 5000.
@item threu
Set U threshold for averaging chrominance values.
Set finer control for max allowed difference between U components
of current pixel and neigbour pixels.
-Default value is 200. Allowed range is from 1 to 200.
+Default value is 5000. Allowed range is from 1 to 5000.
@item threv
Set V threshold for averaging chrominance values.
Set finer control for max allowed difference between V components
of current pixel and neigbour pixels.
-Default value is 200. Allowed range is from 1 to 200.
+Default value is 5000. Allowed range is from 1 to 5000.
+
+ at item distance
+Set distance type used in calculations.
+
+ at table @samp
+ at item manhattan
+Absolute difference.
+ at item euclidean
+Difference squared.
+ at end table
+
+Default distance type is manhattan.
@end table
@subsection Commands
diff --git a/libavfilter/vf_chromanr.c b/libavfilter/vf_chromanr.c
index 66ef1587b6..810fd2ce9d 100644
--- a/libavfilter/vf_chromanr.c
+++ b/libavfilter/vf_chromanr.c
@@ -35,6 +35,7 @@ typedef struct ChromaNRContext {
float threshold_y;
float threshold_u;
float threshold_v;
+ int distance;
int thres;
int thres_y;
int thres_u;
@@ -79,8 +80,11 @@ static int query_formats(AVFilterContext *ctx)
return ff_set_common_formats(ctx, fmts_list);
}
-#define FILTER_FUNC(name, type) \
-static int filter_slice##name(AVFilterContext *ctx, void *arg, int jobnr, int nb_jobs) \
+#define SQR(x) ((x)*(x))
+
+#define FILTER_FUNC(distance, name, ctype, type, fun) \
+static int distance ## _slice##name(AVFilterContext *ctx, void *arg, \
+ int jobnr, int nb_jobs) \
{ \
ChromaNRContext *s = ctx->priv; \
AVFrame *in = arg; \
@@ -146,13 +150,13 @@ static int filter_slice##name(AVFilterContext *ctx, void *arg, int jobnr, int nb
const type *in_vptr = (const type *)(in->data[2] + yy * in_vlinesize); \
\
for (int xx = FFMAX(0, x - sizew); xx < FFMIN(x + sizew, w); xx += stepw) { \
- const int Y = in_yptr[xx * chroma_w]; \
- const int U = in_uptr[xx]; \
- const int V = in_vptr[xx]; \
+ const ctype Y = in_yptr[xx * chroma_w]; \
+ const ctype U = in_uptr[xx]; \
+ const ctype V = in_vptr[xx]; \
\
- if (FFABS(cu - U) + FFABS(cv - V) + FFABS(cy - Y) < thres && \
- FFABS(cu - U) < thres_u && FFABS(cv - V) < thres_v && \
- FFABS(cy - Y) < thres_y && \
+ if (fun(cu - U) + fun(cv - V) + fun(cy - Y) < thres && \
+ fun(cu - U) < thres_u && fun(cv - V) < thres_v && \
+ fun(cy - Y) < thres_y && \
xx != x && yy != y) { \
su += U; \
sv += V; \
@@ -172,8 +176,11 @@ static int filter_slice##name(AVFilterContext *ctx, void *arg, int jobnr, int nb
return 0; \
}
-FILTER_FUNC(8, uint8_t)
-FILTER_FUNC(16, uint16_t)
+FILTER_FUNC(manhattan, 8, int, uint8_t, FFABS)
+FILTER_FUNC(manhattan, 16, int, uint16_t, FFABS)
+
+FILTER_FUNC(euclidean, 8, int, uint8_t, SQR)
+FILTER_FUNC(euclidean, 16, int64_t, uint16_t, SQR)
static int filter_frame(AVFilterLink *inlink, AVFrame *in)
{
@@ -182,6 +189,15 @@ static int filter_frame(AVFilterLink *inlink, AVFrame *in)
ChromaNRContext *s = ctx->priv;
AVFrame *out;
+ switch (s->distance) {
+ case 0:
+ s->filter_slice = s->depth <= 8 ? manhattan_slice8 : manhattan_slice16;
+ break;
+ case 1:
+ s->filter_slice = s->depth <= 8 ? euclidean_slice8 : euclidean_slice16;
+ break;
+ }
+
s->thres = s->threshold * (1 << (s->depth - 8));
s->thres_y = s->threshold_y * (1 << (s->depth - 8));
s->thres_u = s->threshold_u * (1 << (s->depth - 8));
@@ -213,7 +229,6 @@ static int config_input(AVFilterLink *inlink)
s->nb_planes = desc->nb_components;
s->depth = desc->comp[0].depth;
- s->filter_slice = s->depth <= 8 ? filter_slice8 : filter_slice16;
s->chroma_w = 1 << desc->log2_chroma_w;
s->chroma_h = 1 << desc->log2_chroma_h;
s->planeheight[1] = s->planeheight[2] = AV_CEIL_RSHIFT(inlink->h, desc->log2_chroma_h);
@@ -231,14 +246,17 @@ static int config_input(AVFilterLink *inlink)
#define VF AV_OPT_FLAG_VIDEO_PARAM | AV_OPT_FLAG_FILTERING_PARAM | AV_OPT_FLAG_RUNTIME_PARAM
static const AVOption chromanr_options[] = {
- { "thres", "set y+u+v threshold", OFFSET(threshold), AV_OPT_TYPE_FLOAT, {.dbl=30}, 1, 200, VF },
- { "sizew", "set horizontal size", OFFSET(sizew), AV_OPT_TYPE_INT, {.i64=5}, 1, 100, VF },
- { "sizeh", "set vertical size", OFFSET(sizeh), AV_OPT_TYPE_INT, {.i64=5}, 1, 100, VF },
- { "stepw", "set horizontal step", OFFSET(stepw), AV_OPT_TYPE_INT, {.i64=1}, 1, 50, VF },
- { "steph", "set vertical step", OFFSET(steph), AV_OPT_TYPE_INT, {.i64=1}, 1, 50, VF },
- { "threy", "set y threshold", OFFSET(threshold_y), AV_OPT_TYPE_FLOAT, {.dbl=200},1, 200, VF },
- { "threu", "set u threshold", OFFSET(threshold_u), AV_OPT_TYPE_FLOAT, {.dbl=200},1, 200, VF },
- { "threv", "set v threshold", OFFSET(threshold_v), AV_OPT_TYPE_FLOAT, {.dbl=200},1, 200, VF },
+ { "thres", "set y+u+v threshold", OFFSET(threshold), AV_OPT_TYPE_FLOAT, {.dbl=30}, 1, 5000, VF },
+ { "sizew", "set horizontal size", OFFSET(sizew), AV_OPT_TYPE_INT, {.i64=5}, 1, 100, VF },
+ { "sizeh", "set vertical size", OFFSET(sizeh), AV_OPT_TYPE_INT, {.i64=5}, 1, 100, VF },
+ { "stepw", "set horizontal step", OFFSET(stepw), AV_OPT_TYPE_INT, {.i64=1}, 1, 50, VF },
+ { "steph", "set vertical step", OFFSET(steph), AV_OPT_TYPE_INT, {.i64=1}, 1, 50, VF },
+ { "threy", "set y threshold", OFFSET(threshold_y), AV_OPT_TYPE_FLOAT, {.dbl=5000},1, 5000, VF },
+ { "threu", "set u threshold", OFFSET(threshold_u), AV_OPT_TYPE_FLOAT, {.dbl=5000},1, 5000, VF },
+ { "threv", "set v threshold", OFFSET(threshold_v), AV_OPT_TYPE_FLOAT, {.dbl=5000},1, 5000, VF },
+ { "distance", "set distance type", OFFSET(distance), AV_OPT_TYPE_INT, {.i64=0}, 0, 1, VF, "distance" },
+ { "manhattan", "", 0, AV_OPT_TYPE_CONST, {.i64=0}, 0, 0, VF, "distance" },
+ { "euclidean", "", 0, AV_OPT_TYPE_CONST, {.i64=1}, 0, 0, VF, "distance" },
{ NULL }
};
More information about the ffmpeg-cvslog
mailing list