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  int64_t *pts;
37  int flush_idx;
38  int64_t nb_samples;
40 
42 {
43  ReverseContext *s = ctx->priv;
44 
45  s->pts = av_fast_realloc(NULL, &s->pts_size,
46  DEFAULT_LENGTH * sizeof(*(s->pts)));
47  if (!s->pts)
48  return AVERROR(ENOMEM);
49 
50  s->frames = av_fast_realloc(NULL, &s->frames_size,
51  DEFAULT_LENGTH * sizeof(*(s->frames)));
52  if (!s->frames) {
53  av_freep(&s->pts);
54  return AVERROR(ENOMEM);
55  }
56 
57  return 0;
58 }
59 
61 {
62  ReverseContext *s = ctx->priv;
63 
64  while (s->nb_frames > 0) {
65  av_frame_free(&s->frames[s->nb_frames - 1]);
66  s->nb_frames--;
67  }
68 
69  av_freep(&s->pts);
70  av_freep(&s->frames);
71 }
72 
74 {
75  AVFilterContext *ctx = inlink->dst;
76  ReverseContext *s = ctx->priv;
77  void *ptr;
78 
79  if (s->nb_frames + 1 > s->pts_size / sizeof(*(s->pts))) {
80  ptr = av_fast_realloc(s->pts, &s->pts_size, s->pts_size * 2);
81  if (!ptr)
82  return AVERROR(ENOMEM);
83  s->pts = ptr;
84  }
85 
86  if (s->nb_frames + 1 > s->frames_size / sizeof(*(s->frames))) {
87  ptr = av_fast_realloc(s->frames, &s->frames_size, s->frames_size * 2);
88  if (!ptr)
89  return AVERROR(ENOMEM);
90  s->frames = ptr;
91  }
92 
93  s->frames[s->nb_frames] = in;
94  s->pts[s->nb_frames] = in->pts;
95  s->nb_frames++;
96 
97  return 0;
98 }
99 
100 #if CONFIG_REVERSE_FILTER
101 
102 static int request_frame(AVFilterLink *outlink)
103 {
104  AVFilterContext *ctx = outlink->src;
105  ReverseContext *s = ctx->priv;
106  int ret;
107 
108  ret = ff_request_frame(ctx->inputs[0]);
109 
110  if (ret == AVERROR_EOF && s->nb_frames > 0) {
111  AVFrame *out = s->frames[s->nb_frames - 1];
112  out->pts = s->pts[s->flush_idx++];
113  ret = ff_filter_frame(outlink, out);
114  s->frames[s->nb_frames - 1] = NULL;
115  s->nb_frames--;
116  }
117 
118  return ret;
119 }
120 
121 static const AVFilterPad reverse_inputs[] = {
122  {
123  .name = "default",
124  .type = AVMEDIA_TYPE_VIDEO,
125  .filter_frame = filter_frame,
126  },
127 };
128 
129 static const AVFilterPad reverse_outputs[] = {
130  {
131  .name = "default",
132  .type = AVMEDIA_TYPE_VIDEO,
133  .request_frame = request_frame,
134  },
135 };
136 
137 const AVFilter ff_vf_reverse = {
138  .name = "reverse",
139  .description = NULL_IF_CONFIG_SMALL("Reverse a clip."),
140  .priv_size = sizeof(ReverseContext),
141  .init = init,
142  .uninit = uninit,
143  FILTER_INPUTS(reverse_inputs),
144  FILTER_OUTPUTS(reverse_outputs),
145 };
146 
147 #endif /* CONFIG_REVERSE_FILTER */
148 
149 #if CONFIG_AREVERSE_FILTER
150 
151 static void reverse_samples_planar(AVFrame *out)
152 {
153  for (int p = 0; p < out->ch_layout.nb_channels; p++) {
154  switch (out->format) {
155  case AV_SAMPLE_FMT_U8P: {
156  uint8_t *dst = (uint8_t *)out->extended_data[p];
157  for (int i = 0, j = out->nb_samples - 1; i < j; i++, j--)
158  FFSWAP(uint8_t, dst[i], dst[j]);
159  }
160  break;
161  case AV_SAMPLE_FMT_S16P: {
162  int16_t *dst = (int16_t *)out->extended_data[p];
163  for (int i = 0, j = out->nb_samples - 1; i < j; i++, j--)
164  FFSWAP(int16_t, dst[i], dst[j]);
165  }
166  break;
167  case AV_SAMPLE_FMT_S32P: {
168  int32_t *dst = (int32_t *)out->extended_data[p];
169  for (int i = 0, j = out->nb_samples - 1; i < j; i++, j--)
170  FFSWAP(int32_t, dst[i], dst[j]);
171  }
172  break;
173  case AV_SAMPLE_FMT_S64P: {
174  int64_t *dst = (int64_t *)out->extended_data[p];
175  for (int i = 0, j = out->nb_samples - 1; i < j; i++, j--)
176  FFSWAP(int64_t, dst[i], dst[j]);
177  }
178  break;
179  case AV_SAMPLE_FMT_FLTP: {
180  float *dst = (float *)out->extended_data[p];
181  for (int i = 0, j = out->nb_samples - 1; i < j; i++, j--)
182  FFSWAP(float, dst[i], dst[j]);
183  }
184  break;
185  case AV_SAMPLE_FMT_DBLP: {
186  double *dst = (double *)out->extended_data[p];
187  for (int i = 0, j = out->nb_samples - 1; i < j; i++, j--)
188  FFSWAP(double, dst[i], dst[j]);
189  }
190  break;
191  }
192  }
193 }
194 
195 static void reverse_samples_packed(AVFrame *out)
196 {
197  const int channels = out->ch_layout.nb_channels;
198 
199  switch (out->format) {
200  case AV_SAMPLE_FMT_U8: {
201  uint8_t *dst = (uint8_t *)out->extended_data[0];
202  for (int i = 0, j = out->nb_samples - 1; i < j; i++, j--)
203  for (int p = 0; p < channels; p++)
204  FFSWAP(uint8_t, dst[i * channels + p], dst[j * channels + p]);
205  }
206  break;
207  case AV_SAMPLE_FMT_S16: {
208  int16_t *dst = (int16_t *)out->extended_data[0];
209  for (int i = 0, j = out->nb_samples - 1; i < j; i++, j--)
210  for (int p = 0; p < channels; p++)
211  FFSWAP(int16_t, dst[i * channels + p], dst[j * channels + p]);
212  }
213  break;
214  case AV_SAMPLE_FMT_S32: {
215  int32_t *dst = (int32_t *)out->extended_data[0];
216  for (int i = 0, j = out->nb_samples - 1; i < j; i++, j--)
217  for (int p = 0; p < channels; p++)
218  FFSWAP(int32_t, dst[i * channels + p], dst[j * channels + p]);
219  }
220  break;
221  case AV_SAMPLE_FMT_S64: {
222  int64_t *dst = (int64_t *)out->extended_data[0];
223  for (int i = 0, j = out->nb_samples - 1; i < j; i++, j--)
224  for (int p = 0; p < channels; p++)
225  FFSWAP(int64_t, dst[i * channels + p], dst[j * channels + p]);
226  }
227  break;
228  case AV_SAMPLE_FMT_FLT: {
229  float *dst = (float *)out->extended_data[0];
230  for (int i = 0, j = out->nb_samples - 1; i < j; i++, j--)
231  for (int p = 0; p < channels; p++)
232  FFSWAP(float, dst[i * channels + p], dst[j * channels + p]);
233  }
234  break;
235  case AV_SAMPLE_FMT_DBL: {
236  double *dst = (double *)out->extended_data[0];
237  for (int i = 0, j = out->nb_samples - 1; i < j; i++, j--)
238  for (int p = 0; p < channels; p++)
239  FFSWAP(double, dst[i * channels + p], dst[j * channels + p]);
240  }
241  break;
242  }
243 }
244 
245 static int areverse_request_frame(AVFilterLink *outlink)
246 {
247  AVFilterContext *ctx = outlink->src;
248  ReverseContext *s = ctx->priv;
249  int ret;
250 
251  ret = ff_request_frame(ctx->inputs[0]);
252 
253  if (ret == AVERROR_EOF && s->nb_frames > 0) {
254  AVFrame *out = s->frames[s->nb_frames - 1];
255  out->pts = s->pts[s->flush_idx++] - s->nb_samples;
256  s->nb_samples += s->pts[s->flush_idx] - s->pts[s->flush_idx - 1] - out->nb_samples;
257 
258  if (av_sample_fmt_is_planar(out->format))
259  reverse_samples_planar(out);
260  else
261  reverse_samples_packed(out);
262  ret = ff_filter_frame(outlink, out);
263  s->frames[s->nb_frames - 1] = NULL;
264  s->nb_frames--;
265  }
266 
267  return ret;
268 }
269 
270 static const AVFilterPad areverse_inputs[] = {
271  {
272  .name = "default",
273  .type = AVMEDIA_TYPE_AUDIO,
275  .filter_frame = filter_frame,
276  },
277 };
278 
279 static const AVFilterPad areverse_outputs[] = {
280  {
281  .name = "default",
282  .type = AVMEDIA_TYPE_AUDIO,
283  .request_frame = areverse_request_frame,
284  },
285 };
286 
287 const AVFilter ff_af_areverse = {
288  .name = "areverse",
289  .description = NULL_IF_CONFIG_SMALL("Reverse an audio clip."),
290  .priv_size = sizeof(ReverseContext),
291  .init = init,
292  .uninit = uninit,
293  FILTER_INPUTS(areverse_inputs),
294  FILTER_OUTPUTS(areverse_outputs),
295 };
296 
297 #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:999
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:111
AVFrame
This structure describes decoded (raw) audio or video data.
Definition: frame.h:325
AVFrame::pts
int64_t pts
Presentation timestamp in time_base units (time when frame should be shown to user).
Definition: frame.h:432
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:400
AVFilter::name
const char * name
Filter name.
Definition: avfilter.h:175
uninit
static av_cold void uninit(AVFilterContext *ctx)
Definition: f_reverse.c:60
video.h
ReverseContext::nb_samples
int64_t nb_samples
Definition: f_reverse.c:38
formats.h
filter_frame
static int filter_frame(AVFilterLink *inlink, AVFrame *in)
Definition: f_reverse.c:73
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:505
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:41
ctx
AVFormatContext * ctx
Definition: movenc.c:48
channels
channels
Definition: aptx.h:32
FILTER_INPUTS
#define FILTER_INPUTS(array)
Definition: internal.h:190
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:117
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:171
ret
ret
Definition: filter_design.txt:187
FFSWAP
#define FFSWAP(type, a, b)
Definition: macros.h:52
request_frame
static int request_frame(AVFilterLink *outlink)
Definition: af_aecho.c:272
ReverseContext::pts
int64_t * pts
Definition: f_reverse.c:36
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:408
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:191
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:37
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