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.

312 lines
10KB

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