FFmpeg针对音频采样格式的获取与处理,封装有专门工具类。音频采样格式枚举类型与相关API,位于libavutil模块的samplefmt.h,包括获取采样格式名字、获取采样格式类型、获取每个采样所占字节、获取采样数所需缓冲区大小、填充采样数组、分配采样缓冲区、拷贝采样数据、填充采样数据为静音。
1、AVSampleFormat
AVSampleFormat是音频采样格式的枚举类型,按照符号类型分为有符号与无符号,按照存储位数分为8位、16位、32位、64位,按照数值类型分为int、short、float、double,按照存储结构分为packed和planar。具体如下:
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_S64, ///< signed 64 bits
AV_SAMPLE_FMT_S64P, ///< signed 64 bits, planar
AV_SAMPLE_FMT_NB ///< Number of sample formats. DO NOT USE if linking dynamically
};
2、av_get_sample_fmt_name
获取采样格式名字,返回对应AVSampleFormat枚举的name。在samplefmt.c有对应的SampleFmtInfo,包括name、bits、planar、altform等字段:
static const SampleFmtInfo sample_fmt_info[AV_SAMPLE_FMT_NB] = {
[AV_SAMPLE_FMT_U8] = { .name = "u8", .bits = 8, .planar = 0, .altform = AV_SAMPLE_FMT_U8P },
[AV_SAMPLE_FMT_S16] = { .name = "s16", .bits = 16, .planar = 0, .altform = AV_SAMPLE_FMT_S16P },
[AV_SAMPLE_FMT_S32] = { .name = "s32", .bits = 32, .planar = 0, .altform = AV_SAMPLE_FMT_S32P },
[AV_SAMPLE_FMT_S64] = { .name = "s64", .bits = 64, .planar = 0, .altform = AV_SAMPLE_FMT_S64P },
[AV_SAMPLE_FMT_FLT] = { .name = "flt", .bits = 32, .planar = 0, .altform = AV_SAMPLE_FMT_FLTP },
[AV_SAMPLE_FMT_DBL] = { .name = "dbl", .bits = 64, .planar = 0, .altform = AV_SAMPLE_FMT_DBLP },
[AV_SAMPLE_FMT_U8P] = { .name = "u8p", .bits = 8, .planar = 1, .altform = AV_SAMPLE_FMT_U8 },
[AV_SAMPLE_FMT_S16P] = { .name = "s16p", .bits = 16, .planar = 1, .altform = AV_SAMPLE_FMT_S16 },
[AV_SAMPLE_FMT_S32P] = { .name = "s32p", .bits = 32, .planar = 1, .altform = AV_SAMPLE_FMT_S32 },
[AV_SAMPLE_FMT_S64P] = { .name = "s64p", .bits = 64, .planar = 1, .altform = AV_SAMPLE_FMT_S64 },
[AV_SAMPLE_FMT_FLTP] = { .name = "fltp", .bits = 32, .planar = 1, .altform = AV_SAMPLE_FMT_FLT },
[AV_SAMPLE_FMT_DBLP] = { .name = "dblp", .bits = 64, .planar = 1, .altform = AV_SAMPLE_FMT_DBL },
};
3、av_get_sample_fmt
获取采样格式类型,返回对应AVSampleFormat枚举的类型。
4、av_get_alt_sample_fmt
返回planar或packed可选的采样格式类型,如果出错则返回AV_SAMPLE_FMT_NONE。
5、av_get_packed_sample_fmt
获取交错存储的采样格式,返回AVSampleFormat枚举的类型。
6、av_get_planar_sample_fmt
获取平面存储的采样格式,返回AVSampleFormat枚举的类型。
7、av_get_bytes_per_sample
根据采样格式,获取每个采样所占的字节数。
8、av_sample_fmt_is_planar
判断采样格式是否为平面存储。
9、av_samples_get_buffer_size
获取采样数所需要的缓冲区大小,代码实现位于samplefmt.c,具体如下:
int av_samples_get_buffer_size(int *linesize, int nb_channels, int nb_samples,
enum AVSampleFormat sample_fmt, int align)
{
int line_size;
int sample_size = av_get_bytes_per_sample(sample_fmt);
int planar = av_sample_fmt_is_planar(sample_fmt);
// 检查参数是否合法
if (!sample_size || nb_samples <= 0 || nb_channels <= 0)
return AVERROR(EINVAL);
// 如果没有指定,自动选择是否对齐
if (!align) {
if (nb_samples > INT_MAX - 31)
return AVERROR(EINVAL);
align = 1;
nb_samples = FFALIGN(nb_samples, 32);
}
// 检查integer是否溢出
if (nb_channels > INT_MAX / align ||
(int64_t)nb_channels * nb_samples > (INT_MAX - (align * nb_channels)) / sample_size)
return AVERROR(EINVAL);
line_size = planar ? FFALIGN(nb_samples * sample_size, align) :
FFALIGN(nb_samples * sample_size * nb_channels, align);
if (linesize)
*linesize = line_size;
return planar ? line_size * nb_channels : line_size;
}
10、av_samples_fill_arrays
根据采样数填充数组,对于planar平面存储,每个声道的缓冲区设置指针头;对于packed交错存储,整个缓冲区只设置一个指针头:
/**
* Fill plane data pointers and linesize for samples with sample
* format sample_fmt.
*
* The audio_data array is filled with the pointers to the samples data planes:
* for planar, set the start point of each channel's data within the buffer,
* for packed, set the start point of the entire buffer only.
*
* The value pointed to by linesize is set to the aligned size of each
* channel's data buffer for planar layout, or to the aligned size of the
* buffer for all channels for packed layout.
*
* The buffer in buf must be big enough to contain all the samples
* (use av_samples_get_buffer_size() to compute its minimum size),
* otherwise the audio_data pointers will point to invalid data.
*
* @see enum AVSampleFormat
* The documentation for AVSampleFormat describes the data layout.
*
* @param[out] audio_data array to be filled with the pointer for each channel
* @param[out] linesize calculated linesize, may be NULL
* @param buf the pointer to a buffer containing the samples
* @param nb_channels the number of channels
* @param nb_samples the number of samples in a single channel
* @param sample_fmt the sample format
* @param align buffer size alignment (0 = default, 1 = no alignment)
* @return >=0 on success or a negative error code on failure
* @todo return minimum size in bytes required for the buffer in case
* of success at the next bump
*/
int av_samples_fill_arrays(uint8_t **audio_data, int *linesize,
const uint8_t *buf,
int nb_channels, int nb_samples,
enum AVSampleFormat sample_fmt, int align);
11、av_samples_alloc
为指定nb_samples的采样数分配采样缓冲区。
12、av_samples_copy
把采样数据从源地址拷贝到目的地址。
13、av_samples_set_silence
把采样数据填充为静音,如果是AV_SAMPLE_FMT_U8或AV_SAMPLE_FMT_U8P类型,就填充0x80,否则统一填充0x00。代码如下:
int av_samples_set_silence(uint8_t **audio_data, int offset, int nb_samples,
int nb_channels, enum AVSampleFormat sample_fmt)
{
int planar = av_sample_fmt_is_planar(sample_fmt);
int planes = planar ? nb_channels : 1;
int block_align = av_get_bytes_per_sample(sample_fmt) * (planar ? 1 : nb_channels);
int data_size = nb_samples * block_align;
int fill_char = (sample_fmt == AV_SAMPLE_FMT_U8 ||
sample_fmt == AV_SAMPLE_FMT_U8P) ? 0x80 : 0x00;
int i;
offset *= block_align;
for (i = 0; i < planes; i++)
memset(audio_data[i] + offset, fill_char, data_size);
return 0;
}