fifo.c 9.87 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 27 28 29 30
#include "libavutil/avassert.h"
#include "libavutil/audioconvert.h"
#include "libavutil/mathematics.h"
#include "libavutil/samplefmt.h"

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

36 37 38 39
typedef struct Buf {
    AVFilterBufferRef *buf;
    struct Buf        *next;
} Buf;
Stefano Sabatini's avatar
Stefano Sabatini committed
40 41

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

    /**
     * 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
Stefano Sabatini's avatar
Stefano Sabatini committed
51 52
} FifoContext;

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

    return 0;
}

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

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

    avfilter_unref_buffer(fifo->buf_out);
Stefano Sabatini's avatar
Stefano Sabatini committed
73 74
}

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

79
    fifo->last->next = av_mallocz(sizeof(Buf));
80 81 82 83 84
    if (!fifo->last->next) {
        avfilter_unref_buffer(buf);
        return AVERROR(ENOMEM);
    }

Stefano Sabatini's avatar
Stefano Sabatini committed
85
    fifo->last = fifo->last->next;
86
    fifo->last->buf = buf;
87 88 89 90 91 92 93

    return 0;
}

static void start_frame(AVFilterLink *inlink, AVFilterBufferRef *buf)
{
    add_to_queue(inlink, buf);
Stefano Sabatini's avatar
Stefano Sabatini committed
94 95
}

96 97 98 99 100 101 102 103 104
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;
}

Stefano Sabatini's avatar
Stefano Sabatini committed
105 106 107 108
static void end_frame(AVFilterLink *inlink) { }

static void draw_slice(AVFilterLink *inlink, int y, int h, int slice_dir) { }

109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224
/**
 * 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);
            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;
    }
225
    return ff_filter_samples(link, buf_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
        if ((ret = ff_request_frame(outlink->src->inputs[0])) < 0)
Stefano Sabatini's avatar
Stefano Sabatini committed
235 236 237 238 239
            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. */
240 241 242 243 244
    switch (outlink->type) {
    case AVMEDIA_TYPE_VIDEO:
        ff_start_frame(outlink, fifo->root.next->buf);
        ff_draw_slice (outlink, 0, outlink->h, 1);
        ff_end_frame  (outlink);
245
        queue_pop(fifo);
246 247
        break;
    case AVMEDIA_TYPE_AUDIO:
248 249 250
        if (outlink->request_samples) {
            return return_audio_frame(outlink->src);
        } else {
251
            ret = ff_filter_samples(outlink, fifo->root.next->buf);
252 253
            queue_pop(fifo);
        }
254 255 256 257
        break;
    default:
        return AVERROR(EINVAL);
    }
Stefano Sabatini's avatar
Stefano Sabatini committed
258

259
    return ret;
Stefano Sabatini's avatar
Stefano Sabatini committed
260 261 262 263 264 265 266 267 268 269 270 271 272
}

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),

    .inputs    = (AVFilterPad[]) {{ .name            = "default",
                                    .type            = AVMEDIA_TYPE_VIDEO,
273
                                    .get_video_buffer= ff_null_get_video_buffer,
274
                                    .start_frame     = start_frame,
Stefano Sabatini's avatar
Stefano Sabatini committed
275 276 277 278 279 280 281 282 283
                                    .draw_slice      = draw_slice,
                                    .end_frame       = end_frame,
                                    .rej_perms       = AV_PERM_REUSE2, },
                                  { .name = NULL}},
    .outputs   = (AVFilterPad[]) {{ .name            = "default",
                                    .type            = AVMEDIA_TYPE_VIDEO,
                                    .request_frame   = request_frame, },
                                  { .name = NULL}},
};
284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304

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),

    .inputs    = (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   = (AVFilterPad[]) {{ .name             = "default",
                                    .type             = AVMEDIA_TYPE_AUDIO,
                                    .request_frame    = request_frame, },
                                  { .name = NULL}},
};