You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

204 lines
5.8KB

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