如何编写Filter

本文详细介绍了如何在DirectShow中注册和注销Filter,包括注册到特定类别、设置Filter的Merit值以及创建实例。同时,文章讲解了Filter的Pin类型、属性页的添加以及Filter的用途选择。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

 
  注册 Filter
      AX 文件的一个对外接口 DllRegisterServer ,由外部调用,比如注册 AX 的时候: regsvr32 xxx.ax
 
       通常情况下,我们的 filter 可能注册在 ”Direct Show” 目录下,那么直接调用
       // Creates registry entries for the DLL
STDAPI DllRegisterServer ()
{
return AMovieDllRegisterServer2(TRUE) ;
}
 
AMovieDllRegisterServer2 DX 的帮助文档内的说明如下:
The AMovieDllRegisterServer2 function creates registry entries for every component in the g_Templates array. However, this function has some limitations. First, it assigns every filter to the "DirectShow Filters" category (CLSID_LegacyAmFilterCategory), but not every filter belongs in this category. Capture filters and compression filters, for example, have their own categories. Second, if your filter supports a hardware device, you might need to register two additional pieces of information that AMovieDLLRegisterServer2 does not handle: the medium and the pin category. A medium defines a method of communication in a hardware device, such as a bus. The pin category defines the function of a pin. For information on mediums, see KSPIN_MEDIUM in the Microsoft Windows Driver Development Kit (DDK). For a list of pin categories, see Pin Property Set.
      
       如果我们的引擎需要注册到 DirectShow 之外的目录,又该如何做?
       // 注册 Filter Video Compressor
REGFILTER2 rf2FilterReg =
{
    1,                                    // Version 1 (no pin mediums or pin category).
    MERIT_NORMAL,       // Merit.
    1,                                   // Number of pins.
    &sudPins                    // Pointer to pin information.
};
 
//Creates registry entries for the DLL
STDAPI DllRegisterServer( void)
{
    HRESULT hr = E_FAIL;
    IFilterMapper2 *pFM2 = NULL;
 
    hr = AMovieDllRegisterServer2(TRUE);                // 这个还是要调用的
    if (FAILED(hr))       return hr;
hr = CoCreateInstance( CLSID_FilterMapper2, NULL,
                                           CLSCTX_INPROC_SERVER,
                                                     IID_IFilterMapper2, (void **)&pFM2);
 
    if (FAILED(hr))       return hr;
 
    hr = pFM2->RegisterFilter( CLSID_SomeFilter,                        // Filter CLSID.
                                                    g_wszName,                                   // Filter name.
                                                    NULL,                                               // Device moniker.
                                                   &CLSID_VideoCompressorCategory,           // Video compressor category.
                                                   g_wszName,                                   // Instance data.
                                                   &rf2FilterReg                                    // Pointer to filter information.
                                                   );
    pFM2->Release();
    return hr;
}
 
注销 Filter
注销 DirectShow 内的引擎
// Removes registry entries for the DLL
{
return AMovieDllRegisterServer2(FALSE) ;
}
 
       // 注销 Video Compressor 下的引擎
       // Removes registry entries for the DLL
STDAPI DllUnregisterServer()
{
    HRESULT hr = E_FAIL;
    IFilterMapper2* pFM2 = NULL;
 
    hr = AMovieDllRegisterServer2(FALSE);
    if (FAILED(hr))       return hr;
 
    hr = CoCreateInstance(CLSID_FilterMapper2, NULL, CLSCTX_INPROC_SERVER,
                                IID_IFilterMapper2, (void **)&pFM2);
    if (FAILED(hr))       return hr;
 
    hr = pFM2->UnregisterFilter(&CLSID_VideoCompressorCategory,
                                             g_wszName, CLSID_SomeFilter);
 
    pFM2->Release();
    return hr;
}
 
Filter 起个名字
// Pin type 分为 Major Type Subtype
// 比如, Major Type = Video, Subtype = MPEG-2
const  AMOVIESETUP_MEDIATYPE sudPinTypes =
{
};
 
const  AMOVIESETUP_PIN psudPins[] =
{
       // 定义 Input Pin 的信息
{
& sudPinTypes
} ,      // The pin details
{
       }
};
 
// Declare filter information
const AMOVIESETUP_FILTER sudFilter =
{
& CLSID_MPKiller ,       // Filter CLSID
L" HQ MP Killer ",        // Filter name
0x8800000 ,                    // Its merit
2 ,                      // Number of pins
psudPins                       // Pin details
};
 
// declare a global array of CFactoryTemplate class instances, named g_Templates . Each
// CFactoryTemplate class contains registry information for one filter. Several filters can
// 在同一个 DLL 或者 AX 内,可以有多个引擎,比如系统目录下的 quartz.dll
// 所以,如果有多个引擎,相应的数组的大小就是引擎的个数。
CFactoryTemplate g_ Templates[] =
{
{
CImplement ::CreateInstance ,
}
};
 
int g_cTemplates = sizeof(g_Templates) / sizeof(g_Templates[0]);       // 有几个引擎
 
允许 Filter 应用
// CreateInstance CFactory 的一个接口,在 Filter 内部实现它
CUnknown* WINAPI CImplement::CreateInstance(     LPUNKNOWN pUnk,
                                                                                    HRESULT *pHr)
{
    CImplement *pFilter = new CImplement ();
    if  (!pFilter)
    {
        *pHr = E_OUTOFMEMORY;
    }
    return pFilter;
}
 
Filter Merit
Graph 会使用“傻子”机制联接不同的 filter ,这就要通过 filter merit 值的高低进行“傻子”联接。
 
Merit
enum
{
    MERIT_PREFERRED     = 0x800000,
    MERIT_NORMAL        = 0x600000,
    MERIT_UNLIKELY      = 0x400000,
    MERIT_DO_NOT_USE    = 0x200000,
    MERIT_SW_COMPRESSOR = 0x100000,
    MERIT_HW_COMPRESSOR = 0x100050
};
<= MERIT_DO_NOT_USE Merit Filter ,系统是不会去“傻子”联接的。当然 Merit 值可以是 任意值 ,而不一定是枚举出来的。
 
确定 Filter 的用途
不同的 Filter 有不同的用途,可以选择不同的基类,实现不同的方法。详见 DirectX 文档。 Filter 的种类,在内进行了详细的描述 root/DirectShow/DirectShow Reference/Constants and GUIDs/Filter Categories
 
添加属性页
CFactoryTemplate g _Templates[2] =
{
    {
    },
 
       {
                     // 这些数据,为属性页准备
              g_wszArcIPCamProperty,
              &CLSID_ArcIPCamProperty,
              CArcIPCamProperty::CreateInstance
       }
};
      
class CArcIPCam : public xxx, public IArcIPCam , public ISpecifyPropertyPages
{
    ~CArcIPCam();
 
 
 
           // Property Page---
 }
 
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值