mirror of
https://github.com/navidrome/navidrome.git
synced 2026-11-24 03:16:52 +03:00
150 lines
5.2 KiB
Go
150 lines
5.2 KiB
Go
package stream
|
|
|
|
import (
|
|
"slices"
|
|
"strings"
|
|
)
|
|
|
|
// normalizeProbeCodec maps ffprobe codec_name values to the simplified internal
|
|
// codec names used throughout Navidrome (matching inferCodecFromSuffix output).
|
|
// Most ffprobe names match directly; this handles the exceptions.
|
|
func normalizeProbeCodec(codec string) string {
|
|
c := strings.ToLower(codec)
|
|
// DSD variants: dsd_lsbf_planar, dsd_msbf_planar, dsd_lsbf, dsd_msbf
|
|
if strings.HasPrefix(c, "dsd") {
|
|
return "dsd"
|
|
}
|
|
// PCM variants: pcm_s16le, pcm_s24le, pcm_s32be, pcm_f32le, etc.
|
|
if strings.HasPrefix(c, "pcm_") {
|
|
return "pcm"
|
|
}
|
|
return c
|
|
}
|
|
|
|
// isLosslessFormat returns true if the format is a known lossless audio codec/format.
|
|
// Detection is based on codec name only, not bit depth — some lossy codecs (e.g. ADPCM)
|
|
// report non-zero bits_per_sample in ffprobe, so bit depth alone is not a reliable signal.
|
|
//
|
|
// Note: core/ffmpeg has a separate isLosslessOutputFormat that covers only formats
|
|
// ffmpeg can produce as output (a smaller set).
|
|
func isLosslessFormat(format string) bool {
|
|
switch strings.ToLower(format) {
|
|
case "flac", "alac", "wav", "aiff", "ape", "wv", "wavpack", "tta", "tak", "shn", "dsd", "pcm":
|
|
return true
|
|
}
|
|
return false
|
|
}
|
|
|
|
// normalizeSourceSampleRate adjusts a sample rate read from tags for codecs that
|
|
// store it differently than PCM. Currently handles DSD (÷8):
|
|
// DSD64=2822400→352800, DSD128=5644800→705600, etc.
|
|
// ffprobe already reports DSD at this rate, so probe data needs no adjustment.
|
|
// For other codecs, returns the rate unchanged.
|
|
func normalizeSourceSampleRate(sampleRate int, codec string) int {
|
|
if strings.EqualFold(codec, "dsd") && sampleRate > 0 {
|
|
return sampleRate / 8
|
|
}
|
|
return sampleRate
|
|
}
|
|
|
|
// dsdMaxPCMSampleRate caps DSD transcoded to lossless PCM. It divides every
|
|
// 44.1kHz-based DSD rate exactly, DSD carries mostly its own shaped noise above
|
|
// it, and many clients cannot play 352.8kHz PCM.
|
|
const dsdMaxPCMSampleRate = 88200
|
|
|
|
// targetSampleRate returns the sample rate for a transcoded stream before codec
|
|
// and client limits are applied, with DSD capped when capDSD is set.
|
|
func targetSampleRate(srcSampleRate int, srcCodec string, capDSD bool) int {
|
|
if capDSD && strings.EqualFold(srcCodec, "dsd") {
|
|
return min(srcSampleRate, dsdMaxPCMSampleRate)
|
|
}
|
|
return srcSampleRate
|
|
}
|
|
|
|
// 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(src *Details, targetIsLossless bool) int {
|
|
switch {
|
|
case !targetIsLossless:
|
|
return 0
|
|
case strings.EqualFold(src.Codec, "dsd"):
|
|
return 24
|
|
case !src.IsLossless:
|
|
return 16 // the most widely supported depth
|
|
}
|
|
return src.BitDepth
|
|
}
|
|
|
|
// codecFixedOutputSampleRate returns the mandatory output sample rate for codecs
|
|
// that always resample regardless of input (e.g., Opus always outputs 48000Hz).
|
|
// Returns 0 if the codec has no fixed output rate.
|
|
func codecFixedOutputSampleRate(codec string) int {
|
|
switch strings.ToLower(codec) {
|
|
case "opus":
|
|
return 48000
|
|
}
|
|
return 0
|
|
}
|
|
|
|
// codecSampleRates lists, in ascending order, the only sample rates some
|
|
// encoders accept. ffmpeg's libmp3lame and aac encoders exit with an error for
|
|
// any other rate (e.g. 32728Hz from Nintendo 3DS rips).
|
|
var codecSampleRates = map[string][]int{
|
|
"mp3": {8000, 11025, 12000, 16000, 22050, 24000, 32000, 44100, 48000},
|
|
"aac": {7350, 8000, 11025, 12000, 16000, 22050, 24000, 32000, 44100, 48000, 64000, 88200, 96000},
|
|
}
|
|
|
|
// codecSampleRate returns a sample rate the codec's encoder accepts: the
|
|
// lowest supported rate at or above sampleRate, or the codec's highest rate if
|
|
// sampleRate is above it. Codecs with no fixed set, and a zero (unknown) rate,
|
|
// are returned unchanged.
|
|
func codecSampleRate(codec string, sampleRate int) int {
|
|
rates := codecSampleRates[strings.ToLower(codec)]
|
|
if len(rates) == 0 || sampleRate <= 0 {
|
|
return sampleRate
|
|
}
|
|
for _, r := range rates {
|
|
if r >= sampleRate {
|
|
return r
|
|
}
|
|
}
|
|
return rates[len(rates)-1]
|
|
}
|
|
|
|
// codecAcceptsSampleRate reports whether the codec's encoder accepts sampleRate.
|
|
// Codecs with no fixed set accept any rate.
|
|
func codecAcceptsSampleRate(codec string, sampleRate int) bool {
|
|
rates := codecSampleRates[strings.ToLower(codec)]
|
|
return len(rates) == 0 || slices.Contains(rates, sampleRate)
|
|
}
|
|
|
|
// codecSampleRateAtOrBelow returns the highest rate the codec's encoder accepts
|
|
// that is not above sampleRate, and false if there is none. Codecs with no
|
|
// fixed set return sampleRate unchanged.
|
|
func codecSampleRateAtOrBelow(codec string, sampleRate int) (int, bool) {
|
|
rates := codecSampleRates[strings.ToLower(codec)]
|
|
if len(rates) == 0 {
|
|
return sampleRate, true
|
|
}
|
|
for _, r := range slices.Backward(rates) {
|
|
if r <= sampleRate {
|
|
return r, true
|
|
}
|
|
}
|
|
return 0, false
|
|
}
|
|
|
|
// codecMaxChannels returns the hard maximum number of audio channels a codec
|
|
// supports. Returns 0 if the codec has no hard limit (or is unknown), in which
|
|
// case the source/profile constraints applied upstream are authoritative.
|
|
func codecMaxChannels(codec string) int {
|
|
switch strings.ToLower(codec) {
|
|
case "mp3":
|
|
return 2
|
|
case "opus":
|
|
return 8
|
|
}
|
|
return 0
|
|
}
|