[FFmpeg-devel,v1] Add 2 timestamp print formats

Submitted by Ulf Zibis on June 29, 2019, 4:12 p.m.

Details

Message ID fbbd92e9-3021-3968-10e3-d19be45cea21@CoSoCo.de
State New
Headers show

Commit Message

Ulf Zibis June 29, 2019, 4:12 p.m.
Hi,

for my developement of another filter I need additional timestamp print
formats.

So I like to share my efforts. Are you interested to include them to the
project?

For libavutil/timestamp.h I'm also wondering, why these format functions
are designated for "inline". As printing is always a little heavy job,
an explicit function call would not change much for performance IMHO,
but would save project footprint.

Please review.

-Ulf

Patch hide | download patch | download mbox

From 3bf9cb526cb70fd0192918b6b1c1b049974ca8ec Mon Sep 17 00:00:00 2001
From: Ulf Zibis <Ulf.Zibis@CoSoCo.de>
Date: 29.06.2019, 17:52:06

avutil/timestamp: 2 new print formats

diff --git a/libavutil/timestamp.h b/libavutil/timestamp.h
index e082f01..43e8107 100644
--- a/libavutil/timestamp.h
+++ b/libavutil/timestamp.h
@@ -25,6 +25,7 @@ 
 #define AVUTIL_TIMESTAMP_H
 
 #include "common.h"
+#include "avassert.h"
 
 #if defined(__cplusplus) && !defined(__STDC_FORMAT_MACROS) && !defined(PRId64)
 #error missing -D__STDC_FORMAT_MACROS / #define __STDC_FORMAT_MACROS
@@ -51,11 +52,11 @@ 
  * Convenience macro, the return value should be used only directly in
  * function arguments but never stand-alone.
  */
-#define av_ts2str(ts) av_ts_make_string((char[AV_TS_MAX_STRING_SIZE]){0}, ts)
+#define av_ts2str(ts) av_ts_make_string((char[AV_TS_MAX_STRING_SIZE]){'\0'}, ts)
 
 /**
  * Fill the provided buffer with a string containing a timestamp time
- * representation.
+ * representation in seconds.
  *
  * @param buf a buffer with size in bytes of at least AV_TS_MAX_STRING_SIZE
  * @param ts the timestamp to represent
@@ -73,6 +74,63 @@ 
  * Convenience macro, the return value should be used only directly in
  * function arguments but never stand-alone.
  */
-#define av_ts2timestr(ts, tb) av_ts_make_time_string((char[AV_TS_MAX_STRING_SIZE]){0}, ts, tb)
+#define av_ts2timestr(ts, tb) av_ts_make_time_string((char[AV_TS_MAX_STRING_SIZE]){'\0'}, ts, tb)
+
+/**
+ * Fill the provided buffer with a string containing a timestamp time
+ * representation in minutes and seconds.
+ *
+ * @param buf a buffer with size in bytes of at least AV_TS_MAX_STRING_SIZE
+ * @param ts the timestamp to represent
+ * @param tb the timebase of the timestamp
+ * @return the buffer in input
+ */
+static inline char *av_ts_make_minute_string(char *buf, int64_t ts, AVRational *tb)
+{
+    if (ts == AV_NOPTS_VALUE) snprintf(buf, AV_TS_MAX_STRING_SIZE, "NOPTS");
+    else {
+        double time = av_q2d(*tb) * ts;
+        int len = snprintf(buf, AV_TS_MAX_STRING_SIZE, "%3d:%09.6f", (int)time / 60, fabs(fmod(time, 60)));
+        av_assert1(len < MAX_BUF_SZ && len >= 0);
+        while (buf[--len] == '0'); // strip trailing zeros and dot.
+        buf[len + ((buf[len] != '.') & 1)] = '\0'; // terminate
+    }
+    return buf;
+}
+
+/**
+ * Convenience macro, the return value should be used only directly in
+ * function arguments but never stand-alone.
+ */
+#define av_ts2minutestr(ts, tb) av_ts_make_minute_string((char[AV_TS_MAX_STRING_SIZE]){'\0'}, ts, tb)
+
+/**
+ * Fill the provided buffer with a string containing a timestamp time
+ * representation in hours, minutes and seconds.
+ *
+ * @param buf a buffer with size in bytes of at least AV_TS_MAX_STRING_SIZE
+ * @param ts the timestamp to represent
+ * @param tb the timebase of the timestamp
+ * @return the buffer in input
+ */
+static inline char *av_ts_make_hour_string(char *buf, int64_t ts, AVRational *tb)
+{
+    if (ts == AV_NOPTS_VALUE) snprintf(buf, AV_TS_MAX_STRING_SIZE, "NOPTS");
+    else {
+        double time = av_q2d(*tb) * ts;
+        int len = snprintf(buf, AV_TS_MAX_STRING_SIZE, "%d:%02d:%09.6f",
+                (int)time / 60 / 60,  (int)time / 60 % 60, fabs(fmod(time, 60)));
+        av_assert1(len < MAX_BUF_SZ && len >= 0);
+        while (buf[--len] == '0'); // strip trailing zeros and dot.
+        buf[len + ((buf[len] != '.') & 1)] = '\0'; // terminate
+    }
+    return buf;
+}
+
+/**
+ * Convenience macro, the return value should be used only directly in
+ * function arguments but never stand-alone.
+ */
+#define av_ts2hourstr(ts, tb) av_ts_make_hour_string((char[AV_TS_MAX_STRING_SIZE]){'\0'}, ts, tb)
 
 #endif /* AVUTIL_TIMESTAMP_H */