%Copyright (C) 2007-2014  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{FLAC Encoding}
{\relsize{-1}
\input{flac/algorithms/encode_flac}
}
\begin{figure}[h]
\includegraphics{flac/figures/stream.pdf}
\end{figure}
\par
\noindent
All of the fields in the FLAC stream are big-endian.

\clearpage

\subsection{Writing Metadata Blocks}
\label{flac:write_placeholder_blocks}
{\relsize{-1}
\input{flac/algorithms/write_metadata}
}
\par
\noindent
PADDING can be some size other than 4096 bytes.
One simply wants to leave enough room for a VORBIS\_COMMENT block,
SEEKTABLE and so forth.
Other fields such as the minimum/maximum frame size
and the stream's final MD5 sum can't be known in advance;
we'll need to return to this block once encoding is finished
in order to populate them.
\begin{figure}[h]
\includegraphics{flac/figures/metadata.pdf}
\end{figure}


\clearpage

\subsection{Updating Stream MD5 Sum}
\label{flac:update_md5_w}
\ALGORITHM{the frame's signed PCM input samples\footnote{$channel_{c~i}$ indicates the $i$th sample in channel $c$}}{the stream's updated MD5 sum}
\SetKwData{BLOCKSIZE}{block size}
\SetKwData{CHANNELCOUNT}{channel count}
\SetKwData{CHANNEL}{channel}
\For{$i \leftarrow 0$ \emph{\KwTo}\BLOCKSIZE}{
  \For{$c \leftarrow 0$ \emph{\KwTo}\CHANNELCOUNT}{
    bytes $\leftarrow \text{\CHANNEL}_{c~i}$ as signed, little-endian bytes\;
    update stream's MD5 sum with bytes\;
  }
}
\Return stream's MD5 sum\;
\EALGORITHM
\par
\noindent
For example, given a 16 bits per sample stream with the signed sample values:
\begin{table}[h]
\begin{tabular}{r|rr}
$i$ & $\textsf{channel}_0$ & $\textsf{channel}_1$ \\
\hline
0 & 1 & -1 \\
1 & 2 & -2 \\
2 & 3 & -3 \\
\end{tabular}
\end{table}
\par
\noindent
are translated to the bytes:
\begin{table}[h]
\begin{tabular}{r|rr}
$i$ & $\textsf{channel}_0$ & $\textsf{channel}_1$ \\
\hline
0 & \texttt{01 00} & \texttt{FF FF} \\
1 & \texttt{02 00} & \texttt{FE FF} \\
2 & \texttt{03 00} & \texttt{FD FF} \\
\end{tabular}
\end{table}
\par
\noindent
and combined as:
\vskip .15in
\par
\noindent
\texttt{01 00 FF FF 02 00 FE FF 03 00 FD FF}
\vskip .15in
\par
\noindent
whose MD5 sum is:
\vskip .15in
\par
\noindent
\texttt{E7482f6462B27EE04EADC079291C79E9}
\vskip .25in
\par
This process is identical to the MD5 sum calculation performed
during FLAC decoding, but performed in the opposite order.

\clearpage

\subsection{Encoding a FLAC Frame}
\label{flac:encode_frame}
{\relsize{-3}
  \input{flac/algorithms/encode_frame}
}

\clearpage

\subsection{Writing Frame Header}
\label{flac:write_frame_header}
{\relsize{-1}
  \input{flac/algorithms/write_frame_header}
}
\begin{figure}[h]
\includegraphics{flac/figures/frames.pdf}
\end{figure}

\clearpage

\subsubsection{Encoding Block Size}
\label{flac:encode_block_size}
{\relsize{-2}
  \input{flac/algorithms/encode_block_size}
}

\subsubsection{Encoding Sample Rate}
\label{flac:encode_sample_rate}
{\relsize{-2}
  \input{flac/algorithms/encode_sample_rate}
}

\clearpage

\subsubsection{Encoding Bits Per Sample}
\label{flac:encode_bits_per_sample}
{\relsize{-2}
  \input{flac/algorithms/encode_bps}
}

\subsubsection{Encoding UTF-8 Frame Number}
\label{flac:write_utf8}
{\relsize{-2}
  \input{flac/algorithms/write_utf8}
}
\begin{wrapfigure}[10]{r}{2.375in}
\includegraphics{flac/figures/utf8.pdf}
\end{wrapfigure}
{\relsize{-2}
  For example, encoding the frame number 4228 in UTF-8:
\begin{align*}
\textsf{continuation bytes} &\leftarrow 2 \\
(2 + 1) &\rightarrow \textbf{write unary}~\text{with stop bit 1} \\
\left\lfloor\frac{4228}{2 ^ {2 \times 6}}\right\rfloor = 1 &\rightarrow \textbf{write}~(6 - 2)~\text{unsigned bits} \\
i &\leftarrow 1 \\
2 &\rightarrow \textbf{write}~\text{2 unsigned bits} \\
\left\lfloor\frac{4228}{2 ^ {1 \times 6}}\right\rfloor \bmod 64 = 2 &\rightarrow \textbf{write}~\text{6 unsigned bits} \\
i &\leftarrow 0 \\
2 &\rightarrow \textbf{write}~\text{2 unsigned bits} \\
\left\lfloor\frac{4228}{2 ^ {0 \times 6}}\right\rfloor \bmod 64 = 4 &\rightarrow \textbf{write}~\text{6 unsigned bits} \\
\end{align*}
}

