FFmpeg coverage


Directory: ../../../ffmpeg/
File: src/libavfilter/vsrc_sierpinski.c
Date: 2024-04-16 15:12:51
Exec Total Coverage
Lines: 0 83 0.0%
Functions: 0 6 0.0%
Branches: 0 42 0.0%

Line Branch Exec Source
1 /*
2 * Copyright (c) 2019 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 /**
22 * @file
23 * Sierpinski carpet fractal renderer
24 */
25
26 #include "avfilter.h"
27 #include "video.h"
28 #include "internal.h"
29 #include "libavutil/imgutils.h"
30 #include "libavutil/intreadwrite.h"
31 #include "libavutil/opt.h"
32 #include "libavutil/lfg.h"
33 #include "libavutil/random_seed.h"
34 #include <float.h>
35 #include <math.h>
36
37 typedef struct SierpinskiContext {
38 const AVClass *class;
39 int w, h;
40 int type;
41 AVRational frame_rate;
42 uint64_t pts;
43
44 int64_t seed;
45 int jump;
46
47 int pos_x, pos_y;
48 int dest_x, dest_y;
49
50 AVLFG lfg;
51 int (*draw_slice)(AVFilterContext *ctx, void *arg, int job, int nb_jobs);
52 } SierpinskiContext;
53
54 #define OFFSET(x) offsetof(SierpinskiContext, x)
55 #define FLAGS AV_OPT_FLAG_VIDEO_PARAM|AV_OPT_FLAG_FILTERING_PARAM
56
57 static const AVOption sierpinski_options[] = {
58 {"size", "set frame size", OFFSET(w), AV_OPT_TYPE_IMAGE_SIZE, {.str="640x480"}, 0, 0, FLAGS },
59 {"s", "set frame size", OFFSET(w), AV_OPT_TYPE_IMAGE_SIZE, {.str="640x480"}, 0, 0, FLAGS },
60 {"rate", "set frame rate", OFFSET(frame_rate), AV_OPT_TYPE_VIDEO_RATE, {.str="25"}, 0, INT_MAX, FLAGS },
61 {"r", "set frame rate", OFFSET(frame_rate), AV_OPT_TYPE_VIDEO_RATE, {.str="25"}, 0, INT_MAX, FLAGS },
62 {"seed", "set the seed", OFFSET(seed), AV_OPT_TYPE_INT64, {.i64=-1}, -1, UINT32_MAX, FLAGS },
63 {"jump", "set the jump", OFFSET(jump), AV_OPT_TYPE_INT, {.i64=100}, 1, 10000, FLAGS },
64 {"type","set fractal type",OFFSET(type), AV_OPT_TYPE_INT, {.i64=0}, 0, 1, FLAGS, .unit = "type" },
65 {"carpet", "sierpinski carpet", 0, AV_OPT_TYPE_CONST, {.i64=0}, 0, 0, FLAGS, .unit = "type" },
66 {"triangle", "sierpinski triangle", 0, AV_OPT_TYPE_CONST, {.i64=1}, 0, 0, FLAGS, .unit = "type" },
67 {NULL},
68 };
69
70 AVFILTER_DEFINE_CLASS(sierpinski);
71
72 static int fill_sierpinski(SierpinskiContext *s, int x, int y)
73 {
74 int pos_x = x + s->pos_x;
75 int pos_y = y + s->pos_y;
76
77 while (pos_x != 0 && pos_y != 0) {
78 if (FFABS(pos_x % 3) == 1 && FFABS(pos_y % 3) == 1)
79 return 1;
80
81 pos_x /= 3;
82 pos_y /= 3;
83 }
84
85 return 0;
86 }
87
88 static int draw_triangle_slice(AVFilterContext *ctx, void *arg, int job, int nb_jobs)
89 {
90 SierpinskiContext *s = ctx->priv;
91 AVFrame *frame = arg;
92 const int width = frame->width;
93 const int height = frame->height;
94 const int start = (height * job ) / nb_jobs;
95 const int end = (height * (job+1)) / nb_jobs;
96 uint8_t *dst = frame->data[0] + start * frame->linesize[0];
97
98 for (int y = start; y < end; y++) {
99 for (int x = 0; x < width; x++) {
100 if ((s->pos_x + x) & (s->pos_y + y)) {
101 AV_WL32(&dst[x*4], 0x00000000);
102 } else {
103 AV_WL32(&dst[x*4], 0xFFFFFFFF);
104 }
105 }
106
107 dst += frame->linesize[0];
108 }
109
110 return 0;
111 }
112
113 static int draw_carpet_slice(AVFilterContext *ctx, void *arg, int job, int nb_jobs)
114 {
115 SierpinskiContext *s = ctx->priv;
116 AVFrame *frame = arg;
117 const int width = frame->width;
118 const int height = frame->height;
119 const int start = (height * job ) / nb_jobs;
120 const int end = (height * (job+1)) / nb_jobs;
121 uint8_t *dst = frame->data[0] + start * frame->linesize[0];
122
123 for (int y = start; y < end; y++) {
124 for (int x = 0; x < width; x++) {
125 if (fill_sierpinski(s, x, y)) {
126 AV_WL32(&dst[x*4], 0x00000000);
127 } else {
128 AV_WL32(&dst[x*4], 0xFFFFFFFF);
129 }
130 }
131
132 dst += frame->linesize[0];
133 }
134
135 return 0;
136 }
137
138 static int config_output(AVFilterLink *outlink)
139 {
140 AVFilterContext *ctx = outlink->src;
141 SierpinskiContext *s = ctx->priv;
142
143 if (av_image_check_size(s->w, s->h, 0, ctx) < 0)
144 return AVERROR(EINVAL);
145
146 outlink->w = s->w;
147 outlink->h = s->h;
148 outlink->time_base = av_inv_q(s->frame_rate);
149 outlink->sample_aspect_ratio = (AVRational) {1, 1};
150 outlink->frame_rate = s->frame_rate;
151 if (s->seed == -1)
152 s->seed = av_get_random_seed();
153 av_lfg_init(&s->lfg, s->seed);
154
155 s->draw_slice = s->type ? draw_triangle_slice : draw_carpet_slice;
156
157 return 0;
158 }
159
160 static void draw_sierpinski(AVFilterContext *ctx, AVFrame *frame)
161 {
162 SierpinskiContext *s = ctx->priv;
163 AVFilterLink *outlink = ctx->outputs[0];
164
165 if (s->pos_x == s->dest_x && s->pos_y == s->dest_y) {
166 unsigned int rnd = av_lfg_get(&s->lfg);
167 int mod = 2 * s->jump + 1;
168
169 s->dest_x += (int)((rnd & 0xffff) % mod) - s->jump;
170 s->dest_y += (int)((rnd >> 16) % mod) - s->jump;
171 } else {
172 if (s->pos_x < s->dest_x)
173 s->pos_x++;
174 else if (s->pos_x > s->dest_x)
175 s->pos_x--;
176
177 if (s->pos_y < s->dest_y)
178 s->pos_y++;
179 else if (s->pos_y > s->dest_y)
180 s->pos_y--;
181 }
182
183 ff_filter_execute(ctx, s->draw_slice, frame, NULL,
184 FFMIN(outlink->h, ff_filter_get_nb_threads(ctx)));
185 }
186
187 static int sierpinski_request_frame(AVFilterLink *link)
188 {
189 SierpinskiContext *s = link->src->priv;
190 AVFrame *frame = ff_get_video_buffer(link, s->w, s->h);
191
192 if (!frame)
193 return AVERROR(ENOMEM);
194
195 frame->sample_aspect_ratio = (AVRational) {1, 1};
196 frame->pts = s->pts++;
197 frame->duration = 1;
198
199 draw_sierpinski(link->src, frame);
200
201 return ff_filter_frame(link, frame);
202 }
203
204 static const AVFilterPad sierpinski_outputs[] = {
205 {
206 .name = "default",
207 .type = AVMEDIA_TYPE_VIDEO,
208 .request_frame = sierpinski_request_frame,
209 .config_props = config_output,
210 },
211 };
212
213 const AVFilter ff_vsrc_sierpinski = {
214 .name = "sierpinski",
215 .description = NULL_IF_CONFIG_SMALL("Render a Sierpinski fractal."),
216 .priv_size = sizeof(SierpinskiContext),
217 .priv_class = &sierpinski_class,
218 .inputs = NULL,
219 FILTER_OUTPUTS(sierpinski_outputs),
220 FILTER_SINGLE_PIXFMT(AV_PIX_FMT_0BGR32),
221 .flags = AVFILTER_FLAG_SLICE_THREADS,
222 };
223