I have created a minimal stereo FLAC file according to the specifications here. It is a single block of 8192 samples of 0 encoded as a constant:
00000000: 664c 6143 8000 0022 2000 2000 0000 0000
00000010: 0000 0ac4 42f0 0000 0000 bb7d f04e 1b0a
00000020: 2570 6575 27a7 e108 ae23 fff8 d918 0060
00000030: 0000 0000 0000 119a
This file is checked as "ok" if I run flac -t stereo_file.flac
. According to the above linked specification, the high 4 bits of byte 0x18
at 0x2D
indicate channel assignment 0001
, i.e. left and right-channels. If I change that one nybble to 8
, for channel assignment 1000
, or left and side- decorrelated channels (and adjusting the CRC8 at 0x2F
and the CRC16 at 0x36
), I get the following file:
00000000: 664c 6143 8000 0022 2000 2000 0000 0000
00000010: 0000 0ac4 42f0 0000 0000 bb7d f04e 1b0a
00000020: 2570 6575 27a7 e108 ae23 fff8 d988 0081
00000030: 0000 0000 0000 a880
Except this time, when I run the above flac
command, it reports:
stereo_file.flac: *** Got error code 0:FLAC__STREAM_DECODER_ERROR_STATUS_LOST_SYNC
*** Got error code 0:FLAC__STREAM_DECODER_ERROR_STATUS_LOST_SYNC
stereo_file.flac: ERROR during decoding
state = FLAC__STREAM_DECODER_END_OF_STREAM
I can see no reason why this should give an error, unless there is some kind of nuance or caveat not mentioned in the specifications regarding decorrelation. Is there any way I can tell in greater detail what is causing this error for the second file, and how to fix it?
While the left, right and mid channels are 16 bit, the side channel needs to be 17 bit, because otherwise the side channel could clip or overflow.
For example, if at a certain point in the stream, the left channel is 32767 (the maximum positive integer when using signed 16-bit) and the right channel is -32768 (the maximum negative integer) the result when using left-side would be a side sample of -65535, which has to be stored with at least 17 bit.
I agree that this is something not mentioned clearly in the FLAC specification. See also this code on github.