FFmpeg
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"
23 #include "libavutil/random_seed.h"
24 #include "avfilter.h"
25 #include "formats.h"
26 #include "internal.h"
27 #include "video.h"
28 
29 #define MAX_FRAMES 512
30 
31 typedef struct RandomContext {
32  const AVClass *class;
33 
35  int nb_frames;
36  int64_t random_seed;
39  int64_t pts[MAX_FRAMES];
40  int64_t duration[MAX_FRAMES];
41  int flush_idx;
43 
44 #define OFFSET(x) offsetof(RandomContext, x)
45 #define FLAGS AV_OPT_FLAG_VIDEO_PARAM|AV_OPT_FLAG_FILTERING_PARAM
46 
47 static const AVOption random_options[] = {
48  { "frames", "set number of frames in cache", OFFSET(nb_frames), AV_OPT_TYPE_INT, {.i64=30}, 2, MAX_FRAMES, FLAGS },
49  { "seed", "set the seed", OFFSET(random_seed), AV_OPT_TYPE_INT64, {.i64=-1}, -1, UINT32_MAX, FLAGS },
50  { NULL }
51 };
52 
53 AVFILTER_DEFINE_CLASS(random);
54 
56 {
57  RandomContext *s = ctx->priv;
58  uint32_t seed;
59 
60  if (s->random_seed < 0)
61  s->random_seed = av_get_random_seed();
62  seed = s->random_seed;
63  av_lfg_init(&s->lfg, seed);
64 
65  return 0;
66 }
67 
69 {
70  AVFilterContext *ctx = inlink->dst;
71  RandomContext *s = ctx->priv;
72  AVFilterLink *outlink = ctx->outputs[0];
73  AVFrame *out;
74  int idx;
75 
76  if (s->nb_frames_filled < s->nb_frames) {
77  s->frames[s->nb_frames_filled] = in;
78  s->duration[s->nb_frames_filled] = in->duration;
79  s->pts[s->nb_frames_filled++] = in->pts;
80  return 0;
81  }
82 
83  idx = av_lfg_get(&s->lfg) % s->nb_frames;
84 
85  out = s->frames[idx];
86  out->pts = s->pts[0];
87  out->duration = s->duration[0];
88  memmove(&s->pts[0], &s->pts[1], (s->nb_frames - 1) * sizeof(s->pts[0]));
89  memmove(&s->duration[0], &s->duration[1], (s->nb_frames - 1) * sizeof(s->duration[0]));
90  s->frames[idx] = in;
91  s->pts[s->nb_frames - 1] = in->pts;
92  s->duration[s->nb_frames - 1] = in->duration;
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 next:
106  if (ret == AVERROR_EOF && !ctx->is_disabled && s->nb_frames > 0) {
107  AVFrame *out = s->frames[s->nb_frames - 1];
108  if (!out) {
109  s->nb_frames--;
110  goto next;
111  }
112  out->duration = s->duration[s->flush_idx];
113  out->pts = s->pts[s->flush_idx++];
114  ret = ff_filter_frame(outlink, out);
115  s->frames[s->nb_frames - 1] = NULL;
116  s->nb_frames--;
117  }
118 
119  return ret;
120 }
121 
123 {
124  RandomContext *s = ctx->priv;
125 
126  for (int i = 0; i < s->nb_frames; i++)
127  av_frame_free(&s->frames[i]);
128 }
129 
130 static const AVFilterPad random_inputs[] = {
131  {
132  .name = "default",
133  .type = AVMEDIA_TYPE_VIDEO,
134  .filter_frame = filter_frame,
135  },
136 };
137 
138 static const AVFilterPad random_outputs[] = {
139  {
140  .name = "default",
141  .type = AVMEDIA_TYPE_VIDEO,
142  .request_frame = request_frame,
143  },
144 };
145 
147  .name = "random",
148  .description = NULL_IF_CONFIG_SMALL("Return random frames."),
149  .priv_size = sizeof(RandomContext),
150  .priv_class = &random_class,
151  .init = init,
152  .uninit = uninit,
155 };
RandomContext::random_seed
int64_t random_seed
Definition: vf_random.c:36
opt.h
RandomContext::duration
int64_t duration[MAX_FRAMES]
Definition: vf_random.c:40
init
static av_cold int init(AVFilterContext *ctx)
Definition: vf_random.c:55
out
FILE * out
Definition: movenc.c:54
av_lfg_init
av_cold void av_lfg_init(AVLFG *c, unsigned int seed)
Definition: lfg.c:32
ff_filter_frame
int ff_filter_frame(AVFilterLink *link, AVFrame *frame)
Send a frame of data to the next filter.
Definition: avfilter.c:969
AVFrame::duration
int64_t duration
Duration of the frame, in the same units as pts.
Definition: frame.h:728
AVERROR_EOF
#define AVERROR_EOF
End of file.
Definition: error.h:57
request_frame
static int request_frame(AVFilterLink *outlink)
Definition: vf_random.c:97
inlink
The exact code depends on how similar the blocks are and how related they are to the and needs to apply these operations to the correct inlink or outlink if there are several Macros are available to factor that when no extra processing is inlink
Definition: filter_design.txt:212
av_frame_free
void av_frame_free(AVFrame **frame)
Free the frame and any dynamically allocated objects in it, e.g.
Definition: frame.c:99
AVFrame
This structure describes decoded (raw) audio or video data.
Definition: frame.h:330
AVFrame::pts
int64_t pts
Presentation timestamp in time_base units (time when frame should be shown to user).
Definition: frame.h:437
uninit
static av_cold void uninit(AVFilterContext *ctx)
Definition: vf_random.c:122
AVOption
AVOption.
Definition: opt.h:251
random_outputs
static const AVFilterPad random_outputs[]
Definition: vf_random.c:138
ff_request_frame
int ff_request_frame(AVFilterLink *link)
Request an input frame from the filter at the other end of the link.
Definition: avfilter.c:415
AVFilter::name
const char * name
Filter name.
Definition: avfilter.h:165
RandomContext::lfg
AVLFG lfg
Definition: vf_random.c:34
video.h
ff_vf_random
const AVFilter ff_vf_random
Definition: vf_random.c:146
RandomContext::pts
int64_t pts[MAX_FRAMES]
Definition: vf_random.c:39
av_get_random_seed
uint32_t av_get_random_seed(void)
Get a seed to use in conjunction with random functions.
Definition: random_seed.c:121
formats.h
AVFilterPad
A filter pad used for either input or output.
Definition: internal.h:49
av_cold
#define av_cold
Definition: attributes.h:90
s
#define s(width, name)
Definition: cbs_vp9.c:256
av_lfg_get
static unsigned int av_lfg_get(AVLFG *c)
Get the next random unsigned 32-bit number using an ALFG.
Definition: lfg.h:53
lfg.h
AV_OPT_TYPE_INT64
@ AV_OPT_TYPE_INT64
Definition: opt.h:226
ctx
AVFormatContext * ctx
Definition: movenc.c:48
random_inputs
static const AVFilterPad random_inputs[]
Definition: vf_random.c:130
FILTER_INPUTS
#define FILTER_INPUTS(array)
Definition: internal.h:194
AVClass
Describe the class of an AVClass context structure.
Definition: log.h:66
NULL
#define NULL
Definition: coverity.c:32
RandomContext
Definition: vf_random.c:31
RandomContext::nb_frames_filled
int nb_frames_filled
Definition: vf_random.c:37
filter_frame
static int filter_frame(AVFilterLink *inlink, AVFrame *in)
Definition: vf_random.c:68
RandomContext::flush_idx
int flush_idx
Definition: vf_random.c:41
seed
static unsigned int seed
Definition: videogen.c:78
AVLFG
Context structure for the Lagged Fibonacci PRNG.
Definition: lfg.h:33
NULL_IF_CONFIG_SMALL
#define NULL_IF_CONFIG_SMALL(x)
Return NULL if CONFIG_SMALL is true, otherwise the argument without modification.
Definition: internal.h:115
internal.h
OFFSET
#define OFFSET(x)
Definition: vf_random.c:44
i
#define i(width, name, range_min, range_max)
Definition: cbs_h2645.c:269
random_options
static const AVOption random_options[]
Definition: vf_random.c:47
AVFILTER_DEFINE_CLASS
AVFILTER_DEFINE_CLASS(random)
FLAGS
#define FLAGS
Definition: vf_random.c:45
AVFilterPad::name
const char * name
Pad name.
Definition: internal.h:55
AVFilter
Filter definition.
Definition: avfilter.h:161
ret
ret
Definition: filter_design.txt:187
random_seed.h
AV_OPT_TYPE_INT
@ AV_OPT_TYPE_INT
Definition: opt.h:225
avfilter.h
AVFilterContext
An instance of a filter.
Definition: avfilter.h:392
AVMEDIA_TYPE_VIDEO
@ AVMEDIA_TYPE_VIDEO
Definition: avutil.h:201
MAX_FRAMES
#define MAX_FRAMES
Definition: vf_random.c:29
FILTER_OUTPUTS
#define FILTER_OUTPUTS(array)
Definition: internal.h:195
RandomContext::frames
AVFrame * frames[MAX_FRAMES]
Definition: vf_random.c:38
RandomContext::nb_frames
int nb_frames
Definition: vf_random.c:35