您当前的位置: 首页 > 

鱼儿-1226

暂无认证

  • 0浏览

    0关注

    1100博文

    0收益

  • 0浏览

    0点赞

    0打赏

    0留言

私信
关注
热门博文

ffmpeg实现音频resample(重采样)

鱼儿-1226 发布时间:2020-08-07 10:30:57 ,浏览量:0

AVFifoBuffer和音频样本是av_sample_fmt_is_planar的样式采样率讲解,下面上代码

[cpp] view plain copy

  1. AVFifoBuffer * m_fifo = NULL;  
  2.   
  3.   
  4.   
  5. SwrContext * init_pcm_resample(AVFrame *in_frame, AVFrame *out_frame)  
  6. {  
  7.     SwrContext * swr_ctx = NULL;  
  8.     swr_ctx = swr_alloc();  
  9.     if (!swr_ctx)  
  10.     {  
  11.         printf("swr_alloc error \n");  
  12.         return NULL;  
  13.     }  
  14.     AVCodecContext * audio_dec_ctx = icodec->streams[audio_stream_idx]->codec;  
  15.     AVSampleFormat sample_fmt;  
  16.     sample_fmt = (AVSampleFormat)m_dwBitsPerSample; //样本  
  17.     if (audio_dec_ctx->channel_layout == 0)  
  18.     {  
  19.         audio_dec_ctx->channel_layout = av_get_default_channel_layout(icodec->streams[audio_stream_idx]->codec->channels);  
  20.     }  
  21.     /* set options */  
  22.     av_opt_set_int(swr_ctx, "in_channel_layout",    audio_dec_ctx->channel_layout, 0);  
  23.     av_opt_set_int(swr_ctx, "in_sample_rate",       audio_dec_ctx->sample_rate, 0);  
  24.     av_opt_set_sample_fmt(swr_ctx, "in_sample_fmt", audio_dec_ctx->sample_fmt, 0);  
  25.   
  26.     av_opt_set_int(swr_ctx, "out_channel_layout",    audio_dec_ctx->channel_layout, 0);  
  27.     av_opt_set_int(swr_ctx, "out_sample_rate",       audio_dec_ctx->sample_rate, 0);  
  28.     av_opt_set_sample_fmt(swr_ctx, "out_sample_fmt", sample_fmt, 0);  
  29.     swr_init(swr_ctx);  
  30.   
  31.     int64_t src_nb_samples = in_frame->nb_samples;  
  32.     out_frame->nb_samples = av_rescale_rnd(swr_get_delay(swr_ctx,oaudio_st->codec->sample_rate) + src_nb_samples,  
  33.         oaudio_st->codec->sample_rate, oaudio_st->codec->sample_rate, AV_ROUND_UP);  
  34.   
  35.     int ret = av_samples_alloc(out_frame->data, &out_frame->linesize[0],   
  36.         icodec->streams[audio_stream_idx]->codec->channels, out_frame->nb_samples,oaudio_st->codec->sample_fmt,1);  
  37.     if (ret codec->channels,  
  38.         2048, oaudio_st->codec->sample_fmt, 1);  
  39.     m_fifo = av_fifo_alloc(buffersize);  
  40.     return swr_ctx;  
  41. }  
  42.   
  43. int preform_pcm_resample(SwrContext * pSwrCtx,AVFrame *in_frame, AVFrame *out_frame)  
  44. {  
  45.     int ret = 0;  
  46.     if (pSwrCtx != NULL)   
  47.     {  
  48.         ret = swr_convert(pSwrCtx, out_frame->data, out_frame->nb_samples,   
  49.             (const uint8_t**)in_frame->data, in_frame->nb_samples);  
  50.         if (ret linesize[0], oaudio_st->codec->channels,  
  51.             ret, oaudio_st->codec->sample_fmt, 1);  
  52.         int sss = av_fifo_size(m_fifo);  
  53.         sss = av_fifo_realloc2(m_fifo, av_fifo_size(m_fifo) + out_frame->linesize[0]);  
  54.         sss = av_fifo_size(m_fifo);  
  55.         av_fifo_generic_write(m_fifo, out_frame->data[0], out_frame->linesize[0], NULL);  
  56.   
  57.         out_frame->pkt_pts = in_frame->pkt_pts;  
  58.         out_frame->pkt_dts = in_frame->pkt_dts;  
  59.         //有时pkt_pts和pkt_dts不同,并且pkt_pts是编码前的dts,这里要给avframe传入pkt_dts而不能用pkt_pts  
  60.         //out_frame->pts = out_frame->pkt_pts;  
  61.         out_frame->pts = in_frame->pkt_dts;  
  62.     }  
  63.     return 0;  
  64. }  
  65. void uinit_pcm_resample(AVFrame * poutframe,SwrContext * swr_ctx)  
  66. {  
  67.     if (poutframe)  
  68.     {  
  69.         avcodec_free_frame(&poutframe);  
  70.         poutframe = NULL;  
  71.     }  
  72.     if (swr_ctx)  
  73.     {  
  74.         swr_free(&swr_ctx);  
  75.         swr_ctx = NULL;  
  76.     }  
  77.     //析构pcm分包结构  
  78.     if(m_fifo)  
  79.     {  
  80.         av_fifo_free(m_fifo);  
  81.         m_fifo = NULL;  
  82.     }  
  83. }  
  84. int perform_code(int stream_type,AVFrame * picture)  
  85. {  
  86.     AVCodecContext *cctext = NULL;  
  87.     AVPacket pkt_t;  
  88.     av_init_packet(&pkt_t);  
  89.     pkt_t.data = NULL; // packet data will be allocated by the encoder  
  90.     pkt_t.size = 0;  
  91.     int frameFinished = 0 ;  
  92.   
  93.     if (stream_type == AUDIO_ID)  
  94.     {  
  95.         cctext = oaudio_st->codec;  
  96.         //如果进和出的的声道,样本,采样率不同,需要重采样  
  97.         if(icodec->streams[audio_stream_idx]->codec->sample_fmt != (AVSampleFormat)m_dwBitsPerSample ||  
  98.             icodec->streams[audio_stream_idx]->codec->channels != m_dwChannelCount ||  
  99.             icodec->streams[audio_stream_idx]->codec->sample_rate != m_dwFrequency)  
  100.         {  
  101.             int64_t pts_t = picture->pts;  
  102.             int duration_t = (double)cctext->frame_size * (icodec->streams[audio_stream_idx]->time_base.den /icodec->streams[audio_stream_idx]->time_base.num)/   
  103.                 icodec->streams[audio_stream_idx]->codec->sample_rate;  
  104.   
  105.             int frame_bytes = cctext->frame_size * av_get_bytes_per_sample(cctext->sample_fmt)* cctext->channels;  
  106.             AVFrame * pFrameResample = avcodec_alloc_frame();  
  107.             uint8_t * readbuff = new uint8_t[frame_bytes];  
  108.   
  109.             if(av_sample_fmt_is_planar(cctext->sample_fmt))  
  110.             {  
  111.                 frame_bytes /= cctext->channels;  
  112.             }  
  113.   
  114.             while (av_fifo_size(m_fifo) >= frame_bytes) //取出写入的未读的包  
  115.             {  
  116.                 pFrameResample->nb_samples = cctext->frame_size;  
  117.                 av_fifo_generic_read(m_fifo, readbuff, frame_bytes, NULL);  
  118.   
  119.                 //这里一定要考虑音频分片的问题  
  120.                 //如果是分片的avcodec_fill_audio_frame传入的buf是单声道的,但是buf_size 是两个声道加一起的数据量  
  121.                 //如果不是分片的avcodec_fill_audio_frame传入的buf是双声道的,buf_size 是两个声道加一起的数据量  
  122.                 if(av_sample_fmt_is_planar(cctext->sample_fmt))  
  123.                 {  
  124.                     avcodec_fill_audio_frame(pFrameResample,cctext->channels,cctext->sample_fmt,readbuff,frame_bytes * cctext->channels,1);  
  125.                 }  
  126.                 else  
  127.                 {                     
  128.                     avcodec_fill_audio_frame(pFrameResample,cctext->channels,cctext->sample_fmt,readbuff,frame_bytes,0);  
  129.                 }  
  130.   
  131.                 if(m_is_first_audio_pts == 0)  
  132.                 {  
  133.                     m_first_audio_pts = pts_t;  
  134.                     m_is_first_audio_pts = 1;  
  135.                 }  
  136.                 pFrameResample->pts = m_first_audio_pts;  
  137.                 m_first_audio_pts += duration_t;  
  138.   
  139.   
  140.                 pFrameResample->pts = av_rescale_q_rnd(pFrameResample->pts, icodec->streams[audio_stream_idx]->codec->time_base, oaudio_st->codec->time_base, AV_ROUND_NEAR_INF);  
  141.                 nRet = avcodec_encode_audio2(cctext,&pkt_t,pFrameResample,&frameFinished);  
  142.                 if (nRet>=0 && frameFinished)  
  143.                 {  
  144.                     write_frame(ocodec,AUDIO_ID,pkt_t);  
  145.                     av_free_packet(&pkt_t);  
  146.                 }  
  147.             }  
  148.             if (readbuff)  
  149.             {  
  150.                 delete []readbuff;  
  151.             }  
  152.             if (pFrameResample)  
  153.             {  
  154.                 av_free(pFrameResample);  
  155.                 pFrameResample = NULL;  
  156.             }  
  157.         }  
  158.         else  
  159.         {  
  160.             nRet = avcodec_encode_audio2(cctext,&pkt_t,picture,&frameFinished);  
  161.             if (nRet>=0 && frameFinished)  
  162.             {  
  163.                 write_frame(ocodec,AUDIO_ID,pkt_t);  
  164.                 av_free_packet(&pkt_t);  
  165.             }  
  166.         }  
  167.     }  
  168.     else if (stream_type == VIDEO_ID)  
  169.     {  
  170.         cctext = ovideo_st->codec;  
  171.         if(icodec->streams[video_stream_idx]->codec->ticks_per_frame != 1)  
  172.         {  
  173.             AVRational time_base_video_t;  
  174.             time_base_video_t.num = icodec->streams[video_stream_idx]->codec->time_base.num;  
  175.             time_base_video_t.den = icodec->streams[video_stream_idx]->codec->time_base.den /icodec->streams[video_stream_idx]->codec->ticks_per_frame;  
  176.             picture->pts = av_rescale_q_rnd(picture->pts, time_base_video_t, ovideo_st->codec->time_base, AV_ROUND_NEAR_INF);  
  177.         }  
  178.         else  
  179.         {  
  180.             picture->pts = av_rescale_q_rnd(picture->pts, icodec->streams[video_stream_idx]->codec->time_base, ovideo_st->codec->time_base, AV_ROUND_NEAR_INF);  
  181.         }  
  182.         avcodec_encode_video2(cctext,&pkt_t,picture,&frameFinished);  
  183.         picture->pts++;  
  184.         if (frameFinished)  
  185.         {  
  186.             write_frame(ocodec,VIDEO_ID,pkt_t);  
  187.             av_free_packet(&pkt_t);  
  188.         }  
  189.     }  
  190.     return 1;  
  191. }  

1:由于mp3的sample是1152 aac是1024 有时候将解码的mp3编码成aac时如果不做AVFifoBuffer操作,编码的aac音频sample会比原来的少很多,生成的音频会一卡一卡的明显少声音。

 

2:当要编码的音频样本是av_sample_fmt_is_planar分片的时候需要将解码后的视频添加到AVFrame结构体中:但是如图

关注
打赏
1604459285
查看更多评论
立即登录/注册

微信扫码登录

0.0499s