我想使用函数更新 AV 音频编码器avcodec_encode_audio (deprecated)
,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;