0

I've been trying to retrieve the bits per sample in the Media Foundation Framework but it always returns 0, or gives an 'requested attribute not found' HRESULT.

Here's the code I've tried, the

DWORD streamIndex = 0, flag = 0;
LONGLONG audioTimestamp = 0;
ComPtr<IMFSample> pAudioSample = nullptr;
HRESULT result = mImpl->mAudioSourceReader->ReadSample(mImpl->mAudioStreamIndex, 0, &streamIndex, &flag, &audioTimestamp, &pAudioSample);
if (result != S_OK) {
    LogError("Failed to read audio sample..");
    return false;
} 

ComPtr<IMFMediaType> pAudioSampleMediaType;
if (S_OK != mImpl->mAudioSourceReader->GetCurrentMediaType(mImpl->mAudioStreamIndex, &pAudioSampleMediaType)) {
    LogError("Failed to set resampler input...");
    return false;
}

uint32_t inBitsPerSample;
result = pAudioSampleMediaType->GetUINT32(MF_MT_AUDIO_BITS_PER_SAMPLE, &inBitsPerSample);
result = pAudioSample->GetUINT32(MF_MT_AUDIO_BITS_PER_SAMPLE, &inBitsPerSample);

inBitsPerSample = MFGetAttributeUINT32(pAudioSample.Get(), MF_MT_AUDIO_BITS_PER_SAMPLE, 0);

The program is working, but I have set the value to 32, this presumably works because I have a 32 bit audio file. Is there something else I need to do to retrieve the bits per sample from an audio sample?

Many thanks, Peter

Edit:

Details of the media are:

Video file with LPCM Audio (mp4 container)
Sample Rate 48000 Hz
Bits Per Sample 16
Channel Count 6
pma07pg
  • 516
  • 1
  • 4
  • 16

2 Answers2

0

MF_MT_AUDIO_BITS_PER_SAMPLE is applicable to media types only, not to samples.

MF_MT_AUDIO_BITS_PER_SAMPLE is not available for every media type, only for those where it is applicable. If you can't query the value then most likely it does not make much sense for this media type.

You further investigation should be along the lines of dumping the entire media type and attaching it to this question. By this time you are likely to figure out why exactly the value is missing from that specific media type.

Roman R.
  • 68,205
  • 6
  • 94
  • 158
  • Thanks for your answer. The issue is that it's PCM audio, bits per sample is very relevant. When decoding with ffmpeg for example, I get 32 or 16 as bits per sample so I can transform it properly. I need to provide the input bits per sample to the resampler otherwise it wouldn't always know how to process the sample correctly. – pma07pg Jul 15 '21 at 12:17
  • You write it's PCM audio. If you had a chance to verify this statement you should be able to edit your question and append relevant media type information. The current wording of the question does not persuade you're dealing with PCM audio. – Roman R. Jul 15 '21 at 12:18
  • Added media info as requested – pma07pg Jul 15 '21 at 13:36
  • 1
    I mean you are intersted to enumreate everything you have in `pAudioSampleMediaType` – Roman R. Jul 15 '21 at 13:38
  • I've just seen that the MF_MT_AUDIO_AVG_BYTES_PER_SECOND is assigned a value. I can just get the bits per sample from this. Strange that this is populated but MF_MT_AUDIO_BITS_PER_SAMPLE isn't. Anyway, thanks for nudging me in the right direction. – pma07pg Jul 15 '21 at 14:25
0

The question wasn't posed very well. However, it seems to be the case that for 32 bit PCM audio, the relevant attributes aren't set. For 24 or 16 then the attributes are correctly set and can be retrieved.

If I request MF_MT_AUDIO_BITS_PER_SAMPLE and get the attribute not found error, then I assumed 32 bit, otherwise I use the value provided. Could just be a quirk of the Media Foundation API, but seems to work.

pma07pg
  • 516
  • 1
  • 4
  • 16