Skip to content
This repository was archived by the owner on Feb 9, 2018. It is now read-only.

Option to use LPCM for audio when streaming with mencoder and tsmuxer - #10

Merged
ikarsokolov merged 20 commits into
ps3mediaserver:masterfrom
ikarsokolov:lpcm-audio-for-streaming-with-mencoder-and-tsmuxer
Jun 12, 2012
Merged

ikarsokolov merged 20 commits into
ps3mediaserver:masterfrom
ikarsokolov:lpcm-audio-for-streaming-with-mencoder-and-tsmuxer

Conversation

@ikarsokolov

Copy link
Copy Markdown
Member

follow-up to issue 1368 and pull request test :)

Right now LPCM streaming is limited to files with flac and DTS audio tracks. What is the reason behind this decision?

Below is simple patch to enable LPCM transport for ac3, mp3, aac, ogg, wma, mpeg and truehd tracks. I haven't done extensive testing but it seems to work fine. Advantages are obvious:

  • No audio quality loss
  • Less CPU utilization

Bandwidth requirements are about 1.5 Mbit/s for 2.0 and 4.6 Mbit/s for 5.1 audio. No problems for wired connection.

Patch also includes some minor GUI corrections. For example label "You need an HDMI receiver for streaming LPCM 5.1" is misleading because you don't. PS3 will automatically re-encode audio to DD or DTS if its the only available audio output format.

@Raptor399

Copy link
Copy Markdown
Member

Nice, thanks!
The patch looks straight forward enough.

I'm not much of an audio buff myself, so I'll leave merging to someone who knows more. :-)

@ExSport

ExSport commented Jan 30, 2012

Copy link
Copy Markdown
Contributor

Hi.
Please read discussion from here: http://code.google.com/p/ps3mediaserver/issues/detail?id=1368
Maybe you will agree with me it should be widely discussed if it is OK to merge it. I think it will change current logic dramatically. If it will be configurable, I don't have problems with it but now I see side effects of this code change pulled in current state.

@ikarsokolov

Copy link
Copy Markdown
Member Author

ExSport, I think you exaggerate effect of this patch on PS3MS behavior.

Please take look on the code. "Keep DTS audio" has higher priority so it'll be remuxed (with wav hack if necessary) regardless of LPCM setting. Same with "Keep AC3 audio", it'll never be reencoded to LPCM if checked. So it is exactly the same behavior you can see now. The only difference is less common codecs handling like AAC or WMA. Right now PS3MS always reencode them to AC3, with this patch user can chose between AC3 reencoding for them (old behaviour) or LPCM reencoding.

Maybe you can provide usage cases when you see undesirable effects? We can think about additional config option to keep "perfect" backward compatibility if needed.

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

DTS remuxing has higher priority and will be used regardless of isMencoderUsePcm option.

@ExSport

ExSport commented Jan 30, 2012

Copy link
Copy Markdown
Contributor

As I said, I had no time to check the code so mentioned some scenarios so maybe I was wrong.
I know that DTS option has precedence so I have it enabled by default and I manage behavior on renderer level by:
MuxDTSToMpeg=?
WrapDTSIntoPCM=?
MuxLPCMToMpeg=?
Quick scenario - You are on WIFI, you enabled LPCM in PMS Gui for supporting DTS audio on renderers which doesn't support DTS defined by parameters above. Now all audio formats will be reencoded to LPCM like AAC/MP3/OGG etc.
I want to stream them as is without reencoding if possible or rencoded to AC3(384-640) instead of high LPCM bitrates. For WIFI it is not good path.
So if it will be possible same thing like with AC3 to stream it as is when supported, no problem to enable LPCM for other formats. Something like "Remux audio if compatible with renderer".
Maybe not easily understandable so again: How to disable LPCM reencoding for other audio codecs than DTS? For now only DTS option can override it plus AC3 remux as you said but we can't define other codecs not to be overridden.
I didn't think about it deeply so I wanted to initiate discussion if somebody else will find side effects so we will not change expected behavior of this option.

@ExSport

ExSport commented Jan 30, 2012

Copy link
Copy Markdown
Contributor

