kingslay / KSPlayer

A video player for iOS、macOS、tvOS、visionOS , based on AVPlayer and FFmpeg, support the horizontal, vertical screen. support adjust volume, brightness and seek by slide, SwiftUI, support subtitles.
https://apps.apple.com/app/tracyplayer/id6450770064
GNU General Public License v3.0
1.04k stars 202 forks source link

support Dolby Atmos #189

Closed jartas closed 2 years ago

jartas commented 2 years ago

Hi, thx for fix 4K+HEVC, now is ok but don't supported Dolby Atmos, audio is detected as 5.1 digital. Could you add support for Dolby Atmos. Than you very much

There is sample 4K+EAC3 (Dolby Atmos)

https://iscc.site/video.mkv

kingslay commented 2 years ago

I have yet to find an article on how to set up Dolby Atmos in CoreAudio. I will keep looking. If you find a way to set it up, you can also send me a link

UnknownCoder807 commented 2 years ago

I noticed this issue also mentions 4K-EAC3 - I have issues with this with latest KSPlayer. Is there a fix on the way possibly?

It has no sound and plays at about half speed.

edit: MPV player can play these type of streams (4K-EAC3) without issue, maybe its something in ffmepg?

https://github.com/mpv-player/mpv

Alanko5 commented 2 years ago

Not work, now eac3 play as stereo.

kingslay commented 2 years ago

Not work, now eac3 play as stereo.

You can see what the value of KSPlayerManager.audioPlayerMaximumChannels is, if it is 2, then you need to change it to 6 or 8

Alanko5 commented 2 years ago

Not work, now eac3 play as stereo.

You can see what the value of KSPlayerManager.audioPlayerMaximumChannels is, if it is 2, then you need to change it to 6 or 8

How to set atmos?

kingslay commented 2 years ago

Not work, now eac3 play as stereo.

You can see what the value of KSPlayerManager.audioPlayerMaximumChannels is, if it is 2, then you need to change it to 6 or 8

How to set atmos?

I do not understand

KSPlayerManager.audioPlayerMaximumChannels = 6

Alanko5 commented 2 years ago

Not work, now eac3 play as stereo.

You can see what the value of KSPlayerManager.audioPlayerMaximumChannels is, if it is 2, then you need to change it to 6 or 8

How to set atmos?

I do not understand

KSPlayerManager.audioPlayerMaximumChannels = 6

what should I set for Dolby Atmos? If set: audioPlayerMaximumChannels = 6 -> 5.1 surround audioPlayerMaximumChannels = 8 -> 7.1 surround audioPlayerMaximumChannels = ??? -> Dolby Atmos

kingslay commented 2 years ago

Dolby Atmos is 7.1

Alanko5 commented 2 years ago
Screen
kingslay commented 2 years ago

Does your device support 8 audio tracks?

Alanko5 commented 2 years ago

Does your device support 8 audio tracks?

Dolby atmos support…

kingslay commented 2 years ago

Sorry for this question I may not be able to do you. Because I don't have such a hardware device. You can use google to find out how to set the AVAudioFormat of Dolby atmos

Alanko5 commented 2 years ago

Do you think this will help?

https://developer.apple.com/documentation/coreaudiotypes/coreaudiotype_constants/1572101-audio_channel_layout_tags

var kAudioChannelLayoutTag_Atmos_7_1_4: AudioChannelLayoutTag var kAudioChannelLayoutTag_Atmos_9_1_6: AudioChannelLayoutTag

init(standardFormatWithSampleRate: Double, channelLayout: AVAudioChannelLayout)

But I have no idea how to set it up right.

Alanko5 commented 2 years ago

https://forum.juce.com/t/extending-juce-support-for-atmos-beyond-7-1-2-channel-set-including-in-logic-pro-via-new-layout-tags/48375/16

kAudioChannelLayoutTag_Atmos_5_1_2 = (194U<<16) | 8, ///< L R C LFE Ls Rs Ltm Rtm kAudioChannelLayoutTag_Atmos_5_1_4 = (195U<<16) | 10, ///< L R C LFE Ls Rs Vhl Vhr Ltr Rtr kAudioChannelLayoutTag_Atmos_7_1_2 = (196U<<16) | 10, ///< L R C LFE Ls Rs Rls Rrs Ltm Rtm kAudioChannelLayoutTag_Atmos_7_1_4 = (192U<<16) | 12, ///< L R C LFE Ls Rs Rls Rrs Vhl Vhr Ltr Rtr kAudioChannelLayoutTag_Atmos_9_1_6 = (193U<<16) | 16,

