Browse code

avfilter: add random video filter

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

Paul B Mahol authored on 2015/07/15 10:48:37
Showing 6 changed files
... ...
@@ -19,6 +19,7 @@ version <next>:
19 19
 - erosion, dilation, deflate and inflate video filters
20 20
 - Dynamic Audio Normalizer as dynaudnorm filter
21 21
 - Reverse filter
22
+- Random filter
22 23
 
23 24
 
24 25
 version 2.7:
... ...
@@ -8279,6 +8279,24 @@ qp=2+2*sin(PI*qp)
8279 8279
 @end example
8280 8280
 @end itemize
8281 8281
 
8282
+@section random
8283
+
8284
+Flush video frames from internal cache of frames into a random order.
8285
+No frame is discarded.
8286
+Inspired by @ref{frei0r} nervous filter.
8287
+
8288
+@table @option
8289
+@item frames
8290
+Set size in number of frames of internal cache, in range from @code{2} to
8291
+@code{512}. Default is @code{30}.
8292
+
8293
+@item seed
8294
+Set seed for random number generator, must be an integer included between
8295
+@code{0} and @code{UINT32_MAX}. If not specified, or if explicitly set to
8296
+less than @code{0}, the filter will try to use a good random seed on a
8297
+best effort basis.
8298
+@end table
8299
+
8282 8300
 @section removegrain
8283 8301
 
8284 8302
 The removegrain filter is a spatial denoiser for progressive video.
... ...
@@ -183,6 +183,7 @@ OBJS-$(CONFIG_PP7_FILTER)                    += vf_pp7.o
183 183
 OBJS-$(CONFIG_PSNR_FILTER)                   += vf_psnr.o dualinput.o framesync.o
184 184
 OBJS-$(CONFIG_PULLUP_FILTER)                 += vf_pullup.o
185 185
 OBJS-$(CONFIG_QP_FILTER)                     += vf_qp.o
186
+OBJS-$(CONFIG_RANDOM_FILTER)                 += vf_random.o
186 187
 OBJS-$(CONFIG_REMOVEGRAIN_FILTER)            += vf_removegrain.o
187 188
 OBJS-$(CONFIG_REMOVELOGO_FILTER)             += bbox.o lswsutils.o lavfutils.o vf_removelogo.o
188 189
 OBJS-$(CONFIG_REPEATFIELDS_FILTER)           += vf_repeatfields.o
... ...
@@ -198,6 +198,7 @@ void avfilter_register_all(void)
198 198
     REGISTER_FILTER(PSNR,           psnr,           vf);
199 199
     REGISTER_FILTER(PULLUP,         pullup,         vf);
200 200
     REGISTER_FILTER(QP,             qp,             vf);
201
+    REGISTER_FILTER(RANDOM,         random,         vf);
201 202
     REGISTER_FILTER(REMOVEGRAIN,    removegrain,    vf);
202 203
     REGISTER_FILTER(REMOVELOGO,     removelogo,     vf);
