如何在Directshow中使用硬解码功能

    在Directshow中使用硬解码一般通过DXVA技术,这是微软针对Windows系统定义的硬件加速解码的一个标准接口,目前很多显卡厂商已经实现了。通过DXVA接口,我们以一种统一的接口形式访问各种显卡的硬件加速(解码)功能,还可以通过API接口设置参数来控制解码的一些流程。但这篇博文不是给大家讲怎么用DXVA API来解码视频,而是介绍另外一种简单的方法:通过LAV Video Decoder。这是一个Directshow解码器插件,是开源的LAV Filters里面的一个子插件。

   LAVVideo即支持软解码,也支持硬解码(封装了DXVA接口),并且Filter提供接口给外部设置解码器的一些参数。下载LAV Filters的二进制文件包,里面有个developer_info目录,进去找到LAVVideoSettings.h,这个头文件声明了LAVVideo Decode Filter实现的接口。这个类的声明如下:

// LAV Video configuration interface
[uuid("FA40D6E9-4D38-4761-ADD2-71A9EC5FD32F")]
interface ILAVVideoSettings : public IUnknown
{
  // Switch to Runtime Config mode. This will reset all settings to default, and no changes to the settings will be saved
  // You can use this to programmatically configure LAV Video without interfering with the users settings in the registry.
  // Subsequent calls to this function will reset all settings back to defaults, even if the mode does not change.
  //
  // Note that calling this function during playback is not supported and may exhibit undocumented behaviour. 
  // For smooth operations, it must be called before LAV Video is connected to other filters.
  STDMETHOD(SetRuntimeConfig)(BOOL bRuntimeConfig) = 0;

  // Configure which codecs are enabled
  // If vCodec is invalid (possibly a version difference), Get will return FALSE, and Set E_FAIL.
  STDMETHOD_(BOOL,GetFormatConfiguration)(LAVVideoCodec vCodec) = 0;
  STDMETHOD(SetFormatConfiguration)(LAVVideoCodec vCodec, BOOL bEnabled) = 0;

  // Set the number of threads to use for Multi-Threaded decoding (where available)
  //  0 = Auto Detect (based on number of CPU cores)
  //  1 = 1 Thread -- No Multi-Threading
  // >1 = Multi-Threading with the specified number of threads
  STDMETHOD(SetNumThreads)(DWORD dwNum) = 0;

  // Get the number of threads to use for Multi-Threaded decoding (where available)
  //  0 = Auto Detect (based on number of CPU cores)
  //  1 = 1 Thread -- No Multi-Threading
  // >1 = Multi-Threading with the specified number of threads
  STDMETHOD_(DWORD,GetNumThreads)() = 0;

  // Set whether the aspect ratio encoded in the stream should be forwarded to the renderer,
  // or the aspect ratio specified by the source filter should be kept.
  // 0 = AR from the source filter
  // 1 = AR from the Stream
  // 2 = AR from stream if source is not reliable
  STDMETHOD(SetStreamAR)(DWORD bStreamAR) = 0;

  // Get whether the aspect ratio encoded in the stream should be forwarded to the renderer,
  // or the aspect ratio specified by the source filter should be kept.
  // 0 = AR from the source filter
  // 1 = AR from the Stream
  // 2 = AR from stream if source is not reliable
  STDMETHOD_(DWORD,GetStreamAR)() = 0;

  // Configure which pixel formats are enabled for output
  // If pixFmt is invalid, Get will return FALSE and Set E_FAIL
  STDMETHOD_(BOOL,GetPixelFormat)(LAVOutPixFmts pixFmt) = 0;
  STDMETHOD(SetPixelFormat)(LAVOutPixFmts pixFmt, BOOL bEnabled) = 0;

  // Set the RGB output range for the YUV->RGB conversion
  // 0 = Auto (same as input), 1 = Limited (16-235), 2 = Full (0-255)
  STDMETHOD(SetRGBOutputRange)(DWORD dwRange) = 0;

  // Get the RGB output range for the YUV->RGB conversion
  // 0 = Auto (same as input), 1 = Limited (16-235), 2 = Full (0-255)
  STDMETHOD_(DWORD,GetRGBOutputRange)() = 0;

  // Set the deinterlacing field order of the hardware decoder
  STDMETHOD(SetDeintFieldOrder)(LAVDeintFieldOrder fieldOrder) = 0;

  // get the deinterlacing field order of the hardware decoder
  STDMETHOD_(LAVDeintFieldOrder, GetDeintFieldOrder)() = 0;

