MPD  0.20.15
Time.hxx
Go to the documentation of this file.
1 /*
2  * Copyright 2003-2017 The Music Player Daemon Project
3  * http://www.musicpd.org
4  *
5  * This program is free software; you can redistribute it and/or modify
6  * it under the terms of the GNU General Public License as published by
7  * the Free Software Foundation; either version 2 of the License, or
8  * (at your option) any later version.
9  *
10  * This program is distributed in the hope that it will be useful,
11  * but WITHOUT ANY WARRANTY; without even the implied warranty of
12  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13  * GNU General Public License for more details.
14  *
15  * You should have received a copy of the GNU General Public License along
16  * with this program; if not, write to the Free Software Foundation, Inc.,
17  * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
18  */
19 
20 #ifndef MPD_FFMPEG_TIME_HXX
21 #define MPD_FFMPEG_TIME_HXX
22 
23 #include "Chrono.hxx"
24 #include "Compiler.h"
25 
26 extern "C" {
27 #include <libavutil/avutil.h>
28 #include <libavutil/mathematics.h>
29 }
30 
31 #include <assert.h>
32 #include <stdint.h>
33 
34 /* suppress the ffmpeg compatibility macro */
35 #ifdef SampleFormat
36 #undef SampleFormat
37 #endif
38 
43 static inline double
44 FfmpegTimeToDouble(int64_t t, const AVRational time_base) noexcept
45 {
46  assert(t != (int64_t)AV_NOPTS_VALUE);
47 
48  return (double)av_rescale_q(t, time_base, (AVRational){1, 1024})
49  / (double)1024;
50 }
51 
55 template<typename Ratio>
56 static inline constexpr AVRational
58 {
59  return { Ratio::num, Ratio::den };
60 }
61 
66 static inline SongTime
67 FromFfmpegTime(int64_t t, const AVRational time_base) noexcept
68 {
69  assert(t != (int64_t)AV_NOPTS_VALUE);
70 
71  return SongTime::FromMS(av_rescale_q(t, time_base,
72  (AVRational){1, 1000}));
73 }
74 
79 static inline SignedSongTime
80 FromFfmpegTimeChecked(int64_t t, const AVRational time_base) noexcept
81 {
82  return t != (int64_t)AV_NOPTS_VALUE
83  ? SignedSongTime(FromFfmpegTime(t, time_base))
85 }
86 
91 static inline int64_t
92 ToFfmpegTime(SongTime t, const AVRational time_base) noexcept
93 {
94  return av_rescale_q(t.count(),
95  RatioToAVRational<SongTime::period>(),
96  time_base);
97 }
98 
102 static constexpr int64_t
103 FfmpegTimestampFallback(int64_t t, int64_t fallback)
104 {
105  return gcc_likely(t != int64_t(AV_NOPTS_VALUE))
106  ? t
107  : fallback;
108 }
109 
110 #endif
static gcc_const SongTime FromFfmpegTime(int64_t t, const AVRational time_base) noexcept
Convert a FFmpeg time stamp to a SongTime.
Definition: Time.hxx:67
A time stamp within a song.
Definition: Chrono.hxx:31
static constexpr int64_t FfmpegTimestampFallback(int64_t t, int64_t fallback)
Replace #AV_NOPTS_VALUE with the given fallback.
Definition: Time.hxx:103
static constexpr SignedSongTime Negative()
Generate a negative value.
Definition: Chrono.hxx:137
#define gcc_likely(x)
Definition: Compiler.h:124
#define gcc_const
Definition: Compiler.h:109
static constexpr SongTime FromMS(rep ms)
Definition: Chrono.hxx:57
static constexpr AVRational RatioToAVRational()
Convert a std::ratio to a #AVRational.
Definition: Time.hxx:57
static gcc_const int64_t ToFfmpegTime(SongTime t, const AVRational time_base) noexcept
Convert a SongTime to a FFmpeg time stamp with the given base.
Definition: Time.hxx:92
A variant of SongTime that is based on a signed integer.
Definition: Chrono.hxx:115
static gcc_const SignedSongTime FromFfmpegTimeChecked(int64_t t, const AVRational time_base) noexcept
Convert a FFmpeg time stamp to a SignedSongTime.
Definition: Time.hxx:80
static gcc_const double FfmpegTimeToDouble(int64_t t, const AVRational time_base) noexcept
Convert a FFmpeg time stamp to a floating point value (in seconds).
Definition: Time.hxx:44