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;
77 if (s->nb_frames + 1 > s->pts_size / sizeof(*(s->pts))) {
78 ptr = av_fast_realloc(s->pts, &s->pts_size, s->pts_size * 2);
80 return AVERROR(ENOMEM);
84 if (s->nb_frames + 1 > s->frames_size / sizeof(*(s->frames))) {
85 ptr = av_fast_realloc(s->frames, &s->frames_size, s->frames_size * 2);
87 return AVERROR(ENOMEM);
91 s->frames[s->nb_frames] = in;
92 s->pts[s->nb_frames] = in->pts;
98 #if CONFIG_REVERSE_FILTER
100 static int request_frame(AVFilterLink *outlink)
102 AVFilterContext *ctx = outlink->src;
103 ReverseContext *s = ctx->priv;
106 ret = ff_request_frame(ctx->inputs[0]);
108 if (ret == AVERROR_EOF && s->nb_frames > 0) {
109 AVFrame *out = s->frames[s->nb_frames - 1];
110 out->pts = s->pts[s->flush_idx++];
111 ret = ff_filter_frame(outlink, out);
118 static const AVFilterPad reverse_inputs[] = {
121 .type = AVMEDIA_TYPE_VIDEO,
122 .filter_frame = filter_frame,
127 static const AVFilterPad reverse_outputs[] = {
130 .type = AVMEDIA_TYPE_VIDEO,
131 .request_frame = request_frame,
132 .config_props = config_output,
137 AVFilter ff_vf_reverse = {
139 .description = NULL_IF_CONFIG_SMALL("Reverse a clip."),
140 .priv_size = sizeof(ReverseContext),
143 .inputs = reverse_inputs,
144 .outputs = reverse_outputs,
147 #endif /* CONFIG_REVERSE_FILTER */
149 #if CONFIG_AREVERSE_FILTER
151 static int query_formats(AVFilterContext *ctx)
153 AVFilterFormats *formats;
154 AVFilterChannelLayouts *layouts;
157 layouts = ff_all_channel_layouts();
159 return AVERROR(ENOMEM);
160 ret = ff_set_common_channel_layouts(ctx, layouts);
164 ret = ff_set_common_formats(ctx, ff_planar_sample_fmts());
168 formats = ff_all_samplerates();
170 return AVERROR(ENOMEM);
171 return ff_set_common_samplerates(ctx, formats);
174 static int areverse_request_frame(AVFilterLink *outlink)
176 AVFilterContext *ctx = outlink->src;
177 ReverseContext *s = ctx->priv;
180 ret = ff_request_frame(ctx->inputs[0]);
182 if (ret == AVERROR_EOF && s->nb_frames > 0) {
183 AVFrame *out = s->frames[s->nb_frames - 1];
184 out->pts = s->pts[s->flush_idx++];
186 for (p = 0; p < outlink->channels; p++) {
187 switch (outlink->format) {
188 case AV_SAMPLE_FMT_U8P: {
189 uint8_t *dst = (uint8_t *)out->extended_data[p];
190 for (i = 0, j = out->nb_samples - 1; i < j; i++, j--)
191 FFSWAP(uint8_t, dst[i], dst[j]);
194 case AV_SAMPLE_FMT_S16P: {
195 int16_t *dst = (int16_t *)out->extended_data[p];
196 for (i = 0, j = out->nb_samples - 1; i < j; i++, j--)
197 FFSWAP(int16_t, dst[i], dst[j]);
200 case AV_SAMPLE_FMT_S32P: {
201 int32_t *dst = (int32_t *)out->extended_data[p];
202 for (i = 0, j = out->nb_samples - 1; i < j; i++, j--)
203 FFSWAP(int32_t, dst[i], dst[j]);
206 case AV_SAMPLE_FMT_FLTP: {
207 float *dst = (float *)out->extended_data[p];
208 for (i = 0, j = out->nb_samples - 1; i < j; i++, j--)
209 FFSWAP(float, dst[i], dst[j]);
212 case AV_SAMPLE_FMT_DBLP: {
213 double *dst = (double *)out->extended_data[p];
214 for (i = 0, j = out->nb_samples - 1; i < j; i++, j--)
215 FFSWAP(double, dst[i], dst[j]);
221 ret = ff_filter_frame(outlink, out);
228 static const AVFilterPad areverse_inputs[] = {
231 .type = AVMEDIA_TYPE_AUDIO,
232 .filter_frame = filter_frame,
238 static const AVFilterPad areverse_outputs[] = {
241 .type = AVMEDIA_TYPE_AUDIO,
242 .request_frame = areverse_request_frame,
243 .config_props = config_output,
248 AVFilter ff_af_areverse = {
250 .description = NULL_IF_CONFIG_SMALL("Reverse an audio clip."),
251 .query_formats = query_formats,
252 .priv_size = sizeof(ReverseContext),
255 .inputs = areverse_inputs,
256 .outputs = areverse_outputs,
259 #endif /* CONFIG_AREVERSE_FILTER */