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.

299 lines
9.0KB

  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. /* only used for video */
  52. enum PixelFormat *pixel_fmts; ///< list of accepted pixel formats, must be terminated with -1
  53. /* only used for audio */
  54. enum AVSampleFormat *sample_fmts; ///< list of accepted sample formats, terminated by AV_SAMPLE_FMT_NONE
  55. int64_t *channel_layouts; ///< list of accepted channel layouts, terminated by -1
  56. } BufferSinkContext;
  57. #define FIFO_INIT_SIZE 8
  58. static av_cold int common_init(AVFilterContext *ctx)
  59. {
  60. BufferSinkContext *buf = ctx->priv;
  61. buf->fifo = av_fifo_alloc(FIFO_INIT_SIZE*sizeof(AVFilterBufferRef *));
  62. if (!buf->fifo) {
  63. av_log(ctx, AV_LOG_ERROR, "Failed to allocate fifo\n");
  64. return AVERROR(ENOMEM);
  65. }
  66. return 0;
  67. }
  68. static av_cold void common_uninit(AVFilterContext *ctx)
  69. {
  70. BufferSinkContext *buf = ctx->priv;
  71. AVFilterBufferRef *picref;
  72. if (buf->fifo) {
  73. while (av_fifo_size(buf->fifo) >= sizeof(AVFilterBufferRef *)) {
  74. av_fifo_generic_read(buf->fifo, &picref, sizeof(picref), NULL);
  75. avfilter_unref_buffer(picref);
  76. }
  77. av_fifo_free(buf->fifo);
  78. buf->fifo = NULL;
  79. }
  80. }
  81. static void end_frame(AVFilterLink *inlink)
  82. {
  83. AVFilterContext *ctx = inlink->dst;
  84. BufferSinkContext *buf = inlink->dst->priv;
  85. av_assert1(inlink->cur_buf);
  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 (flags & AV_BUFFERSINK_FLAG_NO_REQUEST)
  109. return AVERROR(EAGAIN);
  110. if ((ret = avfilter_request_frame(inlink)) < 0)
  111. return ret;
  112. }
  113. if (!av_fifo_size(buf->fifo))
  114. return AVERROR(EINVAL);
  115. if (flags & AV_BUFFERSINK_FLAG_PEEK)
  116. *bufref = *((AVFilterBufferRef **)av_fifo_peek2(buf->fifo, 0));
  117. else
  118. av_fifo_generic_read(buf->fifo, bufref, sizeof(*bufref), NULL);
  119. return 0;
  120. }
  121. int av_buffersink_poll_frame(AVFilterContext *ctx)
  122. {
  123. BufferSinkContext *buf = ctx->priv;
  124. AVFilterLink *inlink = ctx->inputs[0];
  125. return av_fifo_size(buf->fifo)/sizeof(AVFilterBufferRef *) + avfilter_poll_frame(inlink);
  126. }
  127. #if FF_API_OLD_VSINK_API
  128. int av_vsink_buffer_get_video_buffer_ref(AVFilterContext *ctx,
  129. AVFilterBufferRef **picref, int flags)
  130. {
  131. return av_buffersink_get_buffer_ref(ctx, picref, flags);
  132. }
  133. #endif
  134. #if CONFIG_BUFFERSINK_FILTER
  135. static av_cold int vsink_init(AVFilterContext *ctx, const char *args, void *opaque)
  136. {
  137. BufferSinkContext *buf = ctx->priv;
  138. av_unused AVBufferSinkParams *params;
  139. if (!opaque) {
  140. av_log(ctx, AV_LOG_WARNING,
  141. "No opaque field provided\n");
  142. buf->pixel_fmts = NULL;
  143. } else {
  144. #if FF_API_OLD_VSINK_API
  145. const int *pixel_fmts = (const enum PixelFormat *)opaque;
  146. #else
  147. params = (AVBufferSinkParams *)opaque;
  148. const int *pixel_fmts = params->pixel_fmts;
  149. #endif
  150. buf->pixel_fmts = ff_copy_int_list(pixel_fmts);
  151. if (!buf->pixel_fmts)
  152. return AVERROR(ENOMEM);
  153. }
  154. return common_init(ctx);
  155. }
  156. static av_cold void vsink_uninit(AVFilterContext *ctx)
  157. {
  158. BufferSinkContext *buf = ctx->priv;
  159. av_freep(&buf->pixel_fmts);
  160. return common_uninit(ctx);
  161. }
  162. static int vsink_query_formats(AVFilterContext *ctx)
  163. {
  164. BufferSinkContext *buf = ctx->priv;
  165. if (buf->pixel_fmts)
  166. avfilter_set_common_pixel_formats(ctx, avfilter_make_format_list(buf->pixel_fmts));
  167. else
  168. avfilter_default_query_formats(ctx);
  169. return 0;
  170. }
  171. AVFilter avfilter_vsink_buffersink = {
  172. .name = "buffersink",
  173. .description = NULL_IF_CONFIG_SMALL("Buffer video frames, and make them available to the end of the filter graph."),
  174. .priv_size = sizeof(BufferSinkContext),
  175. .init = vsink_init,
  176. .uninit = vsink_uninit,
  177. .query_formats = vsink_query_formats,
  178. .inputs = (const AVFilterPad[]) {{ .name = "default",
  179. .type = AVMEDIA_TYPE_VIDEO,
  180. .end_frame = end_frame,
  181. .min_perms = AV_PERM_READ, },
  182. { .name = NULL }},
  183. .outputs = (const AVFilterPad[]) {{ .name = NULL }},
  184. };
  185. #endif /* CONFIG_BUFFERSINK_FILTER */
  186. #if CONFIG_ABUFFERSINK_FILTER
  187. static void filter_samples(AVFilterLink *link, AVFilterBufferRef *samplesref)
  188. {
  189. end_frame(link);
  190. }
  191. static av_cold int asink_init(AVFilterContext *ctx, const char *args, void *opaque)
  192. {
  193. BufferSinkContext *buf = ctx->priv;
  194. AVABufferSinkParams *params;
  195. if (!opaque) {
  196. av_log(ctx, AV_LOG_ERROR,
  197. "No opaque field provided, an AVABufferSinkParams struct is required\n");
  198. return AVERROR(EINVAL);
  199. } else
  200. params = (AVABufferSinkParams *)opaque;
  201. buf->sample_fmts = ff_copy_int_list (params->sample_fmts);
  202. buf->channel_layouts = ff_copy_int64_list(params->channel_layouts);
  203. if (!buf->sample_fmts || !buf->channel_layouts) {
  204. av_freep(&buf->sample_fmts);
  205. av_freep(&buf->channel_layouts);
  206. return AVERROR(ENOMEM);
  207. }
  208. return common_init(ctx);
  209. }
  210. static av_cold void asink_uninit(AVFilterContext *ctx)
  211. {
  212. BufferSinkContext *buf = ctx->priv;
  213. av_freep(&buf->sample_fmts);
  214. av_freep(&buf->channel_layouts);
  215. return common_uninit(ctx);
  216. }
  217. static int asink_query_formats(AVFilterContext *ctx)
  218. {
  219. BufferSinkContext *buf = ctx->priv;
  220. AVFilterFormats *formats = NULL;
  221. AVFilterChannelLayouts *layouts = NULL;
  222. if (!(formats = avfilter_make_format_list(buf->sample_fmts)))
  223. return AVERROR(ENOMEM);
  224. avfilter_set_common_sample_formats(ctx, formats);
  225. if (!(layouts = avfilter_make_format64_list(buf->channel_layouts)))
  226. return AVERROR(ENOMEM);
  227. ff_set_common_channel_layouts(ctx, layouts);
  228. ff_set_common_samplerates (ctx, ff_all_samplerates());
  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 */