ffmpeg:音频样本的字节序

时间:2016-10-18 18:36:30

标签: c++ audio ffmpeg decoding libavcodec

我使用ffmpeg的avcodec从我的c ++应用程序中的音乐文件中检索原始音频样本。对于我测试它的文件,似乎这些文件样本的endianness是little-endian,但我想知道对于我尝试解码的所有文件是否总是如此(即来自ffmpeg的实现或者至少它的架构 - 具体,因为我的计算机的架构使用小端)。如果没有,我认为它将取决于特定文件的编码格式。在那种情况下,我如何检查哪个endianess适用于我正在解码的每个文件?我在文档中找不到任何相关信息。

2 个答案:

答案 0 :(得分:3)

内部ffmpeg始终对音频样本使用本机字节序,因为它可以更轻松地对数据执行各种操作(有关此问题的某些文档,请参阅libavutil/samplefmt.h文件);编解码器的任务是根据文件格式转换为/从适当的字节序转换。作为一个简单的例子:有一系列简单的音频解码器用于读取/写入称为pcm_*的原始样本;例如有pcm_s16lepcm_s16be。在little-endian架构上pcm_s16le将不进行转换,而pcm_s16be将在解码/编码数据时交换字节。

答案 1 :(得分:2)

正如安德烈所说,FFMpeg内部解码为本机字节序。这在libavutil / samplefmt.h的头文件中提到

 * Audio sample formats
 *
 * - The data described by the sample format is always in native-endian order.
 *   Sample values can be expressed by native C types, hence the lack of a signed
 *   24-bit sample format even though it is a common raw audio data format.

虽然没有描述* le或* be。可用的格式是:

enum AVSampleFormat {
    AV_SAMPLE_FMT_NONE = -1,
    AV_SAMPLE_FMT_U8,          ///< unsigned 8 bits
    AV_SAMPLE_FMT_S16,         ///< signed 16 bits
    AV_SAMPLE_FMT_S32,         ///< signed 32 bits
    AV_SAMPLE_FMT_FLT,         ///< float
    AV_SAMPLE_FMT_DBL,         ///< double

    AV_SAMPLE_FMT_U8P,         ///< unsigned 8 bits, planar
    AV_SAMPLE_FMT_S16P,        ///< signed 16 bits, planar
    AV_SAMPLE_FMT_S32P,        ///< signed 32 bits, planar
    AV_SAMPLE_FMT_FLTP,        ///< float, planar
    AV_SAMPLE_FMT_DBLP,        ///< double, planar

    AV_SAMPLE_FMT_NB           ///< Number of sample formats. DO NOT USE if linking dynamically
};

通常,您将获得平面16位带符号样本。