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
@@ -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 */