FFmpeg coverage


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