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