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.

153 lines
4.8KB

  1. /*
  2. * Copyright (c) 2007 Bobby Bingham
  3. *
  4. * This file is part of Libav.
  5. *
  6. * Libav 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. * Libav 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 Libav; 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 "audio.h"
  25. #include "avfilter.h"
  26. #include "internal.h"
  27. #include "video.h"
  28. typedef struct Buf {
  29. AVFilterBufferRef *buf;
  30. struct Buf *next;
  31. } Buf;
  32. typedef struct {
  33. Buf root;
  34. Buf *last; ///< last buffered frame
  35. } FifoContext;
  36. static av_cold int init(AVFilterContext *ctx, const char *args, void *opaque)
  37. {
  38. FifoContext *fifo = ctx->priv;
  39. fifo->last = &fifo->root;
  40. av_log(ctx, AV_LOG_INFO, "\n");
  41. return 0;
  42. }
  43. static av_cold void uninit(AVFilterContext *ctx)
  44. {
  45. FifoContext *fifo = ctx->priv;
  46. Buf *buf, *tmp;
  47. for (buf = fifo->root.next; buf; buf = tmp) {
  48. tmp = buf->next;
  49. avfilter_unref_buffer(buf->buf);
  50. av_free(buf);
  51. }
  52. }
  53. static void add_to_queue(AVFilterLink *inlink, AVFilterBufferRef *buf)
  54. {
  55. FifoContext *fifo = inlink->dst->priv;
  56. fifo->last->next = av_mallocz(sizeof(Buf));
  57. fifo->last = fifo->last->next;
  58. fifo->last->buf = buf;
  59. }
  60. static void end_frame(AVFilterLink *inlink) { }
  61. static void draw_slice(AVFilterLink *inlink, int y, int h, int slice_dir) { }
  62. static int request_frame(AVFilterLink *outlink)
  63. {
  64. FifoContext *fifo = outlink->src->priv;
  65. Buf *tmp;
  66. int ret;
  67. if (!fifo->root.next) {
  68. if ((ret = ff_request_frame(outlink->src->inputs[0])) < 0)
  69. return ret;
  70. }
  71. /* by doing this, we give ownership of the reference to the next filter,
  72. * so we don't have to worry about dereferencing it ourselves. */
  73. switch (outlink->type) {
  74. case AVMEDIA_TYPE_VIDEO:
  75. ff_start_frame(outlink, fifo->root.next->buf);
  76. ff_draw_slice (outlink, 0, outlink->h, 1);
  77. ff_end_frame (outlink);
  78. break;
  79. case AVMEDIA_TYPE_AUDIO:
  80. ff_filter_samples(outlink, fifo->root.next->buf);
  81. break;
  82. default:
  83. return AVERROR(EINVAL);
  84. }
  85. if (fifo->last == fifo->root.next)
  86. fifo->last = &fifo->root;
  87. tmp = fifo->root.next->next;
  88. av_free(fifo->root.next);
  89. fifo->root.next = tmp;
  90. return 0;
  91. }
  92. AVFilter avfilter_vf_fifo = {
  93. .name = "fifo",
  94. .description = NULL_IF_CONFIG_SMALL("Buffer input images and send them when they are requested."),
  95. .init = init,
  96. .uninit = uninit,
  97. .priv_size = sizeof(FifoContext),
  98. .inputs = (AVFilterPad[]) {{ .name = "default",
  99. .type = AVMEDIA_TYPE_VIDEO,
  100. .get_video_buffer= ff_null_get_video_buffer,
  101. .start_frame = add_to_queue,
  102. .draw_slice = draw_slice,
  103. .end_frame = end_frame,
  104. .rej_perms = AV_PERM_REUSE2, },
  105. { .name = NULL}},
  106. .outputs = (AVFilterPad[]) {{ .name = "default",
  107. .type = AVMEDIA_TYPE_VIDEO,
  108. .request_frame = request_frame, },
  109. { .name = NULL}},
  110. };
  111. AVFilter avfilter_af_afifo = {
  112. .name = "afifo",
  113. .description = NULL_IF_CONFIG_SMALL("Buffer input frames and send them when they are requested."),
  114. .init = init,
  115. .uninit = uninit,
  116. .priv_size = sizeof(FifoContext),
  117. .inputs = (AVFilterPad[]) {{ .name = "default",
  118. .type = AVMEDIA_TYPE_AUDIO,
  119. .get_audio_buffer = ff_null_get_audio_buffer,
  120. .filter_samples = add_to_queue,
  121. .rej_perms = AV_PERM_REUSE2, },
  122. { .name = NULL}},
  123. .outputs = (AVFilterPad[]) {{ .name = "default",
  124. .type = AVMEDIA_TYPE_AUDIO,
  125. .request_frame = request_frame, },
  126. { .name = NULL}},
  127. };