From 17dcc0658d75770192c1fd660d24dff8fc48f84d Mon Sep 17 00:00:00 2001
From: Paul B Mahol <onemda@gmail.com>
Date: Mon, 12 Aug 2019 09:20:08 +0200
Subject: [PATCH] avfilter: add sierpinski video source

---
 libavfilter/Makefile          |   1 +
 libavfilter/allfilters.c      |   1 +
 libavfilter/vsrc_sierpinski.c | 172 ++++++++++++++++++++++++++++++++++
 3 files changed, 174 insertions(+)
 create mode 100644 libavfilter/vsrc_sierpinski.c

diff --git a/libavfilter/Makefile b/libavfilter/Makefile
index b5d48ff9a3..5ba443e43f 100644
--- a/libavfilter/Makefile
+++ b/libavfilter/Makefile
@@ -451,6 +451,7 @@ OBJS-$(CONFIG_OPENCLSRC_FILTER)              += vf_program_opencl.o opencl.o
 OBJS-$(CONFIG_PAL75BARS_FILTER)              += vsrc_testsrc.o
 OBJS-$(CONFIG_PAL100BARS_FILTER)             += vsrc_testsrc.o
 OBJS-$(CONFIG_RGBTESTSRC_FILTER)             += vsrc_testsrc.o
+OBJS-$(CONFIG_SIERPINSKI_FILTER)             += vsrc_sierpinski.o
 OBJS-$(CONFIG_SMPTEBARS_FILTER)              += vsrc_testsrc.o
 OBJS-$(CONFIG_SMPTEHDBARS_FILTER)            += vsrc_testsrc.o
 OBJS-$(CONFIG_TESTSRC_FILTER)                += vsrc_testsrc.o
diff --git a/libavfilter/allfilters.c b/libavfilter/allfilters.c
index 5d4cec2119..031c8a71f2 100644
--- a/libavfilter/allfilters.c
+++ b/libavfilter/allfilters.c
@@ -430,6 +430,7 @@ extern AVFilter ff_vsrc_openclsrc;
 extern AVFilter ff_vsrc_pal75bars;
 extern AVFilter ff_vsrc_pal100bars;
 extern AVFilter ff_vsrc_rgbtestsrc;
+extern AVFilter ff_vsrc_sierpinski;
 extern AVFilter ff_vsrc_smptebars;
 extern AVFilter ff_vsrc_smptehdbars;
 extern AVFilter ff_vsrc_testsrc;
