sink_buffer.c 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452
  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/audioconvert.h"
  25. #include "libavutil/avassert.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[] = { -1 };
  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[] = { -1 };
  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 PixelFormat *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 end_frame(AVFilterLink *inlink)
  102. {
  103. AVFilterContext *ctx = inlink->dst;
  104. BufferSinkContext *buf = inlink->dst->priv;
  105. int ret;
  106. av_assert1(inlink->cur_buf);
  107. if ((ret = add_buffer_ref(ctx, inlink->cur_buf)) < 0)
  108. return ret;
  109. inlink->cur_buf = NULL;
  110. if (buf->warning_limit &&
  111. av_fifo_size(buf->fifo) / sizeof(AVFilterBufferRef *) >= buf->warning_limit) {
  112. av_log(ctx, AV_LOG_WARNING,
  113. "%d buffers queued in %s, something may be wrong.\n",
  114. buf->warning_limit,
  115. (char *)av_x_if_null(ctx->name, ctx->filter->name));
  116. buf->warning_limit *= 10;
  117. }
  118. return 0;
  119. }
  120. void av_buffersink_set_frame_size(AVFilterContext *ctx, unsigned frame_size)
  121. {
  122. AVFilterLink *inlink = ctx->inputs[0];
  123. inlink->min_samples = inlink->max_samples =
  124. inlink->partial_buf_size = frame_size;
  125. }
  126. int av_buffersink_get_buffer_ref(AVFilterContext *ctx,
  127. AVFilterBufferRef **bufref, int flags)
  128. {
  129. BufferSinkContext *buf = ctx->priv;
  130. AVFilterLink *inlink = ctx->inputs[0];
  131. int ret;
  132. *bufref = NULL;
  133. av_assert0( !strcmp(ctx->filter->name, "buffersink")
  134. || !strcmp(ctx->filter->name, "abuffersink")
  135. || !strcmp(ctx->filter->name, "ffbuffersink")
  136. || !strcmp(ctx->filter->name, "ffabuffersink"));
  137. /* no picref available, fetch it from the filterchain */
  138. if (!av_fifo_size(buf->fifo)) {
  139. if (flags & AV_BUFFERSINK_FLAG_NO_REQUEST)
  140. return AVERROR(EAGAIN);
  141. if ((ret = ff_request_frame(inlink)) < 0)
  142. return ret;
  143. }
  144. if (!av_fifo_size(buf->fifo))
  145. return AVERROR(EINVAL);
  146. if (flags & AV_BUFFERSINK_FLAG_PEEK)
  147. *bufref = *((AVFilterBufferRef **)av_fifo_peek2(buf->fifo, 0));
  148. else
  149. av_fifo_generic_read(buf->fifo, bufref, sizeof(*bufref), NULL);
  150. return 0;
  151. }
  152. AVRational av_buffersink_get_frame_rate(AVFilterContext *ctx)
  153. {
  154. av_assert0( !strcmp(ctx->filter->name, "buffersink")
  155. || !strcmp(ctx->filter->name, "ffbuffersink"));
  156. return ctx->inputs[0]->frame_rate;
  157. }
  158. int av_buffersink_poll_frame(AVFilterContext *ctx)
  159. {
  160. BufferSinkContext *buf = ctx->priv;
  161. AVFilterLink *inlink = ctx->inputs[0];
  162. av_assert0( !strcmp(ctx->filter->name, "buffersink")
  163. || !strcmp(ctx->filter->name, "abuffersink")
  164. || !strcmp(ctx->filter->name, "ffbuffersink")
  165. || !strcmp(ctx->filter->name, "ffabuffersink"));
  166. return av_fifo_size(buf->fifo)/sizeof(AVFilterBufferRef *) + ff_poll_frame(inlink);
  167. }
  168. static av_cold int vsink_init(AVFilterContext *ctx, const char *args, void *opaque)
  169. {
  170. BufferSinkContext *buf = ctx->priv;
  171. AVBufferSinkParams *params = opaque;
  172. if (params && params->pixel_fmts) {
  173. const int *pixel_fmts = params->pixel_fmts;
  174. buf->pixel_fmts = ff_copy_int_list(pixel_fmts);
  175. if (!buf->pixel_fmts)
  176. return AVERROR(ENOMEM);
  177. }
  178. return common_init(ctx);
  179. }
  180. static av_cold void vsink_uninit(AVFilterContext *ctx)
  181. {
  182. BufferSinkContext *buf = ctx->priv;
  183. av_freep(&buf->pixel_fmts);
  184. common_uninit(ctx);
  185. }
  186. static int vsink_query_formats(AVFilterContext *ctx)
  187. {
  188. BufferSinkContext *buf = ctx->priv;
  189. if (buf->pixel_fmts)
  190. ff_set_common_formats(ctx, ff_make_format_list(buf->pixel_fmts));
  191. else
  192. ff_default_query_formats(ctx);
  193. return 0;
  194. }
  195. AVFilter avfilter_vsink_ffbuffersink = {
  196. .name = "ffbuffersink",
  197. .description = NULL_IF_CONFIG_SMALL("Buffer video frames, and make them available to the end of the filter graph."),
  198. .priv_size = sizeof(BufferSinkContext),
  199. .init_opaque = vsink_init,
  200. .uninit = vsink_uninit,
  201. .query_formats = vsink_query_formats,
  202. .inputs = (const AVFilterPad[]) {{ .name = "default",
  203. .type = AVMEDIA_TYPE_VIDEO,
  204. .end_frame = end_frame,
  205. .min_perms = AV_PERM_READ | AV_PERM_PRESERVE, },
  206. { .name = NULL }},
  207. .outputs = (const AVFilterPad[]) {{ .name = NULL }},
  208. };
  209. AVFilter avfilter_vsink_buffersink = {
  210. .name = "buffersink",
  211. .description = NULL_IF_CONFIG_SMALL("Buffer video frames, and make them available to the end of the filter graph."),
  212. .priv_size = sizeof(BufferSinkContext),
  213. .init_opaque = vsink_init,
  214. .uninit = vsink_uninit,
  215. .query_formats = vsink_query_formats,
  216. .inputs = (const AVFilterPad[]) {{ .name = "default",
  217. .type = AVMEDIA_TYPE_VIDEO,
  218. .end_frame = end_frame,
  219. .min_perms = AV_PERM_READ | AV_PERM_PRESERVE, },
  220. { .name = NULL }},
  221. .outputs = (const AVFilterPad[]) {{ .name = NULL }},
  222. };
  223. static int filter_samples(AVFilterLink *link, AVFilterBufferRef *samplesref)
  224. {
  225. end_frame(link);
  226. return 0;
  227. }
  228. static av_cold int asink_init(AVFilterContext *ctx, const char *args, void *opaque)
  229. {
  230. BufferSinkContext *buf = ctx->priv;
  231. AVABufferSinkParams *params = opaque;
  232. if (params && params->sample_fmts) {
  233. buf->sample_fmts = ff_copy_int_list (params->sample_fmts);
  234. if (!buf->sample_fmts)
  235. goto fail_enomem;
  236. }
  237. if (params && params->channel_layouts) {
  238. buf->channel_layouts = ff_copy_int64_list(params->channel_layouts);
  239. if (!buf->channel_layouts)
  240. goto fail_enomem;
  241. }
  242. if (!common_init(ctx))
  243. return 0;
  244. fail_enomem:
  245. av_freep(&buf->sample_fmts);
  246. av_freep(&buf->channel_layouts);
  247. return AVERROR(ENOMEM);
  248. }
  249. static av_cold void asink_uninit(AVFilterContext *ctx)
  250. {
  251. BufferSinkContext *buf = ctx->priv;
  252. av_freep(&buf->sample_fmts);
  253. av_freep(&buf->channel_layouts);
  254. common_uninit(ctx);
  255. }
  256. static int asink_query_formats(AVFilterContext *ctx)
  257. {
  258. BufferSinkContext *buf = ctx->priv;
  259. AVFilterFormats *formats = NULL;
  260. AVFilterChannelLayouts *layouts = NULL;
  261. if (buf->sample_fmts) {
  262. if (!(formats = ff_make_format_list(buf->sample_fmts)))
  263. return AVERROR(ENOMEM);
  264. ff_set_common_formats(ctx, formats);
  265. }
  266. if (buf->channel_layouts) {
  267. if (!(layouts = avfilter_make_format64_list(buf->channel_layouts)))
  268. return AVERROR(ENOMEM);
  269. ff_set_common_channel_layouts(ctx, layouts);
  270. }
  271. return 0;
  272. }
  273. AVFilter avfilter_asink_ffabuffersink = {
  274. .name = "ffabuffersink",
  275. .description = NULL_IF_CONFIG_SMALL("Buffer audio frames, and make them available to the end of the filter graph."),
  276. .init_opaque = asink_init,
  277. .uninit = asink_uninit,
  278. .priv_size = sizeof(BufferSinkContext),
  279. .query_formats = asink_query_formats,
  280. .inputs = (const AVFilterPad[]) {{ .name = "default",
  281. .type = AVMEDIA_TYPE_AUDIO,
  282. .filter_samples = filter_samples,
  283. .min_perms = AV_PERM_READ | AV_PERM_PRESERVE, },
  284. { .name = NULL }},
  285. .outputs = (const AVFilterPad[]) {{ .name = NULL }},
  286. };
  287. AVFilter avfilter_asink_abuffersink = {
  288. .name = "abuffersink",
  289. .description = NULL_IF_CONFIG_SMALL("Buffer audio frames, and make them available to the end of the filter graph."),
  290. .init_opaque = asink_init,
  291. .uninit = asink_uninit,
  292. .priv_size = sizeof(BufferSinkContext),
  293. .query_formats = asink_query_formats,
  294. .inputs = (const AVFilterPad[]) {{ .name = "default",
  295. .type = AVMEDIA_TYPE_AUDIO,
  296. .filter_samples = filter_samples,
  297. .min_perms = AV_PERM_READ | AV_PERM_PRESERVE, },
  298. { .name = NULL }},
  299. .outputs = (const AVFilterPad[]) {{ .name = NULL }},
  300. };
  301. /* Libav compatibility API */
  302. extern AVFilter avfilter_vsink_buffer;
  303. extern AVFilter avfilter_asink_abuffer;
  304. int av_buffersink_read(AVFilterContext *ctx, AVFilterBufferRef **buf)
  305. {
  306. AVFilterBufferRef *tbuf;
  307. int ret;
  308. if (ctx->filter-> inputs[0].start_frame ==
  309. avfilter_vsink_buffer. inputs[0].start_frame ||
  310. ctx->filter-> inputs[0].filter_samples ==
  311. avfilter_asink_abuffer.inputs[0].filter_samples)
  312. return ff_buffersink_read_compat(ctx, buf);
  313. av_assert0(ctx->filter-> inputs[0].end_frame ==
  314. avfilter_vsink_ffbuffersink. inputs[0].end_frame ||
  315. ctx->filter-> inputs[0].filter_samples ==
  316. avfilter_asink_ffabuffersink.inputs[0].filter_samples);
  317. ret = av_buffersink_get_buffer_ref(ctx, &tbuf,
  318. buf ? 0 : AV_BUFFERSINK_FLAG_PEEK);
  319. if (!buf)
  320. return ret >= 0;
  321. if (ret < 0)
  322. return ret;
  323. *buf = tbuf;
  324. return 0;
  325. }
  326. int av_buffersink_read_samples(AVFilterContext *ctx, AVFilterBufferRef **buf,
  327. int nb_samples)
  328. {
  329. BufferSinkContext *sink = ctx->priv;
  330. int ret = 0, have_samples = 0, need_samples;
  331. AVFilterBufferRef *tbuf, *in_buf;
  332. AVFilterLink *link = ctx->inputs[0];
  333. int nb_channels = av_get_channel_layout_nb_channels(link->channel_layout);
  334. if (ctx->filter-> inputs[0].filter_samples ==
  335. avfilter_asink_abuffer.inputs[0].filter_samples)
  336. return ff_buffersink_read_samples_compat(ctx, buf, nb_samples);
  337. av_assert0(ctx->filter-> inputs[0].filter_samples ==
  338. avfilter_asink_ffabuffersink.inputs[0].filter_samples);
  339. tbuf = ff_get_audio_buffer(link, AV_PERM_WRITE, nb_samples);
  340. if (!tbuf)
  341. return AVERROR(ENOMEM);
  342. while (have_samples < nb_samples) {
  343. ret = av_buffersink_get_buffer_ref(ctx, &in_buf,
  344. AV_BUFFERSINK_FLAG_PEEK);
  345. if (ret < 0) {
  346. if (ret == AVERROR_EOF && have_samples) {
  347. nb_samples = have_samples;
  348. ret = 0;
  349. }
  350. break;
  351. }
  352. need_samples = FFMIN(in_buf->audio->nb_samples,
  353. nb_samples - have_samples);
  354. av_samples_copy(tbuf->extended_data, in_buf->extended_data,
  355. have_samples, 0, need_samples,
  356. nb_channels, in_buf->format);
  357. have_samples += need_samples;
  358. if (need_samples < in_buf->audio->nb_samples) {
  359. in_buf->audio->nb_samples -= need_samples;
  360. av_samples_copy(in_buf->extended_data, in_buf->extended_data,
  361. 0, need_samples, in_buf->audio->nb_samples,
  362. nb_channels, in_buf->format);
  363. } else {
  364. av_buffersink_get_buffer_ref(ctx, &in_buf, 0);
  365. avfilter_unref_buffer(in_buf);
  366. }
  367. }
  368. tbuf->audio->nb_samples = have_samples;
  369. if (ret < 0) {
  370. av_assert0(!av_fifo_size(sink->fifo));
  371. if (have_samples)
  372. add_buffer_ref(ctx, tbuf);
  373. else
  374. avfilter_unref_buffer(tbuf);
  375. return ret;
  376. }
  377. *buf = tbuf;
  378. return 0;
  379. }