# =[SPAM?]= [Matroska-devel] Timestamp precision in matroska files]

Frank Klemm pfk at fuchs.offl.uni-jena.de
Fri Jan 9 23:28:59 CET 2004

```Hi,

translation to English and an additional remark.

When you want address sample precise, you need a time base with the
preciion of the reciproce of the sample frequency. You don't need a
saveguard factor of 2 as Mr. Wiesner suggested, but you need precise
rounding rules.

Otherwise you need a indefinite precision for the timebase (or a integral
multiple).

You must also be careful with floating point because of rounding differences
depending on the order of the operations. Note that a*b/c != a/c*b.

In the case of an intergral sample rate (48000 Hz, not 44055.97376127834
Hz), you can use the following code (to avoid 96 bit int arithmetic).

UINT64
WhichSample ( UINT64 timeticks,          // 0...2^64-1
UINT32 timetickspersecond, // 1, 300, 1000, 1200, 48000, 192000, 10^6, 10^9
UINT32 samplefreq )        // 8000, 32000, 44100, 48000, 96000, 192000
{
if ( timetickspersecond == 0 )
return ERROR ;

if ( samplefreq > timetickspersecond )
fprintf ( stderr, "Inprecise\n" );

UINT32  seconds   = timeticks / timetickspersecond ;
UINT32  remaining = timeticks % timetickspersecond ;
UINT64  ret ;

ret  = (UINT64) remaining * samplefreq / timetickspersecond ;
ret += (UINT64) seconds   * samplefreq ;

return ret ;
}

This code rounds down. I suggest to use this rounding control. Other
possiblities are round up, rounding to nearest integer (rounding up 0.5) or
rounding to nearest integer (rounding 0.5 to nearest even integral number).

Note that the last two are nearly identical, but they still gave different
results for some input.

--
Frank Klemm

```

More information about the Matroska-devel mailing list