FFmpeg coverage


Directory: ../../../ffmpeg/
File: src/libavfilter/vsrc_mandelbrot.c
Date: 2026-09-25 02:02:46
Exec Total Coverage
Lines: 0 203 0.0%
Functions: 0 7 0.0%
Branches: 0 127 0.0%

Line Branch Exec Source
1 /*
2 * Copyright (c) 2011 Michael Niedermayer
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 * The vsrc_color filter from Stefano Sabatini was used as template to create
21 * this
22 */
23
24 /**
25 * @file
26 * Mandelbrot fractal renderer
27 */
28
29 #include "avfilter.h"
30 #include "filters.h"
31 #include "video.h"
32 #include "libavutil/imgutils.h"
33 #include "libavutil/mem.h"
34 #include "libavutil/opt.h"
35 #include <float.h>
36 #include <math.h>
37
38 #define SQR(a) ((a)*(a))
39
40 enum Outer{
41 ITERATION_COUNT,
42 NORMALIZED_ITERATION_COUNT,
43 WHITE,
44 OUTZ,
45 };
46
47 enum Inner{
48 BLACK,
49 PERIOD,
50 CONVTIME,
51 MINCOL,
52 };
53
54 typedef struct Point {
55 double p[2];
56 uint32_t val;
57 } Point;
58
59 typedef struct MBContext {
60 const AVClass *class;
61 int w, h;
62 AVRational frame_rate;
63 uint64_t pts;
64 int maxiter;
65 double start_x;
66 double start_y;
67 double start_scale;
68 double end_scale;
69 double end_pts;
70 double bailout;
71 int outer;
72 int inner;
73 int cache_allocated;
74 int cache_used;
75 Point *point_cache;
76 Point *next_cache;
77 double (*zyklus)[2];
78 uint32_t dither;
79
80 double morphxf;
81 double morphyf;
82 double morphamp;
83 } MBContext;
84
85 #define OFFSET(x) offsetof(MBContext, x)
86 #define FLAGS AV_OPT_FLAG_VIDEO_PARAM|AV_OPT_FLAG_FILTERING_PARAM
87
88 static const AVOption mandelbrot_options[] = {
89 {"size", "set frame size", OFFSET(w), AV_OPT_TYPE_IMAGE_SIZE, {.str="640x480"}, 0, 0, FLAGS },
90 {"s", "set frame size", OFFSET(w), AV_OPT_TYPE_IMAGE_SIZE, {.str="640x480"}, 0, 0, FLAGS },
91 {"rate", "set frame rate", OFFSET(frame_rate), AV_OPT_TYPE_VIDEO_RATE, {.str="25"}, 0, INT_MAX, FLAGS },
92 {"r", "set frame rate", OFFSET(frame_rate), AV_OPT_TYPE_VIDEO_RATE, {.str="25"}, 0, INT_MAX, FLAGS },
93 {"maxiter", "set max iterations number", OFFSET(maxiter), AV_OPT_TYPE_INT, {.i64=7189}, 1, INT_MAX, FLAGS },
94 {"start_x", "set the initial x position", OFFSET(start_x), AV_OPT_TYPE_DOUBLE, {.dbl=-0.743643887037158704752191506114774}, -100, 100, FLAGS },
95 {"start_y", "set the initial y position", OFFSET(start_y), AV_OPT_TYPE_DOUBLE, {.dbl=-0.131825904205311970493132056385139}, -100, 100, FLAGS },
96 {"start_scale", "set the initial scale value", OFFSET(start_scale), AV_OPT_TYPE_DOUBLE, {.dbl=3.0}, 0, FLT_MAX, FLAGS },
97 {"end_scale", "set the terminal scale value", OFFSET(end_scale), AV_OPT_TYPE_DOUBLE, {.dbl=0.3}, 0, FLT_MAX, FLAGS },
98 {"end_pts", "set the terminal pts value", OFFSET(end_pts), AV_OPT_TYPE_DOUBLE, {.dbl=400}, 0, INT64_MAX, FLAGS },
99 {"bailout", "set the bailout value", OFFSET(bailout), AV_OPT_TYPE_DOUBLE, {.dbl=10}, 0, FLT_MAX, FLAGS },
100 {"morphxf", "set morph x frequency", OFFSET(morphxf), AV_OPT_TYPE_DOUBLE, {.dbl=0.01}, -FLT_MAX, FLT_MAX, FLAGS },
101 {"morphyf", "set morph y frequency", OFFSET(morphyf), AV_OPT_TYPE_DOUBLE, {.dbl=0.0123}, -FLT_MAX, FLT_MAX, FLAGS },
102 {"morphamp", "set morph amplitude", OFFSET(morphamp), AV_OPT_TYPE_DOUBLE, {.dbl=0}, -FLT_MAX, FLT_MAX, FLAGS },
103
104 {"outer", "set outer coloring mode", OFFSET(outer), AV_OPT_TYPE_INT, {.i64=NORMALIZED_ITERATION_COUNT}, 0, INT_MAX, FLAGS, .unit = "outer" },
105 {"iteration_count", "set iteration count mode", 0, AV_OPT_TYPE_CONST, {.i64=ITERATION_COUNT}, INT_MIN, INT_MAX, FLAGS, .unit = "outer" },
106 {"normalized_iteration_count", "set normalized iteration count mode", 0, AV_OPT_TYPE_CONST, {.i64=NORMALIZED_ITERATION_COUNT}, INT_MIN, INT_MAX, FLAGS, .unit = "outer" },
107 {"white", "set white mode", 0, AV_OPT_TYPE_CONST, {.i64=WHITE}, INT_MIN, INT_MAX, FLAGS, .unit = "outer" },
108 {"outz", "set outz mode", 0, AV_OPT_TYPE_CONST, {.i64=OUTZ}, INT_MIN, INT_MAX, FLAGS, .unit = "outer" },
109
110 {"inner", "set inner coloring mode", OFFSET(inner), AV_OPT_TYPE_INT, {.i64=MINCOL}, 0, INT_MAX, FLAGS, .unit = "inner" },
111 {"black", "set black mode", 0, AV_OPT_TYPE_CONST, {.i64=BLACK}, INT_MIN, INT_MAX, FLAGS, .unit = "inner"},
112 {"period", "set period mode", 0, AV_OPT_TYPE_CONST, {.i64=PERIOD}, INT_MIN, INT_MAX, FLAGS, .unit = "inner"},
113 {"convergence", "show time until convergence", 0, AV_OPT_TYPE_CONST, {.i64=CONVTIME}, INT_MIN, INT_MAX, FLAGS, .unit = "inner"},
114 {"mincol", "color based on point closest to the origin of the iterations", 0, AV_OPT_TYPE_CONST, {.i64=MINCOL}, INT_MIN, INT_MAX, FLAGS, .unit = "inner"},
115
116 {NULL},
117 };
118
119 AVFILTER_DEFINE_CLASS(mandelbrot);
120
121 ✗ static av_cold int init(AVFilterContext *ctx)
122 {
123 ✗ MBContext *s = ctx->priv;
124
125 ✗ s->bailout *= s->bailout;
126
127 ✗ s->start_scale /=s->h;
128 ✗ s->end_scale /=s->h;
129
130 ✗ s->cache_allocated = s->w * s->h * 3;
131 ✗ s->cache_used = 0;
132 ✗ s->point_cache= av_malloc_array(s->cache_allocated, sizeof(*s->point_cache));
133 ✗ s-> next_cache= av_malloc_array(s->cache_allocated, sizeof(*s-> next_cache));
134 ✗ s-> zyklus = av_malloc_array(s->maxiter + 16, sizeof(*s->zyklus));
135
136 ✗ if (!s->point_cache || !s->next_cache || !s->zyklus)
137 ✗ return AVERROR(ENOMEM);
138
139 ✗ return 0;
140 }
141
142 ✗ static av_cold void uninit(AVFilterContext *ctx)
143 {
144 ✗ MBContext *s = ctx->priv;
145
146 ✗ av_freep(&s->point_cache);
147 ✗ av_freep(&s-> next_cache);
148 ✗ av_freep(&s->zyklus);
149 ✗ }
150
151 ✗ static int config_props(AVFilterLink *outlink)
152 {
153 ✗ AVFilterContext *ctx = outlink->src;
154 ✗ FilterLink *l = ff_filter_link(outlink);
155 ✗ MBContext *s = ctx->priv;
156
157 ✗ if (av_image_check_size(s->w, s->h, 0, ctx) < 0)
158 ✗ return AVERROR(EINVAL);
159
160 ✗ outlink->w = s->w;
161 ✗ outlink->h = s->h;
162 ✗ outlink->time_base = av_inv_q(s->frame_rate);
163 ✗ l->frame_rate = s->frame_rate;
164
165 ✗ return 0;
166 }
167
168 ✗ static void fill_from_cache(AVFilterContext *ctx, uint32_t *color, int *in_cidx, int *out_cidx, double py, double scale){
169 ✗ MBContext *s = ctx->priv;
170 ✗ if(s->morphamp)
171 ✗ return;
172 ✗ for(; *in_cidx < s->cache_used; (*in_cidx)++){
173 ✗ Point *p= &s->point_cache[*in_cidx];
174 int x;
175 ✗ if(p->p[1] > py)
176 ✗ break;
177 ✗ x= lrint((p->p[0] - s->start_x) / scale + s->w/2);
178 ✗ if(x<0 || x >= s->w)
179 ✗ continue;
180 ✗ if(color) color[x] = p->val;
181 ✗ if(out_cidx && *out_cidx < s->cache_allocated)
182 ✗ s->next_cache[(*out_cidx)++]= *p;
183 }
184 }
185
186 ✗ static int interpol(MBContext *s, uint32_t *color, int x, int y, int linesize)
187 {
188 uint32_t a,b,c,d, i;
189 ✗ uint32_t ipol=0xFF000000;
190 int dist;
191
192 ✗ if(!x || !y || x+1==s->w || y+1==s->h)
193 ✗ return 0;
194
195 ✗ dist= FFMAX(FFABS(x-(s->w>>1))*s->h, FFABS(y-(s->h>>1))*s->w);
196
197 ✗ if(dist<(s->w*s->h>>3))
198 ✗ return 0;
199
200 ✗ a=color[(x+1) + (y+0)*linesize];
201 ✗ b=color[(x-1) + (y+1)*linesize];
202 ✗ c=color[(x+0) + (y+1)*linesize];
203 ✗ d=color[(x+1) + (y+1)*linesize];
204
205 ✗ if(a&&c){
206 ✗ b= color[(x-1) + (y+0)*linesize];
207 ✗ d= color[(x+0) + (y-1)*linesize];
208 ✗ }else if(b&&d){
209 ✗ a= color[(x+1) + (y-1)*linesize];
210 ✗ c= color[(x-1) + (y-1)*linesize];
211 ✗ }else if(c){
212 ✗ d= color[(x+0) + (y-1)*linesize];
213 ✗ a= color[(x-1) + (y+0)*linesize];
214 ✗ b= color[(x+1) + (y-1)*linesize];
215 ✗ }else if(d){
216 ✗ c= color[(x-1) + (y-1)*linesize];
217 ✗ a= color[(x-1) + (y+0)*linesize];
218 ✗ b= color[(x+1) + (y-1)*linesize];
219 }else
220 ✗ return 0;
221
222 ✗ for(i=0; i<3; i++){
223 ✗ int s= 8*i;
224 ✗ uint8_t ac= a>>s;
225 ✗ uint8_t bc= b>>s;
226 ✗ uint8_t cc= c>>s;
227 ✗ uint8_t dc= d>>s;
228 ✗ int ipolab= (ac + bc);
229 ✗ int ipolcd= (cc + dc);
230 ✗ if(FFABS(ipolab - ipolcd) > 5)
231 ✗ return 0;
232 ✗ if(FFABS(ac-bc)+FFABS(cc-dc) > 20)
233 ✗ return 0;
234 ✗ ipol |= ((ipolab + ipolcd + 2)/4)<<s;
235 }
236 ✗ color[x + y*linesize]= ipol;
237 ✗ return 1;
238 }
239
240 ✗ static void draw_mandelbrot(AVFilterContext *ctx, uint32_t *color, int linesize, int64_t pts)
241 {
242 ✗ MBContext *s = ctx->priv;
243 ✗ int x,y,i, in_cidx=0, next_cidx=0, tmp_cidx;
244 ✗ double scale= s->start_scale*pow(s->end_scale/s->start_scale, pts/s->end_pts);
245 ✗ int use_zyklus=0;
246 ✗ fill_from_cache(ctx, NULL, &in_cidx, NULL, s->start_y+scale*(-s->h/2-0.5), scale);
247 ✗ tmp_cidx= in_cidx;
248 ✗ memset(color, 0, sizeof(*color)*s->w);
249 ✗ for(y=0; y<s->h; y++){
250 ✗ int y1= y+1;
251 ✗ const double ci=s->start_y+scale*(y-s->h/2);
252 ✗ fill_from_cache(ctx, NULL, &in_cidx, &next_cidx, ci, scale);
253 ✗ if(y1<s->h){
254 ✗ memset(color+linesize*y1, 0, sizeof(*color)*s->w);
255 ✗ fill_from_cache(ctx, color+linesize*y1, &tmp_cidx, NULL, ci + 3*scale/2, scale);
256 }
257
258 ✗ for(x=0; x<s->w; x++){
259 ✗ float av_uninit(epsilon);
260 ✗ const double cr=s->start_x+scale*(x-s->w/2);
261 ✗ double zr=cr;
262 ✗ double zi=ci;
263 ✗ uint32_t c=0;
264 ✗ double dv= s->dither / (double)(1LL<<32);
265 ✗ s->dither= s->dither*1664525+1013904223;
266
267 ✗ if(color[x + y*linesize] & 0xFF000000)
268 ✗ continue;
269 ✗ if(!s->morphamp){
270 ✗ if(interpol(s, color, x, y, linesize)){
271 ✗ if(next_cidx < s->cache_allocated){
272 ✗ s->next_cache[next_cidx ].p[0]= cr;
273 ✗ s->next_cache[next_cidx ].p[1]= ci;
274 ✗ s->next_cache[next_cidx++].val = color[x + y*linesize];
275 }
276 ✗ continue;
277 }
278 }else{
279 ✗ zr += cos(pts * s->morphxf) * s->morphamp;
280 ✗ zi += sin(pts * s->morphyf) * s->morphamp;
281 }
282
283 ✗ use_zyklus= (x==0 || s->inner!=BLACK ||color[x-1 + y*linesize] == 0xFF000000);
284 ✗ if(use_zyklus)
285 ✗ epsilon= scale*(abs(x-s->w/2) + abs(y-s->h/2))/s->w;
286
287 #define Z_Z2_C(outr,outi,inr,ini)\
288 outr= inr*inr - ini*ini + cr;\
289 outi= 2*inr*ini + ci;
290
291 #define Z_Z2_C_ZYKLUS(outr,outi,inr,ini, Z)\
292 Z_Z2_C(outr,outi,inr,ini)\
293 if(use_zyklus){\
294 if(Z && fabs(s->zyklus[i>>1][0]-outr)+fabs(s->zyklus[i>>1][1]-outi) <= epsilon)\
295 break;\
296 }\
297 s->zyklus[i][0]= outr;\
298 s->zyklus[i][1]= outi;\
299
300
301
302 ✗ for(i=0; i<s->maxiter-8; i++){
303 double t;
304 ✗ Z_Z2_C_ZYKLUS(t, zi, zr, zi, 0)
305 ✗ i++;
306 ✗ Z_Z2_C_ZYKLUS(zr, zi, t, zi, 1)
307 ✗ i++;
308 ✗ Z_Z2_C_ZYKLUS(t, zi, zr, zi, 0)
309 ✗ i++;
310 ✗ Z_Z2_C_ZYKLUS(zr, zi, t, zi, 1)
311 ✗ i++;
312 ✗ Z_Z2_C_ZYKLUS(t, zi, zr, zi, 0)
313 ✗ i++;
314 ✗ Z_Z2_C_ZYKLUS(zr, zi, t, zi, 1)
315 ✗ i++;
316 ✗ Z_Z2_C_ZYKLUS(t, zi, zr, zi, 0)
317 ✗ i++;
318 ✗ Z_Z2_C_ZYKLUS(zr, zi, t, zi, 1)
319 ✗ if(zr*zr + zi*zi > s->bailout){
320 ✗ i-= FFMIN(7, i);
321 ✗ for(; i<s->maxiter; i++){
322 ✗ zr= s->zyklus[i][0];
323 ✗ zi= s->zyklus[i][1];
324 ✗ if(zr*zr + zi*zi > s->bailout){
325 ✗ switch(s->outer){
326 ✗ case ITERATION_COUNT:
327 ✗ zr = i;
328 ✗ c = lrintf((sinf(zr)+1)*127) + lrintf((sinf(zr/1.234)+1)*127)*256*256 + lrintf((sinf(zr/100)+1)*127)*256;
329 ✗ break;
330 ✗ case NORMALIZED_ITERATION_COUNT:
331 ✗ zr = i + log2(log(s->bailout) / log(zr*zr + zi*zi));
332 ✗ c = lrintf((sinf(zr)+1)*127) + lrintf((sinf(zr/1.234)+1)*127)*256*256 + lrintf((sinf(zr/100)+1)*127)*256;
333 ✗ break;
334 ✗ case WHITE:
335 ✗ c = 0xFFFFFF;
336 ✗ break;
337 ✗ case OUTZ:
338 ✗ zr /= s->bailout;
339 ✗ zi /= s->bailout;
340 ✗ c = (((int)(zr*128+128))&0xFF)*256 + (((int)(zi*128+128))&0xFF);
341 }
342 ✗ break;
343 }
344 }
345 ✗ break;
346 }
347 }
348 ✗ if(!c){
349 ✗ if(s->inner==PERIOD){
350 int j;
351 ✗ for(j=i-1; j; j--)
352 ✗ if(SQR(s->zyklus[j][0]-zr) + SQR(s->zyklus[j][1]-zi) < epsilon*epsilon*10)
353 ✗ break;
354 ✗ if(j){
355 ✗ c= i-j;
356 ✗ c= ((c<<5)&0xE0) + ((c<<10)&0xE000) + ((c<<15)&0xE00000);
357 }
358 ✗ }else if(s->inner==CONVTIME){
359 ✗ c= floor(i*255.0/s->maxiter+dv)*0x010101;
360 ✗ } else if(s->inner==MINCOL){
361 int j;
362 ✗ double closest=9999;
363 ✗ int closest_index=0;
364 ✗ for(j=i-1; j>=0; j--)
365 ✗ if(SQR(s->zyklus[j][0]) + SQR(s->zyklus[j][1]) < closest){
366 ✗ closest= SQR(s->zyklus[j][0]) + SQR(s->zyklus[j][1]);
367 ✗ closest_index= j;
368 }
369 ✗ closest = sqrt(closest);
370 ✗ c= lrintf((s->zyklus[closest_index][0]/closest+1)*127+dv) + lrintf((s->zyklus[closest_index][1]/closest+1)*127+dv)*256;
371 }
372 }
373 ✗ c |= 0xFF000000;
374 ✗ color[x + y*linesize]= c;
375 ✗ if(next_cidx < s->cache_allocated){
376 ✗ s->next_cache[next_cidx ].p[0]= cr;
377 ✗ s->next_cache[next_cidx ].p[1]= ci;
378 ✗ s->next_cache[next_cidx++].val = c;
379 }
380 }
381 ✗ fill_from_cache(ctx, NULL, &in_cidx, &next_cidx, ci + scale/2, scale);
382 }
383 ✗ FFSWAP(void*, s->next_cache, s->point_cache);
384 ✗ s->cache_used = next_cidx;
385 ✗ if(s->cache_used == s->cache_allocated)
386 ✗ av_log(ctx, AV_LOG_INFO, "Mandelbrot cache is too small!\n");
387 ✗ }
388
389 ✗ static int request_frame(AVFilterLink *link)
390 {
391 ✗ MBContext *s = link->src->priv;
392 ✗ AVFrame *picref = ff_get_video_buffer(link, s->w, s->h);
393 ✗ if (!picref)
394 ✗ return AVERROR(ENOMEM);
395
396 ✗ picref->sample_aspect_ratio = (AVRational) {1, 1};
397 ✗ picref->pts = s->pts++;
398 ✗ picref->duration = 1;
399
400 ✗ draw_mandelbrot(link->src, (uint32_t*)picref->data[0], picref->linesize[0]/4, picref->pts);
401 ✗ return ff_filter_frame(link, picref);
402 }
403
404 static const AVFilterPad mandelbrot_outputs[] = {
405 {
406 .name = "default",
407 .type = AVMEDIA_TYPE_VIDEO,
408 .request_frame = request_frame,
409 .config_props = config_props,
410 },
411 };
412
413 const FFFilter ff_vsrc_mandelbrot = {
414 .p.name = "mandelbrot",
415 .p.description = NULL_IF_CONFIG_SMALL("Render a Mandelbrot fractal."),
416 .p.priv_class = &mandelbrot_class,
417 .p.inputs = NULL,
418 .priv_size = sizeof(MBContext),
419 .init = init,
420 .uninit = uninit,
421 FILTER_OUTPUTS(mandelbrot_outputs),
422 FILTER_SINGLE_PIXFMT(AV_PIX_FMT_0BGR32),
423 };
424