%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{Meridian Lossless Packing}

\section{Meridian Lossless Packing}

An MLP stream is comprised of a series of frames.
Each frame contains one or two substreams, each storing a different
set of channels (e.g. $\text{Substream}_1$ may have channels 1-2,
$\text{Substream}_2$ may have channels 3-6, and so on).
Each substream contains one or more blocks.
Finally, each block contains \VAR{Block Data} residuals
and, optionally, decoding information.
\begin{figure}[h]
\includegraphics{figures/mlp/stream.pdf}
\end{figure}
\par
\noindent
\VAR{Total Frame Size} is the size of the entire frame,
including the header, in bytes.
\par
The first frame will contain a \VAR{Major Sync} with information
such as sample rate, bits-per-sample, substream count and so forth.
This information will be constant throughout the stream,
so most of the subsequent MLP frames will not have a sync.

The basic principle of MLP decoding is that we read in a
set of decoding parameters for the first block and use those
as arguments for decoding the block data itself.
Subsequent blocks may may contain truncated parameters
indicating only which arguments have changed so that the
next block can be decoded.

Decoded blocks are concatenated into a stream of PCM frames,
each with one or more channels which comprise an entire substream.

The channels of all decoded substreams
(each with an identical number of PCM frames) are then combined
into a single set of PCM frames and returned.

\clearpage

