FLAC Encoder Settings.

Everything related to MakeMKV
Post Reply
ndjamena
Posts: 830
Joined: Mon Jan 07, 2013 12:23 am

FLAC Encoder Settings.

Post by ndjamena »

Fast and Best are defined in the MakeMKV profiles as compression levels 5 and 12 respectively. Placebo uses these settings [-compression_level 12 -lpc_coeff_precision 15 -lpc_passes 8 -lpc_type 3] taken from this thread and which I've reused here. As far as I'm aware EAC3To has no FLAC encoding options and just uses a compression level of 8 (the exact meaning of that can change depending of the version of FLAC dll being used.)

[The stats for TrueHD do not include the AC3 core which is meaningless in Matroska]

Avatar:
  • [Original DTS-MA] - 5,522,576,168 bytes
  • [Fast] - 4,770,809,049 bytes
  • [Best] - 4,708,765,386 bytes
  • [Placebo] - 4,693,853,169 bytes
  • [EAC3To] - 4,770,395,923 bytes
Ice Age:
  • [Original DTS-MA] - 2,473,173,480 bytes
  • [Fast] - 2,125,667,114 bytes
  • [Best] - 2,112,536,252 bytes
  • [Placebo] - 2,104,601,411 bytes
  • [EAC3To] - 2,122,654,844 bytes
Anchorman: The Legend of Ron Burgundy:
  • [Original DTS-MA] - 2,530,440,244 bytes
  • [Fast] - 2,071,539,828 bytes
  • [Best] - 2,017,713,277 bytes
  • [Placebo] - 2,013,131,453 bytes
  • [EAC3To] - 2,086,029,530 bytes
Robocop (Original):
  • [Original DTS-MA] - 2,618,923,504 bytes
  • [Fast] - 1,031,090,909 bytes
  • [Best] - 1,021,759,463 bytes
  • [Placebo] - 1,019,610,674 bytes
  • [EAC3To] - 1,018,932,897 bytes
Star Trek (Reboot):
  • [Original TrueHD] - 3,427,556,578 bytes
  • [Fast] - 3,258,786,743 bytes
  • [Best] - 3,231,732,725 bytes
  • [Placebo] - 3,219,274,294 bytes
  • [EAC3To] - 3,275,855,661 bytes
Wall•E:
  • [Original DTS-MA] - 3,054,021,908 bytes
  • [Fast] - 2,619,622,706 bytes
  • [Best] - 2,595,497,131 bytes
  • [Placebo] - 2,587,290,048 bytes
  • [EAC3To] - 2,612,753,868 bytes
The Matrix has 16 bit TrueHD with 8 bits of Zero padding, or in other words The Matrix has 24 bit TrueHD but it only actively uses 16 of those bits. EAC3To will automatically remove the zero padding in a second pass when it discovers they're there.

The Matrix:
  • [Original TrueHD (24bit)] - 1,682,948,214 bytes
  • [Fast (24bit)] - 1,519,384,931 bytes
  • [Best (24bit)] - 1,511,301,553 bytes
  • [Placebo (24bit)] - 1,508,356,853 bytes
  • [PCM (24Bit)] - 7,065,650,632 bytes
  • [Fast (16bit)] - 1,518,873,815 bytes
  • [Best (16bit)] - 1,510,790,437 bytes
  • [Placebo (16bit)] - 1,507,845,737 bytes
  • [EAC3To (16bit)] - 1,516,428,541 bytes
  • [PCM (16Bit)] - 4,710,433,796 bytes
Removing the zero padding saves about half a megabyte in the final FLAC, but more importantly, if you used the AAC profile on the track (which converts the 24bit audio to 16bit) you'd wind up with audio with a bitdepth of ~10. When running audio through any kind of filter it's always better to know the actual bit depth beforehand.

No matter the settings, FLAC seems to always be smaller than the original Blu Ray audio tracks. The overall bitrate for the Robocop FLAC files are actually lower than that of the original DTS-MA core.

