Qt
Internal/Contributor docs for the Qt SDK. Note: These are NOT official API docs; those are found at https://doc.qt.io/
Loading...
Searching...
No Matches
qffmpegtime_p.h
Go to the documentation of this file.
1// Copyright (C) 2025 The Qt Company Ltd.
2// SPDX-License-Identifier: LicenseRef-Qt-Commercial OR LGPL-3.0-only OR GPL-2.0-only OR GPL-3.0-only
3
4#ifndef QFFMPEGTIME_P_H
5#define QFFMPEGTIME_P_H
6
7//
8// W A R N I N G
9// -------------
10//
11// This file is not part of the Qt API. It exists purely as an
12// implementation detail. This header file may change from version to
13// version without notice, or even be removed.
14//
15// We mean it.
16//
17
18#include "qglobal.h"
19
20#include <QtCore/qdebug.h>
21#include <QtMultimedia/private/qtaggedtime_p.h>
22#include <QtFFmpegMediaPluginImpl/private/qffmpeg_p.h>
23
24#include <chrono>
25
26#ifdef Q_OS_WIN
27# include <QtCore/qt_windows.h>
28# include <QtCore/private/qwin32ticks_p.h>
29# include <realtimeapiset.h>
30#endif
31
32QT_BEGIN_NAMESPACE
33
34namespace QFFmpeg {
35
36using namespace std::chrono_literals;
37
38struct TrackTimeTag;
39// track position in microseconds, used as
40// a general time position in the playback engine
43
44struct UserTrackTimeTag;
45// track position in milliseconds, that matches the postion in the public API
48
49struct AVStreamTimeTag;
50// position in AVStream, in 'AVStream::time_base * 1sec' units
53
54struct AVContextTimeTag;
55// position in the AVFormatContext, in '1sec / AV_TIME_BASE' units, which is actually
56// microseconds. The position is shifted on AVFormatContext::start_time from TrackTime.
59
60#ifdef Q_OS_WIN
61// std::chrono::steady_clock is based on QueryPerformanceCounter on Windows, which keeps counting
62// while the system is suspended. After wake-up the playback timeline would be ahead of the media
63// by the whole sleeping time, and video-only media would play extremely fast. The interrupt time
64// doesn't include the sleeping time, like CLOCK_MONOTONIC on Linux and CLOCK_UPTIME_RAW on macOS.
66{
69
70 static time_point now() noexcept
71 {
72 ULONGLONG interruptTime = 0; // 100 ns units, excludes the time in sleep/hibernation
74 return time_point(duration(static_cast<duration::rep>(interruptTime)));
75 }
76};
77
79#else
80using SteadyClock = std::chrono::steady_clock;
81#endif
82
83inline std::optional<AVContextDuration> contextStartOffset(const AVFormatContext *formatContext)
84{
85 if (formatContext->start_time == AV_NOPTS_VALUE)
86 return std::nullopt;
87 return AVContextDuration(formatContext->start_time);
88}
89
90inline UserTrackPosition toUserPosition(TrackPosition trackPosition)
91{
92 return UserTrackPosition(trackPosition.get() / 1000);
93}
94
95inline UserTrackDuration toUserDuration(TrackDuration trackDuration)
96{
97 return UserTrackDuration(trackDuration.get() / 1000);
98}
99
100inline TrackDuration toTrackDuration(AVContextDuration contextDuration)
101{
102 return TrackDuration(contextDuration.get() * 1'000'000 / AV_TIME_BASE);
103}
104
105inline TrackPosition toTrackPosition(UserTrackPosition userTrackPosition)
106{
107 return TrackPosition(userTrackPosition.get() * 1000);
108}
109
110inline TrackDuration toTrackDuration(UserTrackDuration userTrackDuration)
111{
112 return TrackDuration(userTrackDuration.get() * 1000);
113}
114
115inline TrackDuration toTrackDuration(AVStreamDuration streamDuration, const AVStream *avStream)
116{
117 return TrackDuration(timeStampUs(streamDuration.get(), avStream->time_base).value_or(0));
118}
119
120inline TrackPosition toTrackPosition(AVStreamPosition streamPosition, const AVStream *avStream,
121 const AVFormatContext *formatContext)
122{
123 auto duration = toTrackDuration(streamPosition.asDuration(), avStream);
124 if (formatContext) {
125 std::optional contextStart = contextStartOffset(formatContext);
126 if (contextStart)
127 duration -= toTrackDuration(*contextStart);
128 }
129
130 return duration.asTimePoint();
131}
132
133inline AVContextPosition toContextPosition(TrackPosition trackPosition,
134 const AVFormatContext *formatContext)
135{
136 return AVContextPosition(trackPosition.get() * AV_TIME_BASE / 1'000'000)
137 + contextStartOffset(formatContext).value_or(AVContextDuration::zero());
138}
139
140inline QDebug operator<<(QDebug debug, const TrackPosition &position)
141{
142 return debug << "TrackPosition{" << position.get() << "us}";
143}
144
145inline QDebug operator<<(QDebug debug, const TrackDuration &duration)
146{
147 return debug << "TrackDuration{" << duration.get() << "us}";
148}
149
150inline QDebug operator<<(QDebug debug, const UserTrackPosition &position)
151{
152 return debug << "UserTrackPosition{" << position.get() << "ms}";
153}
154
155inline QDebug operator<<(QDebug debug, const UserTrackDuration &duration)
156{
157 return debug << "UserTrackDuration{" << duration.get() << "ms}";
158}
159
160inline QDebug operator<<(QDebug debug, const AVStreamPosition &position)
161{
162 return debug << "AVStreamPosition{" << position.get() << "stream units}";
163}
164
165inline QDebug operator<<(QDebug debug, const AVStreamDuration &duration)
166{
167 return debug << "AVStreamDuration{" << duration.get() << "stream units}";
168}
169
170} // namespace QFFmpeg
171
172QT_END_NAMESPACE
173
174#endif // QFFMPEGTIME_P_H
UserTrackDuration toUserDuration(TrackDuration trackDuration)
QDebug operator<<(QDebug debug, const TrackPosition &position)
TrackDuration toTrackDuration(AVStreamDuration streamDuration, const AVStream *avStream)
AVContextPosition toContextPosition(TrackPosition trackPosition, const AVFormatContext *formatContext)
std::optional< AVContextDuration > contextStartOffset(const AVFormatContext *formatContext)
std::chrono::steady_clock SteadyClock
TrackDuration toTrackDuration(AVContextDuration contextDuration)
UserTrackPosition toUserPosition(TrackPosition trackPosition)
TrackPosition toTrackPosition(AVStreamPosition streamPosition, const AVStream *avStream, const AVFormatContext *formatContext)
TrackPosition toTrackPosition(UserTrackPosition userTrackPosition)