fifo.c 8.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313
  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 *fifo = ctx->priv;
  50. fifo->last = &fifo->root;
  51. return 0;
  52. }
  53. static av_cold void uninit(AVFilterContext *ctx)
  54. {
  55. FifoContext *fifo = ctx->priv;
  56. Buf *buf, *tmp;
  57. for (buf = fifo->root.next; buf; buf = tmp) {
  58. tmp = buf->next;
  59. av_frame_free(&buf->frame);
  60. av_free(buf);
  61. }
  62. av_frame_free(&fifo->out);
  63. }
  64. static int add_to_queue(AVFilterLink *inlink, AVFrame *frame)
  65. {
  66. FifoContext *fifo = inlink->dst->priv;
  67. fifo->last->next = av_mallocz(sizeof(Buf));
  68. if (!fifo->last->next) {
  69. av_frame_free(&frame);
  70. return AVERROR(ENOMEM);
  71. }
  72. fifo->last = fifo->last->next;
  73. fifo->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 = av_get_channel_layout_nb_channels(link->channel_layout);
  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. av_get_channel_layout_nb_channels(frame->channel_layout) : 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 return_audio_frame(AVFilterContext *ctx)
  124. {
  125. AVFilterLink *link = ctx->outputs[0];
  126. FifoContext *s = ctx->priv;
  127. AVFrame *head = s->root.next ? s->root.next->frame : NULL;
  128. AVFrame *out;
  129. int ret;
  130. /* if head is NULL then we're flushing the remaining samples in out */
  131. if (!head && !s->out)
  132. return AVERROR_EOF;
  133. if (!s->out &&
  134. head->nb_samples >= link->request_samples &&
  135. calc_ptr_alignment(head) >= 32) {
  136. if (head->nb_samples == link->request_samples) {
  137. out = head;
  138. queue_pop(s);
  139. } else {
  140. out = av_frame_clone(head);
  141. if (!out)
  142. return AVERROR(ENOMEM);
  143. out->nb_samples = link->request_samples;
  144. buffer_offset(link, head, link->request_samples);
  145. }
  146. } else {
  147. int nb_channels = av_get_channel_layout_nb_channels(link->channel_layout);
  148. if (!s->out) {
  149. s->out = ff_get_audio_buffer(link, link->request_samples);
  150. if (!s->out)
  151. return AVERROR(ENOMEM);
  152. s->out->nb_samples = 0;
  153. s->out->pts = head->pts;
  154. s->allocated_samples = link->request_samples;
  155. } else if (link->request_samples != s->allocated_samples) {
  156. av_log(ctx, AV_LOG_ERROR, "request_samples changed before the "
  157. "buffer was returned.\n");
  158. return AVERROR(EINVAL);
  159. }
  160. while (s->out->nb_samples < s->allocated_samples) {
  161. int len;
  162. if (!s->root.next) {
  163. ret = ff_request_frame(ctx->inputs[0]);
  164. if (ret == AVERROR_EOF) {
  165. av_samples_set_silence(s->out->extended_data,
  166. s->out->nb_samples,
  167. s->allocated_samples -
  168. s->out->nb_samples,
  169. nb_channels, link->format);
  170. s->out->nb_samples = s->allocated_samples;
  171. break;
  172. } else if (ret < 0)
  173. return ret;
  174. av_assert0(s->root.next); // If ff_request_frame() succeeded then we should have a frame
  175. }
  176. head = s->root.next->frame;
  177. len = FFMIN(s->allocated_samples - s->out->nb_samples,
  178. head->nb_samples);
  179. av_samples_copy(s->out->extended_data, head->extended_data,
  180. s->out->nb_samples, 0, len, nb_channels,
  181. link->format);
  182. s->out->nb_samples += len;
  183. if (len == head->nb_samples) {
  184. av_frame_free(&head);
  185. queue_pop(s);
  186. } else {
  187. buffer_offset(link, head, len);
  188. }
  189. }
  190. out = s->out;
  191. s->out = NULL;
  192. }
  193. return ff_filter_frame(link, out);
  194. }
  195. static int request_frame(AVFilterLink *outlink)
  196. {
  197. FifoContext *fifo = outlink->src->priv;
  198. int ret = 0;
  199. if (!fifo->root.next) {
  200. if ((ret = ff_request_frame(outlink->src->inputs[0])) < 0) {
  201. if (ret == AVERROR_EOF && outlink->request_samples)
  202. return return_audio_frame(outlink->src);
  203. return ret;
  204. }
  205. av_assert0(fifo->root.next);
  206. }
  207. if (outlink->request_samples) {
  208. return return_audio_frame(outlink->src);
  209. } else {
  210. ret = ff_filter_frame(outlink, fifo->root.next->frame);
  211. queue_pop(fifo);
  212. }
  213. return ret;
  214. }
  215. static const AVFilterPad avfilter_vf_fifo_inputs[] = {
  216. {
  217. .name = "default",
  218. .type = AVMEDIA_TYPE_VIDEO,
  219. .filter_frame = add_to_queue,
  220. },
  221. { NULL }
  222. };
  223. static const AVFilterPad avfilter_vf_fifo_outputs[] = {
  224. {
  225. .name = "default",
  226. .type = AVMEDIA_TYPE_VIDEO,
  227. .request_frame = request_frame,
  228. },
  229. { NULL }
  230. };
  231. AVFilter ff_vf_fifo = {
  232. .name = "fifo",
  233. .description = NULL_IF_CONFIG_SMALL("Buffer input images and send them when they are requested."),
  234. .init = init,
  235. .uninit = uninit,
  236. .priv_size = sizeof(FifoContext),
  237. .inputs = avfilter_vf_fifo_inputs,
  238. .outputs = avfilter_vf_fifo_outputs,
  239. };
  240. static const AVFilterPad avfilter_af_afifo_inputs[] = {
  241. {
  242. .name = "default",
  243. .type = AVMEDIA_TYPE_AUDIO,
  244. .filter_frame = add_to_queue,
  245. },
  246. { NULL }
  247. };
  248. static const AVFilterPad avfilter_af_afifo_outputs[] = {
  249. {
  250. .name = "default",
  251. .type = AVMEDIA_TYPE_AUDIO,
  252. .request_frame = request_frame,
  253. },
  254. { NULL }
  255. };
  256. AVFilter ff_af_afifo = {
  257. .name = "afifo",
  258. .description = NULL_IF_CONFIG_SMALL("Buffer input frames and send them when they are requested."),
  259. .init = init,
  260. .uninit = uninit,
  261. .priv_size = sizeof(FifoContext),
  262. .inputs = avfilter_af_afifo_inputs,
  263. .outputs = avfilter_af_afifo_outputs,
  264. };