#pragma once
// ANSGpuConvert.h — Cross-DLL GPU NV12→BGR conversion.
//
// ANSODEngine.dll exports ANSGpuNV12ToBGR() (defined in nv12_to_rgb.cu).
// This header provides gpu_nv12_to_bgr() which resolves that export at runtime
// via GetProcAddress — no link dependency on ANSODEngine.lib needed.
//
// Usage in video_player.cpp:
//   #include "ANSGpuConvert.h"
//   int rc = gpu_nv12_to_bgr(yPlane, yLinesize, uvPlane, uvLinesize,
//                             bgrOut, bgrStep, width, height, gpuIndex);
//   if (rc != 0) { /* fallback to CPU */ }

#include <cstdint>

#ifdef _WIN32
#define WIN32_LEAN_AND_MEAN
#define NOMINMAX
#include <windows.h>
#endif

// Function signature matching ANSGpuNV12ToBGR export in nv12_to_rgb.cu
typedef int (*ANSGpuNV12ToBGR_Fn)(
    const uint8_t* yPlane,   int yLinesize,
    const uint8_t* uvPlane,  int uvLinesize,
    uint8_t*       bgrOut,   int bgrStep,
    int width, int height, int gpuIndex);

// Resolve ANSGpuNV12ToBGR from ANSODEngine.dll at runtime.
// Returns nullptr if ANSODEngine.dll is not loaded or export not found.
inline ANSGpuNV12ToBGR_Fn resolve_gpu_nv12_to_bgr() {
#ifdef _WIN32
    static ANSGpuNV12ToBGR_Fn s_fn = nullptr;
    static bool s_tried = false;
    if (!s_tried) {
        s_tried = true;
        HMODULE hMod = GetModuleHandleA("ANSODEngine.dll");
        if (hMod) {
            s_fn = reinterpret_cast<ANSGpuNV12ToBGR_Fn>(
                GetProcAddress(hMod, "ANSGpuNV12ToBGR"));
        }
    }
    return s_fn;
#else
    return nullptr;
#endif
}

// Convenience wrapper. Returns 0 on success, negative on error, 1 if not available.
inline int gpu_nv12_to_bgr(
    const uint8_t* yPlane,   int yLinesize,
    const uint8_t* uvPlane,  int uvLinesize,
    uint8_t*       bgrOut,   int bgrStep,
    int width, int height, int gpuIndex = 0)
{
    auto fn = resolve_gpu_nv12_to_bgr();
    if (!fn) return 1;  // ANSODEngine not loaded — caller should use CPU fallback
    return fn(yPlane, yLinesize, uvPlane, uvLinesize, bgrOut, bgrStep, width, height, gpuIndex);
}
