Browse code

avfilter: Add reverse filter

Signed-off-by: Derek Buitenhuis <derek.buitenhuis@gmail.com>

Derek Buitenhuis authored on 2015/07/17 02:43:59
Showing 6 changed files
... ...
@@ -18,6 +18,7 @@ version <next>:
18 18
 - libkvazaar HEVC encoder
19 19
 - erosion, dilation, deflate and inflate video filters
20 20
 - Dynamic Audio Normalizer as dynaudnorm filter
21
+- Reverse filter
21 22
 
22 23
 
23 24
 version 2.7:
... ...
@@ -8418,6 +8418,22 @@ pixels will slow things down on a large logo.
8418 8418
 This filter uses the repeat_field flag from the Video ES headers and hard repeats
8419 8419
 fields based on its value.
8420 8420
 
8421
+@section reverse
8422
+
8423
+Reverse a clip.
8424
+
8425
+Warning: This iflter qequires memory to buffer the entire clip, so trimming is suggested.
8426
+
8427
+@subsection Examples
8428
+
8429
+@itemize
8430
+@item
8431
+Take the first 5 seconds of a clip, and reverse it.
8432
+@example
8433
+trim=end=5,reverse
8434
+@end example
8435
+@end itemize
8436
+
8421 8437
 @section rotate
8422 8438
 
8423 8439
 Rotate video by an arbitrary angle expressed in radians.
... ...
@@ -186,6 +186,7 @@ OBJS-$(CONFIG_QP_FILTER)                     += vf_qp.o
186 186
 OBJS-$(CONFIG_REMOVEGRAIN_FILTER)            += vf_removegrain.o
187 187
 OBJS-$(CONFIG_REMOVELOGO_FILTER)             += bbox.o lswsutils.o lavfutils.o vf_removelogo.o
188 188
 OBJS-$(CONFIG_REPEATFIELDS_FILTER)           += vf_repeatfields.o
189
+OBJS-$(CONFIG_REVERSE_FILTER)                += vf_reverse.o
189 190
 OBJS-$(CONFIG_ROTATE_FILTER)                 += vf_rotate.o
190 191
 OBJS-$(CONFIG_SEPARATEFIELDS_FILTER)         += vf_separatefields.o
191 192
 OBJS-$(CONFIG_SAB_FILTER)                    += vf_sab.o
... ...
@@ -201,6 +201,7 @@ void avfilter_register_all(void)
201 201
     REGISTER_FILTER(REMOVEGRAIN,    removegrain,    vf);
202 202
     REGISTER_FILTER(REMOVELOGO,     removelogo,     vf);
203 203
     REGISTER_FILTER(REPEATFIELDS,   repeatfields,   vf);
204
+    REGISTER_FILTER(REVERSE,        reverse,        vf);
204 205
     REGISTER_FILTER(ROTATE,         rotate,         vf);
205 206
     REGISTER_FILTER(SAB,            sab,            vf);
206 207
     REGISTER_FILTER(SCALE,          scale,          vf);
... ...
@@ -30,7 +30,7 @@
30 30
 #include "libavutil/version.h"
31 31
 
32 32
 #define LIBAVFILTER_VERSION_MAJOR  5
33
-#define LIBAVFILTER_VERSION_MINOR  23
33
+#define LIBAVFILTER_VERSION_MINOR  24
34 34
 #define LIBAVFILTER_VERSION_MICRO 100
35 35
 
36 36
 #define LIBAVFILTER_VERSION_INT AV_VERSION_INT(LIBAVFILTER_VERSION_MAJOR, \
