2011-11-12 00:47:06 +03:00
|
|
|
/*
|
|
|
|
* Copyright (c) 2011 Michael Niedermayer
|
|
|
|
*
|
|
|
|
* This file is part of FFmpeg.
|
|
|
|
*
|
|
|
|
* FFmpeg is free software; you can redistribute it and/or
|
|
|
|
* modify it under the terms of the GNU Lesser General Public
|
|
|
|
* License as published by the Free Software Foundation; either
|
|
|
|
* version 2.1 of the License, or (at your option) any later version.
|
|
|
|
*
|
|
|
|
* FFmpeg is distributed in the hope that it will be useful,
|
|
|
|
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
|
|
|
|
* Lesser General Public License for more details.
|
|
|
|
*
|
|
|
|
* You should have received a copy of the GNU Lesser General Public
|
|
|
|
* License along with FFmpeg; if not, write to the Free Software
|
|
|
|
* Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
|
|
|
|
*
|
|
|
|
* The vsrc_color filter from Stefano Sabatini was used as template to create
|
|
|
|
* this
|
|
|
|
*/
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @file
|
2015-11-25 03:33:07 +02:00
|
|
|
* Mandelbrot fractal renderer
|
2011-11-12 00:47:06 +03:00
|
|
|
*/
|
|
|
|
|
|
|
|
#include "avfilter.h"
|
2012-06-16 12:47:46 +03:00
|
|
|
#include "formats.h"
|
|
|
|
#include "video.h"
|
2012-06-22 15:33:09 +03:00
|
|
|
#include "internal.h"
|
2011-11-12 00:47:06 +03:00
|
|
|
#include "libavutil/imgutils.h"
|
2011-11-13 21:12:00 +03:00
|
|
|
#include "libavutil/opt.h"
|
2011-11-12 00:47:06 +03:00
|
|
|
#include "libavutil/parseutils.h"
|
2011-11-14 07:47:56 +03:00
|
|
|
#include <float.h>
|
2011-11-14 07:56:03 +03:00
|
|
|
#include <math.h>
|
2011-11-12 00:47:06 +03:00
|
|
|
|
2011-11-13 20:14:10 +03:00
|
|
|
#define SQR(a) ((a)*(a))
|
|
|
|
|
2011-11-12 01:22:04 +03:00
|
|
|
enum Outer{
|
|
|
|
ITERATION_COUNT,
|
|
|
|
NORMALIZED_ITERATION_COUNT,
|
2013-03-27 18:00:18 +03:00
|
|
|
WHITE,
|
2013-03-27 18:19:50 +03:00
|
|
|
OUTZ,
|
2011-11-12 01:22:04 +03:00
|
|
|
};
|
|
|
|
|
2011-11-13 21:26:22 +03:00
|
|
|
enum Inner{
|
|
|
|
BLACK,
|
2011-11-13 21:28:24 +03:00
|
|
|
PERIOD,
|
2011-11-15 19:25:16 +03:00
|
|
|
CONVTIME,
|
2011-11-15 21:58:58 +03:00
|
|
|
MINCOL,
|
2011-11-13 21:26:22 +03:00
|
|
|
};
|
|
|
|
|
2011-11-12 17:44:13 +03:00
|
|
|
typedef struct Point {
|
|
|
|
double p[2];
|
|
|
|
uint32_t val;
|
|
|
|
} Point;
|
|
|
|
|
2011-11-12 00:47:06 +03:00
|
|
|
typedef struct {
|
2011-11-13 21:12:00 +03:00
|
|
|
const AVClass *class;
|
2011-11-12 00:47:06 +03:00
|
|
|
int w, h;
|
2013-03-26 15:08:44 +03:00
|
|
|
AVRational frame_rate;
|
2011-11-12 00:47:06 +03:00
|
|
|
uint64_t pts;
|
|
|
|
int maxiter;
|
|
|
|
double start_x;
|
|
|
|
double start_y;
|
|
|
|
double start_scale;
|
2011-11-12 01:58:08 +03:00
|
|
|
double end_scale;
|
|
|
|
double end_pts;
|
2011-11-12 01:21:42 +03:00
|
|
|
double bailout;
|
2015-04-05 15:44:59 +02:00
|
|
|
int outer;
|
|
|
|
int inner;
|
2011-11-12 17:44:13 +03:00
|
|
|
int cache_allocated;
|
|
|
|
int cache_used;
|
|
|
|
Point *point_cache;
|
|
|
|
Point *next_cache;
|
2011-11-12 18:33:12 +03:00
|
|
|
double (*zyklus)[2];
|
2011-11-15 23:33:11 +03:00
|
|
|
uint32_t dither;
|
2013-03-27 21:24:21 +03:00
|
|
|
|
|
|
|
double morphxf;
|
|
|
|
double morphyf;
|
|
|
|
double morphamp;
|
2011-11-12 00:47:06 +03:00
|
|
|
} MBContext;
|
|
|
|
|
2011-11-13 21:12:00 +03:00
|
|
|
#define OFFSET(x) offsetof(MBContext, x)
|
2012-08-13 14:40:01 +03:00
|
|
|
#define FLAGS AV_OPT_FLAG_VIDEO_PARAM|AV_OPT_FLAG_FILTERING_PARAM
|
2011-11-13 21:12:00 +03:00
|
|
|
|
|
|
|
static const AVOption mandelbrot_options[] = {
|
2012-08-13 14:40:01 +03:00
|
|
|
{"size", "set frame size", OFFSET(w), AV_OPT_TYPE_IMAGE_SIZE, {.str="640x480"}, CHAR_MIN, CHAR_MAX, FLAGS },
|
|
|
|
{"s", "set frame size", OFFSET(w), AV_OPT_TYPE_IMAGE_SIZE, {.str="640x480"}, CHAR_MIN, CHAR_MAX, FLAGS },
|
2013-03-26 15:08:44 +03:00
|
|
|
{"rate", "set frame rate", OFFSET(frame_rate), AV_OPT_TYPE_VIDEO_RATE, {.str="25"}, CHAR_MIN, CHAR_MAX, FLAGS },
|
|
|
|
{"r", "set frame rate", OFFSET(frame_rate), AV_OPT_TYPE_VIDEO_RATE, {.str="25"}, CHAR_MIN, CHAR_MAX, FLAGS },
|
2012-09-05 15:26:01 +03:00
|
|
|
{"maxiter", "set max iterations number", OFFSET(maxiter), AV_OPT_TYPE_INT, {.i64=7189}, 1, INT_MAX, FLAGS },
|
2012-08-13 14:40:01 +03:00
|
|
|
{"start_x", "set the initial x position", OFFSET(start_x), AV_OPT_TYPE_DOUBLE, {.dbl=-0.743643887037158704752191506114774}, -100, 100, FLAGS },
|
|
|
|
{"start_y", "set the initial y position", OFFSET(start_y), AV_OPT_TYPE_DOUBLE, {.dbl=-0.131825904205311970493132056385139}, -100, 100, FLAGS },
|
|
|
|
{"start_scale", "set the initial scale value", OFFSET(start_scale), AV_OPT_TYPE_DOUBLE, {.dbl=3.0}, 0, FLT_MAX, FLAGS },
|
|
|
|
{"end_scale", "set the terminal scale value", OFFSET(end_scale), AV_OPT_TYPE_DOUBLE, {.dbl=0.3}, 0, FLT_MAX, FLAGS },
|
|
|
|
{"end_pts", "set the terminal pts value", OFFSET(end_pts), AV_OPT_TYPE_DOUBLE, {.dbl=400}, 0, INT64_MAX, FLAGS },
|
|
|
|
{"bailout", "set the bailout value", OFFSET(bailout), AV_OPT_TYPE_DOUBLE, {.dbl=10}, 0, FLT_MAX, FLAGS },
|
2013-03-27 21:24:21 +03:00
|
|
|
{"morphxf", "set morph x frequency", OFFSET(morphxf), AV_OPT_TYPE_DOUBLE, {.dbl=0.01}, -FLT_MAX, FLT_MAX, FLAGS },
|
|
|
|
{"morphyf", "set morph y frequency", OFFSET(morphyf), AV_OPT_TYPE_DOUBLE, {.dbl=0.0123}, -FLT_MAX, FLT_MAX, FLAGS },
|
|
|
|
{"morphamp", "set morph amplitude", OFFSET(morphamp), AV_OPT_TYPE_DOUBLE, {.dbl=0}, -FLT_MAX, FLT_MAX, FLAGS },
|
2011-11-13 21:12:00 +03:00
|
|
|
|
2012-09-05 15:26:01 +03:00
|
|
|
{"outer", "set outer coloring mode", OFFSET(outer), AV_OPT_TYPE_INT, {.i64=NORMALIZED_ITERATION_COUNT}, 0, INT_MAX, FLAGS, "outer" },
|
2012-09-05 14:58:11 +03:00
|
|
|
{"iteration_count", "set iteration count mode", 0, AV_OPT_TYPE_CONST, {.i64=ITERATION_COUNT}, INT_MIN, INT_MAX, FLAGS, "outer" },
|
|
|
|
{"normalized_iteration_count", "set normalized iteration count mode", 0, AV_OPT_TYPE_CONST, {.i64=NORMALIZED_ITERATION_COUNT}, INT_MIN, INT_MAX, FLAGS, "outer" },
|
2013-03-27 18:00:18 +03:00
|
|
|
{"white", "set white mode", 0, AV_OPT_TYPE_CONST, {.i64=WHITE}, INT_MIN, INT_MAX, FLAGS, "outer" },
|
2013-03-27 18:19:50 +03:00
|
|
|
{"outz", "set outz mode", 0, AV_OPT_TYPE_CONST, {.i64=OUTZ}, INT_MIN, INT_MAX, FLAGS, "outer" },
|
2011-11-13 21:12:00 +03:00
|
|
|
|
2012-09-05 15:26:01 +03:00
|
|
|
{"inner", "set inner coloring mode", OFFSET(inner), AV_OPT_TYPE_INT, {.i64=MINCOL}, 0, INT_MAX, FLAGS, "inner" },
|
2012-09-05 14:58:11 +03:00
|
|
|
{"black", "set black mode", 0, AV_OPT_TYPE_CONST, {.i64=BLACK}, INT_MIN, INT_MAX, FLAGS, "inner"},
|
|
|
|
{"period", "set period mode", 0, AV_OPT_TYPE_CONST, {.i64=PERIOD}, INT_MIN, INT_MAX, FLAGS, "inner"},
|
|
|
|
{"convergence", "show time until convergence", 0, AV_OPT_TYPE_CONST, {.i64=CONVTIME}, INT_MIN, INT_MAX, FLAGS, "inner"},
|
|
|
|
{"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"},
|
2011-11-13 21:12:00 +03:00
|
|
|
|
|
|
|
{NULL},
|
|
|
|
};
|
|
|
|
|
2012-06-22 15:33:09 +03:00
|
|
|
AVFILTER_DEFINE_CLASS(mandelbrot);
|
2011-11-13 21:12:00 +03:00
|
|
|
|
2013-04-12 12:13:33 +03:00
|
|
|
static av_cold int init(AVFilterContext *ctx)
|
2011-11-12 00:47:06 +03:00
|
|
|
{
|
2015-09-13 04:32:44 +02:00
|
|
|
MBContext *s = ctx->priv;
|
2011-11-13 21:12:00 +03:00
|
|
|
|
2015-09-13 04:32:44 +02:00
|
|
|
s->bailout *= s->bailout;
|
2011-11-12 00:47:06 +03:00
|
|
|
|
2015-09-13 04:32:44 +02:00
|
|
|
s->start_scale /=s->h;
|
|
|
|
s->end_scale /=s->h;
|
2011-11-12 00:47:06 +03:00
|
|
|
|
2015-09-13 04:32:44 +02:00
|
|
|
s->cache_allocated = s->w * s->h * 3;
|
|
|
|
s->cache_used = 0;
|
|
|
|
s->point_cache= av_malloc_array(s->cache_allocated, sizeof(*s->point_cache));
|
|
|
|
s-> next_cache= av_malloc_array(s->cache_allocated, sizeof(*s-> next_cache));
|
|
|
|
s-> zyklus = av_malloc_array(s->maxiter + 16, sizeof(*s->zyklus));
|
2011-11-12 17:44:13 +03:00
|
|
|
|
2011-11-12 00:47:06 +03:00
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
static av_cold void uninit(AVFilterContext *ctx)
|
|
|
|
{
|
2015-09-13 04:32:44 +02:00
|
|
|
MBContext *s = ctx->priv;
|
2011-11-12 00:47:06 +03:00
|
|
|
|
2015-09-13 04:32:44 +02:00
|
|
|
av_freep(&s->point_cache);
|
|
|
|
av_freep(&s-> next_cache);
|
|
|
|
av_freep(&s->zyklus);
|
2011-11-12 00:47:06 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
static int query_formats(AVFilterContext *ctx)
|
|
|
|
{
|
2012-10-08 21:54:00 +03:00
|
|
|
static const enum AVPixelFormat pix_fmts[] = {
|
2015-10-02 01:21:36 +02:00
|
|
|
AV_PIX_FMT_0BGR32,
|
2012-10-08 21:54:00 +03:00
|
|
|
AV_PIX_FMT_NONE
|
2011-11-12 00:47:06 +03:00
|
|
|
};
|
|
|
|
|
2015-04-03 19:55:18 +02:00
|
|
|
AVFilterFormats *fmts_list = ff_make_format_list(pix_fmts);
|
|
|
|
if (!fmts_list)
|
|
|
|
return AVERROR(ENOMEM);
|
|
|
|
return ff_set_common_formats(ctx, fmts_list);
|
2011-11-12 00:47:06 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
static int config_props(AVFilterLink *inlink)
|
|
|
|
{
|
|
|
|
AVFilterContext *ctx = inlink->src;
|
2015-09-13 04:32:44 +02:00
|
|
|
MBContext *s = ctx->priv;
|
2011-11-12 00:47:06 +03:00
|
|
|
|
2015-09-13 04:32:44 +02:00
|
|
|
if (av_image_check_size(s->w, s->h, 0, ctx) < 0)
|
2011-11-12 00:47:06 +03:00
|
|
|
return AVERROR(EINVAL);
|
|
|
|
|
2015-09-13 04:32:44 +02:00
|
|
|
inlink->w = s->w;
|
|
|
|
inlink->h = s->h;
|
|
|
|
inlink->time_base = av_inv_q(s->frame_rate);
|
2011-11-12 00:47:06 +03:00
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2011-11-12 17:44:13 +03:00
|
|
|
static void fill_from_cache(AVFilterContext *ctx, uint32_t *color, int *in_cidx, int *out_cidx, double py, double scale){
|
2015-09-13 04:32:44 +02:00
|
|
|
MBContext *s = ctx->priv;
|
|
|
|
if(s->morphamp)
|
2013-03-27 21:24:21 +03:00
|
|
|
return;
|
2015-09-13 04:32:44 +02:00
|
|
|
for(; *in_cidx < s->cache_used; (*in_cidx)++){
|
|
|
|
Point *p= &s->point_cache[*in_cidx];
|
2011-11-12 17:44:13 +03:00
|
|
|
int x;
|
2011-11-13 15:51:09 +03:00
|
|
|
if(p->p[1] > py)
|
2011-11-12 17:44:13 +03:00
|
|
|
break;
|
2015-09-13 04:32:44 +02:00
|
|
|
x= round((p->p[0] - s->start_x) / scale + s->w/2);
|
|
|
|
if(x<0 || x >= s->w)
|
2011-11-12 17:44:13 +03:00
|
|
|
continue;
|
|
|
|
if(color) color[x] = p->val;
|
2015-09-13 04:32:44 +02:00
|
|
|
if(out_cidx && *out_cidx < s->cache_allocated)
|
|
|
|
s->next_cache[(*out_cidx)++]= *p;
|
2011-11-12 17:44:13 +03:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2015-09-13 04:32:44 +02:00
|
|
|
static int interpol(MBContext *s, uint32_t *color, int x, int y, int linesize)
|
2011-11-19 21:41:28 +03:00
|
|
|
{
|
|
|
|
uint32_t a,b,c,d, i;
|
2011-11-20 00:07:10 +03:00
|
|
|
uint32_t ipol=0xFF000000;
|
2011-11-19 21:41:28 +03:00
|
|
|
int dist;
|
|
|
|
|
2015-09-13 04:32:44 +02:00
|
|
|
if(!x || !y || x+1==s->w || y+1==s->h)
|
2011-11-19 21:41:28 +03:00
|
|
|
return 0;
|
|
|
|
|
2015-09-13 04:32:44 +02:00
|
|
|
dist= FFMAX(FFABS(x-(s->w>>1))*s->h, FFABS(y-(s->h>>1))*s->w);
|
2011-11-19 21:41:28 +03:00
|
|
|
|
2015-09-13 04:32:44 +02:00
|
|
|
if(dist<(s->w*s->h>>3))
|
2011-11-19 21:41:28 +03:00
|
|
|
return 0;
|
|
|
|
|
|
|
|
a=color[(x+1) + (y+0)*linesize];
|
|
|
|
b=color[(x-1) + (y+1)*linesize];
|
|
|
|
c=color[(x+0) + (y+1)*linesize];
|
|
|
|
d=color[(x+1) + (y+1)*linesize];
|
|
|
|
|
|
|
|
if(a&&c){
|
|
|
|
b= color[(x-1) + (y+0)*linesize];
|
|
|
|
d= color[(x+0) + (y-1)*linesize];
|
|
|
|
}else if(b&&d){
|
|
|
|
a= color[(x+1) + (y-1)*linesize];
|
|
|
|
c= color[(x-1) + (y-1)*linesize];
|
|
|
|
}else if(c){
|
|
|
|
d= color[(x+0) + (y-1)*linesize];
|
|
|
|
a= color[(x-1) + (y+0)*linesize];
|
|
|
|
b= color[(x+1) + (y-1)*linesize];
|
|
|
|
}else if(d){
|
|
|
|
c= color[(x-1) + (y-1)*linesize];
|
|
|
|
a= color[(x-1) + (y+0)*linesize];
|
|
|
|
b= color[(x+1) + (y-1)*linesize];
|
|
|
|
}else
|
|
|
|
return 0;
|
|
|
|
|
|
|
|
for(i=0; i<3; i++){
|
|
|
|
int s= 8*i;
|
|
|
|
uint8_t ac= a>>s;
|
|
|
|
uint8_t bc= b>>s;
|
|
|
|
uint8_t cc= c>>s;
|
|
|
|
uint8_t dc= d>>s;
|
|
|
|
int ipolab= (ac + bc);
|
|
|
|
int ipolcd= (cc + dc);
|
|
|
|
if(FFABS(ipolab - ipolcd) > 5)
|
|
|
|
return 0;
|
2011-11-20 00:22:21 +03:00
|
|
|
if(FFABS(ac-bc)+FFABS(cc-dc) > 20)
|
|
|
|
return 0;
|
2011-11-19 21:41:28 +03:00
|
|
|
ipol |= ((ipolab + ipolcd + 2)/4)<<s;
|
|
|
|
}
|
2011-11-20 00:07:10 +03:00
|
|
|
color[x + y*linesize]= ipol;
|
2011-11-19 21:41:28 +03:00
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
|
2011-11-12 00:47:06 +03:00
|
|
|
static void draw_mandelbrot(AVFilterContext *ctx, uint32_t *color, int linesize, int64_t pts)
|
|
|
|
{
|
2015-09-13 04:32:44 +02:00
|
|
|
MBContext *s = ctx->priv;
|
2011-11-12 17:44:13 +03:00
|
|
|
int x,y,i, in_cidx=0, next_cidx=0, tmp_cidx;
|
2015-09-13 04:32:44 +02:00
|
|
|
double scale= s->start_scale*pow(s->end_scale/s->start_scale, pts/s->end_pts);
|
2011-11-12 18:33:56 +03:00
|
|
|
int use_zyklus=0;
|
2015-09-13 04:32:44 +02:00
|
|
|
fill_from_cache(ctx, NULL, &in_cidx, NULL, s->start_y+scale*(-s->h/2-0.5), scale);
|
2011-11-19 21:41:28 +03:00
|
|
|
tmp_cidx= in_cidx;
|
2015-09-13 04:32:44 +02:00
|
|
|
memset(color, 0, sizeof(*color)*s->w);
|
|
|
|
for(y=0; y<s->h; y++){
|
2011-11-19 21:41:28 +03:00
|
|
|
int y1= y+1;
|
2015-09-13 04:32:44 +02:00
|
|
|
const double ci=s->start_y+scale*(y-s->h/2);
|
2011-11-19 21:41:28 +03:00
|
|
|
fill_from_cache(ctx, NULL, &in_cidx, &next_cidx, ci, scale);
|
2015-09-13 04:32:44 +02:00
|
|
|
if(y1<s->h){
|
|
|
|
memset(color+linesize*y1, 0, sizeof(*color)*s->w);
|
2011-11-19 21:41:28 +03:00
|
|
|
fill_from_cache(ctx, color+linesize*y1, &tmp_cidx, NULL, ci + 3*scale/2, scale);
|
|
|
|
}
|
2011-11-12 17:44:13 +03:00
|
|
|
|
2015-09-13 04:32:44 +02:00
|
|
|
for(x=0; x<s->w; x++){
|
2011-11-24 02:08:05 +03:00
|
|
|
float av_uninit(epsilon);
|
2015-09-13 04:32:44 +02:00
|
|
|
const double cr=s->start_x+scale*(x-s->w/2);
|
2013-04-08 11:19:11 +03:00
|
|
|
double zr=cr;
|
|
|
|
double zi=ci;
|
2011-11-12 00:47:06 +03:00
|
|
|
uint32_t c=0;
|
2015-09-13 04:32:44 +02:00
|
|
|
double dv= s->dither / (double)(1LL<<32);
|
|
|
|
s->dither= s->dither*1664525+1013904223;
|
2011-11-12 00:47:06 +03:00
|
|
|
|
2011-11-12 17:44:13 +03:00
|
|
|
if(color[x + y*linesize] & 0xFF000000)
|
|
|
|
continue;
|
2015-09-13 04:32:44 +02:00
|
|
|
if(!s->morphamp){
|
|
|
|
if(interpol(s, color, x, y, linesize)){
|
|
|
|
if(next_cidx < s->cache_allocated){
|
|
|
|
s->next_cache[next_cidx ].p[0]= cr;
|
|
|
|
s->next_cache[next_cidx ].p[1]= ci;
|
|
|
|
s->next_cache[next_cidx++].val = color[x + y*linesize];
|
2013-04-08 21:40:54 +03:00
|
|
|
}
|
|
|
|
continue;
|
2013-04-08 11:19:11 +03:00
|
|
|
}
|
|
|
|
}else{
|
2015-09-13 04:32:44 +02:00
|
|
|
zr += cos(pts * s->morphxf) * s->morphamp;
|
|
|
|
zi += sin(pts * s->morphyf) * s->morphamp;
|
2011-11-19 21:41:28 +03:00
|
|
|
}
|
2011-11-12 17:44:13 +03:00
|
|
|
|
2015-09-13 04:32:44 +02:00
|
|
|
use_zyklus= (x==0 || s->inner!=BLACK ||color[x-1 + y*linesize] == 0xFF000000);
|
2011-11-20 01:28:56 +03:00
|
|
|
if(use_zyklus)
|
avfilter/vsrc_mandelbrot: avoid sqrt for epsilon calculation
This rewrites into a similar expression avoiding sqrt. Similarity is
assured since sqrt(x^2 + y^2)/(x+y) lies in [1/sqrt(2), 1] for x, y > 0.
Tested on x86-64, Haswell, GNU/Linux.
Command:
ffmpeg -f lavfi -i mandelbrot -f null -
old (draw_mandelbrot):
277625266 decicycles in draw_mandelbrot, 256 runs, 0 skips
304527322 decicycles in draw_mandelbrot, 512 runs, 0 skips
377593582 decicycles in draw_mandelbrot, 1024 runs, 0 skips
338539499 decicycles in draw_mandelbrot, 2048 runs, 0 skips
583630357 decicycles in draw_mandelbrot, 4096 runs, 0 skips
new (draw_mandelbrot):
274635709 decicycles in draw_mandelbrot, 256 runs, 0 skips
300287046 decicycles in draw_mandelbrot, 512 runs, 0 skips
371819935 decicycles in draw_mandelbrot, 1024 runs, 0 skips
336663765 decicycles in draw_mandelbrot, 2048 runs, 0 skips
581851016 decicycles in draw_mandelbrot, 4096 runs, 0 skips
Reviewed-by: Michael Niedermayer <michael@niedermayer.cc>
Signed-off-by: Ganesh Ajjanagadde <gajjanagadde@gmail.com>
2015-11-22 18:49:15 +02:00
|
|
|
epsilon= scale*(abs(x-s->w/2) + abs(y-s->h/2))/s->w;
|
2011-11-13 17:14:25 +03:00
|
|
|
|
2011-11-19 15:51:44 +03:00
|
|
|
#define Z_Z2_C(outr,outi,inr,ini)\
|
|
|
|
outr= inr*inr - ini*ini + cr;\
|
|
|
|
outi= 2*inr*ini + ci;
|
|
|
|
|
2011-11-19 15:55:51 +03:00
|
|
|
#define Z_Z2_C_ZYKLUS(outr,outi,inr,ini, Z)\
|
|
|
|
Z_Z2_C(outr,outi,inr,ini)\
|
|
|
|
if(use_zyklus){\
|
2015-09-13 04:32:44 +02:00
|
|
|
if(Z && fabs(s->zyklus[i>>1][0]-outr)+fabs(s->zyklus[i>>1][1]-outi) <= epsilon)\
|
2011-11-19 15:55:51 +03:00
|
|
|
break;\
|
2011-11-19 17:02:34 +03:00
|
|
|
}\
|
2015-09-13 04:32:44 +02:00
|
|
|
s->zyklus[i][0]= outr;\
|
|
|
|
s->zyklus[i][1]= outi;\
|
2011-11-19 17:02:34 +03:00
|
|
|
|
2011-11-19 15:55:51 +03:00
|
|
|
|
|
|
|
|
2015-09-13 04:32:44 +02:00
|
|
|
for(i=0; i<s->maxiter-8; i++){
|
2011-11-19 16:15:56 +03:00
|
|
|
double t;
|
|
|
|
Z_Z2_C_ZYKLUS(t, zi, zr, zi, 0)
|
|
|
|
i++;
|
|
|
|
Z_Z2_C_ZYKLUS(zr, zi, t, zi, 1)
|
|
|
|
i++;
|
|
|
|
Z_Z2_C_ZYKLUS(t, zi, zr, zi, 0)
|
|
|
|
i++;
|
|
|
|
Z_Z2_C_ZYKLUS(zr, zi, t, zi, 1)
|
|
|
|
i++;
|
|
|
|
Z_Z2_C_ZYKLUS(t, zi, zr, zi, 0)
|
|
|
|
i++;
|
|
|
|
Z_Z2_C_ZYKLUS(zr, zi, t, zi, 1)
|
|
|
|
i++;
|
|
|
|
Z_Z2_C_ZYKLUS(t, zi, zr, zi, 0)
|
|
|
|
i++;
|
|
|
|
Z_Z2_C_ZYKLUS(zr, zi, t, zi, 1)
|
2015-09-13 04:32:44 +02:00
|
|
|
if(zr*zr + zi*zi > s->bailout){
|
2011-11-19 17:00:26 +03:00
|
|
|
i-= FFMIN(7, i);
|
2015-09-13 04:32:44 +02:00
|
|
|
for(; i<s->maxiter; i++){
|
|
|
|
zr= s->zyklus[i][0];
|
|
|
|
zi= s->zyklus[i][1];
|
|
|
|
if(zr*zr + zi*zi > s->bailout){
|
|
|
|
switch(s->outer){
|
2013-03-27 18:00:18 +03:00
|
|
|
case ITERATION_COUNT:
|
|
|
|
zr = i;
|
avfilter/vsrc_mandelbrot: change sin to sinf for color computation
lrintf is anyway used, suggesting we only care up to floating precision.
Rurthermore, there is a compat hack in avutil/libm for this function,
and it is used in avcodec/aacps_tablegen.h.
This yields a non-negligible speedup. Sample benchmark:
x86-64, Haswell, GNU/Linux:
old (draw_mandelbrot):
274635709 decicycles in draw_mandelbrot, 256 runs, 0 skips
300287046 decicycles in draw_mandelbrot, 512 runs, 0 skips
371819935 decicycles in draw_mandelbrot, 1024 runs, 0 skips
336663765 decicycles in draw_mandelbrot, 2048 runs, 0 skips
581851016 decicycles in draw_mandelbrot, 4096 runs, 0 skips
new (draw_mandelbrot):
269882717 decicycles in draw_mandelbrot, 256 runs, 0 skips
296359285 decicycles in draw_mandelbrot, 512 runs, 0 skips
370076599 decicycles in draw_mandelbrot, 1024 runs, 0 skips
331478354 decicycles in draw_mandelbrot, 2048 runs, 0 skips
571904318 decicycles in draw_mandelbrot, 4096 runs, 0 skips
Reviewed-by: Michael Niedermayer <michael@niedermayer.cc>
Signed-off-by: Ganesh Ajjanagadde <gajjanagadde@gmail.com>
2015-11-24 20:00:54 +02:00
|
|
|
c = lrintf((sinf(zr)+1)*127) + lrintf((sinf(zr/1.234)+1)*127)*256*256 + lrintf((sinf(zr/100)+1)*127)*256;
|
2013-03-27 18:00:18 +03:00
|
|
|
break;
|
|
|
|
case NORMALIZED_ITERATION_COUNT:
|
2015-09-13 04:32:44 +02:00
|
|
|
zr = i + log2(log(s->bailout) / log(zr*zr + zi*zi));
|
avfilter/vsrc_mandelbrot: change sin to sinf for color computation
lrintf is anyway used, suggesting we only care up to floating precision.
Rurthermore, there is a compat hack in avutil/libm for this function,
and it is used in avcodec/aacps_tablegen.h.
This yields a non-negligible speedup. Sample benchmark:
x86-64, Haswell, GNU/Linux:
old (draw_mandelbrot):
274635709 decicycles in draw_mandelbrot, 256 runs, 0 skips
300287046 decicycles in draw_mandelbrot, 512 runs, 0 skips
371819935 decicycles in draw_mandelbrot, 1024 runs, 0 skips
336663765 decicycles in draw_mandelbrot, 2048 runs, 0 skips
581851016 decicycles in draw_mandelbrot, 4096 runs, 0 skips
new (draw_mandelbrot):
269882717 decicycles in draw_mandelbrot, 256 runs, 0 skips
296359285 decicycles in draw_mandelbrot, 512 runs, 0 skips
370076599 decicycles in draw_mandelbrot, 1024 runs, 0 skips
331478354 decicycles in draw_mandelbrot, 2048 runs, 0 skips
571904318 decicycles in draw_mandelbrot, 4096 runs, 0 skips
Reviewed-by: Michael Niedermayer <michael@niedermayer.cc>
Signed-off-by: Ganesh Ajjanagadde <gajjanagadde@gmail.com>
2015-11-24 20:00:54 +02:00
|
|
|
c = lrintf((sinf(zr)+1)*127) + lrintf((sinf(zr/1.234)+1)*127)*256*256 + lrintf((sinf(zr/100)+1)*127)*256;
|
2013-03-27 18:00:18 +03:00
|
|
|
break;
|
|
|
|
case WHITE:
|
|
|
|
c = 0xFFFFFF;
|
2013-03-27 18:19:50 +03:00
|
|
|
break;
|
|
|
|
case OUTZ:
|
2015-09-13 04:32:44 +02:00
|
|
|
zr /= s->bailout;
|
|
|
|
zi /= s->bailout;
|
2013-03-27 18:19:50 +03:00
|
|
|
c = (((int)(zr*128+128))&0xFF)*256 + (((int)(zi*128+128))&0xFF);
|
2011-11-19 17:00:26 +03:00
|
|
|
}
|
|
|
|
break;
|
|
|
|
}
|
2011-11-12 01:22:04 +03:00
|
|
|
}
|
2011-11-12 00:47:06 +03:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
2011-11-15 19:25:16 +03:00
|
|
|
if(!c){
|
2015-09-13 04:32:44 +02:00
|
|
|
if(s->inner==PERIOD){
|
2015-02-15 02:53:21 +02:00
|
|
|
int j;
|
|
|
|
for(j=i-1; j; j--)
|
2015-09-13 04:32:44 +02:00
|
|
|
if(SQR(s->zyklus[j][0]-zr) + SQR(s->zyklus[j][1]-zi) < epsilon*epsilon*10)
|
2015-02-15 02:53:21 +02:00
|
|
|
break;
|
|
|
|
if(j){
|
|
|
|
c= i-j;
|
|
|
|
c= ((c<<5)&0xE0) + ((c<<10)&0xE000) + ((c<<15)&0xE00000);
|
|
|
|
}
|
2015-09-13 04:32:44 +02:00
|
|
|
}else if(s->inner==CONVTIME){
|
|
|
|
c= floor(i*255.0/s->maxiter+dv)*0x010101;
|
|
|
|
} else if(s->inner==MINCOL){
|
2011-11-15 21:58:58 +03:00
|
|
|
int j;
|
|
|
|
double closest=9999;
|
|
|
|
int closest_index=0;
|
2011-11-16 06:22:36 +03:00
|
|
|
for(j=i-1; j>=0; j--)
|
2015-09-13 04:32:44 +02:00
|
|
|
if(SQR(s->zyklus[j][0]) + SQR(s->zyklus[j][1]) < closest){
|
|
|
|
closest= SQR(s->zyklus[j][0]) + SQR(s->zyklus[j][1]);
|
2011-11-15 21:58:58 +03:00
|
|
|
closest_index= j;
|
|
|
|
}
|
|
|
|
closest = sqrt(closest);
|
2015-09-13 04:32:44 +02:00
|
|
|
c= lrintf((s->zyklus[closest_index][0]/closest+1)*127+dv) + lrintf((s->zyklus[closest_index][1]/closest+1)*127+dv)*256;
|
2011-11-15 19:25:16 +03:00
|
|
|
}
|
2011-11-13 21:28:24 +03:00
|
|
|
}
|
2015-11-24 03:59:59 +02:00
|
|
|
c |= 0xFF000000;
|
2011-11-12 00:47:06 +03:00
|
|
|
color[x + y*linesize]= c;
|
2015-09-13 04:32:44 +02:00
|
|
|
if(next_cidx < s->cache_allocated){
|
|
|
|
s->next_cache[next_cidx ].p[0]= cr;
|
|
|
|
s->next_cache[next_cidx ].p[1]= ci;
|
|
|
|
s->next_cache[next_cidx++].val = c;
|
2011-11-12 17:44:13 +03:00
|
|
|
}
|
2011-11-12 00:47:06 +03:00
|
|
|
}
|
2011-11-12 17:44:13 +03:00
|
|
|
fill_from_cache(ctx, NULL, &in_cidx, &next_cidx, ci + scale/2, scale);
|
2011-11-12 00:47:06 +03:00
|
|
|
}
|
2015-09-13 04:32:44 +02:00
|
|
|
FFSWAP(void*, s->next_cache, s->point_cache);
|
|
|
|
s->cache_used = next_cidx;
|
|
|
|
if(s->cache_used == s->cache_allocated)
|
2012-11-23 20:05:16 +03:00
|
|
|
av_log(ctx, AV_LOG_INFO, "Mandelbrot cache is too small!\n");
|
2011-11-12 00:47:06 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
static int request_frame(AVFilterLink *link)
|
|
|
|
{
|
2015-09-13 04:32:44 +02:00
|
|
|
MBContext *s = link->src->priv;
|
|
|
|
AVFrame *picref = ff_get_video_buffer(link, s->w, s->h);
|
2013-04-08 21:48:16 +03:00
|
|
|
if (!picref)
|
|
|
|
return AVERROR(ENOMEM);
|
|
|
|
|
2013-03-10 03:30:30 +03:00
|
|
|
picref->sample_aspect_ratio = (AVRational) {1, 1};
|
2015-09-13 04:32:44 +02:00
|
|
|
picref->pts = s->pts++;
|
2011-11-12 00:47:06 +03:00
|
|
|
|
2011-11-15 19:12:52 +03:00
|
|
|
draw_mandelbrot(link->src, (uint32_t*)picref->data[0], picref->linesize[0]/4, picref->pts);
|
2013-03-13 11:57:18 +03:00
|
|
|
return ff_filter_frame(link, picref);
|
2011-11-12 00:47:06 +03:00
|
|
|
}
|
|
|
|
|
2012-11-28 22:01:59 +03:00
|
|
|
static const AVFilterPad mandelbrot_outputs[] = {
|
|
|
|
{
|
|
|
|
.name = "default",
|
|
|
|
.type = AVMEDIA_TYPE_VIDEO,
|
|
|
|
.request_frame = request_frame,
|
|
|
|
.config_props = config_props,
|
|
|
|
},
|
2013-09-07 15:13:50 +03:00
|
|
|
{ NULL }
|
2012-11-28 22:01:59 +03:00
|
|
|
};
|
|
|
|
|
2013-10-29 13:50:56 +03:00
|
|
|
AVFilter ff_vsrc_mandelbrot = {
|
2013-09-07 15:13:50 +03:00
|
|
|
.name = "mandelbrot",
|
|
|
|
.description = NULL_IF_CONFIG_SMALL("Render a Mandelbrot fractal."),
|
|
|
|
.priv_size = sizeof(MBContext),
|
|
|
|
.priv_class = &mandelbrot_class,
|
|
|
|
.init = init,
|
|
|
|
.uninit = uninit,
|
2011-11-12 00:47:06 +03:00
|
|
|
.query_formats = query_formats,
|
2012-11-28 22:01:59 +03:00
|
|
|
.inputs = NULL,
|
|
|
|
.outputs = mandelbrot_outputs,
|
2011-11-12 00:47:06 +03:00
|
|
|
};
|