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.

304 lines
9.1KB

  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. AVRational av_buffersink_get_frame_rate(AVFilterContext *ctx)
  122. {
  123. return ctx->inputs[0]->frame_rate;
  124. }
  125. int av_buffersink_poll_frame(AVFilterContext *ctx)
  126. {
  127. BufferSinkContext *buf = ctx->priv;
  128. AVFilterLink *inlink = ctx->inputs[0];
  129. return av_fifo_size(buf->fifo)/sizeof(AVFilterBufferRef *) + avfilter_poll_frame(inlink);
  130. }
  131. #if FF_API_OLD_VSINK_API
  132. int av_vsink_buffer_get_video_buffer_ref(AVFilterContext *ctx,
  133. AVFilterBufferRef **picref, int flags)
  134. {
  135. return av_buffersink_get_buffer_ref(ctx, picref, flags);
  136. }
  137. #endif
  138. #if CONFIG_BUFFERSINK_FILTER
  139. static av_cold int vsink_init(AVFilterContext *ctx, const char *args, void *opaque)
  140. {
  141. BufferSinkContext *buf = ctx->priv;
  142. av_unused AVBufferSinkParams *params;
  143. if (!opaque) {
  144. av_log(ctx, AV_LOG_WARNING,
  145. "No opaque field provided\n");
  146. buf->pixel_fmts = NULL;
  147. } else {
  148. #if FF_API_OLD_VSINK_API
  149. const int *pixel_fmts = (const enum PixelFormat *)opaque;
  150. #else
  151. params = (AVBufferSinkParams *)opaque;
  152. const int *pixel_fmts = params->pixel_fmts;
  153. #endif
  154. buf->pixel_fmts = ff_copy_int_list(pixel_fmts);
  155. if (!buf->pixel_fmts)
  156. return AVERROR(ENOMEM);
  157. }
  158. return common_init(ctx);
  159. }
  160. static av_cold void vsink_uninit(AVFilterContext *ctx)
  161. {
  162. BufferSinkContext *buf = ctx->priv;
  163. av_freep(&buf->pixel_fmts);
  164. common_uninit(ctx);
  165. }
  166. static int vsink_query_formats(AVFilterContext *ctx)
  167. {
  168. BufferSinkContext *buf = ctx->priv;
  169. if (buf->pixel_fmts)
  170. avfilter_set_common_pixel_formats(ctx, avfilter_make_format_list(buf->pixel_fmts));
  171. else
  172. avfilter_default_query_formats(ctx);
  173. return 0;
  174. }
  175. AVFilter avfilter_vsink_buffersink = {
  176. .name = "buffersink",
  177. .description = NULL_IF_CONFIG_SMALL("Buffer video frames, and make them available to the end of the filter graph."),
  178. .priv_size = sizeof(BufferSinkContext),
  179. .init = vsink_init,
  180. .uninit = vsink_uninit,
  181. .query_formats = vsink_query_formats,
  182. .inputs = (const AVFilterPad[]) {{ .name = "default",
  183. .type = AVMEDIA_TYPE_VIDEO,
  184. .end_frame = end_frame,
  185. .min_perms = AV_PERM_READ, },
  186. { .name = NULL }},
  187. .outputs = (const AVFilterPad[]) {{ .name = NULL }},
  188. };
  189. #endif /* CONFIG_BUFFERSINK_FILTER */
  190. #if CONFIG_ABUFFERSINK_FILTER
  191. static void filter_samples(AVFilterLink *link, AVFilterBufferRef *samplesref)
  192. {
  193. end_frame(link);
  194. }
  195. static av_cold int asink_init(AVFilterContext *ctx, const char *args, void *opaque)
  196. {
  197. BufferSinkContext *buf = ctx->priv;
  198. AVABufferSinkParams *params;
  199. if (!opaque) {
  200. av_log(ctx, AV_LOG_ERROR,
  201. "No opaque field provided, an AVABufferSinkParams struct is required\n");
  202. return AVERROR(EINVAL);
  203. } else
  204. params = (AVABufferSinkParams *)opaque;
  205. buf->sample_fmts = ff_copy_int_list (params->sample_fmts);
  206. buf->channel_layouts = ff_copy_int64_list(params->channel_layouts);
  207. if (!buf->sample_fmts || !buf->channel_layouts) {
  208. av_freep(&buf->sample_fmts);
  209. av_freep(&buf->channel_layouts);
  210. return AVERROR(ENOMEM);
  211. }
  212. return common_init(ctx);
  213. }
  214. static av_cold void asink_uninit(AVFilterContext *ctx)
  215. {
  216. BufferSinkContext *buf = ctx->priv;
  217. av_freep(&buf->sample_fmts);
  218. av_freep(&buf->channel_layouts);
  219. common_uninit(ctx);
  220. }
  221. static int asink_query_formats(AVFilterContext *ctx)
  222. {
  223. BufferSinkContext *buf = ctx->priv;
  224. AVFilterFormats *formats = NULL;
  225. AVFilterChannelLayouts *layouts = NULL;
  226. if (!(formats = avfilter_make_format_list(buf->sample_fmts)))
  227. return AVERROR(ENOMEM);
  228. avfilter_set_common_sample_formats(ctx, formats);
  229. if (!(layouts = avfilter_make_format64_list(buf->channel_layouts)))
  230. return AVERROR(ENOMEM);
  231. ff_set_common_channel_layouts(ctx, layouts);
  232. ff_set_common_samplerates (ctx, ff_all_samplerates());
  233. return 0;
  234. }
  235. AVFilter avfilter_asink_abuffersink = {
  236. .name = "abuffersink",
  237. .description = NULL_IF_CONFIG_SMALL("Buffer audio frames, and make them available to the end of the filter graph."),
  238. .init = asink_init,
  239. .uninit = asink_uninit,
  240. .priv_size = sizeof(BufferSinkContext),
  241. .query_formats = asink_query_formats,
  242. .inputs = (const AVFilterPad[]) {{ .name = "default",
  243. .type = AVMEDIA_TYPE_AUDIO,
  244. .filter_samples = filter_samples,
  245. .min_perms = AV_PERM_READ, },
  246. { .name = NULL }},
  247. .outputs = (const AVFilterPad[]) {{ .name = NULL }},
  248. };
  249. #endif /* CONFIG_ABUFFERSINK_FILTER */