version
#define LIBSWRESAMPLE_VERSION_MAJOR 2
#define LIBSWRESAMPLE_VERSION_MINOR 9
#define LIBSWRESAMPLE_VERSION_MICRO 100
#define LIBAVCODEC_VERSION_MINOR 31
#define LIBAVCODEC_VERSION_MICRO 102
code
void CFfmpegOps::EncodePCMToAAC(const char *pcm_file, const char *aac_file)
{
AVFormatContext *fmt_ctx = nullptr;
const AVOutputFormat *out_fmt = nullptr;
int ret = -1;
AVStream *avstream = nullptr;
const AVCodec *codec = nullptr;
AVCodecContext *codec_ctx = nullptr;
AVFrame *avframe = nullptr; // pcm file对应的avframe
AVFrame *codec_avframe = nullptr; // 编码器接收的avframe
AVPacket *avpacket = nullptr;
int frame_data_bytes = 0;
FILE *in_fp = nullptr;
size_t n = 0;
int64_t pts = 0;
struct SwrContext *swr_ctx = nullptr; // 音频帧转换器
AVChannelLayout src_channel_layout;
AVChannelLayout dest_channel_layout;
codec = avcodec_find_encoder(AV_CODEC_ID_AAC);
if (!codec)
{
printf("avcodec_find_encoder error\n");
goto END;
}
codec_ctx = avcodec_alloc_context3(codec);
if (!codec_ctx)
{
printf("avcodec_alloc_context3 error\n");
goto END;
}
codec_ctx->sample_fmt = AV_SAMPLE_FMT_FLTP; // 编码器的接受的采样格式
codec_ctx->sample_rate = 44100;
codec_ctx->channel_layout = AV_CH_LAYOUT_STEREO;
codec_ctx->channels = av_get_channel_layout_nb_channels(codec_ctx->channel_layout);
codec_ctx->bit_rate = 128000;
codec_ctx->time_base.num = 1;
codec_ctx->time_base.den = codec_ctx->sample_rate;
ret = avcodec_open2(codec_ctx, codec, nullptr);
if (ret < 0)
{
printf("avcodec_open2 error(%s)\n", GetFfmpegERR(ret));
goto END;
}
printf("codec_ctx->frame_size:%d\n", codec_ctx->frame_size);
codec_avframe = av_frame_alloc();
if (!codec_avframe)
{
printf("av_frame_alloc error\n");
goto END;
}
codec_avframe->format = codec_ctx->sample_fmt; // pcm的采样格式
codec_avframe->sample_rate = codec_c