From 6c4d9bfe5127da2a4e2afbe1d6e585ebeb7c5949 Mon Sep 17 00:00:00 2001 From: rtognimp Date: Mon, 24 Apr 2006 10:58:40 +0000 Subject: [PATCH] 4 - Implement a better way to calculate current audio pts and use it for audio decoded with ad_libvorbis, ad_ffmpeg and ad_faad. Patch by Uoti Urpala git-svn-id: svn://svn.mplayerhq.hu/mplayer/trunk@18243 b3059339-0415-0410-9bf9-f77b7e298cf2 --- libmpcodecs/ad_faad.c | 8 +++++++- libmpcodecs/ad_ffmpeg.c | 8 +++++++- libmpcodecs/ad_libvorbis.c | 8 +++++++- libmpdemux/demuxer.c | 21 +++++++++++++++++++++ libmpdemux/demuxer.h | 1 + libmpdemux/stheader.h | 2 ++ mplayer.c | 20 ++++++++++---------- 7 files changed, 55 insertions(+), 13 deletions(-) diff --git a/libmpcodecs/ad_faad.c b/libmpcodecs/ad_faad.c index b681a9c7a3..80470d4a49 100644 --- a/libmpcodecs/ad_faad.c +++ b/libmpcodecs/ad_faad.c @@ -253,8 +253,13 @@ static int decode_audio(sh_audio_t *sh,unsigned char *buf,int minlen,int maxlen) } else { // packetized (.mp4) aac stream: unsigned char* bufptr=NULL; - int buflen=ds_get_packet(sh->ds, &bufptr); + double pts; + int buflen=ds_get_packet_pts(sh->ds, &bufptr, &pts); if(buflen<=0) break; + if (pts != MP_NOPTS_VALUE) { + sh->pts = pts; + sh->pts_bytes = 0; + } faac_sample_buffer = faacDecDecode(faac_hdec, &faac_finfo, bufptr, buflen); } //for (j=0;jsamplesize*faac_finfo.samples); last_dec_len = sh->samplesize*faac_finfo.samples; len += last_dec_len; + sh->pts_bytes += last_dec_len; //printf("FAAD: buffer: %d bytes consumed: %d \n", k, faac_finfo.bytesconsumed); } } diff --git a/libmpcodecs/ad_ffmpeg.c b/libmpcodecs/ad_ffmpeg.c index 22cb72b8a5..f9fb0455a6 100644 --- a/libmpcodecs/ad_ffmpeg.c +++ b/libmpcodecs/ad_ffmpeg.c @@ -149,8 +149,13 @@ static int decode_audio(sh_audio_t *sh_audio,unsigned char *buf,int minlen,int m int y,len=-1; while(lends,&start); + double pts; + int x=ds_get_packet_pts(sh_audio->ds,&start, &pts); if(x<=0) break; // error + if (pts != MP_NOPTS_VALUE) { + sh_audio->pts = pts; + sh_audio->pts_bytes = 0; + } y=avcodec_decode_audio(sh_audio->context,(int16_t*)buf,&len2,start,x); //printf("return:%d samples_out:%d bitstream_in:%d sample_sum:%d\n", y, len2, x, len); fflush(stdout); if(y<0){ mp_msg(MSGT_DECAUDIO,MSGL_V,"lavc_audio: error\n");break; } @@ -159,6 +164,7 @@ static int decode_audio(sh_audio_t *sh_audio,unsigned char *buf,int minlen,int m //len=len2;break; if(len<0) len=len2; else len+=len2; buf+=len2; + sh_audio->pts_bytes += len2; } mp_dbg(MSGT_DECAUDIO,MSGL_DBG2,"Decoded %d -> %d \n",y,len2); } diff --git a/libmpcodecs/ad_libvorbis.c b/libmpcodecs/ad_libvorbis.c index 3c765b6acd..2d94ac4431 100644 --- a/libmpcodecs/ad_libvorbis.c +++ b/libmpcodecs/ad_libvorbis.c @@ -229,9 +229,14 @@ static int decode_audio(sh_audio_t *sh,unsigned char *buf,int minlen,int maxlen) while(len < minlen) { while((samples=vorbis_synthesis_pcmout(&ov->vd,&pcm))<=0){ ogg_packet op; + double pts; memset(&op,0,sizeof(op)); //op.b_o_s = op.e_o_s = 0; - op.bytes = ds_get_packet(sh->ds,&op.packet); + op.bytes = ds_get_packet_pts(sh->ds,&op.packet, &pts); if(op.bytes<=0) break; + if (pts != MP_NOPTS_VALUE) { + sh->pts = pts; + sh->pts_bytes = 0; + } if(vorbis_synthesis(&ov->vb,&op)==0) /* test for success! */ vorbis_synthesis_blockin(&ov->vd,&ov->vb); } @@ -303,6 +308,7 @@ static int decode_audio(sh_audio_t *sh,unsigned char *buf,int minlen,int maxlen) if(clipflag) mp_msg(MSGT_DECAUDIO,MSGL_DBG2,"Clipping in frame %ld\n",(long)(ov->vd.sequence)); len+=2*ov->vi.channels*bout; + sh->pts_bytes += 2*ov->vi.channels*bout; mp_msg(MSGT_DECAUDIO,MSGL_DBG2,"\n[decoded: %d / %d ]\n",bout,samples); samples-=bout; vorbis_synthesis_read(&ov->vd,bout); /* tell libvorbis how diff --git a/libmpdemux/demuxer.c b/libmpdemux/demuxer.c index f45d325d55..14709e9e82 100644 --- a/libmpdemux/demuxer.c +++ b/libmpdemux/demuxer.c @@ -224,6 +224,7 @@ sh_audio_t* new_sh_audio(demuxer_t *demuxer,int id){ sh->samplesize=2; sh->sample_format=AF_FORMAT_S16_NE; sh->audio_out_minsize=8192;/* default size, maybe not enough for Win32/ACM*/ + sh->pts=MP_NOPTS_VALUE; if (!demux_aid_vid_mismatch) mp_msg(MSGT_IDENTIFY, MSGL_INFO, "ID_AUDIO_ID=%d\n", id); } @@ -502,6 +503,26 @@ int ds_get_packet(demux_stream_t *ds,unsigned char **start){ } } +int ds_get_packet_pts(demux_stream_t *ds,unsigned char **start, double *pts) +{ + int len; + *pts = MP_NOPTS_VALUE; + if(ds->buffer_pos>=ds->buffer_size){ + if (!ds_fill_buffer(ds)) { + // EOF + *start = NULL; + return -1; + } + // Should use MP_NOPTS_VALUE for "unknown pts" in the packets too + if (ds->current->pts) + *pts = ds->current->pts; + } + len=ds->buffer_size-ds->buffer_pos; + *start = &ds->buffer[ds->buffer_pos]; + ds->buffer_pos+=len; + return len; +} + int ds_get_packet_sub(demux_stream_t *ds,unsigned char **start){ while(1){ int len; diff --git a/libmpdemux/demuxer.h b/libmpdemux/demuxer.h index 939a3b5577..35c3ac3982 100644 --- a/libmpdemux/demuxer.h +++ b/libmpdemux/demuxer.h @@ -284,6 +284,7 @@ inline static int demux_getc(demux_stream_t *ds){ void ds_free_packs(demux_stream_t *ds); int ds_get_packet(demux_stream_t *ds,unsigned char **start); +int ds_get_packet_pts(demux_stream_t *ds, unsigned char **start, double *pts); int ds_get_packet_sub(demux_stream_t *ds,unsigned char **start); float ds_get_next_pts(demux_stream_t *ds); diff --git a/libmpdemux/stheader.h b/libmpdemux/stheader.h index 3fcfebac7c..bc6a63cd45 100644 --- a/libmpdemux/stheader.h +++ b/libmpdemux/stheader.h @@ -47,6 +47,8 @@ typedef struct { void* context; // codec-specific stuff (usually HANDLE or struct pointer) unsigned char* codecdata; // extra header data passed from demuxer to codec int codecdata_len; + double pts; // last known pts value in output from decoder + int pts_bytes; // bytes output by decoder after last known pts } sh_audio_t; typedef struct { diff --git a/mplayer.c b/mplayer.c index ea1999bb63..d129e2ef15 100644 --- a/mplayer.c +++ b/mplayer.c @@ -3874,6 +3874,7 @@ if(time_frame>0.001 && !(vo_flags&256)){ delay=sh_audio->delay; delay+=(float)sh_audio->a_buffer_len/(float)sh_audio->o_bps; } + delay += sh_audio->a_out_buffer_len*playback_speed/(float)ao_data.bps; #if 0 if(pts_from_bps){ @@ -3890,16 +3891,15 @@ if(time_frame>0.001 && !(vo_flags&256)){ #endif { // PTS = (last timestamp) + (bytes after last timestamp)/(bytes per sec) - a_pts=d_audio->pts; - if(!delay_corrected) if(a_pts) delay_corrected=1; -#if 0 - mp_msg(MSGT_FIXME, MSGL_FIXME, "\n#X# pts=%5.3f ds_pts=%5.3f buff=%5.3f total=%5.3f\n", - a_pts, - ds_tell_pts(d_audio)/(float)sh_audio->i_bps, - -sh_audio->a_in_buffer_len/(float)sh_audio->i_bps, - a_pts+(ds_tell_pts(d_audio)-sh_audio->a_in_buffer_len)/(float)sh_audio->i_bps); -#endif - a_pts+=(ds_tell_pts(d_audio)-sh_audio->a_in_buffer_len)/(float)sh_audio->i_bps - sh_audio->a_out_buffer_len*playback_speed/(float)ao_data.bps; + a_pts = sh_audio->pts; + if (a_pts == MP_NOPTS_VALUE) { + // Decoder doesn't support tracking timestamps or demuxer doesn't + // set them properly in individual packets, use old inaccurate method + a_pts=d_audio->pts; + a_pts+=(ds_tell_pts(d_audio)-sh_audio->a_in_buffer_len)/(float)sh_audio->i_bps; + } + else + a_pts += sh_audio->pts_bytes / (float)sh_audio->o_bps; } v_pts=sh_video ? sh_video->pts : d_video->pts;