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. static int vsink_query_formats(AVFilterContext *ctx)
  318. {
  319. BufferSinkContext *buf = ctx->priv;
  320. AVFilterFormats *formats = NULL;
  321. unsigned i;
  322. int ret;
  323. if (buf->pixel_fmts_size % sizeof(*buf->pixel_fmts)) {
  324. av_log(ctx, AV_LOG_ERROR, "Invalid size for format list\n");
  325. return AVERROR(EINVAL);
  326. }
  327. if (buf->pixel_fmts_size) {
  328. for (i = 0; i < NB_ITEMS(buf->pixel_fmts); i++)
  329. if ((ret = ff_add_format(&formats, buf->pixel_fmts[i])) < 0)
  330. return ret;
  331. ff_set_common_formats(ctx, formats);
  332. } else {
  333. ff_default_query_formats(ctx);
  334. }
  335. return 0;
  336. }
  337. static av_cold int asink_init(AVFilterContext *ctx, void *opaque)
  338. {
  339. BufferSinkContext *buf = ctx->priv;
  340. AVABufferSinkParams *params = opaque;
  341. int ret;
  342. if (params) {
  343. if ((ret = av_opt_set_int_list(buf, "sample_fmts", params->sample_fmts, AV_SAMPLE_FMT_NONE, 0)) < 0 ||
  344. (ret = av_opt_set_int_list(buf, "sample_rates", params->sample_rates, -1, 0)) < 0 ||
  345. (ret = av_opt_set_int_list(buf, "channel_layouts", params->channel_layouts, -1, 0)) < 0 ||
  346. (ret = av_opt_set_int_list(buf, "channel_counts", params->channel_counts, -1, 0)) < 0 ||
  347. (ret = av_opt_set_int(buf, "all_channel_counts", params->all_channel_counts, 0)) < 0)
  348. return ret;
  349. }
  350. return common_init(ctx);
  351. }
  352. static int asink_query_formats(AVFilterContext *ctx)
  353. {
  354. BufferSinkContext *buf = ctx->priv;
  355. AVFilterFormats *formats = NULL;
  356. AVFilterChannelLayouts *layouts = NULL;
  357. unsigned i;
  358. int ret;
  359. if (buf->sample_fmts_size % sizeof(*buf->sample_fmts) ||
  360. buf->sample_rates_size % sizeof(*buf->sample_rates) ||
  361. buf->channel_layouts_size % sizeof(*buf->channel_layouts) ||
  362. buf->channel_counts_size % sizeof(*buf->channel_counts)) {
  363. av_log(ctx, AV_LOG_ERROR, "Invalid size for format lists\n");
  364. #define LOG_ERROR(field) \
  365. if (buf->field ## _size % sizeof(*buf->field)) \
  366. av_log(ctx, AV_LOG_ERROR, " " #field " is %d, should be " \
  367. "multiple of %d\n", \
  368. buf->field ## _size, (int)sizeof(*buf->field));
  369. LOG_ERROR(sample_fmts);
  370. LOG_ERROR(sample_rates);
  371. LOG_ERROR(channel_layouts);
  372. LOG_ERROR(channel_counts);
  373. #undef LOG_ERROR
  374. return AVERROR(EINVAL);
  375. }
  376. if (buf->sample_fmts_size) {
  377. for (i = 0; i < NB_ITEMS(buf->sample_fmts); i++)
  378. if ((ret = ff_add_format(&formats, buf->sample_fmts[i])) < 0)
  379. return ret;
  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. return ret;
  387. for (i = 0; i < NB_ITEMS(buf->channel_counts); i++)
  388. if ((ret = ff_add_channel_layout(&layouts, FF_COUNT2LAYOUT(buf->channel_counts[i]))) < 0)
  389. return ret;
  390. if (buf->all_channel_counts) {
  391. if (layouts)
  392. av_log(ctx, AV_LOG_WARNING,
  393. "Conflicting all_channel_counts and list in options\n");
  394. else if (!(layouts = ff_all_channel_counts()))
  395. return AVERROR(ENOMEM);
  396. }
  397. ff_set_common_channel_layouts(ctx, layouts);
  398. }
  399. if (buf->sample_rates_size) {
  400. formats = NULL;
  401. for (i = 0; i < NB_ITEMS(buf->sample_rates); i++)
  402. if ((ret = ff_add_format(&formats, buf->sample_rates[i])) < 0)
  403. return ret;
  404. ff_set_common_samplerates(ctx, formats);
  405. }
  406. return 0;
  407. }
  408. #define OFFSET(x) offsetof(BufferSinkContext, x)
  409. #define FLAGS AV_OPT_FLAG_FILTERING_PARAM|AV_OPT_FLAG_VIDEO_PARAM
  410. static const AVOption buffersink_options[] = {
  411. { "pix_fmts", "set the supported pixel formats", OFFSET(pixel_fmts), AV_OPT_TYPE_BINARY, .flags = FLAGS },
  412. { NULL },
  413. };
  414. #undef FLAGS
  415. #define FLAGS AV_OPT_FLAG_FILTERING_PARAM|AV_OPT_FLAG_VIDEO_PARAM
  416. static const AVOption abuffersink_options[] = {
  417. { "sample_fmts", "set the supported sample formats", OFFSET(sample_fmts), AV_OPT_TYPE_BINARY, .flags = FLAGS },
  418. { "sample_rates", "set the supported sample rates", OFFSET(sample_rates), AV_OPT_TYPE_BINARY, .flags = FLAGS },
  419. { "channel_layouts", "set the supported channel layouts", OFFSET(channel_layouts), AV_OPT_TYPE_BINARY, .flags = FLAGS },
  420. { "channel_counts", "set the supported channel counts", OFFSET(channel_counts), AV_OPT_TYPE_BINARY, .flags = FLAGS },
  421. { "all_channel_counts", "accept all channel counts", OFFSET(all_channel_counts), AV_OPT_TYPE_INT, {.i64 = 0}, 0, 1, FLAGS },
  422. { NULL },
  423. };
  424. #undef FLAGS
  425. AVFILTER_DEFINE_CLASS(buffersink);
  426. AVFILTER_DEFINE_CLASS(abuffersink);
  427. #if FF_API_AVFILTERBUFFER
  428. #define ffbuffersink_options buffersink_options
  429. #define ffabuffersink_options abuffersink_options
  430. AVFILTER_DEFINE_CLASS(ffbuffersink);
  431. AVFILTER_DEFINE_CLASS(ffabuffersink);
  432. static const AVFilterPad ffbuffersink_inputs[] = {
  433. {
  434. .name = "default",
  435. .type = AVMEDIA_TYPE_VIDEO,
  436. .filter_frame = filter_frame,
  437. },
  438. { NULL },
  439. };
  440. AVFilter avfilter_vsink_ffbuffersink = {
  441. .name = "ffbuffersink",
  442. .description = NULL_IF_CONFIG_SMALL("Buffer video frames, and make them available to the end of the filter graph."),
  443. .priv_size = sizeof(BufferSinkContext),
  444. .priv_class = &ffbuffersink_class,
  445. .init_opaque = vsink_init,
  446. .uninit = uninit,
  447. .query_formats = vsink_query_formats,
  448. .inputs = ffbuffersink_inputs,
  449. .outputs = NULL,
  450. };
  451. static const AVFilterPad ffabuffersink_inputs[] = {
  452. {
  453. .name = "default",
  454. .type = AVMEDIA_TYPE_AUDIO,
  455. .filter_frame = filter_frame,
  456. },
  457. { NULL },
  458. };
  459. AVFilter avfilter_asink_ffabuffersink = {
  460. .name = "ffabuffersink",
  461. .description = NULL_IF_CONFIG_SMALL("Buffer audio frames, and make them available to the end of the filter graph."),
  462. .init_opaque = asink_init,
  463. .uninit = uninit,
  464. .priv_size = sizeof(BufferSinkContext),
  465. .priv_class = &ffabuffersink_class,
  466. .query_formats = asink_query_formats,
  467. .inputs = ffabuffersink_inputs,
  468. .outputs = NULL,
  469. };
  470. #endif /* FF_API_AVFILTERBUFFER */
  471. static const AVFilterPad avfilter_vsink_buffer_inputs[] = {
  472. {
  473. .name = "default",
  474. .type = AVMEDIA_TYPE_VIDEO,
  475. .filter_frame = filter_frame,
  476. },
  477. { NULL }
  478. };
  479. AVFilter avfilter_vsink_buffer = {
  480. .name = "buffersink",
  481. .description = NULL_IF_CONFIG_SMALL("Buffer video frames, and make them available to the end of the filter graph."),
  482. .priv_size = sizeof(BufferSinkContext),
  483. .priv_class = &buffersink_class,
  484. .init_opaque = vsink_init,
  485. .uninit = uninit,
  486. .query_formats = vsink_query_formats,
  487. .inputs = avfilter_vsink_buffer_inputs,
  488. .outputs = NULL,
  489. };
  490. static const AVFilterPad avfilter_asink_abuffer_inputs[] = {
  491. {
  492. .name = "default",
  493. .type = AVMEDIA_TYPE_AUDIO,
  494. .filter_frame = filter_frame,
  495. },
  496. { NULL }
  497. };
  498. AVFilter avfilter_asink_abuffer = {
  499. .name = "abuffersink",
  500. .description = NULL_IF_CONFIG_SMALL("Buffer audio frames, and make them available to the end of the filter graph."),
  501. .priv_class = &abuffersink_class,
  502. .priv_size = sizeof(BufferSinkContext),
  503. .init_opaque = asink_init,
  504. .uninit = uninit,
  505. .query_formats = asink_query_formats,
  506. .inputs = avfilter_asink_abuffer_inputs,
  507. .outputs = NULL,
  508. };