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.

License plate injection

Police in Wroclaw installed on-road monitoring cameras..

 

91779

 

. that recognize your car's license plate and warn you about your speed:

 

91781

 

("Zwolnij" in Polish means "slow down")

but some drivers decided to fuck the system:

 

dropdatabase

 

:)

Adding basic logic to WPF data binding

Ever tried to bind a control to a data source in WPF? Usually the procedure looks this way: You use or create a control (let's say a ListView), create a data source and point the control to the data source.

But there are some circumstances when you need to add certain behavior to your control based on the data that it's bound to, like marking it red when the value is larger than certain value. .NET framework provides us with the DataTrigger trigerring mechanism, but the limitation of this mechanism is that it could only be triggered when the data is equal to a certain value. It cannot accept other comparisons. You could always do this in the code-behind of your UI, but this won't be a good practice in general (with some exceptions).

 

Now, what I want to create is a ListView, that will contain Label Controls in it's ListViewItem and when the value of the data item is larger than 100 I want to set the foreground to Red brush.

 

Let's start with a basic ListView:

        <ListView Name="listView">
            <ListView.View>
                <GridView>
                    <GridViewColumn Header="Value1" />
                    <GridViewColumn Header="Value2" />
                </GridView>
            </ListView.View>
        </ListView>

 

Next, let's say that we want to bind to it a List of our custom classe

 

class OurCustomClass
{
    public int Value1 { get; set; }
    public string Value2 { get; set; }
}
 
List<OurCustomClass> data = new List<OurCustomClass>();
Random rand = new Random();
for(int i = 0; i < 100; i++)
    data.Add(new OurCustomClass {Value1 = rand.Next(1, 1000), Value2 = rand.Next(3000, 30000).ToString()});

:

listView.ItemsSource = data;

 

What .NET Framework enables us to do is adding a DataTrigger do a DataTemplate:

 

<ListView>
    <ListView.Resources>
        <DataTemplate x:Key="OurTemplate">
            <Label Name="val1Label" />
            <DataTemplate.Triggers>
                <DataTrigger Binding="{Binding Path=Value1}"  Value="SomeValue">
                    <Setter ... />
                </DataTrigger>
            </DataTemplate.Triggers>
        </DataTemplate>
    </ListView.Resources>
    <ListView.View>
        <GridView>
            <GridViewColumn Header="Value1" />
            <GridViewColumn Header="Value2" />
        </GridView>
    </ListView.View>
</ListView>

 

Here as you see the DataTrigger checks if the bound data is equal to the value defined in the Value property, if yes, the trigger is fired and the setters alter the content and attributes of the item. But when we need to check, let's say, if the value is grater than 100 we have to use a small trick.

The binding mechanism provides something called Converter, it is used to convert the bound data to another type and the syntax for it is  Binding="{Binding Path=Value1, Converter=OurConverter}".

When there is a converter assigned to the bound data, they are first passed to it, converted and the comparison is done on the converted data and here we could trick the mechanism with a small hack, that works in a way, that it will convert the int to string. It will convert it to a string of "Hi" if it is higher than 100, or convert it to "Low" if lower. We could write it in a yet more elegant way in this particular example as we are having only two possible options (either it is bigger than 100 or not) so we could return a Boolean value.

In other situations, where there are several possible results of comparison and we should react to each of the results in a different way, a good idea is to create an enum.

 

Ahh, and I forgot to mention that the Converter class should implement the IValueConverter interface and should be added to the resources .

 

The code below illustrates how to set the color of the label in the items' template basing on the data value (the condition is that the value should be > 100):

 

First, the converter:

 

class OurConverter : IValueConverter
    {
        private int originalValue;
        public object Convert(object value, Type targetType, object parameter, CultureInfo culture)
        {
            originalValue = (int)value;
            if (originalValue > 100)
                return true;
            return false;
        }

        public object ConvertBack(object value, Type targetType, object parameter, CultureInfo culture)
        {
            return originalValue;
        }
    }

 

Next we add it to the resources:

 

<UserControl.Resources>
        <MyNamespace:OurConverter x:Key="outConverter" />
</UserControl.Resources>

 

Finally we add the converter to the binding and the final resulting XAML will be:

 

        <ListView>
            <ListView.Resources>
                <DataTemplate x:Key="OurTemplate">
                    <Label Name="val1Label" />
                    <DataTemplate.Triggers>
                        <DataTrigger Binding="{Binding Path=Value1, Converter={StaticResource ourConverter}}"  Value="True">
                            <Setter TargetName="val1Label" Property="Foreground" Value="Red" />
                        </DataTrigger>
                    </DataTemplate.Triggers>
                </DataTemplate>
            </ListView.Resources>
            <ListView.View>
                <GridView>
                    <GridViewColumn Header="Value1" CellTemplate="{StaticResource OurTemplate}" />
                    <GridViewColumn Header="Value2" />
                </GridView>
            </ListView.View>
        </ListView>

 

Enjoy!

Passing LINQ2SQL anonymous results among application layers in ASP.NET MVC

Ever had a LINQ-to-SQL query that returns an anonymous type and you needed to pass the result of your query to the view? probably yes :)

