From patchwork Sat Jun 29 16:12:47 2019 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Ulf Zibis X-Patchwork-Id: 13758 Return-Path: X-Original-To: patchwork@ffaux-bg.ffmpeg.org Delivered-To: patchwork@ffaux-bg.ffmpeg.org Received: from ffbox0-bg.mplayerhq.hu (ffbox0-bg.ffmpeg.org [79.124.17.100]) by ffaux.localdomain (Postfix) with ESMTP id 88493447CE2 for ; Sat, 29 Jun 2019 19:12:56 +0300 (EEST) Received: from [127.0.1.1] (localhost [127.0.0.1]) by ffbox0-bg.mplayerhq.hu (Postfix) with ESMTP id 632BF68A720; Sat, 29 Jun 2019 19:12:56 +0300 (EEST) X-Original-To: ffmpeg-devel@ffmpeg.org Delivered-To: ffmpeg-devel@ffmpeg.org Received: from wp215.webpack.hosteurope.de (wp215.webpack.hosteurope.de [80.237.132.222]) by ffbox0-bg.mplayerhq.hu (Postfix) with ESMTPS id 45E0B68A71C for ; Sat, 29 Jun 2019 19:12:49 +0300 (EEST) Received: from dslb-092-073-105-234.092.073.pools.vodafone-ip.de ([92.73.105.234] helo=[192.168.178.129]); authenticated by wp215.webpack.hosteurope.de running ExIM with esmtpsa (TLS1.2:ECDHE_RSA_AES_128_GCM_SHA256:128) id 1hhFy4-0006TX-0u; Sat, 29 Jun 2019 18:12:48 +0200 To: FFmpeg development discussions and patches From: Ulf Zibis Message-ID: Date: Sat, 29 Jun 2019 18:12:47 +0200 User-Agent: Mozilla/5.0 (X11; Linux x86_64; rv:60.0) Gecko/20100101 Thunderbird/60.7.1 MIME-Version: 1.0 Content-Language: en-US X-bounce-key: webpack.hosteurope.de; ulf.zibis@cosoco.de; 1561824774; 01310761; X-HE-SMSGID: 1hhFy4-0006TX-0u Subject: [FFmpeg-devel] [PATCH v1] Add 2 timestamp print formats X-BeenThere: ffmpeg-devel@ffmpeg.org X-Mailman-Version: 2.1.20 Precedence: list List-Id: FFmpeg development discussions and patches List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Reply-To: FFmpeg development discussions and patches Errors-To: ffmpeg-devel-bounces@ffmpeg.org Sender: "ffmpeg-devel" 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 From 3bf9cb526cb70fd0192918b6b1c1b049974ca8ec Mon Sep 17 00:00:00 2001 From: Ulf Zibis 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 */