fifo.c 8.78 KB
Newer Older
Stefano Sabatini's avatar
Stefano Sabatini committed
1
/*
2
 * Copyright (c) 2007 Bobby Bingham
Stefano Sabatini's avatar
Stefano Sabatini committed
3
 *
4
 * This file is part of Libav.
Stefano Sabatini's avatar
Stefano Sabatini committed
5
 *
6
 * Libav is free software; you can redistribute it and/or
Stefano Sabatini's avatar
Stefano Sabatini committed
7 8 9 10
 * 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.
 *
11
 * Libav is distributed in the hope that it will be useful,
Stefano Sabatini's avatar
Stefano Sabatini committed
12 13 14 15 16
 * 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
17
 * License along with Libav; if not, write to the Free Software
Stefano Sabatini's avatar
Stefano Sabatini committed
18 19 20 21 22
 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
 */

/**
 * @file
23
 * FIFO buffering filter
Stefano Sabatini's avatar
Stefano Sabatini committed
24 25
 */

26
#include "libavutil/avassert.h"
27
#include "libavutil/channel_layout.h"
28
#include "libavutil/common.h"
29 30 31
#include "libavutil/mathematics.h"
#include "libavutil/samplefmt.h"

32
#include "audio.h"
Stefano Sabatini's avatar
Stefano Sabatini committed
33
#include "avfilter.h"
34
#include "internal.h"
35
#include "video.h"
Stefano Sabatini's avatar
Stefano Sabatini committed
36

37
typedef struct Buf {
38
    AVFrame *frame;
39 40
    struct Buf        *next;
} Buf;
Stefano Sabatini's avatar
Stefano Sabatini committed
41

42
typedef struct FifoContext {
43 44
    Buf  root;
    Buf *last;   ///< last buffered frame
45 46 47 48 49

    /**
     * When a specific number of output samples is requested, the partial
     * buffer is stored here
     */
50 51
    AVFrame *out;
    int allocated_samples;      ///< number of samples out was allocated for
Stefano Sabatini's avatar
Stefano Sabatini committed
52 53
} FifoContext;

54
static av_cold int init(AVFilterContext *ctx)
Stefano Sabatini's avatar
Stefano Sabatini committed
55 56 57 58 59 60 61 62 63 64
{
    FifoContext *fifo = ctx->priv;
    fifo->last = &fifo->root;

    return 0;
}

static av_cold void uninit(AVFilterContext *ctx)
{
    FifoContext *fifo = ctx->priv;
65
    Buf *buf, *tmp;
Stefano Sabatini's avatar
Stefano Sabatini committed
66

67 68
    for (buf = fifo->root.next; buf; buf = tmp) {
        tmp = buf->next;
69
        av_frame_free(&buf->frame);
70
        av_free(buf);
Stefano Sabatini's avatar
Stefano Sabatini committed
71
    }
72

73
    av_frame_free(&fifo->out);
Stefano Sabatini's avatar
Stefano Sabatini committed
74 75
}

76
static int add_to_queue(AVFilterLink *inlink, AVFrame *frame)
Stefano Sabatini's avatar
Stefano Sabatini committed
77 78 79
{
    FifoContext *fifo = inlink->dst->priv;

80
    fifo->last->next = av_mallocz(sizeof(Buf));
81
    if (!fifo->last->next) {
82
        av_frame_free(&frame);
83 84 85
        return AVERROR(ENOMEM);
    }

Stefano Sabatini's avatar
Stefano Sabatini committed
86
    fifo->last = fifo->last->next;
87
    fifo->last->frame = frame;
88 89 90 91

    return 0;
}

92 93 94 95 96 97 98 99 100 101 102 103
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.
 */
104
static void buffer_offset(AVFilterLink *link, AVFrame *frame,
105 106 107 108 109 110 111 112
                          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;

113
    av_assert0(frame->nb_samples > offset);
114 115

    for (i = 0; i < planes; i++)
116 117 118 119 120 121 122 123 124 125
        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);
126 127 128
    }
}

