Microsoft DirectX 8.1 (C++)

Setting the Group Media Type

All groups must define an uncompressed media type, either audio or video. The uncompressed media type is the format that viewers see or hear during playback. Typically, the final output will be in a compressed format. For more information, see Rendering a Project.

To set the uncompressed format, create an AM_MEDIA_TYPE structure and fill it with the appropriate major type, subtype, and format header. For video, set the width, height, and bit depth in the format header. For audio, set the sample rate in the format header. If you set only the major type, DES provides reasonable defaults for the other values. In practice, you should set the values explicitly to control the output. The CMediaType base class provides a convenient way to manage the AM_MEDIA_TYPE structure. If you prefer, you can work directly with the AM_MEDIA_TYPE structure.

After initializing the media type structure, call the IAMTimelineGroup::SetMediaType method to set the media type for the group.

The following example specifies 16-bit RGB video, 320 pixels wide by 240 pixels high.

AM_MEDIA_TYPE mtGroup;  
mtGroup.majortype = MEDIATYPE_Video;
mtGroup.subtype = MEDIASUBTYPE_RGB555;

// Set format headers.
mtGroup.pbFormat = (BYTE*)CoTaskMemAlloc(sizeof(VIDEOINFOHEADER));
VIDEOINFOHEADER *pVideoHeader = (VIDEOINFOHEADER*)mtGroup.pbFormat;
ZeroMemory(pVideoHeader, sizeof(VIDEOINFOHEADER));
pVideoHeader->bmiHeader.biBitCount = 16;
pVideoHeader->bmiHeader.biWidth = 320;
pVideoHeader->bmiHeader.biHeight = 240;
pVideoHeader->bmiHeader.biPlanes = 1;
pVideoHeader->bmiHeader.biSize = sizeof(BITMAPINFOHEADER);
pVideoHeader->bmiHeader.biSizeImage = DIBSIZE(pVideoHeader->bmiHeader);

// Set the format type and size.
mtGroup.formattype = FORMAT_VideoInfo;
mtGroup.cbFormat = sizeof(VIDEOINFOHEADER);

// Set the sample size.
mtGroup.bFixedSizeSamples = TRUE;
mtGroup.lSampleSize = DIBSIZE(pVideoHeader->bmiHeader);

// Now use this media type for the group.
pGroup->SetMediaType(&mtGroup);

// Clean up.
CoTaskMemFree(mtGroup.pbFormat);

You can also use the CMediaType class in the DirectShow Base Classes to manage media types. It contains some useful helper methods, and automatically releases the format block. Here is the previous example rewritten to use CMediaType:

CMediaType mtGroup;
mtGroup.SetType(&MEDIATYPE_Video);
mtGroup.SetSubtype(&MEDIASUBTYPE_RGB555);

VIDEOINFOHEADER *pVideoHeader = (VIDEOINFOHEADER*)
    mtGroup.AllocFormatBuffer(sizeof(VIDEOINFOHEADER));
ZeroMemory(pVideoHeader, sizeof(VIDEOINFOHEADER));
pVideoHeader->bmiHeader.biBitCount = 16;
pVideoHeader->bmiHeader.biWidth = 320;
pVideoHeader->bmiHeader.biHeight = 240;
pVideoHeader->bmiHeader.biPlanes = 1;
pVideoHeader->bmiHeader.biSize = sizeof(BITMAPINFOHEADER);
pVideoHeader->bmiHeader.biSizeImage = DIBSIZE(pVideoHeader->bmiHeader);

mtGroup.SetFormatType(&FORMAT_VideoInfo);
mtGroup.SetSampleSize(DIBSIZE(pVideoHeader->bmiHeader));

// Note implicit cast to AM_MEDIA_TYPE*.
pGroup->SetMediaType(&mtGroup);