| @@ -0,0 +1,203 @@ |
| | 1 | /* |
| | 2 | * Copyright (c) 2019 Paul B Mahol |
| | 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 | |
| | 21 | /** |
| | 22 | * @file |
| | 23 | * Sierpinski carpet fractal renderer |
| | 24 | */ |
| | 25 | |
| | 26 | #include "avfilter.h" |
| | 27 | #include "formats.h" |
| | 28 | #include "video.h" |
| | 29 | #include "internal.h" |
| | 30 | #include "libavutil/imgutils.h" |
| | 31 | #include "libavutil/intreadwrite.h" |
| | 32 | #include "libavutil/opt.h" |
| | 33 | #include "libavutil/parseutils.h" |
| | 34 | #include "libavutil/lfg.h" |
| | 35 | #include "libavutil/random_seed.h" |
| | 36 | #include <float.h> |
| | 37 | #include <math.h> |
| | 38 | |
| | 39 | typedef struct SierpinskiContext { |
| | 40 | const AVClass *class; |
| | 41 | int w, h; |
| | 42 | AVRational frame_rate; |
| | 43 | uint64_t pts; |
| | 44 | |
| | 45 | unsigned int seed; |
| | 46 | int jump; |
| | 47 | |
| | 48 | int pos_x, pos_y; |
| | 49 | int dest_x, dest_y; |
| | 50 | |
| | 51 | AVLFG lfg; |
| | 52 | } SierpinskiContext; |
| | 53 | |
| | 54 | #define OFFSET(x) offsetof(SierpinskiContext, x) |
| | 55 | #define FLAGS AV_OPT_FLAG_VIDEO_PARAM|AV_OPT_FLAG_FILTERING_PARAM |
| | 56 | |
| | 57 | static const AVOption sierpinski_options[] = { |
| | 58 | {"size", "set frame size", OFFSET(w), AV_OPT_TYPE_IMAGE_SIZE, {.str="640x480"}, 0, 0, FLAGS }, |
| | 59 | {"s", "set frame size", OFFSET(w), AV_OPT_TYPE_IMAGE_SIZE, {.str="640x480"}, 0, 0, FLAGS }, |
| | 60 | {"rate", "set frame rate", OFFSET(frame_rate), AV_OPT_TYPE_VIDEO_RATE, {.str="25"}, 0, 0, FLAGS }, |
| | 61 | {"r", "set frame rate", OFFSET(frame_rate), AV_OPT_TYPE_VIDEO_RATE, {.str="25"}, 0, 0, FLAGS }, |
| | 62 | {"seed", "set the seed", OFFSET(seed), AV_OPT_TYPE_INT, {.i64=-1}, -1, UINT32_MAX, FLAGS }, |
| | 63 | {"jump", "set the jump", OFFSET(jump), AV_OPT_TYPE_INT, {.i64=100}, 1, 10000, FLAGS }, |
| | 64 | {NULL}, |
| | 65 | }; |
| | 66 | |
| | 67 | AVFILTER_DEFINE_CLASS(sierpinski); |
| | 68 | |
| | 69 | static int query_formats(AVFilterContext *ctx) |
| | 70 | { |
| | 71 | static const enum AVPixelFormat pix_fmts[] = { |
| | 72 | AV_PIX_FMT_0BGR32, |
| | 73 | AV_PIX_FMT_NONE |
| | 74 | }; |
| | 75 | |
| | 76 | AVFilterFormats *fmts_list = ff_make_format_list(pix_fmts); |
| | 77 | if (!fmts_list) |
| | 78 | return AVERROR(ENOMEM); |
| | 79 | return ff_set_common_formats(ctx, fmts_list); |
| | 80 | } |
| | 81 | |
| | 82 | static int config_output(AVFilterLink *inlink) |
| | 83 | { |
| | 84 | AVFilterContext *ctx = inlink->src; |
| | 85 | SierpinskiContext *s = ctx->priv; |
| | 86 | |
| | 87 | if (av_image_check_size(s->w, s->h, 0, ctx) < 0) |
| | 88 | return AVERROR(EINVAL); |
| | 89 | |
| | 90 | inlink->w = s->w; |
| | 91 | inlink->h = s->h; |
| | 92 | inlink->time_base = av_inv_q(s->frame_rate); |
| | 93 | inlink->sample_aspect_ratio = (AVRational) {1, 1}; |
| | 94 | if (s->seed == -1) |
| | 95 | s->seed = av_get_random_seed(); |
| | 96 | av_lfg_init(&s->lfg, s->seed); |
| | 97 | |
| | 98 | return 0; |
| | 99 | } |
| | 100 | |
| | 101 | static int fill_sierpinski(SierpinskiContext *s, int x, int y) |
| | 102 | { |
| | 103 | int pos_x = x + s->pos_x; |
| | 104 | int pos_y = y + s->pos_y; |
| | 105 | |
| | 106 | while (pos_x != 0 && pos_y != 0) { |
| | 107 | if (FFABS(pos_x % 3) == 1 && FFABS(pos_y % 3) == 1) |
| | 108 | return 1; |
| | 109 | |
| | 110 | pos_x /= 3; |
| | 111 | pos_y /= 3; |
| | 112 | } |
| | 113 | |
| | 114 | return 0; |
| | 115 | } |
| | 116 | |
| | 117 | static int draw_slice(AVFilterContext *ctx, void *arg, int job, int nb_jobs) |
| | 118 | { |
| | 119 | SierpinskiContext *s = ctx->priv; |
| | 120 | AVFrame *frame = arg; |
| | 121 | const int width = frame->width; |
| | 122 | const int height = frame->height; |
| | 123 | const int start = (height * job ) / nb_jobs; |
| | 124 | const int end = (height * (job+1)) / nb_jobs; |
| | 125 | uint8_t *dst = frame->data[0] + start * frame->linesize[0]; |
| | 126 | |
| | 127 | for (int y = start; y < end; y++) { |
| | 128 | for (int x = 0; x < width; x++) { |
| | 129 | if (fill_sierpinski(s, x, y)) { |
| | 130 | AV_WL32(&dst[x*4], 0x00000000); |
| | 131 | } else { |
| | 132 | AV_WL32(&dst[x*4], 0xFFFFFFFF); |
| | 133 | } |
| | 134 | } |
| | 135 | |
| | 136 | dst += frame->linesize[0]; |
| | 137 | } |
| | 138 | |
| | 139 | return 0; |
| | 140 | } |
| | 141 | |
| | 142 | static void draw_sierpinski(AVFilterContext *ctx, AVFrame *frame) |
| | 143 | { |
| | 144 | SierpinskiContext *s = ctx->priv; |
| | 145 | AVFilterLink *outlink = ctx->outputs[0]; |
| | 146 | |
| | 147 | if (s->pos_x == s->dest_x && s->pos_y == s->dest_y) { |
| | 148 | unsigned int rnd = av_lfg_get(&s->lfg); |
| | 149 | int mod = 2 * s->jump + 1; |
| | 150 | |
| | 151 | s->dest_x += (int)((rnd & 0xffff) % mod) - s->jump; |
| | 152 | s->dest_y += (int)((rnd >> 16) % mod) - s->jump; |
| | 153 | } else { |
| | 154 | if (s->pos_x < s->dest_x) |
| | 155 | s->pos_x++; |
| | 156 | else if (s->pos_x > s->dest_x) |
| | 157 | s->pos_x--; |
| | 158 | |
| | 159 | if (s->pos_y < s->dest_y) |
| | 160 | s->pos_y++; |
| | 161 | else if (s->pos_y > s->dest_y) |
| | 162 | s->pos_y--; |
| | 163 | } |
| | 164 | |
| | 165 | ctx->internal->execute(ctx, draw_slice, frame, NULL, FFMIN(outlink->h, ff_filter_get_nb_threads(ctx))); |
| | 166 | } |
| | 167 | |
| | 168 | static int sierpinski_request_frame(AVFilterLink *link) |
| | 169 | { |
| | 170 | SierpinskiContext *s = link->src->priv; |
| | 171 | AVFrame *frame = ff_get_video_buffer(link, s->w, s->h); |
| | 172 | |
| | 173 | if (!frame) |
| | 174 | return AVERROR(ENOMEM); |
| | 175 | |
| | 176 | frame->sample_aspect_ratio = (AVRational) {1, 1}; |
| | 177 | frame->pts = s->pts++; |
| | 178 | |
| | 179 | draw_sierpinski(link->src, frame); |
| | 180 | |
| | 181 | return ff_filter_frame(link, frame); |
| | 182 | } |
| | 183 | |
| | 184 | static const AVFilterPad sierpinski_outputs[] = { |
| | 185 | { |
| | 186 | .name = "default", |
| | 187 | .type = AVMEDIA_TYPE_VIDEO, |
| | 188 | .request_frame = sierpinski_request_frame, |
| | 189 | .config_props = config_output, |
| | 190 | }, |
| | 191 | { NULL } |
| | 192 | }; |
| | 193 | |
| | 194 | AVFilter ff_vsrc_sierpinski = { |
| | 195 | .name = "sierpinski", |
| | 196 | .description = NULL_IF_CONFIG_SMALL("Render a Sierpinski carpet fractal."), |
| | 197 | .priv_size = sizeof(SierpinskiContext), |
| | 198 | .priv_class = &sierpinski_class, |
| | 199 | .query_formats = query_formats, |
| | 200 | .inputs = NULL, |
| | 201 | .outputs = sierpinski_outputs, |
| | 202 | .flags = AVFILTER_FLAG_SLICE_THREADS, |
| | 203 | }; |