Browse code

vf_fade: switch to an AVOptions-based system.

Anton Khirnov authored on 2013/02/26 05:21:29
Showing 2 changed files
... ...
@@ -1000,33 +1000,37 @@ For more information about libfreetype, check:
1000 1000
 
1001 1001
 Apply fade-in/out effect to input video.
1002 1002
 
1003
-It accepts the parameters:
1004
-@var{type}:@var{start_frame}:@var{nb_frames}
1003
+This filter accepts the following options:
1004
+
1005
+@table @option
1005 1006
 
1006
-@var{type} specifies if the effect type, can be either "in" for
1007
-fade-in, or "out" for a fade-out effect.
1007
+@item type
1008
+The effect type -- can be either "in" for fade-in, or "out" for a fade-out
1009
+effect.
1010
+
1011
+@item start_frame
1012
+The number of the start frame for starting to apply the fade effect.
1008 1013
 
1009
-@var{start_frame} specifies the number of the start frame for starting
1010
-to apply the fade effect.
1014
+@item nb_frames
1015
+The number of frames for which the fade effect has to last. At the end of the
1016
+fade-in effect the output video will have the same intensity as the input video,
1017
+at the end of the fade-out transition the output video will be completely black.
1011 1018
 
1012
-@var{nb_frames} specifies the number of frames for which the fade
1013
-effect has to last. At the end of the fade-in effect the output video
1014
-will have the same intensity as the input video, at the end of the
1015
-fade-out transition the output video will be completely black.
1019
+@end table
1016 1020
 
1017 1021
 A few usage examples follow, usable too as test scenarios.
1018 1022
 @example
1019 1023
 # fade in first 30 frames of video
1020
-fade=in:0:30
1024
+fade=type=in:nb_frames=30
1021 1025
 
1022 1026
 # fade out last 45 frames of a 200-frame video
1023
-fade=out:155:45
1027
+fade=type=out:start_frame=155:nb_frames=45
1024 1028
 
1025 1029
 # fade in first 25 frames and fade out last 25 frames of a 1000-frame video
1026
-fade=in:0:25, fade=out:975:25
1030
+fade=type=in:start_frame=0:nb_frames=25, fade=type=out:start_frame=975:nb_frames=25
1027 1031
 
1028 1032
 # make first 5 frames black, then fade in from frame 5-24
1029
-fade=in:5:20
1033
+fade=type=in:start_frame=5:nb_frames=20
1030 1034
 @end example
1031 1035
 
1032 1036
 @section fieldorder
... ...
@@ -26,48 +26,42 @@
26 26
  */
27 27
 
28 28
 #include "libavutil/common.h"
29
+#include "libavutil/opt.h"
29 30
 #include "libavutil/pixdesc.h"
30 31
 #include "avfilter.h"
31 32
 #include "formats.h"
32 33
 #include "internal.h"
33 34
 #include "video.h"
34 35
 
36
+#define FADE_IN  0
37
+#define FADE_OUT 1
38
+
35 39
 typedef struct {
40
+    const AVClass *class;
41
+    int type;
36 42
     int factor, fade_per_frame;
37
-    unsigned int frame_index, start_frame, stop_frame;
43
+    int start_frame, nb_frames;
44
+    unsigned int frame_index, stop_frame;
38 45
     int hsub, vsub, bpp;
39 46
 } FadeContext;
40 47
 
41 48
 static av_cold int init(AVFilterContext *ctx, const char *args)
42 49
 {
43 50
     FadeContext *fade = ctx->priv;
44
-    unsigned int nb_frames;
45
-    char in_out[4];
46
-
47
-    if (!args ||
48
-        sscanf(args, " %3[^:]:%u:%u", in_out, &fade->start_frame, &nb_frames) != 3) {
49
-        av_log(ctx, AV_LOG_ERROR,
50
-               "Expected 3 arguments '(in|out):#:#':'%s'\n", args);
51
-        return AVERROR(EINVAL);
52
-    }
53 51
 
54
-    nb_frames = nb_frames ? nb_frames : 1;
55
-    fade->fade_per_frame = (1 << 16) / nb_frames;
56
-    if (!strcmp(in_out, "in"))
52
+    fade->fade_per_frame = (1 << 16) / fade->nb_frames;
53
+    if (fade->type == FADE_IN) {
57 54
         fade->factor = 0;
58
-    else if (!strcmp(in_out, "out")) {
55
+    } else if (fade->type == FADE_OUT) {
59 56
         fade->fade_per_frame = -fade->fade_per_frame;
60 57
         fade->factor = (1 << 16);
61
-    } else {
62
-        av_log(ctx, AV_LOG_ERROR,
63
-               "first argument must be 'in' or 'out':'%s'\n", in_out);
64
-        return AVERROR(EINVAL);
65 58
     }
66
-    fade->stop_frame = fade->start_frame + nb_frames;
59
+    fade->stop_frame = fade->start_frame + fade->nb_frames;
67 60
 
68 61
     av_log(ctx, AV_LOG_VERBOSE,
69 62
            "type:%s start_frame:%d nb_frames:%d\n",
70
-           in_out, fade->start_frame, nb_frames);
63
+           fade->type == FADE_IN ? "in" : "out", fade->start_frame,
64
+           fade->nb_frames);
71 65
     return 0;
72 66
 }
73 67
 
... ...
@@ -143,6 +137,26 @@ static int filter_frame(AVFilterLink *inlink, AVFrame *frame)
143 143
     return ff_filter_frame(inlink->dst->outputs[0], frame);
144 144
 }
145 145
 
146
+#define OFFSET(x) offsetof(FadeContext, x)
147
+#define FLAGS AV_OPT_FLAG_VIDEO_PARAM
148
+static const AVOption options[] = {
149
+    { "type", "'in' or 'out' for fade-in/fade-out", OFFSET(type), AV_OPT_TYPE_INT, { .i64 = FADE_IN }, FADE_IN, FADE_OUT, FLAGS, "type" },
150
+        { "in",  "fade-in",  0, AV_OPT_TYPE_CONST, { .i64 = FADE_IN },  .unit = "type" },
151
+        { "out", "fade-out", 0, AV_OPT_TYPE_CONST, { .i64 = FADE_OUT }, .unit = "type" },
152
+    { "start_frame", "Number of the first frame to which to apply the effect.",
153
+                                                    OFFSET(start_frame), AV_OPT_TYPE_INT, { .i64 = 0 }, 0, INT_MAX, FLAGS },
154
+    { "nb_frames",   "Number of frames to which the effect should be applied.",
155
+                                                    OFFSET(nb_frames),   AV_OPT_TYPE_INT, { .i64 = 1 }, 0, INT_MAX, FLAGS },
156
+    { NULL },
157
+};
158
+
159
+static const AVClass fade_class = {
160
+    .class_name = "fade",
161
+    .item_name  = av_default_item_name,
162
+    .option     = options,
163
+    .version    = LIBAVUTIL_VERSION_INT,
164
+};
165
+
146 166
 static const AVFilterPad avfilter_vf_fade_inputs[] = {
147 167
     {
148 168
         .name             = "default",
... ...
@@ -168,6 +182,7 @@ AVFilter avfilter_vf_fade = {
168 168
     .description   = NULL_IF_CONFIG_SMALL("Fade in/out input video"),
169 169
     .init          = init,
170 170
     .priv_size     = sizeof(FadeContext),
171
+    .priv_class    = &fade_class,
171 172
     .query_formats = query_formats,
172 173
 
173 174
     .inputs    = avfilter_vf_fade_inputs,