  // DEPRECATED, use SetDeinterlacingMode
  STDMETHOD(SetDeintAggressive)(BOOL bAggressive) = 0;

  // DEPRECATED, use GetDeinterlacingMode
  STDMETHOD_(BOOL, GetDeintAggressive)() = 0;

  // DEPRECATED, use SetDeinterlacingMode
  STDMETHOD(SetDeintForce)(BOOL bForce) = 0;

  // DEPRECATED, use GetDeinterlacingMode
  STDMETHOD_(BOOL, GetDeintForce)() = 0;

  // Check if the specified HWAccel is supported
  // Note: This will usually only check the availability of the required libraries (ie. for NVIDIA if a recent enough NVIDIA driver is installed)
  // and not check actual hardware support
  // Returns: 0 = Unsupported, 1 = Supported, 2 = Currently running
  STDMETHOD_(DWORD,CheckHWAccelSupport)(LAVHWAccel hwAccel) = 0;

  // Set which HW Accel method is used
  // See LAVHWAccel for options.
  STDMETHOD(SetHWAccel)(LAVHWAccel hwAccel) = 0;

  // Get which HW Accel method is active
  STDMETHOD_(LAVHWAccel, GetHWAccel)() = 0;

  // Set which codecs should use HW Acceleration
  STDMETHOD(SetHWAccelCodec)(LAVVideoHWCodec hwAccelCodec, BOOL bEnabled) = 0;

  // Get which codecs should use HW Acceleration
  STDMETHOD_(BOOL, GetHWAccelCodec)(LAVVideoHWCodec hwAccelCodec) = 0;

  // Set the deinterlacing mode used by the hardware decoder
  STDMETHOD(SetHWAccelDeintMode)(LAVHWDeintModes deintMode) = 0;

  // Get the deinterlacing mode used by the hardware decoder
  STDMETHOD_(LAVHWDeintModes, GetHWAccelDeintMode)() = 0;

  // Set the deinterlacing output for the hardware decoder
  STDMETHOD(SetHWAccelDeintOutput)(LAVDeintOutput deintOutput) = 0;

  // Get the deinterlacing output for the hardware decoder
  STDMETHOD_(LAVDeintOutput, GetHWAccelDeintOutput)() = 0;

  // Set whether the hardware decoder should force high-quality deinterlacing
  // Note: this option is not supported on all decoder implementations and/or all operating systems
  STDMETHOD(SetHWAccelDeintHQ)(BOOL bHQ) = 0;

  // Get whether the hardware decoder should force high-quality deinterlacing
  // Note: this option is not supported on all decoder implementations and/or all operating systems
  STDMETHOD_(BOOL, GetHWAccelDeintHQ)() = 0;

  // Set the software deinterlacing mode used
  STDMETHOD(SetSWDeintMode)(LAVSWDeintModes deintMode) = 0;

  // Get the software deinterlacing mode used
  STDMETHOD_(LAVSWDeintModes, GetSWDeintMode)() = 0;

  // Set the software deinterlacing output
  STDMETHOD(SetSWDeintOutput)(LAVDeintOutput deintOutput) = 0;

  // Get the software deinterlacing output
  STDMETHOD_(LAVDeintOutput, GetSWDeintOutput)() = 0;

  // DEPRECATED, use SetDeinterlacingMode
  STDMETHOD(SetDeintTreatAsProgressive)(BOOL bEnabled) = 0;

  // DEPRECATED, use GetDeinterlacingMode
  STDMETHOD_(BOOL, GetDeintTreatAsProgressive)() = 0;

  // Set the dithering mode used
  STDMETHOD(SetDitherMode)(LAVDitherMode ditherMode) = 0;

  // Get the dithering mode used
  STDMETHOD_(LAVDitherMode, GetDitherMode)() = 0;

  // Set if the MS WMV9 DMO Decoder should be used for VC-1/WMV3
  STDMETHOD(SetUseMSWMV9Decoder)(BOOL bEnabled) = 0;

  // Get if the MS WMV9 DMO Decoder should be used for VC-1/WMV3
  STDMETHOD_(BOOL, GetUseMSWMV9Decoder)() = 0;

  // Set if DVD Video support is enabled
  STDMETHOD(SetDVDVideoSupport)(BOOL bEnabled) = 0;

  // Get if DVD Video support is enabled
  STDMETHOD_(BOOL,GetDVDVideoSupport)() = 0;

