本文以一个Transform Filter为例,一步步整理Transform Filter一般开发步骤。
本文的Filter名为GrayFilter,把YUV视频信号转化为灰度信号,造成黑白电视的效果。
1. 配置开发环境
1. VC中在Tools->Options->Directories 设置好DirectX SDK的头文件和库文件路径
2. 编译了基类源码,生成strmbasd.lib (debug版), strmbase.lib(release版)
3. VC向导新建一个win32 DLL工程
4. Setting->Link->Output file name: GrayFilter.ax
5. Setting->Link加入strmbasd.lib winmm.lib quartz.lib vfw32.lib
6. 定义一个同名.def文件,加入到工程,内容如下:
LIBRARY GrayFilter.ax
EXPORTS
DllMain PRIVATE
DllGetClassObject PRIVATE
DllCanUnloadNow PRIVATE
DllRegisterServer PRIVATE
DllUnregisterServer PRIVATE
7. 定义DLL的入口函数及注册
extern "C" BOOL WINAPI DllEntryPoint(HINSTANCE, ULONG, LPVOID);
BOOL APIENTRY DllMain( HANDLE hModule,
DWORD ul_reason_for_call,
LPVOID lpReserved
)
...{
return DllEntryPoint((HINSTANCE)(hModule), ul_reason_for_call, lpReserved);
}
//
// DllRegisterServer
//
// Handle registration of this filter
//
STDAPI DllRegisterServer()
...{
return AMovieDllRegisterServer2(TRUE);
} // DllRegisterServer
//
// DllUnregisterServer
//
STDAPI DllUnregisterServer()
...{
return AMovieDllRegisterServer2(FALSE);
} // DllUnregisterServer
BOOL APIENTRY DllMain( HANDLE hModule,
DWORD ul_reason_for_call,
LPVOID lpReserved
)
...{
return DllEntryPoint((HINSTANCE)(hModule), ul_reason_for_call, lpReserved);
}
//
// DllRegisterServer
//
// Handle registration of this filter
//
STDAPI DllRegisterServer()
...{
return AMovieDllRegisterServer2(TRUE);
} // DllRegisterServer
//
// DllUnregisterServer
//
STDAPI DllUnregisterServer()
...{
return AMovieDllRegisterServer2(FALSE);
} // DllUnregisterServer
8. 相关的头文件
#include <olectl.h>
#include <initguid.h>
#include <streams.h>
#if (1100 > _MSC_VER)
#include <olectlid.h>
#endif
#include <olectlid.h>
#endif
2. 开发Filter
1. 生成GUID( 命令行模式下运行guidgen工具)
// {C26EA242-43E4-4326-9304-1CB6ECD29B25}
DEFINE_GUID(CLSID_GrayFilter,
0xc26ea242, 0x43e4, 0x4326, 0x93, 0x4, 0x1c, 0xb6, 0xec, 0xd2, 0x9b, 0x25);
DEFINE_GUID(CLSID_GrayFilter,
0xc26ea242, 0x43e4, 0x4326, 0x93, 0x4, 0x1c, 0xb6, 0xec, 0xd2, 0x9b, 0x25);
2. 继承自Transform Filter
3. 按格式改写构造函数
CVideoGray(TCHAR *tszName, LPUNKNOWN pUnk, HRESULT *phr) :
CTransformFilter(NAME("Video to Gray"), pUnk, CLSID_GrayFilter)
{ }
CTransformFilter(NAME("Video to Gray"), pUnk, CLSID_GrayFilter)
{ }
4. 改写CTransformFilter几个重要函数
HRESULT CheckInputType(const CMediaType *mtIn);
HRESULT CheckTransform(const CMediaType *mtIn, const CMediaType *mtOut);
HRESULT DecideBufferSize(IMemAllocator *pAlloc,
ALLOCATOR_PROPERTIES *pProp);
HRESULT GetMediaType(int iPosition, CMediaType *pMediaType);
HRESULT Transform(IMediaSample *pIn, IMediaSample *pOut);
HRESULT CheckTransform(const CMediaType *mtIn, const CMediaType *mtOut);
HRESULT DecideBufferSize(IMemAllocator *pAlloc,
ALLOCATOR_PROPERTIES *pProp);
HRESULT GetMediaType(int iPosition, CMediaType *pMediaType);
HRESULT Transform(IMediaSample *pIn, IMediaSample *pOut);
5. 注册Filter信息
// 注册信息
const AMOVIESETUP_MEDIATYPE sudPinTypes =
...{
&MEDIATYPE_Video, // Major type
&MEDIASUBTYPE_NULL // Minor type
};
const AMOVIESETUP_FILTER sudContrast =
...{
&CLSID_GrayFilter, // Filter CLSID
L"Video to Gray", // Filter name
MERIT_DO_NOT_USE, // Its merit
0, // Number of pins
NULL // Pin details
};
// List of class IDs and creator functions for the class factory. This
// provides the link between the OLE entry point in the DLL and an object
// being created. The class factory will call the static CreateInstance
CFactoryTemplate g_Templates[1] =
...{
...{ L"Video to Gray"
, &CLSID_GrayFilter
, CVideoGray::CreateInstance
, NULL
, &sudContrast }
};
int g_cTemplates = sizeof(g_Templates) / sizeof(g_Templates[0]);
const AMOVIESETUP_MEDIATYPE sudPinTypes =
...{
&MEDIATYPE_Video, // Major type
&MEDIASUBTYPE_NULL // Minor type
};
const AMOVIESETUP_FILTER sudContrast =
...{
&CLSID_GrayFilter, // Filter CLSID
L"Video to Gray", // Filter name
MERIT_DO_NOT_USE, // Its merit
0, // Number of pins
NULL // Pin details
};
// List of class IDs and creator functions for the class factory. This
// provides the link between the OLE entry point in the DLL and an object
// being created. The class factory will call the static CreateInstance
CFactoryTemplate g_Templates[1] =
...{
...{ L"Video to Gray"
, &CLSID_GrayFilter
, CVideoGray::CreateInstance
, NULL
, &sudContrast }
};
int g_cTemplates = sizeof(g_Templates) / sizeof(g_Templates[0]);
编译成功后生成GrayFilter.ax
命令行运行regsvr32 GrayFilter.ax注册即可