diff --git a/mp4/av1c_test.go b/mp4/av1c_test.go new file mode 100644 index 00000000..4ec9b417 --- /dev/null +++ b/mp4/av1c_test.go @@ -0,0 +1,18 @@ +package mp4 + +import ( + "testing" + + "github.com/Eyevinn/mp4ff/av1" +) + +func TestEncodeDecodeAvc1(t *testing.T) { + adc := Av1CBox{ + CodecConfRec: av1.CodecConfRec{ + Version: 1, + }, + } + + boxDiffAfterEncodeAndDecode(t, &adc) + +} diff --git a/mp4/cdat_test.go b/mp4/cdat_test.go new file mode 100644 index 00000000..581da22c --- /dev/null +++ b/mp4/cdat_test.go @@ -0,0 +1,11 @@ +package mp4 + +import "testing" + +func TestEncodeDecodeCdat(t *testing.T) { + + cdat := CdatBox{ + Data: []byte{0x01, 0x02, 0x03, 0x04}, + } + boxDiffAfterEncodeAndDecode(t, &cdat) +} diff --git a/mp4/ffmpeg_test.go b/mp4/ffmpeg_test.go index c049216f..7508b400 100644 --- a/mp4/ffmpeg_test.go +++ b/mp4/ffmpeg_test.go @@ -14,8 +14,8 @@ func TestDecodeFFMpeg(t *testing.T) { if err != nil { t.Error(err) } - sr := bits.NewFixedSliceReader(raw) - box, err := DecodeBoxSR(0, sr) + buf := bytes.NewBuffer(raw) + box, err := DecodeBox(0, buf) if err != nil { t.Error(err) } @@ -29,5 +29,10 @@ func TestDecodeFFMpeg(t *testing.T) { if !bytes.Equal(raw, encBytes) { t.Errorf("encoded ffmpeg boxes not same as input") } +} +func TestEncodeData(t *testing.T) { + data := []byte("dummy") + db := &DataBox{Data: data} + boxDiffAfterEncodeAndDecode(t, db) } diff --git a/mp4/mfhd_test.go b/mp4/mfhd_test.go new file mode 100644 index 00000000..3d86a691 --- /dev/null +++ b/mp4/mfhd_test.go @@ -0,0 +1,12 @@ +package mp4 + +import "testing" + +func TestEncodeDecodeMfhd(t *testing.T) { + mfhd := &MfhdBox{ + Version: 0, + Flags: 0, + SequenceNumber: 1, + } + boxDiffAfterEncodeAndDecode(t, mfhd) +} diff --git a/mp4/sample_test.go b/mp4/sample_test.go new file mode 100644 index 00000000..31099922 --- /dev/null +++ b/mp4/sample_test.go @@ -0,0 +1,20 @@ +package mp4 + +import ( + "testing" +) + +func TestSample(t *testing.T) { + s := NewSample(SyncSampleFlags, 1000, 100, -1001) + fs := FullSample{ + Sample: s, + DecodeTime: 1000, + Data: []byte{0x01, 0x02, 0x03, 0x04}, + } + if !s.IsSync() { + t.Error("Expected sync sample") + } + if fs.PresentationTime() != 0 { + t.Error("Expected presentation time 0") + } +} diff --git a/mp4/stsd_test.go b/mp4/stsd_test.go new file mode 100644 index 00000000..db8fad53 --- /dev/null +++ b/mp4/stsd_test.go @@ -0,0 +1,52 @@ +package mp4 + +import ( + "bytes" + "testing" + + "github.com/Eyevinn/mp4ff/aac" +) + +func TestStsd(t *testing.T) { + stsd := StsdBox{} + samplingFrequency := 48000 + asc := &aac.AudioSpecificConfig{ + ObjectType: 2, + ChannelConfiguration: 2, + SamplingFrequency: samplingFrequency, + ExtensionFrequency: 0, + SBRPresentFlag: false, + PSPresentFlag: false, + } + buf := &bytes.Buffer{} + err := asc.Encode(buf) + if err != nil { + t.Error(err) + } + ascBytes := buf.Bytes() + esds := CreateEsdsBox(ascBytes) + mp4a := CreateAudioSampleEntryBox("mp4a", + uint16(asc.ChannelConfiguration), + 16, uint16(samplingFrequency), esds) + btrt := BtrtBox{ + BufferSizeDB: 1536, + MaxBitrate: 96000, + AvgBitrate: 96000, + } + mp4a.AddChild(&btrt) + stsd.AddChild(mp4a) + if len(stsd.Children) != 1 { + t.Error("Expected one child") + } + if stsd.Mp4a == nil { + t.Error("Expected mp4a child") + } + gb := stsd.GetBtrt() + if gb == nil { + t.Error("Expected btrt") + } else { + if *gb != btrt { + t.Errorf("Got btrt %v, expected %v", *gb, btrt) + } + } +} diff --git a/mp4/trak.go b/mp4/trak.go index 5eb7f142..9d4a1ae9 100644 --- a/mp4/trak.go +++ b/mp4/trak.go @@ -121,7 +121,7 @@ func (t *TrakBox) GetSampleData(startSampleNr, endSampleNr uint32) ([]Sample, er if ctts != nil { cto = ctts.GetCompositionTimeOffset(nr) } - samples[nr] = Sample{ + samples[nr-1] = Sample{ Flags: createSampleFlagsFromProgressiveBoxes(stss, sdtp, nr), Dur: stts.GetDur(nr), Size: stbl.Stsz.GetSampleSize(int(nr)), diff --git a/mp4/trak_test.go b/mp4/trak_test.go new file mode 100644 index 00000000..b9a4d941 --- /dev/null +++ b/mp4/trak_test.go @@ -0,0 +1,42 @@ +package mp4 + +import ( + "os" + "testing" +) + +func TestTrakSampleFunctions(t *testing.T) { + testFile := "testdata/bbb_prog_10s.mp4" + f, err := os.Open(testFile) + if err != nil { + t.Fatal(err) + } + defer f.Close() + mf, err := DecodeFile(f) + if err != nil { + t.Fatal(err) + } + moov := mf.Moov + traks := moov.Traks + if len(traks) != 2 { + t.Fatalf("expected 2 tracks, got %d", len(traks)) + } + trak := traks[0] + if trak.Tkhd.TrackID != 1 { + t.Fatalf("expected trackID 1, got %d", trak.Tkhd.TrackID) + } + first2Samples, err := trak.GetSampleData(1, 2) + if err != nil { + t.Fatal(err) + } + if len(first2Samples) != 2 { + t.Fatalf("expected 2 samples, got %d", len(first2Samples)) + } + ranges, err := trak.GetRangesForSampleInterval(1, 2) + if err != nil { + t.Fatal(err) + } + if len(ranges) != 1 { + t.Fatalf("expected 1 range, got %d", len(ranges)) + } +} diff --git a/mp4/version_test.go b/mp4/version_test.go new file mode 100644 index 00000000..26dd3949 --- /dev/null +++ b/mp4/version_test.go @@ -0,0 +1,8 @@ +package mp4 + +import "fmt" + +func ExampleGetVersion() { + fmt.Println(GetVersion()) + // Output: v0.46, date: 2024-08-08 +} diff --git a/sei/sei5.go b/sei/sei5.go index 168594a6..2fb0a528 100644 --- a/sei/sei5.go +++ b/sei/sei5.go @@ -8,7 +8,7 @@ import ( // UnregisteredSEI is SEI message of type 5. type UnregisteredSEI struct { UUID []byte - payload []byte + payload []byte // raw rbsp payload including UUID } // Type returns SEI payload type. diff --git a/sei/sei5_test.go b/sei/sei5_test.go new file mode 100644 index 00000000..cd4f3187 --- /dev/null +++ b/sei/sei5_test.go @@ -0,0 +1,31 @@ +package sei + +import ( + "testing" +) + +func TestSEI5_UnregisteredType(t *testing.T) { + raw := []byte{0x00, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07, + 0x08, 0x09, 0x0a, 0x0b, 0x0c, 0x0d, 0x0e, 0x0f, + 0x40, 0x40, 0x40, 0x40} + + us, err := DecodeUserDataUnregisteredSEI(&SEIData{payloadType: SEIUserDataUnregisteredType, + payload: raw}) + if err != nil { + t.Error("Error decoding SEIUserDataUnregisteredType") + } + if us.Type() != SEIUserDataUnregisteredType { + t.Error("Expected SEIUserDataUnregisteredType") + } + if us.Size() != 20 { + t.Errorf("Expected size 20, got %d", us.Size()) + } + if string(us.Payload()) != string(raw) { + t.Errorf("Unexpected payload %v, expected %v", us.Payload(), raw) + } + wantedString := "SEI type 5, size=20, uuid=\"000102030405060708090a0b0c0d0e0f\", payload=\"@@@@\"" + if wantedString != us.String() { + t.Errorf("Unexpected string %q, expected %q", us.String(), wantedString) + } + +}