sink_buffer.c 9.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325
  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/avassert.h"
  25. #include "libavutil/fifo.h"
  26. #include "avfilter.h"
  27. #include "buffersink.h"
  28. #include "internal.h"
  29. AVBufferSinkParams *av_buffersink_params_alloc(void)
  30. {
  31. static const int pixel_fmts[] = { -1 };
  32. AVBufferSinkParams *params = av_malloc(sizeof(AVBufferSinkParams));
  33. if (!params)
  34. return NULL;
  35. params->pixel_fmts = pixel_fmts;
  36. return params;
  37. }
  38. AVABufferSinkParams *av_abuffersink_params_alloc(void)
  39. {
  40. static const int sample_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. return params;
  48. }
  49. typedef struct {
  50. AVFifoBuffer *fifo; ///< FIFO buffer of video frame references
  51. unsigned warning_limit;
  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. } 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. buf->warning_limit = 100;
  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. av_assert1(inlink->cur_buf);
  88. if (av_fifo_space(buf->fifo) < sizeof(AVFilterBufferRef *)) {
  89. /* realloc fifo size */
  90. if (av_fifo_realloc2(buf->fifo, av_fifo_size(buf->fifo) * 2) < 0) {
  91. av_log(ctx, AV_LOG_ERROR,
  92. "Cannot buffer more frames. Consume some available frames "
  93. "before adding new ones.\n");
  94. return;
  95. }
  96. }
  97. /* cache frame */
  98. av_fifo_generic_write(buf->fifo,
  99. &inlink->cur_buf, sizeof(AVFilterBufferRef *), NULL);
  100. if (buf->warning_limit &&
  101. av_fifo_size(buf->fifo) / sizeof(AVFilterBufferRef *) >= buf->warning_limit) {
  102. av_log(ctx, AV_LOG_WARNING,
  103. "%d buffers queued in %s, something may be wrong.\n",
  104. buf->warning_limit,
  105. (char *)av_x_if_null(ctx->name, ctx->filter->name));
  106. buf->warning_limit *= 10;
  107. }
  108. }
  109. void av_buffersink_set_frame_size(AVFilterContext *ctx, unsigned frame_size)
  110. {
  111. AVFilterLink *inlink = ctx->inputs[0];
  112. inlink->min_samples = inlink->max_samples =
  113. inlink->partial_buf_size = frame_size;
  114. }
  115. int av_buffersink_get_buffer_ref(AVFilterContext *ctx,
  116. AVFilterBufferRef **bufref, int flags)
  117. {
  118. BufferSinkContext *buf = ctx->priv;
  119. AVFilterLink *inlink = ctx->inputs[0];
  120. int ret;
  121. *bufref = NULL;
  122. av_assert0(!strcmp(ctx->filter->name, "buffersink") || !strcmp(ctx->filter->name, "abuffersink"));
  123. /* no picref available, fetch it from the filterchain */
  124. if (!av_fifo_size(buf->fifo)) {
  125. if (flags & AV_BUFFERSINK_FLAG_NO_REQUEST)
  126. return AVERROR(EAGAIN);
  127. if ((ret = ff_request_frame(inlink)) < 0)
  128. return ret;
  129. }
  130. if (!av_fifo_size(buf->fifo))
  131. return AVERROR(EINVAL);
  132. if (flags & AV_BUFFERSINK_FLAG_PEEK)
  133. *bufref = *((AVFilterBufferRef **)av_fifo_peek2(buf->fifo, 0));
  134. else
  135. av_fifo_generic_read(buf->fifo, bufref, sizeof(*bufref), NULL);
  136. return 0;
  137. }
  138. AVRational av_buffersink_get_frame_rate(AVFilterContext *ctx)
  139. {
  140. av_assert0(!strcmp(ctx->filter->name, "buffersink"));
  141. return ctx->inputs[0]->frame_rate;
  142. }
  143. int av_buffersink_poll_frame(AVFilterContext *ctx)
  144. {
  145. BufferSinkContext *buf = ctx->priv;
  146. AVFilterLink *inlink = ctx->inputs[0];
  147. av_assert0(!strcmp(ctx->filter->name, "buffersink") || !strcmp(ctx->filter->name, "abuffersink"));
  148. return av_fifo_size(buf->fifo)/sizeof(AVFilterBufferRef *) + ff_poll_frame(inlink);
  149. }
  150. #if CONFIG_BUFFERSINK_FILTER
  151. static av_cold int vsink_init(AVFilterContext *ctx, const char *args)
  152. {
  153. BufferSinkContext *buf = ctx->priv;
  154. AVBufferSinkParams *params = NULL;
  155. // if(args && !strcmp(args, "opaque"))
  156. // params = (AVBufferSinkParams *)(args+7);
  157. if (!params) {
  158. av_log(ctx, AV_LOG_WARNING,
  159. "No opaque field provided\n");
  160. buf->pixel_fmts = NULL;
  161. } else {
  162. const int *pixel_fmts = params->pixel_fmts;
  163. buf->pixel_fmts = ff_copy_int_list(pixel_fmts);
  164. if (!buf->pixel_fmts)
  165. return AVERROR(ENOMEM);
  166. }
  167. return common_init(ctx);
  168. }
  169. static av_cold void vsink_uninit(AVFilterContext *ctx)
  170. {
  171. BufferSinkContext *buf = ctx->priv;
  172. av_freep(&buf->pixel_fmts);
  173. common_uninit(ctx);
  174. }
  175. static int vsink_query_formats(AVFilterContext *ctx)
  176. {
  177. BufferSinkContext *buf = ctx->priv;
  178. if (buf->pixel_fmts)
  179. ff_set_common_formats(ctx, ff_make_format_list(buf->pixel_fmts));
  180. else
  181. ff_default_query_formats(ctx);
  182. return 0;
  183. }
  184. AVFilter avfilter_vsink_buffersink = {
  185. .name = "buffersink",
  186. .description = NULL_IF_CONFIG_SMALL("Buffer video frames, and make them available to the end of the filter graph."),
  187. .priv_size = sizeof(BufferSinkContext),
  188. .init = vsink_init,
  189. .uninit = vsink_uninit,
  190. .query_formats = vsink_query_formats,
  191. .inputs = (const AVFilterPad[]) {{ .name = "default",
  192. .type = AVMEDIA_TYPE_VIDEO,
  193. .end_frame = end_frame,
  194. .min_perms = AV_PERM_READ, },
  195. { .name = NULL }},
  196. .outputs = (const AVFilterPad[]) {{ .name = NULL }},
  197. };
  198. #endif /* CONFIG_BUFFERSINK_FILTER */
  199. #if CONFIG_ABUFFERSINK_FILTER
  200. static void filter_samples(AVFilterLink *link, AVFilterBufferRef *samplesref)
  201. {
  202. end_frame(link);
  203. }
  204. static av_cold int asink_init(AVFilterContext *ctx, const char *args)
  205. {
  206. BufferSinkContext *buf = ctx->priv;
  207. AVABufferSinkParams *params = NULL;
  208. // if(args && !strcmp(args, "opaque"))
  209. // params = (AVABufferSinkParams *)(args+7);
  210. if (params && params->sample_fmts) {
  211. buf->sample_fmts = ff_copy_int_list (params->sample_fmts);
  212. if (!buf->sample_fmts)
  213. goto fail_enomem;
  214. }
  215. if (params && params->channel_layouts) {
  216. buf->channel_layouts = ff_copy_int64_list(params->channel_layouts);
  217. if (!buf->channel_layouts)
  218. goto fail_enomem;
  219. }
  220. if (!common_init(ctx))
  221. return 0;
  222. fail_enomem:
  223. av_freep(&buf->sample_fmts);
  224. av_freep(&buf->channel_layouts);
  225. return AVERROR(ENOMEM);
  226. }
  227. static av_cold void asink_uninit(AVFilterContext *ctx)
  228. {
  229. BufferSinkContext *buf = ctx->priv;
  230. av_freep(&buf->sample_fmts);
  231. av_freep(&buf->channel_layouts);
  232. common_uninit(ctx);
  233. }
  234. static int asink_query_formats(AVFilterContext *ctx)
  235. {
  236. BufferSinkContext *buf = ctx->priv;
  237. AVFilterFormats *formats = NULL;
  238. AVFilterChannelLayouts *layouts = NULL;
  239. if (buf->sample_fmts) {
  240. if (!(formats = ff_make_format_list(buf->sample_fmts)))
  241. return AVERROR(ENOMEM);
  242. ff_set_common_formats(ctx, formats);
  243. }
  244. if (buf->channel_layouts) {
  245. if (!(layouts = avfilter_make_format64_list(buf->channel_layouts)))
  246. return AVERROR(ENOMEM);
  247. ff_set_common_channel_layouts(ctx, layouts);
  248. }
  249. return 0;
  250. }
  251. AVFilter avfilter_asink_abuffersink = {
  252. .name = "abuffersink",
  253. .description = NULL_IF_CONFIG_SMALL("Buffer audio frames, and make them available to the end of the filter graph."),
  254. .init = asink_init,
  255. .uninit = asink_uninit,
  256. .priv_size = sizeof(BufferSinkContext),
  257. .query_formats = asink_query_formats,
  258. .inputs = (const AVFilterPad[]) {{ .name = "default",
  259. .type = AVMEDIA_TYPE_AUDIO,
  260. .filter_samples = filter_samples,
  261. .min_perms = AV_PERM_READ, },
  262. { .name = NULL }},
  263. .outputs = (const AVFilterPad[]) {{ .name = NULL }},
  264. };
  265. #endif /* CONFIG_ABUFFERSINK_FILTER */