[FFmpeg-devel] [PATCH] video stabilization plugins using vid.stab library

Michael Niedermayer michaelni at gmx.at
Mon Mar 18 00:55:19 CET 2013


On Sun, Mar 17, 2013 at 11:59:17PM +0100, Georg Martius wrote:
> Hi,
> 
> here is a patch for adding the filters for video stabilization. They need the 
> vid.stab library installed [1]. I decided to develop the library outside of 
> ffmpeg and only have the thin interfacing plugins in libavfilter.

> I didn't adapt the configure scripts because I am not familiar with them:
> "-lvidstab" is required for linking. 

its quite simple, just see how other libs are detected


> 
> [1] https://github.com/georgmartius/vid.stab
> 
> Best regards,
>   Georg

>  Makefile       |    4 
>  allfilters.c   |    4 
>  vf_stabilize.c |  367 +++++++++++++++++++++++++++++++++++++++++++++++++
>  vf_transform.c |  423 +++++++++++++++++++++++++++++++++++++++++++++++++++++++++
>  4 files changed, 797 insertions(+), 1 deletion(-)
> 90af79d9b6c7dee08e4e122daa717c254e2a2806  0001-video-stabilization-plugins-using-vid.stab-library.patch
> From e295d7d8b123484a2970b10ebee92faa99d870b6 Mon Sep 17 00:00:00 2001
> From: Georg Martius <martius at mis.mpg.de>
> Date: Sun, 17 Mar 2013 23:36:38 +0100
> Subject: [PATCH] video stabilization plugins using vid.stab library
> 
> ---
>  libavfilter/Makefile       |    4 +-
>  libavfilter/allfilters.c   |    4 +
>  libavfilter/vf_stabilize.c |  367 ++++++++++++++++++++++++++++++++++++++
>  libavfilter/vf_transform.c |  423 ++++++++++++++++++++++++++++++++++++++++++++
>  4 files changed, 797 insertions(+), 1 deletion(-)
>  create mode 100644 libavfilter/vf_stabilize.c
>  create mode 100644 libavfilter/vf_transform.c
> 
> diff --git a/libavfilter/Makefile b/libavfilter/Makefile
> index 5835a7e..fa8048f 100644
> --- a/libavfilter/Makefile
> +++ b/libavfilter/Makefile
> @@ -45,7 +45,6 @@ OBJS = allfilters.o                                                     \
>         transform.o                                                      \
>         video.o                                                          \
>  
> -
>  OBJS-$(CONFIG_AVCODEC)                       += avcodec.o
>  OBJS-$(CONFIG_AVFORMAT)                      += lavfutils.o
>  OBJS-$(CONFIG_SWSCALE)                       += lswsutils.o

unrelated


> @@ -151,6 +150,9 @@ OBJS-$(CONFIG_TRANSPOSE_FILTER)              += vf_transpose.o
>  OBJS-$(CONFIG_UNSHARP_FILTER)                += vf_unsharp.o
>  OBJS-$(CONFIG_VFLIP_FILTER)                  += vf_vflip.o
>  OBJS-$(CONFIG_YADIF_FILTER)                  += vf_yadif.o

> +# added

doesnt belong in here


> +OBJS-$(CONFIG_STABILIZE_FILTER)              += vf_stabilize.o
> +OBJS-$(CONFIG_TRANSFORM_FILTER)              += vf_transform.o

someone will soon tell you something about maintaining alphabetic
ordered lists


