ffmpeg实现音频resample(重采样)

来源:互联网 发布:腰部赘肉 知乎 编辑:程序博客网 时间:2024/05/19 04:56

ffmpeg实现音频resample(重采样)

下面代码是实现将音频的三大要素:声道,样本,采样率变更的demo
例如双声道变成单声道,44100->48000,float->s16等等。 


[cpp] view plain copy
  1. int AudioResampling(AVCodecContext * audio_dec_ctx,AVFrame * pAudioDecodeFrame,  
  2.                     int out_sample_fmt,int out_channels ,int out_sample_rate , uint8_t * out_buf)  
  3. {  
  4.     //////////////////////////////////////////////////////////////////////////  
  5.     SwrContext * swr_ctx = NULL;  
  6.     int data_size = 0;  
  7.     int ret = 0;  
  8.     int64_t src_ch_layout = AV_CH_LAYOUT_STEREO; //初始化这样根据不同文件做调整  
  9.     int64_t dst_ch_layout = AV_CH_LAYOUT_STEREO; //这里设定ok  
  10.     int dst_nb_channels = 0;  
  11.     int dst_linesize = 0;  
  12.     int src_nb_samples = 0;  
  13.     int dst_nb_samples = 0;  
  14.     int max_dst_nb_samples = 0;  
  15.     uint8_t **dst_data = NULL;  
  16.     int resampled_data_size = 0;  
  17.       
  18.     //重新采样  
  19.     if (swr_ctx)  
  20.     {  
  21.         swr_free(&swr_ctx);  
  22.     }  
  23.     swr_ctx = swr_alloc();  
  24.     if (!swr_ctx)  
  25.     {  
  26.         printf("swr_alloc error \n");  
  27.         return -1;  
  28.     }  
  29.   
  30.     src_ch_layout = (audio_dec_ctx->channel_layout &&   
  31.         audio_dec_ctx->channels ==   
  32.         av_get_channel_layout_nb_channels(audio_dec_ctx->channel_layout)) ?   
  33.         audio_dec_ctx->channel_layout :   
  34.     av_get_default_channel_layout(audio_dec_ctx->channels);  
  35.   
  36.     if (out_channels == 1)  
  37.     {  
  38.         dst_ch_layout = AV_CH_LAYOUT_MONO;  
  39.     }  
  40.     else if(out_channels == 2)  
  41.     {  
  42.         dst_ch_layout = AV_CH_LAYOUT_STEREO;  
  43.     }  
  44.     else  
  45.     {  
  46.         //可扩展  
  47.     }  
  48.   
  49.     if (src_ch_layout <= 0)  
  50.     {  
  51.         printf("src_ch_layout error \n");  
  52.         return -1;  
  53.     }  
  54.   
  55.     src_nb_samples = pAudioDecodeFrame->nb_samples;  
  56.     if (src_nb_samples <= 0)  
  57.     {  
  58.         printf("src_nb_samples error \n");  
  59.         return -1;  
  60.     }  
  61.   
  62.     /* set options */  
  63.     av_opt_set_int(swr_ctx, "in_channel_layout",    src_ch_layout, 0);  
  64.     av_opt_set_int(swr_ctx, "in_sample_rate",       audio_dec_ctx->sample_rate, 0);  
  65.     av_opt_set_sample_fmt(swr_ctx, "in_sample_fmt", audio_dec_ctx->sample_fmt, 0);  
  66.   
  67.     av_opt_set_int(swr_ctx, "out_channel_layout",    dst_ch_layout, 0);  
  68.     av_opt_set_int(swr_ctx, "out_sample_rate",       out_sample_rate, 0);  
  69.     av_opt_set_sample_fmt(swr_ctx, "out_sample_fmt", (AVSampleFormat)out_sample_fmt, 0);  
  70.     swr_init(swr_ctx);  
  71.   
  72.     max_dst_nb_samples = dst_nb_samples =  
  73.         av_rescale_rnd(src_nb_samples, out_sample_rate, audio_dec_ctx->sample_rate, AV_ROUND_UP);  
  74.     if (max_dst_nb_samples <= 0)  
  75.     {  
  76.         printf("av_rescale_rnd error \n");  
  77.         return -1;  
  78.     }  
  79.   
  80.     dst_nb_channels = av_get_channel_layout_nb_channels(dst_ch_layout);  
  81.     ret = av_samples_alloc_array_and_samples(&dst_data, &dst_linesize, dst_nb_channels,  
  82.         dst_nb_samples, (AVSampleFormat)out_sample_fmt, 0);  
  83.     if (ret < 0)  
  84.     {  
  85.         printf("av_samples_alloc_array_and_samples error \n");  
  86.         return -1;  
  87.     }  
  88.   
  89.   
  90.     dst_nb_samples = av_rescale_rnd(swr_get_delay(swr_ctx, audio_dec_ctx->sample_rate) +  
  91.         src_nb_samples, out_sample_rate, audio_dec_ctx->sample_rate,AV_ROUND_UP);  
  92.     if (dst_nb_samples <= 0)  
  93.     {  
  94.         printf("av_rescale_rnd error \n");  
  95.         return -1;  
  96.     }  
  97.     if (dst_nb_samples > max_dst_nb_samples)  
  98.     {  
  99.         av_free(dst_data[0]);  
  100.         ret = av_samples_alloc(dst_data, &dst_linesize, dst_nb_channels,  
  101.             dst_nb_samples, (AVSampleFormat)out_sample_fmt, 1);  
  102.         max_dst_nb_samples = dst_nb_samples;  
  103.     }  
  104.   
  105.     data_size = av_samples_get_buffer_size(NULL, audio_dec_ctx->channels,  
  106.         pAudioDecodeFrame->nb_samples,  
  107.         audio_dec_ctx->sample_fmt, 1);  
  108.     if (data_size <= 0)  
  109.     {  
  110.         printf("av_samples_get_buffer_size error \n");  
  111.         return -1;  
  112.     }  
  113.     resampled_data_size = data_size;  
  114.       
  115.     if (swr_ctx)  
  116.     {  
  117.         ret = swr_convert(swr_ctx, dst_data, dst_nb_samples,   
  118.             (const uint8_t **)pAudioDecodeFrame->data, pAudioDecodeFrame->nb_samples);  
  119.         if (ret <= 0)  
  120.         {  
  121.             printf("swr_convert error \n");  
  122.             return -1;  
  123.         }  
  124.   
  125.         resampled_data_size = av_samples_get_buffer_size(&dst_linesize, dst_nb_channels,  
  126.             ret, (AVSampleFormat)out_sample_fmt, 1);  
  127.         if (resampled_data_size <= 0)  
  128.         {  
  129.             printf("av_samples_get_buffer_size error \n");  
  130.             return -1;  
  131.         }  
  132.     }  
  133.     else   
  134.     {  
  135.         printf("swr_ctx null error \n");  
  136.         return -1;  
  137.     }  
  138.   
  139.     //将值返回去  
  140.     memcpy(out_buf,dst_data[0],resampled_data_size);  
  141.   
  142.     if (dst_data)  
  143.     {  
  144.         av_freep(&dst_data[0]);  
  145.     }  
  146.     av_freep(&dst_data);  
  147.     dst_data = NULL;  
  148.   
  149.     if (swr_ctx)  
  150.     {  
  151.         swr_free(&swr_ctx);  
  152.     }  
  153.     return resampled_data_size;  
  154. }  
原创粉丝点击