Browse code

avfilter: add shuffleframes filter

Signed-off-by: Paul B Mahol <onemda@gmail.com>

Paul B Mahol authored on 2015/10/20 17:26:54
Showing 6 changed files
... ...
@@ -24,6 +24,7 @@ version <next>:
24 24
 - zscale filter
25 25
 - wve demuxer
26 26
 - zero-copy Intel QSV transcoding in ffmpeg
27
+- shuffleframes filter
27 28
 
28 29
 
29 30
 version 2.8:
... ...
@@ -9946,6 +9946,26 @@ Set the size of the box used to represent one palette color entry. Default is
9946 9946
 @code{30} (for a @code{30x30} pixel box).
9947 9947
 @end table
9948 9948
 
9949
+@section shuffleframes
9950
+
9951
+Reorder and/or duplicate video frames.
9952
+
9953
+It accepts the following parameters:
9954
+
9955
+@table @option
9956
+@item mapping
9957
+Set the destination indexes of input frames.
9958
+This is space or '|' separated list of indexes that maps input frames to output
9959
+frames. Number of indexes also sets maximal value that each index may have.
9960
+@end table
9961
+
9962
+The first frame has the index 0. The default is to keep the input unchanged.
9963
+
9964
+Swap second and third frame of every three frames of the input:
9965
+@example
9966
+ffmpeg -i INPUT -vf "shuffleframes=0 2 1" OUTPUT
9967
+@end example
9968
+
9949 9969
 @section shuffleplanes
9950 9970
 
9951 9971
 Reorder and/or duplicate video planes.
... ...
@@ -215,6 +215,7 @@ OBJS-$(CONFIG_SETSAR_FILTER)                 += vf_aspect.o
215 215
 OBJS-$(CONFIG_SETTB_FILTER)                  += settb.o
216 216
 OBJS-$(CONFIG_SHOWINFO_FILTER)               += vf_showinfo.o
217 217
 OBJS-$(CONFIG_SHOWPALETTE_FILTER)            += vf_showpalette.o
218
+OBJS-$(CONFIG_SHUFFLEFRAMES_FILTER)          += vf_shuffleframes.o
218 219
 OBJS-$(CONFIG_SHUFFLEPLANES_FILTER)          += vf_shuffleplanes.o
219 220
 OBJS-$(CONFIG_SIGNALSTATS_FILTER)            += vf_signalstats.o
220 221
 OBJS-$(CONFIG_SMARTBLUR_FILTER)              += vf_smartblur.o
... ...
@@ -236,6 +236,7 @@ void avfilter_register_all(void)
236 236
     REGISTER_FILTER(SETTB,          settb,          vf);
237 237
     REGISTER_FILTER(SHOWINFO,       showinfo,       vf);
238 238
     REGISTER_FILTER(SHOWPALETTE,    showpalette,    vf);
239
+    REGISTER_FILTER(SHUFFLEFRAMES,  shuffleframes,  vf);
239 240
     REGISTER_FILTER(SHUFFLEPLANES,  shuffleplanes,  vf);
240 241
     REGISTER_FILTER(SIGNALSTATS,    signalstats,    vf);
241 242
     REGISTER_FILTER(SMARTBLUR,      smartblur,      vf);
... ...
@@ -30,7 +30,7 @@
30 30
 #include "libavutil/version.h"
31 31
 
32 32
 #define LIBAVFILTER_VERSION_MAJOR   6
