X-Git-Url: https://git.sesse.net/?a=blobdiff_plain;f=libavfilter%2Ffifo.c;h=e0996ba5014b11c649b5aa6bf0356f3a892a1123;hb=3a3e8c35b63a40c4d59161097dc8652c15d13779;hp=0fa0f86cb3fb7cbb7a09f1f7a2321d7de5767d62;hpb=0ff76ca86e0ea4dcf2b392c45f5fac8e5576bb0d;p=ffmpeg diff --git a/libavfilter/fifo.c b/libavfilter/fifo.c index 0fa0f86cb3f..e0996ba5014 100644 --- a/libavfilter/fifo.c +++ b/libavfilter/fifo.c @@ -23,20 +23,16 @@ * FIFO buffering filter */ -#include "libavutil/avassert.h" -#include "libavutil/channel_layout.h" #include "libavutil/common.h" #include "libavutil/mathematics.h" -#include "libavutil/samplefmt.h" #include "audio.h" #include "avfilter.h" #include "internal.h" -#include "video.h" typedef struct Buf { AVFrame *frame; - struct Buf *next; + struct Buf *next; } Buf; typedef struct FifoContext { @@ -53,38 +49,38 @@ typedef struct FifoContext { static av_cold int init(AVFilterContext *ctx) { - FifoContext *fifo = ctx->priv; - fifo->last = &fifo->root; + FifoContext *s = ctx->priv; + s->last = &s->root; return 0; } static av_cold void uninit(AVFilterContext *ctx) { - FifoContext *fifo = ctx->priv; + FifoContext *s = ctx->priv; Buf *buf, *tmp; - for (buf = fifo->root.next; buf; buf = tmp) { + for (buf = s->root.next; buf; buf = tmp) { tmp = buf->next; av_frame_free(&buf->frame); av_free(buf); } - av_frame_free(&fifo->out); + av_frame_free(&s->out); } static int add_to_queue(AVFilterLink *inlink, AVFrame *frame) { - FifoContext *fifo = inlink->dst->priv; + FifoContext *s = inlink->dst->priv; - fifo->last->next = av_mallocz(sizeof(Buf)); - if (!fifo->last->next) { + s->last->next = av_mallocz(sizeof(Buf)); + if (!s->last->next) { av_frame_free(&frame); return AVERROR(ENOMEM); } - fifo->last = fifo->last->next; - fifo->last->frame = frame; + s->last = s->last->next; + s->last->frame = frame; return 0; } @@ -98,165 +94,27 @@ static void queue_pop(FifoContext *s) s->root.next = tmp; } -/** - * Move data pointers and pts offset samples forward. - */ -static void buffer_offset(AVFilterLink *link, AVFrame *frame, - int offset) -{ - int nb_channels = link->channels; - 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(frame->nb_samples > offset); - - for (i = 0; i < planes; i++) - 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); - } -} - -static int calc_ptr_alignment(AVFrame *frame) -{ - int planes = av_sample_fmt_is_planar(frame->format) ? - frame->channels : 1; - int min_align = 128; - int p; - - for (p = 0; p < planes; p++) { - int cur_align = 128; - while ((intptr_t)frame->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; - AVFrame *head = s->root.next ? s->root.next->frame : NULL; - AVFrame *out; - int ret; - - /* if head is NULL then we're flushing the remaining samples in out */ - if (!head && !s->out) - return AVERROR_EOF; - - if (!s->out && - head->nb_samples >= link->request_samples && - calc_ptr_alignment(head) >= 32) { - if (head->nb_samples == link->request_samples) { - out = head; - queue_pop(s); - } else { - out = av_frame_clone(head); - if (!out) - return AVERROR(ENOMEM); - - out->nb_samples = link->request_samples; - buffer_offset(link, head, link->request_samples); - } - } else { - int nb_channels = link->channels; - - if (!s->out) { - s->out = ff_get_audio_buffer(link, link->request_samples); - if (!s->out) - return AVERROR(ENOMEM); - - s->out->nb_samples = 0; - s->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->out->nb_samples < s->allocated_samples) { - 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; - if (!s->root.next) - return 0; - } - head = s->root.next->frame; - - len = FFMIN(s->allocated_samples - s->out->nb_samples, - head->nb_samples); - - av_samples_copy(s->out->extended_data, head->extended_data, - s->out->nb_samples, 0, len, nb_channels, - link->format); - s->out->nb_samples += len; - - if (len == head->nb_samples) { - av_frame_free(&head); - queue_pop(s); - } else { - buffer_offset(link, head, len); - } - } - out = s->out; - s->out = NULL; - } - return ff_filter_frame(link, out); -} - static int request_frame(AVFilterLink *outlink) { - FifoContext *fifo = outlink->src->priv; + FifoContext *s = outlink->src->priv; int ret = 0; - if (!fifo->root.next) { - if ((ret = ff_request_frame(outlink->src->inputs[0])) < 0) { - if (ret == AVERROR_EOF && outlink->request_samples) - return return_audio_frame(outlink->src); + if (!s->root.next) { + if ((ret = ff_request_frame(outlink->src->inputs[0])) < 0) return ret; - } - if (!fifo->root.next) + if (!s->root.next) return 0; } - - if (outlink->request_samples) { - return return_audio_frame(outlink->src); - } else { - ret = ff_filter_frame(outlink, fifo->root.next->frame); - queue_pop(fifo); - } - + ret = ff_filter_frame(outlink, s->root.next->frame); + queue_pop(s); return ret; } static const AVFilterPad avfilter_vf_fifo_inputs[] = { { - .name = "default", - .type = AVMEDIA_TYPE_VIDEO, - .filter_frame = add_to_queue, + .name = "default", + .type = AVMEDIA_TYPE_VIDEO, + .filter_frame = add_to_queue, }, { NULL } }; @@ -270,24 +128,21 @@ static const AVFilterPad avfilter_vf_fifo_outputs[] = { { NULL } }; -AVFilter ff_vf_fifo = { - .name = "fifo", +const AVFilter ff_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), - - .inputs = avfilter_vf_fifo_inputs, - .outputs = avfilter_vf_fifo_outputs, + .init = init, + .uninit = uninit, + .priv_size = sizeof(FifoContext), + .inputs = avfilter_vf_fifo_inputs, + .outputs = avfilter_vf_fifo_outputs, }; static const AVFilterPad avfilter_af_afifo_inputs[] = { { - .name = "default", - .type = AVMEDIA_TYPE_AUDIO, - .filter_frame = add_to_queue, + .name = "default", + .type = AVMEDIA_TYPE_AUDIO, + .filter_frame = add_to_queue, }, { NULL } }; @@ -301,15 +156,12 @@ static const AVFilterPad avfilter_af_afifo_outputs[] = { { NULL } }; -AVFilter ff_af_afifo = { +const AVFilter ff_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), - - .inputs = avfilter_af_afifo_inputs, - .outputs = avfilter_af_afifo_outputs, + .init = init, + .uninit = uninit, + .priv_size = sizeof(FifoContext), + .inputs = avfilter_af_afifo_inputs, + .outputs = avfilter_af_afifo_outputs, };