129
static int calc_ptr_alignment(AVFrame *frame)
130
{
131 132
    int planes = av_sample_fmt_is_planar(frame->format) ?
                 av_get_channel_layout_nb_channels(frame->channel_layout) : 1;
133 134 135 136 137
    int min_align = 128;
    int p;

    for (p = 0; p < planes; p++) {
        int cur_align = 128;
138
        while ((intptr_t)frame->extended_data[p] % cur_align)
139 140 141 142 143 144 145 146 147 148 149
            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;
150
    AVFrame *head = s->root.next ? s->root.next->frame : NULL;
151
    AVFrame *out;
152 153
    int ret;

154 155 156 157
    /* if head is NULL then we're flushing the remaining samples in out */
    if (!head && !s->out)
        return AVERROR_EOF;

158 159
    if (!s->out &&
        head->nb_samples >= link->request_samples &&
160
        calc_ptr_alignment(head) >= 32) {
161 162
        if (head->nb_samples == link->request_samples) {
            out = head;
163 164
            queue_pop(s);
        } else {
165 166
            out = av_frame_clone(head);
            if (!out)
167 168
                return AVERROR(ENOMEM);

169
            out->nb_samples = link->request_samples;
170 171 172 173 174
            buffer_offset(link, head, link->request_samples);
        }
    } else {
        int nb_channels = av_get_channel_layout_nb_channels(link->channel_layout);

175 176 177
        if (!s->out) {
            s->out = ff_get_audio_buffer(link, link->request_samples);
            if (!s->out)
178 179
                return AVERROR(ENOMEM);

180 181
            s->out->nb_samples = 0;
            s->out->pts                   = head->pts;
182 183 184 185 186 187 188
            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);
        }

189
        while (s->out->nb_samples < s->allocated_samples) {
190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208
            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;
            }
            head = s->root.next->frame;

            len = FFMIN(s->allocated_samples - s->out->nb_samples,
                        head->nb_samples);
209

210 211
            av_samples_copy(s->out->extended_data, head->extended_data,
                            s->out->nb_samples, 0, len, nb_channels,
212
                            link->format);
213
            s->out->nb_samples += len;
214

215 216
            if (len == head->nb_samples) {
                av_frame_free(&head);
217 218 219 220 221
                queue_pop(s);
            } else {
                buffer_offset(link, head, len);
            }
        }
222 223
        out = s->out;
        s->out = NULL;
224
    }
225
    return ff_filter_frame(link, out);
226 227
}

Stefano Sabatini's avatar
Stefano Sabatini committed
228 229 230
static int request_frame(AVFilterLink *outlink)
{
    FifoContext *fifo = outlink->src->priv;
231
    int ret = 0;
Stefano Sabatini's avatar
Stefano Sabatini committed
232 233

    if (!fifo->root.next) {
234 235 236
        if ((ret = ff_request_frame(outlink->src->inputs[0])) < 0) {
            if (ret == AVERROR_EOF && outlink->request_samples)
                return return_audio_frame(outlink->src);
Stefano Sabatini's avatar
Stefano Sabatini committed
237
            return ret;
238
        }
Stefano Sabatini's avatar
Stefano Sabatini committed
239 240
    }

241 242 243
    if (outlink->request_samples) {
        return return_audio_frame(outlink->src);
    } else {
244
        ret = ff_filter_frame(outlink, fifo->root.next->frame);
245
        queue_pop(fifo);
246
    }
Stefano Sabatini's avatar
Stefano Sabatini committed
247

248
    return ret;
Stefano Sabatini's avatar
Stefano Sabatini committed
249 250
}

251 252 253 254 255
static const AVFilterPad avfilter_vf_fifo_inputs[] = {
    {
        .name             = "default",
        .type             = AVMEDIA_TYPE_VIDEO,
        .get_video_buffer = ff_null_get_video_buffer,
256
        .filter_frame     = add_to_queue,
257 258 259 260 261 262 263 264 265 266 267 268 269
    },
    { NULL }
};

static const AVFilterPad avfilter_vf_fifo_outputs[] = {
    {
        .name          = "default",
        .type          = AVMEDIA_TYPE_VIDEO,
        .request_frame = request_frame,
    },
    { NULL }
};

270
AVFilter ff_vf_fifo = {
Stefano Sabatini's avatar
Stefano Sabatini committed
271 272 273 274 275 276 277 278
    .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),

279 280 281 282 283 284 285 286 287
    .inputs    = avfilter_vf_fifo_inputs,
    .outputs   = avfilter_vf_fifo_outputs,
};

static const AVFilterPad avfilter_af_afifo_inputs[] = {
    {
        .name             = "default",
        .type             = AVMEDIA_TYPE_AUDIO,
        .get_audio_buffer = ff_null_get_audio_buffer,
288
        .filter_frame     = add_to_queue,
289 290 291 292 293 294 295 296 297 298 299
    },
    { NULL }
};

static const AVFilterPad avfilter_af_afifo_outputs[] = {
    {
        .name          = "default",
        .type          = AVMEDIA_TYPE_AUDIO,
        .request_frame = request_frame,
    },
    { NULL }
Stefano Sabatini's avatar
Stefano Sabatini committed
300
};
301

302
AVFilter ff_af_afifo = {
303 304 305 306 307 308 309 310
    .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),

311 312
    .inputs    = avfilter_af_afifo_inputs,
    .outputs   = avfilter_af_afifo_outputs,
313
};