aboutsummaryrefslogtreecommitdiffstats
path: root/libavfilter/f_loop.c
blob: f58436e679376252805099f760b53324f5c7efca (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
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
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
/*
 * Copyright (c) 2016 Paul B Mahol
 *
 * 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
 */

#include "config_components.h"

#include "libavutil/audio_fifo.h"
#include "libavutil/internal.h"
#include "libavutil/mem.h"
#include "libavutil/opt.h"
#include "avfilter.h"
#include "audio.h"
#include "filters.h"
#include "internal.h"
#include "video.h"

typedef struct LoopContext {
    const AVClass *class;

    AVAudioFifo *fifo;
    AVAudioFifo *left;
    AVFrame **frames;
    int nb_frames;
    int current_frame;
    int64_t time_pts;
    int64_t duration;
    int64_t current_sample;
    int64_t nb_samples;
    int64_t ignored_samples;

    int loop;
    int eof;
    int64_t size;
    int64_t start;
    int64_t time;
    int64_t pts;
    int64_t pts_offset;
    int64_t eof_pts;
} LoopContext;

#define AFLAGS AV_OPT_FLAG_AUDIO_PARAM|AV_OPT_FLAG_FILTERING_PARAM
#define VFLAGS AV_OPT_FLAG_VIDEO_PARAM|AV_OPT_FLAG_FILTERING_PARAM
#define OFFSET(x) offsetof(LoopContext, x)

static void check_size(AVFilterContext *ctx)
{
    LoopContext *s = ctx->priv;

    if (!s->size)
        av_log(ctx, AV_LOG_WARNING, "Number of %s to loop is not set!\n",
               ctx->input_pads[0].type == AVMEDIA_TYPE_VIDEO ? "frames" : "samples");
}

static void update_time(AVFilterContext *ctx, AVRational tb)
{
    LoopContext *s = ctx->priv;

    if (s->time != INT64_MAX) {
        int64_t time_pts = av_rescale_q(s->time, AV_TIME_BASE_Q, tb);
        if (s->time_pts == AV_NOPTS_VALUE || time_pts < s->time_pts)
            s->time_pts = time_pts;
    }
}

#if CONFIG_ALOOP_FILTER

static int aconfig_input(AVFilterLink *inlink)
{
    AVFilterContext *ctx = inlink->dst;
    LoopContext *s  = ctx->priv;

    s->time_pts = AV_NOPTS_VALUE;

    s->fifo = av_audio_fifo_alloc(inlink->format, inlink->ch_layout.nb_channels, 8192);
    s->left = av_audio_fifo_alloc(inlink->format, inlink->ch_layout.nb_channels, 8192);
    if (!s->fifo || !s->left)
        return AVERROR(ENOMEM);

    check_size(ctx);

    return 0;
}

static av_cold void auninit(AVFilterContext *ctx)
{
    LoopContext *s = ctx->priv;

    av_audio_fifo_free(s->fifo);
    av_audio_fifo_free(s->left);
}

static int push_samples(AVFilterContext *ctx, int nb_samples)
{
    AVFilterLink *outlink = ctx->outputs[0];
    LoopContext *s = ctx->priv;
    AVFrame *out;
    int ret = 0, i = 0;

    while (s->loop != 0 && i < nb_samples) {
        out = ff_get_audio_buffer(outlink, FFMIN(nb_samples, s->nb_samples - s->current_sample));
        if (!out)
            return AVERROR(ENOMEM);
        ret = av_audio_fifo_peek_at(s->fifo, (void **)out->extended_data, out->nb_samples, s->current_sample);
        if (ret < 0) {
            av_frame_free(&out);
            return ret;
        }
        out->pts = s->pts;
        out->nb_samples = ret;
        s->pts += av_rescale_q(out->nb_samples, (AVRational){1, outlink->sample_rate}, outlink->time_base);
        i += out->nb_samples;
        s->current_sample += out->nb_samples;

        ret = ff_filter_frame(outlink, out);
        if (ret < 0)
            return ret;

        if (s->current_sample >= s->nb_samples) {
            s->current_sample = 0;

            if (s->loop > 0)
                s->loop--;
        }
    }

    return ret;
}

static int afilter_frame(AVFilterLink *inlink, AVFrame *frame)
{
    AVFilterContext *ctx = inlink->dst;
    AVFilterLink *outlink = ctx->outputs[0];
    LoopContext *s = ctx->priv;
    int ret = 0;

    if (((s->start >= 0 && s->ignored_samples + frame->nb_samples > s->start) ||
         (s->time_pts != AV_NOPTS_VALUE &&
          frame->pts >= s->time_pts)) &&
        s->size > 0 && s->loop != 0) {
        if (s->nb_samples < s->size) {
            int written = FFMIN(frame->nb_samples, s->size - s->nb_samples);
            int drain = 0;

            if (s->start < 0)
                s->start = inlink->sample_count_out - written;

            ret = av_audio_fifo_write(s->fifo, (void **)frame->extended_data, written);
            if (ret < 0)
                return ret;
            if (!s->nb_samples) {
                drain = FFMAX(0, s->start - s->ignored_samples);
                s->pts = frame->pts;
                av_audio_fifo_drain(s->fifo, drain);
                s->pts += av_rescale_q(s->start - s->ignored_samples, (AVRational){1, outlink->sample_rate}, outlink->time_base);
            }
            s->nb_samples += ret - drain;
            drain = frame->nb_samples - written;
            if (s->nb_samples == s->size && drain > 0) {
                int ret2;

                ret2 = av_audio_fifo_write(s->left, (void **)frame->extended_data, frame->nb_samples);
                if (ret2 < 0)
                   return ret2;
                av_audio_fifo_drain(s->left, drain);
            }
            frame->nb_samples = ret;
            s->pts += av_rescale_q(ret, (AVRational){1, outlink->sample_rate}, outlink->time_base);
            ret = ff_filter_frame(outlink, frame);
        } else {
            int nb_samples = frame->nb_samples;

            av_frame_free(&frame);
            ret = push_samples(ctx, nb_samples);
        }
    } else {
        s->ignored_samples += frame->nb_samples;
        frame->pts = s->pts;
        s->pts += av_rescale_q(frame->nb_samples, (AVRational){1, outlink->sample_rate}, outlink->time_base);
        ret = ff_filter_frame(outlink, frame);
    }

    return ret;
}

static int arequest_frame(AVFilterLink *outlink)
{
    AVFilterContext *ctx = outlink->src;
    LoopContext *s = ctx->priv;
    int ret = 0;

    if ((!s->size) ||
        (s->nb_samples < s->size) ||
        (s->nb_samples >= s->size && s->loop == 0)) {
        int nb_samples = av_audio_fifo_size(s->left);

        if (s->loop == 0 && nb_samples > 0) {
            AVFrame *out;

            out = ff_get_audio_buffer(outlink, nb_samples);
            if (!out)
                return AVERROR(ENOMEM);
            av_audio_fifo_read(s->left, (void **)out->extended_data, nb_samples);
            out->pts = s->pts;
            s->pts += av_rescale_q(nb_samples, (AVRational){1, outlink->sample_rate}, outlink->time_base);
            ret = ff_filter_frame(outlink, out);
            if (ret < 0)
                return ret;
        }
        ret = ff_request_frame(ctx->inputs[0]);
    } else {
        ret = push_samples(ctx, 1024);
    }

    if (s->eof && s->nb_samples > 0 && s->loop != 0) {
        ret = push_samples(ctx, 1024);
    }

    return ret;
}

static int aactivate(AVFilterContext *ctx)
{
    AVFilterLink *inlink = ctx->inputs[0];
    AVFilterLink *outlink = ctx->outputs[0];
    LoopContext *s = ctx->priv;
    AVFrame *frame = NULL;
    int ret, status;

    FF_FILTER_FORWARD_STATUS_BACK(outlink, inlink);

    update_time(ctx, inlink->time_base);

    if (!s->eof && (s->nb_samples < s->size || !s->loop || !s->size)) {
        const int in_nb_samples = FFMIN(1024, s->size - s->nb_samples);
        if (in_nb_samples == 0)
            ret = ff_inlink_consume_frame(inlink, &frame);
        else
            ret = ff_inlink_consume_samples(inlink, in_nb_samples, in_nb_samples, &frame);
        if (ret < 0)
            return ret;
        if (ret > 0)
            return afilter_frame(inlink, frame);
    }

    if (!s->eof && ff_inlink_acknowledge_status(inlink, &status, &s->eof_pts)) {
        if (status == AVERROR_EOF) {
            s->size = s->nb_samples;
            s->eof = 1;
        }
    }

    if (s->eof && (!s->loop || !s->size)) {
        ff_outlink_set_status(outlink, AVERROR_EOF, s->eof_pts + s->pts_offset);
        return 0;
    }

    if (!s->eof && (!s->size ||
        (s->nb_samples < s->size) ||
        (s->nb_samples >= s->size && s->loop == 0))) {
        FF_FILTER_FORWARD_WANTED(outlink, inlink);
    } else if (s->loop && s->nb_samples == s->size) {
        return arequest_frame(outlink);
    }

    return FFERROR_NOT_READY;
}

static const AVOption aloop_options[] = {
    { "loop",  "number of loops",               OFFSET(loop),  AV_OPT_TYPE_INT,   {.i64 = 0 }, -1, INT_MAX,   AFLAGS },
    { "size",  "max number of samples to loop", OFFSET(size),  AV_OPT_TYPE_INT64, {.i64 = 0 },  0, INT32_MAX, AFLAGS },
    { "start", "set the loop start sample",     OFFSET(start), AV_OPT_TYPE_INT64, {.i64 = 0 }, -1, INT64_MAX, AFLAGS },
    { "time",  "set the loop start time",       OFFSET(time),  AV_OPT_TYPE_DURATION, {.i64=INT64_MAX}, INT64_MIN, INT64_MAX, AFLAGS },
    { NULL }
};

AVFILTER_DEFINE_CLASS(aloop);

static const AVFilterPad ainputs[] = {
    {
        .name         = "default",
        .type         = AVMEDIA_TYPE_AUDIO,
        .config_props = aconfig_input,
    },
};

const AVFilter ff_af_aloop = {
    .name          = "aloop",
    .description   = NULL_IF_CONFIG_SMALL("Loop audio samples."),
    .priv_size     = sizeof(LoopContext),
    .priv_class    = &aloop_class,
    .activate      = aactivate,
    .uninit        = auninit,
    FILTER_INPUTS(ainputs),
    FILTER_OUTPUTS(ff_audio_default_filterpad),
};
#endif /* CONFIG_ALOOP_FILTER */

#if CONFIG_LOOP_FILTER

static av_cold int init(AVFilterContext *ctx)
{
    LoopContext *s = ctx->priv;

    s->time_pts = AV_NOPTS_VALUE;

    s->frames = av_calloc(s->size, sizeof(*s->frames));
    if (!s->frames)
        return AVERROR(ENOMEM);

    check_size(ctx);

    return 0;
}

static void free_frames(AVFilterContext *ctx)
{
    LoopContext *s = ctx->priv;

    for (int i = 0; i < s->nb_frames; i++)
        av_frame_free(&s->frames[i]);
}

static av_cold void uninit(AVFilterContext *ctx)
{
    LoopContext *s = ctx->priv;

    free_frames(ctx);
    av_freep(&s->frames);
    s->nb_frames = 0;
}

static int push_frame(AVFilterContext *ctx)
{
    AVFilterLink *outlink = ctx->outputs[0];
    LoopContext *s = ctx->priv;
    AVFrame *out;
    int ret;

    out = av_frame_clone(s->frames[s->current_frame]);
    if (!out)
        return AVERROR(ENOMEM);
    out->pts += s->pts_offset;
    ret = ff_filter_frame(outlink, out);
    s->current_frame++;

    if (s->current_frame >= s->nb_frames) {
        s->current_frame = 0;

        s->pts_offset += s->duration;
        if (s->loop > 0)
            s->loop--;
        if (s->loop == 0)
            free_frames(ctx);
    }

    return ret;
}

static int filter_frame(AVFilterLink *inlink, AVFrame *frame)
{
    AVFilterContext *ctx = inlink->dst;
    AVFilterLink *outlink = ctx->outputs[0];
    LoopContext *s = ctx->priv;
    int64_t duration;
    int ret = 0;

    if (((s->start >= 0 && inlink->frame_count_out >= s->start) ||
         (s->time_pts != AV_NOPTS_VALUE &&
          frame->pts >= s->time_pts)) &&
        s->size > 0 && s->loop != 0) {
        if (s->nb_frames < s->size) {
            s->frames[s->nb_frames] = av_frame_clone(frame);
            if (!s->frames[s->nb_frames]) {
                av_frame_free(&frame);
                return AVERROR(ENOMEM);
            }
            s->nb_frames++;
            if (frame->duration)
                duration = frame->duration;
            else
                duration = av_rescale_q(1, av_inv_q(outlink->frame_rate), outlink->time_base);
            s->duration += duration;
            s->pts_offset = s->duration;
            ret = ff_filter_frame(outlink, frame);
        } else {
            av_frame_free(&frame);
            ret = push_frame(ctx);
        }
    } else {
        frame->pts += s->pts_offset - s->duration;
        ret = ff_filter_frame(outlink, frame);
    }

    return ret;
}

static int activate(AVFilterContext *ctx)
{
    AVFilterLink *inlink = ctx->inputs[0];
    AVFilterLink *outlink = ctx->outputs[0];
    LoopContext *s = ctx->priv;
    AVFrame *frame = NULL;
    int ret, status;

    ret = ff_outlink_get_status(outlink);
    if (ret) {
        ff_inlink_set_status(inlink, ret);
        free_frames(ctx);
        return 0;
    }

    update_time(ctx, inlink->time_base);

    if (!s->eof && (s->nb_frames < s->size || !s->loop || !s->size)) {
        ret = ff_inlink_consume_frame(inlink, &frame);
        if (ret < 0)
            return ret;
        if (ret > 0)
            return filter_frame(inlink, frame);
    }

    if (!s->eof && ff_inlink_acknowledge_status(inlink, &status, &s->eof_pts)) {
        if (status == AVERROR_EOF) {
            s->size = s->nb_frames;
            s->eof = 1;
        }
    }

    if (s->eof && (!s->loop || !s->size)) {
        ff_outlink_set_status(outlink, AVERROR_EOF, s->eof_pts + s->pts_offset);
        free_frames(ctx);
        return 0;
    }

    if (!s->eof && (!s->size ||
        (s->nb_frames < s->size) ||
        (s->nb_frames >= s->size && s->loop == 0))) {
        FF_FILTER_FORWARD_WANTED(outlink, inlink);
    } else if (s->loop && s->nb_frames == s->size) {
        return push_frame(ctx);
    }

    return FFERROR_NOT_READY;
}

static const AVOption loop_options[] = {
    { "loop",  "number of loops",              OFFSET(loop),  AV_OPT_TYPE_INT,   {.i64 = 0 }, -1, INT_MAX,   VFLAGS },
    { "size",  "max number of frames to loop", OFFSET(size),  AV_OPT_TYPE_INT64, {.i64 = 0 },  0, INT16_MAX, VFLAGS },
    { "start", "set the loop start frame",     OFFSET(start), AV_OPT_TYPE_INT64, {.i64 = 0 }, -1, INT64_MAX, VFLAGS },
    { "time",  "set the loop start time",      OFFSET(time),  AV_OPT_TYPE_DURATION, {.i64=INT64_MAX}, INT64_MIN, INT64_MAX, VFLAGS },
    { NULL }
};

AVFILTER_DEFINE_CLASS(loop);

const AVFilter ff_vf_loop = {
    .name        = "loop",
    .description = NULL_IF_CONFIG_SMALL("Loop video frames."),
    .priv_size   = sizeof(LoopContext),
    .priv_class  = &loop_class,
    .init        = init,
    .uninit      = uninit,
    .activate    = activate,
    FILTER_INPUTS(ff_video_default_filterpad),
    FILTER_OUTPUTS(ff_video_default_filterpad),
};
#endif /* CONFIG_LOOP_FILTER */