Alanko5 commented 2 years ago

AudioChannelLayout

LayoutConverter

AVSampleBufferAudioRendererSink

CoreAudioLayouts

Alanko5 commented 2 years ago

Do you have any idea how to work with this?

Alanko5 commented 2 years ago

kAudioUnitProperty_SupportedChannelLayoutTags how to use for read value

kAudioUnitProperty_SupportedChannelLayoutTags documentation

supportedChannelLayoutTags documentation

kingslay commented 2 years ago

kAudioUnitProperty_SupportedChannelLayoutTags how to use for read value

kAudioUnitProperty_SupportedChannelLayoutTags documentation

supportedChannelLayoutTags documentation

you can checkout the code for feature/ChannelLayout

Alanko5 commented 2 years ago

you can checkout the code for feature/ChannelLayout

I'm not with Apple TV right now. Later, when I get to her, I'll try this commit.

Alanko5 commented 2 years ago

Not working. Device detect only PCM not DolbyAtmos

https://docplayer.net/38491764-What-s-new-in-core-audio.html

Alanko5 commented 2 years ago

If you use this code before activation audio then layout and format is wrong

if let channelLayout = format.channelLayout {
            KSPlayerManager.channelLayout = channelLayout
            print("actualFormat: \(String(describing: channelLayout.layoutTag))") // set layout as 0
        }

If saved wrong Layout then AudioSwresample use only 2 channels

Alanko5 commented 2 years ago

Setup Audio:

        let maxOut = AVAudioSession.sharedInstance().maximumOutputNumberOfChannels
        try? AVAudioSession.sharedInstance().setPreferredOutputNumberOfChannels(maxOut)

        AVAudioSession.sharedInstance().outputNumberOfChannels // for me tv is 6

        if #available(tvOS 15.0, *), maxOut > 2 {
            try? AVAudioSession.sharedInstance().setSupportsMultichannelContent(true)
        }

then system enable create DTS format: (tv - device: detect Dolby Audio but not Dolby Atmos)

         let streamDescription = format.streamDescription
        if let layout = AVAudioChannelLayout(layoutTag: kAudioChannelLayoutTag_DTS_6_0_A),
           let updateFormat = AVAudioFormat(streamDescription: streamDescription, channelLayout: layout) {

            format = updateFormat
        }

but not enable use Atmos:

         let streamDescription = format.streamDescription
        if let layout = AVAudioChannelLayout(layoutTag: kAudioChannelLayoutTag_Atmos_5_1_2),
           let updateFormat = AVAudioFormat(streamDescription: streamDescription, channelLayout: layout) {
      ///////////////// NOT CALLED ////////////////      
            format = updateFormat
        }

AppleTV+ and Infuse can switch to Dolby Atmos mode.

Alanko5 commented 2 years ago

@kingslay

this code: private let audioOutput: AudioPlayer & FrameOutput = AudioEnginePlayer()

will be exexute before:

