From 133c35ca97df74cecf97626894757c6c0de93c46 Mon Sep 17 00:00:00 2001 From: tanghao Date: Tue, 5 Sep 2023 19:16:02 +0800 Subject: [PATCH] fix: use preallocated slice --- mp4/lou.go | 19 +++++++++++++------ mp4/lou_test.go | 8 ++++---- 2 files changed, 17 insertions(+), 10 deletions(-) diff --git a/mp4/lou.go b/mp4/lou.go index 9431a79e..38899e33 100644 --- a/mp4/lou.go +++ b/mp4/lou.go @@ -29,7 +29,7 @@ type LoudnessBase struct { BsTruePeakLevel int16 MeasurementSystemForTP uint8 ReliabilityForTP uint8 - Measurements []*Measurement + Measurements []Measurement } type Measurement struct { @@ -55,10 +55,16 @@ func decodeLoudnessBaseBoxSR(hdr BoxHeader, startPos uint64, sr bits.SliceReader } var loudnessBaseCount uint8 if b.Version >= 1 { - loudnessBaseCount = 0x3f & sr.ReadUint8() + loudnessInfoTypeAndCount := sr.ReadUint8() + loudnessInfoType := (loudnessInfoTypeAndCount >> 6) & 0x3 + if loudnessInfoType != 0 { + return nil, fmt.Errorf("loudnessInfoType %d not supported", loudnessInfoType) + } + loudnessBaseCount = 0x3f & loudnessInfoTypeAndCount } else { loudnessBaseCount = 1 } + b.LoudnessBases = make([]*LoudnessBase, 0, loudnessBaseCount) for a := uint8(0); a < loudnessBaseCount; a++ { l := &LoudnessBase{} @@ -75,8 +81,9 @@ func decodeLoudnessBaseBoxSR(hdr BoxHeader, startPos uint64, sr bits.SliceReader l.MeasurementSystemForTP = measurementSystemAndReliablityForTP >> 4 l.ReliabilityForTP = measurementSystemAndReliablityForTP & 0x0f measurementCount := sr.ReadUint8() + l.Measurements = make([]Measurement, 0, measurementCount) for i := uint8(0); i < measurementCount; i++ { - m := &Measurement{} + m := Measurement{} m.MethodDefinition = sr.ReadUint8() m.MethodValue = sr.ReadUint8() measurementSystemAndReliablity := sr.ReadUint8() @@ -96,9 +103,9 @@ func (b *loudnessBaseBox) size() uint64 { } for _, l := range b.LoudnessBases { if b.Version >= 1 { - size += 8 + int(len(l.Measurements)*3) + size += 8 + len(l.Measurements)*3 } else { - size += 7 + int(len(l.Measurements)*3) + size += 7 + len(l.Measurements)*3 } } return uint64(size) @@ -140,7 +147,7 @@ func (b *loudnessBaseBox) encodeSW(sw bits.SliceWriter) error { sw.WriteUint8(measurementSystemAndReliablity) } } - return nil + return sw.AccError() } func (b *loudnessBaseBox) info(realBox Box, w io.Writer, specificBoxLevels, indent, indentStep string) error { diff --git a/mp4/lou_test.go b/mp4/lou_test.go index 4d4e1517..dcf688a1 100644 --- a/mp4/lou_test.go +++ b/mp4/lou_test.go @@ -16,7 +16,7 @@ func TestTlou(t *testing.T) { BsTruePeakLevel: 1086, MeasurementSystemForTP: 2, ReliabilityForTP: 3, - Measurements: []*Measurement{ + Measurements: []Measurement{ { MethodDefinition: 1, MethodValue: 121, @@ -39,7 +39,7 @@ func TestTlou(t *testing.T) { BsTruePeakLevel: 1086, MeasurementSystemForTP: 2, ReliabilityForTP: 3, - Measurements: []*Measurement{ + Measurements: []Measurement{ { MethodDefinition: 4, MethodValue: 124, @@ -74,7 +74,7 @@ func TestAlou(t *testing.T) { BsTruePeakLevel: 1086, MeasurementSystemForTP: 2, ReliabilityForTP: 3, - Measurements: []*Measurement{ + Measurements: []Measurement{ { MethodDefinition: 1, MethodValue: 121, @@ -97,7 +97,7 @@ func TestAlou(t *testing.T) { BsTruePeakLevel: 1086, MeasurementSystemForTP: 2, ReliabilityForTP: 3, - Measurements: []*Measurement{ + Measurements: []Measurement{ { MethodDefinition: 4, MethodValue: 124,