>  
>  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 24df561..83de040 100644
> --- a/libavfilter/allfilters.c
> +++ b/libavfilter/allfilters.c
> @@ -157,6 +157,10 @@ void avfilter_register_all(void)
>      REGISTER_FILTER(SMPTEBARS,      smptebars,      vsrc);
>      REGISTER_FILTER(TESTSRC,        testsrc,        vsrc);
>  
> +    /* video stabilization vid.stab*/
> +    REGISTER_FILTER (STABILIZE,   stabilize,   vf);
> +    REGISTER_FILTER (TRANSFORM,   transform,   vf);
> +
>      REGISTER_FILTER(NULLSINK,       nullsink,       vsink);
>  
>      /* multimedia filters */
> diff --git a/libavfilter/vf_stabilize.c b/libavfilter/vf_stabilize.c
> new file mode 100644
> index 0000000..8f68bed
> --- /dev/null
> +++ b/libavfilter/vf_stabilize.c
> @@ -0,0 +1,367 @@
> +/*
> + *  vf_stabilize.c
> + *
> + *  Copyright (C) Georg Martius - Jan 2012
> + *   georg dot martius at web dot de
> + *
> + *  This file is part of vid.stab, video deshaking lib
> + *
> + *  vid.stab 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, or (at your option)
> + *  any later version.
> + *
> + *  vid.stab 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 GNU Make; see the file COPYING.  If not, write to
> + *  the Free Software Foundation, 675 Mass Ave, Cambridge, MA 02139, USA.
> + *
> + */
> +
> +/* Typical call: (This will visualize some internals in the video)
> + *  ffmpeg -i input -vf stabilize=shakiness=5:show=1 dummy.avi
> + *  all parameters are optional
> + */
> +
> +
> +#define DEFAULT_TRANS_FILE_NAME     "transforms.trf"
> +#define VS_INPUT_MAXLEN 1024
> +
> +#include "libavutil/common.h"
> +#include "libavutil/opt.h"
> +#include "libavutil/imgutils.h"
> +// #include "libavcodec/dsputil.h"
> +#include "avfilter.h"
> +#include "internal.h"
> +
> +#include <vid.stab/libvidstab.h>
> +

> +/* private date structure of this filter*/
> +typedef struct _stab_data {    
> +    AVClass* class;
> +

> +    MotionDetect md;

the struct name should probably have some prefix to avoid namespace
conflicts, if iam guessing correctly that its from libvidstab.h


> +    AVFilterBufferRef *ref;    ///< Previous frame
> +
> +    char* args;
> +    char* result;
> +    FILE* f;
> +} StabData;
> +
> +
> +/*** some conversions from avlib to vid.stab constants and functions ****/
> +

> +/** convert AV's pixelformat to vid.stab pixelformat */
> +static PixelFormat AV2OurPixelFormat(AVFilterContext *ctx, enum AVPixelFormat pf){
> +	switch(pf){
> +    case AV_PIX_FMT_YUV420P:  return PF_YUV420P;
> +		case AV_PIX_FMT_YUV422P:	return PF_YUV422P;

tabs arent allowed in ffmpeg git



> +		case AV_PIX_FMT_YUV444P:	return PF_YUV444P;
> +		case AV_PIX_FMT_YUV410P:	return PF_YUV410P;
> +		case AV_PIX_FMT_YUV411P:	return PF_YUV411P;
> +		case AV_PIX_FMT_YUV440P:	return PF_YUV440P;
> +		case AV_PIX_FMT_YUVA420P: return PF_YUVA420P;
> +		case AV_PIX_FMT_GRAY8:		return PF_GRAY8;
> +		case AV_PIX_FMT_RGB24:		return PF_RGB24;
> +		case AV_PIX_FMT_BGR24:		return PF_BGR24;
> +		case AV_PIX_FMT_RGBA:		  return PF_RGBA;
> +	default:
> +		av_log(ctx, AV_LOG_ERROR, "cannot deal with pixel format %i!\n", pf);
> +		return PF_NONE;
> +	}
> +}
> +

> +/// pointer to context for logging
> +void *_stab_ctx = 0;

non constant globals are generally a bad idea, there can be multiple
filter instances, multiple filter graphs and they can get accessed
at the same time from different threads


