FFmpeg
vsrc_perlin.c
Go to the documentation of this file.
1 /*
2  * This file is part of FFmpeg.
3  *
4  * FFmpeg is free software; you can redistribute it and/or
5  * modify it under the terms of the GNU Lesser General Public
6  * License as published by the Free Software Foundation; either
7  * version 2.1 of the License, or (at your option) any later version.
8  *
9  * FFmpeg is distributed in the hope that it will be useful,
10  * but WITHOUT ANY WARRANTY; without even the implied warranty of
11  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
12  * Lesser General Public License for more details.
13  *
14  * You should have received a copy of the GNU Lesser General Public
15  * License along with FFmpeg; if not, write to the Free Software
16  * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
17  */
18 
19 /**
20  * @file
21  * Perlin noise generator
22  */
23 
24 #include <float.h>
25 
26 #include "perlin.h"
27 #include "libavutil/lfg.h"
28 #include "libavutil/opt.h"
29 #include "avfilter.h"
30 #include "filters.h"
31 #include "formats.h"
32 #include "video.h"
33 
34 typedef struct PerlinContext {
35  const AVClass *class;
36 
37  int w, h;
39 
41  int octaves;
42  double persistence;
43  unsigned int random_seed;
45 
46  double xscale, yscale, tscale;
47  uint64_t pts;
49 
50 #define OFFSET(x) offsetof(PerlinContext, x)
51 #define FLAGS AV_OPT_FLAG_VIDEO_PARAM|AV_OPT_FLAG_FILTERING_PARAM
52 
53 static const AVOption perlin_options[] = {
54  { "size", "set video size", OFFSET(w), AV_OPT_TYPE_IMAGE_SIZE, {.str="320x240"}, 0, 0, FLAGS },
55  { "s", "set video size", OFFSET(w), AV_OPT_TYPE_IMAGE_SIZE, {.str="320x240"}, 0, 0, FLAGS },
56  { "rate", "set video rate", OFFSET(frame_rate), AV_OPT_TYPE_VIDEO_RATE, {.str="25"}, 0, INT_MAX, FLAGS },
57  { "r", "set video rate", OFFSET(frame_rate), AV_OPT_TYPE_VIDEO_RATE, {.str="25"}, 0, INT_MAX, FLAGS },
58  { "octaves", "set the number of components to use to generate the noise", OFFSET(octaves), AV_OPT_TYPE_INT, {.i64=1}, 1, INT_MAX, FLAGS },
59  { "persistence", "set the octaves persistence", OFFSET(persistence), AV_OPT_TYPE_DOUBLE, {.dbl=1}, 0.0, DBL_MAX, FLAGS },
60 
61  { "xscale", "set x-scale factor", OFFSET(xscale), AV_OPT_TYPE_DOUBLE, {.dbl=1}, 0.0, DBL_MAX, FLAGS },
62  { "yscale", "set y-scale factor", OFFSET(yscale), AV_OPT_TYPE_DOUBLE, {.dbl=1}, 0.0, DBL_MAX, FLAGS },
63  { "tscale", "set t-scale factor", OFFSET(tscale), AV_OPT_TYPE_DOUBLE, {.dbl=1}, 0.0, DBL_MAX, FLAGS },
64 
65  { "random_mode", "set random mode used to compute initial pattern", OFFSET(random_mode), AV_OPT_TYPE_INT, {.i64=FF_PERLIN_RANDOM_MODE_RANDOM}, 0, FF_PERLIN_RANDOM_MODE_NB-1, FLAGS, .unit = "random_mode" },
66  { "random", "compute and use random seed", 0, AV_OPT_TYPE_CONST, {.i64=FF_PERLIN_RANDOM_MODE_RANDOM}, 0, 0, FLAGS, .unit = "random_mode" },
67  { "ken", "use the predefined initial pattern defined by Ken Perlin in the original article", 0, AV_OPT_TYPE_CONST, {.i64=FF_PERLIN_RANDOM_MODE_KEN}, 0, 0, FLAGS, .unit = "random_mode" },
68  { "seed", "use the value specified by random_seed", 0, AV_OPT_TYPE_CONST, {.i64=FF_PERLIN_RANDOM_MODE_SEED}, 0, 0, FLAGS, .unit="random_mode" },
69 
70  { "random_seed", "set the seed for filling the initial pattern", OFFSET(random_seed), AV_OPT_TYPE_UINT, {.i64=0}, 0, UINT_MAX, FLAGS },
71  { "seed", "set the seed for filling the initial pattern", OFFSET(random_seed), AV_OPT_TYPE_UINT, {.i64=0}, 0, UINT_MAX, FLAGS },
72  { NULL }
73 };
74 
75 AVFILTER_DEFINE_CLASS(perlin);
76 
78 {
79  PerlinContext *perlin = ctx->priv;
80  int ret;
81 
82  if (ret = ff_perlin_init(&perlin->perlin, -1, perlin->octaves, perlin->persistence,
83  perlin->random_mode, perlin->random_seed)) {
84  return ret;
85  }
86 
88  "s:%dx%d r:%d/%d octaves:%d persistence:%f xscale:%f yscale:%f tscale:%f\n",
89  perlin->w, perlin->h, perlin->frame_rate.num, perlin->frame_rate.den,
90  perlin->octaves, perlin->persistence,
91  perlin->xscale, perlin->yscale, perlin->tscale);
92  return 0;
93 }
94 
95 static int config_props(AVFilterLink *outlink)
96 {
97  PerlinContext *perlin = outlink->src->priv;
98  FilterLink *l = ff_filter_link(outlink);
99 
100  outlink->w = perlin->w;
101  outlink->h = perlin->h;
102  outlink->time_base = av_inv_q(perlin->frame_rate);
103  l->frame_rate = perlin->frame_rate;
104 
105  return 0;
106 }
107 
108 static int request_frame(AVFilterLink *outlink)
109 {
110  AVFilterContext *ctx = outlink->src;
111  PerlinContext *perlin = ctx->priv;
112  AVFrame *picref = ff_get_video_buffer(outlink, perlin->w, perlin->h);
113  int i, j;
114  uint8_t *data0, *data;
115  double x, y, t;
116 
117  if (!picref)
118  return AVERROR(ENOMEM);
119 
120  picref->sample_aspect_ratio = (AVRational) {1, 1};
121  picref->pts = perlin->pts++;
122  picref->duration = 1;
123 
124  t = perlin->tscale * (perlin->pts * av_q2d(outlink->time_base));
125  data0 = picref->data[0];
126 
127  for (i = 0; i < perlin->h; i++) {
128  y = perlin->yscale * (double)i / perlin->h;
129 
130  data = data0;
131 
132  for (j = 0; j < perlin->w; j++) {
133  double res;
134  x = perlin->xscale * (double)j / perlin->w;
135  res = ff_perlin_get(&perlin->perlin, x, y, t);
136  av_log(ctx, AV_LOG_DEBUG, "x:%f y:%f t:%f => %f\n", x, y, t, res);
137  *data++ = res * 255;
138  }
139  data0 += picref->linesize[0];
140  }
141 
142  return ff_filter_frame(outlink, picref);
143 }
144 
146  AVFilterFormatsConfig **cfg_in,
147  AVFilterFormatsConfig **cfg_out)
148 {
150 
151  return ff_set_common_formats_from_list2(ctx, cfg_in, cfg_out, pix_fmts);
152 }
153 
154 static const AVFilterPad perlin_outputs[] = {
155  {
156  .name = "default",
157  .type = AVMEDIA_TYPE_VIDEO,
158  .request_frame = request_frame,
159  .config_props = config_props,
160  },
161 };
162 
164  .name = "perlin",
165  .description = NULL_IF_CONFIG_SMALL("Generate Perlin noise"),
166  .priv_size = sizeof(PerlinContext),
167  .priv_class = &perlin_class,
168  .init = init,
169  .inputs = NULL,
172 };
ff_get_video_buffer
AVFrame * ff_get_video_buffer(AVFilterLink *link, int w, int h)
Request a picture buffer with a specific set of permissions.
Definition: video.c:116
AVPixelFormat
AVPixelFormat
Pixel format.
Definition: pixfmt.h:71
AVFILTER_DEFINE_CLASS
AVFILTER_DEFINE_CLASS(perlin)
AVERROR
Filter the word “frame” indicates either a video frame or a group of audio as stored in an AVFrame structure Format for each input and each output the list of supported formats For video that means pixel format For audio that means channel sample they are references to shared objects When the negotiation mechanism computes the intersection of the formats supported at each end of a all references to both lists are replaced with a reference to the intersection And when a single format is eventually chosen for a link amongst the remaining all references to the list are updated That means that if a filter requires that its input and output have the same format amongst a supported all it has to do is use a reference to the same list of formats query_formats can leave some formats unset and return AVERROR(EAGAIN) to cause the negotiation mechanism toagain later. That can be used by filters with complex requirements to use the format negotiated on one link to set the formats supported on another. Frame references ownership and permissions
opt.h
ff_filter_frame
int ff_filter_frame(AVFilterLink *link, AVFrame *frame)
Send a frame of data to the next filter.
Definition: avfilter.c:1062
AVFrame::duration
int64_t duration
Duration of the frame, in the same units as pts.
Definition: frame.h:795
AV_OPT_TYPE_VIDEO_RATE
@ AV_OPT_TYPE_VIDEO_RATE
Underlying C type is AVRational.
Definition: opt.h:315
perlin.h
PerlinContext::octaves
int octaves
Definition: vsrc_perlin.c:41
AVFrame
This structure describes decoded (raw) audio or video data.
Definition: frame.h:389
AVFrame::pts
int64_t pts
Presentation timestamp in time_base units (time when frame should be shown to user).
Definition: frame.h:501
w
uint8_t w
Definition: llviddspenc.c:38
PerlinContext::random_seed
unsigned int random_seed
Definition: vsrc_perlin.c:43
AVOption
AVOption.
Definition: opt.h:429
data
const char data[16]
Definition: mxf.c:149
OFFSET
#define OFFSET(x)
Definition: vsrc_perlin.c:50
AV_LOG_VERBOSE
#define AV_LOG_VERBOSE
Detailed information.
Definition: log.h:225
float.h
AVFilter::name
const char * name
Filter name.
Definition: avfilter.h:205
perlin_options
static const AVOption perlin_options[]
Definition: vsrc_perlin.c:53
video.h
AVFrame::data
uint8_t * data[AV_NUM_DATA_POINTERS]
pointer to the picture/channel planes.
Definition: frame.h:410
formats.h
perlin_outputs
static const AVFilterPad perlin_outputs[]
Definition: vsrc_perlin.c:154
AVFilterContext::priv
void * priv
private data for use by the filter
Definition: avfilter.h:472
FF_PERLIN_RANDOM_MODE_NB
@ FF_PERLIN_RANDOM_MODE_NB
Definition: perlin.h:35
FF_PERLIN_RANDOM_MODE_SEED
@ FF_PERLIN_RANDOM_MODE_SEED
Definition: perlin.h:34
AVRational::num
int num
Numerator.
Definition: rational.h:59
AVFilterPad
A filter pad used for either input or output.
Definition: filters.h:38
FFPerlin
Perlin generator context.
Definition: perlin.h:42
av_cold
#define av_cold
Definition: attributes.h:90
FLAGS
#define FLAGS
Definition: vsrc_perlin.c:51
AV_OPT_TYPE_DOUBLE
@ AV_OPT_TYPE_DOUBLE
Underlying C type is double.
Definition: opt.h:267
ff_vsrc_perlin
const AVFilter ff_vsrc_perlin
Definition: vsrc_perlin.c:163
av_q2d
static double av_q2d(AVRational a)
Convert an AVRational to a double.
Definition: rational.h:104
lfg.h
filters.h
PerlinContext::w
int w
Definition: vsrc_perlin.c:37
pix_fmts
static enum AVPixelFormat pix_fmts[]
Definition: libkvazaar.c:304
config_props
static int config_props(AVFilterLink *outlink)
Definition: vsrc_perlin.c:95
AV_LOG_DEBUG
#define AV_LOG_DEBUG
Stuff which is only useful for libav* developers.
Definition: log.h:230
ctx
AVFormatContext * ctx
Definition: movenc.c:49
PerlinContext::tscale
double tscale
Definition: vsrc_perlin.c:46
FILTER_OUTPUTS
#define FILTER_OUTPUTS(array)
Definition: filters.h:263
ff_perlin_get
double ff_perlin_get(FFPerlin *perlin, double x, double y, double z)
Compute Perlin noise given the x, y, z coordinates.
Definition: perlin.c:208
PerlinContext::h
int h
Definition: vsrc_perlin.c:37
AVClass
Describe the class of an AVClass context structure.
Definition: log.h:75
NULL
#define NULL
Definition: coverity.c:32
AVRational
Rational number (pair of numerator and denominator).
Definition: rational.h:58
AV_OPT_TYPE_IMAGE_SIZE
@ AV_OPT_TYPE_IMAGE_SIZE
Underlying C type is two consecutive integers.
Definition: opt.h:303
double
double
Definition: af_crystalizer.c:132
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
AV_OPT_TYPE_UINT
@ AV_OPT_TYPE_UINT
Underlying C type is unsigned int.
Definition: opt.h:335
AV_PIX_FMT_GRAY8
@ AV_PIX_FMT_GRAY8
Y , 8bpp.
Definition: pixfmt.h:81
ff_perlin_init
int ff_perlin_init(FFPerlin *perlin, double period, int octaves, double persistence, enum FFPerlinRandomMode random_mode, unsigned int random_seed)
Initialize the Perlin noise generator with parameters.
Definition: perlin.c:101
AVFilterFormatsConfig
Lists of formats / etc.
Definition: avfilter.h:111
ff_filter_link
static FilterLink * ff_filter_link(AVFilterLink *link)
Definition: filters.h:197
PerlinContext::frame_rate
AVRational frame_rate
Definition: vsrc_perlin.c:38
FFPerlinRandomMode
FFPerlinRandomMode
Definition: perlin.h:31
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
for
for(k=2;k<=8;++k)
Definition: h264pred_template.c:425
PerlinContext::pts
uint64_t pts
Definition: vsrc_perlin.c:47
PerlinContext::random_mode
enum FFPerlinRandomMode random_mode
Definition: vsrc_perlin.c:44
PerlinContext::xscale
double xscale
Definition: vsrc_perlin.c:46
query_formats
static int query_formats(const AVFilterContext *ctx, AVFilterFormatsConfig **cfg_in, AVFilterFormatsConfig **cfg_out)
Definition: vsrc_perlin.c:145
i
#define i(width, name, range_min, range_max)
Definition: cbs_h2645.c:256
FILTER_QUERY_FUNC2
#define FILTER_QUERY_FUNC2(func)
Definition: filters.h:239
av_inv_q
static av_always_inline AVRational av_inv_q(AVRational q)
Invert a rational.
Definition: rational.h:159
AVFilterPad::name
const char * name
Pad name.
Definition: filters.h:44
AVFilter
Filter definition.
Definition: avfilter.h:201
ret
ret
Definition: filter_design.txt:187
AVFrame::sample_aspect_ratio
AVRational sample_aspect_ratio
Sample aspect ratio for the video frame, 0/1 if unknown/unspecified.
Definition: frame.h:496
init
static av_cold int init(AVFilterContext *ctx)
Definition: vsrc_perlin.c:77
ff_set_common_formats_from_list2
int ff_set_common_formats_from_list2(const AVFilterContext *ctx, AVFilterFormatsConfig **cfg_in, AVFilterFormatsConfig **cfg_out, const int *fmts)
Definition: formats.c:1016
FF_PERLIN_RANDOM_MODE_KEN
@ FF_PERLIN_RANDOM_MODE_KEN
Definition: perlin.h:33
AVRational::den
int den
Denominator.
Definition: rational.h:60
AV_PIX_FMT_NONE
@ AV_PIX_FMT_NONE
Definition: pixfmt.h:72
AV_OPT_TYPE_INT
@ AV_OPT_TYPE_INT
Underlying C type is int.
Definition: opt.h:259
avfilter.h
PerlinContext::perlin
FFPerlin perlin
Definition: vsrc_perlin.c:40
AVFilterContext
An instance of a filter.
Definition: avfilter.h:457
AVMEDIA_TYPE_VIDEO
@ AVMEDIA_TYPE_VIDEO
Definition: avutil.h:201
PerlinContext::persistence
double persistence
Definition: vsrc_perlin.c:42
request_frame
static int request_frame(AVFilterLink *outlink)
Definition: vsrc_perlin.c:108
FF_PERLIN_RANDOM_MODE_RANDOM
@ FF_PERLIN_RANDOM_MODE_RANDOM
Definition: perlin.h:32
PerlinContext::yscale
double yscale
Definition: vsrc_perlin.c:46
AVFrame::linesize
int linesize[AV_NUM_DATA_POINTERS]
For video, a positive or negative value, which is typically indicating the size in bytes of each pict...
Definition: frame.h:434
av_log
#define av_log(a,...)
Definition: tableprint_vlc.h:27
AV_OPT_TYPE_CONST
@ AV_OPT_TYPE_CONST
Special option type for declaring named constants.
Definition: opt.h:299
PerlinContext
Definition: vsrc_perlin.c:34