Microsoft DirectX 9.0

Using the Demux with Elementary Streams

When the MPEG-2 demux delivers PES payloads, it sends the ES byte stream in batches of media samples. The default sample size is 8K. The demux starts a new media sample on each PES boundary, but may break a single PES payload into several samples. For example, if a PES payload is 20K, it will be delivered in two 8K samples followed by one 4K sample. The demux does not examine the contents of the byte stream. It is up to the decoder to parse the sequence headers and look for format changes.

When the demux filter's output pin connects to the decoder, it offers the media type that was specified when the pin was created. Because the demux does not examine the ES byte stream, it does not validate the media type. In theory, an MPEG-2 decoder should be able to connect with just the major type and subtype filled in, to indicate the type of data. The decoder should then examine the sequence headers that arrive in the media samples. However, in practice, many decoders will not connect unless the media type includes a complete format block.

For example, suppose that PID 0x31 contains MPEG-2 main profile video. At a minimum, you would need to do the following steps.

First, create a media type for MPEG-2 video. Leaving aside the format block for now:

ZeroMemory(&mt, sizeof(AM_MEDIA_TYPE);
mt.majortype = MEDIATYPE_Video ;
// Possibly create a format block (not shown here).

Next, create an output pin on the demux:

// Query the demux filter for IMpeg2Demultiplexer.
IMpeg2Demultiplexer *pDemux;
hr = pFilter->QueryInterface(IID_IMpeg2Demultiplexer, (void**)&pDemux);
if (SUCCEEDED(hr))
    // Create a new output pin.
    IPin *pPin0;
    hr = pDemux->CreateOutputPin(&mt, L"Video Pin", &pPin0);
    if (SUCCEEDED(hr))

Then, query the new pin for the IMPEG2PIDMap interface and call MapPID. Specify PID number 0x30, and MEDIA_ELEMENTARY_STREAM for PES payloads.

// Query the pin for IMPEG2PIDMap. This implicitly configures the
// demux to carry a transport stream. 
IMPEG2PIDMap *pPidMap;
hr = pPin0->QueryInterface(IID_IMPEG2PIDMap, (void**)&pPidMap);
if (SUCCEEDED(hr))
    // Assign PID 0x31 to pin 0. Set the type to "PES payload."
    ULONG Pid = 0x031;
    hr = pPidMap->MapPID(1, &Pid, MEDIA_ELEMENTARY_STREAM);

Finally, release all interfaces when you are done:


Here is a more complete example of setting the media type, including the format block. This example still assumes an MPEG-2 main profile video. The details will vary depending on stream contents:

// Set up a byte array to hold the first sequence header. 
// This may or may not be required, depending on the decoder.
BYTE SeqHdr[] = { ... };  // Contains the sequence header (not shown).

ZeroMemory(&mt, sizeof(AM_MEDIA_TYPE));
mt.majortype = MEDIATYPE_Video ;
mt.formattype = FORMAT_MPEG2Video;

// Allocate the format block, including space for the sequence header. 
mt.cbFormat = sizeof(MPEG2VIDEOINFO) + sizeof(SeqHdr);
mt.pbFormat = (BYTE*)CoTaskMemAlloc(mt.cbFormat);
if (mt.pbFormat == NULL)
    // Out of memory; return an error code.
ZeroMemory(mt.pbFormat, mt.cbFormat);

// Cast the buffer pointer to an MPEG2VIDEOINFO struct.

RECT rcSrc = {0, 480, 0, 720};        // Source rectangle.
pMVIH->hdr.rcSource = rcSrc;
pMVIH->hdr.dwBitRate = 4000000;       // Bit rate.
pMVIH->hdr.AvgTimePerFrame = 333667;  // 29.97 fps.
pMVIH->hdr.dwPictAspectRatioX = 4;    // 4:3 aspect ratio.
pMVIH->hdr.dwPictAspectRatioY = 3;

// BITMAPINFOHEADER information.
pMVIH->hdr.bmiHeader.biSize = 40;
pMVIH->hdr.bmiHeader.biWidth = 720;
pMVIH->hdr.bmiHeader.biHeight = 480;

pMVIH->dwLevel = AM_MPEG2Profile_Main;  // MPEG-2 profile. 
pMVIH->dwProfile = AM_MPEG2Level_Main;  // MPEG-2 level.

// Copy the sequence header into the format block.
pMVIH->cbSequenceHeader = sizeof(SeqHdr); // Size of sequence header.
memcpy(pMVIH->dwSequenceHeader, SeqHdr, sizeof(SeqHdr));

See Also