diff --git a/examples/compat_win.h b/examples/compat_win.h new file mode 100644 index 00000000000..f0fd727ce79 --- /dev/null +++ b/examples/compat_win.h @@ -0,0 +1,122 @@ +#pragma once + +#ifdef _WIN32 + +// ---------------------------------------------------- +// Windows compatibility layer for POSIX-style functions +// ---------------------------------------------------- + +// Suppress CRT security warnings for _open/_read/_write etc. +#define _CRT_SECURE_NO_WARNINGS + +#include +#include +#include +#include +#include +#include +#include +#include + +// ---------------------------------------------------- +// GCC/Clang attribute compatibility +// ---------------------------------------------------- + +// Make GCC-style __attribute__((...)) a no-op on MSVC +#ifndef __attribute__ +#define __attribute__(x) +#endif + +// Handle '__packed__' keyword used in Linux/GCC code +#ifndef __packed__ +#define __packed__ +#endif + +// ---------------------------------------------------- +// POSIX I/O compatibility +// ---------------------------------------------------- +#define open _open +#define read _read +#define write _write +#define close _close +#define lseek _lseek +#define stat _stat64i32 +#define fstat _fstat64i32 + +// Common file flags mapping +#ifndef O_RDONLY +#define O_RDONLY _O_RDONLY +#endif +#ifndef O_WRONLY +#define O_WRONLY _O_WRONLY +#endif +#ifndef O_RDWR +#define O_RDWR _O_RDWR +#endif +#ifndef O_CREAT +#define O_CREAT _O_CREAT +#endif +#ifndef O_TRUNC +#define O_TRUNC _O_TRUNC +#endif + +// ---------------------------------------------------- +// Memory mapping stubs (mmap, munmap, etc.) +// ---------------------------------------------------- +// These emulate minimal mmap behavior using Win32 APIs +// to allow code that expects mmap() to compile and run +// (but with real file I/O behind the scenes). +// ---------------------------------------------------- + +#define PROT_READ 1 +#define PROT_WRITE 2 +#define MAP_PRIVATE 2 +#define MAP_FAILED ((void *)-1) + +static inline void *mmap(void *addr, size_t length, int prot, int flags, int fd, off_t offset) { + HANDLE hMap = CreateFileMappingA( + (HANDLE)_get_osfhandle(fd), + NULL, + PAGE_READWRITE, + 0, + (DWORD)length, + NULL + ); + if (!hMap) { + fprintf(stderr, "CreateFileMappingA failed: %lu\n", GetLastError()); + return MAP_FAILED; + } + + void *map = MapViewOfFile( + hMap, + FILE_MAP_READ | FILE_MAP_WRITE, + 0, + offset, + length + ); + + CloseHandle(hMap); + + if (!map) { + fprintf(stderr, "MapViewOfFile failed: %lu\n", GetLastError()); + return MAP_FAILED; + } + + return map; +} + +static inline int munmap(void *addr, size_t length) { + if (!UnmapViewOfFile(addr)) { + fprintf(stderr, "UnmapViewOfFile failed: %lu\n", GetLastError()); + return -1; + } + return 0; +} + +// ---------------------------------------------------- +// POSIX type aliases and missing symbols +// ---------------------------------------------------- +typedef int mode_t; +typedef intptr_t ssize_t; + +#endif // _WIN32 diff --git a/examples/ffmpeg-transcode.cpp b/examples/ffmpeg-transcode.cpp index 1fae58a4ffa..7c0c43b5b57 100644 --- a/examples/ffmpeg-transcode.cpp +++ b/examples/ffmpeg-transcode.cpp @@ -7,6 +7,8 @@ * Copyright (C) 2024 William Tambellini */ +#include "compat_win.h" + // Just for conveninent C++ API #include #include @@ -20,8 +22,6 @@ #include #include #include -#include -#include extern "C" { #include @@ -325,19 +325,19 @@ static int decode_audio(struct audio_buffer *audio_buf, s16 **data, int *size) // ifname: input file path // owav_data: in mem wav file. Can be forwarded as it to whisper/drwav // return 0 on success -int ffmpeg_decode_audio(const std::string &ifname, std::vector& owav_data) { +bool ffmpeg_decode_audio(const std::string &ifname, std::vector& owav_data) { LOG("ffmpeg_decode_audio: %s\n", ifname.c_str()); int ifd = open(ifname.c_str(), O_RDONLY); if (ifd == -1) { fprintf(stderr, "Couldn't open input file %s\n", ifname.c_str()); - return -1; + return false; } u8 *ibuf = NULL; size_t ibuf_size; int err = map_file(ifd, &ibuf, &ibuf_size); if (err) { LOG("Couldn't map input file %s\n", ifname.c_str()); - return err; + return false; } LOG("Mapped input file: %s size: %d\n", ibuf, (int) ibuf_size); struct audio_buffer inaudio_buf; @@ -351,7 +351,7 @@ int ffmpeg_decode_audio(const std::string &ifname, std::vector& owav_da LOG("decode_audio returned %d \n", err); if (err != 0) { LOG("decode_audio failed\n"); - return err; + return false; } LOG("decode_audio output size: %d\n", osize); @@ -364,5 +364,5 @@ int ffmpeg_decode_audio(const std::string &ifname, std::vector& owav_da // the data: memcpy(owav_data.data() + sizeof(wave_hdr), odata, osize* sizeof(s16)); - return 0; + return true; }