Skip to content

Commit

Permalink
Merge pull request #334 from Eyevinn/fix-kind-box
Browse files Browse the repository at this point in the history
Fix kind box
  • Loading branch information
tobbee authored Mar 9, 2024
2 parents c504a74 + f9988b7 commit 5f41c88
Show file tree
Hide file tree
Showing 3 changed files with 43 additions and 6 deletions.
5 changes: 5 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,10 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0
- Renamed bits.SliceWriterError to bits.ErrSliceWrite
- colr box supports unknown colrType

### Fixed

- kind box full-box header

## [0.42.0] - 2024-01-26

### Fixed
Expand Down Expand Up @@ -84,6 +88,7 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0

- SPS.ChromaArrayType method
- Makefile now builds all CLI applications with version
- DecryptInit extracts pssh boxes

### Changed

Expand Down
15 changes: 11 additions & 4 deletions mp4/kind.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,8 @@ import (

// KindBox - Track Kind Box
type KindBox struct {
Version byte
Flags uint32
SchemeURI string
Value string
}
Expand All @@ -25,14 +27,17 @@ func DecodeKind(hdr BoxHeader, startPos uint64, r io.Reader) (Box, error) {

// DecodeKindSR - box-specific decode
func DecodeKindSR(hdr BoxHeader, startPos uint64, sr bits.SliceReader) (Box, error) {
maxLen := hdr.payloadLen() - 1
versionAndFlags := sr.ReadUint32()
maxLen := hdr.payloadLen() - 4 - 1
schemeURI := sr.ReadZeroTerminatedString(maxLen)
maxLen = hdr.payloadLen() - 1
maxLen = hdr.payloadLen() - 4 - len(schemeURI) - 1
value := sr.ReadZeroTerminatedString(maxLen)
if err := sr.AccError(); err != nil {
return nil, fmt.Errorf("decode kind: %w", err)
}
b := KindBox{
Version: byte(versionAndFlags >> 24),
Flags: versionAndFlags & flagsMask,
SchemeURI: schemeURI,
Value: value,
}
Expand All @@ -46,7 +51,7 @@ func (b *KindBox) Type() string {

// Size - calculated size of box
func (b *KindBox) Size() uint64 {
return uint64(boxHeaderSize + len(b.SchemeURI) + 1 + len(b.Value) + 1)
return uint64(boxHeaderSize + 4 + len(b.SchemeURI) + 1 + len(b.Value) + 1)
}

// Encode - write box to w
Expand All @@ -66,14 +71,16 @@ func (b *KindBox) EncodeSW(sw bits.SliceWriter) error {
if err != nil {
return err
}
versionAndFlags := (uint32(b.Version) << 24) + b.Flags
sw.WriteUint32(versionAndFlags)
sw.WriteString(b.SchemeURI, true)
sw.WriteString(b.Value, true)
return sw.AccError()
}

// Info - write box-specific information
func (b *KindBox) Info(w io.Writer, specificBoxLevels, indent, indentStep string) error {
bd := newInfoDumper(w, indent, b, -1, 0)
bd := newInfoDumper(w, indent, b, int(b.Version), b.Flags)
bd.write(" - schemeURI: %s", b.SchemeURI)
bd.write(" - value: %s", b.Value)
return bd.err
Expand Down
29 changes: 27 additions & 2 deletions mp4/kind_test.go
Original file line number Diff line number Diff line change
@@ -1,10 +1,35 @@
package mp4

import (
"bytes"
"encoding/hex"
"testing"
)

func TestKind(t *testing.T) {
kind := &KindBox{SchemeURI: "urn:mpeg:dash:role:2011", Value: "forced-subtitle"}
boxDiffAfterEncodeAndDecode(t, kind)
t.Run("encode and decode", func(t *testing.T) {
kind := &KindBox{SchemeURI: "urn:mpeg:dash:role:2011", Value: "forced-subtitle"}
boxDiffAfterEncodeAndDecode(t, kind)
})
t.Run("decode with full box header", func(t *testing.T) {
rawHex := ("000000296b696e64" +
"0000000075726e3a6d7065673a646173" +
"683a726f6c653a32303131006d61696e00")
rawBytes, err := hex.DecodeString(rawHex)
if err != nil {
t.Error(err)
}
buffer := bytes.NewReader(rawBytes)
box, err := DecodeBox(0, buffer)
if err != nil {
t.Errorf("Error decoding kind box: %v", err)
}
kind := box.(*KindBox)
if kind.SchemeURI != "urn:mpeg:dash:role:2011" {
t.Errorf("Expected scheme URI 'urn:mpeg:dash:role:2011', got '%s'", kind.SchemeURI)
}
if kind.Value != "main" {
t.Errorf("Expected value 'main', got '%s'", kind.Value)
}
})
}

0 comments on commit 5f41c88

Please sign in to comment.