다음을 통해 공유


압축되지 않은 오디오 미디어 유형

압축되지 않은 전체 오디오 형식을 만들려면 IMFMediaType 인터페이스 포인터에서 적어도 다음 특성을 설정합니다.

attribute Description
MF_MT_MAJOR_TYPE 주 형식입니다. MFMediaType_Audio 설정합니다.
MF_MT_SUBTYPE 하위. 오디오 하위 형식 GUID를 참조하세요.
MF_MT_AUDIO_NUM_CHANNELS 오디오 채널 수입니다.
MF_MT_AUDIO_SAMPLES_PER_SECOND 초당 오디오 샘플 수입니다.
MF_MT_AUDIO_BLOCK_ALIGNMENT 블록 맞춤.
MF_MT_AUDIO_AVG_BYTES_PER_SECOND 초당 평균 바이트 수입니다.
MF_MT_AUDIO_BITS_PER_SAMPLE 오디오 샘플당 비트 수입니다.
MF_MT_ALL_SAMPLES_INDEPENDENT 각 오디오 샘플이 독립적인지 여부를 지정합니다. MFAudioFormat_PCM 및 MFAudioFormat_Float 형식의 경우 TRUE 로 설정합니다.

 

또한 일부 오디오 형식에는 다음 특성이 필요합니다.

attribute Description
MF_MT_AUDIO_VALID_BITS_PER_SAMPLE 각 오디오 샘플의 유효한 오디오 데이터 비트 수입니다. 오디오 샘플에 안쪽 여백이 있는 경우, 즉 각 오디오 샘플의 유효한 비트 수가 샘플 크기보다 작은 경우 이 특성을 설정합니다.
MF_MT_AUDIO_CHANNEL_MASK 스피커 위치에 오디오 채널을 할당합니다. 5.1과 같은 다중 채널 오디오 스트림에 대해 이 특성을 설정합니다. 모노 또는 스테레오 오디오에는 이 특성이 필요하지 않습니다.

 

코드 예

다음 코드에서는 압축되지 않은 PCM 오디오에 대한 미디어 형식을 만드는 방법을 보여 있습니다.

HRESULT CreatePCMAudioType(
    UINT32 sampleRate,        // Samples per second
    UINT32 bitsPerSample,     // Bits per sample
    UINT32 cChannels,         // Number of channels
    IMFMediaType **ppType     // Receives a pointer to the media type.
    )
{
    HRESULT hr = S_OK;

    IMFMediaType *pType = NULL;

    // Calculate derived values.
    UINT32 blockAlign = cChannels * (bitsPerSample / 8);
    UINT32 bytesPerSecond = blockAlign * sampleRate;

    // Create the empty media type.
    hr = MFCreateMediaType(&pType);
    if (FAILED(hr))
    {
        goto done;
    }

    // Set attributes on the type.
    hr = pType->SetGUID(MF_MT_MAJOR_TYPE, MFMediaType_Audio);
    if (FAILED(hr))
    {
        goto done;
    }

    hr = pType->SetGUID(MF_MT_SUBTYPE, MFAudioFormat_PCM);
    if (FAILED(hr))
    {
        goto done;
    }

    hr = pType->SetUINT32(MF_MT_AUDIO_NUM_CHANNELS, cChannels);
    if (FAILED(hr))
    {
        goto done;
    }

    hr = pType->SetUINT32(MF_MT_AUDIO_SAMPLES_PER_SECOND, sampleRate);
    if (FAILED(hr))
    {
        goto done;
    }

    hr = pType->SetUINT32(MF_MT_AUDIO_BLOCK_ALIGNMENT, blockAlign);
    if (FAILED(hr))
    {
        goto done;
    }

    hr = pType->SetUINT32(MF_MT_AUDIO_AVG_BYTES_PER_SECOND, bytesPerSecond);
    if (FAILED(hr))
    {
        goto done;
    }

    hr = pType->SetUINT32(MF_MT_AUDIO_BITS_PER_SAMPLE, bitsPerSample);
    if (FAILED(hr))
    {
        goto done;
    }

    hr = pType->SetUINT32(MF_MT_ALL_SAMPLES_INDEPENDENT, TRUE);
    if (FAILED(hr))
    {
        goto done;
    }

    // Return the type to the caller.
    *ppType = pType;
    (*ppType)->AddRef();

done:
    SafeRelease(&pType);
    return hr;
}

다음 예제에서는 인코딩된 오디오 형식을 입력으로 사용하고 일치하는 PCM 오디오 형식을 만듭니다. 예를 들어 이 형식은 인코더 또는 디코더에 설정하는 데 적합합니다.

//-------------------------------------------------------------------
// ConvertAudioTypeToPCM
//
// Given an audio media type (which might describe a compressed audio
// format), returns a media type that describes the equivalent
// uncompressed PCM format.
//-------------------------------------------------------------------

HRESULT ConvertAudioTypeToPCM(
    IMFMediaType *pType,        // Pointer to an encoded audio type.
    IMFMediaType **ppType       // Receives a matching PCM audio type.
    )
{
    HRESULT hr = S_OK;

    GUID majortype = { 0 };
    GUID subtype = { 0 };

    UINT32 cChannels = 0;
    UINT32 samplesPerSec = 0;
    UINT32 bitsPerSample = 0;

    hr = pType->GetMajorType(&majortype);
    if (FAILED(hr)) 
    { 
        return hr;
    }

    if (majortype != MFMediaType_Audio)
    {
        return MF_E_INVALIDMEDIATYPE;
    }

    // Get the audio subtype.
    hr = pType->GetGUID(MF_MT_SUBTYPE, &subtype);
    if (FAILED(hr)) 
    { 
        return hr;
    }

    if (subtype == MFAudioFormat_PCM)
    {
        // This is already a PCM audio type. Return the same pointer.

        *ppType = pType;
        (*ppType)->AddRef();

        return S_OK;
    }

    // Get the sample rate and other information from the audio format.

    cChannels = MFGetAttributeUINT32(pType, MF_MT_AUDIO_NUM_CHANNELS, 0);
    samplesPerSec = MFGetAttributeUINT32(pType, MF_MT_AUDIO_SAMPLES_PER_SECOND, 0);
    bitsPerSample = MFGetAttributeUINT32(pType, MF_MT_AUDIO_BITS_PER_SAMPLE, 16);

    // Note: Some encoded audio formats do not contain a value for bits/sample.
    // In that case, use a default value of 16. Most codecs will accept this value.

    if (cChannels == 0 || samplesPerSec == 0)
    {
        return MF_E_INVALIDTYPE;
    }

    // Create the corresponding PCM audio type.
    hr = CreatePCMAudioType(samplesPerSec, bitsPerSample, cChannels, ppType);

    return hr;
}

오디오 미디어 유형