Skip to content

Commit 5692fd3

Browse files
committed
WIP
1 parent 3c2c533 commit 5692fd3

12 files changed

+323
-113
lines changed

src/common/logging/log.h

Lines changed: 0 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -39,14 +39,10 @@ void FmtLogMessage(Class log_class, Level log_level, const char* filename, unsig
3939
Common::Log::FmtLogMessage(log_class, log_level, Common::Log::TrimSourcePath(__FILE__), \
4040
__LINE__, __func__, __VA_ARGS__)
4141

42-
#ifdef _DEBUG
4342
#define LOG_TRACE(log_class, ...) \
4443
Common::Log::FmtLogMessage(Common::Log::Class::log_class, Common::Log::Level::Trace, \
4544
Common::Log::TrimSourcePath(__FILE__), __LINE__, __func__, \
4645
__VA_ARGS__)
47-
#else
48-
#define LOG_TRACE(log_class, fmt, ...) (void(0))
49-
#endif
5046

5147
#define LOG_DEBUG(log_class, ...) \
5248
Common::Log::FmtLogMessage(Common::Log::Class::log_class, Common::Log::Level::Debug, \

src/common/native_clock.cpp

Lines changed: 34 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -5,28 +5,51 @@
55
#include "common/rdtsc.h"
66
#include "common/uint128.h"
77

8+
#include <chrono>
9+
10+
#ifdef _WIN64
11+
#include <Windows.h>
12+
13+
#define MM_SHARED_USER_DATA_VA 0x7ffe0000
14+
#define QpcBias ((ULONGLONG volatile*)(MM_SHARED_USER_DATA_VA + 0x3b0))
15+
#endif
16+
817
namespace Common {
918

1019
NativeClock::NativeClock()
1120
: rdtsc_frequency{EstimateRDTSCFrequency()},
12-
ns_rdtsc_factor{GetFixedPoint64Factor(std::nano::den, rdtsc_frequency)},
13-
us_rdtsc_factor{GetFixedPoint64Factor(std::micro::den, rdtsc_frequency)},
14-
ms_rdtsc_factor{GetFixedPoint64Factor(std::milli::den, rdtsc_frequency)} {}
21+
us_rdtsc_factor{GetFixedPoint64Factor(std::micro::den, rdtsc_frequency)} {}
1522

16-
u64 NativeClock::GetTimeNS(u64 base_ptc /*= 0*/) const {
17-
return MultiplyHigh(GetUptime() - base_ptc, ns_rdtsc_factor);
23+
u64 NativeClock::GetTimeUS(u64 time) const {
24+
return MultiplyHigh(time, us_rdtsc_factor);
1825
}
1926

20-
u64 NativeClock::GetTimeUS(u64 base_ptc /*= 0*/) const {
21-
return MultiplyHigh(GetUptime() - base_ptc, us_rdtsc_factor);
27+
u64 NativeClock::GetUptime() const {
28+
#ifdef _WIN64
29+
LARGE_INTEGER counter;
30+
QueryPerformanceCounter(&counter);
31+
return counter.QuadPart;
32+
#else
33+
return FencedRDTSC();
34+
#endif
2235
}
2336

24-
u64 NativeClock::GetTimeMS(u64 base_ptc /*= 0*/) const {
25-
return MultiplyHigh(GetUptime() - base_ptc, ms_rdtsc_factor);
37+
u64 NativeClock::GetUnbiasedUptime() const {
38+
#ifdef _WIN64
39+
ULONGLONG bias = 0;
40+
u64 qpc = 0;
41+
do {
42+
bias = *QpcBias;
43+
qpc = GetUptime();
44+
} while (bias != *QpcBias);
45+
return qpc - bias;
46+
#else
47+
return GetUptime();
48+
#endif
2649
}
2750

28-
u64 NativeClock::GetUptime() const {
29-
return FencedRDTSC();
51+
u64 NativeClock::GetTscFrequency() const {
52+
return rdtsc_frequency;
3053
}
3154

3255
} // namespace Common

src/common/native_clock.h

Lines changed: 3 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,6 @@
33

44
#pragma once
55

6-
#include <chrono>
76
#include "common/types.h"
87

98
namespace Common {
@@ -12,20 +11,15 @@ class NativeClock final {
1211
public:
1312
explicit NativeClock();
1413

15-
u64 GetTscFrequency() const {
16-
return rdtsc_frequency;
17-
}
14+
u64 GetTimeUS(u64 time) const;
1815

19-
u64 GetTimeNS(u64 base_ptc = 0) const;
20-
u64 GetTimeUS(u64 base_ptc = 0) const;
21-
u64 GetTimeMS(u64 base_ptc = 0) const;
2216
u64 GetUptime() const;
17+
u64 GetUnbiasedUptime() const;
18+
u64 GetTscFrequency() const;
2319

2420
private:
2521
u64 rdtsc_frequency;
26-
u64 ns_rdtsc_factor;
2722
u64 us_rdtsc_factor;
28-
u64 ms_rdtsc_factor;
2923
};
3024

3125
} // namespace Common

src/common/rdtsc.cpp

Lines changed: 10 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -6,11 +6,12 @@
66
#include "common/uint128.h"
77

88
#ifdef _WIN64
9-
#include <windows.h>
9+
#include <Windows.h>
1010
#endif
1111

1212
namespace Common {
1313

14+
#ifndef _WIN64
1415
static constexpr size_t SecondToNanoseconds = 1000000000ULL;
1516

1617
template <u64 Nearest>
@@ -20,25 +21,22 @@ static u64 RoundToNearest(u64 value) {
2021
}
2122

2223
static u64 GetTimeNs() {
23-
#ifdef _WIN64
24-
// GetSystemTimePreciseAsFileTime returns the file time in 100ns units.
25-
static constexpr u64 Multiplier = 100;
26-
// Convert Windows epoch to Unix epoch.
27-
static constexpr u64 WindowsEpochToUnixEpoch = 0x19DB1DED53E8000LL;
28-
FILETIME filetime;
29-
GetSystemTimePreciseAsFileTime(&filetime);
30-
return Multiplier * ((static_cast<u64>(filetime.dwHighDateTime) << 32) +
31-
static_cast<u64>(filetime.dwLowDateTime) - WindowsEpochToUnixEpoch);
32-
#elif defined(__APPLE__)
24+
#if defined(__APPLE__)
3325
return clock_gettime_nsec_np(CLOCK_REALTIME);
3426
#else
3527
timespec ts;
3628
clock_gettime(CLOCK_REALTIME, &ts);
3729
return ts.tv_sec * SecondToNanoseconds + ts.tv_nsec;
3830
#endif
3931
}
32+
#endif
4033

4134
u64 EstimateRDTSCFrequency() {
35+
#ifdef _WIN64
36+
LARGE_INTEGER frequency;
37+
QueryPerformanceFrequency(&frequency);
38+
return frequency.QuadPart;
39+
#else
4240
// Discard the first result measuring the rdtsc.
4341
FencedRDTSC();
4442
std::this_thread::sleep_for(std::chrono::milliseconds{1});
@@ -55,6 +53,7 @@ u64 EstimateRDTSCFrequency() {
5553
const u64 tsc_diff = tsc_end - tsc_start;
5654
const u64 tsc_freq = MultiplyAndDivide64(tsc_diff, 1000000000ULL, end_time - start_time);
5755
return RoundToNearest<100'000>(tsc_freq);
56+
#endif
5857
}
5958

6059
} // namespace Common

src/core/libraries/avplayer/avplayer.cpp

Lines changed: 29 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -42,11 +42,14 @@ s32 PS4_SYSV_ABI sceAvPlayerClose(AvPlayerHandle handle) {
4242
}
4343

4444
u64 PS4_SYSV_ABI sceAvPlayerCurrentTime(AvPlayerHandle handle) {
45-
LOG_TRACE(Lib_AvPlayer, "called");
45+
// LOG_TRACE(Lib_AvPlayer, "called");
4646
if (handle == nullptr) {
47+
LOG_TRACE(Lib_AvPlayer, "returning ORBIS_AVPLAYER_ERROR_INVALID_PARAMS");
4748
return ORBIS_AVPLAYER_ERROR_INVALID_PARAMS;
4849
}
49-
return handle->CurrentTime();
50+
const auto res = handle->CurrentTime();
51+
LOG_TRACE(Lib_AvPlayer, "returning {}", res);
52+
return res;
5053
}
5154

5255
s32 PS4_SYSV_ABI sceAvPlayerDisableStream(AvPlayerHandle handle, u32 stream_id) {
@@ -66,11 +69,18 @@ s32 PS4_SYSV_ABI sceAvPlayerEnableStream(AvPlayerHandle handle, u32 stream_id) {
6669
}
6770

6871
bool PS4_SYSV_ABI sceAvPlayerGetAudioData(AvPlayerHandle handle, AvPlayerFrameInfo* p_info) {
69-
LOG_TRACE(Lib_AvPlayer, "called");
72+
// LOG_TRACE(Lib_AvPlayer, "called");
7073
if (handle == nullptr || p_info == nullptr) {
74+
LOG_TRACE(Lib_AvPlayer, "returning false");
7175
return false;
7276
}
73-
return handle->GetAudioData(*p_info);
77+
const auto res = handle->GetAudioData(*p_info);
78+
if (res) {
79+
LOG_TRACE(Lib_AvPlayer, "returning {}, ts = {}", res, p_info->timestamp);
80+
} else {
81+
LOG_TRACE(Lib_AvPlayer, "returning false");
82+
}
83+
return res;
7484
}
7585

7686
s32 PS4_SYSV_ABI sceAvPlayerGetStreamInfo(AvPlayerHandle handle, u32 stream_id,
@@ -92,11 +102,19 @@ bool PS4_SYSV_ABI sceAvPlayerGetVideoData(AvPlayerHandle handle, AvPlayerFrameIn
92102

93103
bool PS4_SYSV_ABI sceAvPlayerGetVideoDataEx(AvPlayerHandle handle,
94104
AvPlayerFrameInfoEx* video_info) {
95-
LOG_TRACE(Lib_AvPlayer, "called");
105+
// LOG_TRACE(Lib_AvPlayer, "called");
96106
if (handle == nullptr || video_info == nullptr) {
107+
LOG_TRACE(Lib_AvPlayer, "returning {}", false);
97108
return false;
98109
}
99-
return handle->GetVideoData(*video_info);
110+
// for (int i = 0; i < 20; ++i) {
111+
if (handle->GetVideoData(*video_info)) {
112+
LOG_TRACE(Lib_AvPlayer, "returning {}, ts = {}", true, video_info->timestamp);
113+
return true;
114+
}
115+
// }
116+
LOG_TRACE(Lib_AvPlayer, "returning {}", false);
117+
return false;
100118
}
101119

102120
AvPlayerHandle PS4_SYSV_ABI sceAvPlayerInit(AvPlayerInitData* data) {
@@ -143,11 +161,14 @@ s32 PS4_SYSV_ABI sceAvPlayerInitEx(const AvPlayerInitDataEx* p_data, AvPlayerHan
143161
}
144162

145163
bool PS4_SYSV_ABI sceAvPlayerIsActive(AvPlayerHandle handle) {
146-
LOG_TRACE(Lib_AvPlayer, "called");
164+
// LOG_TRACE(Lib_AvPlayer, "called");
147165
if (handle == nullptr) {
166+
LOG_TRACE(Lib_AvPlayer, "returning false");
148167
return false;
149168
}
150-
return handle->IsActive();
169+
const auto res = handle->IsActive();
170+
LOG_TRACE(Lib_AvPlayer, "returning {}", res);
171+
return res;
151172
}
152173

153174
s32 PS4_SYSV_ABI sceAvPlayerJumpToTime(AvPlayerHandle handle, uint64_t time) {

src/core/libraries/avplayer/avplayer.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -171,6 +171,7 @@ struct AvPlayerFileReplacement {
171171
};
172172

173173
enum class AvPlayerEvents {
174+
Initial = 0x00,
174175
StateStop = 0x01,
175176
StateReady = 0x02,
176177
StatePlay = 0x03,

src/core/libraries/avplayer/avplayer_common.h

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -24,12 +24,13 @@ enum class AvState {
2424
Stop,
2525
EndOfFile,
2626
Pause,
27-
C0x08,
27+
PauseOnEOF,
2828
Jump,
2929
TrickMode,
3030
C0x0B,
3131
Buffering,
3232
Starting,
33+
C0x10,
3334
Error,
3435
};
3536

0 commit comments

Comments
 (0)