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.

500 lines
16KB

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