2011-06-11 16:43:11 +00: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-05 23:10:28 +00:00
|
|
|
* buffer sink
|
2011-06-11 16:43:11 +00:00
|
|
|
*/
|
|
|
|
|
2013-03-10 15:44:46 +00:00
|
|
|
#include "libavutil/audio_fifo.h"
|
2012-04-24 10:51:37 +00:00
|
|
|
#include "libavutil/avassert.h"
|
2012-11-13 13:16:48 +00:00
|
|
|
#include "libavutil/channel_layout.h"
|
2013-03-10 14:55:33 +00:00
|
|
|
#include "libavutil/common.h"
|
2013-03-27 17:36:51 +00:00
|
|
|
#include "libavutil/internal.h"
|
2013-03-10 14:55:33 +00:00
|
|
|
#include "libavutil/mathematics.h"
|
2013-04-11 12:58:07 +00:00
|
|
|
#include "libavutil/opt.h"
|
2013-03-10 14:55:33 +00:00
|
|
|
|
|
|
|
#include "audio.h"
|
2011-06-11 16:43:11 +00:00
|
|
|
#include "avfilter.h"
|
2011-09-06 16:37:44 +00:00
|
|
|
#include "buffersink.h"
|
2011-12-20 12:08:57 +00:00
|
|
|
#include "internal.h"
|
2011-06-11 16:43:11 +00:00
|
|
|
|
|
|
|
typedef struct {
|
2013-04-11 12:58:07 +00:00
|
|
|
const AVClass *class;
|
2011-08-18 14:21:47 +00:00
|
|
|
AVFifoBuffer *fifo; ///< FIFO buffer of video frame references
|
2012-06-28 09:21:42 +00:00
|
|
|
unsigned warning_limit;
|
2011-08-18 14:21:47 +00:00
|
|
|
|
|
|
|
/* only used for video */
|
2012-10-08 18:54:00 +00:00
|
|
|
enum AVPixelFormat *pixel_fmts; ///< list of accepted pixel formats, must be terminated with -1
|
2013-04-11 12:58:07 +00:00
|
|
|
int pixel_fmts_size;
|
2011-08-18 14:21:47 +00:00
|
|
|
|
|
|
|
/* only used for audio */
|
2011-12-20 12:08:57 +00:00
|
|
|
enum AVSampleFormat *sample_fmts; ///< list of accepted sample formats, terminated by AV_SAMPLE_FMT_NONE
|
2013-04-11 12:58:07 +00:00
|
|
|
int sample_fmts_size;
|
2011-12-20 12:08:57 +00:00
|
|
|
int64_t *channel_layouts; ///< list of accepted channel layouts, terminated by -1
|
2013-04-11 12:58:07 +00:00
|
|
|
int channel_layouts_size;
|
|
|
|
int *channel_counts; ///< list of accepted channel counts, terminated by -1
|
|
|
|
int channel_counts_size;
|
2012-12-26 15:49:16 +00:00
|
|
|
int all_channel_counts;
|
2013-02-03 18:33:58 +00:00
|
|
|
int *sample_rates; ///< list of accepted sample rates, terminated by -1
|
2013-04-11 12:58:07 +00:00
|
|
|
int sample_rates_size;
|
2013-03-10 15:44:46 +00:00
|
|
|
|
|
|
|
/* only used for compat API */
|
2013-07-26 19:09:36 +00:00
|
|
|
AVAudioFifo *audio_fifo; ///< FIFO for audio samples
|
2013-03-10 15:44:46 +00:00
|
|
|
int64_t next_pts; ///< interpolating audio pts
|
2011-06-11 16:43:11 +00:00
|
|
|
} BufferSinkContext;
|
|
|
|
|
2013-04-11 12:58:07 +00:00
|
|
|
#define NB_ITEMS(list) (list ## _size / sizeof(*list))
|
|
|
|
|
2013-03-10 14:55:33 +00:00
|
|
|
static av_cold void uninit(AVFilterContext *ctx)
|
2011-06-11 16:43:11 +00:00
|
|
|
{
|
2013-03-10 14:55:33 +00:00
|
|
|
BufferSinkContext *sink = ctx->priv;
|
|
|
|
AVFrame *frame;
|
2011-06-29 15:31:16 +00:00
|
|
|
|
2013-03-10 15:44:46 +00:00
|
|
|
if (sink->audio_fifo)
|
|
|
|
av_audio_fifo_free(sink->audio_fifo);
|
|
|
|
|
2013-03-10 14:55:33 +00:00
|
|
|
if (sink->fifo) {
|
|
|
|
while (av_fifo_size(sink->fifo) >= sizeof(AVFilterBufferRef *)) {
|
|
|
|
av_fifo_generic_read(sink->fifo, &frame, sizeof(frame), NULL);
|
2013-03-23 22:49:33 +00:00
|
|
|
av_frame_free(&frame);
|
2011-06-29 15:31:16 +00:00
|
|
|
}
|
2013-03-10 14:55:33 +00:00
|
|
|
av_fifo_free(sink->fifo);
|
|
|
|
sink->fifo = NULL;
|
2011-06-29 15:31:16 +00:00
|
|
|
}
|
2011-06-11 16:43:11 +00:00
|
|
|
}
|
|
|
|
|
2013-03-10 00:30:30 +00:00
|
|
|
static int add_buffer_ref(AVFilterContext *ctx, AVFrame *ref)
|
2011-06-11 16:43:11 +00:00
|
|
|
{
|
2012-09-23 18:45:49 +00:00
|
|
|
BufferSinkContext *buf = ctx->priv;
|
2011-06-11 16:43:11 +00:00
|
|
|
|
2011-06-29 15:31:16 +00: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 20:57:02 +00:00
|
|
|
return AVERROR(ENOMEM);
|
2011-06-29 15:31:16 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/* cache frame */
|
2012-09-23 18:45:49 +00:00
|
|
|
av_fifo_generic_write(buf->fifo, &ref, sizeof(AVFilterBufferRef *), NULL);
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2013-03-10 14:55:33 +00:00
|
|
|
static int filter_frame(AVFilterLink *link, AVFrame *frame)
|
2012-09-23 18:45:49 +00:00
|
|
|
{
|
2013-03-10 14:55:33 +00:00
|
|
|
AVFilterContext *ctx = link->dst;
|
|
|
|
BufferSinkContext *buf = link->dst->priv;
|
2012-09-23 18:45:49 +00:00
|
|
|
int ret;
|
|
|
|
|
2013-03-10 14:55:33 +00:00
|
|
|
if ((ret = add_buffer_ref(ctx, frame)) < 0)
|
2012-09-23 18:45:49 +00:00
|
|
|
return ret;
|
2012-06-28 09:21:42 +00: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 20:57:02 +00:00
|
|
|
return 0;
|
2011-06-11 16:43:11 +00:00
|
|
|
}
|
|
|
|
|
2013-07-26 08:31:05 +00:00
|
|
|
int attribute_align_arg av_buffersink_get_frame(AVFilterContext *ctx, AVFrame *frame)
|
2012-06-24 22:30:15 +00:00
|
|
|
{
|
2013-03-10 14:55:33 +00:00
|
|
|
return av_buffersink_get_frame_flags(ctx, frame, 0);
|
2012-06-24 22:30:15 +00:00
|
|
|
}
|
|
|
|
|
2013-03-15 19:44:57 +00:00
|
|
|
int attribute_align_arg av_buffersink_get_frame_flags(AVFilterContext *ctx, AVFrame *frame, int flags)
|
2011-06-11 16:43:11 +00:00
|
|
|
{
|
|
|
|
BufferSinkContext *buf = ctx->priv;
|
|
|
|
AVFilterLink *inlink = ctx->inputs[0];
|
|
|
|
int ret;
|
2013-03-10 00:30:30 +00:00
|
|
|
AVFrame *cur_frame;
|
2012-06-24 12:17:14 +00:00
|
|
|
|
2011-06-11 16:43:11 +00:00
|
|
|
/* no picref available, fetch it from the filterchain */
|
2011-06-29 15:31:16 +00:00
|
|
|
if (!av_fifo_size(buf->fifo)) {
|
2012-04-19 15:08:27 +00:00
|
|
|
if (flags & AV_BUFFERSINK_FLAG_NO_REQUEST)
|
|
|
|
return AVERROR(EAGAIN);
|
2012-06-24 00:09:53 +00:00
|
|
|
if ((ret = ff_request_frame(inlink)) < 0)
|
2011-06-11 16:43:11 +00:00
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
2011-06-29 15:31:16 +00:00
|
|
|
if (!av_fifo_size(buf->fifo))
|
2011-06-11 16:43:11 +00:00
|
|
|
return AVERROR(EINVAL);
|
|
|
|
|
2013-03-10 00:30:30 +00:00
|
|
|
if (flags & AV_BUFFERSINK_FLAG_PEEK) {
|
|
|
|
cur_frame = *((AVFrame **)av_fifo_peek2(buf->fifo, 0));
|
2013-03-20 10:40:28 +00:00
|
|
|
if ((ret = av_frame_ref(frame, cur_frame)) < 0)
|
|
|
|
return ret;
|
2013-03-10 00:30:30 +00:00
|
|
|
} else {
|
|
|
|
av_fifo_generic_read(buf->fifo, &cur_frame, sizeof(cur_frame), NULL);
|
|
|
|
av_frame_move_ref(frame, cur_frame);
|
|
|
|
av_frame_free(&cur_frame);
|
|
|
|
}
|
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2013-03-10 15:44:46 +00:00
|
|
|
static int read_from_fifo(AVFilterContext *ctx, AVFrame *frame,
|
|
|
|
int nb_samples)
|
|
|
|
{
|
|
|
|
BufferSinkContext *s = ctx->priv;
|
|
|
|
AVFilterLink *link = ctx->inputs[0];
|
|
|
|
AVFrame *tmp;
|
|
|
|
|
|
|
|
if (!(tmp = ff_get_audio_buffer(link, nb_samples)))
|
|
|
|
return AVERROR(ENOMEM);
|
|
|
|
av_audio_fifo_read(s->audio_fifo, (void**)tmp->extended_data, nb_samples);
|
|
|
|
|
|
|
|
tmp->pts = s->next_pts;
|
2013-10-09 14:28:51 +00:00
|
|
|
if (s->next_pts != AV_NOPTS_VALUE)
|
|
|
|
s->next_pts += av_rescale_q(nb_samples, (AVRational){1, link->sample_rate},
|
|
|
|
link->time_base);
|
2013-03-10 15:44:46 +00:00
|
|
|
|
|
|
|
av_frame_move_ref(frame, tmp);
|
|
|
|
av_frame_free(&tmp);
|
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2013-03-15 19:44:57 +00:00
|
|
|
int attribute_align_arg av_buffersink_get_samples(AVFilterContext *ctx,
|
|
|
|
AVFrame *frame, int nb_samples)
|
2013-03-10 00:30:30 +00:00
|
|
|
{
|
2013-03-10 15:44:46 +00:00
|
|
|
BufferSinkContext *s = ctx->priv;
|
|
|
|
AVFilterLink *link = ctx->inputs[0];
|
|
|
|
AVFrame *cur_frame;
|
|
|
|
int ret = 0;
|
|
|
|
|
|
|
|
if (!s->audio_fifo) {
|
|
|
|
int nb_channels = link->channels;
|
|
|
|
if (!(s->audio_fifo = av_audio_fifo_alloc(link->format, nb_channels, nb_samples)))
|
|
|
|
return AVERROR(ENOMEM);
|
|
|
|
}
|
|
|
|
|
|
|
|
while (ret >= 0) {
|
|
|
|
if (av_audio_fifo_size(s->audio_fifo) >= nb_samples)
|
|
|
|
return read_from_fifo(ctx, frame, nb_samples);
|
|
|
|
|
|
|
|
if (!(cur_frame = av_frame_alloc()))
|
|
|
|
return AVERROR(ENOMEM);
|
|
|
|
ret = av_buffersink_get_frame_flags(ctx, cur_frame, 0);
|
|
|
|
if (ret == AVERROR_EOF && av_audio_fifo_size(s->audio_fifo)) {
|
|
|
|
av_frame_free(&cur_frame);
|
|
|
|
return read_from_fifo(ctx, frame, av_audio_fifo_size(s->audio_fifo));
|
|
|
|
} else if (ret < 0) {
|
|
|
|
av_frame_free(&cur_frame);
|
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (cur_frame->pts != AV_NOPTS_VALUE) {
|
|
|
|
s->next_pts = cur_frame->pts -
|
|
|
|
av_rescale_q(av_audio_fifo_size(s->audio_fifo),
|
|
|
|
(AVRational){ 1, link->sample_rate },
|
|
|
|
link->time_base);
|
|
|
|
}
|
|
|
|
|
|
|
|
ret = av_audio_fifo_write(s->audio_fifo, (void**)cur_frame->extended_data,
|
|
|
|
cur_frame->nb_samples);
|
|
|
|
av_frame_free(&cur_frame);
|
|
|
|
}
|
|
|
|
|
|
|
|
return ret;
|
2013-03-10 00:30:30 +00:00
|
|
|
}
|
|
|
|
|
2013-03-10 14:55:33 +00:00
|
|
|
AVBufferSinkParams *av_buffersink_params_alloc(void)
|
2013-03-10 00:30:30 +00:00
|
|
|
{
|
2013-03-10 14:55:33 +00:00
|
|
|
static const int pixel_fmts[] = { AV_PIX_FMT_NONE };
|
|
|
|
AVBufferSinkParams *params = av_malloc(sizeof(AVBufferSinkParams));
|
|
|
|
if (!params)
|
|
|
|
return NULL;
|
|
|
|
|
|
|
|
params->pixel_fmts = pixel_fmts;
|
|
|
|
return params;
|
|
|
|
}
|
|
|
|
|
|
|
|
AVABufferSinkParams *av_abuffersink_params_alloc(void)
|
|
|
|
{
|
|
|
|
AVABufferSinkParams *params = av_mallocz(sizeof(AVABufferSinkParams));
|
|
|
|
|
|
|
|
if (!params)
|
|
|
|
return NULL;
|
|
|
|
return params;
|
|
|
|
}
|
|
|
|
|
|
|
|
#define FIFO_INIT_SIZE 8
|
|
|
|
|
|
|
|
static av_cold int common_init(AVFilterContext *ctx)
|
|
|
|
{
|
|
|
|
BufferSinkContext *buf = ctx->priv;
|
|
|
|
|
|
|
|
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);
|
|
|
|
}
|
|
|
|
buf->warning_limit = 100;
|
2013-10-09 14:28:51 +00:00
|
|
|
buf->next_pts = AV_NOPTS_VALUE;
|
2013-03-10 14:55:33 +00:00
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
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;
|
2013-03-10 00:30:30 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
#if FF_API_AVFILTERBUFFER
|
2013-03-27 17:36:51 +00:00
|
|
|
FF_DISABLE_DEPRECATION_WARNINGS
|
2013-03-10 00:30:30 +00:00
|
|
|
static void compat_free_buffer(AVFilterBuffer *buf)
|
|
|
|
{
|
|
|
|
AVFrame *frame = buf->priv;
|
|
|
|
av_frame_free(&frame);
|
|
|
|
av_free(buf);
|
|
|
|
}
|
|
|
|
|
2013-07-26 08:31:05 +00:00
|
|
|
static int compat_read(AVFilterContext *ctx,
|
|
|
|
AVFilterBufferRef **pbuf, int nb_samples, int flags)
|
2013-03-10 00:30:30 +00:00
|
|
|
{
|
|
|
|
AVFilterBufferRef *buf;
|
|
|
|
AVFrame *frame;
|
|
|
|
int ret;
|
|
|
|
|
|
|
|
if (!pbuf)
|
|
|
|
return ff_poll_frame(ctx->inputs[0]);
|
|
|
|
|
|
|
|
frame = av_frame_alloc();
|
|
|
|
if (!frame)
|
|
|
|
return AVERROR(ENOMEM);
|
|
|
|
|
|
|
|
if (!nb_samples)
|
|
|
|
ret = av_buffersink_get_frame_flags(ctx, frame, flags);
|
2011-06-29 15:31:16 +00:00
|
|
|
else
|
2013-03-10 00:30:30 +00:00
|
|
|
ret = av_buffersink_get_samples(ctx, frame, nb_samples);
|
|
|
|
|
|
|
|
if (ret < 0)
|
|
|
|
goto fail;
|
|
|
|
|
2013-04-11 12:58:07 +00:00
|
|
|
AV_NOWARN_DEPRECATED(
|
2013-03-10 00:30:30 +00:00
|
|
|
if (ctx->inputs[0]->type == AVMEDIA_TYPE_VIDEO) {
|
|
|
|
buf = avfilter_get_video_buffer_ref_from_arrays(frame->data, frame->linesize,
|
|
|
|
AV_PERM_READ,
|
|
|
|
frame->width, frame->height,
|
|
|
|
frame->format);
|
|
|
|
} else {
|
|
|
|
buf = avfilter_get_audio_buffer_ref_from_arrays(frame->extended_data,
|
|
|
|
frame->linesize[0], AV_PERM_READ,
|
|
|
|
frame->nb_samples,
|
|
|
|
frame->format,
|
|
|
|
frame->channel_layout);
|
|
|
|
}
|
|
|
|
if (!buf) {
|
|
|
|
ret = AVERROR(ENOMEM);
|
|
|
|
goto fail;
|
|
|
|
}
|
|
|
|
|
|
|
|
avfilter_copy_frame_props(buf, frame);
|
2013-04-11 12:58:07 +00:00
|
|
|
)
|
2013-03-10 00:30:30 +00:00
|
|
|
|
|
|
|
buf->buf->priv = frame;
|
|
|
|
buf->buf->free = compat_free_buffer;
|
|
|
|
|
|
|
|
*pbuf = buf;
|
2011-08-18 14:21:47 +00:00
|
|
|
|
|
|
|
return 0;
|
2013-03-10 00:30:30 +00:00
|
|
|
fail:
|
|
|
|
av_frame_free(&frame);
|
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
2013-07-26 08:31:05 +00:00
|
|
|
int attribute_align_arg av_buffersink_read(AVFilterContext *ctx, AVFilterBufferRef **buf)
|
2013-03-10 00:30:30 +00:00
|
|
|
{
|
|
|
|
return compat_read(ctx, buf, 0, 0);
|
|
|
|
}
|
|
|
|
|
2013-07-26 08:31:05 +00:00
|
|
|
int attribute_align_arg av_buffersink_read_samples(AVFilterContext *ctx, AVFilterBufferRef **buf,
|
|
|
|
int nb_samples)
|
2013-03-10 00:30:30 +00:00
|
|
|
{
|
|
|
|
return compat_read(ctx, buf, nb_samples, 0);
|
|
|
|
}
|
|
|
|
|
2013-07-26 08:31:05 +00:00
|
|
|
int attribute_align_arg av_buffersink_get_buffer_ref(AVFilterContext *ctx,
|
|
|
|
AVFilterBufferRef **bufref, int flags)
|
2013-03-10 00:30:30 +00:00
|
|
|
{
|
|
|
|
*bufref = NULL;
|
|
|
|
|
|
|
|
av_assert0( !strcmp(ctx->filter->name, "buffersink")
|
|
|
|
|| !strcmp(ctx->filter->name, "abuffersink")
|
|
|
|
|| !strcmp(ctx->filter->name, "ffbuffersink")
|
|
|
|
|| !strcmp(ctx->filter->name, "ffabuffersink"));
|
|
|
|
|
|
|
|
return compat_read(ctx, bufref, 0, flags);
|
2011-08-18 14:21:47 +00:00
|
|
|
}
|
2013-03-27 17:36:51 +00:00
|
|
|
FF_ENABLE_DEPRECATION_WARNINGS
|
2013-03-10 00:30:30 +00:00
|
|
|
#endif
|
2011-08-18 14:21:47 +00:00
|
|
|
|
2012-06-05 10:40:37 +00:00
|
|
|
AVRational av_buffersink_get_frame_rate(AVFilterContext *ctx)
|
|
|
|
{
|
2012-08-31 20:02:08 +00:00
|
|
|
av_assert0( !strcmp(ctx->filter->name, "buffersink")
|
|
|
|
|| !strcmp(ctx->filter->name, "ffbuffersink"));
|
2012-06-24 12:17:14 +00:00
|
|
|
|
2012-06-05 10:40:37 +00:00
|
|
|
return ctx->inputs[0]->frame_rate;
|
|
|
|
}
|
|
|
|
|
2013-03-15 19:44:57 +00:00
|
|
|
int attribute_align_arg av_buffersink_poll_frame(AVFilterContext *ctx)
|
2011-12-06 16:02:15 +00:00
|
|
|
{
|
|
|
|
BufferSinkContext *buf = ctx->priv;
|
|
|
|
AVFilterLink *inlink = ctx->inputs[0];
|
|
|
|
|
2012-08-31 20:02:08 +00: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 12:17:14 +00:00
|
|
|
|
2012-06-16 09:47:46 +00:00
|
|
|
return av_fifo_size(buf->fifo)/sizeof(AVFilterBufferRef *) + ff_poll_frame(inlink);
|
2011-12-06 16:02:15 +00:00
|
|
|
}
|
|
|
|
|
2013-04-12 09:13:33 +00:00
|
|
|
static av_cold int vsink_init(AVFilterContext *ctx, void *opaque)
|
2011-08-18 14:21:47 +00:00
|
|
|
{
|
|
|
|
BufferSinkContext *buf = ctx->priv;
|
2012-07-05 23:06:20 +00:00
|
|
|
AVBufferSinkParams *params = opaque;
|
2013-04-11 12:58:07 +00:00
|
|
|
int ret;
|
2011-08-18 14:21:47 +00:00
|
|
|
|
2013-04-11 12:58:07 +00:00
|
|
|
if (params) {
|
|
|
|
if ((ret = av_opt_set_int_list(buf, "pix_fmts", params->pixel_fmts, AV_PIX_FMT_NONE, 0)) < 0)
|
|
|
|
return ret;
|
2011-08-18 14:21:47 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
return common_init(ctx);
|
|
|
|
}
|
|
|
|
|
2013-04-13 09:27:52 +00:00
|
|
|
#define CHECK_LIST_SIZE(field) \
|
|
|
|
if (buf->field ## _size % sizeof(*buf->field)) { \
|
|
|
|
av_log(ctx, AV_LOG_ERROR, "Invalid size for " #field ": %d, " \
|
|
|
|
"should be multiple of %d\n", \
|
|
|
|
buf->field ## _size, (int)sizeof(*buf->field)); \
|
|
|
|
return AVERROR(EINVAL); \
|
|
|
|
}
|
2011-08-18 14:21:47 +00:00
|
|
|
static int vsink_query_formats(AVFilterContext *ctx)
|
|
|
|
{
|
|
|
|
BufferSinkContext *buf = ctx->priv;
|
2013-04-11 12:58:07 +00:00
|
|
|
AVFilterFormats *formats = NULL;
|
|
|
|
unsigned i;
|
|
|
|
int ret;
|
2011-06-11 16:43:11 +00:00
|
|
|
|
2013-04-13 09:27:52 +00:00
|
|
|
CHECK_LIST_SIZE(pixel_fmts)
|
2013-04-11 12:58:07 +00:00
|
|
|
if (buf->pixel_fmts_size) {
|
|
|
|
for (i = 0; i < NB_ITEMS(buf->pixel_fmts); i++)
|
2013-04-21 13:33:18 +00:00
|
|
|
if ((ret = ff_add_format(&formats, buf->pixel_fmts[i])) < 0) {
|
|
|
|
ff_formats_unref(&formats);
|
2013-04-11 12:58:07 +00:00
|
|
|
return ret;
|
2013-04-21 13:33:18 +00:00
|
|
|
}
|
2013-04-11 12:58:07 +00:00
|
|
|
ff_set_common_formats(ctx, formats);
|
|
|
|
} else {
|
2012-06-16 09:47:46 +00:00
|
|
|
ff_default_query_formats(ctx);
|
2013-04-11 12:58:07 +00:00
|
|
|
}
|
2012-04-17 02:01:17 +00:00
|
|
|
|
2011-06-11 16:43:11 +00:00
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2013-04-12 09:13:33 +00:00
|
|
|
static av_cold int asink_init(AVFilterContext *ctx, void *opaque)
|
2011-08-18 14:21:47 +00:00
|
|
|
{
|
|
|
|
BufferSinkContext *buf = ctx->priv;
|
2012-07-05 23:06:20 +00:00
|
|
|
AVABufferSinkParams *params = opaque;
|
2013-04-11 12:58:07 +00:00
|
|
|
int ret;
|
2011-08-18 14:21:47 +00:00
|
|
|
|
2013-04-11 12:58:07 +00:00
|
|
|
if (params) {
|
|
|
|
if ((ret = av_opt_set_int_list(buf, "sample_fmts", params->sample_fmts, AV_SAMPLE_FMT_NONE, 0)) < 0 ||
|
|
|
|
(ret = av_opt_set_int_list(buf, "sample_rates", params->sample_rates, -1, 0)) < 0 ||
|
|
|
|
(ret = av_opt_set_int_list(buf, "channel_layouts", params->channel_layouts, -1, 0)) < 0 ||
|
|
|
|
(ret = av_opt_set_int_list(buf, "channel_counts", params->channel_counts, -1, 0)) < 0 ||
|
|
|
|
(ret = av_opt_set_int(buf, "all_channel_counts", params->all_channel_counts, 0)) < 0)
|
|
|
|
return ret;
|
2012-06-24 21:28:29 +00:00
|
|
|
}
|
2012-12-26 15:49:16 +00:00
|
|
|
return common_init(ctx);
|
2011-08-18 14:21:47 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
static int asink_query_formats(AVFilterContext *ctx)
|
|
|
|
{
|
|
|
|
BufferSinkContext *buf = ctx->priv;
|
|
|
|
AVFilterFormats *formats = NULL;
|
2012-05-16 00:27:31 +00:00
|
|
|
AVFilterChannelLayouts *layouts = NULL;
|
2013-04-11 12:58:07 +00:00
|
|
|
unsigned i;
|
|
|
|
int ret;
|
2011-08-18 14:21:47 +00:00
|
|
|
|
2013-04-13 09:27:52 +00:00
|
|
|
CHECK_LIST_SIZE(sample_fmts)
|
|
|
|
CHECK_LIST_SIZE(sample_rates)
|
|
|
|
CHECK_LIST_SIZE(channel_layouts)
|
|
|
|
CHECK_LIST_SIZE(channel_counts)
|
2013-04-11 12:58:07 +00:00
|
|
|
|
|
|
|
if (buf->sample_fmts_size) {
|
|
|
|
for (i = 0; i < NB_ITEMS(buf->sample_fmts); i++)
|
2013-04-21 13:33:18 +00:00
|
|
|
if ((ret = ff_add_format(&formats, buf->sample_fmts[i])) < 0) {
|
|
|
|
ff_formats_unref(&formats);
|
2013-04-11 12:58:07 +00:00
|
|
|
return ret;
|
2013-04-21 13:33:18 +00:00
|
|
|
}
|
2013-02-03 20:12:37 +00:00
|
|
|
ff_set_common_formats(ctx, formats);
|
2012-06-24 21:28:29 +00:00
|
|
|
}
|
2011-08-18 14:21:47 +00:00
|
|
|
|
2013-04-11 12:58:07 +00:00
|
|
|
if (buf->channel_layouts_size || buf->channel_counts_size ||
|
|
|
|
buf->all_channel_counts) {
|
|
|
|
for (i = 0; i < NB_ITEMS(buf->channel_layouts); i++)
|
2013-04-21 13:33:18 +00:00
|
|
|
if ((ret = ff_add_channel_layout(&layouts, buf->channel_layouts[i])) < 0) {
|
|
|
|
ff_channel_layouts_unref(&layouts);
|
2013-04-11 12:58:07 +00:00
|
|
|
return ret;
|
2013-04-21 13:33:18 +00:00
|
|
|
}
|
2013-04-11 12:58:07 +00:00
|
|
|
for (i = 0; i < NB_ITEMS(buf->channel_counts); i++)
|
2013-04-21 13:33:18 +00:00
|
|
|
if ((ret = ff_add_channel_layout(&layouts, FF_COUNT2LAYOUT(buf->channel_counts[i]))) < 0) {
|
|
|
|
ff_channel_layouts_unref(&layouts);
|
2013-04-11 12:58:07 +00:00
|
|
|
return ret;
|
2013-04-21 13:33:18 +00:00
|
|
|
}
|
2013-04-11 12:58:07 +00:00
|
|
|
if (buf->all_channel_counts) {
|
|
|
|
if (layouts)
|
|
|
|
av_log(ctx, AV_LOG_WARNING,
|
|
|
|
"Conflicting all_channel_counts and list in options\n");
|
|
|
|
else if (!(layouts = ff_all_channel_counts()))
|
|
|
|
return AVERROR(ENOMEM);
|
|
|
|
}
|
2012-12-26 15:49:16 +00:00
|
|
|
ff_set_common_channel_layouts(ctx, layouts);
|
2012-06-24 21:28:29 +00:00
|
|
|
}
|
2012-05-16 00:27:31 +00:00
|
|
|
|
2013-04-11 12:58:07 +00:00
|
|
|
if (buf->sample_rates_size) {
|
|
|
|
formats = NULL;
|
|
|
|
for (i = 0; i < NB_ITEMS(buf->sample_rates); i++)
|
2013-04-21 13:33:18 +00:00
|
|
|
if ((ret = ff_add_format(&formats, buf->sample_rates[i])) < 0) {
|
|
|
|
ff_formats_unref(&formats);
|
2013-04-11 12:58:07 +00:00
|
|
|
return ret;
|
2013-04-21 13:33:18 +00:00
|
|
|
}
|
2013-02-03 18:33:58 +00:00
|
|
|
ff_set_common_samplerates(ctx, formats);
|
|
|
|
}
|
|
|
|
|
2011-08-18 14:21:47 +00:00
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2013-04-11 12:58:07 +00:00
|
|
|
#define OFFSET(x) offsetof(BufferSinkContext, x)
|
|
|
|
#define FLAGS AV_OPT_FLAG_FILTERING_PARAM|AV_OPT_FLAG_VIDEO_PARAM
|
|
|
|
static const AVOption buffersink_options[] = {
|
|
|
|
{ "pix_fmts", "set the supported pixel formats", OFFSET(pixel_fmts), AV_OPT_TYPE_BINARY, .flags = FLAGS },
|
|
|
|
{ NULL },
|
|
|
|
};
|
|
|
|
#undef FLAGS
|
|
|
|
#define FLAGS AV_OPT_FLAG_FILTERING_PARAM|AV_OPT_FLAG_VIDEO_PARAM
|
|
|
|
static const AVOption abuffersink_options[] = {
|
|
|
|
{ "sample_fmts", "set the supported sample formats", OFFSET(sample_fmts), AV_OPT_TYPE_BINARY, .flags = FLAGS },
|
|
|
|
{ "sample_rates", "set the supported sample rates", OFFSET(sample_rates), AV_OPT_TYPE_BINARY, .flags = FLAGS },
|
|
|
|
{ "channel_layouts", "set the supported channel layouts", OFFSET(channel_layouts), AV_OPT_TYPE_BINARY, .flags = FLAGS },
|
|
|
|
{ "channel_counts", "set the supported channel counts", OFFSET(channel_counts), AV_OPT_TYPE_BINARY, .flags = FLAGS },
|
|
|
|
{ "all_channel_counts", "accept all channel counts", OFFSET(all_channel_counts), AV_OPT_TYPE_INT, {.i64 = 0}, 0, 1, FLAGS },
|
|
|
|
{ NULL },
|
|
|
|
};
|
|
|
|
#undef FLAGS
|
|
|
|
|
|
|
|
AVFILTER_DEFINE_CLASS(buffersink);
|
|
|
|
AVFILTER_DEFINE_CLASS(abuffersink);
|
|
|
|
|
2013-03-11 20:49:28 +00:00
|
|
|
#if FF_API_AVFILTERBUFFER
|
2013-04-11 12:58:07 +00:00
|
|
|
|
|
|
|
#define ffbuffersink_options buffersink_options
|
|
|
|
#define ffabuffersink_options abuffersink_options
|
|
|
|
AVFILTER_DEFINE_CLASS(ffbuffersink);
|
|
|
|
AVFILTER_DEFINE_CLASS(ffabuffersink);
|
|
|
|
|
2013-03-10 18:59:48 +00:00
|
|
|
static const AVFilterPad ffbuffersink_inputs[] = {
|
|
|
|
{
|
|
|
|
.name = "default",
|
|
|
|
.type = AVMEDIA_TYPE_VIDEO,
|
|
|
|
.filter_frame = filter_frame,
|
|
|
|
},
|
|
|
|
{ NULL },
|
|
|
|
};
|
|
|
|
|
|
|
|
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),
|
2013-04-11 12:58:07 +00:00
|
|
|
.priv_class = &ffbuffersink_class,
|
2013-03-10 18:59:48 +00:00
|
|
|
.init_opaque = vsink_init,
|
|
|
|
.uninit = uninit,
|
|
|
|
|
|
|
|
.query_formats = vsink_query_formats,
|
|
|
|
.inputs = ffbuffersink_inputs,
|
|
|
|
.outputs = NULL,
|
|
|
|
};
|
|
|
|
|
2012-11-28 19:01:59 +00:00
|
|
|
static const AVFilterPad ffabuffersink_inputs[] = {
|
|
|
|
{
|
|
|
|
.name = "default",
|
|
|
|
.type = AVMEDIA_TYPE_AUDIO,
|
|
|
|
.filter_frame = filter_frame,
|
|
|
|
},
|
|
|
|
{ NULL },
|
|
|
|
};
|
|
|
|
|
2012-08-31 20:02:08 +00: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,
|
2013-03-10 14:55:33 +00:00
|
|
|
.uninit = uninit,
|
2012-08-31 20:02:08 +00:00
|
|
|
.priv_size = sizeof(BufferSinkContext),
|
2013-04-11 12:58:07 +00:00
|
|
|
.priv_class = &ffabuffersink_class,
|
2012-08-31 20:02:08 +00:00
|
|
|
.query_formats = asink_query_formats,
|
2012-11-28 19:01:59 +00:00
|
|
|
.inputs = ffabuffersink_inputs,
|
|
|
|
.outputs = NULL,
|
|
|
|
};
|
2013-03-11 20:49:28 +00:00
|
|
|
#endif /* FF_API_AVFILTERBUFFER */
|
2012-08-31 20:02:08 +00:00
|
|
|
|
2013-03-11 20:26:03 +00:00
|
|
|
static const AVFilterPad avfilter_vsink_buffer_inputs[] = {
|
2013-03-10 18:59:48 +00:00
|
|
|
{
|
2013-07-26 19:09:36 +00:00
|
|
|
.name = "default",
|
|
|
|
.type = AVMEDIA_TYPE_VIDEO,
|
2013-03-10 18:59:48 +00:00
|
|
|
.filter_frame = filter_frame,
|
|
|
|
},
|
|
|
|
{ NULL }
|
|
|
|
};
|
|
|
|
|
2013-03-11 20:26:03 +00:00
|
|
|
AVFilter avfilter_vsink_buffer = {
|
2013-07-26 19:09:36 +00:00
|
|
|
.name = "buffersink",
|
2013-03-10 18:59:48 +00:00
|
|
|
.description = NULL_IF_CONFIG_SMALL("Buffer video frames, and make them available to the end of the filter graph."),
|
2013-07-26 19:09:36 +00:00
|
|
|
.priv_size = sizeof(BufferSinkContext),
|
2013-07-28 09:26:57 +00:00
|
|
|
.priv_class = &buffersink_class,
|
2013-03-10 18:59:48 +00:00
|
|
|
.init_opaque = vsink_init,
|
2013-07-26 19:09:36 +00:00
|
|
|
.uninit = uninit,
|
2013-03-10 18:59:48 +00:00
|
|
|
|
|
|
|
.query_formats = vsink_query_formats,
|
2013-07-26 19:09:36 +00:00
|
|
|
.inputs = avfilter_vsink_buffer_inputs,
|
|
|
|
.outputs = NULL,
|
2013-03-10 18:59:48 +00:00
|
|
|
};
|
|
|
|
|
2013-03-11 20:26:03 +00:00
|
|
|
static const AVFilterPad avfilter_asink_abuffer_inputs[] = {
|
2012-11-28 19:01:59 +00:00
|
|
|
{
|
2013-07-26 19:09:36 +00:00
|
|
|
.name = "default",
|
|
|
|
.type = AVMEDIA_TYPE_AUDIO,
|
|
|
|
.filter_frame = filter_frame,
|
2012-11-28 19:01:59 +00:00
|
|
|
},
|
2013-03-10 18:59:48 +00:00
|
|
|
{ NULL }
|
2012-08-31 20:02:08 +00:00
|
|
|
};
|
|
|
|
|
2013-03-11 20:26:03 +00:00
|
|
|
AVFilter avfilter_asink_abuffer = {
|
2013-07-26 19:09:36 +00:00
|
|
|
.name = "abuffersink",
|
2011-08-18 14:21:47 +00:00
|
|
|
.description = NULL_IF_CONFIG_SMALL("Buffer audio frames, and make them available to the end of the filter graph."),
|
2013-07-28 09:26:57 +00:00
|
|
|
.priv_class = &abuffersink_class,
|
2013-07-26 19:09:36 +00:00
|
|
|
.priv_size = sizeof(BufferSinkContext),
|
2012-07-05 23:06:20 +00:00
|
|
|
.init_opaque = asink_init,
|
2013-07-26 19:09:36 +00:00
|
|
|
.uninit = uninit,
|
2013-03-10 18:59:48 +00:00
|
|
|
|
2011-08-18 14:21:47 +00:00
|
|
|
.query_formats = asink_query_formats,
|
2013-07-26 19:09:36 +00:00
|
|
|
.inputs = avfilter_asink_abuffer_inputs,
|
|
|
|
.outputs = NULL,
|
2011-08-18 14:21:47 +00:00
|
|
|
};
|