diff --git a/libavfilter/vsrc_sierpinski.c b/libavfilter/vsrc_sierpinski.c
new file mode 100644
index 0000000000..20c64df7d0
--- /dev/null
+++ b/libavfilter/vsrc_sierpinski.c
@@ -0,0 +1,172 @@
+/*
+ * Copyright (c) 2019 Paul B Mahol
+ *
+ * This file is part of FFmpeg.
+ *
+ * FFmpeg is free software; you can redistribute it and/or
+ * modify it under the terms of the GNU Lesser General Public
+ * License as published by the Free Software Foundation; either
+ * version 2.1 of the License, or (at your option) any later version.
+ *
+ * FFmpeg is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
+ * Lesser General Public License for more details.
+ *
+ * You should have received a copy of the GNU Lesser General Public
+ * License along with FFmpeg; if not, write to the Free Software
+ * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
+ */
+
+/**
+ * @file
+ * Sierpinski carpet fractal renderer
+ */
+
+#include "avfilter.h"
+#include "formats.h"
+#include "video.h"
+#include "internal.h"
+#include "libavutil/imgutils.h"
+#include "libavutil/intreadwrite.h"
+#include "libavutil/opt.h"
+#include "libavutil/parseutils.h"
+#include <float.h>
+#include <math.h>
+
+typedef struct SierpinskiContext {
+    const AVClass *class;
+    int w, h;
+    AVRational frame_rate;
+    uint64_t pts;
+
+    float scale;
+} SierpinskiContext;
+
+#define OFFSET(x) offsetof(SierpinskiContext, x)
+#define FLAGS AV_OPT_FLAG_VIDEO_PARAM|AV_OPT_FLAG_FILTERING_PARAM
+
+static const AVOption sierpinski_options[] = {
+    {"size", "set frame size", OFFSET(w),          AV_OPT_TYPE_IMAGE_SIZE, {.str="640x480"}, 0, 0, FLAGS },
+    {"s",    "set frame size", OFFSET(w),          AV_OPT_TYPE_IMAGE_SIZE, {.str="640x480"}, 0, 0, FLAGS },
+    {"rate", "set frame rate", OFFSET(frame_rate), AV_OPT_TYPE_VIDEO_RATE, {.str="25"},      0, 0, FLAGS },
+    {"r",    "set frame rate", OFFSET(frame_rate), AV_OPT_TYPE_VIDEO_RATE, {.str="25"},      0, 0, FLAGS },
+    {NULL},
+};
+
+AVFILTER_DEFINE_CLASS(sierpinski);
+
+static int query_formats(AVFilterContext *ctx)
+{
+    static const enum AVPixelFormat pix_fmts[] = {
+        AV_PIX_FMT_0BGR32,
+        AV_PIX_FMT_NONE
+    };
+
+    AVFilterFormats *fmts_list = ff_make_format_list(pix_fmts);
+    if (!fmts_list)
+        return AVERROR(ENOMEM);
+    return ff_set_common_formats(ctx, fmts_list);
+}
+
+static int config_output(AVFilterLink *inlink)
+{
+    AVFilterContext *ctx = inlink->src;
+    SierpinskiContext *s = ctx->priv;
+
+    if (av_image_check_size(s->w, s->h, 0, ctx) < 0)
+        return AVERROR(EINVAL);
+
+    inlink->w = s->w;
+    inlink->h = s->h;
+    inlink->time_base = av_inv_q(s->frame_rate);
+    inlink->sample_aspect_ratio = (AVRational) {1, 1};
+
+    return 0;
+}
+
+static int fill_sierpinski(SierpinskiContext *s, int x, int y)
+{
+    float pos_x = x - s->w / 2.f;
+    float pos_y = y - s->h / 2.f;
+    float offset_x = -0.5f;
+    float offset_y = -0.5f;
+    float scale = s->scale;
+    float size = s->h * scale;
+
+    pos_x += offset_x * s->h * (scale * .5f - .5f);
+    pos_y += offset_y * s->h * (scale * .5f - .5f);
+
+    while (size > 1.f) {
+        int ip_x, ip_y;
+
+        size /= 3.f;
+        ip_x = roundf(pos_x / size);
+        ip_y = roundf(pos_y / size);
+
+        if (ip_x == 0 && ip_y == 0) {
+            return size;
+        } else {
+            pos_x -= ip_x * size;
+            pos_y -= ip_y * size;
+        }
+    }
+
+    return 0;
+}
+
+static void draw_sierpinski(AVFilterContext *ctx, AVFrame *frame)
+{
+    SierpinskiContext *s = ctx->priv;
+    uint8_t *dst = frame->data[0];
+    float t = frame->pts / (av_q2d(s->frame_rate) * 5);
+
+    s->scale = powf(3.f, fmodf(t, 2.f) + 1.f);
+
+    for (int y = 0; y < frame->height; y++) {
+        for (int x = 0; x < frame->width; x++) {
+            if (fill_sierpinski(s, x, y)) {
+                AV_WL32(&dst[x*4], 0xffffffff);
+            } else {
+                AV_WL32(&dst[x*4], 0x00000000);
+            }
+        }
+        dst += frame->linesize[0];
+    }
+}
+
+static int sierpinski_request_frame(AVFilterLink *link)
+{
+    SierpinskiContext *s = link->src->priv;
+    AVFrame *frame = ff_get_video_buffer(link, s->w, s->h);
+
+    if (!frame)
+        return AVERROR(ENOMEM);
+
+    frame->sample_aspect_ratio = (AVRational) {1, 1};
+    frame->pts = s->pts++;
+
+    draw_sierpinski(link->src, frame);
+
+    return ff_filter_frame(link, frame);
+}
+
+static const AVFilterPad sierpinski_outputs[] = {
+    {
+        .name          = "default",
+        .type          = AVMEDIA_TYPE_VIDEO,
+        .request_frame = sierpinski_request_frame,
+        .config_props  = config_output,
+    },
+    { NULL }
+};
+
+AVFilter ff_vsrc_sierpinski = {
+    .name          = "sierpinski",
+    .description   = NULL_IF_CONFIG_SMALL("Render a Sierpinski carpet fractal."),
+    .priv_size     = sizeof(SierpinskiContext),
+    .priv_class    = &sierpinski_class,
+    .query_formats = query_formats,
+    .inputs        = NULL,
+    .outputs       = sierpinski_outputs,
+};
-- 
2.22.0

