Skip to main content

Notice

Please note that most of the software linked on this forum is likely to be safe to use. If you are unsure, feel free to ask in the relevant topics, or send a private message to an administrator or moderator. To help curb the problems of false positives, or in the event that you do find actual malware, you can contribute through the article linked here.
Topic: FLAC audio at 4 bits per sample? (Read 4224 times) previous topic - next topic
0 Members and 1 Guest are viewing this topic.

FLAC audio at 4 bits per sample?

Hello,
I learned earlier that FLAC can take as input an audio file with a depth of between 4 and 32 bits per sample. However, I have the following questions:
- Which format supported as an input file can have a 4-bit depth?
- Will the output FLAC file also have a 4-bit depth?
- Is it possible to specify in the encoder parameters that the output file must have a 4-bit depth?

Re: FLAC audio at 4 bits per sample?

Reply #1
Relevant thread: https://hydrogenaud.io/index.php/topic,121447.0.html

Both WAVE and AIFF can specify "valid number of bits". That would mean that they spend 8 bits encoding four bits abcd as abcd0000, and a header flag saying that only the first four are valid. (Glossing over the details about 8-bit WAVE being unsigned.)
You can store those signals in a .flac file. (Why "4" is supported and "3" is not, I don't know - likely thinking that nobody will ever have use for 3-bit LPCM.)

As for your last question, it is unclear. But at least for two interpretations of it, the answer is negative: The reference FLAC encoder itself does not have an option to decimate to 4 bits, and the reference FLAC decoder does not do that either.

(However, the official WavPack encoder can, using  --pre-quantize=4. So there you have a tool to create files for testing.)

Re: FLAC audio at 4 bits per sample?

Reply #2
I take note of your explanations. Thank you very much for answering my questions (even if the last one was perhaps not clear, (for which I apologize), you have answered it)!

Re: FLAC audio at 4 bits per sample?

Reply #3
The problem is that at some point (which isn't really clear-cut) storing lower bit depths cannot be compressed by FLAC. The residual coding stage cannot in any way use less than 1 bit per sample, so FLAC is never able to compress 1-bit streams (like DSD) with the methods currently implemented. Similarly, performance for 2-bit and 3-bit streams will probably be very poor. The 4-bit boundary is somewhat arbitrary.
Music: sounds arranged such that they construct feelings.

Re: FLAC audio at 4 bits per sample?

Reply #4
Although I think the utility for this is limited (how often do any of us work with 4 bit samples?) i think it could still be useful for the other aspects of a FLAC container for audio besides compression, like a stored mD5 or other metadata that wav does not offer.

Re: FLAC audio at 4 bits per sample?

Reply #5
The following is kinda a moot discussion - who has ever seen a 3-bit file? - but AFAIUnderstand, WavPack handles it. WavPack does give 1 bits a special treatment though: if you ask it to encode a raw file and set bit depth to 1, WavPack will assume it is DSD and compress it that way. Long story short, if you are into weirder signals than FLAC can handle, you can download WavPack right away.

But anyway:

Although I think the utility for this is limited (how often do any of us work with 4 bit samples?) i think it could still be useful for the other aspects of a FLAC container for audio besides compression, like a stored mD5 or other metadata that wav does not offer.
Agreed. Even if everything were to be stored VERBATIM, then a format with error detection is a good thing.
Tagging too - although more applications support WAVE and AIFF tagging now than when FLAC was new.

The problem is that at some point (which isn't really clear-cut) storing lower bit depths cannot be compressed by FLAC. The residual coding stage cannot in any way use less than 1 bit per sample, so FLAC is never able to compress 1-bit streams (like DSD) with the methods currently implemented. Similarly, performance for 2-bit and 3-bit streams will probably be very poor.
Oh, but if compared to a WAVE or AIFF file that would spend 8 bits and flag that only 2 or 3 would be valid, then it is not at all bad I guess.

The 4-bit boundary is somewhat arbitrary.
In the sense that nothing in the code forbids 2 or 3 "except an IF condition that has a 4 because that is what the specification says", right?
IDK whether 1 could lead to weird things.

Re: FLAC audio at 4 bits per sample?

Reply #6
The following is kinda a moot discussion - who has ever seen a 3-bit file?
Correct that to "seen a 3-bit file not created for the sake of testing one".
quote author=Porcus link=msg=1033858 date=1696764889](However, the official WavPack encoder can, using  --pre-quantize=4. So there you have a tool to create files for testing.)[/quote]
... and with recent WavPack you have to use --pre-quantize; the above linked post was written when it would assume that nonzero invalid bits were intended to be zeroed out. Those WAVE files are technically invalid, and now WavPack will err out.

Re: FLAC audio at 4 bits per sample?

Reply #7
The 4-bit boundary is somewhat arbitrary.
In the sense that nothing in the code forbids 2 or 3 "except an IF condition that has a 4 because that is what the specification says", right?
IDK whether 1 could lead to weird things.
You can actually generate and store data with a bitdepth of 2 or 3 bits without problems using sox
Code: [Select]
sox input.flac output.flac dither -p 2
FLAC compresses this using wasted bits of course. On the rarewares test samples set, I get a file size of 76% of the uncompressed 2-bit data (if you could store it in uncompressed form without padding). The audio sounds like an AM international broadcast: just barely intelligible, almost completely drowned in noise. The file size for 3-bit data is 54%, so, actually, not bad at all. It sounds more like a rather noisy FM broadcast.
Music: sounds arranged such that they construct feelings.

Re: FLAC audio at 4 bits per sample?

Reply #8
I take note of your explanations. Thank you very much for answering my questions (even if the last one was perhaps not clear, (for which I apologize), you have answered it)!
Your question was clear enough, how to answer it was not.
It's your privilege to disagree, but that doesn't make you right and me wrong.

Re: FLAC audio at 4 bits per sample?

Reply #9
FLAC compresses this using wasted bits of course. On the rarewares test samples set, I get a file size of 76% of the uncompressed 2-bit data (if you could store it in uncompressed form without padding). The audio sounds like an AM international broadcast: just barely intelligible, almost completely drowned in noise.
There's always "I Won The Loudness War" by Dan Worrall (youtube). It uses only +/- full scale samples :-) I actually downloaded it, copied fragments without his voiceover and fixed the samples back to a single +/- level (after they were mangled by opus compression). The version in the attachment uses only 12th bit in 16-bit container (and a digital silence between the fragments):
Code: [Select]
]$ sox "Dan Worral - I Won The Loudness War.flac" -n stats
             Overall     Left      Right
Pk lev dB     -24.08    -24.08    -24.08
RMS lev dB    -24.25    -24.25    -24.25
...
Bit-depth       1/5       1/5       1/5

]$ ls -l "Dan Worral - I Won The Loudness War."*
...   862311 ... 'Dan Worral - I Won The Loudness War.flac'
... 15209036 ... 'Dan Worral - I Won The Loudness War.wav'

