sink_buffer.c 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464
  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/fifo.h"
  27. #include "avfilter.h"
  28. #include "buffersink.h"
  29. #include "audio.h"
  30. #include "internal.h"
  31. AVBufferSinkParams *av_buffersink_params_alloc(void)
  32. {
  33. static const int pixel_fmts[] = { AV_PIX_FMT_NONE };
  34. AVBufferSinkParams *params = av_malloc(sizeof(AVBufferSinkParams));
  35. if (!params)
  36. return NULL;
  37. params->pixel_fmts = pixel_fmts;
  38. return params;
  39. }
  40. AVABufferSinkParams *av_abuffersink_params_alloc(void)
  41. {
  42. static const int sample_fmts[] = { AV_SAMPLE_FMT_NONE };
  43. static const int64_t channel_layouts[] = { -1 };
  44. AVABufferSinkParams *params = av_malloc(sizeof(AVABufferSinkParams));
  45. if (!params)
  46. return NULL;
  47. params->sample_fmts = sample_fmts;
  48. params->channel_layouts = channel_layouts;
  49. return params;
  50. }
  51. typedef struct {
  52. AVFifoBuffer *fifo; ///< FIFO buffer of video frame references
  53. unsigned warning_limit;
  54. /* only used for video */
  55. enum AVPixelFormat *pixel_fmts; ///< list of accepted pixel formats, must be terminated with -1
  56. /* only used for audio */
  57. enum AVSampleFormat *sample_fmts; ///< list of accepted sample formats, terminated by AV_SAMPLE_FMT_NONE
  58. int64_t *channel_layouts; ///< list of accepted channel layouts, terminated by -1
  59. } BufferSinkContext;
  60. #define FIFO_INIT_SIZE 8
  61. static av_cold int common_init(AVFilterContext *ctx)
  62. {
  63. BufferSinkContext *buf = ctx->priv;
  64. buf->fifo = av_fifo_alloc(FIFO_INIT_SIZE*sizeof(AVFilterBufferRef *));
  65. if (!buf->fifo) {
  66. av_log(ctx, AV_LOG_ERROR, "Failed to allocate fifo\n");
  67. return AVERROR(ENOMEM);
  68. }
  69. buf->warning_limit = 100;
  70. return 0;
  71. }
  72. static av_cold void common_uninit(AVFilterContext *ctx)
  73. {
  74. BufferSinkContext *buf = ctx->priv;
  75. AVFilterBufferRef *picref;
  76. if (buf->fifo) {
  77. while (av_fifo_size(buf->fifo) >= sizeof(AVFilterBufferRef *)) {
  78. av_fifo_generic_read(buf->fifo, &picref, sizeof(picref), NULL);
  79. avfilter_unref_buffer(picref);
  80. }
  81. av_fifo_free(buf->fifo);
  82. buf->fifo = NULL;
  83. }
  84. }
  85. static int add_buffer_ref(AVFilterContext *ctx, AVFilterBufferRef *ref)
  86. {
  87. BufferSinkContext *buf = ctx->priv;
  88. if (av_fifo_space(buf->fifo) < sizeof(AVFilterBufferRef *)) {
  89. /* realloc fifo size */
  90. if (av_fifo_realloc2(buf->fifo, av_fifo_size(buf->fifo) * 2) < 0) {
  91. av_log(ctx, AV_LOG_ERROR,
  92. "Cannot buffer more frames. Consume some available frames "
  93. "before adding new ones.\n");
  94. return AVERROR(ENOMEM);
  95. }
  96. }
  97. /* cache frame */
  98. av_fifo_generic_write(buf->fifo, &ref, sizeof(AVFilterBufferRef *), NULL);
  99. return 0;
  100. }
  101. static int filter_frame(AVFilterLink *inlink, AVFilterBufferRef *ref)
  102. {
  103. AVFilterContext *ctx = inlink->dst;
  104. BufferSinkContext *buf = inlink->dst->priv;
  105. int ret;
  106. if ((ret = add_buffer_ref(ctx, ref)) < 0)
  107. return ret;
  108. if (buf->warning_limit &&
  109. av_fifo_size(buf->fifo) / sizeof(AVFilterBufferRef *) >= buf->warning_limit) {
  110. av_log(ctx, AV_LOG_WARNING,
  111. "%d buffers queued in %s, something may be wrong.\n",
  112. buf->warning_limit,
  113. (char *)av_x_if_null(ctx->name, ctx->filter->name));
  114. buf->warning_limit *= 10;
  115. }
  116. return 0;
  117. }
  118. void av_buffersink_set_frame_size(AVFilterContext *ctx, unsigned frame_size)
  119. {
  120. AVFilterLink *inlink = ctx->inputs[0];
  121. inlink->min_samples = inlink->max_samples =
  122. inlink->partial_buf_size = frame_size;
  123. }
  124. int av_buffersink_get_buffer_ref(AVFilterContext *ctx,
  125. AVFilterBufferRef **bufref, int flags)
  126. {
  127. BufferSinkContext *buf = ctx->priv;
  128. AVFilterLink *inlink = ctx->inputs[0];
  129. int ret;
  130. *bufref = NULL;
  131. av_assert0( !strcmp(ctx->filter->name, "buffersink")
  132. || !strcmp(ctx->filter->name, "abuffersink")
  133. || !strcmp(ctx->filter->name, "ffbuffersink")
  134. || !strcmp(ctx->filter->name, "ffabuffersink"));
  135. /* no picref available, fetch it from the filterchain */
  136. if (!av_fifo_size(buf->fifo)) {
  137. if (flags & AV_BUFFERSINK_FLAG_NO_REQUEST)
  138. return AVERROR(EAGAIN);
  139. if ((ret = ff_request_frame(inlink)) < 0)
  140. return ret;
  141. }
  142. if (!av_fifo_size(buf->fifo))
  143. return AVERROR(EINVAL);
  144. if (flags & AV_BUFFERSINK_FLAG_PEEK)
  145. *bufref = *((AVFilterBufferRef **)av_fifo_peek2(buf->fifo, 0));
  146. else
  147. av_fifo_generic_read(buf->fifo, bufref, sizeof(*bufref), NULL);
  148. return 0;
  149. }
  150. AVRational av_buffersink_get_frame_rate(AVFilterContext *ctx)
  151. {
  152. av_assert0( !strcmp(ctx->filter->name, "buffersink")
  153. || !strcmp(ctx->filter->name, "ffbuffersink"));
  154. return ctx->inputs[0]->frame_rate;
  155. }
  156. int av_buffersink_poll_frame(AVFilterContext *ctx)
  157. {
  158. BufferSinkContext *buf = ctx->priv;
  159. AVFilterLink *inlink = ctx->inputs[0];
  160. av_assert0( !strcmp(ctx->filter->name, "buffersink")
  161. || !strcmp(ctx->filter->name, "abuffersink")
  162. || !strcmp(ctx->filter->name, "ffbuffersink")
  163. || !strcmp(ctx->filter->name, "ffabuffersink"));
  164. return av_fifo_size(buf->fifo)/sizeof(AVFilterBufferRef *) + ff_poll_frame(inlink);
  165. }
  166. static av_cold int vsink_init(AVFilterContext *ctx, const char *args, void *opaque)
  167. {
  168. BufferSinkContext *buf = ctx->priv;
  169. AVBufferSinkParams *params = opaque;
  170. if (params && params->pixel_fmts) {
  171. const int *pixel_fmts = params->pixel_fmts;
  172. buf->pixel_fmts = ff_copy_int_list(pixel_fmts);
  173. if (!buf->pixel_fmts)
  174. return AVERROR(ENOMEM);
  175. }
  176. return common_init(ctx);
  177. }
  178. static av_cold void vsink_uninit(AVFilterContext *ctx)
  179. {
  180. BufferSinkContext *buf = ctx->priv;
  181. av_freep(&buf->pixel_fmts);
  182. common_uninit(ctx);
  183. }
  184. static int vsink_query_formats(AVFilterContext *ctx)
  185. {
  186. BufferSinkContext *buf = ctx->priv;
  187. if (buf->pixel_fmts)
  188. ff_set_common_formats(ctx, ff_make_format_list(buf->pixel_fmts));
  189. else
  190. ff_default_query_formats(ctx);
  191. return 0;
  192. }
  193. static const AVFilterPad ffbuffersink_inputs[] = {
  194. {
  195. .name = "default",
  196. .type = AVMEDIA_TYPE_VIDEO,
  197. .filter_frame = filter_frame,
  198. .min_perms = AV_PERM_READ | AV_PERM_PRESERVE,
  199. },
  200. { NULL },
  201. };
  202. AVFilter avfilter_vsink_ffbuffersink = {
  203. .name = "ffbuffersink",
  204. .description = NULL_IF_CONFIG_SMALL("Buffer video frames, and make them available to the end of the filter graph."),
  205. .priv_size = sizeof(BufferSinkContext),
  206. .init_opaque = vsink_init,
  207. .uninit = vsink_uninit,
  208. .query_formats = vsink_query_formats,
  209. .inputs = ffbuffersink_inputs,
  210. .outputs = NULL,
  211. };
  212. static const AVFilterPad buffersink_inputs[] = {
  213. {
  214. .name = "default",
  215. .type = AVMEDIA_TYPE_VIDEO,
  216. .filter_frame = filter_frame,
  217. .min_perms = AV_PERM_READ | AV_PERM_PRESERVE,
  218. },
  219. { NULL },
  220. };
  221. AVFilter avfilter_vsink_buffersink = {
  222. .name = "buffersink",
  223. .description = NULL_IF_CONFIG_SMALL("Buffer video frames, and make them available to the end of the filter graph."),
  224. .priv_size = sizeof(BufferSinkContext),
  225. .init_opaque = vsink_init,
  226. .uninit = vsink_uninit,
  227. .query_formats = vsink_query_formats,
  228. .inputs = buffersink_inputs,
  229. .outputs = NULL,
  230. };
  231. static av_cold int asink_init(AVFilterContext *ctx, const char *args, void *opaque)
  232. {
  233. BufferSinkContext *buf = ctx->priv;
  234. AVABufferSinkParams *params = opaque;
  235. if (params && params->sample_fmts) {
  236. buf->sample_fmts = ff_copy_int_list (params->sample_fmts);
  237. if (!buf->sample_fmts)
  238. goto fail_enomem;
  239. }
  240. if (params && params->channel_layouts) {
  241. buf->channel_layouts = ff_copy_int64_list(params->channel_layouts);
  242. if (!buf->channel_layouts)
  243. goto fail_enomem;
  244. }
  245. if (!common_init(ctx))
  246. return 0;
  247. fail_enomem:
  248. av_freep(&buf->sample_fmts);
  249. av_freep(&buf->channel_layouts);
  250. return AVERROR(ENOMEM);
  251. }
  252. static av_cold void asink_uninit(AVFilterContext *ctx)
  253. {
  254. BufferSinkContext *buf = ctx->priv;
  255. av_freep(&buf->sample_fmts);
  256. av_freep(&buf->channel_layouts);
  257. common_uninit(ctx);
  258. }
  259. static int asink_query_formats(AVFilterContext *ctx)
  260. {
  261. BufferSinkContext *buf = ctx->priv;
  262. AVFilterFormats *formats = NULL;
  263. AVFilterChannelLayouts *layouts = NULL;
  264. if (buf->sample_fmts) {
  265. if (!(formats = ff_make_format_list(buf->sample_fmts)))
  266. return AVERROR(ENOMEM);
  267. ff_set_common_formats(ctx, formats);
  268. }
  269. if (buf->channel_layouts) {
  270. if (!(layouts = avfilter_make_format64_list(buf->channel_layouts)))
  271. return AVERROR(ENOMEM);
  272. ff_set_common_channel_layouts(ctx, layouts);
  273. }
  274. return 0;
  275. }
  276. static const AVFilterPad ffabuffersink_inputs[] = {
  277. {
  278. .name = "default",
  279. .type = AVMEDIA_TYPE_AUDIO,
  280. .filter_frame = filter_frame,
  281. .min_perms = AV_PERM_READ | AV_PERM_PRESERVE,
  282. },
  283. { NULL },
  284. };
  285. AVFilter avfilter_asink_ffabuffersink = {
  286. .name = "ffabuffersink",
  287. .description = NULL_IF_CONFIG_SMALL("Buffer audio frames, and make them available to the end of the filter graph."),
  288. .init_opaque = asink_init,
  289. .uninit = asink_uninit,
  290. .priv_size = sizeof(BufferSinkContext),
  291. .query_formats = asink_query_formats,
  292. .inputs = ffabuffersink_inputs,
  293. .outputs = NULL,
  294. };
  295. static const AVFilterPad abuffersink_inputs[] = {
  296. {
  297. .name = "default",
  298. .type = AVMEDIA_TYPE_AUDIO,
  299. .filter_frame = filter_frame,
  300. .min_perms = AV_PERM_READ | AV_PERM_PRESERVE,
  301. },
  302. { NULL },
  303. };
  304. AVFilter avfilter_asink_abuffersink = {
  305. .name = "abuffersink",
  306. .description = NULL_IF_CONFIG_SMALL("Buffer audio frames, and make them available to the end of the filter graph."),
  307. .init_opaque = asink_init,
  308. .uninit = asink_uninit,
  309. .priv_size = sizeof(BufferSinkContext),
  310. .query_formats = asink_query_formats,
  311. .inputs = abuffersink_inputs,
  312. .outputs = NULL,
  313. };
  314. /* Libav compatibility API */
  315. extern AVFilter avfilter_vsink_buffer;
  316. extern AVFilter avfilter_asink_abuffer;
  317. int av_buffersink_read(AVFilterContext *ctx, AVFilterBufferRef **buf)
  318. {
  319. AVFilterBufferRef *tbuf;
  320. int ret;
  321. if (ctx->filter-> inputs[0].start_frame ==
  322. avfilter_vsink_buffer. inputs[0].start_frame ||
  323. ctx->filter-> inputs[0].filter_frame ==
  324. avfilter_asink_abuffer.inputs[0].filter_frame)
  325. return ff_buffersink_read_compat(ctx, buf);
  326. av_assert0(ctx->filter-> inputs[0].end_frame ==
  327. avfilter_vsink_ffbuffersink. inputs[0].end_frame ||
  328. ctx->filter-> inputs[0].filter_frame ==
  329. avfilter_asink_ffabuffersink.inputs[0].filter_frame);
  330. ret = av_buffersink_get_buffer_ref(ctx, &tbuf,
  331. buf ? 0 : AV_BUFFERSINK_FLAG_PEEK);
  332. if (!buf)
  333. return ret >= 0;
  334. if (ret < 0)
  335. return ret;
  336. *buf = tbuf;
  337. return 0;
  338. }
  339. int av_buffersink_read_samples(AVFilterContext *ctx, AVFilterBufferRef **buf,
  340. int nb_samples)
  341. {
  342. BufferSinkContext *sink = ctx->priv;
  343. int ret = 0, have_samples = 0, need_samples;
  344. AVFilterBufferRef *tbuf, *in_buf;
  345. AVFilterLink *link = ctx->inputs[0];
  346. int nb_channels = av_get_channel_layout_nb_channels(link->channel_layout);
  347. if (ctx->filter-> inputs[0].filter_frame ==
  348. avfilter_asink_abuffer.inputs[0].filter_frame)
  349. return ff_buffersink_read_samples_compat(ctx, buf, nb_samples);
  350. av_assert0(ctx->filter-> inputs[0].filter_frame ==
  351. avfilter_asink_ffabuffersink.inputs[0].filter_frame);
  352. tbuf = ff_get_audio_buffer(link, AV_PERM_WRITE, nb_samples);
  353. if (!tbuf)
  354. return AVERROR(ENOMEM);
  355. while (have_samples < nb_samples) {
  356. ret = av_buffersink_get_buffer_ref(ctx, &in_buf,
  357. AV_BUFFERSINK_FLAG_PEEK);
  358. if (ret < 0) {
  359. if (ret == AVERROR_EOF && have_samples) {
  360. nb_samples = have_samples;
  361. ret = 0;
  362. }
  363. break;
  364. }
  365. need_samples = FFMIN(in_buf->audio->nb_samples,
  366. nb_samples - have_samples);
  367. av_samples_copy(tbuf->extended_data, in_buf->extended_data,
  368. have_samples, 0, need_samples,
  369. nb_channels, in_buf->format);
  370. have_samples += need_samples;
  371. if (need_samples < in_buf->audio->nb_samples) {
  372. in_buf->audio->nb_samples -= need_samples;
  373. av_samples_copy(in_buf->extended_data, in_buf->extended_data,
  374. 0, need_samples, in_buf->audio->nb_samples,
  375. nb_channels, in_buf->format);
  376. } else {
  377. av_buffersink_get_buffer_ref(ctx, &in_buf, 0);
  378. avfilter_unref_buffer(in_buf);
  379. }
  380. }
  381. tbuf->audio->nb_samples = have_samples;
  382. if (ret < 0) {
  383. av_assert0(!av_fifo_size(sink->fifo));
  384. if (have_samples)
  385. add_buffer_ref(ctx, tbuf);
  386. else
  387. avfilter_unref_buffer(tbuf);
  388. return ret;
  389. }
  390. *buf = tbuf;
  391. return 0;
  392. }