PCM音频采样数据编码为AAC的压缩编码数据

本文介绍一个最简单的基于FFMPEG的音频编码器。该编码器实现了PCM音频采样数据编码为AAC的压缩编码数据。编码器代码十分简单,但是每一行代码都很重要。通过看本编码器的源代码,可以了解FFMPEG音频编码的流程。

下面附一张使用FFmpeg编码音频的流程图。使用该流程,不仅可以编码AAC的音频,而且可以编码MP3,MP2等等各种FFmpeg支持的音频。图中蓝色背景的函数是实际输出数据的函数。浅绿色的函数是音频编码的函数。

简单介绍一下流程中各个函数的意义:

av_register_all():注册FFmpeg所有编解码器。

avformat_alloc_output_context2():初始化输出码流的AVFormatContext。

avio_open():打开输出文件。

av_new_stream():创建输出码流的AVStream。

avcodec_find_encoder():查找编码器。

avcodec_open2():打开编码器。

avformat_write_header():写文件头(对于某些没有文件头的封装格式,不需要此函数。比如说MPEG2TS)。

avcodec_encode_audio2():编码音频。即将AVFrame(存储PCM采样数据)编码为AVPacket(存储AAC,MP3等格式的码流数据)。

av_write_frame():将编码后的视频码流写入文件。

