sink_buffer.c 8.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271
  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. AVBufferSinkParams *av_buffersink_params_alloc(void)
  28. {
  29. static const int pixel_fmts[] = { -1 };
  30. AVBufferSinkParams *params = av_malloc(sizeof(AVBufferSinkParams));
  31. if (!params)
  32. return NULL;
  33. params->pixel_fmts = pixel_fmts;
  34. return params;
  35. }
  36. AVABufferSinkParams *av_abuffersink_params_alloc(void)
  37. {
  38. static const int sample_fmts[] = { -1 };
  39. static const int packing_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. params->packing_fmts = packing_fmts;
  47. return params;
  48. }
  49. typedef struct {
  50. AVFifoBuffer *fifo; ///< FIFO buffer of video frame references
  51. /* only used for video */
  52. const enum PixelFormat *pixel_fmts; ///< list of accepted pixel formats, must be terminated with -1
  53. /* only used for audio */
  54. const enum AVSampleFormat *sample_fmts; ///< list of accepted sample formats, terminated by AV_SAMPLE_FMT_NONE
  55. const int64_t *channel_layouts; ///< list of accepted channel layouts, terminated by -1
  56. const int *packing_fmts; ///< list of accepted packing formats, terminated by -1
  57. } BufferSinkContext;
  58. #define FIFO_INIT_SIZE 8
  59. static av_cold int common_init(AVFilterContext *ctx)
  60. {
  61. BufferSinkContext *buf = ctx->priv;
  62. buf->fifo = av_fifo_alloc(FIFO_INIT_SIZE*sizeof(AVFilterBufferRef *));
  63. if (!buf->fifo) {
  64. av_log(ctx, AV_LOG_ERROR, "Failed to allocate fifo\n");
  65. return AVERROR(ENOMEM);
  66. }
  67. return 0;
  68. }
  69. static av_cold void common_uninit(AVFilterContext *ctx)
  70. {
  71. BufferSinkContext *buf = ctx->priv;
  72. AVFilterBufferRef *picref;
  73. if (buf->fifo) {
  74. while (av_fifo_size(buf->fifo) >= sizeof(AVFilterBufferRef *)) {
  75. av_fifo_generic_read(buf->fifo, &picref, sizeof(picref), NULL);
  76. avfilter_unref_buffer(picref);
  77. }
  78. av_fifo_free(buf->fifo);
  79. buf->fifo = NULL;
  80. }
  81. }
  82. static void end_frame(AVFilterLink *inlink)
  83. {
  84. AVFilterContext *ctx = inlink->dst;
  85. BufferSinkContext *buf = inlink->dst->priv;
  86. if (av_fifo_space(buf->fifo) < sizeof(AVFilterBufferRef *)) {
  87. /* realloc fifo size */
  88. if (av_fifo_realloc2(buf->fifo, av_fifo_size(buf->fifo) * 2) < 0) {
  89. av_log(ctx, AV_LOG_ERROR,
  90. "Cannot buffer more frames. Consume some available frames "
  91. "before adding new ones.\n");
  92. return;
  93. }
  94. }
  95. /* cache frame */
  96. av_fifo_generic_write(buf->fifo,
  97. &inlink->cur_buf, sizeof(AVFilterBufferRef *), NULL);
  98. }
  99. int av_buffersink_get_buffer_ref(AVFilterContext *ctx,
  100. AVFilterBufferRef **bufref, int flags)
  101. {
  102. BufferSinkContext *buf = ctx->priv;
  103. AVFilterLink *inlink = ctx->inputs[0];
  104. int ret;
  105. *bufref = NULL;
  106. /* no picref available, fetch it from the filterchain */
  107. if (!av_fifo_size(buf->fifo)) {
  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_ERROR,
  139. "No opaque field provided\n");
  140. return AVERROR(EINVAL);
  141. } else {
  142. #if FF_API_OLD_VSINK_API
  143. buf->pixel_fmts = (const enum PixelFormat *)opaque;
  144. #else
  145. params = (AVBufferSinkParams *)opaque;
  146. buf->pixel_fmts = params->pixel_fmts;
  147. #endif
  148. }
  149. return common_init(ctx);
  150. }
  151. static int vsink_query_formats(AVFilterContext *ctx)
  152. {
  153. BufferSinkContext *buf = ctx->priv;
  154. avfilter_set_common_pixel_formats(ctx, avfilter_make_format_list(buf->pixel_fmts));
  155. return 0;
  156. }
  157. AVFilter avfilter_vsink_buffersink = {
  158. .name = "buffersink",
  159. .description = NULL_IF_CONFIG_SMALL("Buffer video frames, and make them available to the end of the filter graph."),
  160. .priv_size = sizeof(BufferSinkContext),
  161. .init = vsink_init,
  162. .uninit = common_uninit,
  163. .query_formats = vsink_query_formats,
  164. .inputs = (const AVFilterPad[]) {{ .name = "default",
  165. .type = AVMEDIA_TYPE_VIDEO,
  166. .end_frame = end_frame,
  167. .min_perms = AV_PERM_READ, },
  168. { .name = NULL }},
  169. .outputs = (const AVFilterPad[]) {{ .name = NULL }},
  170. };
  171. #endif /* CONFIG_BUFFERSINK_FILTER */
  172. #if CONFIG_ABUFFERSINK_FILTER
  173. static void filter_samples(AVFilterLink *link, AVFilterBufferRef *samplesref)
  174. {
  175. end_frame(link);
  176. }
  177. static av_cold int asink_init(AVFilterContext *ctx, const char *args, void *opaque)
  178. {
  179. BufferSinkContext *buf = ctx->priv;
  180. AVABufferSinkParams *params;
  181. if (!opaque) {
  182. av_log(ctx, AV_LOG_ERROR,
  183. "No opaque field provided, an AVABufferSinkParams struct is required\n");
  184. return AVERROR(EINVAL);
  185. } else
  186. params = (AVABufferSinkParams *)opaque;
  187. buf->sample_fmts = params->sample_fmts;
  188. buf->channel_layouts = params->channel_layouts;
  189. buf->packing_fmts = params->packing_fmts;
  190. return common_init(ctx);
  191. }
  192. static int asink_query_formats(AVFilterContext *ctx)
  193. {
  194. BufferSinkContext *buf = ctx->priv;
  195. AVFilterFormats *formats = NULL;
  196. if (!(formats = avfilter_make_format_list(buf->sample_fmts)))
  197. return AVERROR(ENOMEM);
  198. avfilter_set_common_sample_formats(ctx, formats);
  199. if (!(formats = avfilter_make_format64_list(buf->channel_layouts)))
  200. return AVERROR(ENOMEM);
  201. avfilter_set_common_channel_layouts(ctx, formats);
  202. if (!(formats = avfilter_make_format_list(buf->packing_fmts)))
  203. return AVERROR(ENOMEM);
  204. avfilter_set_common_packing_formats(ctx, formats);
  205. return 0;
  206. }
  207. AVFilter avfilter_asink_abuffersink = {
  208. .name = "abuffersink",
  209. .description = NULL_IF_CONFIG_SMALL("Buffer audio frames, and make them available to the end of the filter graph."),
  210. .init = asink_init,
  211. .uninit = common_uninit,
  212. .priv_size = sizeof(BufferSinkContext),
  213. .query_formats = asink_query_formats,
  214. .inputs = (const AVFilterPad[]) {{ .name = "default",
  215. .type = AVMEDIA_TYPE_AUDIO,
  216. .filter_samples = filter_samples,
  217. .min_perms = AV_PERM_READ, },
  218. { .name = NULL }},
  219. .outputs = (const AVFilterPad[]) {{ .name = NULL }},
  220. };
  221. #endif /* CONFIG_ABUFFERSINK_FILTER */