# HG changeset patch # User uau # Date 1190670596 0 # Node ID 9118be6575da17de32a01765111f7b60efc1d062 # Parent 8eb1ef462d2996c508a39335f37990200b33641c demux_audio.c: Fix timestamp handling The code calculated the pts values of audio packets by adding the length of the current packet to the pts of the previous one. The length of the previous packet should be added instead. This broke WAV timestamps near the end of the stream where a short packet occurs. Change the code to store the pts of the next packet instead of the last one. This fixes the WAV timestamps and allows some simplifications. MP3 timestamps are not affected as packets are always treated as constant decoded length, and FLAC timestamps still have worse problems (FLAC is treated as as if it was constant bitrate even though it isn't). Also store the timestamps as double instead of float. diff -r 8eb1ef462d29 -r 9118be6575da libmpdemux/demux_audio.c --- a/libmpdemux/demux_audio.c Mon Sep 24 21:49:53 2007 +0000 +++ b/libmpdemux/demux_audio.c Mon Sep 24 21:49:56 2007 +0000 @@ -26,7 +26,7 @@ typedef struct da_priv { int frmt; - float last_pts; + double next_pts; } da_priv_t; //! rather arbitrary value for maximum length of wav-format headers @@ -521,7 +521,7 @@ priv = malloc(sizeof(da_priv_t)); priv->frmt = frmt; - priv->last_pts = -1; + priv->next_pts = 0; demuxer->priv = priv; demuxer->audio->id = 0; demuxer->audio->sh = sh_audio; @@ -570,6 +570,8 @@ if(s->eof) return 0; + double this_pts = priv->next_pts; + switch(priv->frmt) { case MP3 : while(1) { @@ -590,7 +592,7 @@ free_demux_packet(dp); return 0; } - priv->last_pts = priv->last_pts < 0 ? 0 : priv->last_pts + sh_audio->audio.dwScale/(float)sh_audio->samplerate; + priv->next_pts += sh_audio->audio.dwScale/(double)sh_audio->samplerate; break; } } break; @@ -606,14 +608,15 @@ l = (l + align - 1) / align * align; dp = new_demux_packet(l); l = stream_read(s,dp->buffer,l); - priv->last_pts = priv->last_pts < 0 ? 0 : priv->last_pts + l/(float)sh_audio->i_bps; + priv->next_pts += l/(double)sh_audio->i_bps; break; } case fLaC: { l = 65535; dp = new_demux_packet(l); l = stream_read(s,dp->buffer,l); - priv->last_pts = priv->last_pts < 0 ? 0 : priv->last_pts + l/(float)sh_audio->i_bps; + /* FLAC is not a constant-bitrate codec. These values will be wrong. */ + priv->next_pts += l/(double)sh_audio->i_bps; break; } default: @@ -622,7 +625,7 @@ } resize_demux_packet(dp, l); - dp->pts = priv->last_pts; + dp->pts = this_pts; ds_add_packet(ds, dp); return 1; } @@ -642,7 +645,7 @@ continue; } stream_skip(demuxer->stream,len-4); - priv->last_pts += sh->audio.dwScale/(float)sh->samplerate; + priv->next_pts += sh->audio.dwScale/(double)sh->samplerate; nf--; } } @@ -660,11 +663,11 @@ priv = demuxer->priv; if(priv->frmt == MP3 && hr_mp3_seek && !(flags & 2)) { - len = (flags & 1) ? rel_seek_secs - priv->last_pts : rel_seek_secs; + len = (flags & 1) ? rel_seek_secs - priv->next_pts : rel_seek_secs; if(len < 0) { stream_seek(s,demuxer->movi_start); - len = priv->last_pts + len; - priv->last_pts = 0; + len = priv->next_pts + len; + priv->next_pts = 0; } if(len > 0) high_res_mp3_seek(demuxer,len); @@ -682,15 +685,13 @@ } else if(pos < demuxer->movi_start) pos = demuxer->movi_start; - priv->last_pts = (pos-demuxer->movi_start)/(float)sh_audio->i_bps; + priv->next_pts = (pos-demuxer->movi_start)/(double)sh_audio->i_bps; switch(priv->frmt) { case WAV: pos -= (pos - demuxer->movi_start) % (sh_audio->wf->nBlockAlign ? sh_audio->wf->nBlockAlign : (sh_audio->channels * sh_audio->samplesize)); - // We need to decrease the pts by one step to make it the "last one" - priv->last_pts -= sh_audio->wf->nAvgBytesPerSec/(float)sh_audio->i_bps; break; } @@ -719,7 +720,7 @@ case DEMUXER_CTRL_GET_PERCENT_POS: if (audio_length<=0) return DEMUXER_CTRL_DONTKNOW; - *((int *)arg)=(int)( (priv->last_pts*100) / audio_length); + *((int *)arg)=(int)( (priv->next_pts*100) / audio_length); return DEMUXER_CTRL_OK; default: