-
Notifications
You must be signed in to change notification settings - Fork 23
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
mpegts: add time decoder that works with native timestamp
- Loading branch information
Showing
3 changed files
with
115 additions
and
19 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,36 @@ | ||
package mpegts | ||
|
||
const ( | ||
maximum = 0x1FFFFFFFF // 33 bits | ||
negativeThreshold = 0x1FFFFFFFF / 2 | ||
) | ||
|
||
// TimeDecoder2 is a MPEG-TS timestamp decoder. | ||
type TimeDecoder2 struct { | ||
overall int64 | ||
prev int64 | ||
} | ||
|
||
// NewTimeDecoder2 allocates a TimeDecoder. | ||
func NewTimeDecoder2(start int64) *TimeDecoder2 { | ||
return &TimeDecoder2{ | ||
prev: start, | ||
} | ||
} | ||
|
||
// Decode decodes a MPEG-TS timestamp. | ||
func (d *TimeDecoder2) Decode(ts int64) int64 { | ||
diff := (ts - d.prev) & maximum | ||
|
||
// negative difference | ||
if diff > negativeThreshold { | ||
diff = (d.prev - ts) & maximum | ||
d.prev = ts | ||
d.overall -= diff | ||
} else { | ||
d.prev = ts | ||
d.overall += diff | ||
} | ||
|
||
return d.overall | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,71 @@ | ||
package mpegts | ||
|
||
import ( | ||
"testing" | ||
|
||
"github.com/stretchr/testify/require" | ||
) | ||
|
||
func TestTimeDecoder2NegativeDiff(t *testing.T) { | ||
d := NewTimeDecoder2(64523434) | ||
|
||
ts := d.Decode(64523434 - 90000) | ||
require.Equal(t, int64(-90000), ts) | ||
|
||
ts = d.Decode(64523434) | ||
require.Equal(t, int64(0), ts) | ||
|
||
ts = d.Decode(64523434 + 90000*2) | ||
require.Equal(t, int64(2*90000), ts) | ||
|
||
ts = d.Decode(64523434 + 90000) | ||
require.Equal(t, int64(1*90000), ts) | ||
} | ||
|
||
func TestTimeDecoder2Overflow(t *testing.T) { | ||
d := NewTimeDecoder2(0x1FFFFFFFF - 20) | ||
|
||
i := int64(0x1FFFFFFFF - 20) | ||
secs := int64(0) | ||
const stride = 150 | ||
lim := int64(uint64(0x1FFFFFFFF - (stride * 90000))) | ||
|
||
for n := 0; n < 100; n++ { | ||
// overflow | ||
i += 90000 * stride | ||
secs += stride | ||
ts := d.Decode(i) | ||
require.Equal(t, secs*90000, ts) | ||
|
||
// reach 2^32 slowly | ||
secs += stride | ||
i += 90000 * stride | ||
for ; i < lim; i += 90000 * stride { | ||
ts = d.Decode(i) | ||
require.Equal(t, secs*90000, ts) | ||
secs += stride | ||
} | ||
} | ||
} | ||
|
||
func TestTimeDecoder2OverflowAndBack(t *testing.T) { | ||
d := NewTimeDecoder2(0x1FFFFFFFF - 90000 + 1) | ||
|
||
ts := d.Decode(0x1FFFFFFFF - 90000 + 1) | ||
require.Equal(t, int64(0), ts) | ||
|
||
ts = d.Decode(90000) | ||
require.Equal(t, int64(2*90000), ts) | ||
|
||
ts = d.Decode(0x1FFFFFFFF - 90000 + 1) | ||
require.Equal(t, int64(0), ts) | ||
|
||
ts = d.Decode(0x1FFFFFFFF - 90000*2 + 1) | ||
require.Equal(t, int64(-1*90000), ts) | ||
|
||
ts = d.Decode(0x1FFFFFFFF - 90000 + 1) | ||
require.Equal(t, int64(0), ts) | ||
|
||
ts = d.Decode(90000) | ||
require.Equal(t, int64(2*90000), ts) | ||
} |