Makx
Makx

Reputation: 335

DirectShow ISampleGrabber: samples are upside-down and color channels reverse

I have to use MS DirectShow to capture video frames from a camera (I just want the raw pixel data).
I was able to build the Graph/Filter network (capture device filter and ISampleGrabber) and implement the callback (ISampleGrabberCB). I receive samples of appropriate size.

However, they are always upside down (flipped vertically that is, not rotated) and the color channels are BGR order (not RGB).

I tried setting the biHeight field in the BITMAPINFOHEADER to both positive and negative values, but it doesn't have any effect. According to MSDN documentation, ISampleGrapper::SetMediaType() ignores the format block for video data anyways.

Here is what I see (recorded with a different camera, not DS), and what DirectShow ISampleGrabber gives me: The "RGB" is actually in red, green and blue respectively:

Here is what I see (recorded with a different camera, not DS)

Here is what DirectShow ISampleGrabber gives me.

Sample of the code I'm using, slightly simplified:

// Setting the media type...
AM_MEDIA_TYPE*   media_type   = 0 ;
this->ds.device_streamconfig->GetFormat(&media_type); // The IAMStreamConfig of the capture device
// Find the BMI header in the media type struct
BITMAPINFOHEADER* bmi_header;
if (media_type->formattype != FORMAT_VideoInfo) {
    bmi_header = &((VIDEOINFOHEADER*)media_type->pbFormat)->bmiHeader;
} else if (media_type->formattype != FORMAT_VideoInfo2) {
    bmi_header = &((VIDEOINFOHEADER2*)media_type->pbFormat)->bmiHeader;
} else {
    return false;
}
// Apply changes
media_type->subtype  = MEDIASUBTYPE_RGB24;
bmi_header->biWidth  = width;
bmi_header->biHeight = height;
// Set format to video device
this->ds.device_streamconfig->SetFormat(media_type);
// Set format for sample grabber
// bmi_header->biHeight = -(height); // tried this for either and both interfaces, no effect
this->ds.sample_grabber->SetMediaType(media_type);

// Connect filter pins
IPin* out_pin= getFilterPin(this->ds.device_filter, OUT,  0); // IBaseFilter interface for the capture device
IPin* in_pin = getFilterPin(this->ds.sample_grabber_filter,  IN,  0); // IBaseFilter interface for the sample grabber filter
out_pin->Connect(in_pin, media_type);

// Start capturing by callback
this->ds.sample_grabber->SetBufferSamples(false);
this->ds.sample_grabber->SetOneShot(false);
this->ds.sample_grabber->SetCallback(this, 1);
// start recording
this->ds.media_control->Run(); // IMediaControl interface

I'm checking return types for every function and don't get any errors.

I'm thankful for any hint or idea.

Things I already tried:

  1. Setting the biHeight field to a negative value for either the capture device filter or the sample grabber or for both or for neither - doesn't have any effect.

  2. Using IGraphBuilder to connect the pins - same problem.

  3. Connecting the pins before changing the media type - same problem.

  4. Checking if the media type was actually applied by the filter by querying it again - but it apparently is applied or at least stored.

  5. Interpreting the image as total byte reversed (last byte first, first byte last) - then it would be flipped horizontally.

  6. Checking if it's a problem with the video camera - when I test it with VLC (DirectShow capture) it looks normal.

Upvotes: 5

Views: 2764

Answers (2)

Anton Golovkov
Anton Golovkov

Reputation: 11

I noticed that when using the I420 color space turning disappears. In addition, most current codecs (VP8) is used as a format raw I/O I420 color space.

I wrote a simple mirroring frame function in color space I420.

void Camera::OutputCallback(unsigned char* data, int len, uint32_t timestamp, void *instance_)
{
    Camera *instance = reinterpret_cast<Camera*>(instance_);

    Transport::RTPPacket packet;

    packet.rtpHeader.ts = timestamp;

    packet.payload = data;
    packet.payloadSize = len;

    if (instance->mirror)
    {
        Video::ResolutionValues rv = Video::GetValues(instance->resolution);
        int k = 0;

        // Chroma values
        for (int i = 0; i != rv.height; ++i)
        {
            for (int j = rv.width; j != 0; --j)
            {
                int l = ((rv.width * i) + j);
                instance->buffer[k++] = data[l];
            }
        }

        // U values
        for (int i = 0; i != rv.height/2; ++i)
        {
            for (int j = (rv.width/2); j != 0; --j)
            {
                int l = (((rv.width / 2) * i) + j) + rv.height*rv.width;
                instance->buffer[k++] = data[l];
            }
        }

        // V values
        for (int i = 0; i != rv.height / 2; ++i)
        {
            for (int j = (rv.width / 2); j != 0; --j)
            {
                int l = (((rv.width / 2) * i) + j) + rv.height*rv.width + (rv.width/2)*(rv.height/2);
                if (l == len)
                {
                    instance->buffer[k++] = 0;
                }
                else
                {
                    instance->buffer[k++] = data[l];
                }
            }
        }

        packet.payload = instance->buffer;
    }

    instance->receiver->Send(packet);
}

Upvotes: 0

Anton Golovkov
Anton Golovkov

Reputation: 11

My quick hack for this:

void Camera::OutputCallback(unsigned char* data, int len, void *instance_)
{
    Camera *instance = reinterpret_cast<Camera*>(instance_);

    int j = 0;
    for (int i = len-4; i > 0; i-=4)
    {
        instance->buffer[j] = data[i];
        instance->buffer[j + 1] = data[i + 1];
        instance->buffer[j + 2] = data[i + 2];
        instance->buffer[j + 3] = data[i + 3];
        j += 4;
    }

    Transport::RTPPacket packet;

    packet.payload = instance->buffer;
    packet.payloadSize = len;

    instance->receiver->Send(packet);
}

It's correct on RGB32 color space, for other color spaces this code need to be corrected

Upvotes: 0

Related Questions