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: automatically strip AUD from H264 and H265 #132

Merged
merged 1 commit into from
May 25, 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
35 changes: 34 additions & 1 deletion pkg/formats/mpegts/reader.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@

"github.com/bluenviron/mediacommon/pkg/codecs/ac3"
"github.com/bluenviron/mediacommon/pkg/codecs/h264"
"github.com/bluenviron/mediacommon/pkg/codecs/h265"
"github.com/bluenviron/mediacommon/pkg/codecs/mpeg1audio"
"github.com/bluenviron/mediacommon/pkg/codecs/mpeg4audio"
)
Expand Down Expand Up @@ -114,15 +115,47 @@
r.onDecodeError = cb
}

// OnDataH26x sets a callback that is called when data from an H26x track is received.
// OnDataH26x sets a callback that is called when data from an H265 or H264 track is received.
//
// Deprecated: replaced by OnDataH264, OnDataH265
func (r *Reader) OnDataH26x(track *Track, cb ReaderOnDataH26xFunc) {
if _, ok := track.Codec.(*CodecH265); ok {
r.OnDataH265(track, cb)
} else {
r.OnDataH264(track, cb)
}

Check warning on line 126 in pkg/formats/mpegts/reader.go

View check run for this annotation

Codecov / codecov/patch

pkg/formats/mpegts/reader.go#L122-L126

Added lines #L122 - L126 were not covered by tests
}

// OnDataH265 sets a callback that is called when data from an H265 track is received.
func (r *Reader) OnDataH265(track *Track, cb ReaderOnDataH26xFunc) {
r.onData[track.PID] = func(pts int64, dts int64, data []byte) error {
au, err := h264.AnnexBUnmarshal(data)
if err != nil {
r.onDecodeError(err)
return nil
}

Check warning on line 136 in pkg/formats/mpegts/reader.go

View check run for this annotation

Codecov / codecov/patch

pkg/formats/mpegts/reader.go#L134-L136

Added lines #L134 - L136 were not covered by tests

if au[0][0] == byte(h265.NALUType_AUD_NUT<<1) {
au = au[1:]
}

return cb(pts, dts, au)
}
}