Re: FLAC audio at 4 bits per sample?

Reply #10
The version in the attachment uses only 12th bit in 16-bit container (and a digital silence between the fragments):

I think something went wrong somewhere. See these results:
Code: [Select]
$ flac -ac Dan\ Worral\ -\ I\ Won\ The\ Loudness\ War.flac | grep wasted | less | awk '{print $2}' | sort | uniq -c

flac 1.4.2
Copyright (C) 2000-2009  Josh Coalson, 2011-2022  Xiph.Org Foundation
flac comes with ABSOLUTELY NO WARRANTY.  This is free software, and you are
welcome to redistribute it under certain conditions.  Type `flac' for details.

Dan Worral - I Won The Loudness War.flac: done
    332 wasted_bits=0
    958 wasted_bits=11
    568 wasted_bits=12

So, there are 332 frames with no wasted bits (these are frames with only 0 or 1 samples), 958 frames with 11 wasted bits and 568 frames with 12 wasted bits. So, it seems the file does not only use the 12 bit?
Music: sounds arranged such that they construct feelings.

Re: FLAC audio at 4 bits per sample?

Reply #11
AFAICT

So, there are 332 frames with no wasted bits (these are frames with only 0 or 1 samples)
This is the digital silence between the fragments or _SIDE encoded subframes when the channels are the same.

958 frames with 11 wasted bits
These are the RIGHT_, LEFT_ or MID_ encoded subframes.

and 568 frames with 12 wasted bits
These are the _SIDE encoded subframes when there is a difference between the channels.

I noticed that wasted_bits=12 happens in subframe=0 only when the channel_assignment is RIGHT_SIDE and in subframe=1 when channel_assignment is LEFT_SIDE or MID_SIDE:
Code: [Select]
]$ flac -ac "Dan Worral - I Won The Loudness War.flac" 2>/dev/null | grep -B1 'subframe=0.*wasted_bits=12' | grep assignment | cut -f7 | sort -u
channel_assignment=RIGHT_SIDE

]$ flac -ac "Dan Worral - I Won The Loudness War.flac" 2>/dev/null | grep -B5 'subframe=1.*wasted_bits=12' | grep assignment | cut -f7 | sort -u
channel_assignment=LEFT_SIDE
channel_assignment=MID_SIDE

So I'm assuming those subframes get the _SIDE part of the channel_assignment.

I checked the file again and it contains only samples with values +/-2048 or 0. Side encoded subframe will result in +/- 4096 in case there is a difference between channels. So +/- 2048 gives 11 wasted bits and +/- 4096 gives 12 wasted bits.

When I split the file into separate left and right channels and encode that, I get only 0 and 11.

(And I'm sorry if I mixed up "frames" and "blocks"  :) )

Re: FLAC audio at 4 bits per sample?

Reply #12
Ah, yes, sorry, forgot about that.

Anyway, 3.802.248 samples, 2 channels, each 1 bit, would theoretically give you 950.562 byte. Dividing 862.311 byte by that is 90.7%. If I disable the use of constant subframes, I get a filesize of 1.031.562 byte, or a file size 108.5% of the original data size.

In other words, FLAC cannot compress 1-bit data, except when quite a long section of samples has the same value.
Music: sounds arranged such that they construct feelings.

Re: FLAC audio at 4 bits per sample?

Reply #13
Here is a DSD-like (1-bit 705.6kHz) wav file which can be encoded to flac by using --lax.
Warning: Don't try to play the file at the original sample rate, resample to not higher than 44.1/48k to cut the ultrasonic noise.

Re: FLAC audio at 4 bits per sample?

Reply #14
Here is a DSD-like (1-bit 705.6kHz) wav file which can be encoded to flac by using --lax.
Just found that 7z's Ultra preset is smaller than xz, 1613826 bytes.

4796827 flac -1b65535
4831707 flac -8pe
5456186 wv -x6
5508182 wv -hhx6

As expected, using WavPack's PCM mode to compress DSD-like data yielded poor results as well.

Re: FLAC audio at 4 bits per sample?

Reply #15
"DSD" files with arbitrary sample rates can be created using SoX-DSD
https://www.audiofaidate.org/sw/sox-dsd/README.html

Assume input sample rate is 44.1k:

DSD16
sox -r176400 in.wav -p vol .75 rate 2822400 sdm -f clans-8 | sox -r705600 -p -b8 -D out.wav vol .5

DSD23 (attached)
sox -r122713 in.wav -p vol .75 rate 2822400 sdm -f clans-8 | sox -r1014300 -p -b8 -D out.wav vol .5

The first vol is optional for clipping prevention during upsampling, the second vol is required to avoid severe bloating when converting to flac, though the second vol does not greatly affect 7z or xz.

The commands above may not work if the input file format is not .wav.

Re: FLAC audio at 4 bits per sample?

Reply #16
There's always "I Won The Loudness War" by Dan Worrall
That's one mofo of a file. I wonder if the original lossless PCM (that hasn't been through Opus for youtubing) is just as wild compression-wise.

% of WAVE, that is, lower is better. It makes a fool out of any OptimFROG except the lightest mode, and of Monkey's - and at this stage I am not so surprised to see the ape run in the reverse order on weird signals. Also it reverses the usual size order of WavPack modes until you fire -x4 at it, but WavPack does this in a very impressive way - that might be due to the way it handles wasted bits? (It can handle "ones at the end" just as well as "zeroes at the end".)

1.17% to 1.29%   OptimFROG --preset 0 & optimizations from "best" (which does best) to "none"
2.30% to 2.37%   WavPack various modes, -x4 to -x6
2.40% to 2.62%   WavPack various modes, -x1 to -x3, -f best and -hh worst
2.68% to 3.31%   WavPack -f, -g, -h, -hh in that (reverse!) order
3.58% to 3.64%   TAK in close to expected order
4.81%            ALS at -7 -p -l
5.66%            FLAC, "all stereo settings with -e".  Best one: -2e -b2048 --lax -r15.  Oddly, using -6, -7, -5, -8 are a few bytes worse.
5.73% to 5.77%   FLAC, "all stereo settings without -e". 
5.90%            FLAC at -1 (that is "fast & soft joint stereo")
6.88% to 7.14%   FLAC at dual mono
11.8% to 17.1%   OptimFROG, everything else than variations on --preset 0
21.1% to 21.3%   ALAC (refalac and ffmpeg default)
37%              TTA
44%              ALS at default
41% to 50%       Monkey's, in reverse order of what you would expect


FLAC figures are without padding. It seems "-e" doesn't completely brute-force.

Re: FLAC audio at 4 bits per sample?

Reply #17
Here's a DSD-like (1-bit, 882 kHz) WAV file. Note: Don't play this at the original sample rate, and RESAMPLE to 44.1/48 kHz, not higher or will cut the ultrasonic noise.

Re: FLAC audio at 4 bits per sample?

Reply #18
That's one mofo of a file.
[...]I wonder if the original lossless PCM (that hasn't been through Opus for youtubing) is just as wild compression-wise.

% of WAVE, that is, lower is better. It makes a fool out of any OptimFROG except the lightest mode

Left the uselessly ultra-slow SAC on overnight. Not surprising to see many frog-alike outcomes, only slower.

* At its best - spending hours on the minute - it could beat OptimFROG --preset 0, but not when the frog got its "high" and "best" optimizations on top. That was sac with --normal --optimize=high --sparse-pcm . The "high" mode did worse.  Dropping the --sparse-pcm, it was still closer to OptimFROG --preset 0 than to best WavPack. Fast and normal optimization (but --sparse-pcm): somewhat worse than TAK.
* Smallest with "high" compression mode (parameters --high --optimize=normal --sparse-pcm): squeezed itself between the WavPack and the TAK. But with other --optimize, it did worse than the stereo FLACs.
* A few settings meddled in with those frogs that looked like fools
* And the remaining between ALAC and TTA.

It isn't enough to gnaw at long-term patterns.

... oh, and to "new" readers: Monkey's, ALAC and TTA cannot do so-called "wasted bits", which explain why they lose out on these special signals.