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
|
|
|
*
|
2011-03-18 17:35:10 +00:00
|
|
|
* This file is part of Libav.
|
2010-08-24 08:38:00 +00:00
|
|
|
*
|
2011-03-18 17:35:10 +00:00
|
|
|
* Libav is free software; you can redistribute it and/or
|
2010-08-24 08:38:00 +00:00
|
|
|
* 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.
|
|
|
|
*
|
2011-03-18 17:35:10 +00:00
|
|
|
* Libav is distributed in the hope that it will be useful,
|
2010-08-24 08:38:00 +00:00
|
|
|
* 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
|
2011-03-18 17:35:10 +00:00
|
|
|
* License along with Libav; if not, write to the Free Software
|
2010-08-24 08:38:00 +00:00
|
|
|
* 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"
|
|
|
|
#include "libavutil/audioconvert.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 {
|
|
|
|
AVFilterBufferRef *buf;
|
|
|
|
struct Buf *next;
|
|
|
|
} Buf;
|
2010-08-24 08:38:00 +00:00
|
|
|
|
|
|
|
typedef struct {
|
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
|
|
|
|
*/
|
|
|
|
AVFilterBufferRef *buf_out;
|
|
|
|
int allocated_samples; ///< number of samples buf_out was allocated for
|
2010-08-24 08:38:00 +00:00
|
|
|
} FifoContext;
|
|
|
|
|
2012-06-21 05:55:56 +00:00
|
|
|
static av_cold int init(AVFilterContext *ctx, const char *args)
|
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-07-15 08:12:51 +00:00
|
|
|
avfilter_unref_bufferp(&buf->buf);
|
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-07-15 08:12:51 +00:00
|
|
|
avfilter_unref_bufferp(&fifo->buf_out);
|
2010-08-24 08:38:00 +00:00
|
|
|
}
|
|
|
|
|
2012-07-02 18:13:40 +00:00
|
|
|
static int add_to_queue(AVFilterLink *inlink, AVFilterBufferRef *buf)
|
2010-08-24 08:38:00 +00:00
|
|
|
{
|
|
|
|
FifoContext *fifo = inlink->dst->priv;
|
|
|
|
|
2012-07-08 15:29:42 +00:00
|
|
|
inlink->cur_buf = NULL;
|
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) {
|
|
|
|
avfilter_unref_buffer(buf);
|
|
|
|
return AVERROR(ENOMEM);
|
|
|
|
}
|
|
|
|
|
2010-08-24 08:38:00 +00:00
|
|
|
fifo->last = fifo->last->next;
|
2012-05-16 06:21:47 +00:00
|
|
|
fifo->last->buf = buf;
|
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;
|
|
|
|
}
|
|
|
|
|
2012-07-14 07:25:33 +00:00
|
|
|
static int end_frame(AVFilterLink *inlink)
|
|
|
|
{
|
|
|
|
return 0;
|
|
|
|
}
|
2010-08-24 08:38:00 +00:00
|
|
|
|
2012-07-14 07:25:33 +00:00
|
|
|
static int draw_slice(AVFilterLink *inlink, int y, int h, int slice_dir)
|
|
|
|
{
|
|
|
|
return 0;
|
|
|
|
}
|
2010-08-24 08:38:00 +00:00
|
|
|
|
2012-05-27 12:18:49 +00:00
|
|
|
/**
|
|
|
|
* Move data pointers and pts offset samples forward.
|
|
|
|
*/
|
|
|
|
static void buffer_offset(AVFilterLink *link, AVFilterBufferRef *buf,
|
|
|
|
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;
|
|
|
|
|
|
|
|
av_assert0(buf->audio->nb_samples > offset);
|
|
|
|
|
|
|
|
for (i = 0; i < planes; i++)
|
|
|
|
buf->extended_data[i] += block_align*offset;
|
|
|
|
if (buf->data != buf->extended_data)
|
|
|
|
memcpy(buf->data, buf->extended_data,
|
|
|
|
FFMIN(planes, FF_ARRAY_ELEMS(buf->data)) * sizeof(*buf->data));
|
|
|
|
buf->linesize[0] -= block_align*offset;
|
|
|
|
buf->audio->nb_samples -= offset;
|
|
|
|
|
|
|
|
if (buf->pts != AV_NOPTS_VALUE) {
|
|
|
|
buf->pts += av_rescale_q(offset, (AVRational){1, link->sample_rate},
|
|
|
|
link->time_base);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
static int calc_ptr_alignment(AVFilterBufferRef *buf)
|
|
|
|
{
|
|
|
|
int planes = av_sample_fmt_is_planar(buf->format) ?
|
|
|
|
av_get_channel_layout_nb_channels(buf->audio->channel_layout) : 1;
|
|
|
|
int min_align = 128;
|
|
|
|
int p;
|
|
|
|
|
|
|
|
for (p = 0; p < planes; p++) {
|
|
|
|
int cur_align = 128;
|
|
|
|
while ((intptr_t)buf->extended_data[p] % cur_align)
|
|
|
|
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;
|
|
|
|
AVFilterBufferRef *head = s->root.next->buf;
|
|
|
|
AVFilterBufferRef *buf_out;
|
|
|
|
int ret;
|
|
|
|
|
|
|
|
if (!s->buf_out &&
|
|
|
|
head->audio->nb_samples >= link->request_samples &&
|
|
|
|
calc_ptr_alignment(head) >= 32) {
|
|
|
|
if (head->audio->nb_samples == link->request_samples) {
|
|
|
|
buf_out = head;
|
|
|
|
queue_pop(s);
|
|
|
|
} else {
|
|
|
|
buf_out = avfilter_ref_buffer(head, AV_PERM_READ);
|
2012-07-15 09:16:53 +00:00
|
|
|
if (!buf_out)
|
|
|
|
return AVERROR(ENOMEM);
|
|
|
|
|
2012-05-27 12:18:49 +00:00
|
|
|
buf_out->audio->nb_samples = link->request_samples;
|
|
|
|
buffer_offset(link, head, link->request_samples);
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
int nb_channels = av_get_channel_layout_nb_channels(link->channel_layout);
|
|
|
|
|
|
|
|
if (!s->buf_out) {
|
|
|
|
s->buf_out = ff_get_audio_buffer(link, AV_PERM_WRITE,
|
|
|
|
link->request_samples);
|
|
|
|
if (!s->buf_out)
|
|
|
|
return AVERROR(ENOMEM);
|
|
|
|
|
|
|
|
s->buf_out->audio->nb_samples = 0;
|
|
|
|
s->buf_out->pts = head->pts;
|
|
|
|
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);
|
|
|
|
}
|
|
|
|
|
|
|
|
while (s->buf_out->audio->nb_samples < s->allocated_samples) {
|
|
|
|
int len = FFMIN(s->allocated_samples - s->buf_out->audio->nb_samples,
|
|
|
|
head->audio->nb_samples);
|
|
|
|
|
|
|
|
av_samples_copy(s->buf_out->extended_data, head->extended_data,
|
|
|
|
s->buf_out->audio->nb_samples, 0, len, nb_channels,
|
|
|
|
link->format);
|
|
|
|
s->buf_out->audio->nb_samples += len;
|
|
|
|
|
|
|
|
if (len == head->audio->nb_samples) {
|
|
|
|
avfilter_unref_buffer(head);
|
|
|
|
queue_pop(s);
|
|
|
|
|
|
|
|
if (!s->root.next &&
|
|
|
|
(ret = ff_request_frame(ctx->inputs[0])) < 0) {
|
|
|
|
if (ret == AVERROR_EOF) {
|
|
|
|
av_samples_set_silence(s->buf_out->extended_data,
|
|
|
|
s->buf_out->audio->nb_samples,
|
|
|
|
s->allocated_samples -
|
|
|
|
s->buf_out->audio->nb_samples,
|
|
|
|
nb_channels, link->format);
|
|
|
|
s->buf_out->audio->nb_samples = s->allocated_samples;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
head = s->root.next->buf;
|
|
|
|
} else {
|
|
|
|
buffer_offset(link, head, len);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
buf_out = s->buf_out;
|
|
|
|
s->buf_out = NULL;
|
|
|
|
}
|
2012-07-02 18:13:40 +00:00
|
|
|
return ff_filter_samples(link, buf_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) {
|
2012-05-27 20:45:52 +00:00
|
|
|
if ((ret = ff_request_frame(outlink->src->inputs[0])) < 0)
|
2010-08-24 08:38:00 +00:00
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* by doing this, we give ownership of the reference to the next filter,
|
|
|
|
* so we don't have to worry about dereferencing it ourselves. */
|
2012-05-16 06:21:47 +00:00
|
|
|
switch (outlink->type) {
|
|
|
|
case AVMEDIA_TYPE_VIDEO:
|
2012-07-15 07:47:01 +00:00
|
|
|
if ((ret = ff_start_frame(outlink, fifo->root.next->buf)) < 0 ||
|
|
|
|
(ret = ff_draw_slice(outlink, 0, outlink->h, 1)) < 0 ||
|
|
|
|
(ret = ff_end_frame(outlink)) < 0)
|
|
|
|
return ret;
|
|
|
|
|
2012-05-27 12:18:49 +00:00
|
|
|
queue_pop(fifo);
|
2012-05-16 06:21:47 +00:00
|
|
|
break;
|
|
|
|
case AVMEDIA_TYPE_AUDIO:
|
2012-05-27 12:18:49 +00:00
|
|
|
if (outlink->request_samples) {
|
|
|
|
return return_audio_frame(outlink->src);
|
|
|
|
} else {
|
2012-07-02 18:13:40 +00:00
|
|
|
ret = ff_filter_samples(outlink, fifo->root.next->buf);
|
2012-05-27 12:18:49 +00:00
|
|
|
queue_pop(fifo);
|
|
|
|
}
|
2012-05-16 06:21:47 +00:00
|
|
|
break;
|
|
|
|
default:
|
|
|
|
return AVERROR(EINVAL);
|
|
|
|
}
|
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
|
|
|
}
|
|
|
|
|
|
|
|
AVFilter avfilter_vf_fifo = {
|
|
|
|
.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-21 16:45:52 +00:00
|
|
|
.inputs = (const AVFilterPad[]) {{ .name = "default",
|
|
|
|
.type = AVMEDIA_TYPE_VIDEO,
|
|
|
|
.get_video_buffer= ff_null_get_video_buffer,
|
2012-07-08 15:29:42 +00:00
|
|
|
.start_frame = add_to_queue,
|
2012-07-21 16:45:52 +00:00
|
|
|
.draw_slice = draw_slice,
|
|
|
|
.end_frame = end_frame,
|
|
|
|
.rej_perms = AV_PERM_REUSE2, },
|
|
|
|
{ .name = NULL}},
|
|
|
|
.outputs = (const AVFilterPad[]) {{ .name = "default",
|
|
|
|
.type = AVMEDIA_TYPE_VIDEO,
|
|
|
|
.request_frame = request_frame, },
|
|
|
|
{ .name = NULL}},
|
2010-08-24 08:38:00 +00:00
|
|
|
};
|
2012-05-16 06:21:47 +00:00
|
|
|
|
|
|
|
AVFilter avfilter_af_afifo = {
|
|
|
|
.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-21 16:45:52 +00:00
|
|
|
.inputs = (const AVFilterPad[]) {{ .name = "default",
|
|
|
|
.type = AVMEDIA_TYPE_AUDIO,
|
|
|
|
.get_audio_buffer = ff_null_get_audio_buffer,
|
|
|
|
.filter_samples = add_to_queue,
|
|
|
|
.rej_perms = AV_PERM_REUSE2, },
|
|
|
|
{ .name = NULL}},
|
|
|
|
.outputs = (const AVFilterPad[]) {{ .name = "default",
|
|
|
|
.type = AVMEDIA_TYPE_AUDIO,
|
|
|
|
.request_frame = request_frame, },
|
|
|
|
{ .name = NULL}},
|
2012-05-16 06:21:47 +00:00
|
|
|
};
|