sink_buffer.c 8.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296
  1. /*
  2. * Copyright (c) 2011 Stefano Sabatini
  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. * buffer video sink
  23. */
  24. #include "libavutil/fifo.h"
  25. #include "avfilter.h"
  26. #include "buffersink.h"
  27. #include "internal.h"
  28. AVBufferSinkParams *av_buffersink_params_alloc(void)
  29. {
  30. static const int pixel_fmts[] = { -1 };
  31. AVBufferSinkParams *params = av_malloc(sizeof(AVBufferSinkParams));
  32. if (!params)
  33. return NULL;
  34. params->pixel_fmts = pixel_fmts;
  35. return params;
  36. }
  37. AVABufferSinkParams *av_abuffersink_params_alloc(void)
  38. {
  39. static const int sample_fmts[] = { -1 };
  40. static const int64_t channel_layouts[] = { -1 };
  41. AVABufferSinkParams *params = av_malloc(sizeof(AVABufferSinkParams));
  42. if (!params)
  43. return NULL;
  44. params->sample_fmts = sample_fmts;
  45. params->channel_layouts = channel_layouts;
  46. return params;
  47. }
  48. typedef struct {
  49. AVFifoBuffer *fifo; ///< FIFO buffer of video frame references
  50. /* only used for video */
  51. enum PixelFormat *pixel_fmts; ///< list of accepted pixel formats, must be terminated with -1
  52. /* only used for audio */
  53. enum AVSampleFormat *sample_fmts; ///< list of accepted sample formats, terminated by AV_SAMPLE_FMT_NONE
  54. int64_t *channel_layouts; ///< list of accepted channel layouts, terminated by -1
  55. } BufferSinkContext;
  56. #define FIFO_INIT_SIZE 8
  57. static av_cold int common_init(AVFilterContext *ctx)
  58. {
  59. BufferSinkContext *buf = ctx->priv;
  60. buf->fifo = av_fifo_alloc(FIFO_INIT_SIZE*sizeof(AVFilterBufferRef *));
  61. if (!buf->fifo) {
  62. av_log(ctx, AV_LOG_ERROR, "Failed to allocate fifo\n");
  63. return AVERROR(ENOMEM);
  64. }
  65. return 0;
  66. }
  67. static av_cold void common_uninit(AVFilterContext *ctx)
  68. {
  69. BufferSinkContext *buf = ctx->priv;
  70. AVFilterBufferRef *picref;
  71. if (buf->fifo) {
  72. while (av_fifo_size(buf->fifo) >= sizeof(AVFilterBufferRef *)) {
  73. av_fifo_generic_read(buf->fifo, &picref, sizeof(picref), NULL);
  74. avfilter_unref_buffer(picref);
  75. }
  76. av_fifo_free(buf->fifo);
  77. buf->fifo = NULL;
  78. }
  79. }
  80. static void end_frame(AVFilterLink *inlink)
  81. {
  82. AVFilterContext *ctx = inlink->dst;
  83. BufferSinkContext *buf = inlink->dst->priv;
  84. if (av_fifo_space(buf->fifo) < sizeof(AVFilterBufferRef *)) {
  85. /* realloc fifo size */
  86. if (av_fifo_realloc2(buf->fifo, av_fifo_size(buf->fifo) * 2) < 0) {
  87. av_log(ctx, AV_LOG_ERROR,
  88. "Cannot buffer more frames. Consume some available frames "
  89. "before adding new ones.\n");
  90. return;
  91. }
  92. }
  93. /* cache frame */
  94. av_fifo_generic_write(buf->fifo,
  95. &inlink->cur_buf, sizeof(AVFilterBufferRef *), NULL);
  96. }
  97. int av_buffersink_get_buffer_ref(AVFilterContext *ctx,
  98. AVFilterBufferRef **bufref, int flags)
  99. {
  100. BufferSinkContext *buf = ctx->priv;
  101. AVFilterLink *inlink = ctx->inputs[0];
  102. int ret;
  103. *bufref = NULL;
  104. /* no picref available, fetch it from the filterchain */
  105. if (!av_fifo_size(buf->fifo)) {
  106. if (flags & AV_BUFFERSINK_FLAG_NO_REQUEST)
  107. return AVERROR(EAGAIN);
  108. if ((ret = avfilter_request_frame(inlink)) < 0)
  109. return ret;
  110. }
  111. if (!av_fifo_size(buf->fifo))
  112. return AVERROR(EINVAL);
  113. if (flags & AV_BUFFERSINK_FLAG_PEEK)
  114. *bufref = *((AVFilterBufferRef **)av_fifo_peek2(buf->fifo, 0));
  115. else
  116. av_fifo_generic_read(buf->fifo, bufref, sizeof(*bufref), NULL);
  117. return 0;
  118. }
  119. int av_buffersink_poll_frame(AVFilterContext *ctx)
  120. {
  121. BufferSinkContext *buf = ctx->priv;
  122. AVFilterLink *inlink = ctx->inputs[0];
  123. return av_fifo_size(buf->fifo)/sizeof(AVFilterBufferRef *) + avfilter_poll_frame(inlink);
  124. }
  125. #if FF_API_OLD_VSINK_API
  126. int av_vsink_buffer_get_video_buffer_ref(AVFilterContext *ctx,
  127. AVFilterBufferRef **picref, int flags)
  128. {
  129. return av_buffersink_get_buffer_ref(ctx, picref, flags);
  130. }
  131. #endif
  132. #if CONFIG_BUFFERSINK_FILTER
  133. static av_cold int vsink_init(AVFilterContext *ctx, const char *args, void *opaque)
  134. {
  135. BufferSinkContext *buf = ctx->priv;
  136. av_unused AVBufferSinkParams *params;
  137. if (!opaque) {
  138. av_log(ctx, AV_LOG_WARNING,
  139. "No opaque field provided\n");
  140. buf->pixel_fmts = NULL;
  141. } else {
  142. #if FF_API_OLD_VSINK_API
  143. const int *pixel_fmts = (const enum PixelFormat *)opaque;
  144. #else
  145. params = (AVBufferSinkParams *)opaque;
  146. const int *pixel_fmts = params->pixel_fmts;
  147. #endif
  148. buf->pixel_fmts = ff_copy_int_list(pixel_fmts);
  149. if (!buf->pixel_fmts)
  150. return AVERROR(ENOMEM);
  151. }
  152. return common_init(ctx);
  153. }
  154. static av_cold void vsink_uninit(AVFilterContext *ctx)
  155. {
  156. BufferSinkContext *buf = ctx->priv;
  157. av_freep(&buf->pixel_fmts);
  158. return common_uninit(ctx);
  159. }
  160. static int vsink_query_formats(AVFilterContext *ctx)
  161. {
  162. BufferSinkContext *buf = ctx->priv;
  163. if (buf->pixel_fmts)
  164. avfilter_set_common_pixel_formats(ctx, avfilter_make_format_list(buf->pixel_fmts));
  165. else
  166. avfilter_default_query_formats(ctx);
  167. return 0;
  168. }
  169. AVFilter avfilter_vsink_buffersink = {
  170. .name = "buffersink",
  171. .description = NULL_IF_CONFIG_SMALL("Buffer video frames, and make them available to the end of the filter graph."),
  172. .priv_size = sizeof(BufferSinkContext),
  173. .init = vsink_init,
  174. .uninit = vsink_uninit,
  175. .query_formats = vsink_query_formats,
  176. .inputs = (const AVFilterPad[]) {{ .name = "default",
  177. .type = AVMEDIA_TYPE_VIDEO,
  178. .end_frame = end_frame,
  179. .min_perms = AV_PERM_READ, },
  180. { .name = NULL }},
  181. .outputs = (const AVFilterPad[]) {{ .name = NULL }},
  182. };
  183. #endif /* CONFIG_BUFFERSINK_FILTER */
  184. #if CONFIG_ABUFFERSINK_FILTER
  185. static void filter_samples(AVFilterLink *link, AVFilterBufferRef *samplesref)
  186. {
  187. end_frame(link);
  188. }
  189. static av_cold int asink_init(AVFilterContext *ctx, const char *args, void *opaque)
  190. {
  191. BufferSinkContext *buf = ctx->priv;
  192. AVABufferSinkParams *params;
  193. if (!opaque) {
  194. av_log(ctx, AV_LOG_ERROR,
  195. "No opaque field provided, an AVABufferSinkParams struct is required\n");
  196. return AVERROR(EINVAL);
  197. } else
  198. params = (AVABufferSinkParams *)opaque;
  199. buf->sample_fmts = ff_copy_int_list (params->sample_fmts);
  200. buf->channel_layouts = ff_copy_int64_list(params->channel_layouts);
  201. if (!buf->sample_fmts || !buf->channel_layouts) {
  202. av_freep(&buf->sample_fmts);
  203. av_freep(&buf->channel_layouts);
  204. return AVERROR(ENOMEM);
  205. }
  206. return common_init(ctx);
  207. }
  208. static av_cold void asink_uninit(AVFilterContext *ctx)
  209. {
  210. BufferSinkContext *buf = ctx->priv;
  211. av_freep(&buf->sample_fmts);
  212. av_freep(&buf->channel_layouts);
  213. return common_uninit(ctx);
  214. }
  215. static int asink_query_formats(AVFilterContext *ctx)
  216. {
  217. BufferSinkContext *buf = ctx->priv;
  218. AVFilterFormats *formats = NULL;
  219. AVFilterChannelLayouts *layouts = NULL;
  220. if (!(formats = avfilter_make_format_list(buf->sample_fmts)))
  221. return AVERROR(ENOMEM);
  222. avfilter_set_common_sample_formats(ctx, formats);
  223. if (!(layouts = avfilter_make_format64_list(buf->channel_layouts)))
  224. return AVERROR(ENOMEM);
  225. ff_set_common_channel_layouts(ctx, layouts);
  226. ff_set_common_samplerates (ctx, ff_all_samplerates());
  227. return 0;
  228. }
  229. AVFilter avfilter_asink_abuffersink = {
  230. .name = "abuffersink",
  231. .description = NULL_IF_CONFIG_SMALL("Buffer audio frames, and make them available to the end of the filter graph."),
  232. .init = asink_init,
  233. .uninit = asink_uninit,
  234. .priv_size = sizeof(BufferSinkContext),
  235. .query_formats = asink_query_formats,
  236. .inputs = (const AVFilterPad[]) {{ .name = "default",
  237. .type = AVMEDIA_TYPE_AUDIO,
  238. .filter_samples = filter_samples,
  239. .min_perms = AV_PERM_READ, },
  240. { .name = NULL }},
  241. .outputs = (const AVFilterPad[]) {{ .name = NULL }},
  242. };
  243. #endif /* CONFIG_ABUFFERSINK_FILTER */