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 
22 #include "config.h"
23 
24 #include "libavutil/avassert.h"
26 #include "libavutil/common.h"
27 #include "libavutil/log.h"
28 #include "libavutil/mathematics.h"
29 #include "libavutil/opt.h"
30 #include "libavutil/samplefmt.h"
31 
32 #include "audio.h"
33 #include "avfilter.h"
34 #include "internal.h"
35 
36 typedef struct TrimContext {
37  const AVClass *class;
38 
39  /*
40  * AVOptions
41  */
42  double duration;
45  /*
46  * in the link timebase for video,
47  * in 1/samplerate for audio
48  */
49  int64_t start_pts, end_pts;
51 
52  /*
53  * number of video frames that arrived on this filter so far
54  */
55  int64_t nb_frames;
56  /*
57  * number of audio samples that arrived on this filter so far
58  */
59  int64_t nb_samples;
60  /*
61  * timestamp of the first frame in the output, in the timebase units
62  */
63  int64_t first_pts;
64  /*
65  * duration in the timebase units
66  */
67  int64_t duration_tb;
68 
69  int64_t next_pts;
70 
71  int eof;
72 } TrimContext;
73 
74 static int init(AVFilterContext *ctx)
75 {
76  TrimContext *s = ctx->priv;
77 
79 
80  return 0;
81 }
82 
83 static int config_input(AVFilterLink *inlink)
84 {
85  AVFilterContext *ctx = inlink->dst;
86  TrimContext *s = ctx->priv;
87  AVRational tb = (inlink->type == AVMEDIA_TYPE_VIDEO) ?
88  inlink->time_base : (AVRational){ 1, inlink->sample_rate };
89 
90  if (s->start_time != DBL_MAX) {
91  int64_t start_pts = lrint(s->start_time / av_q2d(tb));
92  if (s->start_pts == AV_NOPTS_VALUE || start_pts < s->start_pts)
93  s->start_pts = start_pts;
94  }
95  if (s->end_time != DBL_MAX) {
96  int64_t end_pts = lrint(s->end_time / av_q2d(tb));
97  if (s->end_pts == AV_NOPTS_VALUE || end_pts > s->end_pts)
98  s->end_pts = end_pts;
99  }
100  if (s->duration)
101  s->duration_tb = lrint(s->duration / av_q2d(tb));
102 
103  return 0;
104 }
105 
106 static int config_output(AVFilterLink *outlink)
107 {
108  outlink->flags |= FF_LINK_FLAG_REQUEST_LOOP;
109  return 0;
110 }
111 
112 #define OFFSET(x) offsetof(TrimContext, x)
113 #define COMMON_OPTS \
114  { "start", "Timestamp in seconds of the first frame that " \
115  "should be passed", OFFSET(start_time), AV_OPT_TYPE_DOUBLE, { .dbl = DBL_MAX }, -DBL_MAX, DBL_MAX, FLAGS }, \
116  { "end", "Timestamp in seconds of the first frame that " \
117  "should be dropped again", OFFSET(end_time), AV_OPT_TYPE_DOUBLE, { .dbl = DBL_MAX }, -DBL_MAX, DBL_MAX, FLAGS }, \
118  { "start_pts", "Timestamp of the first frame that should be " \
119  " passed", OFFSET(start_pts), AV_OPT_TYPE_INT64, { .i64 = AV_NOPTS_VALUE }, INT64_MIN, INT64_MAX, FLAGS }, \
120  { "end_pts", "Timestamp of the first frame that should be " \
121  "dropped again", OFFSET(end_pts), AV_OPT_TYPE_INT64, { .i64 = AV_NOPTS_VALUE }, INT64_MIN, INT64_MAX, FLAGS }, \
122  { "duration", "Maximum duration of the output in seconds", OFFSET(duration), AV_OPT_TYPE_DOUBLE, { .dbl = 0 }, 0, DBL_MAX, FLAGS },
123 
124 
125 #if CONFIG_TRIM_FILTER
126 static int trim_filter_frame(AVFilterLink *inlink, AVFrame *frame)
127 {
128  AVFilterContext *ctx = inlink->dst;
129  TrimContext *s = ctx->priv;
130  int drop;
131 
132  /* drop everything if EOF has already been returned */
133  if (s->eof) {
134  av_frame_free(&frame);
135  return 0;
136  }
137 
138  if (s->start_frame >= 0 || s->start_pts != AV_NOPTS_VALUE) {
139  drop = 1;
140  if (s->start_frame >= 0 && s->nb_frames >= s->start_frame)
141  drop = 0;
142  if (s->start_pts != AV_NOPTS_VALUE && frame->pts != AV_NOPTS_VALUE &&
143  frame->pts >= s->start_pts)
144  drop = 0;
145  if (drop)
146  goto drop;
147  }
148 
149  if (s->first_pts == AV_NOPTS_VALUE && frame->pts != AV_NOPTS_VALUE)
150  s->first_pts = frame->pts;
151 
152  if (s->end_frame != INT64_MAX || s->end_pts != AV_NOPTS_VALUE || s->duration_tb) {
153  drop = 1;
154 
155  if (s->end_frame != INT64_MAX && s->nb_frames < s->end_frame)
156  drop = 0;
157  if (s->end_pts != AV_NOPTS_VALUE && frame->pts != AV_NOPTS_VALUE &&
158  frame->pts < s->end_pts)
159  drop = 0;
160  if (s->duration_tb && frame->pts != AV_NOPTS_VALUE &&
161  frame->pts - s->first_pts < s->duration_tb)
162  drop = 0;
163 
164  if (drop) {
165  s->eof = inlink->closed = 1;
166  goto drop;
167  }
168  }
169 
170  s->nb_frames++;
171 
172  return ff_filter_frame(ctx->outputs[0], frame);
173 
174 drop:
175  s->nb_frames++;
176  av_frame_free(&frame);
177  return 0;
178 }
179 
180 #define FLAGS AV_OPT_FLAG_VIDEO_PARAM | AV_OPT_FLAG_FILTERING_PARAM
181 static const AVOption trim_options[] = {
183  { "start_frame", "Number of the first frame that should be passed "
184  "to the output", OFFSET(start_frame), AV_OPT_TYPE_INT64, { .i64 = -1 }, -1, INT64_MAX, FLAGS },
185  { "end_frame", "Number of the first frame that should be dropped "
186  "again", OFFSET(end_frame), AV_OPT_TYPE_INT64, { .i64 = INT64_MAX }, 0, INT64_MAX, FLAGS },
187  { NULL },
188 };
189 #undef FLAGS
190 
192 
193 static const AVFilterPad trim_inputs[] = {
194  {
195  .name = "default",
196  .type = AVMEDIA_TYPE_VIDEO,
197  .filter_frame = trim_filter_frame,
198  .config_props = config_input,
199  },
200  { NULL }
201 };
202 
203 static const AVFilterPad trim_outputs[] = {
204  {
205  .name = "default",
206  .type = AVMEDIA_TYPE_VIDEO,
207  .config_props = config_output,
208  },
209  { NULL }
210 };
211 
212 AVFilter avfilter_vf_trim = {
213  .name = "trim",
214  .description = NULL_IF_CONFIG_SMALL("Pick one continuous section from the input, drop the rest."),
215 
216  .init = init,
217 
218  .priv_size = sizeof(TrimContext),
219  .priv_class = &trim_class,
220 
221  .inputs = trim_inputs,
222  .outputs = trim_outputs,
223 };
224 #endif // CONFIG_TRIM_FILTER
225 
226 #if CONFIG_ATRIM_FILTER
227 static int atrim_filter_frame(AVFilterLink *inlink, AVFrame *frame)
228 {
229  AVFilterContext *ctx = inlink->dst;
230  TrimContext *s = ctx->priv;
231  int64_t start_sample, end_sample = frame->nb_samples;
232  int64_t pts;
233  int drop;
234 
235  /* drop everything if EOF has already been returned */
236  if (s->eof) {
237  av_frame_free(&frame);
238  return 0;
239  }
240 
241  if (frame->pts != AV_NOPTS_VALUE)
242  pts = av_rescale_q(frame->pts, inlink->time_base,
243  (AVRational){ 1, inlink->sample_rate });
244  else
245  pts = s->next_pts;
246  s->next_pts = pts + frame->nb_samples;
247 
248  /* check if at least a part of the frame is after the start time */
249  if (s->start_sample < 0 && s->start_pts == AV_NOPTS_VALUE) {
250  start_sample = 0;
251  } else {
252  drop = 1;
253  start_sample = frame->nb_samples;
254 
255  if (s->start_sample >= 0 &&
256  s->nb_samples + frame->nb_samples > s->start_sample) {
257  drop = 0;
258  start_sample = FFMIN(start_sample, s->start_sample - s->nb_samples);
259  }
260 
261  if (s->start_pts != AV_NOPTS_VALUE && pts != AV_NOPTS_VALUE &&
262  pts + frame->nb_samples > s->start_pts) {
263  drop = 0;
264  start_sample = FFMIN(start_sample, s->start_pts - pts);
265  }
266 
267  if (drop)
268  goto drop;
269  }
270 
271  if (s->first_pts == AV_NOPTS_VALUE)
272  s->first_pts = pts + start_sample;
273 
274  /* check if at least a part of the frame is before the end time */
275  if (s->end_sample == INT64_MAX && s->end_pts == AV_NOPTS_VALUE && !s->duration_tb) {
276  end_sample = frame->nb_samples;
277  } else {
278  drop = 1;
279  end_sample = 0;
280 
281  if (s->end_sample != INT64_MAX &&
282  s->nb_samples < s->end_sample) {
283  drop = 0;
284  end_sample = FFMAX(end_sample, s->end_sample - s->nb_samples);
285  }
286 
287  if (s->end_pts != AV_NOPTS_VALUE && pts != AV_NOPTS_VALUE &&
288  pts < s->end_pts) {
289  drop = 0;
290  end_sample = FFMAX(end_sample, s->end_pts - pts);
291  }
292 
293  if (s->duration_tb && pts - s->first_pts < s->duration_tb) {
294  drop = 0;
295  end_sample = FFMAX(end_sample, s->first_pts + s->duration_tb - pts);
296  }
297 
298  if (drop) {
299  s->eof = inlink->closed = 1;
300  goto drop;
301  }
302  }
303 
304  s->nb_samples += frame->nb_samples;
305  start_sample = FFMAX(0, start_sample);
306  end_sample = FFMIN(frame->nb_samples, end_sample);
307  av_assert0(start_sample < end_sample);
308 
309  if (start_sample) {
310  AVFrame *out = ff_get_audio_buffer(ctx->outputs[0], end_sample - start_sample);
311  if (!out) {
312  av_frame_free(&frame);
313  return AVERROR(ENOMEM);
314  }
315 
316  av_frame_copy_props(out, frame);
317  av_samples_copy(out->extended_data, frame->extended_data, 0, start_sample,
319  frame->format);
320  if (out->pts != AV_NOPTS_VALUE)
321  out->pts += av_rescale_q(start_sample, (AVRational){ 1, out->sample_rate },
322  inlink->time_base);
323 
324  av_frame_free(&frame);
325  frame = out;
326  } else
327  frame->nb_samples = end_sample;
328 
329  return ff_filter_frame(ctx->outputs[0], frame);
330 
331 drop:
332  s->nb_samples += frame->nb_samples;
333  av_frame_free(&frame);
334  return 0;
335 }
336 
337 #define FLAGS AV_OPT_FLAG_AUDIO_PARAM | AV_OPT_FLAG_FILTERING_PARAM
338 static const AVOption atrim_options[] = {
340  { "start_sample", "Number of the first audio sample that should be "
341  "passed to the output", OFFSET(start_sample), AV_OPT_TYPE_INT64, { .i64 = -1 }, -1, INT64_MAX, FLAGS },
342  { "end_sample", "Number of the first audio sample that should be "
343  "dropped again", OFFSET(end_sample), AV_OPT_TYPE_INT64, { .i64 = INT64_MAX }, 0, INT64_MAX, FLAGS },
344  { NULL },
345 };
346 #undef FLAGS
347 
348 AVFILTER_DEFINE_CLASS(atrim);
349 
350 static const AVFilterPad atrim_inputs[] = {
351  {
352  .name = "default",
353  .type = AVMEDIA_TYPE_AUDIO,
354  .filter_frame = atrim_filter_frame,
355  .config_props = config_input,
356  },
357  { NULL }
358 };
359 
360 static const AVFilterPad atrim_outputs[] = {
361  {
362  .name = "default",
363  .type = AVMEDIA_TYPE_AUDIO,
364  .config_props = config_output,
365  },
366  { NULL }
367 };
368 
369 AVFilter avfilter_af_atrim = {
370  .name = "atrim",
371  .description = NULL_IF_CONFIG_SMALL("Pick one continuous section from the input, drop the rest."),
372 
373  .init = init,
374 
375  .priv_size = sizeof(TrimContext),
376  .priv_class = &atrim_class,
377 
378  .inputs = atrim_inputs,
379  .outputs = atrim_outputs,
380 };
381 #endif // CONFIG_ATRIM_FILTER