c++ - 从 libav 收集解码的音频作为 double

标签 c++ audio ffmpeg decode libav

我目前正在尝试收集解码的音频数据(来自多种格式)以执行某些音频操作(使用 *.wav 文件进行测试)。

我有一个通过 FFmpeg libav 处理所有解码的类。如果我将数据作为 unit8_t 提取到 vector 中,并且

for (int i = 0; i < bytevector.size(); i++) {
    fwrite(&bytevector[i], sizeof (uint8_t), 1, outfile2);
}

到原始文件并通过播放play -t raw -r 44100 -b16 -c 1 -e signed sound.raw听起来很好。

但是,怎么可能拥有所有 正确信息例如,当文件是每个样本 2 个字节并且 frame->data信息以 uint8_t 给出?我测试过的 wav 文件是 44100/16bits/1 channel 。 (我已经有将 uint8_t* 更改为 double 的代码)

使用 Scilab 打开相同的文件将显示一半大小的字节 vector 作为 double 数。

wav file in Scilab as an array of doubles shows:
-0.1, -0.099, -0.098, ..., 0.099, +0.1

versus byte vector:
51, 243, 84, 243, 117, 243, ...



51和243真的能组成双吗?关于如何解决这个问题的任何建议?

以下代码供引用:
 while ((av_read_frame(formatContext, &readingPacket)) == 0) {
        if (readingPacket.stream_index == audioStreamIdx) {
            AVPacket decodingPacket = readingPacket;

            while (decodingPacket.size > 0) {
                int gotFrame = 0;
                int result = avcodec_decode_audio4(context, frame, &gotFrame, &decodingPacket);

                if (result < 0) {
                    break;
                }

                decoded = FFMIN(result, decodingPacket.size);

                if (gotFrame) {
                    data_size = (av_get_bytes_per_sample(context->sample_fmt));
                    if (data_size < 0) {
                    }

                    // Only for 1 channel temporarily
                    for (int i = 0; i < frame->nb_samples; i++) {
                        for (int ch = 0; ch < context->channels; ch++) {
                            for (int j = 0; j < data_size; j++) {
                                bytevector.push_back(*(frame->data[ch] + data_size * i + j)); 
                            }
                        }
                    }
                } else {
                    decodingPacket.size = 0;
                    decodingPacket.data = NULL;
                }
                decodingPacket.size -= result;
                decodingPacket.data += result;
            }
        }
        av_free_packet(&readingPacket);
    }

最佳答案

音频数据以许多不同的格式存储。你得到一个uint8_t[]数组意味着相当少。每个数组不是一个字节。相反,您需要知道格式。这里-b16告诉我 uint8_t[] data 实际上是 16 位 PCM 编码的数据,即范围从 -32768 到 +32767。 Scilab 似乎更喜欢浮点刻度,因此除以 32768.0。这只是表示形式的变化;它只是将比例缩小到-1.0,+1.0。

将其与角度进行比较:直角在 pi/2 弧度上是 90 度;确切的数字无关紧要,但两者都是整圈的 1/4。

关于c++ - 从 libav 收集解码的音频作为 double ,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/31659306/

相关文章:

c++ - C++头文件的组织

c++ - eclipse CDT 8.01 - 默认路径(libstdc、libstdc++)在 'includes' 目录中完全消失

iphone - Xcode游戏声音可以在Sim和iPad上播放,但不能在iPhone 6上播放

python - python 2.7中的FFMPEG文件编写器

ffmpeg - 如何用FFMPEG分块记录UDP流?

c++ - 解析 C++ 中的命令行参数?

c++ - 如何通过命令行在 Visual Studio/cmake 上构建 yaml-cpp?

android - Android Studio如何在 Activity 开始时自动播放背景音乐

python - 用python进行视频编辑。使用mhmovie在python中合并.mp3和.mp4文件

command-line - ffplay双显示器设置