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.

506 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/avassert.h"
  25. #include "libavutil/channel_layout.h"
  26. #include "libavutil/fifo.h"
  27. #include "avfilter.h"
  28. #include "buffersink.h"
  29. #include "audio.h"
  30. #include "internal.h"
  31. #include "libavutil/audio_fifo.h"
  32. AVBufferSinkParams *av_buffersink_params_alloc(void)
  33. {
  34. static const int pixel_fmts[] = { AV_PIX_FMT_NONE };
  35. AVBufferSinkParams *params = av_malloc(sizeof(AVBufferSinkParams));
  36. if (!params)
  37. return NULL;
  38. params->pixel_fmts = pixel_fmts;
  39. return params;
  40. }
  41. AVABufferSinkParams *av_abuffersink_params_alloc(void)
  42. {
  43. AVABufferSinkParams *params = av_mallocz(sizeof(AVABufferSinkParams));
  44. if (!params)
  45. return NULL;
  46. return params;
  47. }
  48. typedef struct {
  49. AVFifoBuffer *fifo; ///< FIFO buffer of video frame references
  50. unsigned warning_limit;
  51. /* only used for video */
  52. enum AVPixelFormat *pixel_fmts; ///< list of accepted pixel formats, must be terminated with -1
  53. /* only used for audio */
  54. enum AVSampleFormat *sample_fmts; ///< list of accepted sample formats, terminated by AV_SAMPLE_FMT_NONE
  55. int64_t *channel_layouts; ///< list of accepted channel layouts, terminated by -1
  56. int all_channel_counts;
  57. int *sample_rates; ///< list of accepted sample rates, terminated by -1
  58. } BufferSinkContext;
  59. #define FIFO_INIT_SIZE 8
  60. static av_cold int common_init(AVFilterContext *ctx)
  61. {
  62. BufferSinkContext *buf = ctx->priv;
  63. buf->fifo = av_fifo_alloc(FIFO_INIT_SIZE*sizeof(AVFilterBufferRef *));
  64. if (!buf->fifo) {
  65. av_log(ctx, AV_LOG_ERROR, "Failed to allocate fifo\n");
  66. return AVERROR(ENOMEM);
  67. }
  68. buf->warning_limit = 100;
  69. return 0;
  70. }
  71. static av_cold void common_uninit(AVFilterContext *ctx)
  72. {
  73. BufferSinkContext *buf = ctx->priv;
  74. AVFilterBufferRef *picref;
  75. if (buf->fifo) {
  76. while (av_fifo_size(buf->fifo) >= sizeof(AVFilterBufferRef *)) {
  77. av_fifo_generic_read(buf->fifo, &picref, sizeof(picref), NULL);
  78. av_frame_unref(picref);
  79. }
  80. av_fifo_free(buf->fifo);
  81. buf->fifo = NULL;
  82. }
  83. }
  84. static int add_buffer_ref(AVFilterContext *ctx, AVFrame *ref)
  85. {
  86. BufferSinkContext *buf = ctx->priv;
  87. if (av_fifo_space(buf->fifo) < sizeof(AVFilterBufferRef *)) {
  88. /* realloc fifo size */
  89. if (av_fifo_realloc2(buf->fifo, av_fifo_size(buf->fifo) * 2) < 0) {
  90. av_log(ctx, AV_LOG_ERROR,
  91. "Cannot buffer more frames. Consume some available frames "
  92. "before adding new ones.\n");
  93. return AVERROR(ENOMEM);
  94. }
  95. }
  96. /* cache frame */
  97. av_fifo_generic_write(buf->fifo, &ref, sizeof(AVFilterBufferRef *), NULL);
  98. return 0;
  99. }
  100. static int filter_frame(AVFilterLink *inlink, AVFrame *ref)
  101. {
  102. AVFilterContext *ctx = inlink->dst;
  103. BufferSinkContext *buf = inlink->dst->priv;
  104. int ret;
  105. if ((ret = add_buffer_ref(ctx, ref)) < 0)
  106. return ret;
  107. if (buf->warning_limit &&
  108. av_fifo_size(buf->fifo) / sizeof(AVFilterBufferRef *) >= buf->warning_limit) {
  109. av_log(ctx, AV_LOG_WARNING,
  110. "%d buffers queued in %s, something may be wrong.\n",
  111. buf->warning_limit,
  112. (char *)av_x_if_null(ctx->name, ctx->filter->name));
  113. buf->warning_limit *= 10;
  114. }
  115. return 0;
  116. }
  117. void av_buffersink_set_frame_size(AVFilterContext *ctx, unsigned frame_size)
  118. {
  119. AVFilterLink *inlink = ctx->inputs[0];
  120. inlink->min_samples = inlink->max_samples =
  121. inlink->partial_buf_size = frame_size;
  122. }
  123. int av_buffersink_get_frame_flags(AVFilterContext *ctx, AVFrame *frame, int flags)
  124. {
  125. BufferSinkContext *buf = ctx->priv;
  126. AVFilterLink *inlink = ctx->inputs[0];
  127. int ret;
  128. AVFrame *cur_frame;
  129. /* no picref available, fetch it from the filterchain */
  130. if (!av_fifo_size(buf->fifo)) {
  131. if (flags & AV_BUFFERSINK_FLAG_NO_REQUEST)
  132. return AVERROR(EAGAIN);
  133. if ((ret = ff_request_frame(inlink)) < 0)
  134. return ret;
  135. }
  136. if (!av_fifo_size(buf->fifo))
  137. return AVERROR(EINVAL);
  138. if (flags & AV_BUFFERSINK_FLAG_PEEK) {
  139. cur_frame = *((AVFrame **)av_fifo_peek2(buf->fifo, 0));
  140. av_frame_ref(frame, cur_frame); /* TODO check failure */
  141. } else {
  142. av_fifo_generic_read(buf->fifo, &cur_frame, sizeof(cur_frame), NULL);
  143. av_frame_move_ref(frame, cur_frame);
  144. av_frame_free(&cur_frame);
  145. }
  146. return 0;
  147. }
  148. int av_buffersink_get_frame(AVFilterContext *ctx, AVFrame *frame)
  149. {
  150. return av_buffersink_get_frame_flags(ctx, frame, 0);
  151. }
  152. int av_buffersink_get_samples(AVFilterContext *ctx, AVFrame *frame, int nb_samples)
  153. {
  154. av_assert0(!"TODO");
  155. }
  156. #if FF_API_AVFILTERBUFFER
  157. static void compat_free_buffer(AVFilterBuffer *buf)
  158. {
  159. AVFrame *frame = buf->priv;
  160. av_frame_free(&frame);
  161. av_free(buf);
  162. }
  163. static int compat_read(AVFilterContext *ctx, AVFilterBufferRef **pbuf, int nb_samples, int flags)
  164. {
  165. AVFilterBufferRef *buf;
  166. AVFrame *frame;
  167. int ret;
  168. if (!pbuf)
  169. return ff_poll_frame(ctx->inputs[0]);
  170. frame = av_frame_alloc();
  171. if (!frame)
  172. return AVERROR(ENOMEM);
  173. if (!nb_samples)
  174. ret = av_buffersink_get_frame_flags(ctx, frame, flags);
  175. else
  176. ret = av_buffersink_get_samples(ctx, frame, nb_samples);
  177. if (ret < 0)
  178. goto fail;
  179. if (ctx->inputs[0]->type == AVMEDIA_TYPE_VIDEO) {
  180. buf = avfilter_get_video_buffer_ref_from_arrays(frame->data, frame->linesize,
  181. AV_PERM_READ,
  182. frame->width, frame->height,
  183. frame->format);
  184. } else {
  185. buf = avfilter_get_audio_buffer_ref_from_arrays(frame->extended_data,
  186. frame->linesize[0], AV_PERM_READ,
  187. frame->nb_samples,
  188. frame->format,
  189. frame->channel_layout);
  190. }
  191. if (!buf) {
  192. ret = AVERROR(ENOMEM);
  193. goto fail;
  194. }
  195. avfilter_copy_frame_props(buf, frame);
  196. buf->buf->priv = frame;
  197. buf->buf->free = compat_free_buffer;
  198. *pbuf = buf;
  199. return 0;
  200. fail:
  201. av_frame_free(&frame);
  202. return ret;
  203. }
  204. int av_buffersink_read(AVFilterContext *ctx, AVFilterBufferRef **buf)
  205. {
  206. return compat_read(ctx, buf, 0, 0);
  207. }
  208. int av_buffersink_read_samples(AVFilterContext *ctx, AVFilterBufferRef **buf,
  209. int nb_samples)
  210. {
  211. return compat_read(ctx, buf, nb_samples, 0);
  212. }
  213. int av_buffersink_get_buffer_ref(AVFilterContext *ctx,
  214. AVFilterBufferRef **bufref, int flags)
  215. {
  216. BufferSinkContext *buf = ctx->priv;
  217. AVFilterLink *inlink = ctx->inputs[0];
  218. int ret;
  219. *bufref = NULL;
  220. av_assert0( !strcmp(ctx->filter->name, "buffersink")
  221. || !strcmp(ctx->filter->name, "abuffersink")
  222. || !strcmp(ctx->filter->name, "ffbuffersink")
  223. || !strcmp(ctx->filter->name, "ffabuffersink"));
  224. return compat_read(ctx, bufref, 0, flags);
  225. }
  226. #endif
  227. AVRational av_buffersink_get_frame_rate(AVFilterContext *ctx)
  228. {
  229. av_assert0( !strcmp(ctx->filter->name, "buffersink")
  230. || !strcmp(ctx->filter->name, "ffbuffersink"));
  231. return ctx->inputs[0]->frame_rate;
  232. }
  233. int av_buffersink_poll_frame(AVFilterContext *ctx)
  234. {
  235. BufferSinkContext *buf = ctx->priv;
  236. AVFilterLink *inlink = ctx->inputs[0];
  237. av_assert0( !strcmp(ctx->filter->name, "buffersink")
  238. || !strcmp(ctx->filter->name, "abuffersink")
  239. || !strcmp(ctx->filter->name, "ffbuffersink")
  240. || !strcmp(ctx->filter->name, "ffabuffersink"));
  241. return av_fifo_size(buf->fifo)/sizeof(AVFilterBufferRef *) + ff_poll_frame(inlink);
  242. }
  243. static av_cold int vsink_init(AVFilterContext *ctx, const char *args, void *opaque)
  244. {
  245. BufferSinkContext *buf = ctx->priv;
  246. AVBufferSinkParams *params = opaque;
  247. if (params && params->pixel_fmts) {
  248. const int *pixel_fmts = params->pixel_fmts;
  249. buf->pixel_fmts = ff_copy_int_list(pixel_fmts);
  250. if (!buf->pixel_fmts)
  251. return AVERROR(ENOMEM);
  252. }
  253. return common_init(ctx);
  254. }
  255. static av_cold void vsink_uninit(AVFilterContext *ctx)
  256. {
  257. BufferSinkContext *buf = ctx->priv;
  258. av_freep(&buf->pixel_fmts);
  259. common_uninit(ctx);
  260. }
  261. static int vsink_query_formats(AVFilterContext *ctx)
  262. {
  263. BufferSinkContext *buf = ctx->priv;
  264. if (buf->pixel_fmts)
  265. ff_set_common_formats(ctx, ff_make_format_list(buf->pixel_fmts));
  266. else
  267. ff_default_query_formats(ctx);
  268. return 0;
  269. }
  270. static const AVFilterPad ffbuffersink_inputs[] = {
  271. {
  272. .name = "default",
  273. .type = AVMEDIA_TYPE_VIDEO,
  274. .filter_frame = filter_frame,
  275. .min_perms = AV_PERM_READ | AV_PERM_PRESERVE,
  276. },
  277. { NULL },
  278. };
  279. AVFilter avfilter_vsink_ffbuffersink = {
  280. .name = "ffbuffersink",
  281. .description = NULL_IF_CONFIG_SMALL("Buffer video frames, and make them available to the end of the filter graph."),
  282. .priv_size = sizeof(BufferSinkContext),
  283. .init_opaque = vsink_init,
  284. .uninit = vsink_uninit,
  285. .query_formats = vsink_query_formats,
  286. .inputs = ffbuffersink_inputs,
  287. .outputs = NULL,
  288. };
  289. static const AVFilterPad buffersink_inputs[] = {
  290. {
  291. .name = "default",
  292. .type = AVMEDIA_TYPE_VIDEO,
  293. .filter_frame = filter_frame,
  294. .min_perms = AV_PERM_READ | AV_PERM_PRESERVE,
  295. },
  296. { NULL },
  297. };
  298. AVFilter avfilter_vsink_buffersink = {
  299. .name = "buffersink",
  300. .description = NULL_IF_CONFIG_SMALL("Buffer video frames, and make them available to the end of the filter graph."),
  301. .priv_size = sizeof(BufferSinkContext),
  302. .init_opaque = vsink_init,
  303. .uninit = vsink_uninit,
  304. .query_formats = vsink_query_formats,
  305. .inputs = buffersink_inputs,
  306. .outputs = NULL,
  307. };
  308. static int64_t *concat_channels_lists(const int64_t *layouts, const int *counts)
  309. {
  310. int nb_layouts = 0, nb_counts = 0, i;
  311. int64_t *list;
  312. if (layouts)
  313. for (; layouts[nb_layouts] != -1; nb_layouts++);
  314. if (counts)
  315. for (; counts[nb_counts] != -1; nb_counts++);
  316. if (nb_counts > INT_MAX - 1 - nb_layouts)
  317. return NULL;
  318. if (!(list = av_calloc(nb_layouts + nb_counts + 1, sizeof(*list))))
  319. return NULL;
  320. for (i = 0; i < nb_layouts; i++)
  321. list[i] = layouts[i];
  322. for (i = 0; i < nb_counts; i++)
  323. list[nb_layouts + i] = FF_COUNT2LAYOUT(counts[i]);
  324. list[nb_layouts + nb_counts] = -1;
  325. return list;
  326. }
  327. static av_cold int asink_init(AVFilterContext *ctx, const char *args, void *opaque)
  328. {
  329. BufferSinkContext *buf = ctx->priv;
  330. AVABufferSinkParams *params = opaque;
  331. if (params && params->sample_fmts) {
  332. buf->sample_fmts = ff_copy_int_list(params->sample_fmts);
  333. if (!buf->sample_fmts)
  334. return AVERROR(ENOMEM);
  335. }
  336. if (params && params->sample_rates) {
  337. buf->sample_rates = ff_copy_int_list(params->sample_rates);
  338. if (!buf->sample_rates)
  339. return AVERROR(ENOMEM);
  340. }
  341. if (params && (params->channel_layouts || params->channel_counts)) {
  342. if (params->all_channel_counts) {
  343. av_log(ctx, AV_LOG_ERROR,
  344. "Conflicting all_channel_counts and list in parameters\n");
  345. return AVERROR(EINVAL);
  346. }
  347. buf->channel_layouts = concat_channels_lists(params->channel_layouts,
  348. params->channel_counts);
  349. if (!buf->channel_layouts)
  350. return AVERROR(ENOMEM);
  351. }
  352. if (params)
  353. buf->all_channel_counts = params->all_channel_counts;
  354. return common_init(ctx);
  355. }
  356. static av_cold void asink_uninit(AVFilterContext *ctx)
  357. {
  358. BufferSinkContext *buf = ctx->priv;
  359. av_freep(&buf->sample_fmts);
  360. av_freep(&buf->sample_rates);
  361. av_freep(&buf->channel_layouts);
  362. common_uninit(ctx);
  363. }
  364. static int asink_query_formats(AVFilterContext *ctx)
  365. {
  366. BufferSinkContext *buf = ctx->priv;
  367. AVFilterFormats *formats = NULL;
  368. AVFilterChannelLayouts *layouts = NULL;
  369. if (buf->sample_fmts) {
  370. if (!(formats = ff_make_format_list(buf->sample_fmts)))
  371. return AVERROR(ENOMEM);
  372. ff_set_common_formats(ctx, formats);
  373. }
  374. if (buf->channel_layouts || buf->all_channel_counts) {
  375. layouts = buf->all_channel_counts ? ff_all_channel_counts() :
  376. avfilter_make_format64_list(buf->channel_layouts);
  377. if (!layouts)
  378. return AVERROR(ENOMEM);
  379. ff_set_common_channel_layouts(ctx, layouts);
  380. }
  381. if (buf->sample_rates) {
  382. formats = ff_make_format_list(buf->sample_rates);
  383. if (!formats)
  384. return AVERROR(ENOMEM);
  385. ff_set_common_samplerates(ctx, formats);
  386. }
  387. return 0;
  388. }
  389. static const AVFilterPad ffabuffersink_inputs[] = {
  390. {
  391. .name = "default",
  392. .type = AVMEDIA_TYPE_AUDIO,
  393. .filter_frame = filter_frame,
  394. .min_perms = AV_PERM_READ | AV_PERM_PRESERVE,
  395. },
  396. { NULL },
  397. };
  398. AVFilter avfilter_asink_ffabuffersink = {
  399. .name = "ffabuffersink",
  400. .description = NULL_IF_CONFIG_SMALL("Buffer audio frames, and make them available to the end of the filter graph."),
  401. .init_opaque = asink_init,
  402. .uninit = asink_uninit,
  403. .priv_size = sizeof(BufferSinkContext),
  404. .query_formats = asink_query_formats,
  405. .inputs = ffabuffersink_inputs,
  406. .outputs = NULL,
  407. };
  408. static const AVFilterPad abuffersink_inputs[] = {
  409. {
  410. .name = "default",
  411. .type = AVMEDIA_TYPE_AUDIO,
  412. .filter_frame = filter_frame,
  413. .min_perms = AV_PERM_READ | AV_PERM_PRESERVE,
  414. },
  415. { NULL },
  416. };
  417. AVFilter avfilter_asink_abuffersink = {
  418. .name = "abuffersink",
  419. .description = NULL_IF_CONFIG_SMALL("Buffer audio frames, and make them available to the end of the filter graph."),
  420. .init_opaque = asink_init,
  421. .uninit = asink_uninit,
  422. .priv_size = sizeof(BufferSinkContext),
  423. .query_formats = asink_query_formats,
  424. .inputs = abuffersink_inputs,
  425. .outputs = NULL,
  426. };