%Copyright (C) 2007-2015  Brian Langenberger
%This work is licensed under the
%Creative Commons Attribution-Share Alike 3.0 United States License.
%To view a copy of this license, visit
%http://creativecommons.org/licenses/by-sa/3.0/us/ or send a letter to
%Creative Commons,
%171 Second Street, Suite 300,
%San Francisco, California, 94105, USA.

\section{WavPack Decoding}

\input{wavpack/algorithms/decode_wavpack}
\par
\noindent
For example, four blocks with the following attributes:
\begin{table}[h]
\begin{tabular}{rrr}
channel count & initial block & final block \\
\hline
2 & 1 & 0 \\
1 & 0 & 0 \\
1 & 0 & 0 \\
2 & 0 & 1 \\
\end{tabular}
\end{table}
\par
\noindent
combine into 6 channels of PCM output as follows:
\begin{figure}[h]
\includegraphics{wavpack/figures/block_channels.pdf}
\end{figure}

\clearpage

\subsection{Decoding Block}
\label{wavpack:decode_block}
{\relsize{-1}
  \input{wavpack/algorithms/decode_block}
}

\clearpage

\subsection{Reading Block Header}
\label{wavpack:read_block_header}
{\relsize{-1}
  \input{wavpack/algorithms/read_block_header}
}

\clearpage

\subsubsection{Decoding Sample Rate}
\label{wavpack:decode_sample_rate}
{\relsize{-1}
\input{wavpack/algorithms/decode_sample_rate}
}
\clearpage

\subsubsection{Reading Block Header Example}
\begin{figure}[h]
  \includegraphics[height=3.5in,keepaspectratio]{wavpack/figures/block_header_parse.pdf}
\end{figure}

{\relsize{-2}
  \begin{tabular}{rrl}
    field & value & meaning \\
    \hline
    \textbf{block ID} & \texttt{0x6B707677} & \texttt{"wvpk"} \\
    \textbf{block data size} & \texttt{0x000000B2} & $178 - 24 = 154$ bytes \\
    \textbf{version} & \texttt{0x0407} \\
    \textbf{track number} & \texttt{0} \\
    \textbf{index number} & \texttt{0} \\
    \textbf{total samples} & \texttt{0x00000019} & 25 PCM frames \\
    \textbf{block index} & \texttt{0x00000000} & 0 PCM frames \\
    \textbf{block samples} & \texttt{0x00000019} & 25 PCM frames \\
    \textbf{bits per sample} & \texttt{1} & 16 bits per sample \\
    \textbf{mono output} & \texttt{0} & 2 channel block \\
    \textbf{hybrid mode} & \texttt{0} & lossless data \\
    \textbf{joint stereo} & \texttt{1} & channels stored in joint stereo \\
    \textbf{channel decorrelation} & \texttt{1} & cross-channel decorrelation used \\
    \textbf{hybrid noise shaping} & \texttt{0} \\
    \textbf{floating point data} & \texttt{0} & samples stored as integers \\
    \textbf{extended size integers} & \texttt{0} \\
    \textbf{hybrid controls bitrate} & \texttt{0} \\
    \textbf{hybrid noise balanced} & \texttt{0} \\
    \textbf{initial block} & \texttt{1} & is initial block in sequence \\
    \textbf{final block} & \texttt{1} & is final block in sequence \\
    \textbf{left shift data} & \texttt{0} \\
    \textbf{maximum magnitude} & \texttt{15} & 16 bits per sample output \\
    \textbf{sample rate} & \texttt{9} & 44100 Hz \\
    \textbf{reserved} & \texttt{0} \\
    \textbf{use IIR} & \texttt{0} \\
    \textbf{false stereo} & \texttt{0} & both channels are not identical \\
    \textbf{reserved} & \texttt{0} \\
    \textbf{CRC} & \texttt{0x22D25AD7} \\
  \end{tabular}
}

\clearpage

\subsection{Reading Block Parameters}
\label{wavpack:read_block_parameters}
{\relsize{-2}
\input{wavpack/algorithms/read_block_parameters}
}

\clearpage

\input{wavpack/decode/terms}

\clearpage

\input{wavpack/decode/weights}

\clearpage

\input{wavpack/decode/samples}

\clearpage

\input{wavpack/decode/entropy}

\clearpage

\input{wavpack/decode/bitstream}

\clearpage

\input{wavpack/decode/decorrelation}

\clearpage

\subsection{Undo Joint Stereo}
\label{wavpack:undo_joint_stereo}
\input{wavpack/algorithms/undo_joint_stereo}

