FFmpeg
f_reverse.c
Go to the documentation of this file.
1 /*
2  * Copyright (c) 2015 Derek Buitenhuis
3  *
4  * This file is part of FFmpeg.
5  *
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.
10  *
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.
15  *
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
19  */
20 
21 #include "config_components.h"
22 
23 #include "libavutil/opt.h"
24 #include "avfilter.h"
25 #include "formats.h"
26 #include "internal.h"
27 #include "video.h"
28 
29 #define DEFAULT_LENGTH 300
30 
31 typedef struct ReverseContext {
32  int nb_frames;
34  unsigned int frames_size;
35  unsigned int pts_size;
36  unsigned int duration_size;
37  int64_t *pts;
38  int64_t *duration;
39  int flush_idx;
40  int64_t nb_samples;
42 
44 {
45  ReverseContext *s = ctx->priv;
46 
47  s->pts = av_fast_realloc(NULL, &s->pts_size,
48  DEFAULT_LENGTH * sizeof(*(s->pts)));
49  if (!s->pts)
50  return AVERROR(ENOMEM);
51 
52  s->duration = av_fast_realloc(NULL, &s->duration_size,
53  DEFAULT_LENGTH * sizeof(*(s->duration)));
54  if (!s->duration)
55  return AVERROR(ENOMEM);
56 
57  s->frames = av_fast_realloc(NULL, &s->frames_size,
58  DEFAULT_LENGTH * sizeof(*(s->frames)));
59  if (!s->frames)
60  return AVERROR(ENOMEM);
61 
62  return 0;
63 }
64 
66 {
67  ReverseContext *s = ctx->priv;
68 
69  while (s->nb_frames > 0) {
70  av_frame_free(&s->frames[s->nb_frames - 1]);
71  s->nb_frames--;
72  }
73 
74  av_freep(&s->pts);
75  av_freep(&s->duration);
76  av_freep(&s->frames);
77 }
78 
80 {
81  AVFilterContext *ctx = inlink->dst;
82  ReverseContext *s = ctx->priv;
83  void *ptr;
84 
85  if (s->nb_frames + 1 > s->pts_size / sizeof(*(s->pts))) {
86  ptr = av_fast_realloc(s->pts, &s->pts_size, s->pts_size * 2);
87  if (!ptr)
88  return AVERROR(ENOMEM);
89  s->pts = ptr;
90  }
91 
92  if (s->nb_frames + 1 > s->duration_size / sizeof(*(s->duration))) {
93  ptr = av_fast_realloc(s->duration, &s->duration_size, s->duration_size * 2);
94  if (!ptr)
95  return AVERROR(ENOMEM);
96  s->duration = ptr;
97  }
98 
99  if (s->nb_frames + 1 > s->frames_size / sizeof(*(s->frames))) {
100  ptr = av_fast_realloc(s->frames, &s->frames_size, s->frames_size * 2);
101  if (!ptr)
102  return AVERROR(ENOMEM);
103  s->frames = ptr;
104  }
105 
106  s->frames[s->nb_frames] = in;
107  s->pts[s->nb_frames] = in->pts;
108  s->duration[s->nb_frames] = in->duration;
109  s->nb_frames++;
110 
111  return 0;
112 }
113 
114 #if CONFIG_REVERSE_FILTER
115 
116 static int request_frame(AVFilterLink *outlink)
117 {
118  AVFilterContext *ctx = outlink->src;
119  ReverseContext *s = ctx->priv;
120  int ret;
121 
122  ret = ff_request_frame(ctx->inputs[0]);
123 
124  if (ret == AVERROR_EOF && s->nb_frames > 0) {
125  AVFrame *out = s->frames[s->nb_frames - 1];
126  out->duration= s->duration[s->flush_idx];
127  out->pts = s->pts[s->flush_idx++];
128  ret = ff_filter_frame(outlink, out);
129  s->frames[s->nb_frames - 1] = NULL;
130  s->nb_frames--;
131  }
132 
133  return ret;
134 }
135 
136 static const AVFilterPad reverse_inputs[] = {
137  {
138  .name = "default",
139  .type = AVMEDIA_TYPE_VIDEO,
140  .filter_frame = filter_frame,
141  },
142 };
143 
144 static const AVFilterPad reverse_outputs[] = {
145  {
146  .name = "default",
147  .type = AVMEDIA_TYPE_VIDEO,
148  .request_frame = request_frame,
149  },
150 };
151 
152 const AVFilter ff_vf_reverse = {
153  .name = "reverse",
154  .description = NULL_IF_CONFIG_SMALL("Reverse a clip."),
155  .priv_size = sizeof(ReverseContext),
156  .init = init,
157  .uninit = uninit,
158  FILTER_INPUTS(reverse_inputs),
159  FILTER_OUTPUTS(reverse_outputs),
160 };
161 
162 #endif /* CONFIG_REVERSE_FILTER */
163 
164 #if CONFIG_AREVERSE_FILTER
165 
166 static void reverse_samples_planar(AVFrame *out)
167 {
168  for (int p = 0; p < out->ch_layout.nb_channels; p++) {
169  switch (out->format) {
170  case AV_SAMPLE_FMT_U8P: {
171  uint8_t *dst = (uint8_t *)out->extended_data[p];
172  for (int i = 0, j = out->nb_samples - 1; i < j; i++, j--)
173  FFSWAP(uint8_t, dst[i], dst[j]);
174  }
175  break;
176  case AV_SAMPLE_FMT_S16P: {
177  int16_t *dst = (int16_t *)out->extended_data[p];
178  for (int i = 0, j = out->nb_samples - 1; i < j; i++, j--)
179  FFSWAP(int16_t, dst[i], dst[j]);
180  }
181  break;
182  case AV_SAMPLE_FMT_S32P: {
183  int32_t *dst = (int32_t *)out->extended_data[p];
184  for (int i = 0, j = out->nb_samples - 1; i < j; i++, j--)
185  FFSWAP(int32_t, dst[i], dst[j]);
186  }
187  break;
188  case AV_SAMPLE_FMT_S64P: {
189  int64_t *dst = (int64_t *)out->extended_data[p];
190  for (int i = 0, j = out->nb_samples - 1; i < j; i++, j--)
191  FFSWAP(int64_t, dst[i], dst[j]);
192  }
193  break;
194  case AV_SAMPLE_FMT_FLTP: {
195  float *dst = (float *)out->extended_data[p];
196  for (int i = 0, j = out->nb_samples - 1; i < j; i++, j--)
197  FFSWAP(float, dst[i], dst[j]);
198  }
199  break;
200  case AV_SAMPLE_FMT_DBLP: {
201  double *dst = (double *)out->extended_data[p];
202  for (int i = 0, j = out->nb_samples - 1; i < j; i++, j--)
203  FFSWAP(double, dst[i], dst[j]);
204  }
205  break;
206  }
207  }
208 }
209 
210 static void reverse_samples_packed(AVFrame *out)
211 {
212  const int channels = out->ch_layout.nb_channels;
213 
214  switch (out->format) {
215  case AV_SAMPLE_FMT_U8: {
216  uint8_t *dst = (uint8_t *)out->extended_data[0];
217  for (int i = 0, j = out->nb_samples - 1; i < j; i++, j--)
218  for (int p = 0; p < channels; p++)
219  FFSWAP(uint8_t, dst[i * channels + p], dst[j * channels + p]);
220  }
221  break;
222  case AV_SAMPLE_FMT_S16: {
223  int16_t *dst = (int16_t *)out->extended_data[0];
224  for (int i = 0, j = out->nb_samples - 1; i < j; i++, j--)
225  for (int p = 0; p < channels; p++)
226  FFSWAP(int16_t, dst[i * channels + p], dst[j * channels + p]);
227  }
228  break;
229  case AV_SAMPLE_FMT_S32: {
230  int32_t *dst = (int32_t *)out->extended_data[0];
231  for (int i = 0, j = out->nb_samples - 1; i < j; i++, j--)
232  for (int p = 0; p < channels; p++)
233  FFSWAP(int32_t, dst[i * channels + p], dst[j * channels + p]);
234  }
235  break;
236  case AV_SAMPLE_FMT_S64: {
237  int64_t *dst = (int64_t *)out->extended_data[0];
238  for (int i = 0, j = out->nb_samples - 1; i < j; i++, j--)
239  for (int p = 0; p < channels; p++)
240  FFSWAP(int64_t, dst[i * channels + p], dst[j * channels + p]);
241  }
242  break;
243  case AV_SAMPLE_FMT_FLT: {
244  float *dst = (float *)out->extended_data[0];
245  for (int i = 0, j = out->nb_samples - 1; i < j; i++, j--)
246  for (int p = 0; p < channels; p++)
247  FFSWAP(float, dst[i * channels + p], dst[j * channels + p]);
248  }
249  break;
250  case AV_SAMPLE_FMT_DBL: {
251  double *dst = (double *)out->extended_data[0];
252  for (int i = 0, j = out->nb_samples - 1; i < j; i++, j--)
253  for (int p = 0; p < channels; p++)
254  FFSWAP(double, dst[i * channels + p], dst[j * channels + p]);
255  }
256  break;
257  }
258 }
259 
260 static int areverse_request_frame(AVFilterLink *outlink)
261 {
262  AVFilterContext *ctx = outlink->src;
263  ReverseContext *s = ctx->priv;
264  int ret;
265 
266  ret = ff_request_frame(ctx->inputs[0]);
267 
268  if (ret == AVERROR_EOF && s->nb_frames > 0) {
269  AVFrame *out = s->frames[s->nb_frames - 1];
270  out->duration = s->duration[s->flush_idx];
271  out->pts = s->pts[s->flush_idx++] - s->nb_samples;
272  s->nb_samples += s->pts[s->flush_idx] - s->pts[s->flush_idx - 1] - out->nb_samples;
273 
274  if (av_sample_fmt_is_planar(out->format))
275  reverse_samples_planar(out);
276  else
277  reverse_samples_packed(out);
278  ret = ff_filter_frame(outlink, out);
279  s->frames[s->nb_frames - 1] = NULL;
280  s->nb_frames--;
281  }
282 
283  return ret;
284 }
285 
286 static const AVFilterPad areverse_inputs[] = {
287  {
288  .name = "default",
289  .type = AVMEDIA_TYPE_AUDIO,
291  .filter_frame = filter_frame,
292  },
293 };
294 
295 static const AVFilterPad areverse_outputs[] = {
296  {
297  .name = "default",
298  .type = AVMEDIA_TYPE_AUDIO,
299  .request_frame = areverse_request_frame,
300  },
301 };
302 
303 const AVFilter ff_af_areverse = {
304  .name = "areverse",
305  .description = NULL_IF_CONFIG_SMALL("Reverse an audio clip."),
306  .priv_size = sizeof(ReverseContext),
307  .init = init,
308  .uninit = uninit,
309  FILTER_INPUTS(areverse_inputs),
310  FILTER_OUTPUTS(areverse_outputs),
311 };
312 
313 #endif /* CONFIG_AREVERSE_FILTER */
ReverseContext::nb_frames
int nb_frames
Definition: f_reverse.c:32
AV_SAMPLE_FMT_FLTP
@ AV_SAMPLE_FMT_FLTP
float, planar
Definition: samplefmt.h:66
AVERROR
Filter the word “frame” indicates either a video frame or a group of audio as stored in an AVFrame structure Format for each input and each output the list of supported formats For video that means pixel format For audio that means channel sample they are references to shared objects When the negotiation mechanism computes the intersection of the formats supported at each end of a all references to both lists are replaced with a reference to the intersection And when a single format is eventually chosen for a link amongst the remaining all references to the list are updated That means that if a filter requires that its input and output have the same format amongst a supported all it has to do is use a reference to the same list of formats query_formats can leave some formats unset and return AVERROR(EAGAIN) to cause the negotiation mechanism toagain later. That can be used by filters with complex requirements to use the format negotiated on one link to set the formats supported on another. Frame references ownership and permissions
opt.h
out
FILE * out
Definition: movenc.c:54
ff_filter_frame
int ff_filter_frame(AVFilterLink *link, AVFrame *frame)
Send a frame of data to the next filter.
Definition: avfilter.c:969
AVFrame::duration
int64_t duration
Duration of the frame, in the same units as pts.
Definition: frame.h:728
AVERROR_EOF
#define AVERROR_EOF
End of file.
Definition: error.h:57
inlink
The exact code depends on how similar the blocks are and how related they are to the and needs to apply these operations to the correct inlink or outlink if there are several Macros are available to factor that when no extra processing is inlink
Definition: filter_design.txt:212
av_frame_free
void av_frame_free(AVFrame **frame)
Free the frame and any dynamically allocated objects in it, e.g.
Definition: frame.c:99
AVFrame
This structure describes decoded (raw) audio or video data.
Definition: frame.h:330
AVFrame::pts
int64_t pts
Presentation timestamp in time_base units (time when frame should be shown to user).
Definition: frame.h:437
AV_SAMPLE_FMT_S32P
@ AV_SAMPLE_FMT_S32P
signed 32 bits, planar
Definition: samplefmt.h:65
ff_request_frame
int ff_request_frame(AVFilterLink *link)
Request an input frame from the filter at the other end of the link.
Definition: avfilter.c:415
AVFilter::name
const char * name
Filter name.
Definition: avfilter.h:165
uninit
static av_cold void uninit(AVFilterContext *ctx)
Definition: f_reverse.c:65
video.h
ReverseContext::nb_samples
int64_t nb_samples
Definition: f_reverse.c:40
formats.h
filter_frame
static int filter_frame(AVFilterLink *inlink, AVFrame *in)
Definition: f_reverse.c:79
AV_SAMPLE_FMT_S64P
@ AV_SAMPLE_FMT_S64P
signed 64 bits, planar
Definition: samplefmt.h:69
ReverseContext::frames
AVFrame ** frames
Definition: f_reverse.c:33
AVFilterPad
A filter pad used for either input or output.
Definition: internal.h:49
av_cold
#define av_cold
Definition: attributes.h:90
av_fast_realloc
void * av_fast_realloc(void *ptr, unsigned int *size, size_t min_size)
Reallocate the given buffer if it is not large enough, otherwise do nothing.
Definition: mem.c:495
s
#define s(width, name)
Definition: cbs_vp9.c:256
AVMEDIA_TYPE_AUDIO
@ AVMEDIA_TYPE_AUDIO
Definition: avutil.h:202
ReverseContext
Definition: f_reverse.c:31
av_sample_fmt_is_planar
int av_sample_fmt_is_planar(enum AVSampleFormat sample_fmt)
Check if the sample format is planar.
Definition: samplefmt.c:114
init
static av_cold int init(AVFilterContext *ctx)
Definition: f_reverse.c:43
ctx
AVFormatContext * ctx
Definition: movenc.c:48
channels
channels
Definition: aptx.h:31
FILTER_INPUTS
#define FILTER_INPUTS(array)
Definition: internal.h:194
NULL
#define NULL
Definition: coverity.c:32
NULL_IF_CONFIG_SMALL
#define NULL_IF_CONFIG_SMALL(x)
Return NULL if CONFIG_SMALL is true, otherwise the argument without modification.
Definition: internal.h:115
AV_SAMPLE_FMT_U8P
@ AV_SAMPLE_FMT_U8P
unsigned 8 bits, planar
Definition: samplefmt.h:63
for
for(k=2;k<=8;++k)
Definition: h264pred_template.c:425
DEFAULT_LENGTH
#define DEFAULT_LENGTH
Definition: f_reverse.c:29
ff_af_areverse
const AVFilter ff_af_areverse
AV_SAMPLE_FMT_S16P
@ AV_SAMPLE_FMT_S16P
signed 16 bits, planar
Definition: samplefmt.h:64
internal.h
ReverseContext::frames_size
unsigned int frames_size
Definition: f_reverse.c:34
i
#define i(width, name, range_min, range_max)
Definition: cbs_h2645.c:269
AV_SAMPLE_FMT_U8
@ AV_SAMPLE_FMT_U8
unsigned 8 bits
Definition: samplefmt.h:57
AV_SAMPLE_FMT_S16
@ AV_SAMPLE_FMT_S16
signed 16 bits
Definition: samplefmt.h:58
AVFilterPad::name
const char * name
Pad name.
Definition: internal.h:55
AVFilter
Filter definition.
Definition: avfilter.h:161
ret
ret
Definition: filter_design.txt:187
FFSWAP
#define FFSWAP(type, a, b)
Definition: macros.h:52
ReverseContext::duration
int64_t * duration
Definition: f_reverse.c:38
ReverseContext::duration_size
unsigned int duration_size
Definition: f_reverse.c:36
request_frame
static int request_frame(AVFilterLink *outlink)
Definition: af_aecho.c:272
ReverseContext::pts
int64_t * pts
Definition: f_reverse.c:37
ff_vf_reverse
const AVFilter ff_vf_reverse
avfilter.h
AV_SAMPLE_FMT_DBLP
@ AV_SAMPLE_FMT_DBLP
double, planar
Definition: samplefmt.h:67
AVFilterContext
An instance of a filter.
Definition: avfilter.h:392
AVMEDIA_TYPE_VIDEO
@ AVMEDIA_TYPE_VIDEO
Definition: avutil.h:201
ReverseContext::pts_size
unsigned int pts_size
Definition: f_reverse.c:35
FILTER_OUTPUTS
#define FILTER_OUTPUTS(array)
Definition: internal.h:195
av_freep
#define av_freep(p)
Definition: tableprint_vlc.h:34
int32_t
int32_t
Definition: audioconvert.c:56
AV_SAMPLE_FMT_DBL
@ AV_SAMPLE_FMT_DBL
double
Definition: samplefmt.h:61
AV_SAMPLE_FMT_S32
@ AV_SAMPLE_FMT_S32
signed 32 bits
Definition: samplefmt.h:59
ReverseContext::flush_idx
int flush_idx
Definition: f_reverse.c:39
AV_SAMPLE_FMT_FLT
@ AV_SAMPLE_FMT_FLT
float
Definition: samplefmt.h:60
AV_SAMPLE_FMT_S64
@ AV_SAMPLE_FMT_S64
signed 64 bits
Definition: samplefmt.h:68
AVFILTERPAD_FLAG_NEEDS_WRITABLE
#define AVFILTERPAD_FLAG_NEEDS_WRITABLE
The filter expects writable frames from its input link, duplicating data buffers if needed.
Definition: internal.h:68