  // Set the HW Accel Resolution Flags
  // flags: bitmask of LAVHWResFlag flags
  STDMETHOD(SetHWAccelResolutionFlags)(DWORD dwResFlags) = 0;

  // Get the HW Accel Resolution Flags
  // flags: bitmask of LAVHWResFlag flags
  STDMETHOD_(DWORD, GetHWAccelResolutionFlags)() = 0;

  // Toggle Tray Icon
  STDMETHOD(SetTrayIcon)(BOOL bEnabled) = 0;

  // Get Tray Icon
  STDMETHOD_(BOOL,GetTrayIcon)() = 0;

  // Set the Deint Mode
  STDMETHOD(SetDeinterlacingMode)(LAVDeintMode deintMode) = 0;

  // Get the Deint Mode
  STDMETHOD_(LAVDeintMode,GetDeinterlacingMode)() = 0;
};

我们主要关注SetHWAccel方法:  STDMETHOD(SetHWAccel)(LAVHWAccel hwAccel) = 0; 传入参数的类型LAVHWAccel的定义如下:

// Type of hardware accelerations
typedef enum LAVHWAccel {
  HWAccel_None,
  HWAccel_CUDA,
  HWAccel_QuickSync,
  HWAccel_DXVA2,
  HWAccel_DXVA2CopyBack = HWAccel_DXVA2,
  HWAccel_DXVA2Native

};

我们一般选HWAccel_DXVA2,通过这种方式解码后的数据放在显存,直接显示效率很高。

