ffmpeg重采样中swr_convert和swr_get_out_samples的用法

来源:互联网 发布:淘宝店铺取什么名字好 编辑:程序博客网 时间:2024/06/07 03:39

ffmpeg重采样中swr_convert和swr_get_out_samples的用法

在做mux的时候关于重采样可以用fifo,或者audiofifo做缓存处理,当做demux的时候关于重采样就可以用到上面的swr_convert和swr_get_out_samples做配合处理。先看下这两个函数的注释:

/** Convert audio. * * in and in_count can be set to 0 to flush the last few samples out at the * end. * * If more input is provided than output space, then the input will be buffered. * You can avoid this buffering by using swr_get_out_samples() to retrieve an * upper bound on the required number of output samples for the given number of * input samples. Conversion will run directly without copying whenever possible. * * @param s         allocated Swr context, with parameters set * @param out       output buffers, only the first one need be set in case of packed audio * @param out_count amount of space available for output in samples per channel * @param in        input buffers, only the first one need to be set in case of packed audio * @param in_count  number of input samples available in one channel * * @return number of samples output per channel, negative value on error */int swr_convert(struct SwrContext *s, uint8_t **out, int out_count,                                const uint8_t **in , int in_count);

/** * Find an upper bound on the number of samples that the next swr_convert * call will output, if called with in_samples of input samples. This * depends on the internal state, and anything changing the internal state * (like further swr_convert() calls) will may change the number of samples * swr_get_out_samples() returns for the same number of input samples. * * @param in_samples    number of input samples. * @note any call to swr_inject_silence(), swr_convert(), swr_next_pts() *       or swr_set_compensation() invalidates this limit * @note it is recommended to pass the correct available buffer size *       to all functions like swr_convert() even if swr_get_out_samples() *       indicates that less would be used. * @returns an upper bound on the number of samples that the next swr_convert *          will output or a negative value to indicate an error */int swr_get_out_samples(struct SwrContext *s, int in_samples);


就说如果传入的nb_samles大于了传出的nb_samplse则SwrContext中会有缓存,会导致内存一直暴涨,解决方法,可以看如下代码:

没有缓存的重采样这么处理:

ret = swr_convert(swrcontext, pOutputFrame->data,pOutputFrame->nb_samples,(const uint8_t**)pInputFrame->data,pInputFrame->nb_samples);

有缓存的代码这么处理:

//如果还有缓存在swrcontext中,第二个参数要填写0才能获取到,缓存数据int fifo_size = swr_get_out_samples(swrcontext,0);if ( fifo_size >= pOutputFrame->nb_samples){ret = swr_convert(swrcontext, pOutputFrame->data,pOutputFrame->nb_samples,NULL,0);}

即如果有缓存则先判断是否有缓存在里面,如果有则传入数据为空取出缓存。


如有错误请指正:

交流请加QQ群:62054820
QQ:379969650.



0 0