【问题标题】:Decode Ogg/Opus file解码 Ogg/Opus 文件
【发布时间】:2023-03-26 18:59:01
【问题描述】:

我有一小段 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;
}

【问题讨论】:

  • 应该同步解码还是异步解码?

标签: c++ opus


【解决方案1】:

看来您使用的是 Opus-Tools 而不是 OpusFile。显然,您已经链接到 libopus.a 库,但您还需要下载并构建 OpusFile 0.7 并将您的程序链接到从构建 OpusFile 创建的 libopusfile.a。在 OpusFile 0.7 的程序中包含 opusfile.h。最后,您需要从xiph.org/downloads 下载 libogg 1.3.2 并链接到该库来下载并构建 libogg 库。

This link 是解释如何打开和关闭 ogg opus 流进行解码的文档。

确保您有一个 ogg opus 文件并使用...打开流

OggOpusFile *file = op_open_file(inputfile, error)(inputfile is char* inputfile and error is an int pointer)

使用op_free(file) 关闭流。这是 function documentation 实际解码 ogg opus 流。在调用 op_free 之前,使用...解码音频数据

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

bufferSizesizeof(pcm)/sizeof(*pcm)op_read 将解码更多文件,因此将其放入 for 循环中,直到 op_read 返回 0

【讨论】:

    猜你喜欢
    • 2020-02-05
    • 2019-03-23
    • 2020-09-24
    • 2021-03-21
    • 2021-02-05
    • 2016-11-06
    • 2021-09-02
    • 2020-10-13
    • 2021-11-19
    相关资源
    最近更新 更多