libavfilter/vsrc_mandelbrot.c
658d166d
 /*
  * 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
  * Mandelbrot fraktal renderer
  */
 
 #include "avfilter.h"
 #include "libavutil/imgutils.h"
3a9f2f1d
 #include "libavutil/opt.h"
658d166d
 #include "libavutil/parseutils.h"
70eb58cd
 #include <float.h>
73af8ea3
 #include <math.h>
658d166d
 
41fd1b2d
 #define SQR(a) ((a)*(a))
 
4ccd898a
 enum Outer{
     ITERATION_COUNT,
     NORMALIZED_ITERATION_COUNT,
 };
 
e8499ab1
 enum Inner{
     BLACK,
08dadd33
     PERIOD,
5a68b1ac
     CONVTIME,
8d51cb4f
     MINCOL,
e8499ab1
 };
 
07f272bd
 typedef struct Point {
     double p[2];
     uint32_t val;
 } Point;
 
658d166d
 typedef struct {
3a9f2f1d
     const AVClass *class;
658d166d
     int w, h;
     AVRational time_base;
     uint64_t pts;
3a9f2f1d
     char *size, *rate;
658d166d
     int maxiter;
     double start_x;
     double start_y;
     double start_scale;
ddeb194b
     double end_scale;
     double end_pts;
75e30916
     double bailout;
4ccd898a
     enum Outer outer;
e8499ab1
     enum Inner inner;
07f272bd
     int cache_allocated;
     int cache_used;
     Point *point_cache;
     Point *next_cache;
50b8f938
     double (*zyklus)[2];
04bb26e3
     uint32_t dither;
658d166d
 } MBContext;
 
