请教:ffplay里面的一段代码
ffplay里面有一段代码,不是很明白,代码如下:
static int audio_decode_frame(VideoState *is, uint8_t *audio_buf, int buf_size, double *pts_ptr)
{
AVPacket *pkt = &is->audio_pkt;
int n, len1, data_size;
double pts;
for(;;) {
/* NOTE: the audio packet can contain several frames */
while (is->audio_pkt_size > 0) {
data_size = buf_size;
len1 = avcodec_decode_audio2(is->audio_st->codec,
(int16_t *)audio_buf, &data_size,
is->audio_pkt_data, is->audio_pkt_size);
if (len1 < 0) {
/* if error, we skip the frame */
is->audio_pkt_size = 0;
break;
}
is->audio_pkt_data += len1;
is->audio_pkt_size -= len1;
if (data_size <= 0)
continue; // ??? 这里为什么用continue
/* if no pts, then compute it */
pts = is->audio_clock;
*pts_ptr = pts;
n = 2 * is->audio_st->codec->channels;
is->audio_clock += (double)data_size /
(double)(n * is->audio_st->codec->sample_rate);
#if defined(DEBUG_SYNC)
{
static double last_clock;
printf("audio: delay=%0.3f clock=%0.3f pts=%0.3f\n",
is->audio_clock - last_clock,
is->audio_clock, pts);
last_clock = is->audio_clock;
}
#endif
return data_size;
}
/* free the current packet */
if (pkt->data)
av_free_packet(pkt);
if (is->paused || is->audioq.abort_request) {
return -1;
}
/* read next packet */
if (packet_queue_get(&is->audioq, pkt, 1) < 0)
return -1;
if(pkt->data == flush_pkt.data){
avcodec_flush_buffers(is->audio_st->codec);
continue;
}
is->audio_pkt_data = pkt->data;
is->audio_pkt_size = pkt->size;
/* if update the audio clock with the pts */
if (pkt->pts != AV_NOPTS_VALUE) {
is->audio_clock = av_q2d(is->audio_st->time_base)*pkt->pts;
}
}
}
if (len1 < 0) {
/* if error, we skip the frame */
is->audio_pkt_size = 0;
break;
}
is->audio_pkt_data += len1;
is->audio_pkt_size -= len1;
if (data_size <= 0)
continue;
[解决办法]
data_size是解码器输出数据长度,若为0,说明解码器
无数据输出,那就继续给解码器输入数据。