fifo.c 8.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315
  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 = 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 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 = link->channels;
  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. if (!s->root.next)
  175. return 0;
  176. }
  177. head = s->root.next->frame;
  178. len = FFMIN(s->allocated_samples - s->out->nb_samples,
  179. head->nb_samples);
  180. av_samples_copy(s->out->extended_data, head->extended_data,
  181. s->out->nb_samples, 0, len, nb_channels,
  182. link->format);
  183. s->out->nb_samples += len;
  184. if (len == head->nb_samples) {
  185. av_frame_free(&head);
  186. queue_pop(s);
  187. } else {
  188. buffer_offset(link, head, len);
  189. }
  190. }
  191. out = s->out;
  192. s->out = NULL;
  193. }
  194. return ff_filter_frame(link, out);
  195. }
  196. static int request_frame(AVFilterLink *outlink)
  197. {
  198. FifoContext *fifo = outlink->src->priv;
  199. int ret = 0;
  200. if (!fifo->root.next) {
  201. if ((ret = ff_request_frame(outlink->src->inputs[0])) < 0) {
  202. if (ret == AVERROR_EOF && outlink->request_samples)
  203. return return_audio_frame(outlink->src);
  204. return ret;
  205. }
  206. if (!fifo->root.next)
  207. return 0;
  208. }
  209. if (outlink->request_samples) {
  210. return return_audio_frame(outlink->src);
  211. } else {
  212. ret = ff_filter_frame(outlink, fifo->root.next->frame);
  213. queue_pop(fifo);
  214. }
  215. return ret;
  216. }
  217. static const AVFilterPad avfilter_vf_fifo_inputs[] = {
  218. {
  219. .name = "default",
  220. .type = AVMEDIA_TYPE_VIDEO,
  221. .filter_frame = add_to_queue,
  222. },
  223. { NULL }
  224. };
  225. static const AVFilterPad avfilter_vf_fifo_outputs[] = {
  226. {
  227. .name = "default",
  228. .type = AVMEDIA_TYPE_VIDEO,
  229. .request_frame = request_frame,
  230. },
  231. { NULL }
  232. };
  233. AVFilter ff_vf_fifo = {
  234. .name = "fifo",
  235. .description = NULL_IF_CONFIG_SMALL("Buffer input images and send them when they are requested."),
  236. .init = init,
  237. .uninit = uninit,
  238. .priv_size = sizeof(FifoContext),
  239. .inputs = avfilter_vf_fifo_inputs,
  240. .outputs = avfilter_vf_fifo_outputs,
  241. };
  242. static const AVFilterPad avfilter_af_afifo_inputs[] = {
  243. {
  244. .name = "default",
  245. .type = AVMEDIA_TYPE_AUDIO,
  246. .filter_frame = add_to_queue,
  247. },
  248. { NULL }
  249. };
  250. static const AVFilterPad avfilter_af_afifo_outputs[] = {
  251. {
  252. .name = "default",
  253. .type = AVMEDIA_TYPE_AUDIO,
  254. .request_frame = request_frame,
  255. },
  256. { NULL }
  257. };
  258. AVFilter ff_af_afifo = {
  259. .name = "afifo",
  260. .description = NULL_IF_CONFIG_SMALL("Buffer input frames and send them when they are requested."),
  261. .init = init,
  262. .uninit = uninit,
  263. .priv_size = sizeof(FifoContext),
  264. .inputs = avfilter_af_afifo_inputs,
  265. .outputs = avfilter_af_afifo_outputs,
  266. };