av_samples_fill_arrays是FFmpeg中一个非常重要的函数,用于填充音频数据的指针数组。在音视频处理中,经常需要处理音频数据,而av_samples_fill_arrays可以正确地组织音频数据,以便后续处理和编解码。
av_samples_fill_arrays函数的原型:
/**
* 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 minimum size in bytes required for the buffer on success,
* or a negative error code on failure
*/
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)
{
int ch, planar, buf_size, line_size;
planar = av_sample_fmt_is_planar(sample_fmt);
buf_size = av_samples_get_buffer_size(&line_size, nb_channels, nb_samples,
sample_fmt, align);
if (buf_size < 0)
return buf_size;
if (linesize)
*linesize = line_size;
memset(audio_data, 0, planar
? sizeof(*audio_data) * nb_channels
: sizeof(*audio_data));
if (!buf)
return buf_size;
audio_data[0] = (uint8_t *)buf;
for (ch = 1; planar && ch < nb_channels; ch++)
audio_data[ch] = audio_data[ch-1] + line_size;
return buf_size;
}
参数解释:
- audio_data:指向一个指针数组,用于存储音频数据的指针。/AVFrame.data/
- linesize:存储每个音频数据指针的大小。/AVFrame.linesize/
- buf:指向包含音频数据的缓冲区。
- nb_channels:音频通道数。/AVFrame.channels/
- nb_samples:每个通道的样本数。/AVFrame.nb_samples/
- sample_fmt:音频样本格式。/AVFrame.format/
- align:对齐方式。
正确使用av_samples_fill_arrays函数的关键在于正确理解参数的含义和传入正确的参数值。以下是一个简单的示例代码,演示如何使用av_samples_fill_arrays函数:
#include <libavutil/samplefmt.h>
#include <libavutil/mem.h>
#include <libavutil/opt.h>
void fill_audio_data(uint8_t **audio_data, int *linesize, uint8_t *buf, int nb_channels, int nb_samples, enum AVSampleFormat sample_fmt) {
int ret = av_samples_fill_arrays(audio_data, linesize, buf, nb_channels, nb_samples, sample_fmt, 0);
if (ret < 0) {
// 错误处理
printf("Error filling audio data arrays\n");
}
}
int main() {
int nb_samples = 1024;
int nb_channels = 2;
enum AVSampleFormat sample_fmt = AV_SAMPLE_FMT_FLTP;
uint8_t *buf = (uint8_t *)av_malloc(av_samples_get_buffer_size(NULL, nb_channels, nb_samples, sample_fmt, 1));
uint8_t *audio_data[8]; // 假设最多支持8个通道
int linesize[8];
fill_audio_data(audio_data, linesize, buf, nb_channels, nb_samples, sample_fmt);
// 使用填充好的音频数据进行后续处理
av_free(buf);
return 0;
}
在这段示例代码中,首先分配了一个足够大的缓冲区用于存储音频数据,然后调用fill_audio_data函数填充音频数据指针数组。最后可以使用填充好的音频数据进行后续的音频处理操作。
标签:size,arrays,buf,nb,samples,audio,data,fill From: https://www.cnblogs.com/faithlocus/p/18025852