AC3 Passthrough on Apple TV

236 views
Skip to first unread message

Graham Booker

unread,
Apr 4, 2007, 1:26:06 AM4/4/07
to perian-...@googlegroups.com
Well, I did it!  AC3 Passthrough successfully tested on an Apple TV.  There are some issues, but for the most part, it works.  Now, here is how it works (it is one hell of a hack).  The most important thing is that AC3 data must be passed to the decoder without modifications.

First step:  The audio sample rate of the ATV must be set to the same as that of the AC3 file (Hey, author of ATVFiles, this is a great place for you to step in and change the sample rate of the audio output before playback).  I have tried everything I can think of without this step, but it all fails (either gaps in the audio or the audio starts lagging by 10%).  I solved this by hacking the ATV to run Audio Midi Setup and changing the sample rate to 48KHz.

Second step:  Hack up perian ever so slightly so that it reports 2 channel audio instead of the real number.  If Quicktime on the ATV is given more than 2 channels, it will mix them together into Pro Logic encoding (not Pro logic II interestingly enough).  This destroys the digital data, so we only want 2 channels.

Third step: Make the A52Codec know that it is doing a passthrough, and transfer the AC3 data through with the proper header.  Pad the rest with NULLs.

Final result: It passes through AC3 data straight to the decoder and plays.  Yippie.

Now, for the gotchas:
1) The sample rates MUST match.  If you try and play a 48KHz AC3 sample using 44.1KHz, then the only option is for both perian and A52Codec to fake it and claim the data is 44.1KHz.  This transfers data just fine to the decoder, which is fine and dandy, until you notice that in 10 seconds, the audio is delayed by a second, and it keeps getting worse.  If I change it to put in the AC3 packets at a faster rate than normal, the decoder tends to miss several, and there are gaps in playback.

2) Nothing else can play while doing the passthrough.  Shouldn't be an issue on the ATV though, but something to watch out for.

3) wav files which really contain AC3 data at any other sample rate will fail (since QT will resample them).


The same should be try of DTS in the end.  I suppose I will commit the changes after some cleanup.

- Graham


David Conrad

unread,
Apr 4, 2007, 2:48:24 AM4/4/07
to perian-...@googlegroups.com
On Apr 4, 2007, at 1:26 AM, Graham Booker wrote:

Well, I did it!  AC3 Passthrough successfully tested on an Apple TV.  There are some issues, but for the most part, it works.  Now, here is how it works (it is one hell of a hack).  The most important thing is that AC3 data must be passed to the decoder without modifications.

Yay! Are you planning on enabling this for regular Macs or just the ATV since it's more fragile on regular Macs?

First step:  The audio sample rate of the ATV must be set to the same as that of the AC3 file (Hey, author of ATVFiles, this is a great place for you to step in and change the sample rate of the audio output before playback).  I have tried everything I can think of without this step, but it all fails (either gaps in the audio or the audio starts lagging by 10%).  I solved this by hacking the ATV to run Audio Midi Setup and changing the sample rate to 48KHz.

Couldn't you get the default audio device, save its current kAudioStreamPropertyPhysicalFormat, then set it with the same sample rate as the AC3, then revert it to what it was when the codec is closed? Or does that sound too hacky to be done from within A52Codec? I'm sure it wouldn't work as expected if multiple files were opened at once, but is that an issue on the ATV?

Second step:  Hack up perian ever so slightly so that it reports 2 channel audio instead of the real number.  If Quicktime on the ATV is given more than 2 channels, it will mix them together into Pro Logic encoding (not Pro logic II interestingly enough).  This destroys the digital data, so we only want 2 channels.

