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 "filters.h"
34 #include "video.h"
35 
36 typedef struct TrimContext {
37  const AVClass *class;
38 
39  /*
40  * AVOptions
41  */
45  /*
46  * in the link timebase for video,
47  * in 1/samplerate for audio
48  */
51 
52  /*
53  * number of video frames that arrived on this filter so far
54  */
56  /*
57  * number of audio samples that arrived on this filter so far
58  */
60  /*
61  * timestamp of the first frame in the output, in the timebase units
62  */
64  /*
65  * duration in the timebase units
66  */
68 
70 
71  int eof;
72 
74 } TrimContext;
75 
77 {
78  TrimContext *s = ctx->priv;
79 
80  s->first_pts = AV_NOPTS_VALUE;
81 
82  return 0;
83 }
84 
85 #if CONFIG_TRIM_FILTER
86 static int trim_filter_frame(AVFilterLink *inlink, AVFrame *frame)
87 {
88  AVFilterContext *ctx = inlink->dst;
89  TrimContext *s = ctx->priv;
90  int drop;
91 
92  /* drop everything if EOF has already been returned */
93  if (s->eof) {
95  return 0;
96  }
97 
98  if (s->start_frame >= 0 || s->start_pts != AV_NOPTS_VALUE) {
99  drop = 1;
100  if (s->start_frame >= 0 && s->nb_frames >= s->start_frame)
101  drop = 0;
102  if (s->start_pts != AV_NOPTS_VALUE && frame->pts != AV_NOPTS_VALUE &&
103  frame->pts >= s->start_pts)
104  drop = 0;
105  if (drop)
106  goto drop;
107  }
108 
109  if (s->first_pts == AV_NOPTS_VALUE && frame->pts != AV_NOPTS_VALUE)
110  s->first_pts = frame->pts;
111 
112  if (s->end_frame != INT64_MAX || s->end_pts != AV_NOPTS_VALUE || s->duration_tb) {
113  drop = 1;
114 
115  if (s->end_frame != INT64_MAX && s->nb_frames < s->end_frame)
116  drop = 0;
117  if (s->end_pts != AV_NOPTS_VALUE && frame->pts != AV_NOPTS_VALUE &&
118  frame->pts < s->end_pts)
119  drop = 0;
120  if (s->duration_tb && frame->pts != AV_NOPTS_VALUE &&
121  frame->pts - s->first_pts < s->duration_tb)
122  drop = 0;
123 
124  if (drop) {
125  s->eof = 1;
127  ff_outlink_set_status(ctx->outputs[0], AVERROR_EOF, frame->pts);
128  goto drop;
129  }
130  }
131 
132  s->nb_frames++;
133 
134  return ff_filter_frame(ctx->outputs[0], frame);
135 
136 drop:
137  if (!s->eof)
138  ff_filter_set_ready(ctx, 100);
139  s->nb_frames++;
141  return 0;
142 }
143 #endif // CONFIG_TRIM_FILTER
144 
145 #if CONFIG_ATRIM_FILTER
146 static int atrim_filter_frame(AVFilterLink *inlink, AVFrame *frame)
147 {
148  AVFilterContext *ctx = inlink->dst;
149  TrimContext *s = ctx->priv;
150  int64_t start_sample, end_sample;
151  int64_t pts;
152  int drop;
153 
154  /* drop everything if EOF has already been returned */
155  if (s->eof) {
157  return 0;
158  }
159 
160  if (frame->pts != AV_NOPTS_VALUE)
161  pts = av_rescale_q(frame->pts, inlink->time_base,
162  (AVRational){ 1, inlink->sample_rate });
163  else
164  pts = s->next_pts;
165  s->next_pts = pts + frame->nb_samples;
166 
167  /* check if at least a part of the frame is after the start time */
168  if (s->start_sample < 0 && s->start_pts == AV_NOPTS_VALUE) {
169  start_sample = 0;
170  } else {
171  drop = 1;
172  start_sample = frame->nb_samples;
173 
174  if (s->start_sample >= 0 &&
175  s->nb_samples + frame->nb_samples > s->start_sample) {
176  drop = 0;
177  start_sample = FFMIN(start_sample, s->start_sample - s->nb_samples);
178  }
179 
180  if (s->start_pts != AV_NOPTS_VALUE && pts != AV_NOPTS_VALUE &&
181  pts + frame->nb_samples > s->start_pts) {
182  drop = 0;
183  start_sample = FFMIN(start_sample, s->start_pts - pts);
184  }
185 
186  if (drop)
187  goto drop;
188  }
189 
190  if (s->first_pts == AV_NOPTS_VALUE)
191  s->first_pts = pts + start_sample;
192 
193  /* check if at least a part of the frame is before the end time */
194  if (s->end_sample == INT64_MAX && s->end_pts == AV_NOPTS_VALUE && !s->duration_tb) {
195  end_sample = frame->nb_samples;
196  } else {
197  drop = 1;
198  end_sample = 0;
199 
200  if (s->end_sample != INT64_MAX &&
201  s->nb_samples < s->end_sample) {
202  drop = 0;
203  end_sample = FFMAX(end_sample, s->end_sample - s->nb_samples);
204  }
205 
206  if (s->end_pts != AV_NOPTS_VALUE && pts != AV_NOPTS_VALUE &&
207  pts < s->end_pts) {
208  drop = 0;
209  end_sample = FFMAX(end_sample, s->end_pts - pts);
210  }
211 
212  if (s->duration_tb && pts - s->first_pts < s->duration_tb) {
213  drop = 0;
214  end_sample = FFMAX(end_sample, s->first_pts + s->duration_tb - pts);
215  }
216 
217  if (drop) {
218  s->eof = 1;
220  ff_outlink_set_status(ctx->outputs[0], AVERROR_EOF, frame->pts);
221  goto drop;
222  }
223  }
224 
225  s->nb_samples += frame->nb_samples;
226  start_sample = FFMAX(0, start_sample);
227  end_sample = FFMIN(frame->nb_samples, end_sample);
228  if (start_sample >= end_sample || !frame->nb_samples)
229  goto drop;
230 
231  if (start_sample) {
232  AVFrame *out = ff_get_audio_buffer(ctx->outputs[0], end_sample - start_sample);
233  if (!out) {
235  return AVERROR(ENOMEM);
236  }
237 
239  av_samples_copy(out->extended_data, frame->extended_data, 0, start_sample,
240  out->nb_samples, inlink->ch_layout.nb_channels,
241  frame->format);
242  if (out->pts != AV_NOPTS_VALUE)
243  out->pts += av_rescale_q(start_sample, (AVRational){ 1, out->sample_rate },
244  inlink->time_base);
245 
247  frame = out;
248  } else
249  frame->nb_samples = end_sample;
250 
251  return ff_filter_frame(ctx->outputs[0], frame);
252 
253 drop:
254  if (!s->eof)
255  ff_filter_set_ready(ctx, 100);
256  s->nb_samples += frame->nb_samples;
258  return 0;
259 }
260 #endif // CONFIG_ATRIM_FILTER
261 
263 {
264  AVFilterContext *ctx = inlink->dst;
265  TrimContext *s = ctx->priv;
266  AVRational tb = (inlink->type == AVMEDIA_TYPE_VIDEO) ?
267  inlink->time_base : (AVRational){ 1, inlink->sample_rate };
268 
269 #if CONFIG_TRIM_FILTER
270  if (inlink->type == AVMEDIA_TYPE_VIDEO)
271  s->filter_frame = trim_filter_frame;
272 #endif
273 #if CONFIG_ATRIM_FILTER
274  if (inlink->type == AVMEDIA_TYPE_AUDIO)
275  s->filter_frame = atrim_filter_frame;
276 #endif
277  if (s->start_time != INT64_MAX) {
278  int64_t start_pts = av_rescale_q(s->start_time, AV_TIME_BASE_Q, tb);
279  if (s->start_pts == AV_NOPTS_VALUE || start_pts < s->start_pts)
280  s->start_pts = start_pts;
281  }
282  if (s->end_time != INT64_MAX) {
283  int64_t end_pts = av_rescale_q(s->end_time, AV_TIME_BASE_Q, tb);
284  if (s->end_pts == AV_NOPTS_VALUE || end_pts > s->end_pts)
285  s->end_pts = end_pts;
286  }
287  if (s->duration)
288  s->duration_tb = av_rescale_q(s->duration, AV_TIME_BASE_Q, tb);
289 
290  return 0;
291 }
292 
294 {
295  TrimContext *s = ctx->priv;
296  AVFilterLink *inlink = ctx->inputs[0];
297  AVFilterLink *outlink = ctx->outputs[0];
298 
300 
301  if (!s->eof && ff_inlink_queued_frames(inlink)) {
302  AVFrame *frame = NULL;
303  int ret;
304 
306  if (ret < 0)
307  return ret;
308  if (ret > 0)
309  return s->filter_frame(inlink, frame);
310  }
311 
314 
315  return FFERROR_NOT_READY;
316 }
317 
318 #define OFFSET(x) offsetof(TrimContext, x)
319 #define COMMON_OPTS \
320  { "start", "Timestamp of the first frame that " \
321  "should be passed", OFFSET(start_time), AV_OPT_TYPE_DURATION, { .i64 = INT64_MAX }, INT64_MIN, INT64_MAX, FLAGS }, \
322  { "starti", "Timestamp of the first frame that " \
323  "should be passed", OFFSET(start_time), AV_OPT_TYPE_DURATION, { .i64 = INT64_MAX }, INT64_MIN, INT64_MAX, FLAGS }, \
324  { "end", "Timestamp of the first frame that " \
325  "should be dropped again", OFFSET(end_time), AV_OPT_TYPE_DURATION, { .i64 = INT64_MAX }, INT64_MIN, INT64_MAX, FLAGS }, \
326  { "endi", "Timestamp of the first frame that " \
327  "should be dropped again", OFFSET(end_time), AV_OPT_TYPE_DURATION, { .i64 = INT64_MAX }, INT64_MIN, INT64_MAX, FLAGS }, \
328  { "start_pts", "Timestamp of the first frame that should be " \
329  " passed", OFFSET(start_pts), AV_OPT_TYPE_INT64, { .i64 = AV_NOPTS_VALUE }, INT64_MIN, INT64_MAX, FLAGS }, \
330  { "end_pts", "Timestamp of the first frame that should be " \
331  "dropped again", OFFSET(end_pts), AV_OPT_TYPE_INT64, { .i64 = AV_NOPTS_VALUE }, INT64_MIN, INT64_MAX, FLAGS }, \
332  { "duration", "Maximum duration of the output", OFFSET(duration), AV_OPT_TYPE_DURATION, { .i64 = 0 }, 0, INT64_MAX, FLAGS }, \
333  { "durationi", "Maximum duration of the output", OFFSET(duration), AV_OPT_TYPE_DURATION, { .i64 = 0 }, 0, INT64_MAX, FLAGS },
334 
335 
336 #if CONFIG_TRIM_FILTER
337 
338 #define FLAGS AV_OPT_FLAG_VIDEO_PARAM | AV_OPT_FLAG_FILTERING_PARAM
339 static const AVOption trim_options[] = {
341  { "start_frame", "Number of the first frame that should be passed "
342  "to the output", OFFSET(start_frame), AV_OPT_TYPE_INT64, { .i64 = -1 }, -1, INT64_MAX, FLAGS },
343  { "end_frame", "Number of the first frame that should be dropped "
344  "again", OFFSET(end_frame), AV_OPT_TYPE_INT64, { .i64 = INT64_MAX }, 0, INT64_MAX, FLAGS },
345  { NULL }
346 };
347 #undef FLAGS
348 
350 
351 static const AVFilterPad trim_inputs[] = {
352  {
353  .name = "default",
354  .type = AVMEDIA_TYPE_VIDEO,
355  .config_props = config_input,
356  },
357 };
358 
359 const AVFilter ff_vf_trim = {
360  .name = "trim",
361  .description = NULL_IF_CONFIG_SMALL("Pick one continuous section from the input, drop the rest."),
362  .init = init,
363  .activate = activate,
364  .priv_size = sizeof(TrimContext),
365  .priv_class = &trim_class,
367  FILTER_INPUTS(trim_inputs),
369 };
370 #endif // CONFIG_TRIM_FILTER
371 
372 #if CONFIG_ATRIM_FILTER
373 
374 #define FLAGS AV_OPT_FLAG_AUDIO_PARAM | AV_OPT_FLAG_FILTERING_PARAM
375 static const AVOption atrim_options[] = {
377  { "start_sample", "Number of the first audio sample that should be "
378  "passed to the output", OFFSET(start_sample), AV_OPT_TYPE_INT64, { .i64 = -1 }, -1, INT64_MAX, FLAGS },
379  { "end_sample", "Number of the first audio sample that should be "
380  "dropped again", OFFSET(end_sample), AV_OPT_TYPE_INT64, { .i64 = INT64_MAX }, 0, INT64_MAX, FLAGS },
381  { NULL }
382 };
383 #undef FLAGS
384 
385 AVFILTER_DEFINE_CLASS(atrim);
386 
387 static const AVFilterPad atrim_inputs[] = {
388  {
389  .name = "default",
390  .type = AVMEDIA_TYPE_AUDIO,
391  .config_props = config_input,
392  },
393 };
394 
395 const AVFilter ff_af_atrim = {
396  .name = "atrim",
397  .description = NULL_IF_CONFIG_SMALL("Pick one continuous section from the input, drop the rest."),
398  .init = init,
399  .activate = activate,
400  .priv_size = sizeof(TrimContext),
401  .priv_class = &atrim_class,
403  FILTER_INPUTS(atrim_inputs),
405 };
406 #endif // CONFIG_ATRIM_FILTER
av_samples_copy
int av_samples_copy(uint8_t *const *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
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:98
TrimContext::eof
int eof
Definition: trim.c:71
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:55
ff_filter_frame
int ff_filter_frame(AVFilterLink *link, AVFrame *frame)
Send a frame of data to the next filter.
Definition: avfilter.c:1061
AVERROR_EOF
#define AVERROR_EOF
End of file.
Definition: error.h:57
FFERROR_NOT_READY
return FFERROR_NOT_READY
Definition: filter_design.txt:204
AV_TIME_BASE_Q
#define AV_TIME_BASE_Q
Internal time base represented as fractional value.
Definition: avutil.h:264
int64_t
long long int64_t
Definition: coverity.c:34
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:162
FILTER_INPUTS
#define FILTER_INPUTS(array)
Definition: filters.h:262
AVFrame
This structure describes decoded (raw) audio or video data.
Definition: frame.h:389
TrimContext::duration_tb
int64_t duration_tb
Definition: trim.c:67
AVOption
AVOption.
Definition: opt.h:429
TrimContext::nb_samples
int64_t nb_samples
Definition: trim.c:59
TrimContext::start_sample
int64_t start_sample
Definition: trim.c:50
FLAGS
#define FLAGS
Definition: cmdutils.c:595
mathematics.h
FFMAX
#define FFMAX(a, b)
Definition: macros.h:47
AVFilter::name
const char * name
Filter name.
Definition: avfilter.h:205
video.h
FF_FILTER_FORWARD_STATUS_BACK
#define FF_FILTER_FORWARD_STATUS_BACK(outlink, inlink)
Forward the status on an output link to an input link.
Definition: filters.h:434
TrimContext::filter_frame
int(* filter_frame)(AVFilterLink *inlink, AVFrame *frame)
Definition: trim.c:73
ff_inlink_consume_frame
int ff_inlink_consume_frame(AVFilterLink *link, AVFrame **rframe)
Take a frame from the link's FIFO and update the link's stats.
Definition: avfilter.c:1490
activate
static int activate(AVFilterContext *ctx)
Definition: trim.c:293
samplefmt.h
pts
static int64_t pts
Definition: transcode_aac.c:644
TrimContext::start_pts
int64_t start_pts
Definition: trim.c:49
AVFilterPad
A filter pad used for either input or output.
Definition: filters.h:38
av_cold
#define av_cold
Definition: attributes.h:90
ff_video_default_filterpad
const AVFilterPad ff_video_default_filterpad[1]
An AVFilterPad array whose only entry has name "default" and is of type AVMEDIA_TYPE_VIDEO.
Definition: video.c:37
COMMON_OPTS
#define COMMON_OPTS
Definition: trim.c:319
ff_outlink_set_status
static void ff_outlink_set_status(AVFilterLink *link, int status, int64_t pts)
Set the status field of a link from the source filter.
Definition: filters.h:424
s
#define s(width, name)
Definition: cbs_vp9.c:198
AVMEDIA_TYPE_AUDIO
@ AVMEDIA_TYPE_AUDIO
Definition: avutil.h:202
init
static av_cold int init(AVFilterContext *ctx)
Definition: trim.c:76
AV_OPT_TYPE_INT64
@ AV_OPT_TYPE_INT64
Underlying C type is int64_t.
Definition: opt.h:263
filters.h
ctx
AVFormatContext * ctx
Definition: movenc.c:49
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:318
FILTER_OUTPUTS
#define FILTER_OUTPUTS(array)
Definition: filters.h:263
ff_af_atrim
const AVFilter ff_af_atrim
AVClass
Describe the class of an AVClass context structure.
Definition: log.h:75
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:725
AVRational
Rational number (pair of numerator and denominator).
Definition: rational.h:58
TrimContext::duration
int64_t duration
Definition: trim.c:42
ff_audio_default_filterpad
const AVFilterPad ff_audio_default_filterpad[1]
An AVFilterPad array whose only entry has name "default" and is of type AVMEDIA_TYPE_AUDIO.
Definition: audio.c:34
AVFILTER_DEFINE_CLASS
#define AVFILTER_DEFINE_CLASS(fname)
Definition: filters.h:273
ff_inlink_queued_frames
size_t ff_inlink_queued_frames(AVFilterLink *link)
Get the number of frames available on the link.
Definition: avfilter.c:1453
TrimContext::next_pts
int64_t next_pts
Definition: trim.c:69
ff_inlink_set_status
void ff_inlink_set_status(AVFilterLink *link, int status)
Set the status on an input link.
Definition: avfilter.c:1602
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:94
AV_NOPTS_VALUE
#define AV_NOPTS_VALUE
Undefined timestamp value.
Definition: avutil.h:248
FF_FILTER_FORWARD_WANTED
FF_FILTER_FORWARD_WANTED(outlink, inlink)
TrimContext::end_frame
int64_t end_frame
Definition: trim.c:44
log.h
common.h
FFMIN
#define FFMIN(a, b)
Definition: macros.h:49
TrimContext::end_time
int64_t end_time
Definition: trim.c:43
AVFilterPad::name
const char * name
Pad name.
Definition: filters.h:44
config_input
static int config_input(AVFilterLink *inlink)
Definition: trim.c:262
ff_vf_trim
const AVFilter ff_vf_trim
AVFilter
Filter definition.
Definition: avfilter.h:201
ret
ret
Definition: filter_design.txt:187
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:50
TrimContext::start_frame
int64_t start_frame
Definition: trim.c:44
TrimContext::nb_frames
int64_t nb_frames
Definition: trim.c:55
TrimContext::start_time
int64_t start_time
Definition: trim.c:43
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:168
AVFilterContext
An instance of a filter.
Definition: avfilter.h:457
AVMEDIA_TYPE_VIDEO
@ AVMEDIA_TYPE_VIDEO
Definition: avutil.h:201
audio.h
FF_FILTER_FORWARD_STATUS
FF_FILTER_FORWARD_STATUS(inlink, outlink)
TrimContext
Definition: trim.c:36
flags
#define flags(name, subs,...)
Definition: cbs_av1.c:482
TrimContext::first_pts
int64_t first_pts
Definition: trim.c:63
TrimContext::end_pts
int64_t end_pts
Definition: trim.c:49
ff_filter_set_ready
void ff_filter_set_ready(AVFilterContext *filter, unsigned priority)
Mark a filter ready and schedule it for activation.
Definition: avfilter.c:239