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.

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