FFmpeg
 All Data Structures Namespaces Files Functions Variables Typedefs Enumerations Enumerator Macros Groups Pages
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 
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  av_freep(&s->pts);
62  av_freep(&s->frames);
63 }
64 
65 static int filter_frame(AVFilterLink *inlink, AVFrame *in)
66 {
67  AVFilterContext *ctx = inlink->dst;
68  ReverseContext *s = ctx->priv;
69  void *ptr;
70 
71  if (s->nb_frames + 1 > s->pts_size / sizeof(*(s->pts))) {
72  ptr = av_fast_realloc(s->pts, &s->pts_size, s->pts_size * 2);
73  if (!ptr)
74  return AVERROR(ENOMEM);
75  s->pts = ptr;
76  }
77 
78  if (s->nb_frames + 1 > s->frames_size / sizeof(*(s->frames))) {
79  ptr = av_fast_realloc(s->frames, &s->frames_size, s->frames_size * 2);
80  if (!ptr)
81  return AVERROR(ENOMEM);
82  s->frames = ptr;
83  }
84 
85  s->frames[s->nb_frames] = in;
86  s->pts[s->nb_frames] = in->pts;
87  s->nb_frames++;
88 
89  return 0;
90 }
91 
92 #if CONFIG_REVERSE_FILTER
93 
94 static int request_frame(AVFilterLink *outlink)
95 {
96  AVFilterContext *ctx = outlink->src;
97  ReverseContext *s = ctx->priv;
98  int ret;
99 
100  ret = ff_request_frame(ctx->inputs[0]);
101 
102  if (ret == AVERROR_EOF && s->nb_frames > 0) {
103  AVFrame *out = s->frames[s->nb_frames - 1];
104  out->pts = s->pts[s->flush_idx++];
105  ret = ff_filter_frame(outlink, out);
106  s->nb_frames--;
107  }
108 
109  return ret;
110 }
111 
112 static const AVFilterPad reverse_inputs[] = {
113  {
114  .name = "default",
115  .type = AVMEDIA_TYPE_VIDEO,
116  .filter_frame = filter_frame,
117  },
118  { NULL }
119 };
120 
121 static const AVFilterPad reverse_outputs[] = {
122  {
123  .name = "default",
124  .type = AVMEDIA_TYPE_VIDEO,
125  .request_frame = request_frame,
126  },
127  { NULL }
128 };
129 
131  .name = "reverse",
132  .description = NULL_IF_CONFIG_SMALL("Reverse a clip."),
133  .priv_size = sizeof(ReverseContext),
134  .init = init,
135  .uninit = uninit,
136  .inputs = reverse_inputs,
137  .outputs = reverse_outputs,
138 };
139 
140 #endif /* CONFIG_REVERSE_FILTER */
141 
142 #if CONFIG_AREVERSE_FILTER
143 
144 static int query_formats(AVFilterContext *ctx)
145 {
148  int ret;
149 
150  layouts = ff_all_channel_counts();
151  if (!layouts)
152  return AVERROR(ENOMEM);
153  ret = ff_set_common_channel_layouts(ctx, layouts);
154  if (ret < 0)
155  return ret;
156 
158  if (ret < 0)
159  return ret;
160 
161  formats = ff_all_samplerates();
162  if (!formats)
163  return AVERROR(ENOMEM);
164  return ff_set_common_samplerates(ctx, formats);
165 }
166 
167 static void reverse_samples_planar(AVFrame *out)
168 {
169  for (int p = 0; p < out->channels; p++) {
170  switch (out->format) {
171  case AV_SAMPLE_FMT_U8P: {
172  uint8_t *dst = (uint8_t *)out->extended_data[p];
173  for (int i = 0, j = out->nb_samples - 1; i < j; i++, j--)
174  FFSWAP(uint8_t, dst[i], dst[j]);
175  }
176  break;
177  case AV_SAMPLE_FMT_S16P: {
178  int16_t *dst = (int16_t *)out->extended_data[p];
179  for (int i = 0, j = out->nb_samples - 1; i < j; i++, j--)
180  FFSWAP(int16_t, dst[i], dst[j]);
181  }
182  break;
183  case AV_SAMPLE_FMT_S32P: {
184  int32_t *dst = (int32_t *)out->extended_data[p];
185  for (int i = 0, j = out->nb_samples - 1; i < j; i++, j--)
186  FFSWAP(int32_t, dst[i], dst[j]);
187  }
188  break;
189  case AV_SAMPLE_FMT_FLTP: {
190  float *dst = (float *)out->extended_data[p];
191  for (int i = 0, j = out->nb_samples - 1; i < j; i++, j--)
192  FFSWAP(float, dst[i], dst[j]);
193  }
194  break;
195  case AV_SAMPLE_FMT_DBLP: {
196  double *dst = (double *)out->extended_data[p];
197  for (int i = 0, j = out->nb_samples - 1; i < j; i++, j--)
198  FFSWAP(double, dst[i], dst[j]);
199  }
200  break;
201  }
202  }
203 }
204 
205 static void reverse_samples_packed(AVFrame *out)
206 {
207  const int channels = out->channels;
208 
209  switch (out->format) {
210  case AV_SAMPLE_FMT_U8: {
211  uint8_t *dst = (uint8_t *)out->extended_data[0];
212  for (int i = 0, j = out->nb_samples - 1; i < j; i++, j--)
213  for (int p = 0; p < channels; p++)
214  FFSWAP(uint8_t, dst[i * channels + p], dst[j * channels + p]);
215  }
216  break;
217  case AV_SAMPLE_FMT_S16: {
218  int16_t *dst = (int16_t *)out->extended_data[0];
219  for (int i = 0, j = out->nb_samples - 1; i < j; i++, j--)
220  for (int p = 0; p < channels; p++)
221  FFSWAP(int16_t, dst[i * channels + p], dst[j * channels + p]);
222  }
223  break;
224  case AV_SAMPLE_FMT_S32: {
225  int32_t *dst = (int32_t *)out->extended_data[0];
226  for (int i = 0, j = out->nb_samples - 1; i < j; i++, j--)
227  for (int p = 0; p < channels; p++)
228  FFSWAP(int32_t, dst[i * channels + p], dst[j * channels + p]);
229  }
230  break;
231  case AV_SAMPLE_FMT_FLT: {
232  float *dst = (float *)out->extended_data[0];
233  for (int i = 0, j = out->nb_samples - 1; i < j; i++, j--)
234  for (int p = 0; p < channels; p++)
235  FFSWAP(float, dst[i * channels + p], dst[j * channels + p]);
236  }
237  break;
238  case AV_SAMPLE_FMT_DBL: {
239  double *dst = (double *)out->extended_data[0];
240  for (int i = 0, j = out->nb_samples - 1; i < j; i++, j--)
241  for (int p = 0; p < channels; p++)
242  FFSWAP(double, dst[i * channels + p], dst[j * channels + p]);
243  }
244  break;
245  }
246 }
247 
248 static int areverse_request_frame(AVFilterLink *outlink)
249 {
250  AVFilterContext *ctx = outlink->src;
251  ReverseContext *s = ctx->priv;
252  int ret;
253 
254  ret = ff_request_frame(ctx->inputs[0]);
255 
256  if (ret == AVERROR_EOF && s->nb_frames > 0) {
257  AVFrame *out = s->frames[s->nb_frames - 1];
258  out->pts = s->pts[s->flush_idx++];
259 
261  reverse_samples_planar(out);
262  else
263  reverse_samples_packed(out);
264  ret = ff_filter_frame(outlink, out);
265  s->nb_frames--;
266  }
267 
268  return ret;
269 }
270 
271 static const AVFilterPad areverse_inputs[] = {
272  {
273  .name = "default",
274  .type = AVMEDIA_TYPE_AUDIO,
275  .filter_frame = filter_frame,
276  .needs_writable = 1,
277  },
278  { NULL }
279 };
280 
281 static const AVFilterPad areverse_outputs[] = {
282  {
283  .name = "default",
284  .type = AVMEDIA_TYPE_AUDIO,
285  .request_frame = areverse_request_frame,
286  },
287  { NULL }
288 };
289 
291  .name = "areverse",
292  .description = NULL_IF_CONFIG_SMALL("Reverse an audio clip."),
293  .query_formats = query_formats,
294  .priv_size = sizeof(ReverseContext),
295  .init = init,
296  .uninit = uninit,
297  .inputs = areverse_inputs,
298  .outputs = areverse_outputs,
299 };
300 
301 #endif /* CONFIG_AREVERSE_FILTER */
float, planar
Definition: samplefmt.h:69
#define NULL
Definition: coverity.c:32
int ff_set_common_channel_layouts(AVFilterContext *ctx, AVFilterChannelLayouts *layouts)
A helper for query_formats() which sets all links to the same list of channel layouts/sample rates...
Definition: formats.c:549
This structure describes decoded (raw) audio or video data.
Definition: frame.h:226
AVFilter ff_af_areverse
Main libavfilter public API header.
channels
Definition: aptx.c:30
double, planar
Definition: samplefmt.h:70
static av_cold void uninit(AVFilterContext *ctx)
Definition: f_reverse.c:57
unsigned int pts_size
Definition: f_reverse.c:33
const char * name
Pad name.
Definition: internal.h:60
AVFilterLink ** inputs
array of pointers to input links
Definition: avfilter.h:346
int ff_filter_frame(AVFilterLink *link, AVFrame *frame)
Send a frame of data to the next filter.
Definition: avfilter.c:1080
uint8_t
#define av_cold
Definition: attributes.h:82
AV_SAMPLE_FMT_U8
AVOptions.
int64_t pts
Presentation timestamp in time_base units (time when frame should be shown to user).
Definition: frame.h:319
#define AVERROR_EOF
End of file.
Definition: error.h:55
signed 32 bits
Definition: samplefmt.h:62
AVFilterFormats * ff_all_formats(enum AVMediaType type)
Return a list of all formats supported by FFmpeg for the given media type.
Definition: formats.c:350
int av_sample_fmt_is_planar(enum AVSampleFormat sample_fmt)
Check if the sample format is planar.
Definition: samplefmt.c:112
A filter pad used for either input or output.
Definition: internal.h:54
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:568
#define AVERROR(e)
Definition: error.h:43
#define NULL_IF_CONFIG_SMALL(x)
Return NULL if CONFIG_SMALL is true, otherwise the argument without modification. ...
Definition: internal.h:186
AVFrame ** frames
Definition: f_reverse.c:31
void * priv
private data for use by the filter
Definition: avfilter.h:353
static int filter_frame(AVFilterLink *inlink, AVFrame *in)
Definition: f_reverse.c:65
int channels
number of audio channels, only used for audio.
Definition: frame.h:531
static int request_frame(AVFilterLink *outlink)
Definition: aeval.c:274
signed 32 bits, planar
Definition: samplefmt.h:68
int32_t
AVFormatContext * ctx
Definition: movenc.c:48
#define s(width, name)
Definition: cbs_vp9.c:257
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:464
unsigned 8 bits, planar
Definition: samplefmt.h:66
static const AVFilterPad inputs[]
Definition: af_acontrast.c:193
static const AVFilterPad outputs[]
Definition: af_acontrast.c:203
A list of supported channel layouts.
Definition: formats.h:85
static av_cold int init(AVFilterContext *ctx)
Definition: f_reverse.c:38
int format
format of the frame, -1 if unknown or unset Values correspond to enum AVPixelFormat for video frames...
Definition: frame.h:299
#define DEFAULT_LENGTH
Definition: f_reverse.c:27
int64_t * pts
Definition: f_reverse.c:34
uint8_t pi<< 24) CONV_FUNC_GROUP(AV_SAMPLE_FMT_FLT, float, AV_SAMPLE_FMT_U8, uint8_t,(*(constuint8_t *) pi-0x80)*(1.0f/(1<< 7))) CONV_FUNC_GROUP(AV_SAMPLE_FMT_DBL, double, AV_SAMPLE_FMT_U8, uint8_t,(*(constuint8_t *) pi-0x80)*(1.0/(1<< 7))) CONV_FUNC_GROUP(AV_SAMPLE_FMT_U8, uint8_t, AV_SAMPLE_FMT_S16, int16_t,(*(constint16_t *) pi >>8)+0x80) CONV_FUNC_GROUP(AV_SAMPLE_FMT_FLT, float, AV_SAMPLE_FMT_S16, int16_t,*(constint16_t *) pi *(1.0f/(1<< 15))) CONV_FUNC_GROUP(AV_SAMPLE_FMT_DBL, double, AV_SAMPLE_FMT_S16, int16_t,*(constint16_t *) pi *(1.0/(1<< 15))) CONV_FUNC_GROUP(AV_SAMPLE_FMT_U8, uint8_t, AV_SAMPLE_FMT_S32, int32_t,(*(constint32_t *) pi >>24)+0x80) CONV_FUNC_GROUP(AV_SAMPLE_FMT_FLT, float, AV_SAMPLE_FMT_S32, int32_t,*(constint32_t *) pi *(1.0f/(1U<< 31))) CONV_FUNC_GROUP(AV_SAMPLE_FMT_DBL, double, AV_SAMPLE_FMT_S32, int32_t,*(constint32_t *) pi *(1.0/(1U<< 31))) CONV_FUNC_GROUP(AV_SAMPLE_FMT_U8, uint8_t, AV_SAMPLE_FMT_FLT, float, av_clip_uint8(lrintf(*(constfloat *) pi *(1<< 7))+0x80)) CONV_FUNC_GROUP(AV_SAMPLE_FMT_S16, int16_t, AV_SAMPLE_FMT_FLT, float, av_clip_int16(lrintf(*(constfloat *) pi *(1<< 15)))) CONV_FUNC_GROUP(AV_SAMPLE_FMT_S32, int32_t, AV_SAMPLE_FMT_FLT, float, av_clipl_int32(llrintf(*(constfloat *) pi *(1U<< 31)))) CONV_FUNC_GROUP(AV_SAMPLE_FMT_U8, uint8_t, AV_SAMPLE_FMT_DBL, double, av_clip_uint8(lrint(*(constdouble *) pi *(1<< 7))+0x80)) CONV_FUNC_GROUP(AV_SAMPLE_FMT_S16, int16_t, AV_SAMPLE_FMT_DBL, double, av_clip_int16(lrint(*(constdouble *) pi *(1<< 15)))) CONV_FUNC_GROUP(AV_SAMPLE_FMT_S32, int32_t, AV_SAMPLE_FMT_DBL, double, av_clipl_int32(llrint(*(constdouble *) pi *(1U<< 31))))#defineSET_CONV_FUNC_GROUP(ofmt, ifmt) staticvoidset_generic_function(AudioConvert *ac){}voidff_audio_convert_free(AudioConvert **ac){if(!*ac) return;ff_dither_free(&(*ac) ->dc);av_freep(ac);}AudioConvert *ff_audio_convert_alloc(AVAudioResampleContext *avr, enumAVSampleFormatout_fmt, enumAVSampleFormatin_fmt, intchannels, intsample_rate, intapply_map){AudioConvert *ac;intin_planar, out_planar;ac=av_mallocz(sizeof(*ac));if(!ac) returnNULL;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);returnNULL;}returnac;}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;}elseif(in_planar) ac->func_type=CONV_FUNC_TYPE_INTERLEAVE;elseac->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);returnac;}intff_audio_convert(AudioConvert *ac, AudioData *out, AudioData *in){intuse_generic=1;intlen=in->nb_samples;intp;if(ac->dc){av_log(ac->avr, AV_LOG_TRACE,"%dsamples-audio_convert:%sto%s(dithered)\n", len, av_get_sample_fmt_name(ac->in_fmt), av_get_sample_fmt_name(ac->out_fmt));returnff_convert_dither(ac-> in
Filter definition.
Definition: avfilter.h:144
const char * name
Filter name.
Definition: avfilter.h:148
enum MovChannelLayoutTag * layouts
Definition: mov_chan.c:434
AVFilterFormats * ff_all_samplerates(void)
Definition: formats.c:395
unsigned int frames_size
Definition: f_reverse.c:32
static int query_formats(AVFilterContext *ctx)
Definition: aeval.c:244
signed 16 bits
Definition: samplefmt.h:61
AVFilter ff_vf_reverse
A list of supported formats for one end of a filter link.
Definition: formats.h:64
An instance of a filter.
Definition: avfilter.h:338
FILE * out
Definition: movenc.c:54
#define av_freep(p)
signed 16 bits, planar
Definition: samplefmt.h:67
int ff_request_frame(AVFilterLink *link)
Request an input frame from the filter at the other end of the link.
Definition: avfilter.c:407
formats
Definition: signature.h:48
#define FFSWAP(type, a, b)
Definition: common.h:99
internal API functions
AVFilterChannelLayouts * ff_all_channel_counts(void)
Construct an AVFilterChannelLayouts coding for any channel layout, with known or unknown disposition...
Definition: formats.c:410
uint8_t ** extended_data
pointers to the data planes/channels.
Definition: frame.h:273
int nb_samples
number of audio samples (per channel) described by this frame
Definition: frame.h:292
for(j=16;j >0;--j)
int ff_set_common_samplerates(AVFilterContext *ctx, AVFilterFormats *samplerates)
Definition: formats.c:556