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,
50 static av_cold int init(AVFilterContext *ctx, const char *args, void *opaque)
52 MBContext *mb = ctx->priv;
53 char frame_size [128] = "320x240";
54 char frame_rate [128] = "25";
55 AVRational frame_rate_q;
63 mb->outer= NORMALIZED_ITERATION_COUNT;
65 sscanf(args, "%127[^:]:%127[^:]:%d,%lf:%lf:%lf", frame_size, frame_rate, &mb->maxiter, &mb->start_x, &mb->start_y, &mb->start_scale);
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);
71 mb->start_scale /=mb->h;
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);
78 mb->time_base.num = frame_rate_q.den;
79 mb->time_base.den = frame_rate_q.num;
84 static av_cold void uninit(AVFilterContext *ctx)
86 MBContext *mb = ctx->priv;
91 static int query_formats(AVFilterContext *ctx)
93 static const enum PixelFormat pix_fmts[] = {
98 avfilter_set_common_pixel_formats(ctx, avfilter_make_format_list(pix_fmts));
102 static int config_props(AVFilterLink *inlink)
104 AVFilterContext *ctx = inlink->src;
105 MBContext *mb = ctx->priv;
107 if (av_image_check_size(mb->w, mb->h, 0, ctx) < 0)
108 return AVERROR(EINVAL);
112 inlink->time_base = mb->time_base;
117 static void draw_mandelbrot(AVFilterContext *ctx, uint32_t *color, int linesize, int64_t pts)
119 MBContext *mb = ctx->priv;
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;
130 for(i=0; i<mb->maxiter; i++){
132 if(zr*zr + zi*zi > mb->bailout){
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;
137 c= lrintf((sin(zr)+1)*127) + lrintf((sin(zr/1.234)+1)*127)*256*256 + lrintf((sin(zr/100)+1)*127)*256;
144 color[x + y*linesize]= c;
149 static int request_frame(AVFilterLink *link)
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++;
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);
166 AVFilter avfilter_vsrc_mandelbrot = {
167 .name = "mandelbrot",
168 .description = NULL_IF_CONFIG_SMALL("Mandelbrot renderer"),
170 .priv_size = sizeof(MBContext),
174 .query_formats = query_formats,
176 .inputs = (const AVFilterPad[]) {{ .name = NULL}},
178 .outputs = (const AVFilterPad[]) {{ .name = "default",
179 .type = AVMEDIA_TYPE_VIDEO,
180 .request_frame = request_frame,
181 .config_props = config_props },