DirectShow filter (1) (learn about media type definitions)

Source: Internet
Author: User
  • Source:
  • Author: Lu Qiming
  • Sorting Date: 2004/12/27

    1. Filter Overview
    Filter is a COM component consisting of one or more pins. Pin is also a COM component. The extension of the filter file is. Ax, but it can also be. dll. Filter can be roughly divided into three types based on its input pin or output pin (or its position in filter graph): Source Filter (only output pin), transform filter (with both input pin and output pin) and Renderer Filter (only input pin ).

    Generally, a common Win32 DLL project is used to create a filter. In addition, the filter project generally does not use MFC. In this case, the application uses the cocreateinstance function as the filter instance, and the filter and application work in the binary level. Alternatively, you can create a filter in the MFC application project. In this case, the filter does not need to be registered as a COM component, and the collaboration between the filter and the application is at the source code level. When a filter instance is created, the cocreateinstance function is no longer used, but a filter object is directly created, as follows:
    M_pfilterobject = new cfilterclass ();
    // Make the initial refcount 1 to match com Creation
    M_pfilterobject-> addref ();
    Because the base class of the filter implements the reference counting of objects, even in the second case, the operation on the created filter object can fully follow the com standard.
    Filter is an independent function module. It is recommended that you do not depend on other third-party DLL. Because the filter has the location transparency feature of COM, the filter file can be stored in any location on the hard disk, as long as the location is moved and then re-registered. However, if the filter depends on other DLL, the filter will locate the DLL.

    The filter cannot be used independently from the filter graph. Therefore, if you want to bypass the filter graph and directly use the module function implemented by the filter, port your filter to DMO (DirectX Media object ). For DirectShow application developers, do not forget to use oleinitialize for initialization.

    2. Filter Registration
    Filter is a COM component, so you must register it before use. The registration procedure of filteris regsvr32.exe. If the command line parameter/u is included, it indicates cancellation. If yes/s is included, a prompt dialog box Indicating successful registration or cancellation is not displayed. If you want to automatically register when building the filter project, set the following settings on the vc m build page of Project Settings in VC:
    Description: Register Filter
    Commands: regsvr32/S/C $ (targetpath)
    Echo regsvr32 exe. Time> $ (targetdir) \ $ (targetname). Trg
    Outputs: $ (targetdir) \ $ (targetname). Trg

    The registration information of filter consists of the basic com Information and filter information. Registration information is stored in the registry. The former is hkey_classes_root \ CLSID \ filter CLSID \, and the latter is hkey_classes_root \ CLSID \ category \ instance \ filter CLSID \. The com information indicates that the filter is a standard COM component that can be created through the cocreateinstance function. The filter information indicates the information about the filter that we see through graphedit. If you do not want graphedit to see the filter you write (or let the filter enumerator find it), you do not have to register the filter information. And you don't have to worry about it. Doing so won't affect the filter function at all.
    Blocking the registration of filter information is also very easy. Because cbasefilter implements two functions of the iamoviesetup interface: Register and unregister. We only need to reload these two functions and directly return s_ OK.

    The merit value of the filter. This value is used by Microsoft's "smart connection" function. In graphedit, when we add a source filter and execute "render" on its pin, some filters will be automatically connected. Merit values are as follows:
    Merit_preferred = 0x800000,
    Merit_normal = 0x600000,
    Merit_unlikely = 0x400000,
    Merit_do_not_use = 0x200000,
    Merit_sw_compressor = 0x100000,
    Merit_hw_compressor = 0x100050
    The merit value can only be used by "smart connection" when it is greater than merit_do_not_use. The larger the merit value, the larger the chance of this filter.

    3. Pin Connection process between Filters
    A filter can be used only when it is added to the filter graph and connected to other filters as a complete link. The connection between filters (that is, the connection between pins) is actually a media type negotiation process between the two parties. The connection direction is always from output pin to input pin. The general process of the connection is: if the complete media type has been specified when the connection function is called, the media type is used for the connection. If the connection is successful, the connection process ends; if media type is not specified or not completely specified, the following enumeration process is performed. Enumerate all media on the input pin to be connected
    Type. These media types are connected to the output pin one by one (if the connection function provides incomplete media types, you must first check the matching of each enumerated media type ), if the output pin accepts this media type, the connection between the pins is declared successful. If all media types enumerated on the input pin are not supported, enumerate all media types on the output pin and connect them one by one with the input pin. If the Input Pin accepts one of the media
    Type, the connection between pin is also successful. If all media types and input pin on the output pin are not supported, the connection process between the two pins fails.

    Each pin can implement the getmediatype function to provide all the preferred media types supported on the pin (but generally only implemented on the output pin, the Input Pin mainly implements checkmediatype to check whether the media type currently provided is supported ). All media types obtained from pin Enumeration During connection are provided here.

    In the cbasepin class, there is a protected member variable m_btrymytypesfirst. The default value is false. Change the value of this variable to true in the output pin of the custom filter. You can customize our own connection process (first enumerate the media type on the output pin ).

    When the PIN is successfully connected, the completeconnect function is called on the pin. We can obtain some media type information on the connection and perform some calculations here. In the completeconnect Implementation of the output pin, another important task is to negotiate the memory configuration used for sample transmission after the filter graph runs. This is also an interactive process: first, you need to ask the Configuration Requirements on the input pin. If the Input Pin provides the Memory Manager (Allocator), the memory manager on the input pin is used first; otherwise, use output
    The memory manager generated by the pin. We usually need to implement decidebuffersize to determine the memory size of the stored sample. Note: After the negotiation is completed, the actual memory is not allocated, but the active function on the output pin must be called.

    4. Overview of filter media type
    Media type can be am_media_type or cmediatype. The former is a struct, and the latter is a class inherited from this struct.
    Each media type consists of three parts: Major type, subtype, and format type. All three parts are uniquely identified using guid. Major type mainly describes a media type, such as specifying a video, audio, or stream. Subtype further refines the media type, if video is used, you can specify uyvy, yuy2, rgb24, or rgb32. format type uses a struct to further refine the media type.
    If the three parts of media type specify a specific guid value, the media type is completely specified. If any of the three parts of media type is guid_null, this media type is not completely specified. Guid_null is used as a wildcard.

    Common major type:
    Mediatype_analogvideo; // analog capture
    Mediatype_interleaved; // DV camcorder

    Common subtype:
    Mediasubtype_argb32; // contains Alpha Value
    Mediasubtype_mpeg1payload; // video Payload
    Mediasubtype_mpeg1audiopayload; // audio Payload
    Mediasubtype_mpeg1system; // A/V Payload
    Mediasubtype_dvsd; // dv

    Common Format type:
    Format_dvinfo dvinfo
    Format_mpegvideo mpeg1videoinfo
    Format_mpeg2video mpeg2videoinfo
    Format_videoinfo videoinfoheader
    Format_videoinfo2 videoinfoheader2
    Format_waveformatex waveformatex

    5. data transmission between Filters
    Data between filters is transmitted through sample. Sample is a COM component with its own data buffer. The sample is centrally managed by allocator. As shown in:

    Data transmission between filters can be performed in two modes: Push mode and pull mode.

  • Related Article

    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: 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.