sink_buffer.c 9.1 KB

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