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.

487 lines
14KB

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