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.

303 lines
9.2KB

  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/fifo.h"
  25. #include "avfilter.h"
  26. #include "buffersink.h"
  27. #include "internal.h"
  28. AVBufferSinkParams *av_buffersink_params_alloc(void)
  29. {
  30. static const int pixel_fmts[] = { -1 };
  31. AVBufferSinkParams *params = av_malloc(sizeof(AVBufferSinkParams));
  32. if (!params)
  33. return NULL;
  34. params->pixel_fmts = pixel_fmts;
  35. return params;
  36. }
  37. AVABufferSinkParams *av_abuffersink_params_alloc(void)
  38. {
  39. static const int sample_fmts[] = { -1 };
  40. static const int packing_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. params->packing_fmts = packing_fmts;
  48. return params;
  49. }
  50. typedef struct {
  51. AVFifoBuffer *fifo; ///< FIFO buffer of video frame references
  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. int *packing_fmts; ///< list of accepted packing formats, terminated by -1
  58. } BufferSinkContext;
  59. #define FIFO_INIT_SIZE 8
  60. static av_cold int common_init(AVFilterContext *ctx)
  61. {
  62. BufferSinkContext *buf = ctx->priv;
  63. buf->fifo = av_fifo_alloc(FIFO_INIT_SIZE*sizeof(AVFilterBufferRef *));
  64. if (!buf->fifo) {
  65. av_log(ctx, AV_LOG_ERROR, "Failed to allocate fifo\n");
  66. return AVERROR(ENOMEM);
  67. }
  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. if (av_fifo_space(buf->fifo) < sizeof(AVFilterBufferRef *)) {
  88. /* realloc fifo size */
  89. if (av_fifo_realloc2(buf->fifo, av_fifo_size(buf->fifo) * 2) < 0) {
  90. av_log(ctx, AV_LOG_ERROR,
  91. "Cannot buffer more frames. Consume some available frames "
  92. "before adding new ones.\n");
  93. return;
  94. }
  95. }
  96. /* cache frame */
  97. av_fifo_generic_write(buf->fifo,
  98. &inlink->cur_buf, sizeof(AVFilterBufferRef *), NULL);
  99. }
  100. int av_buffersink_get_buffer_ref(AVFilterContext *ctx,
  101. AVFilterBufferRef **bufref, int flags)
  102. {
  103. BufferSinkContext *buf = ctx->priv;
  104. AVFilterLink *inlink = ctx->inputs[0];
  105. int ret;
  106. *bufref = NULL;
  107. /* no picref available, fetch it from the filterchain */
  108. if (!av_fifo_size(buf->fifo)) {
  109. if ((ret = avfilter_request_frame(inlink)) < 0)
  110. return ret;
  111. }
  112. if (!av_fifo_size(buf->fifo))
  113. return AVERROR(EINVAL);
  114. if (flags & AV_BUFFERSINK_FLAG_PEEK)
  115. *bufref = *((AVFilterBufferRef **)av_fifo_peek2(buf->fifo, 0));
  116. else
  117. av_fifo_generic_read(buf->fifo, bufref, sizeof(*bufref), NULL);
  118. return 0;
  119. }
  120. int av_buffersink_poll_frame(AVFilterContext *ctx)
  121. {
  122. BufferSinkContext *buf = ctx->priv;
  123. AVFilterLink *inlink = ctx->inputs[0];
  124. return av_fifo_size(buf->fifo)/sizeof(AVFilterBufferRef *) + avfilter_poll_frame(inlink);
  125. }
  126. #if FF_API_OLD_VSINK_API
  127. int av_vsink_buffer_get_video_buffer_ref(AVFilterContext *ctx,
  128. AVFilterBufferRef **picref, int flags)
  129. {
  130. return av_buffersink_get_buffer_ref(ctx, picref, flags);
  131. }
  132. #endif
  133. #if CONFIG_BUFFERSINK_FILTER
  134. static av_cold int vsink_init(AVFilterContext *ctx, const char *args, void *opaque)
  135. {
  136. BufferSinkContext *buf = ctx->priv;
  137. av_unused AVBufferSinkParams *params;
  138. if (!opaque) {
  139. av_log(ctx, AV_LOG_WARNING,
  140. "No opaque field provided\n");
  141. buf->pixel_fmts = NULL;
  142. } else {
  143. #if FF_API_OLD_VSINK_API
  144. const int *pixel_fmts = (const enum PixelFormat *)opaque;
  145. #else
  146. params = (AVBufferSinkParams *)opaque;
  147. const int *pixel_fmts = params->pixel_fmts;
  148. #endif
  149. buf->pixel_fmts = ff_copy_int_list(pixel_fmts);
  150. if (!buf->pixel_fmts)
  151. return AVERROR(ENOMEM);
  152. }
  153. return common_init(ctx);
  154. }
  155. static av_cold void vsink_uninit(AVFilterContext *ctx)
  156. {
  157. BufferSinkContext *buf = ctx->priv;
  158. av_freep(&buf->pixel_fmts);
  159. return common_uninit(ctx);
  160. }
  161. static int vsink_query_formats(AVFilterContext *ctx)
  162. {
  163. BufferSinkContext *buf = ctx->priv;
  164. if (buf->pixel_fmts)
  165. avfilter_set_common_pixel_formats(ctx, avfilter_make_format_list(buf->pixel_fmts));
  166. else
  167. avfilter_default_query_formats(ctx);
  168. return 0;
  169. }
  170. AVFilter avfilter_vsink_buffersink = {
  171. .name = "buffersink",
  172. .description = NULL_IF_CONFIG_SMALL("Buffer video frames, and make them available to the end of the filter graph."),
  173. .priv_size = sizeof(BufferSinkContext),
  174. .init = vsink_init,
  175. .uninit = vsink_uninit,
  176. .query_formats = vsink_query_formats,
  177. .inputs = (const AVFilterPad[]) {{ .name = "default",
  178. .type = AVMEDIA_TYPE_VIDEO,
  179. .end_frame = end_frame,
  180. .min_perms = AV_PERM_READ, },
  181. { .name = NULL }},
  182. .outputs = (const AVFilterPad[]) {{ .name = NULL }},
  183. };
  184. #endif /* CONFIG_BUFFERSINK_FILTER */
  185. #if CONFIG_ABUFFERSINK_FILTER
  186. static void filter_samples(AVFilterLink *link, AVFilterBufferRef *samplesref)
  187. {
  188. end_frame(link);
  189. }
  190. static av_cold int asink_init(AVFilterContext *ctx, const char *args, void *opaque)
  191. {
  192. BufferSinkContext *buf = ctx->priv;
  193. AVABufferSinkParams *params;
  194. if (!opaque) {
  195. av_log(ctx, AV_LOG_ERROR,
  196. "No opaque field provided, an AVABufferSinkParams struct is required\n");
  197. return AVERROR(EINVAL);
  198. } else
  199. params = (AVABufferSinkParams *)opaque;
  200. buf->sample_fmts = ff_copy_int_list (params->sample_fmts);
  201. buf->channel_layouts = ff_copy_int64_list(params->channel_layouts);
  202. buf->packing_fmts = ff_copy_int_list (params->packing_fmts);
  203. if (!buf->sample_fmts || !buf->channel_layouts || !buf->sample_fmts) {
  204. av_freep(&buf->sample_fmts);
  205. av_freep(&buf->channel_layouts);
  206. av_freep(&buf->packing_fmts);
  207. return AVERROR(ENOMEM);
  208. }
  209. return common_init(ctx);
  210. }
  211. static av_cold void asink_uninit(AVFilterContext *ctx)
  212. {
  213. BufferSinkContext *buf = ctx->priv;
  214. av_freep(&buf->sample_fmts);
  215. av_freep(&buf->channel_layouts);
  216. av_freep(&buf->packing_fmts);
  217. return common_uninit(ctx);
  218. }
  219. static int asink_query_formats(AVFilterContext *ctx)
  220. {
  221. BufferSinkContext *buf = ctx->priv;
  222. AVFilterFormats *formats = NULL;
  223. if (!(formats = avfilter_make_format_list(buf->sample_fmts)))
  224. return AVERROR(ENOMEM);
  225. avfilter_set_common_sample_formats(ctx, formats);
  226. if (!(formats = avfilter_make_format64_list(buf->channel_layouts)))
  227. return AVERROR(ENOMEM);
  228. avfilter_set_common_channel_layouts(ctx, formats);
  229. if (!(formats = avfilter_make_format_list(buf->packing_fmts)))
  230. return AVERROR(ENOMEM);
  231. avfilter_set_common_packing_formats(ctx, formats);
  232. return 0;
  233. }
  234. AVFilter avfilter_asink_abuffersink = {
  235. .name = "abuffersink",
  236. .description = NULL_IF_CONFIG_SMALL("Buffer audio frames, and make them available to the end of the filter graph."),
  237. .init = asink_init,
  238. .uninit = asink_uninit,
  239. .priv_size = sizeof(BufferSinkContext),
  240. .query_formats = asink_query_formats,
  241. .inputs = (const AVFilterPad[]) {{ .name = "default",
  242. .type = AVMEDIA_TYPE_AUDIO,
  243. .filter_samples = filter_samples,
  244. .min_perms = AV_PERM_READ, },
  245. { .name = NULL }},
  246. .outputs = (const AVFilterPad[]) {{ .name = NULL }},
  247. };
  248. #endif /* CONFIG_ABUFFERSINK_FILTER */