203 204
     REGISTER_FILTER(REPEATFIELDS,   repeatfields,   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  24
33
+#define LIBAVFILTER_VERSION_MINOR  25
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,144 @@
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/lfg.h"
21
+#include "libavutil/opt.h"
22
+#include "libavutil/random_seed.h"
23
+#include "avfilter.h"
24
+#include "formats.h"
25
+#include "internal.h"
26
+#include "video.h"
27
+
28
+#define MAX_FRAMES 512
29
+
30
+typedef struct RandomContext {
31
+    const AVClass *class;
32
+
33
+    AVLFG lfg;
34
+    int nb_frames;
35
+    int64_t random_seed;
36
+    int nb_frames_filled;
37
+    AVFrame *frames[MAX_FRAMES];
38
+    int64_t pts[MAX_FRAMES];
39
+    int flush_idx;
40
+} RandomContext;
41
+
42
+#define OFFSET(x) offsetof(RandomContext, x)
43
+#define FLAGS AV_OPT_FLAG_VIDEO_PARAM|AV_OPT_FLAG_FILTERING_PARAM
44
+
45
+static const AVOption random_options[] = {
46
+    { "frames", "set number of frames in cache", OFFSET(nb_frames),   AV_OPT_TYPE_INT,   {.i64=30},  2, MAX_FRAMES, FLAGS },
47
+    { "seed",   "set the seed",                  OFFSET(random_seed), AV_OPT_TYPE_INT64, {.i64=-1}, -1, UINT32_MAX, FLAGS },
48
+    { NULL }
49
+};
50
+
51
+AVFILTER_DEFINE_CLASS(random);
52
+
53
+static av_cold int init(AVFilterContext *ctx)
54
+{
55
+    RandomContext *s = ctx->priv;
56
+    uint32_t seed;
57
+
58
+    if (s->random_seed < 0)
59
+        s->random_seed = av_get_random_seed();
60
+    seed = s->random_seed;
61
+    av_lfg_init(&s->lfg, seed);
62
+
63
+    return 0;
64
+}
65
+
66
+static int config_output(AVFilterLink *outlink)
67
+{
68
+    outlink->flags |= FF_LINK_FLAG_REQUEST_LOOP;
69
+    return 0;
70
+}
71
+
72
+static int filter_frame(AVFilterLink *inlink, AVFrame *in)
73
+{
74
+    AVFilterContext *ctx = inlink->dst;
75
+    RandomContext *s = ctx->priv;
76
+    AVFilterLink *outlink = ctx->outputs[0];
77
+    AVFrame *out;
78
+    int idx;
79
+
80
+    if (s->nb_frames_filled < s->nb_frames) {
81
+        s->frames[s->nb_frames_filled] = in;
82
+        s->pts[s->nb_frames_filled++] = in->pts;
83
+        return 0;
84
+    }
85
+
86
+    idx = av_lfg_get(&s->lfg) % s->nb_frames;
87
+
88
+    out = s->frames[idx];
89
+    out->pts = s->pts[0];
90
+    memmove(&s->pts[0], &s->pts[1], (s->nb_frames - 1) * sizeof(s->pts[0]));
91
+    s->frames[idx] = in;
92
+    s->pts[s->nb_frames - 1] = in->pts;
93
+
94
+    return ff_filter_frame(outlink, out);
95
+}
96
+
97
+static int request_frame(AVFilterLink *outlink)
98
+{
99
+    AVFilterContext *ctx = outlink->src;
100
+    RandomContext *s = ctx->priv;
101
+    int ret;
102
+
103
+    ret = ff_request_frame(ctx->inputs[0]);
104
+
105
+    if (ret == AVERROR_EOF && !ctx->is_disabled && s->nb_frames > 0) {
106
+        AVFrame *out = s->frames[s->nb_frames - 1];
107
+        out->pts = s->pts[s->flush_idx++];
108
+        ret = ff_filter_frame(outlink, out);
109
+        s->frames[s->nb_frames - 1] = NULL;
110
+        s->nb_frames--;
111
+    }
112
+
113
+    return ret;
114
+}
115
+
116
+static const AVFilterPad random_inputs[] = {
117
+    {
118
+        .name         = "default",
119
+        .type         = AVMEDIA_TYPE_VIDEO,
120
+        .filter_frame = filter_frame,
121
+    },
122
+    { NULL }
123
+};
124
+
125
+static const AVFilterPad random_outputs[] = {
126
+    {
127
+        .name          = "default",
128
+        .type          = AVMEDIA_TYPE_VIDEO,
129
+        .request_frame = request_frame,
130
+        .config_props  = config_output,
131
+    },
132
+    { NULL }
133
+};
134
+
135
+AVFilter ff_vf_random = {
136
+    .name        = "random",
137
+    .description = NULL_IF_CONFIG_SMALL("Return random frames."),
138
+    .priv_size   = sizeof(RandomContext),
139
+    .priv_class  = &random_class,
140
+    .init        = init,
141
+    .inputs      = random_inputs,
142
+    .outputs     = random_outputs,
143
+};