SOS Data Encoding
by bkenwright@xbdev.net
Well before you get to the SOS Section, you should have read in the data from SOF (Start of Frame) Section, which tells you how many Y block, U blocks and V blocks there are.
So using this information we start to read in our data.
Skip the blocks of data at the start of the SOS which tells us how many components there and which tables each component uses
Read in a Huffman code which can be of variable length bits...this is determined by going bit by bit through our data from the start until we find a matching code that is the same as in our Huffman table (e.g. Table ID: 0, Component: 0).
This Huffman code is compared with its equivalent value.. which turns out to be a single byte value.
The byte tells us how many bits to read next... e.g. 6, so the next 6 bits is our image data. We get the next six bits, and convert it to an unsigned char.... can't go having loads of bits of different lengths in our code. As you should well know, 01 is the same as 0000 0001 in binary.
Well after the dc part comes all the ac components. We read the data in similarly...but not the same. First we read the Huffman code value, which can be of any number of bits.. be keep reading the bits until we find a matching value in our Huffman table. We then get the matching value with which it is equal.... it should be a single byte.
Now the upper nibble (top 4 bits) represents how many zero's before our value, and the lower nibble (lower 4 bits) tells us how many bits to read in next to get our image value.
For example if the byte value we decode was 0x38, it would mean that the next three values are 0, and to read in the next 8 bits to get the image value that follows those three 0's in our 64 array element.
Recap!
I know a recap already your saying, well this is very important. I'm going to do some hex dumps later and show you the actual one's and zero's so you can get a feel of what is actually happening in jpeg file.
Things to look for in the stream of data.... if you get a
0xff value followed by 0x00, ignore the 0x00.
If the huf code decodes to 0x00, it means that all the rest of the 64 array are zero's.
DC
1.1 Huffman Value (decodes to a byte, which represents a length of bits)
1.2 Length Bits following the huffman value we had just read in.
AC
2.1 Huffman Value (decode to a byte, which represents two parts)
2.2 Upper Nibble (4 bits) says how many zeros in the 64 array.
2.3 Lower Nibble (4 bits) says how many bits to read next (length of bits).
2.2 Length Bits, following the Huffman value we had just read in.
2* Huffman Value which decodes to 0, indicated the end of the 64 element array, pad with zero's.
2 If a set of binary values ..e.g. is 0xff, and is followed by 00, then ignore the 00, and continue.
