Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

mpegts: change TimeDecoder2 signature #151

Merged
merged 1 commit into from
Oct 7, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
Loading