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.

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