2011-06-11 19:43:11 +03:00
|
|
|
/*
|
|
|
|
* Copyright (c) 2011 Stefano Sabatini
|
|
|
|
*
|
|
|
|
* This file is part of FFmpeg.
|
|
|
|
*
|
|
|
|
* FFmpeg is free software; you can redistribute it and/or
|
|
|
|
* modify it under the terms of the GNU Lesser General Public
|
|
|
|
* License as published by the Free Software Foundation; either
|
|
|
|
* version 2.1 of the License, or (at your option) any later version.
|
|
|
|
*
|
|
|
|
* FFmpeg is distributed in the hope that it will be useful,
|
|
|
|
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
|
|
|
|
* Lesser General Public License for more details.
|
|
|
|
*
|
|
|
|
* You should have received a copy of the GNU Lesser General Public
|
|
|
|
* License along with FFmpeg; if not, write to the Free Software
|
|
|
|
* Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
|
|
|
|
*/
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @file
|
2012-07-06 02:10:28 +03:00
|
|
|
* buffer sink
|
2011-06-11 19:43:11 +03:00
|
|
|
*/
|
|
|
|
|
2012-04-24 13:51:37 +03:00
|
|
|
#include "libavutil/avassert.h"
|
2011-06-29 18:31:16 +03:00
|
|
|
#include "libavutil/fifo.h"
|
2011-06-11 19:43:11 +03:00
|
|
|
#include "avfilter.h"
|
2011-09-06 19:37:44 +03:00
|
|
|
#include "buffersink.h"
|
2011-12-20 15:08:57 +03:00
|
|
|
#include "internal.h"
|
2011-06-11 19:43:11 +03:00
|
|
|
|
2011-08-18 17:21:47 +03:00
|
|
|
AVBufferSinkParams *av_buffersink_params_alloc(void)
|
|
|
|
{
|
|
|
|
static const int pixel_fmts[] = { -1 };
|
|
|
|
AVBufferSinkParams *params = av_malloc(sizeof(AVBufferSinkParams));
|
|
|
|
if (!params)
|
|
|
|
return NULL;
|
|
|
|
|
|
|
|
params->pixel_fmts = pixel_fmts;
|
|
|
|
return params;
|
|
|
|
}
|
|
|
|
|
|
|
|
AVABufferSinkParams *av_abuffersink_params_alloc(void)
|
|
|
|
{
|
|
|
|
static const int sample_fmts[] = { -1 };
|
|
|
|
static const int64_t channel_layouts[] = { -1 };
|
|
|
|
AVABufferSinkParams *params = av_malloc(sizeof(AVABufferSinkParams));
|
|
|
|
|
|
|
|
if (!params)
|
|
|
|
return NULL;
|
|
|
|
|
|
|
|
params->sample_fmts = sample_fmts;
|
|
|
|
params->channel_layouts = channel_layouts;
|
|
|
|
return params;
|
|
|
|
}
|
|
|
|
|
2011-06-11 19:43:11 +03:00
|
|
|
typedef struct {
|
2011-08-18 17:21:47 +03:00
|
|
|
AVFifoBuffer *fifo; ///< FIFO buffer of video frame references
|
2012-06-28 12:21:42 +03:00
|
|
|
unsigned warning_limit;
|
2011-08-18 17:21:47 +03:00
|
|
|
|
|
|
|
/* only used for video */
|
2011-12-20 15:08:57 +03:00
|
|
|
enum PixelFormat *pixel_fmts; ///< list of accepted pixel formats, must be terminated with -1
|
2011-08-18 17:21:47 +03:00
|
|
|
|
|
|
|
/* only used for audio */
|
2011-12-20 15:08:57 +03:00
|
|
|
enum AVSampleFormat *sample_fmts; ///< list of accepted sample formats, terminated by AV_SAMPLE_FMT_NONE
|
|
|
|
int64_t *channel_layouts; ///< list of accepted channel layouts, terminated by -1
|
2011-06-11 19:43:11 +03:00
|
|
|
} BufferSinkContext;
|
|
|
|
|
2011-06-29 18:31:16 +03:00
|
|
|
#define FIFO_INIT_SIZE 8
|
|
|
|
|
2011-08-18 17:21:47 +03:00
|
|
|
static av_cold int common_init(AVFilterContext *ctx)
|
2011-06-11 19:43:11 +03:00
|
|
|
{
|
|
|
|
BufferSinkContext *buf = ctx->priv;
|
|
|
|
|
2011-06-29 18:31:16 +03:00
|
|
|
buf->fifo = av_fifo_alloc(FIFO_INIT_SIZE*sizeof(AVFilterBufferRef *));
|
|
|
|
if (!buf->fifo) {
|
|
|
|
av_log(ctx, AV_LOG_ERROR, "Failed to allocate fifo\n");
|
|
|
|
return AVERROR(ENOMEM);
|
|
|
|
}
|
2012-06-28 12:21:42 +03:00
|
|
|
buf->warning_limit = 100;
|
2011-06-11 19:43:11 +03:00
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2011-08-18 17:21:47 +03:00
|
|
|
static av_cold void common_uninit(AVFilterContext *ctx)
|
2011-06-11 19:43:11 +03:00
|
|
|
{
|
|
|
|
BufferSinkContext *buf = ctx->priv;
|
2011-06-29 18:31:16 +03:00
|
|
|
AVFilterBufferRef *picref;
|
|
|
|
|
|
|
|
if (buf->fifo) {
|
|
|
|
while (av_fifo_size(buf->fifo) >= sizeof(AVFilterBufferRef *)) {
|
|
|
|
av_fifo_generic_read(buf->fifo, &picref, sizeof(picref), NULL);
|
|
|
|
avfilter_unref_buffer(picref);
|
|
|
|
}
|
|
|
|
av_fifo_free(buf->fifo);
|
|
|
|
buf->fifo = NULL;
|
|
|
|
}
|
2011-06-11 19:43:11 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
static void end_frame(AVFilterLink *inlink)
|
|
|
|
{
|
2011-06-29 18:31:16 +03:00
|
|
|
AVFilterContext *ctx = inlink->dst;
|
2011-06-11 19:43:11 +03:00
|
|
|
BufferSinkContext *buf = inlink->dst->priv;
|
|
|
|
|
2012-04-24 13:51:37 +03:00
|
|
|
av_assert1(inlink->cur_buf);
|
2011-06-29 18:31:16 +03:00
|
|
|
if (av_fifo_space(buf->fifo) < sizeof(AVFilterBufferRef *)) {
|
|
|
|
/* realloc fifo size */
|
|
|
|
if (av_fifo_realloc2(buf->fifo, av_fifo_size(buf->fifo) * 2) < 0) {
|
|
|
|
av_log(ctx, AV_LOG_ERROR,
|
|
|
|
"Cannot buffer more frames. Consume some available frames "
|
|
|
|
"before adding new ones.\n");
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/* cache frame */
|
|
|
|
av_fifo_generic_write(buf->fifo,
|
|
|
|
&inlink->cur_buf, sizeof(AVFilterBufferRef *), NULL);
|
2012-06-28 12:21:42 +03:00
|
|
|
if (buf->warning_limit &&
|
|
|
|
av_fifo_size(buf->fifo) / sizeof(AVFilterBufferRef *) >= buf->warning_limit) {
|
|
|
|
av_log(ctx, AV_LOG_WARNING,
|
|
|
|
"%d buffers queued in %s, something may be wrong.\n",
|
|
|
|
buf->warning_limit,
|
|
|
|
(char *)av_x_if_null(ctx->name, ctx->filter->name));
|
|
|
|
buf->warning_limit *= 10;
|
|
|
|
}
|
2011-06-11 19:43:11 +03:00
|
|
|
}
|
|
|
|
|
2012-06-25 01:30:15 +03:00
|
|
|
void av_buffersink_set_frame_size(AVFilterContext *ctx, unsigned frame_size)
|
|
|
|
{
|
|
|
|
AVFilterLink *inlink = ctx->inputs[0];
|
|
|
|
|
|
|
|
inlink->min_samples = inlink->max_samples =
|
|
|
|
inlink->partial_buf_size = frame_size;
|
|
|
|
}
|
|
|
|
|
2011-08-18 17:21:47 +03:00
|
|
|
int av_buffersink_get_buffer_ref(AVFilterContext *ctx,
|
|
|
|
AVFilterBufferRef **bufref, int flags)
|
2011-06-11 19:43:11 +03:00
|
|
|
{
|
|
|
|
BufferSinkContext *buf = ctx->priv;
|
|
|
|
AVFilterLink *inlink = ctx->inputs[0];
|
|
|
|
int ret;
|
2011-08-18 17:21:47 +03:00
|
|
|
*bufref = NULL;
|
2011-06-11 19:43:11 +03:00
|
|
|
|
2012-06-24 15:17:14 +03:00
|
|
|
av_assert0(!strcmp(ctx->filter->name, "buffersink") || !strcmp(ctx->filter->name, "abuffersink"));
|
|
|
|
|
2011-06-11 19:43:11 +03:00
|
|
|
/* no picref available, fetch it from the filterchain */
|
2011-06-29 18:31:16 +03:00
|
|
|
if (!av_fifo_size(buf->fifo)) {
|
2012-04-19 18:08:27 +03:00
|
|
|
if (flags & AV_BUFFERSINK_FLAG_NO_REQUEST)
|
|
|
|
return AVERROR(EAGAIN);
|
2012-06-24 03:09:53 +03:00
|
|
|
if ((ret = ff_request_frame(inlink)) < 0)
|
2011-06-11 19:43:11 +03:00
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
2011-06-29 18:31:16 +03:00
|
|
|
if (!av_fifo_size(buf->fifo))
|
2011-06-11 19:43:11 +03:00
|
|
|
return AVERROR(EINVAL);
|
|
|
|
|
2011-08-18 17:21:47 +03:00
|
|
|
if (flags & AV_BUFFERSINK_FLAG_PEEK)
|
|
|
|
*bufref = *((AVFilterBufferRef **)av_fifo_peek2(buf->fifo, 0));
|
2011-06-29 18:31:16 +03:00
|
|
|
else
|
2011-08-18 17:21:47 +03:00
|
|
|
av_fifo_generic_read(buf->fifo, bufref, sizeof(*bufref), NULL);
|
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2012-06-05 13:40:37 +03:00
|
|
|
AVRational av_buffersink_get_frame_rate(AVFilterContext *ctx)
|
|
|
|
{
|
2012-06-24 15:17:14 +03:00
|
|
|
av_assert0(!strcmp(ctx->filter->name, "buffersink"));
|
|
|
|
|
2012-06-05 13:40:37 +03:00
|
|
|
return ctx->inputs[0]->frame_rate;
|
|
|
|
}
|
|
|
|
|
2011-12-06 19:02:15 +03:00
|
|
|
int av_buffersink_poll_frame(AVFilterContext *ctx)
|
|
|
|
{
|
|
|
|
BufferSinkContext *buf = ctx->priv;
|
|
|
|
AVFilterLink *inlink = ctx->inputs[0];
|
|
|
|
|
2012-06-24 15:17:14 +03:00
|
|
|
av_assert0(!strcmp(ctx->filter->name, "buffersink") || !strcmp(ctx->filter->name, "abuffersink"));
|
|
|
|
|
2012-06-16 12:47:46 +03:00
|
|
|
return av_fifo_size(buf->fifo)/sizeof(AVFilterBufferRef *) + ff_poll_frame(inlink);
|
2011-12-06 19:02:15 +03:00
|
|
|
}
|
|
|
|
|
2011-08-18 17:21:47 +03:00
|
|
|
#if CONFIG_BUFFERSINK_FILTER
|
|
|
|
|
2012-06-27 00:27:59 +03:00
|
|
|
static av_cold int vsink_init(AVFilterContext *ctx, const char *args)
|
2011-08-18 17:21:47 +03:00
|
|
|
{
|
|
|
|
BufferSinkContext *buf = ctx->priv;
|
2012-06-27 00:27:59 +03:00
|
|
|
AVBufferSinkParams *params = NULL;
|
2011-08-18 17:21:47 +03:00
|
|
|
|
2012-06-27 00:27:59 +03:00
|
|
|
// if(args && !strcmp(args, "opaque"))
|
|
|
|
// params = (AVBufferSinkParams *)(args+7);
|
|
|
|
|
|
|
|
if (!params) {
|
2012-04-17 05:01:17 +03:00
|
|
|
av_log(ctx, AV_LOG_WARNING,
|
2011-08-18 17:21:47 +03:00
|
|
|
"No opaque field provided\n");
|
2012-04-17 05:01:17 +03:00
|
|
|
buf->pixel_fmts = NULL;
|
2011-08-18 17:21:47 +03:00
|
|
|
} else {
|
2011-12-20 15:08:57 +03:00
|
|
|
const int *pixel_fmts = params->pixel_fmts;
|
2012-06-25 21:24:50 +03:00
|
|
|
|
2011-12-20 15:08:57 +03:00
|
|
|
buf->pixel_fmts = ff_copy_int_list(pixel_fmts);
|
|
|
|
if (!buf->pixel_fmts)
|
|
|
|
return AVERROR(ENOMEM);
|
2011-08-18 17:21:47 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
return common_init(ctx);
|
|
|
|
}
|
|
|
|
|
2011-12-20 15:08:57 +03:00
|
|
|
static av_cold void vsink_uninit(AVFilterContext *ctx)
|
|
|
|
{
|
|
|
|
BufferSinkContext *buf = ctx->priv;
|
|
|
|
av_freep(&buf->pixel_fmts);
|
2012-06-10 04:30:12 +03:00
|
|
|
common_uninit(ctx);
|
2011-12-20 15:08:57 +03:00
|
|
|
}
|
|
|
|
|
2011-08-18 17:21:47 +03:00
|
|
|
static int vsink_query_formats(AVFilterContext *ctx)
|
|
|
|
{
|
|
|
|
BufferSinkContext *buf = ctx->priv;
|
2011-06-11 19:43:11 +03:00
|
|
|
|
2012-04-17 05:01:17 +03:00
|
|
|
if (buf->pixel_fmts)
|
2012-06-16 12:47:46 +03:00
|
|
|
ff_set_common_formats(ctx, ff_make_format_list(buf->pixel_fmts));
|
2012-04-17 05:01:17 +03:00
|
|
|
else
|
2012-06-16 12:47:46 +03:00
|
|
|
ff_default_query_formats(ctx);
|
2012-04-17 05:01:17 +03:00
|
|
|
|
2011-06-11 19:43:11 +03:00
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
AVFilter avfilter_vsink_buffersink = {
|
|
|
|
.name = "buffersink",
|
2011-06-25 22:53:21 +03:00
|
|
|
.description = NULL_IF_CONFIG_SMALL("Buffer video frames, and make them available to the end of the filter graph."),
|
2011-06-11 19:43:11 +03:00
|
|
|
.priv_size = sizeof(BufferSinkContext),
|
2011-08-18 17:21:47 +03:00
|
|
|
.init = vsink_init,
|
2011-12-20 15:08:57 +03:00
|
|
|
.uninit = vsink_uninit,
|
2011-06-11 19:43:11 +03:00
|
|
|
|
2011-08-18 17:21:47 +03:00
|
|
|
.query_formats = vsink_query_formats,
|
2011-06-11 19:43:11 +03:00
|
|
|
|
2011-11-05 16:39:24 +03:00
|
|
|
.inputs = (const AVFilterPad[]) {{ .name = "default",
|
2011-06-11 19:43:11 +03:00
|
|
|
.type = AVMEDIA_TYPE_VIDEO,
|
|
|
|
.end_frame = end_frame,
|
|
|
|
.min_perms = AV_PERM_READ, },
|
|
|
|
{ .name = NULL }},
|
2011-11-05 16:39:24 +03:00
|
|
|
.outputs = (const AVFilterPad[]) {{ .name = NULL }},
|
2011-06-11 19:43:11 +03:00
|
|
|
};
|
2011-08-18 17:21:47 +03:00
|
|
|
|
|
|
|
#endif /* CONFIG_BUFFERSINK_FILTER */
|
|
|
|
|
|
|
|
#if CONFIG_ABUFFERSINK_FILTER
|
|
|
|
|
|
|
|
static void filter_samples(AVFilterLink *link, AVFilterBufferRef *samplesref)
|
|
|
|
{
|
|
|
|
end_frame(link);
|
|
|
|
}
|
|
|
|
|
2012-06-27 00:27:59 +03:00
|
|
|
static av_cold int asink_init(AVFilterContext *ctx, const char *args)
|
2011-08-18 17:21:47 +03:00
|
|
|
{
|
|
|
|
BufferSinkContext *buf = ctx->priv;
|
2012-06-27 00:27:59 +03:00
|
|
|
AVABufferSinkParams *params = NULL;
|
|
|
|
|
|
|
|
// if(args && !strcmp(args, "opaque"))
|
|
|
|
// params = (AVABufferSinkParams *)(args+7);
|
2011-08-18 17:21:47 +03:00
|
|
|
|
2012-06-25 00:28:29 +03:00
|
|
|
if (params && params->sample_fmts) {
|
|
|
|
buf->sample_fmts = ff_copy_int_list (params->sample_fmts);
|
|
|
|
if (!buf->sample_fmts)
|
|
|
|
goto fail_enomem;
|
2011-12-20 15:08:57 +03:00
|
|
|
}
|
2012-06-25 00:28:29 +03:00
|
|
|
if (params && params->channel_layouts) {
|
|
|
|
buf->channel_layouts = ff_copy_int64_list(params->channel_layouts);
|
|
|
|
if (!buf->channel_layouts)
|
|
|
|
goto fail_enomem;
|
|
|
|
}
|
|
|
|
if (!common_init(ctx))
|
|
|
|
return 0;
|
2011-08-18 17:21:47 +03:00
|
|
|
|
2012-06-25 00:28:29 +03:00
|
|
|
fail_enomem:
|
|
|
|
av_freep(&buf->sample_fmts);
|
|
|
|
av_freep(&buf->channel_layouts);
|
|
|
|
return AVERROR(ENOMEM);
|
2011-08-18 17:21:47 +03:00
|
|
|
}
|
|
|
|
|
2011-12-20 15:08:57 +03:00
|
|
|
static av_cold void asink_uninit(AVFilterContext *ctx)
|
|
|
|
{
|
|
|
|
BufferSinkContext *buf = ctx->priv;
|
|
|
|
|
|
|
|
av_freep(&buf->sample_fmts);
|
|
|
|
av_freep(&buf->channel_layouts);
|
2012-06-10 04:30:12 +03:00
|
|
|
common_uninit(ctx);
|
2011-12-20 15:08:57 +03:00
|
|
|
}
|
|
|
|
|
2011-08-18 17:21:47 +03:00
|
|
|
static int asink_query_formats(AVFilterContext *ctx)
|
|
|
|
{
|
|
|
|
BufferSinkContext *buf = ctx->priv;
|
|
|
|
AVFilterFormats *formats = NULL;
|
2012-05-16 03:27:31 +03:00
|
|
|
AVFilterChannelLayouts *layouts = NULL;
|
2011-08-18 17:21:47 +03:00
|
|
|
|
2012-06-25 00:28:29 +03:00
|
|
|
if (buf->sample_fmts) {
|
2012-06-16 12:47:46 +03:00
|
|
|
if (!(formats = ff_make_format_list(buf->sample_fmts)))
|
2011-08-18 17:21:47 +03:00
|
|
|
return AVERROR(ENOMEM);
|
2012-06-16 12:47:46 +03:00
|
|
|
ff_set_common_formats(ctx, formats);
|
2012-06-25 00:28:29 +03:00
|
|
|
}
|
2011-08-18 17:21:47 +03:00
|
|
|
|
2012-06-25 00:28:29 +03:00
|
|
|
if (buf->channel_layouts) {
|
2012-05-16 03:27:31 +03:00
|
|
|
if (!(layouts = avfilter_make_format64_list(buf->channel_layouts)))
|
2011-08-18 17:21:47 +03:00
|
|
|
return AVERROR(ENOMEM);
|
2012-05-16 03:27:31 +03:00
|
|
|
ff_set_common_channel_layouts(ctx, layouts);
|
2012-06-25 00:28:29 +03:00
|
|
|
}
|
2012-05-16 03:27:31 +03:00
|
|
|
|
2011-08-18 17:21:47 +03:00
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
AVFilter avfilter_asink_abuffersink = {
|
|
|
|
.name = "abuffersink",
|
|
|
|
.description = NULL_IF_CONFIG_SMALL("Buffer audio frames, and make them available to the end of the filter graph."),
|
|
|
|
.init = asink_init,
|
2011-12-20 15:08:57 +03:00
|
|
|
.uninit = asink_uninit,
|
2011-08-18 17:21:47 +03:00
|
|
|
.priv_size = sizeof(BufferSinkContext),
|
|
|
|
.query_formats = asink_query_formats,
|
|
|
|
|
2011-11-05 16:39:24 +03:00
|
|
|
.inputs = (const AVFilterPad[]) {{ .name = "default",
|
2011-08-18 17:21:47 +03:00
|
|
|
.type = AVMEDIA_TYPE_AUDIO,
|
|
|
|
.filter_samples = filter_samples,
|
|
|
|
.min_perms = AV_PERM_READ, },
|
|
|
|
{ .name = NULL }},
|
2011-11-05 16:39:24 +03:00
|
|
|
.outputs = (const AVFilterPad[]) {{ .name = NULL }},
|
2011-08-18 17:21:47 +03:00
|
|
|
};
|
|
|
|
|
|
|
|
#endif /* CONFIG_ABUFFERSINK_FILTER */
|