Browse code

lavfi: add framestep filter

This filter is inspired upon libmpcodecs/vf_framestep.c, by Daniele
Forghieri.

Only-keyframe output is not supported, since that feature can be achieved
through the more versatile select filter.

Stefano Sabatini authored on 2012/08/17 02:04:26
Showing 6 changed files
... ...
@@ -48,6 +48,7 @@ version next:
48 48
 - ICO muxer
49 49
 - SubRip encoder and decoder without embedded timing
50 50
 - edge detection filter
51
+- framestep filter
51 52
 
52 53
 
53 54
 version 0.11:
... ...
@@ -2084,6 +2084,13 @@ Desired output framerate.
2084 2084
 
2085 2085
 @end table
2086 2086
 
2087
+@section framestep
2088
+
2089
+Select one frame every N.
2090
+
2091
+This filter accepts in input a string representing a positive
2092
+integer. Default argument is @code{1}.
2093
+
2087 2094
 @anchor{frei0r}
2088 2095
 @section frei0r
2089 2096
 
... ...
@@ -95,6 +95,7 @@ OBJS-$(CONFIG_FADE_FILTER)                   += vf_fade.o
95 95
 OBJS-$(CONFIG_FIELDORDER_FILTER)             += vf_fieldorder.o
96 96
 OBJS-$(CONFIG_FIFO_FILTER)                   += fifo.o
97 97
 OBJS-$(CONFIG_FORMAT_FILTER)                 += vf_format.o
98
+OBJS-$(CONFIG_FRAMESTEP_FILTER)              += vf_framestep.o
98 99
 OBJS-$(CONFIG_FPS_FILTER)                    += vf_fps.o
99 100
 OBJS-$(CONFIG_FREI0R_FILTER)                 += vf_frei0r.o
100 101
 OBJS-$(CONFIG_GRADFUN_FILTER)                += vf_gradfun.o
... ...
@@ -87,6 +87,7 @@ void avfilter_register_all(void)
87 87
     REGISTER_FILTER (FIFO,        fifo,        vf);
88 88
     REGISTER_FILTER (FORMAT,      format,      vf);
89 89
     REGISTER_FILTER (FPS,         fps,         vf);
90
+    REGISTER_FILTER (FRAMESTEP,   framestep,   vf);
90 91
     REGISTER_FILTER (FREI0R,      frei0r,      vf);
91 92
     REGISTER_FILTER (GRADFUN,     gradfun,     vf);
92 93
     REGISTER_FILTER (HFLIP,       hflip,       vf);
... ...
@@ -29,8 +29,8 @@
29 29
 #include "libavutil/avutil.h"
30 30
 
31 31
 #define LIBAVFILTER_VERSION_MAJOR  3
32
-#define LIBAVFILTER_VERSION_MINOR  10
33
-#define LIBAVFILTER_VERSION_MICRO 101
32
+#define LIBAVFILTER_VERSION_MINOR  11
33
+#define LIBAVFILTER_VERSION_MICRO 100
34 34
 
35 35
 #define LIBAVFILTER_VERSION_INT AV_VERSION_INT(LIBAVFILTER_VERSION_MAJOR, \
36 36
                                                LIBAVFILTER_VERSION_MINOR, \
