From patchwork Wed Jul 3 17:09:02 2019 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Ulf Zibis X-Patchwork-Id: 13810 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 2DD7B4480B2 for ; Wed, 3 Jul 2019 20:09:09 +0300 (EEST) Received: from [127.0.1.1] (localhost [127.0.0.1]) by ffbox0-bg.mplayerhq.hu (Postfix) with ESMTP id 09F0C68AA2D; Wed, 3 Jul 2019 20:09:09 +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 A506C68AA0D for ; Wed, 3 Jul 2019 20:09:03 +0300 (EEST) Received: from dslb-094-221-100-204.094.221.pools.vodafone-ip.de ([94.221.100.204] helo=[192.168.178.26]); authenticated by wp215.webpack.hosteurope.de running ExIM with esmtpsa (TLS1.2:ECDHE_RSA_AES_128_GCM_SHA256:128) id 1hiikh-0005Lt-38; Wed, 03 Jul 2019 19:09:03 +0200 To: ffmpeg-devel@ffmpeg.org References: <917dcaa0-ccc7-a78f-9e93-bee8d2fb1294@CoSoCo.de> <20190701151627.GS3118@michaelspb> <5c28185a-8db5-b2f8-f3cc-136ada732c79@CoSoCo.de> <20190703085204.GA3118@michaelspb> From: Ulf Zibis Message-ID: <10b72bf3-c64d-1780-4310-dece360308f8@CoSoCo.de> Date: Wed, 3 Jul 2019 19:09:02 +0200 User-Agent: Mozilla/5.0 (X11; Linux x86_64; rv:60.0) Gecko/20100101 Thunderbird/60.7.2 MIME-Version: 1.0 In-Reply-To: Content-Language: de-DE X-bounce-key: webpack.hosteurope.de; ulf.zibis@cosoco.de; 1562173748; 3e5008fc; X-HE-SMSGID: 1hiikh-0005Lt-38 Subject: Re: [FFmpeg-devel] [PATCH v4] 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" Am 03.07.19 um 16:49 schrieb Ulf Zibis: > Am 03.07.19 um 10:52 schrieb Michael Niedermayer: >>>>> -#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; >>>> If this could be done without float/double that would be preferred as it >>>> avoids inaccuracies / slight differences between platforms >>> I too thought on that, but the existing functions too rely on >>> float/double. Should I anyway provide a solution with integer arithmetic? >> its indepedant of your patch but i think all these should use integers >> unless its too messy > Thanks for you opinion. > > Here comes a new patch. I missed to mark the big integer literal with 'L' and add "must be less than 2**63/1,000,000/tb->num" to all 3 functions. So I attach a correction. -Ulf From e8c42ed84492d495e506da30dc7163edafccc9e4 Mon Sep 17 00:00:00 2001 From: Ulf Zibis Date: 29.06.2019, 17:52:06 avutil/timestamp: 2 new print formats with av_ts2us() diff --git a/libavutil/timestamp.h b/libavutil/timestamp.h index e082f01..f442fc0 100644 --- a/libavutil/timestamp.h +++ b/libavutil/timestamp.h @@ -33,6 +33,17 @@ #define AV_TS_MAX_STRING_SIZE 32 /** + * Convert a time base scaled timestamp to micro seconds. + * + * @param ts the timestamp to convert, must be less than 2**63/1,000,000/tb->num + * @param tb the timebase of the timestamp + * @return the timestamp in micro seconds + */ +static inline int64_t av_ts2us(int64_t ts, AVRational *tb) { + return ts * 1000000 * tb->num / tb->den; +} + +/** * Fill the provided buffer with a string containing a timestamp * representation. * @@ -55,7 +66,7 @@ /** * 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 @@ -75,4 +86,60 @@ */ #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, must be less than 2**63/1,000,000/tb->num + * @param tb the timebase of the timestamp + * @return the buffer in input + */ +static 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 { + int64_t us = av_ts2us(ts, tb); + int len = snprintf(buf, AV_TS_MAX_STRING_SIZE, "%3ld:%02d.%06d", + us / 60000000, (int)(us / 1000000 % 60), (int)(us % 1000000)); + while (buf[--len] == '0'); // search trailing zeros or ... + buf[len + (buf[len] != '.')] = '\0'; // dot and strip them + } + 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, must be less than 2**63/1,000,000/tb->num + * @param tb the timebase of the timestamp + * @return the buffer in input + */ +static 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 { + int64_t us = av_ts2us(ts, tb); + int len = snprintf(buf, AV_TS_MAX_STRING_SIZE, "%ld:%02d:%02d.%06d", + us / 3600000000L, (int)(us / 60000000 % 60), (int)(us / 1000000 % 60), (int)(us % 1000000)); + while (buf[--len] == '0'); // search trailing zeros or ... + buf[len + (buf[len] != '.')] = '\0'; // dot and strip them + } + 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 */