fifo.c 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312
  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/audioconvert.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. AVFilterBufferRef *buf;
  35. struct Buf *next;
  36. } Buf;
  37. typedef struct {
  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. AVFilterBufferRef *buf_out;
  45. int allocated_samples; ///< number of samples buf_out was allocated for
  46. } FifoContext;
  47. static av_cold int init(AVFilterContext *ctx, const char *args)
  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. avfilter_unref_bufferp(&buf->buf);
  60. av_free(buf);
  61. }
  62. avfilter_unref_bufferp(&fifo->buf_out);
  63. }
  64. static int add_to_queue(AVFilterLink *inlink, AVFilterBufferRef *buf)
  65. {
  66. FifoContext *fifo = inlink->dst->priv;
  67. inlink->cur_buf = NULL;
  68. fifo->last->next = av_mallocz(sizeof(Buf));
  69. if (!fifo->last->next) {
  70. avfilter_unref_buffer(buf);
  71. return AVERROR(ENOMEM);
  72. }
  73. fifo->last = fifo->last->next;
  74. fifo->last->buf = buf;
  75. return 0;
  76. }
  77. static void queue_pop(FifoContext *s)
  78. {
  79. Buf *tmp = s->root.next->next;
  80. if (s->last == s->root.next)
  81. s->last = &s->root;
  82. av_freep(&s->root.next);
  83. s->root.next = tmp;
  84. }
  85. static int end_frame(AVFilterLink *inlink)
  86. {
  87. return 0;
  88. }
  89. static int draw_slice(AVFilterLink *inlink, int y, int h, int slice_dir)
  90. {
  91. return 0;
  92. }
  93. /**
  94. * Move data pointers and pts offset samples forward.
  95. */
  96. static void buffer_offset(AVFilterLink *link, AVFilterBufferRef *buf,
  97. int offset)
  98. {
  99. int nb_channels = av_get_channel_layout_nb_channels(link->channel_layout);
  100. int planar = av_sample_fmt_is_planar(link->format);
  101. int planes = planar ? nb_channels : 1;
  102. int block_align = av_get_bytes_per_sample(link->format) * (planar ? 1 : nb_channels);
  103. int i;
  104. av_assert0(buf->audio->nb_samples > offset);
  105. for (i = 0; i < planes; i++)
  106. buf->extended_data[i] += block_align*offset;
  107. if (buf->data != buf->extended_data)
  108. memcpy(buf->data, buf->extended_data,
  109. FFMIN(planes, FF_ARRAY_ELEMS(buf->data)) * sizeof(*buf->data));
  110. buf->linesize[0] -= block_align*offset;
  111. buf->audio->nb_samples -= offset;
  112. if (buf->pts != AV_NOPTS_VALUE) {
  113. buf->pts += av_rescale_q(offset, (AVRational){1, link->sample_rate},
  114. link->time_base);
  115. }
  116. }
  117. static int calc_ptr_alignment(AVFilterBufferRef *buf)
  118. {
  119. int planes = av_sample_fmt_is_planar(buf->format) ?
  120. av_get_channel_layout_nb_channels(buf->audio->channel_layout) : 1;
  121. int min_align = 128;
  122. int p;
  123. for (p = 0; p < planes; p++) {
  124. int cur_align = 128;
  125. while ((intptr_t)buf->extended_data[p] % cur_align)
  126. cur_align >>= 1;
  127. if (cur_align < min_align)
  128. min_align = cur_align;
  129. }
  130. return min_align;
  131. }
  132. static int return_audio_frame(AVFilterContext *ctx)
  133. {
  134. AVFilterLink *link = ctx->outputs[0];
  135. FifoContext *s = ctx->priv;
  136. AVFilterBufferRef *head = s->root.next->buf;
  137. AVFilterBufferRef *buf_out;
  138. int ret;
  139. if (!s->buf_out &&
  140. head->audio->nb_samples >= link->request_samples &&
  141. calc_ptr_alignment(head) >= 32) {
  142. if (head->audio->nb_samples == link->request_samples) {
  143. buf_out = head;
  144. queue_pop(s);
  145. } else {
  146. buf_out = avfilter_ref_buffer(head, AV_PERM_READ);
  147. if (!buf_out)
  148. return AVERROR(ENOMEM);
  149. buf_out->audio->nb_samples = link->request_samples;
  150. buffer_offset(link, head, link->request_samples);
  151. }
  152. } else {
  153. int nb_channels = av_get_channel_layout_nb_channels(link->channel_layout);
  154. if (!s->buf_out) {
  155. s->buf_out = ff_get_audio_buffer(link, AV_PERM_WRITE,
  156. link->request_samples);
  157. if (!s->buf_out)
  158. return AVERROR(ENOMEM);
  159. s->buf_out->audio->nb_samples = 0;
  160. s->buf_out->pts = head->pts;
  161. s->allocated_samples = link->request_samples;
  162. } else if (link->request_samples != s->allocated_samples) {
  163. av_log(ctx, AV_LOG_ERROR, "request_samples changed before the "
  164. "buffer was returned.\n");
  165. return AVERROR(EINVAL);
  166. }
  167. while (s->buf_out->audio->nb_samples < s->allocated_samples) {
  168. int len = FFMIN(s->allocated_samples - s->buf_out->audio->nb_samples,
  169. head->audio->nb_samples);
  170. av_samples_copy(s->buf_out->extended_data, head->extended_data,
  171. s->buf_out->audio->nb_samples, 0, len, nb_channels,
  172. link->format);
  173. s->buf_out->audio->nb_samples += len;
  174. if (len == head->audio->nb_samples) {
  175. avfilter_unref_buffer(head);
  176. queue_pop(s);
  177. if (!s->root.next &&
  178. (ret = ff_request_frame(ctx->inputs[0])) < 0) {
  179. if (ret == AVERROR_EOF) {
  180. av_samples_set_silence(s->buf_out->extended_data,
  181. s->buf_out->audio->nb_samples,
  182. s->allocated_samples -
  183. s->buf_out->audio->nb_samples,
  184. nb_channels, link->format);
  185. s->buf_out->audio->nb_samples = s->allocated_samples;
  186. break;
  187. }
  188. return ret;
  189. }
  190. head = s->root.next->buf;
  191. } else {
  192. buffer_offset(link, head, len);
  193. }
  194. }
  195. buf_out = s->buf_out;
  196. s->buf_out = NULL;
  197. }
  198. return ff_filter_samples(link, buf_out);
  199. }
  200. static int request_frame(AVFilterLink *outlink)
  201. {
  202. FifoContext *fifo = outlink->src->priv;
  203. int ret = 0;
  204. if (!fifo->root.next) {
  205. if ((ret = ff_request_frame(outlink->src->inputs[0])) < 0)
  206. return ret;
  207. }
  208. /* by doing this, we give ownership of the reference to the next filter,
  209. * so we don't have to worry about dereferencing it ourselves. */
  210. switch (outlink->type) {
  211. case AVMEDIA_TYPE_VIDEO:
  212. if ((ret = ff_start_frame(outlink, fifo->root.next->buf)) < 0 ||
  213. (ret = ff_draw_slice(outlink, 0, outlink->h, 1)) < 0 ||
  214. (ret = ff_end_frame(outlink)) < 0)
  215. return ret;
  216. queue_pop(fifo);
  217. break;
  218. case AVMEDIA_TYPE_AUDIO:
  219. if (outlink->request_samples) {
  220. return return_audio_frame(outlink->src);
  221. } else {
  222. ret = ff_filter_samples(outlink, fifo->root.next->buf);
  223. queue_pop(fifo);
  224. }
  225. break;
  226. default:
  227. return AVERROR(EINVAL);
  228. }
  229. return ret;
  230. }
  231. AVFilter avfilter_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 = (const AVFilterPad[]) {{ .name = "default",
  238. .type = AVMEDIA_TYPE_VIDEO,
  239. .get_video_buffer= ff_null_get_video_buffer,
  240. .start_frame = add_to_queue,
  241. .draw_slice = draw_slice,
  242. .end_frame = end_frame,
  243. .min_perms = AV_PERM_PRESERVE, },
  244. { .name = NULL}},
  245. .outputs = (const AVFilterPad[]) {{ .name = "default",
  246. .type = AVMEDIA_TYPE_VIDEO,
  247. .request_frame = request_frame, },
  248. { .name = NULL}},
  249. };
  250. AVFilter avfilter_af_afifo = {
  251. .name = "afifo",
  252. .description = NULL_IF_CONFIG_SMALL("Buffer input frames and send them when they are requested."),
  253. .init = init,
  254. .uninit = uninit,
  255. .priv_size = sizeof(FifoContext),
  256. .inputs = (const AVFilterPad[]) {{ .name = "default",
  257. .type = AVMEDIA_TYPE_AUDIO,
  258. .get_audio_buffer = ff_null_get_audio_buffer,
  259. .filter_samples = add_to_queue,
  260. .min_perms = AV_PERM_PRESERVE, },
  261. { .name = NULL}},
  262. .outputs = (const AVFilterPad[]) {{ .name = "default",
  263. .type = AVMEDIA_TYPE_AUDIO,
  264. .request_frame = request_frame, },
  265. { .name = NULL}},
  266. };