Browse code

avfilter: add mandelbrot fraktal source

Signed-off-by: Michael Niedermayer <michaelni@gmx.at>

Michael Niedermayer authored on 2011/11/12 06:47:06
Showing 3 changed files
... ...
@@ -84,6 +84,7 @@ OBJS-$(CONFIG_YADIF_FILTER)                  += vf_yadif.o
84 84
 OBJS-$(CONFIG_BUFFER_FILTER)                 += vsrc_buffer.o
85 85
 OBJS-$(CONFIG_COLOR_FILTER)                  += vsrc_color.o
86 86
 OBJS-$(CONFIG_FREI0R_SRC_FILTER)             += vf_frei0r.o
87
+OBJS-$(CONFIG_MANDELBROT_FILTER)             += vsrc_mandelbrot.o
87 88
 OBJS-$(CONFIG_MOVIE_FILTER)                  += src_movie.o
88 89
 OBJS-$(CONFIG_MPTESTSRC_FILTER)              += vsrc_mptestsrc.o
89 90
 OBJS-$(CONFIG_NULLSRC_FILTER)                += vsrc_testsrc.o
... ...
@@ -95,6 +95,7 @@ void avfilter_register_all(void)
95 95
     REGISTER_FILTER (BUFFER,      buffer,      vsrc);
96 96
     REGISTER_FILTER (COLOR,       color,       vsrc);
97 97
     REGISTER_FILTER (FREI0R,      frei0r_src,  vsrc);
98
+    REGISTER_FILTER (MANDELBROT,  mandelbrot,  vsrc);
98 99
     REGISTER_FILTER (MOVIE,       movie,       vsrc);
99 100
     REGISTER_FILTER (MPTESTSRC,   mptestsrc,   vsrc);
100 101
     REGISTER_FILTER (NULLSRC,     nullsrc,     vsrc);
