Загрузка данных


AVPixelFormat AVTypesConverterHelper::toAV(const PixelFormat &fmt)
{
    if (!fmt.isNative())
    {
        if (auto it = namedToFFmpeg.find(fmt.getNamedValue()); it != namedToFFmpeg.end())
        {
            return it->second;
        }
        return AV_PIX_FMT_NONE;
    }
    return static_cast<AVPixelFormat>(fmt.getNativeValue());
}

PixelFormat AVTypesConverterHelper::fromAV(AVPixelFormat fmt)
{
    static const std::map<AVPixelFormat, PixelFormat> ffmpegToNamed = [] {
        std::map<AVPixelFormat, PixelFormat> map;
        for (const auto &pair : namedToFFmpeg)
        {
            map[pair.second] = pair.first;
        }
        return map;
    }();
    if (auto it = ffmpegToNamed.find(fmt); it != ffmpegToNamed.end())
    {
        return PixelFormat(it->second);
    }
    return PixelFormat::fromNativeAV(fmt);
}

std::string AVTypesConverterHelper::toString(const PixelFormat &fmt)
{

    if (fmt.isNamed())
    {
        // Handle named formats
        if (auto it = namedToFFmpeg.find(fmt.getNamedValue()); it != namedToFFmpeg.end())
        {
            return av_get_pix_fmt_name(it->second);
        }
        return "unknown";
    }

    // Handle native formats
    AVPixelFormat avfmt = static_cast<AVPixelFormat>(fmt.getNativeValue());
    if (const char *name = av_get_pix_fmt_name(avfmt))
    {
        return name;
    }
    return "native_" + std::to_string(fmt.getNativeValue());
}
#pragma once

#include <cstdint>
#include <optional>
#include <string>

namespace EasyApi
{

// Pixel formats (aligned with FFmpeg/OpenCV for easy mapping)
class PixelFormat
{
  public:
    enum Type : uint32_t
    {
        unknown,
        // YUV formats
        YUV420P,
        NV12,
        YUYV422,
        // RGB formats
        RGB24,
        RGBA,
        // BGR formats (new additions)
        BGR24,    // Packed BGR 8:8:8
        BGRA,     // Packed BGR with alpha
        BGR0,     // Packed BGR with 0 byte padding
        BAYER_RG8,
        // Other essentials
        GRAY8,
        GRAY16,
        YUV422P,
        YUV444P,
        P010,
        P016,

        VAAPI,
        DRM,
        CUDA,

        //nativeMarker = 0x80000000    // Special indicator for raw formats
    };
    // Constructors
    constexpr PixelFormat()
        : mValue(EasyApi::PixelFormat::unknown)
    {}
    constexpr PixelFormat(EasyApi::PixelFormat::Type fmt)
        : mValue(fmt)
    {}
    // Constructor for raw FFmpeg formats
    //static PixelFormat fromNativeAV(uint32_t ffmpegFormat) { return PixelFormat(ffmpegFormat); }
    //constexpr bool isNamed() const noexcept { return (mValue) == 0; }
    //constexpr bool isNative() const noexcept { return (mValue) != 0; }
    constexpr EasyApi::PixelFormat::Type getNamedValue() const noexcept
    {
        return static_cast<EasyApi::PixelFormat::Type>(mValue);
    }
    // convertable to AV_PIX_FMT
    constexpr uint32_t getValue() const noexcept { return mValue; }

    // // String representation
    // std::string toString() const;
    // static std::optional<PixelFormat> fromString(const std::string &name);

    // Comparison
    bool operator==(const PixelFormat &other) const { return mValue == other.mValue; }
    bool operator!=(const PixelFormat &other) const { return mValue != other.mValue; }
    bool operator<(const PixelFormat &other) const { return mValue < other.mValue; }

  private:
    constexpr PixelFormat(uint32_t val)
        : mValue(val)
    {}
    uint32_t mValue;
};

enum class SampleFormat
{
    none = -1,
    U8,     ///< unsigned 8 bits
    S16,    ///< signed 16 bits
    S32,    ///< signed 32 bits
    FLT,    ///< float
    DBL,    ///< double

    U8P,     ///< unsigned 8 bits, planar
    S16P,    ///< signed 16 bits, planar
    S32P,    ///< signed 32 bits, planar
    FLTP,    ///< float, planar
    DBLP,    ///< double, planar
    S64,     ///< signed 64 bits
    S64P,    ///< signed 64 bits, planar

    // NB    ///< Number of sample formats. DO NOT USE if linking dynamically
};
static std::string toString(const PixelFormat &fmt)
{
    if (!fmt.isNamed())
        return "unsupported_native";
    auto t = fmt.getNamedValue();
    switch (t)
    {
        case PixelFormat::unknown:
            return "unknown";
        case PixelFormat::YUV420P:
            return "YUV420P";
        case PixelFormat::NV12:
            return "NV12";
        case PixelFormat::YUYV422:
            return "YUYV422";
        case PixelFormat::RGB24:
            return "RGB24";
        case PixelFormat::RGBA:
            return "RGBA";
        case PixelFormat::BGR24:
            return "BGR24";
        case PixelFormat::BGRA:
            return "BGRA";
        case PixelFormat::BGR0:
            return "BGR0";
        case PixelFormat::BAYER_RG8:
            return "BAYER_RG8";
        case PixelFormat::GRAY8:
            return "GRAY8";
        case PixelFormat::GRAY16:
            return "GRAY16";
        case PixelFormat::YUV422P:
            return "YUV422P";
        case PixelFormat::YUV444P:
            return "YUV444P";
        case PixelFormat::P010:
            return "P010";
        case PixelFormat::P016:
            return "P016";
        case PixelFormat::VAAPI:
            return "VAAPI";
        case PixelFormat::CUDA:
            return "CUDA";
        case PixelFormat::DRM:
            return "DRM";
    }
    return "not supported";
};
inline std::string toString(SampleFormat fmt)
{
    switch (fmt)
    {
        case SampleFormat::U8:
            return "u8";
        case SampleFormat::S16:
            return "s16";
        case SampleFormat::S32:
            return "s32";
        case SampleFormat::FLT:
            return "flt";
        case SampleFormat::DBL:
            return "dbl";

        case SampleFormat::U8P:
            return "u8p";
        case SampleFormat::S16P:
            return "s16p";
        case SampleFormat::S32P:
            return "s32p";
        case SampleFormat::FLTP:
            return "fltp";
        case SampleFormat::DBLP:
            return "dblp";
        case SampleFormat::S64:
            return "s64";
        case SampleFormat::S64P:
            return "s64p";

        case SampleFormat::none:
            return "none";
        default:
            return "unknown";
    }
}

}    // namespace EasyApi