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