Wrong channel mapping of AC-3 audio after 2.0 -> 5.1 change

Started by Basic.Master, May 11, 2018, 06:44:17 PM

Previous topic - Next topic

Basic.Master

Most german broadcasters that have an AC-3 track usually send in 2.0 format. They only change to 5.1 during programmes that need this. So a recording of such a programme with some minutes before/after will have the following channel changes in it: 2.0 -> 5.1 -> 2.0. The following example (untouched repack of a recording to MKV) contains the start of a 5.1 programme, where in the last seconds the host only talks on the center channel: https://basicmaster.de/test/ac3_2.0_to_5.1.mkv

Unfortunately in such a case, during the 5.1 part, the audio is not really usable: The LFE channel is played on the left output channel, and the left channel is played on the right output channel. So when the host is speaking on the center channel only, he cannot be heard.

The reason seems to be that ADM only inits the audio once and with the channel format at the beginning of the file, so here to 2.0. However the invokation of liba52dec is done in a way that always decodes/outputs all encoded channels. While in the 2.0 case, the channels left, right are output (in that order), in case of 5.1 it means: LFE, left, center, right, left surround, right surround (in that order). As ADM still expects stereo, only the first two channels are continued to be used and the described behaviour occurs. :(

It might not be easy to fix this in a way that requires re-initing the audio device whenever needed. So a way to solve this would be a settings option that forces liba52dec to always downmix to stereo. This is done by invoking a52_frame (in /avidemux_plugins/ADM_audioDecoders/ADM_ad_ac3/ADM_ad_a52.cpp) with a flags parameter of A52_STEREO - instead of using the actual encoded channel format that was returned by a52_syncinfo.

I attach a patch that changes the behaviour hard-coded as described, when a channel count of 2 was detected. As I'm not firm with Qt5, I'm not able to add such a settings option.

Edit: updated example file; improved patch

eumagga0x2a

Thank you, I never looked into anything a52dec related yet. Could you please help me to understand why we should always overwrite A52_DOLBY with A52_STEREO if we've got 2 channels? Are both one and the same for liba52?

Basic.Master

As far as I can see, it doesn't make a difference, but I'm not 100% sure to be honest. Despite that it may be more wise to restrict overwriting the flags to the actual affected case i.e. enforce A52_STEREO only if two channels were initially detected AND the format of the current AC-3 frame is 5.0/5.1. In terms of broadcasting I'm not aware of any other commonly used channel configurations than 2.0 and 5.0/5.1, so this should be enough.

Please find attached an updated version of the patch. Of course a long term solution would be to always adapt the channel config of the audio device to the channel config of the actual frame.

eumagga0x2a

Thank you, my concern is about the initial flags matching A52_DOLBY which we would then overwrite with A52_STEREO upon the switch to 5.0/5.1. I wonder whether we should store the previous flags and then decide whether we request downmix to A52_STEREO or to A52_DOLBY. It is not expressing a particular logic in code that might pose a difficulty, I just don't know exactly what this A52_DOLBY is, why it has always 2 channels and how it should be handled.

Basic.Master

A52_DOLBY means that the audio is not only stereo, but also contains Dolby Surround information (analogue matrix system that encodes surround sound in two channels and is backwards compatible to stereo). When using A52_DOLBY instead of A52_STEREO for the 5.0/5.1 downmix, the surround channels are mixed a bit differently into the output downmix. Nevertheless I would prefer to always use A52_STEREO here, to not create any surround sound that did not exist in the first place and the user probably doesn't have the Dolby Surround decoder enabled on his/her surround speakers.

Currently ADM also behaves the same way when A52_STEREO or A52_DOLBY is returned by liba52dec. So from ADM side there is no need to consider which of the two formats has been detected in the first place.

Some AC-3 background:
If (and only if) an AC-3 frame contains 2.0 content, it also contains the dsurmod flag (= Dolby Surround Mode). This flag indicates whether the 2.0 content contains Dolby Surround. The difference between A52_STEREO and A52_DOLBY is just that it expresses that the Dolby Surround flag was set. And, as mentioned, the slightly different downmix from multi-channel surround.

The AC-3 spec says about the dsurmod flag:
"This information is not used by the AC-3 decoder, but may be used by other portions of the audio reproduction equipment."

eumagga0x2a


Basic.Master

Thank you! And also thanks for fixing the comparison; my flags check was buggy.