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 "internal.h"
26 
27 #define MAX_FRAMES 512
28 
29 typedef struct RandomContext {
30  const AVClass *class;
31 
33  int nb_frames;
34  int64_t random_seed;
37  int64_t pts[MAX_FRAMES];
38  int64_t duration[MAX_FRAMES];
39  int flush_idx;
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 
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 
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 
95 static 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 
103 next:
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 
128 static const AVFilterPad random_inputs[] = {
129  {
130  .name = "default",
131  .type = AVMEDIA_TYPE_VIDEO,
132  .filter_frame = filter_frame,
133  },
134 };
135 
136 static const AVFilterPad random_outputs[] = {
137  {
138  .name = "default",
139  .type = AVMEDIA_TYPE_VIDEO,
140  .request_frame = request_frame,
141  },
142 };
143 
145  .name = "random",
146  .description = NULL_IF_CONFIG_SMALL("Return random frames."),
147  .priv_size = sizeof(RandomContext),
148  .priv_class = &random_class,
149  .init = init,
150  .uninit = uninit,
153 };
RandomContext::random_seed
int64_t random_seed
Definition: vf_random.c:34
opt.h
RandomContext::duration
int64_t duration[MAX_FRAMES]
Definition: vf_random.c:38
init
static av_cold int init(AVFilterContext *ctx)
Definition: vf_random.c:53
out
FILE * out
Definition: movenc.c:55
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:1015
AVFrame::duration
int64_t duration
Duration of the frame, in the same units as pts.
Definition: frame.h:780
AVERROR_EOF
#define AVERROR_EOF
End of file.
Definition: error.h:57
request_frame
static int request_frame(AVFilterLink *outlink)
Definition: vf_random.c:95
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:160
AVFrame
This structure describes decoded (raw) audio or video data.
Definition: frame.h:374
AVFrame::pts
int64_t pts
Presentation timestamp in time_base units (time when frame should be shown to user).
Definition: frame.h:486
uninit
static av_cold void uninit(AVFilterContext *ctx)
Definition: vf_random.c:120
AVOption
AVOption.
Definition: opt.h:346
random_outputs
static const AVFilterPad random_outputs[]
Definition: vf_random.c:136
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:463
AVFilter::name
const char * name
Filter name.
Definition: avfilter.h:170
RandomContext::lfg
AVLFG lfg
Definition: vf_random.c:32
ff_vf_random
const AVFilter ff_vf_random
Definition: vf_random.c:144
RandomContext::pts
int64_t pts[MAX_FRAMES]
Definition: vf_random.c:37
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:167
AVFilterPad
A filter pad used for either input or output.
Definition: internal.h:33
av_cold
#define av_cold
Definition: attributes.h:90
s
#define s(width, name)
Definition: cbs_vp9.c:198
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:236
ctx
AVFormatContext * ctx
Definition: movenc.c:49
random_inputs
static const AVFilterPad random_inputs[]
Definition: vf_random.c:128
FILTER_INPUTS
#define FILTER_INPUTS(array)
Definition: internal.h:182
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:29
RandomContext::nb_frames_filled
int nb_frames_filled
Definition: vf_random.c:35
filter_frame
static int filter_frame(AVFilterLink *inlink, AVFrame *in)
Definition: vf_random.c:66
RandomContext::flush_idx
int flush_idx
Definition: vf_random.c:39
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:94
internal.h
OFFSET
#define OFFSET(x)
Definition: vf_random.c:42
i
#define i(width, name, range_min, range_max)
Definition: cbs_h2645.c:256
random_options
static const AVOption random_options[]
Definition: vf_random.c:45
AVFILTER_DEFINE_CLASS
AVFILTER_DEFINE_CLASS(random)
FLAGS
#define FLAGS
Definition: vf_random.c:43
AVFilterPad::name
const char * name
Pad name.
Definition: internal.h:39
AVFilter
Filter definition.
Definition: avfilter.h:166
ret
ret
Definition: filter_design.txt:187
random_seed.h
AV_OPT_TYPE_INT
@ AV_OPT_TYPE_INT
Definition: opt.h:235
avfilter.h
AVFilterContext
An instance of a filter.
Definition: avfilter.h:407
AVMEDIA_TYPE_VIDEO
@ AVMEDIA_TYPE_VIDEO
Definition: avutil.h:201
MAX_FRAMES
#define MAX_FRAMES
Definition: vf_random.c:27
FILTER_OUTPUTS
#define FILTER_OUTPUTS(array)
Definition: internal.h:183
RandomContext::frames
AVFrame * frames[MAX_FRAMES]
Definition: vf_random.c:36
RandomContext::nb_frames
int nb_frames
Definition: vf_random.c:33