FFmpeg 音频编码器新的编码功能

FFmpeg audio encoder new encode function

我想将使用函数 avcodec_encode_audio (deprecated) 的 AV 音频编码器更新为 avcodec_encode_audio2,而不修改现有编码器的结构:

 outBytes = avcodec_encode_audio(m_handle, dst, sizeBytes, (const short int*)m_samBuf);

其中:

1) m_handle AVCodecContext

2) dst, uint8_t * 目标缓冲区

3) sizeBytes,uint32_t 目标缓冲区的大小

4) m_samBuf void * 到要编码的输入数据块(转换为:const short int*)

有简单的方法吗?

我正在尝试:

int gotPack = 1;
memset (&m_Packet, 0, sizeof (m_Packet));
m_Frame = av_frame_alloc();

av_init_packet(&m_Packet);
m_Packet.data = dst;
m_Packet.size = sizeBytes;

uint8_t* buffer = (uint8_t*)m_samBuf;
m_Frame->nb_samples = m_handle->frame_size;

avcodec_fill_audio_frame(m_Frame,m_handle->channels,m_handle->sample_fmt,buffer,m_FrameSize,1);

outBytes = avcodec_encode_audio2(m_handle, &m_Packet, m_Frame, &gotPack);
char error[256];
av_strerror(outBytes,error,256);

if (outBytes<0){
    m_server->log(1,1,"Input data: %d, encode function call error: %s \n",gotPack, error);
    return AUDIOWRAPPER_ERROR;
}
av_frame_free(&m_Frame);

它编译但不编码任何东西,如果我在 mplayer 上通过管道输出流,我不会在输出中输出音频,这在升级之前就开始了。

我做错了什么?

编码器只接受两种样本格式:

AV_SAMPLE_FMT_S16,         ///< signed 16 bits
AV_SAMPLE_FMT_FLT,         ///< float

缓冲区的分配方式如下:

free(m_samBuf);
int bps = 2;
if(m_handle->codec->sample_fmts[0] == AV_SAMPLE_FMT_FLT) {
    bps = 4;
}
m_FrameSize = bps * m_handle->frame_size * m_handle->channels;
m_samBuf = malloc(m_FrameSize);
m_numSam = 0;

avcodec_fill_audio_frame 应该能让你到达那里

memset (&m_Packet, 0, sizeof (m_Packet));
memset (&m_Frame, 0, sizeof (m_Frame));

av_init_packet(&m_Packet);

m_Packet.data = dst;
m_Packet.size = sizeBytes;

m_Frame->nb_samples = //you need to get this value from somewhere, it is the number of samples (per channel) this frame represents
avcodec_fill_audio_frame(m_Frame, m_handle->channels, m_handle->sample_fmt,
        buffer,
        sizeBytes, 1);


int gotPack = 1;

avcodec_encode_audio2(m_handle, &m_Packet, &m_Frame, &gotPack);