ffmpeg重采样中swr_convert和swr_get_out_samples的用法

ffmpeg重采样中swr_convert和swr_get_out_samples的用法

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

[cpp] view plain copy

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

[cpp] view plain copy

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

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

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

[cpp] view plain copy

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


有缓存的代码这么处理:

[cpp] view plain copy

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

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

猜你喜欢

转载自blog.csdn.net/qq_21743659/article/details/107856861
SWR