2 * Copyright (c) 2011 Michael Niedermayer
4 * This file is part of FFmpeg.
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.
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.
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
20 * The vsrc_color filter from Stefano Sabatini was used as template to create
26 * Mandelbrot fractal renderer
33 #include "libavutil/imgutils.h"
34 #include "libavutil/opt.h"
35 #include "libavutil/parseutils.h"
39 #define SQR(a) ((a)*(a))
43 NORMALIZED_ITERATION_COUNT,
55 typedef struct Point {
60 typedef struct MBContext {
63 AVRational frame_rate;
86 #define OFFSET(x) offsetof(MBContext, x)
87 #define FLAGS AV_OPT_FLAG_VIDEO_PARAM|AV_OPT_FLAG_FILTERING_PARAM
89 static const AVOption mandelbrot_options[] = {
90 {"size", "set frame size", OFFSET(w), AV_OPT_TYPE_IMAGE_SIZE, {.str="640x480"}, 0, 0, FLAGS },
91 {"s", "set frame size", OFFSET(w), AV_OPT_TYPE_IMAGE_SIZE, {.str="640x480"}, 0, 0, FLAGS },
92 {"rate", "set frame rate", OFFSET(frame_rate), AV_OPT_TYPE_VIDEO_RATE, {.str="25"}, 0, INT_MAX, FLAGS },
93 {"r", "set frame rate", OFFSET(frame_rate), AV_OPT_TYPE_VIDEO_RATE, {.str="25"}, 0, INT_MAX, FLAGS },
94 {"maxiter", "set max iterations number", OFFSET(maxiter), AV_OPT_TYPE_INT, {.i64=7189}, 1, INT_MAX, FLAGS },
95 {"start_x", "set the initial x position", OFFSET(start_x), AV_OPT_TYPE_DOUBLE, {.dbl=-0.743643887037158704752191506114774}, -100, 100, FLAGS },
96 {"start_y", "set the initial y position", OFFSET(start_y), AV_OPT_TYPE_DOUBLE, {.dbl=-0.131825904205311970493132056385139}, -100, 100, FLAGS },
97 {"start_scale", "set the initial scale value", OFFSET(start_scale), AV_OPT_TYPE_DOUBLE, {.dbl=3.0}, 0, FLT_MAX, FLAGS },
98 {"end_scale", "set the terminal scale value", OFFSET(end_scale), AV_OPT_TYPE_DOUBLE, {.dbl=0.3}, 0, FLT_MAX, FLAGS },
99 {"end_pts", "set the terminal pts value", OFFSET(end_pts), AV_OPT_TYPE_DOUBLE, {.dbl=400}, 0, INT64_MAX, FLAGS },
100 {"bailout", "set the bailout value", OFFSET(bailout), AV_OPT_TYPE_DOUBLE, {.dbl=10}, 0, FLT_MAX, FLAGS },
101 {"morphxf", "set morph x frequency", OFFSET(morphxf), AV_OPT_TYPE_DOUBLE, {.dbl=0.01}, -FLT_MAX, FLT_MAX, FLAGS },
102 {"morphyf", "set morph y frequency", OFFSET(morphyf), AV_OPT_TYPE_DOUBLE, {.dbl=0.0123}, -FLT_MAX, FLT_MAX, FLAGS },
103 {"morphamp", "set morph amplitude", OFFSET(morphamp), AV_OPT_TYPE_DOUBLE, {.dbl=0}, -FLT_MAX, FLT_MAX, FLAGS },
105 {"outer", "set outer coloring mode", OFFSET(outer), AV_OPT_TYPE_INT, {.i64=NORMALIZED_ITERATION_COUNT}, 0, INT_MAX, FLAGS, "outer" },
106 {"iteration_count", "set iteration count mode", 0, AV_OPT_TYPE_CONST, {.i64=ITERATION_COUNT}, INT_MIN, INT_MAX, FLAGS, "outer" },
107 {"normalized_iteration_count", "set normalized iteration count mode", 0, AV_OPT_TYPE_CONST, {.i64=NORMALIZED_ITERATION_COUNT}, INT_MIN, INT_MAX, FLAGS, "outer" },
108 {"white", "set white mode", 0, AV_OPT_TYPE_CONST, {.i64=WHITE}, INT_MIN, INT_MAX, FLAGS, "outer" },
109 {"outz", "set outz mode", 0, AV_OPT_TYPE_CONST, {.i64=OUTZ}, INT_MIN, INT_MAX, FLAGS, "outer" },
111 {"inner", "set inner coloring mode", OFFSET(inner), AV_OPT_TYPE_INT, {.i64=MINCOL}, 0, INT_MAX, FLAGS, "inner" },
112 {"black", "set black mode", 0, AV_OPT_TYPE_CONST, {.i64=BLACK}, INT_MIN, INT_MAX, FLAGS, "inner"},
113 {"period", "set period mode", 0, AV_OPT_TYPE_CONST, {.i64=PERIOD}, INT_MIN, INT_MAX, FLAGS, "inner"},
114 {"convergence", "show time until convergence", 0, AV_OPT_TYPE_CONST, {.i64=CONVTIME}, INT_MIN, INT_MAX, FLAGS, "inner"},
115 {"mincol", "color based on point closest to the origin of the iterations", 0, AV_OPT_TYPE_CONST, {.i64=MINCOL}, INT_MIN, INT_MAX, FLAGS, "inner"},
120 AVFILTER_DEFINE_CLASS(mandelbrot);
122 static av_cold int init(AVFilterContext *ctx)
124 MBContext *s = ctx->priv;
126 s->bailout *= s->bailout;
128 s->start_scale /=s->h;
131 s->cache_allocated = s->w * s->h * 3;
133 s->point_cache= av_malloc_array(s->cache_allocated, sizeof(*s->point_cache));
134 s-> next_cache= av_malloc_array(s->cache_allocated, sizeof(*s-> next_cache));
135 s-> zyklus = av_malloc_array(s->maxiter + 16, sizeof(*s->zyklus));
140 static av_cold void uninit(AVFilterContext *ctx)
142 MBContext *s = ctx->priv;
144 av_freep(&s->point_cache);
145 av_freep(&s-> next_cache);
146 av_freep(&s->zyklus);
149 static int query_formats(AVFilterContext *ctx)
151 static const enum AVPixelFormat pix_fmts[] = {
156 AVFilterFormats *fmts_list = ff_make_format_list(pix_fmts);
158 return AVERROR(ENOMEM);
159 return ff_set_common_formats(ctx, fmts_list);
162 static int config_props(AVFilterLink *inlink)
164 AVFilterContext *ctx = inlink->src;
165 MBContext *s = ctx->priv;
167 if (av_image_check_size(s->w, s->h, 0, ctx) < 0)
168 return AVERROR(EINVAL);
172 inlink->time_base = av_inv_q(s->frame_rate);
177 static void fill_from_cache(AVFilterContext *ctx, uint32_t *color, int *in_cidx, int *out_cidx, double py, double scale){
178 MBContext *s = ctx->priv;
181 for(; *in_cidx < s->cache_used; (*in_cidx)++){
182 Point *p= &s->point_cache[*in_cidx];
186 x= lrint((p->p[0] - s->start_x) / scale + s->w/2);
189 if(color) color[x] = p->val;
190 if(out_cidx && *out_cidx < s->cache_allocated)
191 s->next_cache[(*out_cidx)++]= *p;
195 static int interpol(MBContext *s, uint32_t *color, int x, int y, int linesize)
198 uint32_t ipol=0xFF000000;
201 if(!x || !y || x+1==s->w || y+1==s->h)
204 dist= FFMAX(FFABS(x-(s->w>>1))*s->h, FFABS(y-(s->h>>1))*s->w);
206 if(dist<(s->w*s->h>>3))
209 a=color[(x+1) + (y+0)*linesize];
210 b=color[(x-1) + (y+1)*linesize];
211 c=color[(x+0) + (y+1)*linesize];
212 d=color[(x+1) + (y+1)*linesize];
215 b= color[(x-1) + (y+0)*linesize];
216 d= color[(x+0) + (y-1)*linesize];
218 a= color[(x+1) + (y-1)*linesize];
219 c= color[(x-1) + (y-1)*linesize];
221 d= color[(x+0) + (y-1)*linesize];
222 a= color[(x-1) + (y+0)*linesize];
223 b= color[(x+1) + (y-1)*linesize];
225 c= color[(x-1) + (y-1)*linesize];
226 a= color[(x-1) + (y+0)*linesize];
227 b= color[(x+1) + (y-1)*linesize];
237 int ipolab= (ac + bc);
238 int ipolcd= (cc + dc);
239 if(FFABS(ipolab - ipolcd) > 5)
241 if(FFABS(ac-bc)+FFABS(cc-dc) > 20)
243 ipol |= ((ipolab + ipolcd + 2)/4)<<s;
245 color[x + y*linesize]= ipol;
249 static void draw_mandelbrot(AVFilterContext *ctx, uint32_t *color, int linesize, int64_t pts)
251 MBContext *s = ctx->priv;
252 int x,y,i, in_cidx=0, next_cidx=0, tmp_cidx;
253 double scale= s->start_scale*pow(s->end_scale/s->start_scale, pts/s->end_pts);
255 fill_from_cache(ctx, NULL, &in_cidx, NULL, s->start_y+scale*(-s->h/2-0.5), scale);
257 memset(color, 0, sizeof(*color)*s->w);
258 for(y=0; y<s->h; y++){
260 const double ci=s->start_y+scale*(y-s->h/2);
261 fill_from_cache(ctx, NULL, &in_cidx, &next_cidx, ci, scale);
263 memset(color+linesize*y1, 0, sizeof(*color)*s->w);
264 fill_from_cache(ctx, color+linesize*y1, &tmp_cidx, NULL, ci + 3*scale/2, scale);
267 for(x=0; x<s->w; x++){
268 float av_uninit(epsilon);
269 const double cr=s->start_x+scale*(x-s->w/2);
273 double dv= s->dither / (double)(1LL<<32);
274 s->dither= s->dither*1664525+1013904223;
276 if(color[x + y*linesize] & 0xFF000000)
279 if(interpol(s, color, x, y, linesize)){
280 if(next_cidx < s->cache_allocated){
281 s->next_cache[next_cidx ].p[0]= cr;
282 s->next_cache[next_cidx ].p[1]= ci;
283 s->next_cache[next_cidx++].val = color[x + y*linesize];
288 zr += cos(pts * s->morphxf) * s->morphamp;
289 zi += sin(pts * s->morphyf) * s->morphamp;
292 use_zyklus= (x==0 || s->inner!=BLACK ||color[x-1 + y*linesize] == 0xFF000000);
294 epsilon= scale*(abs(x-s->w/2) + abs(y-s->h/2))/s->w;
296 #define Z_Z2_C(outr,outi,inr,ini)\
297 outr= inr*inr - ini*ini + cr;\
298 outi= 2*inr*ini + ci;
300 #define Z_Z2_C_ZYKLUS(outr,outi,inr,ini, Z)\
301 Z_Z2_C(outr,outi,inr,ini)\
303 if(Z && fabs(s->zyklus[i>>1][0]-outr)+fabs(s->zyklus[i>>1][1]-outi) <= epsilon)\
306 s->zyklus[i][0]= outr;\
307 s->zyklus[i][1]= outi;\
311 for(i=0; i<s->maxiter-8; i++){
313 Z_Z2_C_ZYKLUS(t, zi, zr, zi, 0)
315 Z_Z2_C_ZYKLUS(zr, zi, t, zi, 1)
317 Z_Z2_C_ZYKLUS(t, zi, zr, zi, 0)
319 Z_Z2_C_ZYKLUS(zr, zi, t, zi, 1)
321 Z_Z2_C_ZYKLUS(t, zi, zr, zi, 0)
323 Z_Z2_C_ZYKLUS(zr, zi, t, zi, 1)
325 Z_Z2_C_ZYKLUS(t, zi, zr, zi, 0)
327 Z_Z2_C_ZYKLUS(zr, zi, t, zi, 1)
328 if(zr*zr + zi*zi > s->bailout){
330 for(; i<s->maxiter; i++){
333 if(zr*zr + zi*zi > s->bailout){
335 case ITERATION_COUNT:
337 c = lrintf((sinf(zr)+1)*127) + lrintf((sinf(zr/1.234)+1)*127)*256*256 + lrintf((sinf(zr/100)+1)*127)*256;
339 case NORMALIZED_ITERATION_COUNT:
340 zr = i + log2(log(s->bailout) / log(zr*zr + zi*zi));
341 c = lrintf((sinf(zr)+1)*127) + lrintf((sinf(zr/1.234)+1)*127)*256*256 + lrintf((sinf(zr/100)+1)*127)*256;
349 c = (((int)(zr*128+128))&0xFF)*256 + (((int)(zi*128+128))&0xFF);
358 if(s->inner==PERIOD){
361 if(SQR(s->zyklus[j][0]-zr) + SQR(s->zyklus[j][1]-zi) < epsilon*epsilon*10)
365 c= ((c<<5)&0xE0) + ((c<<10)&0xE000) + ((c<<15)&0xE00000);
367 }else if(s->inner==CONVTIME){
368 c= floor(i*255.0/s->maxiter+dv)*0x010101;
369 } else if(s->inner==MINCOL){
373 for(j=i-1; j>=0; j--)
374 if(SQR(s->zyklus[j][0]) + SQR(s->zyklus[j][1]) < closest){
375 closest= SQR(s->zyklus[j][0]) + SQR(s->zyklus[j][1]);
378 closest = sqrt(closest);
379 c= lrintf((s->zyklus[closest_index][0]/closest+1)*127+dv) + lrintf((s->zyklus[closest_index][1]/closest+1)*127+dv)*256;
383 color[x + y*linesize]= c;
384 if(next_cidx < s->cache_allocated){
385 s->next_cache[next_cidx ].p[0]= cr;
386 s->next_cache[next_cidx ].p[1]= ci;
387 s->next_cache[next_cidx++].val = c;
390 fill_from_cache(ctx, NULL, &in_cidx, &next_cidx, ci + scale/2, scale);
392 FFSWAP(void*, s->next_cache, s->point_cache);
393 s->cache_used = next_cidx;
394 if(s->cache_used == s->cache_allocated)
395 av_log(ctx, AV_LOG_INFO, "Mandelbrot cache is too small!\n");
398 static int request_frame(AVFilterLink *link)
400 MBContext *s = link->src->priv;
401 AVFrame *picref = ff_get_video_buffer(link, s->w, s->h);
403 return AVERROR(ENOMEM);
405 picref->sample_aspect_ratio = (AVRational) {1, 1};
406 picref->pts = s->pts++;
408 draw_mandelbrot(link->src, (uint32_t*)picref->data[0], picref->linesize[0]/4, picref->pts);
409 return ff_filter_frame(link, picref);
412 static const AVFilterPad mandelbrot_outputs[] = {
415 .type = AVMEDIA_TYPE_VIDEO,
416 .request_frame = request_frame,
417 .config_props = config_props,
422 const AVFilter ff_vsrc_mandelbrot = {
423 .name = "mandelbrot",
424 .description = NULL_IF_CONFIG_SMALL("Render a Mandelbrot fractal."),
425 .priv_size = sizeof(MBContext),
426 .priv_class = &mandelbrot_class,
429 .query_formats = query_formats,
431 .outputs = mandelbrot_outputs,