vsrc_perlin.c 6.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169
  1. /*
  2. * This file is part of FFmpeg.
  3. *
  4. * FFmpeg is free software; you can redistribute it and/or
  5. * modify it under the terms of the GNU Lesser General Public
  6. * License as published by the Free Software Foundation; either
  7. * version 2.1 of the License, or (at your option) any later version.
  8. *
  9. * FFmpeg is distributed in the hope that it will be useful,
  10. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  11. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  12. * Lesser General Public License for more details.
  13. *
  14. * You should have received a copy of the GNU Lesser General Public
  15. * License along with FFmpeg; if not, write to the Free Software
  16. * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
  17. */
  18. /**
  19. * @file
  20. * Perlin noise generator
  21. */
  22. #include <float.h>
  23. #include "perlin.h"
  24. #include "libavutil/lfg.h"
  25. #include "libavutil/opt.h"
  26. #include "avfilter.h"
  27. #include "internal.h"
  28. #include "formats.h"
  29. #include "video.h"
  30. typedef struct PerlinContext {
  31. const AVClass *class;
  32. int w, h;
  33. AVRational frame_rate;
  34. FFPerlin perlin;
  35. int octaves;
  36. double persistence;
  37. unsigned int random_seed;
  38. enum FFPerlinRandomMode random_mode;
  39. double xscale, yscale, tscale;
  40. uint64_t pts;
  41. } PerlinContext;
  42. #define OFFSET(x) offsetof(PerlinContext, x)
  43. #define FLAGS AV_OPT_FLAG_VIDEO_PARAM|AV_OPT_FLAG_FILTERING_PARAM
  44. static const AVOption perlin_options[] = {
  45. { "size", "set video size", OFFSET(w), AV_OPT_TYPE_IMAGE_SIZE, {.str="320x240"}, 0, 0, FLAGS },
  46. { "s", "set video size", OFFSET(w), AV_OPT_TYPE_IMAGE_SIZE, {.str="320x240"}, 0, 0, FLAGS },
  47. { "rate", "set video rate", OFFSET(frame_rate), AV_OPT_TYPE_VIDEO_RATE, {.str="25"}, 0, INT_MAX, FLAGS },
  48. { "r", "set video rate", OFFSET(frame_rate), AV_OPT_TYPE_VIDEO_RATE, {.str="25"}, 0, INT_MAX, FLAGS },
  49. { "octaves", "set the number of components to use to generate the noise", OFFSET(octaves), AV_OPT_TYPE_INT, {.i64=1}, 1, INT_MAX, FLAGS },
  50. { "persistence", "set the octaves persistence", OFFSET(persistence), AV_OPT_TYPE_DOUBLE, {.dbl=1}, 0.0, DBL_MAX, FLAGS },
  51. { "xscale", "set x-scale factor", OFFSET(xscale), AV_OPT_TYPE_DOUBLE, {.dbl=1}, 0.0, DBL_MAX, FLAGS },
  52. { "yscale", "set y-scale factor", OFFSET(yscale), AV_OPT_TYPE_DOUBLE, {.dbl=1}, 0.0, DBL_MAX, FLAGS },
  53. { "tscale", "set t-scale factor", OFFSET(tscale), AV_OPT_TYPE_DOUBLE, {.dbl=1}, 0.0, DBL_MAX, FLAGS },
  54. { "random_mode", "set random mode used to compute initial pattern", OFFSET(random_mode), AV_OPT_TYPE_INT, {.i64=FF_PERLIN_RANDOM_MODE_RANDOM}, 0, FF_PERLIN_RANDOM_MODE_NB-1, FLAGS, .unit = "random_mode" },
  55. { "random", "compute and use random seed", 0, AV_OPT_TYPE_CONST, {.i64=FF_PERLIN_RANDOM_MODE_RANDOM}, 0, 0, FLAGS, .unit = "random_mode" },
  56. { "ken", "use the predefined initial pattern defined by Ken Perlin in the original article", 0, AV_OPT_TYPE_CONST, {.i64=FF_PERLIN_RANDOM_MODE_KEN}, 0, 0, FLAGS, .unit = "random_mode" },
  57. { "seed", "use the value specified by random_seed", 0, AV_OPT_TYPE_CONST, {.i64=FF_PERLIN_RANDOM_MODE_SEED}, 0, 0, FLAGS, .unit="random_mode" },
  58. { "random_seed", "set the seed for filling the initial pattern", OFFSET(random_seed), AV_OPT_TYPE_UINT, {.i64=0}, 0, UINT_MAX, FLAGS },
  59. { "seed", "set the seed for filling the initial pattern", OFFSET(random_seed), AV_OPT_TYPE_UINT, {.i64=0}, 0, UINT_MAX, FLAGS },
  60. { NULL }
  61. };
  62. AVFILTER_DEFINE_CLASS(perlin);
  63. static av_cold int init(AVFilterContext *ctx)
  64. {
  65. PerlinContext *perlin = ctx->priv;
  66. int ret;
  67. if (ret = ff_perlin_init(&perlin->perlin, -1, perlin->octaves, perlin->persistence,
  68. perlin->random_mode, perlin->random_seed)) {
  69. return ret;
  70. }
  71. av_log(ctx, AV_LOG_VERBOSE,
  72. "s:%dx%d r:%d/%d octaves:%d persistence:%f xscale:%f yscale:%f tscale:%f\n",
  73. perlin->w, perlin->h, perlin->frame_rate.num, perlin->frame_rate.den,
  74. perlin->octaves, perlin->persistence,
  75. perlin->xscale, perlin->yscale, perlin->tscale);
  76. return 0;
  77. }
  78. static int config_props(AVFilterLink *outlink)
  79. {
  80. PerlinContext *perlin = outlink->src->priv;
  81. outlink->w = perlin->w;
  82. outlink->h = perlin->h;
  83. outlink->time_base = av_inv_q(perlin->frame_rate);
  84. outlink->frame_rate = perlin->frame_rate;
  85. return 0;
  86. }
  87. static int request_frame(AVFilterLink *outlink)
  88. {
  89. AVFilterContext *ctx = outlink->src;
  90. PerlinContext *perlin = ctx->priv;
  91. AVFrame *picref = ff_get_video_buffer(outlink, perlin->w, perlin->h);
  92. int i, j;
  93. uint8_t *data0, *data;
  94. double x, y, t;
  95. if (!picref)
  96. return AVERROR(ENOMEM);
  97. picref->sample_aspect_ratio = (AVRational) {1, 1};
  98. picref->pts = perlin->pts++;
  99. picref->duration = 1;
  100. t = perlin->tscale * (perlin->pts * av_q2d(outlink->time_base));
  101. data0 = picref->data[0];
  102. for (i = 0; i < perlin->h; i++) {
  103. y = perlin->yscale * (double)i / perlin->h;
  104. data = data0;
  105. for (j = 0; j < perlin->w; j++) {
  106. double res;
  107. x = perlin->xscale * (double)j / perlin->w;
  108. res = ff_perlin_get(&perlin->perlin, x, y, t);
  109. av_log(ctx, AV_LOG_DEBUG, "x:%f y:%f t:%f => %f\n", x, y, t, res);
  110. *data++ = res * 255;
  111. }
  112. data0 += picref->linesize[0];
  113. }
  114. return ff_filter_frame(outlink, picref);
  115. }
  116. static int query_formats(AVFilterContext *ctx)
  117. {
  118. enum AVPixelFormat pix_fmts[] = { AV_PIX_FMT_GRAY8, AV_PIX_FMT_NONE };
  119. return ff_set_common_formats_from_list(ctx, pix_fmts);
  120. }
  121. static const AVFilterPad perlin_outputs[] = {
  122. {
  123. .name = "default",
  124. .type = AVMEDIA_TYPE_VIDEO,
  125. .request_frame = request_frame,
  126. .config_props = config_props,
  127. },
  128. };
  129. const AVFilter ff_vsrc_perlin = {
  130. .name = "perlin",
  131. .description = NULL_IF_CONFIG_SMALL("Generate Perlin noise"),
  132. .priv_size = sizeof(PerlinContext),
  133. .priv_class = &perlin_class,
  134. .init = init,
  135. .inputs = NULL,
  136. FILTER_OUTPUTS(perlin_outputs),
  137. FILTER_QUERY_FUNC(query_formats),
  138. };