\clearpage

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

\subsubsection{Frame Header Encoding Example}
Given a frame header with the following attributes:
\begin{table}[h]
\begin{tabular}{rl}
block size : & 4096 PCM frames \\
sample rate : & 44100 Hz \\
channel assignment : & 1 (2 channels stored independently) \\
bits per sample : & 16 \\
frame number : & 0
\end{tabular}
\end{table}
\par
\noindent
we generate the following frame header bytes:
\begin{figure}[h]
\includegraphics{flac/figures/header-example.pdf}
\end{figure}
\par
\noindent
Note how the CRC-8 is calculated from the preceding 5 header bytes:
\begin{align*}
\text{checksum}_0 = \text{CRC8}(\texttt{FF}\xor\texttt{00}) = \texttt{F3} & &
\text{checksum}_3 = \text{CRC8}(\texttt{18}\xor\texttt{E6}) = \texttt{F4} \\
\text{checksum}_1 = \text{CRC8}(\texttt{F8}\xor\texttt{F3}) = \texttt{31} & &
\text{checksum}_4 = \text{CRC8}(\texttt{00}\xor\texttt{F4}) = \texttt{C2} \\
\text{checksum}_2 = \text{CRC8}(\texttt{C9}\xor\texttt{31}) = \texttt{E6} \\
\end{align*}

\clearpage

\subsection{Encoding a FLAC Subframe}
\label{flac:encode_subframe}
{\relsize{-2}
  \input{flac/algorithms/encode_subframe}
}

\clearpage

\subsubsection{Calculating Wasted Bits Per Sample}
\label{flac:calculate_wasted_bps}
{\relsize{-1}
  \input{flac/algorithms/calculate_wastedbps}
  where the \texttt{wasted} function is defined as:
\begin{equation*}
  \texttt{wasted}(x) =
  \begin{cases}
    \infty & \text{if } x = 0 \\
    0 & \text{if } x \bmod 2 = 1 \\
    1 + \texttt{wasted}(x \div 2) & \text{if } x \bmod 2 = 0 \\
  \end{cases}
\end{equation*}
}

\subsubsection{Writing Subframe Header}
\label{flac:write_subframe_header}
{\relsize{-1}
  \input{flac/algorithms/write_subframe_header}
}
\begin{figure}[h]
  \includegraphics{flac/figures/subframes.pdf}
\end{figure}
\clearpage

\input{flac/encode/fixed}

\clearpage

\input{flac/encode/residual}

\clearpage

\input{flac/encode/lpc}

\clearpage

