2 * Copyright (c) 2015 Paul B Mahol
4 * This file is part of FFmpeg.
6 * FFmpeg is free software; you can redistribute it and/or
7 * modify it under the terms of the GNU Lesser General Public
8 * License as published by the Free Software Foundation; either
9 * version 2.1 of the License, or (at your option) any later version.
11 * FFmpeg is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
14 * Lesser General Public License for more details.
16 * You should have received a copy of the GNU Lesser General Public
17 * License along with FFmpeg; if not, write to the Free Software
18 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
21 #include "libavutil/lfg.h"
22 #include "libavutil/opt.h"
23 #include "libavutil/random_seed.h"
29 #define MAX_FRAMES 512
31 typedef struct RandomContext {
38 AVFrame *frames[MAX_FRAMES];
39 int64_t pts[MAX_FRAMES];
43 #define OFFSET(x) offsetof(RandomContext, x)
44 #define FLAGS AV_OPT_FLAG_VIDEO_PARAM|AV_OPT_FLAG_FILTERING_PARAM
46 static const AVOption random_options[] = {
47 { "frames", "set number of frames in cache", OFFSET(nb_frames), AV_OPT_TYPE_INT, {.i64=30}, 2, MAX_FRAMES, FLAGS },
48 { "seed", "set the seed", OFFSET(random_seed), AV_OPT_TYPE_INT64, {.i64=-1}, -1, UINT32_MAX, FLAGS },
52 AVFILTER_DEFINE_CLASS(random);
54 static av_cold int init(AVFilterContext *ctx)
56 RandomContext *s = ctx->priv;
59 if (s->random_seed < 0)
60 s->random_seed = av_get_random_seed();
61 seed = s->random_seed;
62 av_lfg_init(&s->lfg, seed);
67 static int filter_frame(AVFilterLink *inlink, AVFrame *in)
69 AVFilterContext *ctx = inlink->dst;
70 RandomContext *s = ctx->priv;
71 AVFilterLink *outlink = ctx->outputs[0];
75 if (s->nb_frames_filled < s->nb_frames) {
76 s->frames[s->nb_frames_filled] = in;
77 s->pts[s->nb_frames_filled++] = in->pts;
81 idx = av_lfg_get(&s->lfg) % s->nb_frames;
85 memmove(&s->pts[0], &s->pts[1], (s->nb_frames - 1) * sizeof(s->pts[0]));
87 s->pts[s->nb_frames - 1] = in->pts;
89 return ff_filter_frame(outlink, out);
92 static int request_frame(AVFilterLink *outlink)
94 AVFilterContext *ctx = outlink->src;
95 RandomContext *s = ctx->priv;
98 ret = ff_request_frame(ctx->inputs[0]);
101 if (ret == AVERROR_EOF && !ctx->is_disabled && s->nb_frames > 0) {
102 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;
116 static av_cold void uninit(AVFilterContext *ctx)
118 RandomContext *s = ctx->priv;
120 for (int i = 0; i < s->nb_frames; i++)
121 av_frame_free(&s->frames[i]);
124 static const AVFilterPad random_inputs[] = {
127 .type = AVMEDIA_TYPE_VIDEO,
128 .filter_frame = filter_frame,
133 static const AVFilterPad random_outputs[] = {
136 .type = AVMEDIA_TYPE_VIDEO,
137 .request_frame = request_frame,
142 AVFilter ff_vf_random = {
144 .description = NULL_IF_CONFIG_SMALL("Return random frames."),
145 .priv_size = sizeof(RandomContext),
146 .priv_class = &random_class,
149 .inputs = random_inputs,
150 .outputs = random_outputs,