ffmpeg_function: av_sample_get_buffer_size

本文详细介绍了PCM音频格式的核心参数,包括通道数、采样频率及采样位数,并解释了这些参数如何影响音质。此外,还介绍了如何利用ffmpeg中的av_samples_get_buffer_size函数计算音频数据的实际大小。

音频一般是采用成PCM格式,而计算PCM格式音频尺寸,就需要如下几个参数。


通道数,采样频率,采用格式。


通道数:个人理解,就是同时有个几个设备在进行音频的采样,最少为1,一般通道数越多,音质越好。

采样频率:(也称为采样速度或者采样频率)定义了每秒从连续信号中提取并组成离散信号的采样个数,它用赫兹(Hz)来表示。

采用位数:既然采样频率表示每秒采样的个数,那么如何描述每个采样点呢?用什么方法独立每个采样点值的区别呢?也就是如何度量每个采样点,而这正是采样格式出现的意义。通常使用16bit,也就是2的16次方,共有65536个不同的度量值,这样采样位数越高,音频度量化的就越精细,音质同样也就越高。


所以音频所占用字节数 = 通道数 * 采用频率(Hz) * 采用位数(byte)


而在ffmpeg里面就使用av_sample_get_buffer_size来计算音频占用的字节数。


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

    /* validate parameter ranges */
    if (!sample_size || nb_samples <= 0 || nb_channels <= 0)
        return AVERROR(EINVAL);

    /* auto-select alignment if not specified */
    if (!align) {
        if (nb_samples > INT_MAX - 31)
            return AVERROR(EINVAL);
        align = 1;
        nb_samples = FFALIGN(nb_samples, 32);
    }

    /* check for integer overflow */
    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;
}





参考:

1, http://blog.youkuaiyun.com/oldmtn/article/details/7568452

2. http://blog.youkuaiyun.com/oldmtn/article/details/7743445


参考:

1, http://blog.youkuaiyun.com/oldmtn/article/details/7568452

2. http://blog.youkuaiyun.com/oldmtn/article/details/7743445

