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

2024-04-19 15:08

本文主要是介绍如何在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 threadsSTDMETHOD(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 threadsSTDMETHOD_(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 reliableSTDMETHOD(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 reliableSTDMETHOD_(DWORD,GetStreamAR)() = 0;// Configure which pixel formats are enabled for output// If pixFmt is invalid, Get will return FALSE and Set E_FAILSTDMETHOD_(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 decoderSTDMETHOD(SetDeintFieldOrder)(LAVDeintFieldOrder fieldOrder) = 0;// get the deinterlacing field order of the hardware decoderSTDMETHOD_(LAVDeintFieldOrder, GetDeintFieldOrder)() = 0;// DEPRECATED, use SetDeinterlacingModeSTDMETHOD(SetDeintAggressive)(BOOL bAggressive) = 0;// DEPRECATED, use GetDeinterlacingModeSTDMETHOD_(BOOL, GetDeintAggressive)() = 0;// DEPRECATED, use SetDeinterlacingModeSTDMETHOD(SetDeintForce)(BOOL bForce) = 0;// DEPRECATED, use GetDeinterlacingModeSTDMETHOD_(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 runningSTDMETHOD_(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 activeSTDMETHOD_(LAVHWAccel, GetHWAccel)() = 0;// Set which codecs should use HW AccelerationSTDMETHOD(SetHWAccelCodec)(LAVVideoHWCodec hwAccelCodec, BOOL bEnabled) = 0;// Get which codecs should use HW AccelerationSTDMETHOD_(BOOL, GetHWAccelCodec)(LAVVideoHWCodec hwAccelCodec) = 0;// Set the deinterlacing mode used by the hardware decoderSTDMETHOD(SetHWAccelDeintMode)(LAVHWDeintModes deintMode) = 0;// Get the deinterlacing mode used by the hardware decoderSTDMETHOD_(LAVHWDeintModes, GetHWAccelDeintMode)() = 0;// Set the deinterlacing output for the hardware decoderSTDMETHOD(SetHWAccelDeintOutput)(LAVDeintOutput deintOutput) = 0;// Get the deinterlacing output for the hardware decoderSTDMETHOD_(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 systemsSTDMETHOD(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 systemsSTDMETHOD_(BOOL, GetHWAccelDeintHQ)() = 0;// Set the software deinterlacing mode usedSTDMETHOD(SetSWDeintMode)(LAVSWDeintModes deintMode) = 0;// Get the software deinterlacing mode usedSTDMETHOD_(LAVSWDeintModes, GetSWDeintMode)() = 0;// Set the software deinterlacing outputSTDMETHOD(SetSWDeintOutput)(LAVDeintOutput deintOutput) = 0;// Get the software deinterlacing outputSTDMETHOD_(LAVDeintOutput, GetSWDeintOutput)() = 0;// DEPRECATED, use SetDeinterlacingModeSTDMETHOD(SetDeintTreatAsProgressive)(BOOL bEnabled) = 0;// DEPRECATED, use GetDeinterlacingModeSTDMETHOD_(BOOL, GetDeintTreatAsProgressive)() = 0;// Set the dithering mode usedSTDMETHOD(SetDitherMode)(LAVDitherMode ditherMode) = 0;// Get the dithering mode usedSTDMETHOD_(LAVDitherMode, GetDitherMode)() = 0;// Set if the MS WMV9 DMO Decoder should be used for VC-1/WMV3STDMETHOD(SetUseMSWMV9Decoder)(BOOL bEnabled) = 0;// Get if the MS WMV9 DMO Decoder should be used for VC-1/WMV3STDMETHOD_(BOOL, GetUseMSWMV9Decoder)() = 0;// Set if DVD Video support is enabledSTDMETHOD(SetDVDVideoSupport)(BOOL bEnabled) = 0;// Get if DVD Video support is enabledSTDMETHOD_(BOOL,GetDVDVideoSupport)() = 0;// Set the HW Accel Resolution Flags// flags: bitmask of LAVHWResFlag flagsSTDMETHOD(SetHWAccelResolutionFlags)(DWORD dwResFlags) = 0;// Get the HW Accel Resolution Flags// flags: bitmask of LAVHWResFlag flagsSTDMETHOD_(DWORD, GetHWAccelResolutionFlags)() = 0;// Toggle Tray IconSTDMETHOD(SetTrayIcon)(BOOL bEnabled) = 0;// Get Tray IconSTDMETHOD_(BOOL,GetTrayIcon)() = 0;// Set the Deint ModeSTDMETHOD(SetDeinterlacingMode)(LAVDeintMode deintMode) = 0;// Get the Deint ModeSTDMETHOD_(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%)。


这篇关于如何在Directshow中使用硬解码功能的文章就介绍到这儿,希望我们推荐的文章对编程师们有所帮助!



http://www.chinasem.cn/article/917885

相关文章

Java使用Thumbnailator库实现图片处理与压缩功能

《Java使用Thumbnailator库实现图片处理与压缩功能》Thumbnailator是高性能Java图像处理库,支持缩放、旋转、水印添加、裁剪及格式转换,提供易用API和性能优化,适合Web应... 目录1. 图片处理库Thumbnailator介绍2. 基本和指定大小图片缩放功能2.1 图片缩放的

Python使用Tenacity一行代码实现自动重试详解

《Python使用Tenacity一行代码实现自动重试详解》tenacity是一个专为Python设计的通用重试库,它的核心理念就是用简单、清晰的方式,为任何可能失败的操作添加重试能力,下面我们就来看... 目录一切始于一个简单的 API 调用Tenacity 入门:一行代码实现优雅重试精细控制:让重试按我

深度解析Spring Security 中的 SecurityFilterChain核心功能

《深度解析SpringSecurity中的SecurityFilterChain核心功能》SecurityFilterChain通过组件化配置、类型安全路径匹配、多链协同三大特性,重构了Spri... 目录Spring Security 中的SecurityFilterChain深度解析一、Security

MySQL中EXISTS与IN用法使用与对比分析

《MySQL中EXISTS与IN用法使用与对比分析》在MySQL中,EXISTS和IN都用于子查询中根据另一个查询的结果来过滤主查询的记录,本文将基于工作原理、效率和应用场景进行全面对比... 目录一、基本用法详解1. IN 运算符2. EXISTS 运算符二、EXISTS 与 IN 的选择策略三、性能对比

使用Python构建智能BAT文件生成器的完美解决方案

《使用Python构建智能BAT文件生成器的完美解决方案》这篇文章主要为大家详细介绍了如何使用wxPython构建一个智能的BAT文件生成器,它不仅能够为Python脚本生成启动脚本,还提供了完整的文... 目录引言运行效果图项目背景与需求分析核心需求技术选型核心功能实现1. 数据库设计2. 界面布局设计3

使用IDEA部署Docker应用指南分享

《使用IDEA部署Docker应用指南分享》本文介绍了使用IDEA部署Docker应用的四步流程:创建Dockerfile、配置IDEADocker连接、设置运行调试环境、构建运行镜像,并强调需准备本... 目录一、创建 dockerfile 配置文件二、配置 IDEA 的 Docker 连接三、配置 Do

Android Paging 分页加载库使用实践

《AndroidPaging分页加载库使用实践》AndroidPaging库是Jetpack组件的一部分,它提供了一套完整的解决方案来处理大型数据集的分页加载,本文将深入探讨Paging库... 目录前言一、Paging 库概述二、Paging 3 核心组件1. PagingSource2. Pager3.

python使用try函数详解

《python使用try函数详解》Pythontry语句用于异常处理,支持捕获特定/多种异常、else/final子句确保资源释放,结合with语句自动清理,可自定义异常及嵌套结构,灵活应对错误场景... 目录try 函数的基本语法捕获特定异常捕获多个异常使用 else 子句使用 finally 子句捕获所

C++11右值引用与Lambda表达式的使用

《C++11右值引用与Lambda表达式的使用》C++11引入右值引用,实现移动语义提升性能,支持资源转移与完美转发;同时引入Lambda表达式,简化匿名函数定义,通过捕获列表和参数列表灵活处理变量... 目录C++11新特性右值引用和移动语义左值 / 右值常见的左值和右值移动语义移动构造函数移动复制运算符

Python对接支付宝支付之使用AliPay实现的详细操作指南

《Python对接支付宝支付之使用AliPay实现的详细操作指南》支付宝没有提供PythonSDK,但是强大的github就有提供python-alipay-sdk,封装里很多复杂操作,使用这个我们就... 目录一、引言二、准备工作2.1 支付宝开放平台入驻与应用创建2.2 密钥生成与配置2.3 安装ali