fix(transcoding): honor an explicitly requested lossless format for lossy files (#6472)

This commit is contained in:
Deluan Quintão 2026-11-16 14:53:13 -05:00 committed by GitHub
parent 9b959c5404
commit 35948d566d
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
6 changed files with 77 additions and 10 deletions

View File

@ -61,14 +61,16 @@ func targetSampleRate(srcSampleRate int, srcCodec string, capDSD bool) int {
// targetBitDepth returns the bit depth for a transcoded stream: 0 for lossy
// targets (they have no PCM bit depth), otherwise the source depth, with DSD
// (1 bit in tags, 8 in ffprobe) adjusted to the 24-bit PCM that ffmpeg produces.
func targetBitDepth(srcBitDepth int, srcCodec string, targetIsLossless bool) int {
if !targetIsLossless {
func targetBitDepth(src *Details, targetIsLossless bool) int {
switch {
case !targetIsLossless:
return 0
}
if strings.EqualFold(srcCodec, "dsd") {
case strings.EqualFold(src.Codec, "dsd"):
return 24
case !src.IsLossless:
return 16 // the most widely supported depth
}
return srcBitDepth
return src.BitDepth
}
// codecFixedOutputSampleRate returns the mandatory output sample rate for codecs

View File

@ -257,9 +257,12 @@ func (s *deciderService) computeTranscodedStream(ctx context.Context, src *Detai
targetIsLossless := isLosslessFormat(targetFormat)
// Reject lossy to lossless conversion
if !src.IsLossless && targetIsLossless {
log.Trace(ctx, "Skipping transcoding profile: lossy to lossless not allowed", "targetFormat", targetFormat)
lossyToLossless := !src.IsLossless && targetIsLossless
// Lossless output from a lossy source is far larger than the source, so no bitrate cap can be honored
lossyToLosslessAllowed := clientInfo.AllowLossyToLossless && clientInfo.MaxAudioBitrate == 0
if lossyToLossless && !lossyToLosslessAllowed {
log.Trace(ctx, "Skipping transcoding profile: lossy to lossless not allowed", "sourceCodec", src.Codec,
"targetFormat", targetFormat, "maxAudioBitrate", clientInfo.MaxAudioBitrate)
return nil, ""
}
@ -274,7 +277,7 @@ func (s *deciderService) computeTranscodedStream(ctx context.Context, src *Detai
Codec: targetCodec,
SampleRate: targetSampleRate(src.SampleRate, src.Codec, capDSD),
Channels: src.Channels,
BitDepth: targetBitDepth(src.BitDepth, src.Codec, targetIsLossless),
BitDepth: targetBitDepth(src, targetIsLossless),
IsLossless: targetIsLossless,
}
@ -386,7 +389,7 @@ func (s *deciderService) computeBitrate(ctx context.Context, src *Details, targe
return false
}
}
} else {
} else if !targetIsLossless {
ts.Bitrate = src.Bitrate
}

View File

@ -42,6 +42,7 @@ func buildLegacyClientInfo(mf *model.MediaFile, reqFormat string, reqBitRate int
ci.TranscodingProfiles = []Profile{
{Container: targetFormat, AudioCodec: targetFormat, Protocol: ProtocolHTTP},
}
ci.AllowLossyToLossless = reqFormat != ""
if reqBitRate > 0 {
ci.MaxAudioBitrate = reqBitRate
ci.MaxTranscodingAudioBitrate = reqBitRate

View File

@ -285,6 +285,16 @@ var _ = Describe("ResolveRequest", func() {
Expect(req.BitRate).To(Equal(160))
})
It("serves a lossy source raw when the override targets a lossless format", func() {
mf := withProbe(&model.MediaFile{ID: "1", Suffix: "mp3", Codec: "MP3", BitRate: 320, Channels: 2, SampleRate: 44100})
overrideCtx := request.WithTranscoding(ctx, model.Transcoding{TargetFormat: "flac", DefaultBitRate: 0})
decider := svc.(*deciderService)
req := decider.ResolveRequest(overrideCtx, mf, "", 0, 0)
Expect(req.Format).To(Equal("raw"))
})
It("does not apply override when no transcoding is in context", func() {
mf := withProbe(&model.MediaFile{ID: "1", Suffix: "flac", Codec: "FLAC", BitRate: 1000, Channels: 2, SampleRate: 44100})
@ -295,6 +305,40 @@ var _ = Describe("ResolveRequest", func() {
})
})
Context("lossy source with an explicitly requested lossless format", func() {
var mf *model.MediaFile
BeforeEach(func() {
conf.Update(func(c *conf.Options) { c.DefaultDownsamplingFormat = "opus" })
mf = withProbe(&model.MediaFile{ID: "1", Suffix: "ogg", Codec: "Vorbis", BitRate: 320, Channels: 2, SampleRate: 44100})
})
It("transcodes to the requested format", func() {
decider := svc.(*deciderService)
req := decider.ResolveRequest(ctx, mf, "flac", 0, 0)
Expect(req.Format).To(Equal("flac"))
Expect(req.BitRate).To(BeZero())
Expect(req.BitDepth).To(Equal(16))
})
It("falls back to DefaultDownsamplingFormat when a bitrate limit is also requested", func() {
decider := svc.(*deciderService)
req := decider.ResolveRequest(ctx, mf, "flac", 1000, 0)
Expect(req.Format).To(Equal("opus"))
})
It("falls back to DefaultDownsamplingFormat when the player has a bitrate cap", func() {
playerCtx := request.WithPlayer(ctx, model.Player{MaxBitRate: 1000})
decider := svc.(*deciderService)
req := decider.ResolveRequest(playerCtx, mf, "flac", 0, 0)
Expect(req.Format).To(Equal("opus"))
})
})
Context("Player MaxBitRate cap", func() {
It("applies player MaxBitRate cap when client has no limit", func() {
mf := withProbe(&model.MediaFile{ID: "1", Suffix: "flac", Codec: "FLAC", BitRate: 1000, Channels: 2, SampleRate: 44100, BitDepth: new(16)})

View File

@ -38,6 +38,9 @@ type ClientInfo struct {
DirectPlayProfiles []DirectPlayProfile
TranscodingProfiles []Profile
CodecProfiles []CodecProfile
// AllowLossyToLossless is set when a format was requested by name rather than
// negotiated from a preference list, where it would only inflate lossy files.
AllowLossyToLossless bool
}
// CapBitrate lowers the client's declared audio bitrate limits to maxKbps,

View File

@ -97,6 +97,20 @@ var _ = Describe("Media Retrieval Endpoints", Ordered, func() {
Expect(streamerSpy.LastRequest.Format).To(Equal("opus"))
})
It("transcodes a lossy source to an explicitly requested lossless format", func() {
w := doRawReq("stream", "id", trackID, "format", "flac")
Expect(w.Code).To(Equal(http.StatusOK))
Expect(streamerSpy.LastRequest.Format).To(Equal("flac"))
})
It("falls back to the default downsampling format when a bitrate limit is also requested", func() {
w := doRawReq("stream", "id", trackID, "format", "flac", "maxBitRate", "1000")
Expect(w.Code).To(Equal(http.StatusOK))
Expect(streamerSpy.LastRequest.Format).To(Equal("opus"))
})
It("passes timeOffset through", func() {
w := doRawReq("stream", "id", trackID, "format", "opus", "maxBitRate", "128", "timeOffset", "30")