FFmpeg coverage


Directory: ../../../ffmpeg/
File: src/libavcodec/setts_bsf.c
Date: 2021-09-24 20:55:06
Exec Total Coverage
Lines: 0 68 0.0%
Branches: 0 22 0.0%

Line Branch Exec Source
1 /*
2 * Copyright (c) 2021 Paul B Mahol
3 *
4 * This file is part of FFmpeg.
5 *
6 * FFmpeg is free software; you can redistribute it and/or
7 * modify it under the terms of the GNU Lesser General Public
8 * License as published by the Free Software Foundation; either
9 * version 2.1 of the License, or (at your option) any later version.
10 *
11 * FFmpeg is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
14 * Lesser General Public License for more details.
15 *
16 * You should have received a copy of the GNU Lesser General Public
17 * License along with FFmpeg; if not, write to the Free Software
18 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
19 */
20
21 /**
22 * @file
23 * Change the PTS/DTS timestamps.
24 */
25
26 #include "libavutil/opt.h"
27 #include "libavutil/eval.h"
28
29 #include "bsf.h"
30 #include "bsf_internal.h"
31
32 static const char *const var_names[] = {
33 "N", ///< frame number (starting at zero)
34 "TS",
35 "POS", ///< original position in the file of the frame
36 "PREV_INPTS", ///< previous input PTS
37 "PREV_INDTS", ///< previous input DTS
38 "PREV_OUTPTS", ///< previous output PTS
39 "PREV_OUTDTS", ///< previous output DTS
40 "PTS", ///< original PTS in the file of the frame
41 "DTS", ///< original DTS in the file of the frame
42 "STARTPTS", ///< PTS at start of movie
43 "STARTDTS", ///< DTS at start of movie
44 "TB", ///< timebase of the stream
45 "SR", ///< sample rate of the stream
46 "NOPTS", ///< The AV_NOPTS_VALUE constant
47 NULL
48 };
49
50 enum var_name {
51 VAR_N,
52 VAR_TS,
53 VAR_POS,
54 VAR_PREV_INPTS,
55 VAR_PREV_INDTS,
56 VAR_PREV_OUTPTS,
57 VAR_PREV_OUTDTS,
58 VAR_PTS,
59 VAR_DTS,
60 VAR_STARTPTS,
61 VAR_STARTDTS,
62 VAR_TB,
63 VAR_SR,
64 VAR_NOPTS,
65 VAR_VARS_NB
66 };
67
68 typedef struct SetTSContext {
69 const AVClass *class;
70
71 char *ts_str;
72 char *pts_str;
73 char *dts_str;
74
75 int64_t frame_number;
76
77 int64_t start_pts;
78 int64_t start_dts;
79 int64_t prev_inpts;
80 int64_t prev_indts;
81 int64_t prev_outpts;
82 int64_t prev_outdts;
83
84 double var_values[VAR_VARS_NB];
85
86 AVExpr *ts_expr;
87 AVExpr *pts_expr;
88 AVExpr *dts_expr;
89 } SetTSContext;
90
91 static int setts_init(AVBSFContext *ctx)
92 {
93 SetTSContext *s = ctx->priv_data;
94 int ret;
95
96 if ((ret = av_expr_parse(&s->ts_expr, s->ts_str,
97 var_names, NULL, NULL, NULL, NULL, 0, ctx)) < 0) {
98 av_log(ctx, AV_LOG_ERROR, "Error while parsing ts expression '%s'\n", s->ts_str);
99 return ret;
100 }
101
102 if (s->pts_str) {
103 if ((ret = av_expr_parse(&s->pts_expr, s->pts_str,
104 var_names, NULL, NULL, NULL, NULL, 0, ctx)) < 0) {
105 av_log(ctx, AV_LOG_ERROR, "Error while parsing pts expression '%s'\n", s->pts_str);
106 return ret;
107 }
108 }
109
110 if (s->dts_str) {
111 if ((ret = av_expr_parse(&s->dts_expr, s->dts_str,
112 var_names, NULL, NULL, NULL, NULL, 0, ctx)) < 0) {
113 av_log(ctx, AV_LOG_ERROR, "Error while parsing dts expression '%s'\n", s->dts_str);
114 return ret;
115 }
116 }
117
118 s->frame_number= 0;
119 s->start_pts = AV_NOPTS_VALUE;
120 s->start_dts = AV_NOPTS_VALUE;
121 s->prev_inpts = AV_NOPTS_VALUE;
122 s->prev_indts = AV_NOPTS_VALUE;
123 s->prev_outpts = AV_NOPTS_VALUE;
124 s->prev_outdts = AV_NOPTS_VALUE;
125 s->var_values[VAR_NOPTS] = AV_NOPTS_VALUE;
126
127 return 0;
128 }
129
130 static int setts_filter(AVBSFContext *ctx, AVPacket *pkt)
131 {
132 SetTSContext *s = ctx->priv_data;
133 int64_t new_ts, new_pts, new_dts;
134 int ret;
135
136 ret = ff_bsf_get_packet_ref(ctx, pkt);
137 if (ret < 0)
138 return ret;
139
140 if (s->start_pts == AV_NOPTS_VALUE)
141 s->start_pts = pkt->pts;
142
143 if (s->start_dts == AV_NOPTS_VALUE)
144 s->start_dts = pkt->dts;
145
146 s->var_values[VAR_N] = s->frame_number++;
147 s->var_values[VAR_TS] = pkt->dts;
148 s->var_values[VAR_POS] = pkt->pos;
149 s->var_values[VAR_PTS] = pkt->pts;
150 s->var_values[VAR_DTS] = pkt->dts;
151 s->var_values[VAR_PREV_INPTS] = s->prev_inpts;
152 s->var_values[VAR_PREV_INDTS] = s->prev_indts;
153 s->var_values[VAR_PREV_OUTPTS] = s->prev_outpts;
154 s->var_values[VAR_PREV_OUTDTS] = s->prev_outdts;
155 s->var_values[VAR_STARTPTS] = s->start_pts;
156 s->var_values[VAR_STARTDTS] = s->start_dts;
157 s->var_values[VAR_TB] = ctx->time_base_out.den ? av_q2d(ctx->time_base_out) : 0;
158 s->var_values[VAR_SR] = ctx->par_in->sample_rate;
159
160 new_ts = llrint(av_expr_eval(s->ts_expr, s->var_values, NULL));
161
162 if (s->pts_str) {
163 s->var_values[VAR_TS] = pkt->pts;
164 new_pts = llrint(av_expr_eval(s->pts_expr, s->var_values, NULL));
165 } else {
166 new_pts = new_ts;
167 }
168
169 if (s->dts_str) {
170 s->var_values[VAR_TS] = pkt->dts;
171 new_dts = llrint(av_expr_eval(s->dts_expr, s->var_values, NULL));
172 } else {
173 new_dts = new_ts;
174 }
175
176 s->prev_inpts = pkt->pts;
177 s->prev_indts = pkt->dts;
178 s->prev_outpts = new_pts;
179 s->prev_outdts = new_dts;
180
181 pkt->pts = new_pts;
182 pkt->dts = new_dts;
183
184 return ret;
185 }
186
187 static void setts_close(AVBSFContext *bsf)
188 {
189 SetTSContext *s = bsf->priv_data;
190
191 av_expr_free(s->ts_expr);
192 s->ts_expr = NULL;
193 av_expr_free(s->pts_expr);
194 s->pts_expr = NULL;
195 av_expr_free(s->dts_expr);
196 s->dts_expr = NULL;
197 }
198
199 #define OFFSET(x) offsetof(SetTSContext, x)
200 #define FLAGS (AV_OPT_FLAG_VIDEO_PARAM|AV_OPT_FLAG_AUDIO_PARAM|AV_OPT_FLAG_SUBTITLE_PARAM|AV_OPT_FLAG_BSF_PARAM)
201
202 static const AVOption options[] = {
203 { "ts", "set expression for packet PTS and DTS", OFFSET(ts_str), AV_OPT_TYPE_STRING, {.str="TS"}, 0, 0, FLAGS },
204 { "pts", "set expression for packet PTS", OFFSET(pts_str), AV_OPT_TYPE_STRING, {.str=NULL}, 0, 0, FLAGS },
205 { "dts", "set expression for packet DTS", OFFSET(dts_str), AV_OPT_TYPE_STRING, {.str=NULL}, 0, 0, FLAGS },
206 { NULL },
207 };
208
209 static const AVClass setts_class = {
210 .class_name = "setts_bsf",
211 .item_name = av_default_item_name,
212 .option = options,
213 .version = LIBAVUTIL_VERSION_INT,
214 };
215
216 const AVBitStreamFilter ff_setts_bsf = {
217 .name = "setts",
218 .priv_data_size = sizeof(SetTSContext),
219 .priv_class = &setts_class,
220 .init = setts_init,
221 .close = setts_close,
222 .filter = setts_filter,
223 };
224