后面的工作就简单了。构建一个播放文件的Filter Graph,将LAVVideo Decoder与Video Renderer连接,记住Renderer必须是EVR,否则硬解将不能够生效或者解码时的CPU比较高。运行Filter Graph,然后打开任务管理器查看CPU,会发现播放所占用的CPU很低(在我的酷睿i3 CPU的机器上小于5%)。


  • 1
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
DirectShow分离器和音视解码器(LAV Filters)汉化版是一组基于 ffmpeg 项目的 libavformat/libavcodec 库的 DirectShow 分离器和音视解码器,几乎允许您在 DirectShow 播放器播放任何格式。 LAV Filters 包含三个部分:LAV Splitter | LAV Video Decoder | LAV Audio Decoder,LAV Splitter 是源滤镜/分离器,基于 libavformat 和 libbluray 库,负责将文件分离成单独的基本流。 LAV Audio/Video Decoder 是质量和性能并重的强大的音视频解码器,不存在任何的折和妥协,在解码方案方面,除了通用的 DXVA2 外,也特别支持 NVIDIA CUDA 和 Intel QuickSync。 更新日志: 0.60.1 - 2014/01/14 LAV Splitter - 修复打开含 Stereo3D 元数据的 MPEG-2 文件时的崩溃问题 - 改进创建自内嵌 CUE 曲目单的章节名称的格式 - 修复 IPropertyBag 元数据接口同一些播放器之间的一处兼容性问题 0.60.0 - 2014/01/12 LAV Splitter - 改进一些蓝光光盘的播放,避免在剪辑改变时出现卡顿问题 - 支持将音频文件嵌入的 CUE 曲目单读取为章节 - 增加支持通过 IBitrateInfo 来报告流码率 (用于 MPC-HC 等) - 通过 IPropertyBag 来公开源文件的附加元数据 (包括视频方向信息:"ROTATION") - 修复含有较大 SSA/ASS 格式块(> 1MB)的 MKV 文件的分离 - 让 MKV 的某些 MPEG4 ASP 流能够平滑播放 LAV Video - 为最近的 Intel GPU 增加 VC-1/WMV3 DXVA2 解码支持 (如 Ivy Bridge/Haswell,需要最新的驱动程序) - 修复 AMD 平台在 DXVA2 Native 模式下停止播放或搜索时的崩溃问题 - 修复 WMVA 视频在软件解码模式下的播放问题 - 修复奇数高度的视频在转换至不同像素格式时的一些输出问题 - 修复向 madVR 报告视频范围时的一些极端情况 - 修复宽度未对齐(非 mod16)的原始视频文件在播放时的崩溃问题 LAV Audio - 现在检查 DTS 解码器的 DLL 版本,1.1.0.0 之前的版本将被阻止 (因为它们无法工作) - 修复一处可能会导致音频偶尔卡顿的 DTS 解析问题 - 改进对 Opus 音频预跳的支持
### 回答1: Qt DirectShow是一种用于在Qt应用程序使用DirectShow多媒体框架的库。DirectShow是Microsoft Windows平台上的多媒体框架,它提供了一些类和接口,用于处理音频、视频和其他多媒体数据。通过使用Qt DirectShow库,开发人员可以轻松地在他们的Qt应用程序添加多媒体功能使用Qt DirectShow库,我们可以实现多种功能,例如播放音频和视频文件、捕获和录制音频和视频、处理音频和视频流等。开发人员可以通过使用Qt DirectShow提供的类和接口,来控制音频和视频的播放、暂停、停止以及调整音量和进度等操作。同时,还可以通过使用Qt DirectShow库,来处理和分析音频和视频流的数据,实现诸如声音特效、视频滤镜等功能使用Qt DirectShow还可以实现与摄像头和麦克风等外部件设备的交互。通过使用DirectShow驱动,我们可以选择适用于特定件设备的相应的过滤器,实现对摄像头和麦克风的捕获和录制操作。 总而言之,Qt DirectShow为我们提供了在Qt应用程序使用DirectShow框架的便捷方式。通过使用这个库,开发人员可以轻松实现音频和视频的播放、录制和处理等功能,并且可以与外部件设备进行交互。 ### 回答2: Qt DirectShow是Qt的一个模块,用于集成Windows上的DirectShow多媒体框架。DirectShow是微软开发的一套用于处理多媒体数据的API,通过它可以实现音频和视频的播放、录制和编码等功能。 Qt DirectShow模块提供了一系列用于处理多媒体数据的类和函数,使开发者能够方便地在Qt应用程序集成DirectShow功能。通过使用这些类和函数,开发者可以轻松实现音视频的播放、录制和处理等操作。 Qt DirectShow模块的主要特点包括: 1. 支持常见的音视频格式:通过Qt DirectShow,开发者可以播放和录制诸如AVI、WMV、MP3等常见的音视频格式。这样,开发者可以轻松处理不同的音视频文件,为用户提供更加丰富的多媒体体验。 2. 简化多媒体操作:Qt DirectShow提供了一系列高级的类和函数,使开发者能够更加方便地进行多媒体操作。开发者可以轻松实现音视频的播放和暂停、音量控制、视频截图等功能,大大简化了多媒体处理的过程。 3. 灵活的接口设计:Qt DirectShow模块采用了灵活的接口设计,使得开发者能够根据自己的需求进行更加个性化的功能实现。开发者可以通过Qt的信号和槽机制实现自定义的多媒体事件处理,为应用程序增加更多的交互性。 4. 跨平台支持:尽管DirectShow是Windows上的API,但Qt DirectShow模块提供了跨平台的支持。开发者可以在不同的操作系统上使用Qt DirectShow模块进行多媒体开发,最大限度地提高了开发效率和代码的可移植性。 总之,Qt DirectShow模块为开发者提供了方便、高效和灵活的多媒体处理能力,使他们能够更加轻松地实现音视频的播放、录制和处理等功能。无论是开发音视频播放器、多媒体编辑工具还是实时视频通信应用,Qt DirectShow都是一个不错的选择。 ### 回答3: Qt DirectShow 是一种在使用 Qt 框架开发程序时,用于处理多媒体和视频的插件和库。DirectShow 是 Microsoft 开发的一种多媒体框架,而 Qt DirectShow 则是将 DirectShow功能集成到 Qt ,使开发人员可以更方便地处理多媒体数据。 Qt DirectShow 提供了一些类和接口,用于处理音频和视频流,包括捕获、播放、编辑和编码等操作。使用 Qt DirectShow,我们可以创建一个视频播放器,并通过接口来控制和操作视频的播放、暂停、停止等功能。我们还可以从摄像头捕获视频流,进行实时预览或者录像功能。另外,Qt DirectShow 还支持对视频进行编辑和编码,比如裁剪、拼接、添加字幕等操作。 Qt DirectShow 还提供了一些方便的功能,如件加速和音频处理,可以提高程序的性能和用户体验。我们可以利用件加速来解码和渲染视频,以提高播放的流畅性和质量。而音频处理则可以用于实现音频的混音、变声、降噪等效果。 总而言之,Qt DirectShow使用 Qt 框架进行多媒体和视频处理时提供了丰富的功能和接口,能够方便地实现视频播放、录像、编辑和编码等操作。它的使用可以大大简化开发人员的工作,并提高程序的性能和用户体验。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值