Important:
This is retired content. This content is outdated and is no longer being maintained. It is provided as a courtesy for individuals who are still using these technologies. This content may contain URLs that were valid when originally published, but now link to sites or pages that no longer exist.
A version of this page is also available for
4/8/2010

The following steps show you how to define and instantiate your filter class.

Determine the base classes from which to derive your filter class (and pin classes, if necessary). Typically, your transform filter class derives from the CTransformFilter, CTransInPlaceFilter, or CVideoTransformFiltertransform base classes, or from the more generic CBaseFilterclass.

If you want to transform video media (especially AVI data), derive from CVideoTransformFilter.

If your filter must copy the input media samples, derive from CTransformFilter.

If your filter can transform the sampled media in place, derive from CTransInPlaceFilter.

If you do not want the simple transform filter support provided in the transform base classes, but prefer to implement your own member functions, derive from CBaseFilter.

For more information, see Determine Which Base Classes to Use.

In the following example, the filter class derives from CTransInPlaceFilter.

Copy Code
	class CMyFilter  : public CTransInPlaceFilter

Implement the IUnknowninterface for your object.

In the public section of your filter class definition, create an instance of CUnknown, and then call the DECLARE_IUNKNOWNmacro.

Copy Code
	public:
		static CUnknown *WINAPI CreateInstance(LPUNKNOWN punk,
HRESULT 
	*phr);
		DECLARE_IUNKNOWN;

Define your constructor. Also, define your Transformand CheckInputTypefunctions (this does not apply if your filter class is derived from CBaseFilter).

In the private section of your filter class definition, define your constructor by calling the constructor of the transform filter class you derived from, and then add code to perform the transform and check the input type. For example:

Copy Code
	// Define your constructor by calling the constructor of 
	// CTransInPlaceFilter.
	CMyFilter(TCHAR *tszName, LPUNKNOWN punk, HRESULT *phr)
	: CTransInPlaceFilter (tszName, punk, CLSID_MyFilter, phr)
	{ }

	// Add the transform code.
	HRESULT Transform(IMediaSample *pSample){ 
	// Transform code here
}

	// Add code to check the input type.
	HRESULT CheckInputType(const CMediaType* mtIn) { 
	// Input checking code here
}

Implement CreateInstancefor your filter object. Typically, CreateInstancecalls the constructor of your filter class. For example:

Copy Code
	CUnknown * WINAPI CMyFilter::CreateInstance(LPUNKNOWN punk,
HRESULT *phr) {
	CMyFilter *pNewObject = new CMyFilter(NAME("Description of My
Filter"), 
		punk, phr );
	if (pNewObject == NULL) {
		*phr = E_OUTOFMEMORY;
}
	return pNewObject;
} 

Declare a global array g_Templatesof CFactoryTemplateobjects to inform the default class factory code how to access the CreateInstancefunction.

Copy Code
	CFactoryTemplate g_Templates[]=
		{   { L"My Filter"	
			, &CLSID_MyFilter
			, CMyFilter::CreateInstance  // function called by
class factory
			, NULL
			, &sudMyFilter }	 // address of the
AMOVIESETUP_FILTER structure,
								 //   or NULL if no structure
exists
};
	int g_cTemplates = sizeof(g_Templates)/sizeof(g_Templates[0]); 

The g_cTemplatesvariable defines the number of class factory templates for the filter.

Each template provides a link between COM and the filter and is used to create the base object for the filter. At a minimum, the filter has one template that provides the address of its own CreateInstancefunction, which, when called, creates the base object.

For information about using CFactoryTemplatein registration, see Registering DirectShow Filters.

Generate a GUID for your filter object using either uuidgen.exe or guidgen.exe, which are available in the Microsoft Windows SDK or with Visual Studio. For more information, see MSDN.

Insert the code for the GUID before the class definition in the header file or main file.

The following code shows an example of a GUID definition.

Copy Code
	// {3FA5D260-AF2F-11d0-AE9C-00A0C91F0841}
	DEFINE_GUID(CLSID_MyFilter, 
	0x3fa5d260, 0xaf2f, 0x11d0, 0xae, 0x9c, 0x0, 0xa0, 0xc9, 0x1f,
0x8, 0x41);