PCM编码为AAC
av_register_all():注册FFmpeg所有编解码器。
avformat_alloc_output_context2():初始化输出码流的AVFormatContext。
avio_open():打开输出文件。
avcodec_find_encoder_by_name():查找编码器。
av_new_stream():创建输出码流的AVStream。
avcodec_open2():打开编码器。
avformat_write_header():写文件头。
avcodec_send_frame():编码一帧视频。即将AVFrame编码为AVPacket
avcodec_receive_packet():接收编码后的数据
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采样数据
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;
}
//创建一路音频流
audio_stream = avformat_new_stream(pFormatCtx, pCodec);
if (audio_stream == NULL)
{
av_log(NULL, AV_LOG_ERROR, "%s", "avformat_new_stream error");
return -1;
}
//设置编码器参数
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);
//编码每一帧的字节数
size = av_samples_get_buffer_size(NULL, pCodecCtx->channels, pCodecCtx->frame_size,
pCodecCtx->sample_fmt, 1);
frame_buf = (uint8_t *) av_malloc(size);
//一次读取一帧音频的字节数
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;
//从编码器复制参数
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);
//重采样源数据
const uint8_t *indata[AV_NUM_DATA_POINTERS] = {0};
indata[0] = (uint8_t *) buf;
ret = swr_convert(asc, frame->data, frame->nb_samples, //输出参数,输出存储地址和样本数量
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;
}
//接受编码后的数据
ret = avcodec_receive_packet(pCodecCtx, &pkt);
if (ret < 0)
{
av_log(NULL, AV_LOG_ERROR, "avcodec_receive_packet!error \n");
continue;
}
//pts dts duration转换为以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文件
网友评论