History

avfilter/vf_chromanr: add also euclidean distance for calculations

Paul B Mahol committed 5 years ago

Commit 075157e1 parent 7f6d2092 files · +53 −23Browse files

Unified · Split

2 files
  1. doc/filters.texi +16 −4
  2. libavfilter/vf_chromanr.c +37 −19
doc/filters.texi+16−4
@@ -7857,7 +7857,7 @@ Set threshold for averaging chrominance values. · 7856 unmodified lines
78577857 Sum of absolute difference of Y, U and V pixel components of current
78587858 pixel and neighbour pixels lower than this threshold will be used in
78597859 averaging. Luma component is left unchanged and is copied to output.
7860−Default value is 30. Allowed range is from 1 to 200.
7860+Default value is 30. Allowed range is from 1 to 5000.
78617861
78627862 @item sizew
78637863 Set horizontal radius of rectangle used for averaging.
@@ -7881,19 +7881,31 @@ Mostly useful to speed-up filtering. · 17 unmodified lines
78817881 Set Y threshold for averaging chrominance values.
78827882 Set finer control for max allowed difference between Y components
78837883 of current pixel and neigbour pixels.
7884−Default value is 200. Allowed range is from 1 to 200.
7884+Default value is 5000. Allowed range is from 1 to 5000.
78857885
78867886 @item threu
78877887 Set U threshold for averaging chrominance values.
78887888 Set finer control for max allowed difference between U components
78897889 of current pixel and neigbour pixels.
7890−Default value is 200. Allowed range is from 1 to 200.
7890+Default value is 5000. Allowed range is from 1 to 5000.
78917891
78927892 @item threv
78937893 Set V threshold for averaging chrominance values.
78947894 Set finer control for max allowed difference between V components
78957895 of current pixel and neigbour pixels.
7896−Default value is 200. Allowed range is from 1 to 200.
7896+Default value is 5000. Allowed range is from 1 to 5000.
7897+
7898+@item distance
7899+Set distance type used in calculations.
7900+
7901+@table @samp
7902+@item manhattan
7903+Absolute difference.
7904+@item euclidean
7905+Difference squared.
7906+@end table
7907+
7908+Default distance type is manhattan.
78977909 @end table
78987910
78997911 @subsection Commands
libavfilter/vf_chromanr.c+37−19
@@ -35,6 +35,7 @@ typedef struct ChromaNRContext { · 34 unmodified lines
3535 float threshold_y;
3636 float threshold_u;
3737 float threshold_v;
38+ int distance;
3839 int thres;
3940 int thres_y;
4041 int thres_u;
@@ -79,8 +80,11 @@ static int query_formats(AVFilterContext *ctx) · 38 unmodified lines
7980 return ff_set_common_formats(ctx, fmts_list);
8081 }
8182
82−#define FILTER_FUNC(name, type) \
83−static int filter_slice##name(AVFilterContext *ctx, void *arg, int jobnr, int nb_jobs) \
83+#define SQR(x) ((x)*(x))
84+
85+#define FILTER_FUNC(distance, name, ctype, type, fun) \
86+static int distance ## _slice##name(AVFilterContext *ctx, void *arg, \
87+ int jobnr, int nb_jobs) \
8488 { \
8589 ChromaNRContext *s = ctx->priv; \
8690 AVFrame *in = arg; \
@@ -146,13 +150,13 @@ static int filter_slice##name(AVFilterContext *ctx, void *arg, int jobnr, int nb · 59 unmodified lines
146150 const type *in_vptr = (const type *)(in->data[2] + yy * in_vlinesize); \
147151 \
148152 for (int xx = FFMAX(0, x - sizew); xx < FFMIN(x + sizew, w); xx += stepw) { \
149− const int Y = in_yptr[xx * chroma_w]; \
150− const int U = in_uptr[xx]; \
151− const int V = in_vptr[xx]; \
153+ const ctype Y = in_yptr[xx * chroma_w]; \
154+ const ctype U = in_uptr[xx]; \
155+ const ctype V = in_vptr[xx]; \
152156 \
153− if (FFABS(cu - U) + FFABS(cv - V) + FFABS(cy - Y) < thres && \
154− FFABS(cu - U) < thres_u && FFABS(cv - V) < thres_v && \
155− FFABS(cy - Y) < thres_y && \
157+ if (fun(cu - U) + fun(cv - V) + fun(cy - Y) < thres && \
158+ fun(cu - U) < thres_u && fun(cv - V) < thres_v && \
159+ fun(cy - Y) < thres_y && \
156160 xx != x && yy != y) { \
157161 su += U; \
158162 sv += V; \
@@ -172,8 +176,11 @@ static int filter_slice##name(AVFilterContext *ctx, void *arg, int jobnr, int nb · 13 unmodified lines
172176 return 0; \
173177 }
174178
175−FILTER_FUNC(8, uint8_t)
176−FILTER_FUNC(16, uint16_t)
179+FILTER_FUNC(manhattan, 8, int, uint8_t, FFABS)
180+FILTER_FUNC(manhattan, 16, int, uint16_t, FFABS)
181+
182+FILTER_FUNC(euclidean, 8, int, uint8_t, SQR)
183+FILTER_FUNC(euclidean, 16, int64_t, uint16_t, SQR)
177184
178185 static int filter_frame(AVFilterLink *inlink, AVFrame *in)
179186 {
@@ -182,6 +189,15 @@ static int filter_frame(AVFilterLink *inlink, AVFrame *in) · 2 unmodified lines
182189 ChromaNRContext *s = ctx->priv;
183190 AVFrame *out;
184191
192+ switch (s->distance) {
193+ case 0:
194+ s->filter_slice = s->depth <= 8 ? manhattan_slice8 : manhattan_slice16;
195+ break;
196+ case 1:
197+ s->filter_slice = s->depth <= 8 ? euclidean_slice8 : euclidean_slice16;
198+ break;
199+ }
200+
185201 s->thres = s->threshold * (1 << (s->depth - 8));
186202 s->thres_y = s->threshold_y * (1 << (s->depth - 8));
187203 s->thres_u = s->threshold_u * (1 << (s->depth - 8));
@@ -213,7 +229,6 @@ static int config_input(AVFilterLink *inlink) · 25 unmodified lines
213229
214230 s->nb_planes = desc->nb_components;
215231 s->depth = desc->comp[0].depth;
216− s->filter_slice = s->depth <= 8 ? filter_slice8 : filter_slice16;
217232 s->chroma_w = 1 << desc->log2_chroma_w;
218233 s->chroma_h = 1 << desc->log2_chroma_h;
219234 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) · 11 unmodified lines
231246 #define VF AV_OPT_FLAG_VIDEO_PARAM | AV_OPT_FLAG_FILTERING_PARAM | AV_OPT_FLAG_RUNTIME_PARAM
232247
233248 static const AVOption chromanr_options[] = {
234− { "thres", "set y+u+v threshold", OFFSET(threshold), AV_OPT_TYPE_FLOAT, {.dbl=30}, 1, 200, VF },
235− { "sizew", "set horizontal size", OFFSET(sizew), AV_OPT_TYPE_INT, {.i64=5}, 1, 100, VF },
236− { "sizeh", "set vertical size", OFFSET(sizeh), AV_OPT_TYPE_INT, {.i64=5}, 1, 100, VF },
237− { "stepw", "set horizontal step", OFFSET(stepw), AV_OPT_TYPE_INT, {.i64=1}, 1, 50, VF },
238− { "steph", "set vertical step", OFFSET(steph), AV_OPT_TYPE_INT, {.i64=1}, 1, 50, VF },
239− { "threy", "set y threshold", OFFSET(threshold_y), AV_OPT_TYPE_FLOAT, {.dbl=200},1, 200, VF },
240− { "threu", "set u threshold", OFFSET(threshold_u), AV_OPT_TYPE_FLOAT, {.dbl=200},1, 200, VF },
241− { "threv", "set v threshold", OFFSET(threshold_v), AV_OPT_TYPE_FLOAT, {.dbl=200},1, 200, VF },
249+ { "thres", "set y+u+v threshold", OFFSET(threshold), AV_OPT_TYPE_FLOAT, {.dbl=30}, 1, 5000, VF },
250+ { "sizew", "set horizontal size", OFFSET(sizew), AV_OPT_TYPE_INT, {.i64=5}, 1, 100, VF },
251+ { "sizeh", "set vertical size", OFFSET(sizeh), AV_OPT_TYPE_INT, {.i64=5}, 1, 100, VF },
252+ { "stepw", "set horizontal step", OFFSET(stepw), AV_OPT_TYPE_INT, {.i64=1}, 1, 50, VF },
253+ { "steph", "set vertical step", OFFSET(steph), AV_OPT_TYPE_INT, {.i64=1}, 1, 50, VF },
254+ { "threy", "set y threshold", OFFSET(threshold_y), AV_OPT_TYPE_FLOAT, {.dbl=5000},1, 5000, VF },
255+ { "threu", "set u threshold", OFFSET(threshold_u), AV_OPT_TYPE_FLOAT, {.dbl=5000},1, 5000, VF },
256+ { "threv", "set v threshold", OFFSET(threshold_v), AV_OPT_TYPE_FLOAT, {.dbl=5000},1, 5000, VF },
257+ { "distance", "set distance type", OFFSET(distance), AV_OPT_TYPE_INT, {.i64=0}, 0, 1, VF, "distance" },
258+ { "manhattan", "", 0, AV_OPT_TYPE_CONST, {.i64=0}, 0, 0, VF, "distance" },
259+ { "euclidean", "", 0, AV_OPT_TYPE_CONST, {.i64=1}, 0, 0, VF, "distance" },
242260 { NULL }
243261 };
244262

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