c++ - libvorbis 音频从 C++ 中的内存解码

标签 c++ oggvorbis vorbis

在 C++ 中给定一个编码缓冲区,使用 oggvorbis 结构来解码已经在内存中的数据的步骤是什么?

OggVorbis_File 无法使用,因为 Assets 在压缩文件中。

我正在尝试研究必要的结构和方法,但我对音频编码和解码还很陌生。

也感谢任何可以帮助我进一步阅读的资源!

我应该澄清一下,我打算使用解码后的数据流式传输到 OpenAL。

谢谢。

最佳答案

回答我自己的问题。

这可以通过向 vorbis 提供自定义回调来完成。

struct ogg_file
{
    char* curPtr;
    char* filePtr;
    size_t fileSize;
};

size_t AR_readOgg(void* dst, size_t size1, size_t size2, void* fh)
{
    ogg_file* of = reinterpret_cast<ogg_file*>(fh);
    size_t len = size1 * size2;
    if ( of->curPtr + len > of->filePtr + of->fileSize )
    {
        len = of->filePtr + of->fileSize - of->curPtr;
    }
    memcpy( dst, of->curPtr, len );
    of->curPtr += len;
    return len;
}

int AR_seekOgg( void *fh, ogg_int64_t to, int type ) {
    ogg_file* of = reinterpret_cast<ogg_file*>(fh);

    switch( type ) {
        case SEEK_CUR:
            of->curPtr += to;
            break;
        case SEEK_END:
            of->curPtr = of->filePtr + of->fileSize - to;
            break;
        case SEEK_SET:
            of->curPtr = of->filePtr + to;
            break;
        default:
            return -1;
    }
    if ( of->curPtr < of->filePtr ) {
        of->curPtr = of->filePtr;
        return -1;
    }
    if ( of->curPtr > of->filePtr + of->fileSize ) {
        of->curPtr = of->filePtr + of->fileSize;
        return -1;
    }
    return 0;
}

int AR_closeOgg(void* fh)
{
    return 0;
}

long AR_tellOgg( void *fh )
{
    ogg_file* of = reinterpret_cast<ogg_file*>(fh);
    return (of->curPtr - of->filePtr);
}

用法

ov_callbacks callbacks;
ogg_file t;
t.curPtr = t.filePtr = compressedData;
t.fileSize = compressedDataSize;

OggVorbis_File* ov = new OggVorbis_File;
mOggFile = ov;
memset( ov, 0, sizeof( OggVorbis_File ) );

callbacks.read_func = AR_readOgg;
callbacks.seek_func = AR_seekOgg;
callbacks.close_func = AR_closeOgg;
callbacks.tell_func = AR_tellOgg;

int ret = ov_open_callbacks((void *)&t, ov, NULL, -1, callbacks);

vorbis_info* vi = ov_info(ov, -1);
assert(vi);

/* compressed data is available to use, to uncompress look into ov_read */

特别感谢 Doom3 GPL 源提供的大部分帮助,它可以 查看:here

关于c++ - libvorbis 音频从 C++ 中的内存解码,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/13437422/

相关文章:

c++ - 选择最大的 “n” 值

android - 在Android中编码为Ogg vorbis时是否有延迟时间?

c# - 流式传输 Ogg 文件

ios - 如何在 iPhone/iOS 中播放 xiph.org 音频编解码器?

c++ - 从libuv句柄继承

c++ - Visual C++ Windows 中的基本 NTP 客户端

c# - 调用带参数的方法时 native 回调错误

.net - 如何在 .Net/Mono 中解码 wav、mp3 和/或 ogg?

macos - MAC OSX Intel LLVM汇编程序错误(导致Vorbis OGG加载程序崩溃)

ffmpeg - ffmpeg 捕获中低质量源引起的 ALSA 缓冲区 xrun