]> git.sesse.net Git - ffmpeg/blob - libavfilter/vsrc_mandelbrot.c
mandelbrot: add missing options to the parsing code
[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 Point {
39     double p[2];
40     uint32_t val;
41 } Point;
42
43 typedef struct {
44     int w, h;
45     AVRational time_base;
46     uint64_t pts;
47     int maxiter;
48     double start_x;
49     double start_y;
50     double start_scale;
51     double end_scale;
52     double end_pts;
53     double bailout;
54     enum Outer outer;
55     int cache_allocated;
56     int cache_used;
57     Point *point_cache;
58     Point *next_cache;
59     double (*zyklus)[2];
60 } MBContext;
61
62 static av_cold int init(AVFilterContext *ctx, const char *args, void *opaque)
63 {
64     MBContext *mb = ctx->priv;
65     char frame_size  [128] = "640x480";
66     char frame_rate  [128] = "25";
67     AVRational frame_rate_q;
68     int ret;
69
70     mb->maxiter=4096;
71     mb->start_x=-0.743643887037158704752191506114774;
72     mb->start_y=-0.131825904205311970493132056385139;
73     mb->start_scale=3.0;
74     mb->end_scale=0.3;
75     mb->end_pts=800;
76     mb->bailout=10;
77     mb->outer= NORMALIZED_ITERATION_COUNT;
78     if (args)
79         sscanf(args, "%127[^:]:%127[^:]:%d:%lf:%lf:%lf:%lf:%lf:%lf:%d", frame_size, frame_rate,
80                &mb->maxiter, &mb->start_x, &mb->start_y, &mb->start_scale, &mb->end_scale,
81                &mb->end_pts, &mb->bailout, &mb->outer);
82     mb->bailout *= mb->bailout;
83
84     if (av_parse_video_size(&mb->w, &mb->h, frame_size) < 0) {
85         av_log(ctx, AV_LOG_ERROR, "Invalid frame size: %s\n", frame_size);
86         return AVERROR(EINVAL);
87     }
88     mb->start_scale /=mb->h;
89     mb->end_scale /=mb->h;
90
91     if (av_parse_video_rate(&frame_rate_q, frame_rate) < 0 ||
92         frame_rate_q.den <= 0 || frame_rate_q.num <= 0) {
93         av_log(ctx, AV_LOG_ERROR, "Invalid frame rate: %s\n", frame_rate);
94         return AVERROR(EINVAL);
95     }
96     mb->time_base.num = frame_rate_q.den;
97     mb->time_base.den = frame_rate_q.num;
98
99     mb->cache_allocated = mb->w * mb->h * 3;
100     mb->cache_used = 0;
101     mb->point_cache= av_malloc(sizeof(*mb->point_cache)*mb->cache_allocated);
102     mb-> next_cache= av_malloc(sizeof(*mb-> next_cache)*mb->cache_allocated);
103     mb-> zyklus    = av_malloc(sizeof(*mb->zyklus) * mb->maxiter);
104
105     return 0;
106 }
107
108 static av_cold void uninit(AVFilterContext *ctx)
109 {
110     MBContext *mb = ctx->priv;
111     int i;
112
113     av_freep(&mb->point_cache);
114     av_freep(&mb-> next_cache);
115     av_freep(&mb->zyklus);
116 }
117
118 static int query_formats(AVFilterContext *ctx)
119 {
120     static const enum PixelFormat pix_fmts[] = {
121         PIX_FMT_BGR32,
122         PIX_FMT_NONE
123     };
124
125     avfilter_set_common_pixel_formats(ctx, avfilter_make_format_list(pix_fmts));
126     return 0;
127 }
128
129 static int config_props(AVFilterLink *inlink)
130 {
131     AVFilterContext *ctx = inlink->src;
132     MBContext *mb = ctx->priv;
133
134     if (av_image_check_size(mb->w, mb->h, 0, ctx) < 0)
135         return AVERROR(EINVAL);
136
137     inlink->w = mb->w;
138     inlink->h = mb->h;
139     inlink->time_base = mb->time_base;
140
141     return 0;
142 }
143
144 static void fill_from_cache(AVFilterContext *ctx, uint32_t *color, int *in_cidx, int *out_cidx, double py, double scale){
145     MBContext *mb = ctx->priv;
146     for(; *in_cidx < mb->cache_used; (*in_cidx)++){
147         Point *p= &mb->point_cache[*in_cidx];
148         int x;
149         if(*in_cidx >= mb->cache_used || p->p[1] > py)
150             break;
151         x= round((p->p[0] - mb->start_x) / scale + mb->w/2);
152         if(x<0 || x >= mb->w)
153             continue;
154         if(color) color[x] = p->val;
155         if(out_cidx && *out_cidx < mb->cache_allocated)
156             mb->next_cache[(*out_cidx)++]= *p;
157     }
158 }
159
160 static void draw_mandelbrot(AVFilterContext *ctx, uint32_t *color, int linesize, int64_t pts)
161 {
162     MBContext *mb = ctx->priv;
163     int x,y,i, in_cidx=0, next_cidx=0, tmp_cidx;
164     double scale= mb->start_scale*pow(mb->end_scale/mb->start_scale, pts/mb->end_pts);
165     int use_zyklus=0;
166     fill_from_cache(ctx, NULL, &in_cidx, NULL, mb->start_y+scale*(-mb->h/2-0.5), scale);
167     for(y=0; y<mb->h; y++){
168         const double ci=mb->start_y+scale*(y-mb->h/2);
169         memset(color+linesize*y, 0, sizeof(*color)*mb->w);
170         fill_from_cache(ctx, color+linesize*y, &in_cidx, &next_cidx, ci, scale);
171         tmp_cidx= in_cidx;
172         fill_from_cache(ctx, color+linesize*y, &tmp_cidx, NULL, ci + scale/2, scale);
173
174         for(x=0; x<mb->w; x++){
175             const double cr=mb->start_x+scale*(x-mb->w/2);
176             double zr=cr;
177             double zi=ci;
178             uint32_t c=0;
179
180             if(color[x + y*linesize] & 0xFF000000)
181                 continue;
182
183             for(i=0; i<mb->maxiter; i++){
184                 double t;
185                 if(zr*zr + zi*zi > mb->bailout){
186                     switch(mb->outer){
187                     case            ITERATION_COUNT: zr= i; break;
188                     case NORMALIZED_ITERATION_COUNT: zr= i + (log(log(mb->bailout)) - log(log(sqrt(zr*zr + zi*zi))))/log(2); break;
189                     }
190                     c= lrintf((sin(zr)+1)*127) + lrintf((sin(zr/1.234)+1)*127)*256*256 + lrintf((sin(zr/100)+1)*127)*256;
191                     break;
192                 }
193                 t= zr*zr - zi*zi;
194                 zi= 2*zr*zi + ci;
195                 zr=       t + cr;
196                 if(use_zyklus){
197                     if(i && mb->zyklus[i>>1][0]==zr && mb->zyklus[i>>1][1]==zi)
198                         break;
199                     mb->zyklus[i][0]= zr;
200                     mb->zyklus[i][1]= zi;
201                 }
202             }
203             use_zyklus = !c;
204             c |= 0xFF000000;
205             color[x + y*linesize]= c;
206             if(next_cidx < mb->cache_allocated){
207                 mb->next_cache[next_cidx  ].p[0]= cr;
208                 mb->next_cache[next_cidx  ].p[1]= ci;
209                 mb->next_cache[next_cidx++].val = c;
210             }
211         }
212         fill_from_cache(ctx, NULL, &in_cidx, &next_cidx, ci + scale/2, scale);
213     }
214     FFSWAP(void*, mb->next_cache, mb->point_cache);
215     mb->cache_used = next_cidx;
216     if(mb->cache_used == mb->cache_allocated)
217         av_log(0, AV_LOG_INFO, "Mandelbrot cache is too small!\n");
218 }
219
220 static int request_frame(AVFilterLink *link)
221 {
222     MBContext *mb = link->src->priv;
223     AVFilterBufferRef *picref = avfilter_get_video_buffer(link, AV_PERM_WRITE, mb->w, mb->h);
224     picref->video->sample_aspect_ratio = (AVRational) {1, 1};
225     picref->pts = mb->pts++;
226     picref->pos = -1;
227
228     avfilter_start_frame(link, avfilter_ref_buffer(picref, ~0));
229     draw_mandelbrot(link->src, picref->data[0], picref->linesize[0]/4, picref->pts);
230     avfilter_draw_slice(link, 0, mb->h, 1);
231     avfilter_end_frame(link);
232     avfilter_unref_buffer(picref);
233
234     return 0;
235 }
236
237 AVFilter avfilter_vsrc_mandelbrot = {
238     .name        = "mandelbrot",
239     .description = NULL_IF_CONFIG_SMALL("Mandelbrot renderer"),
240
241     .priv_size = sizeof(MBContext),
242     .init      = init,
243     .uninit    = uninit,
244
245     .query_formats = query_formats,
246
247     .inputs    = (const AVFilterPad[]) {{ .name = NULL}},
248
249     .outputs   = (const AVFilterPad[]) {{ .name      = "default",
250                                     .type            = AVMEDIA_TYPE_VIDEO,
251                                     .request_frame   = request_frame,
252                                     .config_props    = config_props },
253                                   { .name = NULL}},
254 };