7f1af825 |
/* |
3fa77bde |
* Copyright (c) 2007 Bobby Bingham |
7f1af825 |
*
* 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 |
4a97ff2f |
* FIFO buffering filter |
7f1af825 |
*/
|
f75be985 |
#include "libavutil/avassert.h" |
a903f8f0 |
#include "libavutil/channel_layout.h" |
1d9c2dc8 |
#include "libavutil/common.h" |
f75be985 |
#include "libavutil/mathematics.h"
#include "libavutil/samplefmt.h"
|
4a97ff2f |
#include "audio.h" |
7f1af825 |
#include "avfilter.h" |
803391f7 |
#include "internal.h" |
c04c533f |
#include "video.h" |
7f1af825 |
|
4a97ff2f |
typedef struct Buf { |
7e350379 |
AVFrame *frame; |
4a97ff2f |
struct Buf *next;
} Buf; |
7f1af825 |
|
58400ac1 |
typedef struct FifoContext { |
4a97ff2f |
Buf root;
Buf *last; ///< last buffered frame |
f75be985 |
/**
* When a specific number of output samples is requested, the partial
* buffer is stored here
*/ |
7e350379 |
AVFrame *out;
int allocated_samples; ///< number of samples out was allocated for |
7f1af825 |
} FifoContext;
|
d69a4177 |
static av_cold int init(AVFilterContext *ctx) |
7f1af825 |
{
FifoContext *fifo = ctx->priv;
fifo->last = &fifo->root;
return 0;
}
static av_cold void uninit(AVFilterContext *ctx)
{
FifoContext *fifo = ctx->priv; |
4a97ff2f |
Buf *buf, *tmp; |
7f1af825 |
|
4a97ff2f |
for (buf = fifo->root.next; buf; buf = tmp) {
tmp = buf->next; |
7e350379 |
av_frame_free(&buf->frame); |
4a97ff2f |
av_free(buf); |
7f1af825 |
} |
f75be985 |
|
7e350379 |
av_frame_free(&fifo->out); |
7f1af825 |
}
|
7e350379 |
static int add_to_queue(AVFilterLink *inlink, AVFrame *frame) |
7f1af825 |
{
FifoContext *fifo = inlink->dst->priv;
|
4a97ff2f |
fifo->last->next = av_mallocz(sizeof(Buf)); |
cd991462 |
if (!fifo->last->next) { |
7e350379 |
av_frame_free(&frame); |
cd991462 |
return AVERROR(ENOMEM);
}
|
7f1af825 |
fifo->last = fifo->last->next; |
7e350379 |
fifo->last->frame = frame; |
cd991462 |
return 0;
}
|
f75be985 |
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.
*/ |
7e350379 |
static void buffer_offset(AVFilterLink *link, AVFrame *frame, |
f75be985 |
int offset)
{ |
fdd96637 |
int nb_channels = link->channels; |
f75be985 |
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;
|
7e350379 |
av_assert0(frame->nb_samples > offset); |
f75be985 |
for (i = 0; i < planes; i++) |
7e350379 |
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); |
f75be985 |
}
}
|
7e350379 |
static int calc_ptr_alignment(AVFrame *frame) |
f75be985 |
{ |
7e350379 |
int planes = av_sample_fmt_is_planar(frame->format) ? |
6af050d7 |
frame->channels : 1; |
f75be985 |
int min_align = 128;
int p;
for (p = 0; p < planes; p++) {
int cur_align = 128; |
7e350379 |
while ((intptr_t)frame->extended_data[p] % cur_align) |
f75be985 |
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; |
3799376d |
AVFrame *head = s->root.next ? s->root.next->frame : NULL; |
7e350379 |
AVFrame *out; |
f75be985 |
int ret;
|
3799376d |
/* if head is NULL then we're flushing the remaining samples in out */
if (!head && !s->out)
return AVERROR_EOF;
|
7e350379 |
if (!s->out &&
head->nb_samples >= link->request_samples && |
f75be985 |
calc_ptr_alignment(head) >= 32) { |
7e350379 |
if (head->nb_samples == link->request_samples) {
out = head; |
f75be985 |
queue_pop(s);
} else { |
7e350379 |
out = av_frame_clone(head);
if (!out) |
1dc42050 |
return AVERROR(ENOMEM);
|
7e350379 |
out->nb_samples = link->request_samples; |
f75be985 |
buffer_offset(link, head, link->request_samples);
}
} else { |
fdd96637 |
int nb_channels = link->channels; |
f75be985 |
|
7e350379 |
if (!s->out) {
s->out = ff_get_audio_buffer(link, link->request_samples);
if (!s->out) |
f75be985 |
return AVERROR(ENOMEM);
|
7e350379 |
s->out->nb_samples = 0;
s->out->pts = head->pts; |
f75be985 |
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);
}
|
7e350379 |
while (s->out->nb_samples < s->allocated_samples) { |
9bfc6e02 |
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; |
67d3f529 |
if (!s->root.next)
return 0; |
9bfc6e02 |
}
head = s->root.next->frame;
len = FFMIN(s->allocated_samples - s->out->nb_samples,
head->nb_samples); |
f75be985 |
|
7e350379 |
av_samples_copy(s->out->extended_data, head->extended_data,
s->out->nb_samples, 0, len, nb_channels, |
f75be985 |
link->format); |
7e350379 |
s->out->nb_samples += len; |
f75be985 |
|
7e350379 |
if (len == head->nb_samples) {
av_frame_free(&head); |
f75be985 |
queue_pop(s);
} else {
buffer_offset(link, head, len);
}
} |
7e350379 |
out = s->out;
s->out = NULL; |
f75be985 |
} |
7e350379 |
return ff_filter_frame(link, out); |
f75be985 |
}
|
7f1af825 |
static int request_frame(AVFilterLink *outlink)
{
FifoContext *fifo = outlink->src->priv; |
cd991462 |
int ret = 0; |
7f1af825 |
if (!fifo->root.next) { |
3799376d |
if ((ret = ff_request_frame(outlink->src->inputs[0])) < 0) {
if (ret == AVERROR_EOF && outlink->request_samples)
return return_audio_frame(outlink->src); |
7f1af825 |
return ret; |
3799376d |
} |
67d3f529 |
if (!fifo->root.next)
return 0; |
7f1af825 |
}
|
59907340 |
if (outlink->request_samples) {
return return_audio_frame(outlink->src);
} else { |
7e350379 |
ret = ff_filter_frame(outlink, fifo->root.next->frame); |
59907340 |
queue_pop(fifo);
} |
7f1af825 |
|
cd991462 |
return ret; |
7f1af825 |
}
|
568c70e7 |
static const AVFilterPad avfilter_vf_fifo_inputs[] = {
{
.name = "default",
.type = AVMEDIA_TYPE_VIDEO, |
05945c7f |
.filter_frame = add_to_queue, |
568c70e7 |
},
{ NULL }
};
static const AVFilterPad avfilter_vf_fifo_outputs[] = {
{
.name = "default",
.type = AVMEDIA_TYPE_VIDEO,
.request_frame = request_frame,
},
{ NULL }
};
|
cd43ca04 |
AVFilter ff_vf_fifo = { |
7f1af825 |
.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),
|
568c70e7 |
.inputs = avfilter_vf_fifo_inputs,
.outputs = avfilter_vf_fifo_outputs,
};
static const AVFilterPad avfilter_af_afifo_inputs[] = {
{
.name = "default",
.type = AVMEDIA_TYPE_AUDIO, |
cd7febd3 |
.filter_frame = add_to_queue, |
568c70e7 |
},
{ NULL }
};
static const AVFilterPad avfilter_af_afifo_outputs[] = {
{
.name = "default",
.type = AVMEDIA_TYPE_AUDIO,
.request_frame = request_frame,
},
{ NULL } |
7f1af825 |
}; |
4a97ff2f |
|
cd43ca04 |
AVFilter ff_af_afifo = { |
4a97ff2f |
.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),
|
568c70e7 |
.inputs = avfilter_af_afifo_inputs,
.outputs = avfilter_af_afifo_outputs, |
4a97ff2f |
}; |