General Programming

Plain coding.

DirectShow custom transform filter skeleton

After my last struggles with building a DirectShow video filter, I've decided to publish a skeleton application for a video transform filter.

This skeleton application is using the CTransformFilter approach, it means that it performs per frame transformations on a copy of the frame.

 

While you might think that transforming a copy of the video frame is not effective as you are using double the size of the memory for each frame, but in fact - this approach is quite more performance-effective for the project that I was building as accessing and modifying the data in the video card memory consumes more time than working on your local memory in some cases (when your video cards is not fast i.e  an embedded devide) and my project was the case.

 

Of course, you should consult the MSDN documentation upon writing your filters and I would suggest you to write it from scratch if it's a sophisticated one, if you might need more than one input and output pin, or in general you need to customize it to meet your needs. This code is by no means a silver bullet.

I would like you to excuse me for leaving the classes and methods unchanged in the source code. As this skeleton is a crippled-down version of my original project that was really a pain in the ass.

Using the attached source code to create a new filter is limited to implementing one method - the Transform method in the OculusVideoFilter class:

 

HRESULT OculusVideoFilter::Transform(IMediaSample *pMedia)
{
    BYTE *pData;                
    long lDataLen;              
    int iPixel;                 
    int temp,x,y;               
    RGBTRIPLE *pRGB;            

    AM_MEDIA_TYPE* pType = &m_pInput->CurrentMediaType();
    VIDEOINFOHEADER *pvi = (VIDEOINFOHEADER *) pType->pbFormat;
    ASSERT(pvi);

    CheckPointer(pMedia,E_POINTER);
    pMedia->GetPointer(&pData);
    lDataLen = pMedia->GetSize();

    int cxImage    = pvi->bmiHeader.biWidth;
    int cyImage    = pvi->bmiHeader.biHeight;
    int numPixels  = cxImage * cyImage;

    pRGB = (RGBTRIPLE*) pData;
    for (iPixel=0; iPixel < numPixels; iPixel++, pRGB++) {
        /*
            here you implement your per-pixel operation on the currently processed frame.
        */
    }
    return NOERROR;
}
 
This skeleton app is COM-ready (It's required for DirectShow filters to be COM ready), so all you need after implementing your filtering logic is to compile the library, it will generate a .DLL file for you and use regsvr32.exe to register it.
One more note: Please remember to change the CLSID of the class, as it will conflict with other registered interfaces. If you would leave the CLSID:
 
// {5F1E81B9-9669-444D-A007-63EBB5CE92E3}
static const GUID CLSID_OculusFilter = 
{ 0x5f1e81b9, 0x9669, 0x444d, { 0xa0, 0x7, 0x63, 0xeb, 0xb5, 0xce, 0x92, 0xe3 } };
 

Unchanged and one day it might happen that you'll use my piece of software (or two people using this skeleton will not change this CLSID) you will end up with conflicts in your registry.

 

Download package.

 

Now as an example I'll show you how to create a video filter that will grayscale your output video.

The grayscale calculation is: grey = (30 * red + 59 * green + 11 * blue) / 100

A faster (but less precise) method would be: grey = (red + green) / 2

 

Now, with this navigate to the filter class to line number 137 and in your loop should look similar to this:

 

for (iPixel=0; iPixel < numPixels; iPixel++, pRGB++) {
      grey = (prgb->rgbtRed + prgb->rgbtGreen) >> 1;
      pRGB->rgbtRed = prgb->rgbtGreen = pRGB->rgbtBlue = (BYTE) grey;
   }

 

Have fun with your filters!

Microsoft SQL Server Enums

You know the enumeration data type, for sure. Either from programming languages (most often it's the enum keyword) or from database engines like MySQL.

What I was missing lately in my SQL Server database was the enum. To have a column that could accept only a defined set of values. As an example of MySQL enum column, is defining clothing sizes:

 

CREATE TABLE cloth (
    .
size ENUM('small', 'medium', 'large')
    .
);

 

Unfortunately SQL Server doesn't provide this functionality out of the box and I needed to create such data type in my project when trying to define if a financial operation is an income or an outcome. Of course I could bypass this by creating an IsIncome column that stores a boolean value and later on check, if the value is false, then it means that it money where withdrawn. But what if I need to add another financial operation type? Like freezing an amount of money? I could add IsFreeze column of course, but it won't be the smartest way of designing databases.

 

The ideal solution for this would be creating something like MySQL provides, allowing only three values to be set in that column. We could of course create a char column and set it's value to the desired type programmatically, but again, there would be no control over the consistency of our data. Nothing prevents us from adding a random value there that is not in the set of allowed values. There is a solution to that by creating additional table, who's primary key would be of char type. We could use int type, but the problem with integers that later on the system becomes less maintainable and the data unreadable. That's why I recommend you to use a meaningful char as a key.

 

I'll try to reproduce the above MySQL example in SQL Server.

 

First of all, we should create a table that will hold our allowed values. In this case the values are the sizes:

 

CREATE TABLE cloth_size (
   size  char(6) NOT NULL,
   CONSTRAINT PK_Size primary key (size)
)
 

Next, we add three rows to our table:

 

INSERT INTO cloth_size ('small')
INSERT INTO cloth_size ('medium')
INSERT INTO cloth_size ('large')

 

Next we create the cloth table with a column named size and we reference it to the cloth_size table:

 

CREATE TABLE cloth (
    ...
   size CHAR(6)
    ...
   FOREIGN KEY (size) REFERENCES cloth_size(size)
);
 

And now we are allowed only to insert the three values that we've inserted into cloth_size table.

Filters and pipes

Pipes and Filters is a design pattern that is responsible for moving the output of one system into another. This design pattern is useful when trying to connect two different systems with different web services schema or different formats. This often occurs during updating old software, so that the new one can use portions of the old one. Integrating such systems involve a whole series of transformations, like transforming XML-based service communications another XML format or Binary to XML based.

A solution for this problem is to implement a sequence of filter components, where filters transform the received messages and forward the to the next component. The interchanged data between filters and other components are transported through pipes.

When a system needs more than one filter, the pipes carry the data from the first filter to another until its fully transformed into a compatible format. Pipes also buffer the carried data until the target filter is ready to transform them. After applying all needed transformations, the pipe transfer the data to the targeted application which consumes it.

This pattern needs the output data to be compatible with the input of the next filer or application. In general the output of one component should be compatible with the input of the next component in row.

The picture below shows an example of using filers and pipes:

©2008 Karim Agha. All rights reserved.