1 /*
2 * Copyright 2003-2021 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
25 extern "C" {
26 #include <libavutil/avutil.h>
27 #include <libavutil/mathematics.h>
28 }
29
30 #include <cassert>
31 #include <cstdint>
32
33 /* redefine AV_TIME_BASE_Q because libavutil's macro definition is a
34 compound literal, which is illegal in C++ */
35 #ifdef AV_TIME_BASE_Q
36 #undef AV_TIME_BASE_Q
37 #endif
38 static constexpr AVRational AV_TIME_BASE_Q{1, AV_TIME_BASE};
39
40 /**
41 * Convert a FFmpeg time stamp to a floating point value (in seconds).
42 */
43 [[gnu::const]]
44 static inline FloatDuration
FfmpegTimeToDouble(int64_t t,const AVRational time_base)45 FfmpegTimeToDouble(int64_t t, const AVRational time_base) noexcept
46 {
47 assert(t != (int64_t)AV_NOPTS_VALUE);
48
49 return FloatDuration(av_rescale_q(t, time_base, {1, 1024}))
50 / 1024;
51 }
52
53 /**
54 * Convert a std::ratio to a #AVRational.
55 */
56 template<typename Ratio>
57 constexpr AVRational
RatioToAVRational()58 RatioToAVRational()
59 {
60 return { Ratio::num, Ratio::den };
61 }
62
63 /**
64 * Convert a FFmpeg time stamp to a #SongTime.
65 */
66 [[gnu::const]]
67 static inline SongTime
FromFfmpegTime(int64_t t,const AVRational time_base)68 FromFfmpegTime(int64_t t, const AVRational time_base) noexcept
69 {
70 assert(t != (int64_t)AV_NOPTS_VALUE);
71
72 return SongTime::FromMS(av_rescale_q(t, time_base,
73 {1, 1000}));
74 }
75
76 /**
77 * Convert a FFmpeg time stamp to a #SignedSongTime.
78 */
79 [[gnu::const]]
80 static inline SignedSongTime
FromFfmpegTimeChecked(int64_t t,const AVRational time_base)81 FromFfmpegTimeChecked(int64_t t, const AVRational time_base) noexcept
82 {
83 return t != (int64_t)AV_NOPTS_VALUE
84 ? SignedSongTime(FromFfmpegTime(t, time_base))
85 : SignedSongTime::Negative();
86 }
87
88 /**
89 * Convert a #SongTime to a FFmpeg time stamp with the given base.
90 */
91 [[gnu::const]]
92 static inline int64_t
ToFfmpegTime(SongTime t,const AVRational time_base)93 ToFfmpegTime(SongTime t, const AVRational time_base) noexcept
94 {
95 return av_rescale_q(t.count(),
96 RatioToAVRational<SongTime::period>(),
97 time_base);
98 }
99
100 /**
101 * Replace #AV_NOPTS_VALUE with the given fallback.
102 */
103 constexpr int64_t
FfmpegTimestampFallback(int64_t t,int64_t fallback)104 FfmpegTimestampFallback(int64_t t, int64_t fallback)
105 {
106 return t != int64_t(AV_NOPTS_VALUE)
107 ? t
108 : fallback;
109 }
110
111 #endif
112