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