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.

359 lines
11KB

  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 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 int 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 AVERROR(ENOMEM);
  95. }
  96. }
  97. /* cache frame */
  98. av_fifo_generic_write(buf->fifo,
  99. &inlink->cur_buf, sizeof(AVFilterBufferRef *), NULL);
  100. inlink->cur_buf = NULL;
  101. if (buf->warning_limit &&
  102. av_fifo_size(buf->fifo) / sizeof(AVFilterBufferRef *) >= buf->warning_limit) {
  103. av_log(ctx, AV_LOG_WARNING,
  104. "%d buffers queued in %s, something may be wrong.\n",
  105. buf->warning_limit,
  106. (char *)av_x_if_null(ctx->name, ctx->filter->name));
  107. buf->warning_limit *= 10;
  108. }
  109. return 0;
  110. }
  111. void av_buffersink_set_frame_size(AVFilterContext *ctx, unsigned frame_size)
  112. {
  113. AVFilterLink *inlink = ctx->inputs[0];
  114. inlink->min_samples = inlink->max_samples =
  115. inlink->partial_buf_size = frame_size;
  116. }
  117. int av_buffersink_get_buffer_ref(AVFilterContext *ctx,
  118. AVFilterBufferRef **bufref, int flags)
  119. {
  120. BufferSinkContext *buf = ctx->priv;
  121. AVFilterLink *inlink = ctx->inputs[0];
  122. int ret;
  123. *bufref = NULL;
  124. av_assert0( !strcmp(ctx->filter->name, "buffersink")
  125. || !strcmp(ctx->filter->name, "abuffersink")
  126. || !strcmp(ctx->filter->name, "ffbuffersink")
  127. || !strcmp(ctx->filter->name, "ffabuffersink"));
  128. /* no picref available, fetch it from the filterchain */
  129. if (!av_fifo_size(buf->fifo)) {
  130. if (flags & AV_BUFFERSINK_FLAG_NO_REQUEST)
  131. return AVERROR(EAGAIN);
  132. if ((ret = ff_request_frame(inlink)) < 0)
  133. return ret;
  134. }
  135. if (!av_fifo_size(buf->fifo))
  136. return AVERROR(EINVAL);
  137. if (flags & AV_BUFFERSINK_FLAG_PEEK)
  138. *bufref = *((AVFilterBufferRef **)av_fifo_peek2(buf->fifo, 0));
  139. else
  140. av_fifo_generic_read(buf->fifo, bufref, sizeof(*bufref), NULL);
  141. return 0;
  142. }
  143. AVRational av_buffersink_get_frame_rate(AVFilterContext *ctx)
  144. {
  145. av_assert0( !strcmp(ctx->filter->name, "buffersink")
  146. || !strcmp(ctx->filter->name, "ffbuffersink"));
  147. return ctx->inputs[0]->frame_rate;
  148. }
  149. int av_buffersink_poll_frame(AVFilterContext *ctx)
  150. {
  151. BufferSinkContext *buf = ctx->priv;
  152. AVFilterLink *inlink = ctx->inputs[0];
  153. av_assert0( !strcmp(ctx->filter->name, "buffersink")
  154. || !strcmp(ctx->filter->name, "abuffersink")
  155. || !strcmp(ctx->filter->name, "ffbuffersink")
  156. || !strcmp(ctx->filter->name, "ffabuffersink"));
  157. return av_fifo_size(buf->fifo)/sizeof(AVFilterBufferRef *) + ff_poll_frame(inlink);
  158. }
  159. #if CONFIG_BUFFERSINK_FILTER || CONFIG_FFBUFFERSINK_FILTER
  160. static av_cold int vsink_init(AVFilterContext *ctx, const char *args, void *opaque)
  161. {
  162. BufferSinkContext *buf = ctx->priv;
  163. AVBufferSinkParams *params = opaque;
  164. if (params && params->pixel_fmts) {
  165. const int *pixel_fmts = params->pixel_fmts;
  166. buf->pixel_fmts = ff_copy_int_list(pixel_fmts);
  167. if (!buf->pixel_fmts)
  168. return AVERROR(ENOMEM);
  169. }
  170. return common_init(ctx);
  171. }
  172. static av_cold void vsink_uninit(AVFilterContext *ctx)
  173. {
  174. BufferSinkContext *buf = ctx->priv;
  175. av_freep(&buf->pixel_fmts);
  176. common_uninit(ctx);
  177. }
  178. static int vsink_query_formats(AVFilterContext *ctx)
  179. {
  180. BufferSinkContext *buf = ctx->priv;
  181. if (buf->pixel_fmts)
  182. ff_set_common_formats(ctx, ff_make_format_list(buf->pixel_fmts));
  183. else
  184. ff_default_query_formats(ctx);
  185. return 0;
  186. }
  187. AVFilter avfilter_vsink_ffbuffersink = {
  188. .name = "ffbuffersink",
  189. .description = NULL_IF_CONFIG_SMALL("Buffer video frames, and make them available to the end of the filter graph."),
  190. .priv_size = sizeof(BufferSinkContext),
  191. .init_opaque = vsink_init,
  192. .uninit = vsink_uninit,
  193. .query_formats = vsink_query_formats,
  194. .inputs = (const AVFilterPad[]) {{ .name = "default",
  195. .type = AVMEDIA_TYPE_VIDEO,
  196. .end_frame = end_frame,
  197. .min_perms = AV_PERM_READ | AV_PERM_PRESERVE, },
  198. { .name = NULL }},
  199. .outputs = (const AVFilterPad[]) {{ .name = NULL }},
  200. };
  201. AVFilter avfilter_vsink_buffersink = {
  202. .name = "buffersink",
  203. .description = NULL_IF_CONFIG_SMALL("Buffer video frames, and make them available to the end of the filter graph."),
  204. .priv_size = sizeof(BufferSinkContext),
  205. .init_opaque = vsink_init,
  206. .uninit = vsink_uninit,
  207. .query_formats = vsink_query_formats,
  208. .inputs = (const AVFilterPad[]) {{ .name = "default",
  209. .type = AVMEDIA_TYPE_VIDEO,
  210. .end_frame = end_frame,
  211. .min_perms = AV_PERM_READ | AV_PERM_PRESERVE, },
  212. { .name = NULL }},
  213. .outputs = (const AVFilterPad[]) {{ .name = NULL }},
  214. };
  215. #endif /* CONFIG_BUFFERSINK_FILTER */
  216. #if CONFIG_ABUFFERSINK_FILTER || CONFIG_FFABUFFERSINK_FILTER
  217. static int filter_samples(AVFilterLink *link, AVFilterBufferRef *samplesref)
  218. {
  219. end_frame(link);
  220. return 0;
  221. }
  222. static av_cold int asink_init(AVFilterContext *ctx, const char *args, void *opaque)
  223. {
  224. BufferSinkContext *buf = ctx->priv;
  225. AVABufferSinkParams *params = opaque;
  226. if (params && params->sample_fmts) {
  227. buf->sample_fmts = ff_copy_int_list (params->sample_fmts);
  228. if (!buf->sample_fmts)
  229. goto fail_enomem;
  230. }
  231. if (params && params->channel_layouts) {
  232. buf->channel_layouts = ff_copy_int64_list(params->channel_layouts);
  233. if (!buf->channel_layouts)
  234. goto fail_enomem;
  235. }
  236. if (!common_init(ctx))
  237. return 0;
  238. fail_enomem:
  239. av_freep(&buf->sample_fmts);
  240. av_freep(&buf->channel_layouts);
  241. return AVERROR(ENOMEM);
  242. }
  243. static av_cold void asink_uninit(AVFilterContext *ctx)
  244. {
  245. BufferSinkContext *buf = ctx->priv;
  246. av_freep(&buf->sample_fmts);
  247. av_freep(&buf->channel_layouts);
  248. common_uninit(ctx);
  249. }
  250. static int asink_query_formats(AVFilterContext *ctx)
  251. {
  252. BufferSinkContext *buf = ctx->priv;
  253. AVFilterFormats *formats = NULL;
  254. AVFilterChannelLayouts *layouts = NULL;
  255. if (buf->sample_fmts) {
  256. if (!(formats = ff_make_format_list(buf->sample_fmts)))
  257. return AVERROR(ENOMEM);
  258. ff_set_common_formats(ctx, formats);
  259. }
  260. if (buf->channel_layouts) {
  261. if (!(layouts = avfilter_make_format64_list(buf->channel_layouts)))
  262. return AVERROR(ENOMEM);
  263. ff_set_common_channel_layouts(ctx, layouts);
  264. }
  265. return 0;
  266. }
  267. AVFilter avfilter_asink_ffabuffersink = {
  268. .name = "ffabuffersink",
  269. .description = NULL_IF_CONFIG_SMALL("Buffer audio frames, and make them available to the end of the filter graph."),
  270. .init_opaque = asink_init,
  271. .uninit = asink_uninit,
  272. .priv_size = sizeof(BufferSinkContext),
  273. .query_formats = asink_query_formats,
  274. .inputs = (const AVFilterPad[]) {{ .name = "default",
  275. .type = AVMEDIA_TYPE_AUDIO,
  276. .filter_samples = filter_samples,
  277. .min_perms = AV_PERM_READ | AV_PERM_PRESERVE, },
  278. { .name = NULL }},
  279. .outputs = (const AVFilterPad[]) {{ .name = NULL }},
  280. };
  281. AVFilter avfilter_asink_abuffersink = {
  282. .name = "abuffersink",
  283. .description = NULL_IF_CONFIG_SMALL("Buffer audio frames, and make them available to the end of the filter graph."),
  284. .init_opaque = asink_init,
  285. .uninit = asink_uninit,
  286. .priv_size = sizeof(BufferSinkContext),
  287. .query_formats = asink_query_formats,
  288. .inputs = (const AVFilterPad[]) {{ .name = "default",
  289. .type = AVMEDIA_TYPE_AUDIO,
  290. .filter_samples = filter_samples,
  291. .min_perms = AV_PERM_READ | AV_PERM_PRESERVE, },
  292. { .name = NULL }},
  293. .outputs = (const AVFilterPad[]) {{ .name = NULL }},
  294. };
  295. #endif /* CONFIG_ABUFFERSINK_FILTER */