av_write_trailer():写文件尾(对于某些没有文件头的封装格式,不需要此函数。比如说MPEG2TS)。

 

  1. *本程序实现了音频PCM采样数据编码为压缩码流(MP3,WMA,AAC等)。 
  2.  *是最简单的FFmpeg音频编码方面的教程。 
  3.  *通过学习本例子可以了解FFmpeg的编码流程。 
  4.  *This software encode PCM data to AAC bitstream. 
  5.  *It’s the simplest audio encoding software based on FFmpeg.  
  6.  *Suitable for beginner of FFmpeg  
  7.  */  
  8.   
  9. #include <stdio.h>  
  10.   
  11. #define __STDC_CONSTANT_MACROS  
  12.   
  13. #ifdef _WIN32  
  14. //Windows  
  15. extern “C”  
  16. {  
  17. #include “libavcodec/avcodec.h”  
  18. #include “libavformat/avformat.h”  
  19. };  
  20. #else  
  21. //Linux…  
  22. #ifdef __cplusplus  
  23. extern “C”  
  24. {  
  25. #endif  
  26. #include <libavcodec/avcodec.h>  
  27. #include <libavformat/avformat.h>  
  28. #ifdef __cplusplus  
  29. };  
  30. #endif  
  31. #endif  
  32.   
  33.   
  34. int flush_encoder(AVFormatContext *fmt_ctx,unsigned int stream_index){  
  35.     int ret;  
  36.     int got_frame;  
  37.     AVPacket enc_pkt;  
  38.     if (!(fmt_ctx->streams[stream_index]->codec->codec->capabilities &  
  39.         CODEC_CAP_DELAY))  
  40.         return 0;  
  41.     while (1) {  
  42.         enc_pkt.data = NULL;  
  43.         enc_pkt.size = 0;  
  44.         av_init_packet(&enc_pkt);  
  45.         ret = avcodec_encode_audio2 (fmt_ctx->streams[stream_index]->codec, &enc_pkt,  
  46.             NULL, &got_frame);  
  47.         av_frame_free(NULL);  
  48.         if (ret < 0)  
  49.             break;  
  50.         if (!got_frame){  
  51.             ret=0;  
  52.             break;  
  53.         }  
  54.         printf(“Flush Encoder: Succeed to encode 1 frame!\tsize:%5d\n”,enc_pkt.size);  
  55.         /* mux encoded frame */  
  56.         ret = av_write_frame(fmt_ctx, &enc_pkt);  
  57.         if (ret < 0)  
  58.             break;  
  59.     }  
  60.     return ret;  
  61. }  
  62.   
  63. int main(int argc, char* argv[])  
  64. {  
  65.     AVFormatContext* pFormatCtx;  
  66.     AVOutputFormat* fmt;  
  67.     AVStream* audio_st;  
  68.     AVCodecContext* pCodecCtx;  
  69.     AVCodec* pCodec;  
  70.   
  71.     uint8_t* frame_buf;  
  72.     AVFrame* pFrame;  
  73.     AVPacket pkt;  
  74.   
  75.     int got_frame=0;  
  76.     int ret=0;  
  77.     int size=0;  
  78.   
  79.     FILE *in_file=NULL;                         //Raw PCM data  
  80.     int framenum=1000;                          //Audio frame number  
  81.     const char* out_file = “tdjm.aac”;          //Output URL  
  82.     int i;  
  83.   
  84.     in_file= fopen(“tdjm.pcm”“rb”);  
  85.   
  86.     av_register_all();  
  87.   
  88.     //Method 1.  
  89.     pFormatCtx = avformat_alloc_context();  
  90.     fmt = av_guess_format(NULL, out_file, NULL);  
  91.     pFormatCtx->oformat = fmt;  
  92.   
  93.   
  94.     //Method 2.  
  95.     //avformat_alloc_output_context2(&pFormatCtx, NULL, NULL, out_file);  
  96.     //fmt = pFormatCtx->oformat;  
  97.   
  98.     //Open output URL  
  99.     if (avio_open(&pFormatCtx->pb,out_file, AVIO_FLAG_READ_WRITE) < 0){  
  100.         printf(“Failed to open output file!\n”);  
  101.         return -1;  
  102.     }  
  103.   
  104.     audio_st = avformat_new_stream(pFormatCtx, 0);  
  105.     if (audio_st==NULL){  
  106.         return -1;  
  107.     }  
  108.     pCodecCtx = audio_st->codec;  
  109.     pCodecCtx->codec_id = fmt->audio_codec;  
  110.     pCodecCtx->codec_type = AVMEDIA_TYPE_AUDIO;  
  111.     pCodecCtx->sample_fmt = AV_SAMPLE_FMT_S16;  
  112.     pCodecCtx->sample_rate= 44100;  
  113.     pCodecCtx->channel_layout=AV_CH_LAYOUT_STEREO;  
  114.     pCodecCtx->channels = av_get_channel_layout_nb_channels(pCodecCtx->channel_layout);  
  115.     pCodecCtx->bit_rate = 64000;    
  116.   
  117.     //Show some information  
  118.     av_dump_format(pFormatCtx, 0, out_file, 1);  
  119.   
  120.     pCodec = avcodec_find_encoder(pCodecCtx->codec_id);  
  121.     if (!pCodec){  
  122.         printf(“Can not find encoder!\n”);  
  123.         return -1;  
  124.     }  
  125.     if (avcodec_open2(pCodecCtx, pCodec,NULL) < 0){  
  126.         printf(“Failed to open encoder!\n”);  
  127.         return -1;  
  128.     }  
  129.     pFrame = av_frame_alloc();  
  130.     pFrame->nb_samples= pCodecCtx->frame_size;  
  131.     pFrame->format= pCodecCtx->sample_fmt;  
  132.       
  133.     size = av_samples_get_buffer_size(NULL, pCodecCtx->channels,pCodecCtx->frame_size,pCodecCtx->sample_fmt, 1);  
  134.     frame_buf = (uint8_t *)av_malloc(size);  
  135.     avcodec_fill_audio_frame(pFrame, pCodecCtx->channels, pCodecCtx->sample_fmt,(const uint8_t*)frame_buf, size, 1);  
  136.       
  137.     //Write Header  
  138.     avformat_write_header(pFormatCtx,NULL);  
  139.   
  140.     av_new_packet(&pkt,size);  
  141.   
  142.     for (i=0; i<framenum; i++){  
  143.         //Read PCM  
  144.         if (fread(frame_buf, 1, size, in_file) <= 0){  
  145.             printf(“Failed to read raw data! \n”);  
  146.             return -1;  
  147.         }else if(feof(in_file)){  
  148.             break;  
  149.         }  
  150.         pFrame->data[0] = frame_buf;  //PCM Data  
  151.   
  152.         pFrame->pts=i*100;  
  153.         got_frame=0;  
  154.         //Encode  
  155.         ret = avcodec_encode_audio2(pCodecCtx, &pkt,pFrame, &got_frame);  
  156.         if(ret < 0){  
  157.             printf(“Failed to encode!\n”);  
  158.             return -1;  
  159.         }  
  160.         if (got_frame==1){  
  161.             printf(“Succeed to encode 1 frame! \tsize:%5d\n”,pkt.size);  
  162.             pkt.stream_index = audio_st->index;  
  163.             ret = av_write_frame(pFormatCtx, &pkt);  
  164.             av_free_packet(&pkt);  
  165.         }  
  166.     }  
  167.       
  168.     //Flush Encoder  
  169.     ret = flush_encoder(pFormatCtx,0);  
  170.     if (ret < 0) {  
  171.         printf(“Flushing encoder failed\n”);  
  172.         return -1;  
  173.     }  
  174.   
  175.     //Write Trailer  
  176.     av_write_trailer(pFormatCtx);  
  177.   
  178.     //Clean  
  179.     if (audio_st){  
  180.         avcodec_close(audio_st->codec);  
  181.         av_free(pFrame);  
  182.         av_free(frame_buf);  
  183.     }  
  184.     avio_close(pFormatCtx->pb);  
  185.     avformat_free_context(pFormatCtx);  
  186.   
  187.     fclose(in_file);  
  188.   
  189.     return 0;  
  190. }  

雷霄骅 (Lei Xiaohua)
leixiaohua1020@126.com
http://blog.csdn.net/leixiaohua1020

版权声明:本文为博主原创文章,未经博主允许不得转载。

结果

程序运行完成后,会将一个PCM采样数据文件(*.pcm)编码为AAC码流文件(*.aac)。

本文地址:https://blog.csdn.net/ZH952016281/article/details/52768415

(0)
上一篇 2022年3月23日
下一篇 2022年3月23日

相关推荐