vf_random.c 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137
  1. /*
  2. * Copyright (c) 2015 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. #include "libavutil/lfg.h"
  21. #include "libavutil/opt.h"
  22. #include "libavutil/random_seed.h"
  23. #include "avfilter.h"
  24. #include "formats.h"
  25. #include "internal.h"
  26. #include "video.h"
  27. #define MAX_FRAMES 512
  28. typedef struct RandomContext {
  29. const AVClass *class;
  30. AVLFG lfg;
  31. int nb_frames;
  32. int64_t random_seed;
  33. int nb_frames_filled;
  34. AVFrame *frames[MAX_FRAMES];
  35. int64_t pts[MAX_FRAMES];
  36. int flush_idx;
  37. } RandomContext;
  38. #define OFFSET(x) offsetof(RandomContext, x)
  39. #define FLAGS AV_OPT_FLAG_VIDEO_PARAM|AV_OPT_FLAG_FILTERING_PARAM
  40. static const AVOption random_options[] = {
  41. { "frames", "set number of frames in cache", OFFSET(nb_frames), AV_OPT_TYPE_INT, {.i64=30}, 2, MAX_FRAMES, FLAGS },
  42. { "seed", "set the seed", OFFSET(random_seed), AV_OPT_TYPE_INT64, {.i64=-1}, -1, UINT32_MAX, FLAGS },
  43. { NULL }
  44. };
  45. AVFILTER_DEFINE_CLASS(random);
  46. static av_cold int init(AVFilterContext *ctx)
  47. {
  48. RandomContext *s = ctx->priv;
  49. uint32_t seed;
  50. if (s->random_seed < 0)
  51. s->random_seed = av_get_random_seed();
  52. seed = s->random_seed;
  53. av_lfg_init(&s->lfg, seed);
  54. return 0;
  55. }
  56. static int filter_frame(AVFilterLink *inlink, AVFrame *in)
  57. {
  58. AVFilterContext *ctx = inlink->dst;
  59. RandomContext *s = ctx->priv;
  60. AVFilterLink *outlink = ctx->outputs[0];
  61. AVFrame *out;
  62. int idx;
  63. if (s->nb_frames_filled < s->nb_frames) {
  64. s->frames[s->nb_frames_filled] = in;
  65. s->pts[s->nb_frames_filled++] = in->pts;
  66. return 0;
  67. }
  68. idx = av_lfg_get(&s->lfg) % s->nb_frames;
  69. out = s->frames[idx];
  70. out->pts = s->pts[0];
  71. memmove(&s->pts[0], &s->pts[1], (s->nb_frames - 1) * sizeof(s->pts[0]));
  72. s->frames[idx] = in;
  73. s->pts[s->nb_frames - 1] = in->pts;
  74. return ff_filter_frame(outlink, out);
  75. }
  76. static int request_frame(AVFilterLink *outlink)
  77. {
  78. AVFilterContext *ctx = outlink->src;
  79. RandomContext *s = ctx->priv;
  80. int ret;
  81. ret = ff_request_frame(ctx->inputs[0]);
  82. if (ret == AVERROR_EOF && !ctx->is_disabled && s->nb_frames > 0) {
  83. AVFrame *out = s->frames[s->nb_frames - 1];
  84. out->pts = s->pts[s->flush_idx++];
  85. ret = ff_filter_frame(outlink, out);
  86. s->frames[s->nb_frames - 1] = NULL;
  87. s->nb_frames--;
  88. }
  89. return ret;
  90. }
  91. static const AVFilterPad random_inputs[] = {
  92. {
  93. .name = "default",
  94. .type = AVMEDIA_TYPE_VIDEO,
  95. .filter_frame = filter_frame,
  96. },
  97. { NULL }
  98. };
  99. static const AVFilterPad random_outputs[] = {
  100. {
  101. .name = "default",
  102. .type = AVMEDIA_TYPE_VIDEO,
  103. .request_frame = request_frame,
  104. },
  105. { NULL }
  106. };
  107. AVFilter ff_vf_random = {
  108. .name = "random",
  109. .description = NULL_IF_CONFIG_SMALL("Return random frames."),
  110. .priv_size = sizeof(RandomContext),
  111. .priv_class = &random_class,
  112. .init = init,
  113. .inputs = random_inputs,
  114. .outputs = random_outputs,
  115. };