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.

217 lines
5.5KB

  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/avassert.h"
  25. #include "libavutil/channel_layout.h"
  26. #include "libavutil/common.h"
  27. #include "libavutil/mathematics.h"
  28. #include "libavutil/samplefmt.h"
  29. #include "audio.h"
  30. #include "avfilter.h"
  31. #include "internal.h"
  32. #include "video.h"
  33. typedef struct Buf {
  34. AVFrame *frame;
  35. struct Buf *next;
  36. } Buf;
  37. typedef struct FifoContext {
  38. Buf root;
  39. Buf *last; ///< last buffered frame
  40. /**
  41. * When a specific number of output samples is requested, the partial
  42. * buffer is stored here
  43. */
  44. AVFrame *out;
  45. int allocated_samples; ///< number of samples out was allocated for
  46. } FifoContext;
  47. static av_cold int init(AVFilterContext *ctx)
  48. {
  49. FifoContext *s = ctx->priv;
  50. s->last = &s->root;
  51. return 0;
  52. }
  53. static av_cold void uninit(AVFilterContext *ctx)
  54. {
  55. FifoContext *s = ctx->priv;
  56. Buf *buf, *tmp;
  57. for (buf = s->root.next; buf; buf = tmp) {
  58. tmp = buf->next;
  59. av_frame_free(&buf->frame);
  60. av_free(buf);
  61. }
  62. av_frame_free(&s->out);
  63. }
  64. static int add_to_queue(AVFilterLink *inlink, AVFrame *frame)
  65. {
  66. FifoContext *s = inlink->dst->priv;
  67. s->last->next = av_mallocz(sizeof(Buf));
  68. if (!s->last->next) {
  69. av_frame_free(&frame);
  70. return AVERROR(ENOMEM);
  71. }
  72. s->last = s->last->next;
  73. s->last->frame = frame;
  74. return 0;
  75. }
  76. static void queue_pop(FifoContext *s)
  77. {
  78. Buf *tmp = s->root.next->next;
  79. if (s->last == s->root.next)
  80. s->last = &s->root;
  81. av_freep(&s->root.next);
  82. s->root.next = tmp;
  83. }
  84. /**
  85. * Move data pointers and pts offset samples forward.
  86. */
  87. static void buffer_offset(AVFilterLink *link, AVFrame *frame,
  88. int offset)
  89. {
  90. int nb_channels = link->channels;
  91. int planar = av_sample_fmt_is_planar(link->format);
  92. int planes = planar ? nb_channels : 1;
  93. int block_align = av_get_bytes_per_sample(link->format) * (planar ? 1 : nb_channels);
  94. int i;
  95. av_assert0(frame->nb_samples > offset);
  96. for (i = 0; i < planes; i++)
  97. frame->extended_data[i] += block_align * offset;
  98. if (frame->data != frame->extended_data)
  99. memcpy(frame->data, frame->extended_data,
  100. FFMIN(planes, FF_ARRAY_ELEMS(frame->data)) * sizeof(*frame->data));
  101. frame->linesize[0] -= block_align*offset;
  102. frame->nb_samples -= offset;
  103. if (frame->pts != AV_NOPTS_VALUE) {
  104. frame->pts += av_rescale_q(offset, (AVRational){1, link->sample_rate},
  105. link->time_base);
  106. }
  107. }
  108. static int calc_ptr_alignment(AVFrame *frame)
  109. {
  110. int planes = av_sample_fmt_is_planar(frame->format) ?
  111. frame->channels : 1;
  112. int min_align = 128;
  113. int p;
  114. for (p = 0; p < planes; p++) {
  115. int cur_align = 128;
  116. while ((intptr_t)frame->extended_data[p] % cur_align)
  117. cur_align >>= 1;
  118. if (cur_align < min_align)
  119. min_align = cur_align;
  120. }
  121. return min_align;
  122. }
  123. static int request_frame(AVFilterLink *outlink)
  124. {
  125. FifoContext *s = outlink->src->priv;
  126. int ret = 0;
  127. if (!s->root.next) {
  128. if ((ret = ff_request_frame(outlink->src->inputs[0])) < 0)
  129. return ret;
  130. if (!s->root.next)
  131. return 0;
  132. }
  133. ret = ff_filter_frame(outlink, s->root.next->frame);
  134. queue_pop(s);
  135. return ret;
  136. }
  137. static const AVFilterPad avfilter_vf_fifo_inputs[] = {
  138. {
  139. .name = "default",
  140. .type = AVMEDIA_TYPE_VIDEO,
  141. .filter_frame = add_to_queue,
  142. },
  143. { NULL }
  144. };
  145. static const AVFilterPad avfilter_vf_fifo_outputs[] = {
  146. {
  147. .name = "default",
  148. .type = AVMEDIA_TYPE_VIDEO,
  149. .request_frame = request_frame,
  150. },
  151. { NULL }
  152. };
  153. AVFilter ff_vf_fifo = {
  154. .name = "fifo",
  155. .description = NULL_IF_CONFIG_SMALL("Buffer input images and send them when they are requested."),
  156. .init = init,
  157. .uninit = uninit,
  158. .priv_size = sizeof(FifoContext),
  159. .inputs = avfilter_vf_fifo_inputs,
  160. .outputs = avfilter_vf_fifo_outputs,
  161. };
  162. static const AVFilterPad avfilter_af_afifo_inputs[] = {
  163. {
  164. .name = "default",
  165. .type = AVMEDIA_TYPE_AUDIO,
  166. .filter_frame = add_to_queue,
  167. },
  168. { NULL }
  169. };
  170. static const AVFilterPad avfilter_af_afifo_outputs[] = {
  171. {
  172. .name = "default",
  173. .type = AVMEDIA_TYPE_AUDIO,
  174. .request_frame = request_frame,
  175. },
  176. { NULL }
  177. };
  178. AVFilter ff_af_afifo = {
  179. .name = "afifo",
  180. .description = NULL_IF_CONFIG_SMALL("Buffer input frames and send them when they are requested."),
  181. .init = init,
  182. .uninit = uninit,
  183. .priv_size = sizeof(FifoContext),
  184. .inputs = avfilter_af_afifo_inputs,
  185. .outputs = avfilter_af_afifo_outputs,
  186. };