\subsection{The Major Sync}
\VAR{Major Sync} is an optional frame header:
\begin{figure}[h]
\includegraphics{figures/mlp/major_sync.pdf}
\end{figure}
\par
\noindent
We detect a \VAR{Major Sync} by reading a 24-bit \VAR{Sync Words} value
and 8-bit \VAR{Stream Type} value from the stream.
If \texttt{0xF8726F} and \texttt{0xBB}, respectively, we can
read the remainder of the sync.
Otherwise, we must `back up' 32 bits and continue reading substream end
and substream data values from the stream.
If a \VAR{Major Sync} is not present in a frame,
we use the values from the previous sync.
\vskip .25in
\par
\noindent
\begin{tiny}
\begin{tabular}{|c|r|r|r|l|}
\hline
Value & Bits & Sample Rate & Channels & Channel Assignment \\
\hline
\texttt{00000} & 16 & 48000 & 1 & front center \\
\texttt{00001} & 20 & 96000 & 2 & front left, front right\\
\texttt{00010} & 24 & 192000 & 3 & front left, front right, back center \\
\texttt{00011} & & & 4 & front left, front right, back left, back right\\
\texttt{00100} & & & 3 & front left, front right, LFE \\
\texttt{00101} & & & 4 & front left, front right, LFE, back center \\
\texttt{00110} & & & 5 & front left, front right, LFE, back left, back right \\
\texttt{00111} & & & 3 & front left, front right, front center \\
\texttt{01000} & & 44100 & 4 & front left, front right, front center, back center \\
\texttt{01001} & & 88200 & 5 & front left, front right, front center, back left, back right \\
\texttt{01010} & & 176400 & 4 & front left, front right, front center, LFE\\
\texttt{01011} & & & 5 & front left, front right, front center, LFE, back center \\
\texttt{01100} & & & 6 & front left, front right, front center, LFE back left, back right \\
\texttt{01101} & & & 4 & front left, front right, front center, back center \\
\texttt{01110} & & & 5 & front left, front right, front center, back left, back right \\
\texttt{01111} & & & 4 & front left, front right, front center, LFE \\
\texttt{10000} & & & 5 & front left, front right, front center, LFE, back center \\
\texttt{10001} & & & 6 & front left, front right, front center, LFE, back left, back right \\
\texttt{10010} & & & 5 & front left, front right, back left, back right, LFE \\
\texttt{10011} & & & 5 & front left, front right, back left, back right, front center \\
\texttt{10100} & & & 6 & front left, front right, back left, back right, front center, LFE \\
\hline
\end{tabular}
\end{tiny}

\subsection{Substream Info}
There is one \VAR{Substream Info} header per frame substream
(as indicated in the \VAR{Major Sync}).
\begin{figure}[h]
\includegraphics{figures/mlp/substream_size.pdf}
\end{figure}
\par
\noindent
\VAR{Substream End} is the location of the substream's end,
starting from the end of the last \VAR{Substream Info} value.

\clearpage

\subsection{Substream Data}

\begin{figure}[h]
\includegraphics{figures/mlp/substream.pdf}
\end{figure}
We continue to read blocks until a block's \VAR{Last} bit is set.
Each block is prefixed by a \VAR{Parameters Present} bit.
If set, the block has an optional \VAR{Restart Header} block
(indicated by the prefixed \VAR{Header Present} bit)
and required \VAR{Decoding Parameters} block.
If \VAR{Parameters Present} is not set, we continue directly to
\VAR{Block Data} - using the most recently read set of parameters for
decoding.

Once all the blocks have been read, we align the stream to a
mulitple of 16-bits.
Then, if \VAR{Checkdata Present} has been indicated in the
\VAR{Substream Info} header, we read an 8-bit parity
and an 8-bit CRC value.

\subsection{Restart Header}
\begin{figure}[h]
\includegraphics{figures/mlp/restart_header.pdf}
\end{figure}
\par
\noindent
\VAR{Min Channel} and \VAR{Max Channel} indicate the substream's
range of channels.
For example, a 6 channel stream might have values of 0 and 1
for the first substream, and values of 2 and 5 for the second.
So, decoding $\text{substream}_1$ would generate $\text{sample}_{0~i}$
and $\text{sample}_{1~i}$, while decoding $\text{substream}_2$ would
generate $\text{sample}_{2~i}$ through $\text{sample}_{5~i}$
(where $i$ covers the range of total samples in the substreams' blocks).

There are \VAR{Max Matrix Channel} + 1 number of \VAR{Channel Assignment}
values, indicating which MLP channel the decoded output will be sent to
(offset from the substream's minimum channel).

\clearpage

\subsection{Decoding Parameters}

Reading the decoding parameters requires 8,
\VAR{Parameter Presence Flags} bits.
If the flag for a particular parameter is set,
\textit{and} the parameter's prefixed \VAR{Present}
bit is set, we go ahead and read that parameter.
Note that the first decoding parameter is the 8-bit
\VAR{Parameter Presence Flags} value itself.

\begin{figure}[h]
\includegraphics{figures/mlp/decoding_params.pdf}
\end{figure}
\par
\noindent
There is one optional \VAR{Channel Parameters} block per
channel from \VAR{Min Channel} to \VAR{Max Channel}.
Again, each is prefixed by a single \VAR{Present} bit.

For example, given the our 2 substream MLP file from before
(with \VAR{Min Channel} = 0, \VAR{Max Channel} = 1 in $\text{substream}_1$
and \VAR{Min Channel} = 2, \VAR{Max Channel} = 5 in $\text{substream}_2$),
the first substream has optional $\text{Channel Parameters}_0$
and $\text{Channel Parameters}_1$, while the second has
optional $\text{Channel Parameters}_2$ through $\text{Channel Parameters}_5$.

\subsubsection{Parameter Presence Flags}

\begin{figure}[h]
\includegraphics{figures/mlp/parameter_presence_flags.pdf}
\end{figure}
\par
\noindent
By default, all 8 flags are all set.
The \VAR{Huffman Offset}, \VAR{IIR Filter Parameters} and
\VAR{FIR Filter Parameters} flags are checked while
reading \VAR{Channel Parameters}.

\subsubsection{Block Size}

This is the size of the block in PCM frames.
Its value is 8 by default.
All channels within a block will have the same number of samples.

\clearpage

\subsubsection{Matrix Parameters}

%% If \VAR{Matrix Parameters} is indicated during parameter decoding:

\begin{figure}[h]
\includegraphics{figures/mlp/matrix_params.pdf}
\end{figure}
\par
\noindent
There are \VAR{Matrix Count} number of matrices.
There is one coefficient per \VAR{Max Matrix Channel} + 3.
The size of each signed matrix coefficient is \VAR{Fractional Bits} + 2
and its value is calculated as:
\begin{equation*}
\text{Matrix Coefficient}_i = \text{Signed Value}_i \times 2 ^ {14 - \text{Fractional Bits}}
\end{equation*}
If the preceding \VAR{Present} bit is \textit{not} set,
the value of $\text{Matrix Coefficient}_i$ is 0.
If \VAR{Matrix Parameters} is \textit{not} indicated during parameter decoding,
treat \VAR{Matrix Count} as 0 by default.

\subsubsection{Output Shifts}

%% If \VAR{Output Shifts} is indicated during parameter decoding:

\begin{figure}[h]
\includegraphics{figures/mlp/output_shifts.pdf}
\end{figure}
\par
\noindent
There are \VAR{Max Matrix Channel} + 1 number of shift values.
Each is a signed, 4-bit value.
\par
If \VAR{Output Shifts} is \textit{not} indicated during parameter
decoding, all $\text{Output Shift}_i$ values are 0 by default.


\subsubsection{Quant Step Sizes}

%% If \VAR{Quant Step Sizes} is indicated during parameter decoding:
\begin{figure}[h]
\includegraphics{figures/mlp/quant_steps.pdf}
\end{figure}
\par
\noindent
There is one \VAR{Quant Step Size} value per substream channel.
To continue our 2 substream example, if the \VAR{Present} bit is set,
the first substream has $\text{Quant Step Size}_0$ and
$\text{Quant Step Size}_1$,
while the second has $\text{Quant Step Size}_2$ through
$\text{Quant Step Size}_5$.

Each is an unsigned, 4-bit value.
These are analagous to FLAC's wasted bits-per-sample.
If \VAR{Quant Step Sizes} is \textit{not} indicated during parameter
decoding, all $\text{Quant Step Size}_i$ values are 0 by default.

\clearpage

\subsubsection{Channel Parameters}

\begin{figure}[h]
\includegraphics{figures/mlp/channel_parameters.pdf}
\end{figure}
\par
\noindent
Remember that the optional \VAR{FIR Filter Parameters},
\VAR{IIR Filter Parameters} and \VAR{Huffman Offset}
values are read only if the corresponding \VAR{Parameter Presence Flag}
is set.

\VAR{Codebook} and \VAR{Huffman Least-Significant Bits} are unsigned values.
\VAR{Huffman Offset} is a signed value.
\VAR{Signed Huffman Offset} is calculated as follows:
\begin{align*}
\intertext{If $\text{Codebook} > 0$:}
\text{LSB Bits} &= \text{Huffman Least-Significant Bits} - \text{Quant Step Size}_{\text{channel}} \\
\text{Sign Shift} &= \text{LSB Bits} + 2 - \text{Codebook} \\
\text{Signed Huffman Offset} &=
\begin{cases}
\text{Huffman Offset} - 7 \times 2 ^ {\text{LSB Bits}} - 2 ^ {\text{Sign Shift}} & \text{if Sign Shift} \geq 0 \\
\text{Huffman Offset} - 7 \times 2 ^ {\text{LSB Bits}} & \text{if Sign Shift} < 0
\end{cases}
\intertext{If $\text{Codebook} = 0$:}
\text{LSB Bits} &= \text{Huffman Least-Significant Bits} - \text{Quant Step Size}_{\text{channel}} \\
\text{Sign Shift} &= \text{LSB Bits} - 1 \\
\text{Signed Huffman Offset} &=
\begin{cases}
\text{Huffman Offset} - 2 ^ {\text{Sign Shift}} & \text{if Sign Shift} \geq 0 \\
\text{Huffman Offset} & \text{if Sign Shift} < 0
\end{cases}
\end{align*}
The \VAR{Signed Huffman Offset} will be added to each decoded residual
during block decoding.

By default, \VAR{Signed Huffman Offset} is $-2 ^ {23}$, \VAR{Codebook} is 0
and \VAR{Huffman Least-Significant Bits} is 23.

\clearpage

\subsubsection{FIR Filter Parameters}
\begin{figure}[h!]
\includegraphics{figures/mlp/filter_params_fir.pdf}
\end{figure}
\par
\noindent
If \VAR{Order} is 0, we forego reading the rest of the filter parameters.
Otherwise, \VAR{Shift}, \VAR{Coefficient Bits} and \VAR{Coefficient Shift}
are unsigned values - the latter two are used for reading
\VAR{Order} number of signed values
which we convert to filter coefficients as follows:
\begin{equation*}
\text{FIR Filter Coefficient}_i = \text{Signed Coeff}_i \times 2 ^ {\text{Coefficient Shift}}
\end{equation*}
We're primarily interested in the \VAR{Shift} and \VAR{FIR Filter Coefficient}
values, which will be used for filtering decoded residuals.

Note that the \VAR{Set State} value must be 0.  By default,
\VAR{Shift} is 0 and the signed coefficient list is empty.

\subsubsection{IIR Filter Parameters}
\begin{figure}[h]
\includegraphics{figures/mlp/filter_params_iir.pdf}
\end{figure}

As with FIR, if \VAR{Order} is 0, we forego reading the rest of the
filter parameters.
Otherwise, \VAR{Shift}, \VAR{Coefficient Bits} and \VAR{Coefficient Shift}
are unsigned values - the latter two are used for reading
\VAR{Order} number of signed coefficient values
which we convert to filter coefficients as follows:
\begin{equation*}
\text{IIR Filter Coefficient}_i = \text{Signed Coeff}_i \times 2 ^ {\text{Coefficient Shift}}
\end{equation*}
If \VAR{Set State} is 1, we also read \VAR{Order} number of signed state
values which we convert as follows:
\begin{equation*}
\text{State}_i = \text{Signed State}_i \times 2 ^ {\text{State Shift}}
\end{equation*}
We're primarily interested in the \VAR{Shift} and \VAR{IIR Filter Coefficient}
and \VAR{State} values which will be used for filtering decoded residuals.

By default, \VAR{Shift} is 0 and the signed coefficient list is empty.

\clearpage

\subsection{Block Data}

The block data of a given substream block is the residuals
from which our original samples will be reconstructed.
Residuals are interleaved between substream channels.
That is, given a 2 channel substream, $\text{Residual}_1$ is
on $\text{Channel}_1$, $\text{Residual}_2$ is on $\text{Channel}_2$,
$\text{Residual}_3$ is on $\text{Channel}_1$ and so forth.
Furthermore, for each \VAR{Matrix Parameter} in the substream,
if \VAR{LSB Bypass} is set in that parameter,
one must read a \VAR{Bypassed LSB} bit per PCM frame's worth of
residuals prior to reading the residuals themselves.

\begin{figure}[h]
\includegraphics{figures/mlp/residuals.pdf}
\end{figure}

Decoding a given residual value requires the
\VAR{Codebook}, \VAR{Huffman Least-Significant Bits} and
\VAR{Signed Huffman Offset} values from its \VAR{Channel Parameters},
as well as the \VAR{Quant Step Size} for that residual's channel.
First, given a positive \VAR{Codebook} value, we decode a Huffman value
from the bit stream as indiciated by the trees on page
\pageref{mlp_codebooks}.
This value is our \VAR{Most-Signicant Bits} (MSB).
If \VAR{Codebook} is 0, our \VAR{Most-Significant Bits} are also 0.

We then calculate our least-significant bit count as:
\begin{equation*}
\text{LSB Count}_c = \text{Huffman Least-Significant Bits}_c - \text{Quant Step Size}_c
\end{equation*}
We read this number of bits from the stream as
our \VAR{Least-Significant Bits} (LSB) value.
If $\text{LSB Count}_c$ is 0, we read no bits from the stream and our
\VAR{Least-Significant Bits} value is also 0.

Our final residual value is calculated as follows:
\begin{equation*}
\text{Residual}_i = ((\text{MSB}_i \times 2 ^ {\text{LSB Count}_i}) + \text{LSB}_i + \text{Signed Huffman Offset}_c) \times 2 ^ {\text{Quant Step Size}_c}
\end{equation*}

Note that if \VAR{Codebook} is 0 \textit{and} our $\text{LSB Count}_c$ is 0,
we read no bits from the stream and $\text{Residual}_i$ is 0.
This is how MLP stores large runs of 0 residuals.

\clearpage

\begin{figure}[h]
\label{mlp_codebooks}
\includegraphics{figures/mlp/codebook1.pdf}
\caption{Codebook 1}
\vskip 2em
\includegraphics{figures/mlp/codebook2.pdf}
\caption{Codebook 2}
\vskip 2em
\includegraphics{figures/mlp/codebook3.pdf}
\caption{Codebook 3}
\end{figure}

%% \subsubsection{Codebooks}
%% \begin{table}[h]
%% \begin{tabular}{|r|r||r|r||r|r|}
%% \hline
%% \multicolumn{2}{|c||}{Codebook 1} &
%% \multicolumn{2}{c||}{Codebook 2} &
%% \multicolumn{2}{c|}{Codebook 3} \\
%% Pattern & Value & Pattern & Value & Pattern & Value \\
%% \hline
%% \texttt{000000001} & 0 &
%% \texttt{000000001} & 0 &
%% \texttt{000000001} & 0 \\
%% \texttt{00000001} & 1 &
%% \texttt{00000001} & 1 &
%% \texttt{00000001} & 1 \\
%% \texttt{0000001} & 2 &
%% \texttt{0000001} & 2 &
%% \texttt{0000001} & 2 \\
%% \texttt{000001} & 3 &
%% \texttt{000001} & 3 &
%% \texttt{000001} & 3 \\
%% \texttt{00001} & 4 &
%% \texttt{00001} & 4 &
%% \texttt{00001} & 4 \\
%% \texttt{0001} & 5 &
%% \texttt{0001} & 5 &
%% \texttt{0001} & 5 \\
%% \texttt{001} & 6 &
%% \texttt{001} & 6 &
%% \texttt{001} & 6 \\
%% \texttt{100} & 7 &
%% \texttt{10} & 7 &
%% \texttt{1} & 7 \\
%% \texttt{101} & 8 &
%% \texttt{11} & 8 &
%% \texttt{011} & 8 \\
%% \texttt{110} & 9 &
%% \texttt{011} & 9 &
%% \texttt{0101} & 9 \\
%% \texttt{111} & 10 &
%% \texttt{0101} & 10 &
%% \texttt{01001} & 10 \\
%% \texttt{011} & 11 &
%% \texttt{01001} & 11 &
%% \texttt{010001} & 11 \\
%% \texttt{0101} & 12 &
%% \texttt{010001} & 12 &
%% \texttt{0100001} & 12 \\
%% \texttt{01001} & 13 &
%% \texttt{0100001} & 13 &
%% \texttt{01000001} & 13 \\
%% \texttt{010001} & 14 &
%% \texttt{01000001} & 14 &
%% \texttt{010000001} & 14 \\
%% \texttt{0100001} & 15 &
%% \texttt{010000001} & 15 &
%% & \\
%% \texttt{01000001} & 16 &
%% & & & \\
%% \texttt{010000001} & 17 &
%% & & & \\
%% \hline
%% \end{tabular}
%% \end{table}

\clearpage

\subsection{Channel Filtering}

Given a channel's decoded block residuals, we then filter those residuals
with the channel's FIR and IIR filters from its \VAR{Decoding Parameters}.
In particular, we'll need the \VAR{FIR Order}, \VAR{FIR Coefficients},
\VAR{IIR Order}, \VAR{IIR Coefficients}, \VAR{IIR State} and
\VAR{Shift}\footnote{If both both FIR filter and IIR filter
have a positive number of coefficients, their \VAR{Shift} values must be
identical}
parameters, along with the \VAR{Quant Step Size} for the filtered channel.

Note that the \VAR{Filtered} values below 0 are taken from the previously
filtered block.
\label{mlp_mask}
\begin{align*}
\intertext{Quant Step Size for a given channel is used to build a mask function which zeroes out least-significant bits:}
\text{mask(x)} &= \lfloor x \div 2 ^ {\text{Quant Step Size}_c}\rfloor \times 2 ^ {\text{Quant Step Size}_c} \\
\intertext{The filtered values are then calculated as follows:}
\text{FIR Sum}_i &= \overset{\text{FIR Order} - 1}{\underset{j = 0}{\sum}}{\text{FIR Coefficient}_j \times \text{Filtered}_{i - j - 1}} \\
\text{IIR Sum}_i &= \overset{\text{IIR Order} - 1}{\underset{k = 0}{\sum}}{\text{IIR Coefficient}_k \times \text{IIR State}_{i - k - 1 + \text{IIR Order}}} \\
\text{Shifted Sum}_i &= \left\lfloor\frac{\text{FIR Sum}_i + \text{IIR Sum}_i}{2 ^ {\text{Shift}}}\right\rfloor \\
\text{Filtered}_i &= \text{mask}(\text{Shifted Sum}_i + \text{Residual}_i) \\
\text{IIR State}_{i + \text{IIR Order}} &= \text{Filtered}_i - \text{Shifted Sum}_i
\intertext{However, if both \VAR{FIR Order} and \VAR{IIR Order} are 0, this can be reduced to:}
\text{Filtered}_i &= \text{mask}(\text{Residual}_i)
\intertext{Also note that if $\text{Quant Step Size}_c$ = 0:}
\text{mask}(x) &= \lfloor x \div 2 ^ 0\rfloor \times 2 ^ 0 = x \\
\text{IIR State}_{i + \text{IIR Order}} &= \text{mask}(\text{Shifted Sum}_i + \text{Residual}_i) - \text{Shifted Sum}_i \\
&= \text{Residual}_i
\end{align*}

\clearpage

As an example, given the values:
\begin{table}[h]
\begin{tabular}{l c r l c r l c r}
FIR Order & = & 4 &
IIR Order & = & 3 &
$\text{Filtered}_{-4}$ & = & -40 \\
$\text{FIR Coefficient}_0$ & = & 556 &
$\text{IIR Coefficient}_0$ & = & -122 &
$\text{Filtered}_{-3}$ & = & -39 \\
$\text{FIR Coefficient}_1$ & = & -493 &
$\text{IIR Coefficient}_1$ & = & 102 &
$\text{Filtered}_{-2}$ & = & 0 \\
$\text{FIR Coefficient}_2$ & = & 288 &
$\text{IIR Coefficient}_2$ & = & -192 &
$\text{Filtered}_{-1}$ & = & 18 \\
$\text{FIR Coefficient}_3$ & = & -96 &
$\text{IIR State}_0$ & = & 12 &
$\text{Residual}_0$ & = & 34 \\
$\text{Shift}$ & = & 8 &
$\text{IIR State}_1$ & = & 20 &
$\text{Residual}_1$ & = & 9 \\
$\text{Quant Step Size}_c$ & = & 0 &
$\text{IIR State}_2$ & = & -8 &
$\text{Residual}_2$ & = & -23 \\
\end{tabular}
\end{table}

Our filtered values are as follows:
\begin{align*}
\text{FIR Sum}_0 &= (556 \times 18) + (-493 \times 0) + (288 \times -39) + (-96 \times -40) = 2616 \\
\text{IIR Sum}_0 &= (-122 \times 12) + (102 \times 20) + (-192 \times -8) = 2112 \\
\text{Shifted Sum}_0 &= \left\lfloor\frac{2616 + 2112}{2 ^ 8}\right\rfloor = 18 \\
\text{Filtered}_0 &= \text{mask}(18 + 34) = \textbf{52} \\
\text{IIR State}_3 &= 52 - 18 = 34 \\
\text{FIR Sum}_1 &= (556 \times 52) + (-493 \times 18) + (288 \times 0) + (-96 \times -39) = 23782 \\
\text{IIR Sum}_1 &= (-122 \times 34) + (102 \times 12) + (-192 \times 20) = -6764 \\
\text{Shifted Sum}_1 &= \left\lfloor\frac{23782 - 6764}{2 ^ 8}\right\rfloor = 66 \\
\text{Filtered}_1 &= 66 + 9 = \textbf{75} \\
\text{IIR State}_4 &= 75 - 66 = 9 \\
\text{FIR Sum}_2 &= (556 \times 75) + (-493 \times 52) + (288 \times 18) + (-96 \times 0) = 21248 \\
\text{IIR Sum}_2 &= (-122 \times 9) + (102 \times 34) + (-192 \times 12) = 66 \\
\text{Shifted Sum}_2 &= \left\lfloor\frac{21248 + 66}{2 ^ 8}\right\rfloor = 83 \\
\text{Filtered}_2 &= 83 - 23 = \textbf{60} \\
\text{IIR State}_5 &= 60 - 83 = -23 \\
\end{align*}

\clearpage

\subsection{Channel Rematrixing}

Finally, given our filtered channel values and a set of matrix parameters,
we ``rematrix'' those values into the final PCM values.
This is a form of cross-channel decorrelation, roughly analagous
to FLAC's mid-side channel assignment.

Note that if a file has two substreams, we apply matrix parameters
depending on how many substreams are decoded.
That is, when decoding only the first substream, we only apply
the first substream's matrix parameters.
But when decoding both substreams, we apply the second substream's
matrix parameters.
This allows a two substream MLP file to have two different ``mixes''
from the same data.

\subsubsection{Noise Channels}

First, we generate two noise channels, each containing
\VAR{Block Size} number of noise samples, using the
\VAR{Noise Gen Seed} and \VAR{Noise Shift} (all taken from the
current \VAR{Restart Header}).
This requires a simple crop function which converts an integer
to a signed, 8-bit value:
\begin{equation*}
\text{crop}(x) =
\begin{cases}
x \bmod{2 ^ 8} & \text{if } \lfloor (x \bmod{2 ^ 8}) \div 2 ^ 7\rfloor = 0 \\
(x \bmod{2 ^ 7}) - 2 ^ 7 & \text{if } \lfloor (x \bmod{2 ^ 8}) \div 2 ^ 7\rfloor \neq 0
\end{cases}
\end{equation*}

\begin{align*}
\text{Shifted}_i &= \lfloor\text{Seed}_i \div 2 ^ 7\rfloor \bmod{2 ^ {16}} \\
\text{Noise}_{i~1} &= \text{crop}(\lfloor\text{Seed}_i \div 2 ^ {15}\rfloor) \times 2 ^ {\text{Noise Shift}} \\
\text{Noise}_{i~2} &= \text{crop}(\text{Shifted}_i) \times 2 ^ {\text{Noise Shift}} \\
\text{Seed}_{i + 1} &= ((\text{Seed}_i \times 2 ^ {16}) \bmod{2 ^ {32}}) \xor \text{Shifted}_i \xor (\text{Shifted}_i \times 2 ^ 5)
\end{align*}
For example, given a \VAR{Noise Gen Seed} of 7855724 and a
\VAR{Noise Shift} of 0, our first couple of noise samples are
as follows:
\begin{align*}
\text{Seed}_0 &= 7855724 \\
\text{Shifted}_0 &= \lfloor 7855724 \div 2 ^ 7\rfloor \bmod{2 ^ {16}} = 61372 \\
\text{Noise}_{0~1} &= \text{crop}(\lfloor 7855724 \div 2 ^ {15}\rfloor) \times 2 ^ 0 = \text{crop}(239) \times 1 = \textbf{-17} \\
\text{Noise}_{0~2} &= \text{crop}(61372) \times 2 ^ 0 = \textbf{-68} \\
\text{Seed}_1 &= ((7855724 \times 2 ^ {16}) \bmod{2 ^ {32}}) \xor 61372 \xor 61372 \times 2 ^ 5 \\
&= 3731619840 \xor 61372 \xor 1963904 = 3731953724 \\
\text{Shifted}_1 &= \lfloor 3731953724 \div 2 ^ 7 \rfloor \bmod{2 ^ {16}} = 57904 \\
\text{Noise}_{1~1} &= \text{crop}(\lfloor 3731953724 \div 2 ^ {15}\rfloor) \times 2 ^ 1 = \text{crop}(113890) \times 1 = \textbf{-30} \\
\text{Noise}_{1~2} &= \text{crop}(57904) \times 2 ^ 0 = \textbf{48} \\
\text{Seed}_2 &= ((3731953724 \times 2 ^ {16}) \bmod{2 ^ {32}}) \xor 57904 \xor 57904 \times 2 ^ 5 \\
&= 406585344 \xor 57904 \xor 1852928 = 404792368
\end{align*}

\clearpage

\subsubsection{Rematrixing}

Performing the actual rematrixing requires appending the two
noise channels to the end of each PCM frame of filtered residual data.
That is, for a 2 channel stream:
\par
\noindent
\begin{tabular}{r c r}
$\text{Matrix}_{i~1}$ & = & $\text{Filtered}_{i~1}$ \\
$\text{Matrix}_{i~2}$ & = & $\text{Filtered}_{i~2}$ \\
$\text{Matrix}_{i~3}$ & = & $\text{Noise}_{i~1}$ \\
$\text{Matrix}_{i~4}$ & = & $\text{Noise}_{i~2}$
\end{tabular}
\par
\noindent
The calculation for the matrix's output sample is then as follows:
\begin{align*}
\text{Shifted Sum}_{i~\text{Channel}} &= \left\lfloor\frac{\overset{\text{Channel Count + 2}}{\underset{j = 1}{\sum}}{\text{Matrix}_{i~j} \times \text{Matrix Coefficient}_j}}{2 ^ {14}}\right\rfloor \\
\text{Rematrixed}_{i~\text{Channel}} &= \text{mask}(\text{Shifted Sum}_{i~\text{Channel}}) + \text{Bypassed LSB}_i
\end{align*}
Note that the two noise channels are the reason we read
\VAR{Max Matrix Channel} + 3 number of matrix coefficients
instead of one coefficient per channel as one might expect.
Also note that the \VAR{Bypassed LSB} bit we read in the
\VAR{Block Data} for each PCM frame reappears at this point.
The mask function is the one used for channel filtering on page
\pageref{mlp_mask}.
So, given the following values:
\par
\noindent
\begin{tabular}{r c r r c r}
$\text{Filtered}_{0~1}$ & = & -54372 &
$\text{Matrix Coefficient}_1$ & = & -16384 \\
$\text{Filtered}_{0~2}$ & = & 169057 &
$\text{Matrix Coefficient}_2$ & = & 6144 \\
$\text{Noise}_{0~1}$ & = & -17 &
$\text{Matrix Coefficient}_3$ & = & 0 \\
$\text{Noise}_{0~2}$ & = & -68 &
$\text{Matrix Coefficient}_4$ & = & 0 \\
$\text{Filtered}_{1~1}$ & = & -92558 &
$\text{Bypassed LSB}_0$ & = & 0 \\
$\text{Filtered}_{1~2}$ & = & 118830 &
$\text{Bypassed LSB}_1$ & = & 0 \\
$\text{Noise}_{1~1}$ & = & -30 &
Channel Count & = & 2 \\
$\text{Noise}_{1~2}$ & = & -48 &
Out Channel & = & 0
\end{tabular}
\par
\noindent
Our new values for channel 1 (\VAR{Out Channel} + 1) are calculated as follows:
\begin{align*}
\text{Rematrixed}_{0~1} &= \left\lfloor\frac{(-54372 \times -16384) + (169057 \times 6144) + (-17 \times 0) + (-68 \times 0)}{2 ^ {14}}\right\rfloor + 0 \\
&= \left\lfloor\frac{1929517056}{16384}\right\rfloor = \textbf{117768} \\
\text{Rematrixed}_{1~1} &= \left\lfloor\frac{(-92558 \times -16384) + (118830 \times 6144) + (-30 \times 0) + (-48 \times 0)}{2 ^ {14}}\right\rfloor + 0 \\
&= \left\lfloor\frac{2246561792}{16384}\right\rfloor = \textbf{137119} \\
\end{align*}
The values for channel 2 remain unchanged as 169057 and 118830, respectively.
%% \par
%% Note that because the top 2 \VAR{Matrix Coefficient} values are 0,
%% the values from the 2 noise channels don't actually affect the
%% calculation whatsoever.
%% This is not an unusual case.

\subsection{Output Shifts}

If \VAR{Output Shifts} are indicated in the decoding parameters,
one applies them to the rematrixed samples on a per-channel
basis as follows:
\begin{equation*}
\text{Final Sample}_{i~\text{Channel}} = \text{Rematrixed}_{i~\text{Channel}} \times 2 ^ {\text{Output Shift}_\text{Channel}}
\end{equation*}
Naturally, when \VAR{Output Shift} is 0 for a given channel,
the rematrixed samples are our final samples.

Once the final block has been read (as indicated by its \VAR{Last} bit),
we align the stream to a multiple of 16 bits before checking
for an end of stream marker or optional parity/CRC-8 bytes.

\subsection{The End-of-Stream Marker}

The MLP optional end-of-stream marker is indicated by 4 bytes:

\begin{Verbatim}
D2 34 D2 34
\end{Verbatim}
\par
\noindent
which are found \textit{after} byte alignment
but \textit{prior} to the optional parity and CRC-8 bytes.
Not all MLP streams have an end-of-stream marker at the actual
end of the stream, nor is there any guarantee that
a false end-of-stream marker won't appear at the end of
block data.
Therefore, one should only check for an optional marker
once the stream's samples have been exhausted - as indicated
by the \VAR{Track PTS Length} field in the title table,
described on page \pageref{dvda_title_table}.

\clearpage

\subsection{Parity}

If \VAR{Checkdata Present} is indicated in the \VAR{Restart Header},
each substream is followed by a parity and CRC byte.
Calculating the parity value is a simple matter of performing
\xor on all the substream's bytes including the parity byte itself.
If successful, the result should be \texttt{0xA9}.
In mathematical terms, this calculation is as follows:
\begin{align*}
\texttt{0xA9} &= \text{Parity Byte} \xor \text{parity}(\text{Substream Size} - 2)
\intertext{Where the parity function is defined as:}
\text{parity}(x) &=
\begin{cases}
\text{Substream Byte}_0 & \text{if } x = 0 \\
\text{Substream Byte}_x \xor \text{parity}(x - 1) & \text{if } x > 0
\end{cases}
\end{align*}
\par
\noindent
For example, given the substream bytes
\texttt{0x95 0x00 0x20 0x00 0x1C 0xB6},
our parity byte is \texttt{0x1C} and the parity calculation is as follows:
\begin{align*}
\text{Parity} &= \texttt{0x1C} \xor \texttt{0x00} \xor \texttt{0x20} \xor \texttt{0x00} \xor \texttt{0x95} = \texttt{0xA9}
\end{align*}

\clearpage

\subsection{CRC-8}

Calculating the CRC-8 value is similar:
\begin{align*}
\text{CRC-8 Byte} &= \text{crc}(\text{Substream Size} - 2)
\intertext{Where the crc function is defined as:}
\text{crc}(x) &=
\begin{cases}
\text{Substream Byte}_x \xor \text{crc}(x - 1) & \text{if } x = \text{Substream Size} - 2 \\
\text{CRC8}(\text{Substream Byte}_x \xor \text{crc}(x - 1)) & \text{if } x > 0 \\
\text{CRC8}(\text{Substream Byte}_0 \xor \texttt{0x3C}) & \text{if } x = 0 \\
\end{cases}
\end{align*}
\par
\noindent
and CRC8 is taken from the following table:
\begin{table}[h]
{\relsize{-3}\ttfamily
\begin{tabular}{|r||r|r|r|r|r|r|r|r|r|r|r|r|r|r|r|r|r|}
\hline
 & 0x?0 & 0x?1 & 0x?2 & 0x?3 & 0x?4 & 0x?5 & 0x?6 & 0x?7 & 0x?8 & 0x?9 & 0x?A & 0x?B & 0x?C & 0x?D & 0x?E & 0x?F \\
\hline
0x0? & 0x00 & 0x63 & 0xC6 & 0xA5 & 0xEF & 0x8C & 0x29 & 0x4A & 0xBD & 0xDE & 0x7B & 0x18 & 0x52 & 0x31 & 0x94 & 0xF7 \\
0x1? & 0x19 & 0x7A & 0xDF & 0xBC & 0xF6 & 0x95 & 0x30 & 0x53 & 0xA4 & 0xC7 & 0x62 & 0x01 & 0x4B & 0x28 & 0x8D & 0xEE \\
0x2? & 0x32 & 0x51 & 0xF4 & 0x97 & 0xDD & 0xBE & 0x1B & 0x78 & 0x8F & 0xEC & 0x49 & 0x2A & 0x60 & 0x03 & 0xA6 & 0xC5 \\
0x3? & 0x2B & 0x48 & 0xED & 0x8E & 0xC4 & 0xA7 & 0x02 & 0x61 & 0x96 & 0xF5 & 0x50 & 0x33 & 0x79 & 0x1A & 0xBF & 0xDC \\
0x4? & 0x64 & 0x07 & 0xA2 & 0xC1 & 0x8B & 0xE8 & 0x4D & 0x2E & 0xD9 & 0xBA & 0x1F & 0x7C & 0x36 & 0x55 & 0xF0 & 0x93 \\
0x5? & 0x7D & 0x1E & 0xBB & 0xD8 & 0x92 & 0xF1 & 0x54 & 0x37 & 0xC0 & 0xA3 & 0x06 & 0x65 & 0x2F & 0x4C & 0xE9 & 0x8A \\
0x6? & 0x56 & 0x35 & 0x90 & 0xF3 & 0xB9 & 0xDA & 0x7F & 0x1C & 0xEB & 0x88 & 0x2D & 0x4E & 0x04 & 0x67 & 0xC2 & 0xA1 \\
0x7? & 0x4F & 0x2C & 0x89 & 0xEA & 0xA0 & 0xC3 & 0x66 & 0x05 & 0xF2 & 0x91 & 0x34 & 0x57 & 0x1D & 0x7E & 0xDB & 0xB8 \\
0x8? & 0xC8 & 0xAB & 0x0E & 0x6D & 0x27 & 0x44 & 0xE1 & 0x82 & 0x75 & 0x16 & 0xB3 & 0xD0 & 0x9A & 0xF9 & 0x5C & 0x3F \\
0x9? & 0xD1 & 0xB2 & 0x17 & 0x74 & 0x3E & 0x5D & 0xF8 & 0x9B & 0x6C & 0x0F & 0xAA & 0xC9 & 0x83 & 0xE0 & 0x45 & 0x26 \\
0xA? & 0xFA & 0x99 & 0x3C & 0x5F & 0x15 & 0x76 & 0xD3 & 0xB0 & 0x47 & 0x24 & 0x81 & 0xE2 & 0xA8 & 0xCB & 0x6E & 0x0D \\
0xB? & 0xE3 & 0x80 & 0x25 & 0x46 & 0x0C & 0x6F & 0xCA & 0xA9 & 0x5E & 0x3D & 0x98 & 0xFB & 0xB1 & 0xD2 & 0x77 & 0x14 \\
0xC? & 0xAC & 0xCF & 0x6A & 0x09 & 0x43 & 0x20 & 0x85 & 0xE6 & 0x11 & 0x72 & 0xD7 & 0xB4 & 0xFE & 0x9D & 0x38 & 0x5B \\
0xD? & 0xB5 & 0xD6 & 0x73 & 0x10 & 0x5A & 0x39 & 0x9C & 0xFF & 0x08 & 0x6B & 0xCE & 0xAD & 0xE7 & 0x84 & 0x21 & 0x42 \\
0xE? & 0x9E & 0xFD & 0x58 & 0x3B & 0x71 & 0x12 & 0xB7 & 0xD4 & 0x23 & 0x40 & 0xE5 & 0x86 & 0xCC & 0xAF & 0x0A & 0x69 \\
0xF? & 0x87 & 0xE4 & 0x41 & 0x22 & 0x68 & 0x0B & 0xAE & 0xCD & 0x3A & 0x59 & 0xFC & 0x9F & 0xD5 & 0xB6 & 0x13 & 0x70 \\
\hline
\end{tabular}
}
\end{table}

For instance, given the same substream bytes as before,
\texttt{0x95 0x00 0x20 0x00 0x1C 0xB6},
our CRC-8 calculation is as follows:
\begin{align*}
\text{CRC8}(\texttt{0x95} \xor \texttt{0x3C}) = \text{CRC8}(\texttt{0xA9}) &= \texttt{0x24} \\
\text{CRC8}(\texttt{0x00} \xor \texttt{0x24}) = \text{CRC8}(\texttt{0x24}) &= \texttt{0xDD} \\
\text{CRC8}(\texttt{0x20} \xor \texttt{0xDD}) = \text{CRC8}(\texttt{0xFD}) &= \texttt{0xB6} \\
\texttt{0xB6} \xor \texttt{0x00} &= \texttt{0xB6}
\end{align*}
which matches our CRC-8 byte at the end of the substream.

\clearpage
