Improve jpeg conversion

This commit is contained in:
2026-04-16 10:59:28 +10:00
parent 6c72751a14
commit 8bb4f49b09
2 changed files with 39 additions and 10 deletions

View File

@@ -69,22 +69,25 @@ namespace ANSCENTER
// Fixed-size pool of NvJpegCompressors (~40MB VRAM each).
// Threads that can't acquire an encoder fall back to TurboJPEG.
// Fixed pool of NvJpegCompressors sized by GPU VRAM.
// Formula: poolSize = VRAM_GB / 2 (min 1, e.g. 2GB→1, 4GB→2, 8GB→4, 10GB→5).
// Threads that can't acquire an encoder fall back to TurboJPEG.
class NvJpegPool {
public:
static constexpr int kPoolSize = 4;
static NvJpegPool& Instance();
// Try to compress with nvJPEG. Returns empty string if no encoder
// available or on non-NVIDIA hardware — caller should fall back.
[[nodiscard]] std::string tryCompress(const cv::Mat& image, int quality);
[[nodiscard]] bool isAvailable() const noexcept { return _available; }
[[nodiscard]] int poolSize() const noexcept { return _poolSize; }
private:
NvJpegPool();
~NvJpegPool() = default;
NvJpegPool(const NvJpegPool&) = delete;
NvJpegPool& operator=(const NvJpegPool&) = delete;
bool _available = false;
std::array<std::unique_ptr<NvJpegCompressor>, kPoolSize> _encoders;
std::array<std::atomic<bool>, kPoolSize> _inUse; // lock-free slot flags
static int detectPoolSize();
bool _available = false;
int _poolSize = 0;
std::vector<std::unique_ptr<NvJpegCompressor>> _encoders;
std::unique_ptr<std::atomic<bool>[]> _inUse; // can't use vector — atomic is non-copyable
};
/// <summary>
/// // ANSOPENCV class provides various image processing functionalities using OpenCV and ANS Center SDK.