c++ - IMFSourceReader M4A 音频精确帧搜索

标签 c++ audio ms-media-foundation

我正在使用 IMFSourceReader 从磁盘连续缓冲 1 秒的音频文件部分。我无法准确地寻找 M4A 音频数据(AAC 编码),这会导致音频流不连续。

我知道 IMFSourceReader.Read() 返回的数据通常相对于 IMFSourceReader.SetCurrentPosition() 中设置的位置向过去偏移几百帧。然而,即使考虑到这个偏移量,我也无法创建连续的无故障流(参见 readCall == 0 条件)。

我能够准确地寻找部分 WAV 文件(未压缩),因此我的偏移量计算看起来是正确的。

我的问题是 Media Foundation 库是否能够准确地查找/读取 AAC 编码的 M4A 文件(或与此相关的任何压缩音频)的部分?

这是代码。 inStartFrame 是我正在尝试阅读的示例框架。输出格式配置为 32 位 float 据(见最终函数)。为了稍微减少一点,我删除了一些错误检查和清理,例如文件结尾。

bool WindowsM4AReader::read(float** outBuffer, int inNumChannels, int64_t inStartFrame, int64_t inNumFramesToRead)
{
    int64_t hnsToRequest = SampleFrameToHNS(inStartFrame);
    int64_t frameRequested = HNSToSampleFrame(hnsToRequest);

    PROPVARIANT positionProp;
    positionProp.vt = VT_I8;
    positionProp.hVal.QuadPart = hnsToRequest;
    HRESULT hr = mReader->SetCurrentPosition(GUID_NULL, positionProp);
    mReader->Flush(0);

    IMFSample* pSample = nullptr;
    int bytesPerFrame = sizeof(float) * mNumChannels;
    int64_t totalFramesWritten = 0;
    int64_t remainingFrames = inNumFramesToRead;

    int readCall = 0;
    bool quit = false;

    while (!quit) {
        DWORD streamIndex = 0;
        DWORD flags = 0;
        LONGLONG llTimeStamp = 0;

        hr = mReader->ReadSample(
            MF_SOURCE_READER_FIRST_AUDIO_STREAM,    // Stream index.
            0,                                      // Flags.
            &streamIndex,                           // Receives the actual stream index. 
            &flags,                                 // Receives status flags.
            &llTimeStamp,                           // Receives the time stamp.
            &pSample                                // Receives the sample or NULL.
        );

        int64_t frameOffset = 0;

        if (readCall == 0) {
            int64_t hnsOffset = hnsToRequest - llTimeStamp;
            frameOffset = HNSToSampleFrame(hnsOffset);
        }

        ++readCall;

        if (pSample) {
            IMFMediaBuffer* decodedBuffer = nullptr;
            pSample->ConvertToContiguousBuffer(&decodedBuffer);

            BYTE* rawBuffer = nullptr;
            DWORD maxLength = 0;
            DWORD bufferLengthInBytes = 0;
            decodedBuffer->Lock(&rawBuffer, &maxLength, &bufferLengthInBytes);

            int64_t availableFrames = bufferLengthInBytes / bytesPerFrame;
            availableFrames -= frameOffset;
            int64_t framesToCopy = min(availableFrames, remainingFrames);

            // copy to outputBuffer
            float* floatBuffer = (float*)rawBuffer;
            float* offsetBuffer = &floatBuffer[frameOffset * mNumChannels];

            for (int channel = 0; channel < mNumChannels; ++channel) {
                for (int64_t frame = 0; frame < framesToCopy; ++frame) {
                    float sampleValue = offsetBuffer[frame * mNumChannels + channel];
                    outBuffer[channel][totalFramesWritten + frame] = sampleValue;
                }
            }

            decodedBuffer->Unlock();

            totalFramesWritten += framesToCopy;
            remainingFrames -= framesToCopy;

            if (totalFramesWritten >= inNumFramesToRead)
                quit = true;
        }
    }
}

LONGLONG WindowsM4AReader::SampleFrameToHNS(int64_t inFrame)
{
    return inFrame * (10000000.0 / mSampleRate);
}

int64_t WindowsM4AReader::HNSToSampleFrame(LONGLONG inHNS)
{
    return inHNS / 10000000.0 * mSampleRate;
}

bool WindowsM4AReader::ConfigureAsFloatDecoder()
{
    IMFMediaType* outputType = nullptr;

    HRESULT hr = MFCreateMediaType(&outputType);

    UINT32 bitsPerSample = sizeof(float) * 8;
    UINT32 blockAlign = mNumChannels * (bitsPerSample / 8);
    UINT32 bytesPerSecond = blockAlign * (UINT32)mSampleRate;

    hr = outputType->SetGUID(MF_MT_MAJOR_TYPE, MFMediaType_Audio);
    hr = outputType->SetGUID(MF_MT_SUBTYPE, MFAudioFormat_Float);
    hr = outputType->SetUINT32(MF_MT_AUDIO_PREFER_WAVEFORMATEX, TRUE);
    hr = outputType->SetUINT32(MF_MT_AUDIO_NUM_CHANNELS, (UINT32)mNumChannels);
    hr = outputType->SetUINT32(MF_MT_AUDIO_SAMPLES_PER_SECOND, (UINT32)mSampleRate);
    hr = outputType->SetUINT32(MF_MT_AUDIO_BLOCK_ALIGNMENT, blockAlign);
    hr = outputType->SetUINT32(MF_MT_AUDIO_AVG_BYTES_PER_SECOND, bytesPerSecond);
    hr = outputType->SetUINT32(MF_MT_AUDIO_BITS_PER_SAMPLE, bitsPerSample);
    hr = outputType->SetUINT32(MF_MT_ALL_SAMPLES_INDEPENDENT, TRUE);

    DWORD streamIndex = 0;
    hr = mReader->SetCurrentMediaType(streamIndex, NULL, outputType);
    return true;
}

最佳答案

如果您使用的是 Microsoft (AAC Decoder) 提供的 AAC 解码器和 MPEG-4 文件源,是的,我确认,您无法使用与波形文件相同的精度来寻找音频帧。

我将不得不进行更多测试,但我认为可以针对您的情况找到解决方法。

编辑

我已经制作了一个程序来检查 SourceReader 的查找位置:

github mofo7777

在 Stackoverflow 下 > AudioSourceReaderSeek

Wav 格式完美,mp3 好,m4a 不太好。 但是 m4a 文件是用 VLC 编码的。我使用 Mediafoundation 编码器编码了一个 m4a 文件。使用此文件(如 mp3)搜索时效果更好。

所以我会说编码器对于搜索很重要。

用不同的编码器测试不同的音频格式会很有趣。

此外,还有 IMFSeekInfo interface

我无法测试这个界面,因为我在 Windows 7 下,而且它是针对 Win8 的。有人测试会很有趣。

关于c++ - IMFSourceReader M4A 音频精确帧搜索,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/49739170/

相关文章:

C++ 抛出异常

C++ 优化这个算法

flash - 使用 ActionScript 3 降低 MP3 音高

java - 我在 android 上使用 java 时遇到异常 (java.lang.NoClassDefFoundError),为什么?

audio - Windows Media Foundation中的EVR的自定义演示者

c++ - 调用静态方法

c++ - 折叠复制和 move 语义的重载

Java:淡出音乐

windows-phone-8.1 - Windows Phone 8.1 Media Foundation H264 最大分辨率

directshow - IMediaSample(DirectShow) 到 IDirect3DSurface9/IMFSample(MediaFoundation)