Hydrogenaudio Forums

Lossless Audio Compression => FLAC => Topic started by: filoe on 05 September, 2012, 04:35:31 PM

Title: FLAC Subframe size
Post by: filoe on 05 September, 2012, 04:35:31 PM
I am writing a Flac decoder. And there are 4 subframe types.
I am programming in C# and I have to read an amount of bytes of a stream. (the amount of bytes of all subframes or of the next subframe)
But how can I get the size of the ENCODED subframe?
Title: FLAC Subframe size
Post by: tuffy on 06 September, 2012, 08:18:14 AM
There is no subframe size field.  When the decoder has finished with one subframe, it simply expects the next subframe to follow (without any byte alignment) until all of them have been decoded.
Title: FLAC Subframe size
Post by: filoe on 06 September, 2012, 11:00:56 AM
Hmmm so is there a max size?
Because I have to know how many bytes I have to read into a buffer. I can t load the whole file into memory because that would case a outofmem exception
Title: FLAC Subframe size
Post by: pdq on 06 September, 2012, 11:29:57 AM
Worst case would be slightly larger than the uncompressed data.
Title: FLAC Subframe size
Post by: tuffy on 06 September, 2012, 11:32:54 AM
There technically isn't any maximum size for a given FLAC frame; a perfectly valid LPC subframe might be much larger than VERBATIM by populating its tunable fields with junk, for example.  The reference decoder handles this by only working on a bit-by-bit basis and not caring whether whatever's in the byte buffer is part of one frame or another.  You'll just need to ensure callbacks for CRC-8 and CRC-16 calculation are only executed as each byte is consumed rather than as the buffer is filled (or use a 1 byte buffer).