PookyFan
PookyFan

Reputation: 840

ffmpeg: Endianness of audio samples

I use ffmpeg's avcodec to retrieve raw audio samples from music files in my c++ application. For files I test it on it appears that these files samples' endianness is little-endian, but I wonder if that will be always true for all files I'd try to decode (i.e. that comes from ffmpeg's implementation or at least it's architecture-specific since mine computer's architecture uses little endian). If not, I assume it would depend on particular file's encoding format. In that case how can I check which endianess applies for each file I'm decoding? I can't find any relevant information in the docs.

Upvotes: 2

Views: 2729

Answers (2)

WLGfx
WLGfx

Reputation: 1179

As Andrey said, FFMpeg internally decodes to native endianness. This is mentioned in the header file for 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.

It doesn't describe though *le or *be. The formats available are:

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
};

Generally you will get planar 16 bit signed samples.

Upvotes: 3

Andrey Turkin
Andrey Turkin

Reputation: 2509

Internally ffmpeg always uses native endianness for audio samples since it makes it easier to perform various manipulations on the data (see libavutil/samplefmt.h file for some documentation on the matter); it is codec's task to convert to/from an appropriate endianness as dictated by file format. As a simple example of this: there is a family of trivial audiocodecs for reading/writing raw samples called pcm_*; e.g. there are pcm_s16le and pcm_s16be. On little-endian architecture pcm_s16le will do no conversion while pcm_s16be will swap bytes when decoding/encoding data.

Upvotes: 5

Related Questions