%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{DVD-Audio}
DVD-Audio is a format for delivering hi-fidelity, multichannel
audio on DVD media.
A DVD-Audio's \texttt{AUDIO\_TS} directory contains the
relevent data needed for decoding, spread into a lot of files
whose names are more than a little cryptic at first glance.

Unlike CD audio, which is simply a set of 1 to 99 identically-formatted
audio tracks (in terms of channel count, sample rate and bits per sample),
a DVD-Audio disc contains one or more titlesets.
Each titleset contains one or more titles, and each
title contains one or more tracks.
\begin{figure}[h]
\includegraphics{figures/dvda/layout.pdf}
\end{figure}
\par
\noindent
Typically, a DVD-Audio disc will contain two titlesets,
one for audio and the other for video - which we can ignore.
The first titleset will often contain two titles,
one for 2 channel audio and the other for 5.1 channel audio.
Each title will usually contain a consistent number of tracks
as MLP or PCM encoded audio.
\par
With this in mind, we can now make some sense of
the \texttt{AUDIO\_TS} directory's contents:
\vskip .25in
$\texttt{\huge{AUDIO\_TS.IFO}}$
\hfill
information about the disc, including the number of titlesets
\vskip .25in
$\texttt{\huge{ATS\_}}\underbrace{\texttt{\huge{01}}}_{Titleset}\texttt{\huge{\_0.IFO}}$
\hfill
information about all the titles in a given titleset
\vskip .25in
$\texttt{\huge{ATS\_}}\underbrace{\texttt{\huge{01}}}_{Titleset}\texttt{\huge{\_}}\underbrace{\texttt{\huge{1}}}_{AOB~\#}\texttt{\huge{.AOB}}$
\hfill
audio data for one or more tracks in a given titleset
\vskip .25in
\par
All are binary files containing one or more, 2048 byte sectors.

\clearpage

\section{AUDIO\_TS.IFO}
Known as the ``Audio Manager'' or ``AMG'',
this is primarily a container of pointers to
other files on disc.
However, for our purposes, we're only interested
in the \VAR{Audio Titleset Count} value.
\begin{figure}[h]
\includegraphics{figures/dvda/audio_ts_ifo.pdf}
\end{figure}
\section{ATS\_XX\_0.IFO}
Known as the ``Audio Titleset Information'' or ``ATSI'',
there is one of these files per \VAR{Audio Titleset Count}.
Each is typically 4096 bytes long (2 sectors).
\begin{figure}[h]
\includegraphics{figures/dvda/ats_xx_0.pdf}
\end{figure}
\par
\noindent
\VAR{Title Table Offset} is the address of the title's
information table, from the start of the second sector.
For example, given the \VAR{Title Table Offset} value of
\texttt{0x18}, we seek to address \texttt{0x818}
($\texttt{0x800} + \texttt{0x18} = \texttt{0x818}$)
to read the title's information.

\clearpage

\subsection{Title Table}
\label{dvda_title_table}
For each title in the titleset, there is a title table.

\begin{figure}[h]
\includegraphics{figures/dvda/ats_title.pdf}
\end{figure}
\par
\noindent
\VAR{Track Count} is the total number of tracks in the title.
\VAR{Title PTS Length} is the total length of the title in
PTS ticks.
There are 90000 PTS ticks per second.
\VAR{Sector Pointers Offset} is the offset value of the
sector pointers table, relative to the start of the title table.
For example, if our \VAR{Title Table Offset} value is
\texttt{0x18} and the \VAR{Sector Pointers Offset} value is
\texttt{0x1F0}, we seek to address \texttt{0xA08}
to reach the sector pointers table
($\texttt{0x818} + \texttt{0x1F0} = \texttt{0xA08}$).
\par
There is one timestamp value per track.
\VAR{Initial PTS Index} and \VAR{Track PTS Length} values
are the offset and length of the track in PTS ticks, respectively.
\VAR{Index Number} indicates the track's initial
sector pointers values from the following table.

\subsection{Sector Pointers Table}

For each title in the titleset, there is a sector pointers table
containing \VAR{Index Count} number of 96 bit values.

\begin{figure}[h]
\includegraphics{figures/dvda/ats_sectors.pdf}
\end{figure}
\par
\noindent
There is at least one sector pointers value per track.
Each pointer is relative to the start of the titleset's first
AOB file.

For example, if $\text{Track}_1$ has an \VAR{Index Number} of 1 and
$\text{Track}_2$ has an \VAR{Index Number} of 5,
that means $\text{Track}_1$ contains the sector pointers
$\text{Index}_1$, $\text{Index}_2$, $\text{Index}_3$ and $\text{Index}_4$.
However, much of the time \VAR{Track Count} will equal \VAR{Index Count}.
In this case there will be only a single \VAR{Index} value per track.

\clearpage

\section{ATS\_XX\_X.AOB}

All of a titleset's AOB files can be considered part of a
single, contiguous collection of sectors, each 2048 bytes long.
Thus, it's possible for the start and end sectors for a given track
(as indicated in the sector pointers table) to span two or more
AOB files.
Each sector contains one or more packets as part of a
``Packetized Elementary Stream''.

\begin{figure}[h]
\includegraphics{figures/dvda/ats_xx_x.pdf}
\end{figure}
\par
\noindent
Each sector within an AOB file is prefixed by a \VAR{Pack Header},
as follows:
\begin{figure}[h]
\includegraphics{figures/dvda/aob_pack_header.pdf}
\end{figure}
\par
\noindent
The three \VAR{Current PTS} values (3 bits, 15 bits and 15 bits, respectively)
combine to indicate the current position within the stream, in PTS ticks.
\par
Each sector then contains one or more packets.
The packets with a \VAR{Stream ID} of \texttt{0xBD} contain
encoded audio data.

\clearpage

\subsection{Audio Packet Data}

Packets containining audio data have yet another header
prior to the actual data.
This header indicates the stream type (\texttt{0xA0} for raw PCM,
\texttt{0xA1} for MLP).
In the case of PCM, this header also contains details
about the stream itself such as sample rate, bits per sample
and channel assignment.

\subsubsection{PCM Header}

\begin{figure}[h]
\includegraphics{figures/dvda/aob_pcm_payload.pdf}
\end{figure}

\begin{table}[h]
\subfloat{
\begin{tabular}{|c|r|}
\hline
Value & Bits per Sample \\
\hline
\texttt{0000} & 16 \\
\texttt{0001} & 20 \\
\texttt{0010} & 24 \\
\texttt{1111} & Group Unused \\
\hline
\hline
Value & Sample Rate \\
\hline
\texttt{0000} & 48000Hz \\
\texttt{0001} & 96000Hz \\
\texttt{0010} & 192000Hz \\
\texttt{1000} & 44100Hz \\
\texttt{1001} & 88200Hz \\
\texttt{1010} & 176400Hz \\
\texttt{1111} & Group Unused \\
\hline
\end{tabular}
}
\subfloat{
{\relsize{-2}
\begin{tabular}{|c|l|}
\hline
Value & Channel Assignment \\
\hline
\texttt{00000} & front center \\
\texttt{00001} & front left, front right\\
\texttt{00010} & front left, front right, back center \\
\texttt{00011} & front left, front right, back left, back right\\
\texttt{00100} & front left, front right, LFE \\
\texttt{00101} & front left, front right, LFE, back center \\
\texttt{00110} & front left, front right, LFE, back left, back right \\
\texttt{00111} & front left, front right, front center \\
\texttt{01000} & front left, front right, front center, back center \\
\texttt{01001} & front left, front right, front center, back left, back right \\
\texttt{01010} & front left, front right, front center, LFE\\
\texttt{01011} & front left, front right, front center, LFE, back center \\
\texttt{01100} & front left, front right, front center, LFE back left, back right \\
\texttt{01101} & front left, front right, front center, back center \\
\texttt{01110} & front left, front right, front center, back left, back right \\
\texttt{01111} & front left, front right, front center, LFE \\
\texttt{10000} & front left, front right, front center, LFE, back center \\
\texttt{10001} & front left, front right, front center, LFE, back left, back right \\
\texttt{10010} & front left, front right, back left, back right, LFE \\
\texttt{10011} & front left, front right, back left, back right, front center \\
\texttt{10100} & front left, front right, back left, back right, front center, LFE \\
\hline
\end{tabular}
}
}
\end{table}
\par
\noindent

Decoding the PCM data is explained on page \pageref{aob_pcm}.

\clearpage

\subsubsection{MLP Header}

\begin{figure}[h]
\includegraphics{figures/dvda/aob_mlp_payload.pdf}
\end{figure}
\par
\noindent
Since the MLP stream contains details such as bits per sample
and channel assignment, that information is not repeated in the MLP
packet header.

Decoding the MLP data is explained on page \pageref{aob_mlp}.

\clearpage

\section{PCM Decoding}
\label{aob_pcm}

For 16 bits-per-sample streams, PCM data is simply stored in
signed, big-endian format.

However, for 24 bits-per-sample streams, the individual bytes must
be reshuffled into a signed, little-endian depending on
how many channels the stream has.
This reshuffling occurs across \textit{two} PCM frames.
A 1 channel stream is reshuffled as follows:
\begin{figure}[h]
\includegraphics{figures/aobpcm/24bps_1ch.pdf}
\caption{1 channel, 24 bits-per-sample}
\end{figure}
\par
\noindent
For example, given the packet data bytes:
\begin{Verbatim}
EE BB CC DD FF AA
\end{Verbatim}
\par
\noindent
we first reshuffle them to:
\begin{Verbatim}
CC BB AA FF EE DD
\end{Verbatim}
We then transform those bytes into 2 PCM frames worth of 24-bit,
little-endian samples:
\begin{align*}
\text{Frame}_1 &= \texttt{0xAABBCC} = 2800588 - 2 ^ {23} = -5588020 \\
\text{Frame}_2 &= \texttt{0xDDEEFF} = 6156031 - 2 ^ {23} = -2232577
\end{align*}

Streams with more than one channel are handled similarly.

\clearpage

\begin{figure}[h!]
\includegraphics{figures/aobpcm/24bps_2ch.pdf}
\caption{2 channels, 24 bits-per-sample}
\end{figure}
\par
\noindent
For example, given the packet data bytes:
\begin{Verbatim}
66 11 22 BB 55 00 AA 33 44 99 77 88
\end{Verbatim}
\par
\noindent
we first reshuffle them to:
\begin{Verbatim}
22 11 00 55 44 33 88 77 66 BB AA 99
\end{Verbatim}
\par
\noindent
Then, we transform those bytes into 2 PCM frames worth of 24-bit,
little-endian samples:
\begin{align*}
\text{Frame}_{1~1} &= \texttt{0x001122} = 4386 \\
\text{Frame}_{1~2} &= \texttt{0x334455} = 3359829 \\
\text{Frame}_{2~1} &= \texttt{0x667788} = 6715272 \\
\text{Frame}_{2~2} &= \texttt{0x99AABB} = 1682107 - 2 ^ {23} = -6706501
\end{align*}

\clearpage

\begin{figure}[h!]
\includegraphics{figures/aobpcm/24bps_3ch.pdf}
\caption{3 channels, 24 bits-per-sample}
\end{figure}

\begin{figure}[h!]
\includegraphics{figures/aobpcm/24bps_4ch.pdf}
\caption{4 channels, 24 bits-per-sample}
\end{figure}

\clearpage

\begin{figure}[h!]
\includegraphics{figures/aobpcm/24bps_5ch.pdf}
\caption{5 channels, 24 bits-per-sample}
\end{figure}

\begin{figure}[h!]
\includegraphics{figures/aobpcm/24bps_6ch.pdf}
\caption{6 channels, 24 bits-per-sample}
\end{figure}


\clearpage
\label{aob_mlp}
\input{mlp}
