From 8fd119bd3db38474099f8c7b62a6269b0702cb53 Mon Sep 17 00:00:00 2001 From: "Azamat H. Hackimov" Date: Thu, 30 May 2024 00:52:51 +0300 Subject: [PATCH] MVE: Make timer code crossplatform --- ddio/chrono_timer.cpp | 5 +++ ddio/chrono_timer.h | 2 + libmve/CMakeLists.txt | 5 ++- libmve/mveplay.cpp | 94 ++++++++++++------------------------------- movie/d3movie.cpp | 2 +- 5 files changed, 37 insertions(+), 71 deletions(-) diff --git a/ddio/chrono_timer.cpp b/ddio/chrono_timer.cpp index edef9077..84028d97 100644 --- a/ddio/chrono_timer.cpp +++ b/ddio/chrono_timer.cpp @@ -33,4 +33,9 @@ int64_t ChronoTimer::GetTimeMS() { .count(); } +int64_t ChronoTimer::GetTimeUS() { + return std::chrono::duration_cast(std::chrono::steady_clock::now() - m_start_tstamp) + .count(); +} + } // namespace D3 diff --git a/ddio/chrono_timer.h b/ddio/chrono_timer.h index 1c51aae1..05ed968b 100644 --- a/ddio/chrono_timer.h +++ b/ddio/chrono_timer.h @@ -39,6 +39,8 @@ public: /// Get time in milliseconds after class initialization (i.e. application start) static int64_t GetTimeMS(); + /// Get time in microseconds after class initialization (i.e. application start) + static int64_t GetTimeUS(); }; } diff --git a/libmve/CMakeLists.txt b/libmve/CMakeLists.txt index 5762c55d..aed3b840 100644 --- a/libmve/CMakeLists.txt +++ b/libmve/CMakeLists.txt @@ -14,7 +14,10 @@ set(CPPS ) add_library(libmve STATIC ${CPPS}) -target_link_libraries(libmve PRIVATE SDL2::SDL2) +target_link_libraries(libmve PRIVATE + ddio + SDL2::SDL2 +) target_include_directories(libmve PUBLIC $ #include #include #include - -#ifdef _WIN32 -#include -#else -#include -#include -#endif // _WIN32 +#include #include "byteswap.h" +#include "chrono_timer.h" #include "decoders.h" #include "mvelib.h" #include "mve_audio.h" @@ -41,24 +37,24 @@ #endif #endif -#define MVE_OPCODE_ENDOFSTREAM 0x00 -#define MVE_OPCODE_ENDOFCHUNK 0x01 -#define MVE_OPCODE_CREATETIMER 0x02 -#define MVE_OPCODE_INITAUDIOBUFFERS 0x03 -#define MVE_OPCODE_STARTSTOPAUDIO 0x04 -#define MVE_OPCODE_INITVIDEOBUFFERS 0x05 - -#define MVE_OPCODE_DISPLAYVIDEO 0x07 -#define MVE_OPCODE_AUDIOFRAMEDATA 0x08 -#define MVE_OPCODE_AUDIOFRAMESILENCE 0x09 -#define MVE_OPCODE_INITVIDEOMODE 0x0A - -#define MVE_OPCODE_SETPALETTE 0x0C -#define MVE_OPCODE_SETPALETTECOMPRESSED 0x0D - -#define MVE_OPCODE_SETDECODINGMAP 0x0F - -#define MVE_OPCODE_VIDEODATA 0x11 +#define MVE_OPCODE_ENDOFSTREAM 0x00 // mcmd_end +#define MVE_OPCODE_ENDOFCHUNK 0x01 // mcmd_next +#define MVE_OPCODE_CREATETIMER 0x02 // mcmd_syncInit +#define MVE_OPCODE_INITAUDIOBUFFERS 0x03 // mcmd_sndConfigure +#define MVE_OPCODE_STARTSTOPAUDIO 0x04 // mcmd_sndSync +#define MVE_OPCODE_INITVIDEOBUFFERS 0x05 // mcmd_nfConfig +#define MVE_OPCODE_UNKNOWN_06 0x06 // mcmd_nfDecomp +#define MVE_OPCODE_DISPLAYVIDEO 0x07 // mcmd_sfShowFrame +#define MVE_OPCODE_AUDIOFRAMEDATA 0x08 // mcmd_sndAdd +#define MVE_OPCODE_AUDIOFRAMESILENCE 0x09 // mcmd_sndSilence +#define MVE_OPCODE_INITVIDEOMODE 0x0A // mcmd_gfxMode +#define MVE_OPCODE_UNKNOWN_0B 0x0B // mcmd_palMakeSynthPalette +#define MVE_OPCODE_SETPALETTE 0x0C // mcmd_palLoadPalette +#define MVE_OPCODE_SETPALETTECOMPRESSED 0x0D // mcmd_palLoadCompPalette +#define MVE_OPCODE_UNKNOWN_0E 0x0E // mcmd_nfChanges +#define MVE_OPCODE_SETDECODINGMAP 0x0F // mcmd_nfParms +#define MVE_OPCODE_UNKNOWN_10 0x10 // mcmd_nfDecompChg +#define MVE_OPCODE_VIDEODATA 0x11 // mcmd_nfPkDecomp #define MVE_AUDIO_FLAGS_STEREO 1 #define MVE_AUDIO_FLAGS_16BIT 2 @@ -106,56 +102,16 @@ static int micro_frame_delay = 0; static int timer_started = 0; static unsigned long int timer_expire = 0; -#if defined(_WIN32) || defined(macintosh) - unsigned long int timer_getmicroseconds() { - static int counter = 0; -#ifdef _WIN32 - DWORD now = GetTickCount(); -#else - long now = TickCount(); -#endif - counter++; - - return now * 1000 + counter; + return D3::ChronoTimer::GetTimeUS(); } -#else - -unsigned long int timer_getmicroseconds() { - struct timeval tv{}; - static time_t starttime = 0; - - gettimeofday(&tv, nullptr); - - if (!starttime) - starttime = tv.tv_sec; - - return (tv.tv_sec - starttime) * 1000000 + tv.tv_usec; -} - -#endif - void timer_sleepmicroseconds(unsigned long int usec) { -#ifdef _WIN32 - Sleep(usec / 1000); -#elif defined(macintosh) - Delay(usec / 1000); -#else - struct timespec ts{}; - ts.tv_sec = usec / 1000000; - ts.tv_nsec = usec % 1000000 * 1000; - nanosleep(&ts, nullptr); -#endif + std::this_thread::sleep_for(std::chrono::microseconds(usec)); } static int create_timer_handler(unsigned char major, unsigned char minor, unsigned char *data, int len, void *context) { - -#if !defined(_WIN32) && !defined(macintosh) // FIXME - __extension__ long long temp; -#else long temp; -#endif if (timer_created) return 1; @@ -185,8 +141,8 @@ static void timer_start() { } static void do_timer_wait() { - unsigned long int ts; - unsigned long int tv; + uint64_t ts; + uint64_t tv; if (!timer_started) return; diff --git a/movie/d3movie.cpp b/movie/d3movie.cpp index e8c2afc2..6fd7ea8a 100644 --- a/movie/d3movie.cpp +++ b/movie/d3movie.cpp @@ -589,7 +589,7 @@ intptr_t mve_SequenceStart(const char *mvename, void *fhandle, oeApplication *ap MVESTREAM *mve = MVE_rmPrepMovie(fhandle, -1, -1, 0); if (mve == nullptr) { - mprintf((0, "Failed to PrepMovie %s\n", mvename)); + mprintf(0, "Failed to PrepMovie %s\n", mvename); fclose((FILE *)fhandle); return MVELIB_INIT_ERROR; }