2,553
社区成员




int audio_decode_frame(AVCodecContext *aCodecCtx, uint8_t *audio_buf, int buf_size)
{
static AVPacket pkt;
static AVPacket pkt1;
uint8_t *out[] = { audio_buf };
int len1, data_size;
int got_frame = 0;
AVFrame *pAudioFrame = av_frame_alloc();
av_frame_unref(pAudioFrame);
for(;;)
{
while(pkt1.size > 0)
{
data_size = buf_size;
len1 = avcodec_decode_audio4(aCodecCtx, pAudioFrame, &got_frame,
&pkt1);
if(len1 < 0)
{
/* if error, skip frame */
pkt1.size = 0;
break;
}
pkt1.data += len1;
pkt1.size -= len1;
if(got_frame == 0)
{
/* No data yet, get more frames */
continue;
}
else
{
SwrContext *swrContext = swr_alloc();
swr_alloc_set_opts(swrContext, aCodecCtx->channel_layout, AV_SAMPLE_FMT_S16,
aCodecCtx->sample_rate, aCodecCtx->channel_layout, aCodecCtx->sample_fmt,
aCodecCtx->sample_rate, 0, NULL);
swr_init(swrContext);
swr_convert(swrContext, out, buf_size/aCodecCtx->channels/av_get_bytes_per_sample(AV_SAMPLE_FMT_S16),
(const uint8_t **)pAudioFrame->data,
pAudioFrame->linesize[0] / aCodecCtx->channels / av_get_bytes_per_sample((AVSampleFormat)pAudioFrame->format));
data_size = av_samples_get_buffer_size(NULL, aCodecCtx->channels, pAudioFrame->nb_samples,
AV_SAMPLE_FMT_S16, 0);
av_free(pAudioFrame);
av_free_packet(&pkt);
swr_free(&swrContext);
}
/* We have data, return it and come back for more later */
return data_size;
}
if(pkt.data)
{
av_free_packet(&pkt);
}
if(quit)
{
return -1;
}
if(packet_queue_get(&audioq, &pkt, 1) < 0)
{
return -1;
}
pkt1.data = pkt.data;
pkt1.size = pkt.size;
}
}