From c0a0f69b007beaeded1584a81311b7d4d4eb0f13 Mon Sep 17 00:00:00 2001 From: Joose Sainio Date: Fri, 26 Jan 2024 15:13:42 +0200 Subject: [PATCH] Works on windows --- CMakeLists.txt | 5 +- src/encmain.c | 5 ++ src/search.c | 70 +++++++++++++++++++++++---- src/threads.h | 1 + src/threadwrapper/include/semaphore.h | 1 + src/threadwrapper/src/semaphore.cpp | 14 ++++++ 6 files changed, 85 insertions(+), 11 deletions(-) diff --git a/CMakeLists.txt b/CMakeLists.txt index a4d0ea0c..87d56401 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -71,7 +71,7 @@ if(NOT EXISTS "${PROJECT_SOURCE_DIR}/greatest/greatest.h") endif() -find_package(ZeroMQ REQUIRED) +find_package(ZeroMQ REQUIRED SHARED) # Grab -- timestamp for debug purposes string(TIMESTAMP CMAKE_BUILD_DATE %Y-%m-%d) @@ -144,6 +144,7 @@ endif() target_include_directories(uvg266 PUBLIC src) target_include_directories(uvg266 PUBLIC src/extras) target_include_directories(uvg266 PUBLIC src/strategies) +target_include_directories(uvg266 PUBLIC "G:/Local/sainio/Documents/libzmq/out/install/include") file(GLOB LIB_SOURCES_STRATEGIES_AVX2 RELATIVE ${PROJECT_SOURCE_DIR} "src/strategies/avx2/*.c") file(GLOB LIB_SOURCES_STRATEGIES_SSE41 RELATIVE ${PROJECT_SOURCE_DIR} "src/strategies/sse41/*.c") @@ -165,7 +166,7 @@ endif() add_executable(uvg266-bin ${CLI_SOURCES}) -target_link_libraries(uvg266-bin PUBLIC uvg266 zmq) +target_link_libraries(uvg266-bin PUBLIC uvg266 "G:/Local/sainio/Documents/libzmq/out/install/lib/libzmq-mt-4_3_6.lib") set_target_properties(uvg266-bin PROPERTIES OUTPUT_NAME uvg266) set_target_properties(uvg266-bin PROPERTIES RUNTIME_OUTPUT_NAME uvg266) diff --git a/src/encmain.c b/src/encmain.c index 73163db7..4bb166a0 100644 --- a/src/encmain.c +++ b/src/encmain.c @@ -270,7 +270,12 @@ static void* input_read_thread(void* in_args) frame_in->interlacing = args->encoder->cfg.source_scan_type; } +#if defined(__GNUC__) && !defined(__MINGW32__) usleep(33000); +#else + Sleep(33); +#endif + // Wait until main thread is ready to receive the next frame. if (uvg_sem_trywait(args->available_input_slots) == 0) { args->img_in = frame_in; diff --git a/src/search.c b/src/search.c index 1982a81e..5015d9a8 100644 --- a/src/search.c +++ b/src/search.c @@ -34,7 +34,6 @@ #include #include -#include #include "cabac.h" #include "cu.h" @@ -58,6 +57,27 @@ #include "uvg266.h" #include "videoframe.h" + +#if defined(__GNUC__) && !defined(__MINGW32__) +#include +#else +#include +void usleep(__int64 usec) +{ + HANDLE timer; + LARGE_INTEGER ft; + + ft.QuadPart = -( + 10 * + usec); // Convert to 100 nanosecond interval, negative value indicates relative time + + timer = CreateWaitableTimer(NULL, TRUE, NULL); + SetWaitableTimer(timer, &ft, 0, NULL, NULL, 0); + WaitForSingleObject(timer, INFINITE); + CloseHandle(timer); +} +#endif + #define IN_FRAME(x, y, width, height, block_width, block_height) \ ((x) >= 0 && (y) >= 0 \ && (x) + (block_width) <= (width) \ @@ -1639,13 +1659,26 @@ static double search_cu( if (cur_cu->type != CU_NOTSET) { uint8_t type = 0; uint8_t buffer[8192]; - struct timespec ts; - clock_gettime(CLOCK_REALTIME, &ts); - uint64_t timestamp = ts.tv_sec * 1000000000 + ts.tv_nsec; + UVG_CLOCK_T time; + UVG_GET_TIME(&time); + uint64_t time_high = time.dwHighDateTime & 0x000fffff; + time_high <<= 32; + uint64_t time_low = time.dwLowDateTime; + uint64_t timestamp = time_high | time_low; + timestamp *= 100; + // printf("%llu\n", timestamp); uint64_t bytes = 0; memcpy(buffer, &type, 1); bytes++; memcpy(buffer + bytes, ×tamp, 8); bytes+=8; memcpy(buffer + bytes, &state->frame->num, 1); bytes++; + if (cu_height == 64 && cu_width == 64) { + printf("%llu\n", timestamp); + printf("%llx\n", timestamp); + for (int i = 0; i < bytes; i++) { + printf("%2x", buffer[i]); + } + printf("\n"); + } memcpy(buffer + bytes, &cu_loc->x, 2); bytes+=2; memcpy(buffer + bytes, &cu_loc->y, 2); bytes+=2; memcpy(buffer + bytes, &cu_loc->width, 1); bytes++; @@ -1659,6 +1692,13 @@ static double search_cu( memcpy(buffer + bytes, &cur_cu->intra.isp_mode, 1); bytes++; memcpy(buffer + bytes, &cur_cu->lfnst_idx, 1); bytes++; memcpy(buffer + bytes, &cur_cu->tr_idx, 1); bytes++; + if (cu_height == 64 && cu_width == 64) { + for (int i = 0; i < bytes; i++) { + printf("%2x", buffer[i]); + } + printf("\n"); + } + uvg_pixels_blit(&lcu->rec.y[x_local + y_local * LCU_WIDTH], buffer + bytes, cu_width, cu_height, LCU_WIDTH, cu_width); bytes += cu_loc->width*cu_loc->height; @@ -1668,11 +1708,18 @@ static double search_cu( bytes += cu_loc->width*cu_loc->height / 4; zmq_send(state->send_socket, buffer, bytes, 0); - if(state->frame->cfg->speed > 1) { + if (state->frame->cfg->speed > 1) { +#if defined(__GNUC__) && !defined(__MINGW32__) struct timespec sleep_time; sleep_time.tv_sec = 0; sleep_time.tv_nsec = UVG_CLOCK_T_DIFF(start_time, end_time) * 1e9 * (state->frame->cfg->speed - 1); nanosleep(&sleep_time, NULL); +#else + usleep( + UVG_CLOCK_T_DIFF(start_time, end_time) * 1e6 * + (state->frame->cfg->speed - 1)); +#endif + } } @@ -2004,10 +2051,15 @@ static double search_cu( if (cost <= best_split_cost) { uint8_t type = 1; - uint8_t buffer[8192]; - struct timespec ts; - clock_gettime(CLOCK_REALTIME, &ts); - uint64_t timestamp = ts.tv_sec * 1000000000 + ts.tv_nsec; + uint8_t buffer[8192]; + UVG_CLOCK_T time; + UVG_GET_TIME(&time); + uint64_t time_high = time.dwHighDateTime & 0x000fffff; + time_high <<= 32; + uint64_t time_low = time.dwLowDateTime; + uint64_t timestamp = time_high | time_low; + timestamp *= 100; + //printf("%llu\n", timestamp); uint64_t bytes = 0; memcpy(buffer, &type, 1); bytes++; memcpy(buffer + bytes, ×tamp, 8); bytes+=8; diff --git a/src/threads.h b/src/threads.h index 9558e6bd..b91764ce 100644 --- a/src/threads.h +++ b/src/threads.h @@ -76,6 +76,7 @@ #define UVG_GET_TIME(clock_t) { GetSystemTimeAsFileTime(clock_t); } // _FILETIME has 32bit low and high part of 64bit 100ns resolution timestamp (since 12:00 AM January 1, 1601) #define UVG_CLOCK_T_AS_DOUBLE(ts) ((double)(((uint64_t)(ts).dwHighDateTime)<<32 | (uint64_t)(ts).dwLowDateTime) / 1e7) +#define UVG_CLOCK_T_AS_UINT64_T(ts) (((uint64_t)(ts).dwHighDateTime)<<32 | (((uint64_t)(ts).dwLowDateTime) * 100)) #define UVG_CLOCK_T_DIFF(start, stop) ((double)((((uint64_t)(stop).dwHighDateTime)<<32 | (uint64_t)(stop).dwLowDateTime) - \ (((uint64_t)(start).dwHighDateTime)<<32 | (uint64_t)(start).dwLowDateTime)) / 1e7) diff --git a/src/threadwrapper/include/semaphore.h b/src/threadwrapper/include/semaphore.h index f1a87345..d8126d79 100644 --- a/src/threadwrapper/include/semaphore.h +++ b/src/threadwrapper/include/semaphore.h @@ -27,6 +27,7 @@ int sem_destroy(sem_t* sem); int sem_init(sem_t* sem, int /*pshared*/, unsigned int value); int sem_post(sem_t* sem); int sem_wait(sem_t* sem); +int sem_trywait(sem_t* sem); #ifdef __cplusplus } diff --git a/src/threadwrapper/src/semaphore.cpp b/src/threadwrapper/src/semaphore.cpp index b4fe0170..3a950c2c 100644 --- a/src/threadwrapper/src/semaphore.cpp +++ b/src/threadwrapper/src/semaphore.cpp @@ -40,6 +40,15 @@ public: } } + int trywait() + { + std::unique_lock lck(mtx_); + if (val_ > 0) { + --val_; + return 0; + } + return -1; + } private: @@ -70,3 +79,8 @@ int sem_wait(sem_t* sem) { static_cast(*sem)->wait(); return 0; } + +int sem_trywait(sem_t* sem) +{ + return static_cast(*sem)->trywait(); +} \ No newline at end of file