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  int flush_idx;
42 
43 #define OFFSET(x) offsetof(RandomContext, x)
44 #define FLAGS AV_OPT_FLAG_VIDEO_PARAM|AV_OPT_FLAG_FILTERING_PARAM
45 
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 },
49  { NULL }
50 };
51 
52 AVFILTER_DEFINE_CLASS(random);
53 
55 {
56  RandomContext *s = ctx->priv;
57  uint32_t seed;
58 
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);
63 
64  return 0;
65 }
66 
68 {
69  AVFilterContext *ctx = inlink->dst;
70  RandomContext *s = ctx->priv;
71  AVFilterLink *outlink = ctx->outputs[0];
72  AVFrame *out;
73  int idx;
74 
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;
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  memmove(&s->pts[0], &s->pts[1], (s->nb_frames - 1) * sizeof(s->pts[0]));
86  s->frames[idx] = in;
87  s->pts[s->nb_frames - 1] = in->pts;
88 
89  return ff_filter_frame(outlink, out);
90 }
91 
92 static int request_frame(AVFilterLink *outlink)
93 {
94  AVFilterContext *ctx = outlink->src;
95  RandomContext *s = ctx->priv;
96  int ret;
97 
98  ret = ff_request_frame(ctx->inputs[0]);
99 
100  if (ret == AVERROR_EOF && !ctx->is_disabled && s->nb_frames > 0) {
101  AVFrame *out = s->frames[s->nb_frames - 1];
102  out->pts = s->pts[s->flush_idx++];
103  ret = ff_filter_frame(outlink, out);
104  s->frames[s->nb_frames - 1] = NULL;
105  s->nb_frames--;
106  }
107 
108  return ret;
109 }
110 
112 {
113  RandomContext *s = ctx->priv;
114 
115  for (int i = 0; i < s->nb_frames; i++)
116  av_frame_free(&s->frames[i]);
117 }
118 
119 static const AVFilterPad random_inputs[] = {
120  {
121  .name = "default",
122  .type = AVMEDIA_TYPE_VIDEO,
123  .filter_frame = filter_frame,
124  },
125  { NULL }
126 };
127 
128 static const AVFilterPad random_outputs[] = {
129  {
130  .name = "default",
131  .type = AVMEDIA_TYPE_VIDEO,
132  .request_frame = request_frame,
133  },
134  { NULL }
135 };
136 
138  .name = "random",
139  .description = NULL_IF_CONFIG_SMALL("Return random frames."),
140  .priv_size = sizeof(RandomContext),
141  .priv_class = &random_class,
142  .init = init,
143  .uninit = uninit,
146 };
RandomContext::random_seed
int64_t random_seed
Definition: vf_random.c:36
opt.h
init
static av_cold int init(AVFilterContext *ctx)
Definition: vf_random.c:54
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:1080
AVERROR_EOF
#define AVERROR_EOF
End of file.
Definition: error.h:55
request_frame
static int request_frame(AVFilterLink *outlink)
Definition: vf_random.c:92
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:202
AVFrame
This structure describes decoded (raw) audio or video data.
Definition: frame.h:295
uninit
static av_cold void uninit(AVFilterContext *ctx)
Definition: vf_random.c:111
AVOption
AVOption.
Definition: opt.h:246
random_outputs
static const AVFilterPad random_outputs[]
Definition: vf_random.c:128
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:407
AVFilter::name
const char * name
Filter name.
Definition: avfilter.h:148
RandomContext::lfg
AVLFG lfg
Definition: vf_random.c:34
video.h
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:120
formats.h
AVFilterPad
A filter pad used for either input or output.
Definition: internal.h:54
av_cold
#define av_cold
Definition: attributes.h:84
s
#define s(width, name)
Definition: cbs_vp9.c:257
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:224
outputs
static const AVFilterPad outputs[]
Definition: af_acontrast.c:203
ctx
AVFormatContext * ctx
Definition: movenc.c:48
random_inputs
static const AVFilterPad random_inputs[]
Definition: vf_random.c:119
AVClass
Describe the class of an AVClass context structure.
Definition: log.h:67
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:67
RandomContext::flush_idx
int flush_idx
Definition: vf_random.c:40
inputs
these buffered frames must be flushed immediately if a new input produces new the filter must not call request_frame to get more It must just process the frame or queue it The task of requesting more frames is left to the filter s request_frame method or the application If a filter has several inputs
Definition: filter_design.txt:243
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:188
internal.h
OFFSET
#define OFFSET(x)
Definition: vf_random.c:43
in
uint8_t pi<< 24) CONV_FUNC_GROUP(AV_SAMPLE_FMT_FLT, float, AV_SAMPLE_FMT_U8, uint8_t,(*(const uint8_t *) pi - 0x80) *(1.0f/(1<< 7))) CONV_FUNC_GROUP(AV_SAMPLE_FMT_DBL, double, AV_SAMPLE_FMT_U8, uint8_t,(*(const uint8_t *) pi - 0x80) *(1.0/(1<< 7))) CONV_FUNC_GROUP(AV_SAMPLE_FMT_U8, uint8_t, AV_SAMPLE_FMT_S16, int16_t,(*(const int16_t *) pi >> 8)+0x80) CONV_FUNC_GROUP(AV_SAMPLE_FMT_FLT, float, AV_SAMPLE_FMT_S16, int16_t, *(const int16_t *) pi *(1.0f/(1<< 15))) CONV_FUNC_GROUP(AV_SAMPLE_FMT_DBL, double, AV_SAMPLE_FMT_S16, int16_t, *(const int16_t *) pi *(1.0/(1<< 15))) CONV_FUNC_GROUP(AV_SAMPLE_FMT_U8, uint8_t, AV_SAMPLE_FMT_S32, int32_t,(*(const int32_t *) pi >> 24)+0x80) CONV_FUNC_GROUP(AV_SAMPLE_FMT_FLT, float, AV_SAMPLE_FMT_S32, int32_t, *(const int32_t *) pi *(1.0f/(1U<< 31))) CONV_FUNC_GROUP(AV_SAMPLE_FMT_DBL, double, AV_SAMPLE_FMT_S32, int32_t, *(const int32_t *) pi *(1.0/(1U<< 31))) CONV_FUNC_GROUP(AV_SAMPLE_FMT_U8, uint8_t, AV_SAMPLE_FMT_FLT, float, av_clip_uint8(lrintf(*(const float *) pi *(1<< 7))+0x80)) CONV_FUNC_GROUP(AV_SAMPLE_FMT_S16, int16_t, AV_SAMPLE_FMT_FLT, float, av_clip_int16(lrintf(*(const float *) pi *(1<< 15)))) CONV_FUNC_GROUP(AV_SAMPLE_FMT_S32, int32_t, AV_SAMPLE_FMT_FLT, float, av_clipl_int32(llrintf(*(const float *) pi *(1U<< 31)))) CONV_FUNC_GROUP(AV_SAMPLE_FMT_U8, uint8_t, AV_SAMPLE_FMT_DBL, double, av_clip_uint8(lrint(*(const double *) pi *(1<< 7))+0x80)) CONV_FUNC_GROUP(AV_SAMPLE_FMT_S16, int16_t, AV_SAMPLE_FMT_DBL, double, av_clip_int16(lrint(*(const double *) pi *(1<< 15)))) CONV_FUNC_GROUP(AV_SAMPLE_FMT_S32, int32_t, AV_SAMPLE_FMT_DBL, double, av_clipl_int32(llrint(*(const double *) pi *(1U<< 31)))) #define SET_CONV_FUNC_GROUP(ofmt, ifmt) static void set_generic_function(AudioConvert *ac) { } void ff_audio_convert_free(AudioConvert **ac) { if(! *ac) return;ff_dither_free(&(*ac) ->dc);av_freep(ac);} AudioConvert *ff_audio_convert_alloc(AVAudioResampleContext *avr, enum AVSampleFormat out_fmt, enum AVSampleFormat in_fmt, int channels, int sample_rate, int apply_map) { AudioConvert *ac;int in_planar, out_planar;ac=av_mallocz(sizeof(*ac));if(!ac) return NULL;ac->avr=avr;ac->out_fmt=out_fmt;ac->in_fmt=in_fmt;ac->channels=channels;ac->apply_map=apply_map;if(avr->dither_method !=AV_RESAMPLE_DITHER_NONE &&av_get_packed_sample_fmt(out_fmt)==AV_SAMPLE_FMT_S16 &&av_get_bytes_per_sample(in_fmt) > 2) { ac->dc=ff_dither_alloc(avr, out_fmt, in_fmt, channels, sample_rate, apply_map);if(!ac->dc) { av_free(ac);return NULL;} return ac;} in_planar=ff_sample_fmt_is_planar(in_fmt, channels);out_planar=ff_sample_fmt_is_planar(out_fmt, channels);if(in_planar==out_planar) { ac->func_type=CONV_FUNC_TYPE_FLAT;ac->planes=in_planar ? ac->channels :1;} else if(in_planar) ac->func_type=CONV_FUNC_TYPE_INTERLEAVE;else ac->func_type=CONV_FUNC_TYPE_DEINTERLEAVE;set_generic_function(ac);if(ARCH_AARCH64) ff_audio_convert_init_aarch64(ac);if(ARCH_ARM) ff_audio_convert_init_arm(ac);if(ARCH_X86) ff_audio_convert_init_x86(ac);return ac;} int ff_audio_convert(AudioConvert *ac, AudioData *out, AudioData *in) { int use_generic=1;int len=in->nb_samples;int p;if(ac->dc) { av_log(ac->avr, AV_LOG_TRACE, "%d samples - audio_convert: %s to %s (dithered)\n", len, av_get_sample_fmt_name(ac->in_fmt), av_get_sample_fmt_name(ac->out_fmt));return ff_convert_dither(ac-> in
Definition: audio_convert.c:326
i
#define i(width, name, range_min, range_max)
Definition: cbs_h2645.c:259
random_options
static const AVOption random_options[]
Definition: vf_random.c:46
AVFILTER_DEFINE_CLASS
AVFILTER_DEFINE_CLASS(random)
FLAGS
#define FLAGS
Definition: vf_random.c:44
AVFilterPad::name
const char * name
Pad name.
Definition: internal.h:60
AVFilter
Filter definition.
Definition: avfilter.h:144
ret
ret
Definition: filter_design.txt:187
random_seed.h
AV_OPT_TYPE_INT
@ AV_OPT_TYPE_INT
Definition: opt.h:223
avfilter.h
AVFilterContext
An instance of a filter.
Definition: avfilter.h:338
AVMEDIA_TYPE_VIDEO
@ AVMEDIA_TYPE_VIDEO
Definition: avutil.h:201
MAX_FRAMES
#define MAX_FRAMES
Definition: vf_random.c:29
RandomContext::frames
AVFrame * frames[MAX_FRAMES]
Definition: vf_random.c:38
RandomContext::nb_frames
int nb_frames
Definition: vf_random.c:35
ff_vf_random
AVFilter ff_vf_random
Definition: vf_random.c:137