FFmpeg
Loading...
Searching...
No Matches
vf_random.c
Go to the documentation of this file.
1/*
2 * Copyright (c) 2015 Paul B Mahol
3 *
4 * This file is part of FFmpeg.
5 *
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.
10 *
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.
15 *
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
19 */
20
21#include "libavutil/lfg.h"
22#include "libavutil/opt.h"
24#include "avfilter.h"
25#include "filters.h"
26
27#define MAX_FRAMES 512
28
41
42#define OFFSET(x) offsetof(RandomContext, x)
43#define FLAGS AV_OPT_FLAG_VIDEO_PARAM|AV_OPT_FLAG_FILTERING_PARAM
44
45static 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
52
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
66static int filter_frame(AVFilterLink *inlink, AVFrame *in)
67{
68 AVFilterContext *ctx = inlink->dst;
69 RandomContext *s = ctx->priv;
70 AVFilterLink *outlink = ctx->outputs[0];
71 AVFrame *out;
72 int idx;
73
74 if (s->nb_frames_filled < s->nb_frames) {
75 s->frames[s->nb_frames_filled] = in;
76 s->duration[s->nb_frames_filled] = in->duration;
77 s->pts[s->nb_frames_filled++] = in->pts;
78 return 0;
79 }
80
81 idx = av_lfg_get(&s->lfg) % s->nb_frames;
82
83 out = s->frames[idx];
84 out->pts = s->pts[0];
85 out->duration = s->duration[0];
86 memmove(&s->pts[0], &s->pts[1], (s->nb_frames - 1) * sizeof(s->pts[0]));
87 memmove(&s->duration[0], &s->duration[1], (s->nb_frames - 1) * sizeof(s->duration[0]));
88 s->frames[idx] = in;
89 s->pts[s->nb_frames - 1] = in->pts;
90 s->duration[s->nb_frames - 1] = in->duration;
91
92 return ff_filter_frame(outlink, out);
93}
94
95static int request_frame(AVFilterLink *outlink)
96{
97 AVFilterContext *ctx = outlink->src;
98 RandomContext *s = ctx->priv;
99 int ret;
100
101 ret = ff_request_frame(ctx->inputs[0]);
102
103next:
104 if (ret == AVERROR_EOF && !ctx->is_disabled && s->nb_frames > 0) {
105 AVFrame *out = s->frames[s->nb_frames - 1];
106 if (!out) {
107 s->nb_frames--;
108 goto next;
109 }
110 out->duration = s->duration[s->flush_idx];
111 out->pts = s->pts[s->flush_idx++];
112 ret = ff_filter_frame(outlink, out);
113 s->frames[s->nb_frames - 1] = NULL;
114 s->nb_frames--;
115 }
116
117 return ret;
118}
119
121{
122 RandomContext *s = ctx->priv;
123
124 for (int i = 0; i < s->nb_frames; i++)
125 av_frame_free(&s->frames[i]);
126}
127
128static const AVFilterPad random_inputs[] = {
129 {
130 .name = "default",
131 .type = AVMEDIA_TYPE_VIDEO,
132 .filter_frame = filter_frame,
133 },
134};
135
136static const AVFilterPad random_outputs[] = {
137 {
138 .name = "default",
139 .type = AVMEDIA_TYPE_VIDEO,
140 .request_frame = request_frame,
141 },
142};
143
145 .p.name = "random",
146 .p.description = NULL_IF_CONFIG_SMALL("Return random frames."),
147 .p.priv_class = &random_class,
148 .priv_size = sizeof(RandomContext),
149 .init = init,
150 .uninit = uninit,
153};
static int request_frame(AVFilterLink *outlink)
Definition af_aecho.c:272
const FFFilter ff_vf_random
Definition vf_random.c:144
static FILE * out
static AVFormatContext * ctx
int ff_filter_frame(AVFilterLink *link, AVFrame *frame)
Send a frame of data to the next filter.
Definition avfilter.c:1068
int ff_request_frame(AVFilterLink *link)
Request an input frame from the filter at the other end of the link.
Definition avfilter.c:483
Main libavfilter public API header.
#define i(width, name, range_min, range_max)
Definition cbs_h264.c:63
#define s(width, name)
Definition cbs_vp9.c:198
#define FLAGS
Definition cmdutils.c:598
#define NULL
Definition coverity.c:32
long long int64_t
Definition coverity.c:34
static int filter_frame(DBEDecodeContext *s, AVFrame *frame)
Definition dolby_e.c:1067
int(* init)(AVBSFContext *ctx)
Definition dts2pts.c:608
@ AV_OPT_TYPE_INT64
Underlying C type is int64_t.
Definition opt.h:262
@ AV_OPT_TYPE_INT
Underlying C type is int.
Definition opt.h:258
uint32_t av_get_random_seed(void)
Get a seed to use in conjunction with random functions.
#define AVERROR_EOF
End of file.
Definition error.h:57
void av_frame_free(AVFrame **frame)
Free the frame and any dynamically allocated objects in it, e.g.
Definition frame.c:64
@ AVMEDIA_TYPE_VIDEO
Definition avutil.h:200
static av_cold void uninit(AVBitStreamFilterContext *ctx)
av_cold void av_lfg_init(AVLFG *c, unsigned int seed)
Definition lfg.c:32
static unsigned int av_lfg_get(AVLFG *c)
Get the next random unsigned 32-bit number using an ALFG.
Definition lfg.h:53
#define MAX_FRAMES
Definition diracdec.c:54
#define FILTER_INPUTS(array)
Definition filters.h:264
#define FILTER_OUTPUTS(array)
Definition filters.h:265
#define AVFILTER_DEFINE_CLASS(fname)
Definition filters.h:478
#define av_cold
Definition attributes.h:117
#define NULL_IF_CONFIG_SMALL(x)
Return NULL if CONFIG_SMALL is true, otherwise the argument without modification.
Definition internal.h:88
AVOptions.
Describe the class of an AVClass context structure.
Definition log.h:76
An instance of a filter.
Definition avfilter.h:273
A filter pad used for either input or output.
Definition filters.h:40
This structure describes decoded (raw) audio or video data.
Definition frame.h:472
int64_t pts
Presentation timestamp in time_base units (time when frame should be shown to user).
Definition frame.h:574
int64_t duration
Duration of the frame, in the same units as pts.
Definition frame.h:820
Context structure for the Lagged Fibonacci PRNG.
Definition lfg.h:33
AVOption.
Definition opt.h:428
int64_t duration[MAX_FRAMES]
Definition vf_random.c:38
AVFrame * frames[MAX_FRAMES]
Definition vf_random.c:36
int64_t random_seed
Definition vf_random.c:34
int64_t pts[MAX_FRAMES]
Definition vf_random.c:37
int nb_frames_filled
Definition vf_random.c:35
static const AVFilterPad random_inputs[]
Definition vf_random.c:128
#define MAX_FRAMES
Definition vf_random.c:27
static int request_frame(AVFilterLink *outlink)
Definition vf_random.c:95
static int filter_frame(AVFilterLink *inlink, AVFrame *in)
Definition vf_random.c:66
static const AVOption random_options[]
Definition vf_random.c:45
static const AVFilterPad random_outputs[]
Definition vf_random.c:136
static av_cold void uninit(AVFilterContext *ctx)
Definition vf_random.c:120
#define OFFSET(x)
Definition vf_random.c:42
static unsigned int seed
Definition videogen.c:78