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.

309 lines
9.0KB

  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. 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. fifo->last->next = av_mallocz(sizeof(Buf));
  68. if (!fifo->last->next) {
  69. avfilter_unref_buffer(buf);
  70. return AVERROR(ENOMEM);
  71. }
  72. fifo->last = fifo->last->next;
  73. fifo->last->buf = buf;
  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, AVFilterBufferRef *buf,
  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(buf->audio->nb_samples > offset);
  96. for (i = 0; i < planes; i++)
  97. buf->extended_data[i] += block_align*offset;
  98. if (buf->data != buf->extended_data)
  99. memcpy(buf->data, buf->extended_data,
  100. FFMIN(planes, FF_ARRAY_ELEMS(buf->data)) * sizeof(*buf->data));
  101. buf->linesize[0] -= block_align*offset;
  102. buf->audio->nb_samples -= offset;
  103. if (buf->pts != AV_NOPTS_VALUE) {
  104. buf->pts += av_rescale_q(offset, (AVRational){1, link->sample_rate},
  105. link->time_base);
  106. }
  107. }
  108. static int calc_ptr_alignment(AVFilterBufferRef *buf)
  109. {
  110. int planes = av_sample_fmt_is_planar(buf->format) ?
  111. av_get_channel_layout_nb_channels(buf->audio->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)buf->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. AVFilterBufferRef *head = s->root.next->buf;
  128. AVFilterBufferRef *buf_out;
  129. int ret;
  130. if (!s->buf_out &&
  131. head->audio->nb_samples >= link->request_samples &&
  132. calc_ptr_alignment(head) >= 32) {
  133. if (head->audio->nb_samples == link->request_samples) {
  134. buf_out = head;
  135. queue_pop(s);
  136. } else {
  137. buf_out = avfilter_ref_buffer(head, AV_PERM_READ);
  138. if (!buf_out)
  139. return AVERROR(ENOMEM);
  140. buf_out->audio->nb_samples = link->request_samples;
  141. buffer_offset(link, head, link->request_samples);
  142. }
  143. } else {
  144. int nb_channels = av_get_channel_layout_nb_channels(link->channel_layout);
  145. if (!s->buf_out) {
  146. s->buf_out = ff_get_audio_buffer(link, AV_PERM_WRITE,
  147. link->request_samples);
  148. if (!s->buf_out)
  149. return AVERROR(ENOMEM);
  150. s->buf_out->audio->nb_samples = 0;
  151. s->buf_out->pts = head->pts;
  152. s->allocated_samples = link->request_samples;
  153. } else if (link->request_samples != s->allocated_samples) {
  154. av_log(ctx, AV_LOG_ERROR, "request_samples changed before the "
  155. "buffer was returned.\n");
  156. return AVERROR(EINVAL);
  157. }
  158. while (s->buf_out->audio->nb_samples < s->allocated_samples) {
  159. int len = FFMIN(s->allocated_samples - s->buf_out->audio->nb_samples,
  160. head->audio->nb_samples);
  161. av_samples_copy(s->buf_out->extended_data, head->extended_data,
  162. s->buf_out->audio->nb_samples, 0, len, nb_channels,
  163. link->format);
  164. s->buf_out->audio->nb_samples += len;
  165. if (len == head->audio->nb_samples) {
  166. avfilter_unref_buffer(head);
  167. queue_pop(s);
  168. if (!s->root.next &&
  169. (ret = ff_request_frame(ctx->inputs[0])) < 0) {
  170. if (ret == AVERROR_EOF) {
  171. av_samples_set_silence(s->buf_out->extended_data,
  172. s->buf_out->audio->nb_samples,
  173. s->allocated_samples -
  174. s->buf_out->audio->nb_samples,
  175. nb_channels, link->format);
  176. s->buf_out->audio->nb_samples = s->allocated_samples;
  177. break;
  178. }
  179. return ret;
  180. }
  181. head = s->root.next->buf;
  182. } else {
  183. buffer_offset(link, head, len);
  184. }
  185. }
  186. buf_out = s->buf_out;
  187. s->buf_out = NULL;
  188. }
  189. return ff_filter_frame(link, buf_out);
  190. }
  191. static int request_frame(AVFilterLink *outlink)
  192. {
  193. FifoContext *fifo = outlink->src->priv;
  194. int ret = 0;
  195. if (!fifo->root.next) {
  196. if ((ret = ff_request_frame(outlink->src->inputs[0])) < 0)
  197. return ret;
  198. av_assert0(fifo->root.next);
  199. }
  200. if (outlink->request_samples) {
  201. return return_audio_frame(outlink->src);
  202. } else {
  203. ret = ff_filter_frame(outlink, fifo->root.next->buf);
  204. queue_pop(fifo);
  205. }
  206. return ret;
  207. }
  208. static const AVFilterPad avfilter_vf_fifo_inputs[] = {
  209. {
  210. .name = "default",
  211. .type = AVMEDIA_TYPE_VIDEO,
  212. .get_video_buffer = ff_null_get_video_buffer,
  213. .filter_frame = add_to_queue,
  214. .min_perms = AV_PERM_PRESERVE,
  215. },
  216. { NULL }
  217. };
  218. static const AVFilterPad avfilter_vf_fifo_outputs[] = {
  219. {
  220. .name = "default",
  221. .type = AVMEDIA_TYPE_VIDEO,
  222. .request_frame = request_frame,
  223. },
  224. { NULL }
  225. };
  226. AVFilter avfilter_vf_fifo = {
  227. .name = "fifo",
  228. .description = NULL_IF_CONFIG_SMALL("Buffer input images and send them when they are requested."),
  229. .init = init,
  230. .uninit = uninit,
  231. .priv_size = sizeof(FifoContext),
  232. .inputs = avfilter_vf_fifo_inputs,
  233. .outputs = avfilter_vf_fifo_outputs,
  234. };
  235. static const AVFilterPad avfilter_af_afifo_inputs[] = {
  236. {
  237. .name = "default",
  238. .type = AVMEDIA_TYPE_AUDIO,
  239. .get_audio_buffer = ff_null_get_audio_buffer,
  240. .filter_frame = add_to_queue,
  241. .min_perms = AV_PERM_PRESERVE,
  242. },
  243. { NULL }
  244. };
  245. static const AVFilterPad avfilter_af_afifo_outputs[] = {
  246. {
  247. .name = "default",
  248. .type = AVMEDIA_TYPE_AUDIO,
  249. .request_frame = request_frame,
  250. },
  251. { NULL }
  252. };
  253. AVFilter avfilter_af_afifo = {
  254. .name = "afifo",
  255. .description = NULL_IF_CONFIG_SMALL("Buffer input frames and send them when they are requested."),
  256. .init = init,
  257. .uninit = uninit,
  258. .priv_size = sizeof(FifoContext),
  259. .inputs = avfilter_af_afifo_inputs,
  260. .outputs = avfilter_af_afifo_outputs,
  261. };