[FFmpeg-devel] rectification filter
Daniel Oberhoff
danieloberhoff at gmail.com
Tue Jul 29 00:34:52 CEST 2014
Hi again,
Since at least in the web view of the list attachements seem to be stripped, I send it here in the clear:
thanks for reviewing!
Daniel
From 7b5b3b42804714ddc56d2b8cd2c5709b4bb91f70 Mon Sep 17 00:00:00 2001
From: Daniel Oberhoff <daniel at danieloberhoff.de>
Date: Mon, 28 Jul 2014 23:58:12 +0200
Subject: [PATCH] added rectification filter
---
libavfilter/Makefile | 1 +
libavfilter/allfilters.c | 1 +
libavfilter/vf_rectification.c | 277 +++++++++++++++++++++++++++++++++++++++++
3 files changed, 279 insertions(+)
create mode 100644 libavfilter/vf_rectification.c
diff --git a/libavfilter/Makefile b/libavfilter/Makefile
index 0f54381..1488eae 100644
--- a/libavfilter/Makefile
+++ b/libavfilter/Makefile
@@ -199,6 +199,7 @@ OBJS-$(CONFIG_W3FDIF_FILTER) += vf_w3fdif.o
OBJS-$(CONFIG_YADIF_FILTER) += vf_yadif.o
OBJS-$(CONFIG_ZMQ_FILTER) += f_zmq.o
OBJS-$(CONFIG_ZOOMPAN_FILTER) += vf_zoompan.o
+OBJS-$(CONFIG_RECTIFICATION_FILTER) += vf_rectification.o
OBJS-$(CONFIG_CELLAUTO_FILTER) += vsrc_cellauto.o
OBJS-$(CONFIG_COLOR_FILTER) += vsrc_testsrc.o
diff --git a/libavfilter/allfilters.c b/libavfilter/allfilters.c
index 1877557..f6e7bc2 100644
--- a/libavfilter/allfilters.c
+++ b/libavfilter/allfilters.c
@@ -216,6 +216,7 @@ void avfilter_register_all(void)
REGISTER_FILTER(YADIF, yadif, vf);
REGISTER_FILTER(ZMQ, zmq, vf);
REGISTER_FILTER(ZOOMPAN, zoompan, vf);
+ REGISTER_FILTER(RECTIFICATION, rectification, vf);
REGISTER_FILTER(CELLAUTO, cellauto, vsrc);
REGISTER_FILTER(COLOR, color, vsrc);
diff --git a/libavfilter/vf_rectification.c b/libavfilter/vf_rectification.c
new file mode 100644
index 0000000..fda4ebd
--- /dev/null
+++ b/libavfilter/vf_rectification.c
@@ -0,0 +1,277 @@
+/* rectification.c
+ * Copyright (C) 2007 Richard Spindler
+ * This file is a Frei0r plugin.
+ *
+ * This program is free software; you can redistribute it and/or modify
+ * it under the terms of the GNU General Public License as published by
+ * the Free Software Foundation; either version 2 of the License, or
+ * (at your option) any later version.
+ *
+ * This program is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
+ * GNU General Public License for more details.
+ *
+ * You should have received a copy of the GNU General Public License
+ * along with this program; if not, write to the Free Software
+ * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
+ */
+
+#include <stdlib.h>
+#include <math.h>
+#include <assert.h>
+#include <time.h>
+#include <stdio.h>
+#include <string.h>
+
+// todo: use ffmpeg's configure, or available results thereof, to decide this
+// using vectorization is definitely noticeable, but maybe due only to loop unrolling...
+#define HAVE_M512 0
+#define HAVE_M256 0
+#define HAVE_M128 1
+
+// todo: only include what's needed for the given vector width
+#include <emmintrin.h>
+#include <immintrin.h>
+#include <pmmintrin.h>
+#include <x86intrin.h>
+
+#if HAVE_M512
+ typedef __m512 VectorValue;
+# define VECTOR_SIZE 16
+# define SET1(x) _mm512_set1_ps(x)
+#elif HAVE_M256
+ typedef __m256 VectorValue;
+# define SET1(x) _mm256_set1_ps(x)
+# define VECTOR_SIZE 8
+#elif HAVE_M128
+ typedef __m128 VectorValue;
+# define SET1(x) _mm_set1_ps(x)
+# define VECTOR_SIZE 4
+#else
+ typedef float VectorValue;
+# define VECTOR_SIZE 1
+# define SET1(x) x
+#endif
+
+#include "libavutil/opt.h"
+#include "libavutil/intreadwrite.h"
+#include "libavutil/pixdesc.h"
+
+#include "avfilter.h"
+#include "drawutils.h"
+#include "internal.h"
+#include "video.h"
+
+#include <float.h>
+
+typedef struct RectificationCtx
+{
+ AVClass* av_class;
+ unsigned int width;
+ unsigned int height;
+ int hsub, vsub;
+ int nb_planes;
+ FFDrawContext draw;
+ double cx, cy, k1, k2;
+} RectificationCtx;
+
+VectorValue make_spread(float x, float delta);
+
+VectorValue make_spread(float x, float delta)
+{
+ VectorValue result;
+ float* p = (float*)&result;
+ for (size_t i = 0; i < VECTOR_SIZE; ++i, x += delta)
+ {
+ p[i] = x;
+ }
+ return result;
+}
+
+#define FLAGS AV_OPT_FLAG_FILTERING_PARAM|AV_OPT_FLAG_VIDEO_PARAM
+static const AVOption rectification_options[] = {
+ { "cx", "set relative center x", offsetof(RectificationCtx, cx), AV_OPT_TYPE_DOUBLE, {.dbl=0.5}, 0, 1, .flags=FLAGS },
+ { "cy", "set relative center y", offsetof(RectificationCtx, cy), AV_OPT_TYPE_DOUBLE, {.dbl=0.5}, 0, 1, .flags=FLAGS },
+ { "k1", "set quadratic distortion factor", offsetof(RectificationCtx, k1), AV_OPT_TYPE_DOUBLE, {.dbl=0.5}, 0, 1, .flags=FLAGS },
+ { "k2", "set double quadratic distortion factor", offsetof(RectificationCtx, k2), AV_OPT_TYPE_DOUBLE, {.dbl=0.5}, 0, 1, .flags=FLAGS },
+ { NULL }
+};
+
+AVFILTER_DEFINE_CLASS(rectification);
+
+static av_cold int init(AVFilterContext *ctx)
+{
+ return 0;
+}
+
+static av_cold void uninit(AVFilterContext *ctx)
+{
+}
+
+typedef struct ThreadData
+{
+ AVFrame *in, *out;
+ int w, h;
+ int plane;
+ float xcenter, ycenter;
+ float k1, k2;
+}ThreadData;
+
+static int filter_slice(AVFilterContext *ctx, void *arg, int job, int nb_jobs)
+{
+ ThreadData* td = (ThreadData*)arg;
+ RectificationCtx* rect = (RectificationCtx*)ctx->priv;
+
+ typedef float Scalar;
+
+ AVFrame *in = td->in;
+ AVFrame *out = td->out;
+
+ // pre-calc values needed in the loop
+ const int w = td->w, h = td->h;
+ const Scalar xcenter = td->xcenter;
+ const Scalar ycenter = td->ycenter;
+ const Scalar normallise_radius_sq = 4.0 / (w * w + h * h);
+ const VectorValue k1 = SET1(td->k1 - 0.5);
+ const VectorValue k2 = SET1(td->k2 - 0.5);
+ const VectorValue one = SET1(1);
+ const int start = (h * job ) / nb_jobs;
+ const int end = (h * (job+1)) / nb_jobs;
+ const int plane = td->plane;
+ const int inlinesize = in->linesize[plane];
+ const int outlinesize = out->linesize[plane];
+ const uint8_t* indata = in->data[plane];
+ uint8_t* outrow = out->data[plane] + start * outlinesize;
+ for(int i = start; i < end; ++i, outrow += outlinesize)
+ {
+ Scalar off_y = i - ycenter;
+ const Scalar off_y2 = off_y * off_y;
+ VectorValue off_x = make_spread(-xcenter, 1);
+ uint8_t* out = outrow;
+ for(int j = 0; j < w; j += VECTOR_SIZE, off_x += SET1(VECTOR_SIZE), out += VECTOR_SIZE)
+ {
+ const VectorValue r2 = (off_x * off_x + SET1(off_y2)) * SET1(normallise_radius_sq);
+ const VectorValue radius_mult = one + r2 * k1 + r2 * r2 * k2;
+ const VectorValue srcX = SET1(xcenter) + radius_mult * off_x;
+ const VectorValue srcY = SET1(ycenter) + radius_mult * SET1(off_y);
+ const Scalar* X = (const Scalar*)&srcX;
+ const Scalar* Y = (const Scalar*)&srcY;
+#define DOPIXEL(i) {const int x = X[i] + 0.5f, y = Y[i] + 0.5f; const char isvalid = (x > 0 && x < w - 1 && y > 0 && y < h - 1); out[i] = isvalid ? indata[y * inlinesize + x] : 0;}
+#ifdef HAVE_M256
+ DOPIXEL(0);
+ DOPIXEL(1);
+ DOPIXEL(2);
+ DOPIXEL(3);
+ DOPIXEL(4);
+ DOPIXEL(5);
+ DOPIXEL(6);
+ DOPIXEL(7);
+#elif defined(HAVE_M128)
+ DOPIXEL(0);
+ DOPIXEL(1);
+ DOPIXEL(2);
+ DOPIXEL(3);
+#else
+ for (size_t i = 0; i < VECTOR_SIZE; ++i)
+ {
+ DOPIXEL(i);
+ }
+#endif
+#undef DOPIXEL
+ }
+ }
+ return 0;
+}
+
+static int query_formats(AVFilterContext *ctx)
+{
+ static enum PixelFormat pix_fmts[] = {
+ AV_PIX_FMT_YUV410P,
+ AV_PIX_FMT_YUV444P, AV_PIX_FMT_YUVJ444P,
+ AV_PIX_FMT_YUV420P, AV_PIX_FMT_YUVJ420P,
+ AV_PIX_FMT_YUVA444P, AV_PIX_FMT_YUVA420P,
+ AV_PIX_FMT_NONE
+ };
+
+ ff_set_common_formats(ctx, ff_make_format_list(pix_fmts));
+ return 0;
+}
+
+static int config_props(AVFilterLink *outlink)
+{
+ AVFilterContext* ctx = outlink->src;
+ RectificationCtx* rect = ctx->priv;
+ AVFilterLink *inlink = ctx->inputs[0];
+ const AVPixFmtDescriptor *pixdesc = av_pix_fmt_desc_get(inlink->format);
+ ff_draw_init(&rect->draw, inlink->format, 0);
+ rect->hsub = pixdesc->log2_chroma_w;
+ rect->vsub = pixdesc->log2_chroma_h;
+ outlink->w = rect->width = inlink->w;
+ outlink->h = rect->height = inlink->h;
+ rect->nb_planes = av_pix_fmt_count_planes(inlink->format);
+ return 0;
+}
+
+static int filter_frame(AVFilterLink *inlink, AVFrame *in)
+{
+ AVFilterContext *ctx = inlink->dst;
+ AVFilterLink *outlink = ctx->outputs[0];
+ RectificationCtx* rect = (RectificationCtx*)ctx->priv;
+ AVFrame* out = ff_get_video_buffer(outlink, outlink->w, outlink->h);
+ if (!out) {
+ av_frame_free(&in);
+ return AVERROR(ENOMEM);
+ }
+ av_frame_copy_props(out, in);
+ for (int plane = 0; plane < rect->nb_planes; ++plane) {
+ int hsub = plane == 1 || plane == 2 ? rect->hsub : 0;
+ int vsub = plane == 1 || plane == 2 ? rect->vsub : 0;
+ const int w = rect->width >> hsub;
+ const int h = rect->height >> vsub;
+ ThreadData td = { .in = in, .out = out,
+ .w = w,
+ .h = h,
+ .xcenter = rect->cx * w,
+ .ycenter = rect->cy * h,
+ .k1 = rect->k1,
+ .k2 = rect->k2,
+ .plane = plane};
+ ctx->internal->execute(ctx, filter_slice, &td, NULL, FFMIN(h, ctx->graph->nb_threads));
+ }
+
+ av_frame_free(&in);
+ return ff_filter_frame(outlink, out);
+}
+
+static const AVFilterPad rectification_inputs[] = {
+ {
+ .name = "default",
+ .type = AVMEDIA_TYPE_VIDEO,
+ .filter_frame = filter_frame,
+ },
+ { NULL }
+};
+
+static const AVFilterPad rectification_outputs[] = {
+ {
+ .name = "default",
+ .type = AVMEDIA_TYPE_VIDEO,
+ .config_props = config_props,
+ },
+ { NULL }
+};
+
+AVFilter ff_vf_rectification = {
+ .name = "rectification",
+ .description = NULL_IF_CONFIG_SMALL("rectify the image."),
+ .priv_size = sizeof(RectificationCtx),
+ .init = init,
+ .uninit = uninit,
+ .query_formats = query_formats,
+ .process_command = NULL,
+ .inputs = rectification_inputs,
+ .outputs = rectification_outputs,
+ .priv_class = &rectification_class,
+ .flags = AVFILTER_FLAG_SLICE_THREADS,
+};
\ No newline at end of file
--
1.8.2
More information about the ffmpeg-devel
mailing list