diff --git a/internal/sessions/media.go b/internal/sessions/media.go index d72f79e74..3d7ee412f 100644 --- a/internal/sessions/media.go +++ b/internal/sessions/media.go @@ -779,7 +779,7 @@ func (b *Broker) serveMaster(w http.ResponseWriter, r *http.Request, s *brokerSe if bw <= 0 { bw = 8000 } - fmt.Fprintf(&body, "#EXT-X-STREAM-INF:BANDWIDTH=%d%s\nbase/index.m3u8\n", bw*1000, transcode.HLSVariantAttributes(ps.Info, dec)) + fmt.Fprintf(&body, "#EXT-X-STREAM-INF:BANDWIDTH=%d%s%s\nbase/index.m3u8\n", bw*1000, transcode.HLSVariantGeometry(ps.Info, dec), transcode.HLSVariantAttributes(ps.Info, dec)) } if body.String() == "#EXTM3U\n" { http.Error(w, "no playable rendition", http.StatusServiceUnavailable) diff --git a/internal/sessions/profile_decision.go b/internal/sessions/profile_decision.go index 0e7eeda84..8c0465077 100644 --- a/internal/sessions/profile_decision.go +++ b/internal/sessions/profile_decision.go @@ -755,6 +755,11 @@ func profileDeliveryAllowed(p *core.ClientCapabilityProfiles, d core.Decision, i } else if d.StripDolbyVision { stampEncodedVideoRange(s, d) } + if d.Container == transcode.ContainerFMP4 { + if tag := fmp4VideoTag(info.Video(), d); tag != "" { + s.CodecTag = tag + } + } } else if s.Type == "audio" && s.Index == d.AudioStream { s.Codec = d.AudioCodec if !d.AudioCopy { @@ -792,6 +797,25 @@ func profileDeliveryAllowed(p *core.ClientCapabilityProfiles, d core.Decision, i return false } +// fmp4VideoTag is the sample entry fMP4 segments carry for the output video, +// as the transcoder's arguments write it: HEVC is tagged hvc1 (dvh1 for a +// copied Dolby Vision profile 5 source) and H.264 avc1, whatever the source +// container used. A required VideoCodecTag condition, such as AVPlayer's +// hvc1/dvh1, is evaluated against this rather than the source's tag, which +// Matroska does not have. +func fmp4VideoTag(source *core.StreamInfo, d core.Decision) string { + switch d.VideoCodec { + case "hevc": + if d.VideoCopy && source != nil && source.DolbyVision && source.DolbyVisionProfile == 5 { + return "dvh1" + } + return "hvc1" + case "h264": + return "avc1" + } + return "" +} + // constrainedH264 describes only facts guaranteed by the selected encoder // contract. Unknown geometry or rate stays unknown, so required predicates // still fail closed. diff --git a/internal/sessions/swiftfin_hevc_tag_test.go b/internal/sessions/swiftfin_hevc_tag_test.go new file mode 100644 index 000000000..1ac10e218 --- /dev/null +++ b/internal/sessions/swiftfin_hevc_tag_test.go @@ -0,0 +1,81 @@ +package sessions + +import ( + "context" + "encoding/json" + "testing" + + "github.com/braedonsaunders/ether/internal/core" + "github.com/braedonsaunders/ether/internal/frontend" + "github.com/braedonsaunders/ether/internal/frontend/kit/caps" +) + +// Swiftfin's AVPlayer profile (MediaPlayerType+AVPlayer.swift): HEVC is +// accepted only with an hvc1 or dvh1 sample entry, a required condition. +const swiftfinAVPlayerProfile = `{ + "DirectPlayProfiles": [ + {"Type": "Video", "Container": "mp4,m4v", "VideoCodec": "h264,mpeg4,hevc", "AudioCodec": "aac,ac3,alac,eac3,flac"}, + {"Type": "Video", "Container": "mov", "VideoCodec": "h264,mjpeg,mpeg4,hevc", "AudioCodec": "aac,ac3,alac,eac3,mp3,pcm_s16be,pcm_s16le,pcm_s24be,pcm_s24le"} + ], + "TranscodingProfiles": [ + {"Type": "Video", "Container": "mp4", "Protocol": "hls", "Context": "Streaming", "VideoCodec": "hevc,h264,mpeg4", "AudioCodec": "aac,ac3,alac,eac3,flac", "MaxAudioChannels": "8", "MinSegments": 2, "BreakOnNonKeyFrames": true, "EnableSubtitlesInManifest": true} + ], + "CodecProfiles": [ + {"Type": "Video", "Codec": "h264", "Conditions": [ + {"Condition": "EqualsAny", "Property": "VideoRangeType", "Value": "SDR|DOVIWithSDR", "IsRequired": false}, + {"Condition": "NotEquals", "Property": "IsInterlaced", "Value": "true", "IsRequired": false} + ]}, + {"Type": "Video", "Codec": "hevc", "Conditions": [ + {"Condition": "NotEquals", "Property": "IsAnamorphic", "Value": "true", "IsRequired": false}, + {"Condition": "EqualsAny", "Property": "VideoProfile", "Value": "main|main 10", "IsRequired": false}, + {"Condition": "LessThanEqual", "Property": "VideoLevel", "Value": "175", "IsRequired": false}, + {"Condition": "NotEquals", "Property": "IsInterlaced", "Value": "true", "IsRequired": false}, + {"Condition": "EqualsAny", "Property": "VideoRangeType", "Value": "SDR|DOVIWithSDR|HLG|DOVIWithHLG|HDR10|HDR10Plus|DOVIWithHDR10|DOVIWithHDR10Plus|DOVIWithEL|DOVIWithELHDR10Plus|DOVI", "IsRequired": false}, + {"Condition": "EqualsAny", "Property": "VideoCodecTag", "Value": "hvc1|dvh1", "IsRequired": true}, + {"Condition": "LessThanEqual", "Property": "VideoFramerate", "Value": "60", "IsRequired": true} + ]} + ], + "SubtitleProfiles": [ + {"Format": "cc_dec", "Method": "Embed"}, {"Format": "ttml", "Method": "Embed"}, + {"Format": "pgssub", "Method": "Encode"}, {"Format": "vtt", "Method": "Hls"} + ] +}` + +// An HDR10 HEVC MKV has no sample entry tag. Segmented into fMP4 it is +// written as hvc1, so it satisfies Swiftfin's required VideoCodecTag and is +// copied, not re-encoded to SDR H.264. +func TestSwiftfinAVPlayerCopiesHEVCIntoFMP4(t *testing.T) { + var profile caps.DeviceProfile + if err := json.Unmarshal([]byte(swiftfinAVPlayerProfile), &profile); err != nil { + t.Fatal(err) + } + client, err := (caps.Builder{}).Build(context.Background(), caps.DeviceInfo{Product: "Swiftfin tvOS", Platform: "tvOS", Device: "Apple TV"}, profile, nil, "Swiftfin/1.7.1") + if err != nil { + t.Fatal(err) + } + no := false + hdr10 := core.StreamInfo{Index: 0, Type: "video", Codec: "hevc", Profile: "Main 10", Level: 153, BitDepth: 10, PixelFormat: "yuv420p10le", + ColorTransfer: "smpte2084", ColorPrimaries: "bt2020", ColorSpace: "bt2020nc", Width: 3840, Height: 2160, FrameRate: 23.976, + DolbyVisionConfigurationPresent: &no, HDR10Plus: &no} + info := core.MediaInfo{Container: "mkv", BitrateKbps: 40000, Streams: []core.StreamInfo{ + hdr10, + {Index: 1, Type: "audio", Codec: "dts", Channels: 6, SampleRate: 48000}, + }} + broker := &Broker{deps: BrokerDeps{SurfacePolicy: testSessionPolicy, Transcoder: profileTranscoder{}}} + req := frontend.PlanRequest{Protocol: "jellyfin", Caps: client, Intent: frontend.PlayIntent{Options: core.DecideOptions{DirectPlay: true, DirectStream: true, AudioStream: 1, SubtitleStream: -1}}} + d, err := broker.decideProfile(&core.PlaySession{Info: info}, req) + if err != nil { + t.Fatal(err) + } + if !d.VideoCopy || d.VideoCodec != "hevc" || d.ToneMap || d.Container != "fmp4" { + t.Fatalf("HDR10 HEVC MKV: %s (%+v)", d.Reason, d) + } + // A source already tagged hev1 is rewritten to hvc1 as well. + hev1 := info + hev1.Container = "mp4" + hev1.Streams = []core.StreamInfo{hdr10, info.Streams[1]} + hev1.Streams[0].CodecTag = "hev1" + if d, err := broker.decideProfile(&core.PlaySession{Info: hev1}, req); err != nil || !d.VideoCopy || d.VideoCodec != "hevc" { + t.Fatalf("hev1 HEVC: %v %s (%+v)", err, d.Reason, d) + } +} diff --git a/internal/transcode/hls_attributes.go b/internal/transcode/hls_attributes.go index 8568db764..19e189c9f 100644 --- a/internal/transcode/hls_attributes.go +++ b/internal/transcode/hls_attributes.go @@ -15,6 +15,28 @@ import ( // One transport is deliberately different: for the Plex for Android Matroska // HLS session (d.AndroidMatroskaHLS with a Matroska container) CODECS and // SUPPLEMENTAL-CODECS are left out on purpose. See the comment at the omission before changing that. +// HLSVariantGeometry returns the RESOLUTION and FRAME-RATE attributes (each +// with its leading comma) for the output video the decision selects, or +// nothing when the size is unknown. Apple's authoring spec requires both on +// every video variant (9.2, 9.15), and tvOS AVPlayer enforces it for HDR: a +// PQ or Dolby Vision variant without them is rejected at the master playlist, +// while SDR is tolerated. +func HLSVariantGeometry(info core.MediaInfo, d core.Decision) string { + v := info.Video() + if v == nil || v.Width <= 0 || v.Height <= 0 { + return "" + } + w, h := v.Width, v.Height + if !d.VideoCopy { + w, h = decisionOutputSize(v, d) + } + attrs := fmt.Sprintf(",RESOLUTION=%dx%d", w, h) + if v.FrameRate > 0 { + attrs += fmt.Sprintf(",FRAME-RATE=%.3f", v.FrameRate) + } + return attrs +} + func HLSVariantAttributes(info core.MediaInfo, d core.Decision) string { var codecs []string videoRange, supplemental := "", "" diff --git a/internal/transcode/hls_geometry_test.go b/internal/transcode/hls_geometry_test.go new file mode 100644 index 000000000..ea14baaed --- /dev/null +++ b/internal/transcode/hls_geometry_test.go @@ -0,0 +1,36 @@ +package transcode + +import ( + "testing" + + "github.com/braedonsaunders/ether/internal/core" +) + +// Apple's HLS authoring spec (9.2, 9.15) requires RESOLUTION and FRAME-RATE on +// every video variant. tvOS AVPlayer enforces it for HDR: a Dolby Vision 8.1 +// copy master without them was fetched and abandoned, never reaching the +// variant playlist, while the same title transcoded to SDR played. +func TestHLSVariantGeometry(t *testing.T) { + dv := core.StreamInfo{Index: 0, Type: "video", Codec: "hevc", Width: 3840, Height: 1600, FrameRate: 23.976} + info := core.MediaInfo{Streams: []core.StreamInfo{dv, {Index: 1, Type: "audio", Codec: "eac3"}}} + cases := []struct { + name string + info core.MediaInfo + d core.Decision + want string + }{ + {"copy keeps source geometry", info, core.Decision{VideoCopy: true, VideoCodec: "hevc"}, ",RESOLUTION=3840x1600,FRAME-RATE=23.976"}, + {"copy ignores a height cap", info, core.Decision{VideoCopy: true, VideoCodec: "hevc", MaxHeight: 1080}, ",RESOLUTION=3840x1600,FRAME-RATE=23.976"}, + {"transcode follows the encoded size", info, core.Decision{VideoCodec: "h264", MaxHeight: 1080}, ",RESOLUTION=2592x1080,FRAME-RATE=23.976"}, + {"unknown frame rate leaves FRAME-RATE out", core.MediaInfo{Streams: []core.StreamInfo{{Type: "video", Codec: "hevc", Width: 1920, Height: 1080}}}, core.Decision{VideoCopy: true}, ",RESOLUTION=1920x1080"}, + {"unknown size writes nothing", core.MediaInfo{Streams: []core.StreamInfo{{Type: "video", Codec: "hevc", FrameRate: 25}}}, core.Decision{VideoCopy: true}, ""}, + {"audio only writes nothing", core.MediaInfo{Streams: []core.StreamInfo{{Type: "audio", Codec: "aac"}}}, core.Decision{}, ""}, + } + for _, tc := range cases { + t.Run(tc.name, func(t *testing.T) { + if got := HLSVariantGeometry(tc.info, tc.d); got != tc.want { + t.Fatalf("got %q, want %q", got, tc.want) + } + }) + } +}