Now when audio is not compatible it is reencoded to AC3.
Now option to reencode it to LPCM was intended to support high definition audio like DTS or Flac on non compatible receivers/renderes. It wasn't intended to be used for switching between AC3 or LPCM reencoding but your patch does exactly it.
...aaah now I see also description was changed to relevant text:-)....ok now it makes sense and it is easily understandable what exactly this option does but still somebody will loose possibility to configure it to DTS only as before.
I am not on WIFI and almost all my audio is DTS/AC3/MP3 so no problem for me but maybe for others....
For me with applied patch only change will be reencoded MP3 so it don't borther me so much.
Question: What is the logic if I have set supported= for AAC/MP3/AC3 etc. and will enable LPCM reencoding?
I don't want to reencode them because I enabled LPCM only due to DTS not supported by some renderers.
It will be perfect to have this option as you defined but in way how to say PMS to reencode audio, if to AC3 or to LPCM.
But then this option should be totally disjoined from DTS settings so I will be able to define not to use LPCM in GUI but it will be used for DTS if WrapDTSIntoPCM=false. In this case it will be reencoded to AC3 an not to LPCM as before.
My english is not so good so I can't describe it more easily:-)

@ikarsokolov

Copy link
Copy Markdown
Member Author

Behavior proposed in patch:

  • Use LPCM is checked
  • Keep AC3 is not checked
  • Keep DTS is not checked

Result: reencoding to LPCM for all audio codecs,

  • Use LPCM is checked
  • Keep AC3 is checked
  • Keep DTS is not checked

Result: reencoding to LPCM for all audio codecs except AC3. Remux AC3 without reencoding.

  • Use LPCM is checked
  • Keep AC3 is not checked
  • Keep DTS is checked

Result: reencoding to LPCM for all audio codecs except DTS. Remux DTS without reencoding (with WAV header hack if requested in renderer's config).

  • Use LPCM is checked
  • Keep AC3 is checked
  • Keep DTS is checked

Result: reencoding to LPCM for all audio codecs except AC3 and DTS. Remux AC3 without reencoding. Remux DTS without reencoding (with WAV header hack if requested in renderer's config).

  • Use LPCM is not checked

Result: same behavior as above, Use AC3 reencoding instead of LPCM reencoding.

If I understand you right you want to add separate option Use LPCM only for DTS. Is this right?

@ExSport

ExSport commented Jan 30, 2012

Copy link
Copy Markdown
Contributor

Use LPCM is checked
Keep AC3 is checked
Keep DTS is checked
Result: reencoding DTS to LPCM only when DTS is not supported. In other cases mux DTS as is or encapsulated in PCM.
All other codecs should stay as is(no reencode) or when not supported, AC3 is used. This is current behavior.

With new patch I have to disable LPCM to not reencode other codecs but then I will loose DTS reeencoding to LPCM:
Use LPCM is not checked
Keep AC3 is checked
Keep DTS is checked
Result: We will loose option to reencode only high fidelity audio codecs(DTS/Flac) to LPCM(rest to AC3) when we also want to not use LPCM for other codecs. If LPCM will be checked, DTS reencoding logic will stay as it was but also all other codecs will be reencoded to LPCM which I don't want.
So in this case we need to separate LPCM settings from DTS so we don't need to enable LPCM which has new behavior only for achieving old behavior for DTS.
So if LPCM will be enabled, all will be reencoded to LPCM(logical) but if it will be disabled, still DTS will be reencoded to LPCM and the rest to AC3(as it was intended in old versions=only reencode high fidelity codecs to loseless LPCM).
In short: I don't want to use LPCM reencoding for anything else than high fidelity audio like DTS where it has a reason. With new patch it is impossible to configure it this way because when it will be disabled, LPCM will not be used in any way and when enabled, all codecs will be reencoded, also ones which are not high fidelity codecs which is useless in most cases.
So as you said, with this patch I think it will be useful to define some new parameter how to force LPCM for high fidelity codecs(DTS/Flac) also in case LPCM will be disabled. I think it is useless to reencode low quality codecs to LPCM so I will disable it but I want to use it for DTS where LPCM was used for from beginning due to no quality loss.
So something like:
If audio==DTS && MuxDTSToMpeg==false && WrapDTSIntoPCM==false && MuxLPCMToMpeg=true && ForceDTS2LPCM=true then Reencode2LPCMalsoInCaseLPCMisDisabledInPmsGui
Then we will not loose old behavior to reencode to LPCM only DTS/Flac tracks. Why to convert 128Kbit MP3 to 1,5Mbit PCM2.0 stream only to achieve losssless DTS audio on non compatible renderers?
I don't think ForceDTS2LPCM is right parameter but we should somehow enable DTS2LPCM also in case LPCM is disabled. In different words I want to reencode DTS only(to LPCM) and nothing else(mp3/ogg...)
Ehm, I remembered one scenario:
LPCM enabled, keep AC3 enabled, Keep DTS enabled:

  1. renderer which supports DTS => works as it should(DTS streamed) but all other codecs are reencoded instead of stream as is because it is supported (MP3/OGG/AAC/WMA)
  2. renderer without DTS support => DTS is reencoded to LPCM(great!) but also all other codecs are reencoded as described above=also in case it should be streamed(but we can't disable LPCM to achieve this to not loose max quality DTS support)
    Sorry I twisted meaning again...more I am writing about I make it more incomprehensible:-)

@ikarsokolov

Copy link
Copy Markdown
Member Author

I think what you want is (GUI mockup):

[x] Use LPCM
    for [ ] AC3    [x] DTS    [x] FLAC    [ ] everything else
[x] Keep AC3 audio (no reencode)
[ ] Keep DTS audio (no reencode)

Is it correct?

@ExSport

ExSport commented Jan 31, 2012

Copy link
Copy Markdown
Contributor

Yes, but didn't wanted to mess up GUI with another new settings and also didn't think about it if it should be configurable on renderer level or global level:-) But above you mentioned is quite straightforward.
Something what will offer new function to reencode everything to LPCM but also if someone wants to avoid this(LPCM disabled), still we can use LPCM for high fidelity codecs like before(DTS/Flac). But question is what is better, configure it globally or on renderer level...I think we can define it globally, not needed to be specified separately for every renderers.
I understand that this mess the logic of LPCM little bit but when checking the past, it was allways used for high fidelity codecs only where I don't bother with final high bitrate for audio track.
But anyway, maybe I am alone so I will accept any solution which will have the majority of votes:-)