101 102
new file mode 100644
... ...
@@ -0,0 +1,172 @@
0
+/*
1
+ * Copyright (c) 2011 Michael Niedermayer
2
+ *
3
+ * This file is part of FFmpeg.
4
+ *
5
+ * FFmpeg is free software; you can redistribute it and/or
6
+ * modify it under the terms of the GNU Lesser General Public
7
+ * License as published by the Free Software Foundation; either
8
+ * version 2.1 of the License, or (at your option) any later version.
9
+ *
10
+ * FFmpeg is distributed in the hope that it will be useful,
11
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
12
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
13
+ * Lesser General Public License for more details.
14
+ *
15
+ * You should have received a copy of the GNU Lesser General Public
16
+ * License along with FFmpeg; if not, write to the Free Software
17
+ * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
18
+ *
19
+ * The vsrc_color filter from Stefano Sabatini was used as template to create
20
+ * this
21
+ */
22
+
23
+/**
24
+ * @file
25
+ * Mandelbrot fraktal renderer
26
+ */
27
+
28
+#include "avfilter.h"
29
+#include "libavutil/imgutils.h"
30
+#include "libavutil/parseutils.h"
31
+
32
+typedef struct {
33
+    int w, h;
34
+    AVRational time_base;
35
+    uint64_t pts;
36
+    int maxiter;
37
+    double start_x;
38
+    double start_y;
39
+    double start_scale;
40
+} MBContext;
41
+
42
+static av_cold int init(AVFilterContext *ctx, const char *args, void *opaque)
43
+{
44
+    MBContext *mb = ctx->priv;
45
+    char frame_size  [128] = "320x240";
46
+    char frame_rate  [128] = "25";
47
+    AVRational frame_rate_q;
48
+    int ret;
49
+
50
+    mb->maxiter=1024;
51
+    mb->start_x=-2.0;
52
+    mb->start_y=-1.5;
53
+    mb->start_scale=3.0;
54
+    if (args)
55
+        sscanf(args, "%127[^:]:%127[^:]:%d,%lf:%lf:%lf", frame_size, frame_rate, &mb->maxiter, &mb->start_x, &mb->start_y, &mb->start_scale);
56
+
57
+    if (av_parse_video_size(&mb->w, &mb->h, frame_size) < 0) {
58
+        av_log(ctx, AV_LOG_ERROR, "Invalid frame size: %s\n", frame_size);
59
+        return AVERROR(EINVAL);
60
+    }
61
+    mb->start_scale /=mb->h;
62
+
63
+    if (av_parse_video_rate(&frame_rate_q, frame_rate) < 0 ||
64
+        frame_rate_q.den <= 0 || frame_rate_q.num <= 0) {
65
+        av_log(ctx, AV_LOG_ERROR, "Invalid frame rate: %s\n", frame_rate);
66
+        return AVERROR(EINVAL);
67
+    }
68
+    mb->time_base.num = frame_rate_q.den;
69
+    mb->time_base.den = frame_rate_q.num;
70
+
71
+    return 0;
72
+}
73
+
74
+static av_cold void uninit(AVFilterContext *ctx)
75
+{
76
+    MBContext *mb = ctx->priv;
77
+    int i;
78
+
79
+}
80
+
81
+static int query_formats(AVFilterContext *ctx)
82
+{
83
+    static const enum PixelFormat pix_fmts[] = {
84
+        PIX_FMT_BGR32,
85
+        PIX_FMT_NONE
86
+    };
87
+
88
+    avfilter_set_common_pixel_formats(ctx, avfilter_make_format_list(pix_fmts));
89
+    return 0;
90
+}
91
+
92
+static int config_props(AVFilterLink *inlink)
93
+{
94
+    AVFilterContext *ctx = inlink->src;
95
+    MBContext *mb = ctx->priv;
96
+
97
+    if (av_image_check_size(mb->w, mb->h, 0, ctx) < 0)
98
+        return AVERROR(EINVAL);
99
+
100
+    inlink->w = mb->w;
101
+    inlink->h = mb->h;
102
+    inlink->time_base = mb->time_base;
103
+
104
+    return 0;
105
+}
106
+
107
+static void draw_mandelbrot(AVFilterContext *ctx, uint32_t *color, int linesize, int64_t pts)
108
+{
109
+    MBContext *mb = ctx->priv;
110
+    int x,y,i;
111
+
112
+    for(y=0; y<mb->h; y++){
113
+        for(x=0; x<mb->w; x++){
114
+            double cr=mb->start_x+mb->start_scale*x;
115
+            double ci=mb->start_y+mb->start_scale*y;
116
+            double zr=cr;
117
+            double zi=ci;
118
+            const double B=100;
119
+            uint32_t c=0;
120
+
121
+            for(i=0; i<256; i++){
122
+                double t;
123
+                if(zr*zr + zi*zi > B){
124
+                    zr= i + (log(log(B)) - log(log(sqrt(zr*zr + zi*zi))))/log(2);
125
+                    c= lrintf((sin(zr)+1)*127) + lrintf((sin(zr/1.234)+1)*127)*256*256 + lrintf((sin(zr/100)+1)*127)*256;
126
+                    break;
127
+                }
128
+                t= zr*zr - zi*zi;
129
+                zi= 2*zr*zi + ci;
130
+                zr=       t + cr;
131
+            }
132
+            color[x + y*linesize]= c;
133
+        }
134
+    }
135
+}
136
+
137
+static int request_frame(AVFilterLink *link)
138
+{
139
+    MBContext *mb = link->src->priv;
140
+    AVFilterBufferRef *picref = avfilter_get_video_buffer(link, AV_PERM_WRITE, mb->w, mb->h);
141
+    picref->video->sample_aspect_ratio = (AVRational) {1, 1};
142
+    picref->pts = mb->pts++;
143
+    picref->pos = -1;
144
+
145
+    avfilter_start_frame(link, avfilter_ref_buffer(picref, ~0));
146
+    draw_mandelbrot(link->src, picref->data[0], picref->linesize[0]/4, picref->pts);
147
+    avfilter_draw_slice(link, 0, mb->h, 1);
148
+    avfilter_end_frame(link);
149
+    avfilter_unref_buffer(picref);
150
+
151
+    return 0;
152
+}
153
+
154
+AVFilter avfilter_vsrc_mandelbrot = {
155
+    .name        = "mandelbrot",
156
+    .description = NULL_IF_CONFIG_SMALL("Mandelbrot renderer"),
157
+
158
+    .priv_size = sizeof(MBContext),
159
+    .init      = init,
160
+    .uninit    = uninit,
161
+
162
+    .query_formats = query_formats,
163
+
164
+    .inputs    = (const AVFilterPad[]) {{ .name = NULL}},
165
+
166
+    .outputs   = (const AVFilterPad[]) {{ .name      = "default",
167
+                                    .type            = AVMEDIA_TYPE_VIDEO,
168
+                                    .request_frame   = request_frame,
169
+                                    .config_props    = config_props },
170
+                                  { .name = NULL}},
171
+};