Skip to content

Commit

Permalink
mpegts: change TimeDecoder2 signature (#151)
Browse files Browse the repository at this point in the history
  • Loading branch information
aler9 authored Oct 7, 2024
1 parent 6b36e89 commit 8883ba8
Show file tree
Hide file tree
Showing 3 changed files with 27 additions and 11 deletions.
5 changes: 4 additions & 1 deletion pkg/formats/mpegts/time_decoder.go
Original file line number Diff line number Diff line change
Expand Up @@ -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,
}
}

Expand Down
16 changes: 10 additions & 6 deletions pkg/formats/mpegts/time_decoder2.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
17 changes: 13 additions & 4 deletions pkg/formats/mpegts/time_decoder2_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand All @@ -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)
Expand All @@ -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)

Expand Down

0 comments on commit 8883ba8

Please sign in to comment.