summaryrefslogtreecommitdiff
path: root/libavfilter/vf_convolution.c
diff options
context:
space:
mode:
authorPaul B Mahol <onemda@gmail.com>2021-09-10 17:52:23 +0200
committerPaul B Mahol <onemda@gmail.com>2021-09-10 18:43:51 +0200
commit463c71b3b35e121bdc23541a9f9f40af2436d773 (patch)
treea4e1cf5927a2c9846595c7bd9ec550a02c65110e /libavfilter/vf_convolution.c
parent0b4d009587dd3e31b275bc29b197890adeefaf23 (diff)
avfilter/vf_convolution: add scharr operator
Diffstat (limited to 'libavfilter/vf_convolution.c')
-rw-r--r--libavfilter/vf_convolution.c79
1 files changed, 78 insertions, 1 deletions
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 */