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

Clément Bœsch ubitux at gmail.com
Tue Apr 9 10:27:35 CEST 2013


On Tue, Apr 02, 2013 at 12:36:00AM +0200, Georg Martius wrote:
> Hi,
> 
> attached is my new patch with new names for the filters:
> vidstabdetect and vidstabtransform
> 
> with configure script and makefile adaptations
> with documentation in filters.texi
> without tabs ;-)
> 

Sorry for the delay.

The following review is mostly cosmetics now; if you honor the comments
below, it should be OK at the next iteration.

> Regards,
>   Georg
> 
[...]
> From d1474504e130f170bd235f69969e67a1a32b6fb2 Mon Sep 17 00:00:00 2001
> From: Georg Martius <martius at mis.mpg.de>
> Date: Thu, 28 Mar 2013 22:59:16 +0100
> Subject: [PATCH] video stabilization plugins using vid.stab library:
>  vidstabdetect and vidstabtransform
> 
> 
> Signed-off-by: Georg Martius <martius at mis.mpg.de>
> ---
>  Changelog                         |    1 +
>  configure                         |    6 +
>  doc/filters.texi                  |  184 +++++++++++++++++
>  libavfilter/Makefile              |    2 +
>  libavfilter/allfilters.c          |    2 +
>  libavfilter/vf_vidstabdetect.c    |  331 +++++++++++++++++++++++++++++++
>  libavfilter/vf_vidstabtransform.c |  396 +++++++++++++++++++++++++++++++++++++
>  7 files changed, 922 insertions(+)
>  create mode 100644 libavfilter/vf_vidstabdetect.c
>  create mode 100644 libavfilter/vf_vidstabtransform.c
> 
> diff --git a/Changelog b/Changelog
> index 72a51ea..537deff 100644
> --- a/Changelog
> +++ b/Changelog
> @@ -14,6 +14,7 @@ version <next>:
>  - WebP demuxing and decoding support
>  - new ffmpeg options -filter_script and -filter_complex_script, which allow a
>    filtergraph description to be read from a file
> +- stabilize and transform filter: video stabilization using vid.stab library
>  
>  
>  version 1.2:
> diff --git a/configure b/configure
> index 8dc2500..d6284cd 100755
> --- a/configure
> +++ b/configure
> @@ -224,6 +224,7 @@ External library support:
>    --enable-libtwolame      enable MP2 encoding via libtwolame [no]
>    --enable-libutvideo      enable Ut Video encoding and decoding via libutvideo [no]
>    --enable-libv4l2         enable libv4l2/v4l-utils [no]
> +  --enable-libvidstab      enable video stabilization using vid.stab [no]
>    --enable-libvo-aacenc    enable AAC encoding via libvo-aacenc [no]
>    --enable-libvo-amrwbenc  enable AMR-WB encoding via libvo-amrwbenc [no]
>    --enable-libvorbis       enable Vorbis en/decoding via libvorbis,
> @@ -1171,6 +1172,7 @@ EXTERNAL_LIBRARY_LIST="
>      libtwolame
>      libutvideo
>      libv4l2
> +    libvidstab
>      libvo_aacenc
>      libvo_amrwbenc
>      libvorbis
> @@ -2115,6 +2117,8 @@ stereo3d_filter_deps="gpl"
>  subtitles_filter_deps="avformat avcodec libass"
>  super2xsai_filter_deps="gpl"
>  tinterlace_filter_deps="gpl"
> +vidstabdetect_filter_deps="libvidstab"
> +vidstabtransform_filter_deps="libvidstab"
>  yadif_filter_deps="gpl"
>  pixfmts_super2xsai_test_deps="super2xsai_filter"
>  tinterlace_merge_test_deps="tinterlace_filter"
> @@ -3532,6 +3536,7 @@ die_license_disabled_gpl() {
>  
>  die_license_disabled gpl libcdio
>  die_license_disabled gpl libutvideo
> +die_license_disabled gpl libvidstab

Please add just an entry in "compatible libraries" in the section of the
LICENSE file.

>  die_license_disabled gpl libx264
>  die_license_disabled gpl libxavs
>  die_license_disabled gpl libxvid
> @@ -3967,6 +3972,7 @@ enabled libtwolame && require  libtwolame twolame.h twolame_init -ltwolame &&
>                          die "ERROR: libtwolame must be installed and version must be >= 0.3.10"; }
>  enabled libutvideo    && require_cpp utvideo "stdint.h stdlib.h utvideo/utvideo.h utvideo/Codec.h" 'CCodec*' -lutvideo -lstdc++
>  enabled libv4l2    && require_pkg_config libv4l2 libv4l2.h v4l2_ioctl
> +enabled libvidstab && require_pkg_config vidstab vid.stab/libvidstab.h vsMotionDetectInit
>  enabled libvo_aacenc && require libvo_aacenc vo-aacenc/voAAC.h voGetAACEncAPI -lvo-aacenc
>  enabled libvo_amrwbenc && require libvo_amrwbenc vo-amrwbenc/enc_if.h E_IF_init -lvo-amrwbenc
>  enabled libvorbis  && require  libvorbis vorbis/vorbisenc.h vorbis_info_init -lvorbisenc -lvorbis -logg
> diff --git a/doc/filters.texi b/doc/filters.texi
> index 4190cca..7bed8f7 100644
> --- a/doc/filters.texi
> +++ b/doc/filters.texi
> @@ -4706,6 +4706,114 @@ much, but it will increase the amount of blurring needed to cover over
>  the image and will destroy more information than necessary, and extra
>  pixels will slow things down on a large logo.
>  
> + at anchor{vidstabtransform}
> + at section vidstabtransform
> +
> +Video stabilization/deshaking: pass 2 of 2,
> +see @ref{vidstabdetect} for pass 1
> +

