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.

345 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/channel_layout.h"
  26. #include "libavutil/common.h"
  27. #include "libavutil/internal.h"
  28. #include "libavutil/opt.h"
  29. #define FF_INTERNAL_FIELDS 1
  30. #include "framequeue.h"
  31. #include "audio.h"
  32. #include "avfilter.h"
  33. #include "buffersink.h"
  34. #include "filters.h"
  35. #include "internal.h"
  36. typedef struct BufferSinkContext {
  37. const AVClass *class;
  38. unsigned warning_limit;
  39. /* only used for video */
  40. enum AVPixelFormat *pixel_fmts; ///< list of accepted pixel formats, must be terminated with -1
  41. int pixel_fmts_size;
  42. /* only used for audio */
  43. enum AVSampleFormat *sample_fmts; ///< list of accepted sample formats, terminated by AV_SAMPLE_FMT_NONE
  44. int sample_fmts_size;
  45. int64_t *channel_layouts; ///< list of accepted channel layouts, terminated by -1
  46. int channel_layouts_size;
  47. int *channel_counts; ///< list of accepted channel counts, terminated by -1
  48. int channel_counts_size;
  49. int all_channel_counts;
  50. int *sample_rates; ///< list of accepted sample rates, terminated by -1
  51. int sample_rates_size;
  52. AVFrame *peeked_frame;
  53. } BufferSinkContext;
  54. #define NB_ITEMS(list) (list ## _size / sizeof(*list))
  55. int attribute_align_arg av_buffersink_get_frame(AVFilterContext *ctx, AVFrame *frame)
  56. {
  57. return av_buffersink_get_frame_flags(ctx, frame, 0);
  58. }
  59. static int return_or_keep_frame(BufferSinkContext *buf, AVFrame *out, AVFrame *in, int flags)
  60. {
  61. if ((flags & AV_BUFFERSINK_FLAG_PEEK)) {
  62. buf->peeked_frame = in;
  63. return out ? av_frame_ref(out, in) : 0;
  64. } else {
  65. av_assert1(out);
  66. buf->peeked_frame = NULL;
  67. av_frame_move_ref(out, in);
  68. av_frame_free(&in);
  69. return 0;
  70. }
  71. }
  72. static int get_frame_internal(AVFilterContext *ctx, AVFrame *frame, int flags, int samples)
  73. {
  74. BufferSinkContext *buf = ctx->priv;
  75. AVFilterLink *inlink = ctx->inputs[0];
  76. int status, ret;
  77. AVFrame *cur_frame;
  78. int64_t pts;
  79. if (buf->peeked_frame)
  80. return return_or_keep_frame(buf, frame, buf->peeked_frame, flags);
  81. while (1) {
  82. ret = samples ? ff_inlink_consume_samples(inlink, samples, samples, &cur_frame) :
  83. ff_inlink_consume_frame(inlink, &cur_frame);
  84. if (ret < 0) {
  85. return ret;
  86. } else if (ret) {
  87. /* TODO return the frame instead of copying it */
  88. return return_or_keep_frame(buf, frame, cur_frame, flags);
  89. } else if (ff_inlink_acknowledge_status(inlink, &status, &pts)) {
  90. return status;
  91. } else if ((flags & AV_BUFFERSINK_FLAG_NO_REQUEST)) {
  92. return AVERROR(EAGAIN);
  93. } else if (inlink->frame_wanted_out) {
  94. ret = ff_filter_graph_run_once(ctx->graph);
  95. if (ret < 0)
  96. return ret;
  97. } else {
  98. ff_inlink_request_frame(inlink);
  99. }
  100. }
  101. }
  102. int attribute_align_arg av_buffersink_get_frame_flags(AVFilterContext *ctx, AVFrame *frame, int flags)
  103. {
  104. return get_frame_internal(ctx, frame, flags, ctx->inputs[0]->min_samples);
  105. }
  106. int attribute_align_arg av_buffersink_get_samples(AVFilterContext *ctx,
  107. AVFrame *frame, int nb_samples)
  108. {
  109. return get_frame_internal(ctx, frame, 0, nb_samples);
  110. }
  111. #if FF_API_NEXT
  112. AVBufferSinkParams *av_buffersink_params_alloc(void)
  113. {
  114. static const int pixel_fmts[] = { AV_PIX_FMT_NONE };
  115. AVBufferSinkParams *params = av_malloc(sizeof(AVBufferSinkParams));
  116. if (!params)
  117. return NULL;
  118. params->pixel_fmts = pixel_fmts;
  119. return params;
  120. }
  121. AVABufferSinkParams *av_abuffersink_params_alloc(void)
  122. {
  123. AVABufferSinkParams *params = av_mallocz(sizeof(AVABufferSinkParams));
  124. if (!params)
  125. return NULL;
  126. return params;
  127. }
  128. #endif
  129. static av_cold int common_init(AVFilterContext *ctx)
  130. {
  131. BufferSinkContext *buf = ctx->priv;
  132. buf->warning_limit = 100;
  133. return 0;
  134. }
  135. static int activate(AVFilterContext *ctx)
  136. {
  137. BufferSinkContext *buf = ctx->priv;
  138. if (buf->warning_limit &&
  139. ff_framequeue_queued_frames(&ctx->inputs[0]->fifo) >= buf->warning_limit) {
  140. av_log(ctx, AV_LOG_WARNING,
  141. "%d buffers queued in %s, something may be wrong.\n",
  142. buf->warning_limit,
  143. (char *)av_x_if_null(ctx->name, ctx->filter->name));
  144. buf->warning_limit *= 10;
  145. }
  146. /* The frame is queued, the rest is up to get_frame_internal */
  147. return 0;
  148. }
  149. void av_buffersink_set_frame_size(AVFilterContext *ctx, unsigned frame_size)
  150. {
  151. AVFilterLink *inlink = ctx->inputs[0];
  152. inlink->min_samples = inlink->max_samples =
  153. inlink->partial_buf_size = frame_size;
  154. }
  155. #define MAKE_AVFILTERLINK_ACCESSOR(type, field) \
  156. type av_buffersink_get_##field(const AVFilterContext *ctx) { \
  157. av_assert0(ctx->filter->activate == activate); \
  158. return ctx->inputs[0]->field; \
  159. }
  160. MAKE_AVFILTERLINK_ACCESSOR(enum AVMediaType , type )
  161. MAKE_AVFILTERLINK_ACCESSOR(AVRational , time_base )
  162. MAKE_AVFILTERLINK_ACCESSOR(int , format )
  163. MAKE_AVFILTERLINK_ACCESSOR(AVRational , frame_rate )
  164. MAKE_AVFILTERLINK_ACCESSOR(int , w )
  165. MAKE_AVFILTERLINK_ACCESSOR(int , h )
  166. MAKE_AVFILTERLINK_ACCESSOR(AVRational , sample_aspect_ratio)
  167. MAKE_AVFILTERLINK_ACCESSOR(int , channels )
  168. MAKE_AVFILTERLINK_ACCESSOR(uint64_t , channel_layout )
  169. MAKE_AVFILTERLINK_ACCESSOR(int , sample_rate )
  170. MAKE_AVFILTERLINK_ACCESSOR(AVBufferRef * , hw_frames_ctx )
  171. #define CHECK_LIST_SIZE(field) \
  172. if (buf->field ## _size % sizeof(*buf->field)) { \
  173. av_log(ctx, AV_LOG_ERROR, "Invalid size for " #field ": %d, " \
  174. "should be multiple of %d\n", \
  175. buf->field ## _size, (int)sizeof(*buf->field)); \
  176. return AVERROR(EINVAL); \
  177. }
  178. static int vsink_query_formats(AVFilterContext *ctx)
  179. {
  180. BufferSinkContext *buf = ctx->priv;
  181. AVFilterFormats *formats = NULL;
  182. unsigned i;
  183. int ret;
  184. CHECK_LIST_SIZE(pixel_fmts)
  185. if (buf->pixel_fmts_size) {
  186. for (i = 0; i < NB_ITEMS(buf->pixel_fmts); i++)
  187. if ((ret = ff_add_format(&formats, buf->pixel_fmts[i])) < 0)
  188. return ret;
  189. if ((ret = ff_set_common_formats(ctx, formats)) < 0)
  190. return ret;
  191. } else {
  192. if ((ret = ff_default_query_formats(ctx)) < 0)
  193. return ret;
  194. }
  195. return 0;
  196. }
  197. static int asink_query_formats(AVFilterContext *ctx)
  198. {
  199. BufferSinkContext *buf = ctx->priv;
  200. AVFilterFormats *formats = NULL;
  201. AVFilterChannelLayouts *layouts = NULL;
  202. unsigned i;
  203. int ret;
  204. CHECK_LIST_SIZE(sample_fmts)
  205. CHECK_LIST_SIZE(sample_rates)
  206. CHECK_LIST_SIZE(channel_layouts)
  207. CHECK_LIST_SIZE(channel_counts)
  208. if (buf->sample_fmts_size) {
  209. for (i = 0; i < NB_ITEMS(buf->sample_fmts); i++)
  210. if ((ret = ff_add_format(&formats, buf->sample_fmts[i])) < 0)
  211. return ret;
  212. if ((ret = ff_set_common_formats(ctx, formats)) < 0)
  213. return ret;
  214. }
  215. if (buf->channel_layouts_size || buf->channel_counts_size ||
  216. buf->all_channel_counts) {
  217. for (i = 0; i < NB_ITEMS(buf->channel_layouts); i++)
  218. if ((ret = ff_add_channel_layout(&layouts, buf->channel_layouts[i])) < 0)
  219. return ret;
  220. for (i = 0; i < NB_ITEMS(buf->channel_counts); i++)
  221. if ((ret = ff_add_channel_layout(&layouts, FF_COUNT2LAYOUT(buf->channel_counts[i]))) < 0)
  222. return ret;
  223. if (buf->all_channel_counts) {
  224. if (layouts)
  225. av_log(ctx, AV_LOG_WARNING,
  226. "Conflicting all_channel_counts and list in options\n");
  227. else if (!(layouts = ff_all_channel_counts()))
  228. return AVERROR(ENOMEM);
  229. }
  230. if ((ret = ff_set_common_channel_layouts(ctx, layouts)) < 0)
  231. return ret;
  232. }
  233. if (buf->sample_rates_size) {
  234. formats = NULL;
  235. for (i = 0; i < NB_ITEMS(buf->sample_rates); i++)
  236. if ((ret = ff_add_format(&formats, buf->sample_rates[i])) < 0)
  237. return ret;
  238. if ((ret = ff_set_common_samplerates(ctx, formats)) < 0)
  239. return ret;
  240. }
  241. return 0;
  242. }
  243. #define OFFSET(x) offsetof(BufferSinkContext, x)
  244. #define FLAGS AV_OPT_FLAG_FILTERING_PARAM|AV_OPT_FLAG_VIDEO_PARAM
  245. static const AVOption buffersink_options[] = {
  246. { "pix_fmts", "set the supported pixel formats", OFFSET(pixel_fmts), AV_OPT_TYPE_BINARY, .flags = FLAGS },
  247. { NULL },
  248. };
  249. #undef FLAGS
  250. #define FLAGS AV_OPT_FLAG_FILTERING_PARAM|AV_OPT_FLAG_AUDIO_PARAM
  251. static const AVOption abuffersink_options[] = {
  252. { "sample_fmts", "set the supported sample formats", OFFSET(sample_fmts), AV_OPT_TYPE_BINARY, .flags = FLAGS },
  253. { "sample_rates", "set the supported sample rates", OFFSET(sample_rates), AV_OPT_TYPE_BINARY, .flags = FLAGS },
  254. { "channel_layouts", "set the supported channel layouts", OFFSET(channel_layouts), AV_OPT_TYPE_BINARY, .flags = FLAGS },
  255. { "channel_counts", "set the supported channel counts", OFFSET(channel_counts), AV_OPT_TYPE_BINARY, .flags = FLAGS },
  256. { "all_channel_counts", "accept all channel counts", OFFSET(all_channel_counts), AV_OPT_TYPE_BOOL, {.i64 = 0}, 0, 1, FLAGS },
  257. { NULL },
  258. };
  259. #undef FLAGS
  260. AVFILTER_DEFINE_CLASS(buffersink);
  261. AVFILTER_DEFINE_CLASS(abuffersink);
  262. static const AVFilterPad avfilter_vsink_buffer_inputs[] = {
  263. {
  264. .name = "default",
  265. .type = AVMEDIA_TYPE_VIDEO,
  266. },
  267. { NULL }
  268. };
  269. AVFilter ff_vsink_buffer = {
  270. .name = "buffersink",
  271. .description = NULL_IF_CONFIG_SMALL("Buffer video frames, and make them available to the end of the filter graph."),
  272. .priv_size = sizeof(BufferSinkContext),
  273. .priv_class = &buffersink_class,
  274. .init = common_init,
  275. .query_formats = vsink_query_formats,
  276. .activate = activate,
  277. .inputs = avfilter_vsink_buffer_inputs,
  278. .outputs = NULL,
  279. };
  280. static const AVFilterPad avfilter_asink_abuffer_inputs[] = {
  281. {
  282. .name = "default",
  283. .type = AVMEDIA_TYPE_AUDIO,
  284. },
  285. { NULL }
  286. };
  287. AVFilter ff_asink_abuffer = {
  288. .name = "abuffersink",
  289. .description = NULL_IF_CONFIG_SMALL("Buffer audio frames, and make them available to the end of the filter graph."),
  290. .priv_class = &abuffersink_class,
  291. .priv_size = sizeof(BufferSinkContext),
  292. .init = common_init,
  293. .query_formats = asink_query_formats,
  294. .activate = activate,
  295. .inputs = avfilter_asink_abuffer_inputs,
  296. .outputs = NULL,
  297. };