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  { NULL }
125 };
126 
127 static const AVFilterPad reverse_outputs[] = {
128  {
129  .name = "default",
130  .type = AVMEDIA_TYPE_VIDEO,
131  .request_frame = request_frame,
132  },
133  { NULL }
134 };
135 
137  .name = "reverse",
138  .description = NULL_IF_CONFIG_SMALL("Reverse a clip."),
139  .priv_size = sizeof(ReverseContext),
140  .init = init,
141  .uninit = uninit,
142  .inputs = reverse_inputs,
143  .outputs = reverse_outputs,
144 };
145 
146 #endif /* CONFIG_REVERSE_FILTER */
147 
148 #if CONFIG_AREVERSE_FILTER
149 
150 static int query_formats(AVFilterContext *ctx)
151 {
154  int ret;
155 
157  if (!layouts)
158  return AVERROR(ENOMEM);
160  if (ret < 0)
161  return ret;
162 
164  if (ret < 0)
165  return ret;
166 
168  if (!formats)
169  return AVERROR(ENOMEM);
171 }
172 
173 static void reverse_samples_planar(AVFrame *out)
174 {
175  for (int p = 0; p < out->channels; p++) {
176  switch (out->format) {
177  case AV_SAMPLE_FMT_U8P: {
178  uint8_t *dst = (uint8_t *)out->extended_data[p];
179  for (int i = 0, j = out->nb_samples - 1; i < j; i++, j--)
180  FFSWAP(uint8_t, dst[i], dst[j]);
181  }
182  break;
183  case AV_SAMPLE_FMT_S16P: {
184  int16_t *dst = (int16_t *)out->extended_data[p];
185  for (int i = 0, j = out->nb_samples - 1; i < j; i++, j--)
186  FFSWAP(int16_t, dst[i], dst[j]);
187  }
188  break;
189  case AV_SAMPLE_FMT_S32P: {
190  int32_t *dst = (int32_t *)out->extended_data[p];
191  for (int i = 0, j = out->nb_samples - 1; i < j; i++, j--)
192  FFSWAP(int32_t, dst[i], dst[j]);
193  }
194  break;
195  case AV_SAMPLE_FMT_FLTP: {
196  float *dst = (float *)out->extended_data[p];
197  for (int i = 0, j = out->nb_samples - 1; i < j; i++, j--)
198  FFSWAP(float, dst[i], dst[j]);
199  }
200  break;
201  case AV_SAMPLE_FMT_DBLP: {
202  double *dst = (double *)out->extended_data[p];
203  for (int i = 0, j = out->nb_samples - 1; i < j; i++, j--)
204  FFSWAP(double, dst[i], dst[j]);
205  }
206  break;
207  }
208  }
209 }
210 
211 static void reverse_samples_packed(AVFrame *out)
212 {
213  const int channels = out->channels;
214 
215  switch (out->format) {
216  case AV_SAMPLE_FMT_U8: {
217  uint8_t *dst = (uint8_t *)out->extended_data[0];
218  for (int i = 0, j = out->nb_samples - 1; i < j; i++, j--)
219  for (int p = 0; p < channels; p++)
220  FFSWAP(uint8_t, dst[i * channels + p], dst[j * channels + p]);
221  }
222  break;
223  case AV_SAMPLE_FMT_S16: {
224  int16_t *dst = (int16_t *)out->extended_data[0];
225  for (int i = 0, j = out->nb_samples - 1; i < j; i++, j--)
226  for (int p = 0; p < channels; p++)
227  FFSWAP(int16_t, dst[i * channels + p], dst[j * channels + p]);
228  }
229  break;
230  case AV_SAMPLE_FMT_S32: {
231  int32_t *dst = (int32_t *)out->extended_data[0];
232  for (int i = 0, j = out->nb_samples - 1; i < j; i++, j--)
233  for (int p = 0; p < channels; p++)
234  FFSWAP(int32_t, dst[i * channels + p], dst[j * channels + p]);
235  }
236  break;
237  case AV_SAMPLE_FMT_FLT: {
238  float *dst = (float *)out->extended_data[0];
239  for (int i = 0, j = out->nb_samples - 1; i < j; i++, j--)
240  for (int p = 0; p < channels; p++)
241  FFSWAP(float, dst[i * channels + p], dst[j * channels + p]);
242  }
243  break;
244  case AV_SAMPLE_FMT_DBL: {
245  double *dst = (double *)out->extended_data[0];
246  for (int i = 0, j = out->nb_samples - 1; i < j; i++, j--)
247  for (int p = 0; p < channels; p++)
248  FFSWAP(double, dst[i * channels + p], dst[j * channels + p]);
249  }
250  break;
251  }
252 }
253 
254 static int areverse_request_frame(AVFilterLink *outlink)
255 {
256  AVFilterContext *ctx = outlink->src;
257  ReverseContext *s = ctx->priv;
258  int ret;
259 
260  ret = ff_request_frame(ctx->inputs[0]);
261 
262  if (ret == AVERROR_EOF && s->nb_frames > 0) {
263  AVFrame *out = s->frames[s->nb_frames - 1];
264  out->pts = s->pts[s->flush_idx++];
265 
266  if (av_sample_fmt_is_planar(out->format))
267  reverse_samples_planar(out);
268  else
269  reverse_samples_packed(out);
270  ret = ff_filter_frame(outlink, out);
271  s->frames[s->nb_frames - 1] = NULL;
272  s->nb_frames--;
273  }
274 
275  return ret;
276 }
277 
278 static const AVFilterPad areverse_inputs[] = {
279  {
280  .name = "default",
281  .type = AVMEDIA_TYPE_AUDIO,
282  .filter_frame = filter_frame,
283  .needs_writable = 1,
284  },
285  { NULL }
286 };
287 
288 static const AVFilterPad areverse_outputs[] = {
289  {
290  .name = "default",
291  .type = AVMEDIA_TYPE_AUDIO,
292  .request_frame = areverse_request_frame,
293  },
294  { NULL }
295 };
296 
298  .name = "areverse",
299  .description = NULL_IF_CONFIG_SMALL("Reverse an audio clip."),
300  .query_formats = query_formats,
301  .priv_size = sizeof(ReverseContext),
302  .init = init,
303  .uninit = uninit,
304  .inputs = areverse_inputs,
305  .outputs = areverse_outputs,
306 };
307 
308 #endif /* CONFIG_AREVERSE_FILTER */
ReverseContext::nb_frames
int nb_frames
Definition: f_reverse.c:30
formats
formats
Definition: signature.h:48
AV_SAMPLE_FMT_FLTP
@ AV_SAMPLE_FMT_FLTP
float, planar
Definition: samplefmt.h:69
AVFilterChannelLayouts
A list of supported channel layouts.
Definition: formats.h:86
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
FFSWAP
#define FFSWAP(type, a, b)
Definition: common.h:108
ff_filter_frame
int ff_filter_frame(AVFilterLink *link, AVFrame *frame)
Send a frame of data to the next filter.
Definition: avfilter.c:1096
layouts
enum MovChannelLayoutTag * layouts
Definition: mov_chan.c:434
AVERROR_EOF
#define AVERROR_EOF
End of file.
Definition: error.h:55
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
ff_af_areverse
AVFilter ff_af_areverse
av_frame_free
void av_frame_free(AVFrame **frame)
Free the frame and any dynamically allocated objects in it, e.g.
Definition: frame.c:203
ff_all_channel_counts
AVFilterChannelLayouts * ff_all_channel_counts(void)
Construct an AVFilterChannelLayouts coding for any channel layout, with known or unknown disposition.
Definition: formats.c:436
AVFrame
This structure describes decoded (raw) audio or video data.
Definition: frame.h:318
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:408
AVFilter::name
const char * name
Filter name.
Definition: avfilter.h:149
uninit
static av_cold void uninit(AVFilterContext *ctx)
Definition: f_reverse.c:57
video.h
AVFilterFormats
A list of supported formats for one end of a filter link.
Definition: formats.h:65
formats.h
filter_frame
static int filter_frame(AVFilterLink *inlink, AVFrame *in)
Definition: f_reverse.c:70
ReverseContext::frames
AVFrame ** frames
Definition: f_reverse.c:31
ff_all_formats
AVFilterFormats * ff_all_formats(enum AVMediaType type)
Return a list of all formats supported by FFmpeg for the given media type.
Definition: formats.c:345
AVFilterPad
A filter pad used for either input or output.
Definition: internal.h:54
av_cold
#define av_cold
Definition: attributes.h:90
ff_set_common_formats
int ff_set_common_formats(AVFilterContext *ctx, AVFilterFormats *formats)
A helper for query_formats() which sets all links to the same list of formats.
Definition: formats.c:587
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:478
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
outputs
static const AVFilterPad outputs[]
Definition: af_acontrast.c:203
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
int32_t
int32_t
Definition: audio_convert.c:194
NULL
#define NULL
Definition: coverity.c:32
inputs
these buffered frames must be flushed immediately if a new input produces new the filter must not call request_frame to get more It must just process the frame or queue it The task of requesting more frames is left to the filter s request_frame method or the application If a filter has several inputs
Definition: filter_design.txt:243
AV_SAMPLE_FMT_U8
AV_SAMPLE_FMT_U8
Definition: audio_convert.c:194
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
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
in
uint8_t pi<< 24) CONV_FUNC_GROUP(AV_SAMPLE_FMT_FLT, float, AV_SAMPLE_FMT_U8, uint8_t,(*(const uint8_t *) pi - 0x80) *(1.0f/(1<< 7))) CONV_FUNC_GROUP(AV_SAMPLE_FMT_DBL, double, AV_SAMPLE_FMT_U8, uint8_t,(*(const uint8_t *) pi - 0x80) *(1.0/(1<< 7))) CONV_FUNC_GROUP(AV_SAMPLE_FMT_U8, uint8_t, AV_SAMPLE_FMT_S16, int16_t,(*(const int16_t *) pi >> 8)+0x80) CONV_FUNC_GROUP(AV_SAMPLE_FMT_FLT, float, AV_SAMPLE_FMT_S16, int16_t, *(const int16_t *) pi *(1.0f/(1<< 15))) CONV_FUNC_GROUP(AV_SAMPLE_FMT_DBL, double, AV_SAMPLE_FMT_S16, int16_t, *(const int16_t *) pi *(1.0/(1<< 15))) CONV_FUNC_GROUP(AV_SAMPLE_FMT_U8, uint8_t, AV_SAMPLE_FMT_S32, int32_t,(*(const int32_t *) pi >> 24)+0x80) CONV_FUNC_GROUP(AV_SAMPLE_FMT_FLT, float, AV_SAMPLE_FMT_S32, int32_t, *(const int32_t *) pi *(1.0f/(1U<< 31))) CONV_FUNC_GROUP(AV_SAMPLE_FMT_DBL, double, AV_SAMPLE_FMT_S32, int32_t, *(const int32_t *) pi *(1.0/(1U<< 31))) CONV_FUNC_GROUP(AV_SAMPLE_FMT_U8, uint8_t, AV_SAMPLE_FMT_FLT, float, av_clip_uint8(lrintf(*(const float *) pi *(1<< 7))+0x80)) CONV_FUNC_GROUP(AV_SAMPLE_FMT_S16, int16_t, AV_SAMPLE_FMT_FLT, float, av_clip_int16(lrintf(*(const float *) pi *(1<< 15)))) CONV_FUNC_GROUP(AV_SAMPLE_FMT_S32, int32_t, AV_SAMPLE_FMT_FLT, float, av_clipl_int32(llrintf(*(const float *) pi *(1U<< 31)))) CONV_FUNC_GROUP(AV_SAMPLE_FMT_U8, uint8_t, AV_SAMPLE_FMT_DBL, double, av_clip_uint8(lrint(*(const double *) pi *(1<< 7))+0x80)) CONV_FUNC_GROUP(AV_SAMPLE_FMT_S16, int16_t, AV_SAMPLE_FMT_DBL, double, av_clip_int16(lrint(*(const double *) pi *(1<< 15)))) CONV_FUNC_GROUP(AV_SAMPLE_FMT_S32, int32_t, AV_SAMPLE_FMT_DBL, double, av_clipl_int32(llrint(*(const double *) pi *(1U<< 31)))) #define SET_CONV_FUNC_GROUP(ofmt, ifmt) static void set_generic_function(AudioConvert *ac) { } void ff_audio_convert_free(AudioConvert **ac) { if(! *ac) return;ff_dither_free(&(*ac) ->dc);av_freep(ac);} AudioConvert *ff_audio_convert_alloc(AVAudioResampleContext *avr, enum AVSampleFormat out_fmt, enum AVSampleFormat in_fmt, int channels, int sample_rate, int apply_map) { AudioConvert *ac;int in_planar, out_planar;ac=av_mallocz(sizeof(*ac));if(!ac) return NULL;ac->avr=avr;ac->out_fmt=out_fmt;ac->in_fmt=in_fmt;ac->channels=channels;ac->apply_map=apply_map;if(avr->dither_method !=AV_RESAMPLE_DITHER_NONE &&av_get_packed_sample_fmt(out_fmt)==AV_SAMPLE_FMT_S16 &&av_get_bytes_per_sample(in_fmt) > 2) { ac->dc=ff_dither_alloc(avr, out_fmt, in_fmt, channels, sample_rate, apply_map);if(!ac->dc) { av_free(ac);return NULL;} return ac;} in_planar=ff_sample_fmt_is_planar(in_fmt, channels);out_planar=ff_sample_fmt_is_planar(out_fmt, channels);if(in_planar==out_planar) { ac->func_type=CONV_FUNC_TYPE_FLAT;ac->planes=in_planar ? ac->channels :1;} else if(in_planar) ac->func_type=CONV_FUNC_TYPE_INTERLEAVE;else ac->func_type=CONV_FUNC_TYPE_DEINTERLEAVE;set_generic_function(ac);if(ARCH_AARCH64) ff_audio_convert_init_aarch64(ac);if(ARCH_ARM) ff_audio_convert_init_arm(ac);if(ARCH_X86) ff_audio_convert_init_x86(ac);return ac;} int ff_audio_convert(AudioConvert *ac, AudioData *out, AudioData *in) { int use_generic=1;int len=in->nb_samples;int p;if(ac->dc) { av_log(ac->avr, AV_LOG_TRACE, "%d samples - audio_convert: %s to %s (dithered)\n", len, av_get_sample_fmt_name(ac->in_fmt), av_get_sample_fmt_name(ac->out_fmt));return ff_convert_dither(ac-> in
Definition: audio_convert.c:326
i
int i
Definition: input.c:407
uint8_t
uint8_t
Definition: audio_convert.c:194
AV_SAMPLE_FMT_S16
@ AV_SAMPLE_FMT_S16
signed 16 bits
Definition: samplefmt.h:61
ff_vf_reverse
AVFilter ff_vf_reverse
AVFilterPad::name
const char * name
Pad name.
Definition: internal.h:60
AVFilter
Filter definition.
Definition: avfilter.h:145
ret
ret
Definition: filter_design.txt:187
ReverseContext::pts
int64_t * pts
Definition: f_reverse.c:34
ff_all_samplerates
AVFilterFormats * ff_all_samplerates(void)
Definition: formats.c:421
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:341
AVMEDIA_TYPE_VIDEO
@ AVMEDIA_TYPE_VIDEO
Definition: avutil.h:201
ReverseContext::pts_size
unsigned int pts_size
Definition: f_reverse.c:33
av_freep
#define av_freep(p)
Definition: tableprint_vlc.h:35
query_formats
static int query_formats(AVFilterContext *ctx)
Definition: aeval.c:243
ff_set_common_samplerates
int ff_set_common_samplerates(AVFilterContext *ctx, AVFilterFormats *samplerates)
Definition: formats.c:575
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
request_frame
static int request_frame(AVFilterLink *outlink)
Definition: aeval.c:273
ff_set_common_channel_layouts
int ff_set_common_channel_layouts(AVFilterContext *ctx, AVFilterChannelLayouts *channel_layouts)
A helper for query_formats() which sets all links to the same list of channel layouts/sample rates.
Definition: formats.c:568