37 37
new file mode 100644
... ...
@@ -0,0 +1,142 @@
0
+/*
1
+ * Copyright (c) 2015 Derek Buitenhuis
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
+#include "libavutil/opt.h"
21
+#include "avfilter.h"
22
+#include "formats.h"
23
+#include "internal.h"
24
+#include "video.h"
25
+
26
+#define DEFAULT_LENGTH 300
27
+
28
+typedef struct ReverseContext {
29
+    int nb_frames;
30
+    AVFrame **frames;
31
+    unsigned int frames_size;
32
+    unsigned int pts_size;
33
+    int64_t *pts;
34
+    int flush_idx;
35
+} ReverseContext;
36
+
37
+static av_cold int init(AVFilterContext *ctx)
38
+{
39
+    ReverseContext *s = ctx->priv;
40
+
41
+    s->pts = av_fast_realloc(NULL, &s->pts_size,
42
+                             DEFAULT_LENGTH * sizeof(*(s->pts)));
43
+    if (!s->pts)
44
+        return AVERROR(ENOMEM);
45
+
46
+    s->frames = av_fast_realloc(NULL, &s->frames_size,
47
+                                DEFAULT_LENGTH * sizeof(*(s->frames)));
48
+    if (!s->frames) {
49
+        av_freep(&s->pts);
50
+        return AVERROR(ENOMEM);
51
+    }
52
+
53
+    return 0;
54
+}
55
+
56
+static av_cold void uninit(AVFilterContext *ctx)
57
+{
58
+    ReverseContext *s = ctx->priv;
59
+
60
+    av_freep(&s->pts);
61
+    av_freep(&s->frames);
62
+}
63
+
64
+static int config_output(AVFilterLink *outlink)
65
+{
66
+    outlink->flags |= FF_LINK_FLAG_REQUEST_LOOP;
67
+    return 0;
68
+}
69
+
70
+static int filter_frame(AVFilterLink *inlink, AVFrame *in)
71
+{
72
+    AVFilterContext *ctx = inlink->dst;
73
+    ReverseContext *s    = ctx->priv;
74
+
75
+    if (s->nb_frames + 1 > s->frames_size / sizeof(*(s->frames))) {
76
+        void *ptr;
77
+
78
+        ptr = av_fast_realloc(s->pts, &s->pts_size, s->pts_size * 2);
79
+        if (!ptr)
80
+            return AVERROR(ENOMEM);
81
+        s->pts = ptr;
82
+
83
+        ptr = av_fast_realloc(s->frames, &s->frames_size, s->frames_size * 2);
84
+        if (!ptr)
85
+            return AVERROR(ENOMEM);
86
+        s->frames = ptr;
87
+    }
88
+
89
+    s->frames[s->nb_frames] = in;
90
+    s->pts[s->nb_frames]    = in->pts;
91
+    s->nb_frames++;
92
+
93
+    return 0;
94
+}
95
+
96
+static int request_frame(AVFilterLink *outlink)
97
+{
98
+    AVFilterContext *ctx = outlink->src;
99
+    ReverseContext *s = ctx->priv;
100
+    int ret;
101
+
102
+    ret = ff_request_frame(ctx->inputs[0]);
103
+
104
+    if (ret == AVERROR_EOF && s->nb_frames > 0) {
105
+        AVFrame *out = s->frames[s->nb_frames - 1];
106
+        out->pts     = s->pts[s->flush_idx++];
107
+        ret          = ff_filter_frame(outlink, out);
108
+        s->nb_frames--;
109
+    }
110
+
111
+    return ret;
112
+}
113
+
114
+static const AVFilterPad reverse_inputs[] = {
115
+    {
116
+        .name         = "default",
117
+        .type         = AVMEDIA_TYPE_VIDEO,
118
+        .filter_frame = filter_frame,
119
+    },
120
+    { NULL }
121
+};
122
+
123
+static const AVFilterPad reverse_outputs[] = {
124
+    {
125
+        .name          = "default",
126
+        .type          = AVMEDIA_TYPE_VIDEO,
127
+        .request_frame = request_frame,
128
+        .config_props  = config_output,
129
+    },
130
+    { NULL }
131
+};
132
+
133
+AVFilter ff_vf_reverse = {
134
+    .name        = "reverse",
135
+    .description = NULL_IF_CONFIG_SMALL("Reverse a clip."),
136
+    .priv_size   = sizeof(ReverseContext),
137
+    .init        = init,
138
+    .uninit      = uninit,
139
+    .inputs      = reverse_inputs,
140
+    .outputs     = reverse_outputs,
141
+};