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.

486 lines
15KB

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