3a9f2f1d
 #define OFFSET(x) offsetof(MBContext, x)
 
 static const AVOption mandelbrot_options[] = {
     {"size",        "set frame size",                OFFSET(size),    AV_OPT_TYPE_STRING,     {.str="640x480"},  CHAR_MIN, CHAR_MAX },
     {"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 },
90c02ae1
     {"maxiter",     "set max iterations number",     OFFSET(maxiter), AV_OPT_TYPE_INT,        {.dbl=7189},  1,        INT_MAX  },
70eb58cd
     {"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 },
5dd16af6
     {"end_pts",     "set the terminal pts value",    OFFSET(end_pts), AV_OPT_TYPE_DOUBLE,     {.dbl=400},  0, INT64_MAX },
70eb58cd
     {"bailout",     "set the bailout value",         OFFSET(bailout), AV_OPT_TYPE_DOUBLE,     {.dbl=10},   0, FLT_MAX },
3a9f2f1d
 
     {"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" },
 
7575980b
     {"inner",       "set inner coloring mode",       OFFSET(inner), AV_OPT_TYPE_INT, {.dbl=MINCOL}, 0, INT_MAX, 0, "inner"},
3a9f2f1d
     {"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" },
5a68b1ac
     {"convergence", "show time until convergence",   0, AV_OPT_TYPE_CONST, {.dbl=CONVTIME}, INT_MIN, INT_MAX, 0, "inner" },
974d25e2
     {"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" },
3a9f2f1d
 
     {NULL},
 };
 
 static const char *mandelbrot_get_name(void *ctx)
 {
     return "mandelbrot";
 }
 
 static const AVClass mandelbrot_class = {
     "MBContext",
     mandelbrot_get_name,
     mandelbrot_options
 };
 
658d166d
 static av_cold int init(AVFilterContext *ctx, const char *args, void *opaque)
 {
     MBContext *mb = ctx->priv;
3a9f2f1d
     AVRational rate_q;
     int err;
 
     mb->class = &mandelbrot_class;
     av_opt_set_defaults(mb);
 
     if ((err = (av_set_options_string(mb, args, "=", ":"))) < 0) {
         av_log(ctx, AV_LOG_ERROR, "Error parsing options string: '%s'\n", args);
         return err;
     }
459bba58
     mb->bailout *= mb->bailout;
658d166d
 
3a9f2f1d
     if (av_parse_video_size(&mb->w, &mb->h, mb->size) < 0) {
         av_log(ctx, AV_LOG_ERROR, "Invalid frame size: %s\n", mb->size);
658d166d
         return AVERROR(EINVAL);
     }
     mb->start_scale /=mb->h;
ddeb194b
     mb->end_scale /=mb->h;
658d166d
 
3a9f2f1d
     if (av_parse_video_rate(&rate_q, mb->rate) < 0 ||
         rate_q.den <= 0 || rate_q.num <= 0) {
         av_log(ctx, AV_LOG_ERROR, "Invalid frame rate: %s\n", mb->rate);
658d166d
         return AVERROR(EINVAL);
     }
3a9f2f1d
     mb->time_base.num = rate_q.den;
     mb->time_base.den = rate_q.num;
658d166d
 
905202ef
     mb->cache_allocated = mb->w * mb->h * 3;
07f272bd
     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);
e749b378
     mb-> zyklus    = av_malloc(sizeof(*mb->zyklus) * (mb->maxiter+16));
07f272bd
 
658d166d
     return 0;
 }
 
 static av_cold void uninit(AVFilterContext *ctx)
 {
     MBContext *mb = ctx->priv;
 
3a9f2f1d
     av_freep(&mb->size);
     av_freep(&mb->rate);
07f272bd
     av_freep(&mb->point_cache);
     av_freep(&mb-> next_cache);
50b8f938
     av_freep(&mb->zyklus);
658d166d
 }
 
 static int query_formats(AVFilterContext *ctx)
 {
     static const enum PixelFormat pix_fmts[] = {
         PIX_FMT_BGR32,
         PIX_FMT_NONE
     };
 
     avfilter_set_common_pixel_formats(ctx, avfilter_make_format_list(pix_fmts));
     return 0;
 }
 
 static int config_props(AVFilterLink *inlink)
 {
     AVFilterContext *ctx = inlink->src;
     MBContext *mb = ctx->priv;
 
     if (av_image_check_size(mb->w, mb->h, 0, ctx) < 0)
         return AVERROR(EINVAL);
 
     inlink->w = mb->w;
     inlink->h = mb->h;
     inlink->time_base = mb->time_base;
 
     return 0;
 }
 
07f272bd
 static void fill_from_cache(AVFilterContext *ctx, uint32_t *color, int *in_cidx, int *out_cidx, double py, double scale){
     MBContext *mb = ctx->priv;
     for(; *in_cidx < mb->cache_used; (*in_cidx)++){
         Point *p= &mb->point_cache[*in_cidx];
         int x;
c15400af
         if(p->p[1] > py)
07f272bd
             break;
         x= round((p->p[0] - mb->start_x) / scale + mb->w/2);
         if(x<0 || x >= mb->w)
             continue;
         if(color) color[x] = p->val;
         if(out_cidx && *out_cidx < mb->cache_allocated)
             mb->next_cache[(*out_cidx)++]= *p;
     }
 }
 
ca148082
 static int interpol(MBContext *mb, uint32_t *color, int x, int y, int linesize)
 {
     uint32_t a,b,c,d, i;
4ebbcdb3
     uint32_t ipol=0xFF000000;
ca148082
     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;
a5dfedd3
         if(FFABS(ac-bc)+FFABS(cc-dc) > 20)
             return 0;
ca148082
         ipol |= ((ipolab + ipolcd + 2)/4)<<s;
     }
4ebbcdb3
     color[x + y*linesize]= ipol;
ca148082
     return 1;
 }
 
658d166d
 static void draw_mandelbrot(AVFilterContext *ctx, uint32_t *color, int linesize, int64_t pts)
 {
     MBContext *mb = ctx->priv;
07f272bd
     int x,y,i, in_cidx=0, next_cidx=0, tmp_cidx;
ddeb194b
     double scale= mb->start_scale*pow(mb->end_scale/mb->start_scale, pts/mb->end_pts);
6c5dd858
     int use_zyklus=0;
07f272bd
     fill_from_cache(ctx, NULL, &in_cidx, NULL, mb->start_y+scale*(-mb->h/2-0.5), scale);
ca148082
     tmp_cidx= in_cidx;
     memset(color, 0, sizeof(*color)*mb->w);
658d166d
     for(y=0; y<mb->h; y++){
ca148082
         int y1= y+1;
07f272bd
         const double ci=mb->start_y+scale*(y-mb->h/2);
ca148082
         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);
         }
07f272bd
 
658d166d
         for(x=0; x<mb->w; x++){
656911d8
             float av_uninit(epsilon);
ddeb194b
             const double cr=mb->start_x+scale*(x-mb->w/2);
658d166d
             double zr=cr;
             double zi=ci;
             uint32_t c=0;
04bb26e3
             double dv= mb->dither / (double)(1LL<<32);
             mb->dither= mb->dither*1664525+1013904223;
658d166d
 
07f272bd
             if(color[x + y*linesize] & 0xFF000000)
                 continue;
ca148082
             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;
             }
07f272bd
 
