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
24 #include "libavutil/avassert.h"
25 #include "libavutil/channel_layout.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"
36 typedef struct TrimContext {
43 double start_time, end_time;
44 int64_t start_frame, end_frame;
46 * in the link timebase for video,
47 * in 1/samplerate for audio
49 int64_t start_pts, end_pts;
50 int64_t start_sample, end_sample;
53 * number of video frames that arrived on this filter so far
57 * number of audio samples that arrived on this filter so far
61 * timestamp of the first frame in the output, in the timebase units
65 * duration in the timebase units
74 static int init(AVFilterContext *ctx)
76 TrimContext *s = ctx->priv;
78 s->first_pts = AV_NOPTS_VALUE;
83 static int config_input(AVFilterLink *inlink)
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 };
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;
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)
101 s->duration_tb = lrint(s->duration / av_q2d(tb));
106 static int config_output(AVFilterLink *outlink)
108 outlink->flags |= FF_LINK_FLAG_REQUEST_LOOP;
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 },
125 #if CONFIG_TRIM_FILTER
126 static int trim_filter_frame(AVFilterLink *inlink, AVFrame *frame)
128 AVFilterContext *ctx = inlink->dst;
129 TrimContext *s = ctx->priv;
132 /* drop everything if EOF has already been returned */
134 av_frame_free(&frame);
138 if (s->start_frame >= 0 || s->start_pts != AV_NOPTS_VALUE) {
140 if (s->start_frame >= 0 && s->nb_frames >= s->start_frame)
142 if (s->start_pts != AV_NOPTS_VALUE && frame->pts != AV_NOPTS_VALUE &&
143 frame->pts >= s->start_pts)
149 if (s->first_pts == AV_NOPTS_VALUE && frame->pts != AV_NOPTS_VALUE)
150 s->first_pts = frame->pts;
152 if (s->end_frame != INT64_MAX || s->end_pts != AV_NOPTS_VALUE || s->duration_tb) {
155 if (s->end_frame != INT64_MAX && s->nb_frames < s->end_frame)
157 if (s->end_pts != AV_NOPTS_VALUE && frame->pts != AV_NOPTS_VALUE &&
158 frame->pts < s->end_pts)
160 if (s->duration_tb && frame->pts != AV_NOPTS_VALUE &&
161 frame->pts - s->first_pts < s->duration_tb)
165 s->eof = inlink->closed = 1;
172 return ff_filter_frame(ctx->outputs[0], frame);
176 av_frame_free(&frame);
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 },
191 AVFILTER_DEFINE_CLASS(trim);
193 static const AVFilterPad trim_inputs[] = {
196 .type = AVMEDIA_TYPE_VIDEO,
197 .filter_frame = trim_filter_frame,
198 .config_props = config_input,
203 static const AVFilterPad trim_outputs[] = {
206 .type = AVMEDIA_TYPE_VIDEO,
207 .config_props = config_output,
212 AVFilter avfilter_vf_trim = {
214 .description = NULL_IF_CONFIG_SMALL("Pick one continuous section from the input, drop the rest."),
218 .priv_size = sizeof(TrimContext),
219 .priv_class = &trim_class,
221 .inputs = trim_inputs,
222 .outputs = trim_outputs,
224 #endif // CONFIG_TRIM_FILTER
226 #if CONFIG_ATRIM_FILTER
227 static int atrim_filter_frame(AVFilterLink *inlink, AVFrame *frame)
229 AVFilterContext *ctx = inlink->dst;
230 TrimContext *s = ctx->priv;
231 int64_t start_sample, end_sample = frame->nb_samples;
235 /* drop everything if EOF has already been returned */
237 av_frame_free(&frame);
241 if (frame->pts != AV_NOPTS_VALUE)
242 pts = av_rescale_q(frame->pts, inlink->time_base,
243 (AVRational){ 1, inlink->sample_rate });
246 s->next_pts = pts + frame->nb_samples;
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) {
253 start_sample = frame->nb_samples;
255 if (s->start_sample >= 0 &&
256 s->nb_samples + frame->nb_samples > s->start_sample) {
258 start_sample = FFMIN(start_sample, s->start_sample - s->nb_samples);
261 if (s->start_pts != AV_NOPTS_VALUE && pts != AV_NOPTS_VALUE &&
262 pts + frame->nb_samples > s->start_pts) {
264 start_sample = FFMIN(start_sample, s->start_pts - pts);
271 if (s->first_pts == AV_NOPTS_VALUE)
272 s->first_pts = pts + start_sample;
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;
281 if (s->end_sample != INT64_MAX &&
282 s->nb_samples < s->end_sample) {
284 end_sample = FFMAX(end_sample, s->end_sample - s->nb_samples);
287 if (s->end_pts != AV_NOPTS_VALUE && pts != AV_NOPTS_VALUE &&
290 end_sample = FFMAX(end_sample, s->end_pts - pts);
293 if (s->duration_tb && pts - s->first_pts < s->duration_tb) {
295 end_sample = FFMAX(end_sample, s->first_pts + s->duration_tb - pts);
299 s->eof = inlink->closed = 1;
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);
310 AVFrame *out = ff_get_audio_buffer(ctx->outputs[0], end_sample - start_sample);
312 av_frame_free(&frame);
313 return AVERROR(ENOMEM);
316 av_frame_copy_props(out, frame);
317 av_samples_copy(out->extended_data, frame->extended_data, 0, start_sample,
318 out->nb_samples, av_get_channel_layout_nb_channels(frame->channel_layout),
320 if (out->pts != AV_NOPTS_VALUE)
321 out->pts += av_rescale_q(start_sample, (AVRational){ 1, out->sample_rate },
324 av_frame_free(&frame);
327 frame->nb_samples = end_sample;
329 return ff_filter_frame(ctx->outputs[0], frame);
332 s->nb_samples += frame->nb_samples;
333 av_frame_free(&frame);
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 },
348 AVFILTER_DEFINE_CLASS(atrim);
350 static const AVFilterPad atrim_inputs[] = {
353 .type = AVMEDIA_TYPE_AUDIO,
354 .filter_frame = atrim_filter_frame,
355 .config_props = config_input,
360 static const AVFilterPad atrim_outputs[] = {
363 .type = AVMEDIA_TYPE_AUDIO,
364 .config_props = config_output,
369 AVFilter avfilter_af_atrim = {
371 .description = NULL_IF_CONFIG_SMALL("Pick one continuous section from the input, drop the rest."),
375 .priv_size = sizeof(TrimContext),
376 .priv_class = &atrim_class,
378 .inputs = atrim_inputs,
379 .outputs = atrim_outputs,
381 #endif // CONFIG_ATRIM_FILTER