ffmpeg解码音频数据时,进行重采样(即改变文件原有的采样率)
我们使用ffmpeg解码音频的时候,往往需要改变原音频的采样率,即需要重采样。
比如一音乐文件的采样率22050,而播放端往往是固定的采样率,比如44100。在这种情况下,如果把解码出来的数据直接播放,会产生快进的效果。这个时候就需要对解码出来的数据作一次重采样,将数据转化为44100采样率下的数据,才能正确播放。
ffmpeg提供了一组用来重采样的API,主要如下:
/** * Initialize audio resampling context. * * @param output_channels number of output channels * @param input_channels number of input channels * @param output_rate output sample rate * @param input_rate input sample rate * @param sample_fmt_out requested output sample format * @param sample_fmt_in input sample format * @param filter_length length of each FIR filter in the filterbank relative to the cutoff frequency * @param log2_phase_count log2 of the number of entries in the polyphase filterbank * @param linear if 1 then the used FIR filter will be linearly interpolated between the 2 closest, if 0 the closest will be used * @param cutoff cutoff frequency, 1.0 corresponds to half the output sampling rate * @return allocated ReSampleContext, NULL if error occured */ ReSampleContext *av_audio_resample_init(int output_channels, int input_channels, int output_rate, int input_rate, enum AVSampleFormat sample_fmt_out, enum AVSampleFormat sample_fmt_in, int filter_length, int log2_phase_count, int linear, double cutoff); int audio_resample(ReSampleContext *s, short *output, short *input, int nb_samples); /** * Free resample context. * * @param s a non-NULL pointer to a resample context previously * created with av_audio_resample_init() */ void audio_resample_close(ReSampleContext *s);
函数av_audio_resample_init()用来初始化重采样的参数,前4个参数很好理解;后6个参数基本上是使用缺省参数,分别为:
AV_SAMPLE_FMT_S16, AV_SAMPLE_FMT_S16,16, 10, 0, 1
函数audio_resample()用来重采样,前3个参数都好理解,最后一个参数是指“原数据的采样个数”,而不是input的bytes数。该函数的返回值也是采样个数,不过是重采样之后的。
函数audio_resample_close()用来清理重采样时分配的资源。
相关代码如下:
初始化
// need to do re-sample if (m_codec_ctx->sample_rate != m_out_samplerate) { LOGW("%s, need re-sample, initialize re-sample engine! out channels:%d, out sample rate:%d hz, in channels:%d, in sample rate:%d",__FUNCTION__, 2, m_out_samplerate, m_codec_ctx->channels, m_codec_ctx->sample_rate); m_resample_engine = av_audio_resample_init( 2, m_codec_ctx->channels, m_out_samplerate, m_codec_ctx->sample_rate, AV_SAMPLE_FMT_S16, AV_SAMPLE_FMT_S16, 16, 10, 0, 1); }
重采样代码
frame_size = AVCODEC_MAX_AUDIO_FRAME_SIZE; memset(m_audio_buff, 0, AVCODEC_MAX_AUDIO_FRAME_SIZE); decoded_len = avcodec_decode_audio3(m_codec_ctx, (short *)m_audio_buff, &frame_size, &m_avpkt); LOGI("%s, current decoded size:%d", __FUNCTION__, decoded_len); if (decoded_len > 0) { m_avpkt.size -= decoded_len; m_avpkt.data += decoded_len; decoded_audio_len = frame_size; valid_data_pointer = m_audio_buff; // need to re-sample if (m_resample_engine) { // convert byte to short int after_resampled_len = 0; int before_resampled_len = frame_size/(2 * m_codec_ctx->channels); memset(m_audio_resampled_buff, 0, AVCODEC_MAX_AUDIO_FRAME_SIZE); after_resampled_len = audio_resample(m_resample_engine, (short *)m_audio_resampled_buff, (short *)m_audio_buff, before_resampled_len); LOGI("%s, re-sampled! length in:%d, length out:%d", __FUNCTION__, before_resampled_len, after_resampled_len); decoded_audio_len = after_resampled_len * 2 * 2; //convert short to byte, and 2 channels valid_data_pointer = m_audio_resampled_buff; } memcpy(buff+copied_len, valid_data_pointer, decoded_audio_len); copied_len += decoded_audio_len; LOGI("%s, copy1, %d bytes has copied to output buff, total:%d!", __FUNCTION__, decoded_audio_len, copied_len); }
释放代码
if (m_resample_engine) { audio_resample_close(m_resample_engine); m_resample_engine = 0; }