diff --git a/core/stream/codec.go b/core/stream/codec.go index 64b10343f..86d1ef8d2 100644 --- a/core/stream/codec.go +++ b/core/stream/codec.go @@ -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 diff --git a/core/stream/decider.go b/core/stream/decider.go index a98588838..6a231f638 100644 --- a/core/stream/decider.go +++ b/core/stream/decider.go @@ -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 } diff --git a/core/stream/legacy_client.go b/core/stream/legacy_client.go index d528eff04..7a55d1b1f 100644 --- a/core/stream/legacy_client.go +++ b/core/stream/legacy_client.go @@ -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 diff --git a/core/stream/legacy_client_test.go b/core/stream/legacy_client_test.go index c525041e9..f03a94675 100644 --- a/core/stream/legacy_client_test.go +++ b/core/stream/legacy_client_test.go @@ -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)}) diff --git a/core/stream/types.go b/core/stream/types.go index 300017c01..68339e886 100644 --- a/core/stream/types.go +++ b/core/stream/types.go @@ -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, diff --git a/server/subsonic/e2e/subsonic_media_retrieval_test.go b/server/subsonic/e2e/subsonic_media_retrieval_test.go index 27e4b568d..a107f388a 100644 --- a/server/subsonic/e2e/subsonic_media_retrieval_test.go +++ b/server/subsonic/e2e/subsonic_media_retrieval_test.go @@ -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")