FFmpeg
 All Data Structures Namespaces Files Functions Variables Typedefs Enumerations Enumerator Macros Groups Pages
trim.c
Go to the documentation of this file.
1 /*
2  * This file is part of FFmpeg.
3  *
4  * FFmpeg 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  * FFmpeg 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 FFmpeg; 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  int64_t duration;
44  int64_t start_time, end_time;
46 
47  double duration_dbl;
49  /*
50  * in the link timebase for video,
51  * in 1/samplerate for audio
52  */
53  int64_t start_pts, end_pts;
55 
56  /*
57  * number of video frames that arrived on this filter so far
58  */
59  int64_t nb_frames;
60  /*
61  * number of audio samples that arrived on this filter so far
62  */
63  int64_t nb_samples;
64  /*
65  * timestamp of the first frame in the output, in the timebase units
66  */
67  int64_t first_pts;
68  /*
69  * duration in the timebase units
70  */
71  int64_t duration_tb;
72 
73  int64_t next_pts;
74 
75  int eof;
76 } TrimContext;
77 
79 {
80  TrimContext *s = ctx->priv;
81 
83 
84  return 0;
85 }
86 
87 static int config_input(AVFilterLink *inlink)
88 {
89  AVFilterContext *ctx = inlink->dst;
90  TrimContext *s = ctx->priv;
91  AVRational tb = (inlink->type == AVMEDIA_TYPE_VIDEO) ?
92  inlink->time_base : (AVRational){ 1, inlink->sample_rate };
93 
94  if (s->start_time_dbl != DBL_MAX)
95  s->start_time = s->start_time_dbl * 1e6;
96  if (s->end_time_dbl != DBL_MAX)
97  s->end_time = s->end_time_dbl * 1e6;
98  if (s->duration_dbl != 0)
99  s->duration = s->duration_dbl * 1e6;
100 
101  if (s->start_time != INT64_MAX) {
102  int64_t start_pts = av_rescale_q(s->start_time, AV_TIME_BASE_Q, tb);
103  if (s->start_pts == AV_NOPTS_VALUE || start_pts < s->start_pts)
104  s->start_pts = start_pts;
105  }
106  if (s->end_time != INT64_MAX) {
107  int64_t end_pts = av_rescale_q(s->end_time, AV_TIME_BASE_Q, tb);
108  if (s->end_pts == AV_NOPTS_VALUE || end_pts > s->end_pts)
109  s->end_pts = end_pts;
110  }
111  if (s->duration)
113 
114  return 0;
115 }
116 
117 #define OFFSET(x) offsetof(TrimContext, x)
118 #define COMMON_OPTS \
119  { "starti", "Timestamp of the first frame that " \
120  "should be passed", OFFSET(start_time), AV_OPT_TYPE_DURATION, { .i64 = INT64_MAX }, INT64_MIN, INT64_MAX, FLAGS }, \
121  { "endi", "Timestamp of the first frame that " \
122  "should be dropped again", OFFSET(end_time), AV_OPT_TYPE_DURATION, { .i64 = INT64_MAX }, INT64_MIN, INT64_MAX, FLAGS }, \
123  { "start_pts", "Timestamp of the first frame that should be " \
124  " passed", OFFSET(start_pts), AV_OPT_TYPE_INT64, { .i64 = AV_NOPTS_VALUE }, INT64_MIN, INT64_MAX, FLAGS }, \
125  { "end_pts", "Timestamp of the first frame that should be " \
126  "dropped again", OFFSET(end_pts), AV_OPT_TYPE_INT64, { .i64 = AV_NOPTS_VALUE }, INT64_MIN, INT64_MAX, FLAGS }, \
127  { "durationi", "Maximum duration of the output", OFFSET(duration), AV_OPT_TYPE_DURATION, { .i64 = 0 }, 0, INT64_MAX, FLAGS },
128 
129 #define COMPAT_OPTS \
130  { "start", "Timestamp in seconds of the first frame that " \
131  "should be passed", OFFSET(start_time_dbl),AV_OPT_TYPE_DOUBLE, { .dbl = DBL_MAX }, -DBL_MAX, DBL_MAX, FLAGS }, \
132  { "end", "Timestamp in seconds of the first frame that " \
133  "should be dropped again", OFFSET(end_time_dbl), AV_OPT_TYPE_DOUBLE, { .dbl = DBL_MAX }, -DBL_MAX, DBL_MAX, FLAGS }, \
134  { "duration", "Maximum duration of the output in seconds", OFFSET(duration_dbl), AV_OPT_TYPE_DOUBLE, { .dbl = 0 }, 0, DBL_MAX, FLAGS },
135 
136 
137 #if CONFIG_TRIM_FILTER
138 static int trim_filter_frame(AVFilterLink *inlink, AVFrame *frame)
139 {
140  AVFilterContext *ctx = inlink->dst;
141  TrimContext *s = ctx->priv;
142  int drop;
143 
144  /* drop everything if EOF has already been returned */
145  if (s->eof) {
146  av_frame_free(&frame);
147  return 0;
148  }
149 
150  if (s->start_frame >= 0 || s->start_pts != AV_NOPTS_VALUE) {
151  drop = 1;
152  if (s->start_frame >= 0 && s->nb_frames >= s->start_frame)
153  drop = 0;
154  if (s->start_pts != AV_NOPTS_VALUE && frame->pts != AV_NOPTS_VALUE &&
155  frame->pts >= s->start_pts)
156  drop = 0;
157  if (drop)
158  goto drop;
159  }
160 
161  if (s->first_pts == AV_NOPTS_VALUE && frame->pts != AV_NOPTS_VALUE)
162  s->first_pts = frame->pts;
163 
164  if (s->end_frame != INT64_MAX || s->end_pts != AV_NOPTS_VALUE || s->duration_tb) {
165  drop = 1;
166 
167  if (s->end_frame != INT64_MAX && s->nb_frames < s->end_frame)
168  drop = 0;
169  if (s->end_pts != AV_NOPTS_VALUE && frame->pts != AV_NOPTS_VALUE &&
170  frame->pts < s->end_pts)
171  drop = 0;
172  if (s->duration_tb && frame->pts != AV_NOPTS_VALUE &&
173  frame->pts - s->first_pts < s->duration_tb)
174  drop = 0;
175 
176  if (drop) {
177  s->eof = 1;
179  goto drop;
180  }
181  }
182 
183  s->nb_frames++;
184 
185  return ff_filter_frame(ctx->outputs[0], frame);
186 
187 drop:
188  s->nb_frames++;
189  av_frame_free(&frame);
190  return 0;
191 }
192 
193 #define FLAGS AV_OPT_FLAG_VIDEO_PARAM | AV_OPT_FLAG_FILTERING_PARAM
194 static const AVOption trim_options[] = {
196  { "start_frame", "Number of the first frame that should be passed "
197  "to the output", OFFSET(start_frame), AV_OPT_TYPE_INT64, { .i64 = -1 }, -1, INT64_MAX, FLAGS },
198  { "end_frame", "Number of the first frame that should be dropped "
199  "again", OFFSET(end_frame), AV_OPT_TYPE_INT64, { .i64 = INT64_MAX }, 0, INT64_MAX, FLAGS },
201  { NULL }
202 };
203 #undef FLAGS
204 
206 
207 static const AVFilterPad trim_inputs[] = {
208  {
209  .name = "default",
210  .type = AVMEDIA_TYPE_VIDEO,
211  .filter_frame = trim_filter_frame,
212  .config_props = config_input,
213  },
214  { NULL }
215 };
216 
217 static const AVFilterPad trim_outputs[] = {
218  {
219  .name = "default",
220  .type = AVMEDIA_TYPE_VIDEO,
221  },
222  { NULL }
223 };
224 
225 AVFilter ff_vf_trim = {
226  .name = "trim",
227  .description = NULL_IF_CONFIG_SMALL("Pick one continuous section from the input, drop the rest."),
228  .init = init,
229  .priv_size = sizeof(TrimContext),
230  .priv_class = &trim_class,
231  .inputs = trim_inputs,
232  .outputs = trim_outputs,
233 };
234 #endif // CONFIG_TRIM_FILTER
235 
236 #if CONFIG_ATRIM_FILTER
237 static int atrim_filter_frame(AVFilterLink *inlink, AVFrame *frame)
238 {
239  AVFilterContext *ctx = inlink->dst;
240  TrimContext *s = ctx->priv;
241  int64_t start_sample, end_sample;
242  int64_t pts;
243  int drop;
244 
245  /* drop everything if EOF has already been returned */
246  if (s->eof) {
247  av_frame_free(&frame);
248  return 0;
249  }
250 
251  if (frame->pts != AV_NOPTS_VALUE)
252  pts = av_rescale_q(frame->pts, inlink->time_base,
253  (AVRational){ 1, inlink->sample_rate });
254  else
255  pts = s->next_pts;
256  s->next_pts = pts + frame->nb_samples;
257 
258  /* check if at least a part of the frame is after the start time */
259  if (s->start_sample < 0 && s->start_pts == AV_NOPTS_VALUE) {
260  start_sample = 0;
261  } else {
262  drop = 1;
263  start_sample = frame->nb_samples;
264 
265  if (s->start_sample >= 0 &&
266  s->nb_samples + frame->nb_samples > s->start_sample) {
267  drop = 0;
268  start_sample = FFMIN(start_sample, s->start_sample - s->nb_samples);
269  }
270 
271  if (s->start_pts != AV_NOPTS_VALUE && pts != AV_NOPTS_VALUE &&
272  pts + frame->nb_samples > s->start_pts) {
273  drop = 0;
274  start_sample = FFMIN(start_sample, s->start_pts - pts);
275  }
276 
277  if (drop)
278  goto drop;
279  }
280 
281  if (s->first_pts == AV_NOPTS_VALUE)
282  s->first_pts = pts + start_sample;
283 
284  /* check if at least a part of the frame is before the end time */
285  if (s->end_sample == INT64_MAX && s->end_pts == AV_NOPTS_VALUE && !s->duration_tb) {
286  end_sample = frame->nb_samples;
287  } else {
288  drop = 1;
289  end_sample = 0;
290 
291  if (s->end_sample != INT64_MAX &&
292  s->nb_samples < s->end_sample) {
293  drop = 0;
294  end_sample = FFMAX(end_sample, s->end_sample - s->nb_samples);
295  }
296 
297  if (s->end_pts != AV_NOPTS_VALUE && pts != AV_NOPTS_VALUE &&
298  pts < s->end_pts) {
299  drop = 0;
300  end_sample = FFMAX(end_sample, s->end_pts - pts);
301  }
302 
303  if (s->duration_tb && pts - s->first_pts < s->duration_tb) {
304  drop = 0;
305  end_sample = FFMAX(end_sample, s->first_pts + s->duration_tb - pts);
306  }
307 
308  if (drop) {
309  s->eof = 1;
311  goto drop;
312  }
313  }
314 
315  s->nb_samples += frame->nb_samples;
316  start_sample = FFMAX(0, start_sample);
317  end_sample = FFMIN(frame->nb_samples, end_sample);
318  av_assert0(start_sample < end_sample || (start_sample == end_sample && !frame->nb_samples));
319 
320  if (start_sample) {
321  AVFrame *out = ff_get_audio_buffer(ctx->outputs[0], end_sample - start_sample);
322  if (!out) {
323  av_frame_free(&frame);
324  return AVERROR(ENOMEM);
325  }
326 
327  av_frame_copy_props(out, frame);
328  av_samples_copy(out->extended_data, frame->extended_data, 0, start_sample,
329  out->nb_samples, inlink->channels,
330  frame->format);
331  if (out->pts != AV_NOPTS_VALUE)
332  out->pts += av_rescale_q(start_sample, (AVRational){ 1, out->sample_rate },
333  inlink->time_base);
334 
335  av_frame_free(&frame);
336  frame = out;
337  } else
338  frame->nb_samples = end_sample;
339 
340  return ff_filter_frame(ctx->outputs[0], frame);
341 
342 drop:
343  s->nb_samples += frame->nb_samples;
344  av_frame_free(&frame);
345  return 0;
346 }
347 
348 #define FLAGS AV_OPT_FLAG_AUDIO_PARAM | AV_OPT_FLAG_FILTERING_PARAM
349 static const AVOption atrim_options[] = {
351  { "start_sample", "Number of the first audio sample that should be "
352  "passed to the output", OFFSET(start_sample), AV_OPT_TYPE_INT64, { .i64 = -1 }, -1, INT64_MAX, FLAGS },
353  { "end_sample", "Number of the first audio sample that should be "
354  "dropped again", OFFSET(end_sample), AV_OPT_TYPE_INT64, { .i64 = INT64_MAX }, 0, INT64_MAX, FLAGS },
356  { NULL }
357 };
358 #undef FLAGS
359 
360 AVFILTER_DEFINE_CLASS(atrim);
361 
362 static const AVFilterPad atrim_inputs[] = {
363  {
364  .name = "default",
365  .type = AVMEDIA_TYPE_AUDIO,
366  .filter_frame = atrim_filter_frame,
367  .config_props = config_input,
368  },
369  { NULL }
370 };
371 
372 static const AVFilterPad atrim_outputs[] = {
373  {
374  .name = "default",
375  .type = AVMEDIA_TYPE_AUDIO,
376  },
377  { NULL }
378 };
379 
380 AVFilter ff_af_atrim = {
381  .name = "atrim",
382  .description = NULL_IF_CONFIG_SMALL("Pick one continuous section from the input, drop the rest."),
383  .init = init,
384  .query_formats = ff_query_formats_all,
385  .priv_size = sizeof(TrimContext),
386  .priv_class = &atrim_class,
387  .inputs = atrim_inputs,
388  .outputs = atrim_outputs,
389 };
390 #endif // CONFIG_ATRIM_FILTER
#define NULL
Definition: coverity.c:32
const char * s
Definition: avisynth_c.h:631
This structure describes decoded (raw) audio or video data.
Definition: frame.h:181
#define FLAGS
Definition: cmdutils.c:526
AVOption.
Definition: opt.h:245
AVFormatContext * ctx
Definition: movenc-test.c:48
int64_t duration
Definition: trim.c:43
Main libavfilter public API header.
int64_t end_pts
Definition: trim.c:53
#define OFFSET(x)
Definition: trim.c:117
static av_cold int init(AVFilterContext *ctx)
Definition: trim.c:78
int64_t start_sample
Definition: trim.c:54
int64_t duration_tb
Definition: trim.c:71
int64_t start_time
Definition: trim.c:44
const char * name
Pad name.
Definition: internal.h:59
#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:1163
#define av_cold
Definition: attributes.h:82
AVOptions.
int64_t end_sample
Definition: trim.c:54
int64_t next_pts
Definition: trim.c:73
int64_t pts
Presentation timestamp in time_base units (time when frame should be shown to user).
Definition: frame.h:262
double duration_dbl
Definition: trim.c:47
static AVFrame * frame
#define AVERROR_EOF
End of file.
Definition: error.h:55
#define COMPAT_OPTS
Definition: trim.c:129
static int config_input(AVFilterLink *inlink)
Definition: trim.c:87
int64_t start_frame
Definition: trim.c:45
A filter pad used for either input or output.
Definition: internal.h:53
int64_t av_rescale_q(int64_t a, AVRational bq, AVRational cq)
Rescale a 64-bit integer by 2 rational numbers.
Definition: mathematics.c:142
double start_time_dbl
Definition: trim.c:48
AVFrame * ff_get_audio_buffer(AVFilterLink *link, int nb_samples)
Request an audio samples buffer with a specific set of permissions.
Definition: audio.c:65
#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:154
#define NULL_IF_CONFIG_SMALL(x)
Return NULL if CONFIG_SMALL is true, otherwise the argument without modification. ...
Definition: internal.h:176
void * priv
private data for use by the filter
Definition: avfilter.h:319
simple assert() macros that are a bit more flexible than ISO C assert().
#define FFMAX(a, b)
Definition: common.h:94
audio channel layout utility functions
#define FFMIN(a, b)
Definition: common.h:96
void ff_avfilter_link_set_out_status(AVFilterLink *link, int status, int64_t pts)
Set the status field of a link from the destination filter.
Definition: avfilter.c:186
static const AVFilterPad outputs[]
Definition: af_afftfilt.c:385
int eof
Definition: trim.c:75
FILE * out
Definition: movenc-test.c:54
int format
format of the frame, -1 if unknown or unset Values correspond to enum AVPixelFormat for video frames...
Definition: frame.h:242
static const AVFilterPad inputs[]
Definition: af_afftfilt.c:375
double end_time_dbl
Definition: trim.c:48
int64_t nb_frames
Definition: trim.c:59
int64_t first_pts
Definition: trim.c:67
#define AV_TIME_BASE_Q
Internal time base represented as fractional value.
Definition: avutil.h:252
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:211
Describe the class of an AVClass context structure.
Definition: log.h:67
int sample_rate
Sample rate of the audio data.
Definition: frame.h:338
Filter definition.
Definition: avfilter.h:141
int ff_query_formats_all(AVFilterContext *ctx)
Set the formats list to all existing formats.
Definition: formats.c:602
rational number numerator/denominator
Definition: rational.h:43
int64_t nb_samples
Definition: trim.c:63
#define COMMON_OPTS
Definition: trim.c:118
const char * name
Filter name.
Definition: avfilter.h:145
AVFilterLink ** outputs
array of pointers to output links
Definition: avfilter.h:316
int64_t start_pts
Definition: trim.c:53
static int64_t pts
Global timestamp for the audio frames.
common internal and external API header
int64_t end_frame
Definition: trim.c:45
int64_t end_time
Definition: trim.c:44
#define AVFILTER_DEFINE_CLASS(fname)
Definition: internal.h:339
An instance of a filter.
Definition: avfilter.h:304
internal API functions
uint8_t ** extended_data
pointers to the data planes/channels.
Definition: frame.h:225
int nb_samples
number of audio samples (per channel) described by this frame
Definition: frame.h:235
int av_frame_copy_props(AVFrame *dst, const AVFrame *src)
Copy only "metadata" fields from src to dst.
Definition: frame.c:565
#define AV_NOPTS_VALUE
Undefined timestamp value.
Definition: avutil.h:240
#define tb
Definition: regdef.h:68