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
7857Sum of absolute difference of Y, U and V pixel components of current7857Sum of absolute difference of Y, U and V pixel components of current
7858pixel and neighbour pixels lower than this threshold will be used in7858pixel and neighbour pixels lower than this threshold will be used in
7859averaging. Luma component is left unchanged and is copied to output.7859averaging. Luma component is left unchanged and is copied to output.
7860Default value is 30. Allowed range is from 1 to 200.7860Default value is 30. Allowed range is from 1 to 5000.
7861 7861
7862@item sizew7862@item sizew
7863Set horizontal radius of rectangle used for averaging.7863Set horizontal radius of rectangle used for averaging.
@@ -7881,19 +7881,31 @@ Mostly useful to speed-up filtering. · 17 unmodified lines
7881Set Y threshold for averaging chrominance values.7881Set Y threshold for averaging chrominance values.
7882Set finer control for max allowed difference between Y components7882Set finer control for max allowed difference between Y components
7883of current pixel and neigbour pixels.7883of current pixel and neigbour pixels.
7884Default value is 200. Allowed range is from 1 to 200.7884Default value is 5000. Allowed range is from 1 to 5000.
7885 7885
7886@item threu7886@item threu
7887Set U threshold for averaging chrominance values.7887Set U threshold for averaging chrominance values.
7888Set finer control for max allowed difference between U components7888Set finer control for max allowed difference between U components
7889of current pixel and neigbour pixels.7889of current pixel and neigbour pixels.
7890Default value is 200. Allowed range is from 1 to 200.7890Default value is 5000. Allowed range is from 1 to 5000.
7891 7891
7892@item threv7892@item threv
7893Set V threshold for averaging chrominance values.7893Set V threshold for averaging chrominance values.
7894Set finer control for max allowed difference between V components7894Set finer control for max allowed difference between V components
7895of current pixel and neigbour pixels.7895of current pixel and neigbour pixels.
7896Default value is 200. Allowed range is from 1 to 200.7896Default value is 5000. Allowed range is from 1 to 5000.
7897
7898@item distance
7899Set distance type used in calculations.
7900
7901@table @samp
7902@item manhattan
7903Absolute difference.
7904@item euclidean
7905Difference squared.
7906@end table
7907
7908Default distance type is manhattan.
7897@end table7909@end table
7898 7910
7899@subsection Commands7911@subsection Commands
libavfilter/vf_chromanr.c+37−19
@@ -35,6 +35,7 @@ typedef struct ChromaNRContext { · 34 unmodified lines
35 float threshold_y;35 float threshold_y;
36 float threshold_u;36 float threshold_u;
37 float threshold_v;37 float threshold_v;
38 int distance;
38 int thres;39 int thres;
39 int thres_y;40 int thres_y;
40 int thres_u;41 int thres_u;
@@ -79,8 +80,11 @@ static int query_formats(AVFilterContext *ctx) · 38 unmodified lines
79 return ff_set_common_formats(ctx, fmts_list);80 return ff_set_common_formats(ctx, fmts_list);
80}81}
81 82
82#define FILTER_FUNC(name, type) \83#define SQR(x) ((x)*(x))
83static int filter_slice##name(AVFilterContext *ctx, void *arg, int jobnr, int nb_jobs) \84
85#define FILTER_FUNC(distance, name, ctype, type, fun) \
86static int distance ## _slice##name(AVFilterContext *ctx, void *arg, \
87 int jobnr, int nb_jobs) \
84{ \88{ \
85 ChromaNRContext *s = ctx->priv; \89 ChromaNRContext *s = ctx->priv; \
86 AVFrame *in = arg; \90 AVFrame *in = arg; \
@@ -146,13 +150,13 @@ static int filter_slice##name(AVFilterContext *ctx, void *arg, int jobnr, int nb · 59 unmodified lines
146 const type *in_vptr = (const type *)(in->data[2] + yy * in_vlinesize); \150 const type *in_vptr = (const type *)(in->data[2] + yy * in_vlinesize); \
147 \151 \
148 for (int xx = FFMAX(0, x - sizew); xx < FFMIN(x + sizew, w); xx += stepw) { \152 for (int xx = FFMAX(0, x - sizew); xx < FFMIN(x + sizew, w); xx += stepw) { \
149 const int Y = in_yptr[xx * chroma_w]; \153 const ctype Y = in_yptr[xx * chroma_w]; \
150 const int U = in_uptr[xx]; \154 const ctype U = in_uptr[xx]; \
151 const int V = in_vptr[xx]; \155 const ctype V = in_vptr[xx]; \
152 \156 \
153 if (FFABS(cu - U) + FFABS(cv - V) + FFABS(cy - Y) < thres && \157 if (fun(cu - U) + fun(cv - V) + fun(cy - Y) < thres && \
154 FFABS(cu - U) < thres_u && FFABS(cv - V) < thres_v && \158 fun(cu - U) < thres_u && fun(cv - V) < thres_v && \
155 FFABS(cy - Y) < thres_y && \159 fun(cy - Y) < thres_y && \
156 xx != x && yy != y) { \160 xx != x && yy != y) { \
157 su += U; \161 su += U; \
158 sv += V; \162 sv += V; \
@@ -172,8 +176,11 @@ static int filter_slice##name(AVFilterContext *ctx, void *arg, int jobnr, int nb · 13 unmodified lines
172 return 0; \176 return 0; \
173}177}
174 178
175FILTER_FUNC(8, uint8_t)179FILTER_FUNC(manhattan, 8, int, uint8_t, FFABS)
176FILTER_FUNC(16, uint16_t)180FILTER_FUNC(manhattan, 16, int, uint16_t, FFABS)
181
182FILTER_FUNC(euclidean, 8, int, uint8_t, SQR)
183FILTER_FUNC(euclidean, 16, int64_t, uint16_t, SQR)
177 184
178static int filter_frame(AVFilterLink *inlink, AVFrame *in)185static int filter_frame(AVFilterLink *inlink, AVFrame *in)
179{186{
@@ -182,6 +189,15 @@ static int filter_frame(AVFilterLink *inlink, AVFrame *in) · 2 unmodified lines
182 ChromaNRContext *s = ctx->priv;189 ChromaNRContext *s = ctx->priv;
183 AVFrame *out;190 AVFrame *out;
184 191
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
185 s->thres = s->threshold * (1 << (s->depth - 8));201 s->thres = s->threshold * (1 << (s->depth - 8));
186 s->thres_y = s->threshold_y * (1 << (s->depth - 8));202 s->thres_y = s->threshold_y * (1 << (s->depth - 8));
187 s->thres_u = s->threshold_u * (1 << (s->depth - 8));203 s->thres_u = s->threshold_u * (1 << (s->depth - 8));
@@ -213,7 +229,6 @@ static int config_input(AVFilterLink *inlink) · 25 unmodified lines
213 229
214 s->nb_planes = desc->nb_components;230 s->nb_planes = desc->nb_components;
215 s->depth = desc->comp[0].depth;231 s->depth = desc->comp[0].depth;
216 s->filter_slice = s->depth <= 8 ? filter_slice8 : filter_slice16;
217 s->chroma_w = 1 << desc->log2_chroma_w;232 s->chroma_w = 1 << desc->log2_chroma_w;
218 s->chroma_h = 1 << desc->log2_chroma_h;233 s->chroma_h = 1 << desc->log2_chroma_h;
219 s->planeheight[1] = s->planeheight[2] = AV_CEIL_RSHIFT(inlink->h, desc->log2_chroma_h);234 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
231#define VF AV_OPT_FLAG_VIDEO_PARAM | AV_OPT_FLAG_FILTERING_PARAM | AV_OPT_FLAG_RUNTIME_PARAM246#define VF AV_OPT_FLAG_VIDEO_PARAM | AV_OPT_FLAG_FILTERING_PARAM | AV_OPT_FLAG_RUNTIME_PARAM
232 247
233static const AVOption chromanr_options[] = {248static const AVOption chromanr_options[] = {
234 { "thres", "set y+u+v threshold", OFFSET(threshold), AV_OPT_TYPE_FLOAT, {.dbl=30}, 1, 200, VF },249 { "thres", "set y+u+v threshold", OFFSET(threshold), AV_OPT_TYPE_FLOAT, {.dbl=30}, 1, 5000, VF },
235 { "sizew", "set horizontal size", OFFSET(sizew), AV_OPT_TYPE_INT, {.i64=5}, 1, 100, VF },250 { "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 },251 { "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 },252 { "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 },253 { "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 },254 { "threy", "set y threshold", OFFSET(threshold_y), AV_OPT_TYPE_FLOAT, {.dbl=5000},1, 5000, VF },
240 { "threu", "set u threshold", OFFSET(threshold_u), AV_OPT_TYPE_FLOAT, {.dbl=200},1, 200, VF },255 { "threu", "set u threshold", OFFSET(threshold_u), AV_OPT_TYPE_FLOAT, {.dbl=5000},1, 5000, VF },
241 { "threv", "set v threshold", OFFSET(threshold_v), AV_OPT_TYPE_FLOAT, {.dbl=200},1, 200, 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" },
242 { NULL }260 { NULL }
243};261};
244 262

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