EAC3To adds several kilobytes worth of padding to it's FLAC encodes for the benefit of header/tag editors. FFMPEG also adds about 8k worth of padding IF you encode directly to a .flac format. On the other hand if you use FFMPEG to encode FLAC directly into a Matroska File the padding will be reduced to a minimal amount. However, both EAC3To and FFMPEG are Nazis on FLAC default channel layouts and will religiously strip any mention of a channel layout if it happens to equal the FLAC defaults. The current version MakeMKV has begun applying those defaults, but MediaInfo has yet to be updated with that ability. Looking at an FFMPEG or EAC3To FLAC track with a default channel layout using MediaInfo will show no channel layout at all. Until it's updated the only way to get the layout to show when needed is to manually add a WAVEFORMATEXTENSIBLE_CHANNEL_MASK tag using MetaFLAC or another FLAC capable tagging program. MetaFlac won't work on a Matroska file, so to avoid the 8K of padding using FFMPEG and get MediaInfo Channel layouts you'd need to encode to MKA, extract the audio, add the tag then mux the final movie back together again (using MKVMerge not FFMPEG - FFMPEG will strip the tag even if all it's doing is remuxing).

"Placebo" settings reduce ripping speed to about 0.3x on my PC but does tend to save some minimal amount of megabytes in the final output. Placebo may or may not be worth the effort depending on individual preferences.
Last edited by ndjamena on Tue Jul 14, 2015 7:39 pm, edited 2 times in total.
ndjamena
Posts: 830
Joined: Mon Jan 07, 2013 12:23 am

Re: FLAC Encoder Settings.

Post by ndjamena »

OK, so, transcoding FLAC works something like this:

Code: Select all

    <trackSettings input="FLAC-stereo">
        <output outputSettingsName="copy"
                defaultSelection="$app_DefaultSelectionString,-sel:all">
        </output>
        <output outputSettingsName="AC3-192"
                defaultSelection="$app_DefaultSelectionString">
        </output>
    </trackSettings>
    <trackSettings input="FLAC-multi">
        <output outputSettingsName="copy"
                defaultSelection="$app_DefaultSelectionString,-sel:all">
        </output>
        <output outputSettingsName="AC3-448"
                defaultSelection="$app_DefaultSelectionString">
        </output>
        <output outputSettingsName="AC3-192"
                defaultSelection="$app_DefaultSelectionString,-sel:all"
                audioMix="stereo16">
        </output>
    </trackSettings>
In case it's not obvious.

Cutting... in the case where multiple episodes appear within a single title (Supernatural, Iron Man:AA, Tin Tin etc.) or for whatever reason and you'd like to cut them using MKVMerge without incurring a delay in the FLAC track.

Assuming a sample rate of 48000

NTSC FILM: 48000 / (24000 / 1001) = 2002 samples per flac frame

Code: Select all

    <outputSettings name="flac-NTSC" outputFormat="FLAC">
        <description lang="eng">Save as FLAC (Frame-Size: NTSC Film)</description>
        <extraArgs>-compression_level 5 -frame_size 2002</extraArgs>
    </outputSettings>
PAL: 48000 / 25 = 1920 samples per flac frame

Code: Select all

    <outputSettings name="flac-PAL" outputFormat="FLAC">
        <description lang="eng">Save as FLAC (Frame-Size: PAL)</description>
        <extraArgs>-compression_level 5 -frame_size 1920</extraArgs>
    </outputSettings>
24p FILM: 48000 / 24 = 2000 samples per flac frame

Code: Select all

    <outputSettings name="flac-FILM" outputFormat="FLAC">
        <description lang="eng">Save as FLAC (Frame-Size: Film)</description>
        <extraArgs>-compression_level 5 -frame_size 2000</extraArgs>
    </outputSettings>
Unfortunately there no audio frame size to go with NTSC Video since it divides with a remainder.

48000 / (60000 / 1001) = 800.8
48000 / (30000 / 1001) = 1601.6
ndjamena
Posts: 830
Joined: Mon Jan 07, 2013 12:23 am

Re: FLAC Encoder Settings.

Post by ndjamena »

Speaker Layouts:

https://xiph.org/flac/format.html#FRAME_HEADER

These are the ONLY channel layouts FLAC supports by default:

Code: Select all

Channel assignment
 • 0000-0111 : (number of independent channels)-1. Where defined, the channel order follows SMPTE/ITU-R recommendations. The assignments are as follows:
◦1 channel: mono
◦2 channels: left, right
◦3 channels: left, right, center
◦4 channels: front left, front right, back left, back right
◦5 channels: front left, front right, front center, back/surround left, back/surround right
◦6 channels: front left, front right, front center, LFE, back/surround left, back/surround right
◦7 channels: front left, front right, front center, LFE, back center, side left, side right
◦8 channels: front left, front right, front center, LFE, back left, back right, side left, side right

• 1000 : left/side stereo: channel 0 is the left channel, channel 1 is the side(difference) channel 
• 1001 : right/side stereo: channel 0 is the side(difference) channel, channel 1 is the right channel 
• 1010 : mid/side stereo: channel 0 is the mid(average) channel, channel 1 is the side(difference) channel 
• 1011-1111 : reserved
Anything else is not part of the original FLAC specifications (which were developed long before Blu Ray existed and were designed mostly for CD conversions).

There is a TAG that can be added to a file called WAVEFORMATEXTENSIBLE_CHANNEL_MASK which alters the channel layout of the file. MakeMKV adds this tag to every FLAC encode it produces, but even though this tag has existed for almost a decade it was never included in the actual FLAC specifications so support for it is lackluster at best.

The Tag contains a Hexadecimal Value representing a series of bits, each bit corresponds to a speaker position as listed here:

https://msdn.microsoft.com/en-us/librar ... s.85).aspx

Code: Select all

SPEAKER_FRONT_LEFT 0x1 
SPEAKER_FRONT_RIGHT 0x2 
SPEAKER_FRONT_CENTER 0x4 
SPEAKER_LOW_FREQUENCY 0x8 
SPEAKER_BACK_LEFT 0x10 
SPEAKER_BACK_RIGHT 0x20 
SPEAKER_FRONT_LEFT_OF_CENTER 0x40 
SPEAKER_FRONT_RIGHT_OF_CENTER 0x80 
SPEAKER_BACK_CENTER 0x100 
SPEAKER_SIDE_LEFT 0x200 
SPEAKER_SIDE_RIGHT 0x400 
SPEAKER_TOP_CENTER 0x800 
SPEAKER_TOP_FRONT_LEFT 0x1000 
SPEAKER_TOP_FRONT_CENTER 0x2000 
SPEAKER_TOP_FRONT_RIGHT 0x4000 
SPEAKER_TOP_BACK_LEFT 0x8000 
SPEAKER_TOP_BACK_CENTER 0x10000 
SPEAKER_TOP_BACK_RIGHT 0x20000 
Stereo (2.0) = SPEAKER_FRONT_LEFT + SPEAKER_FRONT_RIGHT = 0x1 + 0x2 = 0x3
5.1 (side) = SPEAKER_FRONT_LEFT + SPEAKER_FRONT_RIGHT + SPEAKER_FRONT_CENTER + SPEAKER_LOW_FREQUENCY + SPEAKER_SIDE_LEFT + SPEAKER_SIDE_RIGHT = 0x1 + 0x2 + 0x4 + 0x8 + 0x200 + 0x400 = 0x60F

And so on.

It is the channel layout used in .wav files.

Currently VLC cannot read this tag, neither as far as I'm aware can Kodi. Any FLAC encode that uses a different channel layout than the defaults will not play back properly on these players (and many others).

Further, even though up to 8 channels were supported in a stream in the original specs, no actual 6.1 or 7.1 layouts were defined until recently, so they may not play back properly (or at all) in some players either.

Each flac frame in an encode contains a channel count, yet has no ability to assign layouts. The tag can only be presented at the very start of a stream which provides difficulty to adopting it as a full solution to the problem of the lack of FLAC channel layout information in all cases. Other than inventing a new codec it is the best solution available though.
ndjamena
Posts: 830
Joined: Mon Jan 07, 2013 12:23 am

Re: FLAC Encoder Settings.

Post by ndjamena »

https://github.com/mbunkus/mkvtoolnix/issues/1352

MKVMerge doesn't cut FLAC properly, I learnt that a few years ago

https://github.com/mbunkus/mkvtoolnix/issues/953

The original date was lost with the old bug tracker, I think I have the email still or I guess I could search the WDTV forum to find it out.

But that's not important. There's a hash value in FLAC headers that basically lets a decoder determine if the decoded audio matches the original losslessly. In order for MKVMerge to properly "correct" FLAC headers after splitting them, it would need to decode the entire FLAC stream on the fly then recalculate the hash and write that into the flac header, which is way beyond the general scope of MKVToolNix behaviour.

Therefore MKVMerge will NOT modify FLAC headers.

When you cut FLAC using MKVMerge, be sure to re-encode it later, especially if you intend to extract it later.
ndjamena
Posts: 830
Joined: Mon Jan 07, 2013 12:23 am

Re: FLAC Encoder Settings.

Post by ndjamena »

Converting to FLAC with frame sizes matching the video frame rate is the only way I've found so far to cut audio without incurring a delay caused by audio/video frame sizes not matching.

MKVMerge cuts PCM as if each "packet" of samples is a frame, so there's no advantage to using PCM before cutting with it. FFMPEG adds horrendous amounts of lead-in to PCM before the video starts, which kind of defeats the purpose of avoiding delays.

[[FLAC... have I forgotten something...?]]
Post Reply