Libav
trim.c
Go to the documentation of this file.
1 /*
2  * This file is part of Libav.
3  *
4  * Libav is free software; you can redistribute it and/or
5  * modify it under the terms of the GNU Lesser General Public
6  * License as published by the Free Software Foundation; either
7  * version 2.1 of the License, or (at your option) any later version.
8  *
9  * Libav is distributed in the hope that it will be useful,
10  * but WITHOUT ANY WARRANTY; without even the implied warranty of
11  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
12  * Lesser General Public License for more details.
13  *
14  * You should have received a copy of the GNU Lesser General Public
15  * License along with Libav; if not, write to the Free Software
16  * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
17  */
18 
19 #include <float.h>
20 #include <math.h>
21 #include <stdint.h>
22 
23 #include "config.h"
24 
25 #include "libavutil/avassert.h"
27 #include "libavutil/common.h"
28 #include "libavutil/log.h"
29 #include "libavutil/mathematics.h"
30 #include "libavutil/opt.h"
31 #include "libavutil/samplefmt.h"
32 
33 #include "audio.h"
34 #include "avfilter.h"
35 #include "internal.h"
36 
37 typedef struct TrimContext {
38  const AVClass *class;
39 
40  /*
41  * AVOptions
42  */
43  double duration;
46  /*
47  * in the link timebase for video,
48  * in 1/samplerate for audio
49  */
50  int64_t start_pts, end_pts;
52 
53  /*
54  * number of video frames that arrived on this filter so far
55  */
56  int64_t nb_frames;
57  /*
58  * number of audio samples that arrived on this filter so far
59  */
60  int64_t nb_samples;
61  /*
62  * timestamp of the first frame in the output, in the timebase units
63  */
64  int64_t first_pts;
65  /*
66  * duration in the timebase units
67  */
68  int64_t duration_tb;
69 
70  int64_t next_pts;
71 
72  int eof;
74 } TrimContext;
75 
76 static int init(AVFilterContext *ctx)
77 {
78  TrimContext *s = ctx->priv;
79 
81 
82  return 0;
83 }
84 
85 static int config_input(AVFilterLink *inlink)
86 {
87  AVFilterContext *ctx = inlink->dst;
88  TrimContext *s = ctx->priv;
89  AVRational tb = (inlink->type == AVMEDIA_TYPE_VIDEO) ?
90  inlink->time_base : (AVRational){ 1, inlink->sample_rate };
91 
92  if (s->start_time != DBL_MAX) {
93  int64_t start_pts = lrintf(s->start_time / av_q2d(tb));
94  if (s->start_pts == AV_NOPTS_VALUE || start_pts < s->start_pts)
95  s->start_pts = start_pts;
96  }
97  if (s->end_time != DBL_MAX) {
98  int64_t end_pts = lrintf(s->end_time / av_q2d(tb));
99  if (s->end_pts == AV_NOPTS_VALUE || end_pts > s->end_pts)
100  s->end_pts = end_pts;
101  }
102  if (s->duration)
103  s->duration_tb = lrintf(s->duration / av_q2d(tb));
104 
105  return 0;
106 }
107 
108 static int request_frame(AVFilterLink *outlink)
109 {
110  AVFilterContext *ctx = outlink->src;
111  TrimContext *s = ctx->priv;
112  int ret;
113 
114  s->got_output = 0;
115  while (!s->got_output) {
116  if (s->eof)
117  return AVERROR_EOF;
118 
119  ret = ff_request_frame(ctx->inputs[0]);
120  if (ret < 0)
121  return ret;
122  }
123 
124  return 0;
125 }
126 
127 #define OFFSET(x) offsetof(TrimContext, x)
128 #define COMMON_OPTS \
129  { "start", "Timestamp in seconds of the first frame that " \
130  "should be passed", OFFSET(start_time), AV_OPT_TYPE_DOUBLE, { .dbl = DBL_MAX }, -DBL_MAX, DBL_MAX, FLAGS }, \
131  { "end", "Timestamp in seconds of the first frame that " \
132  "should be dropped again", OFFSET(end_time), AV_OPT_TYPE_DOUBLE, { .dbl = DBL_MAX }, -DBL_MAX, DBL_MAX, FLAGS }, \
133  { "start_pts", "Timestamp of the first frame that should be " \
134  " passed", OFFSET(start_pts), AV_OPT_TYPE_INT64, { .i64 = AV_NOPTS_VALUE }, INT64_MIN, INT64_MAX, FLAGS }, \
135  { "end_pts", "Timestamp of the first frame that should be " \
136  "dropped again", OFFSET(end_pts), AV_OPT_TYPE_INT64, { .i64 = AV_NOPTS_VALUE }, INT64_MIN, INT64_MAX, FLAGS }, \
137  { "duration", "Maximum duration of the output in seconds", OFFSET(duration), AV_OPT_TYPE_DOUBLE, { .dbl = 0 }, 0, DBL_MAX, FLAGS },
138 
139 
140 #if CONFIG_TRIM_FILTER
141 static int trim_filter_frame(AVFilterLink *inlink, AVFrame *frame)
142 {
143  AVFilterContext *ctx = inlink->dst;
144  TrimContext *s = ctx->priv;
145  int drop;
146 
147  /* drop everything if EOF has already been returned */
148  if (s->eof) {
149  av_frame_free(&frame);
150  return 0;
151  }
152 
153  if (s->start_frame >= 0 || s->start_pts != AV_NOPTS_VALUE) {
154  drop = 1;
155  if (s->start_frame >= 0 && s->nb_frames >= s->start_frame)
156  drop = 0;
157  if (s->start_pts != AV_NOPTS_VALUE && frame->pts != AV_NOPTS_VALUE &&
158  frame->pts >= s->start_pts)
159  drop = 0;
160  if (drop)
161  goto drop;
162  }
163 
164  if (s->first_pts == AV_NOPTS_VALUE && frame->pts != AV_NOPTS_VALUE)
165  s->first_pts = frame->pts;
166 
167  if (s->end_frame != INT64_MAX || s->end_pts != AV_NOPTS_VALUE || s->duration_tb) {
168  drop = 1;
169 
170  if (s->end_frame != INT64_MAX && s->nb_frames < s->end_frame)
171  drop = 0;
172  if (s->end_pts != AV_NOPTS_VALUE && frame->pts != AV_NOPTS_VALUE &&
173  frame->pts < s->end_pts)
174  drop = 0;
175  if (s->duration_tb && frame->pts != AV_NOPTS_VALUE &&
176  frame->pts - s->first_pts < s->duration_tb)
177  drop = 0;
178 
179  if (drop) {
180  s->eof = 1;
181  goto drop;
182  }
183  }
184 
185  s->nb_frames++;
186  s->got_output = 1;
187 
188  return ff_filter_frame(ctx->outputs[0], frame);
189 
190 drop:
191  s->nb_frames++;
192  av_frame_free(&frame);
193  return 0;
194 }
195 
196 #define FLAGS AV_OPT_FLAG_VIDEO_PARAM
197 static const AVOption trim_options[] = {
199  { "start_frame", "Number of the first frame that should be passed "
200  "to the output", OFFSET(start_frame), AV_OPT_TYPE_INT64, { .i64 = -1 }, -1, INT64_MAX, FLAGS },
201  { "end_frame", "Number of the first frame that should be dropped "
202  "again", OFFSET(end_frame), AV_OPT_TYPE_INT64, { .i64 = INT64_MAX }, 0, INT64_MAX, FLAGS },
203  { NULL },
204 };
205 #undef FLAGS
206 
207 static const AVClass trim_class = {
208  .class_name = "trim",
209  .item_name = av_default_item_name,
210  .option = trim_options,
211  .version = LIBAVUTIL_VERSION_INT,
212 };
213 
214 static const AVFilterPad trim_inputs[] = {
215  {
216  .name = "default",
217  .type = AVMEDIA_TYPE_VIDEO,
218  .filter_frame = trim_filter_frame,
219  .config_props = config_input,
220  },
221  { NULL }
222 };
223 
224 static const AVFilterPad trim_outputs[] = {
225  {
226  .name = "default",
227  .type = AVMEDIA_TYPE_VIDEO,
228  .request_frame = request_frame,
229  },
230  { NULL }
231 };
232 
233 AVFilter ff_vf_trim = {
234  .name = "trim",
235  .description = NULL_IF_CONFIG_SMALL("Pick one continuous section from the input, drop the rest."),
236 
237  .init = init,
238 
239  .priv_size = sizeof(TrimContext),
240  .priv_class = &trim_class,
241 
242  .inputs = trim_inputs,
243  .outputs = trim_outputs,
244 };
245 #endif // CONFIG_TRIM_FILTER
246 
247 #if CONFIG_ATRIM_FILTER
248 static int atrim_filter_frame(AVFilterLink *inlink, AVFrame *frame)
249 {
250  AVFilterContext *ctx = inlink->dst;
251  TrimContext *s = ctx->priv;
252  int64_t start_sample, end_sample = frame->nb_samples;
253  int64_t pts;
254  int drop;
255 
256  /* drop everything if EOF has already been returned */
257  if (s->eof) {
258  av_frame_free(&frame);
259  return 0;
260  }
261 
262  if (frame->pts != AV_NOPTS_VALUE)
263  pts = av_rescale_q(frame->pts, inlink->time_base,
264  (AVRational){ 1, inlink->sample_rate });
265  else
266  pts = s->next_pts;
267  s->next_pts = pts + frame->nb_samples;
268 
269  /* check if at least a part of the frame is after the start time */
270  if (s->start_sample < 0 && s->start_pts == AV_NOPTS_VALUE) {
271  start_sample = 0;
272  } else {
273  drop = 1;
274  start_sample = frame->nb_samples;
275 
276  if (s->start_sample >= 0 &&
277  s->nb_samples + frame->nb_samples > s->start_sample) {
278  drop = 0;
279  start_sample = FFMIN(start_sample, s->start_sample - s->nb_samples);
280  }
281 
282  if (s->start_pts != AV_NOPTS_VALUE && pts != AV_NOPTS_VALUE &&
283  pts + frame->nb_samples > s->start_pts) {
284  drop = 0;
285  start_sample = FFMIN(start_sample, s->start_pts - pts);
286  }
287 
288  if (drop)
289  goto drop;
290  }
291 
292  if (s->first_pts == AV_NOPTS_VALUE)
293  s->first_pts = pts + start_sample;
294 
295  /* check if at least a part of the frame is before the end time */
296  if (s->end_sample == INT64_MAX && s->end_pts == AV_NOPTS_VALUE && !s->duration_tb) {
297  end_sample = frame->nb_samples;
298  } else {
299  drop = 1;
300  end_sample = 0;
301 
302  if (s->end_sample != INT64_MAX &&
303  s->nb_samples < s->end_sample) {
304  drop = 0;
305  end_sample = FFMAX(end_sample, s->end_sample - s->nb_samples);
306  }
307 
308  if (s->end_pts != AV_NOPTS_VALUE && pts != AV_NOPTS_VALUE &&
309  pts < s->end_pts) {
310  drop = 0;
311  end_sample = FFMAX(end_sample, s->end_pts - pts);
312  }
313 
314  if (s->duration_tb && pts - s->first_pts < s->duration_tb) {
315  drop = 0;
316  end_sample = FFMAX(end_sample, s->first_pts + s->duration_tb - pts);
317  }
318 
319  if (drop) {
320  s->eof = 1;
321  goto drop;
322  }
323  }
324 
325  s->nb_samples += frame->nb_samples;
326  start_sample = FFMAX(0, start_sample);
327  end_sample = FFMIN(frame->nb_samples, end_sample);
328  av_assert0(start_sample < end_sample);
329 
330  if (start_sample) {
331  AVFrame *out = ff_get_audio_buffer(ctx->outputs[0], end_sample - start_sample);
332  if (!out) {
333  av_frame_free(&frame);
334  return AVERROR(ENOMEM);
335  }
336 
337  av_frame_copy_props(out, frame);
338  av_samples_copy(out->extended_data, frame->extended_data, 0, start_sample,
340  frame->format);
341  if (out->pts != AV_NOPTS_VALUE)
342  out->pts += av_rescale_q(start_sample, (AVRational){ 1, out->sample_rate },
343  inlink->time_base);
344 
345  av_frame_free(&frame);
346  frame = out;
347  } else
348  frame->nb_samples = end_sample;
349 
350  s->got_output = 1;
351  return ff_filter_frame(ctx->outputs[0], frame);
352 
353 drop:
354  s->nb_samples += frame->nb_samples;
355  av_frame_free(&frame);
356  return 0;
357 }
358 
359 #define FLAGS AV_OPT_FLAG_AUDIO_PARAM
360 static const AVOption atrim_options[] = {
362  { "start_sample", "Number of the first audio sample that should be "
363  "passed to the output", OFFSET(start_sample), AV_OPT_TYPE_INT64, { .i64 = -1 }, -1, INT64_MAX, FLAGS },
364  { "end_sample", "Number of the first audio sample that should be "
365  "dropped again", OFFSET(end_sample), AV_OPT_TYPE_INT64, { .i64 = INT64_MAX }, 0, INT64_MAX, FLAGS },
366  { NULL },
367 };
368 #undef FLAGS
369 
370 static const AVClass atrim_class = {
371  .class_name = "atrim",
372  .item_name = av_default_item_name,
373  .option = atrim_options,
374  .version = LIBAVUTIL_VERSION_INT,
375 };
376 
377 static const AVFilterPad atrim_inputs[] = {
378  {
379  .name = "default",
380  .type = AVMEDIA_TYPE_AUDIO,
381  .filter_frame = atrim_filter_frame,
382  .config_props = config_input,
383  },
384  { NULL }
385 };
386 
387 static const AVFilterPad atrim_outputs[] = {
388  {
389  .name = "default",
390  .type = AVMEDIA_TYPE_AUDIO,
391  .request_frame = request_frame,
392  },
393  { NULL }
394 };
395 
396 AVFilter ff_af_atrim = {
397  .name = "atrim",
398  .description = NULL_IF_CONFIG_SMALL("Pick one continuous section from the input, drop the rest."),
399 
400  .init = init,
401 
402  .priv_size = sizeof(TrimContext),
403  .priv_class = &atrim_class,
404 
405  .inputs = atrim_inputs,
406  .outputs = atrim_outputs,
407 };
408 #endif // CONFIG_ATRIM_FILTER
This structure describes decoded (raw) audio or video data.
Definition: frame.h:135
#define FLAGS
Definition: cmdutils.c:432
AVOption.
Definition: opt.h:234
static const AVFilterPad outputs[]
Definition: af_ashowinfo.c:232
Main libavfilter public API header.
int64_t end_pts
Definition: trim.c:50
#define OFFSET(x)
Definition: trim.c:127
int64_t start_sample
Definition: trim.c:51
double start_time
Definition: trim.c:44
int64_t duration_tb
Definition: trim.c:68
const char * name
Pad name.
Definition: internal.h:42
const char * class_name
The name of the class; usually it is the same name as the context structure type to which the AVClass...
Definition: log.h:38
AVFilterLink ** inputs
array of pointers to input links
Definition: avfilter.h:571
#define av_assert0(cond)
assert() equivalent, that is always enabled.
Definition: avassert.h:37
int ff_filter_frame(AVFilterLink *link, AVFrame *frame)
Send a frame of data to the next filter.
Definition: avfilter.c:733
double duration
Definition: trim.c:43
AVOptions.
int64_t end_sample
Definition: trim.c:51
int64_t next_pts
Definition: trim.c:70
int64_t pts
Presentation timestamp in time_base units (time when frame should be shown to user).
Definition: frame.h:211
static double av_q2d(AVRational a)
Convert rational to double.
Definition: rational.h:69
#define AVERROR_EOF
End of file.
Definition: error.h:51
static int init(AVFilterContext *ctx)
Definition: trim.c:76
static int config_input(AVFilterLink *inlink)
Definition: trim.c:85
int64_t start_frame
Definition: trim.c:45
A filter pad used for either input or output.
Definition: internal.h:36
int64_t av_rescale_q(int64_t a, AVRational bq, AVRational cq)
Rescale a 64-bit integer by 2 rational numbers.
Definition: mathematics.c:129
double end_time
Definition: trim.c:44
AVFrame * ff_get_audio_buffer(AVFilterLink *link, int nb_samples)
Request an audio samples buffer with a specific set of permissions.
Definition: audio.c:57
#define AVERROR(e)
Definition: error.h:43
void av_frame_free(AVFrame **frame)
Free the frame and any dynamically allocated objects in it, e.g.
Definition: frame.c:69
#define NULL_IF_CONFIG_SMALL(x)
Return NULL if CONFIG_SMALL is true, otherwise the argument without modification. ...
Definition: internal.h:145
void * priv
private data for use by the filter
Definition: avfilter.h:584
int av_get_channel_layout_nb_channels(uint64_t channel_layout)
Return the number of channels in the channel layout.
simple assert() macros that are a bit more flexible than ISO C assert().
#define FFMAX(a, b)
Definition: common.h:55
uint64_t channel_layout
Channel layout of the audio data.
Definition: frame.h:381
audio channel layout utility functions
#define FFMIN(a, b)
Definition: common.h:57
static av_always_inline av_const long int lrintf(float x)
Definition: libm.h:144
int eof
Definition: trim.c:72
LIBAVUTIL_VERSION_INT
Definition: eval.c:55
int format
format of the frame, -1 if unknown or unset Values correspond to enum AVPixelFormat for video frames...
Definition: frame.h:186
NULL
Definition: eval.c:55
int64_t nb_frames
Definition: trim.c:56
int64_t first_pts
Definition: trim.c:64
av_default_item_name
Definition: dnxhdenc.c:52
int av_samples_copy(uint8_t **dst, uint8_t *const *src, int dst_offset, int src_offset, int nb_samples, int nb_channels, enum AVSampleFormat sample_fmt)
Copy samples from src to dst.
Definition: samplefmt.c:187
Describe the class of an AVClass context structure.
Definition: log.h:33
int sample_rate
Sample rate of the audio data.
Definition: frame.h:376
Filter definition.
Definition: avfilter.h:421
static const AVFilterPad inputs[]
Definition: af_ashowinfo.c:221
rational number numerator/denominator
Definition: rational.h:43
int64_t nb_samples
Definition: trim.c:60
#define COMMON_OPTS
Definition: trim.c:128
const char * name
Filter name.
Definition: avfilter.h:425
AVFilterLink ** outputs
array of pointers to output links
Definition: avfilter.h:578
int64_t start_pts
Definition: trim.c:50
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_dlog(ac->avr,"%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-> out
common internal and external API header
int64_t end_frame
Definition: trim.c:45
An instance of a filter.
Definition: avfilter.h:563
int got_output
Definition: trim.c:73
int ff_request_frame(AVFilterLink *link)
Request an input frame from the filter at the other end of the link.
Definition: avfilter.c:249
internal API functions
uint8_t ** extended_data
pointers to the data planes/channels.
Definition: frame.h:169
int nb_samples
number of audio samples (per channel) described by this frame
Definition: frame.h:179
int av_frame_copy_props(AVFrame *dst, const AVFrame *src)
Copy only "metadata" fields from src to dst.
Definition: frame.c:367
#define AV_NOPTS_VALUE
Undefined timestamp value.
Definition: avutil.h:228
static int request_frame(AVFilterLink *outlink)
Definition: trim.c:108