ffmpeg实现音频重采样的核心函数swr_convert功能非常强大,可是ffmpeg文档对它的注释太过简单,在应用中往往会出这样那样的问题,其实在读取数据->重采样->编码数据的循环中在第一次执行swr_convert后还应用swr_convert再作个缓存检测看看是否还有数据,如果有就要把它写到FIFO中去,留在下次再使用,这点在转码和由低向高转换采样率时特别重要。
下面一段简单的代码,摘自我自已写的测试程序.
const int frame_size = FFMIN(fifo_size, m_Opt->encode_pCodecCtx->frame_size); if ((ret = av_audio_fifo_read(m_fifo, (void **)m_fifo_samples_array, frame_size)) < frame_size) { fprintf(stderr, "Could not read data from FIFO\n"); return AVERROR_EXIT; } int out_samples = av_rescale_rnd(swr_get_delay(m_Opt->out_resample_context, 48000) + 1536, 44100, 48000, AV_ROUND_UP); int conver_samples= swr_convert(m_Opt->out_resample_context, m_fifo_conver_samples_array, frame_size, (const uint8_t **)m_fifo_samples_array, frame_size); ret = av_audio_fifo_size(m_conver_fifo); if ((ret = av_audio_fifo_realloc(m_conver_fifo, av_audio_fifo_size(m_conver_fifo) + frame_size)) < 0) { fprintf(stderr, "Could not reallocate FIFO\n"); return ret; } ret = av_audio_fifo_write(m_conver_fifo, (void **)m_fifo_conver_samples_array, conver_samples); fifo_size = av_audio_fifo_size(m_conver_fifo); if ((ret = av_audio_fifo_read(m_conver_fifo, (void **)m_Opt->out_samples_array, frame_size)) < frame_size) { fprintf(stderr, "Could not read data from FIFO\n"); return AVERROR_EXIT; } ret = Encode_audio(ret); int ret1 = 0; while ((ret1 = swr_convert(m_Opt->out_resample_context, m_fifo_conver_samples_array, frame_size, NULL, 0)) > 0) { if ((ret = av_audio_fifo_realloc(m_conver_fifo, av_audio_fifo_size(m_conver_fifo) + ret1)) < 0) { fprintf(stderr, "Could not reallocate FIFO\n"); return ret; } ret = av_audio_fifo_write(m_conver_fifo, (void **)m_fifo_conver_samples_array, ret1); fifo_size = av_audio_fifo_size(m_conver_fifo); if (fifo_size > m_Opt->encode_pFrame->nb_samples) { if ((ret = av_audio_fifo_read(m_conver_fifo, (void **)m_Opt->out_samples_array, frame_size)) < frame_size) { fprintf(stderr, "Could not read data from FIFO\n"); return AVERROR_EXIT; } ret = Encode_audio(ret); }
版权声明:本文为博主原创文章,未经博主允许不得转载。
原文地址:http://blog.csdn.net/qsq3/article/details/47049139