PCM編碼為AAC
????av_register_all():注冊FFmpeg所有編解碼器。
? ? avformat_alloc_output_context2():初始化輸出碼流的AVFormatContext。
? ? avio_open():打開輸出文件。
? ? avcodec_find_encoder_by_name():查找編碼器。
?? av_new_stream():創(chuàng)建輸出碼流的AVStream。
? ? avcodec_open2():打開編碼器。
? ? avformat_write_header():寫文件頭。
? ? avcodec_send_frame():編碼一幀視頻。即將AVFrame編碼為AVPacket
?? avcodec_receive_packet():接收編碼后的數(shù)據(jù)
? ? av_write_frame():將編碼后的視頻碼流寫入文件。
? ? av_write_trailer():寫文件尾。
#include <stdio.h>
extern "C" {
#include "libavcodec/avcodec.h"
#include "libavformat/avformat.h"
#include <libswresample/swresample.h>
//引入時間
#include "libavutil/time.h"
#include "libavutil/imgutils.h"
}
int main(int argc, char* argv[])
{
??? AVFormatContext *pFormatCtx = NULL;
??? AVOutputFormat *outfmt = NULL;
??? AVStream *audio_stream = NULL;
??? AVCodecContext *pCodecCtx = NULL;
??? AVCodec *pCodec = NULL;
??? uint8_t *frame_buf = NULL;
??? AVFrame *frame = NULL;
??? int size = 0;
??? int ret = 0;
??? FILE *in_file = fopen("ws.pcm", "rb");??? //音頻PCM采樣數(shù)據(jù)
??? const char *out_file = "ws.aac";??????????????????? //輸出文件路徑
??? AVSampleFormat inSampleFmt = AV_SAMPLE_FMT_S16;
??? AVSampleFormat outSampleFmt = AV_SAMPLE_FMT_S16;
??? const int sampleRate = 44100;
??? const int channels = 2;
??? const int sampleByte = 2;
??? int readSize = 0;
??? av_register_all();
??? avformat_alloc_output_context2(&pFormatCtx, NULL, NULL, out_file);
??? outfmt = pFormatCtx->oformat;
??? //open out file
??? if (avio_open(&pFormatCtx->pb, out_file, AVIO_FLAG_READ_WRITE) < 0) {
??????? av_log(NULL, AV_LOG_ERROR, "%s", "輸出文件打開失??!\n");
??????? return -1;
??? }
??? pCodec = avcodec_find_encoder_by_name("libfdk_aac");
??? //pCodec = avcodec_find_encoder(AV_CODEC_ID_AAC);
??? if (!pCodec)
??? {
??????? av_log(NULL, AV_LOG_ERROR, "%s", "沒有找到合適的編碼器!");
??????? return -1;
??? }
??? //創(chuàng)建一路音頻流
??? audio_stream = avformat_new_stream(pFormatCtx, pCodec);
??? if (audio_stream == NULL)
??? {
??????? av_log(NULL, AV_LOG_ERROR, "%s", "avformat_new_stream error");
??????? return -1;
??? }
??? //設(shè)置編碼器參數(shù)
??? pCodecCtx = audio_stream->codec;
??? pCodecCtx->codec_id = outfmt->audio_codec;
??? pCodecCtx->codec_type = AVMEDIA_TYPE_AUDIO;
??? pCodecCtx->sample_fmt = outSampleFmt;
??? pCodecCtx->sample_rate = sampleRate;
??? pCodecCtx->channel_layout = AV_CH_LAYOUT_STEREO;
??? pCodecCtx->channels = av_get_channel_layout_nb_channels(pCodecCtx->channel_layout);
??? pCodecCtx->bit_rate = 64000;
??? //輸出格式信息
??? av_dump_format(pFormatCtx, 0, out_file, 1);
??? //2 音頻重采樣 上下文初始化
??? SwrContext *asc = NULL;
??? asc = swr_alloc_set_opts(asc,
???????????????????????????? av_get_default_channel_layout(channels), outSampleFmt,
???????????????????????????? sampleRate,//輸出格式
???????????????????????????? av_get_default_channel_layout(channels), inSampleFmt, sampleRate, 0,
???????????????????????????? 0);//輸入格式
??? if (!asc)
??? {
??????? av_log(NULL, AV_LOG_ERROR, "%s", "swr_alloc_set_opts failed!");
??????? return -1;
??? }
??? ret = swr_init(asc);
??? if (ret < 0)
??? {
??????? return ret;
??? }
??? //打開編碼器
??? if (avcodec_open2(pCodecCtx, pCodec, NULL) < 0)
??? {
??????? av_log(NULL, AV_LOG_ERROR, "%s", "編碼器打開失?。n");
??????? return -1;
??? }
??? frame = av_frame_alloc();
??? frame->nb_samples = pCodecCtx->frame_size;
??? frame->format = pCodecCtx->sample_fmt;
??? av_log(NULL, AV_LOG_DEBUG, "sample_rate:%d,frame_size:%d, channels:%d", sampleRate,
?????????? frame->nb_samples, frame->channels);
??? //編碼每一幀的字節(jié)數(shù)
??? size = av_samples_get_buffer_size(NULL, pCodecCtx->channels, pCodecCtx->frame_size,
????????????????????????????????????? pCodecCtx->sample_fmt, 1);
??? frame_buf = (uint8_t *) av_malloc(size);
??? //一次讀取一幀音頻的字節(jié)數(shù)
??? readSize = frame->nb_samples * channels * sampleByte;
??? char *buf = new char[readSize];
??? avcodec_fill_audio_frame(frame, pCodecCtx->channels, pCodecCtx->sample_fmt,
???????????????????????????? (const uint8_t *) frame_buf, size, 1);
??? audio_stream->codecpar->codec_tag = 0;
??? audio_stream->time_base = audio_stream->codec->time_base;
??? //從編碼器復制參數(shù)
??? avcodec_parameters_from_context(audio_stream->codecpar, pCodecCtx);
??? //寫文件頭
??? avformat_write_header(pFormatCtx, NULL);
??? AVPacket pkt;
??? av_new_packet(&pkt, size);
??? int apts = 0;
??? for (int i = 0;; i++)
??? {
??????? //讀入PCM
??????? if (fread(buf, 1, readSize, in_file) < 0)
??????? {
??????????? printf("文件讀取錯誤!\n");
??????????? return -1;
??????? }
??????? else if (feof(in_file))
??????? {
??????????? break;
??????? }
??????? frame->pts = apts;
??????? //計算pts
??????? AVRational av;
??????? av.num = 1;
??????? av.den = sampleRate;
??????? apts += av_rescale_q(frame->nb_samples, av, pCodecCtx->time_base);
??????? //重采樣源數(shù)據(jù)
??????? const uint8_t *indata[AV_NUM_DATA_POINTERS] = {0};
??????? indata[0] = (uint8_t *) buf;
??????? ret = swr_convert(asc, frame->data, frame->nb_samples, //輸出參數(shù),輸出存儲地址和樣本數(shù)量
????????????????????????? indata, frame->nb_samples
????????????????????????? );
??????? if (ret < 0)
??????? {
??????????? av_log(NULL, AV_LOG_ERROR, "swr_convert error");
??????????? return ret;
??????? }
??????? //編碼
??????? ret = avcodec_send_frame(pCodecCtx, frame);
??????? if (ret < 0)
??????? {
??????????? av_log(NULL, AV_LOG_ERROR, "avcodec_send_frame error\n");
??????????? return ret;
??????? }
??????? //接受編碼后的數(shù)據(jù)
??????? ret = avcodec_receive_packet(pCodecCtx, &pkt);
??????? if (ret < 0)
??????? {
??????????? av_log(NULL, AV_LOG_ERROR, "avcodec_receive_packet!error \n");
??????????? continue;
??????? }
??????? //pts dts duration轉(zhuǎn)換為以audio_stream->time_base為基準的值。
??????? pkt.stream_index = audio_stream->index;
??????? pkt.pts = av_rescale_q(pkt.pts, pCodecCtx->time_base, audio_stream->time_base);
??????? pkt.dts = av_rescale_q(pkt.dts, pCodecCtx->time_base, audio_stream->time_base);
??????? pkt.duration = av_rescale_q(pkt.duration, pCodecCtx->time_base, audio_stream->time_base);
??????? ret = av_write_frame(pFormatCtx, &pkt);
??????? if (ret < 0)
??????? {
??????????? av_log(NULL, AV_LOG_ERROR, "av_write_frame error!");
??????? }
??????? else
??????? {
??????????? av_log(NULL, AV_LOG_DEBUG, " 第%d幀 encode success", i);
??????? }
??????? av_packet_unref(&pkt);
??? }
??? //寫文件尾
??? av_write_trailer(pFormatCtx);
??? //清理
??? avcodec_close(audio_stream->codec);
??? av_free(frame);
??? av_free(frame_buf);
??? avio_close(pFormatCtx->pb);
??? avformat_free_context(pFormatCtx);
??? fclose(in_file);
??? av_log(NULL, AV_LOG_DEBUG, "finish !");
??? return 0;
}
下一篇將介紹一下 yuv文件編碼為h264文件