2011-06-11 18:43:11 +02: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 01:10:28 +02:00
|
|
|
* buffer sink
|
2011-06-11 18:43:11 +02:00
|
|
|
*/
|
|
|
|
|
2012-04-24 12:51:37 +02:00
|
|
|
#include "libavutil/avassert.h"
|
2012-11-13 14:16:48 +01:00
|
|
|
#include "libavutil/channel_layout.h"
|
2011-06-29 17:31:16 +02:00
|
|
|
#include "libavutil/fifo.h"
|
2011-06-11 18:43:11 +02:00
|
|
|
#include "avfilter.h"
|
2011-09-06 18:37:44 +02:00
|
|
|
#include "buffersink.h"
|
2012-09-23 20:45:49 +02:00
|
|
|
#include "audio.h"
|
2011-12-20 13:08:57 +01:00
|
|
|
#include "internal.h"
|
2011-06-11 18:43:11 +02:00
|
|
|
|
2011-08-18 16:21:47 +02:00
|
|
|
AVBufferSinkParams *av_buffersink_params_alloc(void)
|
|
|
|
{
|
2012-11-05 14:34:08 +01:00
|
|
|
static const int pixel_fmts[] = { AV_PIX_FMT_NONE };
|
2011-08-18 16:21:47 +02:00
|
|
|
AVBufferSinkParams *params = av_malloc(sizeof(AVBufferSinkParams));
|
|
|
|
if (!params)
|
|
|
|
return NULL;
|
|
|
|
|
|
|
|
params->pixel_fmts = pixel_fmts;
|
|
|
|
return params;
|
|
|
|
}
|
|
|
|
|
|
|
|
AVABufferSinkParams *av_abuffersink_params_alloc(void)
|
|
|
|
{
|
2012-12-26 16:49:16 +01:00
|
|
|
AVABufferSinkParams *params = av_mallocz(sizeof(AVABufferSinkParams));
|
2011-08-18 16:21:47 +02:00
|
|
|
|
|
|
|
if (!params)
|
|
|
|
return NULL;
|
|
|
|
return params;
|
|
|
|
}
|
|
|
|
|
2011-06-11 18:43:11 +02:00
|
|
|
typedef struct {
|
2011-08-18 16:21:47 +02:00
|
|
|
AVFifoBuffer *fifo; ///< FIFO buffer of video frame references
|
2012-06-28 11:21:42 +02:00
|
|
|
unsigned warning_limit;
|
2011-08-18 16:21:47 +02:00
|
|
|
|
|
|
|
/* only used for video */
|
2012-10-08 20:54:00 +02:00
|
|
|
enum AVPixelFormat *pixel_fmts; ///< list of accepted pixel formats, must be terminated with -1
|
2011-08-18 16:21:47 +02:00
|
|
|
|
|
|
|
/* only used for audio */
|
2011-12-20 13:08:57 +01: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
|
2012-12-26 16:49:16 +01:00
|
|
|
int all_channel_counts;
|
2013-02-03 19:33:58 +01:00
|
|
|
int *sample_rates; ///< list of accepted sample rates, terminated by -1
|
2011-06-11 18:43:11 +02:00
|
|
|
} BufferSinkContext;
|
|
|
|
|
2011-06-29 17:31:16 +02:00
|
|
|
#define FIFO_INIT_SIZE 8
|
|
|
|
|
2011-08-18 16:21:47 +02:00
|
|
|
static av_cold int common_init(AVFilterContext *ctx)
|
2011-06-11 18:43:11 +02:00
|
|
|
{
|
|
|
|
BufferSinkContext *buf = ctx->priv;
|
|
|
|
|
2011-06-29 17:31:16 +02: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 11:21:42 +02:00
|
|
|
buf->warning_limit = 100;
|
2011-06-11 18:43:11 +02:00
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2011-08-18 16:21:47 +02:00
|
|
|
static av_cold void common_uninit(AVFilterContext *ctx)
|
2011-06-11 18:43:11 +02:00
|
|
|
{
|
|
|
|
BufferSinkContext *buf = ctx->priv;
|
2011-06-29 17:31:16 +02: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 18:43:11 +02:00
|
|
|
}
|
|
|
|
|
2012-09-23 20:45:49 +02:00
|
|
|
static int add_buffer_ref(AVFilterContext *ctx, AVFilterBufferRef *ref)
|
2011-06-11 18:43:11 +02:00
|
|
|
{
|
2012-09-23 20:45:49 +02:00
|
|
|
BufferSinkContext *buf = ctx->priv;
|
2011-06-11 18:43:11 +02:00
|
|
|
|
2011-06-29 17:31:16 +02: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");
|
2012-07-22 22:57:02 +02:00
|
|
|
return AVERROR(ENOMEM);
|
2011-06-29 17:31:16 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/* cache frame */
|
2012-09-23 20:45:49 +02:00
|
|
|
av_fifo_generic_write(buf->fifo, &ref, sizeof(AVFilterBufferRef *), NULL);
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2012-12-27 00:43:12 +01:00
|
|
|
static int filter_frame(AVFilterLink *inlink, AVFilterBufferRef *ref)
|
2012-09-23 20:45:49 +02:00
|
|
|
{
|
|
|
|
AVFilterContext *ctx = inlink->dst;
|
|
|
|
BufferSinkContext *buf = inlink->dst->priv;
|
|
|
|
int ret;
|
|
|
|
|
2012-12-27 00:43:12 +01:00
|
|
|
if ((ret = add_buffer_ref(ctx, ref)) < 0)
|
2012-09-23 20:45:49 +02:00
|
|
|
return ret;
|
2012-06-28 11:21:42 +02: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;
|
|
|
|
}
|
2012-07-22 22:57:02 +02:00
|
|
|
return 0;
|
2011-06-11 18:43:11 +02:00
|
|
|
}
|
|
|
|
|
2012-06-25 00:30:15 +02: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 16:21:47 +02:00
|
|
|
int av_buffersink_get_buffer_ref(AVFilterContext *ctx,
|
|
|
|
AVFilterBufferRef **bufref, int flags)
|
2011-06-11 18:43:11 +02:00
|
|
|
{
|
|
|
|
BufferSinkContext *buf = ctx->priv;
|
|
|
|
AVFilterLink *inlink = ctx->inputs[0];
|
|
|
|
int ret;
|
2011-08-18 16:21:47 +02:00
|
|
|
*bufref = NULL;
|
2011-06-11 18:43:11 +02:00
|
|
|
|
2012-08-31 22:02:08 +02:00
|
|
|
av_assert0( !strcmp(ctx->filter->name, "buffersink")
|
|
|
|
|| !strcmp(ctx->filter->name, "abuffersink")
|
|
|
|
|| !strcmp(ctx->filter->name, "ffbuffersink")
|
|
|
|
|| !strcmp(ctx->filter->name, "ffabuffersink"));
|
2012-06-24 14:17:14 +02:00
|
|
|
|
2011-06-11 18:43:11 +02:00
|
|
|
/* no picref available, fetch it from the filterchain */
|
2011-06-29 17:31:16 +02:00
|
|
|
if (!av_fifo_size(buf->fifo)) {
|
2012-04-19 17:08:27 +02:00
|
|
|
if (flags & AV_BUFFERSINK_FLAG_NO_REQUEST)
|
|
|
|
return AVERROR(EAGAIN);
|
2012-06-24 02:09:53 +02:00
|
|
|
if ((ret = ff_request_frame(inlink)) < 0)
|
2011-06-11 18:43:11 +02:00
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
2011-06-29 17:31:16 +02:00
|
|
|
if (!av_fifo_size(buf->fifo))
|
2011-06-11 18:43:11 +02:00
|
|
|
return AVERROR(EINVAL);
|
|
|
|
|
2011-08-18 16:21:47 +02:00
|
|
|
if (flags & AV_BUFFERSINK_FLAG_PEEK)
|
|
|
|
*bufref = *((AVFilterBufferRef **)av_fifo_peek2(buf->fifo, 0));
|
2011-06-29 17:31:16 +02:00
|
|
|
else
|
2011-08-18 16:21:47 +02:00
|
|
|
av_fifo_generic_read(buf->fifo, bufref, sizeof(*bufref), NULL);
|
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2012-06-05 12:40:37 +02:00
|
|
|
AVRational av_buffersink_get_frame_rate(AVFilterContext *ctx)
|
|
|
|
{
|
2012-08-31 22:02:08 +02:00
|
|
|
av_assert0( !strcmp(ctx->filter->name, "buffersink")
|
|
|
|
|| !strcmp(ctx->filter->name, "ffbuffersink"));
|
2012-06-24 14:17:14 +02:00
|
|
|
|
2012-06-05 12:40:37 +02:00
|
|
|
return ctx->inputs[0]->frame_rate;
|
|
|
|
}
|
|
|
|
|
2011-12-06 17:02:15 +01:00
|
|
|
int av_buffersink_poll_frame(AVFilterContext *ctx)
|
|
|
|
{
|
|
|
|
BufferSinkContext *buf = ctx->priv;
|
|
|
|
AVFilterLink *inlink = ctx->inputs[0];
|
|
|
|
|
2012-08-31 22:02:08 +02:00
|
|
|
av_assert0( !strcmp(ctx->filter->name, "buffersink")
|
|
|
|
|| !strcmp(ctx->filter->name, "abuffersink")
|
|
|
|
|| !strcmp(ctx->filter->name, "ffbuffersink")
|
|
|
|
|| !strcmp(ctx->filter->name, "ffabuffersink"));
|
2012-06-24 14:17:14 +02:00
|
|
|
|
2012-06-16 11:47:46 +02:00
|
|
|
return av_fifo_size(buf->fifo)/sizeof(AVFilterBufferRef *) + ff_poll_frame(inlink);
|
2011-12-06 17:02:15 +01:00
|
|
|
}
|
|
|
|
|
2012-07-06 01:06:20 +02:00
|
|
|
static av_cold int vsink_init(AVFilterContext *ctx, const char *args, void *opaque)
|
2011-08-18 16:21:47 +02:00
|
|
|
{
|
|
|
|
BufferSinkContext *buf = ctx->priv;
|
2012-07-06 01:06:20 +02:00
|
|
|
AVBufferSinkParams *params = opaque;
|
2011-08-18 16:21:47 +02:00
|
|
|
|
2012-09-22 10:59:40 +02:00
|
|
|
if (params && params->pixel_fmts) {
|
2011-12-20 13:08:57 +01:00
|
|
|
const int *pixel_fmts = params->pixel_fmts;
|
2012-06-25 20:24:50 +02:00
|
|
|
|
2011-12-20 13:08:57 +01:00
|
|
|
buf->pixel_fmts = ff_copy_int_list(pixel_fmts);
|
|
|
|
if (!buf->pixel_fmts)
|
|
|
|
return AVERROR(ENOMEM);
|
2011-08-18 16:21:47 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
return common_init(ctx);
|
|
|
|
}
|
|
|
|
|
2011-12-20 13:08:57 +01:00
|
|
|
static av_cold void vsink_uninit(AVFilterContext *ctx)
|
|
|
|
{
|
|
|
|
BufferSinkContext *buf = ctx->priv;
|
|
|
|
av_freep(&buf->pixel_fmts);
|
2012-06-10 03:30:12 +02:00
|
|
|
common_uninit(ctx);
|
2011-12-20 13:08:57 +01:00
|
|
|
}
|
|
|
|
|
2011-08-18 16:21:47 +02:00
|
|
|
static int vsink_query_formats(AVFilterContext *ctx)
|
|
|
|
{
|
|
|
|
BufferSinkContext *buf = ctx->priv;
|
2011-06-11 18:43:11 +02:00
|
|
|
|
2012-04-17 04:01:17 +02:00
|
|
|
if (buf->pixel_fmts)
|
2012-06-16 11:47:46 +02:00
|
|
|
ff_set_common_formats(ctx, ff_make_format_list(buf->pixel_fmts));
|
2012-04-17 04:01:17 +02:00
|
|
|
else
|
2012-06-16 11:47:46 +02:00
|
|
|
ff_default_query_formats(ctx);
|
2012-04-17 04:01:17 +02:00
|
|
|
|
2011-06-11 18:43:11 +02:00
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2012-11-28 20:01:59 +01:00
|
|
|
static const AVFilterPad ffbuffersink_inputs[] = {
|
|
|
|
{
|
|
|
|
.name = "default",
|
|
|
|
.type = AVMEDIA_TYPE_VIDEO,
|
2012-12-27 00:43:12 +01:00
|
|
|
.filter_frame = filter_frame,
|
2012-11-28 20:01:59 +01:00
|
|
|
.min_perms = AV_PERM_READ | AV_PERM_PRESERVE,
|
|
|
|
},
|
|
|
|
{ NULL },
|
|
|
|
};
|
|
|
|
|
2012-08-31 22:02:08 +02:00
|
|
|
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,
|
2012-11-28 20:01:59 +01:00
|
|
|
.inputs = ffbuffersink_inputs,
|
|
|
|
.outputs = NULL,
|
|
|
|
};
|
2012-08-31 22:02:08 +02:00
|
|
|
|
2012-11-28 20:01:59 +01:00
|
|
|
static const AVFilterPad buffersink_inputs[] = {
|
|
|
|
{
|
|
|
|
.name = "default",
|
|
|
|
.type = AVMEDIA_TYPE_VIDEO,
|
2012-12-27 00:43:12 +01:00
|
|
|
.filter_frame = filter_frame,
|
2012-11-28 20:01:59 +01:00
|
|
|
.min_perms = AV_PERM_READ | AV_PERM_PRESERVE,
|
|
|
|
},
|
|
|
|
{ NULL },
|
2012-08-31 22:02:08 +02:00
|
|
|
};
|
|
|
|
|
2011-06-11 18:43:11 +02:00
|
|
|
AVFilter avfilter_vsink_buffersink = {
|
|
|
|
.name = "buffersink",
|
2011-06-25 21:53:21 +02:00
|
|
|
.description = NULL_IF_CONFIG_SMALL("Buffer video frames, and make them available to the end of the filter graph."),
|
2011-06-11 18:43:11 +02:00
|
|
|
.priv_size = sizeof(BufferSinkContext),
|
2012-07-06 01:06:20 +02:00
|
|
|
.init_opaque = vsink_init,
|
2011-12-20 13:08:57 +01:00
|
|
|
.uninit = vsink_uninit,
|
2011-06-11 18:43:11 +02:00
|
|
|
|
2011-08-18 16:21:47 +02:00
|
|
|
.query_formats = vsink_query_formats,
|
2012-11-28 20:01:59 +01:00
|
|
|
.inputs = buffersink_inputs,
|
|
|
|
.outputs = NULL,
|
2011-06-11 18:43:11 +02:00
|
|
|
};
|
2011-08-18 16:21:47 +02:00
|
|
|
|
2012-12-26 16:49:16 +01:00
|
|
|
static int64_t *concat_channels_lists(const int64_t *layouts, const int *counts)
|
|
|
|
{
|
|
|
|
int nb_layouts = 0, nb_counts = 0, i;
|
|
|
|
int64_t *list;
|
|
|
|
|
|
|
|
if (layouts)
|
|
|
|
for (; layouts[nb_layouts] != -1; nb_layouts++);
|
|
|
|
if (counts)
|
|
|
|
for (; counts[nb_counts] != -1; nb_counts++);
|
|
|
|
if (nb_counts > INT_MAX - 1 - nb_layouts)
|
|
|
|
return NULL;
|
|
|
|
if (!(list = av_calloc(nb_layouts + nb_counts + 1, sizeof(*list))))
|
|
|
|
return NULL;
|
|
|
|
for (i = 0; i < nb_layouts; i++)
|
|
|
|
list[i] = layouts[i];
|
|
|
|
for (i = 0; i < nb_counts; i++)
|
|
|
|
list[nb_layouts + i] = FF_COUNT2LAYOUT(counts[i]);
|
|
|
|
list[nb_layouts + nb_counts] = -1;
|
|
|
|
return list;
|
|
|
|
}
|
|
|
|
|
2012-07-06 01:06:20 +02:00
|
|
|
static av_cold int asink_init(AVFilterContext *ctx, const char *args, void *opaque)
|
2011-08-18 16:21:47 +02:00
|
|
|
{
|
|
|
|
BufferSinkContext *buf = ctx->priv;
|
2012-07-06 01:06:20 +02:00
|
|
|
AVABufferSinkParams *params = opaque;
|
2011-08-18 16:21:47 +02:00
|
|
|
|
2012-06-24 23:28:29 +02:00
|
|
|
if (params && params->sample_fmts) {
|
2013-02-03 21:12:37 +01:00
|
|
|
buf->sample_fmts = ff_copy_int_list(params->sample_fmts);
|
2012-06-24 23:28:29 +02:00
|
|
|
if (!buf->sample_fmts)
|
2012-12-26 16:49:16 +01:00
|
|
|
return AVERROR(ENOMEM);
|
2011-12-20 13:08:57 +01:00
|
|
|
}
|
2013-02-03 19:33:58 +01:00
|
|
|
if (params && params->sample_rates) {
|
|
|
|
buf->sample_rates = ff_copy_int_list(params->sample_rates);
|
|
|
|
if (!buf->sample_rates)
|
|
|
|
return AVERROR(ENOMEM);
|
|
|
|
}
|
2012-12-26 16:49:16 +01:00
|
|
|
if (params && (params->channel_layouts || params->channel_counts)) {
|
|
|
|
if (params->all_channel_counts) {
|
|
|
|
av_log(ctx, AV_LOG_ERROR,
|
|
|
|
"Conflicting all_channel_counts and list in parameters\n");
|
|
|
|
return AVERROR(EINVAL);
|
|
|
|
}
|
|
|
|
buf->channel_layouts = concat_channels_lists(params->channel_layouts,
|
|
|
|
params->channel_counts);
|
2012-06-24 23:28:29 +02:00
|
|
|
if (!buf->channel_layouts)
|
2012-12-26 16:49:16 +01:00
|
|
|
return AVERROR(ENOMEM);
|
2012-06-24 23:28:29 +02:00
|
|
|
}
|
2012-12-26 16:49:16 +01:00
|
|
|
if (params)
|
|
|
|
buf->all_channel_counts = params->all_channel_counts;
|
|
|
|
return common_init(ctx);
|
2011-08-18 16:21:47 +02:00
|
|
|
}
|
|
|
|
|
2011-12-20 13:08:57 +01:00
|
|
|
static av_cold void asink_uninit(AVFilterContext *ctx)
|
|
|
|
{
|
|
|
|
BufferSinkContext *buf = ctx->priv;
|
|
|
|
|
|
|
|
av_freep(&buf->sample_fmts);
|
2013-02-03 19:33:58 +01:00
|
|
|
av_freep(&buf->sample_rates);
|
2011-12-20 13:08:57 +01:00
|
|
|
av_freep(&buf->channel_layouts);
|
2012-06-10 03:30:12 +02:00
|
|
|
common_uninit(ctx);
|
2011-12-20 13:08:57 +01:00
|
|
|
}
|
|
|
|
|
2011-08-18 16:21:47 +02:00
|
|
|
static int asink_query_formats(AVFilterContext *ctx)
|
|
|
|
{
|
|
|
|
BufferSinkContext *buf = ctx->priv;
|
|
|
|
AVFilterFormats *formats = NULL;
|
2012-05-16 02:27:31 +02:00
|
|
|
AVFilterChannelLayouts *layouts = NULL;
|
2011-08-18 16:21:47 +02:00
|
|
|
|
2012-06-24 23:28:29 +02:00
|
|
|
if (buf->sample_fmts) {
|
2013-02-03 21:12:37 +01:00
|
|
|
if (!(formats = ff_make_format_list(buf->sample_fmts)))
|
|
|
|
return AVERROR(ENOMEM);
|
|
|
|
ff_set_common_formats(ctx, formats);
|
2012-06-24 23:28:29 +02:00
|
|
|
}
|
2011-08-18 16:21:47 +02:00
|
|
|
|
2012-12-26 16:49:16 +01:00
|
|
|
if (buf->channel_layouts || buf->all_channel_counts) {
|
|
|
|
layouts = buf->all_channel_counts ? ff_all_channel_counts() :
|
|
|
|
avfilter_make_format64_list(buf->channel_layouts);
|
|
|
|
if (!layouts)
|
|
|
|
return AVERROR(ENOMEM);
|
|
|
|
ff_set_common_channel_layouts(ctx, layouts);
|
2012-06-24 23:28:29 +02:00
|
|
|
}
|
2012-05-16 02:27:31 +02:00
|
|
|
|
2013-02-03 19:33:58 +01:00
|
|
|
if (buf->sample_rates) {
|
|
|
|
formats = ff_make_format_list(buf->sample_rates);
|
|
|
|
if (!formats)
|
|
|
|
return AVERROR(ENOMEM);
|
|
|
|
ff_set_common_samplerates(ctx, formats);
|
|
|
|
}
|
|
|
|
|
2011-08-18 16:21:47 +02:00
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2012-11-28 20:01:59 +01:00
|
|
|
static const AVFilterPad ffabuffersink_inputs[] = {
|
|
|
|
{
|
|
|
|
.name = "default",
|
|
|
|
.type = AVMEDIA_TYPE_AUDIO,
|
|
|
|
.filter_frame = filter_frame,
|
|
|
|
.min_perms = AV_PERM_READ | AV_PERM_PRESERVE,
|
|
|
|
},
|
|
|
|
{ NULL },
|
|
|
|
};
|
|
|
|
|
2012-08-31 22:02:08 +02:00
|
|
|
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,
|
2012-11-28 20:01:59 +01:00
|
|
|
.inputs = ffabuffersink_inputs,
|
|
|
|
.outputs = NULL,
|
|
|
|
};
|
2012-08-31 22:02:08 +02:00
|
|
|
|
2012-11-28 20:01:59 +01:00
|
|
|
static const AVFilterPad abuffersink_inputs[] = {
|
|
|
|
{
|
|
|
|
.name = "default",
|
|
|
|
.type = AVMEDIA_TYPE_AUDIO,
|
|
|
|
.filter_frame = filter_frame,
|
|
|
|
.min_perms = AV_PERM_READ | AV_PERM_PRESERVE,
|
|
|
|
},
|
|
|
|
{ NULL },
|
2012-08-31 22:02:08 +02:00
|
|
|
};
|
|
|
|
|
2011-08-18 16:21:47 +02:00
|
|
|
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."),
|
2012-07-06 01:06:20 +02:00
|
|
|
.init_opaque = asink_init,
|
2011-12-20 13:08:57 +01:00
|
|
|
.uninit = asink_uninit,
|
2011-08-18 16:21:47 +02:00
|
|
|
.priv_size = sizeof(BufferSinkContext),
|
|
|
|
.query_formats = asink_query_formats,
|
2012-11-28 20:01:59 +01:00
|
|
|
.inputs = abuffersink_inputs,
|
|
|
|
.outputs = NULL,
|
2011-08-18 16:21:47 +02:00
|
|
|
};
|
2012-09-23 20:45:49 +02:00
|
|
|
|
|
|
|
/* 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 ||
|
2012-11-28 13:53:48 +01:00
|
|
|
ctx->filter-> inputs[0].filter_frame ==
|
|
|
|
avfilter_asink_abuffer.inputs[0].filter_frame)
|
2012-09-23 20:45:49 +02:00
|
|
|
return ff_buffersink_read_compat(ctx, buf);
|
|
|
|
av_assert0(ctx->filter-> inputs[0].end_frame ==
|
|
|
|
avfilter_vsink_ffbuffersink. inputs[0].end_frame ||
|
2012-11-28 13:53:48 +01:00
|
|
|
ctx->filter-> inputs[0].filter_frame ==
|
|
|
|
avfilter_asink_ffabuffersink.inputs[0].filter_frame);
|
2012-09-23 20:45:49 +02:00
|
|
|
|
|
|
|
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);
|
|
|
|
|
2012-11-28 13:53:48 +01:00
|
|
|
if (ctx->filter-> inputs[0].filter_frame ==
|
|
|
|
avfilter_asink_abuffer.inputs[0].filter_frame)
|
2012-09-23 20:45:49 +02:00
|
|
|
return ff_buffersink_read_samples_compat(ctx, buf, nb_samples);
|
2012-11-28 13:53:48 +01:00
|
|
|
av_assert0(ctx->filter-> inputs[0].filter_frame ==
|
|
|
|
avfilter_asink_ffabuffersink.inputs[0].filter_frame);
|
2012-09-23 20:45:49 +02:00
|
|
|
|
|
|
|
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;
|
|
|
|
}
|