@ikarsokolov

Copy link
Copy Markdown
Member Author

Ok. For now we can live with this simple GUI:

[x] Use LPCM
[x] Keep AC3 audio (no reencode)
[x] Keep DTS audio (no reencode)

and additional hidden config option MencoderUsePcmForHQAudioOnly to retain full backward compatibility with previous PS3MS versions. Will update the branch.

In the future we can consider full GUI revamp to make it more intuitive, something like this:

Audio options:
AC3    (*) remux  ( ) LPCM   ( ) reencode to AC3
DTS    ( ) remux  (*) LPCM   ( ) reencode to AC3
FLAC   (*) LPCM   ( ) reencode to AC3
AAC    ( ) LPCM   (*) reencode to AC3
WMA    ( ) LPCM   (*) reencode to AC3
Other  ( ) LPCM   (*) reencode to AC3

AC3 reencoding options:
Number of channels:    [5.1]
AC3 audio bitrate:     [640]

@ExSport

ExSport commented Feb 1, 2012

Copy link
Copy Markdown
Contributor

Seem to me perfect! Thanks. Nobody will loose old behavior so I am fine with it:-)

@ikarsokolov

Copy link
Copy Markdown
Member Author

Changes committed.
Now we are good to go.

@ghost

ghost commented Mar 1, 2012

Copy link
Copy Markdown

For me the audio channels don't seem to be properly assigned when passing audio via LPCM. The center channel sounds more like a rear surround channel. Am I doing something wrong?

@ExSport

ExSport commented Mar 1, 2012

Copy link
Copy Markdown
Contributor

https://github.com/ps3mediaserver/ps3mediaserver/blob/master/src/main/java/net/pms/util/CodecUtil.java#L178
When I was trying to make 7.1 LPCM from DTS 7.1 I found that it is impossible right now and also that this channel ordering hardcoded in PMS is not needed with latest MEncoder builds anymore.
You remembered me this issue so reporting it now:-)
But this ordering has notning to do with this patch...it works same quite long time...
EDIT: This code is used when somebody doesn't remux AC3 but reencode it to AC3 again so I am not sure if same reordering is used also for LPCM conversion.

@ExSport

ExSport commented Mar 11, 2012

Copy link
Copy Markdown
Contributor

Bad channel ordering should be fixed by applying this: #21

@ikarsokolov

Copy link
Copy Markdown
Member Author

Thanks, @ExSport . I'll check it out.

@ikarsokolov

Copy link
Copy Markdown
Member Author

@Lycanthor
Can you test it now against master? It seems to work fine with bae11ba.

@ikarsokolov

Copy link
Copy Markdown
Member Author

Ok, channel mapping for PCM is clearly wrong. At least for AC3 sources.
Rear Right is playing Rear Left, where is Rear Left I don't even know :)

Time for more testing.

@ikarsokolov

Copy link
Copy Markdown
Member Author

I found this information.

Channel Arrangement for Multi Channel 5.1 Audio Formats

