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 filter_frame(AVFilterLink *inlink, AVFrame *in)
67 AVFilterContext *ctx = inlink->dst;
68 ReverseContext *s = ctx->priv;
71 if (s->nb_frames + 1 > s->pts_size / sizeof(*(s->pts))) {
72 ptr = av_fast_realloc(s->pts, &s->pts_size, s->pts_size * 2);
74 return AVERROR(ENOMEM);
78 if (s->nb_frames + 1 > s->frames_size / sizeof(*(s->frames))) {
79 ptr = av_fast_realloc(s->frames, &s->frames_size, s->frames_size * 2);
81 return AVERROR(ENOMEM);
85 s->frames[s->nb_frames] = in;
86 s->pts[s->nb_frames] = in->pts;
92 #if CONFIG_REVERSE_FILTER
94 static int request_frame(AVFilterLink *outlink)
96 AVFilterContext *ctx = outlink->src;
97 ReverseContext *s = ctx->priv;
100 ret = ff_request_frame(ctx->inputs[0]);
102 if (ret == AVERROR_EOF && s->nb_frames > 0) {
103 AVFrame *out = s->frames[s->nb_frames - 1];
104 out->pts = s->pts[s->flush_idx++];
105 ret = ff_filter_frame(outlink, out);
112 static const AVFilterPad reverse_inputs[] = {
115 .type = AVMEDIA_TYPE_VIDEO,
116 .filter_frame = filter_frame,
121 static const AVFilterPad reverse_outputs[] = {
124 .type = AVMEDIA_TYPE_VIDEO,
125 .request_frame = request_frame,
130 AVFilter ff_vf_reverse = {
132 .description = NULL_IF_CONFIG_SMALL("Reverse a clip."),
133 .priv_size = sizeof(ReverseContext),
136 .inputs = reverse_inputs,
137 .outputs = reverse_outputs,
140 #endif /* CONFIG_REVERSE_FILTER */
142 #if CONFIG_AREVERSE_FILTER
144 static int query_formats(AVFilterContext *ctx)
146 AVFilterFormats *formats;
147 AVFilterChannelLayouts *layouts;
150 layouts = ff_all_channel_counts();
152 return AVERROR(ENOMEM);
153 ret = ff_set_common_channel_layouts(ctx, layouts);
157 ret = ff_set_common_formats(ctx, ff_all_formats(AVMEDIA_TYPE_AUDIO));
161 formats = ff_all_samplerates();
163 return AVERROR(ENOMEM);
164 return ff_set_common_samplerates(ctx, formats);
167 static void reverse_samples_planar(AVFrame *out)
169 for (int p = 0; p < out->channels; p++) {
170 switch (out->format) {
171 case AV_SAMPLE_FMT_U8P: {
172 uint8_t *dst = (uint8_t *)out->extended_data[p];
173 for (int i = 0, j = out->nb_samples - 1; i < j; i++, j--)
174 FFSWAP(uint8_t, dst[i], dst[j]);
177 case AV_SAMPLE_FMT_S16P: {
178 int16_t *dst = (int16_t *)out->extended_data[p];
179 for (int i = 0, j = out->nb_samples - 1; i < j; i++, j--)
180 FFSWAP(int16_t, dst[i], dst[j]);
183 case AV_SAMPLE_FMT_S32P: {
184 int32_t *dst = (int32_t *)out->extended_data[p];
185 for (int i = 0, j = out->nb_samples - 1; i < j; i++, j--)
186 FFSWAP(int32_t, dst[i], dst[j]);
189 case AV_SAMPLE_FMT_FLTP: {
190 float *dst = (float *)out->extended_data[p];
191 for (int i = 0, j = out->nb_samples - 1; i < j; i++, j--)
192 FFSWAP(float, dst[i], dst[j]);
195 case AV_SAMPLE_FMT_DBLP: {
196 double *dst = (double *)out->extended_data[p];
197 for (int i = 0, j = out->nb_samples - 1; i < j; i++, j--)
198 FFSWAP(double, dst[i], dst[j]);
205 static void reverse_samples_packed(AVFrame *out)
207 const int channels = out->channels;
209 switch (out->format) {
210 case AV_SAMPLE_FMT_U8: {
211 uint8_t *dst = (uint8_t *)out->extended_data[0];
212 for (int i = 0, j = out->nb_samples - 1; i < j; i++, j--)
213 for (int p = 0; p < channels; p++)
214 FFSWAP(uint8_t, dst[i * channels + p], dst[j * channels + p]);
217 case AV_SAMPLE_FMT_S16: {
218 int16_t *dst = (int16_t *)out->extended_data[0];
219 for (int i = 0, j = out->nb_samples - 1; i < j; i++, j--)
220 for (int p = 0; p < channels; p++)
221 FFSWAP(int16_t, dst[i * channels + p], dst[j * channels + p]);
224 case AV_SAMPLE_FMT_S32: {
225 int32_t *dst = (int32_t *)out->extended_data[0];
226 for (int i = 0, j = out->nb_samples - 1; i < j; i++, j--)
227 for (int p = 0; p < channels; p++)
228 FFSWAP(int32_t, dst[i * channels + p], dst[j * channels + p]);
231 case AV_SAMPLE_FMT_FLT: {
232 float *dst = (float *)out->extended_data[0];
233 for (int i = 0, j = out->nb_samples - 1; i < j; i++, j--)
234 for (int p = 0; p < channels; p++)
235 FFSWAP(float, dst[i * channels + p], dst[j * channels + p]);
238 case AV_SAMPLE_FMT_DBL: {
239 double *dst = (double *)out->extended_data[0];
240 for (int i = 0, j = out->nb_samples - 1; i < j; i++, j--)
241 for (int p = 0; p < channels; p++)
242 FFSWAP(double, dst[i * channels + p], dst[j * channels + p]);
248 static int areverse_request_frame(AVFilterLink *outlink)
250 AVFilterContext *ctx = outlink->src;
251 ReverseContext *s = ctx->priv;
254 ret = ff_request_frame(ctx->inputs[0]);
256 if (ret == AVERROR_EOF && s->nb_frames > 0) {
257 AVFrame *out = s->frames[s->nb_frames - 1];
258 out->pts = s->pts[s->flush_idx++];
260 if (av_sample_fmt_is_planar(out->format))
261 reverse_samples_planar(out);
263 reverse_samples_packed(out);
264 ret = ff_filter_frame(outlink, out);
271 static const AVFilterPad areverse_inputs[] = {
274 .type = AVMEDIA_TYPE_AUDIO,
275 .filter_frame = filter_frame,
281 static const AVFilterPad areverse_outputs[] = {
284 .type = AVMEDIA_TYPE_AUDIO,
285 .request_frame = areverse_request_frame,
290 AVFilter ff_af_areverse = {
292 .description = NULL_IF_CONFIG_SMALL("Reverse an audio clip."),
293 .query_formats = query_formats,
294 .priv_size = sizeof(ReverseContext),
297 .inputs = areverse_inputs,
298 .outputs = areverse_outputs,
301 #endif /* CONFIG_AREVERSE_FILTER */