FFmpeg
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 <stdint.h>
20 
21 #include "config.h"
22 #include "config_components.h"
23 
25 #include "libavutil/common.h"
26 #include "libavutil/log.h"
27 #include "libavutil/mathematics.h"
28 #include "libavutil/opt.h"
29 #include "libavutil/samplefmt.h"
30 
31 #include "audio.h"
32 #include "avfilter.h"
33 #include "internal.h"
34 
35 typedef struct TrimContext {
36  const AVClass *class;
37 
38  /*
39  * AVOptions
40  */
41  int64_t duration;
42  int64_t start_time, end_time;
44  /*
45  * in the link timebase for video,
46  * in 1/samplerate for audio
47  */
48  int64_t start_pts, end_pts;
50 
51  /*
52  * number of video frames that arrived on this filter so far
53  */
54  int64_t nb_frames;
55  /*
56  * number of audio samples that arrived on this filter so far
57  */
58  int64_t nb_samples;
59  /*
60  * timestamp of the first frame in the output, in the timebase units
61  */
62  int64_t first_pts;
63  /*
64  * duration in the timebase units
65  */
66  int64_t duration_tb;
67 
68  int64_t next_pts;
69 
70  int eof;
71 } TrimContext;
72 
74 {
75  TrimContext *s = ctx->priv;
76 
77  s->first_pts = AV_NOPTS_VALUE;
78 
79  return 0;
80 }
81 
83 {
84  AVFilterContext *ctx = inlink->dst;
85  TrimContext *s = ctx->priv;
86  AVRational tb = (inlink->type == AVMEDIA_TYPE_VIDEO) ?
87  inlink->time_base : (AVRational){ 1, inlink->sample_rate };
88 
89  if (s->start_time != INT64_MAX) {
90  int64_t start_pts = av_rescale_q(s->start_time, AV_TIME_BASE_Q, tb);
91  if (s->start_pts == AV_NOPTS_VALUE || start_pts < s->start_pts)
92  s->start_pts = start_pts;
93  }
94  if (s->end_time != INT64_MAX) {
95  int64_t end_pts = av_rescale_q(s->end_time, AV_TIME_BASE_Q, tb);
96  if (s->end_pts == AV_NOPTS_VALUE || end_pts > s->end_pts)
97  s->end_pts = end_pts;
98  }
99  if (s->duration)
100  s->duration_tb = av_rescale_q(s->duration, AV_TIME_BASE_Q, tb);
101 
102  return 0;
103 }
104 
105 #define OFFSET(x) offsetof(TrimContext, x)
106 #define COMMON_OPTS \
107  { "start", "Timestamp of the first frame that " \
108  "should be passed", OFFSET(start_time), AV_OPT_TYPE_DURATION, { .i64 = INT64_MAX }, INT64_MIN, INT64_MAX, FLAGS }, \
109  { "starti", "Timestamp of the first frame that " \
110  "should be passed", OFFSET(start_time), AV_OPT_TYPE_DURATION, { .i64 = INT64_MAX }, INT64_MIN, INT64_MAX, FLAGS }, \
111  { "end", "Timestamp of the first frame that " \
112  "should be dropped again", OFFSET(end_time), AV_OPT_TYPE_DURATION, { .i64 = INT64_MAX }, INT64_MIN, INT64_MAX, FLAGS }, \
113  { "endi", "Timestamp of the first frame that " \
114  "should be dropped again", OFFSET(end_time), AV_OPT_TYPE_DURATION, { .i64 = INT64_MAX }, INT64_MIN, INT64_MAX, FLAGS }, \
115  { "start_pts", "Timestamp of the first frame that should be " \
116  " passed", OFFSET(start_pts), AV_OPT_TYPE_INT64, { .i64 = AV_NOPTS_VALUE }, INT64_MIN, INT64_MAX, FLAGS }, \
117  { "end_pts", "Timestamp of the first frame that should be " \
118  "dropped again", OFFSET(end_pts), AV_OPT_TYPE_INT64, { .i64 = AV_NOPTS_VALUE }, INT64_MIN, INT64_MAX, FLAGS }, \
119  { "duration", "Maximum duration of the output", OFFSET(duration), AV_OPT_TYPE_DURATION, { .i64 = 0 }, 0, INT64_MAX, FLAGS }, \
120  { "durationi", "Maximum duration of the output", OFFSET(duration), AV_OPT_TYPE_DURATION, { .i64 = 0 }, 0, INT64_MAX, FLAGS },
121 
122 
123 #if CONFIG_TRIM_FILTER
124 static int trim_filter_frame(AVFilterLink *inlink, AVFrame *frame)
125 {
126  AVFilterContext *ctx = inlink->dst;
127  TrimContext *s = ctx->priv;
128  int drop;
129 
130  /* drop everything if EOF has already been returned */
131  if (s->eof) {
133  return 0;
134  }
135 
136  if (s->start_frame >= 0 || s->start_pts != AV_NOPTS_VALUE) {
137  drop = 1;
138  if (s->start_frame >= 0 && s->nb_frames >= s->start_frame)
139  drop = 0;
140  if (s->start_pts != AV_NOPTS_VALUE && frame->pts != AV_NOPTS_VALUE &&
141  frame->pts >= s->start_pts)
142  drop = 0;
143  if (drop)
144  goto drop;
145  }
146 
147  if (s->first_pts == AV_NOPTS_VALUE && frame->pts != AV_NOPTS_VALUE)
148  s->first_pts = frame->pts;
149 
150  if (s->end_frame != INT64_MAX || s->end_pts != AV_NOPTS_VALUE || s->duration_tb) {
151  drop = 1;
152 
153  if (s->end_frame != INT64_MAX && s->nb_frames < s->end_frame)
154  drop = 0;
155  if (s->end_pts != AV_NOPTS_VALUE && frame->pts != AV_NOPTS_VALUE &&
156  frame->pts < s->end_pts)
157  drop = 0;
158  if (s->duration_tb && frame->pts != AV_NOPTS_VALUE &&
159  frame->pts - s->first_pts < s->duration_tb)
160  drop = 0;
161 
162  if (drop) {
163  s->eof = 1;
165  goto drop;
166  }
167  }
168 
169  s->nb_frames++;
170 
171  return ff_filter_frame(ctx->outputs[0], frame);
172 
173 drop:
174  s->nb_frames++;
176  return 0;
177 }
178 
179 #define FLAGS AV_OPT_FLAG_VIDEO_PARAM | AV_OPT_FLAG_FILTERING_PARAM
180 static const AVOption trim_options[] = {
182  { "start_frame", "Number of the first frame that should be passed "
183  "to the output", OFFSET(start_frame), AV_OPT_TYPE_INT64, { .i64 = -1 }, -1, INT64_MAX, FLAGS },
184  { "end_frame", "Number of the first frame that should be dropped "
185  "again", OFFSET(end_frame), AV_OPT_TYPE_INT64, { .i64 = INT64_MAX }, 0, INT64_MAX, FLAGS },
186  { NULL }
187 };
188 #undef FLAGS
189 
191 
192 static const AVFilterPad trim_inputs[] = {
193  {
194  .name = "default",
195  .type = AVMEDIA_TYPE_VIDEO,
196  .filter_frame = trim_filter_frame,
197  .config_props = config_input,
198  },
199 };
200 
201 static const AVFilterPad trim_outputs[] = {
202  {
203  .name = "default",
204  .type = AVMEDIA_TYPE_VIDEO,
205  },
206 };
207 
208 const AVFilter ff_vf_trim = {
209  .name = "trim",
210  .description = NULL_IF_CONFIG_SMALL("Pick one continuous section from the input, drop the rest."),
211  .init = init,
212  .priv_size = sizeof(TrimContext),
213  .priv_class = &trim_class,
214  FILTER_INPUTS(trim_inputs),
215  FILTER_OUTPUTS(trim_outputs),
216 };
217 #endif // CONFIG_TRIM_FILTER
218 
219 #if CONFIG_ATRIM_FILTER
220 static int atrim_filter_frame(AVFilterLink *inlink, AVFrame *frame)
221 {
222  AVFilterContext *ctx = inlink->dst;
223  TrimContext *s = ctx->priv;
224  int64_t start_sample, end_sample;
225  int64_t pts;
226  int drop;
227 
228  /* drop everything if EOF has already been returned */
229  if (s->eof) {
231  return 0;
232  }
233 
234  if (frame->pts != AV_NOPTS_VALUE)
235  pts = av_rescale_q(frame->pts, inlink->time_base,
236  (AVRational){ 1, inlink->sample_rate });
237  else
238  pts = s->next_pts;
239  s->next_pts = pts + frame->nb_samples;
240 
241  /* check if at least a part of the frame is after the start time */
242  if (s->start_sample < 0 && s->start_pts == AV_NOPTS_VALUE) {
243  start_sample = 0;
244  } else {
245  drop = 1;
246  start_sample = frame->nb_samples;
247 
248  if (s->start_sample >= 0 &&
249  s->nb_samples + frame->nb_samples > s->start_sample) {
250  drop = 0;
251  start_sample = FFMIN(start_sample, s->start_sample - s->nb_samples);
252  }
253 
254  if (s->start_pts != AV_NOPTS_VALUE && pts != AV_NOPTS_VALUE &&
255  pts + frame->nb_samples > s->start_pts) {
256  drop = 0;
257  start_sample = FFMIN(start_sample, s->start_pts - pts);
258  }
259 
260  if (drop)
261  goto drop;
262  }
263 
264  if (s->first_pts == AV_NOPTS_VALUE)
265  s->first_pts = pts + start_sample;
266 
267  /* check if at least a part of the frame is before the end time */
268  if (s->end_sample == INT64_MAX && s->end_pts == AV_NOPTS_VALUE && !s->duration_tb) {
269  end_sample = frame->nb_samples;
270  } else {
271  drop = 1;
272  end_sample = 0;
273 
274  if (s->end_sample != INT64_MAX &&
275  s->nb_samples < s->end_sample) {
276  drop = 0;
277  end_sample = FFMAX(end_sample, s->end_sample - s->nb_samples);
278  }
279 
280  if (s->end_pts != AV_NOPTS_VALUE && pts != AV_NOPTS_VALUE &&
281  pts < s->end_pts) {
282  drop = 0;
283  end_sample = FFMAX(end_sample, s->end_pts - pts);
284  }
285 
286  if (s->duration_tb && pts - s->first_pts < s->duration_tb) {
287  drop = 0;
288  end_sample = FFMAX(end_sample, s->first_pts + s->duration_tb - pts);
289  }
290 
291  if (drop) {
292  s->eof = 1;
294  goto drop;
295  }
296  }
297 
298  s->nb_samples += frame->nb_samples;
299  start_sample = FFMAX(0, start_sample);
300  end_sample = FFMIN(frame->nb_samples, end_sample);
301  if (start_sample >= end_sample || !frame->nb_samples)
302  goto drop;
303 
304  if (start_sample) {
305  AVFrame *out = ff_get_audio_buffer(ctx->outputs[0], end_sample - start_sample);
306  if (!out) {
308  return AVERROR(ENOMEM);
309  }
310 
312  av_samples_copy(out->extended_data, frame->extended_data, 0, start_sample,
313  out->nb_samples, inlink->ch_layout.nb_channels,
314  frame->format);
315  if (out->pts != AV_NOPTS_VALUE)
316  out->pts += av_rescale_q(start_sample, (AVRational){ 1, out->sample_rate },
317  inlink->time_base);
318 
320  frame = out;
321  } else
322  frame->nb_samples = end_sample;
323 
324  return ff_filter_frame(ctx->outputs[0], frame);
325 
326 drop:
327  s->nb_samples += frame->nb_samples;
329  return 0;
330 }
331 
332 #define FLAGS AV_OPT_FLAG_AUDIO_PARAM | AV_OPT_FLAG_FILTERING_PARAM
333 static const AVOption atrim_options[] = {
335  { "start_sample", "Number of the first audio sample that should be "
336  "passed to the output", OFFSET(start_sample), AV_OPT_TYPE_INT64, { .i64 = -1 }, -1, INT64_MAX, FLAGS },
337  { "end_sample", "Number of the first audio sample that should be "
338  "dropped again", OFFSET(end_sample), AV_OPT_TYPE_INT64, { .i64 = INT64_MAX }, 0, INT64_MAX, FLAGS },
339  { NULL }
340 };
341 #undef FLAGS
342 
343 AVFILTER_DEFINE_CLASS(atrim);
344 
345 static const AVFilterPad atrim_inputs[] = {
346  {
347  .name = "default",
348  .type = AVMEDIA_TYPE_AUDIO,
349  .filter_frame = atrim_filter_frame,
350  .config_props = config_input,
351  },
352 };
353 
354 static const AVFilterPad atrim_outputs[] = {
355  {
356  .name = "default",
357  .type = AVMEDIA_TYPE_AUDIO,
358  },
359 };
360 
361 const AVFilter ff_af_atrim = {
362  .name = "atrim",
363  .description = NULL_IF_CONFIG_SMALL("Pick one continuous section from the input, drop the rest."),
364  .init = init,
365  .priv_size = sizeof(TrimContext),
366  .priv_class = &atrim_class,
368  FILTER_INPUTS(atrim_inputs),
369  FILTER_OUTPUTS(atrim_outputs),
370 };
371 #endif // CONFIG_ATRIM_FILTER
ff_get_audio_buffer
AVFrame * ff_get_audio_buffer(AVFilterLink *link, int nb_samples)
Request an audio samples buffer with a specific set of permissions.
Definition: audio.c:100
TrimContext::eof
int eof
Definition: trim.c:70
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
ff_filter_frame
int ff_filter_frame(AVFilterLink *link, AVFrame *frame)
Send a frame of data to the next filter.
Definition: avfilter.c:969
AVERROR_EOF
#define AVERROR_EOF
End of file.
Definition: error.h:57
AV_TIME_BASE_Q
#define AV_TIME_BASE_Q
Internal time base represented as fractional value.
Definition: avutil.h:260
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
av_frame_free
void av_frame_free(AVFrame **frame)
Free the frame and any dynamically allocated objects in it, e.g.
Definition: frame.c:99
AVFrame
This structure describes decoded (raw) audio or video data.
Definition: frame.h:330
TrimContext::duration_tb
int64_t duration_tb
Definition: trim.c:66
AVOption
AVOption.
Definition: opt.h:251
TrimContext::nb_samples
int64_t nb_samples
Definition: trim.c:58
TrimContext::start_sample
int64_t start_sample
Definition: trim.c:49
FLAGS
#define FLAGS
Definition: cmdutils.c:515
mathematics.h
FFMAX
#define FFMAX(a, b)
Definition: macros.h:47
AVFilter::name
const char * name
Filter name.
Definition: avfilter.h:165
samplefmt.h
pts
static int64_t pts
Definition: transcode_aac.c:653
TrimContext::start_pts
int64_t start_pts
Definition: trim.c:48
AVFilterPad
A filter pad used for either input or output.
Definition: internal.h:49
av_cold
#define av_cold
Definition: attributes.h:90
COMMON_OPTS
#define COMMON_OPTS
Definition: trim.c:106
ff_avfilter_link_set_out_status
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:235
s
#define s(width, name)
Definition: cbs_vp9.c:256
AVMEDIA_TYPE_AUDIO
@ AVMEDIA_TYPE_AUDIO
Definition: avutil.h:202
init
static av_cold int init(AVFilterContext *ctx)
Definition: trim.c:73
AV_OPT_TYPE_INT64
@ AV_OPT_TYPE_INT64
Definition: opt.h:226
ctx
AVFormatContext * ctx
Definition: movenc.c:48
av_rescale_q
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
OFFSET
#define OFFSET(x)
Definition: trim.c:105
FILTER_INPUTS
#define FILTER_INPUTS(array)
Definition: internal.h:194
ff_af_atrim
const AVFilter ff_af_atrim
AVClass
Describe the class of an AVClass context structure.
Definition: log.h:66
NULL
#define NULL
Definition: coverity.c:32
av_frame_copy_props
int av_frame_copy_props(AVFrame *dst, const AVFrame *src)
Copy only "metadata" fields from src to dst.
Definition: frame.c:594
AVRational
Rational number (pair of numerator and denominator).
Definition: rational.h:58
TrimContext::duration
int64_t duration
Definition: trim.c:41
TrimContext::next_pts
int64_t next_pts
Definition: trim.c:68
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:115
AV_NOPTS_VALUE
#define AV_NOPTS_VALUE
Undefined timestamp value.
Definition: avutil.h:248
TrimContext::end_frame
int64_t end_frame
Definition: trim.c:43
internal.h
AVFILTER_DEFINE_CLASS
#define AVFILTER_DEFINE_CLASS(fname)
Definition: internal.h:329
av_samples_copy
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:222
log.h
common.h
FFMIN
#define FFMIN(a, b)
Definition: macros.h:49
TrimContext::end_time
int64_t end_time
Definition: trim.c:42
tb
#define tb
Definition: regdef.h:68
AVFilterPad::name
const char * name
Pad name.
Definition: internal.h:55
config_input
static int config_input(AVFilterLink *inlink)
Definition: trim.c:82
ff_vf_trim
const AVFilter ff_vf_trim
AVFilter
Filter definition.
Definition: avfilter.h:161
frame
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 the filter must be ready for frames arriving randomly on any input any filter with several inputs will most likely require some kind of queuing mechanism It is perfectly acceptable to have a limited queue and to drop frames when the inputs are too unbalanced request_frame For filters that do not use the this method is called when a frame is wanted on an output For a it should directly call filter_frame on the corresponding output For a if there are queued frames already one of these frames should be pushed If the filter should request a frame on one of its repeatedly until at least one frame has been pushed Return or at least make progress towards producing a frame
Definition: filter_design.txt:264
TrimContext::end_sample
int64_t end_sample
Definition: trim.c:49
TrimContext::start_frame
int64_t start_frame
Definition: trim.c:43
TrimContext::nb_frames
int64_t nb_frames
Definition: trim.c:54
TrimContext::start_time
int64_t start_time
Definition: trim.c:42
channel_layout.h
avfilter.h
AVFILTER_FLAG_METADATA_ONLY
#define AVFILTER_FLAG_METADATA_ONLY
The filter is a "metadata" filter - it does not modify the frame data in any way.
Definition: avfilter.h:133
AVFilterContext
An instance of a filter.
Definition: avfilter.h:392
AVMEDIA_TYPE_VIDEO
@ AVMEDIA_TYPE_VIDEO
Definition: avutil.h:201
audio.h
FILTER_OUTPUTS
#define FILTER_OUTPUTS(array)
Definition: internal.h:195
TrimContext
Definition: trim.c:35
flags
#define flags(name, subs,...)
Definition: cbs_av1.c:561
TrimContext::first_pts
int64_t first_pts
Definition: trim.c:62
TrimContext::end_pts
int64_t end_pts
Definition: trim.c:48