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