]> git.sesse.net Git - ffmpeg/blobdiff - libavfilter/vsrc_mandelbrot.c
Merge remote-tracking branch 'qatar/master'
[ffmpeg] / libavfilter / vsrc_mandelbrot.c
index 9ebab011cf5d0864363016c886557fb722c4bd18..cc9cb6e9bfb114f5c764f25eccf4b5b675adcd47 100644 (file)
@@ -43,6 +43,8 @@ enum Outer{
 enum Inner{
     BLACK,
     PERIOD,
+    CONVTIME,
+    MINCOL,
 };
 
 typedef struct Point {
@@ -70,6 +72,7 @@ typedef struct {
     Point *point_cache;
     Point *next_cache;
     double (*zyklus)[2];
+    uint32_t dither;
 } MBContext;
 
 #define OFFSET(x) offsetof(MBContext, x)
@@ -79,21 +82,23 @@ static const AVOption mandelbrot_options[] = {
     {"s",           "set frame size",                OFFSET(size),    AV_OPT_TYPE_STRING,     {.str="640x480"},  CHAR_MIN, CHAR_MAX },
     {"rate",        "set frame rate",                OFFSET(rate),    AV_OPT_TYPE_STRING,     {.str="25"},  CHAR_MIN, CHAR_MAX },
     {"r",           "set frame rate",                OFFSET(rate),    AV_OPT_TYPE_STRING,     {.str="25"},  CHAR_MIN, CHAR_MAX },
-    {"maxiter",     "set max iterations number",     OFFSET(maxiter), AV_OPT_TYPE_INT,        {.dbl=4096},  1,        INT_MAX  },
+    {"maxiter",     "set max iterations number",     OFFSET(maxiter), AV_OPT_TYPE_INT,        {.dbl=7189},  1,        INT_MAX  },
     {"start_x",     "set the initial x position",    OFFSET(start_x), AV_OPT_TYPE_DOUBLE,     {.dbl=-0.743643887037158704752191506114774}, -100, 100  },
     {"start_y",     "set the initial y position",    OFFSET(start_y), AV_OPT_TYPE_DOUBLE,     {.dbl=-0.131825904205311970493132056385139}, -100, 100  },
     {"start_scale", "set the initial scale value",   OFFSET(start_scale), AV_OPT_TYPE_DOUBLE, {.dbl=3.0},  0, FLT_MAX },
     {"end_scale",   "set the terminal scale value",  OFFSET(end_scale), AV_OPT_TYPE_DOUBLE,   {.dbl=0.3},  0, FLT_MAX },
-    {"end_pts",     "set the terminal pts value",    OFFSET(end_pts), AV_OPT_TYPE_DOUBLE,     {.dbl=800},  0, INT64_MAX },
+    {"end_pts",     "set the terminal pts value",    OFFSET(end_pts), AV_OPT_TYPE_DOUBLE,     {.dbl=400},  0, INT64_MAX },
     {"bailout",     "set the bailout value",         OFFSET(bailout), AV_OPT_TYPE_DOUBLE,     {.dbl=10},   0, FLT_MAX },
 
     {"outer",       "set outer coloring mode",       OFFSET(outer), AV_OPT_TYPE_INT, {.dbl=NORMALIZED_ITERATION_COUNT}, 0, INT_MAX, 0, "outer"},
     {"iteration_count", "set iteration count mode",  0, AV_OPT_TYPE_CONST, {.dbl=ITERATION_COUNT}, INT_MIN, INT_MAX, 0, "outer" },
     {"normalized_iteration_count", "set normalized iteration count mode",   0, AV_OPT_TYPE_CONST, {.dbl=NORMALIZED_ITERATION_COUNT}, INT_MIN, INT_MAX, 0, "outer" },
 
-    {"inner",       "set inner coloring mode",       OFFSET(inner), AV_OPT_TYPE_INT, {.dbl=BLACK}, 0, INT_MAX, 0, "inner"},
+    {"inner",       "set inner coloring mode",       OFFSET(inner), AV_OPT_TYPE_INT, {.dbl=MINCOL}, 0, INT_MAX, 0, "inner"},
     {"black",       "set black mode",                0, AV_OPT_TYPE_CONST, {.dbl=BLACK}, INT_MIN, INT_MAX, 0, "inner" },
     {"period",      "set period mode",               0, AV_OPT_TYPE_CONST, {.dbl=PERIOD}, INT_MIN, INT_MAX, 0, "inner" },
+    {"convergence", "show time until convergence",   0, AV_OPT_TYPE_CONST, {.dbl=CONVTIME}, INT_MIN, INT_MAX, 0, "inner" },
+    {"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" },
 
     {NULL},
 };
@@ -143,7 +148,7 @@ static av_cold int init(AVFilterContext *ctx, const char *args, void *opaque)
     mb->cache_used = 0;
     mb->point_cache= av_malloc(sizeof(*mb->point_cache)*mb->cache_allocated);
     mb-> next_cache= av_malloc(sizeof(*mb-> next_cache)*mb->cache_allocated);
-    mb-> zyklus    = av_malloc(sizeof(*mb->zyklus) * mb->maxiter);
+    mb-> zyklus    = av_malloc(sizeof(*mb->zyklus) * (mb->maxiter+16));
 
     return 0;
 }
@@ -201,6 +206,60 @@ static void fill_from_cache(AVFilterContext *ctx, uint32_t *color, int *in_cidx,
     }
 }
 
