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