FFmpeg coverage


Directory: ../../../ffmpeg/
File: src/libavfilter/trim.c
Date: 2024-07-17 14:05:47
Exec Total Coverage
Lines: 143 151 94.7%
Functions: 5 5 100.0%
Branches: 115 138 83.3%

Line Branch Exec Source
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
24 #include "libavutil/channel_layout.h"
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 #include "filters.h"
35 #include "video.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 * in the link timebase for video,
48 * in 1/samplerate for audio
49 */
50 int64_t start_pts, end_pts;
51 int64_t start_sample, end_sample;
52
53 /*
54 * number of video frames that arrived on this filter so far
55 */
56 int64_t nb_frames;
57 /*
58 * number of audio samples that arrived on this filter so far
59 */
60 int64_t nb_samples;
61 /*
62 * timestamp of the first frame in the output, in the timebase units
63 */
64 int64_t first_pts;
65 /*
66 * duration in the timebase units
67 */
68 int64_t duration_tb;
69
70 int64_t next_pts;
71
72 int eof;
73
74 int (*filter_frame)(AVFilterLink *inlink, AVFrame *frame);
75 } TrimContext;
76
77 920 static av_cold int init(AVFilterContext *ctx)
78 {
79 920 TrimContext *s = ctx->priv;
80
81 920 s->first_pts = AV_NOPTS_VALUE;
82
83 920 return 0;
84 }
85
86 #if CONFIG_TRIM_FILTER
87 3986 static int trim_filter_frame(AVFilterLink *inlink, AVFrame *frame)
88 {
89 3986 AVFilterContext *ctx = inlink->dst;
90 3986 TrimContext *s = ctx->priv;
91 int drop;
92
93 /* drop everything if EOF has already been returned */
94
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 3986 times.
3986 if (s->eof) {
95 av_frame_free(&frame);
96 return 0;
97 }
98
99
4/4
✓ Branch 0 taken 3964 times.
✓ Branch 1 taken 22 times.
✓ Branch 2 taken 155 times.
✓ Branch 3 taken 3809 times.
3986 if (s->start_frame >= 0 || s->start_pts != AV_NOPTS_VALUE) {
100 177 drop = 1;
101
4/4
✓ Branch 0 taken 22 times.
✓ Branch 1 taken 155 times.
✓ Branch 2 taken 18 times.
✓ Branch 3 taken 4 times.
177 if (s->start_frame >= 0 && s->nb_frames >= s->start_frame)
102 18 drop = 0;
103
3/4
✓ Branch 0 taken 166 times.
✓ Branch 1 taken 11 times.
✓ Branch 2 taken 166 times.
✗ Branch 3 not taken.
177 if (s->start_pts != AV_NOPTS_VALUE && frame->pts != AV_NOPTS_VALUE &&
104
2/2
✓ Branch 0 taken 151 times.
✓ Branch 1 taken 15 times.
166 frame->pts >= s->start_pts)
105 151 drop = 0;
106
2/2
✓ Branch 0 taken 14 times.
✓ Branch 1 taken 163 times.
177 if (drop)
107 14 goto drop;
108 }
109
110
3/4
✓ Branch 0 taken 117 times.
✓ Branch 1 taken 3855 times.
✓ Branch 2 taken 117 times.
✗ Branch 3 not taken.
3972 if (s->first_pts == AV_NOPTS_VALUE && frame->pts != AV_NOPTS_VALUE)
111 117 s->first_pts = frame->pts;
112
113
6/6
✓ Branch 0 taken 3954 times.
✓ Branch 1 taken 18 times.
✓ Branch 2 taken 3951 times.
✓ Branch 3 taken 3 times.
✓ Branch 4 taken 3811 times.
✓ Branch 5 taken 140 times.
3972 if (s->end_frame != INT64_MAX || s->end_pts != AV_NOPTS_VALUE || s->duration_tb) {
114 3832 drop = 1;
115
116
4/4
✓ Branch 0 taken 18 times.
✓ Branch 1 taken 3814 times.
✓ Branch 2 taken 9 times.
✓ Branch 3 taken 9 times.
3832 if (s->end_frame != INT64_MAX && s->nb_frames < s->end_frame)
117 9 drop = 0;
118
3/4
✓ Branch 0 taken 13 times.
✓ Branch 1 taken 3819 times.
✓ Branch 2 taken 13 times.
✗ Branch 3 not taken.
3832 if (s->end_pts != AV_NOPTS_VALUE && frame->pts != AV_NOPTS_VALUE &&
119
2/2
✓ Branch 0 taken 11 times.
✓ Branch 1 taken 2 times.
13 frame->pts < s->end_pts)
120 11 drop = 0;
121
3/4
✓ Branch 0 taken 3811 times.
✓ Branch 1 taken 21 times.
✓ Branch 2 taken 3811 times.
✗ Branch 3 not taken.
3832 if (s->duration_tb && frame->pts != AV_NOPTS_VALUE &&
122
2/2
✓ Branch 0 taken 3705 times.
✓ Branch 1 taken 106 times.
3811 frame->pts - s->first_pts < s->duration_tb)
123 3705 drop = 0;
124
125
2/2
✓ Branch 0 taken 109 times.
✓ Branch 1 taken 3723 times.
3832 if (drop) {
126 109 s->eof = 1;
127 109 ff_inlink_set_status(inlink, AVERROR_EOF);
128 109 ff_outlink_set_status(ctx->outputs[0], AVERROR_EOF, frame->pts);
129 109 goto drop;
130 }
131 }
132
133 3863 s->nb_frames++;
134
135 3863 return ff_filter_frame(ctx->outputs[0], frame);
136
137 123 drop:
138
2/2
✓ Branch 0 taken 14 times.
✓ Branch 1 taken 109 times.
123 if (!s->eof)
139 14 ff_filter_set_ready(ctx, 100);
140 123 s->nb_frames++;
141 123 av_frame_free(&frame);
142 123 return 0;
143 }
144 #endif // CONFIG_TRIM_FILTER
145
146 #if CONFIG_ATRIM_FILTER
147 10382 static int atrim_filter_frame(AVFilterLink *inlink, AVFrame *frame)
148 {
149 10382 AVFilterContext *ctx = inlink->dst;
150 10382 TrimContext *s = ctx->priv;
151 int64_t start_sample, end_sample;
152 int64_t pts;
153 int drop;
154
155 /* drop everything if EOF has already been returned */
156
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 10382 times.
10382 if (s->eof) {
157 av_frame_free(&frame);
158 return 0;
159 }
160
161
1/2
✓ Branch 0 taken 10382 times.
✗ Branch 1 not taken.
10382 if (frame->pts != AV_NOPTS_VALUE)
162 10382 pts = av_rescale_q(frame->pts, inlink->time_base,
163 10382 (AVRational){ 1, inlink->sample_rate });
164 else
165 pts = s->next_pts;
166 10382 s->next_pts = pts + frame->nb_samples;
167
168 /* check if at least a part of the frame is after the start time */
169
4/4
✓ Branch 0 taken 10377 times.
✓ Branch 1 taken 5 times.
✓ Branch 2 taken 8602 times.
✓ Branch 3 taken 1775 times.
10382 if (s->start_sample < 0 && s->start_pts == AV_NOPTS_VALUE) {
170 8602 start_sample = 0;
171 } else {
172 1780 drop = 1;
173 1780 start_sample = frame->nb_samples;
174
175
2/2
✓ Branch 0 taken 5 times.
✓ Branch 1 taken 1775 times.
1780 if (s->start_sample >= 0 &&
176
1/2
✓ Branch 0 taken 5 times.
✗ Branch 1 not taken.
5 s->nb_samples + frame->nb_samples > s->start_sample) {
177 5 drop = 0;
178 5 start_sample = FFMIN(start_sample, s->start_sample - s->nb_samples);
179 }
180
181
3/4
✓ Branch 0 taken 1778 times.
✓ Branch 1 taken 2 times.
✓ Branch 2 taken 1778 times.
✗ Branch 3 not taken.
1780 if (s->start_pts != AV_NOPTS_VALUE && pts != AV_NOPTS_VALUE &&
182
2/2
✓ Branch 0 taken 1454 times.
✓ Branch 1 taken 324 times.
1778 pts + frame->nb_samples > s->start_pts) {
183 1454 drop = 0;
184 1454 start_sample = FFMIN(start_sample, s->start_pts - pts);
185 }
186
187
2/2
✓ Branch 0 taken 324 times.
✓ Branch 1 taken 1456 times.
1780 if (drop)
188 324 goto drop;
189 }
190
191
2/2
✓ Branch 0 taken 460 times.
✓ Branch 1 taken 9598 times.
10058 if (s->first_pts == AV_NOPTS_VALUE)
192 460 s->first_pts = pts + start_sample;
193
194 /* check if at least a part of the frame is before the end time */
195
6/6
✓ Branch 0 taken 4808 times.
✓ Branch 1 taken 5250 times.
✓ Branch 2 taken 4788 times.
✓ Branch 3 taken 20 times.
✓ Branch 4 taken 1446 times.
✓ Branch 5 taken 3342 times.
10058 if (s->end_sample == INT64_MAX && s->end_pts == AV_NOPTS_VALUE && !s->duration_tb) {
196 1446 end_sample = frame->nb_samples;
197 } else {
198 8612 drop = 1;
199 8612 end_sample = 0;
200
201
2/2
✓ Branch 0 taken 5250 times.
✓ Branch 1 taken 3362 times.
8612 if (s->end_sample != INT64_MAX &&
202
2/2
✓ Branch 0 taken 4915 times.
✓ Branch 1 taken 335 times.
5250 s->nb_samples < s->end_sample) {
203 4915 drop = 0;
204 4915 end_sample = FFMAX(end_sample, s->end_sample - s->nb_samples);
205 }
206
207
3/4
✓ Branch 0 taken 23 times.
✓ Branch 1 taken 8589 times.
✓ Branch 2 taken 23 times.
✗ Branch 3 not taken.
8612 if (s->end_pts != AV_NOPTS_VALUE && pts != AV_NOPTS_VALUE &&
208
2/2
✓ Branch 0 taken 20 times.
✓ Branch 1 taken 3 times.
23 pts < s->end_pts) {
209 20 drop = 0;
210 20 end_sample = FFMAX(end_sample, s->end_pts - pts);
211 }
212
213
4/4
✓ Branch 0 taken 3342 times.
✓ Branch 1 taken 5270 times.
✓ Branch 2 taken 3237 times.
✓ Branch 3 taken 105 times.
8612 if (s->duration_tb && pts - s->first_pts < s->duration_tb) {
214 3237 drop = 0;
215 3237 end_sample = FFMAX(end_sample, s->first_pts + s->duration_tb - pts);
216 }
217
218
2/2
✓ Branch 0 taken 442 times.
✓ Branch 1 taken 8170 times.
8612 if (drop) {
219 442 s->eof = 1;
220 442 ff_inlink_set_status(inlink, AVERROR_EOF);
221 442 ff_outlink_set_status(ctx->outputs[0], AVERROR_EOF, frame->pts);
222 442 goto drop;
223 }
224 }
225
226 9616 s->nb_samples += frame->nb_samples;
227 9616 start_sample = FFMAX(0, start_sample);
228 9616 end_sample = FFMIN(frame->nb_samples, end_sample);
229
2/4
✓ Branch 0 taken 9616 times.
✗ Branch 1 not taken.
✗ Branch 2 not taken.
✓ Branch 3 taken 9616 times.
9616 if (start_sample >= end_sample || !frame->nb_samples)
230 goto drop;
231
232
2/2
✓ Branch 0 taken 6 times.
✓ Branch 1 taken 9610 times.
9616 if (start_sample) {
233 6 AVFrame *out = ff_get_audio_buffer(ctx->outputs[0], end_sample - start_sample);
234
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 6 times.
6 if (!out) {
235 av_frame_free(&frame);
236 return AVERROR(ENOMEM);
237 }
238
239 6 av_frame_copy_props(out, frame);
240 6 av_samples_copy(out->extended_data, frame->extended_data, 0, start_sample,
241 out->nb_samples, inlink->ch_layout.nb_channels,
242 6 frame->format);
243
1/2
✓ Branch 0 taken 6 times.
✗ Branch 1 not taken.
6 if (out->pts != AV_NOPTS_VALUE)
244 6 out->pts += av_rescale_q(start_sample, (AVRational){ 1, out->sample_rate },
245 inlink->time_base);
246
247 6 av_frame_free(&frame);
248 6 frame = out;
249 } else
250 9610 frame->nb_samples = end_sample;
251
252 9616 return ff_filter_frame(ctx->outputs[0], frame);
253
254 766 drop:
255
2/2
✓ Branch 0 taken 324 times.
✓ Branch 1 taken 442 times.
766 if (!s->eof)
256 324 ff_filter_set_ready(ctx, 100);
257 766 s->nb_samples += frame->nb_samples;
258 766 av_frame_free(&frame);
259 766 return 0;
260 }
261 #endif // CONFIG_ATRIM_FILTER
262
263 577 static int config_input(AVFilterLink *inlink)
264 {
265 577 AVFilterContext *ctx = inlink->dst;
266 577 TrimContext *s = ctx->priv;
267 117 AVRational tb = (inlink->type == AVMEDIA_TYPE_VIDEO) ?
268
2/2
✓ Branch 0 taken 117 times.
✓ Branch 1 taken 460 times.
577 inlink->time_base : (AVRational){ 1, inlink->sample_rate };
269
270 #if CONFIG_TRIM_FILTER
271
2/2
✓ Branch 0 taken 117 times.
✓ Branch 1 taken 460 times.
577 if (inlink->type == AVMEDIA_TYPE_VIDEO)
272 117 s->filter_frame = trim_filter_frame;
273 #endif
274 #if CONFIG_ATRIM_FILTER
275
2/2
✓ Branch 0 taken 460 times.
✓ Branch 1 taken 117 times.
577 if (inlink->type == AVMEDIA_TYPE_AUDIO)
276 460 s->filter_frame = atrim_filter_frame;
277 #endif
278
2/2
✓ Branch 0 taken 25 times.
✓ Branch 1 taken 552 times.
577 if (s->start_time != INT64_MAX) {
279 25 int64_t start_pts = av_rescale_q(s->start_time, AV_TIME_BASE_Q, tb);
280
1/4
✗ Branch 0 not taken.
✓ Branch 1 taken 25 times.
✗ Branch 2 not taken.
✗ Branch 3 not taken.
25 if (s->start_pts == AV_NOPTS_VALUE || start_pts < s->start_pts)
281 25 s->start_pts = start_pts;
282 }
283
2/2
✓ Branch 0 taken 5 times.
✓ Branch 1 taken 572 times.
577 if (s->end_time != INT64_MAX) {
284 5 int64_t end_pts = av_rescale_q(s->end_time, AV_TIME_BASE_Q, tb);
285
1/4
✗ Branch 0 not taken.
✓ Branch 1 taken 5 times.
✗ Branch 2 not taken.
✗ Branch 3 not taken.
5 if (s->end_pts == AV_NOPTS_VALUE || end_pts > s->end_pts)
286 5 s->end_pts = end_pts;
287 }
288
2/2
✓ Branch 0 taken 218 times.
✓ Branch 1 taken 359 times.
577 if (s->duration)
289 218 s->duration_tb = av_rescale_q(s->duration, AV_TIME_BASE_Q, tb);
290
291 577 return 0;
292 }
293
294 28114 static int activate(AVFilterContext *ctx)
295 {
296 28114 TrimContext *s = ctx->priv;
297 28114 AVFilterLink *inlink = ctx->inputs[0];
298 28114 AVFilterLink *outlink = ctx->outputs[0];
299
300
2/2
✓ Branch 1 taken 6 times.
✓ Branch 2 taken 28108 times.
28114 FF_FILTER_FORWARD_STATUS_BACK(outlink, inlink);
301
302
3/4
✓ Branch 0 taken 28108 times.
✗ Branch 1 not taken.
✓ Branch 3 taken 14368 times.
✓ Branch 4 taken 13740 times.
28108 if (!s->eof && ff_inlink_queued_frames(inlink)) {
303 14368 AVFrame *frame = NULL;
304 int ret;
305
306 14368 ret = ff_inlink_consume_frame(inlink, &frame);
307
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 14368 times.
14368 if (ret < 0)
308 14368 return ret;
309
1/2
✓ Branch 0 taken 14368 times.
✗ Branch 1 not taken.
14368 if (ret > 0)
310 14368 return s->filter_frame(inlink, frame);
311 }
312
313
2/2
✓ Branch 1 taken 14 times.
✓ Branch 2 taken 13726 times.
13740 FF_FILTER_FORWARD_STATUS(inlink, outlink);
314
2/2
✓ Branch 1 taken 13720 times.
✓ Branch 2 taken 6 times.
13726 FF_FILTER_FORWARD_WANTED(outlink, inlink);
315
316 6 return FFERROR_NOT_READY;
317 }
318
319 #define OFFSET(x) offsetof(TrimContext, x)
320 #define COMMON_OPTS \
321 { "start", "Timestamp of the first frame that " \
322 "should be passed", OFFSET(start_time), AV_OPT_TYPE_DURATION, { .i64 = INT64_MAX }, INT64_MIN, INT64_MAX, FLAGS }, \
323 { "starti", "Timestamp of the first frame that " \
324 "should be passed", OFFSET(start_time), AV_OPT_TYPE_DURATION, { .i64 = INT64_MAX }, INT64_MIN, INT64_MAX, FLAGS }, \
325 { "end", "Timestamp of the first frame that " \
326 "should be dropped again", OFFSET(end_time), AV_OPT_TYPE_DURATION, { .i64 = INT64_MAX }, INT64_MIN, INT64_MAX, FLAGS }, \
327 { "endi", "Timestamp of the first frame that " \
328 "should be dropped again", OFFSET(end_time), AV_OPT_TYPE_DURATION, { .i64 = INT64_MAX }, INT64_MIN, INT64_MAX, FLAGS }, \
329 { "start_pts", "Timestamp of the first frame that should be " \
330 " passed", OFFSET(start_pts), AV_OPT_TYPE_INT64, { .i64 = AV_NOPTS_VALUE }, INT64_MIN, INT64_MAX, FLAGS }, \
331 { "end_pts", "Timestamp of the first frame that should be " \
332 "dropped again", OFFSET(end_pts), AV_OPT_TYPE_INT64, { .i64 = AV_NOPTS_VALUE }, INT64_MIN, INT64_MAX, FLAGS }, \
333 { "duration", "Maximum duration of the output", OFFSET(duration), AV_OPT_TYPE_DURATION, { .i64 = 0 }, 0, INT64_MAX, FLAGS }, \
334 { "durationi", "Maximum duration of the output", OFFSET(duration), AV_OPT_TYPE_DURATION, { .i64 = 0 }, 0, INT64_MAX, FLAGS },
335
336
337 #if CONFIG_TRIM_FILTER
338
339 #define FLAGS AV_OPT_FLAG_VIDEO_PARAM | AV_OPT_FLAG_FILTERING_PARAM
340 static const AVOption trim_options[] = {
341 COMMON_OPTS
342 { "start_frame", "Number of the first frame that should be passed "
343 "to the output", OFFSET(start_frame), AV_OPT_TYPE_INT64, { .i64 = -1 }, -1, INT64_MAX, FLAGS },
344 { "end_frame", "Number of the first frame that should be dropped "
345 "again", OFFSET(end_frame), AV_OPT_TYPE_INT64, { .i64 = INT64_MAX }, 0, INT64_MAX, FLAGS },
346 { NULL }
347 };
348 #undef FLAGS
349
350 AVFILTER_DEFINE_CLASS(trim);
351
352 static const AVFilterPad trim_inputs[] = {
353 {
354 .name = "default",
355 .type = AVMEDIA_TYPE_VIDEO,
356 .config_props = config_input,
357 },
358 };
359
360 const AVFilter ff_vf_trim = {
361 .name = "trim",
362 .description = NULL_IF_CONFIG_SMALL("Pick one continuous section from the input, drop the rest."),
363 .init = init,
364 .activate = activate,
365 .priv_size = sizeof(TrimContext),
366 .priv_class = &trim_class,
367 .flags = AVFILTER_FLAG_METADATA_ONLY,
368 FILTER_INPUTS(trim_inputs),
369 FILTER_OUTPUTS(ff_video_default_filterpad),
370 };
371 #endif // CONFIG_TRIM_FILTER
372
373 #if CONFIG_ATRIM_FILTER
374
375 #define FLAGS AV_OPT_FLAG_AUDIO_PARAM | AV_OPT_FLAG_FILTERING_PARAM
376 static const AVOption atrim_options[] = {
377 COMMON_OPTS
378 { "start_sample", "Number of the first audio sample that should be "
379 "passed to the output", OFFSET(start_sample), AV_OPT_TYPE_INT64, { .i64 = -1 }, -1, INT64_MAX, FLAGS },
380 { "end_sample", "Number of the first audio sample that should be "
381 "dropped again", OFFSET(end_sample), AV_OPT_TYPE_INT64, { .i64 = INT64_MAX }, 0, INT64_MAX, FLAGS },
382 { NULL }
383 };
384 #undef FLAGS
385
386 AVFILTER_DEFINE_CLASS(atrim);
387
388 static const AVFilterPad atrim_inputs[] = {
389 {
390 .name = "default",
391 .type = AVMEDIA_TYPE_AUDIO,
392 .config_props = config_input,
393 },
394 };
395
396 const AVFilter ff_af_atrim = {
397 .name = "atrim",
398 .description = NULL_IF_CONFIG_SMALL("Pick one continuous section from the input, drop the rest."),
399 .init = init,
400 .activate = activate,
401 .priv_size = sizeof(TrimContext),
402 .priv_class = &atrim_class,
403 .flags = AVFILTER_FLAG_METADATA_ONLY,
404 FILTER_INPUTS(atrim_inputs),
405 FILTER_OUTPUTS(ff_audio_default_filterpad),
406 };
407 #endif // CONFIG_ATRIM_FILTER
408