> +/** wrapper to log vs_log into av_log */
> +static int av_log_wrapper(int type, const char* tag, const char* format, ...){
> +    va_list ap;
> +    av_log(_stab_ctx, type, "%s: ", tag);
> +    va_start (ap, format);
> +    av_vlog(_stab_ctx, type, format, ap);
> +    va_end (ap);
> +    return VS_OK;
> +}
> +
> +/** sets the memory allocation function and logging constants to av versions */
> +static void setMemAndLogFunctions(void){
> +    vs_malloc  = av_malloc;
> +    vs_zalloc  = av_mallocz;
> +    vs_realloc = av_realloc;
> +    vs_free    = av_free;
> +
> +    VS_ERROR_TYPE = AV_LOG_ERROR;
> +    VS_WARN_TYPE  = AV_LOG_WARNING;
> +    VS_INFO_TYPE  = AV_LOG_INFO;
> +    VS_MSG_TYPE   = AV_LOG_VERBOSE;
> +
> +    vs_log   = av_log_wrapper;
> +
> +    VS_ERROR = 0;
> +    VS_OK    = 1;
> +}
> +
> +/*** Commandline options ****/
> +
> +
> +#define OFFSET(x) offsetof(StabData, x)
> +#define OFFSETMD(x) (offsetof(StabData, md)+offsetof(MotionDetect, x))
> +#define FLAGS AV_OPT_FLAG_FILTERING_PARAM|AV_OPT_FLAG_VIDEO_PARAM
> +
> +static const AVOption stabilize_options[]= {
> +    {"result",    "path to the file used to write the transforms (def:transforms.trf)",
> +     OFFSET(result),    AV_OPT_TYPE_STRING },
> +    {"shakiness",  "how shaky is the video and how quick is the camera? 1: little (fast) 10: very strong/quick (slow) (def: 5)",
> +     OFFSETMD(shakiness),    AV_OPT_TYPE_INT, {.i64 = 5}, 1, 10, FLAGS},
> +    {"accuracy",    "accuracy of detection process (>=shakiness) 1: low (fast) 15: high (slow) (def: 9)",
> +     OFFSETMD(accuracy),    AV_OPT_TYPE_INT, {.i64 = 9 }, 1, 15, FLAGS},
> +    {"stepsize",    "stepsize of search process, region around minimum is scanned with 1 pixel resolution (def: 6)",
> +     OFFSETMD(stepSize),    AV_OPT_TYPE_INT, {.i64 = 6}, 1, 32, FLAGS},
> +    {"mincontrast", "below this contrast a field is discarded (0-1) (def: 0.3)",
> +     OFFSETMD(contrastThreshold), AV_OPT_TYPE_DOUBLE, {.dbl =  0.25}, 0.0, 1.0, FLAGS},
> +    {"show",    "0: draw nothing (def); 1,2: show fields and transforms in the resulting frames",
> +     OFFSETMD(show), AV_OPT_TYPE_INT, {.i64 =  0}, 0, 2, FLAGS},
> +    {"tripod",         "virtual tripod mode (if >0): motion is compared to a \n"
> +    "                  reference frame (frame # is the value) (def: 0)\n",
> +     OFFSETMD(virtualTripod), AV_OPT_TYPE_INT, {.i64 = 0}, 0, INT_MAX, FLAGS},
> +    {NULL},

this would be more readable with some vertical alignment

also see tools/patcheck

[...]

-- 
Michael     GnuPG fingerprint: 9FF2128B147EF6730BADF133611EC787040B0FAB

The bravest are surely those who have the clearest vision
of what is before them, glory and danger alike, and yet
notwithstanding go out to meet it. -- Thucydides
-------------- next part --------------
A non-text attachment was scrubbed...
Name: not available
Type: application/pgp-signature
Size: 198 bytes
Desc: Digital signature
URL: <http://ffmpeg.org/pipermail/ffmpeg-devel/attachments/20130318/68e87faf/attachment.asc>


More information about the ffmpeg-devel mailing list