]> git.sesse.net Git - ffmpeg/blob - libavfilter/vsrc_mandelbrot.c
mandelbrot: more interresting zoom coordinates borrowed from wikipedia.
[ffmpeg] / libavfilter / vsrc_mandelbrot.c
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 fraktal renderer
27  */
28
29 #include "avfilter.h"
30 #include "libavutil/imgutils.h"
31 #include "libavutil/parseutils.h"
32
33 enum Outer{
34     ITERATION_COUNT,
35     NORMALIZED_ITERATION_COUNT,
36 };
37
38 typedef struct {
39     int w, h;
40     AVRational time_base;
41     uint64_t pts;
42     int maxiter;
43     double start_x;
44     double start_y;
45     double start_scale;
46     double end_scale;
47     double end_pts;
48     double bailout;
49     enum Outer outer;
50 } MBContext;
51
52 static av_cold int init(AVFilterContext *ctx, const char *args, void *opaque)
53 {
54     MBContext *mb = ctx->priv;
55     char frame_size  [128] = "320x240";
56     char frame_rate  [128] = "25";
57     AVRational frame_rate_q;
58     int ret;
59
60     mb->maxiter=256;
61     mb->start_x=-0.743643887037158704752191506114774;
62     mb->start_y=-0.131825904205311970493132056385139;
63     mb->start_scale=3.0;
64     mb->end_scale=0.3;
65     mb->end_pts=200;
66     mb->bailout=100;
67     mb->outer= NORMALIZED_ITERATION_COUNT;
68     if (args)
69         sscanf(args, "%127[^:]:%127[^:]:%d,%lf:%lf:%lf", frame_size, frame_rate, &mb->maxiter, &mb->start_x, &mb->start_y, &mb->start_scale);
70
71     if (av_parse_video_size(&mb->w, &mb->h, frame_size) < 0) {
72         av_log(ctx, AV_LOG_ERROR, "Invalid frame size: %s\n", frame_size);
73         return AVERROR(EINVAL);
74     }
75     mb->start_scale /=mb->h;
76     mb->end_scale /=mb->h;
77
78     if (av_parse_video_rate(&frame_rate_q, frame_rate) < 0 ||
79         frame_rate_q.den <= 0 || frame_rate_q.num <= 0) {
80         av_log(ctx, AV_LOG_ERROR, "Invalid frame rate: %s\n", frame_rate);
81         return AVERROR(EINVAL);
82     }
83     mb->time_base.num = frame_rate_q.den;
84     mb->time_base.den = frame_rate_q.num;
85
86     return 0;
87 }
88
89 static av_cold void uninit(AVFilterContext *ctx)
90 {
91     MBContext *mb = ctx->priv;
92     int i;
93
94 }
95
96 static int query_formats(AVFilterContext *ctx)
97 {
98     static const enum PixelFormat pix_fmts[] = {
99         PIX_FMT_BGR32,
100         PIX_FMT_NONE
101     };
102
103     avfilter_set_common_pixel_formats(ctx, avfilter_make_format_list(pix_fmts));
104     return 0;
105 }
106
107 static int config_props(AVFilterLink *inlink)
108 {
109     AVFilterContext *ctx = inlink->src;
110     MBContext *mb = ctx->priv;
111
112     if (av_image_check_size(mb->w, mb->h, 0, ctx) < 0)
113         return AVERROR(EINVAL);
114
115     inlink->w = mb->w;
116     inlink->h = mb->h;
117     inlink->time_base = mb->time_base;
118
119     return 0;
120 }
121
122 static void draw_mandelbrot(AVFilterContext *ctx, uint32_t *color, int linesize, int64_t pts)
123 {
124     MBContext *mb = ctx->priv;
125     int x,y,i;
126
127     double scale= mb->start_scale*pow(mb->end_scale/mb->start_scale, pts/mb->end_pts);
128
129     for(y=0; y<mb->h; y++){
130         for(x=0; x<mb->w; x++){
131             const double cr=mb->start_x+scale*(x-mb->w/2);
132             const double ci=mb->start_y+scale*(y-mb->h/2);
133             double zr=cr;
134             double zi=ci;
135             uint32_t c=0;
136
137             for(i=0; i<mb->maxiter; i++){
138                 double t;
139                 if(zr*zr + zi*zi > mb->bailout){
140                     switch(mb->outer){
141                     case            ITERATION_COUNT: zr= i; break;
142                     case NORMALIZED_ITERATION_COUNT: zr= i + (log(log(mb->bailout)) - log(log(sqrt(zr*zr + zi*zi))))/log(2); break;
143                     }
144                     c= lrintf((sin(zr)+1)*127) + lrintf((sin(zr/1.234)+1)*127)*256*256 + lrintf((sin(zr/100)+1)*127)*256;
145                     break;
146                 }
147                 t= zr*zr - zi*zi;
148                 zi= 2*zr*zi + ci;
149                 zr=       t + cr;
150             }
151             color[x + y*linesize]= c;
152         }
153     }
154 }
155
156 static int request_frame(AVFilterLink *link)
157 {
158     MBContext *mb = link->src->priv;
159     AVFilterBufferRef *picref = avfilter_get_video_buffer(link, AV_PERM_WRITE, mb->w, mb->h);
160     picref->video->sample_aspect_ratio = (AVRational) {1, 1};
161     picref->pts = mb->pts++;
162     picref->pos = -1;
163
164     avfilter_start_frame(link, avfilter_ref_buffer(picref, ~0));
165     draw_mandelbrot(link->src, picref->data[0], picref->linesize[0]/4, picref->pts);
166     avfilter_draw_slice(link, 0, mb->h, 1);
167     avfilter_end_frame(link);
168     avfilter_unref_buffer(picref);
169
170     return 0;
171 }
172
173 AVFilter avfilter_vsrc_mandelbrot = {
174     .name        = "mandelbrot",
175     .description = NULL_IF_CONFIG_SMALL("Mandelbrot renderer"),
176
177     .priv_size = sizeof(MBContext),
178     .init      = init,
179     .uninit    = uninit,
180
181     .query_formats = query_formats,
182
183     .inputs    = (const AVFilterPad[]) {{ .name = NULL}},
184
185     .outputs   = (const AVFilterPad[]) {{ .name      = "default",
186                                     .type            = AVMEDIA_TYPE_VIDEO,
187                                     .request_frame   = request_frame,
188                                     .config_props    = config_props },
189                                   { .name = NULL}},
190 };