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.

607 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,
  151. AVFrame *frame, int nb_samples)
  152. {
  153. BufferSinkContext *s = ctx->priv;
  154. AVFilterLink *link = ctx->inputs[0];
  155. AVFrame *cur_frame;
  156. int ret = 0;
  157. if (!s->audio_fifo) {
  158. int nb_channels = link->channels;
  159. if (!(s->audio_fifo = av_audio_fifo_alloc(link->format, nb_channels, nb_samples)))
  160. return AVERROR(ENOMEM);
  161. }
  162. while (ret >= 0) {
  163. if (av_audio_fifo_size(s->audio_fifo) >= nb_samples)
  164. return read_from_fifo(ctx, frame, nb_samples);
  165. if (!(cur_frame = av_frame_alloc()))
  166. return AVERROR(ENOMEM);
  167. ret = av_buffersink_get_frame_flags(ctx, cur_frame, 0);
  168. if (ret == AVERROR_EOF && av_audio_fifo_size(s->audio_fifo)) {
  169. av_frame_free(&cur_frame);
  170. return read_from_fifo(ctx, frame, av_audio_fifo_size(s->audio_fifo));
  171. } else if (ret < 0) {
  172. av_frame_free(&cur_frame);
  173. return ret;
  174. }
  175. if (cur_frame->pts != AV_NOPTS_VALUE) {
  176. s->next_pts = cur_frame->pts -
  177. av_rescale_q(av_audio_fifo_size(s->audio_fifo),
  178. (AVRational){ 1, link->sample_rate },
  179. link->time_base);
  180. }
  181. ret = av_audio_fifo_write(s->audio_fifo, (void**)cur_frame->extended_data,
  182. cur_frame->nb_samples);
  183. av_frame_free(&cur_frame);
  184. }
  185. return ret;
  186. }
  187. AVBufferSinkParams *av_buffersink_params_alloc(void)
  188. {
  189. static const int pixel_fmts[] = { AV_PIX_FMT_NONE };
  190. AVBufferSinkParams *params = av_malloc(sizeof(AVBufferSinkParams));
  191. if (!params)
  192. return NULL;
  193. params->pixel_fmts = pixel_fmts;
  194. return params;
  195. }
  196. AVABufferSinkParams *av_abuffersink_params_alloc(void)
  197. {
  198. AVABufferSinkParams *params = av_mallocz(sizeof(AVABufferSinkParams));
  199. if (!params)
  200. return NULL;
  201. return params;
  202. }
  203. #define FIFO_INIT_SIZE 8
  204. static av_cold int common_init(AVFilterContext *ctx)
  205. {
  206. BufferSinkContext *buf = ctx->priv;
  207. buf->fifo = av_fifo_alloc(FIFO_INIT_SIZE*sizeof(AVFilterBufferRef *));
  208. if (!buf->fifo) {
  209. av_log(ctx, AV_LOG_ERROR, "Failed to allocate fifo\n");
  210. return AVERROR(ENOMEM);
  211. }
  212. buf->warning_limit = 100;
  213. return 0;
  214. }
  215. void av_buffersink_set_frame_size(AVFilterContext *ctx, unsigned frame_size)
  216. {
  217. AVFilterLink *inlink = ctx->inputs[0];
  218. inlink->min_samples = inlink->max_samples =
  219. inlink->partial_buf_size = frame_size;
  220. }
  221. #if FF_API_AVFILTERBUFFER
  222. static void compat_free_buffer(AVFilterBuffer *buf)
  223. {
  224. AVFrame *frame = buf->priv;
  225. av_frame_free(&frame);
  226. av_free(buf);
  227. }
  228. static int attribute_align_arg compat_read(AVFilterContext *ctx,
  229. AVFilterBufferRef **pbuf, int nb_samples, int flags)
  230. {
  231. AVFilterBufferRef *buf;
  232. AVFrame *frame;
  233. int ret;
  234. if (!pbuf)
  235. return ff_poll_frame(ctx->inputs[0]);
  236. frame = av_frame_alloc();
  237. if (!frame)
  238. return AVERROR(ENOMEM);
  239. if (!nb_samples)
  240. ret = av_buffersink_get_frame_flags(ctx, frame, flags);
  241. else
  242. ret = av_buffersink_get_samples(ctx, frame, nb_samples);
  243. if (ret < 0)
  244. goto fail;
  245. AV_NOWARN_DEPRECATED(
  246. if (ctx->inputs[0]->type == AVMEDIA_TYPE_VIDEO) {
  247. buf = avfilter_get_video_buffer_ref_from_arrays(frame->data, frame->linesize,
  248. AV_PERM_READ,
  249. frame->width, frame->height,
  250. frame->format);
  251. } else {
  252. buf = avfilter_get_audio_buffer_ref_from_arrays(frame->extended_data,
  253. frame->linesize[0], AV_PERM_READ,
  254. frame->nb_samples,
  255. frame->format,
  256. frame->channel_layout);
  257. }
  258. if (!buf) {
  259. ret = AVERROR(ENOMEM);
  260. goto fail;
  261. }
  262. avfilter_copy_frame_props(buf, frame);
  263. )
  264. buf->buf->priv = frame;
  265. buf->buf->free = compat_free_buffer;
  266. *pbuf = buf;
  267. return 0;
  268. fail:
  269. av_frame_free(&frame);
  270. return ret;
  271. }
  272. int av_buffersink_read(AVFilterContext *ctx, AVFilterBufferRef **buf)
  273. {
  274. return compat_read(ctx, buf, 0, 0);
  275. }
  276. int av_buffersink_read_samples(AVFilterContext *ctx, AVFilterBufferRef **buf,
  277. int nb_samples)
  278. {
  279. return compat_read(ctx, buf, nb_samples, 0);
  280. }
  281. int av_buffersink_get_buffer_ref(AVFilterContext *ctx,
  282. AVFilterBufferRef **bufref, int flags)
  283. {
  284. *bufref = NULL;
  285. av_assert0( !strcmp(ctx->filter->name, "buffersink")
  286. || !strcmp(ctx->filter->name, "abuffersink")
  287. || !strcmp(ctx->filter->name, "ffbuffersink")
  288. || !strcmp(ctx->filter->name, "ffabuffersink"));
  289. return compat_read(ctx, bufref, 0, flags);
  290. }
  291. #endif
  292. AVRational av_buffersink_get_frame_rate(AVFilterContext *ctx)
  293. {
  294. av_assert0( !strcmp(ctx->filter->name, "buffersink")
  295. || !strcmp(ctx->filter->name, "ffbuffersink"));
  296. return ctx->inputs[0]->frame_rate;
  297. }
  298. int attribute_align_arg av_buffersink_poll_frame(AVFilterContext *ctx)
  299. {
  300. BufferSinkContext *buf = ctx->priv;
  301. AVFilterLink *inlink = ctx->inputs[0];
  302. av_assert0( !strcmp(ctx->filter->name, "buffersink")
  303. || !strcmp(ctx->filter->name, "abuffersink")
  304. || !strcmp(ctx->filter->name, "ffbuffersink")
  305. || !strcmp(ctx->filter->name, "ffabuffersink"));
  306. return av_fifo_size(buf->fifo)/sizeof(AVFilterBufferRef *) + ff_poll_frame(inlink);
  307. }
  308. static av_cold int vsink_init(AVFilterContext *ctx, void *opaque)
  309. {
  310. BufferSinkContext *buf = ctx->priv;
  311. AVBufferSinkParams *params = opaque;
  312. int ret;
  313. if (params) {
  314. if ((ret = av_opt_set_int_list(buf, "pix_fmts", params->pixel_fmts, AV_PIX_FMT_NONE, 0)) < 0)
  315. return ret;
  316. }
  317. return common_init(ctx);
  318. }
  319. #define CHECK_LIST_SIZE(field) \
  320. if (buf->field ## _size % sizeof(*buf->field)) { \
  321. av_log(ctx, AV_LOG_ERROR, "Invalid size for " #field ": %d, " \
  322. "should be multiple of %d\n", \
  323. buf->field ## _size, (int)sizeof(*buf->field)); \
  324. return AVERROR(EINVAL); \
  325. }
  326. static int vsink_query_formats(AVFilterContext *ctx)
  327. {
  328. BufferSinkContext *buf = ctx->priv;
  329. AVFilterFormats *formats = NULL;
  330. unsigned i;
  331. int ret;
  332. CHECK_LIST_SIZE(pixel_fmts)
  333. if (buf->pixel_fmts_size) {
  334. for (i = 0; i < NB_ITEMS(buf->pixel_fmts); i++)
  335. if ((ret = ff_add_format(&formats, buf->pixel_fmts[i])) < 0) {
  336. ff_formats_unref(&formats);
  337. return ret;
  338. }
  339. ff_set_common_formats(ctx, formats);
  340. } else {
  341. ff_default_query_formats(ctx);
  342. }
  343. return 0;
  344. }
  345. static av_cold int asink_init(AVFilterContext *ctx, void *opaque)
  346. {
  347. BufferSinkContext *buf = ctx->priv;
  348. AVABufferSinkParams *params = opaque;
  349. int ret;
  350. if (params) {
  351. if ((ret = av_opt_set_int_list(buf, "sample_fmts", params->sample_fmts, AV_SAMPLE_FMT_NONE, 0)) < 0 ||
  352. (ret = av_opt_set_int_list(buf, "sample_rates", params->sample_rates, -1, 0)) < 0 ||
  353. (ret = av_opt_set_int_list(buf, "channel_layouts", params->channel_layouts, -1, 0)) < 0 ||
  354. (ret = av_opt_set_int_list(buf, "channel_counts", params->channel_counts, -1, 0)) < 0 ||
  355. (ret = av_opt_set_int(buf, "all_channel_counts", params->all_channel_counts, 0)) < 0)
  356. return ret;
  357. }
  358. return common_init(ctx);
  359. }
  360. static int asink_query_formats(AVFilterContext *ctx)
  361. {
  362. BufferSinkContext *buf = ctx->priv;
  363. AVFilterFormats *formats = NULL;
  364. AVFilterChannelLayouts *layouts = NULL;
  365. unsigned i;
  366. int ret;
  367. CHECK_LIST_SIZE(sample_fmts)
  368. CHECK_LIST_SIZE(sample_rates)
  369. CHECK_LIST_SIZE(channel_layouts)
  370. CHECK_LIST_SIZE(channel_counts)
  371. if (buf->sample_fmts_size) {
  372. for (i = 0; i < NB_ITEMS(buf->sample_fmts); i++)
  373. if ((ret = ff_add_format(&formats, buf->sample_fmts[i])) < 0) {
  374. ff_formats_unref(&formats);
  375. return ret;
  376. }
  377. ff_set_common_formats(ctx, formats);
  378. }
  379. if (buf->channel_layouts_size || buf->channel_counts_size ||
  380. buf->all_channel_counts) {
  381. for (i = 0; i < NB_ITEMS(buf->channel_layouts); i++)
  382. if ((ret = ff_add_channel_layout(&layouts, buf->channel_layouts[i])) < 0) {
  383. ff_channel_layouts_unref(&layouts);
  384. return ret;
  385. }
  386. for (i = 0; i < NB_ITEMS(buf->channel_counts); i++)
  387. if ((ret = ff_add_channel_layout(&layouts, FF_COUNT2LAYOUT(buf->channel_counts[i]))) < 0) {
  388. ff_channel_layouts_unref(&layouts);
  389. return ret;
  390. }
  391. if (buf->all_channel_counts) {
  392. if (layouts)
  393. av_log(ctx, AV_LOG_WARNING,
  394. "Conflicting all_channel_counts and list in options\n");
  395. else if (!(layouts = ff_all_channel_counts()))
  396. return AVERROR(ENOMEM);
  397. }
  398. ff_set_common_channel_layouts(ctx, layouts);
  399. }
  400. if (buf->sample_rates_size) {
  401. formats = NULL;
  402. for (i = 0; i < NB_ITEMS(buf->sample_rates); i++)
  403. if ((ret = ff_add_format(&formats, buf->sample_rates[i])) < 0) {
  404. ff_formats_unref(&formats);
  405. return ret;
  406. }
  407. ff_set_common_samplerates(ctx, formats);
  408. }
  409. return 0;
  410. }
  411. #define OFFSET(x) offsetof(BufferSinkContext, x)
  412. #define FLAGS AV_OPT_FLAG_FILTERING_PARAM|AV_OPT_FLAG_VIDEO_PARAM
  413. static const AVOption buffersink_options[] = {
  414. { "pix_fmts", "set the supported pixel formats", OFFSET(pixel_fmts), AV_OPT_TYPE_BINARY, .flags = FLAGS },
  415. { NULL },
  416. };
  417. #undef FLAGS
  418. #define FLAGS AV_OPT_FLAG_FILTERING_PARAM|AV_OPT_FLAG_VIDEO_PARAM
  419. static const AVOption abuffersink_options[] = {
  420. { "sample_fmts", "set the supported sample formats", OFFSET(sample_fmts), AV_OPT_TYPE_BINARY, .flags = FLAGS },
  421. { "sample_rates", "set the supported sample rates", OFFSET(sample_rates), AV_OPT_TYPE_BINARY, .flags = FLAGS },
  422. { "channel_layouts", "set the supported channel layouts", OFFSET(channel_layouts), AV_OPT_TYPE_BINARY, .flags = FLAGS },
  423. { "channel_counts", "set the supported channel counts", OFFSET(channel_counts), AV_OPT_TYPE_BINARY, .flags = FLAGS },
  424. { "all_channel_counts", "accept all channel counts", OFFSET(all_channel_counts), AV_OPT_TYPE_INT, {.i64 = 0}, 0, 1, FLAGS },
  425. { NULL },
  426. };
  427. #undef FLAGS
  428. AVFILTER_DEFINE_CLASS(buffersink);
  429. AVFILTER_DEFINE_CLASS(abuffersink);
  430. #if FF_API_AVFILTERBUFFER
  431. #define ffbuffersink_options buffersink_options
  432. #define ffabuffersink_options abuffersink_options
  433. AVFILTER_DEFINE_CLASS(ffbuffersink);
  434. AVFILTER_DEFINE_CLASS(ffabuffersink);
  435. static const AVFilterPad ffbuffersink_inputs[] = {
  436. {
  437. .name = "default",
  438. .type = AVMEDIA_TYPE_VIDEO,
  439. .filter_frame = filter_frame,
  440. },
  441. { NULL },
  442. };
  443. AVFilter avfilter_vsink_ffbuffersink = {
  444. .name = "ffbuffersink",
  445. .description = NULL_IF_CONFIG_SMALL("Buffer video frames, and make them available to the end of the filter graph."),
  446. .priv_size = sizeof(BufferSinkContext),
  447. .priv_class = &ffbuffersink_class,
  448. .init_opaque = vsink_init,
  449. .uninit = uninit,
  450. .query_formats = vsink_query_formats,
  451. .inputs = ffbuffersink_inputs,
  452. .outputs = NULL,
  453. };
  454. static const AVFilterPad ffabuffersink_inputs[] = {
  455. {
  456. .name = "default",
  457. .type = AVMEDIA_TYPE_AUDIO,
  458. .filter_frame = filter_frame,
  459. },
  460. { NULL },
  461. };
  462. AVFilter avfilter_asink_ffabuffersink = {
  463. .name = "ffabuffersink",
  464. .description = NULL_IF_CONFIG_SMALL("Buffer audio frames, and make them available to the end of the filter graph."),
  465. .init_opaque = asink_init,
  466. .uninit = uninit,
  467. .priv_size = sizeof(BufferSinkContext),
  468. .priv_class = &ffabuffersink_class,
  469. .query_formats = asink_query_formats,
  470. .inputs = ffabuffersink_inputs,
  471. .outputs = NULL,
  472. };
  473. #endif /* FF_API_AVFILTERBUFFER */
  474. static const AVFilterPad avfilter_vsink_buffer_inputs[] = {
  475. {
  476. .name = "default",
  477. .type = AVMEDIA_TYPE_VIDEO,
  478. .filter_frame = filter_frame,
  479. },
  480. { NULL }
  481. };
  482. AVFilter avfilter_vsink_buffer = {
  483. .name = "buffersink",
  484. .description = NULL_IF_CONFIG_SMALL("Buffer video frames, and make them available to the end of the filter graph."),
  485. .priv_size = sizeof(BufferSinkContext),
  486. .priv_class = &buffersink_class,
  487. .init_opaque = vsink_init,
  488. .uninit = uninit,
  489. .query_formats = vsink_query_formats,
  490. .inputs = avfilter_vsink_buffer_inputs,
  491. .outputs = NULL,
  492. };
  493. static const AVFilterPad avfilter_asink_abuffer_inputs[] = {
  494. {
  495. .name = "default",
  496. .type = AVMEDIA_TYPE_AUDIO,
  497. .filter_frame = filter_frame,
  498. },
  499. { NULL }
  500. };
  501. AVFilter avfilter_asink_abuffer = {
  502. .name = "abuffersink",
  503. .description = NULL_IF_CONFIG_SMALL("Buffer audio frames, and make them available to the end of the filter graph."),
  504. .priv_class = &abuffersink_class,
  505. .priv_size = sizeof(BufferSinkContext),
  506. .init_opaque = asink_init,
  507. .uninit = uninit,
  508. .query_formats = asink_query_formats,
  509. .inputs = avfilter_asink_abuffer_inputs,
  510. .outputs = NULL,
  511. };