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.

306 lines
9.9KB

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