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.

318 lines
9.4KB

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