Skip to content

Commit

Permalink
mpegts: add WriteH2642, WriteH2652
Browse files Browse the repository at this point in the history
  • Loading branch information
aler9 committed Jan 21, 2025
1 parent 20b296a commit dcaaed2
Show file tree
Hide file tree
Showing 2 changed files with 36 additions and 22 deletions.
40 changes: 34 additions & 6 deletions pkg/formats/mpegts/writer.go
Original file line number Diff line number Diff line change
Expand Up @@ -105,12 +105,11 @@ func (w *Writer) WriteH26x(
return w.WriteH264(track, pts, dts, randomAccess, au)
}

// WriteH265 writes a H265 access unit.
func (w *Writer) WriteH265(
// WriteH2652 writes a H265 access unit.
func (w *Writer) WriteH2652(
track *Track,
pts int64,
dts int64,
randomAccess bool,
au [][]byte,
) error {
// prepend an AUD. This is required by video.js, iOS, QuickTime
Expand All @@ -125,15 +124,29 @@ func (w *Writer) WriteH265(
return err
}

randomAccess := h265.IsRandomAccess(au)

return w.writeVideo(track, pts, dts, randomAccess, enc)
}

// WriteH264 writes a H264 access unit.
func (w *Writer) WriteH264(
// WriteH265 writes a H265 access unit.
//
// Deprecated: replaced by WriteH2652
func (w *Writer) WriteH265(
track *Track,
pts int64,
dts int64,
_ bool,
au [][]byte,
) error {
return w.WriteH2652(track, pts, dts, au)
}

// WriteH2642 writes a H264 access unit.
func (w *Writer) WriteH2642(
track *Track,
pts int64,
dts int64,
randomAccess bool,
au [][]byte,
) error {
// prepend an AUD. This is required by video.js, iOS, QuickTime
Expand All @@ -148,9 +161,24 @@ func (w *Writer) WriteH264(
return err
}

randomAccess := h264.IDRPresent(au)

return w.writeVideo(track, pts, dts, randomAccess, enc)
}

// WriteH264 writes a H264 access unit.
//
// Deprecated: replaced by WriteH2642
func (w *Writer) WriteH264(
track *Track,
pts int64,
dts int64,
_ bool,
au [][]byte,
) error {
return w.WriteH2642(track, pts, dts, au)
}

// WriteMPEG4Video writes a MPEG-4 Video frame.
func (w *Writer) WriteMPEG4Video(
track *Track,
Expand Down
18 changes: 2 additions & 16 deletions pkg/formats/mpegts/writer_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,22 +9,8 @@ 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"
)

func h265RandomAccessPresent(au [][]byte) bool {
for _, nalu := range au {
typ := h265.NALUType((nalu[0] >> 1) & 0b111111)
switch typ {
case h265.NALUType_IDR_W_RADL, h265.NALUType_IDR_N_LP, h265.NALUType_CRA_NUT:
return true
}
}
return false
}

func TestWriter(t *testing.T) {
for _, ca := range casesReadWriter {
t.Run(ca.name, func(t *testing.T) {
Expand All @@ -34,11 +20,11 @@ func TestWriter(t *testing.T) {
for _, sample := range ca.samples {
switch ca.track.Codec.(type) {
case *CodecH265:
err := w.WriteH26x(ca.track, sample.pts, sample.dts, h265RandomAccessPresent(sample.data), sample.data)
err := w.WriteH2652(ca.track, sample.pts, sample.dts, sample.data)
require.NoError(t, err)

case *CodecH264:
err := w.WriteH26x(ca.track, sample.pts, sample.dts, h264.IDRPresent(sample.data), sample.data)
err := w.WriteH2642(ca.track, sample.pts, sample.dts, sample.data)
require.NoError(t, err)

case *CodecMPEG4Video:
Expand Down

0 comments on commit dcaaed2

Please # to comment.