LOCAL void reset_audio_buffer(int session_id, int session_type) { for (int i = g_audio_play_params_loop.start_index; i <= g_audio_play_params_loop.end_index; i++) { if (session_id == g_audio_play_params_loop.session_id[i] && session_type == g_audio_play_params_loop.session_type[i]) { g_audio_play_params_loop.discard[i] = 1; } } } /** * @brief Checks if any audio channel has enough data for processing * @param frame_size Required size in bytes to check against * @return TRUE if at least one channel has more data than frame_size, FALSE otherwise */ LOCAL int has_enough_data(size_t frame_size) { if (NULL == g_mix_buffer) { return FALSE; } for (int ch = 0; ch < MIX_BUF_CH; ch++) { if (g_mix_buffer->mix_channels[ch].valid_len > frame_size) { return TRUE; } } return FALSE; } /** * @brief Mixes audio data from multiple channels into a single output stream * * This function reads audio data from multiple channels in a ring buffer, * mixes them together, and writes the mixed PCM data to the output. * After reading, the source buffer is cleared. * * @param frame_size Size of each audio frame in bytes * @param read_data Buffer to store data read from channels * @param mixed_data Buffer to store final mixed audio data * * @note Assumes g_mix_buffer is properly initialized with MIX_BUF_CH channels */ LOCAL void mix_audio_ex(size_t frame_size, unsigned char *read_data, unsigned char *mixed_data) { unsigned char *src_data[MIX_BUF_CH] = {NULL}; int ch = 0, i = 0; size_t start_pos = 0, read_len = 0, buffer_index = 0, valid_cnt = 0; memset(read_data, 0, frame_size * MIX_BUF_CH); // 从所有通道读取数据 for (ch = 0; ch < MIX_BUF_CH; ch++) { start_pos = g_mix_buffer->mix_channels[ch].read_pos; if (g_mix_buffer->mix_channels[ch].valid_len > 0) { valid_cnt++; // 从环形缓冲区复制数据 read_len = min(g_mix_buffer->mix_channels[ch].valid_len, frame_size); for (i = 0; i < read_len; i++) { buffer_index = (start_pos + i) % g_mix_buffer->buf_size; read_data[i + ch * frame_size] = g_mix_buffer->buffer[ch][buffer_index]; g_mix_buffer->buffer[ch][buffer_index] = 0; // 清空已读取数据 } // 更新缓冲区状态 g_mix_buffer->mix_channels[ch].read_pos = (start_pos + read_len) % g_mix_buffer->buf_size; g_mix_buffer->mix_channels[ch].valid_len -= read_len; } src_data[ch] = read_data + ch * frame_size; } // 环形内存中有数据的channel数量小于反向音频流的数量属于异常现象 if (valid_cnt < g_audio_stream_count) { SPEAKER_DEBUG("Audio stream with missing segments!"); } // 混合并写入PCM数据 mix_pcm_sound(src_data, MIX_BUF_CH, mixed_data, frame_size); write_PCM_sound((uint8_t*)mixed_data, frame_size, frame_size); } /** * @brief Audio mixing thread function that combines multiple audio streams * @param param Thread parameter (unused) * * Continuously reads audio data from input streams, mixes them together, * and processes the mixed output. The function allocates buffers for reading * and mixing audio frames, processes data while audio streams are active, * and cleans up resources before exiting. * * @return NULL */ static void *mix_audio(void *param) { size_t frame_size = g_chunk_byte * 2; unsigned char *read_data = calloc(frame_size * MIX_BUF_CH, sizeof(unsigned char)); unsigned char *mixed_data = calloc(frame_size, sizeof(unsigned char)); int32_t time_interval = 1000 * frame_size / (g_audio_play_params_loop.spk_sample_rate * AUDIO_PRE_FRAME_BIT / 8); if (!read_data || !mixed_data) { SPEAKER_ERROR("fail to alloc memory for read_data or mixed_data!"); if (read_data) free(read_data); if (mixed_data) free(mixed_data); return NULL; } while (0 < g_audio_stream_count) { if (TRUE == has_enough_data(frame_size)) { mix_audio_ex(frame_size, read_data, mixed_data); } } /* 当所有用户关闭双讲后,播放环形缓冲区剩余音频,至多播放一圈 */ unsigned long long int start_time = nvmp_get_us() / 1000; unsigned long long int now_time = start_time; while (TRUE == has_enough_data(frame_size)) { mix_audio_ex(frame_size, read_data, mixed_data); now_time = nvmp_get_us() / 1000; if (now_time - start_time > MIX_BUF_BL * time_interval) { break; } } g_audio_stream_context.mix_active = 0; free(read_data); free(mixed_data); return NULL; } /** * @brief Initialize the audio mixing thread and buffer * * Initializes a mixing buffer for audio channels if not already created. * Creates a thread for audio mixing operations. * The buffer can store 5 maximum-sized audio frames per channel. * * @return OK on success, ERROR on failure */ LOCAL int init_mix_audio_thread() { if (NULL == g_mix_buffer) { g_mix_buffer = init_mix_buf(MIX_BUF_CH, g_chunk_byte * 2 * MIX_BUF_BL); //每个音频通道可以存放MIX_BUF_BL个SDK层能够播放的最大音频帧 if (NULL == g_mix_buffer) { SPEAKER_ERROR("fail to init mix buffer\n"); return ERROR; } } g_audio_stream_count = 1; g_audio_stream_context.mix_active = 1; if (0 != pthread_create(&(g_audio_stream_context.mix_pid), NULL, mix_audio, NULL)) { SPEAKER_ERROR("fail to init mix audio thread\n"); g_audio_stream_count = 0; g_audio_stream_context.mix_active = 0; return ERROR; } else { pthread_detach(g_audio_stream_context.mix_pid); } return OK; }解析代码,这些可以用在我录像的混音中吗?
最新发布
10-17
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值