2 * Copyright (c) 2015 Derek Buitenhuis
4 * This file is part of FFmpeg.
6 * FFmpeg is free software; you can redistribute it and/or
7 * modify it under the terms of the GNU Lesser General Public
8 * License as published by the Free Software Foundation; either
9 * version 2.1 of the License, or (at your option) any later version.
11 * FFmpeg is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
14 * Lesser General Public License for more details.
16 * You should have received a copy of the GNU Lesser General Public
17 * License along with FFmpeg; if not, write to the Free Software
18 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
21 #include "libavutil/opt.h"
27 #define DEFAULT_LENGTH 300
29 typedef struct ReverseContext {
32 unsigned int frames_size;
33 unsigned int pts_size;
38 static av_cold int init(AVFilterContext *ctx)
40 ReverseContext *s = ctx->priv;
42 s->pts = av_fast_realloc(NULL, &s->pts_size,
43 DEFAULT_LENGTH * sizeof(*(s->pts)));
45 return AVERROR(ENOMEM);
47 s->frames = av_fast_realloc(NULL, &s->frames_size,
48 DEFAULT_LENGTH * sizeof(*(s->frames)));
51 return AVERROR(ENOMEM);
57 static av_cold void uninit(AVFilterContext *ctx)
59 ReverseContext *s = ctx->priv;
65 static int config_output(AVFilterLink *outlink)
67 outlink->flags |= FF_LINK_FLAG_REQUEST_LOOP;
71 static int filter_frame(AVFilterLink *inlink, AVFrame *in)
73 AVFilterContext *ctx = inlink->dst;
74 ReverseContext *s = ctx->priv;
76 if (s->nb_frames + 1 > s->frames_size / sizeof(*(s->frames))) {
79 ptr = av_fast_realloc(s->pts, &s->pts_size, s->pts_size * 2);
81 return AVERROR(ENOMEM);
84 ptr = av_fast_realloc(s->frames, &s->frames_size, s->frames_size * 2);
86 return AVERROR(ENOMEM);
90 s->frames[s->nb_frames] = in;
91 s->pts[s->nb_frames] = in->pts;
97 static int request_frame(AVFilterLink *outlink)
99 AVFilterContext *ctx = outlink->src;
100 ReverseContext *s = ctx->priv;
103 ret = ff_request_frame(ctx->inputs[0]);
105 if (ret == AVERROR_EOF && s->nb_frames > 0) {
106 AVFrame *out = s->frames[s->nb_frames - 1];
107 out->pts = s->pts[s->flush_idx++];
108 ret = ff_filter_frame(outlink, out);
115 static const AVFilterPad reverse_inputs[] = {
118 .type = AVMEDIA_TYPE_VIDEO,
119 .filter_frame = filter_frame,
124 static const AVFilterPad reverse_outputs[] = {
127 .type = AVMEDIA_TYPE_VIDEO,
128 .request_frame = request_frame,
129 .config_props = config_output,
134 AVFilter ff_vf_reverse = {
136 .description = NULL_IF_CONFIG_SMALL("Reverse a clip."),
137 .priv_size = sizeof(ReverseContext),
140 .inputs = reverse_inputs,
141 .outputs = reverse_outputs,