public required init(url: URL, options: KSOptions) {
         KSAVPlayer.setAudioSession()

AudioSession start after get AVAudioFormat...

kingslay commented 2 years ago

I fixed

@kingslay

this code: private let audioOutput: AudioPlayer & FrameOutput = AudioEnginePlayer()

will be exexute before:

public required init(url: URL, options: KSOptions) {
         KSAVPlayer.setAudioSession()

AudioSession start after get AVAudioFormat...

I fixed

Alanko5 commented 2 years ago

I don understand this:

        if let channelLayout = format.channelLayout, KSPlayerManager.channelLayout != channelLayout {
            format = AVAudioFormat(commonFormat: format.commonFormat,
                                   sampleRate: format.sampleRate,
                                   interleaved: format.isInterleaved,
                                   channelLayout: KSPlayerManager.channelLayout) // why use stereo layout?
        }

why use -> KSPlayerManager.channelLayout but not -> format.channelLayout

kingslay commented 2 years ago

I don understand this:

        if let channelLayout = format.channelLayout, KSPlayerManager.channelLayout != channelLayout {
            format = AVAudioFormat(commonFormat: format.commonFormat,
                                   sampleRate: format.sampleRate,
                                   interleaved: format.isInterleaved,
                                   channelLayout: KSPlayerManager.channelLayout) // why use stereo layout?
        }

why use -> KSPlayerManager.channelLayout but not -> format.channelLayout

Is the value of format.channelLayout correct now? I wrote this so that you can specify the value of KSPlayerManager.channelLayout,

Alanko5 commented 2 years ago

How to work audio proccesing in KSMEPlayer?

we need detect this:

  1. Content format (is support Atmos/DTS/Channel count?) -> ???
  2. HW supported format -> engine.outputNode.outputFormat(forBus: 0)

How to detect supported HW format:

var value = [AudioChannelLayoutTag]()
var size = UInt32(MemoryLayout<AudioChannelLayoutTag>.size) * value.count
let status = AudioUnitGetProperty(audioUnit, kAudioUnitProperty_SupportedChannelLayoutTags, kAudioUnitScope_Global, 0, &value, &size)

But this code not work. I don't know where the problem is. (kAudioUnitErr_InvalidElement (err code: -10877))

So, if detect supported HW AVAudioFormat, then we can adjust the data accordingly.

I don't fully understand how audio works.

Well, we need PlayerNode to be able to edit the data for the supported HW options. Of course we have to pay attention to the source.

Please can you show me where you have: PlayerNode, MixerNode, OutputNode

It would help me understand the issue a little more.

AppleDoc1 AppleDoc2
Alanko5 commented 2 years ago

I don understand this:

        if let channelLayout = format.channelLayout, KSPlayerManager.channelLayout != channelLayout {
            format = AVAudioFormat(commonFormat: format.commonFormat,
                                   sampleRate: format.sampleRate,
                                   interleaved: format.isInterleaved,
                                   channelLayout: KSPlayerManager.channelLayout) // why use stereo layout?
        }

why use -> KSPlayerManager.channelLayout but not -> format.channelLayout

Is the value of format.channelLayout correct now? I wrote this so that you can specify the value of KSPlayerManager.channelLayout,

you set this: static var channelLayout = AVAudioChannelLayout(layoutTag: kAudioChannelLayoutTag_Stereo)! but newer update....

kingslay commented 2 years ago

I is only use AVAudioEngine for audio proccesing

kingslay commented 2 years ago

you need detect Content format (is support Atmos/DTS/Channel count?). and then set KSPlayerManager.channelLayout yourself

Alanko5 commented 2 years ago

you need detect Content format (is support Atmos/DTS/Channel count?). and then set KSPlayerManager.channelLayout yourself

This is wrong way.

but need too mutch info.

that information can only be obtained at this layer.

whole audio processing should but automatically

I have no information on whether the source supports dts or atmos etc…

Alanko5 commented 2 years ago

Not working….

Alanko5 commented 2 years ago

@Alanko5 have you found a fix for that? it is just sad that user who has home theatre set-up can't have 7+1/dolby atmos sound

No, i dont have fix for atmos.

Alanko5 commented 2 years ago

Yes, but I can't verify that it works correctly. 🤷🏿

Alanko5 commented 2 years ago

What's New in Audio - WWDC17 - Videos - Apple Developer

15:45 - manual rendering mode 45:30 - audio formats

What is LPCM?

LPCM is an uncompressed, lossless format which on the Apple TV supports up to 7.1 channels of 24-bit, 48KHz audio.

Alanko5 commented 2 years ago

@kingslay ffmpeg is downstreeming from atoms to 7.1 DTS/5.1 DTS? Or give a raw format? If you modify it during decoding, then it is clear that CoreAudio cannot send it as atmos.

kingslay commented 2 years ago

@kingslay ffmpeg is downstreeming from atoms to 7.1 DTS/5.1 DTS? Or give a raw format? If you modify it during decoding, then it is clear that CoreAudio cannot send it as atmos.

I know ffmpeg support Dolby E, but I didn't find the article that ffmpeg supports Dolby atmos @Alanko5

Alanko5 commented 2 years ago

Patch for atmos?

same problem??

need enable truehd decoding?

decode atmos substream?

Alanko5 commented 2 years ago

is it possible to send a compressed stream to tv in truehd? Without decoding.

Alanko5 commented 2 years ago

This project is a test project for sending data to player without ffmpeg decoding:

https://github.com/MrMC/ac3_tests

It's a shame it's not finished