37 37
new file mode 100644
... ...
@@ -0,0 +1,142 @@
0
+/*
1
+ * Copyright (c) 2012 Stefano Sabatini
2
+ *
3
+ * This file is part of FFmpeg.
4
+ *
5
+ * FFmpeg is free software; you can redistribute it and/or
6
+ * modify it under the terms of the GNU Lesser General Public
7
+ * License as published by the Free Software Foundation; either
8
+ * version 2.1 of the License, or (at your option) any later version.
9
+ *
10
+ * FFmpeg is distributed in the hope that it will be useful,
11
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
12
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
13
+ * Lesser General Public License for more details.
14
+ *
15
+ * You should have received a copy of the GNU Lesser General Public
16
+ * License along with FFmpeg; if not, write to the Free Software
17
+ * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
18
+ */
19
+
20
+/**
21
+ * @file framestep filter, inspired on libmpcodecs/vf_framestep.c by
22
+ * Daniele Fornighieri <guru AT digitalfantasy it>.
23
+ */
24
+
25
+#include "avfilter.h"
26
+#include "internal.h"
27
+#include "video.h"
28
+
29
+typedef struct {
30
+    int frame_step, frame_count, frame_selected;
31
+} FrameStepContext;
32
+
33
+static av_cold int init(AVFilterContext *ctx, const char *args)
34
+{
35
+    FrameStepContext *framestep = ctx->priv;
36
+    char *tailptr;
37
+    long int n;
38
+
39
+    framestep->frame_step = 1;
40
+
41
+    if (args) {
42
+        n = strtol(args, &tailptr, 10);
43
+        if (*tailptr || n <= 0 || n >= INT_MAX) {
44
+            av_log(ctx, AV_LOG_ERROR,
45
+                   "Invalid argument '%s', must be a positive integer <= INT_MAX\n", args);
46
+            return AVERROR(EINVAL);
47
+        }
48
+    }
49
+
50
+    framestep->frame_step = n;
51
+    return 0;
52
+}
53
+
54
+static int config_output_props(AVFilterLink *outlink)
55
+{
56
+    AVFilterContext *ctx = outlink->src;
57
+    FrameStepContext *framestep = ctx->priv;
58
+    AVFilterLink *inlink = ctx->inputs[0];
59
+
60
+    outlink->frame_rate =
61
+        av_div_q(inlink->frame_rate, (AVRational){framestep->frame_step, 1});
62
+
63
+    av_log(ctx, AV_LOG_VERBOSE, "step:%d frame_rate:%d/%d(%f) -> frame_rate:%d/%d(%f)\n",
64
+           framestep->frame_step,
65
+           inlink->frame_rate.num, inlink->frame_rate.den, av_q2d(inlink->frame_rate),
66
+           outlink->frame_rate.num, outlink->frame_rate.den, av_q2d(outlink->frame_rate));
67
+    return 0;
68
+}
69
+
70
+static int start_frame(AVFilterLink *inlink, AVFilterBufferRef *ref)
71
+{
72
+    FrameStepContext *framestep = inlink->dst->priv;
73
+
74
+    framestep->frame_selected = 0;
75
+    if (!(framestep->frame_count++ % framestep->frame_step)) {
76
+        inlink->cur_buf = NULL;
77
+        framestep->frame_selected = 1;
78
+        return ff_start_frame(inlink->dst->outputs[0], ref);
79
+    }
80
+    return 0;
81
+}
82
+
83
+static int draw_slice(AVFilterLink *inlink, int y, int h, int slice_dir)
84
+{
85
+    FrameStepContext *framestep = inlink->dst->priv;
86
+
87
+    if (framestep->frame_selected)
88
+        return ff_draw_slice(inlink->dst->outputs[0], y, h, slice_dir);
89
+    return 0;
90
+}
91
+
92
+static int end_frame(AVFilterLink *inlink)
93
+{
94
+    FrameStepContext *framestep = inlink->dst->priv;
95
+
96
+    if (framestep->frame_selected)
97
+        return ff_end_frame(inlink->dst->outputs[0]);
98
+    return 0;
99
+}
100
+
101
+static int request_frame(AVFilterLink *outlink)
102
+{
103
+    FrameStepContext *framestep = outlink->src->priv;
104
+    AVFilterLink *inlink = outlink->src->inputs[0];
105
+    int ret;
106
+
107
+    framestep->frame_selected = 0;
108
+    do {
109
+        ret = ff_request_frame(inlink);
110
+    } while (!framestep->frame_selected && ret >= 0);
111
+
112
+    return ret;
113
+}
114
+
115
+AVFilter avfilter_vf_framestep = {
116
+    .name      = "framestep",
117
+    .description = NULL_IF_CONFIG_SMALL("Select one frame every N frames."),
118
+    .init      = init,
119
+    .priv_size = sizeof(FrameStepContext),
120
+
121
+    .inputs = (const AVFilterPad[]) {
122
+        {
123
+            .name             = "default",
124
+            .type             = AVMEDIA_TYPE_VIDEO,
125
+            .get_video_buffer = ff_null_get_video_buffer,
126
+            .start_frame      = start_frame,
127
+            .draw_slice       = draw_slice,
128
+            .end_frame        = end_frame,
129
+        },
130
+        { .name = NULL }
131
+    },
132
+    .outputs = (const AVFilterPad[]) {
133
+        {
134
+            .name             = "default",
135
+            .type             = AVMEDIA_TYPE_VIDEO,
136
+            .config_props     = config_output_props,
137
+            .request_frame    = request_frame,
138
+        },
139
+        { .name = NULL }
140
+    },
141
+};