44f669e7 |
/*
* 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 |
3e3e414f |
* buffer sink |
44f669e7 |
*/
|
72f3786d |
#include "libavutil/avassert.h" |
1acd2f6b |
#include "libavutil/channel_layout.h" |
3560089e |
#include "libavutil/fifo.h" |
44f669e7 |
#include "avfilter.h" |
4f7dfe12 |
#include "buffersink.h" |
9de731e9 |
#include "audio.h" |
386aee68 |
#include "internal.h" |
44f669e7 |
|
c4415f6e |
AVBufferSinkParams *av_buffersink_params_alloc(void)
{ |
e870a7dd |
static const int pixel_fmts[] = { AV_PIX_FMT_NONE }; |
c4415f6e |
AVBufferSinkParams *params = av_malloc(sizeof(AVBufferSinkParams));
if (!params)
return NULL;
params->pixel_fmts = pixel_fmts;
return params;
}
AVABufferSinkParams *av_abuffersink_params_alloc(void)
{ |
e870a7dd |
static const int sample_fmts[] = { AV_SAMPLE_FMT_NONE }; |
c4415f6e |
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;
}
|
44f669e7 |
typedef struct { |
c4415f6e |
AVFifoBuffer *fifo; ///< FIFO buffer of video frame references |
fcf8706e |
unsigned warning_limit; |
c4415f6e |
/* only used for video */ |
ac627b3d |
enum AVPixelFormat *pixel_fmts; ///< list of accepted pixel formats, must be terminated with -1 |
c4415f6e |
/* only used for audio */ |
386aee68 |
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 |
44f669e7 |
} BufferSinkContext;
|
3560089e |
#define FIFO_INIT_SIZE 8
|
c4415f6e |
static av_cold int common_init(AVFilterContext *ctx) |
44f669e7 |
{
BufferSinkContext *buf = ctx->priv;
|
3560089e |
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);
} |
fcf8706e |
buf->warning_limit = 100; |
44f669e7 |
return 0;
}
|
c4415f6e |
static av_cold void common_uninit(AVFilterContext *ctx) |
44f669e7 |
{
BufferSinkContext *buf = ctx->priv; |
3560089e |
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;
} |
44f669e7 |
}
|
9de731e9 |
static int add_buffer_ref(AVFilterContext *ctx, AVFilterBufferRef *ref) |
44f669e7 |
{ |
9de731e9 |
BufferSinkContext *buf = ctx->priv; |
44f669e7 |
|
3560089e |
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"); |
88beb2df |
return AVERROR(ENOMEM); |
3560089e |
}
}
/* cache frame */ |
9de731e9 |
av_fifo_generic_write(buf->fifo, &ref, sizeof(AVFilterBufferRef *), NULL);
return 0;
}
|
c36302a7 |
static int filter_frame(AVFilterLink *inlink, AVFilterBufferRef *ref) |
9de731e9 |
{
AVFilterContext *ctx = inlink->dst;
BufferSinkContext *buf = inlink->dst->priv;
int ret;
|
c36302a7 |
if ((ret = add_buffer_ref(ctx, ref)) < 0) |
9de731e9 |
return ret; |
fcf8706e |
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;
} |
88beb2df |
return 0; |
44f669e7 |
}
|
1f5c1333 |
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;
}
|
c4415f6e |
int av_buffersink_get_buffer_ref(AVFilterContext *ctx,
AVFilterBufferRef **bufref, int flags) |
44f669e7 |
{
BufferSinkContext *buf = ctx->priv;
AVFilterLink *inlink = ctx->inputs[0];
int ret; |
c4415f6e |
*bufref = NULL; |
44f669e7 |
|
e4e02a7d |
av_assert0( !strcmp(ctx->filter->name, "buffersink")
|| !strcmp(ctx->filter->name, "abuffersink")
|| !strcmp(ctx->filter->name, "ffbuffersink")
|| !strcmp(ctx->filter->name, "ffabuffersink")); |
3013722c |
|
44f669e7 |
/* no picref available, fetch it from the filterchain */ |
3560089e |
if (!av_fifo_size(buf->fifo)) { |
e11110de |
if (flags & AV_BUFFERSINK_FLAG_NO_REQUEST)
return AVERROR(EAGAIN); |
1c600888 |
if ((ret = ff_request_frame(inlink)) < 0) |
44f669e7 |
return ret;
}
|
3560089e |
if (!av_fifo_size(buf->fifo)) |
44f669e7 |
return AVERROR(EINVAL);
|
c4415f6e |
if (flags & AV_BUFFERSINK_FLAG_PEEK)
*bufref = *((AVFilterBufferRef **)av_fifo_peek2(buf->fifo, 0)); |
3560089e |
else |
c4415f6e |
av_fifo_generic_read(buf->fifo, bufref, sizeof(*bufref), NULL);
return 0;
}
|
283cc059 |
AVRational av_buffersink_get_frame_rate(AVFilterContext *ctx)
{ |
e4e02a7d |
av_assert0( !strcmp(ctx->filter->name, "buffersink")
|| !strcmp(ctx->filter->name, "ffbuffersink")); |
3013722c |
|
283cc059 |
return ctx->inputs[0]->frame_rate;
}
|
a502939d |
int av_buffersink_poll_frame(AVFilterContext *ctx)
{
BufferSinkContext *buf = ctx->priv;
AVFilterLink *inlink = ctx->inputs[0];
|
e4e02a7d |
av_assert0( !strcmp(ctx->filter->name, "buffersink")
|| !strcmp(ctx->filter->name, "abuffersink")
|| !strcmp(ctx->filter->name, "ffbuffersink")
|| !strcmp(ctx->filter->name, "ffabuffersink")); |
3013722c |
|
c9e183b4 |
return av_fifo_size(buf->fifo)/sizeof(AVFilterBufferRef *) + ff_poll_frame(inlink); |
a502939d |
}
|
a5382b50 |
static av_cold int vsink_init(AVFilterContext *ctx, const char *args, void *opaque) |
c4415f6e |
{
BufferSinkContext *buf = ctx->priv; |
a5382b50 |
AVBufferSinkParams *params = opaque; |
c4415f6e |
|
225efcce |
if (params && params->pixel_fmts) { |
386aee68 |
const int *pixel_fmts = params->pixel_fmts; |
8dd0e87d |
|
386aee68 |
buf->pixel_fmts = ff_copy_int_list(pixel_fmts);
if (!buf->pixel_fmts)
return AVERROR(ENOMEM); |
c4415f6e |
}
return common_init(ctx);
}
|
386aee68 |
static av_cold void vsink_uninit(AVFilterContext *ctx)
{
BufferSinkContext *buf = ctx->priv;
av_freep(&buf->pixel_fmts); |
d35c27db |
common_uninit(ctx); |
386aee68 |
}
|
c4415f6e |
static int vsink_query_formats(AVFilterContext *ctx)
{
BufferSinkContext *buf = ctx->priv; |
44f669e7 |
|
47787831 |
if (buf->pixel_fmts) |
c9e183b4 |
ff_set_common_formats(ctx, ff_make_format_list(buf->pixel_fmts)); |
47787831 |
else |
c9e183b4 |
ff_default_query_formats(ctx); |
47787831 |
|
44f669e7 |
return 0;
}
|
2d9d4440 |
static const AVFilterPad ffbuffersink_inputs[] = {
{
.name = "default",
.type = AVMEDIA_TYPE_VIDEO, |
c36302a7 |
.filter_frame = filter_frame, |
2d9d4440 |
.min_perms = AV_PERM_READ | AV_PERM_PRESERVE,
},
{ NULL },
};
|
e4e02a7d |
AVFilter avfilter_vsink_ffbuffersink = {
.name = "ffbuffersink",
.description = NULL_IF_CONFIG_SMALL("Buffer video frames, and make them available to the end of the filter graph."),
.priv_size = sizeof(BufferSinkContext),
.init_opaque = vsink_init,
.uninit = vsink_uninit,
.query_formats = vsink_query_formats, |
2d9d4440 |
.inputs = ffbuffersink_inputs,
.outputs = NULL,
}; |
e4e02a7d |
|
2d9d4440 |
static const AVFilterPad buffersink_inputs[] = {
{
.name = "default",
.type = AVMEDIA_TYPE_VIDEO, |
c36302a7 |
.filter_frame = filter_frame, |
2d9d4440 |
.min_perms = AV_PERM_READ | AV_PERM_PRESERVE,
},
{ NULL }, |
e4e02a7d |
};
|
44f669e7 |
AVFilter avfilter_vsink_buffersink = {
.name = "buffersink", |
5aca97e2 |
.description = NULL_IF_CONFIG_SMALL("Buffer video frames, and make them available to the end of the filter graph."), |
44f669e7 |
.priv_size = sizeof(BufferSinkContext), |
a5382b50 |
.init_opaque = vsink_init, |
386aee68 |
.uninit = vsink_uninit, |
44f669e7 |
|
c4415f6e |
.query_formats = vsink_query_formats, |
2d9d4440 |
.inputs = buffersink_inputs,
.outputs = NULL, |
44f669e7 |
}; |
c4415f6e |
|
a5382b50 |
static av_cold int asink_init(AVFilterContext *ctx, const char *args, void *opaque) |
c4415f6e |
{
BufferSinkContext *buf = ctx->priv; |
a5382b50 |
AVABufferSinkParams *params = opaque; |
c4415f6e |
|
784675ca |
if (params && params->sample_fmts) {
buf->sample_fmts = ff_copy_int_list (params->sample_fmts);
if (!buf->sample_fmts)
goto fail_enomem; |
386aee68 |
} |
784675ca |
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; |
c4415f6e |
|
784675ca |
fail_enomem:
av_freep(&buf->sample_fmts);
av_freep(&buf->channel_layouts);
return AVERROR(ENOMEM); |
c4415f6e |
}
|
386aee68 |
static av_cold void asink_uninit(AVFilterContext *ctx)
{
BufferSinkContext *buf = ctx->priv;
av_freep(&buf->sample_fmts);
av_freep(&buf->channel_layouts); |
d35c27db |
common_uninit(ctx); |
386aee68 |
}
|
c4415f6e |
static int asink_query_formats(AVFilterContext *ctx)
{
BufferSinkContext *buf = ctx->priv;
AVFilterFormats *formats = NULL; |
1cbf7fb4 |
AVFilterChannelLayouts *layouts = NULL; |
c4415f6e |
|
784675ca |
if (buf->sample_fmts) { |
c9e183b4 |
if (!(formats = ff_make_format_list(buf->sample_fmts))) |
c4415f6e |
return AVERROR(ENOMEM); |
c9e183b4 |
ff_set_common_formats(ctx, formats); |
784675ca |
} |
c4415f6e |
|
784675ca |
if (buf->channel_layouts) { |
1cbf7fb4 |
if (!(layouts = avfilter_make_format64_list(buf->channel_layouts))) |
c4415f6e |
return AVERROR(ENOMEM); |
1cbf7fb4 |
ff_set_common_channel_layouts(ctx, layouts); |
784675ca |
} |
1cbf7fb4 |
|
c4415f6e |
return 0;
}
|
2d9d4440 |
static const AVFilterPad ffabuffersink_inputs[] = {
{
.name = "default",
.type = AVMEDIA_TYPE_AUDIO,
.filter_frame = filter_frame,
.min_perms = AV_PERM_READ | AV_PERM_PRESERVE,
},
{ NULL },
};
|
e4e02a7d |
AVFilter avfilter_asink_ffabuffersink = {
.name = "ffabuffersink",
.description = NULL_IF_CONFIG_SMALL("Buffer audio frames, and make them available to the end of the filter graph."),
.init_opaque = asink_init,
.uninit = asink_uninit,
.priv_size = sizeof(BufferSinkContext),
.query_formats = asink_query_formats, |
2d9d4440 |
.inputs = ffabuffersink_inputs,
.outputs = NULL,
}; |
e4e02a7d |
|
2d9d4440 |
static const AVFilterPad abuffersink_inputs[] = {
{
.name = "default",
.type = AVMEDIA_TYPE_AUDIO,
.filter_frame = filter_frame,
.min_perms = AV_PERM_READ | AV_PERM_PRESERVE,
},
{ NULL }, |
e4e02a7d |
};
|
c4415f6e |
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."), |
a5382b50 |
.init_opaque = asink_init, |
386aee68 |
.uninit = asink_uninit, |
c4415f6e |
.priv_size = sizeof(BufferSinkContext),
.query_formats = asink_query_formats, |
2d9d4440 |
.inputs = abuffersink_inputs,
.outputs = NULL, |
c4415f6e |
}; |
9de731e9 |
/* Libav compatibility API */
extern AVFilter avfilter_vsink_buffer;
extern AVFilter avfilter_asink_abuffer;
int av_buffersink_read(AVFilterContext *ctx, AVFilterBufferRef **buf)
{
AVFilterBufferRef *tbuf;
int ret;
if (ctx->filter-> inputs[0].start_frame ==
avfilter_vsink_buffer. inputs[0].start_frame || |
cd7febd3 |
ctx->filter-> inputs[0].filter_frame ==
avfilter_asink_abuffer.inputs[0].filter_frame) |
9de731e9 |
return ff_buffersink_read_compat(ctx, buf);
av_assert0(ctx->filter-> inputs[0].end_frame ==
avfilter_vsink_ffbuffersink. inputs[0].end_frame || |
cd7febd3 |
ctx->filter-> inputs[0].filter_frame ==
avfilter_asink_ffabuffersink.inputs[0].filter_frame); |
9de731e9 |
ret = av_buffersink_get_buffer_ref(ctx, &tbuf,
buf ? 0 : AV_BUFFERSINK_FLAG_PEEK);
if (!buf)
return ret >= 0;
if (ret < 0)
return ret;
*buf = tbuf;
return 0;
}
int av_buffersink_read_samples(AVFilterContext *ctx, AVFilterBufferRef **buf,
int nb_samples)
{
BufferSinkContext *sink = ctx->priv;
int ret = 0, have_samples = 0, need_samples;
AVFilterBufferRef *tbuf, *in_buf;
AVFilterLink *link = ctx->inputs[0];
int nb_channels = av_get_channel_layout_nb_channels(link->channel_layout);
|
cd7febd3 |
if (ctx->filter-> inputs[0].filter_frame ==
avfilter_asink_abuffer.inputs[0].filter_frame) |
9de731e9 |
return ff_buffersink_read_samples_compat(ctx, buf, nb_samples); |
cd7febd3 |
av_assert0(ctx->filter-> inputs[0].filter_frame ==
avfilter_asink_ffabuffersink.inputs[0].filter_frame); |
9de731e9 |
tbuf = ff_get_audio_buffer(link, AV_PERM_WRITE, nb_samples);
if (!tbuf)
return AVERROR(ENOMEM);
while (have_samples < nb_samples) {
ret = av_buffersink_get_buffer_ref(ctx, &in_buf,
AV_BUFFERSINK_FLAG_PEEK);
if (ret < 0) {
if (ret == AVERROR_EOF && have_samples) {
nb_samples = have_samples;
ret = 0;
}
break;
}
need_samples = FFMIN(in_buf->audio->nb_samples,
nb_samples - have_samples);
av_samples_copy(tbuf->extended_data, in_buf->extended_data,
have_samples, 0, need_samples,
nb_channels, in_buf->format);
have_samples += need_samples;
if (need_samples < in_buf->audio->nb_samples) {
in_buf->audio->nb_samples -= need_samples;
av_samples_copy(in_buf->extended_data, in_buf->extended_data,
0, need_samples, in_buf->audio->nb_samples,
nb_channels, in_buf->format);
} else {
av_buffersink_get_buffer_ref(ctx, &in_buf, 0);
avfilter_unref_buffer(in_buf);
}
}
tbuf->audio->nb_samples = have_samples;
if (ret < 0) {
av_assert0(!av_fifo_size(sink->fifo));
if (have_samples)
add_buffer_ref(ctx, tbuf);
else
avfilter_unref_buffer(tbuf);
return ret;
}
*buf = tbuf;
return 0;
} |