2 * This file is part of FFmpeg.
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.
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.
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
25 #include "libavutil/avassert.h"
26 #include "libavutil/channel_layout.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"
37 typedef struct TrimContext {
44 int64_t start_time, end_time;
45 int64_t start_frame, end_frame;
48 double start_time_dbl, end_time_dbl;
50 * in the link timebase for video,
51 * in 1/samplerate for audio
53 int64_t start_pts, end_pts;
54 int64_t start_sample, end_sample;
57 * number of video frames that arrived on this filter so far
61 * number of audio samples that arrived on this filter so far
65 * timestamp of the first frame in the output, in the timebase units
69 * duration in the timebase units
78 static av_cold int init(AVFilterContext *ctx)
80 TrimContext *s = ctx->priv;
82 s->first_pts = AV_NOPTS_VALUE;
87 static int config_input(AVFilterLink *inlink)
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 };
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;
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;
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;
112 s->duration_tb = av_rescale_q(s->duration, AV_TIME_BASE_Q, tb);
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 },
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 },
137 #if CONFIG_TRIM_FILTER
138 static int trim_filter_frame(AVFilterLink *inlink, AVFrame *frame)
140 AVFilterContext *ctx = inlink->dst;
141 TrimContext *s = ctx->priv;
144 /* drop everything if EOF has already been returned */
146 av_frame_free(&frame);
150 if (s->start_frame >= 0 || s->start_pts != AV_NOPTS_VALUE) {
152 if (s->start_frame >= 0 && s->nb_frames >= s->start_frame)
154 if (s->start_pts != AV_NOPTS_VALUE && frame->pts != AV_NOPTS_VALUE &&
155 frame->pts >= s->start_pts)
161 if (s->first_pts == AV_NOPTS_VALUE && frame->pts != AV_NOPTS_VALUE)
162 s->first_pts = frame->pts;
164 if (s->end_frame != INT64_MAX || s->end_pts != AV_NOPTS_VALUE || s->duration_tb) {
167 if (s->end_frame != INT64_MAX && s->nb_frames < s->end_frame)
169 if (s->end_pts != AV_NOPTS_VALUE && frame->pts != AV_NOPTS_VALUE &&
170 frame->pts < s->end_pts)
172 if (s->duration_tb && frame->pts != AV_NOPTS_VALUE &&
173 frame->pts - s->first_pts < s->duration_tb)
178 ff_avfilter_link_set_out_status(inlink, AVERROR_EOF, AV_NOPTS_VALUE);
185 return ff_filter_frame(ctx->outputs[0], frame);
189 av_frame_free(&frame);
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 },
205 AVFILTER_DEFINE_CLASS(trim);
207 static const AVFilterPad trim_inputs[] = {
210 .type = AVMEDIA_TYPE_VIDEO,
211 .filter_frame = trim_filter_frame,
212 .config_props = config_input,
217 static const AVFilterPad trim_outputs[] = {
220 .type = AVMEDIA_TYPE_VIDEO,
225 AVFilter ff_vf_trim = {
227 .description = NULL_IF_CONFIG_SMALL("Pick one continuous section from the input, drop the rest."),
229 .priv_size = sizeof(TrimContext),
230 .priv_class = &trim_class,
231 .inputs = trim_inputs,
232 .outputs = trim_outputs,
234 #endif // CONFIG_TRIM_FILTER
236 #if CONFIG_ATRIM_FILTER
237 static int atrim_filter_frame(AVFilterLink *inlink, AVFrame *frame)
239 AVFilterContext *ctx = inlink->dst;
240 TrimContext *s = ctx->priv;
241 int64_t start_sample, end_sample;
245 /* drop everything if EOF has already been returned */
247 av_frame_free(&frame);
251 if (frame->pts != AV_NOPTS_VALUE)
252 pts = av_rescale_q(frame->pts, inlink->time_base,
253 (AVRational){ 1, inlink->sample_rate });
256 s->next_pts = pts + frame->nb_samples;
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) {
263 start_sample = frame->nb_samples;
265 if (s->start_sample >= 0 &&
266 s->nb_samples + frame->nb_samples > s->start_sample) {
268 start_sample = FFMIN(start_sample, s->start_sample - s->nb_samples);
271 if (s->start_pts != AV_NOPTS_VALUE && pts != AV_NOPTS_VALUE &&
272 pts + frame->nb_samples > s->start_pts) {
274 start_sample = FFMIN(start_sample, s->start_pts - pts);
281 if (s->first_pts == AV_NOPTS_VALUE)
282 s->first_pts = pts + start_sample;
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;
291 if (s->end_sample != INT64_MAX &&
292 s->nb_samples < s->end_sample) {
294 end_sample = FFMAX(end_sample, s->end_sample - s->nb_samples);
297 if (s->end_pts != AV_NOPTS_VALUE && pts != AV_NOPTS_VALUE &&
300 end_sample = FFMAX(end_sample, s->end_pts - pts);
303 if (s->duration_tb && pts - s->first_pts < s->duration_tb) {
305 end_sample = FFMAX(end_sample, s->first_pts + s->duration_tb - pts);
310 ff_avfilter_link_set_out_status(inlink, AVERROR_EOF, AV_NOPTS_VALUE);
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));
321 AVFrame *out = ff_get_audio_buffer(ctx->outputs[0], end_sample - start_sample);
323 av_frame_free(&frame);
324 return AVERROR(ENOMEM);
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,
331 if (out->pts != AV_NOPTS_VALUE)
332 out->pts += av_rescale_q(start_sample, (AVRational){ 1, out->sample_rate },
335 av_frame_free(&frame);
338 frame->nb_samples = end_sample;
340 return ff_filter_frame(ctx->outputs[0], frame);
343 s->nb_samples += frame->nb_samples;
344 av_frame_free(&frame);
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 },
360 AVFILTER_DEFINE_CLASS(atrim);
362 static const AVFilterPad atrim_inputs[] = {
365 .type = AVMEDIA_TYPE_AUDIO,
366 .filter_frame = atrim_filter_frame,
367 .config_props = config_input,
372 static const AVFilterPad atrim_outputs[] = {
375 .type = AVMEDIA_TYPE_AUDIO,
380 AVFilter ff_af_atrim = {
382 .description = NULL_IF_CONFIG_SMALL("Pick one continuous section from the input, drop the rest."),
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,
390 #endif // CONFIG_ATRIM_FILTER