2 * Copyright (c) 2011 Michael Niedermayer
4 * This file is part of FFmpeg.
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.
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.
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
20 * The vsrc_color filter from Stefano Sabatini was used as template to create
26 * Mandelbrot fraktal renderer
30 #include "libavutil/imgutils.h"
31 #include "libavutil/parseutils.h"
35 NORMALIZED_ITERATION_COUNT,
52 static av_cold int init(AVFilterContext *ctx, const char *args, void *opaque)
54 MBContext *mb = ctx->priv;
55 char frame_size [128] = "320x240";
56 char frame_rate [128] = "25";
57 AVRational frame_rate_q;
61 mb->start_x=-0.743643887037158704752191506114774;
62 mb->start_y=-0.131825904205311970493132056385139;
67 mb->outer= NORMALIZED_ITERATION_COUNT;
69 sscanf(args, "%127[^:]:%127[^:]:%d,%lf:%lf:%lf", frame_size, frame_rate, &mb->maxiter, &mb->start_x, &mb->start_y, &mb->start_scale);
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);
75 mb->start_scale /=mb->h;
76 mb->end_scale /=mb->h;
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);
83 mb->time_base.num = frame_rate_q.den;
84 mb->time_base.den = frame_rate_q.num;
89 static av_cold void uninit(AVFilterContext *ctx)
91 MBContext *mb = ctx->priv;
96 static int query_formats(AVFilterContext *ctx)
98 static const enum PixelFormat pix_fmts[] = {
103 avfilter_set_common_pixel_formats(ctx, avfilter_make_format_list(pix_fmts));
107 static int config_props(AVFilterLink *inlink)
109 AVFilterContext *ctx = inlink->src;
110 MBContext *mb = ctx->priv;
112 if (av_image_check_size(mb->w, mb->h, 0, ctx) < 0)
113 return AVERROR(EINVAL);
117 inlink->time_base = mb->time_base;
122 static void draw_mandelbrot(AVFilterContext *ctx, uint32_t *color, int linesize, int64_t pts)
124 MBContext *mb = ctx->priv;
127 double scale= mb->start_scale*pow(mb->end_scale/mb->start_scale, pts/mb->end_pts);
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);
137 for(i=0; i<mb->maxiter; i++){
139 if(zr*zr + zi*zi > mb->bailout){
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;
144 c= lrintf((sin(zr)+1)*127) + lrintf((sin(zr/1.234)+1)*127)*256*256 + lrintf((sin(zr/100)+1)*127)*256;
151 color[x + y*linesize]= c;
156 static int request_frame(AVFilterLink *link)
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++;
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);
173 AVFilter avfilter_vsrc_mandelbrot = {
174 .name = "mandelbrot",
175 .description = NULL_IF_CONFIG_SMALL("Mandelbrot renderer"),
177 .priv_size = sizeof(MBContext),
181 .query_formats = query_formats,
183 .inputs = (const AVFilterPad[]) {{ .name = NULL}},
185 .outputs = (const AVFilterPad[]) {{ .name = "default",
186 .type = AVMEDIA_TYPE_VIDEO,
187 .request_frame = request_frame,
188 .config_props = config_props },