音频 MP2 编码和解码产生一半的数据C++和 FFMPEG
Audio MP2 encoding and decoding producing half data C++ and FFMPEG
我有 16 位 PCM 数据,带有立体声设置,我从麦克风中获取。
获得数据后,我使用以下编码器设置对其进行编码
AVCodec* audio_codec = avcodec_find_encoder(AV_CODEC_ID_MP2);
AVCodecContext* audio_codec_ctx = avcodec_alloc_context3(audio_codec);
audio_codec_ctx->bit_rate = 64000;
audio_codec_ctx->channels = 2;
audio_codec_ctx->channel_layout = AV_CH_LAYOUT_STEREO;
audio_codec_ctx->sample_rate = 44100;
audio_codec_ctx->sample_fmt = AV_SAMPLE_FMT_S16;
当我将音频数据传递到编码器时,我看到它每次需要 4608 字节的数据并将其正确编码为 MP2 数据。 麦克风抓取的 PCM 数据为 88320 字节,编码器每次获取 4608 字节并进行压缩。
如果我获取每个已编码的 4608 字节部分,并将其传递到具有与上述相同设置但使用解码器的解码器。
AVCodecID audio_codec_id = AV_CODEC_ID_MP2;
AVCodec * audio_decodec = avcodec_find_decoder(audio_codec_id);
audio_decodecContext = avcodec_alloc_context3(audio_decodec);
audio_decodecContext->bit_rate = 64000;
audio_decodecContext->channels = 2;
audio_decodecContext->channel_layout = AV_CH_LAYOUT_STEREO;
audio_decodecContext->sample_rate = 44100;
audio_decodecContext->sample_fmt = AV_SAMPLE_FMT_S16;
解码有效并且成功,但是当我查看数据大小时,它正好是编码的 2034 的一半。 我不明白为什么会这样。 考虑到编码器和解码器相同,我会想象我会得到 4608。
谁能阐明为什么会发生这种情况。 我应该设置什么?
应使用 audio_decodecContext->request_sample_fmt
设置请求的解码器示例格式。 sample_fmt
由解码器本身设置,并且可能不同,在这种情况下,您应该使用 libswresample 在示例格式之间进行转换。
相关文章:
- 防止主数据类型C++的隐式转换
- 用于访问容器<T>数据成员的正确 API
- 嵌套在类中时无法设置成员数据
- 使用流处理接收到的数据
- 静态数据成员的问题-修复链接错误会导致编译器错误
- 处理小于cpu数据总线的数据类型.(c++转换为机器代码)
- 在cuda线程之间共享大量常量数据
- C++将文本文件中的数据读取到结构数组中
- 如何在C++中序列化结构数据
- 在C++中打印指向不同基元数据类型的指针的内存地址
- 通过套接字[TCP]传输数据 如何在C / C ++中打包多个整数并使用send() recv()传输数据
- 在c代码之间共享数据的最佳方式
- 链表,反向函数,数据结构
- 数据成员SFINAE的C++17测试:gcc vs clang
- C++浮点数据类型和字符串数据类型无法子到模板函数中
- 如何对点云数据进行排序
- 从矢量<无符号字符>转换为字符* 包括垃圾数据
- 尝试通过OCI例程从Oracle获取blob数据,但出现错误:ORA-01008:并非所有变量都绑定
- Cuda C++:设备上的Malloc类,并用来自主机的数据填充它
- 在运行时处理类型擦除的数据-如何不重新发明轮子