buffersink.c 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454
  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/audio_fifo.h"
  25. #include "libavutil/avassert.h"
  26. #include "libavutil/channel_layout.h"
  27. #include "libavutil/common.h"
  28. #include "libavutil/internal.h"
  29. #include "libavutil/mathematics.h"
  30. #include "libavutil/opt.h"
  31. #include "audio.h"
  32. #include "avfilter.h"
  33. #include "buffersink.h"
  34. #include "internal.h"
  35. typedef struct BufferSinkContext {
  36. const AVClass *class;
  37. AVFifoBuffer *fifo; ///< FIFO buffer of video frame references
  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. /* only used for compat API */
  53. AVAudioFifo *audio_fifo; ///< FIFO for audio samples
  54. int64_t next_pts; ///< interpolating audio pts
  55. } BufferSinkContext;
  56. #define NB_ITEMS(list) (list ## _size / sizeof(*list))
  57. #define FIFO_INIT_SIZE 8
  58. #define FIFO_INIT_ELEMENT_SIZE sizeof(void *)
  59. static av_cold void uninit(AVFilterContext *ctx)
  60. {
  61. BufferSinkContext *sink = ctx->priv;
  62. AVFrame *frame;
  63. if (sink->audio_fifo)
  64. av_audio_fifo_free(sink->audio_fifo);
  65. if (sink->fifo) {
  66. while (av_fifo_size(sink->fifo) >= FIFO_INIT_ELEMENT_SIZE) {
  67. av_fifo_generic_read(sink->fifo, &frame, sizeof(frame), NULL);
  68. av_frame_free(&frame);
  69. }
  70. av_fifo_freep(&sink->fifo);
  71. }
  72. }
  73. static int add_buffer_ref(AVFilterContext *ctx, AVFrame *ref)
  74. {
  75. BufferSinkContext *buf = ctx->priv;
  76. if (av_fifo_space(buf->fifo) < FIFO_INIT_ELEMENT_SIZE) {
  77. /* realloc fifo size */
  78. if (av_fifo_realloc2(buf->fifo, av_fifo_size(buf->fifo) * 2) < 0) {
  79. av_log(ctx, AV_LOG_ERROR,
  80. "Cannot buffer more frames. Consume some available frames "
  81. "before adding new ones.\n");
  82. return AVERROR(ENOMEM);
  83. }
  84. }
  85. /* cache frame */
  86. av_fifo_generic_write(buf->fifo, &ref, FIFO_INIT_ELEMENT_SIZE, NULL);
  87. return 0;
  88. }
  89. static int filter_frame(AVFilterLink *link, AVFrame *frame)
  90. {
  91. AVFilterContext *ctx = link->dst;
  92. BufferSinkContext *buf = link->dst->priv;
  93. int ret;
  94. if ((ret = add_buffer_ref(ctx, frame)) < 0)
  95. return ret;
  96. if (buf->warning_limit &&
  97. av_fifo_size(buf->fifo) / FIFO_INIT_ELEMENT_SIZE >= buf->warning_limit) {
  98. av_log(ctx, AV_LOG_WARNING,
  99. "%d buffers queued in %s, something may be wrong.\n",
  100. buf->warning_limit,
  101. (char *)av_x_if_null(ctx->name, ctx->filter->name));
  102. buf->warning_limit *= 10;
  103. }
  104. return 0;
  105. }
  106. int attribute_align_arg av_buffersink_get_frame(AVFilterContext *ctx, AVFrame *frame)
  107. {
  108. return av_buffersink_get_frame_flags(ctx, frame, 0);
  109. }
  110. int attribute_align_arg av_buffersink_get_frame_flags(AVFilterContext *ctx, AVFrame *frame, int flags)
  111. {
  112. BufferSinkContext *buf = ctx->priv;
  113. AVFilterLink *inlink = ctx->inputs[0];
  114. int ret;
  115. AVFrame *cur_frame;
  116. /* no picref available, fetch it from the filterchain */
  117. while (!av_fifo_size(buf->fifo)) {
  118. if (inlink->status)
  119. return inlink->status;
  120. if (flags & AV_BUFFERSINK_FLAG_NO_REQUEST)
  121. return AVERROR(EAGAIN);
  122. if ((ret = ff_request_frame(inlink)) < 0)
  123. return ret;
  124. while (inlink->frame_wanted_out) {
  125. ret = ff_filter_graph_run_once(ctx->graph);
  126. if (ret < 0)
  127. return ret;
  128. }
  129. }
  130. if (flags & AV_BUFFERSINK_FLAG_PEEK) {
  131. cur_frame = *((AVFrame **)av_fifo_peek2(buf->fifo, 0));
  132. if ((ret = av_frame_ref(frame, cur_frame)) < 0)
  133. return ret;
  134. } else {
  135. av_fifo_generic_read(buf->fifo, &cur_frame, sizeof(cur_frame), NULL);
  136. av_frame_move_ref(frame, cur_frame);
  137. av_frame_free(&cur_frame);
  138. }
  139. return 0;
  140. }
  141. static int read_from_fifo(AVFilterContext *ctx, AVFrame *frame,
  142. int nb_samples)
  143. {
  144. BufferSinkContext *s = ctx->priv;
  145. AVFilterLink *link = ctx->inputs[0];
  146. AVFrame *tmp;
  147. if (!(tmp = ff_get_audio_buffer(link, nb_samples)))
  148. return AVERROR(ENOMEM);
  149. av_audio_fifo_read(s->audio_fifo, (void**)tmp->extended_data, nb_samples);
  150. tmp->pts = s->next_pts;
  151. if (s->next_pts != AV_NOPTS_VALUE)
  152. s->next_pts += av_rescale_q(nb_samples, (AVRational){1, link->sample_rate},
  153. link->time_base);
  154. av_frame_move_ref(frame, tmp);
  155. av_frame_free(&tmp);
  156. return 0;
  157. }
  158. int attribute_align_arg av_buffersink_get_samples(AVFilterContext *ctx,
  159. AVFrame *frame, int nb_samples)
  160. {
  161. BufferSinkContext *s = ctx->priv;
  162. AVFilterLink *link = ctx->inputs[0];
  163. AVFrame *cur_frame;
  164. int ret = 0;
  165. if (!s->audio_fifo) {
  166. int nb_channels = link->channels;
  167. if (!(s->audio_fifo = av_audio_fifo_alloc(link->format, nb_channels, nb_samples)))
  168. return AVERROR(ENOMEM);
  169. }
  170. while (ret >= 0) {
  171. if (av_audio_fifo_size(s->audio_fifo) >= nb_samples)
  172. return read_from_fifo(ctx, frame, nb_samples);
  173. if (!(cur_frame = av_frame_alloc()))
  174. return AVERROR(ENOMEM);
  175. ret = av_buffersink_get_frame_flags(ctx, cur_frame, 0);
  176. if (ret == AVERROR_EOF && av_audio_fifo_size(s->audio_fifo)) {
  177. av_frame_free(&cur_frame);
  178. return read_from_fifo(ctx, frame, av_audio_fifo_size(s->audio_fifo));
  179. } else if (ret < 0) {
  180. av_frame_free(&cur_frame);
  181. return ret;
  182. }
  183. if (cur_frame->pts != AV_NOPTS_VALUE) {
  184. s->next_pts = cur_frame->pts -
  185. av_rescale_q(av_audio_fifo_size(s->audio_fifo),
  186. (AVRational){ 1, link->sample_rate },
  187. link->time_base);
  188. }
  189. ret = av_audio_fifo_write(s->audio_fifo, (void**)cur_frame->extended_data,
  190. cur_frame->nb_samples);
  191. av_frame_free(&cur_frame);
  192. }
  193. return ret;
  194. }
  195. AVBufferSinkParams *av_buffersink_params_alloc(void)
  196. {
  197. static const int pixel_fmts[] = { AV_PIX_FMT_NONE };
  198. AVBufferSinkParams *params = av_malloc(sizeof(AVBufferSinkParams));
  199. if (!params)
  200. return NULL;
  201. params->pixel_fmts = pixel_fmts;
  202. return params;
  203. }
  204. AVABufferSinkParams *av_abuffersink_params_alloc(void)
  205. {
  206. AVABufferSinkParams *params = av_mallocz(sizeof(AVABufferSinkParams));
  207. if (!params)
  208. return NULL;
  209. return params;
  210. }
  211. static av_cold int common_init(AVFilterContext *ctx)
  212. {
  213. BufferSinkContext *buf = ctx->priv;
  214. buf->fifo = av_fifo_alloc_array(FIFO_INIT_SIZE, FIFO_INIT_ELEMENT_SIZE);
  215. if (!buf->fifo) {
  216. av_log(ctx, AV_LOG_ERROR, "Failed to allocate fifo\n");
  217. return AVERROR(ENOMEM);
  218. }
  219. buf->warning_limit = 100;
  220. buf->next_pts = AV_NOPTS_VALUE;
  221. return 0;
  222. }
  223. void av_buffersink_set_frame_size(AVFilterContext *ctx, unsigned frame_size)
  224. {
  225. AVFilterLink *inlink = ctx->inputs[0];
  226. inlink->min_samples = inlink->max_samples =
  227. inlink->partial_buf_size = frame_size;
  228. }
  229. AVRational av_buffersink_get_frame_rate(AVFilterContext *ctx)
  230. {
  231. av_assert0( !strcmp(ctx->filter->name, "buffersink")
  232. || !strcmp(ctx->filter->name, "ffbuffersink"));
  233. return ctx->inputs[0]->frame_rate;
  234. }
  235. static av_cold int vsink_init(AVFilterContext *ctx, void *opaque)
  236. {
  237. BufferSinkContext *buf = ctx->priv;
  238. AVBufferSinkParams *params = opaque;
  239. int ret;
  240. if (params) {
  241. if ((ret = av_opt_set_int_list(buf, "pix_fmts", params->pixel_fmts, AV_PIX_FMT_NONE, 0)) < 0)
  242. return ret;
  243. }
  244. return common_init(ctx);
  245. }
  246. #define CHECK_LIST_SIZE(field) \
  247. if (buf->field ## _size % sizeof(*buf->field)) { \
  248. av_log(ctx, AV_LOG_ERROR, "Invalid size for " #field ": %d, " \
  249. "should be multiple of %d\n", \
  250. buf->field ## _size, (int)sizeof(*buf->field)); \
  251. return AVERROR(EINVAL); \
  252. }
  253. static int vsink_query_formats(AVFilterContext *ctx)
  254. {
  255. BufferSinkContext *buf = ctx->priv;
  256. AVFilterFormats *formats = NULL;
  257. unsigned i;
  258. int ret;
  259. CHECK_LIST_SIZE(pixel_fmts)
  260. if (buf->pixel_fmts_size) {
  261. for (i = 0; i < NB_ITEMS(buf->pixel_fmts); i++)
  262. if ((ret = ff_add_format(&formats, buf->pixel_fmts[i])) < 0)
  263. return ret;
  264. if ((ret = ff_set_common_formats(ctx, formats)) < 0)
  265. return ret;
  266. } else {
  267. if ((ret = ff_default_query_formats(ctx)) < 0)
  268. return ret;
  269. }
  270. return 0;
  271. }
  272. static av_cold int asink_init(AVFilterContext *ctx, void *opaque)
  273. {
  274. BufferSinkContext *buf = ctx->priv;
  275. AVABufferSinkParams *params = opaque;
  276. int ret;
  277. if (params) {
  278. if ((ret = av_opt_set_int_list(buf, "sample_fmts", params->sample_fmts, AV_SAMPLE_FMT_NONE, 0)) < 0 ||
  279. (ret = av_opt_set_int_list(buf, "sample_rates", params->sample_rates, -1, 0)) < 0 ||
  280. (ret = av_opt_set_int_list(buf, "channel_layouts", params->channel_layouts, -1, 0)) < 0 ||
  281. (ret = av_opt_set_int_list(buf, "channel_counts", params->channel_counts, -1, 0)) < 0 ||
  282. (ret = av_opt_set_int(buf, "all_channel_counts", params->all_channel_counts, 0)) < 0)
  283. return ret;
  284. }
  285. return common_init(ctx);
  286. }
  287. static int asink_query_formats(AVFilterContext *ctx)
  288. {
  289. BufferSinkContext *buf = ctx->priv;
  290. AVFilterFormats *formats = NULL;
  291. AVFilterChannelLayouts *layouts = NULL;
  292. unsigned i;
  293. int ret;
  294. CHECK_LIST_SIZE(sample_fmts)
  295. CHECK_LIST_SIZE(sample_rates)
  296. CHECK_LIST_SIZE(channel_layouts)
  297. CHECK_LIST_SIZE(channel_counts)
  298. if (buf->sample_fmts_size) {
  299. for (i = 0; i < NB_ITEMS(buf->sample_fmts); i++)
  300. if ((ret = ff_add_format(&formats, buf->sample_fmts[i])) < 0)
  301. return ret;
  302. if ((ret = ff_set_common_formats(ctx, formats)) < 0)
  303. return ret;
  304. }
  305. if (buf->channel_layouts_size || buf->channel_counts_size ||
  306. buf->all_channel_counts) {
  307. for (i = 0; i < NB_ITEMS(buf->channel_layouts); i++)
  308. if ((ret = ff_add_channel_layout(&layouts, buf->channel_layouts[i])) < 0)
  309. return ret;
  310. for (i = 0; i < NB_ITEMS(buf->channel_counts); i++)
  311. if ((ret = ff_add_channel_layout(&layouts, FF_COUNT2LAYOUT(buf->channel_counts[i]))) < 0)
  312. return ret;
  313. if (buf->all_channel_counts) {
  314. if (layouts)
  315. av_log(ctx, AV_LOG_WARNING,
  316. "Conflicting all_channel_counts and list in options\n");
  317. else if (!(layouts = ff_all_channel_counts()))
  318. return AVERROR(ENOMEM);
  319. }
  320. if ((ret = ff_set_common_channel_layouts(ctx, layouts)) < 0)
  321. return ret;
  322. }
  323. if (buf->sample_rates_size) {
  324. formats = NULL;
  325. for (i = 0; i < NB_ITEMS(buf->sample_rates); i++)
  326. if ((ret = ff_add_format(&formats, buf->sample_rates[i])) < 0)
  327. return ret;
  328. if ((ret = ff_set_common_samplerates(ctx, formats)) < 0)
  329. return ret;
  330. }
  331. return 0;
  332. }
  333. #define OFFSET(x) offsetof(BufferSinkContext, x)
  334. #define FLAGS AV_OPT_FLAG_FILTERING_PARAM|AV_OPT_FLAG_VIDEO_PARAM
  335. static const AVOption buffersink_options[] = {
  336. { "pix_fmts", "set the supported pixel formats", OFFSET(pixel_fmts), AV_OPT_TYPE_BINARY, .flags = FLAGS },
  337. { NULL },
  338. };
  339. #undef FLAGS
  340. #define FLAGS AV_OPT_FLAG_FILTERING_PARAM|AV_OPT_FLAG_VIDEO_PARAM
  341. static const AVOption abuffersink_options[] = {
  342. { "sample_fmts", "set the supported sample formats", OFFSET(sample_fmts), AV_OPT_TYPE_BINARY, .flags = FLAGS },
  343. { "sample_rates", "set the supported sample rates", OFFSET(sample_rates), AV_OPT_TYPE_BINARY, .flags = FLAGS },
  344. { "channel_layouts", "set the supported channel layouts", OFFSET(channel_layouts), AV_OPT_TYPE_BINARY, .flags = FLAGS },
  345. { "channel_counts", "set the supported channel counts", OFFSET(channel_counts), AV_OPT_TYPE_BINARY, .flags = FLAGS },
  346. { "all_channel_counts", "accept all channel counts", OFFSET(all_channel_counts), AV_OPT_TYPE_BOOL, {.i64 = 0}, 0, 1, FLAGS },
  347. { NULL },
  348. };
  349. #undef FLAGS
  350. AVFILTER_DEFINE_CLASS(buffersink);
  351. AVFILTER_DEFINE_CLASS(abuffersink);
  352. static const AVFilterPad avfilter_vsink_buffer_inputs[] = {
  353. {
  354. .name = "default",
  355. .type = AVMEDIA_TYPE_VIDEO,
  356. .filter_frame = filter_frame,
  357. },
  358. { NULL }
  359. };
  360. AVFilter ff_vsink_buffer = {
  361. .name = "buffersink",
  362. .description = NULL_IF_CONFIG_SMALL("Buffer video frames, and make them available to the end of the filter graph."),
  363. .priv_size = sizeof(BufferSinkContext),
  364. .priv_class = &buffersink_class,
  365. .init_opaque = vsink_init,
  366. .uninit = uninit,
  367. .query_formats = vsink_query_formats,
  368. .inputs = avfilter_vsink_buffer_inputs,
  369. .outputs = NULL,
  370. };
  371. static const AVFilterPad avfilter_asink_abuffer_inputs[] = {
  372. {
  373. .name = "default",
  374. .type = AVMEDIA_TYPE_AUDIO,
  375. .filter_frame = filter_frame,
  376. },
  377. { NULL }
  378. };
  379. AVFilter ff_asink_abuffer = {
  380. .name = "abuffersink",
  381. .description = NULL_IF_CONFIG_SMALL("Buffer audio frames, and make them available to the end of the filter graph."),
  382. .priv_class = &abuffersink_class,
  383. .priv_size = sizeof(BufferSinkContext),
  384. .init_opaque = asink_init,
  385. .uninit = uninit,
  386. .query_formats = asink_query_formats,
  387. .inputs = avfilter_asink_abuffer_inputs,
  388. .outputs = NULL,
  389. };