Period missing.

> +Reads a file with transform information for each frame and
> +applies/compensates them. Together with the @ref{vidstabdetect}

Read, apply, compensate; we don't use the third person for some reason.

> +filter this can be used to deshake videos. See also
> + at url{http://public.hronopik.de/vid.stab}. It is important to also use
> +the unsharp filter, see below.
> +
> +To enable compilation of this filter you need to configure FFmpeg with
> + at code{--enable-libvidstab}.
> +
> +This filter accepts the following named options, expressed as a
> +sequence of @var{key}=@var{value} pairs, separated by ":".
> +
> + at table @option
> +
> + at item input
> +path to the file used to read the transforms (default: transforms.trf)
> +

@file{transforms.trf}

> + at item smoothing
> +number of frames*2 + 1 used for lowpass filtering used for stabilizing
> +(default: 10) For example a number of 10 means that 21 frames are used

Please add some little punctuation here.

> +(10 in the past and 10 in the future) to smoothen the motion in the
> +video. A larger values leads to a smoother video but limits the
> +acceleration of the camera (pan/tilt movements)
> +
> + at item maxshift
> +maximal number of pixels to translate frames (default: -1 no limit)
> +
> + at item maxangle
> +maximal angle in radians (degree*PI/180) to rotate frames (default: -1
> +no limit)
> +
> + at item crop
> +How to deal with borders that may be visible due to movement
> +compensation

"Available values are:"

> + at table @option

@table @samp

> + at item keep
> +keep image information from previous frame (default)
> + at item black
> +fill the border black
> + at end table
> +
> + at item invert
> + 0: keep normal (default), 1: invert transforms
> +

A table with the two entries would be better.

> + at item relative
> +consider transforms as 0: absolute, 1: relative to previous frame
> +(default)
> +

Ditto

> + at item zoom
> +percentage to zoom >0: zoom in, <0 zoom out (default: 0)
> +
> + at item optzoom
> +0: nothing, 1: determine optimal zoom (default).
> +Optimal zoom means no (or only little) border should be visible.
> +Note that the value given at zoom is added to the one calculated
> +here.
> +
> + at item interpol
> +type of interpolation
> +

"Available values are:"

> + at table @option

@table @samp

> + at item no
> +no interpolation
> + at item linear
> +linear only horizontal
> + at item bilinear
> +linear in both directions (default)
> + at item bicubic
> +cubic in both directions (slow)
> + at end table
> +
> + at item tripod
> +0: off (default),
> +1: virtual tripod mode: equivalent to @code{relative=0:smoothing=0}
> +

Again a table would be welcome

> + at end table
> +
> + at subsection Examples
> +
> + at itemize
> + at item
> +typical call with default default values:
> + (note the unsharp filter which is always recommended)
> + at example
> +  ffmpeg -i inp.mpeg -vf vidstabtransform,unsharp=5:5:0.8:3:3:0.4 inp_stabilized.mpeg
> + at end example
> +

Please don't indent the examples; since it's in a <pre> it will be shifted
in comparison with the others.

> + at item
> +zoom in a bit more and load transform data from a given file
> + at example
> +  vidstabtransform=zoom=5:input="mytransforms.trf"
> + at end example
> +
> + at item
> +smoothen the video even more
> + at example
> +  vidstabtransform=smoothing=30
> + at end example
> +
> + at end itemize
> +
> +
>  @section scale
>  
>  Scale (resize) the input video, using the libswscale library.
> @@ -5084,6 +5192,82 @@ in [-30,0] will filter edges. Default value is 0.
>  If a chroma option is not explicitly set, the corresponding luma value
>  is set.
>  
> + at anchor{vidstabdetect}
> + at section vidstabdetect
> +
> +Video stabilization/deshaking: pass 1 of 2 see @ref{vidstabtransform}
> +for pass 2
> +
> +Generates a file with relative transform information translation,
> +rotation about subsequent frames.
> +
> +To enable compilation of this filter you need to configure FFmpeg with
> + at code{--enable-libvidstab}.
> +
> +This filter accepts the following named options, expressed as a
> +sequence of @var{key}=@var{value} pairs, separated by ":".
> +
> + at table @option
> + at item result
> +path to the file used to write the transforms (default:inputfile.stab)
> +

@file

> + at item shakiness
> +how shaky is the video and how quick is the camera?
> +1: little (fast) 10: very strong/quick (slow) (defaultault: 5)
> +

@table ...

> + at item accuracy
> +accuracy of detection process (>=shakiness)
> +1: low (fast) 15: high (slow) (default: 9)
> +

@table ...

[...]

Same comments as previously apply here as well.

>  @section stereo3d
>  
>  Convert between different stereoscopic image formats.
> diff --git a/libavfilter/Makefile b/libavfilter/Makefile
> index 690b1cb..6be4b31 100644
> --- a/libavfilter/Makefile
> +++ b/libavfilter/Makefile
> @@ -166,6 +166,8 @@ OBJS-$(CONFIG_TINTERLACE_FILTER)             += vf_tinterlace.o
>  OBJS-$(CONFIG_TRANSPOSE_FILTER)              += vf_transpose.o
>  OBJS-$(CONFIG_UNSHARP_FILTER)                += vf_unsharp.o
>  OBJS-$(CONFIG_VFLIP_FILTER)                  += vf_vflip.o
> +OBJS-$(CONFIG_VIDSTABDETECT_FILTER)          += vf_vidstabdetect.o
> +OBJS-$(CONFIG_VIDSTABTRANSFORM_FILTER)       += vf_vidstabtransform.o
>  OBJS-$(CONFIG_YADIF_FILTER)                  += vf_yadif.o
>  
>  OBJS-$(CONFIG_CELLAUTO_FILTER)               += vsrc_cellauto.o
> diff --git a/libavfilter/allfilters.c b/libavfilter/allfilters.c
> index 45a67e5..d921272 100644
> --- a/libavfilter/allfilters.c
> +++ b/libavfilter/allfilters.c
> @@ -162,6 +162,8 @@ void avfilter_register_all(void)
>      REGISTER_FILTER(TRANSPOSE,      transpose,      vf);
>      REGISTER_FILTER(UNSHARP,        unsharp,        vf);
>      REGISTER_FILTER(VFLIP,          vflip,          vf);
> +    REGISTER_FILTER(VIDSTABDETECT,  vidstabdetect,  vf);
> +    REGISTER_FILTER(VIDSTABTRANSFORM, vidstabtransform, vf);
>      REGISTER_FILTER(YADIF,          yadif,          vf);
>  
>      REGISTER_FILTER(CELLAUTO,       cellauto,       vsrc);
> diff --git a/libavfilter/vf_vidstabdetect.c b/libavfilter/vf_vidstabdetect.c
> new file mode 100644
> index 0000000..d7c73f3
> --- /dev/null
> +++ b/libavfilter/vf_vidstabdetect.c
> @@ -0,0 +1,331 @@
> +/*
> + *  vf_vidstabdetect.c
> + *

This is is useless and will likely be forgotten in case of rename (let's
say if we want to remove all the vf_ etc prefixes at some point)

> + *  Copyright (C) Georg Martius - Jan 2012
> + *   georg dot martius at web dot de
> + *

This is a bit inconsistent with the other copyright forms of the codebase.

> + * This file is part of FFmpeg.
> + *
> + * FFmpeg is free software; you can redistribute it and/or
> + * modify it under the terms of the GNU Lesser General Public
> + * License as published by the Free Software Foundation; either
> + * version 2.1 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
> + * Lesser General Public License for more details.
> + *
> + * You should have received a copy of the GNU Lesser 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
> + */
> +
> +#define DEFAULT_RESULT_NAME     "transforms.trf"
> +
> +#include <vid.stab/libvidstab.h>
> +
> +#include "libavutil/common.h"
> +#include "libavutil/opt.h"
> +#include "libavutil/imgutils.h"

> +// #include "libavcodec/dsputil.h"

If you don't need it, please remove

> +#include "avfilter.h"
> +#include "internal.h"
> +
> +/* private date structure of this filter*/

Useless comment

> +typedef struct _stab_data {

Please remove the '_', or the _stab_data completely.

> +    const AVClass* class;
> +
> +    VSMotionDetect md;
> +
> +    char* args;
> +    char* result;
> +    FILE* f;
> +} StabData;
> +
> +

> +/* ** Commandline options *** */
> +

Useless comment

> +#define OFFSET(x) offsetof(StabData, x)
> +#define OFFSETMD(x) (offsetof(StabData, md)+offsetof(VSMotionDetect, x))
> +#define FLAGS AV_OPT_FLAG_FILTERING_PARAM|AV_OPT_FLAG_VIDEO_PARAM
> +
> +static const AVOption vidstabdetect_options[]= {
> +    {"result",      "path to the file used to write the transforms (def:transforms.trf)", OFFSET(result),              AV_OPT_TYPE_STRING, {.str = DEFAULT_RESULT_NAME}},
> +    {"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",    "(>=shakiness) 1: low 15: high (slow) (def: 9)",                      OFFSETMD(accuracy),          AV_OPT_TYPE_INT,    {.i64 = 9 }, 1, 15,       FLAGS},
> +    {"stepsize",    "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",             OFFSETMD(show),              AV_OPT_TYPE_INT,    {.i64 =  0}, 0, 2,        FLAGS},
> +    {"tripod",      "virtual tripod mode (if >0): motion is compared to a reference"
> +                    " reference frame (frame # is the value) (def: 0)",                   OFFSETMD(virtualTripod),     AV_OPT_TYPE_INT,    {.i64 = 0},  0, INT_MAX,  FLAGS},
> +    {NULL},
> +};
> +
> +AVFILTER_DEFINE_CLASS(vidstabdetect);
> +

> +/* ** some conversions from avlib to vid.stab constants and functions *** */
> +
> +/** convert AV's pixelformat to vid.stab pixelformat */
> +static VSPixelFormat av_2_vs_pixel_format(AVFilterContext *ctx, enum AVPixelFormat pf){
> +    switch(pf){
> +    case AV_PIX_FMT_YUV420P:  return PF_YUV420P;
> +    case AV_PIX_FMT_YUV422P:  return PF_YUV422P;
> +    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;
> +    }
> +}
> +
> +/// struct to hold a valid context for logging from within vid.stab lib
> +typedef struct {
> +    const AVClass* class;
> +} StabLogCtx;
> +
> +/** wrapper to log vs_log into av_log */
> +static int av_log_wrapper(int type, const char* tag, const char* format, ...){
> +    va_list ap;
> +    StabLogCtx ctx;
> +    ctx.class = &vidstabdetect_class;
> +    av_log(&ctx,  type, "%s: ", tag);
> +    va_start (ap, format);
> +    av_vlog(&ctx, type, format, ap);
> +    va_end (ap);
> +    return VS_OK;
> +}
> +
> +/** sets the memory allocation function and logging constants to av versions */
> +static void set_mem_and_log_functions(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;
> +}
> +
> +

> +/*************************************************************************/
> +
> +/* Module interface routines and data. */
> +
> +/*************************************************************************/
> +

I believe this huge comment is not really necessary.

> +static av_cold int init(AVFilterContext *ctx, const char *args)
> +{
> +    StabData* sd = ctx->priv;
> +    set_mem_and_log_functions();
> +
> +    sd->class = &vidstabdetect_class;
> +    av_opt_set_defaults(sd); // the default values are overwritten by initMotiondetect later
> +
> +    av_log(ctx, AV_LOG_VERBOSE, "vidstabdetect filter: init %s\n", LIBVIDSTAB_VERSION);
> +
> +    // save args for later, because the initialization of the vid.stab library requires
> +    //  knowledge about the input.
> +    sd->args=av_strdup(args);
> +
> +    return 0;
> +}
> +
> +static av_cold void uninit(AVFilterContext *ctx)
> +{
> +    StabData *sd = ctx->priv;
> +    VSMotionDetect* md = &(sd->md);
> +
> +    av_opt_free(sd);
> +    if (sd->f) {
> +        fclose(sd->f);
> +        sd->f = NULL;
> +    }
> +
> +    vsMotionDetectionCleanup(md);
> +    av_free(sd->args);
> +}
> +
> +static int query_formats(AVFilterContext *ctx)
> +{
> +    // If you add something here also add it to the above mapping function
> +    static const enum AVPixelFormat pix_fmts[] = {
> +        AV_PIX_FMT_YUV444P,  AV_PIX_FMT_YUV422P, AV_PIX_FMT_YUV420P,
> +        AV_PIX_FMT_YUV411P,  AV_PIX_FMT_YUV410P, AV_PIX_FMT_YUVA420P,
> +        AV_PIX_FMT_YUV440P,  AV_PIX_FMT_GRAY8,
> +        AV_PIX_FMT_RGB24, AV_PIX_FMT_BGR24, AV_PIX_FMT_RGBA,
> +        AV_PIX_FMT_NONE
> +    };
> +
> +    ff_set_common_formats(ctx, ff_make_format_list(pix_fmts));
> +    return 0;
> +}
> +
> +
> +static int config_input(AVFilterLink *inlink)
> +{
> +    AVFilterContext *ctx = inlink->dst;
> +    StabData *sd = ctx->priv;
> +    int returnval;
> +
> +    VSMotionDetect* md = &(sd->md);
> +    VSFrameInfo fi;
> +    const AVPixFmtDescriptor *desc = av_pix_fmt_desc_get(inlink->format);
> +
> +    vsFrameInfoInit(&fi,inlink->w, inlink->h, av_2_vs_pixel_format(ctx, inlink->format));

> +    // check

useless comment

> +    if(fi.bytesPerPixel != av_get_bits_per_pixel(desc)/8){
> +        av_log(ctx, AV_LOG_ERROR, "pixel-format error: wrong bits/per/pixel");
> +        return AVERROR(EINVAL);
> +    }
> +    if(fi.log2ChromaW != desc->log2_chroma_w){
> +        av_log(ctx, AV_LOG_ERROR, "pixel-format error: log2_chroma_w");
> +        return AVERROR(EINVAL);
> +    }
> +
> +    if(fi.log2ChromaH != desc->log2_chroma_h){
> +        av_log(ctx, AV_LOG_ERROR, "pixel-format error: log2_chroma_h");
> +        return AVERROR(EINVAL);
> +    }
> +

These error messages will let users desperate.

> +    if(vsMotionDetectInit(md, &fi, "vidstabdetect") != VS_OK){
> +        av_log(ctx, AV_LOG_ERROR, "initialization of Motion Detection failed");
> +        return AVERROR(EINVAL);
> +    }
> +
> +    // we need to do it after vsMotionDetectInit because otherwise the values are overwritten
> +    if ((returnval = (av_set_options_string(sd, sd->args, "=", ":"))) < 0)
> +        return returnval;
> +

> +    // display help
> +    /* if(optstr_lookup(sd->options, "help")) { */
> +    /*     av_log(ctx, AV_LOG_INFO, vs_motiondetect_help); */
> +    /*     return AVERROR(EINVAL); */
> +    /* } */
> +

Could you remove such dead code?

> +    if(vsMotionDetectConfigure(md)!= VS_OK){
> +        av_log(ctx, AV_LOG_ERROR, "configuration of Motion Detection failed\n");
> +        return AVERROR(EINVAL);
> +    }
> +
> +    av_log(ctx, AV_LOG_INFO, "Image Stabilization Settings:\n");
> +    av_log(ctx, AV_LOG_INFO, "     shakiness = %d\n", md->shakiness);
> +    av_log(ctx, AV_LOG_INFO, "      accuracy = %d\n", md->accuracy);
> +    av_log(ctx, AV_LOG_INFO, "      stepsize = %d\n", md->stepSize);
> +    av_log(ctx, AV_LOG_INFO, "   mincontrast = %f\n", md->contrastThreshold);
> +    av_log(ctx, AV_LOG_INFO, "          show = %d\n", md->show);
> +    av_log(ctx, AV_LOG_INFO, "        result = %s\n", sd->result);
> +
> +    sd->f = fopen(sd->result, "w");
> +    if (sd->f == NULL) {
> +        av_log(ctx, AV_LOG_ERROR, "cannot open transform file %s\n", sd->result);
> +        return AVERROR(EINVAL);
> +    }else{
> +        if(vsPrepareFile(md, sd->f) != VS_OK){
> +            av_log(ctx, AV_LOG_ERROR, "cannot write to transform file %s\n", sd->result);
> +            return AVERROR(EINVAL);
> +        }
> +    }
> +    return 0;
> +}
> +
> +
> +static int filter_frame(AVFilterLink *inlink, AVFrame *in)
> +{
> +    AVFilterContext *ctx = inlink->dst;
> +    StabData *sd = ctx->priv;
> +    VSMotionDetect* md = &(sd->md);
> +    LocalMotions localmotions;
> +
> +    AVFilterLink *outlink = inlink->dst->outputs[0];
> +    int direct = 0;
> +    AVFrame *out;
> +    VSFrame frame;
> +    int plane;
> +
> +    if (av_frame_is_writable(in)) {
> +        direct = 1;
> +        out = in;
> +    } else {
> +        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(plane=0; plane < md->fi.planes; plane++){
> +        frame.data[plane] = in->data[plane];
> +        frame.linesize[plane] = in->linesize[plane];
> +    }
> +    if(vsMotionDetection(md, &localmotions, &frame) !=  VS_OK){
> +        av_log(ctx, AV_LOG_ERROR, "motion detection failed");
> +        return AVERROR(AVERROR_EXTERNAL);
> +    } else {
> +        if(vsWriteToFile(md, sd->f, &localmotions) != VS_OK){

> +            av_log(ctx, AV_LOG_ERROR, "cannot write to transform file!");

The exclamation mark is too violent.

> +            return AVERROR(errno);
> +        }
> +        vs_vector_del(&localmotions);
> +    }
> +    if(md->show>0 && !direct){

> +        // copy

We can guess from the next line that "av_image_copy()" is a copy.

> +        av_image_copy(out->data, out->linesize,
> +                      (void*)in->data, in->linesize,
> +                      in->format, in->width, in->height);
> +    }
> +
> +    if (!direct)
> +        av_frame_free(&in);
> +
> +    return ff_filter_frame(outlink, out);
> +}
> +
> +static const AVFilterPad avfilter_vf_vidstabdetect_inputs[] = {
> +    {
> +        .name             = "default",
> +        .type             = AVMEDIA_TYPE_VIDEO,
> +        .filter_frame     = filter_frame,
> +        .config_props     = config_input,
> +    },
> +    { NULL }
> +};
> +
> +static const AVFilterPad avfilter_vf_vidstabdetect_outputs[] = {
> +    {
> +        .name             = "default",
> +        .type             = AVMEDIA_TYPE_VIDEO,
> +    },
> +    { NULL }
> +};
> +
> +AVFilter avfilter_vf_vidstabdetect = {
> +    .name          = "vidstabdetect",
> +    .description   = NULL_IF_CONFIG_SMALL("pass 1 of 2 for stabilization"
> +                                          "extracts relative transformations"
> +                                          "(pass 2 see vidstabtransform)"),
> +    .priv_size     = sizeof(StabData),
> +    .init          = init,
> +    .uninit        = uninit,
> +    .query_formats = query_formats,
> +
> +    .inputs        = avfilter_vf_vidstabdetect_inputs,
> +    .outputs       = avfilter_vf_vidstabdetect_outputs,
> +    .priv_class    = &vidstabdetect_class,
> +};

Most of the comments above apply to vf_vidstabtransform.c as well.

> diff --git a/libavfilter/vf_vidstabtransform.c b/libavfilter/vf_vidstabtransform.c
[...]
> +
> +/** convert AV's pixelformat to vid.stab pixelformat */
> +static VSPixelFormat av_2_vs_pixel_format(AVFilterContext *ctx, enum AVPixelFormat pf){
> +    switch(pf){
> +    case AV_PIX_FMT_YUV420P:  return PF_YUV420P;
> +    case AV_PIX_FMT_YUV422P:  return PF_YUV422P;
> +    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;
> +    }
> +}
> +
> +
> +/// struct to hold a valid context for logging from within vid.stab lib
> +typedef struct {
> +    const AVClass* class;
> +} TransformLogCtx;
> +
> +/** wrapper to log vs_log into av_log */
> +static int av_log_wrapper(int type, const char* tag, const char* format, ...){
> +    va_list ap;
> +    TransformLogCtx ctx;
> +    ctx.class = &vidstabtransform_class;
> +    av_log(&ctx,  type, "%s: ", tag);
> +    va_start (ap, format);
> +    av_vlog(&ctx, type, format, ap);
> +    va_end (ap);
> +    return VS_OK;
> +}
> +
> +/** sets the memory allocation function and logging constants to av versions */
> +static void set_mem_and_log_functions(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;
> +}
> +

Could it make sense to factorize all these 3 functions in a
libavfilter/vidstabutils.c or similar?

[...]

-- 
Clément B.
-------------- next part --------------
A non-text attachment was scrubbed...
Name: not available
Type: application/pgp-signature
Size: 490 bytes
Desc: not available
URL: <http://ffmpeg.org/pipermail/ffmpeg-devel/attachments/20130409/76c4b49d/attachment.asc>


More information about the ffmpeg-devel mailing list