diff --git a/pkg/formats/mpegts/time_decoder.go b/pkg/formats/mpegts/time_decoder.go index 170fd54..c6be6fe 100644 --- a/pkg/formats/mpegts/time_decoder.go +++ b/pkg/formats/mpegts/time_decoder.go @@ -27,8 +27,11 @@ type TimeDecoder struct { // // Deprecated: replaced by NewTimeDecoder2. func NewTimeDecoder(start int64) *TimeDecoder { + td := NewTimeDecoder2() + td.Decode(start) + return &TimeDecoder{ - wrapped: NewTimeDecoder2(start), + wrapped: td, } } diff --git a/pkg/formats/mpegts/time_decoder2.go b/pkg/formats/mpegts/time_decoder2.go index d0325ea..0d8690a 100644 --- a/pkg/formats/mpegts/time_decoder2.go +++ b/pkg/formats/mpegts/time_decoder2.go @@ -7,19 +7,23 @@ const ( // TimeDecoder2 is a MPEG-TS timestamp decoder. type TimeDecoder2 struct { - overall int64 - prev int64 + initialized bool + overall int64 + prev int64 } // NewTimeDecoder2 allocates a TimeDecoder. -func NewTimeDecoder2(start int64) *TimeDecoder2 { - return &TimeDecoder2{ - prev: start, - } +func NewTimeDecoder2() *TimeDecoder2 { + return &TimeDecoder2{} } // Decode decodes a MPEG-TS timestamp. func (d *TimeDecoder2) Decode(ts int64) int64 { + if !d.initialized { + d.initialized = true + d.prev = ts + } + diff := (ts - d.prev) & maximum // negative difference diff --git a/pkg/formats/mpegts/time_decoder2_test.go b/pkg/formats/mpegts/time_decoder2_test.go index 1c1d6df..91102eb 100644 --- a/pkg/formats/mpegts/time_decoder2_test.go +++ b/pkg/formats/mpegts/time_decoder2_test.go @@ -7,9 +7,12 @@ import ( ) func TestTimeDecoder2NegativeDiff(t *testing.T) { - d := NewTimeDecoder2(64523434) + d := NewTimeDecoder2() - ts := d.Decode(64523434 - 90000) + ts := d.Decode(64523434) + require.Equal(t, int64(0), ts) + + ts = d.Decode(64523434 - 90000) require.Equal(t, int64(-90000), ts) ts = d.Decode(64523434) @@ -23,7 +26,10 @@ func TestTimeDecoder2NegativeDiff(t *testing.T) { } func TestTimeDecoder2Overflow(t *testing.T) { - d := NewTimeDecoder2(0x1FFFFFFFF - 20) + d := NewTimeDecoder2() + + ts := d.Decode(0x1FFFFFFFF - 20) + require.Equal(t, int64(0), ts) i := int64(0x1FFFFFFFF - 20) secs := int64(0) @@ -49,11 +55,14 @@ func TestTimeDecoder2Overflow(t *testing.T) { } func TestTimeDecoder2OverflowAndBack(t *testing.T) { - d := NewTimeDecoder2(0x1FFFFFFFF - 90000 + 1) + d := NewTimeDecoder2() ts := d.Decode(0x1FFFFFFFF - 90000 + 1) require.Equal(t, int64(0), ts) + ts = d.Decode(0x1FFFFFFFF - 90000 + 1) + require.Equal(t, int64(0), ts) + ts = d.Decode(90000) require.Equal(t, int64(2*90000), ts)