Format      Chan 0  Chan 1  Chan 2  Chan 3  Chan 4  Chan 5
5.1 WAV     FL      FR      FC      LFE     SL      SR
5.1 AC3     FL      FC      FR      SL      SR      LFE
5.1 DTS     FC      FL      FR      SL      SR      LFE
5.1 AAC     FC      FL      FR      SL      SR      LFE
5.1 AIFF    FL      SL      FC      FR      SR      LFE

FL : Front Left
FC : Front Center
FR : Front Right
SL : Surround Left
SR : Surround Right
LFE : Low Frequency Effects (Sub)

I think we need to remap channels when going from ac3/dts to pcm. Why for God's sake mencoder can't do this automatically?

@ExSport

ExSport commented Mar 25, 2012

Copy link
Copy Markdown
Contributor

Thanks for update. I tested it mainly on anything to LPCM conversion with 7.1 sample and it worked ok. Maybe conversion to AC3 needs reordering but to LPCM not?
Can you test it to use LPCM settings enabled instead of AC3 reencoding?
Thx
EDIT:
Now see your latest post...ehm, true that my source test was DTS what I remember so maybe because of that I had different results?

@ikarsokolov

Copy link
Copy Markdown
Member Author

@ExSport I found this test file to be quite convenient to test 5.1 sound.
5.1-AC3-check.vob

It is MPEG-2/AC3 vob so you can play it on PS3 both natively and with mencoder transcoding to compare results. From my experience:

  • Native playback (AC3) obviously works fine
  • AC3 => AC3 reencoding works fine
  • AC3 => LPCM wrong channel mapping

I think table above explains it. We need to manually change channel order when going from AC3 to LPCM.

@ExSport

ExSport commented Mar 25, 2012

Copy link
Copy Markdown
Contributor

will try to test it.
I used files from: http://www.demo-world.eu/trailers/high-definition-trailers.php
I tried to make 7.1 channels to work (DTS2LPCM) but MEncoder/Mplayer decoder supports 5.1 only so I gave it up.
But spotted this reordering when DTS2LPCM was used so as you described, maybe AC32AC3/AC32LCPM needs another fix. Let you know the results of my testings with your file and latest PMS.

@ikarsokolov

Copy link
Copy Markdown
Member Author

I am focused on making 5.1 work at first.

It appears we still have to remap channel for PCM output even with latest mencoder (SB28). With my last commits 5.1 AC3 and 5.1 AAC work fine both with AC3 and LPCM transport. WMA9 is still buggy though and works only with AC3 transcoding.

Here you can find AAC and WMA9 test files:
http://www.mediafire.com/?29xbi5i1cdhqa

Does anyone have 5.1 DTS test video file?

@ExSport

ExSport commented Mar 25, 2012

Copy link
Copy Markdown
Contributor

When I tested it last time I was fighting with 7.1 output if possible so that I spotted reordered channels it was a bonus.
But as you said AC3->AC3 conversion now works OK but NOT DTS2LPCM. As you said, Rear Right is played from left and Rear Left is completelly missing-EDIT:it is opposite as described in next post. When I tested it last time, it was in combination with eac3to so maybe because of that I didn't spotted it:-(
Anyway I can't test AC3toLPCM for now because it is not supported in official builds without this patch.
Now downloading 5.1 DTS-MA(didn't found general one with 5.1) from link I posed above and let you know if there are some difference between 7.1/5.1 DTS. I don't suppose it because only core (5.1) is extracted but nobody never know:-)

@ExSport

ExSport commented Mar 25, 2012

Copy link
Copy Markdown
Contributor

Ok so the corrections follow from new testing, latest PMS(with removed reordering from old PMS builds):
When DTS is converted to AC3 - whatever it is (7.1/5.1/core), audio channels are ordered correctly.
When AC3 is converted to AC3 - everything is correct
When AC3 is converted to LPCM - can't say because official builds of PMS don't support it
When DTS is converted to LPCM - FL=OK, FC=OK, FR=OK, SL=SR, SR=missing, LFE=OK
So problem is definitelly only when conversion to LPCM is used.
You described that you hear Rear Right in Rear Left and Rear Left is missing what is opposite to my testing with DTS:-)
Anyway it seems PCM encoding is corrupt in MEncoder because we can't fix something what is missing so we should swap one rear channel and other one create from scratch.
Better for now not to use this feature so pure DTS or AC3 is streaming is used and when not possible, then use AC3 conversion only:-)

@ExSport

ExSport commented Mar 25, 2012

