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.

298 lines
8.9KB

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