carsten-gross / ts2shout

Convert mpeg transport stream into shoutcast or native mpeg data stream
GNU General Public License v2.0
8 stars 2 forks source link

AAC LATM to ADTS #3

Open mrwish7 opened 2 years ago

mrwish7 commented 2 years ago

Hi there,

I've been trying out your really useful tool with a Sonos speaker as well as a Ruark internet radio (Frontier Silicon/Frontier Nuvola).

It works perfectly on the MP2 ARD stations that are due to be turned off in December.

However, I've run into the same difficulty as you mention on your webpage regarding some Squeezebox setups not supporting the newer AAC format - LATM.

Both the Sonos and the Ruark radio refuse to play the new ARD stations via ts2shout (MIME audio/aacp) - I can see the metadata coming through fine from the RDS PID (e.g. on SWR3) but the audio is not decoded. On the Sonos I eventually get an 'unsupported format' error message after a few seconds, whereas the Ruark will happily show and update the metadata while playing no audio at all!

The direct streams from TVHeadend using profile=audio do work successfully on both, and I've discovered the reason for this is that TVHeadend seems to convert LATM to ADTS (I think in file parser_latm.c) when outputting raw audio.

An idea could be to enable a similar parser into ts2shout, maybe with a flag that can be set, that also does this format conversion to ADTS, for internet radios that only understand that format? If I understand correctly, this wouldn't work for all LATM streams if they contain formats not supported by ADTS, but it definitely works for ARD stations.

carsten-gross commented 2 years ago

Hello Adam,

Am Donnerstag, 25. November 2021, 16:11:11 CET schrieb Adam Wisher:

It works perfectly on the MP2 ARD stations that are due to be turned off in December.

However, I've run into the same difficulty as you mention on your webpage regarding some Squeezebox setups not supporting the newer AAC format - LATM.

Yes, unfortunatly most media players refuse to play AAC-LATM directly. I already changed ffmpeg for reading inline AAC data stream elements. So I though about using ffmpegs format conversion options to convert from AAC-LATM to e.g. FLAC or some other well implemented format.

This would require some changes in the internal handling of the stream data to do the right output. Put the code to decode AAC is already there, the only thing missing is converting the raw decoded audio into a new compressed format (e.g. FLAC).

I'll take a look into this - I hope I've some time left to implement this, because it would also ease my installation at home.

An idea could be to enable a similar parser into ts2shout, maybe with a flag that can be set, that also does this format conversion to ADTS, for internet radios that only understand that format? If I understand correctly, this wouldn't work for all LATM streams if they contain formats not supported by ADTS, but it definitely works for ARD stations.

Regards,

Carsten

mrwish7 commented 2 years ago

Hallo Carsten, thank you for your response!

Regarding ffmpeg's format conversion options, annoyingly there's a bitstream filter that does the opposite to what's wanted here - converting ADTS to MPEG4-compliant AAC without re-encoding the audio, e.g. for LATM - https://ffmpeg.org/ffmpeg-bitstream-filters.html#toc-aac_005fadtstoasc

But the reverse doesn't seem possible, even though TVHeadend seems to do this successfully with its LATM parser -

Raw TVHeadend 'audio' stream -

ffmpeg -i http://host:9981/stream/channelname/SWR3?profile=audio

Stream #0:0: Audio: aac (LC), 48000 Hz, stereo, fltp, 123 kb/s

Audio output from ts2shout -

ffmpeg -c:a aac_latm -i http://host/radio/SWR3

Stream #0:0: Audio: aac_latm (LC), 48000 Hz, stereo, fltp

carsten-gross commented 2 years ago

Hello Adam,

Am Freitag, 26. November 2021, 10:50:21 CET schrieb Adam Wisher:

Regarding ffmpeg's format conversion options, annoyingly there's a bitstream filter that does the opposite to what's wanted here, converts ADTS to MPEG4-compliant AAC without re-encoding the audio, e.g. for LATM - https://ffmpeg.org/ffmpeg-bitstream-filters.html#toc-aac_005fadtstoasc but not the reverse, which TVHeadend seems to be doing with its LATM parser -

Ah, I would prefer converting to FLAC, this means LATM-AAC goes in, Shoutcasted' FLAC goes out. At least it's supported by the Squeezebox, I'd expect that this is supported by your equipment, too?

Regards,

Carsten

mrwish7 commented 2 years ago

Hi Carsten,

That sounds a neat solution using lossless FLAC.

Unfortunately my Sonos speaker doesn't seem to support FLAC webstreams, which is a little annoying! I think I will have to stick to the regular TVHeadend audio streams for now.

mrwish7 commented 2 years ago

I did some investigation into how the LATM to ADTS conversion is performed within the TVHeadend code. I managed to replicate it in Python (a language I know more about!) and I successfully converted a LATM clip produced with ts2shout into ADTS.

The process is -

I will try to investigate further how this could work in ts2shout.

mrwish7 commented 2 years ago

I have created a proof of concept of the above. It's hard-coded with the 5 bit offset for the AAC audio data. This works with SWR3 but not other ARD stations I have tried (I guess their LATM config is different, meaning that the offset is different.)

Output is ADTS audio/aac, and plays successfully on my Sonos speaker (for SWR3).

https://github.com/mrwish7/ts2shout/commit/2a410d74e6f576904f2ec678898dbe23055d190c

Screenshot 2022-02-01 160716