Types de médias audio non compressés
Pour créer un type audio non compressé complet, définissez au moins les attributs suivants sur le pointeur d’interface IMFMediaType .
Attribut | Description |
---|---|
MF_MT_MAJOR_TYPE | Type principal. Définissez sur MFMediaType_Audio. |
MF_MT_SUBTYPE | Sous-type. Consultez GUID de sous-type audio. |
MF_MT_AUDIO_NUM_CHANNELS | Nombre de canaux audio. |
MF_MT_AUDIO_SAMPLES_PER_SECOND | Nombre d’échantillons audio par seconde. |
MF_MT_AUDIO_BLOCK_ALIGNMENT | Alignement des blocs. |
MF_MT_AUDIO_AVG_BYTES_PER_SECOND | Nombre moyen d’octets par seconde. |
MF_MT_AUDIO_BITS_PER_SAMPLE | Nombre de bits par exemple audio. |
MF_MT_ALL_SAMPLES_INDEPENDENT | Spécifie si chaque exemple audio est indépendant. Définissez sur TRUE pour les formats MFAudioFormat_PCM et MFAudioFormat_Float. |
En outre, les attributs suivants sont requis pour certains formats audio.
Attribut | Description |
---|---|
MF_MT_AUDIO_VALID_BITS_PER_SAMPLE | Nombre de bits de données audio valides dans chaque exemple audio. Définissez cet attribut si les exemples audio ont un remplissage, c’est-à-dire si le nombre de bits valides dans chaque exemple audio est inférieur à la taille de l’exemple. |
MF_MT_AUDIO_CHANNEL_MASK | Affectation de canaux audio aux positions d’orateur. Définissez cet attribut pour les flux audio multicanal, tels que 5.1. Cet attribut n’est pas requis pour l’audio mono ou stéréo. |
Exemple de code
Le code suivant montre comment créer un type de média pour l’audio PCM non compressé.
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;
}
L’exemple suivant prend un format audio encodé comme entrée et crée un type audio PCM correspondant. Ce type peut être défini sur un encodeur ou un décodeur, par exemple.
//-------------------------------------------------------------------
// 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;
}
Rubriques connexes
Commentaires
https://aka.ms/ContentUserFeedback.
Bientôt disponible : Tout au long de 2024, nous allons supprimer progressivement GitHub Issues comme mécanisme de commentaires pour le contenu et le remplacer par un nouveau système de commentaires. Pour plus d’informations, consultezEnvoyer et afficher des commentaires pour