fifo.c 9.7 KB

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