\subsubsection{Joint Stereo Example}
\begin{table}[h]
\begin{tabular}{|r|r|r||>{$}r<{$}|>{$}r<{$}|}
$i$ & $\textsf{mid}_i$ & $\textsf{side}_i$ & \textsf{right}_i & \textsf{left}_i \\
\hline
0 & -64 & 32 &
32 - \lfloor-64 \div 2\rfloor = 64 &
-64 + 64 = 0 \\
1 & -46 & 39 &
39 - \lfloor-46 \div 2\rfloor = 62 &
-46 + 62 = 16 \\
2 & -25 & 43 &
43 - \lfloor-25 \div 2\rfloor = 56 &
-25 + 56 = 31 \\
3 & -3 & 45 &
45 - \lfloor-3 \div 2\rfloor = 47 &
-3 + 47 = 44 \\
4 & 20 & 44 &
44 - \lfloor20 \div 2\rfloor = 34 &
20 + 34 = 54 \\
5 & 41 & 40 &
40 - \lfloor41 \div 2\rfloor = 20 &
41 + 20 = 61 \\
6 & 60 & 34 &
34 - \lfloor60 \div 2\rfloor = 4 &
60 + 4 = 64 \\
7 & 75 & 25 &
25 - \lfloor75 \div 2\rfloor = -12 &
75 + -12 = 63 \\
8 & 85 & 15 &
15 - \lfloor85 \div 2\rfloor = -27 &
85 + -27 = 58 \\
9 & 90 & 4 &
4 - \lfloor90 \div 2\rfloor = -41 &
90 + -41 = 49 \\
\hline
\end{tabular}
\end{table}

\clearpage

\subsection{Checksum Calculation}
\label{wavpack:verify_crc}
\input{wavpack/algorithms/checksum_calculation}

\subsubsection{Checksum Calculation Example}
{\relsize{-1}
\begin{tabular}{|r|r|r||>{$}r<{$}|>{$}r<{$}|>{$}r<{$}|}
$i$ & $\textsf{channel}_{0~i}$ & $\textsf{channel}_{1~i}$ & \textsf{LCRC}_i & \text{SCRC}_i & \textsf{CRC}_i \\
\hline
0 & 0 & 64 &
(3 \times \texttt{FFFFFFFF}) + 0 = \texttt{2FFFFFFFD} &
(3 \times \texttt{2FFFFFFFD}) + 64 = \texttt{900000037} &
\texttt{00000037} \\
1 & 16 & 62 &
(3 \times \texttt{00000037}) + 16 = \texttt{000000B5} &
(3 \times \texttt{000000B5}) + 62 = \texttt{0000025D} &
\texttt{0000025D} \\
2 & 31 & 56 &
(3 \times \texttt{0000025D}) + 31 = \texttt{00000736} &
(3 \times \texttt{00000736}) + 56 = \texttt{000015DA} &
\texttt{000015DA} \\
3 & 44 & 47 &
(3 \times \texttt{000015DA}) + 44 = \texttt{000041BA} &
(3 \times \texttt{000041BA}) + 47 = \texttt{0000C55D} &
\texttt{0000C55D} \\
4 & 54 & 34 &
(3 \times \texttt{0000C55D}) + 54 = \texttt{0002504D} &
(3 \times \texttt{0002504D}) + 34 = \texttt{0006F109} &
\texttt{0006F109} \\
5 & 61 & 20 &
(3 \times \texttt{0006F109}) + 61 = \texttt{0014D358} &
(3 \times \texttt{0014D358}) + 20 = \texttt{003E7A1C} &
\texttt{003E7A1C} \\
6 & 64 & 4 &
(3 \times \texttt{003E7A1C}) + 64 = \texttt{00BB6E94} &
(3 \times \texttt{00BB6E94}) + 4 = \texttt{02324BC0} &
\texttt{02324BC0} \\
7 & 63 & -12 &
(3 \times \texttt{02324BC0}) + 63 = \texttt{0696E37F} &
(3 \times \texttt{0696E37F}) - 12 = \texttt{13C4AA71} &
\texttt{13C4AA71} \\
8 & 58 & -27 &
(3 \times \texttt{13C4AA71}) + 58 = \texttt{3B4DFF8D} &
(3 \times \texttt{3B4DFF8D}) - 27 = \texttt{B1E9FE8C} &
\texttt{B1E9FE8C} \\
9 & 49 & -41 &
(3 \times \texttt{B1E9FE8C}) + 49 = \texttt{215BDFBD5} &
(3 \times \texttt{215BDFBD5}) - 41 = \texttt{64139F356} &
\texttt{4139F356} \\
\hline
\end{tabular}
}
\vskip 1em
\par
\noindent
Resulting in a final CRC of \texttt{0x4139F356}

\clearpage

\subsection{Reading Extended Integers}
\label{wavpack:read_extended_integers}
\input{wavpack/algorithms/read_extended_integers}

\begin{figure}[h]
  \includegraphics{wavpack/figures/extended_integers.pdf}
\end{figure}

\subsection{Applying Extended Integers}
\label{wavpack:apply_extended_integers}
{\relsize{-1}
  \input{wavpack/algorithms/apply_extended_integers}
}

\clearpage

\subsection{MD5 Sum}
\label{wavpack:md5_sum}
The MD5 is the hash of all the PCM samples, on a PCM frame basis,
in little-endian format and signed if the bits per sample is greater than 0.

\begin{figure}[h]
\includegraphics{wavpack/figures/md5sum.pdf}
\end{figure}

\subsection{RIFF WAVE Header and Footer}

These sub-blocks are typically found in the first and last
WavPack block, respectively.
The header must always be present in the file while
the footer is optional.

\begin{figure}[h]
\includegraphics{wavpack/figures/pcm_sandwich.pdf}
\end{figure}
