Ffmpeg SIGABRT:中止尝试将PCM编码为AAC

Ffmpeg SIGABRT:中止尝试将PCM编码为AAC,ffmpeg,pcm,aac,cgo,libav,Ffmpeg,Pcm,Aac,Cgo,Libav,我正在尝试将传入的原始PCM音频数据编码到AAC编码的音频文件中。SIGABRT在点击avcodec\u encode\u audio2调用时发生以下崩溃: aac_encoding.c #include <stdio.h> #include <libavcodec/avcodec.h> #include <libavformat/avformat.h> #include <libswresample/swresample.h> typedef

我正在尝试将传入的原始PCM音频数据编码到AAC编码的音频文件中。SIGABRT在点击
avcodec\u encode\u audio2
调用时发生以下崩溃:

aac_encoding.c

#include <stdio.h>
#include <libavcodec/avcodec.h>
#include <libavformat/avformat.h>
#include <libswresample/swresample.h>

typedef struct AACEncoder {
    AVFormatContext* pFormatCtx;
    AVStream* audio_st;
    AVCodecContext* pCodecCtx;
    AVFrame* pFrame;
    AVPacket* pkt;
    uint8_t* frame_buf;
} AACEncoder;

AACEncoder *openEncoder(char* out_file) {
    AACEncoder* encoder = (AACEncoder*)malloc(sizeof(AACEncoder*));

    av_register_all();

    AVFormatContext* pFormatCtx = avformat_alloc_context();
    encoder->pFormatCtx = pFormatCtx;

    AVOutputFormat* outFormat = av_guess_format(NULL, out_file, NULL);
    pFormatCtx->oformat = outFormat;

    if (avio_open(&pFormatCtx->pb, out_file, AVIO_FLAG_READ_WRITE) < 0) {
        printf("Failed to open output file!\n");
        return NULL;
    }

    AVStream* audio_st = avformat_new_stream(pFormatCtx, 0);
    if (audio_st==NULL){
        return NULL;
    }
    encoder->audio_st;

    AVCodecContext* pCodecCtx = audio_st->codec;
    encoder->pCodecCtx = pCodecCtx;

    pCodecCtx->codec_id = outFormat->audio_codec;
    pCodecCtx->codec_type = AVMEDIA_TYPE_AUDIO;
    pCodecCtx->sample_fmt = AV_SAMPLE_FMT_FLTP;
    pCodecCtx->sample_rate= 48000;
    pCodecCtx->channel_layout = AV_CH_LAYOUT_MONO;
    pCodecCtx->channels = av_get_channel_layout_nb_channels(pCodecCtx->channel_layout);
    pCodecCtx->bit_rate = 64000;

    av_dump_format(pFormatCtx, 0, out_file, 1);

    AVCodec* pCodec = avcodec_find_encoder(pCodecCtx->codec_id);
    if (!pCodec){
        printf("Can not find encoder!\n");
        return NULL;
    }

    if (avcodec_open2(pCodecCtx, pCodec,NULL) < 0){
        printf("Failed to open encoder!\n");
        return NULL;
    }

    AVFrame* pFrame = av_frame_alloc();
    encoder->pFrame = pFrame;

    pFrame->nb_samples= pCodecCtx->frame_size;
    pFrame->format= pCodecCtx->sample_fmt;

    int size = av_samples_get_buffer_size(NULL, pCodecCtx->channels,pCodecCtx->frame_size,pCodecCtx->sample_fmt, 1);
    uint8_t* frame_buf = (uint8_t *)av_malloc(size);
    encoder->frame_buf = frame_buf;

    avcodec_fill_audio_frame(pFrame, pCodecCtx->channels, pCodecCtx->sample_fmt,(const uint8_t*)frame_buf, size, 1);

    //Write Header
    avformat_write_header(pFormatCtx,NULL);

    AVPacket pkt;
    encoder->pkt = &pkt;

    av_new_packet(&pkt,size);

    return encoder;
}