33
-#define LIBAVFILTER_VERSION_MINOR  12
33
+#define LIBAVFILTER_VERSION_MINOR  13
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,162 @@
0
+/*
1
+ * Copyright (c) 2015 Paul B Mahol
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/avstring.h"
21
+#include "libavutil/common.h"
22
+#include "libavutil/internal.h"
23
+#include "libavutil/opt.h"
24
+
25
+#include "avfilter.h"
26
+#include "internal.h"
27
+#include "video.h"
28
+
29
+typedef struct ShuffleFramesContext {
30
+    const AVClass *class;
31
+    char *mapping;
32
+    AVFrame **frames;
33
+    int *map;
34
+    int64_t *pts;
35
+    int in_frames;
36
+    int nb_frames;
37
+} ShuffleFramesContext;
38
+
39
+static av_cold int init(AVFilterContext *ctx)
40
+{
41
+    ShuffleFramesContext *s = ctx->priv;
42
+    char *mapping, *saveptr = NULL, *p;
43
+    int n, nb_items;
44
+
45
+    nb_items = 1;
46
+    for (p = s->mapping; *p; p++) {
47
+        if (*p == '|' || *p == ' ')
48
+            nb_items++;
49
+    }
50
+
51
+    s->frames = av_calloc(nb_items, sizeof(*s->frames));
52
+    s->map    = av_calloc(nb_items, sizeof(*s->map));
53
+    s->pts    = av_calloc(nb_items, sizeof(*s->pts));
54
+    if (!s->map || !s->frames || !s->pts) {
55
+        return AVERROR(ENOMEM);
56
+    }
57
+
58
+    mapping = av_strdup(s->mapping);
59
+    if (!mapping)
60
+        return AVERROR(ENOMEM);
61
+
62
+    for (n = 0; n < nb_items; n++) {
63
+        char *map = av_strtok(n == 0 ? mapping : NULL, " |", &saveptr);
64
+        if (!map || sscanf(map, "%d", &s->map[n]) != 1) {
65
+            av_free(mapping);
66
+            return AVERROR(EINVAL);
67
+        }
68
+
69
+        if (s->map[n] < 0 || s->map[n] >= nb_items) {
70
+            av_log(ctx, AV_LOG_ERROR, "Index out of range.\n");
71
+            av_free(mapping);
72
+            return AVERROR(EINVAL);
73
+        }
74
+    }
75
+
76
+    s->nb_frames = nb_items;
77
+    av_free(mapping);
78
+    return 0;
79
+}
80
+
81
+static int filter_frame(AVFilterLink *inlink, AVFrame *frame)
82
+{
83
+    AVFilterContext    *ctx = inlink->dst;
84
+    ShuffleFramesContext *s = ctx->priv;
85
+    int ret;
86
+
87
+    if (s->in_frames < s->nb_frames) {
88
+        s->frames[s->in_frames] = frame;
89
+        s->pts[s->in_frames] = frame->pts;
90
+        s->in_frames++;
91
+        ret = 0;
92
+    }
93
+
94
+    if (s->in_frames == s->nb_frames) {
95
+        int n, x;
96
+
97
+        for (n = 0; n < s->nb_frames; n++) {
98
+            AVFrame *out;
99
+
100
+            x = s->map[n];
101
+            out = av_frame_clone(s->frames[x]);
102
+            if (!out)
103
+                return AVERROR(ENOMEM);
104
+            out->pts = s->pts[n];
105
+            ret = ff_filter_frame(ctx->outputs[0], out);
106
+            s->in_frames--;
107
+        }
108
+
109
+        for (n = 0; n < s->nb_frames; n++)
110
+            av_frame_free(&s->frames[n]);
111
+    }
112
+
113
+    return ret;
114
+}
115
+
116
+static av_cold void uninit(AVFilterContext *ctx)
117
+{
118
+    ShuffleFramesContext *s = ctx->priv;
119
+
120
+    av_freep(&s->frames);
121
+    av_freep(&s->map);
122
+    av_freep(&s->pts);
123
+}
124
+
125
+#define OFFSET(x) offsetof(ShuffleFramesContext, x)
126
+#define FLAGS (AV_OPT_FLAG_FILTERING_PARAM | AV_OPT_FLAG_VIDEO_PARAM)
127
+static const AVOption shuffleframes_options[] = {
128
+    { "mapping", "set destination indexes of input frames",  OFFSET(mapping), AV_OPT_TYPE_STRING, {.str="0"}, 0, 0, FLAGS },
129
+    { NULL },
130
+};
131
+
132
+AVFILTER_DEFINE_CLASS(shuffleframes);
133
+
134
+static const AVFilterPad shuffleframes_inputs[] = {
135
+    {
136
+        .name         = "default",
137
+        .type         = AVMEDIA_TYPE_VIDEO,
138
+        .filter_frame = filter_frame,
139
+    },
140
+    { NULL },
141
+};
142
+
143
+static const AVFilterPad shuffleframes_outputs[] = {
144
+    {
145
+        .name = "default",
146
+        .type = AVMEDIA_TYPE_VIDEO,
147
+    },
148
+    { NULL },
149
+};
150
+
151
+AVFilter ff_vf_shuffleframes = {
152
+    .name          = "shuffleframes",
153
+    .description   = NULL_IF_CONFIG_SMALL("Shuffle video frames"),
154
+    .priv_size     = sizeof(ShuffleFramesContext),
155
+    .priv_class    = &shuffleframes_class,
156
+    .init          = init,
157
+    .uninit        = uninit,
158
+    .inputs        = shuffleframes_inputs,
159
+    .outputs       = shuffleframes_outputs,
160
+    .flags         = AVFILTER_FLAG_SUPPORT_TIMELINE_GENERIC,
161
+};