FFmpeg
textutils.c
Go to the documentation of this file.
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 /**
20  * @file
21  * text expansion utilities
22  */
23 
24 #include <fenv.h>
25 #include <math.h>
26 #include <string.h>
27 
28 #include "textutils.h"
29 #include "libavutil/avutil.h"
30 #include "libavutil/error.h"
31 #include "libavutil/file.h"
32 #include "libavutil/time.h"
33 
34 static int ff_expand_text_function_internal(FFExpandTextContext *expand_text, AVBPrint *bp,
35  char *name, unsigned argc, char **argv)
36 {
37  void *log_ctx = expand_text->log_ctx;
38  FFExpandTextFunction *functions = expand_text->functions;
39  unsigned i;
40 
41  for (i = 0; i < expand_text->functions_nb; i++) {
42  if (strcmp(name, functions[i].name))
43  continue;
44  if (argc < functions[i].argc_min) {
45  av_log(log_ctx, AV_LOG_ERROR, "%%{%s} requires at least %d arguments\n",
46  name, functions[i].argc_min);
47  return AVERROR(EINVAL);
48  }
49  if (argc > functions[i].argc_max) {
50  av_log(log_ctx, AV_LOG_ERROR, "%%{%s} requires at most %d arguments\n",
51  name, functions[i].argc_max);
52  return AVERROR(EINVAL);
53  }
54  break;
55  }
56  if (i >= expand_text->functions_nb) {
57  av_log(log_ctx, AV_LOG_ERROR, "%%{%s} is not known\n", name);
58  return AVERROR(EINVAL);
59  }
60 
61  return functions[i].func(log_ctx, bp, name, argc, argv);
62 }
63 
64 /**
65  * Expand text template pointed to by *rtext.
66  *
67  * Expand text template defined in text using the logic defined in a text
68  * expander object.
69  *
70  * This function expects the text to be in the format %{FUNCTION_NAME[:PARAMS]},
71  * where PARAMS is a sequence of strings separated by : and represents the function
72  * arguments to use for the function evaluation.
73  *
74  * @param text_expander TextExpander object used to expand the text
75  * @param bp BPrint object where the expanded text is written to
76  * @param rtext pointer to pointer to the text to expand, it is updated to point
77  * to the next part of the template to process
78  * @return negative value corresponding to an AVERROR error code in case of
79  * errors, a non-negative value otherwise
80  */
81 static int ff_expand_text_function(FFExpandTextContext *expand_text, AVBPrint *bp, char **rtext)
82 {
83  void *log_ctx = expand_text->log_ctx;
84  const char *text = *rtext;
85  char *argv[16] = { NULL };
86  unsigned argc = 0, i;
87  int ret;
88 
89  if (*text != '{') {
90  av_log(log_ctx, AV_LOG_ERROR, "Stray %% near '%s'\n", text);
91  return AVERROR(EINVAL);
92  }
93  text++;
94  while (1) {
95  if (!(argv[argc++] = av_get_token(&text, ":}"))) {
96  ret = AVERROR(ENOMEM);
97  goto end;
98  }
99  if (!*text) {
100  av_log(log_ctx, AV_LOG_ERROR, "Unterminated %%{} near '%s'\n", *rtext);
101  ret = AVERROR(EINVAL);
102  goto end;
103  }
104  if (argc == FF_ARRAY_ELEMS(argv))
105  av_freep(&argv[--argc]); /* error will be caught later */
106  if (*text == '}')
107  break;
108  text++;
109  }
110 
111  if ((ret = ff_expand_text_function_internal(expand_text, bp, argv[0], argc - 1, argv + 1)) < 0)
112  goto end;
113  ret = 0;
114  *rtext = (char *)text + 1;
115 
116 end:
117  for (i = 0; i < argc; i++)
118  av_freep(&argv[i]);
119  return ret;
120 }
121 
122 int ff_expand_text(FFExpandTextContext *expand_text, char *text, AVBPrint *bp)
123 {
124  int ret;
125 
126  av_bprint_clear(bp);
127  if (!text)
128  return 0;
129 
130  while (*text) {
131  if (*text == '\\' && text[1]) {
132  av_bprint_chars(bp, text[1], 1);
133  text += 2;
134  } else if (*text == '%') {
135  text++;
136  if ((ret = ff_expand_text_function(expand_text, bp, &text)) < 0)
137  return ret;
138  } else {
139  av_bprint_chars(bp, *text, 1);
140  text++;
141  }
142  }
143  if (!av_bprint_is_complete(bp))
144  return AVERROR(ENOMEM);
145  return 0;
146 }
147 
148 int ff_print_pts(void *log_ctx, AVBPrint *bp, double pts, const char *delta,
149  const char *fmt, const char *strftime_fmt)
150 {
151  int ret;
152 
153  if (delta) {
154  int64_t delta_i;
155  if ((ret = av_parse_time(&delta_i, delta, 1)) < 0) {
156  av_log(log_ctx, AV_LOG_ERROR, "Invalid delta '%s'\n", delta);
157  return ret;
158  }
159  pts += (double)delta_i / AV_TIME_BASE;
160  }
161 
162  if (!strcmp(fmt, "flt")) {
163  av_bprintf(bp, "%.6f", pts);
164  } else if (!strcmp(fmt, "hms") ||
165  !strcmp(fmt, "hms24hh")) {
166  if (isnan(pts)) {
167  av_bprintf(bp, " ??:??:??.???");
168  } else {
169  int64_t ms = llrint(pts * 1000);
170  char sign = ' ';
171  if (ms < 0) {
172  sign = '-';
173  ms = -ms;
174  }
175  if (!strcmp(fmt, "hms24hh")) {
176  /* wrap around 24 hours */
177  ms %= 24 * 60 * 60 * 1000;
178  }
179  av_bprintf(bp, "%c%02d:%02d:%02d.%03d", sign,
180  (int)(ms / (60 * 60 * 1000)),
181  (int)(ms / (60 * 1000)) % 60,
182  (int)(ms / 1000) % 60,
183  (int)(ms % 1000));
184  }
185  } else if (!strcmp(fmt, "localtime") ||
186  !strcmp(fmt, "gmtime")) {
187  struct tm tm;
188  time_t ms = (time_t)pts;
189  if (!strcmp(fmt, "localtime"))
190  localtime_r(&ms, &tm);
191  else
192  gmtime_r(&ms, &tm);
193  av_bprint_strftime(bp, av_x_if_null(strftime_fmt, "%Y-%m-%d %H:%M:%S"), &tm);
194  } else {
195  av_log(log_ctx, AV_LOG_ERROR, "Invalid format '%s'\n", fmt);
196  return AVERROR(EINVAL);
197  }
198  return 0;
199 }
200 
201 int ff_print_time(void *log_ctx, AVBPrint *bp,
202  const char *strftime_fmt, char localtime)
203 {
204  const char *fmt = av_x_if_null(strftime_fmt, "%Y-%m-%d %H:%M:%S");
205  const char *fmt_begin = fmt;
206  int64_t unow;
207  time_t now;
208  struct tm tm;
209  const char *begin;
210  const char *tmp;
211  int len;
212  int div;
213  AVBPrint fmt_bp;
214 
216 
217  unow = av_gettime();
218  now = unow / 1000000;
219  if (localtime)
220  localtime_r(&now, &tm);
221  else
222  tm = *gmtime_r(&now, &tm);
223 
224  // manually parse format for %N (fractional seconds)
225  begin = fmt;
226  while ((begin = strchr(begin, '%'))) {
227  tmp = begin + 1;
228  len = 0;
229 
230  // skip escaped "%%"
231  if (*tmp == '%') {
232  begin = tmp + 1;
233  continue;
234  }
235 
236  // count digits between % and possible N
237  while (*tmp != '\0' && av_isdigit((int)*tmp)) {
238  len++;
239  tmp++;
240  }
241 
242  // N encountered, insert time
243  if (*tmp == 'N') {
244  int num_digits = 3; // default show millisecond [1,6]
245 
246  // if digit given, expect [1,6], warn & clamp otherwise
247  if (len == 1) {
248  num_digits = av_clip(*(begin + 1) - '0', 1, 6);
249  } else if (len > 1) {
250  av_log(log_ctx, AV_LOG_WARNING, "Invalid number of decimals for %%N, using default of %i\n", num_digits);
251  }
252 
253  len += 2; // add % and N to get length of string part
254 
255  div = pow(10, 6 - num_digits);
256 
257  av_bprintf(&fmt_bp, "%.*s%0*d", (int)(begin - fmt_begin), fmt_begin, num_digits, (int)(unow % 1000000) / div);
258 
259  begin += len;
260  fmt_begin = begin;
261 
262  continue;
263  }
264 
265  begin = tmp;
266  }
267 
268  av_bprintf(&fmt_bp, "%s", fmt_begin);
269  if (!av_bprint_is_complete(&fmt_bp)) {
270  av_log(log_ctx, AV_LOG_WARNING, "Format string truncated at %u/%u.", fmt_bp.size, fmt_bp.len);
271  }
272 
273  av_bprint_strftime(bp, fmt_bp.str, &tm);
274 
275  av_bprint_finalize(&fmt_bp, NULL);
276 
277  return 0;
278 }
279 
280 int ff_print_eval_expr(void *log_ctx, AVBPrint *bp,
281  const char *expr,
282  const char * const *fun_names, const ff_eval_func2 *fun_values,
283  const char * const *var_names, const double *var_values,
284  void *eval_ctx)
285 {
286  double res;
287  int ret;
288 
289  ret = av_expr_parse_and_eval(&res, expr, var_names, var_values,
290  NULL, NULL, fun_names, fun_values,
291  eval_ctx, 0, log_ctx);
292  if (ret < 0)
293  av_log(log_ctx, AV_LOG_ERROR,
294  "Text expansion expression '%s' is not valid\n",
295  expr);
296  else
297  av_bprintf(bp, "%f", res);
298 
299  return ret;
300 }
301 
302 int ff_print_formatted_eval_expr(void *log_ctx, AVBPrint *bp,
303  const char *expr,
304  const char * const *fun_names, const ff_eval_func2 *fun_values,
305  const char * const *var_names, const double *var_values,
306  void *eval_ctx,
307  const char format, int positions)
308 {
309  double res;
310  int intval;
311  int ret;
312  char fmt_str[30] = "%";
313 
314  ret = av_expr_parse_and_eval(&res, expr, var_names, var_values,
315  NULL, NULL, fun_names, fun_values,
316  eval_ctx, 0, log_ctx);
317  if (ret < 0) {
318  av_log(log_ctx, AV_LOG_ERROR,
319  "Text expansion expression '%s' is not valid\n",
320  expr);
321  return ret;
322  }
323 
324  if (!strchr("xXdu", format)) {
325  av_log(log_ctx, AV_LOG_ERROR, "Invalid format '%c' specified,"
326  " allowed values: 'x', 'X', 'd', 'u'\n", format);
327  return AVERROR(EINVAL);
328  }
329 
330  feclearexcept(FE_ALL_EXCEPT);
331  intval = res;
332 #if defined(FE_INVALID) && defined(FE_OVERFLOW) && defined(FE_UNDERFLOW)
333  if ((ret = fetestexcept(FE_INVALID|FE_OVERFLOW|FE_UNDERFLOW))) {
334  av_log(log_ctx, AV_LOG_ERROR, "Conversion of floating-point result to int failed. Control register: 0x%08x. Conversion result: %d\n", ret, intval);
335  return AVERROR(EINVAL);
336  }
337 #endif
338 
339  if (positions >= 0)
340  av_strlcatf(fmt_str, sizeof(fmt_str), "0%u", positions);
341  av_strlcatf(fmt_str, sizeof(fmt_str), "%c", format);
342 
343  av_log(log_ctx, AV_LOG_DEBUG, "Formatting value %f (expr '%s') with spec '%s'\n",
344  res, expr, fmt_str);
345 
346  av_bprintf(bp, fmt_str, intval);
347 
348  return 0;
349 }
350 
351 
352 int ff_load_textfile(void *log_ctx, const char *textfile,
353  unsigned char **text, size_t *text_size)
354 {
355  int err;
356  uint8_t *textbuf;
357  uint8_t *tmp;
358  size_t textbuf_size;
359 
360  if ((err = av_file_map(textfile, &textbuf, &textbuf_size, 0, log_ctx)) < 0) {
361  av_log(log_ctx, AV_LOG_ERROR,
362  "The text file '%s' could not be read or is empty\n",
363  textfile);
364  return err;
365  }
366 
367  if (textbuf_size > 0 && ff_is_newline(textbuf[textbuf_size - 1]))
368  textbuf_size--;
369  if (textbuf_size > SIZE_MAX - 1 || !(tmp = av_realloc(*text, textbuf_size + 1))) {
370  av_file_unmap(textbuf, textbuf_size);
371  return AVERROR(ENOMEM);
372  }
373  *text = tmp;
374  memcpy(*text, textbuf, textbuf_size);
375  (*text)[textbuf_size] = 0;
376  if (text_size)
377  *text_size = textbuf_size;
378  av_file_unmap(textbuf, textbuf_size);
379 
380  return 0;
381 }
382 
AV_LOG_WARNING
#define AV_LOG_WARNING
Something somehow does not look correct.
Definition: log.h:186
AV_BPRINT_SIZE_UNLIMITED
#define AV_BPRINT_SIZE_UNLIMITED
name
it s the only field you need to keep assuming you have a context There is some magic you don t need to care about around this just let it vf default minimum maximum flags name is the option name
Definition: writing_filters.txt:88
ff_expand_text
int ff_expand_text(FFExpandTextContext *expand_text, char *text, AVBPrint *bp)
Expand text template.
Definition: textutils.c:122
av_clip
#define av_clip
Definition: common.h:98
av_bprint_is_complete
static int av_bprint_is_complete(const AVBPrint *buf)
Test if the print buffer is complete (not truncated).
Definition: bprint.h:218
AVERROR
Filter the word “frame” indicates either a video frame or a group of audio as stored in an AVFrame structure Format for each input and each output the list of supported formats For video that means pixel format For audio that means channel sample they are references to shared objects When the negotiation mechanism computes the intersection of the formats supported at each end of a all references to both lists are replaced with a reference to the intersection And when a single format is eventually chosen for a link amongst the remaining all references to the list are updated That means that if a filter requires that its input and output have the same format amongst a supported all it has to do is use a reference to the same list of formats query_formats can leave some formats unset and return AVERROR(EAGAIN) to cause the negotiation mechanism toagain later. That can be used by filters with complex requirements to use the format negotiated on one link to set the formats supported on another. Frame references ownership and permissions
av_bprint_init
void av_bprint_init(AVBPrint *buf, unsigned size_init, unsigned size_max)
Definition: bprint.c:69
int64_t
long long int64_t
Definition: coverity.c:34
tmp
static uint8_t tmp[11]
Definition: aes_ctr.c:28
FFExpandTextContext::functions
FFExpandTextFunction * functions
list of functions to use to expand sequences in the format FUNCTION_NAME{PARAMS}
Definition: textutils.h:77
av_strlcatf
size_t av_strlcatf(char *dst, size_t size, const char *fmt,...)
Definition: avstring.c:103
FFExpandTextContext::functions_nb
unsigned int functions_nb
number of functions
Definition: textutils.h:82
positions
const static uint16_t positions[][14][3]
Definition: vf_vectorscope.c:817
av_file_map
int av_file_map(const char *filename, uint8_t **bufptr, size_t *size, int log_offset, void *log_ctx)
Read the file with name filename, and put its content in a newly allocated buffer or map it with mmap...
Definition: file.c:55
ff_expand_text_function
static int ff_expand_text_function(FFExpandTextContext *expand_text, AVBPrint *bp, char **rtext)
Expand text template pointed to by *rtext.
Definition: textutils.c:81
gmtime_r
#define gmtime_r
Definition: time_internal.h:34
FFExpandTextContext::log_ctx
void * log_ctx
log context to pass to the function, used for logging and for accessing the context for the function
Definition: textutils.h:71
pts
static int64_t pts
Definition: transcode_aac.c:643
FFExpandTextFunction
Function used to expand a template sequence in the format %{FUNCTION_NAME[:PARAMS]},...
Definition: textutils.h:36
AV_LOG_ERROR
#define AV_LOG_ERROR
Something went wrong and cannot losslessly be recovered.
Definition: log.h:180
FF_ARRAY_ELEMS
#define FF_ARRAY_ELEMS(a)
Definition: sinewin_tablegen.c:29
ff_load_textfile
int ff_load_textfile(void *log_ctx, const char *textfile, unsigned char **text, size_t *text_size)
Definition: textutils.c:352
var_names
static const char *const var_names[]
Definition: noise.c:30
format
Filter the word “frame” indicates either a video frame or a group of audio as stored in an AVFrame structure Format for each input and each output the list of supported formats For video that means pixel format For audio that means channel sample format(the sample packing is implied by the sample format) and sample rate. The lists are not just lists
AV_LOG_DEBUG
#define AV_LOG_DEBUG
Stuff which is only useful for libav* developers.
Definition: log.h:201
av_file_unmap
void av_file_unmap(uint8_t *bufptr, size_t size)
Unmap or free the buffer bufptr created by av_file_map().
Definition: file.c:146
NULL
#define NULL
Definition: coverity.c:32
ff_print_formatted_eval_expr
int ff_print_formatted_eval_expr(void *log_ctx, AVBPrint *bp, const char *expr, const char *const *fun_names, const ff_eval_func2 *fun_values, const char *const *var_names, const double *var_values, void *eval_ctx, const char format, int positions)
Definition: textutils.c:302
isnan
#define isnan(x)
Definition: libm.h:340
textutils.h
double
double
Definition: af_crystalizer.c:131
av_parse_time
int av_parse_time(int64_t *timeval, const char *timestr, int duration)
Parse timestr and return in *time a corresponding number of microseconds.
Definition: parseutils.c:589
time.h
av_bprint_strftime
void av_bprint_strftime(AVBPrint *buf, const char *fmt, const struct tm *tm)
Append a formatted date and time to a print buffer.
Definition: bprint.c:181
error.h
av_expr_parse_and_eval
int av_expr_parse_and_eval(double *d, const char *s, const char *const *const_names, const double *const_values, const char *const *func1_names, double(*const *funcs1)(void *, double), const char *const *func2_names, double(*const *funcs2)(void *, double, double), void *opaque, int log_offset, void *log_ctx)
Parse and evaluate an expression.
Definition: eval.c:804
av_bprint_finalize
int av_bprint_finalize(AVBPrint *buf, char **ret_str)
Finalize a print buffer.
Definition: bprint.c:240
localtime_r
#define localtime_r
Definition: time_internal.h:46
ff_print_eval_expr
int ff_print_eval_expr(void *log_ctx, AVBPrint *bp, const char *expr, const char *const *fun_names, const ff_eval_func2 *fun_values, const char *const *var_names, const double *var_values, void *eval_ctx)
Definition: textutils.c:280
av_isdigit
static av_const int av_isdigit(int c)
Locale-independent conversion of ASCII isdigit.
Definition: avstring.h:202
FFExpandTextFunction::func
int(* func)(void *ctx, AVBPrint *bp, const char *function_name, unsigned argc, char **args)
actual function used to perform the expansion
Definition: textutils.h:51
ff_print_pts
int ff_print_pts(void *log_ctx, AVBPrint *bp, double pts, const char *delta, const char *fmt, const char *strftime_fmt)
Definition: textutils.c:148
i
#define i(width, name, range_min, range_max)
Definition: cbs_h2645.c:255
AV_TIME_BASE
#define AV_TIME_BASE
Internal time base represented as integer.
Definition: avutil.h:254
delta
float delta
Definition: vorbis_enc_data.h:430
len
int len
Definition: vorbis_enc_data.h:426
ret
ret
Definition: filter_design.txt:187
av_bprintf
void av_bprintf(AVBPrint *buf, const char *fmt,...)
Definition: bprint.c:99
av_get_token
char * av_get_token(const char **buf, const char *term)
Unescape the given string until a non escaped terminating char, and return the token corresponding to...
Definition: avstring.c:143
av_bprint_clear
void av_bprint_clear(AVBPrint *buf)
Reset the string to "" but keep internal allocated data.
Definition: bprint.c:232
FFExpandTextContext
in a text template, followed by any character, always expands to the second character.
Definition: textutils.h:66
file.h
av_gettime
int64_t av_gettime(void)
Get the current time in microseconds.
Definition: time.c:39
avutil.h
llrint
#define llrint(x)
Definition: libm.h:394
av_freep
#define av_freep(p)
Definition: tableprint_vlc.h:34
ff_expand_text_function_internal
static int ff_expand_text_function_internal(FFExpandTextContext *expand_text, AVBPrint *bp, char *name, unsigned argc, char **argv)
Definition: textutils.c:34
av_log
#define av_log(a,...)
Definition: tableprint_vlc.h:27
av_bprint_chars
void av_bprint_chars(AVBPrint *buf, char c, unsigned n)
Append char c n times to a print buffer.
Definition: bprint.c:145
ff_print_time
int ff_print_time(void *log_ctx, AVBPrint *bp, const char *strftime_fmt, char localtime)
Definition: textutils.c:201
av_x_if_null
static void * av_x_if_null(const void *p, const void *x)
Return x default pointer in case p is NULL.
Definition: avutil.h:312
av_realloc
void * av_realloc(void *ptr, size_t size)
Allocate, reallocate, or free a block of memory.
Definition: mem.c:153