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.

374 lines
12KB

  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. AVBufferSinkParams *av_buffersink_params_alloc(void)
  112. {
  113. static const int pixel_fmts[] = { AV_PIX_FMT_NONE };
  114. AVBufferSinkParams *params = av_malloc(sizeof(AVBufferSinkParams));
  115. if (!params)
  116. return NULL;
  117. params->pixel_fmts = pixel_fmts;
  118. return params;
  119. }
  120. AVABufferSinkParams *av_abuffersink_params_alloc(void)
  121. {
  122. AVABufferSinkParams *params = av_mallocz(sizeof(AVABufferSinkParams));
  123. if (!params)
  124. return NULL;
  125. return params;
  126. }
  127. static av_cold int common_init(AVFilterContext *ctx)
  128. {
  129. BufferSinkContext *buf = ctx->priv;
  130. buf->warning_limit = 100;
  131. return 0;
  132. }
  133. static int activate(AVFilterContext *ctx)
  134. {
  135. BufferSinkContext *buf = ctx->priv;
  136. if (buf->warning_limit &&
  137. ff_framequeue_queued_frames(&ctx->inputs[0]->fifo) >= buf->warning_limit) {
  138. av_log(ctx, AV_LOG_WARNING,
  139. "%d buffers queued in %s, something may be wrong.\n",
  140. buf->warning_limit,
  141. (char *)av_x_if_null(ctx->name, ctx->filter->name));
  142. buf->warning_limit *= 10;
  143. }
  144. /* The frame is queued, the rest is up to get_frame_internal */
  145. return 0;
  146. }
  147. void av_buffersink_set_frame_size(AVFilterContext *ctx, unsigned frame_size)
  148. {
  149. AVFilterLink *inlink = ctx->inputs[0];
  150. inlink->min_samples = inlink->max_samples =
  151. inlink->partial_buf_size = frame_size;
  152. }
  153. #define MAKE_AVFILTERLINK_ACCESSOR(type, field) \
  154. type av_buffersink_get_##field(const AVFilterContext *ctx) { \
  155. av_assert0(ctx->filter->activate == activate); \
  156. return ctx->inputs[0]->field; \
  157. }
  158. MAKE_AVFILTERLINK_ACCESSOR(enum AVMediaType , type )
  159. MAKE_AVFILTERLINK_ACCESSOR(AVRational , time_base )
  160. MAKE_AVFILTERLINK_ACCESSOR(int , format )
  161. MAKE_AVFILTERLINK_ACCESSOR(AVRational , frame_rate )
  162. MAKE_AVFILTERLINK_ACCESSOR(int , w )
  163. MAKE_AVFILTERLINK_ACCESSOR(int , h )
  164. MAKE_AVFILTERLINK_ACCESSOR(AVRational , sample_aspect_ratio)
  165. MAKE_AVFILTERLINK_ACCESSOR(int , channels )
  166. MAKE_AVFILTERLINK_ACCESSOR(uint64_t , channel_layout )
  167. MAKE_AVFILTERLINK_ACCESSOR(int , sample_rate )
  168. MAKE_AVFILTERLINK_ACCESSOR(AVBufferRef * , hw_frames_ctx )
  169. static av_cold int vsink_init(AVFilterContext *ctx, void *opaque)
  170. {
  171. BufferSinkContext *buf = ctx->priv;
  172. AVBufferSinkParams *params = opaque;
  173. int ret;
  174. if (params) {
  175. if ((ret = av_opt_set_int_list(buf, "pix_fmts", params->pixel_fmts, AV_PIX_FMT_NONE, 0)) < 0)
  176. return ret;
  177. }
  178. return common_init(ctx);
  179. }
  180. #define CHECK_LIST_SIZE(field) \
  181. if (buf->field ## _size % sizeof(*buf->field)) { \
  182. av_log(ctx, AV_LOG_ERROR, "Invalid size for " #field ": %d, " \
  183. "should be multiple of %d\n", \
  184. buf->field ## _size, (int)sizeof(*buf->field)); \
  185. return AVERROR(EINVAL); \
  186. }
  187. static int vsink_query_formats(AVFilterContext *ctx)
  188. {
  189. BufferSinkContext *buf = ctx->priv;
  190. AVFilterFormats *formats = NULL;
  191. unsigned i;
  192. int ret;
  193. CHECK_LIST_SIZE(pixel_fmts)
  194. if (buf->pixel_fmts_size) {
  195. for (i = 0; i < NB_ITEMS(buf->pixel_fmts); i++)
  196. if ((ret = ff_add_format(&formats, buf->pixel_fmts[i])) < 0)
  197. return ret;
  198. if ((ret = ff_set_common_formats(ctx, formats)) < 0)
  199. return ret;
  200. } else {
  201. if ((ret = ff_default_query_formats(ctx)) < 0)
  202. return ret;
  203. }
  204. return 0;
  205. }
  206. static av_cold int asink_init(AVFilterContext *ctx, void *opaque)
  207. {
  208. BufferSinkContext *buf = ctx->priv;
  209. AVABufferSinkParams *params = opaque;
  210. int ret;
  211. if (params) {
  212. if ((ret = av_opt_set_int_list(buf, "sample_fmts", params->sample_fmts, AV_SAMPLE_FMT_NONE, 0)) < 0 ||
  213. (ret = av_opt_set_int_list(buf, "sample_rates", params->sample_rates, -1, 0)) < 0 ||
  214. (ret = av_opt_set_int_list(buf, "channel_layouts", params->channel_layouts, -1, 0)) < 0 ||
  215. (ret = av_opt_set_int_list(buf, "channel_counts", params->channel_counts, -1, 0)) < 0 ||
  216. (ret = av_opt_set_int(buf, "all_channel_counts", params->all_channel_counts, 0)) < 0)
  217. return ret;
  218. }
  219. return common_init(ctx);
  220. }
  221. static int asink_query_formats(AVFilterContext *ctx)
  222. {
  223. BufferSinkContext *buf = ctx->priv;
  224. AVFilterFormats *formats = NULL;
  225. AVFilterChannelLayouts *layouts = NULL;
  226. unsigned i;
  227. int ret;
  228. CHECK_LIST_SIZE(sample_fmts)
  229. CHECK_LIST_SIZE(sample_rates)
  230. CHECK_LIST_SIZE(channel_layouts)
  231. CHECK_LIST_SIZE(channel_counts)
  232. if (buf->sample_fmts_size) {
  233. for (i = 0; i < NB_ITEMS(buf->sample_fmts); i++)
  234. if ((ret = ff_add_format(&formats, buf->sample_fmts[i])) < 0)
  235. return ret;
  236. if ((ret = ff_set_common_formats(ctx, formats)) < 0)
  237. return ret;
  238. }
  239. if (buf->channel_layouts_size || buf->channel_counts_size ||
  240. buf->all_channel_counts) {
  241. for (i = 0; i < NB_ITEMS(buf->channel_layouts); i++)
  242. if ((ret = ff_add_channel_layout(&layouts, buf->channel_layouts[i])) < 0)
  243. return ret;
  244. for (i = 0; i < NB_ITEMS(buf->channel_counts); i++)
  245. if ((ret = ff_add_channel_layout(&layouts, FF_COUNT2LAYOUT(buf->channel_counts[i]))) < 0)
  246. return ret;
  247. if (buf->all_channel_counts) {
  248. if (layouts)
  249. av_log(ctx, AV_LOG_WARNING,
  250. "Conflicting all_channel_counts and list in options\n");
  251. else if (!(layouts = ff_all_channel_counts()))
  252. return AVERROR(ENOMEM);
  253. }
  254. if ((ret = ff_set_common_channel_layouts(ctx, layouts)) < 0)
  255. return ret;
  256. }
  257. if (buf->sample_rates_size) {
  258. formats = NULL;
  259. for (i = 0; i < NB_ITEMS(buf->sample_rates); i++)
  260. if ((ret = ff_add_format(&formats, buf->sample_rates[i])) < 0)
  261. return ret;
  262. if ((ret = ff_set_common_samplerates(ctx, formats)) < 0)
  263. return ret;
  264. }
  265. return 0;
  266. }
  267. #define OFFSET(x) offsetof(BufferSinkContext, x)
  268. #define FLAGS AV_OPT_FLAG_FILTERING_PARAM|AV_OPT_FLAG_VIDEO_PARAM
  269. static const AVOption buffersink_options[] = {
  270. { "pix_fmts", "set the supported pixel formats", OFFSET(pixel_fmts), AV_OPT_TYPE_BINARY, .flags = FLAGS },
  271. { NULL },
  272. };
  273. #undef FLAGS
  274. #define FLAGS AV_OPT_FLAG_FILTERING_PARAM|AV_OPT_FLAG_AUDIO_PARAM
  275. static const AVOption abuffersink_options[] = {
  276. { "sample_fmts", "set the supported sample formats", OFFSET(sample_fmts), AV_OPT_TYPE_BINARY, .flags = FLAGS },
  277. { "sample_rates", "set the supported sample rates", OFFSET(sample_rates), AV_OPT_TYPE_BINARY, .flags = FLAGS },
  278. { "channel_layouts", "set the supported channel layouts", OFFSET(channel_layouts), AV_OPT_TYPE_BINARY, .flags = FLAGS },
  279. { "channel_counts", "set the supported channel counts", OFFSET(channel_counts), AV_OPT_TYPE_BINARY, .flags = FLAGS },
  280. { "all_channel_counts", "accept all channel counts", OFFSET(all_channel_counts), AV_OPT_TYPE_BOOL, {.i64 = 0}, 0, 1, FLAGS },
  281. { NULL },
  282. };
  283. #undef FLAGS
  284. AVFILTER_DEFINE_CLASS(buffersink);
  285. AVFILTER_DEFINE_CLASS(abuffersink);
  286. static const AVFilterPad avfilter_vsink_buffer_inputs[] = {
  287. {
  288. .name = "default",
  289. .type = AVMEDIA_TYPE_VIDEO,
  290. },
  291. { NULL }
  292. };
  293. AVFilter ff_vsink_buffer = {
  294. .name = "buffersink",
  295. .description = NULL_IF_CONFIG_SMALL("Buffer video frames, and make them available to the end of the filter graph."),
  296. .priv_size = sizeof(BufferSinkContext),
  297. .priv_class = &buffersink_class,
  298. .init_opaque = vsink_init,
  299. .query_formats = vsink_query_formats,
  300. .activate = activate,
  301. .inputs = avfilter_vsink_buffer_inputs,
  302. .outputs = NULL,
  303. };
  304. static const AVFilterPad avfilter_asink_abuffer_inputs[] = {
  305. {
  306. .name = "default",
  307. .type = AVMEDIA_TYPE_AUDIO,
  308. },
  309. { NULL }
  310. };
  311. AVFilter ff_asink_abuffer = {
  312. .name = "abuffersink",
  313. .description = NULL_IF_CONFIG_SMALL("Buffer audio frames, and make them available to the end of the filter graph."),
  314. .priv_class = &abuffersink_class,
  315. .priv_size = sizeof(BufferSinkContext),
  316. .init_opaque = asink_init,
  317. .query_formats = asink_query_formats,
  318. .activate = activate,
  319. .inputs = avfilter_asink_abuffer_inputs,
  320. .outputs = NULL,
  321. };