Today I was faced with a problem that required me to query several table, extract the results from them and finally create a list of objects with nested values and all this should be passed to the view so that there I could iterate it and show a sensible results.

Let's first examine the tables I needed to query, here you have the schema containing the significant fields I'll need:

 

Table User Table TutorSubjects Table Subject Table Comment
Id: Guid TutorId: Guid Id: Guid TutorId: Guid
FirstName: string SubjectId: Guid Name: string Rating: int
LastName: string      
AboutMe: string      

These are the significant fields I required to get a list of Tutors along with their ratings and their subjects. The SQL query I wrote for it was:

   1:  SELECT T.Id,
   2:         T.FirstName, 
   3:         T.LastName, 
   4:         T2.Rating, 
   5:         TutorSubjects.Id, 
   6:        (SELECT [Subject].Name
   7:         FROM [Subject]
   8:         WHERE [Subject].Id = TutorSubjects.SubjectId) SubjectName,
   9:         T.AboutMe
  10:  FROM [User] T
  11:       LEFT OUTER JOIN (SELECT Tutor.Id, SUM(Comment.Rating) Rating
  12:                        FROM [User] Tutor, Comment
  13:                        WHERE Comment.TutorId = Tutor.Id AND Tutor.Type = 2
  14:                        GROUP BY Tutor.Id) T2
  15:             ON T.Id = T2.Id
  16:       LEFT OUTER JOIN TutorSubjects
  17:             ON TutorSubjects.TutorId = T.Id
  18:  WHERE T.Type = 2
  19:  ORDER BY T2.Rating DESC

Now, the application is based on ASP.NET MVC, and it uses LINQ-to-SQL orm. First of all, we should somehow execute this query using LINQ, we could do it in several ways. Either write a ninja-query joining multiple entities or as I did, create a view in the database that executes this query and import the view to linq2sql, so that we could use it as an entity. Our new view (and the new object ) is called TutorRatingWithSubjects.

 

In the code, when I query it, I want to have a structure that looks somehow like this:

aggregation1

So that when I'll pass the results to the view I could display them without any extra effort. I could accomplish it with the C# feature - anonymous types, but the problem in here is with passing it to the view.

 

   1:  var tutors = from tutor in db.TutorRatingWithSubjects
   2:               select new
   3:                          {
   4:                              tutor.Id,
   5:                              tutor.FirstName,
   6:                              tutor.LastName,
   7:                              tutor.Rating,
   8:                              tutor.AboutMe,
   9:                              Subjects = (from ts in db.TutorRatingWithSubjects
  10:                                          where ts.Id == tutor.Id
  11:                                          select new
  12:                                                     {
  13:                                                         ts.SubjectId,
  14:                                                         ts.SubjectName
  15:                                                     }
  16:                                         )
  17:                          };

When I'm passing it, the whole anonymous type get's up-casted to an object when it's stored in the ViewData. Here, the problem is that you are unable (directly) to reproduce the original anonymous type from the ViewData.

There are several ways to read data from a boxed anonymous type:

Using reflection

In the view you cast it to IQueryable and iterate through it:

    <ul>
    <%
        IQueryable tutors = (IQueryable) ViewData["Tutors"];
        foreach(object v in tutors) {
      %>
       <li> <%= v.GetType().InvokeMember("FirstName", System.Reflection.BindingFlags.GetProperty, null, v, null) %></li>
  <% } %>
    </ul>
Now, while this method works, I highly discourage it's usage, as using reflection is just cheating. It has weak performance, the code is less readable, etc...
There is a more elegant way to read the results of the linq query in our view.
Using strongly-typed list
We create a nested class that has the same properties as our anonymous type:
internal class TutorsWithSubjectsResult
{
    public Guid TutorId { get; set; }
    public String FirstName { get; set; }
    public String LastName { get; set; }
    public int? Rating { get; set; }
    public string AboutMe { get; set; }

    internal class Subject
    {
        public Guid? SubjectId { get; set; }
        public String SubjectName { get; set; }
    }

    public List<Subject> Subjects { get; set; }
}

 

Now we need to select the data with our linq into the TutorsWithSubjectsResult:

List<TutorsWithSubjectsResult> tutors = (from tutor in db.TutorRatingWithSubjects
                                         select new TutorsWithSubjectsResult
                                                    {
                                                        TutorId = tutor.Id,
                                                        FirstName = tutor.FirstName,
                                                        LastName = tutor.LastName,
                                                        Rating = tutor.Rating,
                                                        AboutMe = tutor.AboutMe,
                                                        Subjects = (from ts in db.TutorRatingWithSubjects
                                                                    where ts.Id == tutor.Id
                                                                    select
                                                                        new TutorsWithSubjectsResult.Subject
                                                                            {
                                                                                SubjectId = ts.SubjectId,
                                                                                SubjectName = ts.SubjectName
                                                                            }
                                                                   ).ToList()
                                                    }).ToList();
Voila! we have a strongly-typed list, that could be easily unboxed in the view, or if you create a strongly typed-view you would have a type that could be the type of the ViewData.
©2008 Karim Agha. All rights reserved.