5. 关于码率的讨论
5.1 airconnect方案的视线
大学毕业之后就把c语言还给老师了,简单看了一下airconnect的实现,他做了个转接,接收airplay协议之后,使用alac解码,之后用flac@44/16编码,转发给upnp终端。
解码的代码在hairtunes_init中,
ctx->alac_codec = alac_init(fmtp);
编码的代码在flac_init中,
ok &= FLAC__stream_encoder_set_verify(codec, false);
ok &= FLAC__stream_encoder_set_compression_level(codec, ctx->encode.config.flac.level);
ok &= FLAC__stream_encoder_set_channels(codec, 2);
ok &= FLAC__stream_encoder_set_bits_per_sample(codec, 16);
ok &= FLAC__stream_encoder_set_sample_rate(codec, 44100);
ok &= FLAC__stream_encoder_set_blocksize(codec, FLAC_BLOCK_SIZE);
ok &= FLAC__stream_encoder_set_streamable_subset(codec, true);
ok &= !FLAC__stream_encoder_init_stream(codec, flac_write_callback, NULL, NULL, NULL, ctx);
可以看到,接收到的文件用alac解码后之后编码的时候是强制44100/16的。似乎是有点让人失望,但是可能看了下面三小节内容,可能就没那么失望了。
5.2 philippe44对airplay协议的解释
参考【6】。简单的说,airplay是支持有损aac接收的,但airconnect方案只实现了无损的alac/pcm解码。之后默认封装成无损flac发给upnp。
AirConnect receives ALAC or raw PCM from its controller / source, there is no AAC involved. Then I re-encode and send to the player either FLAC, PCM/WAV or MP3. So to be crystal clear AirConnect does NOT receive AAC from controllers.
Now, what was that file before it has been sent over AirPlay, I have no idea. The controller can do any sort of transcoding it wants. All I know is that it sends lossless ALAC or raw PCM
As said in my previous response, I only support PCM and ALAC in my AirPlay manifest, hence AirConnect will always receive one of these. AFAIK, there is support for AAC and AAC-ELD in AirPlay 1 but I don't support these.
But as a summary, AirConnect receives 16@44.1 alac or plain PCM and it translates it to raw pcm/or wav (just adding header) or flac lossless (0..9 compression level) or mp3 lossy (64..320 Kbps) and then send that to the Chromecast device.. Nothing more nothing less. Does that answer your question? I'm not sure anymore what you're looking for
5.3 mikebrady对airplay协议的解释
参考【7】。简单的说,airplay1只支持固定的44100/16。airplay2支持两种模式,实时的alac CD质量,和带缓冲的aac。
Shairport Sync uses the AirPlay 1 protocol. In my experience, the AirPlay 1 protocol uses a fixed format: 44,100 frames per second, 16 bit linear PCM, interleaved stereo, losslessly compressed in ALAC -- let's call it "CD quality ALAC" . It doesn't matter what it's being sent from or what it's being sent to, the format remains the same. If the source material is encoded in a different form, it is transcoded to this format before transmission. This accords with your experience. If the source material is actually CD quality, stored losslessly, then AFAIR it used to be the case that it was transmitted bit-perfectly over AirPlay 1 by iTunes. I don't know if that is still the case, but I presume it is.
It follows that there is no scope for, and no need for, adjustments to Shairport Sync to control how audio is encoded -- it's always sent in CD-quality ALAC.
At the receiving end, Shairport Sync is meant to modify the audio data as little as possible. For example -- assuming you are outputting to the alsa back end -- if you have a CD-quality DAC and use its hardware mixer for volume control, or if you choose to ignore the volume control information coming from the source, or if you set the volume control to max, the audio material is sent to the DAC unmodified except for an occasional sync event.
You can force Shairport Sync to choose a particular output depth, but if it's set to auto, it will select the greatest depth. For instance, if you have a CD-quality DAC and a hardware mixer but with 24 or 32 bit resolution, the highest resolution is chosen and the material is recoded from 16 to 24 or 32 bits but without doing anything to it -- 8 or 16 binary zeroes are simply added to each sample as appropriate. In other words, as before, the audio material is effectively unaltered except for occasional sync events.
If you use Shairport Sync's software mixer or if you select monophonic output (in other words, if Shairport Sync has to process the audio material), then the necessary calculations are done at 32-bit precision (giving 64-bit results for multiplications) and dithered to the appropriate bit depth -- 32 / 24 / 16 or 8 (!) bits for the output device.
As far as AirPlay 2 is concerned, what we know is that it uses two transmission modes -- Realtime and Buffered -- and two encoding schemes: CD-Quality ALAC for Realtime streams and AAC for Buffered streams. So it fits that the new lossless formats might be transcoded to AAC for onward transmission to AirPlay 2 players.
5.4 苹果官方对airplay协议的解释
我简单搜索了一下,得到的结论是:
A. airplay/airplay2码率是44/48,但是48只有视频文件才支持,详见【8】。
AirPlay (1 and 2) is capable of ALAC (lossless) streaming up to 44.1 kHz (48 kHz for video contents).
B. airplay2只支持16bit。详见【9】。
【6】https://github.com/philippe44/AirConnect/issues/356
【7】https://github.com/mikebrady/shairport-sync/issues/1205
【8】https://discussions.apple.com/thread/252828942
【9】https://www.reddit.com/r/AppleMusic/comments/nkj26w/does_airplay_support_24_bit_how_do_we_take/
5.5 小结
综合5.1/5.2/5.3小节内容,我们知道airplay1/2对音频的支持上限就是alac@44/16,因此本帖子讨论的airconnect已经完美实现了无损的解码编码,所有的转码都发生在苹果自己的apple music投放airplay过程中。
也就是说,如果你希望使用airplay投放,可以放心的使用本帖方案;如果你追求高码率,那么直接使用ipad otg直连解码是更好的选择。 |