解码 Ogg/Opus 文件

3
我有一段非常简短的C++代码,试图打开一个经过ogg/opus编码的文件,并使用opus API 来解码它,使用函数opus_decode()。问题是,几乎一半的opus_decode()调用返回负(错误)代码 -4和-2(无效包和缓冲区太短)我无法解决。输出如下:
N 解码: 960 N 解码: -4 N 解码: -4 N 解码: 960 N 解码: -4 N 解码: 1920 N 解码: 960 N 解码: -4 N 解码: -4
依此类推。
#include <string.h>
#include <opus/opus.h>
#include <stdio.h>
#include <stdlib.h>
#include <cstdio>
#include <iostream>
#include <fstream>

#define LEN 1024
#define FREQ 48000
#define CHANNELS 1
#define FRAMESIZE 1920

int main(int argc, char *argv[]) {

    int size = opus_decoder_get_size(CHANNELS);

    OpusDecoder *decoders = (OpusDecoder*)malloc(size);
    int error = opus_decoder_init(decoders, FREQ, CHANNELS);

    std::ifstream inputfile;
    inputfile.open("/home/vir/Descargas/detodos.opus"); //48000Hz, Mono

    char input[LEN];

    opus_int16 *data = (opus_int16*)calloc(CHANNELS*FRAMESIZE,sizeof(opus_int16));


    if(inputfile.is_open())
        while (!inputfile.eof()) {

            inputfile >> input;         

            std::cerr << "N decoded: " << opus_decode(decoders, (const unsigned char*)&input[0], LEN, data, FRAMESIZE, 0)  << "\n";

        }


    return error;
}

1
这个应该同步解码还是异步解码? - Justin
1个回答

8
似乎您正在使用 Opus-Tools 而不是 OpusFile。显然,您已经将 libopus.a 库链接起来,但您还需要下载和构建 OpusFile 0.7,并将您的程序链接到从 OpusFile 构建的 libopusfile.a。从 OpusFile 0.7 中包含 opusfile.h 到您的程序中。最后,您需要从xiph.org/downloads 下载 libogg 1.3.2 并链接到该库。 这个链接 是解释如何打开和关闭 ogg opus 流进行解码的文档。
确保您有一个 ogg opus 文件,并使用以下方式打开流...
OggOpusFile *file = op_open_file(inputfile, error)(inputfile is char* inputfile and error is an int pointer)

使用 op_free(file) 关闭流。这是实际解码 ogg opus 流的函数文档。在调用 op_free 解码音频数据之前,请执行以下操作...

op_read(file,buffer,bufferSize,null), buffer is opus_int16 pcm[120*48*2]

bufferSizepcm 数组的元素数量,可以通过 sizeof(pcm)/sizeof(*pcm) 计算得出。使用 op_read 函数可以解码文件,因此需要将其放在一个 for 循环中,直到 op_read 返回 0 为止。


网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接