08dadd33
             use_zyklus= (x==0 || mb->inner!=BLACK ||color[x-1 + y*linesize] == 0xFF000000);
690860d0
             if(use_zyklus)
                 epsilon= scale*1*sqrt(SQR(x-mb->w/2) + SQR(y-mb->h/2))/mb->w;
2fee131b
 
cf670fbc
 #define Z_Z2_C(outr,outi,inr,ini)\
             outr= inr*inr - ini*ini + cr;\
             outi= 2*inr*ini + ci;
 
fe2efc52
 #define Z_Z2_C_ZYKLUS(outr,outi,inr,ini, Z)\
             Z_Z2_C(outr,outi,inr,ini)\
             if(use_zyklus){\
690860d0
                 if(Z && fabs(mb->zyklus[i>>1][0]-outr)+fabs(mb->zyklus[i>>1][1]-outi) <= epsilon)\
fe2efc52
                     break;\
56fc4cf0
             }\
             mb->zyklus[i][0]= outr;\
             mb->zyklus[i][1]= outi;\
 
fe2efc52
 
 
649d3932
             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)
75e30916
                 if(zr*zr + zi*zi > mb->bailout){
da1344e0
                     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;
                         }
4ccd898a
                     }
658d166d
                     break;
                 }
             }
5a68b1ac
             if(!c){
                 if(mb->inner==PERIOD){
08dadd33
                 int j;
                 for(j=i-1; j; j--)
2c44aed8
                     if(SQR(mb->zyklus[j][0]-zr) + SQR(mb->zyklus[j][1]-zi) < epsilon*epsilon*10)
08dadd33
                         break;
                 if(j){
                     c= i-j;
                     c= ((c<<5)&0xE0) + ((c<<16)&0xE000) + ((c<<27)&0xE00000);
                 }
5a68b1ac
                 }else if(mb->inner==CONVTIME){
04bb26e3
                     c= floor(i*255.0/mb->maxiter+dv)*0x010101;
8d51cb4f
                 } else if(mb->inner==MINCOL){
                     int j;
                     double closest=9999;
                     int closest_index=0;
974d25e2
                     for(j=i-1; j>=0; j--)
8d51cb4f
                         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);
04bb26e3
                     c= lrintf((mb->zyklus[closest_index][0]/closest+1)*127+dv) + lrintf((mb->zyklus[closest_index][1]/closest+1)*127+dv)*256;
5a68b1ac
                 }
08dadd33
             }
07f272bd
             c |= 0xFF000000;
658d166d
             color[x + y*linesize]= c;
07f272bd
             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 = c;
             }
658d166d
         }
07f272bd
         fill_from_cache(ctx, NULL, &in_cidx, &next_cidx, ci + scale/2, scale);
658d166d
     }
07f272bd
     FFSWAP(void*, mb->next_cache, mb->point_cache);
     mb->cache_used = next_cidx;
32dd6716
     if(mb->cache_used == mb->cache_allocated)
         av_log(0, AV_LOG_INFO, "Mandelbrot cache is too small!\n");
658d166d
 }
 
 static int request_frame(AVFilterLink *link)
 {
     MBContext *mb = link->src->priv;
     AVFilterBufferRef *picref = avfilter_get_video_buffer(link, AV_PERM_WRITE, mb->w, mb->h);
     picref->video->sample_aspect_ratio = (AVRational) {1, 1};
     picref->pts = mb->pts++;
     picref->pos = -1;
 
     avfilter_start_frame(link, avfilter_ref_buffer(picref, ~0));
4c52adad
     draw_mandelbrot(link->src, (uint32_t*)picref->data[0], picref->linesize[0]/4, picref->pts);
658d166d
     avfilter_draw_slice(link, 0, mb->h, 1);
     avfilter_end_frame(link);
     avfilter_unref_buffer(picref);
 
     return 0;
 }
 
 AVFilter avfilter_vsrc_mandelbrot = {
     .name        = "mandelbrot",
5582e557
     .description = NULL_IF_CONFIG_SMALL("Mandelbrot fractal renderer"),
658d166d
 
     .priv_size = sizeof(MBContext),
     .init      = init,
     .uninit    = uninit,
 
     .query_formats = query_formats,
 
     .inputs    = (const AVFilterPad[]) {{ .name = NULL}},
 
     .outputs   = (const AVFilterPad[]) {{ .name      = "default",
                                     .type            = AVMEDIA_TYPE_VIDEO,
                                     .request_frame   = request_frame,
                                     .config_props    = config_props },
                                   { .name = NULL}},
 };