Create your own Filter

Source: Internet
Author: User

I recently learned how to write my own filter in DirectShow. I can see a good summary on the Internet.ArticleA small part is added here, because the notes are always copied and the hands are a little sour. The best way to learn DirectShow is: first look at the basic knowledge, then lookCodeAnd implement filter by yourself.

 

4 How to implement your own Filter

Here we will talk about how to create your ownFilterNow, let's writeCtransformfilterExample

1Select a base class and declare your own class

CreateFilterYou only need to select different base classes based on your needs.FilterDerive your ownFilterIt supportsComFeature. 

Logically Filter Select an appropriate Filter The base class is crucial. To do this, you must Filter Has a considerable understanding of the base class. In practical applications, Filter Does not always choose Cbasefilter . On the contrary, most of the data we write is intermediate transmission. Filter ( Transform Filter ), So select the base class CtransformfilterAnd Ctransinplacefilter Mostly. If we write the source Filter , We can choose Csource As the base class; If yes Renderer Filter , You can select Cbaserenderer Or Cbasevideorenderer .
In short, select Filter Is very important. Of course, select Filter The base class is also very flexible, there is no absolute standard. Can pass Ctransformfilter Implemented FilterOf course Cbasefilter Step by step. Next, based on my actual experience Filter Some suggestions on the selection of base classes are provided for your reference.

First, you must specify thisFilterWhat kind of function is required?FilterProject. Keep as much as possibleFilterThe singularity of the implemented functions. If necessary, you can break down the requirements by two (or more) functions with a singleFilterTo achieve the overall functional requirements.

Second, you should clarify this Filter Generally Filter graph Location, this Filter What is the input data, what is the output data, there are several inputs Pin , Several outputs PinAnd so on. You can plot this Filter . Find out what is important at 01:10, which will directly decide which one you use " Model " Of Filter . For example, if Filter Only one input Pin And an output Pin And the media type is the same as the input Ctransinplacefilter As Filter . If the media types are different, select Ctransformfilter As the base class.
Furthermore, some special requirements for data transmission and processing are considered. For example Filter Input and Output Sample This is not a one-to-one correspondence. Pin The data is cached, and the output Pin Use a dedicated thread for data processing. In this case, Filter Base class selection Csource (Although this Filter Not the source Filter ).
When Filter After the base class is selected, Pin The base class is selected accordingly. Next Filter And Pin. One thing to note is that from the software design point of view, your logic class code should be the same Filter The code is separated. Next, let's take a look at the input. Pin . You need to implement all pure virtual functions of the base class, such Checkmediatype . In Checkmediatype You can check the media type to see if it is what you expect. Because most Filter The data is transmitted in Push mode. Pin Generally Receive Method. Some base classes have implemented Receive In Filter Class. In this case, you do not need to reload Receive Unless the implementation of the base class does not meet your actual requirements. If you reload Receive Method. Generally, the following three functions are reloaded at the same time. Endofstream , Beginflush And Endflush . Let's take a look at the output. Pin . Generally, you need to implement all the pure virtual functions of the base class, Checkmediatype In addition to media type check Decidebuffersize To decide Sample Memory usage, Getmediatype Supported media types.

Finally, let's take a look.FilterClass implementation. First of all, you must implement all pure virtual functions of the base class. In addition,FilterAlso implementCreateinstanceTo provideComTo achieveNondelegatingqueryinterfaceTo expose the supported interfaces. If we create custom Input and OutputPinIn general, we need to reloadGetpincountAndGetpinTwo functions.

 For exampleFilterNoPinInterface, but in myDemoInFilterBut there isOut pinAnd oneInput Pin.

 MyFilterThe class is defined as follows:

Class Cmyfilter:PublicCcritsec,PublicCbasefilter

{

Public:

cmyfilter ( tchar * pname , lpunknown punk , hresult * HR );

virtual ~ cmyfilter ();

static cunknown * winapi createinstance ( lpunknown punk , hresult * phr );

Cbasepin *Getpin(Int N);

Int Getpincount();

};

Note: Because the base class is a pure virtual base classFilterBe sure to derive a pure virtual function. Otherwise, the compiler will prompt that your derived class is also a pure virtual class. You are creating thisComWhen a component object is created, objects cannot be created by pure virtual classes.

2 filter Generate a CLSID

You can useGuidgenOrUuidgenToYour ownFilterGenerate128BitIDAnd then useDefine_guidMacroFilterThe header fileFilterOfCLSID;

 
[Myfilter. H]

