Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
122 changes: 122 additions & 0 deletions examples/compat_win.h
Original file line number Diff line number Diff line change
@@ -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 <io.h>
#include <fcntl.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <windows.h>
#include <stdint.h>
#include <stdlib.h>
#include <stdio.h>

// ----------------------------------------------------
// 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
14 changes: 7 additions & 7 deletions examples/ffmpeg-transcode.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,8 @@
* Copyright (C) 2024 William Tambellini <william.tambellini@gmail.com>
*/

#include "compat_win.h"

// Just for conveninent C++ API
#include <vector>
#include <string>
Expand All @@ -20,8 +22,6 @@
#include <sys/types.h>
#include <sys/stat.h>
#include <fcntl.h>
#include <unistd.h>
#include <sys/mman.h>

extern "C" {
#include <libavutil/opt.h>
Expand Down Expand Up @@ -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<uint8_t>& owav_data) {
bool ffmpeg_decode_audio(const std::string &ifname, std::vector<uint8_t>& 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;
Expand All @@ -351,7 +351,7 @@ int ffmpeg_decode_audio(const std::string &ifname, std::vector<uint8_t>& 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);

Expand All @@ -364,5 +364,5 @@ int ffmpeg_decode_audio(const std::string &ifname, std::vector<uint8_t>& owav_da
// the data:
memcpy(owav_data.data() + sizeof(wave_hdr), odata, osize* sizeof(s16));

return 0;
return true;
}