I thought that there was some way for a core audio decoder to output a different number of channels than what the input format claimed (e.g. for AAC's parametric stereo), but it seems that I was thinking about sample rate conversion...

Sort of related: I've kinda been working on a virtual AC3 audio device. Turns out that JackOSX already had a userspace audio device driver written. At this point I pretty much just need to actually send the encoded AC3 over the S/PDIF out and construct the correct Jack graph automatically. I was looking at VLC's code to set the encoded output of the audio device, but it would probably just be easier to do something like what you're doing.

Graham Booker

unread,
Apr 4, 2007, 1:30:12 PM4/4/07
to perian-...@googlegroups.com

On Apr 4, 2007, at 1:48 AM, David Conrad wrote:

> On Apr 4, 2007, at 1:26 AM, Graham Booker wrote:
>
>> Well, I did it! AC3 Passthrough successfully tested on an Apple
>> TV. There are some issues, but for the most part, it works. Now,
>> here is how it works (it is one hell of a hack). The most
>> important thing is that AC3 data must be passed to the decoder
>> without modifications.
>
> Yay! Are you planning on enabling this for regular Macs or just the
> ATV since it's more fragile on regular Macs?
>

Well, it is not not like anyone can't take the source, make the same
hack and thus enable it if I didn't. The issue is that it is more
fragile, as you said, since on the ATV nothing else plays while
playing a video. Truthfully, I have no idea if this will work on any
of the Macs as is. I only had an AppleTV to test, and got it working
there. The ATV's S/PDIF takes little endian data, and the AC3 is big
endian, so there is an endian swap in there. I know that some macs
have big endian S/PDIF interfaces. I actually have the macs (both
with S/PDIF out, one PPC, one Intel) to test with, but I don't have a
dolby digital decoder to connect to them, so this is likely as far as
I can go in the near term.

>> First step: The audio sample rate of the ATV must be set to the
>> same as that of the AC3 file (Hey, author of ATVFiles, this is a
>> great place for you to step in and change the sample rate of the
>> audio output before playback). I have tried everything I can
>> think of without this step, but it all fails (either gaps in the
>> audio or the audio starts lagging by 10%). I solved this by
>> hacking the ATV to run Audio Midi Setup and changing the sample
>> rate to 48KHz.
>
> Couldn't you get the default audio device, save its current
> kAudioStreamPropertyPhysicalFormat, then set it with the same
> sample rate as the AC3, then revert it to what it was when the
> codec is closed? Or does that sound too hacky to be done from
> within A52Codec? I'm sure it wouldn't work as expected if multiple
> files were opened at once, but is that an issue on the ATV?
>

I have considered this, but I just feel that this is the kind of
thing that really doesn't belong in the codec. While this could work
well enough on the ATV, it would be problematic on the mac in
general. Plus, when opening a video file and the initial playback,
core audio (even on the ATV) tends to create and destroy several
instances of the codec before finally settling on one.

Audio tends to sound better when it is not down or up sampled, so on
the ATV, setting the sample rate to match the source is the best
thing to do. I see ATVFiles as the best place to put this (if the
source were available, I likely could have done it already). In
addition, on the mac side, since the application should really select
the S/PDIF as its output interface and put it in "encoded audio
output" mode anyway (prevents other apps from mixing in their audio),
it is not too much to expect it to set the sample rate as well.

>> Second step: Hack up perian ever so slightly so that it reports 2
>> channel audio instead of the real number. If Quicktime on the ATV
>> is given more than 2 channels, it will mix them together into Pro
>> Logic encoding (not Pro logic II interestingly enough). This
>> destroys the digital data, so we only want 2 channels.
>
> I thought that there was some way for a core audio decoder to
> output a different number of channels than what the input format
> claimed (e.g. for AAC's parametric stereo), but it seems that I was
> thinking about sample rate conversion...
>

I tried doing this. Let Perian say it is 6 channel, and have the
codec only claim that it outputs 2 channel. The result was the codec
was not used. This would have been easier, as it wouldn't require
the perian hack to make passthrough work.

> Sort of related: I've kinda been working on a virtual AC3 audio
> device. Turns out that JackOSX already had a userspace audio device
> driver written. At this point I pretty much just need to actually
> send the encoded AC3 over the S/PDIF out and construct the correct
> Jack graph automatically. I was looking at VLC's code to set the
> encoded output of the audio device, but it would probably just be
> easier to do something like what you're doing.
>

Yeah, that idea has also been suggested on my trac: http://
trac.cod3r.com/a52codec/ticket/14 Would certainly help the 5.1 AAC
files apple distributes in their HD trailers (these DO NOT PLAY IN
5.1 on the AppleTV, but instead use Pro Logic (not even Pro Logic
II)). Re-encoding the 6 channels to AC3 would really help there.
IMHO, this is something that Apple SHOULD HAVE DONE on the AppleTV in
the first place! What is the point of optical output when nothing
will output anything above Pro Logic (without these hacks that is)?
Aside from hacking AC3 or DTS into a wav file, is it possible to play
anything in true multi-channel surround on the ATV?

I used a fair amount of VLC's code to do the same with this hack.
Watch the endianness though.

P.S. check the way I set the passthrough variable in the MKV code. I
tried to put it in with the least impact, but you may need to hook up
some more stuff to it or something. Essentially, it as pref lookup
and check. Maybe it should really be put in the ac3 parser only?

<snip>

David Conrad

unread,
Apr 4, 2007, 11:03:31 PM4/4/07
to perian-...@googlegroups.com
On Apr 4, 2007, at 1:30 PM, Graham Booker wrote:

> On Apr 4, 2007, at 1:48 AM, David Conrad wrote:
>
>> On Apr 4, 2007, at 1:26 AM, Graham Booker wrote:
>>
>>> Well, I did it! AC3 Passthrough successfully tested on an Apple
>>> TV. There are some issues, but for the most part, it works. Now,
>>> here is how it works (it is one hell of a hack). The most
>>> important thing is that AC3 data must be passed to the decoder
>>> without modifications.
>>
>> Yay! Are you planning on enabling this for regular Macs or just the
>> ATV since it's more fragile on regular Macs?
>>
>
> Well, it is not not like anyone can't take the source, make the same
> hack and thus enable it if I didn't. The issue is that it is more
> fragile, as you said, since on the ATV nothing else plays while
> playing a video. Truthfully, I have no idea if this will work on any
> of the Macs as is. I only had an AppleTV to test, and got it working
> there. The ATV's S/PDIF takes little endian data, and the AC3 is big
> endian, so there is an endian swap in there. I know that some macs
> have big endian S/PDIF interfaces. I actually have the macs (both
> with S/PDIF out, one PPC, one Intel) to test with, but I don't have a
> dolby digital decoder to connect to them, so this is likely as far as
> I can go in the near term.

Indeed, my PowerBook requires big-endian output. The attached patch
gets AC3 passthrough to work for me.

>>> First step: The audio sample rate of the ATV must be set to the
>>> same as that of the AC3 file (Hey, author of ATVFiles, this is a
>>> great place for you to step in and change the sample rate of the
>>> audio output before playback). I have tried everything I can
>>> think of without this step, but it all fails (either gaps in the
>>> audio or the audio starts lagging by 10%). I solved this by
>>> hacking the ATV to run Audio Midi Setup and changing the sample
>>> rate to 48KHz.
>>
>> Couldn't you get the default audio device, save its current
>> kAudioStreamPropertyPhysicalFormat, then set it with the same
>> sample rate as the AC3, then revert it to what it was when the
>> codec is closed? Or does that sound too hacky to be done from
>> within A52Codec? I'm sure it wouldn't work as expected if multiple
>> files were opened at once, but is that an issue on the ATV?
>>
>
> I have considered this, but I just feel that this is the kind of
> thing that really doesn't belong in the codec. While this could work
> well enough on the ATV, it would be problematic on the mac in
> general. Plus, when opening a video file and the initial playback,
> core audio (even on the ATV) tends to create and destroy several
> instances of the codec before finally settling on one.

Yeah, I can see how it could easily get weird from within the codec.
I guess that if we eventually have an easy-install process for Perian
on the ATV we'd go the ATVFiles route of a new plugin for movies.

>>> Second step: Hack up perian ever so slightly so that it reports 2
>>> channel audio instead of the real number. If Quicktime on the ATV
>>> is given more than 2 channels, it will mix them together into Pro
>>> Logic encoding (not Pro logic II interestingly enough). This
>>> destroys the digital data, so we only want 2 channels.
>>
>> I thought that there was some way for a core audio decoder to
>> output a different number of channels than what the input format
>> claimed (e.g. for AAC's parametric stereo), but it seems that I was
>> thinking about sample rate conversion...
>>
>
> I tried doing this. Let Perian say it is 6 channel, and have the
> codec only claim that it outputs 2 channel. The result was the codec
> was not used. This would have been easier, as it wouldn't require
> the perian hack to make passthrough work.

That's unfortunate since if you save a reference movie with the
correct layout, you can't use passthrough...

> Yeah, that idea has also been suggested on my trac: http://
> trac.cod3r.com/a52codec/ticket/14 Would certainly help the 5.1 AAC
> files apple distributes in their HD trailers (these DO NOT PLAY IN
> 5.1 on the AppleTV, but instead use Pro Logic (not even Pro Logic
> II)). Re-encoding the 6 channels to AC3 would really help there.
> IMHO, this is something that Apple SHOULD HAVE DONE on the AppleTV in
> the first place! What is the point of optical output when nothing
> will output anything above Pro Logic (without these hacks that is)?
> Aside from hacking AC3 or DTS into a wav file, is it possible to play
> anything in true multi-channel surround on the ATV?

Since the HDMI on the ATV seems only to support 2 channels as well,
it seems that there isn't any way to play true multichannel surround
on the ATV out-of-the-box. But optical isn't all about multichannel
audio; it also gives bit-exact audio to the receiver, which will
likely have a better DAC than the ATV would.

> I used a fair amount of VLC's code to do the same with this hack.
> Watch the endianness though.
>
> P.S. check the way I set the passthrough variable in the MKV code. I
> tried to put it in with the least impact, but you may need to hook up
> some more stuff to it or something. Essentially, it as pref lookup
> and check. Maybe it should really be put in the ac3 parser only?

The only thing missing in MKV is the constructor stuff. Should it
check the same pref as in the A52Codec? It seems that the AVI
importer isn't checking it at the moment. As for putting it only in
the ac3 parser, that makes some sense for what we're currently using
it for and would simplify the code some.

big_endian.patch

Graham Booker

unread,
Apr 5, 2007, 11:32:38 PM4/5/07
to perian-...@googlegroups.com

On Apr 4, 2007, at 10:03 PM, David Conrad wrote:

> <snip>


>
> Indeed, my PowerBook requires big-endian output. The attached patch
> gets AC3 passthrough to work for me.
>

Applied. I'll take your word for it that it works.

>>> <snip>


>>> Couldn't you get the default audio device, save its current
>>> kAudioStreamPropertyPhysicalFormat, then set it with the same
>>> sample rate as the AC3, then revert it to what it was when the
>>> codec is closed? Or does that sound too hacky to be done from
>>> within A52Codec? I'm sure it wouldn't work as expected if multiple
>>> files were opened at once, but is that an issue on the ATV?
>>>
>>
>> I have considered this, but I just feel that this is the kind of
>> thing that really doesn't belong in the codec. While this could work
>> well enough on the ATV, it would be problematic on the mac in
>> general. Plus, when opening a video file and the initial playback,
>> core audio (even on the ATV) tends to create and destroy several
>> instances of the codec before finally settling on one.
>
> Yeah, I can see how it could easily get weird from within the codec.
> I guess that if we eventually have an easy-install process for Perian
> on the ATV we'd go the ATVFiles route of a new plugin for movies.
>

Yeah, I would hope we have something like this. The darwin image on
the flash drive hack would be a great launch point for this, assuming
they get it working. Putting the set together would be the best.

Sort of a, download this program, insert a flash drive, run the
program, put the flash drive in your ATV, hold these buttons, wait
till it says it is done, remove the flash drive, and reboot.

>> <snip>


>> I tried doing this. Let Perian say it is 6 channel, and have the
>> codec only claim that it outputs 2 channel. The result was the codec
>> was not used. This would have been easier, as it wouldn't require
>> the perian hack to make passthrough work.
>
> That's unfortunate since if you save a reference movie with the
> correct layout, you can't use passthrough...
>

Yeah, I was hoping it would work because it wouldn't require messing
with perian at all. I was also scared of doing this because I was a
bit concerned that it may try to mix in the other 4 channels and
possibly screw things up even if it were all 0s.

>> Yeah, that idea has also been suggested on my trac: http://
>> trac.cod3r.com/a52codec/ticket/14 Would certainly help the 5.1 AAC
>> files apple distributes in their HD trailers (these DO NOT PLAY IN
>> 5.1 on the AppleTV, but instead use Pro Logic (not even Pro Logic
>> II)). Re-encoding the 6 channels to AC3 would really help there.
>> IMHO, this is something that Apple SHOULD HAVE DONE on the AppleTV in
>> the first place! What is the point of optical output when nothing
>> will output anything above Pro Logic (without these hacks that is)?
>> Aside from hacking AC3 or DTS into a wav file, is it possible to play
>> anything in true multi-channel surround on the ATV?
>
> Since the HDMI on the ATV seems only to support 2 channels as well,
> it seems that there isn't any way to play true multichannel surround
> on the ATV out-of-the-box. But optical isn't all about multichannel
> audio; it also gives bit-exact audio to the receiver, which will
> likely have a better DAC than the ATV would.
>

Yeah, I ranted a bit there. There is a great use to having the
receiver doing the DAC. I suppose I was thinking more from the
standpoint of, "You put optical on this thing and didn't provide any
way for multichannel audio?" Maybe, just maybe, Apple (or maybe an
engineer or two there) was hoping that someone would do exactly what
I did. I think I'll be optimistic and adopt that stance.

On a side note, I already got a request to do the same with DTS :)

>> I used a fair amount of VLC's code to do the same with this hack.
>> Watch the endianness though.
>>
>> P.S. check the way I set the passthrough variable in the MKV code. I
>> tried to put it in with the least impact, but you may need to hook up
>> some more stuff to it or something. Essentially, it as pref lookup
>> and check. Maybe it should really be put in the ac3 parser only?
>
> The only thing missing in MKV is the constructor stuff. Should it
> check the same pref as in the A52Codec? It seems that the AVI
> importer isn't checking it at the moment. As for putting it only in
> the ac3 parser, that makes some sense for what we're currently using
> it for and would simplify the code some.

I reverted that commit and just put the pref check in the bitstream
info stuff. Much simpler in the code, and truthfully, that is where
it belongs.

- Graham


Augie Fackler

unread,
Apr 5, 2007, 11:59:29 PM4/5/07
to perian-...@googlegroups.com

On Apr 5, 2007, at 11:32 PM, Graham Booker wrote:

>>
>> Yeah, I can see how it could easily get weird from within the codec.
>> I guess that if we eventually have an easy-install process for Perian
>> on the ATV we'd go the ATVFiles route of a new plugin for movies.
>>
>
> Yeah, I would hope we have something like this. The darwin image on
> the flash drive hack would be a great launch point for this, assuming
> they get it working. Putting the set together would be the best.
>
> Sort of a, download this program, insert a flash drive, run the
> program, put the flash drive in your ATV, hold these buttons, wait
> till it says it is done, remove the flash drive, and reboot.

Are there any modern versions of linux that have HFS+ write support?
It'd be easier to make a minimally bootable flash drive on linux,
even if there'd be some irony in the entire setup at that point....


Graham Booker

unread,
Apr 6, 2007, 11:12:26 AM4/6/07
to perian-...@googlegroups.com

On Apr 5, 2007, at 10:59 PM, Augie Fackler wrote:

>
> Are there any modern versions of linux that have HFS+ write support?
> It'd be easier to make a minimally bootable flash drive on linux,
> even if there'd be some irony in the entire setup at that point....
>

Actually, they are not that far off without linux. If you have a
copy of Intel OS X, you can do it already since the full OS X can
boot the ATV. Also, since linux HFS+ support on linux seems to be
limited to an SF project, I am not very inclined to trust it.

Not a free solution, but that is where darwin should step in. At
least its HFS+ support works.

- Graham


Reply all
Reply to author
Forward
0 new messages