// {1915c5c7-02aa-440f-890f-76d94c85aaf1}

Define_guid (clsid_myfilter,

0x1915c5c7, 0x2aa, 0x0000f, 0x89, 0xf, 0x76, 0xd9, 0x4c, 0x85, 0xaa, 0xf1 );

 
This clsid_myfilter is used in the category factory array and also used when registering the filter.

3 cmyfilterSimple implementation of Classes

This class is simple for demonstration purposes. You can refer to myDemo, ThatFilterWrite Functions are complete.

Cmyfilter: cmyfilter (tchar * pname, lpunknown punk, hresult * hr)

: Cbasefilter (name ("My filter"), punk, this, clsid_myfilter)

{}

Cmyfilter ::~ Cmyfilter ()

{}

 

// Public method that returns a new instance.

Cunknown * winapi cmyfilter: createinstance (lpunknown punk, hresult * phr)

{

Cmyfilter * pfilter = new cmyfilter (name ("My filter"), punk, PHR );

If (pfilter = NULL)

{

* Phr = e_outofmemory;

}

Return pfilter;

}

 

Cbasepin * cmyfilter: getpin (int n)

{

Return NULL;

}

Int cmyfilter: getpincount ()

{

Return 0;

}

In this way,FilterBut thisFilterNot associatedPin, But the implementationFilterAs for the logic, suchFilterAndPinHow to connect and how data streams flowSDKYou can writeFilter.

 Below is a summary.FilterAt least those things are needed.

1 FilterImplementation class

Here isCmyfilterClass, in which you can implement your own logical functions, including defining yourFilterFor youFilterEquipmentPinInterfaces.

2 comComponent Extraction Function

Five global functions

Dllmain // DLLEntry Function

Dllgetclassobject //ObtainComComponent factory object

Dllcanunloadnow // comCan components be detached?

Dllregisterserver //RegisterComComponents

Dllunregisterserver //UninstallComComponents

 WhereDllgetclassobjectYou only need to complete three functions by yourself as the base class has been completed.Dllmain,Dllregisterserver,Dllunregisterserver.

3 ComComponent factory object

 The class factory object is used to generateFilterThe template class defines a global Template Class Object array. The general format is as follows:

Cfactorytemplate g_templates [1] =

{

{

L "my filter", // name

& Clsid_myfilter, // FilterOfCLSID

Cmyfilter: createinstance ,//CreateFilterMethod

Null, // initialization Function

&Sudinftee// Filter(A structure)

}

};

Int g_ctemplates = sizeof (g_templates)/sizeof (g_templates [0]);

4About your own definitionFilterAndPinInformation

These are global structural variables used to describe yourFilterAnd what you definedPin, At registrationFilterIt will be used as follows:

Amoviesetup_filter DescriptionFilter

Amoviesetup_pinDescriptionPin

Amoviesetup_mediatype Description Data Type

The following code describesFilterWithOutput pin

Static const wchar g_wszname [] = l "some filter ";

Amoviesetup_mediatypeSudmediatypes[] = {

{& Mediatype_video, & mediasubtype_rgb24 },

{& Mediatype_video, & mediasubtype_rgb32 },

};

Amoviesetup_pinSudoutputpin= {

L "", // obsolete, not used.

False, // is this pin rendered?

True, // is it an output pin?

False, // can the filter create zero instances?

False, // does the filter create multiple instances?

& Guid_null, // obsolete.

Null, // obsolete.

2, // number of media types.

Sudmediatypes// Pointer to media types.

};

 

Amoviesetup_filter sudfilterreg = {

& Clsid_somefilter, // filter clsid.

G_wszname, // Filter Name.

Merit_normal, // merit.

1, // Number of Pin types.

&Sudoutputpin // Pointer to pin information.

};

LastSudfilterregWill be used in the class factory object array.

Cfactorytemplate g_templates [1] =

{

{

L "my filter", // name

& Clsid_myfilter, // CLSID

Cmyfilter: createinstance, // method to create an instance of mycomponent

Null, // initialization Function

&Sudfilterreg// Set-up information (for filters)

}

};

Finally, if you still cannot debug the file, check if you include the following header file.

# Include <streams. h> # include <initguid. h>

# Include <tchar. h> # include <stdio. h>

 

 

 

Intelligent fishAoosang 2004-09-01

 

Author's blog: Http://blog.csdn.net/aoosang/

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.