Skip to content

Commit 884553e

Browse files
committed
Examples now ignore decoded frames with non monotonic PTS
1 parent a6e3f5a commit 884553e

File tree

5 files changed

+37
-0
lines changed

5 files changed

+37
-0
lines changed

examples/demuxing_decoding/main.go

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@ import (
88
"strings"
99

1010
"github.com/asticode/go-astiav"
11+
"github.com/asticode/go-astikit"
1112
)
1213

1314
var (
@@ -17,6 +18,7 @@ var (
1718
type stream struct {
1819
decCodec *astiav.Codec
1920
decCodecContext *astiav.CodecContext
21+
decLastPTS *int64
2022
inputStream *astiav.Stream
2123
}
2224

@@ -146,6 +148,12 @@ func main() {
146148
// Make sure to unreference the frame
147149
defer f.Unref()
148150

151+
// Ignore frames with non monotonic PTS
152+
if s.decLastPTS != nil && *s.decLastPTS >= f.Pts() {
153+
return false
154+
}
155+
s.decLastPTS = astikit.Int64Ptr(f.Pts())
156+
149157
// Log
150158
log.Printf("new %s frame: stream %d - pts: %d", s.inputStream.CodecParameters().MediaType(), pkt.StreamIndex(), f.Pts())
151159
return false

examples/filtering/main.go

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,7 @@ type stream struct {
2727
decCodec *astiav.Codec
2828
decCodecContext *astiav.CodecContext
2929
decFrame *astiav.Frame
30+
decLastPTS *int64
3031
filterFrame *astiav.Frame
3132
filterGraph *astiav.FilterGraph
3233
inputStream *astiav.Stream
@@ -112,6 +113,12 @@ func main() {
112113
// Make sure to unreference the frame
113114
defer s.decFrame.Unref()
114115

116+
// Ignore frames with non monotonic PTS
117+
if s.decLastPTS != nil && *s.decLastPTS >= s.decFrame.Pts() {
118+
return false
119+
}
120+
s.decLastPTS = astikit.Int64Ptr(s.decFrame.Pts())
121+
115122
// Filter frame
116123
if err := filterFrame(s.decFrame, s); err != nil {
117124
log.Fatal(fmt.Errorf("main: filtering frame failed: %w", err))

examples/hardware_decoding_filtering/main.go

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,7 @@ var (
2525
c = astikit.NewCloser()
2626
decCodec *astiav.Codec
2727
decCodecContext *astiav.CodecContext
28+
decLastPTS *int64
2829
decodedHardwareFrame *astiav.Frame
2930
filterGraph *astiav.FilterGraph
3031
filteredHardwareFrame *astiav.Frame
@@ -214,6 +215,12 @@ func main() {
214215
// Make sure to unreference hardware frame
215216
defer decodedHardwareFrame.Unref()
216217

218+
// Ignore frames with non monotonic PTS
219+
if decLastPTS != nil && *decLastPTS >= decodedHardwareFrame.Pts() {
220+
return false
221+
}
222+
decLastPTS = astikit.Int64Ptr(decodedHardwareFrame.Pts())
223+
217224
// Invalid pixel format
218225
if decodedHardwareFrame.PixelFormat() != hardwarePixelFormat {
219226
log.Fatalf("main: invalid decoded pixel format %s, expected %s", decodedHardwareFrame.PixelFormat(), hardwarePixelFormat)

examples/resampling_audio/main.go

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@ import (
88
"strings"
99

1010
"github.com/asticode/go-astiav"
11+
"github.com/asticode/go-astikit"
1112
)
1213

1314
var (
@@ -16,6 +17,7 @@ var (
1617

1718
var (
1819
af *astiav.AudioFifo
20+
decLastPTS *int64
1921
decodedFrame *astiav.Frame
2022
finalFrame *astiav.Frame
2123
resampledFrame *astiav.Frame
@@ -186,6 +188,12 @@ func main() {
186188
// Make sure to unreference the frame
187189
defer decodedFrame.Unref()
188190

191+
// Ignore frames with non monotonic PTS
192+
if decLastPTS != nil && *decLastPTS >= decodedFrame.Pts() {
193+
return false
194+
}
195+
decLastPTS = astikit.Int64Ptr(decodedFrame.Pts())
196+
189197
// Log
190198
log.Printf("new decoded frame: nb samples: %d", decodedFrame.NbSamples())
191199

examples/transcoding/main.go

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,7 @@ type stream struct {
2929
decCodec *astiav.Codec
3030
decCodecContext *astiav.CodecContext
3131
decFrame *astiav.Frame
32+
decLastPTS *int64
3233
encCodec *astiav.Codec
3334
encCodecContext *astiav.CodecContext
3435
encPkt *astiav.Packet
@@ -126,6 +127,12 @@ func main() {
126127
// Make sure to unreference the frame
127128
defer s.decFrame.Unref()
128129

130+
// Ignore frames with non monotonic PTS
131+
if s.decLastPTS != nil && *s.decLastPTS >= s.decFrame.Pts() {
132+
return false
133+
}
134+
s.decLastPTS = astikit.Int64Ptr(s.decFrame.Pts())
135+
129136
// Filter, encode and write frame
130137
if err := filterEncodeWriteFrame(s.decFrame, s); err != nil {
131138
log.Fatal(fmt.Errorf("main: filtering, encoding and writing frame failed: %w", err))

0 commit comments

Comments
 (0)