LCOV - code coverage report
Current view: top level - libavfilter - vsrc_mandelbrot.c (source / functions) Hit Total Coverage
Test: coverage.info Lines: 0 203 0.0 %
Date: 2017-12-11 04:34:20 Functions: 0 8 0.0 %

          Line data    Source code
       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 "formats.h"
      31             : #include "video.h"
      32             : #include "internal.h"
      33             : #include "libavutil/imgutils.h"
      34             : #include "libavutil/opt.h"
      35             : #include "libavutil/parseutils.h"
      36             : #include <float.h>
      37             : #include <math.h>
      38             : 
      39             : #define SQR(a) ((a)*(a))
      40             : 
      41             : enum Outer{
      42             :     ITERATION_COUNT,
      43             :     NORMALIZED_ITERATION_COUNT,
      44             :     WHITE,
      45             :     OUTZ,
      46             : };
      47             : 
      48             : enum Inner{
      49             :     BLACK,
      50             :     PERIOD,
      51             :     CONVTIME,
      52             :     MINCOL,
      53             : };
      54             : 
      55             : typedef struct Point {
      56             :     double p[2];
      57             :     uint32_t val;
      58             : } Point;
      59             : 
      60             : typedef struct MBContext {
      61             :     const AVClass *class;
      62             :     int w, h;
      63             :     AVRational frame_rate;
      64             :     uint64_t pts;
      65             :     int maxiter;
      66             :     double start_x;
      67             :     double start_y;
      68             :     double start_scale;
      69             :     double end_scale;
      70             :     double end_pts;
      71             :     double bailout;
      72             :     int outer;
      73             :     int inner;
      74             :     int cache_allocated;
      75             :     int cache_used;
      76             :     Point *point_cache;
      77             :     Point *next_cache;
      78             :     double (*zyklus)[2];
      79             :     uint32_t dither;
      80             : 
      81             :     double morphxf;
      82             :     double morphyf;
      83             :     double morphamp;
      84             : } MBContext;
      85             : 
      86             : #define OFFSET(x) offsetof(MBContext, x)
      87             : #define FLAGS AV_OPT_FLAG_VIDEO_PARAM|AV_OPT_FLAG_FILTERING_PARAM
      88             : 
      89             : static const AVOption mandelbrot_options[] = {
      90             :     {"size",        "set frame size",                OFFSET(w),       AV_OPT_TYPE_IMAGE_SIZE, {.str="640x480"},  CHAR_MIN, CHAR_MAX, FLAGS },
      91             :     {"s",           "set frame size",                OFFSET(w),       AV_OPT_TYPE_IMAGE_SIZE, {.str="640x480"},  CHAR_MIN, CHAR_MAX, FLAGS },
      92             :     {"rate",        "set frame rate",                OFFSET(frame_rate), AV_OPT_TYPE_VIDEO_RATE, {.str="25"},  CHAR_MIN, CHAR_MAX, FLAGS },
      93             :     {"r",           "set frame rate",                OFFSET(frame_rate), AV_OPT_TYPE_VIDEO_RATE, {.str="25"},  CHAR_MIN, CHAR_MAX, FLAGS },
      94             :     {"maxiter",     "set max iterations number",     OFFSET(maxiter), AV_OPT_TYPE_INT,        {.i64=7189},  1,        INT_MAX, FLAGS },
      95             :     {"start_x",     "set the initial x position",    OFFSET(start_x), AV_OPT_TYPE_DOUBLE,     {.dbl=-0.743643887037158704752191506114774}, -100, 100, FLAGS },
      96             :     {"start_y",     "set the initial y position",    OFFSET(start_y), AV_OPT_TYPE_DOUBLE,     {.dbl=-0.131825904205311970493132056385139}, -100, 100, FLAGS },
      97             :     {"start_scale", "set the initial scale value",   OFFSET(start_scale), AV_OPT_TYPE_DOUBLE, {.dbl=3.0},  0, FLT_MAX, FLAGS },
      98             :     {"end_scale",   "set the terminal scale value",  OFFSET(end_scale), AV_OPT_TYPE_DOUBLE,   {.dbl=0.3},  0, FLT_MAX, FLAGS },
      99             :     {"end_pts",     "set the terminal pts value",    OFFSET(end_pts), AV_OPT_TYPE_DOUBLE,     {.dbl=400},  0, INT64_MAX, FLAGS },
     100             :     {"bailout",     "set the bailout value",         OFFSET(bailout), AV_OPT_TYPE_DOUBLE,     {.dbl=10},   0, FLT_MAX, FLAGS },
     101             :     {"morphxf",     "set morph x frequency",         OFFSET(morphxf), AV_OPT_TYPE_DOUBLE,     {.dbl=0.01},   -FLT_MAX, FLT_MAX, FLAGS },
     102             :     {"morphyf",     "set morph y frequency",         OFFSET(morphyf), AV_OPT_TYPE_DOUBLE,     {.dbl=0.0123}, -FLT_MAX, FLT_MAX, FLAGS },
     103             :     {"morphamp",    "set morph amplitude",           OFFSET(morphamp), AV_OPT_TYPE_DOUBLE,    {.dbl=0},      -FLT_MAX, FLT_MAX, FLAGS },
     104             : 
     105             :     {"outer",       "set outer coloring mode",       OFFSET(outer), AV_OPT_TYPE_INT, {.i64=NORMALIZED_ITERATION_COUNT}, 0, INT_MAX, FLAGS, "outer" },
     106             :     {"iteration_count", "set iteration count mode",  0, AV_OPT_TYPE_CONST, {.i64=ITERATION_COUNT}, INT_MIN, INT_MAX, FLAGS, "outer" },
     107             :     {"normalized_iteration_count", "set normalized iteration count mode",   0, AV_OPT_TYPE_CONST, {.i64=NORMALIZED_ITERATION_COUNT}, INT_MIN, INT_MAX, FLAGS, "outer" },
     108             :     {"white", "set white mode",                      0, AV_OPT_TYPE_CONST, {.i64=WHITE}, INT_MIN, INT_MAX, FLAGS, "outer" },
     109             :     {"outz",        "set outz mode",                 0, AV_OPT_TYPE_CONST, {.i64=OUTZ}, INT_MIN, INT_MAX, FLAGS, "outer" },
     110             : 
     111             :     {"inner",       "set inner coloring mode",       OFFSET(inner), AV_OPT_TYPE_INT, {.i64=MINCOL}, 0, INT_MAX, FLAGS, "inner" },
     112             :     {"black",       "set black mode",                0, AV_OPT_TYPE_CONST, {.i64=BLACK}, INT_MIN, INT_MAX, FLAGS, "inner"},
     113             :     {"period",      "set period mode",               0, AV_OPT_TYPE_CONST, {.i64=PERIOD}, INT_MIN, INT_MAX, FLAGS, "inner"},
     114             :     {"convergence", "show time until convergence",   0, AV_OPT_TYPE_CONST, {.i64=CONVTIME}, INT_MIN, INT_MAX, FLAGS, "inner"},
     115             :     {"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"},
     116             : 
     117             :     {NULL},
     118             : };
     119             : 
     120             : AVFILTER_DEFINE_CLASS(mandelbrot);
     121             : 
     122           0 : static av_cold int init(AVFilterContext *ctx)
     123             : {
     124           0 :     MBContext *s = ctx->priv;
     125             : 
     126           0 :     s->bailout *= s->bailout;
     127             : 
     128           0 :     s->start_scale /=s->h;
     129           0 :     s->end_scale /=s->h;
     130             : 
     131           0 :     s->cache_allocated = s->w * s->h * 3;
     132           0 :     s->cache_used = 0;
     133           0 :     s->point_cache= av_malloc_array(s->cache_allocated, sizeof(*s->point_cache));
     134           0 :     s-> next_cache= av_malloc_array(s->cache_allocated, sizeof(*s-> next_cache));
     135           0 :     s-> zyklus    = av_malloc_array(s->maxiter + 16, sizeof(*s->zyklus));
     136             : 
     137           0 :     return 0;
     138             : }
     139             : 
     140           0 : static av_cold void uninit(AVFilterContext *ctx)
     141             : {
     142           0 :     MBContext *s = ctx->priv;
     143             : 
     144           0 :     av_freep(&s->point_cache);
     145           0 :     av_freep(&s-> next_cache);
     146           0 :     av_freep(&s->zyklus);
     147           0 : }
     148             : 
     149           0 : static int query_formats(AVFilterContext *ctx)
     150             : {
     151             :     static const enum AVPixelFormat pix_fmts[] = {
     152             :         AV_PIX_FMT_0BGR32,
     153             :         AV_PIX_FMT_NONE
     154             :     };
     155             : 
     156           0 :     AVFilterFormats *fmts_list = ff_make_format_list(pix_fmts);
     157           0 :     if (!fmts_list)
     158           0 :         return AVERROR(ENOMEM);
     159           0 :     return ff_set_common_formats(ctx, fmts_list);
     160             : }
     161             : 
     162           0 : static int config_props(AVFilterLink *inlink)
     163             : {
     164           0 :     AVFilterContext *ctx = inlink->src;
     165           0 :     MBContext *s = ctx->priv;
     166             : 
     167           0 :     if (av_image_check_size(s->w, s->h, 0, ctx) < 0)
     168           0 :         return AVERROR(EINVAL);
     169             : 
     170           0 :     inlink->w = s->w;
     171           0 :     inlink->h = s->h;
     172           0 :     inlink->time_base = av_inv_q(s->frame_rate);
     173             : 
     174           0 :     return 0;
     175             : }
     176             : 
     177           0 : static void fill_from_cache(AVFilterContext *ctx, uint32_t *color, int *in_cidx, int *out_cidx, double py, double scale){
     178           0 :     MBContext *s = ctx->priv;
     179           0 :     if(s->morphamp)
     180           0 :         return;
     181           0 :     for(; *in_cidx < s->cache_used; (*in_cidx)++){
     182           0 :         Point *p= &s->point_cache[*in_cidx];
     183             :         int x;
     184           0 :         if(p->p[1] > py)
     185           0 :             break;
     186           0 :         x= lrint((p->p[0] - s->start_x) / scale + s->w/2);
     187           0 :         if(x<0 || x >= s->w)
     188           0 :             continue;
     189           0 :         if(color) color[x] = p->val;
     190           0 :         if(out_cidx && *out_cidx < s->cache_allocated)
     191           0 :             s->next_cache[(*out_cidx)++]= *p;
     192             :     }
     193             : }
     194             : 
     195           0 : static int interpol(MBContext *s, uint32_t *color, int x, int y, int linesize)
     196             : {
     197             :     uint32_t a,b,c,d, i;
     198           0 :     uint32_t ipol=0xFF000000;
     199             :     int dist;
     200             : 
     201           0 :     if(!x || !y || x+1==s->w || y+1==s->h)
     202           0 :         return 0;
     203             : 
     204           0 :     dist= FFMAX(FFABS(x-(s->w>>1))*s->h, FFABS(y-(s->h>>1))*s->w);
     205             : 
     206           0 :     if(dist<(s->w*s->h>>3))
     207           0 :         return 0;
     208             : 
     209           0 :     a=color[(x+1) + (y+0)*linesize];
     210           0 :     b=color[(x-1) + (y+1)*linesize];
     211           0 :     c=color[(x+0) + (y+1)*linesize];
     212           0 :     d=color[(x+1) + (y+1)*linesize];
     213             : 
     214           0 :     if(a&&c){
     215           0 :         b= color[(x-1) + (y+0)*linesize];
     216           0 :         d= color[(x+0) + (y-1)*linesize];
     217           0 :     }else if(b&&d){
     218           0 :         a= color[(x+1) + (y-1)*linesize];
     219           0 :         c= color[(x-1) + (y-1)*linesize];
     220           0 :     }else if(c){
     221           0 :         d= color[(x+0) + (y-1)*linesize];
     222           0 :         a= color[(x-1) + (y+0)*linesize];
     223           0 :         b= color[(x+1) + (y-1)*linesize];
     224           0 :     }else if(d){
     225           0 :         c= color[(x-1) + (y-1)*linesize];
     226           0 :         a= color[(x-1) + (y+0)*linesize];
     227           0 :         b= color[(x+1) + (y-1)*linesize];
     228             :     }else
     229           0 :         return 0;
     230             : 
     231           0 :     for(i=0; i<3; i++){
     232           0 :         int s= 8*i;
     233           0 :         uint8_t ac= a>>s;
     234           0 :         uint8_t bc= b>>s;
     235           0 :         uint8_t cc= c>>s;
     236           0 :         uint8_t dc= d>>s;
     237           0 :         int ipolab= (ac + bc);
     238           0 :         int ipolcd= (cc + dc);
     239           0 :         if(FFABS(ipolab - ipolcd) > 5)
     240           0 :             return 0;
     241           0 :         if(FFABS(ac-bc)+FFABS(cc-dc) > 20)
     242           0 :             return 0;
     243           0 :         ipol |= ((ipolab + ipolcd + 2)/4)<<s;
     244             :     }
     245           0 :     color[x + y*linesize]= ipol;
     246           0 :     return 1;
     247             : }
     248             : 
     249           0 : static void draw_mandelbrot(AVFilterContext *ctx, uint32_t *color, int linesize, int64_t pts)
     250             : {
     251           0 :     MBContext *s = ctx->priv;
     252           0 :     int x,y,i, in_cidx=0, next_cidx=0, tmp_cidx;
     253           0 :     double scale= s->start_scale*pow(s->end_scale/s->start_scale, pts/s->end_pts);
     254           0 :     int use_zyklus=0;
     255           0 :     fill_from_cache(ctx, NULL, &in_cidx, NULL, s->start_y+scale*(-s->h/2-0.5), scale);
     256           0 :     tmp_cidx= in_cidx;
     257           0 :     memset(color, 0, sizeof(*color)*s->w);
     258           0 :     for(y=0; y<s->h; y++){
     259           0 :         int y1= y+1;
     260           0 :         const double ci=s->start_y+scale*(y-s->h/2);
     261           0 :         fill_from_cache(ctx, NULL, &in_cidx, &next_cidx, ci, scale);
     262           0 :         if(y1<s->h){
     263           0 :             memset(color+linesize*y1, 0, sizeof(*color)*s->w);
     264           0 :             fill_from_cache(ctx, color+linesize*y1, &tmp_cidx, NULL, ci + 3*scale/2, scale);
     265             :         }
     266             : 
     267           0 :         for(x=0; x<s->w; x++){
     268           0 :             float av_uninit(epsilon);
     269           0 :             const double cr=s->start_x+scale*(x-s->w/2);
     270           0 :             double zr=cr;
     271           0 :             double zi=ci;
     272           0 :             uint32_t c=0;
     273           0 :             double dv= s->dither / (double)(1LL<<32);
     274           0 :             s->dither= s->dither*1664525+1013904223;
     275             : 
     276           0 :             if(color[x + y*linesize] & 0xFF000000)
     277           0 :                 continue;
     278           0 :             if(!s->morphamp){
     279           0 :                 if(interpol(s, color, x, y, linesize)){
     280           0 :                     if(next_cidx < s->cache_allocated){
     281           0 :                         s->next_cache[next_cidx  ].p[0]= cr;
     282           0 :                         s->next_cache[next_cidx  ].p[1]= ci;
     283           0 :                         s->next_cache[next_cidx++].val = color[x + y*linesize];
     284             :                     }
     285           0 :                     continue;
     286             :                 }
     287             :             }else{
     288           0 :                 zr += cos(pts * s->morphxf) * s->morphamp;
     289           0 :                 zi += sin(pts * s->morphyf) * s->morphamp;
     290             :             }
     291             : 
     292           0 :             use_zyklus= (x==0 || s->inner!=BLACK ||color[x-1 + y*linesize] == 0xFF000000);
     293           0 :             if(use_zyklus)
     294           0 :                 epsilon= scale*(abs(x-s->w/2) + abs(y-s->h/2))/s->w;
     295             : 
     296             : #define Z_Z2_C(outr,outi,inr,ini)\
     297             :             outr= inr*inr - ini*ini + cr;\
     298             :             outi= 2*inr*ini + ci;
     299             : 
     300             : #define Z_Z2_C_ZYKLUS(outr,outi,inr,ini, Z)\
     301             :             Z_Z2_C(outr,outi,inr,ini)\
     302             :             if(use_zyklus){\
     303             :                 if(Z && fabs(s->zyklus[i>>1][0]-outr)+fabs(s->zyklus[i>>1][1]-outi) <= epsilon)\
     304             :                     break;\
     305             :             }\
     306             :             s->zyklus[i][0]= outr;\
     307             :             s->zyklus[i][1]= outi;\
     308             : 
     309             : 
     310             : 
     311           0 :             for(i=0; i<s->maxiter-8; i++){
     312             :                 double t;
     313           0 :                 Z_Z2_C_ZYKLUS(t, zi, zr, zi, 0)
     314           0 :                 i++;
     315           0 :                 Z_Z2_C_ZYKLUS(zr, zi, t, zi, 1)
     316           0 :                 i++;
     317           0 :                 Z_Z2_C_ZYKLUS(t, zi, zr, zi, 0)
     318           0 :                 i++;
     319           0 :                 Z_Z2_C_ZYKLUS(zr, zi, t, zi, 1)
     320           0 :                 i++;
     321           0 :                 Z_Z2_C_ZYKLUS(t, zi, zr, zi, 0)
     322           0 :                 i++;
     323           0 :                 Z_Z2_C_ZYKLUS(zr, zi, t, zi, 1)
     324           0 :                 i++;
     325           0 :                 Z_Z2_C_ZYKLUS(t, zi, zr, zi, 0)
     326           0 :                 i++;
     327           0 :                 Z_Z2_C_ZYKLUS(zr, zi, t, zi, 1)
     328           0 :                 if(zr*zr + zi*zi > s->bailout){
     329           0 :                     i-= FFMIN(7, i);
     330           0 :                     for(; i<s->maxiter; i++){
     331           0 :                         zr= s->zyklus[i][0];
     332           0 :                         zi= s->zyklus[i][1];
     333           0 :                         if(zr*zr + zi*zi > s->bailout){
     334           0 :                             switch(s->outer){
     335           0 :                             case            ITERATION_COUNT:
     336           0 :                                 zr = i;
     337           0 :                                 c = lrintf((sinf(zr)+1)*127) + lrintf((sinf(zr/1.234)+1)*127)*256*256 + lrintf((sinf(zr/100)+1)*127)*256;
     338           0 :                                 break;
     339           0 :                             case NORMALIZED_ITERATION_COUNT:
     340           0 :                                 zr = i + log2(log(s->bailout) / log(zr*zr + zi*zi));
     341           0 :                                 c = lrintf((sinf(zr)+1)*127) + lrintf((sinf(zr/1.234)+1)*127)*256*256 + lrintf((sinf(zr/100)+1)*127)*256;
     342           0 :                                 break;
     343           0 :                             case                      WHITE:
     344           0 :                                 c = 0xFFFFFF;
     345           0 :                                 break;
     346           0 :                             case                      OUTZ:
     347           0 :                                 zr /= s->bailout;
     348           0 :                                 zi /= s->bailout;
     349           0 :                                 c = (((int)(zr*128+128))&0xFF)*256 + (((int)(zi*128+128))&0xFF);
     350             :                             }
     351           0 :                             break;
     352             :                         }
     353             :                     }
     354           0 :                     break;
     355             :                 }
     356             :             }
     357           0 :             if(!c){
     358           0 :                 if(s->inner==PERIOD){
     359             :                     int j;
     360           0 :                     for(j=i-1; j; j--)
     361           0 :                         if(SQR(s->zyklus[j][0]-zr) + SQR(s->zyklus[j][1]-zi) < epsilon*epsilon*10)
     362           0 :                             break;
     363           0 :                     if(j){
     364           0 :                         c= i-j;
     365           0 :                         c= ((c<<5)&0xE0) + ((c<<10)&0xE000) + ((c<<15)&0xE00000);
     366             :                     }
     367           0 :                 }else if(s->inner==CONVTIME){
     368           0 :                     c= floor(i*255.0/s->maxiter+dv)*0x010101;
     369           0 :                 } else if(s->inner==MINCOL){
     370             :                     int j;
     371           0 :                     double closest=9999;
     372           0 :                     int closest_index=0;
     373           0 :                     for(j=i-1; j>=0; j--)
     374           0 :                         if(SQR(s->zyklus[j][0]) + SQR(s->zyklus[j][1]) < closest){
     375           0 :                             closest= SQR(s->zyklus[j][0]) + SQR(s->zyklus[j][1]);
     376           0 :                             closest_index= j;
     377             :                         }
     378           0 :                     closest = sqrt(closest);
     379           0 :                     c= lrintf((s->zyklus[closest_index][0]/closest+1)*127+dv) + lrintf((s->zyklus[closest_index][1]/closest+1)*127+dv)*256;
     380             :                 }
     381             :             }
     382           0 :             c |= 0xFF000000;
     383           0 :             color[x + y*linesize]= c;
     384           0 :             if(next_cidx < s->cache_allocated){
     385           0 :                 s->next_cache[next_cidx  ].p[0]= cr;
     386           0 :                 s->next_cache[next_cidx  ].p[1]= ci;
     387           0 :                 s->next_cache[next_cidx++].val = c;
     388             :             }
     389             :         }
     390           0 :         fill_from_cache(ctx, NULL, &in_cidx, &next_cidx, ci + scale/2, scale);
     391             :     }
     392           0 :     FFSWAP(void*, s->next_cache, s->point_cache);
     393           0 :     s->cache_used = next_cidx;
     394           0 :     if(s->cache_used == s->cache_allocated)
     395           0 :         av_log(ctx, AV_LOG_INFO, "Mandelbrot cache is too small!\n");
     396           0 : }
     397             : 
     398           0 : static int request_frame(AVFilterLink *link)
     399             : {
     400           0 :     MBContext *s = link->src->priv;
     401           0 :     AVFrame *picref = ff_get_video_buffer(link, s->w, s->h);
     402           0 :     if (!picref)
     403           0 :         return AVERROR(ENOMEM);
     404             : 
     405           0 :     picref->sample_aspect_ratio = (AVRational) {1, 1};
     406           0 :     picref->pts = s->pts++;
     407             : 
     408           0 :     draw_mandelbrot(link->src, (uint32_t*)picref->data[0], picref->linesize[0]/4, picref->pts);
     409           0 :     return ff_filter_frame(link, picref);
     410             : }
     411             : 
     412             : static const AVFilterPad mandelbrot_outputs[] = {
     413             :     {
     414             :         .name          = "default",
     415             :         .type          = AVMEDIA_TYPE_VIDEO,
     416             :         .request_frame = request_frame,
     417             :         .config_props  = config_props,
     418             :     },
     419             :     { NULL }
     420             : };
     421             : 
     422             : AVFilter ff_vsrc_mandelbrot = {
     423             :     .name          = "mandelbrot",
     424             :     .description   = NULL_IF_CONFIG_SMALL("Render a Mandelbrot fractal."),
     425             :     .priv_size     = sizeof(MBContext),
     426             :     .priv_class    = &mandelbrot_class,
     427             :     .init          = init,
     428             :     .uninit        = uninit,
     429             :     .query_formats = query_formats,
     430             :     .inputs        = NULL,
     431             :     .outputs       = mandelbrot_outputs,
     432             : };

Generated by: LCOV version 1.13