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 <QtMultimedia/private/qtaggedtime_p.h> |
21 | #include <QtFFmpegMediaPluginImpl/private/qffmpeg_p.h> |
22 | |
23 | #include <chrono> |
24 | |
25 | QT_BEGIN_NAMESPACE |
26 | |
27 | namespace QFFmpeg { |
28 | |
29 | using namespace std::chrono_literals; |
30 | |
31 | struct TrackTimeTag; |
32 | // track position in microseconds, used as |
33 | // a general time position in the playback engine |
34 | using TrackPosition = QTaggedTimePoint<qint64, TrackTimeTag>; |
35 | using TrackDuration = QTaggedDuration<qint64, TrackTimeTag>; |
36 | |
37 | struct UserTrackTimeTag; |
38 | // track position in milliseconds, that matches the postion in the public API |
39 | using UserTrackPosition = QTaggedTimePoint<qint64, UserTrackTimeTag>; |
40 | using UserTrackDuration = QTaggedDuration<qint64, UserTrackTimeTag>; |
41 | |
42 | struct AVStreamTimeTag; |
43 | // position in AVStream, in 'AVStream::time_base * 1sec' units |
44 | using AVStreamPosition = QTaggedTimePoint<qint64, AVStreamTimeTag>; |
45 | using AVStreamDuration = QTaggedDuration<qint64, AVStreamTimeTag>; |
46 | |
47 | struct AVContextTimeTag; |
48 | // position in the AVFormatContext, in '1sec / AV_TIME_BASE' units, which is actually |
49 | // microseconds. The position is shifted on AVFormatContext::start_time from TrackTime. |
50 | using AVContextPosition = QTaggedTimePoint<qint64, AVContextTimeTag>; |
51 | using AVContextDuration = QTaggedDuration<qint64, AVContextTimeTag>; |
52 | |
53 | using RealClock = std::chrono::steady_clock; |
54 | |
55 | inline AVContextDuration contextStartOffset(const AVFormatContext *formatContext) |
56 | |
57 | { |
58 | return AVContextDuration( |
59 | formatContext->start_time == AV_NOPTS_VALUE ? 0 : formatContext->start_time); |
60 | } |
61 | |
62 | inline UserTrackPosition toUserPosition(TrackPosition trackPosition) |
63 | { |
64 | return UserTrackPosition(trackPosition.get() / 1000); |
65 | } |
66 | |
67 | inline UserTrackDuration toUserDuration(TrackDuration trackDuration) |
68 | { |
69 | return UserTrackDuration(trackDuration.get() / 1000); |
70 | } |
71 | |
72 | inline TrackDuration toTrackDuration(AVContextDuration contextDuration) |
73 | { |
74 | return TrackDuration(contextDuration.get() * 1'000'000 / AV_TIME_BASE); |
75 | } |
76 | |
77 | inline TrackPosition toTrackPosition(UserTrackPosition userTrackPosition) |
78 | { |
79 | return TrackPosition(userTrackPosition.get() * 1000); |
80 | } |
81 | |
82 | inline TrackDuration toTrackDuration(UserTrackDuration userTrackDuration) |
83 | { |
84 | return TrackDuration(userTrackDuration.get() * 1000); |
85 | } |
86 | |
87 | inline TrackDuration toTrackDuration(AVStreamDuration streamDuration, const AVStream *avStream) |
88 | { |
89 | return TrackDuration(timeStampUs(ts: streamDuration.get(), base: avStream->time_base).value_or(u: 0)); |
90 | } |
91 | |
92 | inline TrackPosition toTrackPosition(AVStreamPosition streamPosition, const AVStream *avStream, |
93 | const AVFormatContext *formatContext) |
94 | { |
95 | const auto duration = toTrackDuration(streamDuration: streamPosition.asDuration(), avStream) |
96 | - toTrackDuration(contextDuration: contextStartOffset(formatContext)); |
97 | return duration.asTimePoint(); |
98 | } |
99 | |
100 | inline AVContextPosition toContextPosition(TrackPosition trackPosition, |
101 | const AVFormatContext *formatContext) |
102 | { |
103 | |
104 | return AVContextPosition(trackPosition.get() * AV_TIME_BASE / 1'000'000) |
105 | + contextStartOffset(formatContext); |
106 | } |
107 | |
108 | } // namespace QFFmpeg |
109 | |
110 | QT_END_NAMESPACE |
111 | |
112 | #endif // QFFMPEGTIME_P_H |
113 | |