\subsection{Calculating Frame CRC-16}
\label{flac:calculate_crc16}
CRC-16 is used to checksum the entire FLAC frame, including the header
and any padding bits after the final subframe.
Given a byte of input and the previous CRC-16 checksum,
or 0 as an initial value, the current checksum can be calculated as follows:
\begin{equation}
\text{checksum}_i = \texttt{CRC16}(byte\xor(\text{checksum}_{i - 1} \gg 8 ))\xor(\text{checksum}_{i - 1} \ll 8)
\end{equation}
\par
\noindent
and the checksum is always truncated to 16-bits.
\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|}
\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? & 0000 & 8005 & 800f & 000a & 801b & 001e & 0014 & 8011 & 8033 & 0036 & 003c & 8039 & 0028 & 802d & 8027 & 0022 \\
0x1? & 8063 & 0066 & 006c & 8069 & 0078 & 807d & 8077 & 0072 & 0050 & 8055 & 805f & 005a & 804b & 004e & 0044 & 8041 \\
0x2? & 80c3 & 00c6 & 00cc & 80c9 & 00d8 & 80dd & 80d7 & 00d2 & 00f0 & 80f5 & 80ff & 00fa & 80eb & 00ee & 00e4 & 80e1 \\
0x3? & 00a0 & 80a5 & 80af & 00aa & 80bb & 00be & 00b4 & 80b1 & 8093 & 0096 & 009c & 8099 & 0088 & 808d & 8087 & 0082 \\
0x4? & 8183 & 0186 & 018c & 8189 & 0198 & 819d & 8197 & 0192 & 01b0 & 81b5 & 81bf & 01ba & 81ab & 01ae & 01a4 & 81a1 \\
0x5? & 01e0 & 81e5 & 81ef & 01ea & 81fb & 01fe & 01f4 & 81f1 & 81d3 & 01d6 & 01dc & 81d9 & 01c8 & 81cd & 81c7 & 01c2 \\
0x6? & 0140 & 8145 & 814f & 014a & 815b & 015e & 0154 & 8151 & 8173 & 0176 & 017c & 8179 & 0168 & 816d & 8167 & 0162 \\
0x7? & 8123 & 0126 & 012c & 8129 & 0138 & 813d & 8137 & 0132 & 0110 & 8115 & 811f & 011a & 810b & 010e & 0104 & 8101 \\
0x8? & 8303 & 0306 & 030c & 8309 & 0318 & 831d & 8317 & 0312 & 0330 & 8335 & 833f & 033a & 832b & 032e & 0324 & 8321 \\
0x9? & 0360 & 8365 & 836f & 036a & 837b & 037e & 0374 & 8371 & 8353 & 0356 & 035c & 8359 & 0348 & 834d & 8347 & 0342 \\
0xA? & 03c0 & 83c5 & 83cf & 03ca & 83db & 03de & 03d4 & 83d1 & 83f3 & 03f6 & 03fc & 83f9 & 03e8 & 83ed & 83e7 & 03e2 \\
0xB? & 83a3 & 03a6 & 03ac & 83a9 & 03b8 & 83bd & 83b7 & 03b2 & 0390 & 8395 & 839f & 039a & 838b & 038e & 0384 & 8381 \\
0xC? & 0280 & 8285 & 828f & 028a & 829b & 029e & 0294 & 8291 & 82b3 & 02b6 & 02bc & 82b9 & 02a8 & 82ad & 82a7 & 02a2 \\
0xD? & 82e3 & 02e6 & 02ec & 82e9 & 02f8 & 82fd & 82f7 & 02f2 & 02d0 & 82d5 & 82df & 02da & 82cb & 02ce & 02c4 & 82c1 \\
0xE? & 8243 & 0246 & 024c & 8249 & 0258 & 825d & 8257 & 0252 & 0270 & 8275 & 827f & 027a & 826b & 026e & 0264 & 8261 \\
0xF? & 0220 & 8225 & 822f & 022a & 823b & 023e & 0234 & 8231 & 8213 & 0216 & 021c & 8219 & 0208 & 820d & 8207 & 0202 \\
\hline
\end{tabular}
}
\end{table}
\par
\noindent
For example, given the frame bytes:
\texttt{FF F8 CC 1C 00 C0 EB 00 00 00 00 00 00 00 00},
the frame's CRC-16 can be calculated:
{\relsize{-2}
\begin{align*}
\CRCSIXTEEN{0}{0xFF}{0x0000}{0xFF}{0x0000}{0x0202} \\
\CRCSIXTEEN{1}{0xF8}{0x0202}{0xFA}{0x0200}{0x001C} \\
\CRCSIXTEEN{2}{0xCC}{0x001C}{0xCC}{0x1C00}{0x1EA8} \\
\CRCSIXTEEN{3}{0x1C}{0x1EA8}{0x02}{0xA800}{0x280F} \\
\CRCSIXTEEN{4}{0x00}{0x280F}{0x28}{0x0F00}{0x0FF0} \\
\CRCSIXTEEN{5}{0xC0}{0x0FF0}{0xCF}{0xF000}{0xF2A2} \\
\CRCSIXTEEN{6}{0xEB}{0xF2A2}{0x19}{0xA200}{0x2255} \\
\CRCSIXTEEN{7}{0x00}{0x2255}{0x22}{0x5500}{0x55CC} \\
\CRCSIXTEEN{8}{0x00}{0x55CC}{0x55}{0xCC00}{0xCDFE} \\
\CRCSIXTEEN{9}{0x00}{0xCDFE}{0xCD}{0xFE00}{0x7CAD} \\
\CRCSIXTEEN{10}{0x00}{0x7CAD}{0x7C}{0xAD00}{0x2C0B} \\
\CRCSIXTEEN{11}{0x00}{0x2C0B}{0x2C}{0x0B00}{0x8BEB} \\
\CRCSIXTEEN{12}{0x00}{0x8BEB}{0x8B}{0xEB00}{0xE83A} \\
\CRCSIXTEEN{13}{0x00}{0xE83A}{0xE8}{0x3A00}{0x3870} \\
\CRCSIXTEEN{14}{0x00}{0x3870}{0x38}{0x7000}{0xF093} \\
\intertext{Thus, the next two bytes after the final subframe should be
\texttt{0xF0} and \texttt{0x93}.
Again, when the checksum bytes are run through the checksumming procedure:}
\CRCSIXTEEN{15}{0xF0}{0xF093}{0x00}{0x9300}{0x9300} \\
\CRCSIXTEEN{16}{0x93}{0x9300}{0x00}{0x0000}{0x0000}
\end{align*}
the result will also always be 0, just as in the CRC-8.
}