+static int interpol(MBContext *mb, uint32_t *color, int x, int y, int linesize)
+{
+    uint32_t a,b,c,d, i;
+    uint32_t ipol=0xFF000000;
+    int dist;
+
+    if(!x || !y || x+1==mb->w || y+1==mb->h)
+        return 0;
+
+    dist= FFMAX(FFABS(x-(mb->w>>1))*mb->h, FFABS(y-(mb->h>>1))*mb->w);
+
+    if(dist<(mb->w*mb->h>>3))
+        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;
+        if(FFABS(ac-bc)+FFABS(cc-dc) > 20)
+            return 0;
+        ipol |= ((ipolab + ipolcd + 2)/4)<<s;
+    }
+    color[x + y*linesize]= ipol;
+    return 1;
+}
+
 static void draw_mandelbrot(AVFilterContext *ctx, uint32_t *color, int linesize, int64_t pts)
 {
     MBContext *mb = ctx->priv;
@@ -208,59 +267,114 @@ static void draw_mandelbrot(AVFilterContext *ctx, uint32_t *color, int linesize,
     double scale= mb->start_scale*pow(mb->end_scale/mb->start_scale, pts/mb->end_pts);
     int use_zyklus=0;
     fill_from_cache(ctx, NULL, &in_cidx, NULL, mb->start_y+scale*(-mb->h/2-0.5), scale);
+    tmp_cidx= in_cidx;
+    memset(color, 0, sizeof(*color)*mb->w);
     for(y=0; y<mb->h; y++){
+        int y1= y+1;
         const double ci=mb->start_y+scale*(y-mb->h/2);
-        memset(color+linesize*y, 0, sizeof(*color)*mb->w);
-        fill_from_cache(ctx, color+linesize*y, &in_cidx, &next_cidx, ci, scale);
-        tmp_cidx= in_cidx;
-        fill_from_cache(ctx, color+linesize*y, &tmp_cidx, NULL, ci + scale/2, scale);
+        fill_from_cache(ctx, NULL, &in_cidx, &next_cidx, ci, scale);
+        if(y1<mb->h){
+            memset(color+linesize*y1, 0, sizeof(*color)*mb->w);
+            fill_from_cache(ctx, color+linesize*y1, &tmp_cidx, NULL, ci + 3*scale/2, scale);
+        }
 
         for(x=0; x<mb->w; x++){
+            float av_uninit(epsilon);
             const double cr=mb->start_x+scale*(x-mb->w/2);
             double zr=cr;
             double zi=ci;
             uint32_t c=0;
+            double dv= mb->dither / (double)(1LL<<32);
+            mb->dither= mb->dither*1664525+1013904223;
 
             if(color[x + y*linesize] & 0xFF000000)
                 continue;
+            if(interpol(mb, color, x, y, linesize)){
+                if(next_cidx < mb->cache_allocated){
+                    mb->next_cache[next_cidx  ].p[0]= cr;
+                    mb->next_cache[next_cidx  ].p[1]= ci;
+                    mb->next_cache[next_cidx++].val = color[x + y*linesize];
+                }
+                continue;
+            }
 
             use_zyklus= (x==0 || mb->inner!=BLACK ||color[x-1 + y*linesize] == 0xFF000000);
+            if(use_zyklus)
+                epsilon= scale*1*sqrt(SQR(x-mb->w/2) + SQR(y-mb->h/2))/mb->w;
+
+#define Z_Z2_C(outr,outi,inr,ini)\
+            outr= inr*inr - ini*ini + cr;\
+            outi= 2*inr*ini + ci;
+
+#define Z_Z2_C_ZYKLUS(outr,outi,inr,ini, Z)\
+            Z_Z2_C(outr,outi,inr,ini)\
+            if(use_zyklus){\
+                if(Z && fabs(mb->zyklus[i>>1][0]-outr)+fabs(mb->zyklus[i>>1][1]-outi) <= epsilon)\
+                    break;\
+            }\
+            mb->zyklus[i][0]= outr;\
+            mb->zyklus[i][1]= outi;\
+
+
 
-            for(i=0; i<mb->maxiter; i++){
+            for(i=0; i<mb->maxiter-8; i++){
                 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)
                 if(zr*zr + zi*zi > mb->bailout){
-                    switch(mb->outer){
-                    case            ITERATION_COUNT: zr = i - (SQR(zr-cr)+SQR(zi-ci) > SQR(mb->bailout)); break;
-                    case NORMALIZED_ITERATION_COUNT: zr= i + log2(log(mb->bailout) / log(zr*zr + zi*zi)); break;
+                    i-= FFMIN(7, i);
+                    for(; i<mb->maxiter; i++){
+                        zr= mb->zyklus[i][0];
+                        zi= mb->zyklus[i][1];
+                        if(zr*zr + zi*zi > mb->bailout){
+                            switch(mb->outer){
+                            case            ITERATION_COUNT: zr = i; break;
+                            case NORMALIZED_ITERATION_COUNT: zr= i + log2(log(mb->bailout) / log(zr*zr + zi*zi)); break;
+                            }
+                            c= lrintf((sin(zr)+1)*127) + lrintf((sin(zr/1.234)+1)*127)*256*256 + lrintf((sin(zr/100)+1)*127)*256;
+                            break;
+                        }
                     }
-                    c= lrintf((sin(zr)+1)*127) + lrintf((sin(zr/1.234)+1)*127)*256*256 + lrintf((sin(zr/100)+1)*127)*256;
                     break;
                 }
-                t= zr*zr - zi*zi + cr;
-                zi= 2*zr*zi + ci;
-                if(use_zyklus){
-                    mb->zyklus[i][0]= t;
-                    mb->zyklus[i][1]= zi;
-                }
-                i++;
-                zr= t*t - zi*zi+cr;
-                zi= 2*t*zi + ci;
-                if(use_zyklus){
-                    if(mb->zyklus[i>>1][0]==zr && mb->zyklus[i>>1][1]==zi)
-                        break;
-                    mb->zyklus[i][0]= zr;
-                    mb->zyklus[i][1]= zi;
-                }
             }
-            if(!c && mb->inner==PERIOD){
+            if(!c){
+                if(mb->inner==PERIOD){
                 int j;
                 for(j=i-1; j; j--)
-                    if(SQR(mb->zyklus[j][0]-zr) + SQR(mb->zyklus[j][1]-zi) < 0.0000000000000001)
+                    if(SQR(mb->zyklus[j][0]-zr) + SQR(mb->zyklus[j][1]-zi) < epsilon*epsilon*10)
                         break;
                 if(j){
                     c= i-j;
                     c= ((c<<5)&0xE0) + ((c<<16)&0xE000) + ((c<<27)&0xE00000);
                 }
+                }else if(mb->inner==CONVTIME){
+                    c= floor(i*255.0/mb->maxiter+dv)*0x010101;
+                } else if(mb->inner==MINCOL){
+                    int j;
+                    double closest=9999;
+                    int closest_index=0;
+                    for(j=i-1; j>=0; j--)
+                        if(SQR(mb->zyklus[j][0]) + SQR(mb->zyklus[j][1]) < closest){
+                            closest= SQR(mb->zyklus[j][0]) + SQR(mb->zyklus[j][1]);
+                            closest_index= j;
+                        }
+                    closest = sqrt(closest);
+                    c= lrintf((mb->zyklus[closest_index][0]/closest+1)*127+dv) + lrintf((mb->zyklus[closest_index][1]/closest+1)*127+dv)*256;
+                }
             }
             c |= 0xFF000000;
             color[x + y*linesize]= c;
@@ -297,7 +411,7 @@ static int request_frame(AVFilterLink *link)
 
 AVFilter avfilter_vsrc_mandelbrot = {
     .name        = "mandelbrot",
-    .description = NULL_IF_CONFIG_SMALL("Mandelbrot renderer"),
+    .description = NULL_IF_CONFIG_SMALL("Mandelbrot fractal renderer"),
 
     .priv_size = sizeof(MBContext),
     .init      = init,