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.

168 lines
4.0KB

  1. /*
  2. * Copyright (c) 2007 Bobby Bingham
  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. * FIFO buffering filter
  23. */
  24. #include "libavutil/common.h"
  25. #include "libavutil/mathematics.h"
  26. #include "audio.h"
  27. #include "avfilter.h"
  28. #include "internal.h"
  29. typedef struct Buf {
  30. AVFrame *frame;
  31. struct Buf *next;
  32. } Buf;
  33. typedef struct FifoContext {
  34. Buf root;
  35. Buf *last; ///< last buffered frame
  36. /**
  37. * When a specific number of output samples is requested, the partial
  38. * buffer is stored here
  39. */
  40. AVFrame *out;
  41. int allocated_samples; ///< number of samples out was allocated for
  42. } FifoContext;
  43. static av_cold int init(AVFilterContext *ctx)
  44. {
  45. FifoContext *s = ctx->priv;
  46. s->last = &s->root;
  47. return 0;
  48. }
  49. static av_cold void uninit(AVFilterContext *ctx)
  50. {
  51. FifoContext *s = ctx->priv;
  52. Buf *buf, *tmp;
  53. for (buf = s->root.next; buf; buf = tmp) {
  54. tmp = buf->next;
  55. av_frame_free(&buf->frame);
  56. av_free(buf);
  57. }
  58. av_frame_free(&s->out);
  59. }
  60. static int add_to_queue(AVFilterLink *inlink, AVFrame *frame)
  61. {
  62. FifoContext *s = inlink->dst->priv;
  63. s->last->next = av_mallocz(sizeof(Buf));
  64. if (!s->last->next) {
  65. av_frame_free(&frame);
  66. return AVERROR(ENOMEM);
  67. }
  68. s->last = s->last->next;
  69. s->last->frame = frame;
  70. return 0;
  71. }
  72. static void queue_pop(FifoContext *s)
  73. {
  74. Buf *tmp = s->root.next->next;
  75. if (s->last == s->root.next)
  76. s->last = &s->root;
  77. av_freep(&s->root.next);
  78. s->root.next = tmp;
  79. }
  80. static int request_frame(AVFilterLink *outlink)
  81. {
  82. FifoContext *s = outlink->src->priv;
  83. int ret = 0;
  84. if (!s->root.next) {
  85. if ((ret = ff_request_frame(outlink->src->inputs[0])) < 0)
  86. return ret;
  87. if (!s->root.next)
  88. return 0;
  89. }
  90. ret = ff_filter_frame(outlink, s->root.next->frame);
  91. queue_pop(s);
  92. return ret;
  93. }
  94. static const AVFilterPad avfilter_vf_fifo_inputs[] = {
  95. {
  96. .name = "default",
  97. .type = AVMEDIA_TYPE_VIDEO,
  98. .filter_frame = add_to_queue,
  99. },
  100. { NULL }
  101. };
  102. static const AVFilterPad avfilter_vf_fifo_outputs[] = {
  103. {
  104. .name = "default",
  105. .type = AVMEDIA_TYPE_VIDEO,
  106. .request_frame = request_frame,
  107. },
  108. { NULL }
  109. };
  110. AVFilter ff_vf_fifo = {
  111. .name = "fifo",
  112. .description = NULL_IF_CONFIG_SMALL("Buffer input images and send them when they are requested."),
  113. .init = init,
  114. .uninit = uninit,
  115. .priv_size = sizeof(FifoContext),
  116. .inputs = avfilter_vf_fifo_inputs,
  117. .outputs = avfilter_vf_fifo_outputs,
  118. };
  119. static const AVFilterPad avfilter_af_afifo_inputs[] = {
  120. {
  121. .name = "default",
  122. .type = AVMEDIA_TYPE_AUDIO,
  123. .filter_frame = add_to_queue,
  124. },
  125. { NULL }
  126. };
  127. static const AVFilterPad avfilter_af_afifo_outputs[] = {
  128. {
  129. .name = "default",
  130. .type = AVMEDIA_TYPE_AUDIO,
  131. .request_frame = request_frame,
  132. },
  133. { NULL }
  134. };
  135. AVFilter ff_af_afifo = {
  136. .name = "afifo",
  137. .description = NULL_IF_CONFIG_SMALL("Buffer input frames and send them when they are requested."),
  138. .init = init,
  139. .uninit = uninit,
  140. .priv_size = sizeof(FifoContext),
  141. .inputs = avfilter_af_afifo_inputs,
  142. .outputs = avfilter_af_afifo_outputs,
  143. };