%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{ALAC Encoding}

To encode an ALAC file, we need a stream of PCM sample integers
along with that stream's sample rate, bits-per-sample and number of
channels.
We'll start by encoding all of the non-audio ALAC atoms,
most of which are contained within the \ATOM{moov} atom.
There's over twenty atoms in a typical ALAC file,
most of which are packed with seemingly redundant or
nonessential data,
so it will take awhile before we can move on to the actual
audio encoding process.

Remember, all of an ALAC's fields are big-endian.

\input{alac/encode/atoms}

\clearpage

\subsection{Encoding mdat Atom}
{\relsize{-2}
  \input{alac/algorithms/alac_encode}
}
\begin{figure}[h]
\includegraphics{alac/figures/stream.pdf}
\end{figure}

\clearpage

\subsection{Encoding Frameset}
\label{alac:encode_frameset}
{\relsize{-2}
  \input{alac/algorithms/encode_frameset}
}

%% \subsubsection{Channel Assignment}
%% \begin{tabular}{r|l}
%% channels & assignment \\
%% \hline
%% 1 & mono \\
%% 2 & left, right \\
%% 3 & center, left, right \\
%% 4 & center, left, right, center surround \\
%% 5 & center, left, right, left surround, right surround \\
%% 6 & center, left, right, left surround, right surround, LFE \\
%% 7 & center, left, right, left surround, right surround, center surround, LFE \\
%% 8 & center, left center, right center, left, right, left surround, right surround, LFE \\
%% \end{tabular}

\clearpage

\subsection{Encoding Frame}
\label{alac:encode_frame}
{\relsize{-1}
  \input{alac/algorithms/encode_frame}
}

\clearpage

\subsection{Encoding Uncompressed Frame}
\label{alac:write_uncompressed_frame}
{\relsize{-1}
  \input{alac/algorithms/encode_uncompressed_frame}
}

\begin{figure}[h]
  \includegraphics{alac/figures/uncompressed_frame.pdf}
\end{figure}

\clearpage

\subsection{Encoding Compressed Frame}
\label{alac:write_compressed_frame}
{\relsize{-2}
  \input{alac/algorithms/encode_compressed_frame}
}

\clearpage

\subsection{Encoding Non-Interlaced Frame}
\label{alac:write_non_interlaced_frame}
{\relsize{-1}
  \input{alac/algorithms/encode_noninterlaced_frame}
}
\begin{figure}[h]
  \includegraphics{alac/figures/noninterlaced_frame.pdf}
\end{figure}

\clearpage

\subsection{Writing Subframe Header}
\label{alac:write_subframe_header}
\input{alac/algorithms/write_subframe_header}
\begin{figure}[h]
\includegraphics{alac/figures/subframe_header.pdf}
\end{figure}
\par
\noindent
For example, given the QLP coefficients
\texttt{1170, -1088, 565, -161},
the subframe header is written as:
\begin{figure}[h]
\includegraphics{alac/figures/subframe-build.pdf}
\end{figure}

\clearpage

\subsection{Encoding Interlaced Frame}
\label{alac:write_interlaced_frame}
{\relsize{-1}
  \input{alac/algorithms/encode_interlaced_frame}
}

\clearpage

\begin{figure}[h]
  \includegraphics{alac/figures/interlaced_frame.pdf}
\end{figure}

\subsubsection{Correlating Channels}
\label{alac:correlate_channels}
{\relsize{-1}
  \input{alac/algorithms/correlate_channels}
\par
\noindent
For example, given an \VAR{interlacing shift} value of 2 and an
\VAR{interlacing leftweight} value of 3:
\par
\noindent
{\relsize{-1}
\begin{tabular}{r||r|r||>{$}r<{$}|>{$}r<{$}|}
$i$ & $\textsf{channel}_{0~i}$ & $\textsf{channel}_{1~i}$ & \textsf{correlated}_{0~i} & \textsf{correlated}_{1~i} \\
\hline
0 & 18 & 2 & 2 + \lfloor((18 - 2) \times 3) \div 2 ^ 2\rfloor = 14 & 18 - 2 = 16 \\
1 & 20 & 3 & 3 + \lfloor((20 - 3) \times 3) \div 2 ^ 2\rfloor = 15 & 20 - 3 = 17 \\
2 & 26 & 0 & 0 + \lfloor((26 - 0) \times 3) \div 2 ^ 2\rfloor = 19 & 26 - 0 = 26 \\
3 & 24 & -1 & -1 + \lfloor((24 + 1) \times 3) \div 2 ^ 2\rfloor = 17 & 24 + 1 = 25 \\
4 & 24 & 0 & 0 + \lfloor((24 - 0) \times 3) \div 2 ^ 2\rfloor = 18 & 24 - 0 = 24 \\
\end{tabular}
}
}

\clearpage

\input{alac/encode/lpc}

\clearpage

\input{alac/encode/residual}

