[Flac-dev] FLAC StreamInfo Parsing

Brian Willoughby brianw at sounds.wa.com
Tue Jun 22 21:38:10 PDT 2010


On Jun 22, 2010, at 21:27, Ilia Ternovich wrote:
> Thank you very much! But how to deal with endianness in the case of
> bit stream? Some blocks (for example MinBlockSize) require 16bits
> (simply swap first and second), some block (e.g.MinFrameSize) require
> 3 byte-array to be reverted.
>
> Finally totalSamples is stored in 5 bytes ( only last 4 bits from
> first one byte are used). It was a real issue to make it little-endian
> (here is how I did it:
> http://code.google.com/p/sharpflac/source/browse/trunk/SharpFlac/ 
> Blocks/StreamInfo.cs).
>
> So to make stream little endian, I can't simple get each 4 bytes of
> the stream one by one and revert them...
>
> Anyways I've managed to decode StreamInfo, VorbisComments,SeekPoints
> and Application blocks without bitStream implementation. But I'd be
> really pleased if anyone pointed me out how to deal with endianness.


Endianness involves multiple bytes.  Thus, if you never read more  
than one byte at a time, then you won't have endianness issues.  You  
should convert from stream endianness to native endianness before  
combining bits into data of more than one byte.

Your bitstream parser must read only one byte at a time and pull bits  
from it until they are completely exhausted before reading the next  
byte.  The FLAC stream itself has an endianness within the byte that  
is predefined and will be the same across all platforms.  When you  
need to read more than 8 bits for a field, you must only grab 8 bits  
at a time from the stream, then add them to your own accumulator that  
is large enough to hold the largest field (32 bits?).

In other words, I think your only problem is that you're reading 4  
bytes of the stream when you should only be reading 1 byte at a time.

Another hint is that you should not try to maintain the same  
structure in your host-native data as in the FLAC stream.  i.e.  Do  
not try to convert the stream from big endian to little endian.   
Instead, convert each value to separate fields in a structure, or  
even to separate variables.  Packing is only needed to save stream  
bandwidth - you should have plenty of memory in your program.

Brian Willoughby
Sound Consulting



More information about the Flac-dev mailing list