FFmpeg coverage


Directory: ../../../ffmpeg/
File: src/libavfilter/vsrc_perlin.c
Date: 2026-04-20 20:24:43
Exec Total Coverage
Lines: 0 39 0.0%
Functions: 0 4 0.0%
Branches: 0 8 0.0%

Line Branch Exec Source
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;
38 AVRational frame_rate;
39
40 FFPerlin perlin;
41 int octaves;
42 double persistence;
43 unsigned int random_seed;
44 /* enum FFPerlinRandomMode */
45 int random_mode;
46
47 double xscale, yscale, tscale;
48 uint64_t pts;
49 } PerlinContext;
50
51 #define OFFSET(x) offsetof(PerlinContext, x)
52 #define FLAGS AV_OPT_FLAG_VIDEO_PARAM|AV_OPT_FLAG_FILTERING_PARAM
53
54 static const AVOption perlin_options[] = {
55 { "size", "set video size", OFFSET(w), AV_OPT_TYPE_IMAGE_SIZE, {.str="320x240"}, 0, 0, FLAGS },
56 { "s", "set video size", OFFSET(w), AV_OPT_TYPE_IMAGE_SIZE, {.str="320x240"}, 0, 0, FLAGS },
57 { "rate", "set video rate", OFFSET(frame_rate), AV_OPT_TYPE_VIDEO_RATE, {.str="25"}, 0, INT_MAX, FLAGS },
58 { "r", "set video rate", OFFSET(frame_rate), AV_OPT_TYPE_VIDEO_RATE, {.str="25"}, 0, INT_MAX, FLAGS },
59 { "octaves", "set the number of components to use to generate the noise", OFFSET(octaves), AV_OPT_TYPE_INT, {.i64=1}, 1, INT_MAX, FLAGS },
60 { "persistence", "set the octaves persistence", OFFSET(persistence), AV_OPT_TYPE_DOUBLE, {.dbl=1}, 0.0, DBL_MAX, FLAGS },
61
62 { "xscale", "set x-scale factor", OFFSET(xscale), AV_OPT_TYPE_DOUBLE, {.dbl=1}, 0.0, DBL_MAX, FLAGS },
63 { "yscale", "set y-scale factor", OFFSET(yscale), AV_OPT_TYPE_DOUBLE, {.dbl=1}, 0.0, DBL_MAX, FLAGS },
64 { "tscale", "set t-scale factor", OFFSET(tscale), AV_OPT_TYPE_DOUBLE, {.dbl=1}, 0.0, DBL_MAX, FLAGS },
65
66 { "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" },
67 { "random", "compute and use random seed", 0, AV_OPT_TYPE_CONST, {.i64=FF_PERLIN_RANDOM_MODE_RANDOM}, 0, 0, FLAGS, .unit = "random_mode" },
68 { "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" },
69 { "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" },
70
71 { "random_seed", "set the seed for filling the initial pattern", OFFSET(random_seed), AV_OPT_TYPE_UINT, {.i64=0}, 0, UINT_MAX, FLAGS },
72 { "seed", "set the seed for filling the initial pattern", OFFSET(random_seed), AV_OPT_TYPE_UINT, {.i64=0}, 0, UINT_MAX, FLAGS },
73 { NULL }
74 };
75
76 AVFILTER_DEFINE_CLASS(perlin);
77
78 static av_cold int init(AVFilterContext *ctx)
79 {
80 PerlinContext *perlin = ctx->priv;
81 int ret;
82
83 if (ret = ff_perlin_init(&perlin->perlin, -1, perlin->octaves, perlin->persistence,
84 perlin->random_mode, perlin->random_seed)) {
85 return ret;
86 }
87
88 av_log(ctx, AV_LOG_VERBOSE,
89 "s:%dx%d r:%d/%d octaves:%d persistence:%f xscale:%f yscale:%f tscale:%f\n",
90 perlin->w, perlin->h, perlin->frame_rate.num, perlin->frame_rate.den,
91 perlin->octaves, perlin->persistence,
92 perlin->xscale, perlin->yscale, perlin->tscale);
93 return 0;
94 }
95
96 static int config_props(AVFilterLink *outlink)
97 {
98 PerlinContext *perlin = outlink->src->priv;
99 FilterLink *l = ff_filter_link(outlink);
100
101 outlink->w = perlin->w;
102 outlink->h = perlin->h;
103 outlink->time_base = av_inv_q(perlin->frame_rate);
104 l->frame_rate = perlin->frame_rate;
105
106 return 0;
107 }
108
109 static int request_frame(AVFilterLink *outlink)
110 {
111 AVFilterContext *ctx = outlink->src;
112 PerlinContext *perlin = ctx->priv;
113 AVFrame *picref = ff_get_video_buffer(outlink, perlin->w, perlin->h);
114 int i, j;
115 uint8_t *data0, *data;
116 double x, y, t;
117
118 if (!picref)
119 return AVERROR(ENOMEM);
120
121 picref->sample_aspect_ratio = (AVRational) {1, 1};
122 picref->pts = perlin->pts++;
123 picref->duration = 1;
124
125 t = perlin->tscale * (perlin->pts * av_q2d(outlink->time_base));
126 data0 = picref->data[0];
127
128 for (i = 0; i < perlin->h; i++) {
129 y = perlin->yscale * (double)i / perlin->h;
130
131 data = data0;
132
133 for (j = 0; j < perlin->w; j++) {
134 double res;
135 x = perlin->xscale * (double)j / perlin->w;
136 res = ff_perlin_get(&perlin->perlin, x, y, t);
137 av_log(ctx, AV_LOG_DEBUG, "x:%f y:%f t:%f => %f\n", x, y, t, res);
138 *data++ = res * 255;
139 }
140 data0 += picref->linesize[0];
141 }
142
143 return ff_filter_frame(outlink, picref);
144 }
145
146 static int query_formats(const AVFilterContext *ctx,
147 AVFilterFormatsConfig **cfg_in,
148 AVFilterFormatsConfig **cfg_out)
149 {
150 enum AVPixelFormat pix_fmts[] = { AV_PIX_FMT_GRAY8, AV_PIX_FMT_NONE };
151
152 return ff_set_pixel_formats_from_list2(ctx, cfg_in, cfg_out, pix_fmts);
153 }
154
155 static const AVFilterPad perlin_outputs[] = {
156 {
157 .name = "default",
158 .type = AVMEDIA_TYPE_VIDEO,
159 .request_frame = request_frame,
160 .config_props = config_props,
161 },
162 };
163
164 const FFFilter ff_vsrc_perlin = {
165 .p.name = "perlin",
166 .p.description = NULL_IF_CONFIG_SMALL("Generate Perlin noise"),
167 .p.priv_class = &perlin_class,
168 .p.inputs = NULL,
169 .priv_size = sizeof(PerlinContext),
170 .init = init,
171 FILTER_OUTPUTS(perlin_outputs),
172 FILTER_QUERY_FUNC2(query_formats),
173 };
174