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.

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