Line | Branch | Exec | Source |
---|---|---|---|
1 | /* | ||
2 | * Copyright (c) 2010 Stefano Sabatini | ||
3 | * Copyright (c) 2008 Victor Paesa | ||
4 | * | ||
5 | * This file is part of FFmpeg. | ||
6 | * | ||
7 | * FFmpeg is free software; you can redistribute it and/or | ||
8 | * modify it under the terms of the GNU Lesser General Public | ||
9 | * License as published by the Free Software Foundation; either | ||
10 | * version 2.1 of the License, or (at your option) any later version. | ||
11 | * | ||
12 | * FFmpeg is distributed in the hope that it will be useful, | ||
13 | * but WITHOUT ANY WARRANTY; without even the implied warranty of | ||
14 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU | ||
15 | * Lesser General Public License for more details. | ||
16 | * | ||
17 | * You should have received a copy of the GNU Lesser General Public | ||
18 | * License along with FFmpeg; if not, write to the Free Software | ||
19 | * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA | ||
20 | */ | ||
21 | |||
22 | /** | ||
23 | * @file | ||
24 | * video presentation timestamp (PTS) modification filter | ||
25 | */ | ||
26 | |||
27 | #include "config_components.h" | ||
28 | |||
29 | #include <inttypes.h> | ||
30 | |||
31 | #include "libavutil/eval.h" | ||
32 | #include "libavutil/internal.h" | ||
33 | #include "libavutil/mathematics.h" | ||
34 | #include "libavutil/opt.h" | ||
35 | #include "libavutil/time.h" | ||
36 | #include "audio.h" | ||
37 | #include "avfilter.h" | ||
38 | #include "filters.h" | ||
39 | #include "video.h" | ||
40 | |||
41 | static const char *const var_names[] = { | ||
42 | "FRAME_RATE", ///< defined only for constant frame-rate video | ||
43 | "INTERLACED", ///< tell if the current frame is interlaced | ||
44 | "N", ///< frame / sample number (starting at zero) | ||
45 | "NB_CONSUMED_SAMPLES", ///< number of samples consumed by the filter (only audio) | ||
46 | "NB_SAMPLES", ///< number of samples in the current frame (only audio) | ||
47 | #if FF_API_FRAME_PKT | ||
48 | "POS", ///< original position in the file of the frame | ||
49 | #endif | ||
50 | "PREV_INPTS", ///< previous input PTS | ||
51 | "PREV_INT", ///< previous input time in seconds | ||
52 | "PREV_OUTPTS", ///< previous output PTS | ||
53 | "PREV_OUTT", ///< previous output time in seconds | ||
54 | "PTS", ///< original pts in the file of the frame | ||
55 | "SAMPLE_RATE", ///< sample rate (only audio) | ||
56 | "STARTPTS", ///< PTS at start of movie | ||
57 | "STARTT", ///< time at start of movie | ||
58 | "T", ///< original time in the file of the frame | ||
59 | "TB", ///< timebase | ||
60 | "RTCTIME", ///< wallclock (RTC) time in micro seconds | ||
61 | "RTCSTART", ///< wallclock (RTC) time at the start of the movie in micro seconds | ||
62 | "S", // Number of samples in the current frame | ||
63 | "SR", // Audio sample rate | ||
64 | "FR", ///< defined only for constant frame-rate video | ||
65 | "T_CHANGE", ///< time of first frame after latest command was applied | ||
66 | NULL | ||
67 | }; | ||
68 | |||
69 | enum var_name { | ||
70 | VAR_FRAME_RATE, | ||
71 | VAR_INTERLACED, | ||
72 | VAR_N, | ||
73 | VAR_NB_CONSUMED_SAMPLES, | ||
74 | VAR_NB_SAMPLES, | ||
75 | #if FF_API_FRAME_PKT | ||
76 | VAR_POS, | ||
77 | #endif | ||
78 | VAR_PREV_INPTS, | ||
79 | VAR_PREV_INT, | ||
80 | VAR_PREV_OUTPTS, | ||
81 | VAR_PREV_OUTT, | ||
82 | VAR_PTS, | ||
83 | VAR_SAMPLE_RATE, | ||
84 | VAR_STARTPTS, | ||
85 | VAR_STARTT, | ||
86 | VAR_T, | ||
87 | VAR_TB, | ||
88 | VAR_RTCTIME, | ||
89 | VAR_RTCSTART, | ||
90 | VAR_S, | ||
91 | VAR_SR, | ||
92 | VAR_FR, | ||
93 | VAR_T_CHANGE, | ||
94 | VAR_VARS_NB, | ||
95 | }; | ||
96 | |||
97 | typedef struct SetPTSContext { | ||
98 | const AVClass *class; | ||
99 | char *expr_str; | ||
100 | AVExpr *expr; | ||
101 | double var_values[VAR_VARS_NB]; | ||
102 | enum AVMediaType type; | ||
103 | } SetPTSContext; | ||
104 | |||
105 | #define V(name_) \ | ||
106 | setpts->var_values[VAR_##name_] | ||
107 | |||
108 | 129 | static av_cold int init(AVFilterContext *ctx) | |
109 | { | ||
110 | 129 | SetPTSContext *setpts = ctx->priv; | |
111 | int ret; | ||
112 | |||
113 |
1/2✗ Branch 1 not taken.
✓ Branch 2 taken 129 times.
|
129 | if ((ret = av_expr_parse(&setpts->expr, setpts->expr_str, |
114 | var_names, NULL, NULL, NULL, NULL, 0, ctx)) < 0) { | ||
115 | ✗ | av_log(ctx, AV_LOG_ERROR, "Error while parsing expression '%s'\n", setpts->expr_str); | |
116 | ✗ | return ret; | |
117 | } | ||
118 | |||
119 | 129 | V(N) = 0.0; | |
120 | 129 | V(S) = 0.0; | |
121 | 129 | V(PREV_INPTS) = NAN; | |
122 | 129 | V(PREV_INT) = NAN; | |
123 | 129 | V(PREV_OUTPTS) = NAN; | |
124 | 129 | V(PREV_OUTT) = NAN; | |
125 | 129 | V(STARTPTS) = NAN; | |
126 | 129 | V(STARTT) = NAN; | |
127 | 129 | V(T_CHANGE) = NAN; | |
128 | 129 | return 0; | |
129 | } | ||
130 | |||
131 | 65 | static int config_input(AVFilterLink *inlink) | |
132 | { | ||
133 | 65 | FilterLink *l = ff_filter_link(inlink); | |
134 | 65 | AVFilterContext *ctx = inlink->dst; | |
135 | 65 | SetPTSContext *setpts = ctx->priv; | |
136 | |||
137 | 65 | setpts->type = inlink->type; | |
138 | 65 | V(TB) = av_q2d(inlink->time_base); | |
139 | 65 | V(RTCSTART) = av_gettime(); | |
140 | |||
141 | 65 | V(SR) = V(SAMPLE_RATE) = | |
142 |
2/2✓ Branch 0 taken 54 times.
✓ Branch 1 taken 11 times.
|
65 | setpts->type == AVMEDIA_TYPE_AUDIO ? inlink->sample_rate : NAN; |
143 | |||
144 | 65 | V(FRAME_RATE) = V(FR) = | |
145 |
1/2✓ Branch 0 taken 11 times.
✗ Branch 1 not taken.
|
11 | l->frame_rate.num && l->frame_rate.den ? |
146 |
2/2✓ Branch 0 taken 11 times.
✓ Branch 1 taken 54 times.
|
76 | av_q2d(l->frame_rate) : NAN; |
147 | |||
148 | 65 | av_log(inlink->src, AV_LOG_VERBOSE, "TB:%f FRAME_RATE:%f SAMPLE_RATE:%f\n", | |
149 | V(TB), V(FRAME_RATE), V(SAMPLE_RATE)); | ||
150 | 65 | return 0; | |
151 | } | ||
152 | |||
153 | 11 | static int config_output_video(AVFilterLink *outlink) | |
154 | { | ||
155 | 11 | FilterLink *l = ff_filter_link(outlink); | |
156 | |||
157 | 11 | l->frame_rate = (AVRational){ 1, 0 }; | |
158 | |||
159 | 11 | return 0; | |
160 | } | ||
161 | |||
162 | #define BUF_SIZE 64 | ||
163 | |||
164 | 4742 | static inline char *double2int64str(char *buf, double v) | |
165 | { | ||
166 |
1/2✗ Branch 0 not taken.
✓ Branch 1 taken 4742 times.
|
4742 | if (isnan(v)) snprintf(buf, BUF_SIZE, "nan"); |
167 | 4742 | else snprintf(buf, BUF_SIZE, "%"PRId64, (int64_t)v); | |
168 | 4742 | return buf; | |
169 | } | ||
170 | |||
171 | 2371 | static double eval_pts(SetPTSContext *setpts, AVFilterLink *inlink, AVFrame *frame, int64_t pts) | |
172 | { | ||
173 |
2/2✓ Branch 0 taken 65 times.
✓ Branch 1 taken 2306 times.
|
2371 | if (isnan(V(STARTPTS))) { |
174 |
1/2✓ Branch 0 taken 65 times.
✗ Branch 1 not taken.
|
65 | V(STARTPTS) = TS2D(pts); |
175 |
1/2✓ Branch 0 taken 65 times.
✗ Branch 1 not taken.
|
65 | V(STARTT ) = TS2T(pts, inlink->time_base); |
176 | } | ||
177 |
2/2✓ Branch 0 taken 65 times.
✓ Branch 1 taken 2306 times.
|
2371 | if (isnan(V(T_CHANGE))) { |
178 |
1/2✓ Branch 0 taken 65 times.
✗ Branch 1 not taken.
|
65 | V(T_CHANGE) = TS2T(pts, inlink->time_base); |
179 | } | ||
180 |
1/2✓ Branch 0 taken 2371 times.
✗ Branch 1 not taken.
|
2371 | V(PTS ) = TS2D(pts); |
181 |
1/2✓ Branch 0 taken 2371 times.
✗ Branch 1 not taken.
|
2371 | V(T ) = TS2T(pts, inlink->time_base); |
182 | #if FF_API_FRAME_PKT | ||
183 | FF_DISABLE_DEPRECATION_WARNINGS | ||
184 |
4/4✓ Branch 0 taken 2311 times.
✓ Branch 1 taken 60 times.
✓ Branch 2 taken 1444 times.
✓ Branch 3 taken 867 times.
|
2371 | V(POS ) = !frame || frame->pkt_pos == -1 ? NAN : frame->pkt_pos; |
185 | FF_ENABLE_DEPRECATION_WARNINGS | ||
186 | #endif | ||
187 | 2371 | V(RTCTIME ) = av_gettime(); | |
188 | |||
189 |
2/2✓ Branch 0 taken 2311 times.
✓ Branch 1 taken 60 times.
|
2371 | if (frame) { |
190 |
2/2✓ Branch 0 taken 502 times.
✓ Branch 1 taken 1809 times.
|
2311 | if (inlink->type == AVMEDIA_TYPE_VIDEO) { |
191 |
1/2✗ Branch 0 not taken.
✓ Branch 1 taken 502 times.
|
502 | V(INTERLACED) = !!(frame->flags & AV_FRAME_FLAG_INTERLACED); |
192 |
1/2✓ Branch 0 taken 1809 times.
✗ Branch 1 not taken.
|
1809 | } else if (inlink->type == AVMEDIA_TYPE_AUDIO) { |
193 | 1809 | V(S) = frame->nb_samples; | |
194 | 1809 | V(NB_SAMPLES) = frame->nb_samples; | |
195 | } | ||
196 | } | ||
197 | |||
198 | 2371 | return av_expr_eval(setpts->expr, setpts->var_values, NULL); | |
199 | } | ||
200 | #define d2istr(v) double2int64str((char[BUF_SIZE]){0}, v) | ||
201 | |||
202 | 2311 | static int filter_frame(AVFilterLink *inlink, AVFrame *frame) | |
203 | { | ||
204 | 2311 | SetPTSContext *setpts = inlink->dst->priv; | |
205 | 2311 | int64_t in_pts = frame->pts; | |
206 | double d; | ||
207 | |||
208 | 2311 | d = eval_pts(setpts, inlink, frame, frame->pts); | |
209 |
1/2✓ Branch 0 taken 2311 times.
✗ Branch 1 not taken.
|
2311 | frame->pts = D2TS(d); |
210 | 2311 | frame->duration = 0; | |
211 | |||
212 | 4622 | av_log(inlink->dst, AV_LOG_TRACE, | |
213 | "N:%"PRId64" PTS:%s T:%f", | ||
214 | 2311 | (int64_t)V(N), d2istr(V(PTS)), V(T)); | |
215 |
2/3✓ Branch 0 taken 502 times.
✓ Branch 1 taken 1809 times.
✗ Branch 2 not taken.
|
2311 | switch (inlink->type) { |
216 | 502 | case AVMEDIA_TYPE_VIDEO: | |
217 | 502 | av_log(inlink->dst, AV_LOG_TRACE, " INTERLACED:%"PRId64, | |
218 | 502 | (int64_t)V(INTERLACED)); | |
219 | 502 | break; | |
220 | 1809 | case AVMEDIA_TYPE_AUDIO: | |
221 | 1809 | av_log(inlink->dst, AV_LOG_TRACE, " NB_SAMPLES:%"PRId64" NB_CONSUMED_SAMPLES:%"PRId64, | |
222 | 1809 | (int64_t)V(NB_SAMPLES), | |
223 | 1809 | (int64_t)V(NB_CONSUMED_SAMPLES)); | |
224 | 1809 | break; | |
225 | } | ||
226 |
1/2✓ Branch 0 taken 2311 times.
✗ Branch 1 not taken.
|
2311 | av_log(inlink->dst, AV_LOG_TRACE, " -> PTS:%s T:%f\n", d2istr(d), TS2T(d, inlink->time_base)); |
227 | |||
228 |
2/2✓ Branch 0 taken 502 times.
✓ Branch 1 taken 1809 times.
|
2311 | if (inlink->type == AVMEDIA_TYPE_VIDEO) { |
229 | 502 | V(N) += 1.0; | |
230 | } else { | ||
231 | 1809 | V(N) += frame->nb_samples; | |
232 | } | ||
233 | |||
234 |
1/2✓ Branch 0 taken 2311 times.
✗ Branch 1 not taken.
|
2311 | V(PREV_INPTS ) = TS2D(in_pts); |
235 |
1/2✓ Branch 0 taken 2311 times.
✗ Branch 1 not taken.
|
2311 | V(PREV_INT ) = TS2T(in_pts, inlink->time_base); |
236 |
1/2✓ Branch 0 taken 2311 times.
✗ Branch 1 not taken.
|
2311 | V(PREV_OUTPTS) = TS2D(frame->pts); |
237 |
1/2✓ Branch 0 taken 2311 times.
✗ Branch 1 not taken.
|
2311 | V(PREV_OUTT) = TS2T(frame->pts, inlink->time_base); |
238 |
2/2✓ Branch 0 taken 1809 times.
✓ Branch 1 taken 502 times.
|
2311 | if (setpts->type == AVMEDIA_TYPE_AUDIO) { |
239 | 1809 | V(NB_CONSUMED_SAMPLES) += frame->nb_samples; | |
240 | } | ||
241 | 2311 | return ff_filter_frame(inlink->dst->outputs[0], frame); | |
242 | } | ||
243 | |||
244 | 4706 | static int activate(AVFilterContext *ctx) | |
245 | { | ||
246 | 4706 | SetPTSContext *setpts = ctx->priv; | |
247 | 4706 | AVFilterLink *inlink = ctx->inputs[0]; | |
248 | 4706 | AVFilterLink *outlink = ctx->outputs[0]; | |
249 | AVFrame *in; | ||
250 | int status; | ||
251 | int64_t pts; | ||
252 | int ret; | ||
253 | |||
254 |
2/2✓ Branch 1 taken 4 times.
✓ Branch 2 taken 4702 times.
|
4706 | FF_FILTER_FORWARD_STATUS_BACK(outlink, inlink); |
255 | |||
256 | 4702 | ret = ff_inlink_consume_frame(inlink, &in); | |
257 |
1/2✗ Branch 0 not taken.
✓ Branch 1 taken 4702 times.
|
4702 | if (ret < 0) |
258 | ✗ | return ret; | |
259 |
2/2✓ Branch 0 taken 2311 times.
✓ Branch 1 taken 2391 times.
|
4702 | if (ret > 0) |
260 | 2311 | return filter_frame(inlink, in); | |
261 | |||
262 |
2/2✓ Branch 1 taken 60 times.
✓ Branch 2 taken 2331 times.
|
2391 | if (ff_inlink_acknowledge_status(inlink, &status, &pts)) { |
263 | 60 | double d = eval_pts(setpts, inlink, NULL, pts); | |
264 | |||
265 |
1/2✓ Branch 0 taken 60 times.
✗ Branch 1 not taken.
|
180 | av_log(ctx, AV_LOG_TRACE, "N:EOF PTS:%s T:%f -> PTS:%s T:%f\n", |
266 | 120 | d2istr(V(PTS)), V(T), d2istr(d), TS2T(d, inlink->time_base)); | |
267 |
1/2✓ Branch 0 taken 60 times.
✗ Branch 1 not taken.
|
60 | ff_outlink_set_status(outlink, status, D2TS(d)); |
268 | 60 | return 0; | |
269 | } | ||
270 | |||
271 |
1/2✓ Branch 1 taken 2331 times.
✗ Branch 2 not taken.
|
2331 | FF_FILTER_FORWARD_WANTED(outlink, inlink); |
272 | |||
273 | ✗ | return FFERROR_NOT_READY; | |
274 | } | ||
275 | |||
276 | 129 | static av_cold void uninit(AVFilterContext *ctx) | |
277 | { | ||
278 | 129 | SetPTSContext *setpts = ctx->priv; | |
279 | 129 | av_expr_free(setpts->expr); | |
280 | 129 | setpts->expr = NULL; | |
281 | 129 | } | |
282 | |||
283 | ✗ | static int process_command(AVFilterContext *ctx, const char *cmd, const char *arg, | |
284 | char *res, int res_len, int flags) | ||
285 | { | ||
286 | ✗ | SetPTSContext *setpts = ctx->priv; | |
287 | AVExpr *new_expr; | ||
288 | int ret; | ||
289 | |||
290 | ✗ | ret = ff_filter_process_command(ctx, cmd, arg, res, res_len, flags); | |
291 | |||
292 | ✗ | if (ret < 0) | |
293 | ✗ | return ret; | |
294 | |||
295 | ✗ | if (!strcmp(cmd, "expr")) { | |
296 | ✗ | ret = av_expr_parse(&new_expr, arg, var_names, NULL, NULL, NULL, NULL, 0, ctx); | |
297 | // Only free and replace previous expression if new one succeeds, | ||
298 | // otherwise defensively keep everything intact even if reporting an error. | ||
299 | ✗ | if (ret < 0) { | |
300 | ✗ | av_log(ctx, AV_LOG_ERROR, "Error while parsing expression '%s'\n", arg); | |
301 | } else { | ||
302 | ✗ | av_expr_free(setpts->expr); | |
303 | ✗ | setpts->expr = new_expr; | |
304 | ✗ | V(T_CHANGE) = NAN; | |
305 | } | ||
306 | } else { | ||
307 | ✗ | ret = AVERROR(EINVAL); | |
308 | } | ||
309 | |||
310 | ✗ | return ret; | |
311 | } | ||
312 | #undef V | ||
313 | |||
314 | #define OFFSET(x) offsetof(SetPTSContext, x) | ||
315 | #define V AV_OPT_FLAG_VIDEO_PARAM | ||
316 | #define A AV_OPT_FLAG_AUDIO_PARAM | ||
317 | #define R AV_OPT_FLAG_RUNTIME_PARAM | ||
318 | #define F AV_OPT_FLAG_FILTERING_PARAM | ||
319 | |||
320 | #if CONFIG_SETPTS_FILTER | ||
321 | static const AVOption setpts_options[] = { | ||
322 | { "expr", "Expression determining the frame timestamp", OFFSET(expr_str), AV_OPT_TYPE_STRING, { .str = "PTS" }, .flags = V|F|R }, | ||
323 | { NULL } | ||
324 | }; | ||
325 | AVFILTER_DEFINE_CLASS(setpts); | ||
326 | |||
327 | static const AVFilterPad avfilter_vf_setpts_inputs[] = { | ||
328 | { | ||
329 | .name = "default", | ||
330 | .type = AVMEDIA_TYPE_VIDEO, | ||
331 | .config_props = config_input, | ||
332 | }, | ||
333 | }; | ||
334 | |||
335 | static const AVFilterPad outputs_video[] = { | ||
336 | { | ||
337 | .name = "default", | ||
338 | .type = AVMEDIA_TYPE_VIDEO, | ||
339 | .config_props = config_output_video, | ||
340 | }, | ||
341 | }; | ||
342 | |||
343 | const AVFilter ff_vf_setpts = { | ||
344 | .name = "setpts", | ||
345 | .description = NULL_IF_CONFIG_SMALL("Set PTS for the output video frame."), | ||
346 | .init = init, | ||
347 | .activate = activate, | ||
348 | .uninit = uninit, | ||
349 | .process_command = process_command, | ||
350 | .flags = AVFILTER_FLAG_METADATA_ONLY, | ||
351 | |||
352 | .priv_size = sizeof(SetPTSContext), | ||
353 | .priv_class = &setpts_class, | ||
354 | |||
355 | FILTER_INPUTS(avfilter_vf_setpts_inputs), | ||
356 | FILTER_OUTPUTS(outputs_video), | ||
357 | }; | ||
358 | #endif /* CONFIG_SETPTS_FILTER */ | ||
359 | |||
360 | #if CONFIG_ASETPTS_FILTER | ||
361 | |||
362 | static const AVOption asetpts_options[] = { | ||
363 | { "expr", "Expression determining the frame timestamp", OFFSET(expr_str), AV_OPT_TYPE_STRING, { .str = "PTS" }, .flags = A|F|R }, | ||
364 | { NULL } | ||
365 | }; | ||
366 | AVFILTER_DEFINE_CLASS(asetpts); | ||
367 | |||
368 | static const AVFilterPad asetpts_inputs[] = { | ||
369 | { | ||
370 | .name = "default", | ||
371 | .type = AVMEDIA_TYPE_AUDIO, | ||
372 | .config_props = config_input, | ||
373 | }, | ||
374 | }; | ||
375 | |||
376 | const AVFilter ff_af_asetpts = { | ||
377 | .name = "asetpts", | ||
378 | .description = NULL_IF_CONFIG_SMALL("Set PTS for the output audio frame."), | ||
379 | .init = init, | ||
380 | .activate = activate, | ||
381 | .uninit = uninit, | ||
382 | .process_command = process_command, | ||
383 | .priv_size = sizeof(SetPTSContext), | ||
384 | .priv_class = &asetpts_class, | ||
385 | .flags = AVFILTER_FLAG_METADATA_ONLY, | ||
386 | FILTER_INPUTS(asetpts_inputs), | ||
387 | FILTER_OUTPUTS(ff_audio_default_filterpad), | ||
388 | }; | ||
389 | #endif /* CONFIG_ASETPTS_FILTER */ | ||
390 |