From 463c71b3b35e121bdc23541a9f9f40af2436d773 Mon Sep 17 00:00:00 2001 From: Paul B Mahol Date: Fri, 10 Sep 2021 17:52:23 +0200 Subject: [PATCH] avfilter/vf_convolution: add scharr operator --- Changelog | 1 + doc/filters.texi | 21 ++++++++++ libavfilter/Makefile | 1 + libavfilter/allfilters.c | 1 + libavfilter/version.h | 4 +- libavfilter/vf_convolution.c | 79 +++++++++++++++++++++++++++++++++++- 6 files changed, 104 insertions(+), 3 deletions(-) diff --git a/Changelog b/Changelog index df24c69a69..06cb2b2190 100644 --- a/Changelog +++ b/Changelog @@ -18,6 +18,7 @@ version : - AV1 Low overhead bitstream format muxer - swscale slice threading - MSN Siren decoder +- scharr video filter version 4.4: diff --git a/doc/filters.texi b/doc/filters.texi index 9ad6031d23..94804ca32b 100644 --- a/doc/filters.texi +++ b/doc/filters.texi @@ -18349,6 +18349,27 @@ If the specified expression is not valid, it is kept at its current value. @end table +@section scharr +Apply scharr operator to input video stream. + +The filter accepts the following option: + +@table @option +@item planes +Set which planes will be processed, unprocessed planes will be copied. +By default value 0xf, all planes will be processed. + +@item scale +Set value which will be multiplied with filtered result. + +@item delta +Set value which will be added to filtered result. +@end table + +@subsection Commands + +This filter supports the all above options as @ref{commands}. + @section scroll Scroll input video horizontally and/or vertically by constant speed. diff --git a/libavfilter/Makefile b/libavfilter/Makefile index af957a5ac0..9d71aa6b3c 100644 --- a/libavfilter/Makefile +++ b/libavfilter/Makefile @@ -409,6 +409,7 @@ OBJS-$(CONFIG_SCALE_VAAPI_FILTER) += vf_scale_vaapi.o scale_eval.o va OBJS-$(CONFIG_SCALE_VULKAN_FILTER) += vf_scale_vulkan.o vulkan.o OBJS-$(CONFIG_SCALE2REF_FILTER) += vf_scale.o scale_eval.o OBJS-$(CONFIG_SCDET_FILTER) += vf_scdet.o +OBJS-$(CONFIG_SCHARR_FILTER) += vf_convolution.o OBJS-$(CONFIG_SCROLL_FILTER) += vf_scroll.o OBJS-$(CONFIG_SEGMENT_FILTER) += f_segment.o OBJS-$(CONFIG_SELECT_FILTER) += f_select.o diff --git a/libavfilter/allfilters.c b/libavfilter/allfilters.c index 0c6b2347c8..9313a0674b 100644 --- a/libavfilter/allfilters.c +++ b/libavfilter/allfilters.c @@ -390,6 +390,7 @@ extern const AVFilter ff_vf_scale_vaapi; extern const AVFilter ff_vf_scale_vulkan; extern const AVFilter ff_vf_scale2ref; extern const AVFilter ff_vf_scdet; +extern const AVFilter ff_vf_scharr; extern const AVFilter ff_vf_scroll; extern const AVFilter ff_vf_segment; extern const AVFilter ff_vf_select; diff --git a/libavfilter/version.h b/libavfilter/version.h index 2110048b77..306bb62ff4 100644 --- a/libavfilter/version.h +++ b/libavfilter/version.h @@ -30,8 +30,8 @@ #include "libavutil/version.h" #define LIBAVFILTER_VERSION_MAJOR 8 -#define LIBAVFILTER_VERSION_MINOR 7 -#define LIBAVFILTER_VERSION_MICRO 101 +#define LIBAVFILTER_VERSION_MINOR 8 +#define LIBAVFILTER_VERSION_MICRO 100 #define LIBAVFILTER_VERSION_INT AV_VERSION_INT(LIBAVFILTER_VERSION_MAJOR, \ diff --git a/libavfilter/vf_convolution.c b/libavfilter/vf_convolution.c index 9824edd452..5f59307c16 100644 --- a/libavfilter/vf_convolution.c +++ b/libavfilter/vf_convolution.c @@ -160,6 +160,26 @@ static void filter16_sobel(uint8_t *dstp, int width, } } +static void filter16_scharr(uint8_t *dstp, int width, + float scale, float delta, const int *const matrix, + const uint8_t *c[], int peak, int radius, + int dstride, int stride, int size) +{ + uint16_t *dst = (uint16_t *)dstp; + int x; + + for (x = 0; x < width; x++) { + float suma = AV_RN16A(&c[0][2 * x]) * -47 + AV_RN16A(&c[1][2 * x]) * -162 + AV_RN16A(&c[2][2 * x]) * -47 + + AV_RN16A(&c[6][2 * x]) * 47 + AV_RN16A(&c[7][2 * x]) * 162 + AV_RN16A(&c[8][2 * x]) * 47; + float sumb = AV_RN16A(&c[0][2 * x]) * -47 + AV_RN16A(&c[2][2 * x]) * 47 + AV_RN16A(&c[3][2 * x]) * -162 + + AV_RN16A(&c[5][2 * x]) * 162 + AV_RN16A(&c[6][2 * x]) * -47 + AV_RN16A(&c[8][2 * x]) * 47; + + suma /= 256.f; + sumb /= 256.f; + dst[x] = av_clip(sqrtf(suma*suma + sumb*sumb) * scale + delta, 0, peak); + } +} + static void filter16_kirsch(uint8_t *dstp, int width, float scale, float delta, const int *const matrix, const uint8_t *c[], int peak, int radius, @@ -264,6 +284,28 @@ static void filter_sobel(uint8_t *dst, int width, } } +static void filter_scharr(uint8_t *dst, int width, + float scale, float delta, const int *const matrix, + const uint8_t *c[], int peak, int radius, + int dstride, int stride, int size) +{ + const uint8_t *c0 = c[0], *c1 = c[1], *c2 = c[2]; + const uint8_t *c3 = c[3], *c5 = c[5]; + const uint8_t *c6 = c[6], *c7 = c[7], *c8 = c[8]; + int x; + + for (x = 0; x < width; x++) { + float suma = c0[x] * -47 + c1[x] * -162 + c2[x] * -47 + + c6[x] * 47 + c7[x] * 162 + c8[x] * 47; + float sumb = c0[x] * -47 + c2[x] * 47 + c3[x] * -162 + + c5[x] * 162 + c6[x] * -47 + c8[x] * 47; + + suma /= 256.f; + sumb /= 256.f; + dst[x] = av_clip_uint8(sqrtf(suma*suma + sumb*sumb) * scale + delta); + } +} + static void filter_kirsch(uint8_t *dst, int width, float scale, float delta, const int *const matrix, const uint8_t *c[], int peak, int radius, @@ -715,6 +757,10 @@ static int config_input(AVFilterLink *inlink) if (s->depth > 8) for (p = 0; p < s->nb_planes; p++) s->filter[p] = filter16_kirsch; + } else if (!strcmp(ctx->filter->name, "scharr")) { + if (s->depth > 8) + for (p = 0; p < s->nb_planes; p++) + s->filter[p] = filter16_scharr; } return 0; @@ -867,6 +913,17 @@ static av_cold int init(AVFilterContext *ctx) s->rdiv[i] = s->scale; s->bias[i] = s->delta; } + } else if (!strcmp(ctx->filter->name, "scharr")) { + for (i = 0; i < 4; i++) { + if ((1 << i) & s->planes) + s->filter[i] = filter_scharr; + else + s->copy[i] = 1; + s->size[i] = 3; + s->setup[i] = setup_3x3; + s->rdiv[i] = s->scale; + s->bias[i] = s->delta; + } } return 0; @@ -1006,4 +1063,24 @@ const AVFilter ff_vf_kirsch = { #endif /* CONFIG_KIRSCH_FILTER */ -#endif /* CONFIG_PREWITT_FILTER || CONFIG_ROBERTS_FILTER || CONFIG_SOBEL_FILTER */ +#if CONFIG_SCHARR_FILTER + +#define scharr_options prewitt_roberts_sobel_options +AVFILTER_DEFINE_CLASS(scharr); + +const AVFilter ff_vf_scharr = { + .name = "scharr", + .description = NULL_IF_CONFIG_SMALL("Apply scharr operator."), + .priv_size = sizeof(ConvolutionContext), + .priv_class = &scharr_class, + .init = init, + .query_formats = query_formats, + FILTER_INPUTS(convolution_inputs), + FILTER_OUTPUTS(convolution_outputs), + .flags = AVFILTER_FLAG_SUPPORT_TIMELINE_GENERIC | AVFILTER_FLAG_SLICE_THREADS, + .process_command = process_command, +}; + +#endif /* CONFIG_SCHARR_FILTER */ + +#endif /* CONFIG_PREWITT_FILTER || CONFIG_ROBERTS_FILTER || CONFIG_SOBEL_FILTER || CONFIG_SCHARR_FILTER */