我正在尝试使用xaudio2来播放libav的音频。我使用的xaudio2代码与使用avcodec_decode_audio2的旧的ffmpeg一起工作,但是对于avcodec_decode_audio4来说,这已经被废弃了。我试过跟随各种libav例子,但似乎无法播放音频。视频播放得很好(更确切地说,它现在播放得很快,因为我还没有编码任何同步代码)。
首先音频进入,没有错误,视频进入,然后分组:
while (1) {
//is this packet from the video or audio stream?
if (packet.stream_index == player.v_id) {
add_video_to_queue(&packet);
} else if (packet.stream_index == player.a_id) {
add_sound_to_queue(&packet);
} else {
av_free_packet(&packet);
}
}然后在add_sound_to_queue中:
int add_sound_to_queue(AVPacket * packet) {
AVFrame *decoded_frame = NULL;
int done = AVCODEC_MAX_AUDIO_FRAME_SIZE;
int got_frame = 0;
if (!decoded_frame) {
if (!(decoded_frame = avcodec_alloc_frame())) {
printf("[ADD_SOUND_TO_QUEUE] Out of memory\n");
return -1;
}
} else {
avcodec_get_frame_defaults(decoded_frame);
}
if (avcodec_decode_audio4(player.av_acodecctx, decoded_frame, &got_frame, packet) < 0) {
printf("[ADD_SOUND_TO_QUEUE] Error in decoding audio\n");
av_free_packet(packet);
//continue;
return -1;
}
if (got_frame) {
int data_size;
if (packet->size > done) {
data_size = done;
} else {
data_size = packet->size;
}
BYTE * snd = (BYTE *)malloc( data_size * sizeof(BYTE));
XMemCpy(snd,
AudioBytes,
data_size * sizeof(BYTE)
);
XMemSet(&g_SoundBuffer,0,sizeof(XAUDIO2_BUFFER));
g_SoundBuffer.AudioBytes = data_size;
g_SoundBuffer.pAudioData = snd;
g_SoundBuffer.pContext = (VOID*)snd;
XAUDIO2_VOICE_STATE state;
while( g_pSourceVoice->GetState( &state ), state.BuffersQueued > 60 ) {
WaitForSingleObject( XAudio2_Notifier.hBufferEndEvent, INFINITE );
}
g_pSourceVoice->SubmitSourceBuffer( &g_SoundBuffer );
}
return 0;
}我似乎找不出这个问题,我在init中添加了错误消息,打开视频,编解码器处理等等。正如前面提到的,xaudio2代码正在使用一个旧的ffmpeg,所以我可能错过了avcodec_decode_audio4的一些东西吗?
如果这段代码不够,我可以把所有的代码都贴出来,我认为问题就在代码中:
发布于 2013-08-25 09:23:33
我没看到你解码后在任何地方访问decoded_frame。否则,你希望怎样才能把数据拿出来呢?
BYTE * snd = (BYTE *)malloc( data_size * sizeof(BYTE));这看起来也很可疑,因为data_size是从数据包大小派生出来的。分组大小是压缩数据的大小,它与解码的PCM帧的大小没有什么关系。
解码后的数据位于decoded_frame->extended_data中,这是指向数据平面的指针数组,有关详细信息,请参阅这里。解码数据的大小由decoded_frame->nb_samples确定。注意,在最近的Libav版本中,许多解码器返回平面音频,因此不同的通道存在于不同的数据缓冲区中。对于许多用例,您需要将其转换为交错格式,其中只有一个带所有通道的缓冲区。为此请使用利巴夫样。
https://stackoverflow.com/questions/18423324
复制相似问题