FFmpeg
Loading...
Searching...
No Matches
vsrc_mandelbrot.c
Go to the documentation of this file.
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
46
53
54typedef struct Point {
55 double p[2];
56 uint32_t val;
57} Point;
58
59typedef struct MBContext {
60 const AVClass *class;
61 int w, h;
63 uint64_t pts;
65 double start_x;
66 double start_y;
68 double end_scale;
69 double end_pts;
70 double bailout;
71 int outer;
72 int inner;
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
88static 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
120
122{
123 MBContext *s = ctx->priv;
124 int ret = av_image_check_size(s->w, s->h, 0, ctx);
125
126 if (ret < 0)
127 return ret;
128
129 s->bailout *= s->bailout;
130
131 s->start_scale /=s->h;
132 s->end_scale /=s->h;
133
134 s->cache_allocated = s->w * s->h * 3;
135 s->cache_used = 0;
136 s->point_cache= av_malloc_array(s->cache_allocated, sizeof(*s->point_cache));
137 s-> next_cache= av_malloc_array(s->cache_allocated, sizeof(*s-> next_cache));
138 s-> zyklus = av_malloc_array(s->maxiter + 16LL, sizeof(*s->zyklus));
139
140 if (!s->point_cache || !s->next_cache || !s->zyklus)
141 return AVERROR(ENOMEM);
142
143 return 0;
144}
145
147{
148 MBContext *s = ctx->priv;
149
150 av_freep(&s->point_cache);
151 av_freep(&s-> next_cache);
152 av_freep(&s->zyklus);
153}
154
155static int config_props(AVFilterLink *outlink)
156{
157 AVFilterContext *ctx = outlink->src;
158 FilterLink *l = ff_filter_link(outlink);
159 MBContext *s = ctx->priv;
160
161 outlink->w = s->w;
162 outlink->h = s->h;
163 outlink->time_base = av_inv_q(s->frame_rate);
164 l->frame_rate = s->frame_rate;
165
166 return 0;
167}
168
169static void fill_from_cache(AVFilterContext *ctx, uint32_t *color, int *in_cidx, int *out_cidx, double py, double scale){
170 MBContext *s = ctx->priv;
171 if(s->morphamp)
172 return;
173 for(; *in_cidx < s->cache_used; (*in_cidx)++){
174 Point *p= &s->point_cache[*in_cidx];
175 int x;
176 if(p->p[1] > py)
177 break;
178 x= lrint((p->p[0] - s->start_x) / scale + s->w/2);
179 if(x<0 || x >= s->w)
180 continue;
181 if(color) color[x] = p->val;
182 if(out_cidx && *out_cidx < s->cache_allocated)
183 s->next_cache[(*out_cidx)++]= *p;
184 }
185}
186
187static int interpol(MBContext *s, uint32_t *color, int x, int y, int linesize)
188{
189 uint32_t a,b,c,d, i;
190 uint32_t ipol=0xFF000000;
191 int dist;
192
193 if(!x || !y || x+1==s->w || y+1==s->h)
194 return 0;
195
196 dist= FFMAX(FFABS(x-(s->w>>1))*s->h, FFABS(y-(s->h>>1))*s->w);
197
198 if(dist<(s->w*s->h>>3))
199 return 0;
200
201 a=color[(x+1) + (y+0)*linesize];
202 b=color[(x-1) + (y+1)*linesize];
203 c=color[(x+0) + (y+1)*linesize];
204 d=color[(x+1) + (y+1)*linesize];
205
206 if(a&&c){
207 b= color[(x-1) + (y+0)*linesize];
208 d= color[(x+0) + (y-1)*linesize];
209 }else if(b&&d){
210 a= color[(x+1) + (y-1)*linesize];
211 c= color[(x-1) + (y-1)*linesize];
212 }else if(c){
213 d= color[(x+0) + (y-1)*linesize];
214 a= color[(x-1) + (y+0)*linesize];
215 b= color[(x+1) + (y-1)*linesize];
216 }else if(d){
217 c= color[(x-1) + (y-1)*linesize];
218 a= color[(x-1) + (y+0)*linesize];
219 b= color[(x+1) + (y-1)*linesize];
220 }else
221 return 0;
222
223 for(i=0; i<3; i++){
224 int s= 8*i;
225 uint8_t ac= a>>s;
226 uint8_t bc= b>>s;
227 uint8_t cc= c>>s;
228 uint8_t dc= d>>s;
229 int ipolab= (ac + bc);
230 int ipolcd= (cc + dc);
231 if(FFABS(ipolab - ipolcd) > 5)
232 return 0;
233 if(FFABS(ac-bc)+FFABS(cc-dc) > 20)
234 return 0;
235 ipol |= ((ipolab + ipolcd + 2)/4)<<s;
236 }
237 color[x + y*linesize]= ipol;
238 return 1;
239}
240
241static void draw_mandelbrot(AVFilterContext *ctx, uint32_t *color, int linesize, int64_t pts)
242{
243 MBContext *s = ctx->priv;
244 int x,y,i, in_cidx=0, next_cidx=0, tmp_cidx;
245 double scale= s->start_scale*pow(s->end_scale/s->start_scale, pts/s->end_pts);
246 int use_zyklus=0;
247 fill_from_cache(ctx, NULL, &in_cidx, NULL, s->start_y+scale*(-s->h/2-0.5), scale);
248 tmp_cidx= in_cidx;
249 memset(color, 0, sizeof(*color)*s->w);
250 for(y=0; y<s->h; y++){
251 int y1= y+1;
252 const double ci=s->start_y+scale*(y-s->h/2);
253 fill_from_cache(ctx, NULL, &in_cidx, &next_cidx, ci, scale);
254 if(y1<s->h){
255 memset(color+linesize*y1, 0, sizeof(*color)*s->w);
256 fill_from_cache(ctx, color+linesize*y1, &tmp_cidx, NULL, ci + 3*scale/2, scale);
257 }
258
259 for(x=0; x<s->w; x++){
260 float av_uninit(epsilon);
261 const double cr=s->start_x+scale*(x-s->w/2);
262 double zr=cr;
263 double zi=ci;
264 uint32_t c=0;
265 double dv= s->dither / (double)(1LL<<32);
266 s->dither= s->dither*1664525+1013904223;
267
268 if(color[x + y*linesize] & 0xFF000000)
269 continue;
270 if(!s->morphamp){
271 if(interpol(s, color, x, y, linesize)){
272 if(next_cidx < s->cache_allocated){
273 s->next_cache[next_cidx ].p[0]= cr;
274 s->next_cache[next_cidx ].p[1]= ci;
275 s->next_cache[next_cidx++].val = color[x + y*linesize];
276 }
277 continue;
278 }
279 }else{
280 zr += cos(pts * s->morphxf) * s->morphamp;
281 zi += sin(pts * s->morphyf) * s->morphamp;
282 }
283
284 use_zyklus= (x==0 || s->inner!=BLACK ||color[x-1 + y*linesize] == 0xFF000000);
285 if(use_zyklus)
286 epsilon= scale*(abs(x-s->w/2) + abs(y-s->h/2))/s->w;
287
288#define Z_Z2_C(outr,outi,inr,ini)\
289 outr= inr*inr - ini*ini + cr;\
290 outi= 2*inr*ini + ci;
291
292#define Z_Z2_C_ZYKLUS(outr,outi,inr,ini, Z)\
293 Z_Z2_C(outr,outi,inr,ini)\
294 if(use_zyklus){\
295 if(Z && fabs(s->zyklus[i>>1][0]-outr)+fabs(s->zyklus[i>>1][1]-outi) <= epsilon)\
296 break;\
297 }\
298 s->zyklus[i][0]= outr;\
299 s->zyklus[i][1]= outi;\
300
301
302
303 for(i=0; i<s->maxiter-8; i++){
304 double t;
305 Z_Z2_C_ZYKLUS(t, zi, zr, zi, 0)
306 i++;
307 Z_Z2_C_ZYKLUS(zr, zi, t, zi, 1)
308 i++;
309 Z_Z2_C_ZYKLUS(t, zi, zr, zi, 0)
310 i++;
311 Z_Z2_C_ZYKLUS(zr, zi, t, zi, 1)
312 i++;
313 Z_Z2_C_ZYKLUS(t, zi, zr, zi, 0)
314 i++;
315 Z_Z2_C_ZYKLUS(zr, zi, t, zi, 1)
316 i++;
317 Z_Z2_C_ZYKLUS(t, zi, zr, zi, 0)
318 i++;
319 Z_Z2_C_ZYKLUS(zr, zi, t, zi, 1)
320 if(zr*zr + zi*zi > s->bailout){
321 i-= FFMIN(7, i);
322 for(; i<s->maxiter; i++){
323 zr= s->zyklus[i][0];
324 zi= s->zyklus[i][1];
325 if(zr*zr + zi*zi > s->bailout){
326 switch(s->outer){
327 case ITERATION_COUNT:
328 zr = i;
329 c = lrintf((sinf(zr)+1)*127) + lrintf((sinf(zr/1.234)+1)*127)*256*256 + lrintf((sinf(zr/100)+1)*127)*256;
330 break;
332 zr = i + log2(log(s->bailout) / log(zr*zr + zi*zi));
333 c = lrintf((sinf(zr)+1)*127) + lrintf((sinf(zr/1.234)+1)*127)*256*256 + lrintf((sinf(zr/100)+1)*127)*256;
334 break;
335 case WHITE:
336 c = 0xFFFFFF;
337 break;
338 case OUTZ:
339 zr /= s->bailout;
340 zi /= s->bailout;
341 c = (((int)(zr*128+128))&0xFF)*256 + (((int)(zi*128+128))&0xFF);
342 }
343 break;
344 }
345 }
346 break;
347 }
348 }
349 if(!c && i){
350 if(s->inner==PERIOD){
351 int j;
352 for(j=i-1; j; j--)
353 if(SQR(s->zyklus[j][0]-zr) + SQR(s->zyklus[j][1]-zi) < epsilon*epsilon*10)
354 break;
355 if(j){
356 c= i-j;
357 c= ((c<<5)&0xE0) + ((c<<10)&0xE000) + ((c<<15)&0xE00000);
358 }
359 }else if(s->inner==CONVTIME){
360 c= floor(i*255.0/s->maxiter+dv)*0x010101;
361 } else if(s->inner==MINCOL){
362 int j;
363 double closest=9999;
364 int closest_index=0;
365 for(j=i-1; j>=0; j--)
366 if(SQR(s->zyklus[j][0]) + SQR(s->zyklus[j][1]) < closest){
367 closest= SQR(s->zyklus[j][0]) + SQR(s->zyklus[j][1]);
368 closest_index= j;
369 }
370 closest = sqrt(closest);
371 c= lrintf((s->zyklus[closest_index][0]/closest+1)*127+dv) + lrintf((s->zyklus[closest_index][1]/closest+1)*127+dv)*256;
372 }
373 }
374 c |= 0xFF000000;
375 color[x + y*linesize]= c;
376 if(next_cidx < s->cache_allocated){
377 s->next_cache[next_cidx ].p[0]= cr;
378 s->next_cache[next_cidx ].p[1]= ci;
379 s->next_cache[next_cidx++].val = c;
380 }
381 }
382 fill_from_cache(ctx, NULL, &in_cidx, &next_cidx, ci + scale/2, scale);
383 }
384 FFSWAP(void*, s->next_cache, s->point_cache);
385 s->cache_used = next_cidx;
386 if(s->cache_used == s->cache_allocated)
387 av_log(ctx, AV_LOG_INFO, "Mandelbrot cache is too small!\n");
388}
389
391{
392 MBContext *s = link->src->priv;
393 AVFrame *picref = ff_get_video_buffer(link, s->w, s->h);
394 if (!picref)
395 return AVERROR(ENOMEM);
396
397 picref->sample_aspect_ratio = (AVRational) {1, 1};
398 picref->pts = s->pts++;
399 picref->duration = 1;
400
401 draw_mandelbrot(link->src, (uint32_t*)picref->data[0], picref->linesize[0]/4, picref->pts);
402 return ff_filter_frame(link, picref);
403}
404
406 {
407 .name = "default",
408 .type = AVMEDIA_TYPE_VIDEO,
409 .request_frame = request_frame,
410 .config_props = config_props,
411 },
412};
413
415 .p.name = "mandelbrot",
416 .p.description = NULL_IF_CONFIG_SMALL("Render a Mandelbrot fractal."),
417 .p.priv_class = &mandelbrot_class,
418 .p.inputs = NULL,
419 .priv_size = sizeof(MBContext),
420 .init = init,
421 .uninit = uninit,
424};
static int request_frame(AVFilterLink *outlink)
Definition af_aecho.c:272
const FFFilter ff_vsrc_mandelbrot
#define log2(x)
Definition math.h:26
static AVFormatContext * ctx
int ff_filter_frame(AVFilterLink *link, AVFrame *frame)
Send a frame of data to the next filter.
Definition avfilter.c:1079
Main libavfilter public API header.
#define i(width, name, range_min, range_max)
Definition cbs_h264.c:63
#define s(width, name)
Definition cbs_vp9.c:198
#define FLAGS
Definition cmdutils.c:596
#define FFABS(a)
Absolute value, Note, INT_MIN / INT64_MIN result in undefined behavior as they are not representable ...
Definition common.h:74
#define NULL
Definition coverity.c:32
long long int64_t
Definition coverity.c:34
#define abs(x)
static __device__ float floor(float a)
int(* init)(AVBSFContext *ctx)
Definition dts2pts.c:613
int ret
Definition ffplay.c:391
@ AV_OPT_TYPE_IMAGE_SIZE
Underlying C type is two consecutive integers.
Definition opt.h:302
@ AV_OPT_TYPE_CONST
Special option type for declaring named constants.
Definition opt.h:298
@ AV_OPT_TYPE_VIDEO_RATE
Underlying C type is AVRational.
Definition opt.h:314
@ AV_OPT_TYPE_INT
Underlying C type is int.
Definition opt.h:258
@ AV_OPT_TYPE_DOUBLE
Underlying C type is double.
Definition opt.h:266
#define AVERROR(e)
Definition error.h:45
#define AV_LOG_INFO
Standard information.
Definition log.h:221
static av_always_inline AVRational av_inv_q(AVRational q)
Invert a rational.
Definition rational.h:159
@ AVMEDIA_TYPE_VIDEO
Definition avutil.h:200
int av_image_check_size(unsigned int w, unsigned int h, int log_offset, void *log_ctx)
Check if the given dimension of an image is valid, meaning that all bytes of the image can be address...
Definition imgutils.c:318
int a
static av_always_inline void scale(vec_s32 res[4], vec_s16 res_packed[2], const int shift)
misc image utilities
#define b
Definition input.c:43
static av_cold void uninit(AVBitStreamFilterContext *ctx)
#define FILTER_OUTPUTS(array)
Definition filters.h:280
static FilterLink * ff_filter_link(AVFilterLink *link)
Definition filters.h:199
#define FILTER_SINGLE_PIXFMT(pix_fmt_)
Definition filters.h:269
#define AVFILTER_DEFINE_CLASS(fname)
Definition filters.h:493
#define av_uninit(x)
Definition attributes.h:187
#define av_cold
Definition attributes.h:117
#define NULL_IF_CONFIG_SMALL(x)
Return NULL if CONFIG_SMALL is true, otherwise the argument without modification.
Definition internal.h:97
#define lrintf(x)
Definition libm_mips.h:74
uint8_t w
Definition llvidencdsp.c:39
#define FFSWAP(type, a, b)
Definition macros.h:52
#define FFMIN(a, b)
Definition macros.h:49
#define FFMAX(a, b)
Definition macros.h:47
Memory handling functions.
AVOptions.
#define AV_PIX_FMT_0BGR32
Definition pixfmt.h:525
static int ipol(uint8_t *src, int x, int y)
Definition rotozoom.c:65
#define SQR(x)
Definition rpzaenc.c:66
static int config_props(AVBitStreamFilterLink *link)
Definition source.c:181
Describe the class of an AVClass context structure.
Definition log.h:76
An instance of a filter.
Definition avfilter.h:279
void * priv
private data for use by the filter
Definition avfilter.h:294
A filter pad used for either input or output.
Definition filters.h:40
This structure describes decoded (raw) audio or video data.
Definition frame.h:479
int64_t pts
Presentation timestamp in time_base units (time when frame should be shown to user).
Definition frame.h:581
uint8_t * data[AV_NUM_DATA_POINTERS]
pointer to the picture/channel planes.
Definition frame.h:500
AVRational sample_aspect_ratio
Sample aspect ratio for the video frame, 0/1 if unknown/unspecified.
Definition frame.h:576
int linesize[AV_NUM_DATA_POINTERS]
For video, a positive or negative value, which is typically indicating the size in bytes of each pict...
Definition frame.h:524
int64_t duration
Duration of the frame, in the same units as pts.
Definition frame.h:830
AVOption.
Definition opt.h:428
Rational number (pair of numerator and denominator).
Definition rational.h:58
Point * next_cache
AVRational frame_rate
double(* zyklus)[2]
double start_scale
double end_scale
double morphamp
uint64_t pts
uint32_t dither
Point * point_cache
double p[2]
uint32_t val
#define lrint
Definition tablegen.h:53
#define av_malloc_array(a, b)
#define av_freep(p)
#define av_log(a,...)
static int64_t pts
static double cr(void *priv, double x, double y)
Definition vf_geq.c:248
AVFrame * ff_get_video_buffer(AVFilterLink *link, int w, int h)
Request a picture buffer with a specific set of permissions.
Definition video.c:120
@ OUTZ
@ WHITE
@ NORMALIZED_ITERATION_COUNT
@ ITERATION_COUNT
static const AVFilterPad mandelbrot_outputs[]
static int config_props(AVFilterLink *outlink)
#define Z_Z2_C_ZYKLUS(outr, outi, inr, ini, Z)
static const AVOption mandelbrot_options[]
@ MINCOL
@ CONVTIME
@ PERIOD
@ BLACK
static void draw_mandelbrot(AVFilterContext *ctx, uint32_t *color, int linesize, int64_t pts)
static int request_frame(AVFilterLink *link)
static av_cold void uninit(AVFilterContext *ctx)
static int interpol(MBContext *s, uint32_t *color, int x, int y, int linesize)
#define OFFSET(x)
static void fill_from_cache(AVFilterContext *ctx, uint32_t *color, int *in_cidx, int *out_cidx, double py, double scale)
#define SQR(a)
static double c[64]