FFmpeg coverage


Directory: ../../../ffmpeg/
File: src/libavfilter/f_segment.c
Date: 2024-02-16 17:37:06
Exec Total Coverage
Lines: 108 137 78.8%
Functions: 8 9 88.9%
Branches: 62 82 75.6%

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 "config_components.h"
20
21 #include <stdint.h>
22
23 #include "libavutil/avstring.h"
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/parseutils.h"
30 #include "libavutil/samplefmt.h"
31
32 #include "audio.h"
33 #include "avfilter.h"
34 #include "filters.h"
35 #include "internal.h"
36
37 typedef struct SegmentContext {
38 const AVClass *class;
39
40 char *timestamps_str;
41 char *points_str;
42 int use_timestamps;
43
44 int current_point;
45 int nb_points;
46 int64_t last_pts;
47
48 int64_t *points;
49 } SegmentContext;
50
51 8 static void count_points(char *item_str, int *nb_items)
52 {
53 char *p;
54
55
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 8 times.
8 if (!item_str)
56 return;
57
58 8 *nb_items = 1;
59
2/2
✓ Branch 0 taken 60 times.
✓ Branch 1 taken 8 times.
68 for (p = item_str; *p; p++) {
60
2/2
✓ Branch 0 taken 8 times.
✓ Branch 1 taken 52 times.
60 if (*p == '|')
61 8 (*nb_items)++;
62 }
63 }
64
65 8 static int parse_points(AVFilterContext *ctx, char *item_str, int nb_points, int64_t *points)
66 {
67 8 SegmentContext *s = ctx->priv;
68 8 char *arg, *p = item_str;
69 8 char *saveptr = NULL;
70 8 int64_t ref, cur = 0;
71 8 int ret = 0;
72
73
2/2
✓ Branch 0 taken 16 times.
✓ Branch 1 taken 8 times.
24 for (int i = 0; i < nb_points; i++) {
74
1/2
✗ Branch 1 not taken.
✓ Branch 2 taken 16 times.
16 if (!(arg = av_strtok(p, "|", &saveptr)))
75 return AVERROR(EINVAL);
76
77 16 p = NULL;
78 16 ref = 0;
79
2/2
✓ Branch 0 taken 4 times.
✓ Branch 1 taken 12 times.
16 if (*arg == '+') {
80 4 ref = cur;
81 4 arg++;
82 }
83
84
2/2
✓ Branch 0 taken 8 times.
✓ Branch 1 taken 8 times.
16 if (s->use_timestamps) {
85 8 ret = av_parse_time(&points[i], arg, s->use_timestamps);
86 } else {
87
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 8 times.
8 if (sscanf(arg, "%"SCNd64, &points[i]) != 1)
88 ret = AVERROR(EINVAL);
89 }
90
91
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 16 times.
16 if (ret < 0) {
92 av_log(ctx, AV_LOG_ERROR, "Invalid splits supplied: %s\n", arg);
93 return ret;
94 }
95
96 16 cur = points[i];
97 16 points[i] += ref;
98 }
99
100 8 return 0;
101 }
102
103 8 static av_cold int init(AVFilterContext *ctx, enum AVMediaType type)
104 {
105 8 SegmentContext *s = ctx->priv;
106 char *split_str;
107 int ret;
108
109
3/4
✓ Branch 0 taken 4 times.
✓ Branch 1 taken 4 times.
✗ Branch 2 not taken.
✓ Branch 3 taken 4 times.
8 if (s->timestamps_str && s->points_str) {
110 av_log(ctx, AV_LOG_ERROR, "Both timestamps and counts supplied.\n");
111 return AVERROR(EINVAL);
112
2/2
✓ Branch 0 taken 4 times.
✓ Branch 1 taken 4 times.
8 } else if (s->timestamps_str) {
113 4 s->use_timestamps = 1;
114 4 split_str = s->timestamps_str;
115
1/2
✓ Branch 0 taken 4 times.
✗ Branch 1 not taken.
4 } else if (s->points_str) {
116 4 split_str = s->points_str;
117 } else {
118 av_log(ctx, AV_LOG_ERROR, "Neither timestamps nor durations nor counts supplied.\n");
119 return AVERROR(EINVAL);
120 }
121
122 8 count_points(split_str, &s->nb_points);
123 8 s->nb_points++;
124
125 8 s->points = av_calloc(s->nb_points, sizeof(*s->points));
126
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 8 times.
8 if (!s->points)
127 return AVERROR(ENOMEM);
128
129 8 ret = parse_points(ctx, split_str, s->nb_points - 1, s->points);
130
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 8 times.
8 if (ret < 0)
131 return ret;
132
133 8 s->points[s->nb_points - 1] = INT64_MAX;
134
135
2/2
✓ Branch 0 taken 24 times.
✓ Branch 1 taken 8 times.
32 for (int i = 0; i < s->nb_points; i++) {
136 24 AVFilterPad pad = { 0 };
137
138 24 pad.type = type;
139 24 pad.name = av_asprintf("output%d", i);
140
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 24 times.
24 if (!pad.name)
141 return AVERROR(ENOMEM);
142
143
1/2
✗ Branch 1 not taken.
✓ Branch 2 taken 24 times.
24 if ((ret = ff_append_outpad_free_name(ctx, &pad)) < 0)
144 return ret;
145 }
146
147 8 return 0;
148 }
149
150 4 static int config_input(AVFilterLink *inlink)
151 {
152 4 AVFilterContext *ctx = inlink->dst;
153 4 SegmentContext *s = ctx->priv;
154 4 AVRational tb = inlink->time_base;
155
156
2/2
✓ Branch 0 taken 2 times.
✓ Branch 1 taken 2 times.
4 if (s->use_timestamps) {
157
2/2
✓ Branch 0 taken 4 times.
✓ Branch 1 taken 2 times.
6 for (int i = 0; i < s->nb_points - 1; i++)
158 4 s->points[i] = av_rescale_q(s->points[i], AV_TIME_BASE_Q, tb);
159 }
160
161 4 return 0;
162 }
163
164 1038 static int current_segment_finished(AVFilterContext *ctx, AVFrame *frame)
165 {
166 1038 SegmentContext *s = ctx->priv;
167 1038 AVFilterLink *inlink = ctx->inputs[0];
168 1038 int ret = 0;
169
170
2/2
✓ Branch 0 taken 520 times.
✓ Branch 1 taken 518 times.
1038 if (s->use_timestamps) {
171 520 ret = frame->pts >= s->points[s->current_point];
172 } else {
173
1/3
✗ Branch 0 not taken.
✓ Branch 1 taken 518 times.
✗ Branch 2 not taken.
518 switch (inlink->type) {
174 case AVMEDIA_TYPE_VIDEO:
175 ret = inlink->frame_count_out - 1 >= s->points[s->current_point];
176 break;
177 518 case AVMEDIA_TYPE_AUDIO:
178 518 ret = inlink->sample_count_out - frame->nb_samples >= s->points[s->current_point];
179 518 break;
180 }
181 }
182
183 1038 return ret;
184 }
185
186 1796 static int activate(AVFilterContext *ctx)
187 {
188 1796 AVFilterLink *inlink = ctx->inputs[0];
189 1796 SegmentContext *s = ctx->priv;
190 1796 AVFrame *frame = NULL;
191 int ret, status;
192 int64_t max_samples;
193 int64_t diff;
194 int64_t pts;
195
196
2/2
✓ Branch 0 taken 2228 times.
✓ Branch 1 taken 1792 times.
4020 for (int i = s->current_point; i < s->nb_points; i++) {
197
4/4
✓ Branch 1 taken 4 times.
✓ Branch 2 taken 2224 times.
✓ Branch 4 taken 4 times.
✓ Branch 5 taken 4 times.
2232 FF_FILTER_FORWARD_STATUS_BACK_ALL(ctx->outputs[i], ctx);
198 }
199
200
1/3
✗ Branch 0 not taken.
✓ Branch 1 taken 1792 times.
✗ Branch 2 not taken.
1792 switch (inlink->type) {
201 case AVMEDIA_TYPE_VIDEO:
202 ret = ff_inlink_consume_frame(inlink, &frame);
203 break;
204 1792 case AVMEDIA_TYPE_AUDIO:
205 1792 diff = s->points[s->current_point] - inlink->sample_count_out;
206
2/2
✓ Branch 0 taken 8 times.
✓ Branch 1 taken 1792 times.
1800 while (diff <= 0) {
207 8 ff_outlink_set_status(ctx->outputs[s->current_point], AVERROR_EOF, s->last_pts);
208 8 s->current_point++;
209
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 8 times.
8 if (s->current_point >= s->nb_points)
210 return AVERROR(EINVAL);
211
212 8 diff = s->points[s->current_point] - inlink->sample_count_out;
213 }
214
2/2
✓ Branch 0 taken 786 times.
✓ Branch 1 taken 1006 times.
1792 if (s->use_timestamps) {
215 786 max_samples = av_rescale_q(diff, av_make_q(1, inlink->sample_rate), inlink->time_base);
216 } else {
217
1/2
✓ Branch 0 taken 1006 times.
✗ Branch 1 not taken.
1006 max_samples = FFMAX(1, FFMIN(diff, INT_MAX));
218 }
219
3/4
✓ Branch 0 taken 1792 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 520 times.
✓ Branch 3 taken 1272 times.
1792 if (max_samples <= 0 || max_samples > INT_MAX)
220 520 ret = ff_inlink_consume_frame(inlink, &frame);
221 else
222 1272 ret = ff_inlink_consume_samples(inlink, 1, max_samples, &frame);
223 1792 break;
224 default:
225 return AVERROR_BUG;
226 }
227
228
2/2
✓ Branch 0 taken 1038 times.
✓ Branch 1 taken 754 times.
1792 if (ret > 0) {
229 1038 s->last_pts = frame->pts;
230
1/2
✗ Branch 1 not taken.
✓ Branch 2 taken 1038 times.
1038 while (current_segment_finished(ctx, frame)) {
231 ff_outlink_set_status(ctx->outputs[s->current_point], AVERROR_EOF, frame->pts);
232 s->current_point++;
233 }
234
235
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 1038 times.
1038 if (s->current_point >= s->nb_points) {
236 av_frame_free(&frame);
237 return AVERROR(EINVAL);
238 }
239
240 1038 ret = ff_filter_frame(ctx->outputs[s->current_point], frame);
241 }
242
243
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 1792 times.
1792 if (ret < 0) {
244 return ret;
245
2/2
✓ Branch 1 taken 4 times.
✓ Branch 2 taken 1788 times.
1792 } else if (ff_inlink_acknowledge_status(inlink, &status, &pts)) {
246
2/2
✓ Branch 0 taken 4 times.
✓ Branch 1 taken 4 times.
8 for (int i = s->current_point; i < s->nb_points; i++)
247 4 ff_outlink_set_status(ctx->outputs[i], status, pts);
248 4 return 0;
249 } else {
250
2/2
✓ Branch 0 taken 2212 times.
✓ Branch 1 taken 1788 times.
4000 for (int i = s->current_point; i < s->nb_points; i++) {
251
2/2
✓ Branch 1 taken 1036 times.
✓ Branch 2 taken 1176 times.
2212 if (ff_outlink_frame_wanted(ctx->outputs[i]))
252 1036 ff_inlink_request_frame(inlink);
253 }
254 1788 return 0;
255 }
256 }
257
258 8 static av_cold void uninit(AVFilterContext *ctx)
259 {
260 8 SegmentContext *s = ctx->priv;
261
262 8 av_freep(&s->points);
263 8 }
264
265 #define OFFSET(x) offsetof(SegmentContext, x)
266 #define COMMON_OPTS \
267 { "timestamps", "timestamps of input at which to split input", OFFSET(timestamps_str), AV_OPT_TYPE_STRING, { .str = NULL }, 0, 0, FLAGS }, \
268
269 #if CONFIG_SEGMENT_FILTER
270
271 static av_cold int video_init(AVFilterContext *ctx)
272 {
273 return init(ctx, AVMEDIA_TYPE_VIDEO);
274 }
275
276 #define FLAGS AV_OPT_FLAG_VIDEO_PARAM | AV_OPT_FLAG_FILTERING_PARAM
277 static const AVOption segment_options[] = {
278 COMMON_OPTS
279 { "frames", "frames at which to split input", OFFSET(points_str), AV_OPT_TYPE_STRING, { .str = NULL }, 0, 0, FLAGS },
280 { NULL }
281 };
282 #undef FLAGS
283
284 AVFILTER_DEFINE_CLASS(segment);
285
286 static const AVFilterPad segment_inputs[] = {
287 {
288 .name = "default",
289 .type = AVMEDIA_TYPE_VIDEO,
290 .config_props = config_input,
291 },
292 };
293
294 const AVFilter ff_vf_segment = {
295 .name = "segment",
296 .description = NULL_IF_CONFIG_SMALL("Segment video stream."),
297 .init = video_init,
298 .uninit = uninit,
299 .priv_size = sizeof(SegmentContext),
300 .priv_class = &segment_class,
301 .activate = activate,
302 FILTER_INPUTS(segment_inputs),
303 .outputs = NULL,
304 .flags = AVFILTER_FLAG_DYNAMIC_OUTPUTS | AVFILTER_FLAG_METADATA_ONLY,
305 };
306 #endif // CONFIG_SEGMENT_FILTER
307
308 #if CONFIG_ASEGMENT_FILTER
309
310 8 static av_cold int audio_init(AVFilterContext *ctx)
311 {
312 8 return init(ctx, AVMEDIA_TYPE_AUDIO);
313 }
314
315 #define FLAGS AV_OPT_FLAG_AUDIO_PARAM | AV_OPT_FLAG_FILTERING_PARAM
316 static const AVOption asegment_options[] = {
317 COMMON_OPTS
318 { "samples", "samples at which to split input", OFFSET(points_str), AV_OPT_TYPE_STRING, { .str = NULL }, 0, 0, FLAGS },
319 { NULL }
320 };
321 #undef FLAGS
322
323 AVFILTER_DEFINE_CLASS(asegment);
324
325 static const AVFilterPad asegment_inputs[] = {
326 {
327 .name = "default",
328 .type = AVMEDIA_TYPE_AUDIO,
329 .config_props = config_input,
330 },
331 };
332
333 const AVFilter ff_af_asegment = {
334 .name = "asegment",
335 .description = NULL_IF_CONFIG_SMALL("Segment audio stream."),
336 .init = audio_init,
337 .uninit = uninit,
338 .priv_size = sizeof(SegmentContext),
339 .priv_class = &asegment_class,
340 .activate = activate,
341 FILTER_INPUTS(asegment_inputs),
342 .outputs = NULL,
343 .flags = AVFILTER_FLAG_DYNAMIC_OUTPUTS | AVFILTER_FLAG_METADATA_ONLY,
344 };
345 #endif // CONFIG_ASEGMENT_FILTER
346