c++ - 如何通过 UVC 支持硬件编码 H264

标签 c++ video directshow h.264

我在使用 DirectShow 创建的视频聊天应用程序中使用罗技 C930e 网络摄像头。到目前为止,我能够在 YUY2 或 mJPEG 中使用原始流。不管怎样,我发现网络摄像头通过 UVC 接口(interface)支持硬件 H264 编码。

现在我使用标准方法获取可能的网络摄像头捕获引脚配置,但那里没有 H264 引脚。

void list_cameras {
    ICreateDevEnum *pDevEnum = nullptr;
    IEnumMoniker *pEnum = nullptr;

    // Create the System Device Enumerator.
    HRESULT hr = CoCreateInstance(CLSID_SystemDeviceEnum, nullptr,
                                  CLSCTX_INPROC_SERVER, IID_ICreateDevEnum,
                                  reinterpret_cast<void**>(&pDevEnum));
    if (SUCCEEDED(hr)) {
        // Create an enumerator for the video capture category.
        hr = pDevEnum->CreateClassEnumerator(
            CLSID_VideoInputDeviceCategory,
            &pEnum, 0);
        if (hr == S_FALSE) {
            return;
        }
    }

    IMoniker *pMoniker = nullptr; // IMoniker is the device
    int index = 0;

    // for each device
    while (pEnum->Next(1, &pMoniker, nullptr) == S_OK) {

        // bind camera to filter to be able to use it
        if (cam.device->BindToObject(nullptr, nullptr, IID_IBaseFilter, reinterpret_cast<void**>(&_pCapture)) != S_OK) {
            continue;
        }

        // fetch the configuration interface
        IAMStreamConfig *pConfig = nullptr;
        HRESULT hr = _capture->FindInterface(
            &PIN_CATEGORY_CAPTURE, // Preview pin.
            nullptr, // Any media type.
            _pCapture, // Pointer to the capture filter.
            IID_IAMStreamConfig, reinterpret_cast<void**>(&pConfig));

        if (FAILED(hr)) {
            continue;
        }

        // fetch possible configurations
        int iCount = 0, iSize = 0;
        if (pConfig->GetNumberOfCapabilities(&iCount, &iSize) != S_OK) {
            continue;
        }

        // store each configuration
        AM_MEDIA_TYPE *pmtConfig;
        for (int iFormat = 0; iFormat < iCount; iFormat++) {
            // get config
            VIDEO_STREAM_CONFIG_CAPS scc;
            if (pConfig->GetStreamCaps(iFormat, &pmtConfig, reinterpret_cast<BYTE*>(&scc)) != S_OK) {
                continue;
            }

            // copy config data
            VIDEOINFOHEADER *pVih = new VIDEOINFOHEADER(); // deleted afterwards
            *pVih = *reinterpret_cast<VIDEOINFOHEADER *>(pmtConfig->pbFormat);

            AM_MEDIA_TYPE mt;
            mt = *pmtConfig;
            mt.pbFormat = reinterpret_cast<BYTE *>(pVih);
            auto fcc = FOURCCMap(pVih->bmiHeader.biCompression);

            // wrap it
            CameraConfig config = { mt, pVih->bmiHeader.biWidth, pVih->bmiHeader.biHeight, 1000 / (static_cast<float>(pVih->AvgTimePerFrame) / 10000), fcc };

            // if resolution is bad (or unsupported), skip this configuration
            if (config.width == 0 || config.height == 0 ) // bad
                continue;

            cam.configurations.push_back(config);
        }
        _cameras.push_back(cam);

        pConfig->Release();
        _pCapture->Release();
    }

    pEnum->Release();
    pDevEnum->Release();
}

_cameras是Camera的 vector ,定义如下:

typedef struct {
    //! Pointer to DirectShow device.
    DSDevice device; 

    //! Camera name
    string name;

    //! List of supported configurations.
    vector<CameraConfig> configurations; // list of all available configurations

    //! Index of selected configuration.
    int selected;
} Camera;

_pCapture 是指向创建的捕获过滤器的指针。 CameraConfig 定义如下:

typedef struct {
    //! Media type.
    AM_MEDIA_TYPE _mediaType;

    //! Output width.
    int width;

    //! Outpus height.
    int height;

    //! Output framerate.
    float fps;

    //! Compression algoritm. YUY2 and mJPEG are supported for now.
    FOURCCMap compression;
} CameraConfig;

如何实现对 UVC 设备的支持?硬件编码器的哪些参数可以控制?

谢谢。

最佳答案

我能够在 Windows 8.x 下获得该流的唯一方法是不使用 LOGITECH 驱动程序。这是一个 UVC 1.5 兼容相机,它将由操作系统自动配置。使用该驱动程序(来自 Microsoft),使用引脚 1(而不是 0),您将获得大量 H264 格式。

我认为在 Windows 7 下,Logitech 的一些驱动程序也提供了一些 H264 格式。

如果您使用的是 Windows 8.x,并且您已经安装了 Logitech 驱动程序,则必须将其删除。这很难。我通常通过编写这样的结构(以管理员身份运行)来做到这一点:

BOOL res;
res = SetupUninstallOEMInf(TEXT("oem131.inf"), SUOI_FORCEDELETE, nullptr );

至于要删除什么 inf,这很简单:转到 Windows\Inf 文件夹并搜索 Logitech。然后检查每个文件,看看到底是什么(您可能有鼠标或其他您想要保留的东西)。通常不止一个。

关于c++ - 如何通过 UVC 支持硬件编码 H264,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/24675538/

相关文章:

c++ - 为什么类型应该放在未命名的命名空间中?

c++ - 模板参数请引用自己的类型

c# - 限制通过C#上传到YouTube的视频的长度

ffmpeg - FFMPEG 中缺少 DirectShow 过滤器

c++ - 在 C++ 中使用 DirectShow 解码带有 H.264 流的 MP4 视频

c++ - 使用 linux echo 命令的变量内容列表 Run with qtProcess

c++ - 流操纵器如何工作?

python - python完成后如何停止ffmpeg

video - 如何使用 Youtube 的新嵌入代码样式自动播放视频?

audio - 使用 DirectShow 确定音频文件的长度