[FFmpeg-cvslog] vf_fade: switch to an AVOptions-based system.

Anton Khirnov git at videolan.org
Wed Apr 10 18:53:37 CEST 2013


ffmpeg | branch: master | Anton Khirnov <anton at khirnov.net> | Mon Feb 25 21:21:29 2013 +0100| [b9dfee9fa259dfc885508179a359dccc9e7840bd] | committer: Anton Khirnov

vf_fade: switch to an AVOptions-based system.

> http://git.videolan.org/gitweb.cgi/ffmpeg.git/?a=commit;h=b9dfee9fa259dfc885508179a359dccc9e7840bd
---

 doc/filters.texi      |   32 +++++++++++++++-------------
 libavfilter/vf_fade.c |   55 +++++++++++++++++++++++++++++++------------------
 2 files changed, 53 insertions(+), 34 deletions(-)

diff --git a/doc/filters.texi b/doc/filters.texi
index 27b6093..0cbbe6b 100644
--- a/doc/filters.texi
+++ b/doc/filters.texi
@@ -1000,33 +1000,37 @@ For more information about libfreetype, check:
 
 Apply fade-in/out effect to input video.
 
-It accepts the parameters:
- at var{type}:@var{start_frame}:@var{nb_frames}
+This filter accepts the following options:
+
+ at table @option
 
- at var{type} specifies if the effect type, can be either "in" for
-fade-in, or "out" for a fade-out effect.
+ at item type
+The effect type -- can be either "in" for fade-in, or "out" for a fade-out
+effect.
+
+ at item start_frame
+The number of the start frame for starting to apply the fade effect.
 
- at var{start_frame} specifies the number of the start frame for starting
-to apply the fade effect.
+ at item nb_frames
+The number of frames for which the fade effect has to last. At the end of the
+fade-in effect the output video will have the same intensity as the input video,
+at the end of the fade-out transition the output video will be completely black.
 
- at var{nb_frames} specifies the number of frames for which the fade
-effect has to last. At the end of the fade-in effect the output video
-will have the same intensity as the input video, at the end of the
-fade-out transition the output video will be completely black.
+ at end table
 
 A few usage examples follow, usable too as test scenarios.
 @example
 # fade in first 30 frames of video
-fade=in:0:30
+fade=type=in:nb_frames=30
 
 # fade out last 45 frames of a 200-frame video
-fade=out:155:45
+fade=type=out:start_frame=155:nb_frames=45
 
 # fade in first 25 frames and fade out last 25 frames of a 1000-frame video
-fade=in:0:25, fade=out:975:25
+fade=type=in:start_frame=0:nb_frames=25, fade=type=out:start_frame=975:nb_frames=25
 
 # make first 5 frames black, then fade in from frame 5-24
-fade=in:5:20
+fade=type=in:start_frame=5:nb_frames=20
 @end example
 
 @section fieldorder
diff --git a/libavfilter/vf_fade.c b/libavfilter/vf_fade.c
index f537404..5839c6c 100644
--- a/libavfilter/vf_fade.c
+++ b/libavfilter/vf_fade.c
@@ -26,48 +26,42 @@
  */
 
 #include "libavutil/common.h"
+#include "libavutil/opt.h"
 #include "libavutil/pixdesc.h"
 #include "avfilter.h"
 #include "formats.h"
 #include "internal.h"
 #include "video.h"
 
+#define FADE_IN  0
+#define FADE_OUT 1
+
 typedef struct {
+    const AVClass *class;
+    int type;
     int factor, fade_per_frame;
-    unsigned int frame_index, start_frame, stop_frame;
+    int start_frame, nb_frames;
+    unsigned int frame_index, stop_frame;
     int hsub, vsub, bpp;
 } FadeContext;
 
 static av_cold int init(AVFilterContext *ctx, const char *args)
 {
     FadeContext *fade = ctx->priv;
-    unsigned int nb_frames;
-    char in_out[4];
-
-    if (!args ||
-        sscanf(args, " %3[^:]:%u:%u", in_out, &fade->start_frame, &nb_frames) != 3) {
-        av_log(ctx, AV_LOG_ERROR,
-               "Expected 3 arguments '(in|out):#:#':'%s'\n", args);
-        return AVERROR(EINVAL);
-    }
 
-    nb_frames = nb_frames ? nb_frames : 1;
-    fade->fade_per_frame = (1 << 16) / nb_frames;
-    if (!strcmp(in_out, "in"))
+    fade->fade_per_frame = (1 << 16) / fade->nb_frames;
+    if (fade->type == FADE_IN) {
         fade->factor = 0;
-    else if (!strcmp(in_out, "out")) {
+    } else if (fade->type == FADE_OUT) {
         fade->fade_per_frame = -fade->fade_per_frame;
         fade->factor = (1 << 16);
-    } else {
-        av_log(ctx, AV_LOG_ERROR,
-               "first argument must be 'in' or 'out':'%s'\n", in_out);
-        return AVERROR(EINVAL);
     }
-    fade->stop_frame = fade->start_frame + nb_frames;
+    fade->stop_frame = fade->start_frame + fade->nb_frames;
 
     av_log(ctx, AV_LOG_VERBOSE,
            "type:%s start_frame:%d nb_frames:%d\n",
-           in_out, fade->start_frame, nb_frames);
+           fade->type == FADE_IN ? "in" : "out", fade->start_frame,
+           fade->nb_frames);
     return 0;
 }
 
@@ -143,6 +137,26 @@ static int filter_frame(AVFilterLink *inlink, AVFrame *frame)
     return ff_filter_frame(inlink->dst->outputs[0], frame);
 }
 
+#define OFFSET(x) offsetof(FadeContext, x)
+#define FLAGS AV_OPT_FLAG_VIDEO_PARAM
+static const AVOption options[] = {
+    { "type", "'in' or 'out' for fade-in/fade-out", OFFSET(type), AV_OPT_TYPE_INT, { .i64 = FADE_IN }, FADE_IN, FADE_OUT, FLAGS, "type" },
+        { "in",  "fade-in",  0, AV_OPT_TYPE_CONST, { .i64 = FADE_IN },  .unit = "type" },
+        { "out", "fade-out", 0, AV_OPT_TYPE_CONST, { .i64 = FADE_OUT }, .unit = "type" },
+    { "start_frame", "Number of the first frame to which to apply the effect.",
+                                                    OFFSET(start_frame), AV_OPT_TYPE_INT, { .i64 = 0 }, 0, INT_MAX, FLAGS },
+    { "nb_frames",   "Number of frames to which the effect should be applied.",
+                                                    OFFSET(nb_frames),   AV_OPT_TYPE_INT, { .i64 = 1 }, 0, INT_MAX, FLAGS },
+    { NULL },
+};
+
+static const AVClass fade_class = {
+    .class_name = "fade",
+    .item_name  = av_default_item_name,
+    .option     = options,
+    .version    = LIBAVUTIL_VERSION_INT,
+};
+
 static const AVFilterPad avfilter_vf_fade_inputs[] = {
     {
         .name             = "default",
@@ -168,6 +182,7 @@ AVFilter avfilter_vf_fade = {
     .description   = NULL_IF_CONFIG_SMALL("Fade in/out input video"),
     .init          = init,
     .priv_size     = sizeof(FadeContext),
+    .priv_class    = &fade_class,
     .query_formats = query_formats,
 
     .inputs    = avfilter_vf_fade_inputs,



More information about the ffmpeg-cvslog mailing list