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.

605 lines
19KB

  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/mathematics.h"
  29. #include "libavutil/opt.h"
  30. #include "audio.h"
  31. #include "avfilter.h"
  32. #include "buffersink.h"
  33. #include "internal.h"
  34. typedef struct {
  35. const AVClass *class;
  36. AVFifoBuffer *fifo; ///< FIFO buffer of video frame references
  37. unsigned warning_limit;
  38. /* only used for video */
  39. enum AVPixelFormat *pixel_fmts; ///< list of accepted pixel formats, must be terminated with -1
  40. int pixel_fmts_size;
  41. /* only used for audio */
  42. enum AVSampleFormat *sample_fmts; ///< list of accepted sample formats, terminated by AV_SAMPLE_FMT_NONE
  43. int sample_fmts_size;
  44. int64_t *channel_layouts; ///< list of accepted channel layouts, terminated by -1
  45. int channel_layouts_size;
  46. int *channel_counts; ///< list of accepted channel counts, terminated by -1
  47. int channel_counts_size;
  48. int all_channel_counts;
  49. int *sample_rates; ///< list of accepted sample rates, terminated by -1
  50. int sample_rates_size;
  51. /* only used for compat API */
  52. AVAudioFifo *audio_fifo; ///< FIFO for audio samples
  53. int64_t next_pts; ///< interpolating audio pts
  54. } BufferSinkContext;
  55. #define NB_ITEMS(list) (list ## _size / sizeof(*list))
  56. static av_cold void uninit(AVFilterContext *ctx)
  57. {
  58. BufferSinkContext *sink = ctx->priv;
  59. AVFrame *frame;
  60. if (sink->audio_fifo)
  61. av_audio_fifo_free(sink->audio_fifo);
  62. if (sink->fifo) {
  63. while (av_fifo_size(sink->fifo) >= sizeof(AVFilterBufferRef *)) {
  64. av_fifo_generic_read(sink->fifo, &frame, sizeof(frame), NULL);
  65. av_frame_free(&frame);
  66. }
  67. av_fifo_free(sink->fifo);
  68. sink->fifo = NULL;
  69. }
  70. }
  71. static int add_buffer_ref(AVFilterContext *ctx, AVFrame *ref)
  72. {
  73. BufferSinkContext *buf = ctx->priv;
  74. if (av_fifo_space(buf->fifo) < sizeof(AVFilterBufferRef *)) {
  75. /* realloc fifo size */
  76. if (av_fifo_realloc2(buf->fifo, av_fifo_size(buf->fifo) * 2) < 0) {
  77. av_log(ctx, AV_LOG_ERROR,
  78. "Cannot buffer more frames. Consume some available frames "
  79. "before adding new ones.\n");
  80. return AVERROR(ENOMEM);
  81. }
  82. }
  83. /* cache frame */
  84. av_fifo_generic_write(buf->fifo, &ref, sizeof(AVFilterBufferRef *), NULL);
  85. return 0;
  86. }
  87. static int filter_frame(AVFilterLink *link, AVFrame *frame)
  88. {
  89. AVFilterContext *ctx = link->dst;
  90. BufferSinkContext *buf = link->dst->priv;
  91. int ret;
  92. if ((ret = add_buffer_ref(ctx, frame)) < 0)
  93. return ret;
  94. if (buf->warning_limit &&
  95. av_fifo_size(buf->fifo) / sizeof(AVFilterBufferRef *) >= buf->warning_limit) {
  96. av_log(ctx, AV_LOG_WARNING,
  97. "%d buffers queued in %s, something may be wrong.\n",
  98. buf->warning_limit,
  99. (char *)av_x_if_null(ctx->name, ctx->filter->name));
  100. buf->warning_limit *= 10;
  101. }
  102. return 0;
  103. }
  104. int av_buffersink_get_frame(AVFilterContext *ctx, AVFrame *frame)
  105. {
  106. return av_buffersink_get_frame_flags(ctx, frame, 0);
  107. }
  108. int attribute_align_arg av_buffersink_get_frame_flags(AVFilterContext *ctx, AVFrame *frame, int flags)
  109. {
  110. BufferSinkContext *buf = ctx->priv;
  111. AVFilterLink *inlink = ctx->inputs[0];
  112. int ret;
  113. AVFrame *cur_frame;
  114. /* no picref available, fetch it from the filterchain */
  115. if (!av_fifo_size(buf->fifo)) {
  116. if (flags & AV_BUFFERSINK_FLAG_NO_REQUEST)
  117. return AVERROR(EAGAIN);
  118. if ((ret = ff_request_frame(inlink)) < 0)
  119. return ret;
  120. }
  121. if (!av_fifo_size(buf->fifo))
  122. return AVERROR(EINVAL);
  123. if (flags & AV_BUFFERSINK_FLAG_PEEK) {
  124. cur_frame = *((AVFrame **)av_fifo_peek2(buf->fifo, 0));
  125. if ((ret = av_frame_ref(frame, cur_frame)) < 0)
  126. return ret;
  127. } else {
  128. av_fifo_generic_read(buf->fifo, &cur_frame, sizeof(cur_frame), NULL);
  129. av_frame_move_ref(frame, cur_frame);
  130. av_frame_free(&cur_frame);
  131. }
  132. return 0;
  133. }
  134. static int read_from_fifo(AVFilterContext *ctx, AVFrame *frame,
  135. int nb_samples)
  136. {
  137. BufferSinkContext *s = ctx->priv;
  138. AVFilterLink *link = ctx->inputs[0];
  139. AVFrame *tmp;
  140. if (!(tmp = ff_get_audio_buffer(link, nb_samples)))
  141. return AVERROR(ENOMEM);
  142. av_audio_fifo_read(s->audio_fifo, (void**)tmp->extended_data, nb_samples);
  143. tmp->pts = s->next_pts;
  144. s->next_pts += av_rescale_q(nb_samples, (AVRational){1, link->sample_rate},
  145. link->time_base);
  146. av_frame_move_ref(frame, tmp);
  147. av_frame_free(&tmp);
  148. return 0;
  149. }
  150. int attribute_align_arg av_buffersink_get_samples(AVFilterContext *ctx, AVFrame *frame, int nb_samples)
  151. {
  152. BufferSinkContext *s = ctx->priv;
  153. AVFilterLink *link = ctx->inputs[0];
  154. AVFrame *cur_frame;
  155. int ret = 0;
  156. if (!s->audio_fifo) {
  157. int nb_channels = link->channels;
  158. if (!(s->audio_fifo = av_audio_fifo_alloc(link->format, nb_channels, nb_samples)))
  159. return AVERROR(ENOMEM);
  160. }
  161. while (ret >= 0) {
  162. if (av_audio_fifo_size(s->audio_fifo) >= nb_samples)
  163. return read_from_fifo(ctx, frame, nb_samples);
  164. if (!(cur_frame = av_frame_alloc()))
  165. return AVERROR(ENOMEM);
  166. ret = av_buffersink_get_frame_flags(ctx, cur_frame, 0);
  167. if (ret == AVERROR_EOF && av_audio_fifo_size(s->audio_fifo)) {
  168. av_frame_free(&cur_frame);
  169. return read_from_fifo(ctx, frame, av_audio_fifo_size(s->audio_fifo));
  170. } else if (ret < 0) {
  171. av_frame_free(&cur_frame);
  172. return ret;
  173. }
  174. if (cur_frame->pts != AV_NOPTS_VALUE) {
  175. s->next_pts = cur_frame->pts -
  176. av_rescale_q(av_audio_fifo_size(s->audio_fifo),
  177. (AVRational){ 1, link->sample_rate },
  178. link->time_base);
  179. }
  180. ret = av_audio_fifo_write(s->audio_fifo, (void**)cur_frame->extended_data,
  181. cur_frame->nb_samples);
  182. av_frame_free(&cur_frame);
  183. }
  184. return ret;
  185. }
  186. AVBufferSinkParams *av_buffersink_params_alloc(void)
  187. {
  188. static const int pixel_fmts[] = { AV_PIX_FMT_NONE };
  189. AVBufferSinkParams *params = av_malloc(sizeof(AVBufferSinkParams));
  190. if (!params)
  191. return NULL;
  192. params->pixel_fmts = pixel_fmts;
  193. return params;
  194. }
  195. AVABufferSinkParams *av_abuffersink_params_alloc(void)
  196. {
  197. AVABufferSinkParams *params = av_mallocz(sizeof(AVABufferSinkParams));
  198. if (!params)
  199. return NULL;
  200. return params;
  201. }
  202. #define FIFO_INIT_SIZE 8
  203. static av_cold int common_init(AVFilterContext *ctx)
  204. {
  205. BufferSinkContext *buf = ctx->priv;
  206. buf->fifo = av_fifo_alloc(FIFO_INIT_SIZE*sizeof(AVFilterBufferRef *));
  207. if (!buf->fifo) {
  208. av_log(ctx, AV_LOG_ERROR, "Failed to allocate fifo\n");
  209. return AVERROR(ENOMEM);
  210. }
  211. buf->warning_limit = 100;
  212. return 0;
  213. }
  214. void av_buffersink_set_frame_size(AVFilterContext *ctx, unsigned frame_size)
  215. {
  216. AVFilterLink *inlink = ctx->inputs[0];
  217. inlink->min_samples = inlink->max_samples =
  218. inlink->partial_buf_size = frame_size;
  219. }
  220. #if FF_API_AVFILTERBUFFER
  221. static void compat_free_buffer(AVFilterBuffer *buf)
  222. {
  223. AVFrame *frame = buf->priv;
  224. av_frame_free(&frame);
  225. av_free(buf);
  226. }
  227. static int attribute_align_arg compat_read(AVFilterContext *ctx, AVFilterBufferRef **pbuf, int nb_samples, int flags)
  228. {
  229. AVFilterBufferRef *buf;
  230. AVFrame *frame;
  231. int ret;
  232. if (!pbuf)
  233. return ff_poll_frame(ctx->inputs[0]);
  234. frame = av_frame_alloc();
  235. if (!frame)
  236. return AVERROR(ENOMEM);
  237. if (!nb_samples)
  238. ret = av_buffersink_get_frame_flags(ctx, frame, flags);
  239. else
  240. ret = av_buffersink_get_samples(ctx, frame, nb_samples);
  241. if (ret < 0)
  242. goto fail;
  243. AV_NOWARN_DEPRECATED(
  244. if (ctx->inputs[0]->type == AVMEDIA_TYPE_VIDEO) {
  245. buf = avfilter_get_video_buffer_ref_from_arrays(frame->data, frame->linesize,
  246. AV_PERM_READ,
  247. frame->width, frame->height,
  248. frame->format);
  249. } else {
  250. buf = avfilter_get_audio_buffer_ref_from_arrays(frame->extended_data,
  251. frame->linesize[0], AV_PERM_READ,
  252. frame->nb_samples,
  253. frame->format,
  254. frame->channel_layout);
  255. }
  256. if (!buf) {
  257. ret = AVERROR(ENOMEM);
  258. goto fail;
  259. }
  260. avfilter_copy_frame_props(buf, frame);
  261. )
  262. buf->buf->priv = frame;
  263. buf->buf->free = compat_free_buffer;
  264. *pbuf = buf;
  265. return 0;
  266. fail:
  267. av_frame_free(&frame);
  268. return ret;
  269. }
  270. int av_buffersink_read(AVFilterContext *ctx, AVFilterBufferRef **buf)
  271. {
  272. return compat_read(ctx, buf, 0, 0);
  273. }
  274. int av_buffersink_read_samples(AVFilterContext *ctx, AVFilterBufferRef **buf,
  275. int nb_samples)
  276. {
  277. return compat_read(ctx, buf, nb_samples, 0);
  278. }
  279. int av_buffersink_get_buffer_ref(AVFilterContext *ctx,
  280. AVFilterBufferRef **bufref, int flags)
  281. {
  282. *bufref = NULL;
  283. av_assert0( !strcmp(ctx->filter->name, "buffersink")
  284. || !strcmp(ctx->filter->name, "abuffersink")
  285. || !strcmp(ctx->filter->name, "ffbuffersink")
  286. || !strcmp(ctx->filter->name, "ffabuffersink"));
  287. return compat_read(ctx, bufref, 0, flags);
  288. }
  289. #endif
  290. AVRational av_buffersink_get_frame_rate(AVFilterContext *ctx)
  291. {
  292. av_assert0( !strcmp(ctx->filter->name, "buffersink")
  293. || !strcmp(ctx->filter->name, "ffbuffersink"));
  294. return ctx->inputs[0]->frame_rate;
  295. }
  296. int attribute_align_arg av_buffersink_poll_frame(AVFilterContext *ctx)
  297. {
  298. BufferSinkContext *buf = ctx->priv;
  299. AVFilterLink *inlink = ctx->inputs[0];
  300. av_assert0( !strcmp(ctx->filter->name, "buffersink")
  301. || !strcmp(ctx->filter->name, "abuffersink")
  302. || !strcmp(ctx->filter->name, "ffbuffersink")
  303. || !strcmp(ctx->filter->name, "ffabuffersink"));
  304. return av_fifo_size(buf->fifo)/sizeof(AVFilterBufferRef *) + ff_poll_frame(inlink);
  305. }
  306. static av_cold int vsink_init(AVFilterContext *ctx, void *opaque)
  307. {
  308. BufferSinkContext *buf = ctx->priv;
  309. AVBufferSinkParams *params = opaque;
  310. int ret;
  311. if (params) {
  312. if ((ret = av_opt_set_int_list(buf, "pix_fmts", params->pixel_fmts, AV_PIX_FMT_NONE, 0)) < 0)
  313. return ret;
  314. }
  315. return common_init(ctx);
  316. }
  317. #define CHECK_LIST_SIZE(field) \
  318. if (buf->field ## _size % sizeof(*buf->field)) { \
  319. av_log(ctx, AV_LOG_ERROR, "Invalid size for " #field ": %d, " \
  320. "should be multiple of %d\n", \
  321. buf->field ## _size, (int)sizeof(*buf->field)); \
  322. return AVERROR(EINVAL); \
  323. }
  324. static int vsink_query_formats(AVFilterContext *ctx)
  325. {
  326. BufferSinkContext *buf = ctx->priv;
  327. AVFilterFormats *formats = NULL;
  328. unsigned i;
  329. int ret;
  330. CHECK_LIST_SIZE(pixel_fmts)
  331. if (buf->pixel_fmts_size) {
  332. for (i = 0; i < NB_ITEMS(buf->pixel_fmts); i++)
  333. if ((ret = ff_add_format(&formats, buf->pixel_fmts[i])) < 0) {
  334. ff_formats_unref(&formats);
  335. return ret;
  336. }
  337. ff_set_common_formats(ctx, formats);
  338. } else {
  339. ff_default_query_formats(ctx);
  340. }
  341. return 0;
  342. }
  343. static av_cold int asink_init(AVFilterContext *ctx, void *opaque)
  344. {
  345. BufferSinkContext *buf = ctx->priv;
  346. AVABufferSinkParams *params = opaque;
  347. int ret;
  348. if (params) {
  349. if ((ret = av_opt_set_int_list(buf, "sample_fmts", params->sample_fmts, AV_SAMPLE_FMT_NONE, 0)) < 0 ||
  350. (ret = av_opt_set_int_list(buf, "sample_rates", params->sample_rates, -1, 0)) < 0 ||
  351. (ret = av_opt_set_int_list(buf, "channel_layouts", params->channel_layouts, -1, 0)) < 0 ||
  352. (ret = av_opt_set_int_list(buf, "channel_counts", params->channel_counts, -1, 0)) < 0 ||
  353. (ret = av_opt_set_int(buf, "all_channel_counts", params->all_channel_counts, 0)) < 0)
  354. return ret;
  355. }
  356. return common_init(ctx);
  357. }
  358. static int asink_query_formats(AVFilterContext *ctx)
  359. {
  360. BufferSinkContext *buf = ctx->priv;
  361. AVFilterFormats *formats = NULL;
  362. AVFilterChannelLayouts *layouts = NULL;
  363. unsigned i;
  364. int ret;
  365. CHECK_LIST_SIZE(sample_fmts)
  366. CHECK_LIST_SIZE(sample_rates)
  367. CHECK_LIST_SIZE(channel_layouts)
  368. CHECK_LIST_SIZE(channel_counts)
  369. if (buf->sample_fmts_size) {
  370. for (i = 0; i < NB_ITEMS(buf->sample_fmts); i++)
  371. if ((ret = ff_add_format(&formats, buf->sample_fmts[i])) < 0) {
  372. ff_formats_unref(&formats);
  373. return ret;
  374. }
  375. ff_set_common_formats(ctx, formats);
  376. }
  377. if (buf->channel_layouts_size || buf->channel_counts_size ||
  378. buf->all_channel_counts) {
  379. for (i = 0; i < NB_ITEMS(buf->channel_layouts); i++)
  380. if ((ret = ff_add_channel_layout(&layouts, buf->channel_layouts[i])) < 0) {
  381. ff_channel_layouts_unref(&layouts);
  382. return ret;
  383. }
  384. for (i = 0; i < NB_ITEMS(buf->channel_counts); i++)
  385. if ((ret = ff_add_channel_layout(&layouts, FF_COUNT2LAYOUT(buf->channel_counts[i]))) < 0) {
  386. ff_channel_layouts_unref(&layouts);
  387. return ret;
  388. }
  389. if (buf->all_channel_counts) {
  390. if (layouts)
  391. av_log(ctx, AV_LOG_WARNING,
  392. "Conflicting all_channel_counts and list in options\n");
  393. else if (!(layouts = ff_all_channel_counts()))
  394. return AVERROR(ENOMEM);
  395. }
  396. ff_set_common_channel_layouts(ctx, layouts);
  397. }
  398. if (buf->sample_rates_size) {
  399. formats = NULL;
  400. for (i = 0; i < NB_ITEMS(buf->sample_rates); i++)
  401. if ((ret = ff_add_format(&formats, buf->sample_rates[i])) < 0) {
  402. ff_formats_unref(&formats);
  403. return ret;
  404. }
  405. ff_set_common_samplerates(ctx, formats);
  406. }
  407. return 0;
  408. }
  409. #define OFFSET(x) offsetof(BufferSinkContext, x)
  410. #define FLAGS AV_OPT_FLAG_FILTERING_PARAM|AV_OPT_FLAG_VIDEO_PARAM
  411. static const AVOption buffersink_options[] = {
  412. { "pix_fmts", "set the supported pixel formats", OFFSET(pixel_fmts), AV_OPT_TYPE_BINARY, .flags = FLAGS },
  413. { NULL },
  414. };
  415. #undef FLAGS
  416. #define FLAGS AV_OPT_FLAG_FILTERING_PARAM|AV_OPT_FLAG_VIDEO_PARAM
  417. static const AVOption abuffersink_options[] = {
  418. { "sample_fmts", "set the supported sample formats", OFFSET(sample_fmts), AV_OPT_TYPE_BINARY, .flags = FLAGS },
  419. { "sample_rates", "set the supported sample rates", OFFSET(sample_rates), AV_OPT_TYPE_BINARY, .flags = FLAGS },
  420. { "channel_layouts", "set the supported channel layouts", OFFSET(channel_layouts), AV_OPT_TYPE_BINARY, .flags = FLAGS },
  421. { "channel_counts", "set the supported channel counts", OFFSET(channel_counts), AV_OPT_TYPE_BINARY, .flags = FLAGS },
  422. { "all_channel_counts", "accept all channel counts", OFFSET(all_channel_counts), AV_OPT_TYPE_INT, {.i64 = 0}, 0, 1, FLAGS },
  423. { NULL },
  424. };
  425. #undef FLAGS
  426. AVFILTER_DEFINE_CLASS(buffersink);
  427. AVFILTER_DEFINE_CLASS(abuffersink);
  428. #if FF_API_AVFILTERBUFFER
  429. #define ffbuffersink_options buffersink_options
  430. #define ffabuffersink_options abuffersink_options
  431. AVFILTER_DEFINE_CLASS(ffbuffersink);
  432. AVFILTER_DEFINE_CLASS(ffabuffersink);
  433. static const AVFilterPad ffbuffersink_inputs[] = {
  434. {
  435. .name = "default",
  436. .type = AVMEDIA_TYPE_VIDEO,
  437. .filter_frame = filter_frame,
  438. },
  439. { NULL },
  440. };
  441. AVFilter avfilter_vsink_ffbuffersink = {
  442. .name = "ffbuffersink",
  443. .description = NULL_IF_CONFIG_SMALL("Buffer video frames, and make them available to the end of the filter graph."),
  444. .priv_size = sizeof(BufferSinkContext),
  445. .priv_class = &ffbuffersink_class,
  446. .init_opaque = vsink_init,
  447. .uninit = uninit,
  448. .query_formats = vsink_query_formats,
  449. .inputs = ffbuffersink_inputs,
  450. .outputs = NULL,
  451. };
  452. static const AVFilterPad ffabuffersink_inputs[] = {
  453. {
  454. .name = "default",
  455. .type = AVMEDIA_TYPE_AUDIO,
  456. .filter_frame = filter_frame,
  457. },
  458. { NULL },
  459. };
  460. AVFilter avfilter_asink_ffabuffersink = {
  461. .name = "ffabuffersink",
  462. .description = NULL_IF_CONFIG_SMALL("Buffer audio frames, and make them available to the end of the filter graph."),
  463. .init_opaque = asink_init,
  464. .uninit = uninit,
  465. .priv_size = sizeof(BufferSinkContext),
  466. .priv_class = &ffabuffersink_class,
  467. .query_formats = asink_query_formats,
  468. .inputs = ffabuffersink_inputs,
  469. .outputs = NULL,
  470. };
  471. #endif /* FF_API_AVFILTERBUFFER */
  472. static const AVFilterPad avfilter_vsink_buffer_inputs[] = {
  473. {
  474. .name = "default",
  475. .type = AVMEDIA_TYPE_VIDEO,
  476. .filter_frame = filter_frame,
  477. },
  478. { NULL }
  479. };
  480. AVFilter avfilter_vsink_buffer = {
  481. .name = "buffersink",
  482. .description = NULL_IF_CONFIG_SMALL("Buffer video frames, and make them available to the end of the filter graph."),
  483. .priv_size = sizeof(BufferSinkContext),
  484. .priv_class = &buffersink_class,
  485. .init_opaque = vsink_init,
  486. .uninit = uninit,
  487. .query_formats = vsink_query_formats,
  488. .inputs = avfilter_vsink_buffer_inputs,
  489. .outputs = NULL,
  490. };
  491. static const AVFilterPad avfilter_asink_abuffer_inputs[] = {
  492. {
  493. .name = "default",
  494. .type = AVMEDIA_TYPE_AUDIO,
  495. .filter_frame = filter_frame,
  496. },
  497. { NULL }
  498. };
  499. AVFilter avfilter_asink_abuffer = {
  500. .name = "abuffersink",
  501. .description = NULL_IF_CONFIG_SMALL("Buffer audio frames, and make them available to the end of the filter graph."),
  502. .priv_class = &abuffersink_class,
  503. .priv_size = sizeof(BufferSinkContext),
  504. .init_opaque = asink_init,
  505. .uninit = uninit,
  506. .query_formats = asink_query_formats,
  507. .inputs = avfilter_asink_abuffer_inputs,
  508. .outputs = NULL,
  509. };