Skip to content
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: 5 additions & 0 deletions pkg/formats/mpegts/codecs/opus.go
Original file line number Diff line number Diff line change
@@ -1,8 +1,13 @@
package codecs

import "github.com/bluenviron/mediacommon/v2/pkg/formats/mpegts/substructs"

// Opus is a Opus codec.
// Specification: ETSI TS Opus 0.1.3-draft
type Opus struct {
Desc *substructs.OpusAudioDescriptor

// Deprecated: use Desc instead.
ChannelCount int
}

Expand Down
15 changes: 8 additions & 7 deletions pkg/formats/mpegts/reader.go
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ import (
"github.com/bluenviron/mediacommon/v2/pkg/codecs/mpeg1audio"
"github.com/bluenviron/mediacommon/v2/pkg/codecs/mpeg4audio"
"github.com/bluenviron/mediacommon/v2/pkg/formats/mpegts/codecs"
"github.com/bluenviron/mediacommon/v2/pkg/formats/mpegts/substructs"
"github.com/bluenviron/mediacommon/v2/pkg/rewindablereader"
)

Expand Down Expand Up @@ -68,8 +69,8 @@ func findPMT(dem *robustDemuxer) (*astits.PMTData, error) {
func readMetadataAUWrapper(in []byte) ([]byte, error) {
expectedSeqNum := 0

var au metadataAUCell
n, err := au.unmarshal(in)
var au substructs.MetadataAUCell
n, err := au.Unmarshal(in)
if err != nil {
return nil, err
}
Expand All @@ -96,7 +97,7 @@ func readMetadataAUWrapper(in []byte) ([]byte, error) {

for {
var n2 int
n2, err = au.unmarshal(in[n:])
n2, err = au.Unmarshal(in[n:])
if err != nil {
return nil, err
}
Expand Down Expand Up @@ -156,14 +157,14 @@ func writeMetadataAUWrapper(in []byte) ([]byte, error) {
fragmentIndication = 0b00
}

n2, err := metadataAUCell{
n2, err := substructs.MetadataAUCell{
MetadataServiceID: 0,
SequenceNumber: uint8(i),
CellFragmentIndication: fragmentIndication,
DecoderConfigFlag: false,
RandomAccessIndicator: true,
AUCellData: cellData,
}.marshalTo(out[n:])
}.MarshalTo(out[n:])
if err != nil {
return nil, err
}
Expand Down Expand Up @@ -311,8 +312,8 @@ func (r *Reader) OnDataOpus(track *Track, cb ReaderOnDataOpusFunc) {
var packets [][]byte

for {
var au opusAccessUnit
n, err := au.unmarshal(data[pos:])
var au substructs.OpusAccessUnit
n, err := au.Unmarshal(data[pos:])
if err != nil {
r.onDecodeError(err)
return nil
Expand Down
4 changes: 4 additions & 0 deletions pkg/formats/mpegts/reader_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ import (
"github.com/bluenviron/mediacommon/v2/pkg/codecs/h265"
"github.com/bluenviron/mediacommon/v2/pkg/codecs/mpeg4audio"
"github.com/bluenviron/mediacommon/v2/pkg/formats/mpegts/codecs"
"github.com/bluenviron/mediacommon/v2/pkg/formats/mpegts/substructs"
)

var testH265SPS = []byte{
Expand Down Expand Up @@ -370,6 +371,9 @@ var casesReadWriter = []struct {
&Track{
PID: 257,
Codec: &codecs.Opus{
Desc: &substructs.OpusAudioDescriptor{
ChannelConfigCode: 2,
},
ChannelCount: 2,
},
},
Expand Down
Original file line number Diff line number Diff line change
@@ -1,10 +1,11 @@
package mpegts
// Package substructs contains MPEG-TS substructures.
package substructs

import "fmt"

// metadataAUCell is a metadata_AU_cell.
// MetadataAUCell is a metadata_AU_cell.
// Specification: ISO 13818-1, table 2-97
type metadataAUCell struct {
type MetadataAUCell struct {
MetadataServiceID uint8
SequenceNumber uint8
CellFragmentIndication uint8
Expand All @@ -13,7 +14,8 @@ type metadataAUCell struct {
AUCellData []byte
}

func (c *metadataAUCell) unmarshal(buf []byte) (int, error) {
// Unmarshal decodes a MetadataAUCell.
func (c *MetadataAUCell) Unmarshal(buf []byte) (int, error) {
if len(buf) < 5 {
return 0, fmt.Errorf("buffer is too small")
}
Expand Down Expand Up @@ -42,17 +44,18 @@ func (c *metadataAUCell) unmarshal(buf []byte) (int, error) {
return n, nil
}

func (c metadataAUCell) marshalSize() int {
func (c MetadataAUCell) marshalSize() int {
return 5 + len(c.AUCellData)
}

func (c metadataAUCell) marshal() ([]byte, error) {
func (c MetadataAUCell) marshal() ([]byte, error) {
buf := make([]byte, c.marshalSize())
_, err := c.marshalTo(buf)
_, err := c.MarshalTo(buf)
return buf, err
}

func (c metadataAUCell) marshalTo(buf []byte) (int, error) {
// MarshalTo marshals a MetadataAUCell to a buffer.
func (c MetadataAUCell) MarshalTo(buf []byte) (int, error) {
n := 0

buf[n] = c.MetadataServiceID
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package mpegts
package substructs

import (
"testing"
Expand All @@ -8,12 +8,12 @@ import (

var casesMetadataAUCell = []struct {
name string
dec metadataAUCell
dec MetadataAUCell
enc []byte
}{
{
"a",
metadataAUCell{
MetadataAUCell{
MetadataServiceID: 15,
SequenceNumber: 18,
CellFragmentIndication: 3,
Expand All @@ -28,8 +28,8 @@ var casesMetadataAUCell = []struct {
func TestMetadataAUCellUnmarshal(t *testing.T) {
for _, ca := range casesMetadataAUCell {
t.Run(ca.name, func(t *testing.T) {
var h metadataAUCell
n, err := h.unmarshal(ca.enc)
var h MetadataAUCell
n, err := h.Unmarshal(ca.enc)
require.NoError(t, err)
require.Equal(t, n, len(ca.enc))
require.Equal(t, ca.dec, h)
Expand All @@ -49,8 +49,8 @@ func TestMetadataAUCellMarshal(t *testing.T) {

func FuzzMetadataAUCell(f *testing.F) {
f.Fuzz(func(t *testing.T, buf []byte) {
var c metadataAUCell
_, err := c.unmarshal(buf)
var c MetadataAUCell
_, err := c.Unmarshal(buf)
if err != nil {
return
}
Expand Down
Original file line number Diff line number Diff line change
@@ -1,10 +1,10 @@
package mpegts
package substructs

import "fmt"

// ISO 13818-1, table 2-45
const (
descriptorTagMetadata = 0x26
DescriptorTagMetadata = 0x26
)

func flagToByte(v bool) byte {
Expand All @@ -14,9 +14,9 @@ func flagToByte(v bool) byte {
return 0
}

// metadataDescriptor is a metadata_descriptor.
// MetadataDescriptor is a metadata_descriptor.
// Specification: ISO 13818-1, table 2-86
type metadataDescriptor struct {
type MetadataDescriptor struct {
MetadataApplicationFormat uint16

// metadata_application_format == 0xFFFF
Expand Down Expand Up @@ -46,7 +46,8 @@ type metadataDescriptor struct {
PrivateData []uint8
}

func (d *metadataDescriptor) unmarshal(buf []byte) error {
// Unmarshal decodes a MetadataDescriptor.
func (d *MetadataDescriptor) Unmarshal(buf []byte) error {
n := 0

if len(buf[n:]) < 2 {
Expand Down Expand Up @@ -157,7 +158,7 @@ func (d *metadataDescriptor) unmarshal(buf []byte) error {
return nil
}

func (d metadataDescriptor) marshalSize() int {
func (d MetadataDescriptor) marshalSize() int {
v := 5

if d.MetadataApplicationFormat == 0xFFFF {
Expand Down Expand Up @@ -191,7 +192,8 @@ func (d metadataDescriptor) marshalSize() int {
return v
}

func (d metadataDescriptor) marshal() ([]byte, error) {
// Marshal marshals a MetadataDescriptor to a byte slice.
func (d MetadataDescriptor) Marshal() ([]byte, error) {
buf := make([]byte, d.marshalSize())
n := 0

Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package mpegts
package substructs

import (
"testing"
Expand All @@ -8,12 +8,12 @@ import (

var casesMetadataDescriptor = []struct {
name string
dec metadataDescriptor
dec MetadataDescriptor
enc []byte
}{
{
"a",
metadataDescriptor{
MetadataDescriptor{
MetadataApplicationFormat: 0xFFFF,
MetadataApplicationFormatIdentifier: 893234,
MetadataFormat: 23,
Expand All @@ -31,8 +31,8 @@ var casesMetadataDescriptor = []struct {
func TestMetadataDescriptorUnmarshal(t *testing.T) {
for _, ca := range casesMetadataDescriptor {
t.Run(ca.name, func(t *testing.T) {
var h metadataDescriptor
err := h.unmarshal(ca.enc)
var h MetadataDescriptor
err := h.Unmarshal(ca.enc)
require.NoError(t, err)
require.Equal(t, ca.dec, h)
})
Expand All @@ -42,7 +42,7 @@ func TestMetadataDescriptorUnmarshal(t *testing.T) {
func TestMetadataDescriptorMarshal(t *testing.T) {
for _, ca := range casesMetadataDescriptor {
t.Run(ca.name, func(t *testing.T) {
buf, err := ca.dec.marshal()
buf, err := ca.dec.Marshal()
require.NoError(t, err)
require.Equal(t, ca.enc, buf)
})
Expand All @@ -51,13 +51,13 @@ func TestMetadataDescriptorMarshal(t *testing.T) {

func FuzzMetadataDescriptor(f *testing.F) {
f.Fuzz(func(t *testing.T, buf []byte) {
var dm metadataDescriptor
err := dm.unmarshal(buf)
var dm MetadataDescriptor
err := dm.Unmarshal(buf)
if err != nil {
return
}

_, err = dm.marshal()
_, err = dm.Marshal()
require.NoError(t, err)
})
}
Original file line number Diff line number Diff line change
@@ -1,21 +1,22 @@
package mpegts
package substructs

import "fmt"

// ISO 13818-1, table 2-45
const (
descriptorTagMetadataSTD = 0x27
DescriptorTagMetadataSTD = 0x27
)

// metadataSTDDescriptor is a metadata_std_descriptor.
// MetadataSTDDescriptor is a metadata_std_descriptor.
// Specification: ISO 13818-1, table 2-88
type metadataSTDDescriptor struct {
type MetadataSTDDescriptor struct {
MetadataInputLeakRate uint32
MetadataBufferSize uint32
MetadataOutputLeakRate uint32
}

func (d *metadataSTDDescriptor) unmarshal(buf []byte) error {
// Unmarshal decodes a MetadataSTDDescriptor.
func (d *MetadataSTDDescriptor) Unmarshal(buf []byte) error {
if len(buf) < 9 {
return fmt.Errorf("buffer is too small")
}
Expand All @@ -38,11 +39,12 @@ func (d *metadataSTDDescriptor) unmarshal(buf []byte) error {
return nil
}

func (d metadataSTDDescriptor) marshalSize() int {
func (d MetadataSTDDescriptor) marshalSize() int {
return 9
}

func (d metadataSTDDescriptor) marshal() ([]byte, error) {
// Marshal encodes a MetadataSTDDescriptor.
func (d MetadataSTDDescriptor) Marshal() ([]byte, error) {
buf := make([]byte, d.marshalSize())
n := 0

Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package mpegts
package substructs

import (
"testing"
Expand All @@ -8,12 +8,12 @@ import (

var casesMetadataSTDDescriptor = []struct {
name string
dec metadataSTDDescriptor
dec MetadataSTDDescriptor
enc []byte
}{
{
"a",
metadataSTDDescriptor{
MetadataSTDDescriptor{
MetadataInputLeakRate: 463412,
MetadataBufferSize: 834523,
MetadataOutputLeakRate: 845324,
Expand All @@ -25,8 +25,8 @@ var casesMetadataSTDDescriptor = []struct {
func TestMetadataSTDDescriptorUnmarshal(t *testing.T) {
for _, ca := range casesMetadataSTDDescriptor {
t.Run(ca.name, func(t *testing.T) {
var h metadataSTDDescriptor
err := h.unmarshal(ca.enc)
var h MetadataSTDDescriptor
err := h.Unmarshal(ca.enc)
require.NoError(t, err)
require.Equal(t, ca.dec, h)
})
Expand All @@ -36,7 +36,7 @@ func TestMetadataSTDDescriptorUnmarshal(t *testing.T) {
func TestMetadataSTDDescriptorMarshal(t *testing.T) {
for _, ca := range casesMetadataSTDDescriptor {
t.Run(ca.name, func(t *testing.T) {
buf, err := ca.dec.marshal()
buf, err := ca.dec.Marshal()
require.NoError(t, err)
require.Equal(t, ca.enc, buf)
})
Expand All @@ -45,13 +45,13 @@ func TestMetadataSTDDescriptorMarshal(t *testing.T) {

func FuzzMetadataSTDDescriptor(f *testing.F) {
f.Fuzz(func(t *testing.T, buf []byte) {
var dm metadataSTDDescriptor
err := dm.unmarshal(buf)
var dm MetadataSTDDescriptor
err := dm.Unmarshal(buf)
if err != nil {
return
}

_, err = dm.marshal()
_, err = dm.Marshal()
require.NoError(t, err)
})
}
Loading