%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.

\chapter{WavPack}
WavPack is a format for compressing Wave files, typically in lossless mode.
Notably, it also has a lossy mode and even a hybrid mode which allows
the `correction' file to be separated from a lossy core.

Metadata is stored as an APEv2 tag, which is described on page \pageref{apev2}.

Its stream of data is stored little-endian.

\section{the WavPack File Stream}
\begin{figure}[h]
\includegraphics{wavpack/figures/stream.pdf}
\end{figure}

\clearpage

\subsection{Block Header}
\begin{figure}[h]
\includegraphics{wavpack/figures/block_header.pdf}
\end{figure}

\begin{table}[h]
\begin{tabular}{rl}
\textbf{block ID} & always \texttt{"wvpk"} or \texttt{0x6B707677} \\
\textbf{block size} & number of bytes between \VAR{block size} field's end and block's end \\
\textbf{version} & the file version, between \texttt{0x0402} and \texttt{0x0410} \\
\textbf{track number} & always 0 \\
\textbf{index number} & always 0 \\
\textbf{total samples} & length of the entire file in PCM frames \\
\textbf{block index} & block's PCM frame offset in the stream \\
\textbf{block samples} & length of the block in PCM frames \\
\textbf{CRC} & checksum of decoded PCM data \\
\end{tabular}
\end{table}

\clearpage

\begin{table}[h]
{\relsize{-1}
\begin{tabular}{rrl}
flag & value & meaning \\
\hline
\hline
\textbf{bits per sample} & \texttt{0} & 8 bits per sample \\
& \texttt{1} & 16 bits per sample \\
& \texttt{2} & 24 bits per sample \\
& \texttt{3} & 32 bits per sample \\
\hline
\textbf{mono output} & \texttt{0} & block has 2 channels of output \\
& \texttt{1} & block has 1 channel of output \\
\hline
\textbf{hybrid mode} & \texttt{0} & data stored lossless \\
& \texttt{1} & data stored lossy, with external correction file \\
\hline
\textbf{joint stereo} & \texttt{0} & channels stored in true stereo \\
& \texttt{1} & channels stored in joint stereo \\
\hline
\textbf{channel decorrelation} & \texttt{0} & channels stored independently \\
& \texttt{1} & cross-channel decorrelation between channels \\
\hline
\textbf{hybrid noise shaping} & \texttt{0} & hybrid has flat noise spectrum \\
& \texttt{1} & hybrid has noise shaping \\
\hline
\textbf{floating point data} & \texttt{0} & samples stored as integers \\
& \texttt{1} & samples stored as floats \\
\hline
\textbf{extended size integers} & \texttt{1} & $\text{bits per sample} > 24$ or left shifted \\
\hline
\textbf{hybrid controls bitrate} & \texttt{0} & hybrid parameters control noise level \\
& \texttt{1} & hybrid parameters control bitrate \\
\hline
\textbf{hybrid noise balanced} & \texttt{1} & hybrid noise balanced between channels \\
\hline
\textbf{initial block} & \texttt{1} & initial block in multichannel sequence \\
\hline
\textbf{final block} & \texttt{1} & final block in multichannel sequence \\
\hline
\textbf{left shift data} & & amount of bits to left-shift decoded data \\
\hline
\textbf{maximum magnitude} & & bit length of largest integer value, minus 1 \\
\hline
\textbf{sample rate} & \texttt{0} & 6000 Hz \\
& \texttt{1} & 8000 Hz \\
& \texttt{2} & 9600 Hz \\
& \texttt{3} & 11025 Hz \\
& \texttt{4} & 12000 Hz \\
& \texttt{5} & 16000 Hz \\
& \texttt{6} & 22050 Hz \\
& \texttt{7} & 24000 Hz \\
& \texttt{8} & 32000 Hz \\
& \texttt{9} & 44100 Hz \\
& \texttt{10} & 48000 Hz \\
& \texttt{11} & 64000 Hz \\
& \texttt{12} & 88200 Hz \\
& \texttt{13} & 96000 Hz \\
& \texttt{14} & 192000 Hz \\
& \texttt{15} & reserved \\
\hline
\textbf{reserved} & & ignore if set \\
\hline
\textbf{use IIR} & & use IIR for negative hybrid noise shaping \\
\hline
\textbf{false stereo} & & mono block which decodes to 2 identical channels \\
\hline
\textbf{reserved} & & always 0 \\
\end{tabular}
}
\end{table}

\clearpage

\subsection{Sub Block}

\begin{figure}[h]
\includegraphics{wavpack/figures/subblock.pdf}
\end{figure}
\par
\noindent
If \VAR{large sub block} is 0, \VAR{sub block size} is an 8 bit field.
Otherwise, it is 24 bits.
If \VAR{actual size 1 less} is 1, the contents of
\VAR{sub block data} are 1 byte shorter than its length;
the block itself is padded with 1 empty byte.
\vskip .15in
\par
\noindent
\begin{table}[h]
\begin{tabular}{rrl}
metadata function & nondecoder data & contents \\
\hline
\texttt{0} & \texttt{0} & padding \\
\texttt{2} & \texttt{0} & decorrelation terms and deltas \\
\texttt{3} & \texttt{0} & initial decorrelation weights \\
\texttt{4} & \texttt{0} & decorrelation samples \\
\texttt{5} & \texttt{0} & initial entropy variables \\
\texttt{6} & \texttt{0} & hybrid entropy variables \\
\texttt{7} & \texttt{0} & hybrid lossless info \\
\texttt{8} & \texttt{0} & floating point info \\
\texttt{9} & \texttt{0} & large integer info \\
\texttt{10} & \texttt{0} & normal compressed audio bitstream \\
\texttt{11} & \texttt{0} & correction file bitstream \\
\texttt{12} & \texttt{0} & large floating point info \\
\texttt{13} & \texttt{0} & channel count and channel mask \\
\hline
\texttt{1} & \texttt{1} & RIFF header \\
\texttt{2} & \texttt{1} & RIFF trailer \\
\texttt{5} & \texttt{1} & encoding info \\
\texttt{6} & \texttt{1} & audio data MD5 sum \\
\texttt{7} & \texttt{1} & non-standard sample rate \\
\end{tabular}
\end{table}

\clearpage

\input{wavpack/decode}

\clearpage

\input{wavpack/encode}