Copy link
Copy Markdown
Contributor

How do you fixed DTS/AC3 to LPCM conversion? Without reordering matrix used in MEncoder parameters, Rear Right is missing so how you regenerated it? OK shifted Rear left can be switched to right but Rear right is missing so can't be reconstructed without quality loss(so 2AC3 transcoding is better in this case)

@ikarsokolov

Copy link
Copy Markdown
Member Author

I have tested with last commits on this branch:

  • 5.1 AC3: works fine both LPCM and AC3 transcoding
  • 5.1 AAC: works fine both LPCM and AC3 transcoding
  • 5.1 WMA: works fine with AC3 transcoding, LPCM transcoding is disabled and falls back to AC3

DTS test file you linked is huge (~800 Mb) and split on several parts on very slow server so it is still downloading

@ikarsokolov

Copy link
Copy Markdown
Member Author

After remaining bugs were fixed I have these tests results:

  • 2.0 AC3: everything works fine. Remuxing (bitstream), LPCM and AC3 transcoding
  • 5.1 AC3: everything works fine. Remuxing (bitstream), LPCM and AC3 transcoding
  • 5.1 DTS: everything works fine. Remuxing (bitstream), LPCM and AC3 transcoding
  • 5.1 DTS-HD MA: everything works fine. Remuxing of DTS core (bitstream), LPCM and AC3 transcoding
  • 2.0 AAC: works fine both LPCM and AC3 transcoding
  • 5.1 AAC: works fine both LPCM and AC3 transcoding
  • 2.0 FLAC: works fine both LPCM and AC3 transcoding
  • 5.1 WMA: works fine with AC3 transcoding, LPCM transcoding is disabled due to mencoder bug and falls back to AC3

With positive results from other members I think this patch is ready to be merged with master.

@Raptor399

Copy link
Copy Markdown
Member

Nice work, @Happy-Neko and @ExSport !

@ExSport

ExSport commented Mar 26, 2012

Copy link
Copy Markdown
Contributor

Great work Happy-Neko:-)
Only what I don't understand is that in past you said Rear Left channel was missing, same as me but in my tests rear right was missing. So how you fixed it? Need to check your patch but I suppose you copied another channel and copied it to missing possition so better something(not original channel) than nothing?
mixer = "channels=6;6;0;0;1;2;2;1;3;4;4;5;5;3"
Hm, interresting. With avoiding mixer params in MEncoder I had 1 channel missing and one shifted but others (4) were correct and here I see that matrix say only one channel wasn't shifted but all others yes so it is logically weird but great that it works:-) Unfortunatelly I can't test it for now for 2-3 weeks, not at home.

@ikarsokolov

Copy link
Copy Markdown
Member Author

I managed to fix 7.1 Dolby TrueHD playback. At least it works as it should on 5.1 speakers. I can't test surround and rare channels on the same side (left or right) because they play through same speakers on 5.1 so they can be in wrong order. If anyone can confirm this on real 7.1 system that'll be great.

Other test results:

  • 7.1 Dolby TrueHD: LPCM works fine in true 7.1, AC3 transcoding is buggy with missing channels because of incorrect 5.1 downmix and is not recommended
  • 7.1 DTS HD-MA: Remuxing of DTS core (bitstream) and AC3 transcoding work fine. LPCM transcoding is disabled and falls back to AC3

@ExSport if you had missing channels with 7.1 files thats because previously PS3MS parsed them as 6 channel tracks, not 8 (check last commits).

@ExSport

ExSport commented Mar 27, 2012

Copy link
Copy Markdown
Contributor

No, I fixed this in code so it wasn't problem. Problem is that Mplayer/MEncoder can't decode 7.1 channels from DTS tracks. Decoder is limited to 5.1 only. What I remember I demuxed audio with eac3to so final 7.1 file was ok on PS3.
Also when I was googling I spotted that TrueHD 7.1 decoding is possible with MEncoder but I don't have any so tested DTS only.
Core 5.1 tracks decoded from DTS-MA 7.1 was OK but not any 7.1 transcoded to LPCM.
Now I checked your code and I did the same but when tested I commented any mixer definitions to avoid manual wrong channel reordering from start:-)

@ikarsokolov

Copy link
Copy Markdown
Member Author

I think it's time to merge into master. The growing distance between this branch and HEAD is frightening.

@ikarsokolov

Copy link
Copy Markdown
Member Author

If you don't mind I'll merge this request with master.

@ikarsokolov
ikarsokolov merged commit 760fd93 into ps3mediaserver:master Jun 12, 2012
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants