mirror of
https://github.com/navidrome/navidrome.git
synced 2026-08-01 07:21:17 +00:00
fix(subsonic): omit bit depth for lossy targets in transcode decision (#5768)
getTranscodeDecision was copying the source file's bit depth into the transcodeStream details, so a 24-bit FLAC negotiated to Opus reported audioBitdepth=24. Lossy codecs (Opus, MP3, AAC) have no PCM bit depth, and ffmpeg only honors a bit depth constraint (-sample_fmt) for lossless outputs, so the value was both meaningless and misleading to clients that use it as a quality indicator. Only set the transcoded stream's bit depth when the target format is lossless; a zero value omits audioBitdepth from the response. This also makes audioBitdepth codec limitations a no-op for lossy targets instead of rejecting the profile. Lossless targets (e.g. FLAC->FLAC downconvert) keep reporting and clamping bit depth as before.
This commit is contained in:
parent
a5efba9a08
commit
6b9f85efcc
@ -43,14 +43,17 @@ func normalizeSourceSampleRate(sampleRate int, codec string) int {
|
||||
return sampleRate
|
||||
}
|
||||
|
||||
// normalizeSourceBitDepth adjusts the source bit depth for codecs that use
|
||||
// non-standard bit depths. Currently handles DSD (1-bit → 24-bit PCM, which is
|
||||
// what ffmpeg produces). For other codecs, returns the depth unchanged.
|
||||
func normalizeSourceBitDepth(bitDepth int, codec string) int {
|
||||
if strings.EqualFold(codec, "dsd") && bitDepth == 1 {
|
||||
// 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
|
||||
// adjusted to the 24-bit PCM that ffmpeg produces.
|
||||
func targetBitDepth(srcBitDepth int, srcCodec string, targetIsLossless bool) int {
|
||||
if !targetIsLossless {
|
||||
return 0
|
||||
}
|
||||
if strings.EqualFold(srcCodec, "dsd") && srcBitDepth == 1 {
|
||||
return 24
|
||||
}
|
||||
return bitDepth
|
||||
return srcBitDepth
|
||||
}
|
||||
|
||||
// codecFixedOutputSampleRate returns the mandatory output sample rate for codecs
|
||||
|
||||
@ -269,7 +269,7 @@ func (s *deciderService) computeTranscodedStream(ctx context.Context, src *Detai
|
||||
Codec: strings.ToLower(profile.AudioCodec),
|
||||
SampleRate: normalizeSourceSampleRate(src.SampleRate, src.Codec),
|
||||
Channels: src.Channels,
|
||||
BitDepth: normalizeSourceBitDepth(src.BitDepth, src.Codec),
|
||||
BitDepth: targetBitDepth(src.BitDepth, src.Codec, targetIsLossless),
|
||||
IsLossless: targetIsLossless,
|
||||
}
|
||||
if ts.Codec == "" {
|
||||
|
||||
@ -656,6 +656,44 @@ var _ = Describe("Decider", func() {
|
||||
Expect(decision.TargetBitDepth).To(Equal(24))
|
||||
})
|
||||
|
||||
It("omits bit depth when transcoding to a lossy format", func() {
|
||||
mf := withProbe(&model.MediaFile{ID: "1", Suffix: "flac", Codec: "FLAC", BitRate: 1000, Channels: 2, SampleRate: 96000, BitDepth: new(24)})
|
||||
ci := &ClientInfo{
|
||||
MaxTranscodingAudioBitrate: 320,
|
||||
TranscodingProfiles: []Profile{
|
||||
{Container: "opus", AudioCodec: "opus", Protocol: ProtocolHTTP},
|
||||
},
|
||||
}
|
||||
decision, err := svc.MakeDecision(ctx, mf, ci, TranscodeOptions{})
|
||||
Expect(err).ToNot(HaveOccurred())
|
||||
Expect(decision.CanTranscode).To(BeTrue())
|
||||
Expect(decision.TranscodeStream.BitDepth).To(BeZero())
|
||||
Expect(decision.TargetBitDepth).To(BeZero())
|
||||
})
|
||||
|
||||
It("ignores audioBitdepth limitation when transcoding to a lossy format", func() {
|
||||
mf := withProbe(&model.MediaFile{ID: "1", Suffix: "flac", Codec: "FLAC", BitRate: 1000, Channels: 2, SampleRate: 96000, BitDepth: new(24)})
|
||||
ci := &ClientInfo{
|
||||
MaxTranscodingAudioBitrate: 320,
|
||||
TranscodingProfiles: []Profile{
|
||||
{Container: "opus", AudioCodec: "opus", Protocol: ProtocolHTTP},
|
||||
},
|
||||
CodecProfiles: []CodecProfile{
|
||||
{
|
||||
Type: CodecProfileTypeAudio,
|
||||
Name: "opus",
|
||||
Limitations: []Limitation{
|
||||
{Name: LimitationAudioBitdepth, Comparison: ComparisonGreaterThanEqual, Values: []string{"32"}, Required: true},
|
||||
},
|
||||
},
|
||||
},
|
||||
}
|
||||
decision, err := svc.MakeDecision(ctx, mf, ci, TranscodeOptions{})
|
||||
Expect(err).ToNot(HaveOccurred())
|
||||
Expect(decision.CanTranscode).To(BeTrue())
|
||||
Expect(decision.TranscodeStream.BitDepth).To(BeZero())
|
||||
})
|
||||
|
||||
It("rejects transcoding profile when GreaterThanEqual cannot be satisfied", func() {
|
||||
mf := withProbe(&model.MediaFile{ID: "1", Suffix: "flac", Codec: "FLAC", BitRate: 1000, Channels: 2, SampleRate: 44100, BitDepth: new(16)})
|
||||
ci := &ClientInfo{
|
||||
@ -695,9 +733,9 @@ var _ = Describe("Decider", func() {
|
||||
// DSD64 2822400 / 8 = 352800, capped by MP3 max of 48000
|
||||
Expect(decision.TranscodeStream.SampleRate).To(Equal(48000))
|
||||
Expect(decision.TargetSampleRate).To(Equal(48000))
|
||||
// DSD 1-bit → 24-bit PCM
|
||||
Expect(decision.TranscodeStream.BitDepth).To(Equal(24))
|
||||
Expect(decision.TargetBitDepth).To(Equal(24))
|
||||
// MP3 is lossy: no bit depth on the transcoded stream
|
||||
Expect(decision.TranscodeStream.BitDepth).To(BeZero())
|
||||
Expect(decision.TargetBitDepth).To(BeZero())
|
||||
})
|
||||
|
||||
It("converts DSD sample rate for FLAC target without codec limit", func() {
|
||||
|
||||
Loading…
x
Reference in New Issue
Block a user