To Enumerate Input Formats

[The feature associated with this page, Windows Media Format 11 SDK, is a legacy feature. It has been superseded by Source Reader and Sink Writer. Source Reader and Sink Writer have been optimized for Windows 10 and Windows 11. Microsoft strongly recommends that new code use Source Reader and Sink Writer instead of Windows Media Format 11 SDK, when possible. Microsoft suggests that existing code that uses the legacy APIs be rewritten to use the new APIs if possible.]

Each of the Windows Media codecs accepts one or more types of input media for compression. The Windows Media Format SDK enables you to input a wider variety of formats than those supported by the codecs. The SDK does this by performing pre-processing transformations on the inputs when necessary, such as resizing video frames or resampling audio. In any case, you must ensure that the input formats for the files you write match the data you send to the writer. Each codec has a default input media format that is set in the writer when the profile is loaded. You can examine the default input format by calling IWMWriter::GetInputProps.

The video codecs support the following formats: IYUV, I420, YV12, YUY2, UYVY, YVYU, YVU9, RGB 32, RGB 24, RGB 565, RGB 555, and RGB 8. The audio codecs support PCM audio.

To enumerate the input formats supported by a codec, perform the following steps:

  1. Create a writer object and set a profile to use. For more information about setting profiles in the writer, see To Use Profiles with the Writer.
  2. Identify the input number for which you want to check the formats. For more information about identifying input numbers, see To Identify Inputs By Number.
  3. Retrieve the total number of input formats supported by the desired input by calling IWMWriter::GetInputFormatCount.
  4. Loop through all of the supported input formats, performing the following steps for each.
    • Retrieve the IWMInputMediaProps interface for the input format by calling IWMWriter::GetInputFormat.
    • Retrieve the WM_MEDIA_TYPE structure for the input format. Call IWMMediaProps::GetMediaType, passing NULL for the pType parameter to get the size of the structure. Then allocate memory to hold the structure and call GetMediaType again to get the structure. IWMInputMediaProps inherits from IWMMediaProps, so you can make the calls to GetMediaType from the instance of IWMInputMediaProps retrieved in the previous step.
    • The format described in the WM_MEDIA_TYPE structure contains all of the pertinent information about the input format. The basic format of the media is identified by WM_MEDIA_TYPE.subtype. For video streams, the pbFormat member points to a dynamically allocated WMVIDEOINFOHEADER structure which contains further details about the stream, including rectangle size. The size of the input frames is not required to match exactly a size supported by the codec. If they do not match, the SDK run-time components, in many cases, will automatically resize the input video frames to something the codec can accept.

The following example code finds the input format of the subtype passed as a parameter. For more information about using this code, see Using the Code Examples.

HRESULT FindInputFormat(IWMWriter* pWriter, 
                       DWORD dwInput,
                       GUID guidSubType,
                       IWMInputMediaProps** ppProps)
{
    DWORD   cFormats = 0;
    DWORD   cbSize   = 0;

    WM_MEDIA_TYPE*      pType  = NULL;
    IWMInputMediaProps* pProps = NULL;

    // Set the ppProps parameter to point to NULL. This will
    //  be used to check the results of the function later.
    *ppProps = NULL;

    // Find the number of formats supported by this input.
    HRESULT hr = pWriter->GetInputFormatCount(dwInput, &cFormats);
    if (FAILED(hr))
    {
        goto Exit;
    }
    // Loop through all of the supported formats.
    for (DWORD formatIndex = 0; formatIndex < cFormats; formatIndex++)
    {
        // Get the input media properties for the input format.
        hr = pWriter->GetInputFormat(dwInput, formatIndex, &pProps);
        if (FAILED(hr))
        {
            goto Exit;
        }
        // Get the size of the media type structure.
        hr = pProps->GetMediaType(NULL, &cbSize);
        if (FAILED(hr))
        {
            goto Exit;
        }
        // Allocate memory for the media type structure.
        pType = (WM_MEDIA_TYPE*) new (std::nothrow) BYTE[cbSize];
        if (pType == NULL)
        {
            hr = E_OUTOFMEMORY;
            goto Exit;
        }
        
        // Get the media type structure.
        hr = pProps->GetMediaType(pType, &cbSize);
        if (FAILED(hr))
        {
            goto Exit;
        }

        if(pType->subtype == guidSubType)
        {
            *ppProps = pProps;
            (*ppProps)->AddRef();
            goto Exit;
        }

        // Clean up for next iteration.
        delete [] pType;
        pType = NULL;
        SAFE_RELEASE(pProps);
    } // End for formatIndex.

    // If execution made it to this point, no matching format was found.
    hr = NS_E_INVALID_INPUT_FORMAT;

Exit:
    delete [] pType;
    SAFE_RELEASE(pProps);
    return hr;
}

IWMWriter Interface

Writing ASF Files