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.

145 lines
3.8KB

  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 config_output(AVFilterLink *outlink)
  57. {
  58. outlink->flags |= FF_LINK_FLAG_REQUEST_LOOP;
  59. return 0;
  60. }
  61. static int filter_frame(AVFilterLink *inlink, AVFrame *in)
  62. {
  63. AVFilterContext *ctx = inlink->dst;
  64. RandomContext *s = ctx->priv;
  65. AVFilterLink *outlink = ctx->outputs[0];
  66. AVFrame *out;
  67. int idx;
  68. if (s->nb_frames_filled < s->nb_frames) {
  69. s->frames[s->nb_frames_filled] = in;
  70. s->pts[s->nb_frames_filled++] = in->pts;
  71. return 0;
  72. }
  73. idx = av_lfg_get(&s->lfg) % s->nb_frames;
  74. out = s->frames[idx];
  75. out->pts = s->pts[0];
  76. memmove(&s->pts[0], &s->pts[1], (s->nb_frames - 1) * sizeof(s->pts[0]));
  77. s->frames[idx] = in;
  78. s->pts[s->nb_frames - 1] = in->pts;
  79. return ff_filter_frame(outlink, out);
  80. }
  81. static int request_frame(AVFilterLink *outlink)
  82. {
  83. AVFilterContext *ctx = outlink->src;
  84. RandomContext *s = ctx->priv;
  85. int ret;
  86. ret = ff_request_frame(ctx->inputs[0]);
  87. if (ret == AVERROR_EOF && !ctx->is_disabled && s->nb_frames > 0) {
  88. AVFrame *out = s->frames[s->nb_frames - 1];
  89. out->pts = s->pts[s->flush_idx++];
  90. ret = ff_filter_frame(outlink, out);
  91. s->frames[s->nb_frames - 1] = NULL;
  92. s->nb_frames--;
  93. }
  94. return ret;
  95. }
  96. static const AVFilterPad random_inputs[] = {
  97. {
  98. .name = "default",
  99. .type = AVMEDIA_TYPE_VIDEO,
  100. .filter_frame = filter_frame,
  101. },
  102. { NULL }
  103. };
  104. static const AVFilterPad random_outputs[] = {
  105. {
  106. .name = "default",
  107. .type = AVMEDIA_TYPE_VIDEO,
  108. .request_frame = request_frame,
  109. .config_props = config_output,
  110. },
  111. { NULL }
  112. };
  113. AVFilter ff_vf_random = {
  114. .name = "random",
  115. .description = NULL_IF_CONFIG_SMALL("Return random frames."),
  116. .priv_size = sizeof(RandomContext),
  117. .priv_class = &random_class,
  118. .init = init,
  119. .inputs = random_inputs,
  120. .outputs = random_outputs,
  121. };