00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
00022
00023
00029 #include "avfilter.h"
00030 #include "libavutil/imgutils.h"
00031 #include "libavutil/opt.h"
00032 #include "libavutil/parseutils.h"
00033 #include <float.h>
00034 #include <math.h>
00035
00036 #define SQR(a) ((a)*(a))
00037
00038 enum Outer{
00039 ITERATION_COUNT,
00040 NORMALIZED_ITERATION_COUNT,
00041 };
00042
00043 enum Inner{
00044 BLACK,
00045 PERIOD,
00046 CONVTIME,
00047 MINCOL,
00048 };
00049
00050 typedef struct Point {
00051 double p[2];
00052 uint32_t val;
00053 } Point;
00054
00055 typedef struct {
00056 const AVClass *class;
00057 int w, h;
00058 AVRational time_base;
00059 uint64_t pts;
00060 char *rate;
00061 int maxiter;
00062 double start_x;
00063 double start_y;
00064 double start_scale;
00065 double end_scale;
00066 double end_pts;
00067 double bailout;
00068 enum Outer outer;
00069 enum Inner inner;
00070 int cache_allocated;
00071 int cache_used;
00072 Point *point_cache;
00073 Point *next_cache;
00074 double (*zyklus)[2];
00075 uint32_t dither;
00076 } MBContext;
00077
00078 #define OFFSET(x) offsetof(MBContext, x)
00079
00080 static const AVOption mandelbrot_options[] = {
00081 {"size", "set frame size", OFFSET(w), AV_OPT_TYPE_IMAGE_SIZE, {.str="640x480"}, CHAR_MIN, CHAR_MAX },
00082 {"s", "set frame size", OFFSET(w), AV_OPT_TYPE_IMAGE_SIZE, {.str="640x480"}, CHAR_MIN, CHAR_MAX },
00083 {"rate", "set frame rate", OFFSET(rate), AV_OPT_TYPE_STRING, {.str="25"}, CHAR_MIN, CHAR_MAX },
00084 {"r", "set frame rate", OFFSET(rate), AV_OPT_TYPE_STRING, {.str="25"}, CHAR_MIN, CHAR_MAX },
00085 {"maxiter", "set max iterations number", OFFSET(maxiter), AV_OPT_TYPE_INT, {.dbl=7189}, 1, INT_MAX },
00086 {"start_x", "set the initial x position", OFFSET(start_x), AV_OPT_TYPE_DOUBLE, {.dbl=-0.743643887037158704752191506114774}, -100, 100 },
00087 {"start_y", "set the initial y position", OFFSET(start_y), AV_OPT_TYPE_DOUBLE, {.dbl=-0.131825904205311970493132056385139}, -100, 100 },
00088 {"start_scale", "set the initial scale value", OFFSET(start_scale), AV_OPT_TYPE_DOUBLE, {.dbl=3.0}, 0, FLT_MAX },
00089 {"end_scale", "set the terminal scale value", OFFSET(end_scale), AV_OPT_TYPE_DOUBLE, {.dbl=0.3}, 0, FLT_MAX },
00090 {"end_pts", "set the terminal pts value", OFFSET(end_pts), AV_OPT_TYPE_DOUBLE, {.dbl=400}, 0, INT64_MAX },
00091 {"bailout", "set the bailout value", OFFSET(bailout), AV_OPT_TYPE_DOUBLE, {.dbl=10}, 0, FLT_MAX },
00092
00093 {"outer", "set outer coloring mode", OFFSET(outer), AV_OPT_TYPE_INT, {.dbl=NORMALIZED_ITERATION_COUNT}, 0, INT_MAX, 0, "outer"},
00094 {"iteration_count", "set iteration count mode", 0, AV_OPT_TYPE_CONST, {.dbl=ITERATION_COUNT}, INT_MIN, INT_MAX, 0, "outer" },
00095 {"normalized_iteration_count", "set normalized iteration count mode", 0, AV_OPT_TYPE_CONST, {.dbl=NORMALIZED_ITERATION_COUNT}, INT_MIN, INT_MAX, 0, "outer" },
00096
00097 {"inner", "set inner coloring mode", OFFSET(inner), AV_OPT_TYPE_INT, {.dbl=MINCOL}, 0, INT_MAX, 0, "inner"},
00098 {"black", "set black mode", 0, AV_OPT_TYPE_CONST, {.dbl=BLACK}, INT_MIN, INT_MAX, 0, "inner" },
00099 {"period", "set period mode", 0, AV_OPT_TYPE_CONST, {.dbl=PERIOD}, INT_MIN, INT_MAX, 0, "inner" },
00100 {"convergence", "show time until convergence", 0, AV_OPT_TYPE_CONST, {.dbl=CONVTIME}, INT_MIN, INT_MAX, 0, "inner" },
00101 {"mincol", "color based on point closest to the origin of the iterations", 0, AV_OPT_TYPE_CONST, {.dbl=MINCOL}, INT_MIN, INT_MAX, 0, "inner" },
00102
00103 {NULL},
00104 };
00105
00106 static const char *mandelbrot_get_name(void *ctx)
00107 {
00108 return "mandelbrot";
00109 }
00110
00111 static const AVClass mandelbrot_class = {
00112 "MBContext",
00113 mandelbrot_get_name,
00114 mandelbrot_options
00115 };
00116
00117 static av_cold int init(AVFilterContext *ctx, const char *args, void *opaque)
00118 {
00119 MBContext *mb = ctx->priv;
00120 AVRational rate_q;
00121 int err;
00122
00123 mb->class = &mandelbrot_class;
00124 av_opt_set_defaults(mb);
00125
00126 if ((err = (av_set_options_string(mb, args, "=", ":"))) < 0) {
00127 av_log(ctx, AV_LOG_ERROR, "Error parsing options string: '%s'\n", args);
00128 return err;
00129 }
00130 mb->bailout *= mb->bailout;
00131
00132 mb->start_scale /=mb->h;
00133 mb->end_scale /=mb->h;
00134
00135 if (av_parse_video_rate(&rate_q, mb->rate) < 0 ||
00136 rate_q.den <= 0 || rate_q.num <= 0) {
00137 av_log(ctx, AV_LOG_ERROR, "Invalid frame rate: %s\n", mb->rate);
00138 return AVERROR(EINVAL);
00139 }
00140 mb->time_base.num = rate_q.den;
00141 mb->time_base.den = rate_q.num;
00142
00143 mb->cache_allocated = mb->w * mb->h * 3;
00144 mb->cache_used = 0;
00145 mb->point_cache= av_malloc(sizeof(*mb->point_cache)*mb->cache_allocated);
00146 mb-> next_cache= av_malloc(sizeof(*mb-> next_cache)*mb->cache_allocated);
00147 mb-> zyklus = av_malloc(sizeof(*mb->zyklus) * (mb->maxiter+16));
00148
00149 return 0;
00150 }
00151
00152 static av_cold void uninit(AVFilterContext *ctx)
00153 {
00154 MBContext *mb = ctx->priv;
00155
00156 av_freep(&mb->rate);
00157 av_freep(&mb->point_cache);
00158 av_freep(&mb-> next_cache);
00159 av_freep(&mb->zyklus);
00160 }
00161
00162 static int query_formats(AVFilterContext *ctx)
00163 {
00164 static const enum PixelFormat pix_fmts[] = {
00165 PIX_FMT_BGR32,
00166 PIX_FMT_NONE
00167 };
00168
00169 avfilter_set_common_pixel_formats(ctx, avfilter_make_format_list(pix_fmts));
00170 return 0;
00171 }
00172
00173 static int config_props(AVFilterLink *inlink)
00174 {
00175 AVFilterContext *ctx = inlink->src;
00176 MBContext *mb = ctx->priv;
00177
00178 if (av_image_check_size(mb->w, mb->h, 0, ctx) < 0)
00179 return AVERROR(EINVAL);
00180
00181 inlink->w = mb->w;
00182 inlink->h = mb->h;
00183 inlink->time_base = mb->time_base;
00184
00185 return 0;
00186 }
00187
00188 static void fill_from_cache(AVFilterContext *ctx, uint32_t *color, int *in_cidx, int *out_cidx, double py, double scale){
00189 MBContext *mb = ctx->priv;
00190 for(; *in_cidx < mb->cache_used; (*in_cidx)++){
00191 Point *p= &mb->point_cache[*in_cidx];
00192 int x;
00193 if(p->p[1] > py)
00194 break;
00195 x= round((p->p[0] - mb->start_x) / scale + mb->w/2);
00196 if(x<0 || x >= mb->w)
00197 continue;
00198 if(color) color[x] = p->val;
00199 if(out_cidx && *out_cidx < mb->cache_allocated)
00200 mb->next_cache[(*out_cidx)++]= *p;
00201 }
00202 }
00203
00204 static int interpol(MBContext *mb, uint32_t *color, int x, int y, int linesize)
00205 {
00206 uint32_t a,b,c,d, i;
00207 uint32_t ipol=0xFF000000;
00208 int dist;
00209
00210 if(!x || !y || x+1==mb->w || y+1==mb->h)
00211 return 0;
00212
00213 dist= FFMAX(FFABS(x-(mb->w>>1))*mb->h, FFABS(y-(mb->h>>1))*mb->w);
00214
00215 if(dist<(mb->w*mb->h>>3))
00216 return 0;
00217
00218 a=color[(x+1) + (y+0)*linesize];
00219 b=color[(x-1) + (y+1)*linesize];
00220 c=color[(x+0) + (y+1)*linesize];
00221 d=color[(x+1) + (y+1)*linesize];
00222
00223 if(a&&c){
00224 b= color[(x-1) + (y+0)*linesize];
00225 d= color[(x+0) + (y-1)*linesize];
00226 }else if(b&&d){
00227 a= color[(x+1) + (y-1)*linesize];
00228 c= color[(x-1) + (y-1)*linesize];
00229 }else if(c){
00230 d= color[(x+0) + (y-1)*linesize];
00231 a= color[(x-1) + (y+0)*linesize];
00232 b= color[(x+1) + (y-1)*linesize];
00233 }else if(d){
00234 c= color[(x-1) + (y-1)*linesize];
00235 a= color[(x-1) + (y+0)*linesize];
00236 b= color[(x+1) + (y-1)*linesize];
00237 }else
00238 return 0;
00239
00240 for(i=0; i<3; i++){
00241 int s= 8*i;
00242 uint8_t ac= a>>s;
00243 uint8_t bc= b>>s;
00244 uint8_t cc= c>>s;
00245 uint8_t dc= d>>s;
00246 int ipolab= (ac + bc);
00247 int ipolcd= (cc + dc);
00248 if(FFABS(ipolab - ipolcd) > 5)
00249 return 0;
00250 if(FFABS(ac-bc)+FFABS(cc-dc) > 20)
00251 return 0;
00252 ipol |= ((ipolab + ipolcd + 2)/4)<<s;
00253 }
00254 color[x + y*linesize]= ipol;
00255 return 1;
00256 }
00257
00258 static void draw_mandelbrot(AVFilterContext *ctx, uint32_t *color, int linesize, int64_t pts)
00259 {
00260 MBContext *mb = ctx->priv;
00261 int x,y,i, in_cidx=0, next_cidx=0, tmp_cidx;
00262 double scale= mb->start_scale*pow(mb->end_scale/mb->start_scale, pts/mb->end_pts);
00263 int use_zyklus=0;
00264 fill_from_cache(ctx, NULL, &in_cidx, NULL, mb->start_y+scale*(-mb->h/2-0.5), scale);
00265 tmp_cidx= in_cidx;
00266 memset(color, 0, sizeof(*color)*mb->w);
00267 for(y=0; y<mb->h; y++){
00268 int y1= y+1;
00269 const double ci=mb->start_y+scale*(y-mb->h/2);
00270 fill_from_cache(ctx, NULL, &in_cidx, &next_cidx, ci, scale);
00271 if(y1<mb->h){
00272 memset(color+linesize*y1, 0, sizeof(*color)*mb->w);
00273 fill_from_cache(ctx, color+linesize*y1, &tmp_cidx, NULL, ci + 3*scale/2, scale);
00274 }
00275
00276 for(x=0; x<mb->w; x++){
00277 float av_uninit(epsilon);
00278 const double cr=mb->start_x+scale*(x-mb->w/2);
00279 double zr=cr;
00280 double zi=ci;
00281 uint32_t c=0;
00282 double dv= mb->dither / (double)(1LL<<32);
00283 mb->dither= mb->dither*1664525+1013904223;
00284
00285 if(color[x + y*linesize] & 0xFF000000)
00286 continue;
00287 if(interpol(mb, color, x, y, linesize)){
00288 if(next_cidx < mb->cache_allocated){
00289 mb->next_cache[next_cidx ].p[0]= cr;
00290 mb->next_cache[next_cidx ].p[1]= ci;
00291 mb->next_cache[next_cidx++].val = color[x + y*linesize];
00292 }
00293 continue;
00294 }
00295
00296 use_zyklus= (x==0 || mb->inner!=BLACK ||color[x-1 + y*linesize] == 0xFF000000);
00297 if(use_zyklus)
00298 epsilon= scale*1*sqrt(SQR(x-mb->w/2) + SQR(y-mb->h/2))/mb->w;
00299
00300 #define Z_Z2_C(outr,outi,inr,ini)\
00301 outr= inr*inr - ini*ini + cr;\
00302 outi= 2*inr*ini + ci;
00303
00304 #define Z_Z2_C_ZYKLUS(outr,outi,inr,ini, Z)\
00305 Z_Z2_C(outr,outi,inr,ini)\
00306 if(use_zyklus){\
00307 if(Z && fabs(mb->zyklus[i>>1][0]-outr)+fabs(mb->zyklus[i>>1][1]-outi) <= epsilon)\
00308 break;\
00309 }\
00310 mb->zyklus[i][0]= outr;\
00311 mb->zyklus[i][1]= outi;\
00312
00313
00314
00315 for(i=0; i<mb->maxiter-8; i++){
00316 double t;
00317 Z_Z2_C_ZYKLUS(t, zi, zr, zi, 0)
00318 i++;
00319 Z_Z2_C_ZYKLUS(zr, zi, t, zi, 1)
00320 i++;
00321 Z_Z2_C_ZYKLUS(t, zi, zr, zi, 0)
00322 i++;
00323 Z_Z2_C_ZYKLUS(zr, zi, t, zi, 1)
00324 i++;
00325 Z_Z2_C_ZYKLUS(t, zi, zr, zi, 0)
00326 i++;
00327 Z_Z2_C_ZYKLUS(zr, zi, t, zi, 1)
00328 i++;
00329 Z_Z2_C_ZYKLUS(t, zi, zr, zi, 0)
00330 i++;
00331 Z_Z2_C_ZYKLUS(zr, zi, t, zi, 1)
00332 if(zr*zr + zi*zi > mb->bailout){
00333 i-= FFMIN(7, i);
00334 for(; i<mb->maxiter; i++){
00335 zr= mb->zyklus[i][0];
00336 zi= mb->zyklus[i][1];
00337 if(zr*zr + zi*zi > mb->bailout){
00338 switch(mb->outer){
00339 case ITERATION_COUNT: zr = i; break;
00340 case NORMALIZED_ITERATION_COUNT: zr= i + log2(log(mb->bailout) / log(zr*zr + zi*zi)); break;
00341 }
00342 c= lrintf((sin(zr)+1)*127) + lrintf((sin(zr/1.234)+1)*127)*256*256 + lrintf((sin(zr/100)+1)*127)*256;
00343 break;
00344 }
00345 }
00346 break;
00347 }
00348 }
00349 if(!c){
00350 if(mb->inner==PERIOD){
00351 int j;
00352 for(j=i-1; j; j--)
00353 if(SQR(mb->zyklus[j][0]-zr) + SQR(mb->zyklus[j][1]-zi) < epsilon*epsilon*10)
00354 break;
00355 if(j){
00356 c= i-j;
00357 c= ((c<<5)&0xE0) + ((c<<16)&0xE000) + ((c<<27)&0xE00000);
00358 }
00359 }else if(mb->inner==CONVTIME){
00360 c= floor(i*255.0/mb->maxiter+dv)*0x010101;
00361 } else if(mb->inner==MINCOL){
00362 int j;
00363 double closest=9999;
00364 int closest_index=0;
00365 for(j=i-1; j>=0; j--)
00366 if(SQR(mb->zyklus[j][0]) + SQR(mb->zyklus[j][1]) < closest){
00367 closest= SQR(mb->zyklus[j][0]) + SQR(mb->zyklus[j][1]);
00368 closest_index= j;
00369 }
00370 closest = sqrt(closest);
00371 c= lrintf((mb->zyklus[closest_index][0]/closest+1)*127+dv) + lrintf((mb->zyklus[closest_index][1]/closest+1)*127+dv)*256;
00372 }
00373 }
00374 c |= 0xFF000000;
00375 color[x + y*linesize]= c;
00376 if(next_cidx < mb->cache_allocated){
00377 mb->next_cache[next_cidx ].p[0]= cr;
00378 mb->next_cache[next_cidx ].p[1]= ci;
00379 mb->next_cache[next_cidx++].val = c;
00380 }
00381 }
00382 fill_from_cache(ctx, NULL, &in_cidx, &next_cidx, ci + scale/2, scale);
00383 }
00384 FFSWAP(void*, mb->next_cache, mb->point_cache);
00385 mb->cache_used = next_cidx;
00386 if(mb->cache_used == mb->cache_allocated)
00387 av_log(0, AV_LOG_INFO, "Mandelbrot cache is too small!\n");
00388 }
00389
00390 static int request_frame(AVFilterLink *link)
00391 {
00392 MBContext *mb = link->src->priv;
00393 AVFilterBufferRef *picref = avfilter_get_video_buffer(link, AV_PERM_WRITE, mb->w, mb->h);
00394 picref->video->sample_aspect_ratio = (AVRational) {1, 1};
00395 picref->pts = mb->pts++;
00396 picref->pos = -1;
00397
00398 avfilter_start_frame(link, avfilter_ref_buffer(picref, ~0));
00399 draw_mandelbrot(link->src, (uint32_t*)picref->data[0], picref->linesize[0]/4, picref->pts);
00400 avfilter_draw_slice(link, 0, mb->h, 1);
00401 avfilter_end_frame(link);
00402 avfilter_unref_buffer(picref);
00403
00404 return 0;
00405 }
00406
00407 AVFilter avfilter_vsrc_mandelbrot = {
00408 .name = "mandelbrot",
00409 .description = NULL_IF_CONFIG_SMALL("Render a Mandelbrot fractal."),
00410
00411 .priv_size = sizeof(MBContext),
00412 .init = init,
00413 .uninit = uninit,
00414
00415 .query_formats = query_formats,
00416
00417 .inputs = (const AVFilterPad[]) {{ .name = NULL}},
00418
00419 .outputs = (const AVFilterPad[]) {{ .name = "default",
00420 .type = AVMEDIA_TYPE_VIDEO,
00421 .request_frame = request_frame,
00422 .config_props = config_props },
00423 { .name = NULL}},
00424 };