droboticflow/internal/api/annotation_source_movie_clock_test.go

79 lines
2.7 KiB
Go

package api
import (
"bytes"
"encoding/binary"
"fmt"
"math"
"os"
"os/exec"
"path/filepath"
"testing"
)
func TestSourceClockAcceptsEditListTailRoundingWithoutRelaxingFrames(t *testing.T) {
if _, err := exec.LookPath("ffmpeg"); err != nil {
t.Skip("ffmpeg unavailable")
}
for _, count := range []int{6343, 7957, 11239} {
t.Run(fmt.Sprint(count), func(t *testing.T) {
path := filepath.Join(t.TempDir(), "edit-list.mp4")
args := []string{"-v", "error", "-y", "-f", "lavfi", "-i", "color=c=navy:s=32x32:r=30", "-frames:v", fmt.Sprint(count), "-c:v", "libx264", "-preset", "ultrafast", "-bf", "3", "-video_track_timescale", "15360", "-movie_timescale", "1000", "-use_editlist", "1", path}
if out, err := exec.CommandContext(t.Context(), "ffmpeg", args...).CombinedOutput(); err != nil {
t.Fatalf("encode: %v %s", err, out)
}
body, err := os.ReadFile(path)
if err != nil {
t.Fatal(err)
}
at := bytes.Index(body, []byte("elst"))
if at < 4 || at+24 > len(body) || body[at+4] != 0 || binary.BigEndian.Uint32(body[at+8:]) != 1 {
t.Fatal("fixture needs one version-0 edit")
}
writeEndpoint := func(milliseconds uint32) {
binary.BigEndian.PutUint32(body[at+12:], milliseconds)
if err := os.WriteFile(path, body, 0600); err != nil {
t.Fatal(err)
}
}
writeEndpoint(uint32(count * 1000 / 30))
duration, err := ffprobeDurationSeconds(t.Context(), path)
if err != nil {
t.Fatal(err)
}
if delta := float64(count)/30 - duration; delta < .0003 || delta > .0004 {
t.Fatalf("fixture does not reproduce endpoint rounding: %.9f", delta)
}
if err := validateSourceMediaClock(t.Context(), path, count, duration); err != nil {
t.Fatalf("complete CFR source rejected: %v", err)
}
if err := validateSourceMediaClock(t.Context(), path, count-1, duration); err == nil {
t.Fatal("frame/PTS mismatch admitted")
}
writeEndpoint(uint32((count - 1) * 1000 / 30))
duration, err = ffprobeDurationSeconds(t.Context(), path)
if err != nil {
t.Fatal(err)
}
if err := validateSourceMediaClock(t.Context(), path, count, duration); err == nil {
t.Fatal("whole-frame endpoint truncation admitted")
}
})
}
}
func TestSourceMovieClockMalformedInputKeepsStrictTolerance(t *testing.T) {
for _, data := range [][]byte{nil, []byte("not MP4"), {0xff, 0xff, 0xff, 0xff, 'm', 'o', 'o', 'v'}, {0, 0, 0, 1, 'm', 'o', 'o', 'v'}} {
path := filepath.Join(t.TempDir(), "bad.mp4")
if err := os.WriteFile(path, data, 0600); err != nil {
t.Fatal(err)
}
if _, err := sourceMovieClockTick(path); err == nil {
t.Fatal("malformed movie clock accepted")
}
if got := sourceClockEndTolerance(path, 1.0/15360, 1.0/30); math.Abs(got-.0001) > 1e-12 {
t.Fatal(got)
}
}
}