FFmpeg coverage


Directory: ../../../ffmpeg/
File: src/libavfilter/setpts.c
Date: 2022-11-26 13:19:19
Exec Total Coverage
Lines: 99 103 96.1%
Branches: 45 63 71.4%

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 "internal.h"
40 #include "video.h"
41
42 static const char *const var_names[] = {
43 "FRAME_RATE", ///< defined only for constant frame-rate video
44 "INTERLACED", ///< tell if the current frame is interlaced
45 "N", ///< frame / sample number (starting at zero)
46 "NB_CONSUMED_SAMPLES", ///< number of samples consumed by the filter (only audio)
47 "NB_SAMPLES", ///< number of samples in the current frame (only audio)
48 "POS", ///< original position in the file of the frame
49 "PREV_INPTS", ///< previous input PTS
50 "PREV_INT", ///< previous input time in seconds
51 "PREV_OUTPTS", ///< previous output PTS
52 "PREV_OUTT", ///< previous output time in seconds
53 "PTS", ///< original pts in the file of the frame
54 "SAMPLE_RATE", ///< sample rate (only audio)
55 "STARTPTS", ///< PTS at start of movie
56 "STARTT", ///< time at start of movie
57 "T", ///< original time in the file of the frame
58 "TB", ///< timebase
59 "RTCTIME", ///< wallclock (RTC) time in micro seconds
60 "RTCSTART", ///< wallclock (RTC) time at the start of the movie in micro seconds
61 "S", // Number of samples in the current frame
62 "SR", // Audio sample rate
63 "FR", ///< defined only for constant frame-rate video
64 NULL
65 };
66
67 enum var_name {
68 VAR_FRAME_RATE,
69 VAR_INTERLACED,
70 VAR_N,
71 VAR_NB_CONSUMED_SAMPLES,
72 VAR_NB_SAMPLES,
73 VAR_POS,
74 VAR_PREV_INPTS,
75 VAR_PREV_INT,
76 VAR_PREV_OUTPTS,
77 VAR_PREV_OUTT,
78 VAR_PTS,
79 VAR_SAMPLE_RATE,
80 VAR_STARTPTS,
81 VAR_STARTT,
82 VAR_T,
83 VAR_TB,
84 VAR_RTCTIME,
85 VAR_RTCSTART,
86 VAR_S,
87 VAR_SR,
88 VAR_FR,
89 VAR_VARS_NB
90 };
91
92 typedef struct SetPTSContext {
93 const AVClass *class;
94 char *expr_str;
95 AVExpr *expr;
96 double var_values[VAR_VARS_NB];
97 enum AVMediaType type;
98 } SetPTSContext;
99
100 56 static av_cold int init(AVFilterContext *ctx)
101 {
102 56 SetPTSContext *setpts = ctx->priv;
103 int ret;
104
105
1/2
✗ Branch 1 not taken.
✓ Branch 2 taken 56 times.
56 if ((ret = av_expr_parse(&setpts->expr, setpts->expr_str,
106 var_names, NULL, NULL, NULL, NULL, 0, ctx)) < 0) {
107 av_log(ctx, AV_LOG_ERROR, "Error while parsing expression '%s'\n", setpts->expr_str);
108 return ret;
109 }
110
111 56 setpts->var_values[VAR_N] = 0.0;
112 56 setpts->var_values[VAR_S] = 0.0;
113 56 setpts->var_values[VAR_PREV_INPTS] = NAN;
114 56 setpts->var_values[VAR_PREV_INT] = NAN;
115 56 setpts->var_values[VAR_PREV_OUTPTS] = NAN;
116 56 setpts->var_values[VAR_PREV_OUTT] = NAN;
117 56 setpts->var_values[VAR_STARTPTS] = NAN;
118 56 setpts->var_values[VAR_STARTT] = NAN;
119 56 return 0;
120 }
121
122 55 static int config_input(AVFilterLink *inlink)
123 {
124 55 AVFilterContext *ctx = inlink->dst;
125 55 SetPTSContext *setpts = ctx->priv;
126
127 55 setpts->type = inlink->type;
128 55 setpts->var_values[VAR_TB] = av_q2d(inlink->time_base);
129 55 setpts->var_values[VAR_RTCSTART] = av_gettime();
130
131 55 setpts->var_values[VAR_SR] =
132 55 setpts->var_values[VAR_SAMPLE_RATE] =
133
2/2
✓ Branch 0 taken 53 times.
✓ Branch 1 taken 2 times.
55 setpts->type == AVMEDIA_TYPE_AUDIO ? inlink->sample_rate : NAN;
134
135 55 setpts->var_values[VAR_FRAME_RATE] =
136 112 setpts->var_values[VAR_FR] = inlink->frame_rate.num &&
137
1/2
✓ Branch 0 taken 2 times.
✗ Branch 1 not taken.
2 inlink->frame_rate.den ?
138
2/2
✓ Branch 0 taken 2 times.
✓ Branch 1 taken 53 times.
57 av_q2d(inlink->frame_rate) : NAN;
139
140 55 av_log(inlink->src, AV_LOG_VERBOSE, "TB:%f FRAME_RATE:%f SAMPLE_RATE:%f\n",
141 setpts->var_values[VAR_TB],
142 setpts->var_values[VAR_FRAME_RATE],
143 setpts->var_values[VAR_SAMPLE_RATE]);
144 55 return 0;
145 }
146
147 #define BUF_SIZE 64
148
149 5724 static inline char *double2int64str(char *buf, double v)
150 {
151
2/2
✓ Branch 0 taken 880 times.
✓ Branch 1 taken 4844 times.
5724 if (isnan(v)) snprintf(buf, BUF_SIZE, "nan");
152 4844 else snprintf(buf, BUF_SIZE, "%"PRId64, (int64_t)v);
153 5724 return buf;
154 }
155
156 1908 static double eval_pts(SetPTSContext *setpts, AVFilterLink *inlink, AVFrame *frame, int64_t pts)
157 {
158
2/2
✓ Branch 0 taken 55 times.
✓ Branch 1 taken 1853 times.
1908 if (isnan(setpts->var_values[VAR_STARTPTS])) {
159
1/2
✓ Branch 0 taken 55 times.
✗ Branch 1 not taken.
55 setpts->var_values[VAR_STARTPTS] = TS2D(pts);
160
1/2
✓ Branch 0 taken 55 times.
✗ Branch 1 not taken.
55 setpts->var_values[VAR_STARTT ] = TS2T(pts, inlink->time_base);
161 }
162
1/2
✓ Branch 0 taken 1908 times.
✗ Branch 1 not taken.
1908 setpts->var_values[VAR_PTS ] = TS2D(pts);
163
1/2
✓ Branch 0 taken 1908 times.
✗ Branch 1 not taken.
1908 setpts->var_values[VAR_T ] = TS2T(pts, inlink->time_base);
164
4/4
✓ Branch 0 taken 1853 times.
✓ Branch 1 taken 55 times.
✓ Branch 2 taken 1028 times.
✓ Branch 3 taken 825 times.
1908 setpts->var_values[VAR_POS ] = !frame || frame->pkt_pos == -1 ? NAN : frame->pkt_pos;
165 1908 setpts->var_values[VAR_RTCTIME ] = av_gettime();
166
167
2/2
✓ Branch 0 taken 1853 times.
✓ Branch 1 taken 55 times.
1908 if (frame) {
168
2/2
✓ Branch 0 taken 61 times.
✓ Branch 1 taken 1792 times.
1853 if (inlink->type == AVMEDIA_TYPE_VIDEO) {
169 61 setpts->var_values[VAR_INTERLACED] = frame->interlaced_frame;
170
1/2
✓ Branch 0 taken 1792 times.
✗ Branch 1 not taken.
1792 } else if (inlink->type == AVMEDIA_TYPE_AUDIO) {
171 1792 setpts->var_values[VAR_S] = frame->nb_samples;
172 1792 setpts->var_values[VAR_NB_SAMPLES] = frame->nb_samples;
173 }
174 }
175
176 1908 return av_expr_eval(setpts->expr, setpts->var_values, NULL);
177 }
178 #define d2istr(v) double2int64str((char[BUF_SIZE]){0}, v)
179
180 1853 static int filter_frame(AVFilterLink *inlink, AVFrame *frame)
181 {
182 1853 SetPTSContext *setpts = inlink->dst->priv;
183 1853 int64_t in_pts = frame->pts;
184 double d;
185
186 1853 d = eval_pts(setpts, inlink, frame, frame->pts);
187
1/2
✓ Branch 0 taken 1853 times.
✗ Branch 1 not taken.
1853 frame->pts = D2TS(d);
188
189 5559 av_log(inlink->dst, AV_LOG_TRACE,
190 "N:%"PRId64" PTS:%s T:%f POS:%s",
191 1853 (int64_t)setpts->var_values[VAR_N],
192 1853 d2istr(setpts->var_values[VAR_PTS]),
193 setpts->var_values[VAR_T],
194 1853 d2istr(setpts->var_values[VAR_POS]));
195
2/3
✓ Branch 0 taken 61 times.
✓ Branch 1 taken 1792 times.
✗ Branch 2 not taken.
1853 switch (inlink->type) {
196 61 case AVMEDIA_TYPE_VIDEO:
197 61 av_log(inlink->dst, AV_LOG_TRACE, " INTERLACED:%"PRId64,
198 61 (int64_t)setpts->var_values[VAR_INTERLACED]);
199 61 break;
200 1792 case AVMEDIA_TYPE_AUDIO:
201 1792 av_log(inlink->dst, AV_LOG_TRACE, " NB_SAMPLES:%"PRId64" NB_CONSUMED_SAMPLES:%"PRId64,
202 1792 (int64_t)setpts->var_values[VAR_NB_SAMPLES],
203 1792 (int64_t)setpts->var_values[VAR_NB_CONSUMED_SAMPLES]);
204 1792 break;
205 }
206
1/2
✓ Branch 0 taken 1853 times.
✗ Branch 1 not taken.
1853 av_log(inlink->dst, AV_LOG_TRACE, " -> PTS:%s T:%f\n", d2istr(d), TS2T(d, inlink->time_base));
207
208
2/2
✓ Branch 0 taken 61 times.
✓ Branch 1 taken 1792 times.
1853 if (inlink->type == AVMEDIA_TYPE_VIDEO) {
209 61 setpts->var_values[VAR_N] += 1.0;
210 } else {
211 1792 setpts->var_values[VAR_N] += frame->nb_samples;
212 }
213
214
1/2
✓ Branch 0 taken 1853 times.
✗ Branch 1 not taken.
1853 setpts->var_values[VAR_PREV_INPTS ] = TS2D(in_pts);
215
1/2
✓ Branch 0 taken 1853 times.
✗ Branch 1 not taken.
1853 setpts->var_values[VAR_PREV_INT ] = TS2T(in_pts, inlink->time_base);
216
1/2
✓ Branch 0 taken 1853 times.
✗ Branch 1 not taken.
1853 setpts->var_values[VAR_PREV_OUTPTS] = TS2D(frame->pts);
217
1/2
✓ Branch 0 taken 1853 times.
✗ Branch 1 not taken.
1853 setpts->var_values[VAR_PREV_OUTT] = TS2T(frame->pts, inlink->time_base);
218
2/2
✓ Branch 0 taken 1792 times.
✓ Branch 1 taken 61 times.
1853 if (setpts->type == AVMEDIA_TYPE_AUDIO) {
219 1792 setpts->var_values[VAR_NB_CONSUMED_SAMPLES] += frame->nb_samples;
220 }
221 1853 return ff_filter_frame(inlink->dst->outputs[0], frame);
222 }
223
224 3815 static int activate(AVFilterContext *ctx)
225 {
226 3815 SetPTSContext *setpts = ctx->priv;
227 3815 AVFilterLink *inlink = ctx->inputs[0];
228 3815 AVFilterLink *outlink = ctx->outputs[0];
229 AVFrame *in;
230 int status;
231 int64_t pts;
232 int ret;
233
234
2/2
✓ Branch 1 taken 53 times.
✓ Branch 2 taken 3762 times.
3815 FF_FILTER_FORWARD_STATUS_BACK(outlink, inlink);
235
236 3762 ret = ff_inlink_consume_frame(inlink, &in);
237
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 3762 times.
3762 if (ret < 0)
238 return ret;
239
2/2
✓ Branch 0 taken 1853 times.
✓ Branch 1 taken 1909 times.
3762 if (ret > 0)
240 1853 return filter_frame(inlink, in);
241
242
2/2
✓ Branch 1 taken 55 times.
✓ Branch 2 taken 1854 times.
1909 if (ff_inlink_acknowledge_status(inlink, &status, &pts)) {
243 55 double d = eval_pts(setpts, inlink, NULL, pts);
244
245
1/2
✓ Branch 0 taken 55 times.
✗ Branch 1 not taken.
165 av_log(ctx, AV_LOG_TRACE, "N:EOF PTS:%s T:%f POS:%s -> PTS:%s T:%f\n",
246 55 d2istr(setpts->var_values[VAR_PTS]),
247 setpts->var_values[VAR_T],
248 55 d2istr(setpts->var_values[VAR_POS]),
249 110 d2istr(d), TS2T(d, inlink->time_base));
250
1/2
✓ Branch 0 taken 55 times.
✗ Branch 1 not taken.
55 ff_outlink_set_status(outlink, status, D2TS(d));
251 55 return 0;
252 }
253
254
1/2
✓ Branch 1 taken 1854 times.
✗ Branch 2 not taken.
1854 FF_FILTER_FORWARD_WANTED(outlink, inlink);
255
256 return FFERROR_NOT_READY;
257 }
258
259 56 static av_cold void uninit(AVFilterContext *ctx)
260 {
261 56 SetPTSContext *setpts = ctx->priv;
262 56 av_expr_free(setpts->expr);
263 56 setpts->expr = NULL;
264 56 }
265
266 #define OFFSET(x) offsetof(SetPTSContext, x)
267 #define V AV_OPT_FLAG_VIDEO_PARAM
268 #define A AV_OPT_FLAG_AUDIO_PARAM
269 #define F AV_OPT_FLAG_FILTERING_PARAM
270
271 #if CONFIG_SETPTS_FILTER
272 static const AVOption setpts_options[] = {
273 { "expr", "Expression determining the frame timestamp", OFFSET(expr_str), AV_OPT_TYPE_STRING, { .str = "PTS" }, .flags = V|F },
274 { NULL }
275 };
276 AVFILTER_DEFINE_CLASS(setpts);
277
278 static const AVFilterPad avfilter_vf_setpts_inputs[] = {
279 {
280 .name = "default",
281 .type = AVMEDIA_TYPE_VIDEO,
282 .config_props = config_input,
283 },
284 };
285
286 static const AVFilterPad avfilter_vf_setpts_outputs[] = {
287 {
288 .name = "default",
289 .type = AVMEDIA_TYPE_VIDEO,
290 },
291 };
292
293 const AVFilter ff_vf_setpts = {
294 .name = "setpts",
295 .description = NULL_IF_CONFIG_SMALL("Set PTS for the output video frame."),
296 .init = init,
297 .activate = activate,
298 .uninit = uninit,
299 .flags = AVFILTER_FLAG_METADATA_ONLY,
300
301 .priv_size = sizeof(SetPTSContext),
302 .priv_class = &setpts_class,
303
304 FILTER_INPUTS(avfilter_vf_setpts_inputs),
305 FILTER_OUTPUTS(avfilter_vf_setpts_outputs),
306 };
307 #endif /* CONFIG_SETPTS_FILTER */
308
309 #if CONFIG_ASETPTS_FILTER
310
311 static const AVOption asetpts_options[] = {
312 { "expr", "Expression determining the frame timestamp", OFFSET(expr_str), AV_OPT_TYPE_STRING, { .str = "PTS" }, .flags = A|F },
313 { NULL }
314 };
315 AVFILTER_DEFINE_CLASS(asetpts);
316
317 static const AVFilterPad asetpts_inputs[] = {
318 {
319 .name = "default",
320 .type = AVMEDIA_TYPE_AUDIO,
321 .config_props = config_input,
322 },
323 };
324
325 static const AVFilterPad asetpts_outputs[] = {
326 {
327 .name = "default",
328 .type = AVMEDIA_TYPE_AUDIO,
329 },
330 };
331
332 const AVFilter ff_af_asetpts = {
333 .name = "asetpts",
334 .description = NULL_IF_CONFIG_SMALL("Set PTS for the output audio frame."),
335 .init = init,
336 .activate = activate,
337 .uninit = uninit,
338 .priv_size = sizeof(SetPTSContext),
339 .priv_class = &asetpts_class,
340 .flags = AVFILTER_FLAG_METADATA_ONLY,
341 FILTER_INPUTS(asetpts_inputs),
342 FILTER_OUTPUTS(asetpts_outputs),
343 };
344 #endif /* CONFIG_ASETPTS_FILTER */
345