Audio ffmpeg库pcm到ac3编码

Audio ffmpeg库pcm到ac3编码,audio,encoding,ffmpeg,directshow,Audio,Encoding,Ffmpeg,Directshow,我是ffmpeg库的新手,正在开发一个定制的directshow过滤器。我决定使用ffmpeg库对我需要实现的内容进行编码。我对一些参数和ffmpeg所期望的正确值有点困惑 我目前正在处理定制过滤器的ac3部分。 我在ffmpeg文档中查看了MP2编码的音频编码示例,我理解了它,但我不理解如何使其适应我的特定需求 传入样本为每秒48K个样本,每个样本16位,并且是立体声交错的。上游过滤器以25fps的速度提供给我,因此我得到了每个音频帧1920字节的传入“音频样本包”。我想将这些数据编码成一个a

我是ffmpeg库的新手,正在开发一个定制的directshow过滤器。我决定使用ffmpeg库对我需要实现的内容进行编码。我对一些参数和ffmpeg所期望的正确值有点困惑

我目前正在处理定制过滤器的ac3部分。 我在ffmpeg文档中查看了MP2编码的音频编码示例,我理解了它,但我不理解如何使其适应我的特定需求

传入样本为每秒48K个样本,每个样本16位,并且是立体声交错的。上游过滤器以25fps的速度提供给我,因此我得到了每个音频帧1920字节的传入“音频样本包”。我想将这些数据编码成一个ac3数据包,并将其传递给下一个我将自己进行的进程

但我不确定以下代码中每个组件的正确参数

到目前为止我掌握的代码。在关键位置的评论中有几个问题

AVCodec*         g_pCodec = nullptr;
AVCodecContext*  g_pContext = nullptr;
AVFrame*         g_pFrame = nullptr;
AVPacket         g_pPacket;
LPVOID           g_pInSampleBuffer;

avcodec_register_all();
g_pCodec = avcodec_find_encoder(CODEC_ID_AC3);

// What am I'm describing here? the incoming sample params or the outgoing sample params?
// An educated guess is the outgoing sample params
g_pContext = avcodec_alloc_context3(g_pCodec);
g_pContext->bit_rate = 448000;
g_pContext->sample_rate = 48000;
g_pContext->channels = 2;
g_pContext->sample_fmt = AV_SAMPLE_FMT_FLTP;
g_pContext->channel_layout = AV_CH_LAYOUT_STEREO;

// And this is the incoming sample params?
g_pFrame = av_frame_alloc();
g_pFrame->nb_samples = 1920; ?? What figure is the codec expecting me to give it here? 1920 / bytes_per_sample? 
g_pFrame->format = AV_SAMPLE_FMT_S16;
g_pFrame->channel_layout = AV_CH_LAYOUT_STEREO;

// I assume this going to give me the size of a buffer that I use to fill with my incoming samples? I get a dwSize of 15360 but my samples are only coming in at 1920, does this matter?
dwSize = av_samples_get_buffer_size(nullptr,2,1920,AV_SAMPLE_FMT_S16,0);

// do I need to use av_malloc and copy my samples into g_pInSampleBuffer or can I supply the address of my own buffer ( created outside of the libav framework ) ?
g_pInSampleBuffer = (LPVOID)av_malloc(dwSize)
avcodec_fill_audio_frame(g_pFrame,2,AV_SAMPLE_FMT_S16,(const uint8_t*)g_pInSampleBuffer,*dwSize,0);

// Encoding loop - samples are given to me through a directshow interface - DSInbuffer is the buffer containing the incoming samples
av_init_packet(&g_pPacket);
g_pPacket.data = nullptr;
g_pPacket.size = 0;

int gotpacket = 0;
int ok = avcodec_encode_audio2(g_pContext,&g_pPacket,g_pFrame,&gotpacket);
if((ok == 0) && gotpacket){
   // I then copy from g_pPacket.data an amount of g_pPacket.size bytes into another directshow interface buffer that sends the encoded sample downstream.

    av_free_packet(&g_pPacket);
}
目前,它将在avcodec_encode_audio2调用时崩溃。如果我在avcodec\u fill\u audio\u frame调用中将format参数更改为AV\u SAMPLE\u FMT\u FLTP,那么它不会崩溃,但它只编码1帧数据,在下一帧中我得到错误-22。在第一次avcode_encode_AUDIO 2调用之后,pPacket.size参数为1792 7*256

由于我是ffmpeg新手,我确信这可能是我错过的或误解的一些非常直接的东西,我对传入样本和传出样本的参数在哪里感到困惑

这显然是从我创建的主函数中提取的,我已经手动输入到论坛中。如果这里有拼写错误,则编译并运行原始代码

戴夫