[FFmpeg-devel] [PATCH] lavfi: port perspective filter from libmpcodecs
Stefano Sabatini
stefasab at gmail.com
Sun Jul 14 12:49:17 CEST 2013
On date Friday 2013-07-12 21:03:07 +0000, Paul B Mahol encoded:
> Signed-off-by: Paul B Mahol <onemda at gmail.com>
> ---
> doc/filters.texi | 22 +++
> libavfilter/Makefile | 1 +
> libavfilter/allfilters.c | 1 +
> libavfilter/vf_perspective.c | 382 +++++++++++++++++++++++++++++++++++++++++++
> 4 files changed, 406 insertions(+)
> create mode 100644 libavfilter/vf_perspective.c
>
> diff --git a/doc/filters.texi b/doc/filters.texi
> index bcc9687..9720ac2 100644
> --- a/doc/filters.texi
> +++ b/doc/filters.texi
> @@ -5717,6 +5717,28 @@ pad="2*iw:2*ih:ow-iw:oh-ih"
> @end example
> @end itemize
>
> + at section perspective
> +
> +Correct perspective of videos not recorded perpendicular to the screen.
> +
> +A description of the accepted parameters follows.
> +
> + at table @option
> + at item x0, y0, x1, y1, x2, y2, x3, y3,
Separate line for each item
@item x0
@item y0
...
> +Set coordinates of top left, top right, bottom left and bottom right corners.
> +
> + at item interpolation
> +Set interpolation for perspective correction.
> +
> +It accepts the following values:
> + at table @option
@table @samp
> + at item linear
> + at item cubic
> + at end table
> +
> +Default one is @var{linear}.
@samp{linear}
> + at end table
> +
> @section pixdesctest
>
> Pixel format descriptor test filter, mainly useful for internal
> diff --git a/libavfilter/Makefile b/libavfilter/Makefile
> index 306b24c..7f10409 100644
> --- a/libavfilter/Makefile
> +++ b/libavfilter/Makefile
> @@ -166,6 +166,7 @@ OBJS-$(CONFIG_OVERLAY_FILTER) += vf_overlay.o dualinput.o
> OBJS-$(CONFIG_OWDENOISE_FILTER) += vf_owdenoise.o
> OBJS-$(CONFIG_PAD_FILTER) += vf_pad.o
> OBJS-$(CONFIG_PERMS_FILTER) += f_perms.o
> +OBJS-$(CONFIG_PERSPECTIVE_FILTER) += vf_perspective.o
> OBJS-$(CONFIG_PIXDESCTEST_FILTER) += vf_pixdesctest.o
> OBJS-$(CONFIG_PP_FILTER) += vf_pp.o
> OBJS-$(CONFIG_PSNR_FILTER) += vf_psnr.o dualinput.o
> diff --git a/libavfilter/allfilters.c b/libavfilter/allfilters.c
> index 26472f8..bda6e3c 100644
> --- a/libavfilter/allfilters.c
> +++ b/libavfilter/allfilters.c
> @@ -161,6 +161,7 @@ void avfilter_register_all(void)
> REGISTER_FILTER(OWDENOISE, owdenoise, vf);
> REGISTER_FILTER(PAD, pad, vf);
> REGISTER_FILTER(PERMS, perms, vf);
> + REGISTER_FILTER(PERSPECTIVE, perspective, vf);
> REGISTER_FILTER(PIXDESCTEST, pixdesctest, vf);
> REGISTER_FILTER(PP, pp, vf);
> REGISTER_FILTER(PSNR, psnr, vf);
> diff --git a/libavfilter/vf_perspective.c b/libavfilter/vf_perspective.c
> new file mode 100644
> index 0000000..09eb548
> --- /dev/null
> +++ b/libavfilter/vf_perspective.c
> @@ -0,0 +1,382 @@
> +/*
> + * Copyright (c) 2002 Michael Niedermayer <michaelni at gmx.at>
> + *
> + * This file is part of FFmpeg.
> + *
> + * FFmpeg 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.
> + *
> + * FFmpeg 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 FFmpeg; if not, write to the Free Software
> + * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
> + */
> +
> +#include <float.h>
> +
> +#include "libavutil/pixdesc.h"
> +#include "libavutil/opt.h"
> +#include "avfilter.h"
> +#include "formats.h"
> +#include "internal.h"
> +#include "video.h"
> +
> +#define SUB_PIXEL_BITS 8
> +#define SUB_PIXELS (1 << SUB_PIXEL_BITS)
> +#define COEFF_BITS 11
> +
> +#define LINEAR 0
> +#define CUBIC 1
> +
> +typedef struct PerspectiveContext {
> + const AVClass *class;
> + double ref[4][2];
> + int32_t (*pv)[2];
> + int32_t coeff[SUB_PIXELS][4];
> + int interpolation;
> + int linesize;
> + int hsub, vsub;
> + int nb_planes;
> +
> + void (*perspective)(struct PerspectiveContext *s,
> + uint8_t *dst, int dst_linesize,
> + uint8_t *src, int src_linesize,
> + int w, int h, int hsub, int vsub);
> +} PerspectiveContext;
> +
> +#define OFFSET(x) offsetof(PerspectiveContext, x)
> +#define FLAGS AV_OPT_FLAG_FILTERING_PARAM|AV_OPT_FLAG_VIDEO_PARAM
> +
> +static const AVOption perspective_options[] = {
> + { "x0", "", OFFSET(ref[0][0]), AV_OPT_TYPE_DOUBLE, {.dbl=0}, -DBL_MAX, DBL_MAX, FLAGS },
> + { "y0", "", OFFSET(ref[0][1]), AV_OPT_TYPE_DOUBLE, {.dbl=0}, -DBL_MAX, DBL_MAX, FLAGS },
> + { "x1", "", OFFSET(ref[1][0]), AV_OPT_TYPE_DOUBLE, {.dbl=0}, -DBL_MAX, DBL_MAX, FLAGS },
> + { "y1", "", OFFSET(ref[1][1]), AV_OPT_TYPE_DOUBLE, {.dbl=0}, -DBL_MAX, DBL_MAX, FLAGS },
> + { "x2", "", OFFSET(ref[2][0]), AV_OPT_TYPE_DOUBLE, {.dbl=0}, -DBL_MAX, DBL_MAX, FLAGS },
> + { "y2", "", OFFSET(ref[2][1]), AV_OPT_TYPE_DOUBLE, {.dbl=0}, -DBL_MAX, DBL_MAX, FLAGS },
> + { "x3", "", OFFSET(ref[3][0]), AV_OPT_TYPE_DOUBLE, {.dbl=0}, -DBL_MAX, DBL_MAX, FLAGS },
> + { "y3", "", OFFSET(ref[3][1]), AV_OPT_TYPE_DOUBLE, {.dbl=0}, -DBL_MAX, DBL_MAX, FLAGS },
add help here, so it is shown in the automatic output
x0 -> set top left x position
y0 -> set top left y position
...
> + { "interpolation", "", OFFSET(interpolation), AV_OPT_TYPE_INT, {.i64=LINEAR}, 0, 1, FLAGS, "interpolation" },
same here
> + { "linear", "", 0, AV_OPT_TYPE_CONST, {.i64=LINEAR}, 0, 0, FLAGS, "interpolation" },
> + { "cubic", "", 0, AV_OPT_TYPE_CONST, {.i64=CUBIC}, 0, 0, FLAGS, "interpolation" },
> + { NULL }
> +};
> +
> +AVFILTER_DEFINE_CLASS(perspective);
> +
> +static int query_formats(AVFilterContext *ctx)
> +{
> + static const enum AVPixelFormat pix_fmts[] = {
> + AV_PIX_FMT_YUVA444P, AV_PIX_FMT_YUVA422P, AV_PIX_FMT_YUVA420P,
> + AV_PIX_FMT_YUVJ444P, AV_PIX_FMT_YUVJ440P, AV_PIX_FMT_YUVJ422P,AV_PIX_FMT_YUVJ420P, AV_PIX_FMT_YUVJ411P,
> + AV_PIX_FMT_YUV444P, AV_PIX_FMT_YUV440P, AV_PIX_FMT_YUV422P, AV_PIX_FMT_YUV420P, AV_PIX_FMT_YUV411P, AV_PIX_FMT_YUV410P,
> + AV_PIX_FMT_GBRP, AV_PIX_FMT_GBRAP, AV_PIX_FMT_GRAY8, AV_PIX_FMT_NONE
> + };
> +
> + ff_set_common_formats(ctx, ff_make_format_list(pix_fmts));
> + return 0;
> +}
> +
> +static inline double get_coeff(double d)
> +{
> + double coeff, A = -0.60;
> +
> + d = fabs(d);
> +
> + if (d < 1.0)
> + coeff = (1.0 - (A + 3.0) * d * d + (A + 2.0) * d * d * d);
> + else if (d < 2.0)
> + coeff = (-4.0 * A + 8.0 * A * d - 5.0 * A * d * d + A * d * d * d);
> + else
> + coeff = 0.0;
> +
> + return coeff;
> +}
If you understand the math please add some explanation here, so the
code is not completely obfuscated, or try to get some explanations out
of michaelni's head.
[...]
Should be good otherwise, assuming the output is the same as that of
mp=perspective (I can't get mp=perspective to work here).
--
FFmpeg = Fanciful Forgiving Mind-dumbing Political Elaborated Geisha
More information about the ffmpeg-devel
mailing list