// OnDataH264 sets a callback that is called when data from an H264 track is received.
func (r *Reader) OnDataH264(track *Track, cb ReaderOnDataH26xFunc) {
r.onData[track.PID] = func(pts int64, dts int64, data []byte) error {
au, err := h264.AnnexBUnmarshal(data)
if err != nil {
r.onDecodeError(err)
return nil
}

if au[0][0] == byte(h264.NALUTypeAccessUnitDelimiter) {
au = au[1:]
}

return cb(pts, dts, au)
}
}
Expand Down
22 changes: 13 additions & 9 deletions pkg/formats/mpegts/reader_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,6 @@ import (
"github.com/asticode/go-astits"
"github.com/stretchr/testify/require"

"github.com/bluenviron/mediacommon/pkg/codecs/h264"
"github.com/bluenviron/mediacommon/pkg/codecs/h265"
"github.com/bluenviron/mediacommon/pkg/codecs/mpeg4audio"
)
Expand Down Expand Up @@ -60,7 +59,6 @@ var casesReadWriter = []struct {
30 * 90000,
30 * 90000,
[][]byte{
{byte(h265.NALUType_AUD_NUT) << 1, 1, 0x50}, // AUD
testH265SPS, // SPS
testH265PPS, // PPS
{byte(h265.NALUType_CRA_NUT) << 1},
Expand All @@ -70,7 +68,6 @@ var casesReadWriter = []struct {
30*90000 + 2*90000,
30*90000 + 1*90000,
[][]byte{
{byte(h265.NALUType_AUD_NUT) << 1, 1, 0x50}, // AUD
{byte(h265.NALUType_TRAIL_N) << 1},
},
},
Expand Down Expand Up @@ -162,7 +159,6 @@ var casesReadWriter = []struct {
30 * 90000,
30 * 90000,
[][]byte{
{byte(h264.NALUTypeAccessUnitDelimiter), 240}, // AUD
testH264SPS, // SPS
{8}, // PPS
{5}, // IDR
Expand All @@ -172,7 +168,6 @@ var casesReadWriter = []struct {
30*90000 + 2*90000,
30*90000 + 1*90000,
[][]byte{
{byte(h264.NALUTypeAccessUnitDelimiter), 240}, // AUD
{1}, // non-IDR
},
},
Expand Down Expand Up @@ -879,8 +874,17 @@ func TestReader(t *testing.T) {
i := 0

switch ca.track.Codec.(type) {
case *CodecH265, *CodecH264:
r.OnDataH26x(ca.track, func(pts int64, dts int64, au [][]byte) error {
case *CodecH265:
r.OnDataH265(ca.track, func(pts int64, dts int64, au [][]byte) error {
require.Equal(t, ca.samples[i].pts, pts)
require.Equal(t, ca.samples[i].dts, dts)
require.Equal(t, ca.samples[i].data, au)
i++
return nil
})

case *CodecH264:
r.OnDataH264(ca.track, func(pts int64, dts int64, au [][]byte) error {
require.Equal(t, ca.samples[i].pts, pts)
require.Equal(t, ca.samples[i].dts, dts)
require.Equal(t, ca.samples[i].data, au)
Expand Down Expand Up @@ -1243,7 +1247,7 @@ func TestReaderDecodeErrors(t *testing.T) {

switch ca {
case "missing pts", "h26x invalid avcc":
r.OnDataH26x(r.Tracks()[0], func(_, _ int64, _ [][]byte) error {
r.OnDataH264(r.Tracks()[0], func(_, _ int64, _ [][]byte) error {
return nil
})

Expand All @@ -1269,7 +1273,7 @@ func TestReaderDecodeErrors(t *testing.T) {

case "garbage":
counter := 0
r.OnDataH26x(r.Tracks()[0], func(_, _ int64, _ [][]byte) error {
r.OnDataH264(r.Tracks()[0], func(_, _ int64, _ [][]byte) error {
counter++
if counter == 2 {
dataRecv = true
Expand Down
16 changes: 8 additions & 8 deletions pkg/formats/mpegts/writer.go
Original file line number Diff line number Diff line change
Expand Up @@ -105,18 +105,18 @@ func (w *Writer) WriteH26x(
return w.WriteH264(track, pts, dts, randomAccess, au)
}

// WriteH264 writes a H264 access unit.
func (w *Writer) WriteH264(
// WriteH265 writes a H265 access unit.
func (w *Writer) WriteH265(
track *Track,
pts int64,
dts int64,
randomAccess bool,
au [][]byte,
) error {
// prepend an AUD. This is required by video.js, iOS, QuickTime
if h264.NALUType(au[0][0]&0x1F) != h264.NALUTypeAccessUnitDelimiter {
if au[0][0] != byte(h265.NALUType_AUD_NUT<<1) {
au = append([][]byte{
{byte(h264.NALUTypeAccessUnitDelimiter), 240},
{byte(h265.NALUType_AUD_NUT) << 1, 1, 0x50},
}, au...)
}

Expand All @@ -128,18 +128,18 @@ func (w *Writer) WriteH264(
return w.writeVideo(track, pts, dts, randomAccess, enc)
}

// WriteH265 writes a H265 access unit.
func (w *Writer) WriteH265(
// WriteH264 writes a H264 access unit.
func (w *Writer) WriteH264(
track *Track,
pts int64,
dts int64,
randomAccess bool,
au [][]byte,
) error {
// prepend an AUD. This is required by video.js, iOS, QuickTime
if h265.NALUType(au[0][0]>>1) != h265.NALUType_AUD_NUT {
if au[0][0] != byte(h264.NALUTypeAccessUnitDelimiter) {
au = append([][]byte{
{byte(h265.NALUType_AUD_NUT) << 1, 1, 0x50},
{byte(h264.NALUTypeAccessUnitDelimiter), 240},
}, au...)
}

Expand Down
Loading