Skip to content

Commit 8fbc41c

Browse files
committed
Document the 10-bit vs 12-bit rANS 4x16 order-1 frequency table size
1 parent 20db82f commit 8fbc41c

1 file changed

Lines changed: 18 additions & 14 deletions

File tree

CRAMcodecs.tex

Lines changed: 18 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -860,21 +860,24 @@ \subsection{Frequency tables}
860860

861861
The order-1 frequency table itself may still be quite large, so is
862862
optionally compressed using the order-0 rANS4x16 codec. This is
863-
specified in the first byte. If this is 1, it is followed by 7-bit
864-
encoded uncompressed and compressed lengths and then the compressed
865-
frequency data. The pseudocode here differs slightly to elsewhere as
866-
it indicates the input sources, which are either the uncompressed
867-
frequency buffer or the default (unspecified) source.
863+
specified in the bottom bit of the first byte. If this is 1, it is
864+
followed by 7-bit encoded uncompressed and compressed lengths and then
865+
the compressed frequency data. The pseudocode here differs slightly
866+
to elsewhere as it indicates the input sources, which are either the
867+
uncompressed frequency buffer or the default (unspecified) source.
868+
The top 4 bits of the first byte indicate the number of bits used for
869+
the frequency tables. Permitted values are 10 and 12.
868870

869871
% Should we just have a generic INPUT buffer so we can use either
870872
% unspecified inputs or specified ones?
871873

872874
\begin{algorithmic}[1]
873875
\Statex (Reads a table of Order-1 symbol frequencies $F_{i,j}$
874876
\Statex (and sets the cumulative frequency table $C_{i,j+1} = C_{i,j}+F_{i,j}$)
875-
\Procedure{ReadFrequencies4x16\_1}{$F, C$}
877+
\Procedure{ReadFrequencies4x16\_1}{$F, C, bits$}
876878
\State $comp \gets$ \Call{ReadUint8}{}
877-
\If{$comp > 0$}
879+
\State $bits \gets comp \shiftr 4$
880+
\If{$(comp \logand 1) \ne 0$}
878881
\State $u\_size \gets$ \Call{ReadUint7}{} \Comment{Uncompressed size}
879882
\State $c\_size \gets$ \Call{ReadUint7}{} \Comment{Compressed size}
880883
\State $c\_data \gets$ \Call{ReadData}{$c\_size$}
@@ -897,7 +900,7 @@ \subsection{Frequency tables}
897900
\EndIf
898901
\EndIf
899902
\EndForeach
900-
\State \Call{NormaliseFrequencies4x16\_0}{$F_i$, $10$}
903+
\State \Call{NormaliseFrequencies4x16\_0}{$F_i$, $bits$}
901904
\State
902905
\State $C_{i,0} \gets 0$
903906
\For{$j\gets 0 \algorithmicto 255$}
@@ -951,36 +954,37 @@ \subsection{rANS 4x16 Order-1}
951954

952955
The Order-1 code is comparable to Order-0 but with an extra dimension
953956
(the previous value) to the $F$ and $C$ matrices and a more complex
954-
system for storing the frequencies.
957+
system for storing the frequencies. The frequencies may also add up
958+
to either 1024 or 4096 (10 or 12 bits).
955959

956960
The 4 rANS streams aren't interleaved either, but in distinct regions.
957961
This makes the handling of data that isn't a multiple of 4 is a little
958962
more complex too.
959963

960964
\begin{algorithmic}[1]
961965
\Function{RansDecode4x16\_1}{$len$}
962-
\State \Call{ReadFrequencies4x16\_1}{$F$, $C$}
966+
\State \Call{ReadFrequencies4x16\_1}{$F$, $C$, $bits$}
963967
\For{$j \gets 0 \algorithmicto 3$}
964968
\State $R_j \gets$ \Call{ReadUint32}{}
965969
\State $L_j \gets 0$
966970
\EndFor
967971
\Statex (The primary unrollable loop)
968972
\For{$i\gets 0 \algorithmicto \lfloor len/4 \rfloor - 1$}
969973
\For{$j\gets 0 \algorithmicto 3$}
970-
\State $f \gets$ \Call{RansGetCumulativeFreq4x16}{$R_j,\ 10$}
974+
\State $f \gets$ \Call{RansGetCumulativeFreq4x16}{$R_j,\ bits$}
971975
\State $s \gets$ \Call{RansGetSymbolFromFreq}{$C_{L_j},\ f$}
972976
\State $out_{i + j \times \lfloor len/4 \rfloor} \gets s$
973-
\State $R_j \gets$\ \Call{RansAdvanceStep4x16}{$R_j,\ C_{L_j, s},\ F_{L_j, s},\ 10$}
977+
\State $R_j \gets$\ \Call{RansAdvanceStep4x16}{$R_j,\ C_{L_j, s},\ F_{L_j, s},\ bits$}
974978
\State $R_j \gets$\ \Call{RansRenorm4x16}{$R_j$}
975979
\State $L_j \gets s$
976980
\EndFor
977981
\EndFor
978982
\Statex (The remainder for data not a multiple of 4 in size, using $R_3$ throughout.)
979983
\For{$i \gets i \times 4 \algorithmicto len-1$}
980-
\State $f \gets$ \Call{RansGetCumulativeFreq4x16}{$R_3,\ 10$}
984+
\State $f \gets$ \Call{RansGetCumulativeFreq4x16}{$R_3,\ bits$}
981985
\State $s \gets$ \Call{RansGetSymbolFromFreq}{$C_{L_3},\ f$}
982986
\State $out_i \gets s$
983-
\State $R_3 \gets$\ \Call{RansAdvanceStep4x16}{$R_3,\ C_{L_3,s},\ F_{L_3,s},\ 10$}
987+
\State $R_3 \gets$\ \Call{RansAdvanceStep4x16}{$R_3,\ C_{L_3,s},\ F_{L_3,s},\ bits$}
984988
\State $R_3 \gets$\ \Call{RansRenorm4x16}{$R_3$}
985989
\State $L_3 \gets s$
986990
\EndFor

0 commit comments

Comments
 (0)