Skip to content

Commit

Permalink
Merge pull request #172 from abema/boxstring
Browse files Browse the repository at this point in the history
Fix boxstring null-character issue #171
  • Loading branch information
sunfish-shogi authored Jan 14, 2025
2 parents 89fe2fe + 7d53424 commit ec6c654
Show file tree
Hide file tree
Showing 4 changed files with 38 additions and 27 deletions.
16 changes: 8 additions & 8 deletions box_types_iso14496_30.go
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ func init() {

type WebVTTConfigurationBox struct {
Box
Config string `mp4:"0,string"`
Config string `mp4:"0,boxstring"`
}

func (WebVTTConfigurationBox) GetType() BoxType {
Expand All @@ -23,7 +23,7 @@ func (WebVTTConfigurationBox) GetType() BoxType {

type WebVTTSourceLabelBox struct {
Box
SourceLabel string `mp4:"0,string"`
SourceLabel string `mp4:"0,boxstring"`
}

func (WebVTTSourceLabelBox) GetType() BoxType {
Expand Down Expand Up @@ -75,7 +75,7 @@ func (CueSourceIDBox) GetType() BoxType {

type CueTimeBox struct {
Box
CueCurrentTime string `mp4:"0,string"`
CueCurrentTime string `mp4:"0,boxstring"`
}

func (CueTimeBox) GetType() BoxType {
Expand All @@ -84,7 +84,7 @@ func (CueTimeBox) GetType() BoxType {

type CueIDBox struct {
Box
CueId string `mp4:"0,string"`
CueId string `mp4:"0,boxstring"`
}

func (CueIDBox) GetType() BoxType {
Expand All @@ -93,7 +93,7 @@ func (CueIDBox) GetType() BoxType {

type CueSettingsBox struct {
Box
Settings string `mp4:"0,string"`
Settings string `mp4:"0,boxstring"`
}

func (CueSettingsBox) GetType() BoxType {
Expand All @@ -102,7 +102,7 @@ func (CueSettingsBox) GetType() BoxType {

type CuePayloadBox struct {
Box
CueText string `mp4:"0,string"`
CueText string `mp4:"0,boxstring"`
}

func (CuePayloadBox) GetType() BoxType {
Expand All @@ -119,9 +119,9 @@ func (VTTEmptyCueBox) GetType() BoxType {

type VTTAdditionalTextBox struct {
Box
CueAdditionalText string `mp4:"0,string"`
CueAdditionalText string `mp4:"0,boxstring"`
}

func (VTTAdditionalTextBox) GetType() BoxType {
return BoxTypeVtta()
}
}
34 changes: 17 additions & 17 deletions box_types_iso14496_30_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ func TestBoxTypesISO14496_30(t *testing.T) {
Config: "WEBVTT\n",
},
dst: &WebVTTConfigurationBox{},
bin: []byte{'W', 'E', 'B', 'V', 'T', 'T', '\n', 0 },
bin: []byte{'W', 'E', 'B', 'V', 'T', 'T', '\n'},
str: `Config="WEBVTT."`,
},
{
Expand All @@ -33,7 +33,7 @@ func TestBoxTypesISO14496_30(t *testing.T) {
SourceLabel: "Source",
},
dst: &WebVTTSourceLabelBox{},
bin: []byte{'S', 'o', 'u', 'r', 'c', 'e', 0 },
bin: []byte{'S', 'o', 'u', 'r', 'c', 'e'},
str: `SourceLabel="Source"`,
},
{
Expand All @@ -45,23 +45,23 @@ func TestBoxTypesISO14496_30(t *testing.T) {
},
},
dst: &WVTTSampleEntry{SampleEntry: SampleEntry{AnyTypeBox: AnyTypeBox{Type: StrToBoxType("wvtt")}}},
bin: []byte{ 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x12, 0x34 },
bin: []byte{0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x12, 0x34},
str: `DataReferenceIndex=4660`,
},
{
name: "vttc",
src: &VTTCueBox{},
dst: &VTTCueBox{},
bin: []byte(nil),
str: ``,
src: &VTTCueBox{},
dst: &VTTCueBox{},
bin: []byte(nil),
str: ``,
},
{
name: "vsid",
src: &CueSourceIDBox{
SourceId: 0,
},
dst: &CueSourceIDBox{},
bin: []byte{ 0, 0, 0, 0 },
bin: []byte{0, 0, 0, 0},
str: `SourceId=0`,
},
{
Expand All @@ -70,7 +70,7 @@ func TestBoxTypesISO14496_30(t *testing.T) {
CueCurrentTime: "00:00:00.000",
},
dst: &CueTimeBox{},
bin: []byte{'0', '0', ':', '0', '0', ':', '0', '0', '.', '0', '0', '0', 0 },
bin: []byte{'0', '0', ':', '0', '0', ':', '0', '0', '.', '0', '0', '0'},
str: `CueCurrentTime="00:00:00.000"`,
},
{
Expand All @@ -79,7 +79,7 @@ func TestBoxTypesISO14496_30(t *testing.T) {
CueId: "example_id",
},
dst: &CueIDBox{},
bin: []byte{'e', 'x', 'a', 'm', 'p', 'l', 'e', '_', 'i', 'd', 0 },
bin: []byte{'e', 'x', 'a', 'm', 'p', 'l', 'e', '_', 'i', 'd'},
str: `CueId="example_id"`,
},
{
Expand All @@ -88,7 +88,7 @@ func TestBoxTypesISO14496_30(t *testing.T) {
Settings: "line=0",
},
dst: &CueSettingsBox{},
bin: []byte{'l', 'i', 'n', 'e', '=', '0', 0 },
bin: []byte{'l', 'i', 'n', 'e', '=', '0'},
str: `Settings="line=0"`,
},
{
Expand All @@ -97,23 +97,23 @@ func TestBoxTypesISO14496_30(t *testing.T) {
CueText: "sample",
},
dst: &CuePayloadBox{},
bin: []byte{'s', 'a', 'm', 'p', 'l', 'e', 0 },
bin: []byte{'s', 'a', 'm', 'p', 'l', 'e'},
str: `CueText="sample"`,
},
{
name: "vtte",
src: &VTTEmptyCueBox{},
dst: &VTTEmptyCueBox{},
bin: []byte(nil),
str: ``,
src: &VTTEmptyCueBox{},
dst: &VTTEmptyCueBox{},
bin: []byte(nil),
str: ``,
},
{
name: "vtta",
src: &VTTAdditionalTextBox{
CueAdditionalText: "test",
},
dst: &VTTAdditionalTextBox{},
bin: []byte{'t', 'e', 's', 't', 0},
bin: []byte{'t', 'e', 's', 't'},
str: `CueAdditionalText="test"`,
},
}
Expand Down
8 changes: 8 additions & 0 deletions field.go
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@ const (
fieldVarint // 8
fieldSizeDynamic // 9
fieldLengthDynamic // 10
fieldBoxString // 11 - non-null-terminated string (14496-30)
)

type field struct {
Expand Down Expand Up @@ -69,6 +70,9 @@ func buildFieldsStruct(t reflect.Type) []*field {
continue
}
f := buildField(t.Field(i).Name, tag)
if f.is(fieldBoxString) && i != t.NumField()-1 {
fmt.Fprint(os.Stderr, "go-mp4: boxstring must be the last field!!\n")
}
f.children = buildFieldsAny(ft)
fs = append(fs, f)
}
Expand Down Expand Up @@ -112,6 +116,10 @@ func buildField(fieldName string, tag string) *field {
}
}

if _, contained := tagMap["boxstring"]; contained {
f.set(fieldBoxString)
}

if _, contained := tagMap["varint"]; contained {
f.set(fieldVarint)
}
Expand Down
7 changes: 5 additions & 2 deletions marshaller.go
Original file line number Diff line number Diff line change
Expand Up @@ -85,7 +85,7 @@ func (m *marshaller) marshal(v reflect.Value, fi *fieldInstance) error {
case reflect.Bool:
return m.marshalBool(v, fi)
case reflect.String:
return m.marshalString(v)
return m.marshalString(v, fi)
default:
return fmt.Errorf("unsupported type: %s", v.Type().Kind())
}
Expand Down Expand Up @@ -231,14 +231,17 @@ func (m *marshaller) marshalBool(v reflect.Value, fi *fieldInstance) error {
return nil
}

func (m *marshaller) marshalString(v reflect.Value) error {
func (m *marshaller) marshalString(v reflect.Value, fi *fieldInstance) error {
data := []byte(v.String())
for _, b := range data {
if err := m.writer.WriteBits([]byte{b}, 8); err != nil {
return err
}
m.wbits += 8
}
if fi.is(fieldBoxString) {
return nil
}
// null character
if err := m.writer.WriteBits([]byte{0x00}, 8); err != nil {
return err
Expand Down

0 comments on commit ec6c654

Please sign in to comment.