ffmpeg实现音频resample(重采样)


  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. }  


  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
要在 C 语言中实现 FFmpeg音频重采样,需要使用 FFmpeg 的 libswresample 库。下面是一个简单的音频重采样的示例代码: ```c #include <stdio.h> #include <stdlib.h> #include <libswresample/swresample.h> int main(int argc, char **argv) { // source audio data uint8_t *src_data[2] = { NULL }; src_data[0] = (uint8_t *)malloc(src_nb_samples * src_channels * sizeof(uint8_t)); src_data[1] = NULL; int src_nb_samples = 1024; int src_channels = 2; int src_sample_rate = 44100; int src_bytes_per_sample = 2; // destination audio data uint8_t *dst_data[2] = { NULL }; dst_data[0] = (uint8_t *)malloc(dst_nb_samples * dst_channels * sizeof(uint8_t)); dst_data[1] = NULL; int dst_nb_samples = 1024; int dst_channels = 1; int dst_sample_rate = 48000; int dst_bytes_per_sample = 2; // create resampler context SwrContext *swr_ctx = swr_alloc_set_opts(NULL, av_get_default_channel_layout(dst_channels), AV_SAMPLE_FMT_S16, dst_sample_rate, av_get_default_channel_layout(src_channels), AV_SAMPLE_FMT_S16, src_sample_rate, 0, NULL); swr_init(swr_ctx); // perform audio resampling swr_convert(swr_ctx, dst_data, dst_nb_samples, (const uint8_t **)src_data, src_nb_samples); // cleanup swr_free(&swr_ctx); free(src_data[0]); free(dst_data[0]); return 0; } ``` 上述代码中,我们首先定义了源音频和目标音频的相关参数,包括采样率、声道数、采样位数等。然后我们创建了一个 SwrContext 上下文对象,并设置了源和目标音频的相关参数。接着我们调用 swr_init 函数初始化这个上下文对象,然后使用 swr_convert 函数将原始音频数据重采样为目标音频数据。最后我们释放了上下文对象和内存空间。 需要注意的是,该示例代码仅为了演示音频重采样的基本流程,实际使用时还需要进行参数检查和错误处理等操作。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值