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


\documentclass[letter]{scrbook}
\setlength{\pdfpagewidth}{\paperwidth}
\setlength{\pdfpageheight}{\paperheight}
\setlength{\textwidth}{6in}
\usepackage{amsmath}
\usepackage{graphicx}
\usepackage{picins}
\usepackage{fancyvrb}
\usepackage{relsize}
\usepackage{array}
\usepackage{wrapfig}
\usepackage{subfig}
\usepackage{multicol}
\usepackage{paralist}
\usepackage{textcomp}
\usepackage{fancyvrb}
\usepackage{multirow}
\usepackage{rotating}
\usepackage[toc,page]{appendix}
\usepackage{hyperref}
\usepackage{xtab}
\newcommand{\xor}{\textbf{ xor }}
%#1 = i
%#2 = byte
%#3 = previous checksum
%#4 = shift results
%#5 = new xor
%#6 = new CRC-16
\newcommand{\CRCSIXTEEN}[6]{\text{checksum}_{#1} &= \text{CRC16}(\texttt{#2}\xor(\texttt{#3} \gg \texttt{8}))\xor(\texttt{#3} \ll \texttt{8}) = \text{CRC16}(\texttt{#4})\xor \texttt{#5} = \texttt{#6}}
\newcommand{\LINK}[1]{\href{#1}{\texttt{#1}}}
\newcommand{\REFERENCE}[2]{\item #1 \\ \LINK{#2}}
\newcommand{\VAR}[1]{``{#1}''}
\newcommand{\ATOM}[1]{\texttt{#1}}
\newcommand{\SAMPLE}[0]{\text{Sample}}
\newcommand{\RESIDUAL}[0]{\text{Residual}}
\newcommand{\WARMUP}[0]{\text{Warm Up}}
\newcommand{\COEFF}[0]{\text{LPC Coefficient}}
\newcommand{\SCOEFF}[0]{C}
\newcommand{\SSAMPLE}[0]{S}
\title{MP3 Decoding}
\author{Brian Langenberger}
\begin{document}
\maketitle
\tableofcontents
\chapter{MP3 Decoding}
\section{MP3 Stream}
MP3 files are comprised of a set of physical MP3 frames.
Each physical frame contains a 32 bit frame header,
an optional 16 bit CRC, 136 or 256 bits of side data
(depending on if the stream is 1 or 2 channels)
and the remainder is main data.

\begin{figure}[h]
\includegraphics{figures/mp3/stream2.pdf}
\end{figure}

However, the main data block in a particular physical frame
does \textit{not} necessarily accompany the frame's header,
CRC and side data.
Instead, $\text{Main Data}_1$ may accompany
$\text{Header}_2$, $\text{CRC}_2$ and $\text{Side Data}_2$
to form a complete logical frame which can be decoded.
The reason for this piece of added complexity is to allow
frames with bits to spare to give those bits to subsequent frames
without affecting the MP3's overall bitrate.

\clearpage

\section{Header}
\begin{figure}[h]
\includegraphics{figures/mp3/frame_header.pdf}
\end{figure}

\begin{wrapfigure}[20]{r}{2in}
\begin{tabular}{|c|r|}
\hline
bits & Bitrate \\
\hline
\texttt{0000} & free \\
\texttt{0001} & 32000 \\
\texttt{0010} & 40000 \\
\texttt{0011} & 48000 \\
\texttt{0100} & 56000 \\
\texttt{0101} & 64000 \\
\texttt{0110} & 80000 \\
\texttt{0111} & 96000 \\
\texttt{1000} & 112000 \\
\texttt{1001} & 128000 \\
\texttt{1010} & 160000 \\
\texttt{1011} & 192000 \\
\texttt{1100} & 224000 \\
\texttt{1101} & 256000 \\
\texttt{1110} & 320000 \\
\texttt{1111} & invalid \\
\hline
\hline
bits & Sample Rate \\
\hline
\texttt{00} & 44100 \\
\texttt{01} & 48000 \\
\texttt{10} & 32000 \\
\texttt{11} & reserved \\
\hline
\hline
bits & Channels \\
\hline
\texttt{00} & Stereo \\
\texttt{01} & Joint stereo \\
\texttt{10} & Dual channel stereo\\
\texttt{11} & Mono \\
\hline
\end{tabular}
\end{wrapfigure}

An MP3's frame header contains little info needed for actual decoding;
instead, its main use is to determine the physical frame's full size
via its bitrate and sample rate fields.

\begin{align*}
\text{Frame Length (in bytes)} &= \frac{144 \times \text{Bitrate}}{\text{Sample Rate}} + \text{Pad}
\end{align*}
\par
\noindent
For example, an MP3 frame with a sample rate of 44100,
a bitrate of 128000 and a set pad bit is 418 bytes long, including the header.
\begin{equation*}
\frac{144 \times 128000}{44100} + 1 = 418
\end{equation*}

\section{CRC}

If the \VAR{Protection} bit is set in the frame header,
a 16-bit CRC value immediately follows the header.
Since this CRC only covers the header and side data
and takes away precious bits from the stream,
it is typically omitted by encoders.

\clearpage

\section{Side Data}

The size and layout of the side data depends on whether the stream
is 1 or 2 channels, which can be determined from the frame header.

\begin{figure}[h]
\includegraphics{figures/mp3/side_data_1ch.pdf}
\caption{Side Data for 1 Channel}
\vskip .25in
\includegraphics{figures/mp3/side_data_2ch.pdf}
\caption{Side Data for 2 Channels}
\end{figure}
\par
\noindent
\VAR{Copy ScaleFactor} bits indicate that we should copy
the scale factor values from the previous frame
rather than read a new value from the main data.

%% FIXME - Explain Main Data Offset
%% FIXME - Explain Granules

\begin{figure}[h]
\includegraphics{figures/mp3/granule.pdf}
\caption{Granule Info}
\end{figure}
\begin{description}
\item[Part 2 / Part 3] for parsing main data
\item[Big Values] for parsing Huffman codes in main data
\item[Global Gain] for decoding audio
\item[Scale Lengths] for parsing scales in main data
\item[Table 1 / Table 2 / Table 3] for parsing Huffman codes in main data
\item[Region A / Region B] for parsing Huffman codes in main data
\item[Block Type] for determining r0len / r1len / r2len - for Huffman codes
\item[Subgain 1 / Subgain 2 / Subgain 3] for decoding audio
\item[Pre Flag] for parsing scales in main data
\item[Scale Factor] for parsing scales in main data
\item[Count 1 Table] for parsing Huffman codes in main data
\end{description}

\begin{table}[h]
\begin{tabular}{|c|r|r|}
\hline
bits & $\text{length}_1$ & $\text{length}_2$ \\
\hline
\texttt{0000} & 0 & 0 \\
\texttt{0001} & 0 & 1 \\
\texttt{0010} & 0 & 2 \\
\texttt{0011} & 0 & 3 \\
\texttt{0100} & 3 & 0 \\
\texttt{0101} & 1 & 1 \\
\texttt{0110} & 1 & 2 \\
\texttt{0111} & 1 & 3 \\
\texttt{1000} & 2 & 1 \\
\texttt{1001} & 2 & 2 \\
\texttt{1010} & 2 & 3 \\
\texttt{1011} & 3 & 1 \\
\texttt{1100} & 3 & 2 \\
\texttt{1101} & 3 & 3 \\
\texttt{1110} & 4 & 2 \\
\texttt{1111} & 4 & 3 \\
\hline
\end{tabular}
\caption{the length of each scale factor, in bits}
\end{table}

\section{Main Data}

\begin{figure}[h]
\includegraphics{figures/mp3/main_data.pdf}
\end{figure}

\begin{figure}[h]
\includegraphics{figures/mp3/scalefactors.pdf}
\end{figure}

\clearpage

\subsection{Huffman Tables}

\begin{table}[h]
\begin{tabular}{|c|r|r|}
\hline
bits & Huffman table & additional bits \\
\hline
\texttt{00000} & \multicolumn{2}{c|}{invalid} \\
\texttt{00001} & \texttt{00} & 0 \\
\texttt{00010} & \texttt{01} & 0 \\
\texttt{00011} & \texttt{02} & 0 \\
\texttt{00100} & \multicolumn{2}{c|}{invalid} \\
\texttt{00101} & \texttt{03} & 0 \\
\texttt{00110} & \texttt{04} & 0 \\
\texttt{00111} & \texttt{05} & 0 \\
\texttt{01000} & \texttt{06} & 0 \\
\texttt{01001} & \texttt{07} & 0 \\
\texttt{01010} & \texttt{08} & 0 \\
\texttt{01011} & \texttt{09} & 0 \\
\texttt{01100} & \texttt{10} & 0 \\
\texttt{01101} & \texttt{11} & 0 \\
\texttt{01110} & \multicolumn{2}{c|}{invalid} \\
\texttt{01111} & \texttt{12} & 0 \\
\texttt{10000} & \texttt{13} & 1 \\
\texttt{10001} & \texttt{13} & 2 \\
\texttt{10010} & \texttt{13} & 3 \\
\texttt{10011} & \texttt{13} & 4 \\
\texttt{10100} & \texttt{13} & 6 \\
\texttt{10101} & \texttt{13} & 8 \\
\texttt{10110} & \texttt{13} & 10 \\
\texttt{10111} & \texttt{13} & 13 \\
\texttt{11000} & \texttt{14} & 4 \\
\texttt{11001} & \texttt{14} & 5 \\
\texttt{11010} & \texttt{14} & 6 \\
\texttt{11011} & \texttt{14} & 7 \\
\texttt{11100} & \texttt{14} & 8 \\
\texttt{11101} & \texttt{14} & 9 \\
\texttt{11110} & \texttt{14} & 11 \\
\texttt{11111} & \texttt{14} & 13 \\
\hline
\end{tabular}
\end{table}

\end{document}
