ffmpeg重采样后如何获取sample_size大小的数据
在ffmpeg用avfilter进行重采样后,可能得不到编码器设定好的sample_size大小的数据,这个时候需要重新组合采样数据,让sample data的大小等于_acodec_ctx_p->frame_size,才能拿去编码。
这里采用ffmpeg api中av_audio_fifo的实用方法来实现
1. 将sample data放入 audio fifo buffer
int v_encode::add_samples_to_fifo(uint8_t **data, const int frame_size) {
int error;
error = av_audio_fifo_realloc(_fifo_p, av_audio_fifo_size(_fifo_p) + frame_size);
if(error < 0 ) {
return error;
}
if(av_audio_fifo_write(_fifo_p, (void **)data, frame_size) < frame_size) {
ErrorLogf("Could not write data to fifo, src url:%s", _src_url.c_str());
return AVERROR_EXIT;
}
return 0;
}
调用的方法:
add_samples_to_fifo(filtered_frame->data, filtered_frame->nb_samples);
2. 编码器所需size的sample data
while(av_audio_fifo_size(_fifo_p) > _acodec_ctx_p->frame_size) {
uint8_t* frame_buf = nullptr;
AVFrame* dst_frame_p = get_new_audio_frame(frame_buf);
av_audio_fifo_read(_fifo_p, (void **)dst_frame_p->data, _acodec_ctx_p->frame_size);
av_frame_copy_props(dst_frame_p, filtered_frame);
dst_frame_p->nb_samples = _acodec_ctx_p->frame_size;
dst_frame_p->channels = _acodec_ctx_p->channels;
dst_frame_p->channel_layout = _acodec_ctx_p->channel_layout;
dst_frame_p->format = _acodec_ctx_p->sample_fmt;
dst_frame_p->pkt_dts = filtered_frame->pkt_dts;
dst_frame_p->pts = filtered_frame->pts;
dst_frame_p->pict_type = AV_PICTURE_TYPE_NONE;
dst_frame_p->pts = av_rescale_q(dst_frame_p->pts, filter_tb, _acodec_ctx_p->time_base);
//dst_framed拿去编码
.......
}