ffmpeg库pcm到ac3编码

时间:2014-07-16 14:36:27

标签: audio encoding ffmpeg directshow

我是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_fill_audio_frame调用中将format参数更改为AV_SAMPLE_FMT_FLTP,那么它不会崩溃,但它只编码1帧数据,我在下一帧得到错误-22。第一次avcode_encode_audio2调用后,pPacket.size参数为1792(7 * 256)。

由于我是ffmpeg的新手,我确信它可能是一件非常直接的事情,我错过了或者我误解了,而且我对输入样品的参数和传出的位置感到困惑样品

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

戴夫。

0 个答案:

没有答案