2010-08-24 08:38:00 +00:00
|
|
|
/*
|
2010-11-28 10:22:58 +00:00
|
|
|
* Copyright (c) 2007 Bobby Bingham
|
2010-08-24 08:38:00 +00:00
|
|
|
*
|
|
|
|
* 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-05-16 06:21:47 +00:00
|
|
|
* FIFO buffering filter
|
2010-08-24 08:38:00 +00:00
|
|
|
*/
|
|
|
|
|
2012-05-27 12:18:49 +00:00
|
|
|
#include "libavutil/avassert.h"
|
2012-11-10 15:00:00 +00:00
|
|
|
#include "libavutil/channel_layout.h"
|
2012-08-06 13:49:32 +00:00
|
|
|
#include "libavutil/common.h"
|
2012-05-27 12:18:49 +00:00
|
|
|
#include "libavutil/mathematics.h"
|
|
|
|
#include "libavutil/samplefmt.h"
|
|
|
|
|
2012-05-16 06:21:47 +00:00
|
|
|
#include "audio.h"
|
2010-08-24 08:38:00 +00:00
|
|
|
#include "avfilter.h"
|
2012-05-30 09:20:32 +00:00
|
|
|
#include "internal.h"
|
2012-05-19 08:37:56 +00:00
|
|
|
#include "video.h"
|
2010-08-24 08:38:00 +00:00
|
|
|
|
2012-05-16 06:21:47 +00:00
|
|
|
typedef struct Buf {
|
2012-11-28 07:41:07 +00:00
|
|
|
AVFrame *frame;
|
2012-05-16 06:21:47 +00:00
|
|
|
struct Buf *next;
|
|
|
|
} Buf;
|
2010-08-24 08:38:00 +00:00
|
|
|
|
2014-04-11 09:54:15 +00:00
|
|
|
typedef struct FifoContext {
|
2012-05-16 06:21:47 +00:00
|
|
|
Buf root;
|
|
|
|
Buf *last; ///< last buffered frame
|
2012-05-27 12:18:49 +00:00
|
|
|
|
|
|
|
/**
|
|
|
|
* When a specific number of output samples is requested, the partial
|
|
|
|
* buffer is stored here
|
|
|
|
*/
|
2012-11-28 07:41:07 +00:00
|
|
|
AVFrame *out;
|
|
|
|
int allocated_samples; ///< number of samples out was allocated for
|
2010-08-24 08:38:00 +00:00
|
|
|
} FifoContext;
|
|
|
|
|
2013-03-13 07:26:39 +00:00
|
|
|
static av_cold int init(AVFilterContext *ctx)
|
2010-08-24 08:38:00 +00:00
|
|
|
{
|
|
|
|
FifoContext *fifo = ctx->priv;
|
|
|
|
fifo->last = &fifo->root;
|
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
static av_cold void uninit(AVFilterContext *ctx)
|
|
|
|
{
|
|
|
|
FifoContext *fifo = ctx->priv;
|
2012-05-16 06:21:47 +00:00
|
|
|
Buf *buf, *tmp;
|
2010-08-24 08:38:00 +00:00
|
|
|
|
2012-05-16 06:21:47 +00:00
|
|
|
for (buf = fifo->root.next; buf; buf = tmp) {
|
|
|
|
tmp = buf->next;
|
2012-11-28 07:41:07 +00:00
|
|
|
av_frame_free(&buf->frame);
|
2012-05-16 06:21:47 +00:00
|
|
|
av_free(buf);
|
2010-08-24 08:38:00 +00:00
|
|
|
}
|
2012-05-27 12:18:49 +00:00
|
|
|
|
2012-11-28 07:41:07 +00:00
|
|
|
av_frame_free(&fifo->out);
|
2010-08-24 08:38:00 +00:00
|
|
|
}
|
|
|
|
|
2012-11-28 07:41:07 +00:00
|
|
|
static int add_to_queue(AVFilterLink *inlink, AVFrame *frame)
|
2010-08-24 08:38:00 +00:00
|
|
|
{
|
|
|
|
FifoContext *fifo = inlink->dst->priv;
|
|
|
|
|
2012-05-16 06:21:47 +00:00
|
|
|
fifo->last->next = av_mallocz(sizeof(Buf));
|
2012-07-02 18:13:40 +00:00
|
|
|
if (!fifo->last->next) {
|
2012-11-28 07:41:07 +00:00
|
|
|
av_frame_free(&frame);
|
2012-07-02 18:13:40 +00:00
|
|
|
return AVERROR(ENOMEM);
|
|
|
|
}
|
|
|
|
|
2010-08-24 08:38:00 +00:00
|
|
|
fifo->last = fifo->last->next;
|
2012-11-28 07:41:07 +00:00
|
|
|
fifo->last->frame = frame;
|
2012-07-02 18:13:40 +00:00
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2012-05-27 12:18:49 +00:00
|
|
|
static void queue_pop(FifoContext *s)
|
|
|
|
{
|
|
|
|
Buf *tmp = s->root.next->next;
|
|
|
|
if (s->last == s->root.next)
|
|
|
|
s->last = &s->root;
|
|
|
|
av_freep(&s->root.next);
|
|
|
|
s->root.next = tmp;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Move data pointers and pts offset samples forward.
|
|
|
|
*/
|
2012-11-28 07:41:07 +00:00
|
|
|
static void buffer_offset(AVFilterLink *link, AVFrame *frame,
|
2012-05-27 12:18:49 +00:00
|
|
|
int offset)
|
|
|
|
{
|
|
|
|
int nb_channels = av_get_channel_layout_nb_channels(link->channel_layout);
|
|
|
|
int planar = av_sample_fmt_is_planar(link->format);
|
|
|
|
int planes = planar ? nb_channels : 1;
|
|
|
|
int block_align = av_get_bytes_per_sample(link->format) * (planar ? 1 : nb_channels);
|
|
|
|
int i;
|
|
|
|
|
2012-11-28 07:41:07 +00:00
|
|
|
av_assert0(frame->nb_samples > offset);
|
2012-05-27 12:18:49 +00:00
|
|
|
|
|
|
|
for (i = 0; i < planes; i++)
|
2012-11-28 07:41:07 +00:00
|
|
|
frame->extended_data[i] += block_align * offset;
|
|
|
|
if (frame->data != frame->extended_data)
|
|
|
|
memcpy(frame->data, frame->extended_data,
|
|
|
|
FFMIN(planes, FF_ARRAY_ELEMS(frame->data)) * sizeof(*frame->data));
|
|
|
|
frame->linesize[0] -= block_align*offset;
|
|
|
|
frame->nb_samples -= offset;
|
|
|
|
|
|
|
|
if (frame->pts != AV_NOPTS_VALUE) {
|
|
|
|
frame->pts += av_rescale_q(offset, (AVRational){1, link->sample_rate},
|
|
|
|
link->time_base);
|
2012-05-27 12:18:49 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2012-11-28 07:41:07 +00:00
|
|
|
static int calc_ptr_alignment(AVFrame *frame)
|
2012-05-27 12:18:49 +00:00
|
|
|
{
|
2012-11-28 07:41:07 +00:00
|
|
|
int planes = av_sample_fmt_is_planar(frame->format) ?
|
|
|
|
av_get_channel_layout_nb_channels(frame->channel_layout) : 1;
|
2012-05-27 12:18:49 +00:00
|
|
|
int min_align = 128;
|
|
|
|
int p;
|
|
|
|
|
|
|
|
for (p = 0; p < planes; p++) {
|
|
|
|
int cur_align = 128;
|
2012-11-28 07:41:07 +00:00
|
|
|
while ((intptr_t)frame->extended_data[p] % cur_align)
|
2012-05-27 12:18:49 +00:00
|
|
|
cur_align >>= 1;
|
|
|
|
if (cur_align < min_align)
|
|
|
|
min_align = cur_align;
|
|
|
|
}
|
|
|
|
return min_align;
|
|
|
|
}
|
|
|
|
|
|
|
|
static int return_audio_frame(AVFilterContext *ctx)
|
|
|
|
{
|
|
|
|
AVFilterLink *link = ctx->outputs[0];
|
|
|
|
FifoContext *s = ctx->priv;
|
2013-08-04 10:10:23 +00:00
|
|
|
AVFrame *head = s->root.next ? s->root.next->frame : NULL;
|
2012-11-28 07:41:07 +00:00
|
|
|
AVFrame *out;
|
2012-05-27 12:18:49 +00:00
|
|
|
int ret;
|
|
|
|
|
2013-08-04 10:10:23 +00:00
|
|
|
/* if head is NULL then we're flushing the remaining samples in out */
|
|
|
|
if (!head && !s->out)
|
|
|
|
return AVERROR_EOF;
|
|
|
|
|
2012-11-28 07:41:07 +00:00
|
|
|
if (!s->out &&
|
|
|
|
head->nb_samples >= link->request_samples &&
|
2012-05-27 12:18:49 +00:00
|
|
|
calc_ptr_alignment(head) >= 32) {
|
2012-11-28 07:41:07 +00:00
|
|
|
if (head->nb_samples == link->request_samples) {
|
|
|
|
out = head;
|
2012-05-27 12:18:49 +00:00
|
|
|
queue_pop(s);
|
|
|
|
} else {
|
2012-11-28 07:41:07 +00:00
|
|
|
out = av_frame_clone(head);
|
|
|
|
if (!out)
|
2012-07-15 09:16:53 +00:00
|
|
|
return AVERROR(ENOMEM);
|
|
|
|
|
2012-11-28 07:41:07 +00:00
|
|
|
out->nb_samples = link->request_samples;
|
2012-05-27 12:18:49 +00:00
|
|
|
buffer_offset(link, head, link->request_samples);
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
int nb_channels = av_get_channel_layout_nb_channels(link->channel_layout);
|
|
|
|
|
2012-11-28 07:41:07 +00:00
|
|
|
if (!s->out) {
|
|
|
|
s->out = ff_get_audio_buffer(link, link->request_samples);
|
|
|
|
if (!s->out)
|
2012-05-27 12:18:49 +00:00
|
|
|
return AVERROR(ENOMEM);
|
|
|
|
|
2012-11-28 07:41:07 +00:00
|
|
|
s->out->nb_samples = 0;
|
|
|
|
s->out->pts = head->pts;
|
2012-05-27 12:18:49 +00:00
|
|
|
s->allocated_samples = link->request_samples;
|
|
|
|
} else if (link->request_samples != s->allocated_samples) {
|
|
|
|
av_log(ctx, AV_LOG_ERROR, "request_samples changed before the "
|
|
|
|
"buffer was returned.\n");
|
|
|
|
return AVERROR(EINVAL);
|
|
|
|
}
|
|
|
|
|
2012-11-28 07:41:07 +00:00
|
|
|
while (s->out->nb_samples < s->allocated_samples) {
|
2013-04-16 19:53:56 +00:00
|
|
|
int len;
|
|
|
|
|
|
|
|
if (!s->root.next) {
|
|
|
|
ret = ff_request_frame(ctx->inputs[0]);
|
|
|
|
if (ret == AVERROR_EOF) {
|
|
|
|
av_samples_set_silence(s->out->extended_data,
|
|
|
|
s->out->nb_samples,
|
|
|
|
s->allocated_samples -
|
|
|
|
s->out->nb_samples,
|
|
|
|
nb_channels, link->format);
|
|
|
|
s->out->nb_samples = s->allocated_samples;
|
|
|
|
break;
|
|
|
|
} else if (ret < 0)
|
|
|
|
return ret;
|
2013-08-09 07:28:11 +00:00
|
|
|
av_assert0(s->root.next); // If ff_request_frame() succeeded then we should have a frame
|
2013-04-16 19:53:56 +00:00
|
|
|
}
|
|
|
|
head = s->root.next->frame;
|
|
|
|
|
|
|
|
len = FFMIN(s->allocated_samples - s->out->nb_samples,
|
|
|
|
head->nb_samples);
|
2012-05-27 12:18:49 +00:00
|
|
|
|
2012-11-28 07:41:07 +00:00
|
|
|
av_samples_copy(s->out->extended_data, head->extended_data,
|
|
|
|
s->out->nb_samples, 0, len, nb_channels,
|
2012-05-27 12:18:49 +00:00
|
|
|
link->format);
|
2012-11-28 07:41:07 +00:00
|
|
|
s->out->nb_samples += len;
|
2012-05-27 12:18:49 +00:00
|
|
|
|
2012-11-28 07:41:07 +00:00
|
|
|
if (len == head->nb_samples) {
|
|
|
|
av_frame_free(&head);
|
2012-05-27 12:18:49 +00:00
|
|
|
queue_pop(s);
|
|
|
|
} else {
|
|
|
|
buffer_offset(link, head, len);
|
|
|
|
}
|
|
|
|
}
|
2012-11-28 07:41:07 +00:00
|
|
|
out = s->out;
|
|
|
|
s->out = NULL;
|
2012-05-27 12:18:49 +00:00
|
|
|
}
|
2012-11-28 07:41:07 +00:00
|
|
|
return ff_filter_frame(link, out);
|
2012-05-27 12:18:49 +00:00
|
|
|
}
|
|
|
|
|
2010-08-24 08:38:00 +00:00
|
|
|
static int request_frame(AVFilterLink *outlink)
|
|
|
|
{
|
|
|
|
FifoContext *fifo = outlink->src->priv;
|
2012-07-02 18:13:40 +00:00
|
|
|
int ret = 0;
|
2010-08-24 08:38:00 +00:00
|
|
|
|
|
|
|
if (!fifo->root.next) {
|
2013-08-04 10:10:23 +00:00
|
|
|
if ((ret = ff_request_frame(outlink->src->inputs[0])) < 0) {
|
|
|
|
if (ret == AVERROR_EOF && outlink->request_samples)
|
|
|
|
return return_audio_frame(outlink->src);
|
2010-08-24 08:38:00 +00:00
|
|
|
return ret;
|
2013-08-04 10:10:23 +00:00
|
|
|
}
|
2012-11-01 17:02:23 +00:00
|
|
|
av_assert0(fifo->root.next);
|
2010-08-24 08:38:00 +00:00
|
|
|
}
|
|
|
|
|
2012-11-28 18:13:51 +00:00
|
|
|
if (outlink->request_samples) {
|
|
|
|
return return_audio_frame(outlink->src);
|
|
|
|
} else {
|
2012-11-28 07:41:07 +00:00
|
|
|
ret = ff_filter_frame(outlink, fifo->root.next->frame);
|
2012-11-28 18:13:51 +00:00
|
|
|
queue_pop(fifo);
|
|
|
|
}
|
2010-08-24 08:38:00 +00:00
|
|
|
|
2012-07-02 18:13:40 +00:00
|
|
|
return ret;
|
2010-08-24 08:38:00 +00:00
|
|
|
}
|
|
|
|
|
2012-07-24 13:14:01 +00:00
|
|
|
static const AVFilterPad avfilter_vf_fifo_inputs[] = {
|
|
|
|
{
|
|
|
|
.name = "default",
|
|
|
|
.type = AVMEDIA_TYPE_VIDEO,
|
2012-11-28 16:19:06 +00:00
|
|
|
.filter_frame = add_to_queue,
|
2012-07-24 13:14:01 +00:00
|
|
|
},
|
|
|
|
{ NULL }
|
|
|
|
};
|
|
|
|
|
|
|
|
static const AVFilterPad avfilter_vf_fifo_outputs[] = {
|
|
|
|
{
|
|
|
|
.name = "default",
|
|
|
|
.type = AVMEDIA_TYPE_VIDEO,
|
|
|
|
.request_frame = request_frame,
|
|
|
|
},
|
|
|
|
{ NULL }
|
|
|
|
};
|
|
|
|
|
2013-10-28 06:44:24 +00:00
|
|
|
AVFilter ff_vf_fifo = {
|
2010-08-24 08:38:00 +00:00
|
|
|
.name = "fifo",
|
|
|
|
.description = NULL_IF_CONFIG_SMALL("Buffer input images and send them when they are requested."),
|
|
|
|
|
|
|
|
.init = init,
|
|
|
|
.uninit = uninit,
|
|
|
|
|
|
|
|
.priv_size = sizeof(FifoContext),
|
|
|
|
|
2012-07-24 13:14:01 +00:00
|
|
|
.inputs = avfilter_vf_fifo_inputs,
|
|
|
|
.outputs = avfilter_vf_fifo_outputs,
|
|
|
|
};
|
|
|
|
|
|
|
|
static const AVFilterPad avfilter_af_afifo_inputs[] = {
|
|
|
|
{
|
|
|
|
.name = "default",
|
|
|
|
.type = AVMEDIA_TYPE_AUDIO,
|
2012-11-28 12:53:48 +00:00
|
|
|
.filter_frame = add_to_queue,
|
2012-07-24 13:14:01 +00:00
|
|
|
},
|
|
|
|
{ NULL }
|
|
|
|
};
|
|
|
|
|
|
|
|
static const AVFilterPad avfilter_af_afifo_outputs[] = {
|
|
|
|
{
|
|
|
|
.name = "default",
|
|
|
|
.type = AVMEDIA_TYPE_AUDIO,
|
|
|
|
.request_frame = request_frame,
|
|
|
|
},
|
|
|
|
{ NULL }
|
2010-08-24 08:38:00 +00:00
|
|
|
};
|
2012-05-16 06:21:47 +00:00
|
|
|
|
2013-10-28 06:44:24 +00:00
|
|
|
AVFilter ff_af_afifo = {
|
2012-05-16 06:21:47 +00:00
|
|
|
.name = "afifo",
|
|
|
|
.description = NULL_IF_CONFIG_SMALL("Buffer input frames and send them when they are requested."),
|
|
|
|
|
|
|
|
.init = init,
|
|
|
|
.uninit = uninit,
|
|
|
|
|
|
|
|
.priv_size = sizeof(FifoContext),
|
|
|
|
|
2012-07-24 13:14:01 +00:00
|
|
|
.inputs = avfilter_af_afifo_inputs,
|
|
|
|
.outputs = avfilter_af_afifo_outputs,
|
2012-05-16 06:21:47 +00:00
|
|
|
};
|