int writePCM(AACEncoder* encoder, int16_t* pcmData, size_t pcmSize) {
    SwrContext* swr = swr_alloc();

    av_opt_set_int(swr, "in_channel_layout",  encoder->pCodecCtx->channel_layout, 0);
    av_opt_set_int(swr, "out_channel_layout", encoder->pCodecCtx->channel_layout,  0);
    av_opt_set_int(swr, "in_sample_rate",     encoder->pCodecCtx->sample_rate, 0);
    av_opt_set_int(swr, "out_sample_rate",    encoder->pCodecCtx->sample_rate, 0);
    av_opt_set_sample_fmt(swr, "in_sample_fmt",  AV_SAMPLE_FMT_S16, 0);
    av_opt_set_sample_fmt(swr, "out_sample_fmt", AV_SAMPLE_FMT_FLT,  0);
    swr_init(swr);

    printf("Initialized SwrContext\n");
    fflush(stdout);

    int ret = swr_convert(swr, encoder->pFrame->extended_data, pcmSize,  &pcmData,  pcmSize);
    int got_frame=0;
    if(ret < 0){
        printf("Failed to resample!\n");
        return -1;
    }

    //Encode
    ret = avcodec_encode_audio2(encoder->pCodecCtx, encoder->pkt, encoder->pFrame, &got_frame);
    printf("Encoded audio using AAC\n");
    fflush(stdout);

    swr_free(&swr);
    if(ret < 0){
        printf("Failed to encode!\n");
        return -1;
    }
    if (got_frame==1){
        printf("Succeed to encode 1 frame! \tsize:%5d\n", encoder->pkt->size);
        encoder->pkt->stream_index = encoder->audio_st->index;
        ret = av_write_frame(encoder->pFormatCtx, encoder->pkt);
        av_free_packet(encoder->pkt);
    }   
}
#包括
#包括
#包括
#包括
类型定义结构AACEncoder{
AVFormatContext*pFormatCtx;
AVStream*音频;
AVCodecContext*pCodecCtx;
AVFrame*pFrame;
AVPacket*pkt;
uint8_t*帧_buf;
}AACEncoder;
AACEncoder*openEncoder(字符*输出文件){
AACEncoder*编码器=(AACEncoder*)malloc(sizeof(AACEncoder*));
av_寄存器_all();
AVFormatContext*pFormatCtx=avformat_alloc_context();
编码器->pFormatCtx=pFormatCtx;
AVOutputFormat*OUFORMAT=av_guess_格式(NULL,out_文件,NULL);
pFormatCtx->oformat=格式外;
如果(avio_打开(&pFormatCtx->pb,输出文件,avio_标志,读写)<0){
printf(“打开输出文件失败!\n”);
返回NULL;
}
AVStream*audio\u st=avformat\u new\u流(PFORMATCTTX,0);
如果(音频_st==NULL){
返回NULL;
}
编码器->音频;
AVCodecContext*pCodecCtx=音频->编解码器;
编码器->pCodecCtx=pCodecCtx;
pCodecCtx->codec_id=outFormat->audio_codec;
pCodecCtx->codec_type=AVMEDIA_type_AUDIO;
pCodecCtx->sample\u fmt=AV\u sample\u fmt\u FLTP;
PCODECTX->采样率=48000;
PCODECTX->channel_layout=AV_Chu layout_MONO;
pCodecCtx->channels=av\u get\u channel\u layout\u nb\u channels(pCodecCtx->channel\u layout);
PCODECTX->比特率=64000;
av_转储_格式(pFormatCtx,0,out_文件,1);
AVCodec*pCodec=AVCodec\u find\u编码器(pCodecCtx->codec\u id);
如果(!pCodec){
printf(“找不到编码器!\n”);
返回NULL;
}
if(avcodec_open2(pCodecCtx,pCodec,NULL)<0){
printf(“未能打开编码器!\n”);
返回NULL;
}
AVFrame*pFrame=av_frame_alloc();
编码器->pFrame=pFrame;
pFrame->nb_samples=pCodecCtx->frame_size;
pFrame->format=pCodecCtx->sample\u fmt;
int size=av_samples_get_buffer_size(NULL,pCodecCtx->channels,pCodecCtx->frame_size,pCodecCtx->sample_fmt,1);
uint8_t*帧_buf=(uint8_t*)av_malloc(大小);
编码器->帧\u buf=帧\u buf;
avcodec_fill_audio_frame(pFrame,PCODECTX->通道,PCODECTX->sample_fmt,(const uint8_t*)帧,大小,1);
//写头
avformat_write_头(pFormatCtx,NULL);
AVPacket-pkt;
编码器->pkt=&pkt;
av_新_数据包(&pkt,大小);
返回编码器;
}
int writePCM(AACEncoder*编码器,int16_t*pcmData,size_t pcmSize){
SwrContext*swr=swr_alloc();
av_opt_set_int(swr,“输入通道布局”,编码器->PCODECTX->通道布局,0);
av_opt_set_int(swr,“输出通道布局”,编码器->PCODECTX->通道布局,0);
av_opt_set_int(swr,“输入采样率”,编码器->PCODECTX->采样率,0);
av_opt_set_int(swr,“输出采样率”,编码器->PCODECTX->采样率,0);
av_opt_set_sample_fmt(swr,“in_sample_fmt”,av_sample_fmt_S16,0);
av_opt_set_sample_fmt(swr,“out_sample_fmt”,av_sample_fmt_FLT,0);
swr_init(swr);
printf(“初始化的SwrContext\n”);
fflush(stdout);
int ret=swr_转换(swr,编码器->pFrame->扩展_数据,pcmSize,&pcmData,pcmSize);
int got_frame=0;
如果(ret<0){
printf(“重新采样失败!\n”);
返回-1;
}
//编码
ret=avcodec_encode_audio2(编码器->PCODECTX,编码器->pkt,编码器->pFrame,&got_帧);
printf(“使用AAC的编码音频\n”);
fflush(stdout);
swr_免费(&swr);
如果(ret<0){
printf(“编码失败!\n”);
返回-1;
}
if(got_frame==1){
printf(“成功编码1帧!\t大小:%5d\n”,编码器->包装->大小);
编码器->打包->流索引=编码器->音频->索引;
ret=av_写入_帧(编码器->PFormatCx,编码器->pkt);
av_免费_数据包(编码器->打包);
}   
}