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.

296 lines
9.7KB

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