为了适应上述要求,我们需要进行以下改动:
- 使用 newer encoding API,即
avcodec_send_frame和avcodec_receive_packet,而不是avcodec_encode_video2和avcodec_encode_audio4。
- 由于 G.711a 要求的采样率为 8KHz,而输入的 AAC 音频采样率为 44.1KHz,我们需要使用
SwrContext 进行重采样。
这里是改进后的代码:
#include <libavcodec/avcodec.h>
#include <libavutil/imgutils.h>
#include <libavutil/opt.h>
#include <libswresample/swresample.h>
int main() {
// 初始化解码器和编码器
AVCodec *h265D_codec = avcodec_find_decoder(AV_CODEC_ID_HEVC);
AVCodec *aacD_codec = avcodec_find_decoder(AV_CODEC_ID_AAC);
AVCodec *h264E_codec = avcodec_find_encoder(AV_CODEC_ID_H264);
AVCodec *g711aE_codec = avcodec_find_encoder(AV_CODEC_ID_PCM_ALAW);
// 分配解码器和编码器的数据结构
AVCodecContext *h265D_codec_ctx = avcodec_alloc_context3(h265D_codec);
AVCodecContext *aacD_codec_ctx = avcodec_alloc_context3(aacD_codec);
AVCodecContext *h264E_codec_ctx = avcodec_alloc_context3(h264E_codec);
AVCodecContext *g711aE_codec_ctx = avcodec_alloc_context3(g711aE_codec);
// 初始化解码器和编码器
avcodec_open2(h265D_codec_ctx, h265D_codec, NULL);
avcodec_open2(aacD_codec_ctx, aacD_codec, NULL);
avcodec_open2(h264E_codec_ctx, h264E_codec, NULL);
avcodec_open2(g711aE_codec_ctx, g711aE_codec, NULL);
// 设置编码器参数
h264E_codec_ctx->width = h265D_codec_ctx->width;
h264E_codec_ctx->height = h265D_codec_ctx->height;
h264E_codec_ctx->pix_fmt = h265D_codec_ctx->pix_fmt;
h264E_codec_ctx->time_base = h265D_codec_ctx->time_base;
h264E_codec_ctx->bit_rate = h265D_codec_ctx->bit_rate;
h264E_codec_ctx->gop_size = h265D_codec_ctx->gop_size;
g711aE_codec_ctx->sample_rate = 8000;
g711aE_codec_ctx->channels = aacD_codec_ctx->channels;
g711aE_codec_ctx->channel_layout = aacD_codec_ctx->channel_layout;
g711aE_codec_ctx->sample_fmt = AV_SAMPLE_FMT_S16;
// 更新编解码器设置
avcodec_parameters_from_context(h264E_codec_ctx->priv_data, h264E_codec_ctx);
avcodec_parameters_from_context(g711aE_codec_ctx->priv_data, g711aE_codec_ctx);
// 创建输入的 H.265 数据包
AVPacket h265_pkt;
av_init_packet(&h265_pkt);
h265_pkt.data = in_h265_buff;
h265_pkt.size = in_h265_size;
// 创建输入的 AAC 数据包
AVPacket aac_pkt;
av_init_packet(&aac_pkt);
aac_pkt.data = in_aac_buff;
aac_pkt.size = in_aac_size;
// 解码输入数据,生成解码后的帧
AVFrame *decoded_frame = av_frame_alloc();
avcodec_send_packet(h265D_codec_ctx, &h265_pkt);
avcodec_receive_frame(h265D_codec_ctx, decoded_frame);
// 编码解码后的帧
avcodec_send_frame(h264E_codec_ctx, decoded_frame);
AVPacket out_h264_pkt;
av_init_packet(&out_h264_pkt);
avcodec_receive_packet(h264E_codec_ctx, &out_h264_pkt);
// 解码输入音频数据,生成解码后的帧
AVFrame *decoded_audio_frame = av_frame_alloc();
avcodec_send_packet(aacD_codec_ctx, &aac_pkt);
avcodec_receive_frame(aacD_codec_ctx, decoded_audio_frame);
// 配置音频重采样
SwrContext *swr_ctx = swr_alloc_set_opts(NULL,
g711aE_codec_ctx->channel_layout, g711aE_codec_ctx->sample_fmt, g711aE_codec_ctx->sample_rate,
aacD_codec_ctx->channel_layout, aacD_codec_ctx->sample_fmt, aacD_codec_ctx->sample_rate, 0, NULL);
swr_init(swr_ctx);
// 重采样音频并存储到新的帧中
AVFrame *resampled_audio_frame = av_frame_alloc();
resampled_audio_frame->channel_layout = g711aE_codec_ctx->channel_layout;
resampled_audio_frame->sample_rate = g711aE_codec_ctx->sample_rate;
resampled_audio_frame->format = g711aE_codec_ctx->sample_fmt;
resampled_audio_frame->channels = g711aE_codec_ctx->channels;
int samples_per_channel = swr_convert(
swr_ctx,
resampled_audio_frame->data,
decoded_audio_frame->nb_samples,
(const uint8_t**)decoded_audio_frame->data,
decoded_audio_frame->nb_samples
);
resampled_audio_frame->nb_samples = samples_per_channel;
// 编码解码后的音频帧
avcodec_send_frame(g711aE_codec_ctx, resampled_audio_frame);
AVPacket out_g711a_pkt;
av_init_packet(&out_g711a_pkt);
avcodec_receive_packet(g711aE_codec_ctx, &out_g711a_pkt);
// 现在 out_h264_pkt 和 out_g711a_pkt 包含编码后的数据
// 释放
av_packet_unref(&h265_pkt);
av_packet_unref(&aac_pkt);
av_packet_unref(&out_h264_pkt);
av_packet_unref(&out_g711a_pkt);
av_frame_free(&decoded_frame);
av_frame_free(&decoded_audio_frame);
av_frame_free(&resampled_audio_frame);
avcodec_close(h265D_codec_ctx);
avcodec_close(aacD_codec_ctx);
avcodec_close(h264E_codec_ctx);
avcodec_close(g711aE_codec_ctx);
avcodec_free_context(&h265D_codec_ctx);
avcodec_free_context(&aacD_codec_ctx);
avcodec_free_context(&h264E_codec_ctx);
avcodec_free_context(&g711aE_codec_ctx);
swr_close(swr_ctx);
swr_free(&swr_ctx);
return 0;
}
在此示例中,我们使用了较新的编码 API :avcodec_send_frame 和 avcodec_receive_packet,并添加了重采样处理以将输入的 44.1KHz AAC 音频转换成 8KHz G.711a 音频。
为了进行重采样,我们创建了一个 SwrContext,并使用 swr_alloc_set_opts 设置重采样参数。 然后通过 swr_init 初始化重采样上下文。我们使用 swr_convert 函数将解码后的音频帧数据转换为 8KHz 并将结果存储到 resampled_audio_frame 中。最后我们将它发送到 G.711a 编码器并接收编码后的数据包。所有分配的内存在结束时均得到释放。