Recognizes if input is ogg or not.
[xiph/unicode.git] / theora / doc / spec / spec.tex
blob219ff44d82a3bdb841f96adf80783e5a853cf6e8
1 \documentclass[9pt,letterpaper]{book}
3 \usepackage{latexsym}
4 \usepackage{amssymb}
5 \usepackage{amsmath}
6 \usepackage{bm}
7 \usepackage{textcomp}
8 \usepackage{graphicx}
9 \usepackage{booktabs}
10 \usepackage{tabularx}
11 \usepackage{longtable}
12 \usepackage{ltablex}
13 \usepackage{wrapfig}
14 \usepackage[pdfpagemode=None,pdfstartview=FitH,pdfview=FitH,colorlinks=true]%
15 {hyperref}
17 \newtheorem{theorem}{Theorem}[section]
18 \newcommand{\idx}[1]{{\ensuremath{\mathit{#1}}}}
19 \newcommand{\qti}{\idx{qti}}
20 \newcommand{\qtj}{\idx{qtj}}
21 \newcommand{\pli}{\idx{pli}}
22 \newcommand{\plj}{\idx{plj}}
23 \newcommand{\qi}{\idx{qi}}
24 \newcommand{\ci}{\idx{ci}}
25 \newcommand{\bmi}{\idx{bmi}}
26 \newcommand{\bmj}{\idx{bmj}}
27 \newcommand{\qri}{\idx{qri}}
28 \newcommand{\qrj}{\idx{qrj}}
29 \newcommand{\hti}{\idx{hti}}
30 \newcommand{\sbi}{\idx{sbi}}
31 \newcommand{\bi}{\idx{bi}}
32 \newcommand{\bj}{\idx{bj}}
33 \newcommand{\mbi}{\idx{mbi}}
34 \newcommand{\mbj}{\idx{mbj}}
35 \newcommand{\mi}{\idx{mi}}
36 \newcommand{\cbi}{\idx{cbi}}
37 \newcommand{\qii}{\idx{qii}}
38 \newcommand{\ti}{\idx{ti}}
39 \newcommand{\tj}{\idx{tj}}
40 \newcommand{\rfi}{\idx{rfi}}
41 \newcommand{\zzi}{\idx{zzi}}
42 \newcommand{\ri}{\idx{ri}}
43 %This somewhat odd construct ensures that \bitvar{\qi}, etc., will set the
44 % qi in bold face, even though it is in a \mathit font, yet \bitvar{VAR} will
45 % set VAR in a bold, roman font.
46 \newcommand{\bitvar}[1]{\ensuremath{\mathbf{\bm{#1}}}}
47 \newcommand{\locvar}[1]{\ensuremath{\mathrm{#1}}}
48 \newcommand{\term}[1]{{\em #1}}
49 \newcommand{\bin}[1]{\ensuremath{\mathtt{b#1}}}
50 \newcommand{\hex}[1]{\ensuremath{\mathtt{0x#1}}}
51 \newcommand{\ilog}{\ensuremath{\mathop{\mathrm{ilog}}\nolimits}}
52 \newcommand{\round}{\ensuremath{\mathop{\mathrm{round}}\nolimits}}
53 \newcommand{\sign}{\ensuremath{\mathop{\mathrm{sign}}\nolimits}}
54 \newcommand{\lflim}{\ensuremath{\mathop{\mathrm{lflim}}\nolimits}}
56 %Section-based table, figure, and equation numbering.
57 \numberwithin{equation}{chapter}
58 \numberwithin{figure}{chapter}
59 \numberwithin{table}{chapter}
61 \keepXColumns
63 \pagestyle{headings}
64 \bibliographystyle{alpha}
66 \title{Theora Specification}
67 \author{Xiph.org Foundation}
68 \date{\today}
71 \begin{document}
73 \frontmatter
75 \begin{titlepage}
76 \maketitle
77 \end{titlepage}
78 \thispagestyle{empty}
79 \cleardoublepage
81 \pagenumbering{roman}
83 \thispagestyle{plain}
84 \tableofcontents
85 \cleardoublepage
87 \thispagestyle{plain}
88 \listoffigures
89 \cleardoublepage
91 \thispagestyle{plain}
92 \listoftables
93 \cleardoublepage
95 \thispagestyle{plain}
96 \markboth{{\sc Notation and Conventions}}{{\sc Notation and Conventions}}
97 \chapter*{Notation and Conventions}
99 All parameters either passed in or out of a decoding procedure are given in
100 \bitvar{bold\ face}.
102 The prefix \bin{} indicates that the following value is to be interpreted as a
103 binary number (base 2).
104 \begin{verse}
105 {\bf Example:} The value \bin{1110100} is equal to the decimal value 116.
106 \end{verse}
108 The prefix \hex{} indicates the the following value is to be interpreted as a
109 hexadecimal number (base 16).
110 \begin{verse}
111 {\bf Example:} The value \hex{74} is equal to the decimal value 116.
112 \end{verse}
114 All arithmetic defined by this specification is exact.
115 However, any real numbers that do arise will always be converted back to
116 integers again in short order.
117 The entire specification can be implemented using only normal integer
118 operations.
119 All operations are to be implemented with sufficiently large integers so that
120 overflow cannot occur.
121 Where the result of a computation is to be truncated to a fixed-sized binary
122 representation, this will be explicitly noted.
123 The size given for all variables is the maximum number of bits needed to store
124 any value in that variable.
125 Intermediate computations involving that variable may require more bits.
127 The following operators are defined:
129 \begin{description}
130 \item[$|a|$]
131 The absolute value of a number $a$.
132 \begin{align*}
133 |a| & = \left\{\begin{array}{ll}
134 -a, & a < 0 \\
135 a, & a \ge 0
136 \end{array}\right.
137 \end{align*}
139 \item[$a*b$]
140 Multiplication of a number $a$ by a number $b$.
141 \item[$\frac{a}{b}$]
142 Exact division of a number $a$ by a number $b$, producing a potentially
143 non-integer result.
145 \item[$\left\lfloor a\right\rfloor$]
146 The largest integer less than or equal to a real number $a$.
148 \item[$\left\lceil a\right\rceil$]
149 The smallest integer greater than or equal to a real number $a$.
151 \item[$a//b$]
152 Integer division of $a$ by $b$.
153 \begin{align*}
154 a//b & = \left\{\begin{array}{ll}
155 \left\lceil\frac{a}{b}\right\rceil, & a < 0 \\
156 \left\lfloor\frac{a}{b}\right\rfloor, & a \ge 0
157 \end{array}\right.
158 \end{align*}
160 \item[$a\%b$]
161 The remainder from the integer division of $a$ by $b$.
162 \begin{align*}
163 a\%b & = a-|b|*\left\lfloor\frac{a}{|b|}\right\rfloor
164 \end{align*}
165 Note that with this definition, the result is always non-negative and less than
166 $|b|$.
168 \item[$a<<b$]
169 The value obtained by left-shifting the two's complement integer $a$ by $b$
170 bits.
171 For purposes of this specification, overflow is ignored, and so this is
172 equivalent to integer multiplication of $a$ by $2^b$.
174 \item[$a>>b$]
175 The value obtained by right-shifting the two's complement integer $a$ by $b$
176 bits, filling in the leftmost bits of the new value with $0$ if $a$ is
177 non-negative and $1$ if $a$ is negative.
178 This is {\em not} equivalent to integer division of $a$ by $2^b$.
179 Instead,
180 \begin{align*}
181 a>>b & = \left\lfloor\frac{a}{2^b}\right\rfloor.
182 \end{align*}
184 \item[$\round(a)$]
185 Rounds a number $a$ to the nearest integer, with ties rounded away from $0$.
186 \begin{align*}
187 \round(a) = \left\{\begin{array}{ll}
188 \lceil a-\frac{1}{2}\rceil & a \le 0 \\
189 \lfloor a+\frac{1}{2}\rfloor & a > 0
190 \end{array}\right.
191 \end{align*}
193 \item[$\sign(a)$]
194 Returns the sign of a given number.
195 \begin{align*}
196 \sign(a) = \left\{\begin{array}{ll}
197 -1 & a < 0 \\
198 0 & a = 0 \\
199 1 & a > 0
200 \end{array}\right.
201 \end{align*}
203 \item[$\ilog(a)$]
204 The minimum number of bits required to store a positive integer $a$ in
205 two's complement notation, or $0$ for a non-positive integer $a$.
206 \begin{align*}
207 \ilog(a) = \left\{\begin{array}{ll}
208 0, & a \le 0 \\
209 \left\lfloor\log_2{a}\right\rfloor+1, & a > 0
210 \end{array}\right.
211 \end{align*}
213 \begin{verse}
214 {\bf Examples:}
215 \begin{itemize}
216 \item $\ilog(-1)=0$
217 \item $\ilog(0)=0$
218 \item $\ilog(1)=1$
219 \item $\ilog(2)=2$
220 \item $\ilog(3)=2$
221 \item $\ilog(4)=3$
222 \item $\ilog(7)=3$
223 \end{itemize}
224 \end{verse}
226 \item[$\min(a,b)$]
227 The minimum of two numbers $a$ and $b$.
229 \item[$\max(a,b)$]
230 The maximum of two numbers $a$ and $b$.
232 \end{description}
233 \cleardoublepage
236 \thispagestyle{plain}
237 \markboth{{\sc Key words}}{{\sc Key words}}
238 \chapter*{Key words}
240 %We can't rewrite this, because this is text required by RFC 2119, so we use
241 % some emergency stretching to get it typeset properly.
242 \setlength{\emergencystretch}{2em}
243 The key words ``MUST'', ``MUST NOT'', ``REQUIRED'', ``SHALL'', ``SHALL NOT'',
244 ``SHOULD'', ``SHOULD NOT'', ``RECOMMENDED'', ``MAY'', and ``OPTIONAL'' in this
245 document are to be intrepreted as described in RFC 2119 \cite{rfc2119}.\par
246 \setlength{\emergencystretch}{0em}
248 Where such assertions are placed on the contents of a Theora bitstream itself,
249 implementations should be prepared to encounter bitstreams that do not follow
250 these requirements.
251 An application's behavior in the presecence of such non-conforming bitstreams
252 is not defined by this specification, but any reasonable method of handling
253 them MAY be used.
254 By way of example, applications MAY discard the current frame, retain the
255 current output thus far, or attempt to continue on by assuming some default
256 values for the erroneous bits.
257 When such an error occurs in the bitstream headers, an application MAY refuse
258 to decode the entire stream.
259 An application SHOULD NOT allow such non-conformant bitstreams to overflow
260 buffers and potentially execute arbitrary code, as this represents a serious
261 security risk.
263 An application MUST, however, ensure any bits marked as reserved have the value
264 zero, and refuse to decode the stream if they do not.
265 These are used as place holders for future bitstream features with which the
266 current bitstream is forward-compatible.
267 Such features may not increment the bitstream version number, and can only be
268 recognized by checking the value of these reserved bits.
270 \cleardoublepage
274 \mainmatter
276 \pagenumbering{arabic}
277 \setcounter{page}{1}
279 \chapter{Introduction}
281 Theora is a general purpose, lossy video codec.
282 It is based on the VP3 video codec produced by On2 Technologies
283 (\url{http://www.on2.com/}).
284 On2 donated the VP3.1 source code to the Xiph.org Foundation and released it
285 under a BSD-like license.
286 On2 also made an irrevocable, royalty-free license grant for any patent claims
287 it might have over the software and any derivatives.
288 No formal specification exists for the VP3 format beyond this source code,
289 however Mike Melanson maintains a detailed description \cite{Mel04}.
290 Portions of this specification were adopted from that text with permission.
292 \section{VP3 and Theora}
294 Theora contains a superset of the features that were available in the original
295 VP3 codec.
296 Content encoded with VP3.1 can be losslessly transcoded into the Theora format.
297 Theora content cannot, in general, be losslessly transcoded into the VP3
298 format.
299 If a feature is not available in the original VP3 format, this is mentioned
300 when that feature is defined.
301 A complete list of these features appears in Appendix~\ref{app:vp3-compat}.
302 %TODO: VP3 - theora comparison in appendix
304 \section{Video Formats}
306 Theora currently supports progressive video data of arbitrary dimensions at a
307 constant frame rate in one of several $Y'C_bC_r$ color spaces.
308 The precise definition the supported color spaces appears in
309 Section~\ref{sec:colorspaces}.
310 Three different chroma subsampling formats are supported: 4:2:0, 4:2:2,
311 and 4:4:4.
312 The precise details of each of these formats and their sampling locations are
313 described in Section~\ref{sec:pixfmts}.
315 The Theora format does not support interlaced material, variable frame rates,
316 bit-depths larger than 8 bits per component, nor alternate color spaces such
317 as RGB or arbitrary multi-channel spaces.
318 Black and white content can be efficiently encoded, however, because the
319 uniform chroma planes compress well.
320 Support for interlaced material is planned for a future version.
321 \begin{verse}
322 {\bf Note:} Infrequently changing frame rates---as when film and video
323 sequences are cut together---can be supported in the Ogg container format by
324 chaining several Theora streams together.
325 \end{verse}
326 Support for increased bit depths or additional color spaces is not planned.
328 \section{Classification}
330 Theora is a block-based lossy transform codec that utilizes an
331 $8\times 8$ Type-II Discrete Cosine Transform and block-based motion
332 compensation.
333 This places it in the same class of codecs as MPEG-1, -2, -4, and H.263.
334 The details of how individual blocks are organized and how DCT coefficients are
335 stored in the bitstream differ substantially from these codecs, however.
336 Theora supports only intra frames (I frames in MPEG) and inter frames (P frames
337 in MPEG).
338 There is no equivalent to the bi-predictive frames (B frames) found in MPEG
339 codecs.
341 \section{Assumptions}
343 The Theora codec design assumes a complex, psychovisually-aware encoder and a
344 simple, low-complexity decoder.
345 %TODO: Talk more about implementation complexity.
347 Theora provides none of its own framing, synchronization, or protection against
348 transmission errors.
349 An encoder is solely a method of accepting input video frames and
350 compressing these frames into raw, unformatted `packets'.
351 The decoder then accepts these raw packets in sequence, decodes them, and
352 synthesizes a fascimile of the original video frames.
353 Theora is a free-form variable bit rate (VBR) codec, and packets have no
354 minimum size, maximum size, or fixed/expected size.
356 Theora packets are thus intended to be used with a transport mechanism that
357 provides free-form framing, synchronization, positioning, and error correction
358 in accordance with these design assumptions, such as Ogg (for file transport)
359 or RTP (for network multicast).
360 For the purposes of a few examples in this document, we will assume that Theora
361 is embedded in an Ogg stream specifically, although this is by no means a
362 requirement or fundamental assumption in the Theora design.
364 The specification for embedding Theora into an Ogg transport stream is given in
365 Appendix~\ref{app:oggencapsulation}.
367 \section{Codec Setup and Probability Model}
369 Theora's heritage is the proprietary commerical codec VP3, and it retains a
370 fair amount of inflexibility when compared to Vorbis \cite{vorbis}, the first
371 Xiph.org codec, which began as a research codec.
372 However, to provide additional scope for encoder improvement, Theora adopts
373 some of the configurable aspects of decoder setup that are present in Vorbis.
374 This configuration data is not available in VP3, which uses hardcoded values
375 instead.
377 Theora makes the same controversial design decision that Vorbis made to include
378 the entire probability model for the DCT coefficients and all the quantization
379 parameters in the bitstream headers.
380 This is often several hundred fields.
381 It is therefore impossible to decode any frame in the stream without
382 having previously fetched the codec info and codec setup headers.
384 \begin{verse}
385 {\bf Note:} Theora {\em can} initiate decode at an arbitrary intra-frame packet
386 within a bitstream so long as the codec has been initialized with the setup
387 headers.
388 \end{verse}
390 Thus, Theora headers are both required for decode to begin and relatively large
391 as bitstream headers go.
392 The header size is unbounded, although as a rule-of-thumb less than 16kB is
393 recommended, and Xiph.org's reference encoder follows this suggestion.
394 %TODO: Is 8kB enough? My setup header is 7.4kB, that doesn't leave much room
395 % for comments.
396 %RG: the lesson from vorbis is that as small as possible is really
397 % important in some applications. Practically, what's acceptable
398 % depends a great deal on the target bitrate. I'd leave 16 kB in the
399 % spec for now. fwiw more than 1k of comments is quite unusual.
401 Our own design work indicates that the primary liability of the required header
402 is in mindshare; it is an unusual design and thus causes some amount of
403 complaint among engineers as this runs against current design trends and
404 points out limitations in some existing software/interface designs.
405 However, we find that it does not fundamentally limit Theora's suitable
406 application space.
408 %silvia: renamed
409 %\subsection{Format Specification}
410 \section{Format Conformance}
412 The Theora format is well-defined by its decode specification; any encoder that
413 produces packets that are correctly decoded by an implementation following
414 this specification may be considered a proper Theora encoder.
415 A decoder must faithfully and completely implement the specification defined
416 herein %, except where noted,
417 to be considered a conformant Theora decoder.
418 A decoder need not be implemented strictly as described, but the
419 actual decoder process MUST be {\em entirely mathematically equivalent}
420 to the described process.
421 Where appropriate, a non-normative description of encoder processes is
422 included.
423 These sections will be marked as such, and a proper Theora encoder is not
424 bound to follow them.
426 %TODO: \subsection{Hardware Profile}
429 \chapter{Coded Video Structure}
431 Theora's encoding and decoding process is based on $8\times 8$ blocks of
432 pixels.
433 This sections describes how a video frame is laid out, divided into
434 blocks, and how those blocks are organized.
436 \section{Frame Layout}
438 A video frame in Theora is a two-dimensional array of pixels.
439 Theora, like VP3, uses a right-handed coordinate system, with the origin in the
440 lower-left corner of the frame.
441 This is contrary to many video formats which use a left-handed coordinate
442 system with the origin in the upper-left corner of the frame.
443 %INT: This means that for interlaced material, the definition of `even fields'
444 %INT: and `odd fields' may be reversed between Theora and other video codecs.
445 %INT: This document will always refer to them as `top fields' and `bottom
446 %INT: fields'.
448 Theora divides the pixel array up into three separate \term{color planes}, one
449 for each of the $Y'$, $C_b$, and $C_r$ components of the pixel.
450 The $Y'$ plane is also called the \term{luma plane}, and the $C_b$ and $C_r$
451 planes are also called the \term{chroma planes}.
452 Each plane is assigned a numerical value, as shown in
453 Table~\ref{tab:color-planes}.
455 \begin{table}[htbp]
456 \begin{center}
457 \begin{tabular}{cl}\toprule
458 Index & Color Plane \\\midrule
459 $0$ & $Y'$ \\
460 $1$ & $C_b$ \\
461 $2$ & $C_r$ \\
462 \bottomrule\end{tabular}
463 \end{center}
464 \caption{Color Plane Indices}
465 \label{tab:color-planes}
466 \end{table}
468 In some pixel formats, the chroma planes are subsampled by a factor of two
469 in one or both directions.
470 This means that the width or height of the chroma planes may be half that of
471 the total frame width and height.
472 The luma plane is never subsampled.
474 \section{Picture Region}
476 An encoded video frame in Theora is required to have a width and height that
477 are multiples of sixteen, making an integral number of blocks even when the
478 chroma planes are subsampled.
479 However, inside a frame a smaller \term{picture region} may be defined
480 to present material whose dimensions are not a multiple of sixteen pixels, as
481 shown in Figure~\ref{fig:pic-frame}.
482 The picture region can be offset from the lower-left corner of the frame by up
483 to 255 pixels in each direction, and may have an arbitrary width and height,
484 provided that it is contained entirely within the coded frame.
485 It is this picture region that contains the actual video data.
486 The portions of the frame which lie outside the picture region may contain
487 arbitrary image data, so the frame must be cropped to the picture region
488 before display.
489 The picture region plays no other role in the decode process, which operates on
490 the entire video frame.
492 \begin{figure}[htbp]
493 \begin{center}
494 \includegraphics{pic-frame}
495 \end{center}
496 \caption{Location of frame and picture regions}
497 \label{fig:pic-frame}
498 \end{figure}
500 \section{Blocks and Super Blocks}
501 \label{sec:blocks-and-sbs}
503 Each color plane is subdivided into \term{blocks} of $8\times 8$ pixels.
504 Blocks are grouped into $4\times 4$ arrays called \term{super blocks} as
505 shown in Figure~\ref{fig:superblock}.
506 Each color plane has its own set of blocks and super blocks.
507 If the chroma planes are subsampled, they are still divided into $8\times 8$
508 blocks of pixels; there are just fewer blocks than in the luma plane.
509 The boundaries of blocks and super blocks in the luma plane do not necessarily
510 coincide with those of the chroma planes, if the chroma planes have been
511 subsampled.
513 \begin{figure}[htbp]
514 \begin{center}
515 \includegraphics{superblock}
516 \end{center}
517 \caption{Subdivision of a frame into blocks and super blocks}
518 \label{fig:superblock}
519 \end{figure}
521 Blocks are accessed in two different orders in the various decoder processes.
522 The first is \term{raster order}, illustrated in Figure~\ref{fig:raster-block}.
523 This accesses each block in row-major order, starting in the lower left of the
524 frame and continuing along the bottom row of the entire frame, followed by the
525 next row up, starting on the left edge of the frame, etc.
527 \begin{figure}[htbp]
528 \begin{center}
529 \includegraphics{raster-block}
530 \end{center}
531 \caption{Raster ordering of $n\times m$ blocks}
532 \label{fig:raster-block}
533 \end{figure}
535 The second is \term{coded order}.
536 In coded order, blocks are accessed by super block.
537 Within each frame, super blocks are traversed in raster order,
538 similar to raster order for blocks.
539 Within each super block, however, blocks are accessed in a Hilbert curve
540 pattern, illustrated in Figure~\ref{fig:hilbert-block}.
541 If a color plane does not contain a complete super block on the top or right
542 sides, the same ordering is still used, simply with any blocks outside the
543 frame boundary ommitted.
545 \begin{figure}[htbp]
546 \begin{center}
547 \includegraphics{hilbert-block}
548 \end{center}
549 \caption{Hilbert curve ordering of blocks within a super block}
550 \label{fig:hilbert-block}
551 \end{figure}
553 To illustrate this ordering, consider a frame that is 240 pixels wide and
554 48 pixels high.
555 Each row of the luma plane has 30 blocks and 8 super blocks, and there are 6
556 rows of blocks and two rows of super blocks.
558 %When accessed in raster order, each block in the luma plane is assigned the
559 % following indices:
561 %\vspace{\baselineskip}
562 %\begin{center}
563 %\begin{tabular}{|ccccccc|}\hline
564 %150 & 151 & 152 & 153 & $\ldots$ & 178 & 179 \\
565 %120 & 121 & 122 & 123 & $\ldots$ & 148 & 149 \\\hline
566 % 90 & 91 & 92 & 93 & $\ldots$ & 118 & 119 \\
567 % 60 & 61 & 62 & 63 & $\ldots$ & 88 & 89 \\
568 % 30 & 31 & 32 & 33 & $\ldots$ & 58 & 59 \\
569 % 0 & 1 & 2 & 3 & $\ldots$ & 28 & 29 \\\hline
570 %\end{tabular}
571 %\end{center}
572 %\vspace{\baselineskip}
574 When accessed in coded order, each block in the luma plane is assigned the
575 following indices:
577 \vspace{\baselineskip}
578 \begin{center}
579 \begin{tabular}{|cccc|c|cc|}\hline
580 123 & 122 & 125 & 124 & $\ldots$ & 179 & 178 \\
581 120 & 121 & 126 & 127 & $\ldots$ & 176 & 177 \\\hline
582 5 & 6 & 9 & 10 & $\ldots$ & 117 & 118 \\
583 4 & 7 & 8 & 11 & $\ldots$ & 116 & 119 \\
584 3 & 2 & 13 & 12 & $\ldots$ & 115 & 114 \\
585 0 & 1 & 14 & 15 & $\ldots$ & 112 & 113 \\\hline
586 \end{tabular}
587 \end{center}
588 \vspace{\baselineskip}
590 Here the index values specify the order in which the blocks would be accessed.
591 The indices of the blocks are numbered continuously from one color plane to the
592 next.
593 They do not reset to zero at the start of each plane.
594 Instead, the numbering increases continuously from the $Y'$ plane to the $C_b$
595 plane to the $C_r$ plane.
596 The implication is that the blocks from all planes are treated as a unit during
597 the various processing steps.
599 Although blocks are sometimes accessed in raster order, in this document the
600 index associated with a block is {\em always} its index in coded order.
602 \section{Macro Blocks}
603 \label{sec:mbs}
605 A macro block contains a $2\times 2$ array of blocks in the luma plane
606 {\em and} the co-located blocks in the chroma planes, as shown in
607 Figure~\ref{fig:macroblock}.
608 Thus macro blocks can represent anywhere from six to twelve blocks, depending
609 on how the chroma planes are subsampled.
610 This is in contrast to super blocks, which only contain blocks from a single
611 color plane.
612 % the whole super vs. macro blocks thing is a little confusing, and it can be
613 % hard to remember which is what initially. A figure would/will help here,
614 % but I tried to add some text emphasizing the difference in terms of
615 % functionality.
616 %TBT: At this point we haven't described any functionality yet.
617 %TBT: As far as the reader knows, the only purpose of the blocks, macro blocks
618 %TBT: and super blocks is for data organization---and for blocks and super
619 %TBT: blocks, this is essentially true.
620 %TBT: So lets restrict the differences we emphasize to those of data
621 %TBT: organization, which the sentence I just added above does.
622 Macro blocks contain information about coding mode and motion vectors for the
623 corresponding blocks in all color planes.
625 \begin{figure}[htbp]
626 \begin{center}
627 \includegraphics{macroblock}
628 \end{center}
629 \caption{Subdivision of a frame into macro blocks}
630 \label{fig:macroblock}
631 \end{figure}
633 Macro blocks are also accessed in a \term{coded order}.
634 This coded order proceeds by examining each super block in the luma plane in
635 raster order, and traversing the four macro blocks inside using a smaller
636 Hilbert curve, as shown in Figure~\ref{fig:hilbert-mb}.
637 %r: I rearranged the wording to make a more formal idiom here
638 If the luma plane does not contain a complete super block on the top or right
639 sides, the same ordering is still used, with any macro blocks outside
640 the frame boundary simply omitted.
641 Because the frame size is constrained to be a multiple of 16, there are never
642 any partial macro blocks.
643 Unlike blocks, macro blocks need never be accessed in a pure raster order.
645 \begin{figure}[htbp]
646 \begin{center}
647 \includegraphics{hilbert-mb}
648 \end{center}
649 \caption{Hilbert curve ordering of macro blocks within a super block}
650 \label{fig:hilbert-mb}
651 \end{figure}
653 Using the same frame size as the example above, there are 15 macro blocks in
654 each row and 3 rows of macro blocks.
655 The macro blocks are assigned the following indices:
657 \vspace{\baselineskip}
658 \begin{center}
659 \begin{tabular}{|cc|cc|c|cc|c|}\hline
660 30 & 31 & 32 & 33 & $\cdots$ & 42 & 43 & 44 \\\hline
661 1 & 2 & 5 & 6 & $\cdots$ & 25 & 26 & 29 \\
662 0 & 3 & 4 & 7 & $\cdots$ & 24 & 27 & 28 \\\hline
663 \end{tabular}
664 \end{center}
665 \vspace{\baselineskip}
667 \section{Coding Modes and Prediction}
669 Each block is coded using one of a small, fixed set of \term{coding modes} that
670 define how the block is predicted from previous frames.
671 A block is predicted using one of two \term{reference frames}, selected
672 according to the coding mode.
673 A reference frame is the fully decoded version of a previous frame in the
674 stream.
675 The first available reference frame is the previous intra frame, called the
676 \term{golden frame}.
677 The second available reference frame is the previous frame, whether it was an
678 intra frame or an inter frame.
679 If the previous frame was an intra frame, then both reference frames are the
680 same.
681 See Figure~\ref{fig:reference-frames} for an illustration of the reference
682 frames used for an intra frame that does not follow an intra frame.
684 \begin{figure}[htbp]
685 \begin{center}
686 \includegraphics{reference-frames}
687 \end{center}
688 \caption{Example of reference frames for an inter frame}
689 \label{fig:reference-frames}
690 \end{figure}
692 Two coding modes in particular are worth mentioning here.
693 The INTRA mode is used for blocks that are not predicted from either reference
694 frame.
695 This is the only coding mode allowed in intra frames.
696 The INTER\_NOMV coding mode uses the co-located contents of the block in the
697 previous frame as the predictor.
698 This is the default coding mode.
700 \section{DCT Coefficients}
701 \label{sec:dct-coeffs}
703 A \term{residual} is added to the predicted contents of a block to form the
704 final reconstruction.
705 The residual is stored as a set of quantized coefficients from an integer
706 approximation of a two-dimensional Type II Discrete Cosine Transform.
707 The DCT takes an $8\times 8$ array of pixel values as input and returns an
708 $8\times 8$ array of coefficient values.
709 The \term{natural ordering} of these coefficients is defined to be row-major
710 order, from lowest to highest frequency.
711 They are also often indexed in \term{zig-zag order}, as shown in
712 Figure~\ref{tab:zig-zag}.
714 \begin{figure}[htbp]
715 \begin{center}
716 \begin{tabular}[c]{rr|c@{}c@{}c@{}c@{}c@{}c@{}c@{}c@{}c@{}c@{}c@{}c@{}c@{}c@{}c}
717 &\multicolumn{1}{r}{} & && &&&&&$c$&&& && && \\
718 &\multicolumn{1}{r}{} &0&&1&&2&&3&&4&&5&&6&&7 \\\cline{3-17}
719 &0 & 0 &$\rightarrow$& 1 && 5 &$\rightarrow$& 6 && 14 &$\rightarrow$& 15 && 27 &$\rightarrow$& 28 \\[-0.5\defaultaddspace]
720 & & &$\swarrow$&&$\nearrow$& &$\swarrow$&&$\nearrow$& &$\swarrow$&&$\nearrow$& &$\swarrow$& \\
721 &1 & 2 & & 4 && 7 & & 13 && 16 & & 26 && 29 & & 42 \\[-0.5\defaultaddspace]
722 & &$\downarrow$&$\nearrow$&&$\swarrow$&&$\nearrow$&&$\swarrow$&&$\nearrow$&&$\swarrow$&&$\nearrow$&$\downarrow$ \\
723 &2 & 3 & & 8 && 12 & & 17 && 25 & & 30 && 41 & & 43 \\[-0.5\defaultaddspace]
724 & & &$\swarrow$&&$\nearrow$& &$\swarrow$&&$\nearrow$& &$\swarrow$&&$\nearrow$& &$\swarrow$& \\
725 &3 & 9 & & 11 && 18 & & 24 && 31 & & 40 && 44 & & 53 \\[-0.5\defaultaddspace]
726 $r$&&$\downarrow$&$\nearrow$&&$\swarrow$&&$\nearrow$&&$\swarrow$&&$\nearrow$&&$\swarrow$&&$\nearrow$&$\downarrow$ \\
727 &4 & 10 & & 19 && 23 & & 32 && 39 & & 45 && 52 & & 54 \\[-0.5\defaultaddspace]
728 & & &$\swarrow$&&$\nearrow$& &$\swarrow$&&$\nearrow$& &$\swarrow$&&$\nearrow$& &$\swarrow$& \\
729 &5 & 20 & & 22 && 33 & & 38 && 46 & & 51 && 55 & & 60 \\[-0.5\defaultaddspace]
730 & &$\downarrow$&$\nearrow$&&$\swarrow$&&$\nearrow$&&$\swarrow$&&$\nearrow$&&$\swarrow$&&$\nearrow$&$\downarrow$ \\
731 &6 & 21 & & 34 && 37 & & 47 && 50 & & 56 && 59 & & 61 \\[-0.5\defaultaddspace]
732 & & &$\swarrow$&&$\nearrow$& &$\swarrow$&&$\nearrow$& &$\swarrow$&&$\nearrow$& &$\swarrow$& \\
733 &7 & 35 &$\rightarrow$& 36 && 48 &$\rightarrow$& 49 && 57 &$\rightarrow$& 58 && 62 &$\rightarrow$& 63
734 \end{tabular}
735 \end{center}
736 \caption{Zig-zag order}
737 \label{tab:zig-zag}
738 \end{figure}
740 \begin{verse}
741 {\bf Note:} the row and column indices refer to {\em frequency number} and not
742 pixel locations.
743 The frequency numbers are defined independently of the memory organization of
744 the pixels.
745 They have been written from top to bottom here to follow conventional notation,
746 despite the right-handed coordinate system Theora uses for pixel locations.
747 %RG: I'd rather we were internally consistent and put dc at the lower left.
748 Many implementations of the DCT operate `in-place'.
749 That is, they return DCT coefficients in the same memory buffer that the
750 initial pixel values were stored in.
751 Due to the right-handed coordinate system used for pixel locations in Theora,
752 one must note carefully how both pixel values and DCT coefficients are
753 organized in memory in such a system.
754 \end{verse}
756 DCT coefficient $(0,0)$ is called the \term{DC coefficient}.
757 All the other coefficients are called \term{AC coefficients}.
760 \chapter{Decoding Overview}
762 This section provides a high level description of the Theora codec's
763 construction.
764 A bit-by-bit specification appears beginning in Section~\ref{sec:bitpacking}.
765 The later sections assume a high-level understanding of the Theora decode
766 process, which is provided below.
768 \section{Decoder Configuration}
770 Decoder setup consists of configuration of the quantization matrices and the
771 Huffman codebooks for the DCT coefficients, and a table of limit values for
772 the deblocking filter.
773 The remainder of the decoding pipeline is not configurable.
775 \subsection{Global Configuration}
777 The global codec configuration consists of a few video related fields, such as
778 frame rate, frame size, picture size and offset, aspect ratio, color space,
779 pixel format, and a version number.
780 The version number is divided into a major version, a minor version, amd a
781 minor revision number.
782 %r: afaik the released vp3 codec called itself 3.1 and is compatible w/ theora
783 %r: even though we received the in-progress 3.2 codebase
784 For the format defined in this specification, these are `3', `2', and
785 `1', respectively, in reference to Theora's origin as a successor to
786 the VP3.1 format.
788 \subsection{Quantization Matrices}
790 Theora allows up to 384 different quantization matrices to be defined, one for
791 each \term{quantization type}, \term{color plane} ($Y'$, $C_b$, or $C_r$), and
792 \term{quantization index}, \qi, which ranges from zero to 63, inclusive.
793 There are currently two quantization types defined, which depend on the coding
794 mode of the block being dequantized, as shown in Table~\ref{tab:quant-types}.
796 \begin{table}[htbp]
797 \begin{center}
798 \begin{tabular}{cl}\toprule
799 Quantization Type & Usage \\\midrule
800 $0$ & INTRA-mode blocks \\
801 $1$ & Blocks in any other mode. \\
802 \bottomrule\end{tabular}
803 \end{center}
804 \caption{Quantization Type Indices}
805 \label{tab:quant-types}
806 \end{table}
808 %r: I think 'nominally' is more specific than 'generally' here
809 The quantization index, on the other hand, nominally represents a progressive
810 range of quality levels, from low quality near zero to high quality near 63.
811 However, the interpretation is arbitrary, and it is possible, for example, to
812 partition the scale into two completely separate ranges with 32 levels each
813 that are meant to represent different classes of source material, or any
814 other arrangement that suits the encoder's requirements.
816 Each quantization matrix is an $8\times 8$ matrix of 16-bit values, which is
817 used to quantize the output of the $8\times 8$ DCT\@.
818 Quantization matrices are specified using three components: a
819 \term{base matrix} and two \term{scale values}.
820 The first scale value is the \term{DC scale}, which is applied to the DC
821 component of the base matrix.
822 The second scale value is the \term{AC scale}, which is applied to all the
823 other components of the base matrix.
824 There are 64 DC scale values and 64 AC scale values, one for each \qi\ value.
826 There are 64 elements in each base matrix, one for each DCT coefficient.
827 They are stored in natural order (cf. Section~\ref{sec:dct-coeffs}).
828 There is a separate set of base matrices for each quantization type and each
829 color plane, with up to 64 possible base matrices in each set, one for each
830 \qi\ value.
831 %r: we will mention that the given matricies must bound the \qi range
832 %r: in the detailed section. it's not important at this level.
833 Typically the bitstream contains matrices for only a sparse subset of the
834 possible \qi\ values.
835 The base matrices for the remainder of the \qi\ values are computed using
836 linear interpolation.
837 This configuration allows the encoder to adjust the quantization matrices to
838 approximate the complex, non-linear response of the human visual system to
839 different quantization errors.
841 Finally, because the in-loop deblocking filter strength depends on the strength
842 of the quantization matrices defined in this header, a table of 64 \term{loop
843 filter limit values} is defined, one for each \qi\ value.
845 The precise specification of how all of this information is decoded appears in
846 Section~\ref{sub:loop-filter-limits} and Section~\ref{sub:quant-params}.
848 \subsection{Huffman Codebooks}
850 Theora uses 80 configurable binary Huffman codes to represent the 32 tokens
851 used to encode DCT coefficients.
852 Each of the 32 token values has a different semantic meaning and is used to
853 represent single coefficient values, zero runs, combinations of the two, and
854 \term{End-Of-Block markers}.
856 The 80 codes are divided up into five groups of 16, with each group
857 corresponding to a set of DCT coefficient indices.
858 The first group corresponds to the DC coefficient, while the remaining four
859 groups correspond to different subsets of the AC coefficients.
860 Within each frame, two pairs of 4-bit codebook indices are stored.
861 The first pair selects which codebooks to use from the DC coefficient group for
862 the $Y'$ coefficients and the $C_b$ and $C_r$ coefficients.
863 The second pair selects which codebooks to use from {\em all four} of the AC
864 coefficient groups for the $Y'$ coefficients and the $C_b$ and $C_r$
865 coefficients.
867 The precise specification of how the codebooks are decoded appears in
868 Section~\ref{sub:huffman-tables}.
870 \section{High-Level Decode Process}
872 \subsection{Decoder Setup}
874 Before decoding can begin, a decoder MUST be initialized using the bitstream
875 headers corresponding to the stream to be decoded.
876 Theora uses three header packets; all are required, in order, by this
877 specification.
878 Once set up, decode may begin at any intra-frame packet---or even inter-frame
879 packets, provided the appropriate decoded reference frames have already been
880 decoded and cached---belonging to the Theora stream.
881 In Theora I, all packets after the three initial headers are intra-frame or
882 inter-frame packets.
884 The header packets are, in order, the identification header, the comment
885 header, and the setup header.
887 \paragraph{Identification Header}
889 The identification header identifies the stream as Theora, provides a version
890 number, and defines the characteristics of the video stream such as frame
891 size.
892 A complete description of the identification header appears in
893 Section~\ref{sec:idheader}.
895 \paragraph{Comment Header}
897 The comment header includes user text comments (`tags') and a vendor string
898 for the application/library that produced the stream.
899 The format of the comment header is the same as that used in the Vorbis I and
900 Speex codecs, with slight modifications due to the use of a different bit
901 packing mechanism.
902 A complete description of how the comment header is coded appears in
903 Section~\ref{sec:commentheader}, along with a suggested set of tags.
905 \paragraph{Setup Header}
907 The setup header includes extensive codec setup information, including the
908 complete set of quantization matrices and Huffman codebooks needed to decode
909 the DCT coefficients.
910 A complete description of the setup header appears in
911 Section~\ref{sec:setupheader}.
913 \subsection{Decode Procedure}
915 The decoding and synthesis procedure for all video packets is fundamentally the
916 same, with some steps omitted for intra frames.
917 \begin{itemize}
918 \item
919 Decode packet type flag.
920 \item
921 Decode frame header.
922 \item
923 Decode coded block information (inter frames only).
924 \item
925 Decode macro block mode information (inter frames only).
926 \item
927 Decode motion vectors (inter frames only).
928 \item
929 Decode block-level \qi\ information.
930 \item
931 Decode DC coefficient for each coded block.
932 \item
933 Decode 1st AC coefficient for each coded block.
934 \item
935 Decode 2nd AC coefficient for each coded block.
936 \item
937 $\ldots$
938 \item
939 Decode 63rd AC coefficient for each coded block.
940 \item Perform DC coefficient prediction.
941 \item Reconstruct coded blocks.
942 \item Copy uncoded bocks.
943 \item Perform loop filtering.
944 \end{itemize}
946 \begin{verse}
947 {\bf Note:} clever rearrangement of the steps in this process is possible.
948 As an example, in a memory-constrained environment, one can make multiple
949 passes through the DCT coefficients to avoid buffering them all in memory.
950 On the first pass, the starting location of each coefficient is identified, and
951 then 64 separate get pointers are used to read in the 64 DCT coefficients
952 required to reconstruct each coded block in sequence.
953 This operation produces entirely equivalent output and is naturally perfectly
954 legal.
955 It may even be a benefit in non-memory-constrained environments due to a
956 reduced cache footprint.
957 \end{verse}
959 Theora makes equivalence easy to check by defining all decoding operations in
960 terms of exact integer operations.
961 No floating-point math is required, and in particular, the implementation of
962 the iDCT transform MUST be followed precisely.
963 This prevents the decoder mismatch problem commonly associated with codecs that
964 provide a less rigorous transform specification.
965 Such a mismatch problem would be devastating to Theora, since a single rounding
966 error in one frame could propagate throughout the entire succeeding frame due
967 to DC prediction.
969 \paragraph{Packet Type Decode}
971 Theora uses four packet types.
972 The first three packet types mark each of the three Theora headers described
973 above.
974 The fourth packet type marks a video packet.
975 All other packet types are reserved; packets marked with a reserved type should
976 be ignored.
978 Additionally, zero-length packets are treated as if they were an inter
979 frame with no blocks coded. That is, as a duplicate frame.
981 \paragraph{Frame Header Decode}
983 The frame header contains some global information about the current frame.
984 The first is the frame type field, which specifies if this is an intra frame or
985 an inter frame.
986 Inter frames predict their contents from previously decoded reference frames.
987 Intra frames can be independently decoded with no established reference frames.
989 The next piece of information in the frame header is the list of \qi\ values
990 allowed in the frame.
991 Theora allows from one to three different \qi\ values to be used in a single
992 frame, each of which selects a set of six quantization matrices, one for each
993 quantization type (inter or intra), and one for each color plane.
994 The first \qi\ value is {\em always} used when dequantizing DC coefficients.
995 The \qi\ value used when dequantizing AC coefficients, however, can vary from
996 block to block.
997 VP3, in contrast, only allows a single \qi\ value per frame for both the DC and
998 AC coefficients.
1000 \paragraph{Coded Block Information}
1002 This stage determines which blocks in the frame are coded and which are
1003 uncoded.
1004 A \term{coded block list} is constructed which lists all the coded blocks in
1005 coded order.
1006 For intra frames, every block is coded, and so no data needs to be read from
1007 the packet.
1009 \paragraph{Macro Block Mode Information}
1011 For intra frames, every block is coded in INTRA mode, and this stage is
1012 skipped.
1013 In inter frames a \term{coded macro block list} is constructed from the coded
1014 block list.
1015 Any macro block which has at least one of its luma blocks coded is considered
1016 coded; all other macro blocks are uncoded, even if they contain coded chroma
1017 blocks.
1018 A coding mode is decoded for each coded macro block, and assigned to all its
1019 constituent coded blocks.
1020 All coded chroma blocks in uncoded macro blocks are assigned the INTER\_NOMV
1021 coding mode.
1023 \paragraph{Motion Vectors}
1025 Intra frames are coded entirely in INTRA mode, and so this stage is skipped.
1026 Some inter coding modes, however, require one or more motion vectors to be
1027 specified for each macro block.
1028 These are decoded in this stage, and an appropriate motion vector is assigned
1029 to each coded block in the macro block.
1031 \paragraph{Block-Level \qi\ Information}
1033 If a frame allows multiple \qi\ values, the \qi\ value assigned to each block
1034 is decoded here.
1035 Frames that use only a single \qi\ value have nothing to decode.
1037 \paragraph{DCT Coefficients}
1039 Finally, the quantized DCT coefficients are decoded.
1040 A list of DCT coefficients in zig-zag order for a single block is represented
1041 by a list of tokens.
1042 A token can take on one of 32 different values, each with a different semantic
1043 meaning.
1044 A single token can represent a single DCT coefficient, a run of zero
1045 coefficients within a single block, a combination of a run of zero
1046 coefficients followed by a single non-zero coefficient, an
1047 \term{End-Of-Block marker}, or a run of EOB markers.
1048 EOB markers signify that the remainder of the block is one long zero run.
1049 Unlike JPEG and MPEG, there is no requirement for each block to end with
1050 a special marker.
1051 If non-EOB tokens yield values for all 64 of the coefficients in a block, then
1052 no EOB marker occurs.
1054 Each token is associated with a specific \term{token index} in a block.
1055 For single-coefficient tokens, this index is the zig-zag index of the token in
1056 the block.
1057 For zero-run tokens, this index is the zig-zag index of the {\em first}
1058 coefficient in the run.
1059 For combination tokens, the index is again the zig-zag index of the first
1060 coefficient in the zero run.
1061 For EOB markers, which signify that the remainder of the block is one long zero
1062 run, the index is the zig-zag index of the first zero coefficient in that run.
1063 For EOB runs, the token index is that of the first EOB marker in the run.
1064 Due to zero runs and EOB markers, a block does not have to have a token for
1065 every zig-zag index.
1067 Tokens are grouped in the stream by token index, not by the block they
1068 originate from.
1069 This means that for each zig-zag index in turn, the tokens with that index from
1070 {\em all} the coded blocks are coded in coded block order.
1071 When decoding, a current token index is maintained for each coded block.
1072 This index is advanced by the number of coefficients that are added to the
1073 block as each token is decoded.
1074 After fully decoding all the tokens with token index \ti, the current token
1075 index of every coded block will be \ti\ or greater.
1077 If an EOB run of $n$ blocks is decoded at token index \ti, then it ends the
1078 next $n$ blocks in coded block order whose current token index is equal to
1079 \ti, but not greater.
1080 If there are fewer than $n$ blocks with a current token index of \ti, then the
1081 decoder goes through the coded block list again from the start, ending blocks
1082 with a current token index of $\ti+1$, and so on, until $n$ blocks have been
1083 ended.
1085 Tokens are read by parsing a Huffman code that depends on \ti\ and the color
1086 plane of the next coded block whose current token index is equal to \ti, but
1087 not greater.
1088 The Huffman codebooks are selected on a per-frame basis from the 80 codebooks
1089 defined in the setup header.
1090 Many tokens have a fixed number of \term{extra bits} associated with them.
1091 These bits are read from the packet immediately after the token is decoded.
1092 These are used to define things such as coefficient magnitude, sign, and the
1093 length of runs.
1095 \paragraph{DC Prediction}
1097 After the coefficients for each block are decoded, the quantized DC value of
1098 each block is adjusted based on the DC values of its neighbors.
1099 This adjustment is performed by scanning the blocks in raster order, not coded
1100 block order.
1102 \paragraph{Reconstruction}
1104 Finally, using the coding mode, motion vector (if applicable), quantized
1105 coefficient list, and \qi\ value defined for each block, all the coded blocks
1106 are reconstructed.
1107 The DCT coefficients are dequantized, an inverse DCT transform is applied, and
1108 the predictor is formed from the coding mode and motion vector and added to
1109 the result.
1111 \paragraph{Loop Filtering}
1113 To complete the reconstructed frame, an ``in-loop'' deblocking filter is
1114 applied to the edges of all coded blocks.
1117 \chapter{Video Formats}
1119 This section gives a precise description of the video formats that Theora is
1120 capable of storing.
1121 The Theora bitstream is capable of handling video at any arbitrary resolution
1122 up to $1048560\times 1048560$.
1123 Such video would require almost three terabytes of storage per frame for
1124 uncompressed data, so compliant decoders MAY refuse to decode images with
1125 sizes beyond their capabilities.
1126 %TODO: What MUST a "compliant" decoder accept?
1127 %TODO: What SHOULD a decoder use for an upper bound? (derive from total amount
1128 %TODO: of memory and memory bandwidth)
1129 %TODO: Any lower limits?
1130 %TODO: We really need hardware device profiles, but such things should be
1131 %TODO: developed with input from the hardware community.
1132 %TODO: And even then sometimes they're useless
1134 The remainder of this section talks about two specific aspects of the video
1135 format: the color space and the pixel format.
1136 The first describes how color is represented and how to transform that color
1137 representation into a device independent color space such as CIE $XYZ$ (1931).
1138 The second describes the various schemes for sampling the color values in time
1139 and space.
1141 \section{Color Space Conventions}
1143 There are a large number of different color standards used in digital video.
1144 Since Theora is a lossy codec, it restricts itself to only a few of them to
1145 simplify playback.
1146 Unlike the alternate method of describing all the parameters of the color
1147 model, this allows a few dedicated routines for color conversion to be written
1148 and heavily optimized in a decoder.
1149 More flexible conversion functions should instead be specified in an encoder,
1150 where additional computational complexity is more easily tolerated.
1151 The color spaces were selected to give a fair representation of color standards
1152 in use around the world today.
1153 Most of the standards that do not exactly match one of these can be converted
1154 to one fairly easily.
1156 All Theora color spaces are $Y'C_bC_r$ color spaces with one luma channel and
1157 two chroma channels.
1158 Each channel contains 8-bit discrete values in the range $0\ldots255$, which
1159 represent non-linear gamma pre-corrected signals.
1160 The Theora identification header contains an 8-bit value that describes the
1161 color space.
1162 This merely selects one of the color spaces available from an enumerated list.
1163 Currently, only two color spaces are defined, with a third possibility that
1164 indicates the color space is ``unknown".
1166 \section{Color Space Conversions and Parameters}
1167 \label{sec:color-xforms}
1169 The parameters which describe the conversions between each color space are
1170 listed below.
1171 These are the parameters needed to map colors from the encoded $Y'C_bC_r$
1172 representation to the device-independent color space CIE $XYZ$ (1931).
1173 These parameters define abstract mathematical conversion functions which are
1174 infinitely precise.
1175 The accuracy and precision with which the conversions are performed in a real
1176 system is determined by the quality of output desired and the available
1177 processing power.
1178 Exact decoder output is defined by this specification only in the original
1179 $Y'C_bC_r$ space.
1181 \begin{description}
1182 \item[$Y'C_bC_r$ to $Y'P_bP_r$:]
1183 \vspace{\baselineskip}\hfill
1185 This conversion takes 8-bit discrete values in the range $[0\ldots255]$ and
1186 maps them to real values in the range $[0\ldots1]$ for Y and
1187 $[-\frac{1}{2}\ldots\frac{1}{2}]$ for $P_b$ and $P_r$.
1188 Because some values may fall outside the offset and excursion defined for each
1189 channel in the $Y'C_bC_r$ space, the results may fall outside these ranges in
1190 $Y'P_bP_r$ space.
1191 No clamping should be done at this stage.
1193 \begin{align}
1194 Y'_\mathrm{out} & =
1195 \frac{Y'_\mathrm{in}-\mathrm{Offset}_Y}{\mathrm{Excursion}_Y} \\
1196 P_b & =
1197 \frac{C_b-\mathrm{Offset}_{C_b}}{\mathrm{Excursion}_{C_b}} \\
1198 P_r & =
1199 \frac{C_r-\mathrm{Offset}_{C_r}}{\mathrm{Excursion}_{C_r}}
1200 \end{align}
1202 Parameters: $\mathrm{Offset}_{Y,C_b,C_r}$, $\mathrm{Excursion}_{Y,C_b,C_r}$.
1204 \item[$Y'P_bP_r$ to $R'G'B'$:]
1205 \vspace{\baselineskip}\hfill
1207 This conversion takes the one luma and two chroma channel representation and
1208 maps it to the non-linear $R'G'B'$ space used to drive actual output devices.
1209 Values should be clamped into the range $[0\ldots1]$ after this stage.
1211 \begin{align}
1212 R' & = Y'+2(1-K_r)P_r \\
1213 G' & = Y'-2\frac{(1-K_b)K_b}{1-K_b-K_r}P_b-2\frac{(1-K_r)K_r}{1-K_b-K_r}P_r\\
1214 B' & = Y'+2(1-K_b)P_b
1215 \end{align}
1217 Parameters: $K_b,K_r$.
1219 \item[$R'G'B'$ to $RGB$ (Output device gamma correction):]
1220 \vspace{\baselineskip}\hfill
1222 This conversion takes the non-linear $R'G'B'$ voltage levels and maps them to
1223 linear light levels produced by the actual output device.
1224 Note that this conversion is only that of the output device, and its inverse is
1225 {\em not} that used by the input device.
1226 Because a dim viewing environment is assumed in most television standards, the
1227 overall gamma between the input and output devices is usually around $1.1$ to
1228 $1.2$, and not a strict $1.0$.
1230 For calibration with actual output devices, the model
1231 \begin{align}
1232 L & =(E'+\Delta)^\gamma
1233 \end{align}
1234 should be used, with $\Delta$ the free parameter and $\gamma$ held fixed to
1235 the value specified in this document.
1236 The conversion function presented here is an idealized version with $\Delta=0$.
1238 \begin{align}
1239 R & = R'^\gamma \\
1240 G & = G'^\gamma \\
1241 B & = B'^\gamma
1242 \end{align}
1244 Parameters: $\gamma$.
1246 \item[$RGB$ to $R'G'B'$ (Input device gamma correction):]
1247 \vspace{\baselineskip}\hfill
1249 %TODO: Tag section as non-normative
1251 This conversion takes linear light levels and maps them to the non-linear
1252 voltage levels produced in the actual input device.
1253 This information is merely informative.
1254 It is not required for building a decoder or for converting between the various
1255 formats and the actual output capabilities of a particular device.
1257 A linear segment is introduced on the low end to reduce noise in dark areas of
1258 the image.
1259 The rest of the scale is adjusted so that the power segment of the curve
1260 intersects the linear segment with the proper slope, and so that it still maps
1261 0 to 0 and 1 to 1.
1263 \begin{align}
1264 R' & = \left\{
1265 \begin{array}{ll}
1266 \alpha R, & 0\le R<\delta \\
1267 (1+\epsilon)R^\beta-\epsilon, & \delta\le R\le1
1268 \end{array}\right. \\
1269 G' & = \left\{
1270 \begin{array}{ll}
1271 \alpha G, & 0\le G<\delta \\
1272 (1+\epsilon)G^\beta-\epsilon, & \delta\le G\le1
1273 \end{array}\right. \\
1274 B' & = \left\{
1275 \begin{array}{ll}
1276 \alpha B, & 0\le B<\delta \\
1277 (1+\epsilon)B^\beta-\epsilon, & \delta\le B\le1
1278 \end{array}\right.
1279 \end{align}
1281 Parameters: $\beta$, $\alpha$, $\delta$, $\epsilon$.
1283 \item[$RGB$ to CIE $XYZ$ (1931):]
1284 \vspace{\baselineskip}\hfill
1286 This conversion maps a device-dependent linear RGB space to the
1287 device-independent linear CIE $XYZ$ space.
1288 The parameters are the CIE chromaticity coordinates of the three
1289 primaries---red, green, and blue---as well as the chromaticity coordinates
1290 of the white point of the device.
1291 This is how hardware manufacturers and standards typically describe a
1292 particular $RGB$ space.
1293 The math required to convert these parameters into a useful transformation
1294 matrix is reproduced below.
1296 \begin{align}
1297 F & =
1298 \left[\begin{array}{ccc}
1299 \frac{x_r}{y_r} & \frac{x_g}{y_g} & \frac{x_b}{y_b} \\
1300 1 & 1 & 1 \\
1301 \frac{1-x_r-y_r}{y_r} & \frac{1-x_g-y_g}{y_g} & \frac{1-x_b-y_b}{y_b}
1302 \end{array}\right] \\
1303 \left[\begin{array}{c}
1304 s_r \\
1305 s_g \\
1307 \end{array}\right] & =
1308 F^{-1}\left[\begin{array}{c}
1309 \frac{x_w}{y_w} \\
1310 1 \\
1311 \frac{1-x_w-y_w}{y_w}
1312 \end{array}\right] \\
1313 \left[\begin{array}{c}
1314 X \\
1315 Y \\
1317 \end{array}\right] & =
1318 F\left[\begin{array}{c}
1319 s_rR \\
1320 s_gG \\
1321 s_bB
1322 \end{array}\right]
1323 \end{align}
1324 Parameters: $x_r,x_g,x_b,x_w, y_r,y_g,y_b,y_w$.
1326 \end{description}
1328 \section{Available Color Spaces}
1329 \label{sec:colorspaces}
1331 These are the color spaces currently defined for use by Theora video.
1332 Each one has a short name, with which it is referred to in this document, and
1333 a more detailed specification of the standards from which its parameters are
1334 derived.
1335 Some standards do not specify all the parameters necessary.
1336 For these unspecified parameters, this document serves as the definition of
1337 what should be used when encoding or decoding Theora video.
1339 \subsection{Rec.~470M (Rec.~ITU-R~BT.470-6 System M/NTSC with
1340 Rec.~ITU-R~BT.601-5)}
1341 \label{sec:470m}
1343 This color space is used by broadcast television and DVDs in much of the
1344 Americas, Japan, Korea, and the Union of Myanmar \cite{rec470}.
1345 This color space may also be used for System M/PAL (Brazil), with an
1346 appropriate conversion supplied by the encoder to compensate for the
1347 different gamma value.
1348 See Section~\ref{sec:470bg} for an appropriate gamma value to assume for M/PAL
1349 input.
1351 In the US, studio monitors are adjusted to a D65 white point
1352 ($x_w,y_w=0.313,0.329$).
1353 In Japan, studio monitors are adjusted to a D white of 9300K
1354 ($x_w,y_w=0.285,0.293$).
1356 Rec.~470 does not specify a digital encoding of the color signals.
1357 For Theora, Rec.~ITU-R~BT.601-5 \cite{rec601} is used, starting from the
1358 $R'G'B'$ signals specified by Rec.~470.
1360 Rec.~470 does not specify an input gamma function.
1361 For Theora, the Rec.~709 \cite{rec709} input function is assumed.
1362 This is the same as that specified by SMPTE 170M \cite{smpte170m}, which claims
1363 to reflect modern practice in the creation of NTSC signals circa 1994.
1365 The parameters for all the color transformations defined in
1366 Section~\ref{sec:color-xforms} are given in Table~\ref{tab:470m}.
1368 \begin{table}[htb]
1369 \begin{align*}
1370 \mathrm{Offset}_{Y,C_b,C_r} & = (16, 128, 128) \\
1371 \mathrm{Excursion}_{Y,C_b,C_r} & = (219, 224, 224) \\
1372 K_r & = 0.299 \\
1373 K_b & = 0.114 \\
1374 \gamma & = 2.2 \\
1375 \beta & = 0.45 \\
1376 \alpha & = 4.5 \\
1377 \delta & = 0.018 \\
1378 \epsilon & = 0.099 \\
1379 x_r,y_r & = 0.67, 0.33 \\
1380 x_g,y_g & = 0.21, 0.71 \\
1381 x_b,y_b & = 0.14, 0.08 \\
1382 \text{(Illuminant C) } x_w,y_w & = 0.310, 0.316 \\
1383 \end{align*}
1384 \caption{Rec.~470M Parameters}
1385 \label{tab:470m}
1386 \end{table}
1388 \subsection{Rec.~470BG (Rec.~ITU-R~BT.470-6 Systems B and G with
1389 Rec.~ITU-R~BT.601-5)}
1390 \label{sec:470bg}
1392 This color space is used by the PAL and SECAM systems in much of the rest of
1393 the world \cite{rec470}
1394 This can be used directly by systems (B, B1, D, D1, G, H, I, K, N)/PAL and (B,
1395 D, G, H, K, K1, L)/SECAM\@.
1397 \begin{verse}
1398 {\bf Note:} the Rec.~470BG chromaticity values are different from those
1399 specified in Rec.~470M\@.
1400 When PAL and SECAM systems were first designed, they were based upon the same
1401 primaries as NTSC\@.
1402 However, as methods of making color picture tubes have changed, the primaries
1403 used have changed as well.
1404 The U.S. recommends using correction circuitry to approximate the existing,
1405 standard NTSC primaries.
1406 Current PAL and SECAM systems have standardized on primaries in accord with
1407 more recent technology.
1408 \end{verse}
1410 Rec.~470 provisionally permits the use of the NTSC chromaticity values (given
1411 in Section~\ref{sec:470m}) with legacy PAL and SECAM equipment.
1412 In Theora, material must be decoded assuming the new PAL and SECAM primaries.
1413 Material intended for display on old legacy devices should be converted by the
1414 decoder.
1416 The official Rec.~470BG specifies a gamma value of $\gamma=2.8$.
1417 However, in practice this value is unrealistically high \cite{Poyn97}.
1418 Rec.~470BG states that the overall system gamma should be approximately
1419 $\gamma\beta=1.2$.
1420 Since most cameras pre-correct with a gamma value of $\beta=0.45$,
1421 this suggests an output device gamma of approximately $\gamma=2.67$.
1422 This is the value recommended for use with PAL systems in Theora.
1424 Rec.~470 does not specify a digital encoding of the color signals.
1425 For Theora, Rec.~ITU-R~BT.601-5 \cite{rec601} is used, starting from the
1426 $R'G'B'$ signals specified by Rec.~470.
1428 Rec.~470 does not specify an input gamma function.
1429 For Theora, the Rec 709 \cite{rec709} input function is assumed.
1431 The parameters for all the color transformations defined in
1432 Section~\ref{sec:color-xforms} are given in Table~\ref{tab:470bg}.
1434 \begin{table}[htb]
1435 \begin{align*}
1436 \mathrm{Offset}_{Y,C_b,C_r} & = (16, 128, 128) \\
1437 \mathrm{Excursion}_{Y,C_b,C_r} & = (219, 224, 224) \\
1438 K_r & = 0.299 \\
1439 K_b & = 0.114 \\
1440 \gamma & = 2.67 \\
1441 \beta & = 0.45 \\
1442 \alpha & = 4.5 \\
1443 \delta & = 0.018 \\
1444 \epsilon & = 0.099 \\
1445 x_r,y_r & = 0.64, 0.33 \\
1446 x_g,y_g & = 0.29, 0.60 \\
1447 x_b,y_b & = 0.15, 0.06 \\
1448 \text{(D65) } x_w,y_w & = 0.313, 0.329 \\
1449 \end{align*}
1450 \caption{Rec.~470BG Parameters}
1451 \label{tab:470bg}
1452 \end{table}
1454 \section{Pixel Formats}
1455 \label{sec:pixfmts}
1457 Theora supports several different pixel formats, each of which uses different
1458 subsampling for the chroma planes relative to the luma plane.
1459 A decoder may need to recover a full resolution chroma plane with samples
1460 co-sited with the luma plane in order to convert to RGB for display or perform
1461 other processing.
1462 Decoders can assume that the chroma signal satisfies the Nyquist-Shannon
1463 sampling theorem.
1464 The ideal low-pass reconstruction filter this implies is not practical, but any
1465 suitable approximation can be used, depending on the available computing
1466 power.
1467 Decoders MAY simply use a box filter, assigning to each luma sample the chroma
1468 sample closest to it.
1469 Encoders would not go wrong in assuming that this will be the most common
1470 approach.
1472 \subsection{4:4:4 Subsampling}
1473 \label{sec:444}
1475 All three color planes are stored at full resolution---each pixel has a $Y'$,
1476 a $C_b$ and a $C_r$ value (see Figure~\ref{fig:pixel444}).
1477 The samples in the different planes are all at co-located sites.
1479 \begin{figure}[htbp]
1480 \begin{center}
1481 \includegraphics{pixel444}
1482 \end{center}
1483 \caption{Pixels encoded 4:4:4}
1484 \label{fig:pixel444}
1485 \end{figure}
1487 % Figure.
1488 %YRB YRB
1492 %YRB YRB
1498 \subsection{4:2:2 Subsampling}
1499 \label{sec:422}
1501 The $C_b$ and $C_r$ planes are stored with half the horizontal resolution of
1502 the $Y'$ plane.
1503 Thus, each of these planes has half the number of horizontal blocks as the luma
1504 plane (see Figure~\ref{fig:pixel422}).
1505 Similarly, they have half the number of horizontal super blocks, rounded up.
1506 Macro blocks are defined across color planes, and so their number does not
1507 change, but each macro block contains half as many chroma blocks.
1509 The chroma samples are vertically aligned with the luma samples, but
1510 horizontally centered between two luma samples.
1511 Thus, each luma sample has a unique closest chroma sample.
1512 A horizontal phase shift may be required to produce signals which use different
1513 horizontal chroma sampling locations for compatibility with different systems.
1515 \begin{figure}[htbp]
1516 \begin{center}
1517 \includegraphics{pixel422}
1518 \end{center}
1519 \caption{Pixels encoded 4:2:2}
1520 \label{fig:pixel422}
1521 \end{figure}
1523 % Figure.
1524 %Y RB Y Y RB Y
1528 %Y RB Y Y RB Y
1533 \subsection{4:2:0 Subsampling}
1534 \label{sec:420}
1536 The $C_b$ and $C_r$ planes are stored with half the horizontal and half the
1537 vertical resolution of the $Y'$ plane.
1538 Thus, each of these planes has half the number of horizontal blocks and half
1539 the number of vertical blocks as the luma plane, for a total of one quarter
1540 the number of blocks (see Figure~\ref{fig:pixel420}).
1541 Similarly, they have half the number of horizontal super blocks and half the
1542 number of vertical super blocks, rounded up.
1543 Macro blocks are defined across color planes, and so their number does not
1544 change, but each macro block contains within it one quarter as many
1545 chroma blocks.
1547 The chroma samples are vertically and horizontally centered between four luma
1548 samples.
1549 Thus, each luma sample has a unique closest chroma sample.
1550 This is the same sub-sampling pattern used with JPEG, MJPEG, and MPEG-1, and
1551 was inherited from VP3.
1552 A horizontal or vertical phase shift may be required to produce signals which
1553 use different chroma sampling locations for compatibility with different
1554 systems.
1556 \begin{figure}[htbp]
1557 \begin{center}
1558 \includegraphics{pixel420}
1559 \end{center}
1560 \caption{Pixels encoded 4:2:0}
1561 \label{fig:pixel420}
1562 \end{figure}
1564 % Figure.
1565 %Y Y Y Y
1567 % RB RB
1569 %Y Y Y Y
1573 %Y Y Y Y
1575 % RB RB
1577 %Y Y Y Y
1582 \subsection{Subsampling and the Picture Region}
1584 Although the frame size must be an integral number of macro blocks, and thus
1585 both the number of pixels and the number of blocks in each direction must be
1586 even, no such requirement is made of the picture region.
1587 Thus, when using subsampled pixel formats, careful attention must be paid to
1588 which chroma samples correspond to which luma samples.
1590 As mentioned above, for each pixel format, there is a unique chroma sample that
1591 is the closest to each luma sample.
1592 When cropping the chroma planes to the picture region, all the chroma samples
1593 corresponding to a luma sample in the cropped picture region must be included.
1594 Thus, when dividing the width or height of the picture region by two to obtain
1595 the size of the subsampled chroma planes, they must be rounded up.
1597 Furthermore, the sampling locations are defined relative to the frame,
1598 {\em not} the picture region.
1599 When using the 4:2:2 and 4:2:0 formats, the locations of chroma samples
1600 relative to the luma samples depends on whether or not the X offset of the
1601 picture region is odd.
1602 If the offset is even, each column of chroma samples corresponds to two columns
1603 of luma samples (see Figure~\ref{fig:pic_even} for an example).
1604 The only exception is if the width is odd, in which case the last column
1605 corresponds to only one column of luma samples (see Figure~\ref{fig:pic_even_odd}).
1606 If the offset is odd, then the first column of chroma samples corresponds to
1607 only one column of luma samples, while the remaining columns each correspond
1608 to two (see Figure~\ref{fig:pic_odd}).
1609 In this case, if the width is even, the last column again corresponds to only
1610 one column of luma samples (see Figure~\ref{fig:pic_odd_even}).
1612 A similar process is followed with the rows of a picture region of odd height
1613 encoded in the 4:2:0 format.
1614 If the Y offset is even, each row of chroma samples corresponds to two rows of
1615 luma samples (see Figure~\ref{fig:pic_even}), except with an odd height, where
1616 the last row corresponds to one row of chroma luna samples only (see
1617 Figure~\ref{fig:pic_even_odd}).
1618 If the offset is odd, then it is the first row of chroma samples which
1619 corresponds to only one row of luma samples, while the remaining rows each
1620 correspond to two (Figure~\ref{fig:pic_odd}), except with an even height,
1621 where the last row also corresponds to one (Figure~\ref{fig:pic_odd_even}).
1623 Encoders should be aware of these differences in the subsampling when using an
1624 even or odd offset.
1625 In the typical case, with an even width and height, where one expects two rows
1626 or columns of luma samples for every row or column of chroma samples, the
1627 encoder must take care to ensure that the offsets used are both even.
1629 \begin{figure}[htbp]
1630 \begin{center}
1631 \includegraphics[width=\textwidth]{pic_even}
1632 \end{center}
1633 \caption{Pixel correspondence between color planes with even picture
1634 offset and even picture size}
1635 \label{fig:pic_even}
1636 \end{figure}
1638 \begin{figure}[htbp]
1639 \begin{center}
1640 \includegraphics[width=\textwidth]{pic_even_odd}
1641 \end{center}
1642 \caption{Pixel correspondence with even picture offset and
1643 odd picture size}
1644 \label{fig:pic_even_odd}
1645 \end{figure}
1647 \begin{figure}[htbp]
1648 \begin{center}
1649 \includegraphics[width=\textwidth]{pic_odd}
1650 \end{center}
1651 \caption{Pixel correspondence with odd picture offset and
1652 odd picture size}
1653 \label{fig:pic_odd}
1654 \end{figure}
1656 \begin{figure}[htbp]
1657 \begin{center}
1658 \includegraphics[width=\textwidth]{pic_odd_even}
1659 \end{center}
1660 \caption{Pixel correspondence with odd picture offset and
1661 even picture size}
1662 \label{fig:pic_odd_even}
1663 \end{figure}
1666 \chapter{Bitpacking Convention}
1667 \label{sec:bitpacking}
1669 \section{Overview}
1671 The Theora codec uses relatively unstructured raw packets containing
1672 binary integer fields of arbitrary width.
1673 Logically, each packet is a bitstream in which bits are written one-by-one by
1674 the encoder and then read one-by-one in the same order by the decoder.
1675 Most current binary storage arrangements group bits into a native storage unit
1676 of eight bits (octets), sixteen bits, thirty-two bits, or less commonly other
1677 fixed sizes.
1678 The Theora bitpacking convention specifies the correct mapping of the logical
1679 packet bitstream into an actual representation in fixed-width units.
1681 \subsection{Octets and Bytes}
1683 In most contemporary architectures, a `byte' is synonymous with an `octect',
1684 that is, eight bits.
1685 For purposes of the bitpacking convention, a byte implies the smallest native
1686 integer storage representation offered by a platform.
1687 Modern file systems invariably offer bytes as the fundamental atom of storage.
1689 The most ubiquitous architectures today consider a `byte' to be an octet.
1690 Note, however, that the Theora bitpacking convention is still well defined for
1691 any native byte size; an implementation can use the native bit-width of a
1692 given storage system.
1693 This document assumes that a byte is one octet for purposes of example only.
1695 \subsection{Words and Byte Order}
1697 A `word' is an integer size that is a grouped multiple of the byte size.
1698 Most architectures consider a word to be a group of two, four, or eight bytes.
1699 Each byte in the word can be ranked by order of `significance', e.g.\ the
1700 significance of the bits in each byte when storing a binary integer in the
1701 word.
1702 Several byte orderings are possible in a word.
1703 The common ones are
1704 \begin{itemize}
1705 \item{Big-endian:}
1706 in which the most significant byte comes first, e.g.\ 3-2-1-0,
1707 \item{Little-endian:}
1708 in which the least significant byte comes first, e.g.\ 0-1-2-3, and
1709 \item{Mixed-endian:}
1710 one of the less-common orderings that cannot be put into the above two
1711 categories, e.g.\ 3-1-2-0 or 0-2-1-3.
1712 \end{itemize}
1714 The Theora bitpacking convention specifies storage and bitstream manipulation
1715 at the byte, not word, level.
1716 Thus host word ordering is of a concern only during optimization, when writing
1717 code that operates on a word of storage at a time rather than a byte.
1718 Logically, bytes are always encoded and decoded in order from byte zero through
1719 byte $n$.
1721 \subsection{Bit Order}
1723 A byte has a well-defined `least significant' bit (LSb), which is the only bit
1724 set when the byte is storing the two's complement integer value $+1$.
1725 A byte's `most significant' bit (MSb) is at the opposite end.
1726 Bits in a byte are numbered from zero at the LSb to $n$ for the MSb, where
1727 $n=7$ in an octet.
1729 \section{Coding Bits into Bytes}
1731 The Theora codec needs to encode arbitrary bit-width integers from zero to 32
1732 bits wide into packets.
1733 These integer fields are not aligned to the boundaries of the byte
1734 representation; the next field is read at the bit position immediately
1735 after the end of the previous field.
1737 The decoder logically unpacks integers by first reading the MSb of a binary
1738 integer from the logical bitstream, followed by the next most significant
1739 bit, etc., until the required number of bits have been read.
1740 When unpacking the bytes into bits, the decoder begins by reading the MSb of
1741 the integer to be read from the most significant unread bit position of the
1742 source byte, followed by the next-most significant bit position of the
1743 destination integer, and so on up to the requested number of bits.
1744 Note that this differs from the Vorbis I codec, which
1745 begins decoding with the LSb of the source integer, reading it from the
1746 LSb of the source byte.
1747 When all the bits of the current source byte are read, decoding continues with
1748 the MSb of the next byte.
1749 Any unfilled bits in the last byte of the packet MUST be cleared to zero by the
1750 encoder.
1752 \subsection{Signedness}
1754 The binary integers decoded by the above process may be either signed or
1755 unsigned.
1756 This varies from integer to integer, and this specification
1757 indicates how each value should be interpreted as it is read.
1758 That is, depending on context, the three bit binary pattern \bin{111} can be
1759 taken to represent either `$7$' as an unsigned integer or `$-1$' as a signed,
1760 two's complement integer.
1762 \subsection{Encoding Example}
1764 The following example shows the state of an (8-bit) byte stream after several
1765 binary integers are encoded, including the location of the put pointer for the
1766 next bit to write to and the total length of the stream in bytes.
1768 Encode the 4 bit unsigned integer value `12' (\bin{1100}) into an empty byte
1769 stream.
1771 \begin{tabular}{r|ccccccccl}
1772 \multicolumn{1}{r}{}& &&&&$\downarrow$&&&& \\
1773 & 7 & 6 & 5 & 4 & 3 & 2 & 1 & 0 & \\\cline{1-9}
1774 byte 0 & \textbf{1} & \textbf{1} & \textbf{0} & \textbf{0} &
1775 0 & 0 & 0 & 0 & $\leftarrow$ \\
1776 byte 1 & 0 & 0 & 0 & 0 & 0 & 0 & 0 & 0 & \\
1777 byte 2 & 0 & 0 & 0 & 0 & 0 & 0 & 0 & 0 & \\
1778 byte 3 & 0 & 0 & 0 & 0 & 0 & 0 & 0 & 0 & \\
1779 \multicolumn{1}{c|}{$\vdots$}&\multicolumn{8}{c}{$\vdots$}& \\
1780 byte $n$ & 0 & 0 & 0 & 0 & 0 & 0 & 0 & 0 &
1781 byte stream length: 1 byte
1782 \end{tabular}
1783 \vspace{\baselineskip}
1785 Continue by encoding the 3 bit signed integer value `-1' (\bin{111}).
1787 \begin{tabular}{r|ccccccccl}
1788 \multicolumn{1}{r}{} &&&&&&&&$\downarrow$& \\
1789 & 7 & 6 & 5 & 4 & 3 & 2 & 1 & 0 & \\\cline{1-9}
1790 byte 0 & \textbf{1} & \textbf{1} & \textbf{0} & \textbf{0} &
1791 \textbf{1} & \textbf{1} & \textbf{1} & 0 & $\leftarrow$ \\
1792 byte 1 & 0 & 0 & 0 & 0 & 0 & 0 & 0 & 0 & \\
1793 byte 2 & 0 & 0 & 0 & 0 & 0 & 0 & 0 & 0 & \\
1794 byte 3 & 0 & 0 & 0 & 0 & 0 & 0 & 0 & 0 & \\
1795 \multicolumn{1}{c|}{$\vdots$}&\multicolumn{8}{c}{$\vdots$}& \\
1796 byte $n$ & 0 & 0 & 0 & 0 & 0 & 0 & 0 & 0 &
1797 byte stream length: 1 byte
1798 \end{tabular}
1799 \vspace{\baselineskip}
1801 Continue by encoding the 7 bit integer value `17' (\bin{0010001}).
1803 \begin{tabular}{r|ccccccccl}
1804 \multicolumn{1}{r}{} &&&&&&&$\downarrow$&& \\
1805 & 7 & 6 & 5 & 4 & 3 & 2 & 1 & 0 & \\\cline{1-9}
1806 byte 0 & \textbf{1} & \textbf{1} & \textbf{0} & \textbf{0} &
1807 \textbf{1} & \textbf{1} & \textbf{1} & \textbf{0} & \\
1808 byte 1 & \textbf{0} & \textbf{1} & \textbf{0} & \textbf{0} &
1809 \textbf{0} & \textbf{1} & 0 & 0 & $\leftarrow$ \\
1810 byte 2 & 0 & 0 & 0 & 0 & 0 & 0 & 0 & 0 & \\
1811 byte 3 & 0 & 0 & 0 & 0 & 0 & 0 & 0 & 0 & \\
1812 \multicolumn{1}{c|}{$\vdots$}&\multicolumn{8}{c}{$\vdots$}& \\
1813 byte $n$ & 0 & 0 & 0 & 0 & 0 & 0 & 0 & 0 &
1814 byte stream length: 2 bytes
1815 \end{tabular}
1816 \vspace{\baselineskip}
1818 Continue by encoding the 13 bit integer value `6969' (\bin{11011\ 00111001}).
1820 \begin{tabular}{r|ccccccccl}
1821 \multicolumn{1}{r}{} &&&&$\downarrow$&&&&& \\
1822 & 7 & 6 & 5 & 4 & 3 & 2 & 1 & 0 & \\\cline{1-9}
1823 byte 0 & \textbf{1} & \textbf{1} & \textbf{0} & \textbf{0} &
1824 \textbf{1} & \textbf{1} & \textbf{1} & \textbf{0} & \\
1825 byte 1 & \textbf{0} & \textbf{1} & \textbf{0} & \textbf{0} &
1826 \textbf{0} & \textbf{1} & \textbf{1} & \textbf{1} & \\
1827 byte 2 & \textbf{0} & \textbf{1} & \textbf{1} & \textbf{0} &
1828 \textbf{0} & \textbf{1} & \textbf{1} & \textbf{1} & \\
1829 byte 3 & \textbf{0} & \textbf{0} & \textbf{1} &
1830 0 & 0 & 0 & 0 & 0 & $\leftarrow$ \\
1831 \multicolumn{1}{c|}{$\vdots$}&\multicolumn{8}{c}{$\vdots$}& \\
1832 byte $n$ & 0 & 0 & 0 & 0 & 0 & 0 & 0 & 0 &
1833 byte stream length: 4 bytes
1834 \end{tabular}
1835 \vspace{\baselineskip}
1837 \subsection{Decoding Example}
1839 The following example shows the state of the (8-bit) byte stream encoded in the
1840 previous example after several binary integers are decoded, including the
1841 location of the get pointer for the next bit to read.
1843 Read a two bit unsigned integer from the example encoded above.
1845 \begin{tabular}{r|ccccccccl}
1846 \multicolumn{1}{r}{} &&&$\downarrow$&&&&&& \\
1847 & 7 & 6 & 5 & 4 & 3 & 2 & 1 & 0 & \\\cline{1-9}
1848 byte 0 & \textbf{1} & \textbf{1} & 0 & 0 & 1 & 1 & 1 & 0 & $\leftarrow$ \\
1849 byte 1 & 0 & 1 & 0 & 0 & 0 & 1 & 1 & 1 & \\
1850 byte 2 & 0 & 1 & 1 & 0 & 0 & 1 & 1 & 1 & \\
1851 byte 3 & 0 & 0 & 1 & 0 & 0 & 0 & 0 & 0 &
1852 byte stream length: 4 bytes
1853 \end{tabular}
1854 \vspace{\baselineskip}
1856 Value read: 3 (\bin{11}).
1858 Read another two bit unsigned integer from the example encoded above.
1860 \begin{tabular}{r|ccccccccl}
1861 \multicolumn{1}{r}{} &&&&&$\downarrow$&&&& \\
1862 & 7 & 6 & 5 & 4 & 3 & 2 & 1 & 0 & \\\cline{1-9}
1863 byte 0 & \textbf{1} & \textbf{1} & \textbf{0} & \textbf{0} &
1864 1 & 1 & 1 & 0 & $\leftarrow$ \\
1865 byte 1 & 0 & 1 & 0 & 0 & 0 & 1 & 1 & 1 & \\
1866 byte 2 & 0 & 1 & 1 & 0 & 0 & 1 & 1 & 1 & \\
1867 byte 3 & 0 & 0 & 1 & 0 & 0 & 0 & 0 & 0 &
1868 byte stream length: 4 bytes
1869 \end{tabular}
1870 \vspace{\baselineskip}
1872 Value read: 0 (\bin{00}).
1874 Two things are worth noting here.
1875 \begin{itemize}
1876 \item
1877 Although these four bits were originally written as a single four-bit integer,
1878 reading some other combination of bit-widths from the bitstream is well
1879 defined.
1880 No artificial alignment boundaries are maintained in the bitstream.
1881 \item
1882 The first value is the integer `$3$' only because the context stated we were
1883 reading an unsigned integer.
1884 Had the context stated we were reading a signed integer, the returned value
1885 would have been the integer `$-1$'.
1886 \end{itemize}
1888 \subsection{End-of-Packet Alignment}
1890 The typical use of bitpacking is to produce many independent byte-aligned
1891 packets which are embedded into a larger byte-aligned container structure,
1892 such as an Ogg transport bitstream.
1893 Externally, each bitstream encoded as a byte stream MUST begin and end on a
1894 byte boundary.
1895 Often, the encoded packet bitstream is not an integer number of bytes, and so
1896 there is unused space in the last byte of a packet.
1898 %r: I think the generality here is necessary to be consistent with our assertions
1899 %r: elsewhere about being independent of transport and byte width
1900 When a Theora encoder produces packets for embedding in a byte-aligned
1901 container, unused space in the last byte of a packet is always zeroed during
1902 the encoding process.
1903 Thus, should this unused space be read, it will return binary zeroes.
1904 There is no marker pattern or stuffing bits that will allow the decoder to
1905 obtain the exact size, in bits, of the original bitstream.
1906 This knowledge is not required for decoding.
1908 Attempting to read past the end of an encoded packet results in an
1909 `end-of-packet' condition.
1910 Any further read operations after an `end-of-packet' condition shall also
1911 return `end-of-packet'.
1912 Unlike Vorbis, Theora does not use truncated packets as a normal mode of
1913 operation.
1914 Therefore if a decoder encounters the `end-of-packet' condition during normal
1915 decoding, it may attempt to use the bits that were read to recover as much of
1916 encoded data as possible, signal a warning or error, or both.
1918 \subsection{Reading Zero Bit Integers}
1920 Reading a zero bit integer returns the value `$0$' and does not increment
1921 the stream pointer.
1922 Reading to the end of the packet, but not past the end, so that an
1923 `end-of-packet' condition is not triggered, and then reading a zero bit
1924 integer shall succeed, returning `$0$', and not trigger an `end-of-packet'
1925 condition.
1926 Reading a zero bit integer after a previous read sets the `end-of-packet'
1927 condition shall fail, also returning `end-of-packet'.
1929 \chapter{Bitstream Headers}
1930 \label{sec:headers}
1932 A Theora bitstream begins with three header packets.
1933 The header packets are, in order, the identification header, the comment
1934 header, and the setup header.
1935 All are required for decode compliance.
1936 An end-of-packet condition encountered while decoding the identification or
1937 setup header packets renders the stream undecodable.
1938 An end-of-packet condition encountered while decode the comment header is a
1939 non-fatal error condition, and MAY be ignored by a decoder.
1941 \paragraph{VP3 Compatibility}
1943 VP3 relies on the headers provided by its container, usually either AVI or
1944 Quicktime.
1945 As such, several parameters available in these headers are not available to VP3
1946 streams.
1947 These are indicated as they appear in the sections below.
1949 \section{Common Header Decode}
1950 \label{sub:common-header}
1952 \begin{figure}[Htbp]
1953 \begin{center}
1954 \begin{verbatim}
1955 0 1 2 3
1956 0 1 2 3 4 5 6 7 8 9 0 1 2 3 4 5 6 7 8 9 0 1 2 3 4 5 6 7 8 9 0 1
1957 +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
1958 | header type | `t' | `h' | `e' |
1959 +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
1960 | `o' | `r' | `a' | data... |
1961 +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
1962 | ... header-specific data ... |
1963 | ... |
1964 +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
1965 \end{verbatim}
1966 \end{center}
1967 \caption{Common Header Packet Layout}
1968 \label{fig:commonheader}
1969 \end{figure}
1972 \paragraph{Input parameters:} None.
1974 \paragraph{Output parameters:}\hfill\\*
1975 \begin{tabularx}{\textwidth}{@{}llrcX@{}}\toprule
1976 \multicolumn{1}{c}{Name} &
1977 \multicolumn{1}{c}{Type} &
1978 \multicolumn{1}{p{30pt}}{\centering Size (bits)} &
1979 \multicolumn{1}{c}{Signed?} &
1980 \multicolumn{1}{c}{Description and restrictions} \\\midrule\endhead
1981 \bitvar{HEADERTYPE} & Integer & 8 & No & The type of the header being
1982 decoded. \\
1983 \bottomrule\end{tabularx}
1985 \paragraph{Variables used:} None.
1986 \medskip
1988 Each header packet begins with the same header fields, which are decoded as
1989 follows:
1991 \begin{enumerate}
1992 \item
1993 Read an 8-bit unsigned integer as \bitvar{HEADERTYPE}.
1994 If the most significant bit of this integer is not set, then stop.
1995 This is not a header packet.
1996 \item
1997 Read 6 8-bit unsigned integers.
1998 If these do not have the values \hex{74}, \hex{68}, \hex{65}, \hex{6F},
1999 \hex{72}, and \hex{61}, respectively, then stop.
2000 This stream is not decodable by this specification.
2001 These values correspond to the ASCII values of the characters `t', `h', `e',
2002 `o', `r', and `a'.
2003 \end{enumerate}
2005 Decode continues according to \bitvar{HEADERTYPE}.
2006 The identification header is type \hex{80}, the comment header is type
2007 \hex{81}, and the setup header is type \hex{82}.
2008 These packets must occur in the order: identification, comment, setup.
2009 %r: I clarified the initial-bit scheme here
2010 %TBT: Dashes let the reader know they'll have to pick up the rest of the
2011 %TBT: sentence after the explanatory phrase.
2012 %TBT: Otherwise it just sounds like the bit must exist.
2013 All header packets have the most significant bit of the type
2014 field---which is the initial bit in the packet---set.
2015 This distinguishes them from video data packets in which the first bit
2016 is unset.
2017 % extra header packets are a feature Dan argued for way back when for
2018 % backward-compatible extensions (and icc colourspace for example)
2019 % I think it's reasonable
2020 %TBT: You can always just stick more stuff in the setup header.
2021 Packets with other header types (\hex{83}--\hex{FF}) are reserved and MUST be
2022 ignored.
2024 \section{Identification Header Decode}
2025 \label{sec:idheader}
2027 \begin{figure}[Htbp]
2028 \begin{center}
2029 \begin{verbatim}
2030 0 1 2 3
2031 0 1 2 3 4 5 6 7 8 9 0 1 2 3 4 5 6 7 8 9 0 1 2 3 4 5 6 7 8 9 0 1
2032 +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
2033 | 0x80 | `t' | `h' | `e' |
2034 +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
2035 | `o' | `r' | `a' | VMAJ |
2036 +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
2037 | VMIN | VREV | FMBW |
2038 +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
2039 | FMBH | PICW... |
2040 +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
2041 | ...PICW | PICH |
2042 +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
2043 | PICX | PICY | FRN... |
2044 +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
2045 | ...FRN | FRD... |
2046 +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
2047 | ...FRD | PARN... |
2048 +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
2049 | ...PARN | PARD |
2050 +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
2051 | CS | NOMBR |
2052 +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
2053 | QUAL | KFGSHIFT| PF| Res |
2054 +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
2055 \end{verbatim}
2056 \end{center}
2057 \caption{Identification Header Packet}
2058 \label{fig:idheader}
2059 \end{figure}
2061 \paragraph{Input parameters:} None.
2063 \paragraph{Output parameters:}\hfill\\*
2064 \begin{tabularx}{\textwidth}{@{}llrcX@{}}\toprule
2065 \multicolumn{1}{c}{Name} &
2066 \multicolumn{1}{c}{Type} &
2067 \multicolumn{1}{p{30pt}}{\centering Size (bits)} &
2068 \multicolumn{1}{c}{Signed?} &
2069 \multicolumn{1}{c}{Description and restrictions} \\\midrule\endhead
2070 \bitvar{VMAJ} & Integer & 8 & No & The major version number. \\
2071 \bitvar{VMIN} & Integer & 8 & No & The minor version number. \\
2072 \bitvar{VREV} & Integer & 8 & No & The version revision number. \\
2073 \bitvar{FMBW} & Integer & 16 & No & The width of the frame in macro
2074 blocks. \\
2075 \bitvar{FMBH} & Integer & 16 & No & The height of the frame in macro
2076 blocks. \\
2077 \bitvar{NSBS} & Integer & 32 & No & The total number of super blocks in a
2078 frame. \\
2079 \bitvar{NBS} & Integer & 36 & No & The total number of blocks in a
2080 frame. \\
2081 \bitvar{NMBS} & Integer & 32 & No & The total number of macro blocks in a
2082 frame. \\
2083 \bitvar{PICW} & Integer & 20 & No & The width of the picture region in
2084 pixels. \\
2085 \bitvar{PICH} & Integer & 20 & No & The height of the picture region in
2086 pixels. \\
2087 \bitvar{PICX} & Integer & 8 & No & The X offset of the picture region in
2088 pixels. \\
2089 \bitvar{PICY} & Integer & 8 & No & The Y offset of the picture region in
2090 pixels. \\
2091 \bitvar{FRN} & Integer & 32 & No & The frame-rate numerator. \\
2092 \bitvar{FRD} & Integer & 32 & No & The frame-rate denominator. \\
2093 \bitvar{PARN} & Integer & 24 & No & The pixel aspect-ratio numerator. \\
2094 \bitvar{PARD} & Integer & 24 & No & The pixel aspect-ratio denominator. \\
2095 \bitvar{CS} & Integer & 8 & No & The color space. \\
2096 \bitvar{PF} & Integer & 2 & No & The pixel format. \\
2097 \bitvar{NOMBR} & Integer & 24 & No & The nominal bitrate of the stream, in
2098 bits per second. \\
2099 \bitvar{QUAL} & Integer & 6 & No & The quality hint. \\
2100 \bitvar{KFGSHIFT} & Integer & 5 & No & The amount to shift the key frame
2101 number by in the granule position. \\
2102 \bottomrule\end{tabularx}
2104 \paragraph{Variables used:} None.
2105 \medskip
2107 The identification header is a short header with only a few fields used to
2108 declare the stream definitively as Theora and provide detailed information
2109 about the format of the fully decoded video data.
2110 The identification header is decoded as follows:
2112 \begin{enumerate}
2113 \item
2114 Decode the common header fields according to the procedure described in
2115 Section~\ref{sub:common-header}.
2116 If \bitvar{HEADERTYPE} returned by this procedure is not \hex{80}, then stop.
2117 This packet is not the identification header.
2118 \item
2119 Read an 8-bit unsigned integer as \bitvar{VMAJ}.
2120 If \bitvar{VMAJ} is not $3$, then stop.
2121 This stream is not decodable according to this specification.
2122 \item
2123 Read an 8-bit unsigned integer as \bitvar{VMIN}.
2124 If \bitvar{VMIN} is not $2$, then stop.
2125 This stream is not decodable according to this specification.
2126 \item
2127 Read an 8-bit unsigned integer as \bitvar{VREV}.
2128 If \bitvar{VREV} is greater than $1$, then this stream
2129 may contain optional features or interpretational changes
2130 documented in a future version of this specification.
2131 Regardless of the value of \bitvar{VREV}, the stream is decodable
2132 according to this specification.
2133 \item
2134 Read a 16-bit unsigned integer as \bitvar{FMBW}.
2135 This MUST be greater than zero.
2136 This specifies the width of the coded frame in macro blocks.
2137 The actual width of the frame in pixels is $\bitvar{FMBW}*16$.
2138 \item
2139 Read a 16-bit unsigned integer as \bitvar{FMBH}.
2140 This MUST be greater than zero.
2141 This specifies the height of the coded frame in macro blocks.
2142 The actual height of the frame in pixels is $\bitvar{FMBH}*16$.
2143 \item
2144 Read a 24-bit unsigned integer as \bitvar{PICW}.
2145 This MUST be no greater than $(\bitvar{FMBW}*16)$.
2146 Note that 24 bits are read, even though only 20 bits are sufficient to specify
2147 any value of the picture width.
2148 This is done to preserve octet alignment in this header, to allow for a
2149 simplified parser implementation.
2150 \item
2151 Read a 24-bit unsigned integer as \bitvar{PICH}.
2152 This MUST be no greater than $(\bitvar{FMBH}*16)$.
2153 Together with \bitvar{PICW}, this specifies the size of the displayable picture
2154 region within the coded frame.
2155 See Figure~\ref{fig:pic-frame}.
2156 Again, 24 bits are read instead of 20.
2157 \item
2158 Read an 8-bit unsigned integer as \bitvar{PICX}.
2159 This MUST be no greater than $(\bitvar{FMBW}*16-\bitvar{PICX})$.
2160 \item
2161 Read an 8-bit unsigned integer as \bitvar{PICY}.
2162 This MUST be no greater than $(\bitvar{FMBH}*16-\bitvar{PICY})$.
2163 Together with \bitvar{PICX}, this specifies the location of the lower-left
2164 corner of the displayable picture region.
2165 See Figure~\ref{fig:pic-frame}.
2166 \item
2167 Read a 32-bit unsigned integer as \bitvar{FRN}.
2168 This MUST be greater than zero.
2169 \item
2170 Read a 32-bit unsigned integer as \bitvar{FRD}.
2171 This MUST be greater than zero.
2172 Theora is a fixed-frame rate video codec.
2173 Frames are sampled at the constant rate of $\frac{\bitvar{FRN}}{\bitvar{FRD}}$
2174 frames per second.
2175 The presentation time of the first frame is at zero seconds.
2176 No mechanism is provided to specify a non-zero offset for the initial
2177 frame.
2178 \item
2179 Read a 24-bit unsigned integer as \bitvar{PARN}.
2180 \item
2181 Read a 24-bit unsigned integer as \bitvar{PARD}.
2182 Together with \bitvar{PARN}, these specify the aspect ratio of the pixels
2183 within a frame, defined as the ratio of the physical width of a pixel to its
2184 physical height.
2185 This is given by the ratio $\bitvar{PARN}:\bitvar{PARD}$.
2186 If either of these fields are zero, this indicates that pixel aspect ratio
2187 information was not available to the encoder.
2188 In this case it MAY be specified by the application via an external means, or
2189 a default value of $1:1$ MAY be used.
2190 \item
2191 Read an 8-bit unsigned integer as \bitvar{CS}.
2192 This is a value from an enumerated list of the available color spaces, given in
2193 Table~\ref{tab:colorspaces}.
2194 The `Undefined' value indicates that color space information was not available
2195 to the encoder.
2196 It MAY be specified by the application via an external means.
2197 If a reserved value is given, a decoder MAY refuse to decode the stream.
2198 \begin{table}[htbp]
2199 \begin{center}
2200 \begin{tabular*}{215pt}{cl@{\extracolsep{\fill}}c}\toprule
2201 Value & Color Space \\\midrule
2202 $0$ & Undefined. \\
2203 $1$ & Rec.~470M (see Section~\ref{sec:470m}). \\
2204 $2$ & Rec.~470BG (see Section~\ref{sec:470bg}). \\
2205 $3$ & Reserved. \\
2206 $\vdots$ & \\
2207 $255$ & \\
2208 \bottomrule\end{tabular*}
2209 \end{center}
2210 \caption{Enumerated List of Color Spaces}
2211 \label{tab:colorspaces}
2212 \end{table}
2213 \item
2214 Read a 24-bit unsigned integer as \bitvar{NOMBR}.
2215 The \bitvar{NOMBR} field is used only as a hint.
2216 For pure VBR streams, this value may be considerably off.
2217 The field MAY be set to zero to indicate that the encoder did not care to
2218 speculate.
2219 %TODO: units?
2220 \item
2221 Read a 6-bit unsigned integer as \bitvar{QUAL}.
2222 This value is used to provide a hint as to the relative quality of the stream
2223 when compared to others produced by the same encoder.
2224 Larger values indicate higher quality.
2225 This can be used, for example, to select among several streams containing the
2226 same material encoded with different settings.
2227 \item
2228 Read a 5-bit unsigned integer as \bitvar{KFGSHIFT}.
2229 The \bitvar{KFGSHIFT} is used to partition the granule position associated with
2230 each packet into two different parts.
2231 The frame number of the last key frame, starting from zero, is stored in the
2232 upper $64-\bitvar{KFGSHIFT}$ bits, while the lower \bitvar{KFGSHIFT} bits
2233 contain the number of frames since the last keyframe.
2234 Complete details on the granule position mapping are specified in Section~REF.
2235 \item
2236 Read a 2-bit unsigned integer as \bitvar{PF}.
2237 The \bitvar{PF} field contains a value from an enumerated list of the available
2238 pixel formats, given in Table~\ref{tab:pixel-formats}.
2239 If the reserved value $1$ is given, stop.
2240 This stream is not decodable according to this specification.
2242 \begin{table}[htbp]
2243 \begin{center}
2244 \begin{tabular*}{215pt}{cl@{\extracolsep{\fill}}c}\toprule
2245 Value & Pixel Format \\\midrule
2246 $0$ & 4:2:0 (see Section~\ref{sec:420}). \\
2247 $1$ & Reserved. \\
2248 $2$ & 4:2:2 (see Section~\ref{sec:422}). \\
2249 $3$ & 4:4:4 (see Section~\ref{sec:444}). \\
2250 \bottomrule\end{tabular*}
2251 \end{center}
2252 \caption{Enumerated List of Pixel Formats}
2253 \label{tab:pixel-formats}
2254 \end{table}
2256 \item
2257 Read a 3-bit unsigned integer.
2258 These bits are reserved.
2259 If this value is not zero, then stop.
2260 This stream is not decodable according to this specification.
2261 \item
2262 Assign \bitvar{NSBS} a value according to \bitvar{PF}, as given by
2263 Table~\ref{tab:nsbs-for-pf}.
2265 \begin{table}[bt]
2266 \begin{center}
2267 \begin{tabular}{cc}\toprule
2268 \bitvar{PF} & \bitvar{NSBS} \\\midrule
2269 $0$ & $\begin{aligned}
2270 &((\bitvar{FMBW}+1)//2)*((\bitvar{FMBH}+1)//2)\\
2271 & +2*((\bitvar{FMBW}+3)//4)*((\bitvar{FMBH}+3)//4)
2272 \end{aligned}$ \\\midrule
2273 $2$ & $\begin{aligned}
2274 &((\bitvar{FMBW}+1)//2)*((\bitvar{FMBH}+1)//2)\\
2275 & +2*((\bitvar{FMBW}+3)//4)*((\bitvar{FMBH}+1)//2)
2276 \end{aligned}$ \\\midrule
2277 $3$ & $3*((\bitvar{FMBW}+1)//2)*((\bitvar{FMBH}+1)//2)$ \\
2278 \bottomrule\end{tabular}
2279 \end{center}
2280 \caption{Number of Super Blocks for each Pixel Format}
2281 \label{tab:nsbs-for-pf}
2282 \end{table}
2284 \item
2285 Assign \bitvar{NBS} a value according to \bitvar{PF}, as given by
2286 Table~\ref{tab:nbs-for-pf}.
2288 \begin{table}[tb]
2289 \begin{center}
2290 \begin{tabular}{cc}\toprule
2291 \bitvar{PF} & \bitvar{NBS} \\\midrule
2292 $0$ & $6*\bitvar{FMBW}*\bitvar{FMBH}$ \\\midrule
2293 $2$ & $8*\bitvar{FMBW}*\bitvar{FMBH}$ \\\midrule
2294 $3$ & $12*\bitvar{FMBW}*\bitvar{FMBH}$ \\
2295 \bottomrule\end{tabular}
2296 \end{center}
2297 \caption{Number of Blocks for each Pixel Format}
2298 \label{tab:nbs-for-pf}
2299 \end{table}
2301 \item
2302 Assign \bitvar{NMBS} the value $(\bitvar{FMBW}*\bitvar{FMBH})$.
2304 \end{enumerate}
2306 \paragraph{VP3 Compatibility}
2308 VP3 does not correctly handle frame sizes that are not a multiple of 16.
2309 Thus, \bitvar{PICW} and \bitvar{PICH} should be set to the frame width and
2310 height in pixels, respectively, and \bitvar{PICX} and \bitvar{PICY} should be
2311 set to zero.
2312 VP3 headers do not specify a color space.
2313 VP3 only supports the 4:2:0 pixel format.
2315 \section{Comment Header}
2316 \label{sec:commentheader}
2318 The Theora comment header is the second of three header packets that begin a
2319 Theora stream.
2320 It is meant for short text comments, not aribtrary metadata; arbitrary metadata
2321 belongs in a separate logical stream that provides greater structure and
2322 machine parseability.
2324 %r: I tried to morph this a little more in the direction of our
2325 % application space
2326 The comment field is meant to be used much like someone jotting a quick note on
2327 the label of a video.
2328 It should be a little information to remember the disc or tape by and explain it to
2329 others; a short, to-the-point text note that can be more than a couple words,
2330 but isn't going to be more than a short paragraph.
2331 The essentials, in other words, whatever they turn out to be, e.g.:
2333 %TODO: Example
2335 The comment header is stored as a logical list of eight-bit clean vectors; the
2336 number of vectors is bounded at $2^{32}-1$ and the length of each vector is
2337 limited to $2^{32}-1$ bytes.
2338 The vector length is encoded; the vector contents themselves are not null
2339 terminated.
2340 In addition to the vector list, there is a single vector for a vendor name,
2341 also eight-bit clean with a length encoded in 32 bits.
2342 %TODO: The 1.0 release of libtheora sets the vendor string to ...
2344 \subsection{Comment Length Decode}
2345 \label{sub:comment-len}
2347 \begin{figure}
2348 \begin{center}
2349 \begin{tabular}{ | c | c | }
2350 \hline
2351 4 byte length &
2352 UTF-8 encoded string ...\\
2353 \hline
2354 \end{tabular}
2355 \end{center}
2356 \caption{Length encoded string layout}
2357 \label{fig:comment-len}
2358 \end{figure}
2360 \paragraph{Input parameters:} None.
2362 \paragraph{Output parameters:}\hfill\\*
2363 \begin{tabularx}{\textwidth}{@{}llrcX@{}}\toprule
2364 \multicolumn{1}{c}{Name} &
2365 \multicolumn{1}{c}{Type} &
2366 \multicolumn{1}{p{30pt}}{\centering Size (bits)} &
2367 \multicolumn{1}{c}{Signed?} &
2368 \multicolumn{1}{c}{Description and restrictions} \\\midrule\endhead
2369 \bitvar{LEN} & Integer & 32 & No & A single 32-bit length value. \\
2370 \bottomrule\end{tabularx}
2372 \paragraph{Variables used:}\hfill\\*
2373 \begin{tabularx}{\textwidth}{@{}llrcX@{}}\toprule
2374 \multicolumn{1}{c}{Name} &
2375 \multicolumn{1}{c}{Type} &
2376 \multicolumn{1}{p{30pt}}{\centering Size (bits)} &
2377 \multicolumn{1}{c}{Signed?} &
2378 \multicolumn{1}{c}{Description and restrictions} \\\midrule\endhead
2379 \locvar{LEN0} & Integer & 8 & No & The first octet of the string length. \\
2380 \locvar{LEN1} & Integer & 8 & No & The second octet of the string length. \\
2381 \locvar{LEN2} & Integer & 8 & No & The third octet of the string length. \\
2382 \locvar{LEN3} & Integer & 8 & No & The fourth octet of the string
2383 length. \\
2384 \bottomrule\end{tabularx}
2385 \medskip
2387 A single comment vector is decoded as follows:
2389 \begin{enumerate}
2390 \item
2391 Read an 8-bit unsigned integer as \locvar{LEN0}.
2392 \item
2393 Read an 8-bit unsigned integer as \locvar{LEN1}.
2394 \item
2395 Read an 8-bit unsigned integer as \locvar{LEN2}.
2396 \item
2397 Read an 8-bit unsigned integer as \locvar{LEN3}.
2398 \item
2399 Assign \bitvar{LEN} the value $(\locvar{LEN0}+(\locvar{LEN1}<<8)+
2400 (\locvar{LEN2}<<16)+(\locvar{LEN3}<<24))$.
2401 This construction is used so that on platforms with 8-bit bytes, the memory
2402 organization of the comment header is identical with that of Vorbis I,
2403 allowing for common parsing code despite the different bit packing
2404 conventions.
2405 \end{enumerate}
2407 \subsection{Comment Header Decode}
2409 \begin{figure}
2410 \begin{center}
2411 \begin{tabular}{ | c | }
2412 \hline
2413 vendor string \\ \hline
2414 number of comments \\ \hline
2415 comment string \\ \hline
2416 comment string \\ \hline
2417 ... \\
2418 \hline
2419 \end{tabular}
2420 \end{center}
2421 \caption{Comment Header Layout}
2422 \label{fig:commentheader}
2423 \end{figure}
2425 \paragraph{Input parameters:} None.
2427 \paragraph{Output parameters:}\hfill\\*
2428 \begin{tabularx}{\textwidth}{@{}llrcX@{}}\toprule
2429 \multicolumn{1}{c}{Name} &
2430 \multicolumn{1}{c}{Type} &
2431 \multicolumn{1}{p{30pt}}{\centering Size (bits)} &
2432 \multicolumn{1}{c}{Signed?} &
2433 \multicolumn{1}{c}{Description and restrictions} \\\midrule\endhead
2434 \bitvar{VENDOR} & \multicolumn{3}{l}{String} & The vendor string. \\
2435 \bitvar{NCOMMENTS} & Integer & 32 & No & The number of user
2436 comments. \\
2437 \bitvar{COMMENTS} & \multicolumn{3}{l}{String Array} & A list of
2438 \bitvar{NCOMMENTS} user comment values. \\
2439 \bottomrule\end{tabularx}
2441 \paragraph{Variables used:}\hfill\\*
2442 \begin{tabularx}{\textwidth}{@{}llrcX@{}}\toprule
2443 \multicolumn{1}{c}{Name} &
2444 \multicolumn{1}{c}{Type} &
2445 \multicolumn{1}{p{30pt}}{\centering Size (bits)} &
2446 \multicolumn{1}{c}{Signed?} &
2447 \multicolumn{1}{c}{Description and restrictions} \\\midrule\endhead
2448 \locvar{\ci} & Integer & 32 & No & The index of the current user
2449 comment. \\
2450 \bottomrule\end{tabularx}
2451 \medskip
2453 The complete comment header is decoded as follows:
2455 \begin{enumerate}
2456 \item
2457 Decode the common header fields according to the procedure described in
2458 Section~\ref{sub:common-header}.
2459 If \bitvar{HEADERTYPE} returned by this procedure is not \hex{81}, then stop.
2460 This packet is not the comment header.
2461 \item
2462 Decode the length of the vendor string using the procedure given in
2463 Section~\ref{sub:comment-len} into \bitvar{LEN}.
2464 \item
2465 Read \bitvar{LEN} 8-bit unsigned integers.
2466 \item
2467 Set the string \bitvar{VENDOR} to the contents of these octets.
2468 \item
2469 Decode the number of user comments using the procedure given in
2470 Section~\ref{sub:comment-len} into \bitvar{LEN}.
2471 \item
2472 Assign \bitvar{NCOMMENTS} the value stored in \bitvar{LEN}.
2473 \item
2474 For each consecutive value of \locvar{\ci} from $0$ to
2475 $(\bitvar{NCOMMENTS}-1)$, inclusive:
2476 \begin{enumerate}
2477 \item
2478 Decode the length of the current user comment using the procedure given in
2479 Section~\ref{sub:comment-len} into \bitvar{LEN}.
2480 \item
2481 Read \bitvar{LEN} 8-bit unsigned integers.
2482 \item
2483 Set the string $\bitvar{COMMENTS}[\locvar{\ci}]$ to the contents of these
2484 octets.
2485 \end{enumerate}
2486 \end{enumerate}
2488 The comment header comprises the entirety of the second header packet.
2489 Unlike the first header packet, it is not generally the only packet on the
2490 second page and may span multiple pages.
2491 The length of the comment header packet is (practically) unbounded.
2492 The comment header packet is not optional; it must be present in the stream
2493 even if it is logically empty.
2495 %TODO: \paragraph{VP3 Compatibility}
2497 \subsection{User Comment Format}
2499 The user comment vectors are structured similarly to a UNIX environment
2500 variable.
2501 That is, comment fields consist of a field name and a corresponding value and
2502 look like:
2503 \begin{center}
2504 \begin{tabular}{rcl}
2505 $\bitvar{COMMENTS}[0]$ & = & ``TITLE=the look of Theora" \\
2506 $\bitvar{COMMENTS}[1]$ & = & ``DIRECTOR=me"
2507 \end{tabular}
2508 \end{center}
2510 The field name is case-insensitive and MUST consist of ASCII characters
2511 \hex{20} through \hex{7D}, \hex{3D} (`=') excluded.
2512 ASCII \hex{41} through \hex{5A} inclusive (characters `A'--`Z') are to be
2513 considered equivalent to ASCII \hex{61} through \hex{7A} inclusive
2514 (characters `a'--`z').
2515 An entirely empty field name---one that is zero characters long---is not
2516 disallowed.
2518 The field name is immediately followed by ASCII \hex{3D} (`='); this equals
2519 sign is used to terminate the field name.
2521 The data immediately after \hex{3D} until the end of the vector is the eight-bit
2522 clean value of the field contents encoded as a UTF-8 string~\cite{rfc2044}.
2524 Field names MUST NOT be `internationalized'; this is a concession to
2525 simplicity, not an attempt to exclude the majority of the world that doesn't
2526 speak English.
2527 Applications MAY wish to present internationalized versions of the standard
2528 field names listed below to the user, but they are not to be stored in the
2529 bitstream.
2530 Field {\em contents}, however, use the UTF-8 character encoding to allow easy
2531 representation of any language.
2533 Individual `vendors' MAY use non-standard field names within reason.
2534 The proper use of comment fields as human-readable notes has already been
2535 explained.
2536 Abuse will be discouraged.
2538 There is no vendor-specific prefix to `non-standard' field names.
2539 Vendors SHOULD make some effort to avoid arbitrarily polluting the common
2540 namespace.
2541 %"and other bodies"?
2542 %If you're going to be that vague, you might as well not say anything at all.
2543 Xiph.org and other bodies will generally collect and rationalize the more
2544 useful tags to help with standardization.
2546 Field names are not restricted to occur only once within a comment header.
2547 %TODO: Example
2549 \paragraph{Field Names}
2551 %r should this be an appendix?
2553 Below is a proposed, minimal list of standard field names with a description of
2554 their intended use.
2555 No field names are mandatory; a comment header may contain one or more, all, or
2556 none of the names in this list.
2558 \begin{description}
2559 \item{TITLE:} Video name.
2560 \item{ARTIST:} Filmmaker or other creator name.
2561 \item{VERSION:} Subtitle, remix info, or other text distinguishing
2562 versions of a video.
2563 \item{DATE:} Date associated with the video. Implementations SHOULD attempt
2564 to parse this field as an ISO 8601 date for machine interpretation and
2565 conversion.
2566 \item{LOCATION:} Location associated with the video. This is usually the
2567 filming location for non-fiction works.
2568 \item{COPYRIGHT:} Copyright statement.
2569 \item{LICENSE:} Copyright and other licensing information.
2570 Implementations wishing to do automatic parsing of e.g
2571 of distribution terms SHOULD look here for a URL uniquely defining
2572 the license. If no instance of this field is present, or if no
2573 instance contains a parseable URL, and implementation MAY look
2574 in the COPYRIGHT field for such a URL.
2575 \item{ORGANIZATION:} Studio name, Publisher, or other organization
2576 involved in the creation of the video.
2578 \item{DIRECTOR:} Director or Filmmaker credit, similar to ARTIST.
2579 \item{PRODUCER:} Producer credit for the video.
2580 \item{COMPOSER:} Music credit for the video.
2581 \item{ACTOR:} Acting credit for the video.
2583 \item{TAG:} subject or category tag, keyword, or other content
2584 classification labels. The value of each instance of this
2585 field SHOULD be treated as a single label, with multiple
2586 instances of the field for multiple tags. The value of
2587 a single field SHOULD NOT be parsed into multiple tags
2588 based on some internal delimeter.
2589 \item{DESCRIPTION:} General description, summary, or blurb.
2590 \end{description}
2592 \section{Setup Header}
2593 \label{sec:setupheader}
2595 The Theora setup header contains the limit values used to drive the loop
2596 filter, the base matrices and scale values used to build the dequantization
2597 tables, and the Huffman tables used to unpack the DCT tokens.
2598 Because the contents of this header are specific to Theora, no concessions have
2599 been made to keep the fields octet-aligned for easy parsing.
2601 \begin{figure}
2602 \begin{center}
2603 \begin{tabular}{ | c | }
2604 \hline
2605 common header block \\ \hline
2606 loop filter table resolution \\ \hline
2607 loop filter table \\ \hline
2608 scale table resolution \\ \hline
2609 AC scale table \\ \hline
2610 DC scale table \\ \hline
2611 number of base matricies \\ \hline
2612 base quatization matricies \\ \hline
2613 ... \\ \hline
2614 quant range interpolation table \\ \hline
2615 DCT token Huffman tables \\
2616 \hline
2617 \end{tabular}
2618 \end{center}
2619 \caption{Setup Header structure}
2620 \label{fig:setupheader}
2621 \end{figure}
2623 \subsection{Loop Filter Limit Table Decode}
2624 \label{sub:loop-filter-limits}
2626 \paragraph{Input parameters:} None.
2628 \paragraph{Output parameters:}\hfill\\*
2629 \begin{tabularx}{\textwidth}{@{}llrcX@{}}\toprule
2630 \multicolumn{1}{c}{Name} &
2631 \multicolumn{1}{c}{Type} &
2632 \multicolumn{1}{p{30pt}}{\centering Size (bits)} &
2633 \multicolumn{1}{c}{Signed?} &
2634 \multicolumn{1}{c}{Description and restrictions} \\\midrule\endhead
2635 \bitvar{LFLIMS} & \multicolumn{1}{p{40pt}}{Integer array} &
2636 7 & No & A 64-element array of loop filter limit
2637 values. \\
2638 \bottomrule\end{tabularx}
2640 \paragraph{Variables used:}\hfill\\*
2641 \begin{tabularx}{\textwidth}{@{}llrcX@{}}\toprule
2642 \multicolumn{1}{c}{Name} &
2643 \multicolumn{1}{c}{Type} &
2644 \multicolumn{1}{p{30pt}}{\centering Size (bits)} &
2645 \multicolumn{1}{c}{Signed?} &
2646 \multicolumn{1}{c}{Description and restrictions} \\\midrule\endhead
2647 \locvar{\qi} & Integer & 6 & No & The quantization index. \\
2648 \locvar{NBITS} & Integer & 3 & No & The size of values being read in the
2649 current table. \\
2650 \bottomrule\end{tabularx}
2651 \medskip
2653 This procedure decodes the table of loop filter limit values used to drive the
2654 loop filter, which is described in Section~\ref{sub:loop-filter-limits}.
2655 It is decoded as follows:
2657 \begin{enumerate}
2658 \item
2659 Read a 3-bit unsigned integer as \locvar{NBITS}.
2660 \item
2661 For each consecutive value of \locvar{\qi} from $0$ to $63$, inclusive:
2662 \begin{enumerate}
2663 \item
2664 Read an \locvar{NBITS}-bit unsigned integer as $\bitvar{LFLIMS}[\locvar{\qi}]$.
2665 \end{enumerate}
2666 \end{enumerate}
2668 \paragraph{VP3 Compatibility}
2670 The loop filter limit values are hardcoded in VP3.
2671 The values used are given in Appendix~\ref{app:vp3-loop-filter-limits}.
2673 \subsection{Quantization Parameters Decode}
2674 \label{sub:quant-params}
2676 \paragraph{Input parameters:} None.
2678 \paragraph{Output parameters:}\hfill\\*
2679 \begin{tabularx}{\textwidth}{@{}llrcX@{}}\toprule
2680 \multicolumn{1}{c}{Name} &
2681 \multicolumn{1}{c}{Type} &
2682 \multicolumn{1}{p{30pt}}{\centering Size (bits)} &
2683 \multicolumn{1}{c}{Signed?} &
2684 \multicolumn{1}{c}{Description and restrictions} \\\midrule\endhead
2685 \bitvar{ACSCALE} & \multicolumn{1}{p{40pt}}{Integer array} &
2686 16 & No & A 64-element array of scale values for
2687 AC coefficients for each \qi\ value. \\
2688 \bitvar{DCSCALE} & \multicolumn{1}{p{40pt}}{Integer array} &
2689 16 & No & A 64-element array of scale values for
2690 the DC coefficient for each \qi\ value. \\
2691 \bitvar{NBMS} & Integer & 10 & No & The number of base matrices. \\
2692 \bitvar{BMS} & \multicolumn{1}{p{50pt}}{2D Integer array} &
2693 8 & No & A $\bitvar{NBMS}\times 64$ array
2694 containing the base matrices. \\
2695 \bitvar{NQRS} & \multicolumn{1}{p{50pt}}{2D Integer array} &
2696 6 & No & A $2\times 3$ array containing the
2697 number of quant ranges for a given \qti\ and \pli, respectively.
2698 This is at most $63$. \\
2699 \bitvar{QRSIZES} & \multicolumn{1}{p{50pt}}{3D Integer array} &
2700 6 & No & A $2\times 3\times 63$ array of the
2701 sizes of each quant range for a given \qti\ and \pli, respectively.
2702 Only the first $\bitvar{NQRS}[\qti][\pli]$ values are used. \\
2703 \bitvar{QRBMIS} & \multicolumn{1}{p{50pt}}{3D Integer array} &
2704 9 & No & A $2\times 3\times 64$ array of the
2705 \bmi's used for each quant range for a given \qti\ and \pli, respectively.
2706 Only the first $(\bitvar{NQRS}[\qti][\pli]+1)$ values are used. \\
2707 \bottomrule\end{tabularx}
2709 \paragraph{Variables used:}\hfill\\*
2710 \begin{tabularx}{\textwidth}{@{}llrcX@{}}\toprule
2711 \multicolumn{1}{c}{Name} &
2712 \multicolumn{1}{c}{Type} &
2713 \multicolumn{1}{p{30pt}}{\centering Size (bits)} &
2714 \multicolumn{1}{c}{Signed?} &
2715 \multicolumn{1}{c}{Description and restrictions} \\\midrule\endhead
2716 \locvar{\qti} & Integer & 1 & No & A quantization type index.
2717 See Table~\ref{tab:quant-types}.\\
2718 \locvar{\qtj} & Integer & 1 & No & A quantization type index. \\
2719 \locvar{\pli} & Integer & 2 & No & A color plane index.
2720 See Table~\ref{tab:color-planes}.\\
2721 \locvar{\plj} & Integer & 2 & No & A color plane index. \\
2722 \locvar{\qi} & Integer & 6 & No & The quantization index. \\
2723 \locvar{\ci} & Integer & 6 & No & The DCT coefficient index. \\
2724 \locvar{\bmi} & Integer & 9 & No & The base matrix index. \\
2725 \locvar{\qri} & Integer & 6 & No & The quant range index. \\
2726 \locvar{NBITS} & Integer & 5 & No & The size of fields to read. \\
2727 \locvar{NEWQR} & Integer & 1 & No & Flag that indicates a new set of quant
2728 ranges will be defined. \\
2729 \locvar{RPQR} & Integer & 1 & No & Flag that indicates the quant ranges to
2730 copy will come from the same color plane. \\
2731 \bottomrule\end{tabularx}
2732 \medskip
2734 The AC scale and DC scale values are defined in two simple tables with 64
2735 values each, one for each \qi\ value.
2736 The same scale values are used for every quantization type and color plane.
2738 The base matrices for all quantization types and color planes are stored in a
2739 single table.
2740 These are then referenced by index in several sets of \term{quant ranges}.
2741 The purpose of the quant ranges is to specify which base matrices are used for
2742 which \qi\ values.
2744 A set of quant ranges is defined for each quantization type and color plane.
2745 To save space in the header, bit flags allow a set of quant ranges to be copied
2746 from a previously defined set instead of being specified explicitly.
2747 Every set except the first one can be copied from the immediately preceding
2748 set.
2749 Similarly, if the quantization type is not $0$, the set can be copied from the
2750 set defined for the same color plane for the preceding quantization type.
2751 This formulation allows compact representation of, for example, the same
2752 set of quant ranges in both chroma channels, as is done in the original VP3,
2753 or the same set of quant ranges in INTRA and INTER modes.
2755 Each quant range is defined by a size and two base matrix indices, one for each
2756 end of the range.
2757 The base matrix for the end of one range is used as the start of the next
2758 range, so that for $n$ ranges, $n+1$ base matrices are specified.
2759 The base matrices for the \qi\ values between the two endpoints of the range
2760 are generated by linear interpolation.
2762 %TODO: figure
2764 The location of the endpoints of each range is encoded by their size.
2765 The \qi\ value for the left end-point is the sum of the sizes of all preceding
2766 ranges, and the \qi\ value for the right end-point adds the size of the
2767 current range.
2768 Thus the sum of the sizes of all the ranges MUST be 63, so that the last range
2769 falls on the last possible \qi\ value.
2771 The complete set of quantization parameters are decoded as follows:
2773 \begin{enumerate}
2774 \item
2775 Read a 4-bit unsigned integer.
2776 Assign \locvar{NBITS} the value read, plus one.
2777 \item
2778 For each consecutive value of \locvar{\qi} from $0$ to $63$, inclusive:
2779 \begin{enumerate}
2780 \item
2781 Read an \locvar{NBITS}-bit unsigned integer as
2782 $\bitvar{ACSCALE}[\locvar{\qi}]$.
2783 \end{enumerate}
2784 \item
2785 Read a 4-bit unsigned integer.
2786 Assign \locvar{NBITS} the value read, plus one.
2787 \item
2788 For each consecutive value of \locvar{\qi} from $0$ to $63$, inclusive:
2789 \begin{enumerate}
2790 \item
2791 Read an \locvar{NBITS}-bit unsigned integer as
2792 $\bitvar{DCSCALE}[\locvar{\qi}]$.
2793 \end{enumerate}
2794 \item
2795 Read a 9-bit unsigned integer.
2796 Assign \bitvar{NBMS} the value decoded, plus one.
2797 \bitvar{NBMS} MUST be no greater than 384.
2798 \item
2799 For each consecutive value of \locvar{\bmi} from $0$ to $(\bitvar{NBMS}-1)$,
2800 inclusive:
2801 \begin{enumerate}
2802 \item
2803 For each consecutive value of \locvar{\ci} from $0$ to $63$, inclusive:
2804 \begin{enumerate}
2805 \item
2806 Read an 8-bit unsigned integer as $\bitvar{BMS}[\locvar{\bmi}][\locvar{\ci}]$.
2807 \end{enumerate}
2808 \end{enumerate}
2809 \item
2810 For each consecutive value of \locvar{\qti} from $0$ to $1$, inclusive:
2811 \begin{enumerate}
2812 \item
2813 For each consecutive value of \locvar{\pli} from $0$ to $2$, inclusive:
2814 \begin{enumerate}
2815 \item
2816 If $\locvar{\qti}>0$ or $\locvar{\pli}>0$, read a 1-bit unsigned integer as
2817 \locvar{NEWQR}.
2818 \item
2819 Else, assign \locvar{NEWQR} the value one.
2820 \item
2821 If \locvar{NEWQR} is zero, then we are copying a previously defined set of
2822 quant ranges.
2823 In that case:
2824 \begin{enumerate}
2825 \item
2826 If $\locvar{\qti}>0$, read a 1-bit unsigned integer as \locvar{RPQR}.
2827 \item
2828 Else, assign \locvar{RPQR} the value zero.
2829 \item
2830 If \locvar{RPQR} is one, assign \locvar{\qtj} the value $(\locvar{\qti}-1)$
2831 and assign \locvar{\plj} the value \locvar{\pli}.
2832 This selects the set of quant ranges defined for the same color plane as this
2833 one, but for the previous quantization type.
2834 \item
2835 Else assign \locvar{\qtj} the value $(3*\locvar{\qti}+\locvar{\pli}-1)//3$ and
2836 assign \locvar{\plj} the value $(\locvar{\pli}+2)\%3$.
2837 This selects the most recent set of quant ranges defined.
2838 \item
2839 Assign $\bitvar{NQRS}[\locvar{\qti}][\locvar{\pli}]$ the value
2840 $\bitvar{NQRS}[\locvar{\qtj}][\locvar{\plj}]$.
2841 \item
2842 Assign $\bitvar{QRSIZES}[\locvar{\qti}][\locvar{\pli}]$ the values in
2843 $\bitvar{QRSIZES}[\locvar{\qtj}][\locvar{\plj}]$.
2844 \item
2845 Assign $\bitvar{QRBMIS}[\locvar{\qti}][\locvar{\pli}]$ the values in
2846 $\bitvar{QRBMIS}[\locvar{\qtj}][\locvar{\plj}]$.
2847 \end{enumerate}
2848 \item
2849 Else, \locvar{NEWQR} is one, which indicates that we are defining a new set of
2850 quant ranges.
2851 In that case:
2852 \begin{enumerate}
2853 \item
2854 Assign $\locvar{\qri}$ the value zero.
2855 \item
2856 Assign $\locvar{\qi}$ the value zero.
2857 \item
2858 Read an $\ilog(\bitvar{NBMS}-1)$-bit unsigned integer as\\
2859 $\bitvar{QRBMIS}[\locvar{\qti}][\locvar{\pli}][\locvar{\qri}]$.
2860 If this is greater than or equal to \bitvar{NBMS}, stop.
2861 The stream is undecodable.
2862 \item
2863 \label{step:qr-loop}
2864 Read an $\ilog(62-\locvar{\qi})$-bit unsigned integer.
2865 Assign\\ $\bitvar{QRSIZES}[\locvar{\qti}][\locvar{\pli}][\locvar{\qri}]$ the value
2866 read, plus one.
2867 \item
2868 Assign \locvar{\qi} the value $\locvar{\qi}+
2869 \bitvar{QRSIZES}[\locvar{\qti}][\locvar{\pli}][\locvar{\qri}]$.
2870 \item
2871 Assign \locvar{\qri} the value $\locvar{\qri}+1$.
2872 \item
2873 Read an $\ilog(\bitvar{NBMS}-1)$-bit unsigned integer as\\
2874 $\bitvar{QRBMIS}[\locvar{\qti}][\locvar{\pli}][\locvar{\qri}]$.
2875 \item
2876 If \locvar{\qi} is less than 63, go back to step~\ref{step:qr-loop}.
2877 \item
2878 If \locvar{\qi} is greater than 63, stop.
2879 The stream is undecodable.
2880 \item
2881 Assign $\bitvar{NQRS}[\locvar{\qti}][\locvar{\pli}]$ the value \locvar{\qri}.
2882 \end{enumerate}
2883 \end{enumerate}
2884 \end{enumerate}
2885 \end{enumerate}
2887 \paragraph{VP3 Compatibility}
2889 The quantization parameters are hardcoded in VP3.
2890 The values used are given in Appendix~\ref{app:vp3-quant-params}.
2892 \subsection{Computing a Quantization Matrix}
2893 \label{sub:quant-mat}
2895 \paragraph{Input parameters:}\hfill\\*
2896 \begin{tabularx}{\textwidth}{@{}llrcX@{}}\toprule
2897 \multicolumn{1}{c}{Name} &
2898 \multicolumn{1}{c}{Type} &
2899 \multicolumn{1}{p{30pt}}{\centering Size (bits)} &
2900 \multicolumn{1}{c}{Signed?} &
2901 \multicolumn{1}{c}{Description and restrictions} \\\midrule\endhead
2902 \bitvar{ACSCALE} & \multicolumn{1}{p{40pt}}{Integer array} &
2903 16 & No & A 64-element array of scale values for
2904 AC coefficients for each \qi\ value. \\
2905 \bitvar{DCSCALE} & \multicolumn{1}{p{40pt}}{Integer array} &
2906 16 & No & A 64-element array of scale values for
2907 the DC coefficient for each \qi\ value. \\
2908 \bitvar{BMS} & \multicolumn{1}{p{50pt}}{2D Integer array} &
2909 8 & No & A $\bitvar{NBMS}\times 64$ array
2910 containing the base matrices. \\
2911 \bitvar{NQRS} & \multicolumn{1}{p{50pt}}{2D Integer array} &
2912 6 & No & A $2\times 3$ array containing the
2913 number of quant ranges for a given \qti\ and \pli, respectively.
2914 This is at most $63$. \\
2915 \bitvar{QRSIZES} & \multicolumn{1}{p{50pt}}{3D Integer array} &
2916 6 & No & A $2\times 3\times 63$ array of the
2917 sizes of each quant range for a given \qti\ and \pli, respectively.
2918 Only the first $\bitvar{NQRS}[\qti][\pli]$ values are used. \\
2919 \bitvar{QRBMIS} & \multicolumn{1}{p{50pt}}{3D Integer array} &
2920 9 & No & A $2\times 3\times 64$ array of the
2921 \bmi's used for each quant range for a given \qti\ and \pli, respectively.
2922 Only the first $(\bitvar{NQRS}[\qti][\pli]+1)$ values are used. \\
2923 \bitvar{\qti} & Integer & 1 & No & A quantization type index.
2924 See Table~\ref{tab:quant-types}.\\
2925 \bitvar{\pli} & Integer & 2 & No & A color plane index.
2926 See Table~\ref{tab:color-planes}.\\
2927 \bitvar{\qi} & Integer & 6 & No & The quantization index. \\
2928 \bottomrule\end{tabularx}
2930 \paragraph{Output parameters:}\hfill\\*
2931 \begin{tabularx}{\textwidth}{@{}llrcX@{}}\toprule
2932 \multicolumn{1}{c}{Name} &
2933 \multicolumn{1}{c}{Type} &
2934 \multicolumn{1}{p{30pt}}{\centering Size (bits)} &
2935 \multicolumn{1}{c}{Signed?} &
2936 \multicolumn{1}{c}{Description and restrictions} \\\midrule\endhead
2937 \bitvar{QMAT} & \multicolumn{1}{p{40pt}}{Integer array} &
2938 16 & No & A 64-element array of quantization
2939 values for each DCT coefficient in natural order. \\
2940 \bottomrule\end{tabularx}
2942 \paragraph{Variables used:}\hfill\\*
2943 \begin{tabularx}{\textwidth}{@{}llrcX@{}}\toprule
2944 \multicolumn{1}{c}{Name} &
2945 \multicolumn{1}{c}{Type} &
2946 \multicolumn{1}{p{30pt}}{\centering Size (bits)} &
2947 \multicolumn{1}{c}{Signed?} &
2948 \multicolumn{1}{c}{Description and restrictions} \\\midrule\endhead
2949 \locvar{\ci} & Integer & 6 & No & The DCT coefficient index. \\
2950 \locvar{\bmi} & Integer & 9 & No & The base matrix index. \\
2951 \locvar{\bmj} & Integer & 9 & No & The base matrix index. \\
2952 \locvar{\qri} & Integer & 6 & No & The quant range index. \\
2953 \locvar{QISTART} & Integer & 6 & No & The left end-point of the \qi\ range. \\
2954 \locvar{QIEND } & Integer & 6 & No & The right end-point of the \qi\ range. \\
2955 \locvar{BM} & \multicolumn{1}{p{40pt}}{Integer array} &
2956 8 & No & A 64-element array containing the
2957 interpolated base matrix. \\
2958 \locvar{QMIN} & Integer & 16 & No & The minimum quantization value allowed
2959 for the current coefficient. \\
2960 \locvar{QSCALE} & Integer & 16 & No & The current scale value. \\
2961 \bottomrule\end{tabularx}
2962 \medskip
2964 The following procedure can be used to generate a single quantization matrix
2965 for a given quantization type, color plane, and \qi\ value, given the
2966 quantization parameters decoded in Section~\ref{sub:quant-params}.
2968 Note that the product of the scale value and the base matrix value is in units
2969 of $100$ths of a pixel value, and thus is divided by $100$ to return it to
2970 units of a single pixel value.
2971 This value is then scaled by four, to match the scaling of the DCT output,
2972 which is also a factor of four larger than the orthonormal version of the
2973 transform.
2975 \begin{enumerate}
2976 \item
2977 Assign \locvar{\qri} the index of a quant range such that
2978 \begin{displaymath}
2979 \bitvar{\qi} \ge \sum_{\qrj=0}^{\locvar{\qri}-1}
2980 \bitvar{QRSIZES}[\bitvar{\qti}][\bitvar{\pli}][\qrj],
2981 \end{displaymath}
2983 \begin{displaymath}
2984 \bitvar{\qi} \le \sum_{\qrj=0}^{\locvar{\qri}}
2985 \bitvar{QRSIZES}[\bitvar{\qti}][\bitvar{\pli}][\qrj],
2986 \end{displaymath}
2987 where summation from $0$ to $-1$ is defined to be zero.
2988 If there is more than one such value of $\locvar{\qri}$, i.e., if \bitvar{\qi}
2989 lies on the boundary between two quant ranges, then the output will be the
2990 same regardless of which one is chosen.
2991 \item
2992 Assign \locvar{QISTART} the value
2993 \begin{displaymath}
2994 \sum_{\qrj=0}^{\qri-1} \bitvar{QRSIZES}[\bitvar{\qti}][\bitvar{\pli}][\qrj].
2995 \end{displaymath}
2996 \item
2997 Assign \locvar{QIEND} the value
2998 \begin{displaymath}
2999 \sum_{\qrj=0}^{\qri} \bitvar{QRSIZES}[\bitvar{\qti}][\bitvar{\pli}][\qrj].
3000 \end{displaymath}
3001 \item
3002 Assign \locvar{\bmi} the value
3003 $\bitvar{QRBMIS}[\bitvar{\qti}][\bitvar{\pli}][\qri]$.
3004 \item
3005 Assign \locvar{\bmj} the value
3006 $\bitvar{QRBMIS}[\bitvar{\qti}][\bitvar{\pli}][\qri+1]$.
3007 \item
3008 For each consecutive value of \locvar{\ci} from $0$ to $63$, inclusive:
3009 \begin{enumerate}
3010 \item
3011 Assign $\locvar{BM}[\locvar{\ci}]$ the value
3012 \begin{displaymath}
3013 \begin{split}
3014 (&2*(\locvar{QIEND}-\bitvar{\qi})*\bitvar{BMS}[\locvar{\bmi}][\locvar{\ci}]\\
3015 &+2*(\bitvar{\qi}-
3016 \locvar{QISTART})*\bitvar{BMS}[\locvar{\bmj}][\locvar{\ci}]\\
3017 &+\bitvar{QRSIZES}[\bitvar{\qti}][\bitvar{\pli}][\locvar{\qri}])//
3018 (2*\bitvar{QRSIZES}[\bitvar{\qti}][\bitvar{\pli}][\locvar{\qri}])
3019 \end{split}
3020 \end{displaymath}
3021 \item
3022 Assign \locvar{QMIN} the value given by Table~\ref{tab:qmin} according to
3023 \bitvar{\qti} and \locvar{\ci}.
3025 \begin{table}[htbp]
3026 \begin{center}
3027 \begin{tabular}{clr}\toprule
3028 Coefficient & \multicolumn{1}{c}{\bitvar{\qti}}
3029 & \locvar{QMIN} \\\midrule
3030 $\locvar{\ci}=0$ & $0$ (Intra) & $16$ \\
3031 $\locvar{\ci}>0$ & $0$ (Intra) & $8$ \\
3032 $\locvar{\ci}=0$ & $1$ (Inter) & $32$ \\
3033 $\locvar{\ci}>0$ & $1$ (Inter) & $16$ \\
3034 \bottomrule\end{tabular}
3035 \end{center}
3036 \caption{Minimum Quantization Values}
3037 \label{tab:qmin}
3038 \end{table}
3040 \item
3041 If \locvar{\ci} equals zero, assign $\locvar{QSCALE}$ the value
3042 $\bitvar{DCSCALE}[\bitvar{\qi}]$.
3043 \item
3044 Else, assign $\locvar{QSCALE}$ the value
3045 $\bitvar{ACSCALE}[\bitvar{\qi}]$.
3046 \item
3047 Assign $\bitvar{QMAT}[\locvar{\ci}]$ the value
3048 \begin{displaymath}
3049 \max(\locvar{QMIN},
3050 \min((\locvar{QSCALE}*\locvar{BM}[\locvar{\ci}]//100)*4,4096)).
3051 \end{displaymath}
3052 \end{enumerate}
3053 \end{enumerate}
3055 \subsection{DCT Token Huffman Tables}
3056 \label{sub:huffman-tables}
3058 \paragraph{Input parameters:} None.
3060 \paragraph{Output parameters:}\hfill\\*
3061 \begin{tabularx}{\textwidth}{@{}llrcX@{}}\toprule
3062 \multicolumn{1}{c}{Name} &
3063 \multicolumn{1}{c}{Type} &
3064 \multicolumn{1}{p{30pt}}{\centering Size (bits)} &
3065 \multicolumn{1}{c}{Signed?} &
3066 \multicolumn{1}{c}{Description and restrictions} \\\midrule\endhead
3067 \bitvar{HTS} & \multicolumn{3}{l}{Huffman table array}
3068 & An 80-element array of Huffman tables
3069 with up to 32 entries each. \\
3070 \bottomrule\end{tabularx}
3072 \paragraph{Variables used:}\hfill\\*
3073 \begin{tabularx}{\textwidth}{@{}llrcX@{}}\toprule
3074 \multicolumn{1}{c}{Name} &
3075 \multicolumn{1}{c}{Type} &
3076 \multicolumn{1}{p{30pt}}{\centering Size (bits)} &
3077 \multicolumn{1}{c}{Signed?} &
3078 \multicolumn{1}{c}{Description and restrictions} \\\midrule\endhead
3079 \locvar{HBITS} & Bit string & 32 & No & A string of up to 32 bits. \\
3080 \locvar{TOKEN} & Integer & 5 & No & A single DCT token value. \\
3081 \locvar{ISLEAF} & Integer & 1 & No & Flag that indicates if the current
3082 node of the tree being decoded is a leaf node. \\
3083 \bottomrule\end{tabularx}
3084 \medskip
3086 The Huffman tables used to decode DCT tokens are stored in the setup header in
3087 the form of a binary tree.
3088 This enforces the requirements that the code be full---so that any sequence of
3089 bits will produce a valid sequence of tokens---and that the code be
3090 prefix-free so that there is no ambiguity when decoding.
3092 One more restriction is placed on the tables that is not explicitly enforced by
3093 the bitstream syntax, but nevertheless must be obeyed by compliant encoders.
3094 There must be no more than 32 entries in a single table.
3095 Note that this restriction along with the fullness requirement limit the
3096 maximum size of a single Huffman code to 32 bits.
3097 It is probably a good idea to enforce this latter consequence explicitly when
3098 implementing the decoding procedure as a recursive algorithm, so as to prevent
3099 a possible stack overflow given an invalid bitstream.
3101 Although there are 32 different DCT tokens, and thus a normal table will have
3102 exactly 32 entries, this is not explicitly required.
3103 It is allowable to use a Huffman code that omits some---but not all---of the
3104 possible token values.
3105 It is also allowable, if not particularly useful, to specify multiple codes for
3106 the same token value in a single table.
3107 Note also that token values may appear in the tree in any order.
3108 In particular, it is not safe to assume that token value zero (which ends a
3109 single block), has a Huffman code of all zeros.
3111 The tree is decoded as follows:
3113 \begin{enumerate}
3114 \item
3115 For each consecutive value of \locvar{\hti} from $0$ to $79$, inclusive:
3116 \begin{enumerate}
3117 \item
3118 Set \locvar{HBITS} to the empty string.
3119 \item
3120 \label{step:huff-tree-loop}
3121 If \locvar{HBITS} is longer than 32 bits in length, stop.
3122 The stream is undecodable.
3123 \item
3124 Read a 1-bit unsigned integer as \locvar{ISLEAF}.
3125 \item
3126 If \locvar{ISLEAF} is one:
3127 \begin{enumerate}
3128 \item
3129 If the number of entries in table $\bitvar{HTS}[\locvar{\hti}]$ is already 32,
3130 stop.
3131 The stream is undecodable.
3132 \item
3133 Read a 5-bit unsigned integer as \locvar{TOKEN}.
3134 \item
3135 Add the pair $(\locvar{HBITS},\locvar{TOKEN})$ to Huffman table
3136 $\bitvar{HTS}[\locvar{\hti}]$.
3137 \end{enumerate}
3138 \item
3139 Otherwise:
3140 \begin{enumerate}
3141 \item
3142 Add a `0' to the end of \locvar{HBITS}.
3143 \item
3144 Decode the `0' sub-tree using this procedure, starting from
3145 step~\ref{step:huff-tree-loop}.
3146 \item
3147 Remove the `0' from the end of \locvar{HBITS} and add a `1' to the end of
3148 \locvar{HBITS}.
3149 \item
3150 Decode the `1' sub-tree using this procedure, starting from
3151 step~\ref{step:huff-tree-loop}.
3152 \item
3153 Remove the `1' from the end of \locvar{HBITS}.
3154 \end{enumerate}
3155 \end{enumerate}
3156 \end{enumerate}
3158 \paragraph{VP3 Compatibility}
3160 The DCT token Huffman tables are hardcoded in VP3.
3161 The values used are given in Appendix~\ref{app:vp3-huffman-tables}.
3163 \subsection{Setup Header Decode}
3165 \paragraph{Input parameters:} None.
3167 \paragraph{Output parameters:}\hfill\\*
3168 \begin{tabularx}{\textwidth}{@{}llrcX@{}}\toprule
3169 \multicolumn{1}{c}{Name} &
3170 \multicolumn{1}{c}{Type} &
3171 \multicolumn{1}{p{30pt}}{\centering Size (bits)} &
3172 \multicolumn{1}{c}{Signed?} &
3173 \multicolumn{1}{c}{Description and restrictions} \\\midrule\endhead
3174 \bitvar{LFLIMS} & \multicolumn{1}{p{40pt}}{Integer array} &
3175 7 & No & A 64-element array of loop filter limit
3176 values. \\
3177 \bitvar{ACSCALE} & \multicolumn{1}{p{40pt}}{Integer array} &
3178 16 & No & A 64-element array of scale values for
3179 AC coefficients for each \qi\ value. \\
3180 \bitvar{DCSCALE} & \multicolumn{1}{p{40pt}}{Integer array} &
3181 16 & No & A 64-element array of scale values for
3182 the DC coefficient for each \qi\ value. \\
3183 \bitvar{NBMS} & Integer & 10 & No & The number of base matrices. \\
3184 \bitvar{BMS} & \multicolumn{1}{p{50pt}}{2D Integer array} &
3185 8 & No & A $\bitvar{NBMS}\times 64$ array
3186 containing the base matrices. \\
3187 \bitvar{NQRS} & \multicolumn{1}{p{50pt}}{2D Integer array} &
3188 6 & No & A $2\times 3$ array containing the
3189 number of quant ranges for a given \qti\ and \pli, respectively.
3190 This is at most $63$. \\
3191 \bitvar{QRSIZES} & \multicolumn{1}{p{50pt}}{3D Integer array} &
3192 6 & No & A $2\times 3\times 63$ array of the
3193 sizes of each quant range for a given \qti\ and \pli, respectively.
3194 Only the first $\bitvar{NQRS}[\qti][\pli]$ values will be used. \\
3195 \bitvar{QRBMIS} & \multicolumn{1}{p{50pt}}{3D Integer array} &
3196 9 & No & A $2\times 3\times 64$ array of the
3197 \bmi's used for each quant range for a given \qti\ and \pli, respectively.
3198 Only the first $(\bitvar{NQRS}[\qti][\pli]+1)$ values will be used. \\
3199 \bitvar{HTS} & \multicolumn{3}{l}{Huffman table array}
3200 & An 80-element array of Huffman tables
3201 with up to 32 entries each. \\
3202 \bottomrule\end{tabularx}
3204 \paragraph{Variables used:} None.
3205 \medskip
3207 The complete setup header is decoded as follows:
3209 \begin{enumerate}
3210 \item
3211 Decode the common header fields according to the procedure described in
3212 Section~\ref{sub:common-header}.
3213 If \bitvar{HEADERTYPE} returned by this procedure is not \hex{82}, then stop.
3214 This packet is not the setup header.
3215 \item
3216 Decode the loop filter limit value table using the procedure given in
3217 Section~\ref{sub:loop-filter-limits} into \bitvar{LFLIMS}.
3218 \item
3219 Decode the quantization parameters using the procedure given in
3220 Section~\ref{sub:quant-params}.
3221 The results are stored in \bitvar{ACSCALE}, \bitvar{DCSCALE}, \bitvar{NBMS},
3222 \bitvar{BMS}, \bitvar{NQRS}, \bitvar{QRSIZES}, and \bitvar{QRBMIS}.
3223 \item
3224 Decode the DCT token Huffman tables using the procedure given in
3225 Section~\ref{sub:huffman-tables} into \bitvar{HTS}.
3226 \end{enumerate}
3228 \chapter{Frame Decode}
3230 This section describes the complete procedure necessary to decode a single
3231 frame.
3232 This begins with the frame header, followed by coded block flags, macro block
3233 modes, motion vectors, block-level \qi\ values, and finally the DCT residual
3234 tokens, which are used to reconstruct the frame.
3236 \section{Frame Header Decode}
3237 \label{sub:frame-header}
3239 \paragraph{Input parameters:} None.
3241 \paragraph{Output parameters:}\hfill\\*
3242 \begin{tabularx}{\textwidth}{@{}llrcX@{}}\toprule
3243 \multicolumn{1}{c}{Name} &
3244 \multicolumn{1}{c}{Type} &
3245 \multicolumn{1}{p{30pt}}{\centering Size (bits)} &
3246 \multicolumn{1}{c}{Signed?} &
3247 \multicolumn{1}{c}{Description and restrictions} \\\midrule\endhead
3248 \bitvar{FTYPE} & Integer & 1 & No & The frame type. \\
3249 \bitvar{NQIS} & Integer & 2 & No & The number of \qi\ values. \\
3250 \bitvar{QIS} & \multicolumn{1}{p{40pt}}{Integer array} &
3251 6 & No & An \bitvar{NQIS}-element array of
3252 \qi\ values. \\
3253 \bottomrule\end{tabularx}
3255 \paragraph{Variables used:}\hfill\\*
3256 \begin{tabularx}{\textwidth}{@{}llrcX@{}}\toprule
3257 \multicolumn{1}{c}{Name} &
3258 \multicolumn{1}{c}{Type} &
3259 \multicolumn{1}{p{30pt}}{\centering Size (bits)} &
3260 \multicolumn{1}{c}{Signed?} &
3261 \multicolumn{1}{c}{Description and restrictions} \\\midrule\endhead
3262 \locvar{MOREQIS} & Integer & 1 & No & A flag indicating there are more
3263 \qi\ values to be decoded. \\
3264 \bottomrule\end{tabularx}
3265 \medskip
3267 The frame header selects which type of frame is being decoded, intra or inter,
3268 and contains the list of \qi\ values that will be used in this frame.
3269 The first \qi\ value will be used for {\em all} DC coefficients in all blocks.
3270 This is done to ensure that DC prediction, which is done in the quantized
3271 domain, works as expected.
3272 The AC coefficients, however, can be dequantized using any \qi\ value on the
3273 list, selected on a block-by-block basis.
3275 \begin{enumerate}
3276 \item
3277 Read a 1-bit unsigned integer.
3278 If the value read is not zero, stop.
3279 This is not a data packet.
3280 \item
3281 Read a 1-bit unsigned integer as \bitvar{FTYPE}.
3282 This is the type of frame being decoded, as given in
3283 Table~\ref{tab:frame-type}.
3284 If this is the first frame being decoded, this MUST be zero.
3286 \begin{table}[htbp]
3287 \begin{center}
3288 \begin{tabular}{cl}\toprule
3289 \bitvar{FTYPE} & Frame Type \\\midrule
3290 $0$ & Intra frame \\
3291 $1$ & Inter frame \\
3292 \bottomrule\end{tabular}
3293 \end{center}
3294 \caption{Frame Type Values}
3295 \label{tab:frame-type}
3296 \end{table}
3298 \item
3299 Read in a 6-bit unsigned integer as $\bitvar{QIS}[0]$.
3300 \item
3301 Read a 1-bit unsigned integer as \locvar{MOREQIS}.
3302 \item
3303 If \locvar{MOREQIS} is zero, set \bitvar{NQIS} to 1.
3304 \item
3305 Otherwise:
3306 \begin{enumerate}
3307 \item
3308 Read in a 6-bit unsigned integer as $\bitvar{QIS}[1]$.
3309 \item
3310 Read a 1-bit unsigned integer as \locvar{MOREQIS}.
3311 \item
3312 If \locvar{MOREQIS} is zero, set \bitvar{NQIS} to 2.
3313 \item
3314 Otherwise:
3315 \begin{enumerate}
3316 \item
3317 Read in a 6-bit unsigned integer as $\bitvar{QIS}[2]$.
3318 \item
3319 Set \bitvar{NQIS} to 3.
3320 \end{enumerate}
3321 \end{enumerate}
3322 \item
3323 If \bitvar{FTYPE} is 0, read a 3-bit unsigned integer.
3324 These bits are reserved.
3325 If this value is not zero, stop.
3326 This frame is not decodable according to this specification.
3327 \end{enumerate}
3329 \paragraph{VP3 Compatibility}
3331 The precise format of the frame header is substantially different in Theora
3332 than in VP3.
3333 The original VP3 format includes a larger number of unused, reserved bits that
3334 are required to be zero.
3335 The original VP3 frame header also can contain only a single \qi\ value,
3336 because VP3 does not support block-level \qi\ values and uses the same
3337 \qi\ value for all the coefficients in a frame.
3339 \section{Run-Length Encoded Bit Strings}
3341 Two variations of run-length encoding are used to store sequences of bits for
3342 the block coded flags and the block-level \qi\ values.
3343 The procedures to decode these bit sequences are specified in the following two
3344 sections.
3346 \subsection{Long-Run Bit String Decode}
3347 \label{sub:long-run}
3349 \paragraph{Input parameters:}\hfill\\*
3350 \begin{tabularx}{\textwidth}{@{}llrcX@{}}\toprule
3351 \multicolumn{1}{c}{Name} &
3352 \multicolumn{1}{c}{Type} &
3353 \multicolumn{1}{p{30pt}}{\centering Size (bits)} &
3354 \multicolumn{1}{c}{Signed?} &
3355 \multicolumn{1}{c}{Description and restrictions} \\\midrule\endhead
3356 \bitvar{NBITS} & Integer & 36 & No & The number of bits to decode. \\
3357 \bottomrule\end{tabularx}
3359 \paragraph{Output parameters:}\hfill\\*
3360 \begin{tabularx}{\textwidth}{@{}llrcX@{}}\toprule
3361 \multicolumn{1}{c}{Name} &
3362 \multicolumn{1}{c}{Type} &
3363 \multicolumn{1}{p{30pt}}{\centering Size (bits)} &
3364 \multicolumn{1}{c}{Signed?} &
3365 \multicolumn{1}{c}{Description and restrictions} \\\midrule\endhead
3366 \bitvar{BITS} & Bit string & & & The decoded bits. \\
3367 \bottomrule\end{tabularx}
3369 \paragraph{Variables used:}\hfill\\*
3370 \begin{tabularx}{\textwidth}{@{}llrcX@{}}\toprule
3371 \multicolumn{1}{c}{Name} &
3372 \multicolumn{1}{c}{Type} &
3373 \multicolumn{1}{p{30pt}}{\centering Size (bits)} &
3374 \multicolumn{1}{c}{Signed?} &
3375 \multicolumn{1}{c}{Description and restrictions} \\\midrule\endhead
3376 \locvar{LEN} & Integer & 36 & No & The number of bits decoded so far. \\
3377 \locvar{BIT} & Integer & 1 & No & The value associated with the current
3378 run. \\
3379 \locvar{RLEN} & Integer & 13 & No & The length of the current run. \\
3380 \locvar{RBITS} & Integer & 4 & No & The number of extra bits needed to
3381 decode the run length. \\
3382 \locvar{RSTART} & Integer & 6 & No & The start of the possible run-length
3383 values for a given Huffman code. \\
3384 \locvar{ROFFS} & Integer & 12 & No & The offset from \locvar{RSTART} of the
3385 run-length. \\
3386 \bottomrule\end{tabularx}
3387 \medskip
3389 There is no practical limit to the number of consecutive 0's and 1's that can
3390 be decoded with this procedure.
3391 In reality, the run length is limited by the number of blocks in a single
3392 frame, because more will never be requested.
3393 A separate procedure described in Section~\ref{sub:short-run} is used when
3394 there is a known limit on the maximum size of the runs.
3396 For the first run, a single bit value is read, and then a Huffman-coded
3397 representation of a run length is decoded, and that many copies of the bit
3398 value are appended to the bit string.
3399 For each consecutive run, the value of the bit is toggled instead of being read
3400 from the bitstream.
3402 The only exception is if the length of the previous run was 4129, the maximum
3403 possible length encodable by the Huffman-coded representation.
3404 In this case another bit value is read from the stream, to allow for
3405 consecutive runs of 0's or 1's longer than this maximum.
3407 Note that in both cases---for the first run and after a run of length 4129---if
3408 no more bits are needed, then no bit value is read.
3410 The complete decoding procedure is as follows:
3412 \begin{enumerate}
3413 \item
3414 Assign \locvar{LEN} the value 0.
3415 \item
3416 Assign \bitvar{BITS} the empty string.
3417 \item
3418 If \locvar{LEN} equals \bitvar{NBITS}, return the completely decoded string
3419 \bitvar{BITS}.
3420 \item
3421 Read a 1-bit unsigned integer as \locvar{BIT}.
3422 \item
3423 \label{step:long-run-loop}
3424 Read a bit at a time until one of the Huffman codes given in
3425 Table~\ref{tab:long-run} is recognized.
3427 \begin{table}[htbp]
3428 \begin{center}
3429 \begin{tabular}{lrrl}\toprule
3430 Huffman Code & \locvar{RSTART} & \locvar{RBITS} & Run Lengths \\\midrule
3431 \bin{0} & $1$ & $0$ & $1$ \\
3432 \bin{10} & $2$ & $1$ & $2\ldots 3$ \\
3433 \bin{110} & $4$ & $1$ & $4\ldots 5$ \\
3434 \bin{1110} & $6$ & $2$ & $6\ldots 9$ \\
3435 \bin{11110} & $10$ & $3$ & $10\ldots 17$ \\
3436 \bin{111110} & $18$ & $4$ & $18\ldots 33$ \\
3437 \bin{111111} & $34$ & $12$ & $34\ldots 4129$ \\
3438 \bottomrule\end{tabular}
3439 \end{center}
3440 \caption{Huffman Codes for Long Run Lengths}
3441 \label{tab:long-run}
3442 \end{table}
3444 \item
3445 Assign \locvar{RSTART} and \locvar{RBITS} the values given in
3446 Table~\ref{tab:long-run} according to the Huffman code read.
3447 \item
3448 Read an \locvar{RBITS}-bit unsigned integer as \locvar{ROFFS}.
3449 \item
3450 Assign \locvar{RLEN} the value $(\locvar{RSTART}+\locvar{ROFFS})$.
3451 \item
3452 Append \locvar{RLEN} copies of \locvar{BIT} to \bitvar{BITS}.
3453 \item
3454 Add \locvar{RLEN} to the value \locvar{LEN}.
3455 \locvar{LEN} MUST be less than or equal to \bitvar{NBITS}.
3456 \item
3457 If \locvar{LEN} equals \bitvar{NBITS}, return the completely decoded string
3458 \bitvar{BITS}.
3459 \item
3460 If \locvar{RLEN} equals 4129, read a 1-bit unsigned integer as \locvar{BIT}.
3461 \item
3462 Otherwise, assign \locvar{BIT} the value $(1-\locvar{BIT})$.
3463 \item
3464 Continue decoding runs from step~\ref{step:long-run-loop}.
3465 \end{enumerate}
3467 \paragraph{VP3 Compatibility}
3469 VP3 does not read a new bit value after decoding a run length of 4129.
3470 This limits the maximum number of consecutive 0's or 1's to 4129 in
3471 VP3-compatible streams.
3472 For reasonable video sizes of $1920\times 1080$ or less in 4:2:0 format---the
3473 only pixel format VP3 supports---this does not pose any problems because runs
3474 longer than 4129 are not needed.
3476 \subsection{Short-Run Bit String Decode}
3477 \label{sub:short-run}
3479 \paragraph{Input parameters:}\hfill\\*
3480 \begin{tabularx}{\textwidth}{@{}llrcX@{}}\toprule
3481 \multicolumn{1}{c}{Name} &
3482 \multicolumn{1}{c}{Type} &
3483 \multicolumn{1}{p{30pt}}{\centering Size (bits)} &
3484 \multicolumn{1}{c}{Signed?} &
3485 \multicolumn{1}{c}{Description and restrictions} \\\midrule\endhead
3486 \bitvar{NBITS} & Integer & 36 & No & The number of bits to decode. \\
3487 \bottomrule\end{tabularx}
3489 \paragraph{Output parameters:}\hfill\\*
3490 \begin{tabularx}{\textwidth}{@{}llrcX@{}}\toprule
3491 \multicolumn{1}{c}{Name} &
3492 \multicolumn{1}{c}{Type} &
3493 \multicolumn{1}{p{30pt}}{\centering Size (bits)} &
3494 \multicolumn{1}{c}{Signed?} &
3495 \multicolumn{1}{c}{Description and restrictions} \\\midrule\endhead
3496 \bitvar{BITS} & Bit string & & & The decoded bits. \\
3497 \bottomrule\end{tabularx}
3499 \paragraph{Variables used:}\hfill\\*
3500 \begin{tabularx}{\textwidth}{@{}llrcX@{}}\toprule
3501 \multicolumn{1}{c}{Name} &
3502 \multicolumn{1}{c}{Type} &
3503 \multicolumn{1}{p{30pt}}{\centering Size (bits)} &
3504 \multicolumn{1}{c}{Signed?} &
3505 \multicolumn{1}{c}{Description and restrictions} \\\midrule\endhead
3506 \locvar{LEN} & Integer & 36 & No & The number of bits decoded so far. \\
3507 \locvar{BIT} & Integer & 1 & No & The value associated with the current
3508 run. \\
3509 \locvar{RLEN} & Integer & 13 & No & The length of the current run. \\
3510 \locvar{RBITS} & Integer & 4 & No & The number of extra bits needed to
3511 decode the run length. \\
3512 \locvar{RSTART} & Integer & 6 & No & The start of the possible run-length
3513 values for a given Huffman code. \\
3514 \locvar{ROFFS} & Integer & 12 & No & The offset from \locvar{RSTART} of the
3515 run-length. \\
3516 \bottomrule\end{tabularx}
3517 \medskip
3519 This procedure is similar to the procedure outlined in
3520 Section~\ref{sub:long-run}, except that the maximum number of consecutive 0's
3521 or 1's is limited to 30.
3522 This is the maximum run length needed when encoding a bit for each of the 16
3523 blocks in a super block when it is known that not all the bits in a super
3524 block are the same.
3526 The complete decoding procedure is as follows:
3528 \begin{enumerate}
3529 \item
3530 Assign \locvar{LEN} the value 0.
3531 \item
3532 Assign \bitvar{BITS} the empty string.
3533 \item
3534 If \locvar{LEN} equals \bitvar{NBITS}, return the completely decoded string
3535 \bitvar{BITS}.
3536 \item
3537 Read a 1-bit unsigned integer as \locvar{BIT}.
3538 \item
3539 \label{step:short-run-loop}
3540 Read a bit at a time until one of the Huffman codes given in
3541 Table~\ref{tab:short-run} is recognized.
3543 \begin{table}[htbp]
3544 \begin{center}
3545 \begin{tabular}{lrrl}\toprule
3546 Huffman Code & \locvar{RSTART} & \locvar{RBITS} & Run Lengths \\\midrule
3547 \bin{0} & $1$ & $1$ & $1\ldots 2$ \\
3548 \bin{10} & $3$ & $1$ & $3\ldots 4$ \\
3549 \bin{110} & $5$ & $1$ & $5\ldots 6$ \\
3550 \bin{1110} & $7$ & $2$ & $7\ldots 10$ \\
3551 \bin{11110} & $11$ & $2$ & $11\ldots 14$ \\
3552 \bin{11111} & $15$ & $4$ & $15\ldots 30$ \\
3553 \bottomrule\end{tabular}
3554 \end{center}
3555 \caption{Huffman Codes for Short Run Lengths}
3556 \label{tab:short-run}
3557 \end{table}
3559 \item
3560 Assign \locvar{RSTART} and \locvar{RBITS} the values given in
3561 Table~\ref{tab:short-run} according to the Huffman code read.
3562 \item
3563 Read an \locvar{RBITS}-bit unsigned integer as \locvar{ROFFS}.
3564 \item
3565 Assign \locvar{RLEN} the value $(\locvar{RSTART}+\locvar{ROFFS})$.
3566 \item
3567 Append \locvar{RLEN} copies of \locvar{BIT} to \bitvar{BITS}.
3568 \item
3569 Add \locvar{RLEN} to the value \locvar{LEN}.
3570 \locvar{LEN} MUST be less than or equal to \bitvar{NBITS}.
3571 \item
3572 If \locvar{LEN} equals \bitvar{NBITS}, return the completely decoded string
3573 \bitvar{BITS}.
3574 \item
3575 Assign \locvar{BIT} the value $(1-\locvar{BIT})$.
3576 \item
3577 Continue decoding runs from step~\ref{step:short-run-loop}.
3578 \end{enumerate}
3580 \section{Coded Block Flags Decode}
3581 \label{sub:coded-blocks}
3583 \paragraph{Input parameters:}\hfill\\*
3584 \begin{tabularx}{\textwidth}{@{}llrcX@{}}\toprule
3585 \multicolumn{1}{c}{Name} &
3586 \multicolumn{1}{c}{Type} &
3587 \multicolumn{1}{p{30pt}}{\centering Size (bits)} &
3588 \multicolumn{1}{c}{Signed?} &
3589 \multicolumn{1}{c}{Description and restrictions} \\\midrule\endhead
3590 \bitvar{FTYPE} & Integer & 1 & No & The frame type. \\
3591 \bitvar{NSBS} & Integer & 32 & No & The total number of super blocks in a
3592 frame. \\
3593 \bitvar{NBS} & Integer & 36 & No & The total number of blocks in a
3594 frame. \\
3595 \bottomrule\end{tabularx}
3597 \paragraph{Output parameters:}\hfill\\*
3598 \begin{tabularx}{\textwidth}{@{}llrcX@{}}\toprule
3599 \multicolumn{1}{c}{Name} &
3600 \multicolumn{1}{c}{Type} &
3601 \multicolumn{1}{p{30pt}}{\centering Size (bits)} &
3602 \multicolumn{1}{c}{Signed?} &
3603 \multicolumn{1}{c}{Description and restrictions} \\\midrule\endhead
3604 \bitvar{BCODED} & \multicolumn{1}{p{40pt}}{Integer Array} &
3605 1 & No & An \bitvar{NBS}-element array of flags
3606 indicating which blocks are coded. \\
3607 \bottomrule\end{tabularx}
3609 \paragraph{Variables used:}\hfill\\*
3610 \begin{tabularx}{\textwidth}{@{}llrcX@{}}\toprule
3611 \multicolumn{1}{c}{Name} &
3612 \multicolumn{1}{c}{Type} &
3613 \multicolumn{1}{p{30pt}}{\centering Size (bits)} &
3614 \multicolumn{1}{c}{Signed?} &
3615 \multicolumn{1}{c}{Description and restrictions} \\\midrule\endhead
3616 \locvar{NBITS} & Integer & 36 & No & The length of a bit string to decode. \\
3617 \locvar{BITS} & Bit string & & & A decoded set of flags. \\
3618 \locvar{SBPCODED} & \multicolumn{1}{p{40pt}}{Integer Array} &
3619 1 & No & An \bitvar{NSBS}-element array of flags
3620 indicating whether or not each super block is partially coded. \\
3621 \locvar{SBFCODED} & \multicolumn{1}{p{40pt}}{Integer Array} &
3622 1 & No & An \bitvar{NSBS}-element array of flags
3623 indicating whether or not each non-partially coded super block is fully
3624 coded. \\
3625 \locvar{\sbi} & Integer & 32 & No & The index of the current super
3626 block. \\
3627 \locvar{\bi} & Integer & 36 & No & The index of the current block in coded
3628 order. \\
3629 \bottomrule\end{tabularx}
3630 \medskip
3632 This procedure determines which blocks are coded in a given frame.
3633 In an intra frame, it marks all blocks coded.
3634 In an inter frame, however, any or all of the blocks may remain uncoded.
3635 The output is a list of bit flags, one for each block, marking it coded or not
3636 coded.
3638 It is important to note that flags are still decoded for any blocks which lie
3639 entirely outside the picture region, even though they are not displayed.
3640 Encoders MAY choose to code such blocks.
3641 Decoders MUST faithfully reconstruct such blocks, because their contents can be
3642 used for predictors in future frames.
3643 Flags are \textit{not} decoded for portions of a super block which lie outside
3644 the full frame, as there are no blocks in those regions.
3646 The complete procedure is as follows:
3648 \begin{enumerate}
3649 \item
3650 If \bitvar{FTYPE} is zero (intra frame):
3651 \begin{enumerate}
3652 \item
3653 For each consecutive value of \locvar{\bi} from 0 to $(\locvar{NBS}-1)$, assign
3654 $\bitvar{BCODED}[\locvar{\bi}]$ the value one.
3655 \end{enumerate}
3656 \item
3657 Otherwise (inter frame):
3658 \begin{enumerate}
3659 \item
3660 Assign \locvar{NBITS} the value \bitvar{NSBS}.
3661 \item
3662 Read an \locvar{NBITS}-bit bit string into \locvar{BITS}, using the procedure
3663 described in Section~\ref{sub:long-run}.
3664 This represents the list of partially coded super blocks.
3665 \item
3666 For each consecutive value of \locvar{\sbi} from 0 to $(\locvar{NSBS}-1)$,
3667 remove the bit at the head of the string \locvar{BITS} and assign it to
3668 $\locvar{SBPCODED}[\locvar{\sbi}]$.
3669 \item
3670 Assign \locvar{NBITS} the total number of super blocks such that \\
3671 $\locvar{SBPCODED}[\locvar{\sbi}]$ equals zero.
3672 \item
3673 Read an \locvar{NBITS}-bit bit string into \locvar{BITS}, using the procedure
3674 described in Section~\ref{sub:long-run}.
3675 This represents the list of fully coded super blocks.
3676 \item
3677 For each consecutive value of \locvar{\sbi} from 0 to $(\locvar{NSBS}-1)$ such
3678 that $\locvar{SBPCODED}[\locvar{\sbi}]$ equals zero, remove the bit at the
3679 head of the string \locvar{BITS} and assign it to
3680 $\locvar{SBFCODED}[\locvar{\sbi}]$.
3681 \item
3682 Assign \locvar{NBITS} the number of blocks contained in super blocks where
3683 $\locvar{SBPCODED}[\locvar{\sbi}]$ equals one.
3684 Note that this might {\em not} be equal to 16 times the number of partially
3685 coded super blocks, since super blocks which overlap the edge of the frame
3686 will have fewer than 16 blocks in them.
3687 \item
3688 Read an \locvar{NBITS}-bit bit string into \locvar{BITS}, using the procedure
3689 described in Section~\ref{sub:short-run}.
3690 \item
3691 For each block in coded order---indexed by \locvar{\bi}:
3692 \begin{enumerate}
3693 \item
3694 Assign \locvar{\sbi} the index of the super block containing block
3695 \locvar{\bi}.
3696 \item
3697 If $\locvar{SBPCODED}[\locvar{\sbi}]$ is zero, assign
3698 $\bitvar{BCODED}[\locvar{\bi}]$ the value $\locvar{SBFCODED}[\locvar{\sbi}]$.
3699 \item
3700 Otherwise, remove the bit at the head of the string \locvar{BITS} and assign it
3701 to $\bitvar{BCODED}[\locvar{\bi}]$.
3702 \end{enumerate}
3703 \end{enumerate}
3704 \end{enumerate}
3706 \section{Macro Block Coding Modes}
3707 \label{sub:mb-modes}
3709 \paragraph{Input parameters:}\hfill\\*
3710 \begin{tabularx}{\textwidth}{@{}llrcX@{}}\toprule
3711 \multicolumn{1}{c}{Name} &
3712 \multicolumn{1}{c}{Type} &
3713 \multicolumn{1}{p{30pt}}{\centering Size (bits)} &
3714 \multicolumn{1}{c}{Signed?} &
3715 \multicolumn{1}{c}{Description and restrictions} \\\midrule\endhead
3716 \bitvar{FTYPE} & Integer & 1 & No & The frame type. \\
3717 \bitvar{NMBS} & Integer & 32 & No & The total number of macro blocks in a
3718 frame. \\
3719 \bitvar{NBS} & Integer & 36 & No & The total number of blocks in a
3720 frame. \\
3721 \bitvar{BCODED} & \multicolumn{1}{p{40pt}}{Integer Array} &
3722 1 & No & An \bitvar{NBS}-element array of flags
3723 indicating which blocks are coded. \\
3724 \bottomrule\end{tabularx}
3726 \paragraph{Output parameters:}\hfill\\*
3727 \begin{tabularx}{\textwidth}{@{}llrcX@{}}\toprule
3728 \multicolumn{1}{c}{Name} &
3729 \multicolumn{1}{c}{Type} &
3730 \multicolumn{1}{p{30pt}}{\centering Size (bits)} &
3731 \multicolumn{1}{c}{Signed?} &
3732 \multicolumn{1}{c}{Description and restrictions} \\\midrule\endhead
3733 \bitvar{MBMODES} & \multicolumn{1}{p{40pt}}{Integer Array} &
3734 3 & No & An \bitvar{NMBS}-element array of coding
3735 modes for each macro block. \\
3736 \bottomrule\end{tabularx}
3738 \paragraph{Variables used:}\hfill\\*
3739 \begin{tabularx}{\textwidth}{@{}llrcX@{}}\toprule
3740 \multicolumn{1}{c}{Name} &
3741 \multicolumn{1}{c}{Type} &
3742 \multicolumn{1}{p{30pt}}{\centering Size (bits)} &
3743 \multicolumn{1}{c}{Signed?} &
3744 \multicolumn{1}{c}{Description and restrictions} \\\midrule\endhead
3745 \locvar{MSCHEME} & Integer & 3 & No & The mode coding scheme. \\
3746 \locvar{MALPHABET} & \multicolumn{1}{p{40pt}}{Integer array}
3747 & 3 & No & The list of modes corresponding to each
3748 Huffman code. \\
3749 \locvar{\mbi} & Integer & 32 & No & The index of the current macro
3750 block. \\
3751 \locvar{\bi} & Integer & 36 & No & The index of the current block in
3752 coded order. \\
3753 \locvar{\mi} & Integer & 3 & No & The index of a Huffman code from
3754 Table~\ref{tab:mode-codes}, starting from $0$. \\
3755 \bottomrule\end{tabularx}
3756 \medskip
3758 In an intra frame, every macro block marked as coded in INTRA mode.
3759 In an inter frame, however, a macro block can be coded in one of eight coding
3760 modes, given in Table~\ref{tab:coding-modes}.
3761 All of the blocks in all color planes contained in a macro block will be
3762 assigned the coding mode of that macro block.
3764 \begin{table}[htbp]
3765 \begin{center}
3766 \begin{tabular}{cl}\toprule
3767 Index & Coding Mode \\\midrule
3768 $0$ & INTER\_NOMV \\
3769 $1$ & INTRA \\
3770 $2$ & INTER\_MV \\
3771 $3$ & INTER\_MV\_LAST \\
3772 $4$ & INTER\_MV\_LAST2 \\
3773 $5$ & INTER\_GOLDEN\_NOMV \\
3774 $6$ & INTER\_GOLDEN\_MV \\
3775 $7$ & INTER\_MV\_FOUR \\
3776 \bottomrule\end{tabular}
3777 \end{center}
3778 \caption{Macro Block Coding Modes}
3779 \label{tab:coding-modes}
3780 \end{table}
3782 An important thing to note is that a coding mode is only stored in the
3783 bitstream for a macro block if it has at least one {\em luma} block coded.
3784 A macro block that contains coded blocks in the chroma planes, but not in the
3785 luma plane, MUST be coded in INTER\_NOMV mode.
3786 Thus, no coding mode needs to be decoded for such a macro block.
3788 Coding modes are encoded using one of eight different schemes.
3789 Schemes 0 through 6 use the same simple Huffman code to represent the mode
3790 numbers, as given in Table~\ref{tab:mode-codes}.
3791 The difference in the schemes is the mode number assigned to each code.
3792 Scheme 0 uses an assignment specified in the bitstream, while schemes 1--6 use
3793 a fixed assignment, also given in Table~\ref{tab:mode-codes}.
3794 Scheme 7 simply codes each mode directly in the bitstream using three bits.
3796 \begin{table}[htbp]
3797 \begin{center}
3798 \begin{tabular}{lccccccc}\toprule
3799 Scheme & $1$ & $2$ & $3$ & $4$ & $5$ & $6$ & $7$ \\\cmidrule{2-7}
3800 Huffman Code & \multicolumn{6}{c}{Coding Mode} & \locvar{\mi} \\\midrule
3801 \bin{0} & $3$ & $3$ & $3$ & $3$ & $0$ & $0$ & $0$ \\
3802 \bin{10} & $4$ & $4$ & $2$ & $2$ & $3$ & $5$ & $1$ \\
3803 \bin{110} & $2$ & $0$ & $4$ & $0$ & $4$ & $3$ & $2$ \\
3804 \bin{1110} & $0$ & $2$ & $0$ & $4$ & $2$ & $4$ & $3$ \\
3805 \bin{11110} & $1$ & $1$ & $1$ & $1$ & $1$ & $2$ & $4$ \\
3806 \bin{111110} & $5$ & $5$ & $5$ & $5$ & $5$ & $1$ & $5$ \\
3807 \bin{1111110} & $6$ & $6$ & $6$ & $6$ & $6$ & $6$ & $6$ \\
3808 \bin{1111111} & $7$ & $7$ & $7$ & $7$ & $7$ & $7$ & $7$ \\
3809 \bottomrule\end{tabular}
3810 \end{center}
3811 \caption{Macro Block Mode Schemes}
3812 \label{tab:mode-codes}
3813 \end{table}
3815 \begin{enumerate}
3816 \item
3817 If \bitvar{FTYPE} is 0 (intra frame):
3818 \begin{enumerate}
3819 \item
3820 For each consecutive value of \locvar{\mbi} from 0 to $(\bitvar{NMBS}-1)$,
3821 inclusive, assign $\bitvar{MBMODES}[\mbi]$ the value 1 (INTRA).
3822 \end{enumerate}
3823 \item
3824 Otherwise (inter frame):
3825 \begin{enumerate}
3826 \item
3827 Read a 3-bit unsigned integer as \locvar{MSCHEME}.
3828 \item
3829 If \locvar{MSCHEME} is 0:
3830 \begin{enumerate}
3831 \item
3832 For each consecutive value of \locvar{MODE} from 0 to 7, inclusive:
3833 \begin{enumerate}
3834 \item
3835 Read a 3-bit unsigned integer as \locvar{\mi}.
3836 \item
3837 Assign $\locvar{MALPHABET}[\mi]$ the value \locvar{MODE}.
3838 \end{enumerate}
3839 \end{enumerate}
3840 \item
3841 Otherwise, if \locvar{MSCHEME} is not 7, assign the entries of
3842 \locvar{MALPHABET} the values in the corresponding column of
3843 Table~\ref{tab:mode-codes}.
3844 \item
3845 For each consecutive macro block in coded order (cf.
3846 Section~\ref{sec:mbs})---indexed by \locvar{\mbi}:
3847 \begin{enumerate}
3848 \item
3849 If a block \locvar{\bi} in the luma plane of macro block \locvar{\mbi} exists
3850 such that $\bitvar{BCODED}[\locvar{\bi}]$ is 1:
3851 \begin{enumerate}
3852 \item
3853 If \locvar{MSCHEME} is not 7, read one bit at a time until one of the Huffman
3854 codes in Table~\ref{tab:mode-codes} is recognized, and assign
3855 $\bitvar{MBMODES}[\locvar{\mbi}]$ the value
3856 $\locvar{MALPHABET}[\locvar{\mi}]$, where \locvar{\mi} is the index of the
3857 Huffman code decoded.
3858 \item
3859 Otherwise, read a 3-bit unsigned integer as $\bitvar{MBMODES}[\locvar{\mbi}]$.
3860 \end{enumerate}
3861 \item
3862 Otherwise, if no luma-plane blocks in the macro block are coded, assign
3863 $\bitvar{MBMODES}[\locvar{\mbi}]$ the value 0 (INTER\_NOMV).
3864 \end{enumerate}
3865 \end{enumerate}
3866 \end{enumerate}
3868 \section{Motion Vectors}
3870 In an intra frame, no motion vectors are used, and so motion vector decoding is
3871 skipped.
3872 In an inter frame, however, many of the inter coding modes require a motion
3873 vector in order to specify an offset into the reference frame from which to
3874 predict a block.
3875 These procedures assigns such a motion vector to every block.
3877 \subsection{Motion Vector Decode}
3878 \label{sub:mv-decode}
3880 \paragraph{Input parameters:}\hfill\\*
3881 \begin{tabularx}{\textwidth}{@{}llrcX@{}}\toprule
3882 \multicolumn{1}{c}{Name} &
3883 \multicolumn{1}{c}{Type} &
3884 \multicolumn{1}{p{30pt}}{\centering Size (bits)} &
3885 \multicolumn{1}{c}{Signed?} &
3886 \multicolumn{1}{c}{Description and restrictions} \\\midrule\endhead
3887 \bitvar{MVMODE} & Integer & 1 & No & The motion vector decoding method. \\
3888 \bottomrule\end{tabularx}
3890 \paragraph{Output parameters:}\hfill\\*
3891 \begin{tabularx}{\textwidth}{@{}llrcX@{}}\toprule
3892 \multicolumn{1}{c}{Name} &
3893 \multicolumn{1}{c}{Type} &
3894 \multicolumn{1}{p{30pt}}{\centering Size (bits)} &
3895 \multicolumn{1}{c}{Signed?} &
3896 \multicolumn{1}{c}{Description and restrictions} \\\midrule\endhead
3897 \bitvar{MVX} & Integer & 6 & Yes & The X component of the motion
3898 vector. \\
3899 \bitvar{MVY} & Integer & 6 & Yes & The Y component of the motion
3900 vector. \\
3901 \bottomrule\end{tabularx}
3903 \paragraph{Variables used:}\hfill\\*
3904 \begin{tabularx}{\textwidth}{@{}llrcX@{}}\toprule
3905 \multicolumn{1}{c}{Name} &
3906 \multicolumn{1}{c}{Type} &
3907 \multicolumn{1}{p{30pt}}{\centering Size (bits)} &
3908 \multicolumn{1}{c}{Signed?} &
3909 \multicolumn{1}{c}{Description and restrictions} \\\midrule\endhead
3910 \locvar{MVSIGN} & Integer & 1 & No & The sign of the motion vector component
3911 just decoded. \\
3912 \bottomrule\end{tabularx}
3913 \medskip
3915 The individual components of a motion vector can be coded using one of two
3916 methods.
3917 The first uses a variable length Huffman code, given in
3918 Table~\ref{tab:mv-huff-codes}.
3919 The second encodes the magnitude of the component directly in 5 bits, and the
3920 sign in one bit.
3921 Note that in this case there are two representations for the value zero.
3922 For compatibility with VP3, a sign bit is read even if the magnitude read is
3923 zero.
3924 One scheme is chosen and used for the entire frame.
3926 Each component can take on integer values from $-31\ldots 31$, inclusive, at
3927 half-pixel resolution, i.e. $-15.5\ldots 15.5$ pixels in the luma plane.
3928 For each subsampled axis in the chroma planes, the corresponding motion vector
3929 component is interpreted as being at quarter-pixel resolution, i.e.
3930 $-7.75\ldots 7.75$ pixels.
3931 The precise details of how these vectors are used to compute predictors for
3932 each block are described in Section~\ref{sec:predictors}.
3934 \begin{table}[ht]
3935 \begin{center}
3936 \begin{tabular}{lrlr}\toprule
3937 Huffman Code & Value & Huffman Code & Value \\\midrule
3938 \bin{000} & $0$ \\
3939 \bin{001} & $1$ & \bin{010} & $-1$ \\
3940 \bin{0110} & $2$ & \bin{0111} & $-2$ \\
3941 \bin{1000} & $3$ & \bin{1001} & $-3$ \\
3942 \bin{101000} & $4$ & \bin{101001} & $-4$ \\
3943 \bin{101010} & $5$ & \bin{101011} & $-5$ \\
3944 \bin{101100} & $6$ & \bin{101101} & $-6$ \\
3945 \bin{101110} & $7$ & \bin{101111} & $-7$ \\
3946 \bin{1100000} & $8$ & \bin{1100001} & $-8$ \\
3947 \bin{1100010} & $9$ & \bin{1100011} & $-9$ \\
3948 \bin{1100100} & $10$ & \bin{1100101} & $-10$ \\
3949 \bin{1100110} & $11$ & \bin{1100111} & $-11$ \\
3950 \bin{1101000} & $12$ & \bin{1101001} & $-12$ \\
3951 \bin{1101010} & $13$ & \bin{1101011} & $-13$ \\
3952 \bin{1101100} & $14$ & \bin{1101101} & $-14$ \\
3953 \bin{1101110} & $15$ & \bin{1101111} & $-15$ \\
3954 \bin{11100000} & $16$ & \bin{11100001} & $-16$ \\
3955 \bin{11100010} & $17$ & \bin{11100011} & $-17$ \\
3956 \bin{11100100} & $18$ & \bin{11100101} & $-18$ \\
3957 \bin{11100110} & $19$ & \bin{11100111} & $-19$ \\
3958 \bin{11101000} & $20$ & \bin{11101001} & $-20$ \\
3959 \bin{11101010} & $21$ & \bin{11101011} & $-21$ \\
3960 \bin{11101100} & $22$ & \bin{11101101} & $-22$ \\
3961 \bin{11101110} & $23$ & \bin{11101111} & $-23$ \\
3962 \bin{11110000} & $24$ & \bin{11110001} & $-24$ \\
3963 \bin{11110010} & $25$ & \bin{11110011} & $-25$ \\
3964 \bin{11110100} & $26$ & \bin{11110101} & $-26$ \\
3965 \bin{11110110} & $27$ & \bin{11110111} & $-27$ \\
3966 \bin{11111000} & $28$ & \bin{11111001} & $-28$ \\
3967 \bin{11111010} & $29$ & \bin{11111011} & $-29$ \\
3968 \bin{11111100} & $30$ & \bin{11111101} & $-30$ \\
3969 \bin{11111110} & $31$ & \bin{11111111} & $-31$ \\
3970 \bottomrule\end{tabular}
3971 \end{center}
3972 \caption{Huffman Codes for Motion Vector Components}
3973 \label{tab:mv-huff-codes}
3974 \end{table}
3976 A single motion vector is decoded is follows:
3978 \begin{enumerate}
3979 \item
3980 If \bitvar{MVMODE} is 0:
3981 \begin{enumerate}
3982 \item
3983 Read 1 bit at a time until one of the Huffman codes in
3984 Table~\ref{tab:mv-huff-codes} is recognized, and assign the value to
3985 \locvar{MVX}.
3986 \item
3987 Read 1 bit at a time until one of the Huffman codes in
3988 Table~\ref{tab:mv-huff-codes} is recognized, and assign the value to
3989 \locvar{MVY}.
3990 \end{enumerate}
3991 \item
3992 Otherwise:
3993 \begin{enumerate}
3994 \item
3995 Read a 5-bit unsigned integer as \bitvar{MVX}.
3996 \item
3997 Read a 1-bit unsigned integer as \locvar{MVSIGN}.
3998 \item
3999 If \locvar{MVSIGN} is 1, assign \bitvar{MVX} the value $-\bitvar{MVX}$.
4000 \item
4001 Read a 5-bit unsigned integer as \bitvar{MVY}.
4002 \item
4003 Read a 1-bit unsigned integer as \locvar{MVSIGN}.
4004 \item
4005 If \locvar{MVSIGN} is 1, assign \bitvar{MVY} the value $-\bitvar{MVY}$.
4006 \end{enumerate}
4007 \end{enumerate}
4009 \subsection{Macro Block Motion Vector Decode}
4010 \label{sub:mb-mv-decode}
4012 \paragraph{Input parameters:}\hfill\\*
4013 \begin{tabularx}{\textwidth}{@{}llrcX@{}}\toprule
4014 \multicolumn{1}{c}{Name} &
4015 \multicolumn{1}{c}{Type} &
4016 \multicolumn{1}{p{30pt}}{\centering Size (bits)} &
4017 \multicolumn{1}{c}{Signed?} &
4018 \multicolumn{1}{c}{Description and restrictions} \\\midrule\endhead
4019 \bitvar{PF} & Integer & 2 & No & The pixel format. \\
4020 \bitvar{NMBS} & Integer & 32 & No & The total number of macro blocks in a
4021 frame. \\
4022 \bitvar{MBMODES} & \multicolumn{1}{p{40pt}}{Integer Array} &
4023 3 & No & An \bitvar{NMBS}-element array of coding
4024 modes for each macro block. \\
4025 \bitvar{NBS} & Integer & 36 & No & The total number of blocks in a
4026 frame. \\
4027 \bitvar{BCODED} & \multicolumn{1}{p{40pt}}{Integer Array} &
4028 1 & No & An \bitvar{NBS}-element array of flags
4029 indicating which blocks are coded. \\
4030 \bottomrule\end{tabularx}
4032 \paragraph{Output parameters:}\hfill\\*
4033 \begin{tabularx}{\textwidth}{@{}llrcX@{}}\toprule
4034 \multicolumn{1}{c}{Name} &
4035 \multicolumn{1}{c}{Type} &
4036 \multicolumn{1}{p{30pt}}{\centering Size (bits)} &
4037 \multicolumn{1}{c}{Signed?} &
4038 \multicolumn{1}{c}{Description and restrictions} \\\midrule\endhead
4039 \bitvar{MVECTS} & \multicolumn{1}{p{50pt}}{Array of 2D Integer Vectors} &
4040 6 & Yes & An \bitvar{NBS}-element array of
4041 motion vectors for each block. \\
4042 \bottomrule\end{tabularx}
4044 \paragraph{Variables used:}\hfill\\*
4045 \begin{tabularx}{\textwidth}{@{}llrcX@{}}\toprule
4046 \multicolumn{1}{c}{Name} &
4047 \multicolumn{1}{c}{Type} &
4048 \multicolumn{1}{p{30pt}}{\centering Size (bits)} &
4049 \multicolumn{1}{c}{Signed?} &
4050 \multicolumn{1}{c}{Description and restrictions} \\\midrule\endhead
4051 \locvar{LAST1} & \multicolumn{1}{p{50pt}}{2D Integer Vector} &
4052 6 & Yes & The last motion vector. \\
4053 \locvar{LAST2} & \multicolumn{1}{p{50pt}}{2D Integer Vector} &
4054 6 & Yes & The second to last motion vector. \\
4055 \locvar{MVX} & Integer & 6 & Yes & The X component of a motion vector. \\
4056 \locvar{MVY} & Integer & 6 & Yes & The Y component of a motion vector. \\
4057 \locvar{\mbi} & Integer & 32 & No & The index of the current macro
4058 block. \\
4059 \locvar{A} & Integer & 36 & No & The index of the lower-left luma block
4060 in the macro block. \\
4061 \locvar{B} & Integer & 36 & No & The index of the lower-right luma
4062 block in the macro block. \\
4063 \locvar{C} & Integer & 36 & No & The index of the upper-left luma block
4064 in the macro block. \\
4065 \locvar{D} & Integer & 36 & No & The index of the upper-right luma
4066 block in the macro block. \\
4067 \locvar{E} & Integer & 36 & No & The index of a chroma block in the
4068 macro block, depending on the pixel format. \\
4069 \locvar{F} & Integer & 36 & No & The index of a chroma block in the
4070 macro block, depending on the pixel format. \\
4071 \locvar{G} & Integer & 36 & No & The index of a chroma block in the
4072 macro block, depending on the pixel format. \\
4073 \locvar{H} & Integer & 36 & No & The index of a chroma block in the
4074 macro block, depending on the pixel format. \\
4075 \locvar{I} & Integer & 36 & No & The index of a chroma block in the
4076 macro block, depending on the pixel format. \\
4077 \locvar{J} & Integer & 36 & No & The index of a chroma block in the
4078 macro block, depending on the pixel format. \\
4079 \locvar{K} & Integer & 36 & No & The index of a chroma block in the
4080 macro block, depending on the pixel format. \\
4081 \locvar{L} & Integer & 36 & No & The index of a chroma block in the
4082 macro block, depending on the pixel format. \\
4083 \bottomrule\end{tabularx}
4084 \medskip
4086 Motion vectors are stored for each macro block.
4087 In every mode except for INTER\_MV\_FOUR, every block in all the color planes
4088 are assigned the same motion vector.
4089 In INTER\_MV\_FOUR mode, all four blocks in the luma plane are assigned their
4090 own motion vector, and motion vectors for blocks in the chroma planes are
4091 computed from these, using averaging appropriate to the pixel format.
4093 For INTER\_MV and INTER\_GOLDEN\_MV modes, a single motion vector is decoded
4094 and applied to each block.
4095 For INTER\_MV\_FOUR macro blocks, a motion vector is decoded for each coded
4096 luma block.
4097 Uncoded luma blocks receive the default $(0,0)$ vector for the purposes of
4098 computing the chroma motion vectors.
4100 None of the remaining macro block coding modes require decoding motion vectors
4101 from the stream.
4102 INTRA mode does not use a motion-compensated predictor, and so requires no
4103 motion vector, and INTER\_NOMV and INTER\_GOLDEN\_NOMV modes use the default
4104 vector $(0,0)$ for each block.
4105 This also includes all macro blocks with no coded luma blocks, as they are
4106 coded in INTER\_NOMV mode by definition.
4108 The modes INTER\_MV\_LAST and INTER\_MV\_LAST2 use the motion vector from the
4109 last macro block (in coded order) and the second to last macro block,
4110 respectively, that contained a motion vector pointing to the previous frame.
4111 Thus no explicit motion vector needs to be decoded for these modes.
4112 Macro blocks coded in INTRA mode or one of the GOLDEN modes are not considered
4113 in this process.
4114 If an insufficient number of macro blocks have been coded in one of the INTER
4115 modes, then the $(0,0)$ vector is used instead.
4116 For macro blocks coded in INTER\_MV\_FOUR mode, the vector from the upper-right
4117 luma block is used, even if the upper-right block is not coded.
4119 The motion vectors are decoded from the stream as follows:
4121 \begin{enumerate}
4122 \item
4123 Assign \locvar{LAST1} and \locvar{LAST2} both the value $(0,0)$.
4124 \item
4125 Read a 1-bit unsigned integer as \locvar{MVMODE}.
4126 Note that this value is read even if no macro blocks require a motion vector to
4127 be decoded.
4128 \item
4129 For each consecutive value of \locvar{\mbi} from 0 to $(\bitvar{NMBS}-1)$:
4130 \begin{enumerate}
4131 \item
4132 If $\bitvar{MBMODES}[\locvar{\mbi}]$ is 7 (INTER\_MV\_FOUR):
4133 \begin{enumerate}
4134 \item
4135 Let \locvar{A}, \locvar{B}, \locvar{C}, and \locvar{D} be the indices in coded
4136 order \locvar{\bi} of the luma blocks in macro block \locvar{\mbi}, arranged
4137 into raster order.
4138 Thus, \locvar{A} is the index in coded order of the block in the lower left,
4139 \locvar{B} the lower right, \locvar{C} the upper left, and \locvar{D} the
4140 upper right. % TODO: as shown in Figure~REF.
4141 \item If $\bitvar{BCODED}[\locvar{A}]$ is non-zero:
4142 \begin{enumerate}
4143 \item Decode a single motion vector into \locvar{MVX} and \locvar{MVY} using
4144 the procedure described in Section~\ref{sub:mv-decode}.
4145 \item Assign $\bitvar{MVECTS}[\locvar{A}]$ the value
4146 $(\locvar{MVX},\locvar{MVY})$.
4147 \end{enumerate}
4148 \item Otherwise, assign $\bitvar{MVECTS}[\locvar{A}]$ the value $(0,0)$.
4149 \item If $\bitvar{BCODED}[\locvar{B}]$ is non-zero:
4150 \begin{enumerate}
4151 \item Decode a single motion vector into \locvar{MVX} and \locvar{MVY} using
4152 the procedure described in Section~\ref{sub:mv-decode}.
4153 \item Assign $\bitvar{MVECTS}[\locvar{B}]$ the value
4154 $(\locvar{MVX},\locvar{MVY})$.
4155 \end{enumerate}
4156 \item
4157 Otherwise assign $\bitvar{MVECTS}[\locvar{B}]$ the value $(0,0)$.
4158 \item If $\bitvar{BCODED}[\locvar{C}]$ is non-zero:
4159 \begin{enumerate}
4160 \item Decode a single motion vector into \locvar{MVX} and \locvar{MVY} using
4161 the procedure described in Section~\ref{sub:mv-decode}.
4162 \item Assign $\bitvar{MVECTS}[\locvar{C}]$ the value
4163 $(\locvar{MVX},\locvar{MVY})$.
4164 \end{enumerate}
4165 \item Otherwise assign $\bitvar{MVECTS}[\locvar{C}]$ the value $(0,0)$.
4166 \item If $\bitvar{BCODED}[\locvar{D}]$ is non-zero:
4167 \begin{enumerate}
4168 \item Decode a single motion vector into \locvar{MVX} and \locvar{MVY} using
4169 the procedure described in Section~\ref{sub:mv-decode}.
4170 \item Assign $\bitvar{MVECTS}[\locvar{D}]$ the value
4171 $(\locvar{MVX},\locvar{MVY})$.
4172 \end{enumerate}
4173 \item
4174 Otherwise, assign $\bitvar{MVECTS}[\locvar{D}]$ the value $(0,0)$.
4175 \item
4176 If \bitvar{PF} is 0 (4:2:0):
4177 \begin{enumerate}
4178 \item
4179 Let \locvar{E} and \locvar{F} be the index in coded order of the one block in
4180 the macro block from the $C_b$ and $C_r$ planes, respectively.
4181 \item
4182 Assign $\bitvar{MVECTS}[\locvar{E}]$ and $\bitvar{MVECTS}[\locvar{F}]$ the
4183 value
4184 \begin{multline*}
4185 (\round\biggl(\frac{\begin{aligned}
4186 \bitvar{MVECTS}[\locvar{A}]_x+\bitvar{MVECTS}[\locvar{B}]_x+\\
4187 \bitvar{MVECTS}[\locvar{C}]_x+\bitvar{MVECTS}[\locvar{D}]_x
4188 \end{aligned}}{4}\biggr), \\
4189 \round\biggl(\frac{\begin{aligned}
4190 \bitvar{MVECTS}[\locvar{A}]_y+\bitvar{MVECTS}[\locvar{B}]_y+\\
4191 \bitvar{MVECTS}[\locvar{C}]_y+\bitvar{MVECTS}[\locvar{D}]_y
4192 \end{aligned}}{4}\biggr))
4193 \end{multline*}
4194 \end{enumerate}
4195 \item
4196 If \bitvar{PF} is 2 (4:2:2):
4197 \begin{enumerate}
4198 \item
4199 Let \locvar{E} and \locvar{F} be the indices in coded order of the bottom and
4200 top blocks in the macro block from the $C_b$ plane, respectively, and
4201 \locvar{G} and \locvar{H} be the indices in coded order of the bottom and top
4202 blocks in the $C_r$ plane, respectively. %TODO: as shown in Figure~REF.
4203 \item
4204 Assign $\bitvar{MVECTS}[\locvar{E}]$ and $\bitvar{MVECTS}[\locvar{G}]$ the
4205 value
4206 \begin{multline*}
4207 (\round\left(\frac{
4208 \bitvar{MVECTS}[\locvar{A}]_x+\bitvar{MVECTS}[\locvar{B}]_x}{2}\right), \\
4209 \round\left(\frac{
4210 \bitvar{MVECTS}[\locvar{A}]_y+\bitvar{MVECTS}[\locvar{B}]_y}{2}\right))
4211 \end{multline*}
4212 \item
4213 Assign $\bitvar{MVECTS}[\locvar{F}]$ and $\bitvar{MVECTS}[\locvar{H}]$ the
4214 value
4215 \begin{multline*}
4216 (\round\left(\frac{
4217 \bitvar{MVECTS}[\locvar{C}]_x+\bitvar{MVECTS}[\locvar{D}]_x}{2}\right), \\
4218 \round\left(\frac{
4219 \bitvar{MVECTS}[\locvar{C}]_y+\bitvar{MVECTS}[\locvar{D}]_y}{2}\right))
4220 \end{multline*}
4221 \end{enumerate}
4222 \item
4223 If \bitvar{PF} is 3 (4:4:4):
4224 \begin{enumerate}
4225 \item
4226 Let \locvar{E}, \locvar{F}, \locvar{G}, and \locvar{H} be the indices
4227 \locvar{\bi} in coded order of the $C_b$ plane blocks in macro block
4228 \locvar{\mbi}, arranged into raster order, and \locvar{I}, \locvar{J},
4229 \locvar{K}, and \locvar{L} be the indices \locvar{\bi} in coded order of the
4230 $C_r$ plane blocks in macro block \locvar{\mbi}, arranged into raster order.
4231 %TODO: as shown in Figure~REF.
4232 \item
4233 Assign $\bitvar{MVECTS}[\locvar{E}]$ and $\bitvar{MVECTS}[\locvar{I}]$ the
4234 value \\ $\bitvar{MVECTS}[\locvar{A}]$.
4235 \item
4236 Assign $\bitvar{MVECTS}[\locvar{F}]$ and $\bitvar{MVECTS}[\locvar{J}]$ the
4237 value \\ $\bitvar{MVECTS}[\locvar{B}]$.
4238 \item
4239 Assign $\bitvar{MVECTS}[\locvar{G}]$ and $\bitvar{MVECTS}[\locvar{K}]$ the
4240 value \\ $\bitvar{MVECTS}[\locvar{C}]$.
4241 \item
4242 Assign $\bitvar{MVECTS}[\locvar{H}]$ and $\bitvar{MVECTS}[\locvar{L}]$ the
4243 value \\ $\bitvar{MVECTS}[\locvar{D}]$.
4244 \end{enumerate}
4245 \item
4246 Assign \locvar{LAST2} the value \locvar{LAST1}.
4247 \item
4248 Assign \locvar{LAST1} the value $(\locvar{MVX},\locvar{MVY})$.
4249 This is the value of the motion vector decoded from the last coded luma block
4250 in raster order.
4251 There must always be at least one, since macro blocks with no coded luma blocks
4252 must use mode 0:~INTER\_NOMV.
4253 \end{enumerate}
4254 \item
4255 Otherwise, if $\bitvar{MBMODES}[\locvar{\mbi}]$ is 6 (INTER\_GOLDEN\_MV),
4256 decode a single motion vector into \locvar{MVX} and \locvar{MVY} using the
4257 procedure described in Section~\ref{sub:mv-decode}.
4258 \item
4259 Otherwise, if $\bitvar{MBMODES}[\locvar{\mbi}]$ is 4 (INTER\_MV\_LAST2):
4260 \begin{enumerate}
4261 \item
4262 Assign $(\locvar{MVX},\locvar{MVY})$ the value \locvar{LAST2}.
4263 \item
4264 Assign \locvar{LAST2} the value \locvar{LAST1}.
4265 \item
4266 Assign \locvar{LAST1} the value $(\locvar{MVX},\locvar{MVY})$.
4267 \end{enumerate}
4268 \item
4269 Otherwise, if $\bitvar{MBMODES}[\locvar{\mbi}]$ is 3 (INTER\_MV\_LAST), assign
4270 $(\locvar{MVX},\locvar{MVY})$ the value \locvar{LAST1}.
4271 \item
4272 Otherwise, if $\bitvar{MBMODES}[\locvar{\mbi}]$ is 2 (INTER\_MV):
4273 \begin{enumerate}
4274 \item
4275 Decode a single motion vector into \locvar{MVX} and \locvar{MVY} using the
4276 procedure described in Section~\ref{sub:mv-decode}.
4277 \item
4278 Assign \locvar{LAST2} the value \locvar{LAST1}.
4279 \item
4280 Assign \locvar{LAST1} the value $(\locvar{MVX},\locvar{MVY})$.
4281 \end{enumerate}
4282 \item
4283 Otherwise ($\bitvar{MBMODES}[\locvar{\mbi}]$ is 5:~INTER\_GOLDEN\_NOMV,
4284 1:~INTRA, or 0:~INTER\_NOMV), assign \locvar{MVX} and \locvar{MVY} the value
4285 zero.
4286 \item
4287 If $\bitvar{MBMODES}[\locvar{\mbi}]$ is not 7 (not INTER\_MV\_FOUR), then for
4288 each coded block \locvar{\bi} in macro block \locvar{\mbi}:
4289 \begin{enumerate}
4290 \item
4291 Assign $\bitvar{MVECTS}[\locvar{\bi}]$ the value $(\locvar{MVX},\locvar{MVY})$.
4292 \end{enumerate}
4293 \end{enumerate}
4294 \end{enumerate}
4296 \paragraph{VP3 Compatibility}
4298 Unless all four luma blocks in the macro block are coded, the VP3 encoder does
4299 not select mode INTER\_MV\_FOUR.
4300 Theora removes this restriction by treating the motion vector for an uncoded
4301 luma block as the default $(0,0)$ vector.
4302 This is consistent with the premise that the block has not changed since the
4303 previous frame and that chroma information can be largely ignored when
4304 estimating motion.
4306 No modification is required for INTER\_MV\_FOUR macro blocks in VP3 streams to
4307 be decoded correctly by a Theora decoder.
4308 However, regardless of how many of the luma blocks are actually coded, the VP3
4309 decoder always reads four motion vectors from the stream for INTER\_MV\_FOUR
4310 mode.
4311 The motion vectors read are used to calculate the motion vectors for the chroma
4312 blocks, but are otherwise ignored.
4313 Thus, care should be taken when creating Theora streams meant to be backwards
4314 compatible with VP3 to only use INTER\_MV\_FOUR mode when all four luma
4315 blocks are coded.
4317 \section{Block-Level \qi\ Decode}
4318 \label{sub:block-qis}
4320 \paragraph{Input parameters:}\hfill\\*
4321 \begin{tabularx}{\textwidth}{@{}llrcX@{}}\toprule
4322 \multicolumn{1}{c}{Name} &
4323 \multicolumn{1}{c}{Type} &
4324 \multicolumn{1}{p{30pt}}{\centering Size (bits)} &
4325 \multicolumn{1}{c}{Signed?} &
4326 \multicolumn{1}{c}{Description and restrictions} \\\midrule\endhead
4327 \bitvar{NBS} & Integer & 36 & No & The total number of blocks in a
4328 frame. \\
4329 \bitvar{BCODED} & \multicolumn{1}{p{40pt}}{Integer Array} &
4330 1 & No & An \bitvar{NBS}-element array of flags
4331 indicating which blocks are coded. \\
4332 \bitvar{NQIS} & Integer & 2 & No & The number of \qi\ values. \\
4333 \bottomrule\end{tabularx}
4335 \paragraph{Output parameters:}\hfill\\*
4336 \begin{tabularx}{\textwidth}{@{}llrcX@{}}\toprule
4337 \multicolumn{1}{c}{Name} &
4338 \multicolumn{1}{c}{Type} &
4339 \multicolumn{1}{p{30pt}}{\centering Size (bits)} &
4340 \multicolumn{1}{c}{Signed?} &
4341 \multicolumn{1}{c}{Description and restrictions} \\\midrule\endhead
4342 \bitvar{QIIS} & \multicolumn{1}{p{40pt}}{Integer Array} &
4343 2 & No & An \bitvar{NBS}-element array of
4344 \locvar{\qii} values for each block. \\
4345 \bottomrule\end{tabularx}
4347 \paragraph{Variables used:}\hfill\\*
4348 \begin{tabularx}{\textwidth}{@{}llrcX@{}}\toprule
4349 \multicolumn{1}{c}{Name} &
4350 \multicolumn{1}{c}{Type} &
4351 \multicolumn{1}{p{30pt}}{\centering Size (bits)} &
4352 \multicolumn{1}{c}{Signed?} &
4353 \multicolumn{1}{c}{Description and restrictions} \\\midrule\endhead
4354 \locvar{NBITS} & Integer & 36 & No & The length of a bit string to decode. \\
4355 \locvar{BITS} & Bit string & & & A decoded set of flags. \\
4356 \locvar{\bi} & Integer & 36 & No & The index of the current block in
4357 coded order. \\
4358 \locvar{\qii} & Integer & 2 & No & The index of \qi\ value in the list of
4359 \qi\ values defined for this frame. \\
4360 \bottomrule\end{tabularx}
4361 \medskip
4363 This procedure selects the \qi\ value to be used for dequantizing the AC
4364 coefficients of each block.
4365 DC coefficients all use the same \qi\ value, so as to avoid interference with
4366 the DC prediction mechanism, which occurs in the quantized domain.
4368 The value is actually represented by an index \locvar{\qii} into the list of
4369 \qi\ values defined for the frame.
4370 The decoder makes multiple passes through the list of coded blocks, one for
4371 each \qi\ value except the last one.
4372 In each pass, an RLE-coded bitmask is decoded to divide the blocks into two
4373 groups: those that use the current \qi\ value in the list, and those that use
4374 a value from later in the list.
4375 Each subsequent pass is restricted to the blocks in the second group.
4377 \begin{enumerate}
4378 \item
4379 For each value of \locvar{\bi} from 0 to $(\bitvar{NBS}-1)$, assign
4380 $\bitvar{QIIS}[\locvar{\bi}]$ the value zero.
4381 \item
4382 For each consecutive value of \locvar{\qii} from 0 to $(\bitvar{NQIS}-2)$:
4383 \begin{enumerate}
4384 \item
4385 Assign \locvar{NBITS} be the number of blocks \locvar{\bi} such that
4386 $\bitvar{BCODED}[\locvar{\bi}]$ is non-zero and $\bitvar{QIIS}[\locvar{\bi}]$
4387 equals $\locvar{\qii}$.
4388 \item
4389 Read an \locvar{NBITS}-bit bit string into \locvar{BITS}, using the procedure
4390 described in Section~\ref{sub:long-run}.
4391 This represents the list of blocks that use \qi\ value \locvar{\qii} or higher.
4392 \item
4393 For each consecutive value of \locvar{\bi} from 0 to $(\bitvar{NBS}-1)$ such
4394 that $\bitvar{BCODED}[\locvar{\bi}]$ is non-zero and
4395 $\bitvar{QIIS}[\locvar{\bi}]$ equals $\locvar{\qii}$:
4396 \begin{enumerate}
4397 \item
4398 Remove the bit at the head of the string \locvar{BITS} and add its value to
4399 $\bitvar{QIIS}[\locvar{\bi}]$.
4400 \end{enumerate}
4401 \end{enumerate}
4402 \end{enumerate}
4404 \paragraph{VP3 Compatibility}
4406 For VP3 compatible streams, only one \qi\ value can be specified in the frame
4407 header, so the main loop of the above procedure, which would iterate from $0$
4408 to $-1$, is never executed.
4409 Thus, no bits are read, and each block uses the one \qi\ value defined for the
4410 frame.
4412 \cleardoublepage
4414 \section{DCT Coefficients}
4415 \label{sec:dct-decode}
4417 The quantized DCT coefficients are decoded by making 64 passes through the list
4418 of coded blocks, one for each token index in zig-zag order.
4419 For the DC tokens, two Huffman tables are chosen from among the first 16, one
4420 for the luma plane and one for the chroma planes.
4421 The AC tokens, however, are divided into four different groups.
4422 Again, two 4-bit indices are decoded, one for the luma plane, and one for the
4423 chroma planes, but these select the codebooks for {\em all four} groups.
4424 AC coefficients in group one use codebooks $16\ldots 31$, while group two uses
4425 $32\ldots 47$, etc.
4426 Note that this second set of indices is decoded even if there are no non-zero
4427 AC coefficients in the frame.
4429 Tokens are divided into two major types: EOB tokens, which fill the remainder
4430 of one or more blocks with zeros, and coefficient tokens, which fill in one or
4431 more coefficients within a single block.
4432 A decoding procedure for the first is given in Section~\ref{sub:eob-token}, and
4433 for the second in Section~\ref{sub:coeff-token}.
4434 The decoding procedure for the complete set of quantized coefficients is given
4435 in Section~\ref{sub:dct-coeffs}.
4437 \subsection{EOB Token Decode}
4438 \label{sub:eob-token}
4440 \paragraph{Input parameters:}\hfill\\*
4441 \begin{tabularx}{\textwidth}{@{}llrcX@{}}\toprule
4442 \multicolumn{1}{c}{Name} &
4443 \multicolumn{1}{c}{Type} &
4444 \multicolumn{1}{p{30pt}}{\centering Size (bits)} &
4445 \multicolumn{1}{c}{Signed?} &
4446 \multicolumn{1}{c}{Description and restrictions} \\\midrule\endhead
4447 \bitvar{TOKEN} & Integer & 5 & No & The token being decoded.
4448 This must be in the range $0\ldots 6$. \\
4449 \bitvar{NBS} & Integer & 36 & No & The total number of blocks in a
4450 frame. \\
4451 \bitvar{TIS} & \multicolumn{1}{p{40pt}}{Integer Array} &
4452 7 & No & An \bitvar{NBS}-element array of the
4453 current token index for each block. \\
4454 \bitvar{NCOEFFS} & \multicolumn{1}{p{40pt}}{Integer Array} &
4455 7 & No & An \bitvar{NBS}-element array of the
4456 coefficient count for each block. \\
4457 \bitvar{COEFFS} & \multicolumn{1}{p{50pt}}{2D Integer Array} &
4458 16 & Yes & An $\bitvar{NBS}\times 64$ array of
4459 quantized DCT coefficient values for each block in zig-zag order. \\
4460 \bitvar{\bi} & Integer & 36 & No & The index of the current block in
4461 coded order. \\
4462 \bitvar{\ti} & Integer & 6 & No & The current token index. \\
4463 \bottomrule\end{tabularx}
4465 \paragraph{Output parameters:}\hfill\\*
4466 \begin{tabularx}{\textwidth}{@{}llrcX@{}}\toprule
4467 \multicolumn{1}{c}{Name} &
4468 \multicolumn{1}{c}{Type} &
4469 \multicolumn{1}{p{30pt}}{\centering Size (bits)} &
4470 \multicolumn{1}{c}{Signed?} &
4471 \multicolumn{1}{c}{Description and restrictions} \\\midrule\endhead
4472 \bitvar{TIS} & \multicolumn{1}{p{40pt}}{Integer Array} &
4473 7 & No & An \bitvar{NBS}-element array of the
4474 current token index for each block. \\
4475 \bitvar{COEFFS} & \multicolumn{1}{p{50pt}}{2D Integer Array} &
4476 16 & Yes & An $\bitvar{NBS}\times 64$ array of
4477 quantized DCT coefficient values for each block in zig-zag order. \\
4478 \bitvar{EOBS} & Integer & 36 & No & The remaining length of the current
4479 EOB run. \\
4480 \bottomrule\end{tabularx}
4482 \paragraph{Variables used:}\hfill\\*
4483 \begin{tabularx}{\textwidth}{@{}llrcX@{}}\toprule
4484 \multicolumn{1}{c}{Name} &
4485 \multicolumn{1}{c}{Type} &
4486 \multicolumn{1}{p{30pt}}{\centering Size (bits)} &
4487 \multicolumn{1}{c}{Signed?} &
4488 \multicolumn{1}{c}{Description and restrictions} \\\midrule\endhead
4489 \locvar{\bj} & Integer & 36 & No & Another index of a block in coded
4490 order. \\
4491 \locvar{\tj} & Integer & 6 & No & Another token index. \\
4492 \bottomrule\end{tabularx}
4493 \medskip
4495 A summary of the EOB tokens is given in Table~\ref{tab:eob-tokens}.
4496 An important thing to note is that token 6 does not add an offset to the
4497 decoded run value, even though in general it should only be used for runs of
4498 size 32 or longer.
4499 If a value of zero is decoded for this run, it is treated as an EOB run the
4500 size of the remaining coded blocks.
4502 \begin{table}[htbp]
4503 \begin{center}
4504 \begin{tabular}{ccl}\toprule
4505 Token Value & Extra Bits & EOB Run Lengths \\\midrule
4506 $0$ & $0$ & $1$ \\
4507 $1$ & $0$ & $2$ \\
4508 $2$ & $0$ & $3$ \\
4509 $3$ & $2$ & $4\ldots 7$ \\
4510 $4$ & $3$ & $8\ldots 15$ \\
4511 $5$ & $4$ & $16\ldots 31$ \\
4512 $6$ & $12$ & $1\ldots 4095$, or all remaining blocks \\
4513 \bottomrule\end{tabular}
4514 \end{center}
4515 \caption{EOB Token Summary}
4516 \label{tab:eob-tokens}
4517 \end{table}
4519 There is no restriction that one EOB token cannot be immediately followed by
4520 another, so no special cases are necessary to extend the range of the maximum
4521 run length as were required in Section~\ref{sub:long-run}.
4522 Indeed, depending on the lengths of the Huffman codes, it may even cheaper to
4523 encode, by way of example, an EOB run of length 31 followed by an EOB run of
4524 length 1 than to encode an EOB run of length 32 directly.
4525 There is also no restriction that an EOB run stop at the end of a color plane
4526 or a token index.
4527 The run MUST, however, end at or before the end of the frame.
4529 \begin{enumerate}
4530 \item
4531 If \bitvar{TOKEN} is 0, assign \bitvar{EOBS} the value 1.
4532 \item
4533 Otherwise, if \bitvar{TOKEN} is 1, assign \bitvar{EOBS} the value 2.
4534 \item
4535 Otherwise, if \bitvar{TOKEN} is 2, assign \bitvar{EOBS} the value 3.
4536 \item
4537 Otherwise, if \bitvar{TOKEN} is 3:
4538 \begin{enumerate}
4539 \item
4540 Read a 2-bit unsigned integer as \bitvar{EOBS}.
4541 \item
4542 Assign \bitvar{EOBS} the value $(\bitvar{EOBS}+4)$.
4543 \end{enumerate}
4544 \item
4545 Otherwise, if \bitvar{TOKEN} is 4:
4546 \begin{enumerate}
4547 \item
4548 Read a 3-bit unsigned integer as \bitvar{EOBS}.
4549 \item
4550 Assign \bitvar{EOBS} the value $(\bitvar{EOBS}+8)$.
4551 \end{enumerate}
4552 \item
4553 Otherwise, if \bitvar{TOKEN} is 5:
4554 \begin{enumerate}
4555 \item
4556 Read a 4-bit unsigned integer as \bitvar{EOBS}.
4557 \item
4558 Assign \bitvar{EOBS} the value $(\bitvar{EOBS}+16)$.
4559 \end{enumerate}
4560 \item
4561 Otherwise, \bitvar{TOKEN} is 6:
4562 \begin{enumerate}
4563 \item
4564 Read a 12-bit unsigned integer as \bitvar{EOBS}.
4565 \item
4566 If \bitvar{EOBS} is zero, assign \bitvar{EOBS} to be the number of coded blocks
4567 \locvar{\bj} such that $\bitvar{TIS}[\locvar{\bj}]$ is less than 64.
4568 \end{enumerate}
4569 \item
4570 For each value of \locvar{\tj} from $\bitvar{\ti}$ to 63, assign
4571 $\bitvar{COEFFS}[\bitvar{\bi}][\locvar{\tj}]$ the value zero.
4572 \item
4573 Assign $\bitvar{NCOEFFS}[\bitvar{\bi}]$ the value $\bitvar{TIS}[\bitvar{\bi}]$.
4574 \item
4575 Assign $\bitvar{TIS}[\bitvar{\bi}]$ the value 64.
4576 \item
4577 Assign \bitvar{EOBS} the value $(\bitvar{EOBS}-1)$.
4578 \end{enumerate}
4580 \paragraph{VP3 Compatibility}
4582 The VP3 encoder does not use the special interpretation of a zero-length EOB
4583 run, though its decoder {\em does} support it.
4584 That may be due more to a happy accident in the way the decoder was written
4585 than intentional design, however, and other VP3 implementations might not
4586 reproduce it faithfully.
4587 For backwards compatibility, it may be wise to avoid it, especially as for most
4588 frame sizes there are fewer than 4095 blocks, making it unnecessary.
4590 \subsection{Coefficient Token Decode}
4591 \label{sub:coeff-token}
4593 \paragraph{Input parameters:}\hfill\\*
4594 \begin{tabularx}{\textwidth}{@{}llrcX@{}}\toprule
4595 \multicolumn{1}{c}{Name} &
4596 \multicolumn{1}{c}{Type} &
4597 \multicolumn{1}{p{30pt}}{\centering Size (bits)} &
4598 \multicolumn{1}{c}{Signed?} &
4599 \multicolumn{1}{c}{Description and restrictions} \\\midrule\endhead
4600 \bitvar{TOKEN} & Integer & 5 & No & The token being decoded.
4601 This must be in the range $7\ldots 31$. \\
4602 \bitvar{NBS} & Integer & 36 & No & The total number of blocks in a
4603 frame. \\
4604 \bitvar{TIS} & \multicolumn{1}{p{40pt}}{Integer Array} &
4605 7 & No & An \bitvar{NBS}-element array of the
4606 current token index for each block. \\
4607 \bitvar{COEFFS} & \multicolumn{1}{p{50pt}}{2D Integer Array} &
4608 16 & Yes & An $\bitvar{NBS}\times 64$ array of
4609 quantized DCT coefficient values for each block in zig-zag order. \\
4610 \bitvar{\bi} & Integer & 36 & No & The index of the current block in
4611 coded order. \\
4612 \bitvar{\ti} & Integer & 6 & No & The current token index. \\
4613 \bottomrule\end{tabularx}
4615 \paragraph{Output parameters:}\hfill\\*
4616 \begin{tabularx}{\textwidth}{@{}llrcX@{}}\toprule
4617 \multicolumn{1}{c}{Name} &
4618 \multicolumn{1}{c}{Type} &
4619 \multicolumn{1}{p{30pt}}{\centering Size (bits)} &
4620 \multicolumn{1}{c}{Signed?} &
4621 \multicolumn{1}{c}{Description and restrictions} \\\midrule\endhead
4622 \bitvar{TIS} & \multicolumn{1}{p{40pt}}{Integer Array} &
4623 7 & No & An \bitvar{NBS}-element array of the
4624 current token index for each block. \\
4625 \bitvar{NCOEFFS} & \multicolumn{1}{p{40pt}}{Integer Array} &
4626 7 & No & An \bitvar{NBS}-element array of the
4627 coefficient count for each block. \\
4628 \bitvar{COEFFS} & \multicolumn{1}{p{50pt}}{2D Integer Array} &
4629 16 & Yes & An $\bitvar{NBS}\times 64$ array of
4630 quantized DCT coefficient values for each block in zig-zag order. \\
4631 \bottomrule\end{tabularx}
4633 \paragraph{Variables used:}\hfill\\*
4634 \begin{tabularx}{\textwidth}{@{}llrcX@{}}\toprule
4635 \multicolumn{1}{c}{Name} &
4636 \multicolumn{1}{c}{Type} &
4637 \multicolumn{1}{p{30pt}}{\centering Size (bits)} &
4638 \multicolumn{1}{c}{Signed?} &
4639 \multicolumn{1}{c}{Description and restrictions} \\\midrule\endhead
4640 \locvar{SIGN} & Integer & 1 & No & A flag indicating the sign of the
4641 current coefficient. \\
4642 \locvar{MAG} & Integer & 10 & No & The magnitude of the current
4643 coefficient. \\
4644 \locvar{RLEN} & Integer & 6 & No & The length of the current zero run. \\
4645 \locvar{\tj} & Integer & 6 & No & Another token index. \\
4646 \bottomrule\end{tabularx}
4647 \medskip
4649 Each of these tokens decodes one or more coefficients in the current block.
4650 A summary of the meanings of the token values is presented in
4651 Table~\ref{tab:coeff-tokens}.
4652 There are often several different ways to tokenize a given coefficient list.
4653 Which one is optimal depends on the exact lengths of the Huffman codes used to
4654 represent each token.
4655 Note that we do not update the coefficient count for the block if we decode a
4656 pure zero run.
4658 \begin{table}[htbp]
4659 \begin{center}
4660 \begin{tabularx}{\textwidth}{cclX}\toprule
4661 Token Value & Extra Bits & \multicolumn{1}{p{55pt}}{Number of Coefficients}
4662 & Description \\\midrule
4663 $7$ & $3$ & $1\ldots 8$ & Short zero run. \\
4664 $8$ & $6$ & $1\ldots 64$ & Zero run. \\
4665 $9$ & $0$ & $1$ & $1$. \\
4666 $10$ & $0$ & $1$ & $-1$. \\
4667 $11$ & $0$ & $1$ & $2$. \\
4668 $12$ & $0$ & $1$ & $-2$. \\
4669 $13$ & $1$ & $1$ & $\pm 3$. \\
4670 $14$ & $1$ & $1$ & $\pm 4$. \\
4671 $15$ & $1$ & $1$ & $\pm 5$. \\
4672 $16$ & $1$ & $1$ & $\pm 6$. \\
4673 $17$ & $2$ & $1$ & $\pm 7\ldots 8$. \\
4674 $18$ & $3$ & $1$ & $\pm 9\ldots 12$. \\
4675 $19$ & $4$ & $1$ & $\pm 13\ldots 20$. \\
4676 $20$ & $5$ & $1$ & $\pm 21\ldots 36$. \\
4677 $21$ & $6$ & $1$ & $\pm 37\ldots 68$. \\
4678 $22$ & $10$ & $1$ & $\pm 69\ldots 580$. \\
4679 $23$ & $1$ & $2$ & One zero followed by $\pm 1$. \\
4680 $24$ & $1$ & $3$ & Two zeros followed by $\pm 1$. \\
4681 $25$ & $1$ & $4$ & Three zeros followed by
4682 $\pm 1$. \\
4683 $26$ & $1$ & $5$ & Four zeros followed by
4684 $\pm 1$. \\
4685 $27$ & $1$ & $6$ & Five zeros followed by
4686 $\pm 1$. \\
4687 $28$ & $3$ & $7\ldots 10$ & $6\ldots 9$ zeros followed by
4688 $\pm 1$. \\
4689 $29$ & $4$ & $11\ldots 18$ & $10\ldots 17$ zeros followed by
4690 $\pm 1$.\\
4691 $30$ & $2$ & $2$ & One zero followed by
4692 $\pm 2\ldots 3$. \\
4693 $31$ & $3$ & $3\ldots 4$ & $2\ldots 3$ zeros followed by
4694 $\pm 2\ldots 3$. \\
4695 \bottomrule\end{tabularx}
4696 \end{center}
4697 \caption{Coefficient Token Summary}
4698 \label{tab:coeff-tokens}
4699 \end{table}
4701 For tokens which represent more than one coefficient, they MUST NOT bring the
4702 total number of coefficients in the block to more than 64.
4703 Care should be taken in a decoder to check for this, as otherwise it may permit
4704 buffer overflows from invalidly formed packets.
4705 \begin{verse}
4706 {\bf Note:} One way to achieve this efficiently is to combine the inverse
4707 zig-zag mapping (described later in Section~\ref{sub:dequant}) with
4708 coefficient decode, and use a table look-up to map zig-zag indices greater
4709 than 63 to a safe location.
4710 \end{verse}
4712 \begin{enumerate}
4713 \item
4714 If \bitvar{TOKEN} is 7:
4715 \begin{enumerate}
4716 \item
4717 Read in a 3-bit unsigned integer as \locvar{RLEN}.
4718 \item
4719 Assign \locvar{RLEN} the value $(\locvar{RLEN}+1)$.
4720 \item
4721 For each value of \locvar{\tj} from \bitvar{\ti} to
4722 $(\bitvar{\ti}+\locvar{RLEN}-1)$, assign
4723 $\bitvar{COEFFS}[\bitvar{\bi}][\locvar{\tj}]$ the value zero.
4724 \item
4725 Assign $\bitvar{TIS}[\bitvar{\bi}]$ the value
4726 $\bitvar{TIS}[\bitvar{\bi}]+\locvar{RLEN}$.
4727 \end{enumerate}
4728 \item
4729 Otherwise, if \bitvar{TOKEN} is 8:
4730 \begin{enumerate}
4731 \item
4732 Read in a 6-bit unsigned integer as \locvar{RLEN}.
4733 \item
4734 Assign \locvar{RLEN} the value $(\locvar{RLEN}+1)$.
4735 \item
4736 For each value of \locvar{\tj} from \bitvar{\ti} to
4737 $(\bitvar{\ti}+\locvar{RLEN}-1)$, assign
4738 $\bitvar{COEFFS}[\bitvar{\bi}][\locvar{\tj}]$ the value zero.
4739 \item
4740 Assign $\bitvar{TIS}[\bitvar{\bi}]$ the value
4741 $\bitvar{TIS}[\bitvar{\bi}]+\locvar{RLEN}$.
4742 \end{enumerate}
4743 \item
4744 Otherwise, if \bitvar{TOKEN} is 9:
4745 \begin{enumerate}
4746 \item
4747 Assign $\bitvar{COEFFS}[\bitvar{\bi}][\bitvar{\ti}]$ the value $1$.
4748 \item
4749 Assign $\bitvar{TIS}[\bitvar{\bi}]$ the value $\bitvar{TIS}[\bitvar{\bi}]+1$.
4750 \item
4751 Assign $\bitvar{NCOEFFS}[\bitvar{\bi}]$ the value $\bitvar{TIS}[\bitvar{\bi}]$.
4752 \end{enumerate}
4753 \item
4754 Otherwise, if \bitvar{TOKEN} is 10:
4755 \begin{enumerate}
4756 \item
4757 Assign $\bitvar{COEFFS}[\bitvar{\bi}][\bitvar{\ti}]$ the value $-1$.
4758 \item
4759 Assign $\bitvar{TIS}[\bitvar{\bi}]$ the value $\bitvar{TIS}[\bitvar{\bi}]+1$.
4760 \item
4761 Assign $\bitvar{NCOEFFS}[\bitvar{\bi}]$ the value $\bitvar{TIS}[\bitvar{\bi}]$.
4762 \end{enumerate}
4763 \item
4764 Otherwise, if \bitvar{TOKEN} is 11:
4765 \begin{enumerate}
4766 \item
4767 Assign $\bitvar{COEFFS}[\bitvar{\bi}][\bitvar{\ti}]$ the value $2$.
4768 \item
4769 Assign $\bitvar{TIS}[\bitvar{\bi}]$ the value $\bitvar{TIS}[\bitvar{\bi}]+1$.
4770 \item
4771 Assign $\bitvar{NCOEFFS}[\bitvar{\bi}]$ the value $\bitvar{TIS}[\bitvar{\bi}]$.
4772 \end{enumerate}
4773 \item
4774 Otherwise, if \bitvar{TOKEN} is 12:
4775 \begin{enumerate}
4776 \item
4777 Assign $\bitvar{COEFFS}[\bitvar{\bi}][\bitvar{\ti}]$ the value $-2$.
4778 \item
4779 Assign $\bitvar{TIS}[\bitvar{\bi}]$ the value $\bitvar{TIS}[\bitvar{\bi}]+1$.
4780 \item
4781 Assign $\bitvar{NCOEFFS}[\bitvar{\bi}]$ the value $\bitvar{TIS}[\bitvar{\bi}]$.
4782 \end{enumerate}
4783 \item
4784 Otherwise, if \bitvar{TOKEN} is 13:
4785 \begin{enumerate}
4786 \item
4787 Read a 1-bit unsigned integer as \locvar{SIGN}.
4788 \item
4789 If \locvar{SIGN} is zero, assign $\bitvar{COEFFS}[\bitvar{\bi}][\bitvar{\ti}]$
4790 the value $3$.
4791 \item
4792 Otherwise, assign $\bitvar{COEFFS}[\bitvar{\bi}][\bitvar{\ti}]$ the value $-3$.
4793 \item
4794 Assign $\bitvar{TIS}[\bitvar{\bi}]$ the value $\bitvar{TIS}[\bitvar{\bi}]+1$.
4795 \item
4796 Assign $\bitvar{NCOEFFS}[\bitvar{\bi}]$ the value $\bitvar{TIS}[\bitvar{\bi}]$.
4797 \end{enumerate}
4798 \item
4799 Otherwise, if \bitvar{TOKEN} is 14:
4800 \begin{enumerate}
4801 \item
4802 Read a 1-bit unsigned integer as \locvar{SIGN}.
4803 \item
4804 If \locvar{SIGN} is zero, assign $\bitvar{COEFFS}[\bitvar{\bi}][\bitvar{\ti}]$
4805 the value $4$.
4806 \item
4807 Otherwise, assign $\bitvar{COEFFS}[\bitvar{\bi}][\bitvar{\ti}]$ the value $-4$.
4808 \item
4809 Assign $\bitvar{TIS}[\bitvar{\bi}]$ the value $\bitvar{TIS}[\bitvar{\bi}]+1$.
4810 \item
4811 Assign $\bitvar{NCOEFFS}[\bitvar{\bi}]$ the value $\bitvar{TIS}[\bitvar{\bi}]$.
4812 \end{enumerate}
4813 \item
4814 Otherwise, if \bitvar{TOKEN} is 15:
4815 \begin{enumerate}
4816 \item
4817 Read a 1-bit unsigned integer as \locvar{SIGN}.
4818 \item
4819 If \locvar{SIGN} is zero, assign $\bitvar{COEFFS}[\bitvar{\bi}][\bitvar{\ti}]$
4820 the value $5$.
4821 \item
4822 Otherwise, assign $\bitvar{COEFFS}[\bitvar{\bi}][\bitvar{\ti}]$ the value $-5$.
4823 \item
4824 Assign $\bitvar{TIS}[\bitvar{\bi}]$ the value $\bitvar{TIS}[\bitvar{\bi}]+1$.
4825 \item
4826 Assign $\bitvar{NCOEFFS}[\bitvar{\bi}]$ the value $\bitvar{TIS}[\bitvar{\bi}]$.
4827 \end{enumerate}
4828 \item
4829 Otherwise, if \bitvar{TOKEN} is 16:
4830 \begin{enumerate}
4831 \item
4832 Read a 1-bit unsigned integer as \locvar{SIGN}.
4833 \item
4834 If \locvar{SIGN} is zero, assign $\bitvar{COEFFS}[\bitvar{\bi}][\bitvar{\ti}]$
4835 the value $6$.
4836 \item
4837 Otherwise, assign $\bitvar{COEFFS}[\bitvar{\bi}][\bitvar{\ti}]$ the value $-6$.
4838 \item
4839 Assign $\bitvar{TIS}[\bitvar{\bi}]$ the value $\bitvar{TIS}[\bitvar{\bi}]+1$.
4840 \item
4841 Assign $\bitvar{NCOEFFS}[\bitvar{\bi}]$ the value $\bitvar{TIS}[\bitvar{\bi}]$.
4842 \end{enumerate}
4843 \item
4844 Otherwise, if \bitvar{TOKEN} is 17:
4845 \begin{enumerate}
4846 \item
4847 Read a 1-bit unsigned integer as \locvar{SIGN}.
4848 \item
4849 Read a 1-bit unsigned integer as \locvar{MAG}.
4850 \item
4851 Assign \locvar{MAG} the value $(\locvar{MAG}+7)$.
4852 \item
4853 If \locvar{SIGN} is zero, assign $\bitvar{COEFFS}[\bitvar{\bi}][\bitvar{\ti}]$
4854 the value $\locvar{MAG}$.
4855 \item
4856 Otherwise, assign $\bitvar{COEFFS}[\bitvar{\bi}][\bitvar{\ti}]$ the value
4857 $-\locvar{MAG}$.
4858 \item
4859 Assign $\bitvar{TIS}[\bitvar{\bi}]$ the value $\bitvar{TIS}[\bitvar{\bi}]+1$.
4860 \item
4861 Assign $\bitvar{NCOEFFS}[\bitvar{\bi}]$ the value $\bitvar{TIS}[\bitvar{\bi}]$.
4862 \end{enumerate}
4863 \item
4864 Otherwise, if \bitvar{TOKEN} is 18:
4865 \begin{enumerate}
4866 \item
4867 Read a 1-bit unsigned integer as \locvar{SIGN}.
4868 \item
4869 Read a 2-bit unsigned integer as \locvar{MAG}.
4870 \item
4871 Assign \locvar{MAG} the value $(\locvar{MAG}+9)$.
4872 \item
4873 If \locvar{SIGN} is zero, assign $\bitvar{COEFFS}[\bitvar{\bi}][\bitvar{\ti}]$
4874 the value $\locvar{MAG}$.
4875 \item
4876 Otherwise, assign $\bitvar{COEFFS}[\bitvar{\bi}][\bitvar{\ti}]$ the value
4877 $-\locvar{MAG}$.
4878 \item
4879 Assign $\bitvar{TIS}[\bitvar{\bi}]$ the value $\bitvar{TIS}[\bitvar{\bi}]+1$.
4880 \item
4881 Assign $\bitvar{NCOEFFS}[\bitvar{\bi}]$ the value $\bitvar{TIS}[\bitvar{\bi}]$.
4882 \end{enumerate}
4883 \item
4884 Otherwise, if \bitvar{TOKEN} is 19:
4885 \begin{enumerate}
4886 \item
4887 Read a 1-bit unsigned integer as \locvar{SIGN}.
4888 \item
4889 Read a 3-bit unsigned integer as \locvar{MAG}.
4890 \item
4891 Assign \locvar{MAG} the value $(\locvar{MAG}+13)$.
4892 \item
4893 If \locvar{SIGN} is zero, assign $\bitvar{COEFFS}[\bitvar{\bi}][\bitvar{\ti}]$
4894 the value $\locvar{MAG}$.
4895 \item
4896 Otherwise, assign $\bitvar{COEFFS}[\bitvar{\bi}][\bitvar{\ti}]$ the value
4897 $-\locvar{MAG}$.
4898 \item
4899 Assign $\bitvar{TIS}[\bitvar{\bi}]$ the value $\bitvar{TIS}[\bitvar{\bi}]+1$.
4900 \item
4901 Assign $\bitvar{NCOEFFS}[\bitvar{\bi}]$ the value $\bitvar{TIS}[\bitvar{\bi}]$.
4902 \end{enumerate}
4903 \item
4904 Otherwise, if \bitvar{TOKEN} is 20:
4905 \begin{enumerate}
4906 \item
4907 Read a 1-bit unsigned integer as \locvar{SIGN}.
4908 \item
4909 Read a 4-bit unsigned integer as \locvar{MAG}.
4910 \item
4911 Assign \locvar{MAG} the value $(\locvar{MAG}+21)$.
4912 \item
4913 If \locvar{SIGN} is zero, assign $\bitvar{COEFFS}[\bitvar{\bi}][\bitvar{\ti}]$
4914 the value $\locvar{MAG}$.
4915 \item
4916 Otherwise, assign $\bitvar{COEFFS}[\bitvar{\bi}][\bitvar{\ti}]$ the value
4917 $-\locvar{MAG}$.
4918 \item
4919 Assign $\bitvar{TIS}[\bitvar{\bi}]$ the value $\bitvar{TIS}[\bitvar{\bi}]+1$.
4920 \item
4921 Assign $\bitvar{NCOEFFS}[\bitvar{\bi}]$ the value $\bitvar{TIS}[\bitvar{\bi}]$.
4922 \end{enumerate}
4923 \item
4924 Otherwise, if \bitvar{TOKEN} is 21:
4925 \begin{enumerate}
4926 \item
4927 Read a 1-bit unsigned integer as \locvar{SIGN}.
4928 \item
4929 Read a 5-bit unsigned integer as \locvar{MAG}.
4930 \item
4931 Assign \locvar{MAG} the value $(\locvar{MAG}+37)$.
4932 \item
4933 If \locvar{SIGN} is zero, assign $\bitvar{COEFFS}[\bitvar{\bi}][\bitvar{\ti}]$
4934 the value $\locvar{MAG}$.
4935 \item
4936 Otherwise, assign $\bitvar{COEFFS}[\bitvar{\bi}][\bitvar{\ti}]$ the value
4937 $-\locvar{MAG}$.
4938 \item
4939 Assign $\bitvar{TIS}[\bitvar{\bi}]$ the value $\bitvar{TIS}[\bitvar{\bi}]+1$.
4940 \item
4941 Assign $\bitvar{NCOEFFS}[\bitvar{\bi}]$ the value $\bitvar{TIS}[\bitvar{\bi}]$.
4942 \end{enumerate}
4943 \item
4944 Otherwise, if \bitvar{TOKEN} is 22:
4945 \begin{enumerate}
4946 \item
4947 Read a 1-bit unsigned integer as \locvar{SIGN}.
4948 \item
4949 Read a 9-bit unsigned integer as \locvar{MAG}.
4950 \item
4951 Assign \locvar{MAG} the value $(\locvar{MAG}+69)$.
4952 \item
4953 If \locvar{SIGN} is zero, assign $\bitvar{COEFFS}[\bitvar{\bi}][\bitvar{\ti}]$
4954 the value $\locvar{MAG}$.
4955 \item
4956 Otherwise, assign $\bitvar{COEFFS}[\bitvar{\bi}][\bitvar{\ti}]$ the value
4957 $-\locvar{MAG}$.
4958 \item
4959 Assign $\bitvar{TIS}[\bitvar{\bi}]$ the value $\bitvar{TIS}[\bitvar{\bi}]+1$.
4960 \item
4961 Assign $\bitvar{NCOEFFS}[\bitvar{\bi}]$ the value $\bitvar{TIS}[\bitvar{\bi}]$.
4962 \end{enumerate}
4963 \item
4964 Otherwise, if \bitvar{TOKEN} is 23:
4965 \begin{enumerate}
4966 \item
4967 Assign $\bitvar{COEFFS}[\bitvar{\bi}][\bitvar{\ti}]$ the value zero.
4968 \item
4969 Read a 1-bit unsigned integer as SIGN.
4970 \item
4971 If \locvar{SIGN} is zero, assign
4972 $\bitvar{COEFFS}[\bitvar{\bi}][\bitvar{\ti}+1]$ the value $1$.
4973 \item
4974 Otherwise, assign $\bitvar{COEFFS}[\bitvar{\bi}][\bitvar{\ti}+1]$ the value
4975 $-1$.
4976 \item
4977 Assign $\bitvar{TIS}[\bitvar{\bi}]$ the value $\bitvar{TIS}[\bitvar{\bi}]+2$.
4978 \item
4979 Assign $\bitvar{NCOEFFS}[\bitvar{\bi}]$ the value $\bitvar{TIS}[\bitvar{\bi}]$.
4980 \end{enumerate}
4981 \item
4982 Otherwise, if \bitvar{TOKEN} is 24:
4983 \begin{enumerate}
4984 \item
4985 For each value of \locvar{\tj} from \bitvar{\ti} to $(\bitvar{\ti}+1)$, assign
4986 $\bitvar{COEFFS}[\bitvar{\bi}][\locvar{\tj}]$ the value zero.
4987 \item
4988 Read a 1-bit unsigned integer as SIGN.
4989 \item
4990 If \locvar{SIGN} is zero, assign
4991 $\bitvar{COEFFS}[\bitvar{\bi}][\bitvar{\ti}+2]$ the value $1$.
4992 \item
4993 Otherwise, assign $\bitvar{COEFFS}[\bitvar{\bi}][\bitvar{\ti}+2]$ the value
4994 $-1$.
4995 \item
4996 Assign $\bitvar{TIS}[\bitvar{\bi}]$ the value $\bitvar{TIS}[\bitvar{\bi}]+3$.
4997 \item
4998 Assign $\bitvar{NCOEFFS}[\bitvar{\bi}]$ the value $\bitvar{TIS}[\bitvar{\bi}]$.
4999 \end{enumerate}
5000 \item
5001 Otherwise, if \bitvar{TOKEN} is 25:
5002 \begin{enumerate}
5003 \item
5004 For each value of \locvar{\tj} from \bitvar{\ti} to $(\bitvar{\ti}+2)$, assign
5005 $\bitvar{COEFFS}[\bitvar{\bi}][\locvar{\tj}]$ the value zero.
5006 \item
5007 Read a 1-bit unsigned integer as SIGN.
5008 \item
5009 If \locvar{SIGN} is zero, assign
5010 $\bitvar{COEFFS}[\bitvar{\bi}][\bitvar{\ti}+3]$ the value $1$.
5011 \item
5012 Otherwise, assign $\bitvar{COEFFS}[\bitvar{\bi}][\bitvar{\ti}+3]$ the value
5013 $-1$.
5014 \item
5015 Assign $\bitvar{TIS}[\bitvar{\bi}]$ the value $\bitvar{TIS}[\bitvar{\bi}]+4$.
5016 \item
5017 Assign $\bitvar{NCOEFFS}[\bitvar{\bi}]$ the value $\bitvar{TIS}[\bitvar{\bi}]$.
5018 \end{enumerate}
5019 \item
5020 Otherwise, if \bitvar{TOKEN} is 26:
5021 \begin{enumerate}
5022 \item
5023 For each value of \locvar{\tj} from \bitvar{\ti} to $(\bitvar{\ti}+3)$, assign
5024 $\bitvar{COEFFS}[\bitvar{\bi}][\locvar{\tj}]$ the value zero.
5025 \item
5026 Read a 1-bit unsigned integer as SIGN.
5027 \item
5028 If \locvar{SIGN} is zero, assign
5029 $\bitvar{COEFFS}[\bitvar{\bi}][\bitvar{\ti}+4]$ the value $1$.
5030 \item
5031 Otherwise, assign $\bitvar{COEFFS}[\bitvar{\bi}][\bitvar{\ti}+4]$ the value
5032 $-1$.
5033 \item
5034 Assign $\bitvar{TIS}[\bitvar{\bi}]$ the value $\bitvar{TIS}[\bitvar{\bi}]+5$.
5035 \item
5036 Assign $\bitvar{NCOEFFS}[\bitvar{\bi}]$ the value $\bitvar{TIS}[\bitvar{\bi}]$.
5037 \end{enumerate}
5038 \item
5039 Otherwise, if \bitvar{TOKEN} is 27:
5040 \begin{enumerate}
5041 \item
5042 For each value of \locvar{\tj} from \bitvar{\ti} to $(\bitvar{\ti}+4)$, assign
5043 $\bitvar{COEFFS}[\bitvar{\bi}][\locvar{\tj}]$ the value zero.
5044 \item
5045 Read a 1-bit unsigned integer as SIGN.
5046 \item
5047 If \locvar{SIGN} is zero, assign
5048 $\bitvar{COEFFS}[\bitvar{\bi}][\bitvar{\ti}+5]$ the value $1$.
5049 \item
5050 Otherwise, assign $\bitvar{COEFFS}[\bitvar{\bi}][\bitvar{\ti}+5]$ the value
5051 $-1$.
5052 \item
5053 Assign $\bitvar{TIS}[\bitvar{\bi}]$ the value $\bitvar{TIS}[\bitvar{\bi}]+6$.
5054 \item
5055 Assign $\bitvar{NCOEFFS}[\bitvar{\bi}]$ the value $\bitvar{TIS}[\bitvar{\bi}]$.
5056 \end{enumerate}
5057 \item
5058 Otherwise, if \bitvar{TOKEN} is 28:
5059 \begin{enumerate}
5060 \item
5061 Read a 1-bit unsigned integer as \locvar{SIGN}.
5062 \item
5063 Read a 2-bit unsigned integer as \locvar{RLEN}.
5064 \item
5065 Assign \locvar{RLEN} the value $(\locvar{RLEN}+6)$.
5066 \item
5067 For each value of \locvar{\tj} from \bitvar{\ti} to
5068 $(\bitvar{\ti}+\locvar{RLEN}-1)$, assign
5069 $\bitvar{COEFFS}[\bitvar{\bi}][\locvar{\tj}]$ the value zero.
5070 \item
5071 If \locvar{SIGN} is zero, assign
5072 $\bitvar{COEFFS}[\bitvar{\bi}][\bitvar{\ti}+\locvar{RLEN}]$ the value $1$.
5073 \item
5074 Otherwise, assign $\bitvar{COEFFS}[\bitvar{\bi}][\bitvar{\ti}+\locvar{RLEN}]$
5075 the value $-1$.
5076 \item
5077 Assign $\bitvar{TIS}[\bitvar{\bi}]$ the value
5078 $\bitvar{TIS}[\bitvar{\bi}]+\locvar{RLEN}+1$.
5079 \item
5080 Assign $\bitvar{NCOEFFS}[\bitvar{\bi}]$ the value $\bitvar{TIS}[\bitvar{\bi}]$.
5081 \end{enumerate}
5082 \item
5083 Otherwise, if \bitvar{TOKEN} is 29:
5084 \begin{enumerate}
5085 \item
5086 Read a 1-bit unsigned integer as \locvar{SIGN}.
5087 \item
5088 Read a 3-bit unsigned integer as \locvar{RLEN}.
5089 \item
5090 Assign \locvar{RLEN} the value $(\locvar{RLEN}+10)$.
5091 \item
5092 For each value of \locvar{\tj} from \bitvar{\ti} to
5093 $(\bitvar{\ti}+\locvar{RLEN}-1)$, assign
5094 $\bitvar{COEFFS}[\bitvar{\bi}][\locvar{\tj}]$ the value zero.
5095 \item
5096 If \locvar{SIGN} is zero, assign
5097 $\bitvar{COEFFS}[\bitvar{\bi}][\bitvar{\ti}+\locvar{RLEN}]$ the value $1$.
5098 \item
5099 Otherwise, assign $\bitvar{COEFFS}[\bitvar{\bi}][\bitvar{\ti}+\locvar{RLEN}]$
5100 the value $-1$.
5101 \item
5102 Assign $\bitvar{TIS}[\bitvar{\bi}]$ the value
5103 $\bitvar{TIS}[\bitvar{\bi}]+\locvar{RLEN}+1$.
5104 Assign $\bitvar{NCOEFFS}[\bitvar{\bi}]$ the value $\bitvar{TIS}[\bitvar{\bi}]$.
5105 \end{enumerate}
5106 \item
5107 Otherwise, if \bitvar{TOKEN} is 30:
5108 \begin{enumerate}
5109 \item
5110 Assign $\bitvar{COEFFS}[\bitvar{\bi}][\locvar{\ti}]$ the value zero.
5111 \item
5112 Read a 1-bit unsigned integer as \locvar{SIGN}.
5113 \item
5114 Read a 1-bit unsigned integer as \locvar{MAG}.
5115 \item
5116 Assign \locvar{MAG} the value $(\locvar{MAG}+2)$.
5117 \item
5118 If \locvar{SIGN} is zero, assign
5119 $\bitvar{COEFFS}[\bitvar{\bi}][\bitvar{\ti}+1]$ the value $\locvar{MAG}$.
5120 \item
5121 Otherwise, assign $\bitvar{COEFFS}[\bitvar{\bi}][\bitvar{\ti}+1]$ the value
5122 $-\locvar{MAG}$.
5123 \item
5124 Assign $\bitvar{TIS}[\bitvar{\bi}]$ the value $\bitvar{TIS}[\bitvar{\bi}]+2$.
5125 Assign $\bitvar{NCOEFFS}[\bitvar{\bi}]$ the value $\bitvar{TIS}[\bitvar{\bi}]$.
5126 \end{enumerate}
5127 \item
5128 Otherwise, if \bitvar{TOKEN} is 31:
5129 \begin{enumerate}
5130 \item
5131 Read a 1-bit unsigned integer as \locvar{SIGN}.
5132 \item
5133 Read a 1-bit unsigned integer as \locvar{MAG}.
5134 \item
5135 Assign \locvar{MAG} the value $(\locvar{MAG}+2)$.
5136 \item
5137 Read a 1-bit unsigned integer as \locvar{RLEN}.
5138 \item
5139 Assign \locvar{RLEN} the value $(\locvar{RLEN}+2)$.
5140 \item
5141 For each value of \locvar{\tj} from \bitvar{\ti} to
5142 $(\bitvar{\ti}+\locvar{RLEN}-1)$, assign
5143 $\bitvar{COEFFS}[\bitvar{\bi}][\locvar{\tj}]$ the value zero.
5144 \item
5145 If \locvar{SIGN} is zero, assign
5146 $\bitvar{COEFFS}[\bitvar{\bi}][\bitvar{\ti}+\locvar{RLEN}]$ the value
5147 $\locvar{MAG}$.
5148 \item
5149 Otherwise, assign $\bitvar{COEFFS}[\bitvar{\bi}][\bitvar{\ti}+\locvar{RLEN}]$
5150 the value $-\locvar{MAG}$.
5151 \item
5152 Assign $\bitvar{TIS}[\bitvar{\bi}]$ the value
5153 $\bitvar{TIS}[\bitvar{\bi}]+\locvar{RLEN}+1$.
5154 Assign $\bitvar{NCOEFFS}[\bitvar{\bi}]$ the value $\bitvar{TIS}[\bitvar{\bi}]$.
5155 \end{enumerate}
5156 \end{enumerate}
5158 \subsection{DCT Coefficient Decode}
5159 \label{sub:dct-coeffs}
5161 \paragraph{Input parameters:}\hfill\\*
5162 \begin{tabularx}{\textwidth}{@{}llrcX@{}}\toprule
5163 \multicolumn{1}{c}{Name} &
5164 \multicolumn{1}{c}{Type} &
5165 \multicolumn{1}{p{30pt}}{\centering Size (bits)} &
5166 \multicolumn{1}{c}{Signed?} &
5167 \multicolumn{1}{c}{Description and restrictions} \\\midrule\endhead
5168 \bitvar{NBS} & Integer & 36 & No & The total number of blocks in a
5169 frame. \\
5170 \bitvar{BCODED} & \multicolumn{1}{p{40pt}}{Integer Array} &
5171 1 & No & An \bitvar{NBS}-element array of flags
5172 indicating which blocks are coded. \\
5173 \bitvar{NMBS} & Integer & 32 & No & The total number of macro blocks in a
5174 frame. \\
5175 \bitvar{HTS} & \multicolumn{3}{l}{Huffman table array}
5176 & An 80-element array of Huffman tables
5177 with up to 32 entries each. \\
5178 \bottomrule\end{tabularx}
5180 \paragraph{Output parameters:}\hfill\\*
5181 \begin{tabularx}{\textwidth}{@{}llrcX@{}}\toprule
5182 \multicolumn{1}{c}{Name} &
5183 \multicolumn{1}{c}{Type} &
5184 \multicolumn{1}{p{30pt}}{\centering Size (bits)} &
5185 \multicolumn{1}{c}{Signed?} &
5186 \multicolumn{1}{c}{Description and restrictions} \\\midrule\endhead
5187 \bitvar{COEFFS} & \multicolumn{1}{p{50pt}}{2D Integer Array} &
5188 16 & Yes & An $\bitvar{NBS}\times 64$ array of
5189 quantized DCT coefficient values for each block in zig-zag order. \\
5190 \bitvar{NCOEFFS} & \multicolumn{1}{p{40pt}}{Integer Array} &
5191 7 & No & An \bitvar{NBS}-element array of the
5192 coefficient count for each block. \\
5193 \bottomrule\end{tabularx}
5195 \paragraph{Variables used:}\hfill\\*
5196 \begin{tabularx}{\textwidth}{@{}llrcX@{}}\toprule
5197 \multicolumn{1}{c}{Name} &
5198 \multicolumn{1}{c}{Type} &
5199 \multicolumn{1}{p{30pt}}{\centering Size (bits)} &
5200 \multicolumn{1}{c}{Signed?} &
5201 \multicolumn{1}{c}{Description and restrictions} \\\midrule\endhead
5202 \locvar{NLBS} & Integer & 34 & No & The number of blocks in the luma
5203 plane. \\
5204 \locvar{TIS} & \multicolumn{1}{p{40pt}}{Integer Array} &
5205 7 & No & An \bitvar{NBS}-element array of the
5206 current token index for each block. \\
5207 \locvar{EOBS} & Integer & 36 & No & The remaining length of the current
5208 EOB run. \\
5209 \locvar{TOKEN} & Integer & 5 & No & The current token being decoded. \\
5210 \locvar{HG} & Integer & 3 & No & The current Huffman table group. \\
5211 \locvar{\cbi} & Integer & 36 & No & The index of the current block in the
5212 coded block list. \\
5213 \locvar{\bi} & Integer & 36 & No & The index of the current block in
5214 coded order. \\
5215 \locvar{\bj} & Integer & 36 & No & Another index of a block in coded
5216 order. \\
5217 \locvar{\ti} & Integer & 6 & No & The current token index. \\
5218 \locvar{\tj} & Integer & 6 & No & Another token index. \\
5219 \locvar{\hti_L} & Integer & 4 & No & The index of the current Huffman table
5220 to use for the luma plane within a group. \\
5221 \locvar{\hti_C} & Integer & 4 & No & The index of the current Huffman table
5222 to use for the chroma planes within a group. \\
5223 \locvar{\hti} & Integer & 7 & No & The index of the current Huffman table
5224 to use. \\
5225 \bottomrule\end{tabularx}
5226 \medskip
5228 This procedure puts the above two procedures to work to decode the entire set
5229 of DCT coefficients for the frame.
5230 At the end of this procedure, \locvar{EOBS} MUST be zero, and
5231 $\locvar{TIS}[\locvar{\bi}]$ MUST be 64 for every coded \locvar{\bi}.
5233 Note that we update the coefficient count of every block before continuing an
5234 EOB run or decoding a token, despite the fact that it is already up to date
5235 unless the previous token was a pure zero run.
5236 This is done intentionally to mimic the VP3 accounting rules.
5237 Thus the only time the coefficient count does not include the coefficients in a
5238 pure zero run is when when that run reaches all the way to coefficient 63.
5239 Note, however, that regardless of the coefficient count, any additional
5240 coefficients are still set to zero.
5241 The only use of the count is in determining if a special case of the inverse
5242 DCT can be used in Section~\ref{sub:2d-idct}.
5244 \begin{enumerate}
5245 \item
5246 Assign \locvar{NLBS} the value $(\bitvar{NMBS}*4)$.
5247 \item
5248 For each consecutive value of \locvar{\bi} from 0 to $(\bitvar{NBS}-1)$,
5249 assign $\locvar{TIS}[\locvar{\bi}]$ the value zero.
5250 \item
5251 Assign \locvar{EOBS} the value 0.
5252 \item
5253 For each consecutive value of \locvar{\ti} from 0 to 63:
5254 \begin{enumerate}
5255 \item
5256 If \locvar{\ti} is $0$ or $1$:
5257 \begin{enumerate}
5258 \item
5259 Read a 4-bit unsigned integer as \locvar{\hti_L}.
5260 \item
5261 Read a 4-bit unsigned integer as \locvar{\hti_C}.
5262 \end{enumerate}
5263 \item
5264 For each consecutive value of \locvar{\bi} from 0 to $(\bitvar{NBS}-1)$ for
5265 which $\bitvar{BCODED}[\locvar{\bi}]$ is non-zero and
5266 $\locvar{TIS}[\locvar{\bi}]$ equals \locvar{\ti}:
5267 \begin{enumerate}
5268 \item
5269 Assign $\bitvar{NCOEFFS}[\locvar{\bi}]$ the value \locvar{\ti}.
5270 \item
5271 If \locvar{EOBS} is greater than zero:
5272 \begin{enumerate}
5273 \item
5274 For each value of \locvar{\tj} from $\locvar{\ti}$ to 63, assign
5275 $\bitvar{COEFFS}[\locvar{\bi}][\locvar{\tj}]$ the value zero.
5276 \item
5277 Assign $\locvar{TIS}[\locvar{\bi}]$ the value 64.
5278 \item
5279 Assign \locvar{EOBS} the value $(\locvar{EOBS}-1)$.
5280 \end{enumerate}
5281 \item
5282 Otherwise:
5283 \begin{enumerate}
5284 \item
5285 Assign \locvar{HG} a value based on \locvar{\ti} from
5286 Table~\ref{tab:huff-groups}.
5288 \begin{table}[htbp]
5289 \begin{center}
5290 \begin{tabular}{lc}\toprule
5291 \locvar{\ti} & \locvar{HG} \\\midrule
5292 $0$ & $0$ \\
5293 $1\ldots 5$ & $1$ \\
5294 $6\ldots 14$ & $2$ \\
5295 $15\ldots 27$ & $3$ \\
5296 $28\ldots 63$ & $4$ \\
5297 \bottomrule\end{tabular}
5298 \end{center}
5299 \caption{Huffman Table Groups}
5300 \label{tab:huff-groups}
5301 \end{table}
5303 \item
5304 If \locvar{\bi} is less than \locvar{NLBS}, assign \locvar{\hti} the value
5305 $(16*\locvar{HG}+\locvar{\hti_L})$.
5306 \item
5307 Otherwise, assign \locvar{\hti} the value
5308 $(16*\locvar{HG}+\locvar{\hti_C})$.
5309 \item
5310 Read one bit at a time until one of the codes in $\bitvar{HTS}[\locvar{\hti}]$
5311 is recognized, and assign the value to \locvar{TOKEN}.
5312 \item
5313 If \locvar{TOKEN} is less than 7, expand an EOB token using the procedure given
5314 in Section~\ref{sub:eob-token} to update $\locvar{TIS}[\locvar{\bi}]$,
5315 $\bitvar{COEFFS}[\locvar{\bi}]$, and \locvar{EOBS}.
5316 \item
5317 Otherwise, expand a coefficient token using the procedure given in
5318 Section~\ref{sub:coeff-token} to update $\locvar{TIS}[\locvar{\bi}]$,
5319 $\bitvar{COEFFS}[\locvar{\bi}]$, and $\bitvar{NCOEFFS}[\locvar{\bi}]$.
5320 \end{enumerate}
5321 \end{enumerate}
5322 \end{enumerate}
5323 \end{enumerate}
5325 \section{Undoing DC Prediction}
5327 The actual value of a DC coefficient decoded by Section~\ref{sec:dct-decode} is
5328 the residual from a predicted value computed by the encoder.
5329 This prediction is only applied to DC coefficients.
5330 Quantized AC coefficients are encoded directly.
5332 This section describes how to undo this prediction to recover the original
5333 DC coefficients.
5334 The predicted DC value for a block is computed from the DC values of its
5335 immediate neighbors which precede the block in raster order.
5336 Thus, reversing this prediction must procede in raster order, instead of coded
5337 order.
5339 Note that this step comes before dequantizing the coefficients.
5340 For this reason, DC coefficients are all quantized with the same \qi\ value,
5341 regardless of the block-level \qi\ values decoded in
5342 Section~\ref{sub:block-qis}.
5343 Those \qi\ values are applied only to the AC coefficients.
5345 \subsection{Computing the DC Predictor}
5346 \label{sub:dc-pred}
5348 \paragraph{Input parameters:}\hfill\\*
5349 \begin{tabularx}{\textwidth}{@{}llrcX@{}}\toprule
5350 \multicolumn{1}{c}{Name} &
5351 \multicolumn{1}{c}{Type} &
5352 \multicolumn{1}{p{30pt}}{\centering Size (bits)} &
5353 \multicolumn{1}{c}{Signed?} &
5354 \multicolumn{1}{c}{Description and restrictions} \\\midrule\endhead
5355 \bitvar{BCODED} & \multicolumn{1}{p{40pt}}{Integer Array} &
5356 1 & No & An \bitvar{NBS}-element array of flags
5357 indicating which blocks are coded. \\
5358 \bitvar{MBMODES} & \multicolumn{1}{p{40pt}}{Integer Array} &
5359 3 & No & An \bitvar{NMBS}-element array of
5360 coding modes for each macro block. \\
5361 \bitvar{LASTDC} & \multicolumn{1}{p{40pt}}{Integer Array} &
5362 16 & Yes & A 3-element array containing the
5363 most recently decoded DC value, one for inter mode and for each reference
5364 frame. \\
5365 \bitvar{COEFFS} & \multicolumn{1}{p{50pt}}{2D Integer Array} &
5366 16 & Yes & An $\bitvar{NBS}\times 64$ array of
5367 quantized DCT coefficient values for each block in zig-zag order. \\
5368 \bitvar{\bi} & Integer & 36 & No & The index of the current block in
5369 coded order. \\
5370 \bottomrule\end{tabularx}
5372 \paragraph{Output parameters:}\hfill\\*
5373 \begin{tabularx}{\textwidth}{@{}llrcX@{}}\toprule
5374 \multicolumn{1}{c}{Name} &
5375 \multicolumn{1}{c}{Type} &
5376 \multicolumn{1}{p{30pt}}{\centering Size (bits)} &
5377 \multicolumn{1}{c}{Signed?} &
5378 \multicolumn{1}{c}{Description and restrictions} \\\midrule\endhead
5379 \bitvar{DCPRED} & Integer & 16 & Yes & The predicted DC value for the current
5380 block. \\
5381 \bottomrule\end{tabularx}
5383 \paragraph{Variables used:}\hfill\\*
5384 \begin{tabularx}{\textwidth}{@{}llrcX@{}}\toprule
5385 \multicolumn{1}{c}{Name} &
5386 \multicolumn{1}{c}{Type} &
5387 \multicolumn{1}{p{30pt}}{\centering Size (bits)} &
5388 \multicolumn{1}{c}{Signed?} &
5389 \multicolumn{1}{c}{Description and restrictions} \\\midrule\endhead
5390 \locvar{P} & \multicolumn{1}{p{40pt}}{Integer Array} &
5391 1 & No & A 4-element array indicating which
5392 neighbors can be used for DC prediction. \\
5393 \locvar{PBI} & \multicolumn{1}{p{40pt}}{Integer Array} &
5394 36 & No & A 4-element array containing the
5395 coded-order block index of the current block's neighbors. \\
5396 \locvar{W} & \multicolumn{1}{p{40pt}}{Integer Array} &
5397 7 & Yes & A 4-element array of the weights to
5398 apply to each neighboring DC value. \\
5399 \locvar{PDIV} & Integer & 8 & No & The valud to divide the weighted sum
5400 by. \\
5401 \locvar{\bj} & Integer & 36 & No & The index of a neighboring block in
5402 coded order. \\
5403 \locvar{\mbi} & Integer & 32 & No & The index of the macro block
5404 containing block \locvar{\bi}. \\
5405 \locvar{\mbi} & Integer & 32 & No & The index of the macro block
5406 containing block \locvar{\bj}. \\
5407 \locvar{\rfi} & Integer & 2 & No & The index of the reference frame
5408 indicated by the coding mode for macro block \locvar{\mbi}. \\
5409 \bottomrule\end{tabularx}
5410 \medskip
5412 This procedure outlines how a predictor is formed for a single block.
5414 The predictor is computed as a weighted sum of the neighboring DC values from
5415 coded blocks which use the same reference frame.
5416 This latter condition is determined only by checking the coding mode for the
5417 block.
5418 Even if the golden frame and the previous frame are in fact the same, e.g. for
5419 the first inter frame after an intra frame, they are still treated as being
5420 different for the purposes of DC prediction.
5421 The weighted sum is divided by a power of two, with truncation towards zero,
5422 and the result is checked for outranging if necessary.
5424 If there are no neighboring coded blocks which use the same reference frame as
5425 the current block, then the most recent DC value of any block that used that
5426 reference frame is used instead.
5427 If no such block exists, then the predictor is set to zero.
5429 \begin{enumerate}
5430 \item
5431 Assign \locvar{\mbi} the index of the macro block containing block
5432 \bitvar{\bi}.
5433 \item
5434 Assign \locvar{\rfi} the value of the Reference Frame Index column of
5435 Table~\ref{tab:cm-refs} corresponding to $\bitvar{MBMODES}[\locvar{\mbi}]$.
5437 \begin{table}[htpb]
5438 \begin{center}
5439 \begin{tabular}{ll}\toprule
5440 Coding Mode & Reference Frame Index \\\midrule
5441 $0$ (INTER\_NOMV) & $1$ (Previous) \\
5442 $1$ (INTRA) & $0$ (None) \\
5443 $2$ (INTER\_MV) & $1$ (Previous) \\
5444 $3$ (INTER\_MV\_LAST) & $1$ (Previous) \\
5445 $4$ (INTER\_MV\_LAST2) & $1$ (Previous) \\
5446 $5$ (INTER\_GOLDEN\_NOMV) & $2$ (Golden) \\
5447 $6$ (INTER\_GOLDEN\_MV) & $2$ (Golden) \\
5448 $7$ (INTER\_MV\_FOUR) & $1$ (Previous) \\
5449 \bottomrule\end{tabular}
5450 \end{center}
5451 \caption{Reference Frames for Each Coding Mode}
5452 \label{tab:cm-refs}
5453 \end{table}
5455 \item
5456 If block \locvar{\bi} is not along the left edge of the coded frame:
5457 \begin{enumerate}
5458 \item
5459 Assign \locvar{\bj} the coded-order index of block \locvar{\bi}'s left
5460 neighbor, i.e., in the same row but one column to the left.
5461 \item
5462 If $\bitvar{BCODED}[\bj]$ is not zero:
5463 \begin{enumerate}
5464 \item
5465 Assign \locvar{\mbj} the index of the macro block containing block
5466 \locvar{\bj}.
5467 \item
5468 If the value of the Reference Frame Index column of Table~\ref{tab:cm-refs}
5469 corresonding to $\bitvar{MBMODES}[\locvar{\mbj}]$ equals \locvar{\rfi}:
5470 \begin{enumerate}
5471 \item
5472 Assign $\locvar{P}[0]$ the value $1$.
5473 \item
5474 Assign $\locvar{PBI}[0]$ the value \locvar{\bj}.
5475 \end{enumerate}
5476 \item
5477 Otherwise, assign $\locvar{P}[0]$ the value zero.
5478 \end{enumerate}
5479 \item
5480 Otherwise, assign $\locvar{P}[0]$ the value zero.
5481 \end{enumerate}
5482 \item
5483 Otherwise, assign $\locvar{P}[0]$ the value zero.
5485 \item
5486 If block \locvar{\bi} is not along the left edge nor the bottom edge of the
5487 coded frame:
5488 \begin{enumerate}
5489 \item
5490 Assign \locvar{\bj} the coded-order index of block \locvar{\bi}'s lower-left
5491 neighbor, i.e., one row down and one column to the left.
5492 \item
5493 If $\bitvar{BCODED}[\bj]$ is not zero:
5494 \begin{enumerate}
5495 \item
5496 Assign \locvar{\mbj} the index of the macro block containing block
5497 \locvar{\bj}.
5498 \item
5499 If the value of the Reference Frame Index column of Table~\ref{tab:cm-refs}
5500 corresonding to $\bitvar{MBMODES}[\locvar{\mbj}]$ equals \locvar{\rfi}:
5501 \begin{enumerate}
5502 \item
5503 Assign $\locvar{P}[1]$ the value $1$.
5504 \item
5505 Assign $\locvar{PBI}[1]$ the value \locvar{\bj}.
5506 \end{enumerate}
5507 \item
5508 Otherwise, assign $\locvar{P}[1]$ the value zero.
5509 \end{enumerate}
5510 \item
5511 Otherwise, assign $\locvar{P}[1]$ the value zero.
5512 \end{enumerate}
5513 \item
5514 Otherwise, assign $\locvar{P}[1]$ the value zero.
5516 \item
5517 If block \locvar{\bi} is not along the the bottom edge of the coded frame:
5518 \begin{enumerate}
5519 \item
5520 Assign \locvar{\bj} the coded-order index of block \locvar{\bi}'s lower
5521 neighbor, i.e., in the same column but one row down.
5522 \item
5523 If $\bitvar{BCODED}[\bj]$ is not zero:
5524 \begin{enumerate}
5525 \item
5526 Assign \locvar{\mbj} the index of the macro block containing block
5527 \locvar{\bj}.
5528 \item
5529 If the value of the Reference Frame Index column of Table~\ref{tab:cm-refs}
5530 corresonding to $\bitvar{MBMODES}[\locvar{\mbj}]$ equals \locvar{\rfi}:
5531 \begin{enumerate}
5532 \item
5533 Assign $\locvar{P}[2]$ the value $1$.
5534 \item
5535 Assign $\locvar{PBI}[2]$ the value \locvar{\bj}.
5536 \end{enumerate}
5537 \item
5538 Otherwise, assign $\locvar{P}[2]$ the value zero.
5539 \end{enumerate}
5540 \item
5541 Otherwise, assign $\locvar{P}[2]$ the value zero.
5542 \end{enumerate}
5543 \item
5544 Otherwise, assign $\locvar{P}[2]$ the value zero.
5546 \item
5547 If block \locvar{\bi} is not along the right edge nor the bottom edge of the
5548 coded frame:
5549 \begin{enumerate}
5550 \item
5551 Assign \locvar{\bj} the coded-order index of block \locvar{\bi}'s lower-right
5552 neighbor, i.e., one row down and one column to the right.
5553 \item
5554 If $\bitvar{BCODED}[\bj]$ is not zero:
5555 \begin{enumerate}
5556 \item
5557 Assign \locvar{\mbj} the index of the macro block containing block
5558 \locvar{\bj}.
5559 \item
5560 If the value of the Reference Frame Index column of Table~\ref{tab:cm-refs}
5561 corresonding to $\bitvar{MBMODES}[\locvar{\mbj}]$ equals \locvar{\rfi}:
5562 \begin{enumerate}
5563 \item
5564 Assign $\locvar{P}[3]$ the value $1$.
5565 \item
5566 Assign $\locvar{PBI}[3]$ the value \locvar{\bj}.
5567 \end{enumerate}
5568 \item
5569 Otherwise, assign $\locvar{P}[3]$ the value zero.
5570 \end{enumerate}
5571 \item
5572 Otherwise, assign $\locvar{P}[3]$ the value zero.
5573 \end{enumerate}
5574 \item
5575 Otherwise, assign $\locvar{P}[3]$ the value zero.
5577 \item
5578 If none of the values $\locvar{P}[0]$, $\locvar{P}[1]$, $\locvar{P}[2]$, nor
5579 $\locvar{P}[3]$ are non-zero, then assign \bitvar{DCPRED} the value
5580 $\bitvar{LASTDC}[\locvar{\rfi}]$.
5581 \item
5582 Otherwise:
5583 \begin{enumerate}
5584 \item
5585 Assign the array \locvar{W} and the variable \locvar{PDIV} the values from the
5586 row of Table~\ref{tab:dc-weights} corresonding to the values of each
5587 $\locvar{P}[\idx{i}]$.
5589 \begin{table}[htb]
5590 \begin{center}
5591 \begin{tabular}{ccccrrrrr}\toprule
5592 \multicolumn{1}{p{25pt}}{\centering$\locvar{P}[0]$ (L)} &
5593 \multicolumn{1}{p{25pt}}{\centering$\locvar{P}[1]$ (DL)} &
5594 \multicolumn{1}{p{25pt}}{\centering$\locvar{P}[2]$ (D)} &
5595 \multicolumn{1}{p{25pt}}{\centering$\locvar{P}[3]$ (DR)} &
5596 \multicolumn{1}{p{25pt}}{\centering$\locvar{W}[3]$ (L)} &
5597 \multicolumn{1}{p{25pt}}{\centering$\locvar{W}[1]$ (DL)} &
5598 \multicolumn{1}{p{25pt}}{\centering$\locvar{W}[2]$ (D)} &
5599 \multicolumn{1}{p{25pt}}{\centering$\locvar{W}[3]$ (DR)} &
5600 \locvar{PDIV} \\\midrule
5601 $1$ & $0$ & $0$ & $0$ & $1$ & $0$ & $0$ & $0$ & $1$ \\
5602 $0$ & $1$ & $0$ & $0$ & $0$ & $1$ & $0$ & $0$ & $1$ \\
5603 $1$ & $1$ & $0$ & $0$ & $1$ & $0$ & $0$ & $0$ & $1$ \\
5604 $0$ & $0$ & $1$ & $0$ & $0$ & $0$ & $1$ & $0$ & $1$ \\
5605 $1$ & $0$ & $1$ & $0$ & $1$ & $0$ & $1$ & $0$ & $2$ \\
5606 $0$ & $1$ & $1$ & $0$ & $0$ & $0$ & $1$ & $0$ & $1$ \\
5607 $1$ & $1$ & $1$ & $0$ & $29$ & $-26$ & $29$ & $0$ & $32$ \\
5608 $0$ & $0$ & $0$ & $1$ & $0$ & $0$ & $0$ & $1$ & $1$ \\
5609 $1$ & $0$ & $0$ & $1$ & $75$ & $0$ & $0$ & $53$ & $128$ \\
5610 $0$ & $1$ & $0$ & $1$ & $0$ & $1$ & $0$ & $1$ & $2$ \\
5611 $1$ & $1$ & $0$ & $1$ & $75$ & $0$ & $0$ & $53$ & $128$ \\
5612 $0$ & $0$ & $1$ & $1$ & $0$ & $0$ & $1$ & $0$ & $1$ \\
5613 $1$ & $0$ & $1$ & $1$ & $75$ & $0$ & $0$ & $53$ & $128$ \\
5614 $0$ & $1$ & $1$ & $1$ & $0$ & $3$ & $10$ & $3$ & $16$ \\
5615 $1$ & $1$ & $1$ & $1$ & $29$ & $-26$ & $29$ & $0$ & $32$ \\
5616 \bottomrule\end{tabular}
5617 \end{center}
5618 \caption{Weights and Divisors for Each Set of Available DC Predictors}
5619 \label{tab:dc-weights}
5620 \end{table}
5622 \item
5623 Assign \bitvar{DCPRED} the value zero.
5624 \item
5625 If $\locvar{P}[0]$ is non-zero, assign \bitvar{DCPRED} the value
5626 $(\bitvar{DCPRED}+\locvar{W}[0]*\bitvar{COEFFS}[\locvar{PBI}[0]][0])$.
5627 \item
5628 If $\locvar{P}[1]$ is non-zero, assign \bitvar{DCPRED} the value
5629 $(\bitvar{DCPRED}+\locvar{W}[1]*\bitvar{COEFFS}[\locvar{PBI}[1]][0])$.
5630 \item
5631 If $\locvar{P}[2]$ is non-zero, assign \bitvar{DCPRED} the value
5632 $(\bitvar{DCPRED}+\locvar{W}[2]*\bitvar{COEFFS}[\locvar{PBI}[2]][0])$.
5633 \item
5634 If $\locvar{P}[3]$ is non-zero, assign \bitvar{DCPRED} the value
5635 $(\bitvar{DCPRED}+\locvar{W}[3]*\bitvar{COEFFS}[\locvar{PBI}[3]][0])$.
5636 \item
5637 Assign \bitvar{DCPRED} the value $(\bitvar{DCPRED}//\locvar{PDIV})$.
5638 \item
5639 If $\locvar{P}[0]$, $\locvar{P}[1]$, and $\locvar{P}[2]$ are all non-zero:
5640 \begin{enumerate}
5641 \item
5642 If $|\bitvar{DCPRED}-\bitvar{COEFFS}[\locvar{PBI}[2]][0]|$ is greater than
5643 $128$, assign \bitvar{DCPRED} the value $\bitvar{COEFFS}[\locvar{PBI}[2]][0]$.
5644 \item
5645 Otherwise, if $|\bitvar{DCPRED}-\bitvar{COEFFS}[\locvar{PBI}[0]][0]|$ is
5646 greater than $128$, assign \bitvar{DCPRED} the value
5647 $\bitvar{COEFFS}[\locvar{PBI}[0]][0]$.
5648 \item
5649 Otherwise, if $|\bitvar{DCPRED}-\bitvar{COEFFS}[\locvar{PBI}[1]][0]|$ is
5650 greater than $128$, assign \bitvar{DCPRED} the value
5651 $\bitvar{COEFFS}[\locvar{PBI}[1]][0]$.
5652 \end{enumerate}
5653 \end{enumerate}
5654 \end{enumerate}
5656 \subsection{Inverting the DC Prediction Process}
5657 \label{sub:dc-pred-undo}
5659 \paragraph{Input parameters:}\hfill\\*
5660 \begin{tabularx}{\textwidth}{@{}llrcX@{}}\toprule
5661 \multicolumn{1}{c}{Name} &
5662 \multicolumn{1}{c}{Type} &
5663 \multicolumn{1}{p{30pt}}{\centering Size (bits)} &
5664 \multicolumn{1}{c}{Signed?} &
5665 \multicolumn{1}{c}{Description and restrictions} \\\midrule\endhead
5666 \bitvar{BCODED} & \multicolumn{1}{p{40pt}}{Integer Array} &
5667 1 & No & An \bitvar{NBS}-element array of flags
5668 indicating which blocks are coded. \\
5669 \bitvar{MBMODES} & \multicolumn{1}{p{40pt}}{Integer Array} &
5670 3 & No & An \bitvar{NMBS}-element array of
5671 coding modes for each macro block. \\
5672 \bitvar{COEFFS} & \multicolumn{1}{p{50pt}}{2D Integer Array} &
5673 16 & Yes & An $\bitvar{NBS}\times 64$ array of
5674 quantized DCT coefficient values for each block in zig-zag order. \\
5675 \bottomrule\end{tabularx}
5677 \paragraph{Output parameters:}\hfill\\*
5678 \begin{tabularx}{\textwidth}{@{}llrcX@{}}\toprule
5679 \multicolumn{1}{c}{Name} &
5680 \multicolumn{1}{c}{Type} &
5681 \multicolumn{1}{p{30pt}}{\centering Size (bits)} &
5682 \multicolumn{1}{c}{Signed?} &
5683 \multicolumn{1}{c}{Description and restrictions} \\\midrule\endhead
5684 \bitvar{COEFFS} & \multicolumn{1}{p{50pt}}{2D Integer Array} &
5685 16 & Yes & An $\bitvar{NBS}\times 64$ array of
5686 quantized DCT coefficient values for each block in zig-zag order. The DC
5687 value of each block will be updated. \\
5688 \bottomrule\end{tabularx}
5690 \paragraph{Variables used:}\hfill\\*
5691 \begin{tabularx}{\textwidth}{@{}llrcX@{}}\toprule
5692 \multicolumn{1}{c}{Name} &
5693 \multicolumn{1}{c}{Type} &
5694 \multicolumn{1}{p{30pt}}{\centering Size (bits)} &
5695 \multicolumn{1}{c}{Signed?} &
5696 \multicolumn{1}{c}{Description and restrictions} \\\midrule\endhead
5697 \locvar{LASTDC} & \multicolumn{1}{p{40pt}}{Integer Array} &
5698 16 & Yes & A 3-element array containing the
5699 most recently decoded DC value, one for inter mode and for each reference
5700 frame. \\
5701 \locvar{DCPRED} & Integer & 11 & Yes & The predicted DC value for the current
5702 block. \\
5703 \locvar{DC} & Integer & 17 & Yes & The actual DC value for the current
5704 block. \\
5705 \locvar{\bi} & Integer & 36 & No & The index of the current block in
5706 coded order. \\
5707 \locvar{\mbi} & Integer & 32 & No & The index of the macro block
5708 containing block \locvar{\bi}. \\
5709 \locvar{\rfi} & Integer & 2 & No & The index of the reference frame
5710 indicated by the coding mode for macro block \locvar{\mbi}. \\
5711 \locvar{\pli} & Integer & 2 & No & A color plane index. \\
5712 \bottomrule\end{tabularx}
5713 \medskip
5715 This procedure describes the complete process of undoing the DC prediction to
5716 recover the original DC values.
5717 Because it is possible to add a value as large as $580$ to the predicted DC
5718 coefficient value at every block, which will then be used to increase the
5719 predictor for the next block, the reconstructed DC value could overflow a
5720 16-bit integer.
5721 This is handled by truncating the result to a 16-bit signed representation,
5722 simply throwing away any higher bits in the two's complement representation of
5723 the number.
5725 \begin{enumerate}
5726 \item
5727 For each consecutive value of \locvar{\pli} from $0$ to $2$:
5728 \begin{enumerate}
5729 \item
5730 Assign $\locvar{LASTDC}[0]$ the value zero.
5731 \item
5732 Assign $\locvar{LASTDC}[1]$ the value zero.
5733 \item
5734 Assign $\locvar{LASTDC}[2]$ the value zero.
5735 \item
5736 For each block of color plane \locvar{\pli} in {\em raster} order, with
5737 coded-order index \locvar{\bi}:
5738 \begin{enumerate}
5739 \item
5740 If $\bitvar{BCODED}[\locvar{\bi}]$ is non-zero:
5741 \begin{enumerate}
5742 \item
5743 Compute the value \locvar{DCPRED} using the procedure outlined in
5744 Section~\ref{sub:dc-pred}.
5745 \item
5746 Assign \locvar{DC} the value
5747 $(\bitvar{COEFFS}[\locvar{\bi}][0]+\locvar{DCPRED})$.
5748 \item
5749 Truncate \locvar{DC} to a 16-bit representation by dropping any higher-order
5750 bits.
5751 \item
5752 Assign $\bitvar{COEFFS}[\locvar{\bi}][0]$ the value \locvar{DC}.
5753 \item
5754 Assign \locvar{\mbi} the index of the macro block containing block
5755 \locvar{\bi}.
5756 \item
5757 Assign \locvar{\rfi} the value of the Reference Frame Index column of
5758 Table~\ref{tab:cm-refs} corresponding to $\bitvar{MBMODES}[\locvar{\mbi}]$.
5759 \item
5760 Assign $\locvar{LASTDC}[\rfi]$ the value $\locvar{DC}$.
5761 \end{enumerate}
5762 \end{enumerate}
5763 \end{enumerate}
5764 \end{enumerate}
5766 \section{Reconstruction}
5768 At this stage, the complete contents of the data packet have been decoded.
5769 All that remains is to reconstruct the contents of the new frame.
5770 This is applied on a block by block basis, and as each block is independent,
5771 the order they are processed in does not matter.
5773 \subsection{Predictors}
5774 \label{sec:predictors}
5776 For each block, a predictor is formed based on its coding mode and motion
5777 vector.
5778 There are three basic types of predictors: the intra predictor, the whole-pixel
5779 predictor, and the half-pixel predictor.
5780 The former is used for all blocks coded in INTRA mode, while all other blocks
5781 use one of the latter two.
5782 The whole-pixel predictor is used if the fractional part of both motion vector
5783 components is zero, otherwise the half-pixel predictor is used.
5785 \subsubsection{The Intra Predictor}
5786 \label{sub:predintra}
5788 \paragraph{Input parameters:} None.
5790 \paragraph{Output parameters:}\hfill\\*
5791 \begin{tabularx}{\textwidth}{@{}llrcX@{}}\toprule
5792 \multicolumn{1}{c}{Name} &
5793 \multicolumn{1}{c}{Type} &
5794 \multicolumn{1}{p{30pt}}{\centering Size (bits)} &
5795 \multicolumn{1}{c}{Signed?} &
5796 \multicolumn{1}{c}{Description and restrictions} \\\midrule\endhead
5797 \bitvar{PRED} & \multicolumn{1}{p{50pt}}{2D Integer Array} &
5798 8 & No & An $8\times 8$ array of predictor
5799 values to use for INTRA coded blocks. \\
5800 \bottomrule\end{tabularx}
5802 \paragraph{Variables used:}\hfill\\*
5803 \begin{tabularx}{\textwidth}{@{}llrcX@{}}\toprule
5804 \multicolumn{1}{c}{Name} &
5805 \multicolumn{1}{c}{Type} &
5806 \multicolumn{1}{p{30pt}}{\centering Size (bits)} &
5807 \multicolumn{1}{c}{Signed?} &
5808 \multicolumn{1}{c}{Description and restrictions} \\\midrule\endhead
5809 \locvar{\idx{bx}} & Integer & 3 & No & The horizontal pixel index in the
5810 block. \\
5811 \locvar{\idx{by}} & Integer & 3 & No & The vertical pixel index in the
5812 block. \\
5813 \bottomrule\end{tabularx}
5814 \medskip
5816 The intra predictor is nothing more than the constant value $128$.
5817 This is applied for the sole purpose of centering the range of possible DC
5818 values for INTRA blocks around zero.
5820 \begin{enumerate}
5821 \item
5822 For each value of \locvar{\idx{by}} from $0$ to $7$, inclusive:
5823 \begin{enumerate}
5824 \item
5825 For each value of \locvar{\idx{bx}} from $0$ to $7$, inclusive:
5826 \begin{enumerate}
5827 \item
5828 Assign $\bitvar{PRED}[\locvar{\idx{by}}][\locvar{\idx{bx}}]$ the value $128$.
5829 \end{enumerate}
5830 \end{enumerate}
5831 \end{enumerate}
5833 \subsubsection{The Whole-Pixel Predictor}
5834 \label{sub:predfullpel}
5836 \paragraph{Input parameters:}\hfill\\*
5837 \begin{tabularx}{\textwidth}{@{}llrcX@{}}\toprule
5838 \multicolumn{1}{c}{Name} &
5839 \multicolumn{1}{c}{Type} &
5840 \multicolumn{1}{p{30pt}}{\centering Size (bits)} &
5841 \multicolumn{1}{c}{Signed?} &
5842 \multicolumn{1}{c}{Description and restrictions} \\\midrule\endhead
5843 \bitvar{RPW} & Integer & 20 & No & The width of the current plane of the
5844 reference frame in pixels. \\
5845 \bitvar{RPH} & Integer & 20 & No & The height of the current plane of the
5846 reference frame in pixels. \\
5847 \bitvar{REFP} & \multicolumn{1}{p{50pt}}{2D Integer Array} &
5848 8 & No & A $\bitvar{RPH}\times\bitvar{RPW}$
5849 array containing the contents of the current plane of the reference frame. \\
5850 \bitvar{BX} & Integer & 20 & No & The horizontal pixel index of the
5851 lower-left corner of the current block. \\
5852 \bitvar{BY} & Integer & 20 & No & The vertical pixel index of the
5853 lower-left corner of the current block. \\
5854 \bitvar{MVX} & Integer & 5 & No & The horizontal component of the block
5855 motion vector.
5856 This is always a whole-pixel value. \\
5857 \bitvar{MVY} & Integer & 5 & No & The vertical component of the block
5858 motion vector.
5859 This is always a whole-pixel value. \\
5860 \bottomrule\end{tabularx}
5862 \paragraph{Output parameters:}\hfill\\*
5863 \begin{tabularx}{\textwidth}{@{}llrcX@{}}\toprule
5864 \multicolumn{1}{c}{Name} &
5865 \multicolumn{1}{c}{Type} &
5866 \multicolumn{1}{p{30pt}}{\centering Size (bits)} &
5867 \multicolumn{1}{c}{Signed?} &
5868 \multicolumn{1}{c}{Description and restrictions} \\\midrule\endhead
5869 \bitvar{PRED} & \multicolumn{1}{p{50pt}}{2D Integer Array} &
5870 8 & No & An $8\times 8$ array of predictor
5871 values to use for INTER coded blocks. \\
5872 \bottomrule\end{tabularx}
5874 \paragraph{Variables used:}\hfill\\*
5875 \begin{tabularx}{\textwidth}{@{}llrcX@{}}\toprule
5876 \multicolumn{1}{c}{Name} &
5877 \multicolumn{1}{c}{Type} &
5878 \multicolumn{1}{p{30pt}}{\centering Size (bits)} &
5879 \multicolumn{1}{c}{Signed?} &
5880 \multicolumn{1}{c}{Description and restrictions} \\\midrule\endhead
5881 \locvar{\idx{bx}} & Integer & 3 & Yes & The horizontal pixel index in the
5882 block. \\
5883 \locvar{\idx{by}} & Integer & 3 & Yes & The vertical pixel index in the
5884 block. \\
5885 \locvar{\idx{rx}} & Integer & 20 & No & The horizontal pixel index in the
5886 reference frame. \\
5887 \locvar{\idx{ry}} & Integer & 20 & No & The vertical pixel index in the
5888 reference frame. \\
5889 \bottomrule\end{tabularx}
5890 \medskip
5892 The whole pixel predictor simply copies verbatim the contents of the reference
5893 frame pointed to by the block's motion vector.
5894 If the vector points outside the reference frame, then the closest value on the
5895 edge of the reference frame is used instead.
5896 In practice, this is usually implemented by expanding the size of the reference
5897 frame by $8$ or $16$ pixels on each side---depending on whether or not the
5898 corresponding axis is subsampled in the current plane---and copying the border
5899 pixels into this region.
5901 \begin{enumerate}
5902 \item
5903 For each value of \locvar{\idx{by}} from $0$ to $7$, inclusive:
5904 \begin{enumerate}
5905 \item
5906 Assign \locvar{\idx{ry}} the value
5907 $(\bitvar{BY}+\bitvar{MVY}+\locvar{\idx{by}})$.
5908 \item
5909 If \locvar{\idx{ry}} is greater than $(\bitvar{RPH}-1)$, assign
5910 \locvar{\idx{ry}} the value $(\bitvar{RPH}-1)$.
5911 \item
5912 If \locvar{\idx{ry}} is less than zero, assign \locvar{\idx{ry}} the value
5913 zero.
5914 \item
5915 For each value of \locvar{\idx{bx}} from $0$ to $7$, inclusive:
5916 \begin{enumerate}
5917 \item
5918 Assign \locvar{\idx{rx}} the value
5919 $(\bitvar{BX}+\bitvar{MVX}+\locvar{\idx{bx}})$.
5920 \item
5921 If \locvar{\idx{rx}} is greater than $(\bitvar{RPW}-1)$, assign
5922 \locvar{\idx{rx}} the value $(\bitvar{RPW}-1)$.
5923 \item
5924 If \locvar{\idx{rx}} is less than zero, assign \locvar{\idx{rx}} the value
5925 zero.
5926 \item
5927 Assign $\bitvar{PRED}[\locvar{\idx{by}}][\locvar{\idx{bx}}]$ the value
5928 $\bitvar{REFP}[\locvar{\idx{ry}}][\locvar{\idx{rx}}]$.
5929 \end{enumerate}
5930 \end{enumerate}
5931 \end{enumerate}
5933 \subsubsection{The Half-Pixel Predictor}
5934 \label{sub:predhalfpel}
5936 \paragraph{Input parameters:}\hfill\\*
5937 \begin{tabularx}{\textwidth}{@{}llrcX@{}}\toprule
5938 \multicolumn{1}{c}{Name} &
5939 \multicolumn{1}{c}{Type} &
5940 \multicolumn{1}{p{30pt}}{\centering Size (bits)} &
5941 \multicolumn{1}{c}{Signed?} &
5942 \multicolumn{1}{c}{Description and restrictions} \\\midrule\endhead
5943 \bitvar{RPW} & Integer & 20 & No & The width of the current plane of the
5944 reference frame in pixels. \\
5945 \bitvar{RPH} & Integer & 20 & No & The height of the current plane of the
5946 reference frame in pixels. \\
5947 \bitvar{REFP} & \multicolumn{1}{p{50pt}}{2D Integer Array} &
5948 8 & No & A $\bitvar{RPH}\times\bitvar{RPW}$
5949 array containing the contents of the current plane of the reference frame. \\
5950 \bitvar{BX} & Integer & 20 & No & The horizontal pixel index of the
5951 lower-left corner of the current block. \\
5952 \bitvar{BY} & Integer & 20 & No & The vertical pixel index of the
5953 lower-left corner of the current block. \\
5954 \bitvar{MVX} & Integer & 5 & No & The horizontal component of the first
5955 whole-pixel motion vector. \\
5956 \bitvar{MVY} & Integer & 5 & No & The vertical component of the first
5957 whole-pixel motion vector. \\
5958 \bitvar{MVX2} & Integer & 5 & No & The horizontal component of the second
5959 whole-pixel motion vector. \\
5960 \bitvar{MVY2} & Integer & 5 & No & The vertical component of the second
5961 whole-pixel motion vector. \\
5962 \bottomrule\end{tabularx}
5964 \paragraph{Output parameters:}\hfill\\*
5965 \begin{tabularx}{\textwidth}{@{}llrcX@{}}\toprule
5966 \multicolumn{1}{c}{Name} &
5967 \multicolumn{1}{c}{Type} &
5968 \multicolumn{1}{p{30pt}}{\centering Size (bits)} &
5969 \multicolumn{1}{c}{Signed?} &
5970 \multicolumn{1}{c}{Description and restrictions} \\\midrule\endhead
5971 \bitvar{PRED} & \multicolumn{1}{p{50pt}}{2D Integer Array} &
5972 8 & No & An $8\times 8$ array of predictor
5973 values to use for INTER coded blocks. \\
5974 \bottomrule\end{tabularx}
5976 \paragraph{Variables used:}\hfill\\*
5977 \begin{tabularx}{\textwidth}{@{}llrcX@{}}\toprule
5978 \multicolumn{1}{c}{Name} &
5979 \multicolumn{1}{c}{Type} &
5980 \multicolumn{1}{p{30pt}}{\centering Size (bits)} &
5981 \multicolumn{1}{c}{Signed?} &
5982 \multicolumn{1}{c}{Description and restrictions} \\\midrule\endhead
5983 \locvar{\idx{bx}} & Integer & 3 & Yes & The horizontal pixel index in the
5984 block. \\
5985 \locvar{\idx{by}} & Integer & 3 & Yes & The vertical pixel index in the
5986 block. \\
5987 \locvar{\idx{rx1}} & Integer & 20 & No & The first horizontal pixel index in
5988 the reference frame. \\
5989 \locvar{\idx{ry1}} & Integer & 20 & No & The first vertical pixel index in the
5990 reference frame. \\
5991 \locvar{\idx{rx2}} & Integer & 20 & No & The second horizontal pixel index in
5992 the reference frame. \\
5993 \locvar{\idx{ry2}} & Integer & 20 & No & The second vertical pixel index in
5994 the reference frame. \\
5995 \bottomrule\end{tabularx}
5996 \medskip
5998 If one or both of the components of the block motion vector is not a
5999 whole-pixel value, then the half-pixel predictor is used.
6000 The half-pixel predictor converts the fractional motion vector into two
6001 whole-pixel motion vectors.
6002 The first is formed by truncating the values of each component towards zero,
6003 and the second is formed by truncating them away from zero.
6004 The contributions from the reference frame at the locations pointed to by each
6005 vector are averaged, truncating towards negative infinity.
6007 Only two samples from the reference frame contribute to each predictor value,
6008 even if both components of the motion vector have non-zero fractional
6009 components.
6010 Motion vector components with quarter-pixel accuracy in the chroma planes are
6011 treated exactly the same as those with half-pixel accuracy.
6012 Any non-zero fractional part gets rounded one way in the first vector, and the
6013 other way in the second.
6015 \begin{enumerate}
6016 \item
6017 For each value of \locvar{\idx{by}} from $0$ to $7$, inclusive:
6018 \begin{enumerate}
6019 \item
6020 Assign \locvar{\idx{ry1}} the value
6021 $(\bitvar{BY}+\bitvar{MVY1}+\locvar{\idx{by}})$.
6022 \item
6023 If \locvar{\idx{ry1}} is greater than $(\bitvar{RPH}-1)$, assign
6024 \locvar{\idx{ry1}} the value $(\bitvar{RPH}-1)$.
6025 \item
6026 If \locvar{\idx{ry1}} is less than zero, assign \locvar{\idx{ry1}} the value
6027 zero.
6028 \item
6029 Assign \locvar{\idx{ry2}} the value
6030 $(\bitvar{BY}+\bitvar{MVY2}+\locvar{\idx{by}})$.
6031 \item
6032 If \locvar{\idx{ry2}} is greater than $(\bitvar{RPH}-1)$, assign
6033 \locvar{\idx{ry2}} the value $(\bitvar{RPH}-1)$.
6034 \item
6035 If \locvar{\idx{ry2}} is less than zero, assign \locvar{\idx{ry2}} the value
6036 zero.
6037 \item
6038 For each value of \locvar{\idx{bx}} from $0$ to $7$, inclusive:
6039 \begin{enumerate}
6040 \item
6041 Assign \locvar{\idx{rx1}} the value
6042 $(\bitvar{BX}+\bitvar{MVX1}+\locvar{\idx{bx}})$.
6043 \item
6044 If \locvar{\idx{rx1}} is greater than $(\bitvar{RPW}-1)$, assign
6045 \locvar{\idx{rx1}} the value $(\bitvar{RPW}-1)$.
6046 \item
6047 If \locvar{\idx{rx1}} is less than zero, assign \locvar{\idx{rx1}} the value
6048 zero.
6049 \item
6050 Assign \locvar{\idx{rx2}} the value
6051 $(\bitvar{BX}+\bitvar{MVX2}+\locvar{\idx{bx}})$.
6052 \item
6053 If \locvar{\idx{rx2}} is greater than $(\bitvar{RPW}-1)$, assign
6054 \locvar{\idx{rx2}} the value $(\bitvar{RPW}-1)$.
6055 \item
6056 If \locvar{\idx{rx2}} is less than zero, assign \locvar{\idx{rx2}} the value
6057 zero.
6058 \item
6059 Assign $\bitvar{PRED}[\locvar{\idx{by}}][\locvar{\idx{bx}}]$ the value
6060 \begin{equation*}
6061 (\bitvar{REFP}[\locvar{\idx{ry1}}][\locvar{\idx{rx1}}]+
6062 \bitvar{REFP}[\locvar{\idx{ry2}}][\locvar{\idx{rx2}}])>>1.
6063 \end{equation*}
6064 \end{enumerate}
6065 \end{enumerate}
6066 \end{enumerate}
6068 \subsection{Dequantization}
6069 \label{sub:dequant}
6071 \paragraph{Input parameters:}\hfill\\*
6072 \begin{tabularx}{\textwidth}{@{}llrcX@{}}\toprule
6073 \multicolumn{1}{c}{Name} &
6074 \multicolumn{1}{c}{Type} &
6075 \multicolumn{1}{p{30pt}}{\centering Size (bits)} &
6076 \multicolumn{1}{c}{Signed?} &
6077 \multicolumn{1}{c}{Description and restrictions} \\\midrule\endhead
6078 \bitvar{COEFFS} & \multicolumn{1}{p{50pt}}{2D Integer Array} &
6079 16 & Yes & An $\bitvar{NBS}\times 64$ array of
6080 quantized DCT coefficient values for each block in zig-zag order. \\
6081 \bitvar{ACSCALE} & \multicolumn{1}{p{40pt}}{Integer array} &
6082 16 & No & A 64-element array of scale values for
6083 AC coefficients for each \qi\ value. \\
6084 \bitvar{DCSCALE} & \multicolumn{1}{p{40pt}}{Integer array} &
6085 16 & No & A 64-element array of scale values for
6086 the DC coefficient for each \qi\ value. \\
6087 \bitvar{BMS} & \multicolumn{1}{p{50pt}}{2D Integer array} &
6088 8 & No & A $\bitvar{NBMS}\times 64$ array
6089 containing the base matrices. \\
6090 \bitvar{NQRS} & \multicolumn{1}{p{50pt}}{2D Integer array} &
6091 6 & No & A $2\times 3$ array containing the
6092 number of quant ranges for a given \qti\ and \pli, respectively.
6093 This is at most $63$. \\
6094 \bitvar{QRSIZES} & \multicolumn{1}{p{50pt}}{3D Integer array} &
6095 6 & No & A $2\times 3\times 63$ array of the
6096 sizes of each quant range for a given \qti\ and \pli, respectively.
6097 Only the first $\bitvar{NQRS}[\qti][\pli]$ values are used. \\
6098 \bitvar{QRBMIS} & \multicolumn{1}{p{50pt}}{3D Integer array} &
6099 9 & No & A $2\times 3\times 64$ array of the
6100 \bmi's used for each quant range for a given \qti\ and \pli, respectively.
6101 Only the first $(\bitvar{NQRS}[\qti][\pli]+1)$ values are used. \\
6102 \bitvar{\qti} & Integer & 1 & No & A quantization type index.
6103 See Table~\ref{tab:quant-types}.\\
6104 \bitvar{\pli} & Integer & 2 & No & A color plane index.
6105 See Table~\ref{tab:color-planes}.\\
6106 \bitvar{\idx{qi0}} & Integer & 6 & No & The quantization index of the DC
6107 coefficient. \\
6108 \bitvar{\qi} & Integer & 6 & No & The quantization index of the AC
6109 coefficients. \\
6110 \bitvar{\bi} & Integer & 36 & No & The index of the current block in
6111 coded order. \\
6112 \bottomrule\end{tabularx}
6114 \paragraph{Output parameters:}\hfill\\*
6115 \begin{tabularx}{\textwidth}{@{}llrcX@{}}\toprule
6116 \multicolumn{1}{c}{Name} &
6117 \multicolumn{1}{c}{Type} &
6118 \multicolumn{1}{p{30pt}}{\centering Size (bits)} &
6119 \multicolumn{1}{c}{Signed?} &
6120 \multicolumn{1}{c}{Description and restrictions} \\\midrule\endhead
6121 \bitvar{DQC} & \multicolumn{1}{p{40pt}}{Integer Array} &
6122 14 & Yes & A $64$-element array of dequantized
6123 DCT coefficients in natural order (cf. Section~\ref{sec:dct-coeffs}). \\
6124 \bottomrule\end{tabularx}
6126 \paragraph{Variables used:}\hfill\\*
6127 \begin{tabularx}{\textwidth}{@{}llrcX@{}}\toprule
6128 \multicolumn{1}{c}{Name} &
6129 \multicolumn{1}{c}{Type} &
6130 \multicolumn{1}{p{30pt}}{\centering Size (bits)} &
6131 \multicolumn{1}{c}{Signed?} &
6132 \multicolumn{1}{c}{Description and restrictions} \\\midrule\endhead
6133 \locvar{QMAT} & \multicolumn{1}{p{40pt}}{Integer array} &
6134 16 & No & A 64-element array of quantization
6135 values for each DCT coefficient in natural order. \\
6136 \locvar{\ci} & Integer & 6 & No & The DCT coefficient index in natural
6137 order. \\
6138 \locvar{\zzi} & Integer & 6 & No & The DCT coefficient index in zig-zag
6139 order. \\
6140 \locvar{C} & Integer & 29 & Yes & A single dequantized coefficient. \\
6141 \bottomrule\end{tabularx}
6142 \medskip
6144 This procedure takes the quantized DCT coefficient values in zig-zag order for
6145 a single block---after DC prediction has been undone---and returns the
6146 dequantized values in natural order.
6147 If large coefficient values are decoded for coarsely quantized coefficients,
6148 the resulting dequantized value can be significantly larger than 16 bits.
6149 Such a coefficient is truncated to a signed 16-bit representation by discarding
6150 the higher-order bits of its twos-complement representation.
6152 Although this procedure recomputes the quantization matrices from the
6153 parameters in the setup header for each block, there are at most six different
6154 ones used for each color plane.
6155 An efficient implementation could compute them once in advance.
6157 \begin{enumerate}
6158 \item
6159 Using \bitvar{ACSCALE}, \bitvar{DCSCALE}, \bitvar{BMS}, \bitvar{NQRS},
6160 \bitvar{QRSIZES}, \bitvar{QRBMIS}, \bitvar{\qti}, \bitvar{\pli}, and
6161 \bitvar{\idx{qi0}}, use the procedure given in Section~\ref{sub:quant-mat} to
6162 compute the DC quantization matrix \locvar{QMAT}.
6163 \item
6164 Assign \locvar{C} the value
6165 $\bitvar{COEFFS}[\bitvar{\bi}][0]*\locvar{QMAT}[0]$.
6166 \item
6167 Truncate \locvar{C} to a 16-bit representation by dropping any higher-order
6168 bits.
6169 \item
6170 Assign $\bitvar{DQC}[0]$ the value \locvar{C}.
6171 \item
6172 Using \bitvar{ACSCALE}, \bitvar{DCSCALE}, \bitvar{BMS}, \bitvar{NQRS},
6173 \bitvar{QRSIZES}, \bitvar{QRBMIS}, \bitvar{\qti}, \bitvar{\pli}, and
6174 \bitvar{\qi}, use the procedure given in Section~\ref{sub:quant-mat} to
6175 compute the AC quantization matrix \locvar{QMAT}.
6176 \item
6177 For each value of \locvar{\ci} from 1 to 63, inclusive:
6178 \begin{enumerate}
6179 \item
6180 Assign \locvar{\zzi} the index in zig-zag order corresponding to \locvar{\ci}.
6181 E.g., the value at row $(\locvar{\ci}//8)$ and column $(\locvar{\ci}\%8)$ in
6182 Figure~\ref{tab:zig-zag}
6183 \item
6184 Assign \locvar{C} the value
6185 $\bitvar{COEFFS}[\bitvar{\bi}][\locvar{\zzi}]*\locvar{QMAT}[\locvar{\ci}]$.
6186 \item
6187 Truncate \locvar{C} to a 16-bit representation by dropping any higher-order
6188 bits.
6189 \item
6190 Assign $\bitvar{DQC}[\locvar{\ci}]$ the value \locvar{C}.
6191 \end{enumerate}
6192 \end{enumerate}
6194 \subsection{The Inverse DCT}
6196 The 2D inverse DCT is separated into two applications of the 1D inverse DCT.
6197 The transform is first applied to each row, and then applied to each column of
6198 the result.
6200 Each application of the 1D inverse DCT scales the values by a factor of two
6201 relative to the orthonormal version of the transform, for a total scale factor
6202 of four for the 2D transform.
6203 It is assumed that a similar scale factor is applied during the forward DCT
6204 used in the encoder, so that a division by 16 is required after the transform
6205 has been applied in both directions.
6206 The inclusion of this scale factor allows the integerized transform to operate
6207 with increased precision.
6208 All divisions throughout the transform are implemented with right shifts.
6209 Only the final division by $16$ is rounded, with ties rounded towards positive
6210 infinity.
6212 All intermediate values are truncated to a 32-bit signed representation by
6213 discarding any higher-order bits in their two's complement representation.
6214 The final output of each 1D transform is truncated to a 16-bit signed value in
6215 the same manner.
6216 In practice, if the high word of a $16\times 16$ bit multiplication can be
6217 obtained directly, 16 bits is sufficient for every calculation except scaling
6218 by $C4$.
6219 Thus we truncate to 16 bits before that multiplication to allow an
6220 implementation entirely in 16-bit registers.
6221 Implementations using larger registers must sign-extend the 16-bit value to
6222 maintain compatibility.
6224 Note that if 16-bit register are used, overflow in the additions and
6225 subtractions should be handled using \textit{unsaturated} arithmetic.
6226 That is, the high-order bits should be discarded and the low-order bits
6227 retained, instead of clamping the result to the maximum or minimum value.
6228 This allows the maximum flexibility in re-ordering these instructions without
6229 deviating from this specification.
6231 The 1D transform can only overflow if input coefficients larger than $\pm 6201$
6232 are present.
6233 However, the result of applying the 2D forward transform on pixel values in the
6234 range $-255\ldots 255$ can be as large as $\pm 8157$ due to the scale factor
6235 of four that is applied, and quantization errors could make this even larger.
6236 Therefore, the coefficients cannot simply be clamped into a valid range before
6237 the transform.
6239 \subsubsection{The 1D Inverse DCT}
6240 \label{sub:1d-idct}
6242 \paragraph{Input parameters:}\hfill\\*
6243 \begin{tabularx}{\textwidth}{@{}llrcX@{}}\toprule
6244 \multicolumn{1}{c}{Name} &
6245 \multicolumn{1}{c}{Type} &
6246 \multicolumn{1}{p{30pt}}{\centering Size (bits)} &
6247 \multicolumn{1}{c}{Signed?} &
6248 \multicolumn{1}{c}{Description and restrictions} \\\midrule\endhead
6249 \bitvar{Y} & \multicolumn{1}{p{40pt}}{Integer Array} &
6250 16 & Yes & An 8-element array of DCT
6251 coefficients. \\
6252 \bottomrule\end{tabularx}
6254 \paragraph{Output parameters:}\hfill\\*
6255 \begin{tabularx}{\textwidth}{@{}llrcX@{}}\toprule
6256 \multicolumn{1}{c}{Name} &
6257 \multicolumn{1}{c}{Type} &
6258 \multicolumn{1}{p{30pt}}{\centering Size (bits)} &
6259 \multicolumn{1}{c}{Signed?} &
6260 \multicolumn{1}{c}{Description and restrictions} \\\midrule\endhead
6261 \bitvar{X} & \multicolumn{1}{p{40pt}}{Integer Array} &
6262 16 & Yes & An 8-element array of output values. \\
6263 \bottomrule\end{tabularx}
6265 \paragraph{Variables used:}\hfill\\*
6266 \begin{tabularx}{\textwidth}{@{}llrcX@{}}\toprule
6267 \multicolumn{1}{c}{Name} &
6268 \multicolumn{1}{c}{Type} &
6269 \multicolumn{1}{p{30pt}}{\centering Size (bits)} &
6270 \multicolumn{1}{c}{Signed?} &
6271 \multicolumn{1}{c}{Description and restrictions} \\\midrule\endhead
6272 \locvar{T} & \multicolumn{1}{p{40pt}}{Integer Array} &
6273 32 & Yes & An 8-element array containing the
6274 current value of each signal line. \\
6275 \locvar{R} & Integer & 32 & Yes & A temporary value. \\
6276 \bottomrule\end{tabularx}
6277 \medskip
6279 A compliant decoder MUST use the exact implementation of the inverse DCT
6280 defined in this specification.
6281 Some operations may be re-ordered, but the result must be precisely equivalent.
6282 This is a design decision that limits some avenues of decoder optimization, but
6283 prevents any drift in the prediction loop.
6284 Theora uses a 16-bit integerized approximation of of the 8-point 1D inverse DCT
6285 based on the Chen factorization \cite{CSF77}.
6286 It requires 16 multiplications and 26 additions and subtractions.
6288 \begin{figure}[htbp]
6289 \begin{center}
6290 \includegraphics[width=\textwidth]{idct}
6291 \end{center}
6292 \caption{Signal Flow Graph for the 1D Inverse DCT}
6293 \label{fig:idct}
6294 \end{figure}
6296 A signal flow graph of the transformation is presented in
6297 Figure~\ref{fig:idct}.
6298 This graph provides a good visualization of which parts of the transform are
6299 parallelizable.
6300 Time increases from left to right.
6302 Each signal line is involved in an operation where the line is marked with a
6303 dot $\cdot$ or a circled plus sign $\oplus$.
6304 The constants $\locvar{C}i$ and $\locvar{S}j$ are the 16-bit integer
6305 approximations of $\cos(\frac{i\pi}{16})$ and $\sin(\frac{j\pi}{16})$ listed
6306 in Table~\ref{tab:dct-consts}.
6307 When they appear next to a signal line, the value on that line is scaled by the
6308 given constant.
6309 A circled minus sign $\ominus$ next to a signal line indicates that the value
6310 on that line is negated.
6312 Operations on a single signal path through the graph cannot be reordered, but
6313 operations on different paths may be, or may be executed in parallel.
6314 Different graphs may be obtainable using the associative, commutative, and
6315 distributive properties of unsaturated arithmetic.
6316 The column of numbers on the left represents an initial permutation of the
6317 input DCT coefficients.
6318 The column on the right represents the unpermuted output.
6319 One can be obtained by bit-reversing the 3-bit binary representation of the
6320 other.
6322 \begin{table}[htbp]
6323 \begin{center}
6324 \begin{tabular}{llr}\toprule
6325 $\locvar{C}i$ & $\locvar{S}j$ & Value \\\midrule
6326 $\locvar{C1}$ & $\locvar{S7}$ & $64277$ \\
6327 $\locvar{C2}$ & $\locvar{S6}$ & $60547$ \\
6328 $\locvar{C3}$ & $\locvar{S5}$ & $54491$ \\
6329 $\locvar{C4}$ & $\locvar{S4}$ & $46341$ \\
6330 $\locvar{C5}$ & $\locvar{S3}$ & $36410$ \\
6331 $\locvar{C6}$ & $\locvar{S2}$ & $25080$ \\
6332 $\locvar{C7}$ & $\locvar{S1}$ & $12785$ \\
6333 \bottomrule\end{tabular}
6334 \end{center}
6335 \caption{16-bit Approximations of Sines and Cosines}
6336 \label{tab:dct-consts}
6337 \end{table}
6339 \begin{enumerate}
6340 \item
6341 Assign $\locvar{T}[0]$ the value $\bitvar{Y}[0]+\bitvar{Y}[4]$.
6342 \item
6343 Truncate $\locvar{T}[0]$ to a 16-bit signed representation by dropping any
6344 higher-order bits.
6345 \item
6346 Assign $\locvar{T}[0]$ the value
6347 $\locvar{C4}*\locvar{T}[0]>>16$.
6348 \item
6349 Assign $\locvar{T}[1]$ the value $\bitvar{Y}[0]-\bitvar{Y}[4]$.
6350 \item
6351 Truncate $\locvar{T}[1]$ to a 16-bit signed representation by dropping any
6352 higher-order bits.
6353 \item
6354 Assign $\locvar{T}[1]$ the value $\locvar{C4}*\locvar{T}[1]>>16$.
6355 \item
6356 Assign $\locvar{T}[2]$ the value $(\locvar{C6}*\bitvar{Y}[2]>>16)-
6357 (\locvar{S6}*\bitvar{Y}[6]>>16)$.
6358 \item
6359 Assign $\locvar{T}[3]$ the value $(\locvar{S6}*\bitvar{Y}[2]>>16)+
6360 (\locvar{C6}*\bitvar{Y}[6]>>16)$.
6361 \item
6362 Assign $\locvar{T}[4]$ the value $(\locvar{C7}*\bitvar{Y}[1]>>16)-
6363 (\locvar{S7}*\bitvar{Y}[7]>>16)$.
6364 \item
6365 Assign $\locvar{T}[5]$ the value $(\locvar{C3}*\bitvar{Y}[5]>>16)-
6366 (\locvar{S3}*\bitvar{Y}[3]>>16)$.
6367 \item
6368 Assign $\locvar{T}[6]$ the value $(\locvar{S3}*\bitvar{Y}[5]>>16)+
6369 (\locvar{C3}*\bitvar{Y}[3]>>16)$.
6370 \item
6371 Assign $\locvar{T}[7]$ the value $(\locvar{S7}*\bitvar{Y}[1]>>16)+
6372 (\locvar{C7}*\bitvar{Y}[7]>>16)$.
6373 \item
6374 Assign \locvar{R} the value $\locvar{T}[4]+\locvar{T}[5]$.
6375 \item
6376 Assign $\locvar{T}[5]$ the value $\locvar{T}[4]-\locvar{T}[5]$.
6377 \item
6378 Truncate $\locvar{T}[5]$ to a 16-bit signed representation by dropping any
6379 higher-order bits.
6380 \item
6381 Assign $\locvar{T}[5]$ the value $\locvar{C4}*\locvar{T}[5]>>16$.
6382 \item
6383 Assign $\locvar{T}[4]$ the value $\locvar{R}$.
6384 \item
6385 Assign \locvar{R} the value $\locvar{T}[7]+\locvar{T}[6]$.
6386 \item
6387 Assign $\locvar{T}[6]$ the value $\locvar{T}[7]-\locvar{T}[6]$.
6388 \item
6389 Truncate $\locvar{T}[6]$ to a 16-bit signed representation by dropping any
6390 higher-order bits.
6391 \item
6392 Assign $\locvar{T}[6]$ the value $\locvar{C4}*\locvar{T}[6]>>16$.
6393 \item
6394 Assign $\locvar{T}[7]$ the value $\locvar{R}$.
6395 \item
6396 Assign \locvar{R} the value $\locvar{T}[0]+\locvar{T}[3]$.
6397 \item
6398 Assign $\locvar{T}[3]$ the value $\locvar{T}[0]-\locvar{T}[3]$.
6399 \item
6400 Assign $\locvar{T}[0]$ the value \locvar{R}.
6401 \item
6402 Assign \locvar{R} the value $\locvar{T}[1]+\locvar{T}[2]$
6403 \item
6404 Assign $\locvar{T}[2]$ the value $\locvar{T}[1]-\locvar{T}[2]$
6405 \item
6406 Assign $\locvar{T}[1]$ the value \locvar{R}.
6407 \item
6408 Assign \locvar{R} the value $\locvar{T}[6]+\locvar{T}[5]$.
6409 \item
6410 Assign $\locvar{T}[5]$ the value $\locvar{T}[6]-\locvar{T}[5]$.
6411 \item
6412 Assign $\locvar{T}[6]$ the value \locvar{R}.
6413 \item
6414 Assign \locvar{R} the value $\locvar{T}[0]+\locvar{T}[7]$.
6415 \item
6416 Truncate \locvar{R} to a 16-bit signed representation by dropping any
6417 higher-order bits.
6418 \item
6419 Assign $\bitvar{X}[0]$ the value \locvar{R}.
6420 \item
6421 Assign \locvar{R} the value $\locvar{T}[1]+\locvar{T}[6]$.
6422 \item
6423 Truncate \locvar{R} to a 16-bit signed representation by dropping any
6424 higher-order bits.
6425 \item
6426 Assign $\bitvar{X}[1]$ the value \locvar{R}.
6427 \item
6428 Assign \locvar{R} the value $\locvar{T}[2]+\locvar{T}[5]$.
6429 \item
6430 Truncate \locvar{R} to a 16-bit signed representation by dropping any
6431 higher-order bits.
6432 \item
6433 Assign $\bitvar{X}[2]$ the value \locvar{R}.
6434 \item
6435 Assign \locvar{R} the value $\locvar{T}[3]+\locvar{T}[4]$.
6436 \item
6437 Truncate \locvar{R} to a 16-bit signed representation by dropping any
6438 higher-order bits.
6439 \item
6440 Assign $\bitvar{X}[3]$ the value \locvar{R}.
6441 \item
6442 Assign \locvar{R} the value $\locvar{T}[3]-\locvar{T}[4]$.
6443 \item
6444 Truncate \locvar{R} to a 16-bit signed representation by dropping any
6445 higher-order bits.
6446 \item
6447 Assign $\bitvar{X}[4]$ the value \locvar{R}.
6448 \item
6449 Assign \locvar{R} the value $\locvar{T}[2]-\locvar{T}[5]$.
6450 \item
6451 Truncate \locvar{R} to a 16-bit signed representation by dropping any
6452 higher-order bits.
6453 \item
6454 Assign $\bitvar{X}[5]$ the value \locvar{R}.
6455 \item
6456 Assign \locvar{R} the value $\locvar{T}[1]-\locvar{T}[6]$.
6457 \item
6458 Truncate \locvar{R} to a 16-bit signed representation by dropping any
6459 higher-order bits.
6460 \item
6461 Assign $\bitvar{X}[6]$ the value \locvar{R}.
6462 \item
6463 Assign \locvar{R} the value $\locvar{T}[0]-\locvar{T}[7]$.
6464 \item
6465 Truncate \locvar{R} to a 16-bit signed representation by dropping any
6466 higher-order bits.
6467 \item
6468 Assign $\bitvar{X}[7]$ the value \locvar{R}.
6469 \end{enumerate}
6471 \subsubsection{The 2D Inverse DCT}
6472 \label{sub:2d-idct}
6474 \paragraph{Input parameters:}\hfill\\*
6475 \begin{tabularx}{\textwidth}{@{}llrcX@{}}\toprule
6476 \multicolumn{1}{c}{Name} &
6477 \multicolumn{1}{c}{Type} &
6478 \multicolumn{1}{p{30pt}}{\centering Size (bits)} &
6479 \multicolumn{1}{c}{Signed?} &
6480 \multicolumn{1}{c}{Description and restrictions} \\\midrule\endhead
6481 \bitvar{DQC} & \multicolumn{1}{p{40pt}}{Integer Array} &
6482 14 & Yes & A $64$-element array of dequantized
6483 DCT coefficients in natural order (cf. Section~\ref{sec:dct-coeffs}). \\
6484 \bottomrule\end{tabularx}
6486 \paragraph{Output parameters:}\hfill\\*
6487 \begin{tabularx}{\textwidth}{@{}llrcX@{}}\toprule
6488 \multicolumn{1}{c}{Name} &
6489 \multicolumn{1}{c}{Type} &
6490 \multicolumn{1}{p{30pt}}{\centering Size (bits)} &
6491 \multicolumn{1}{c}{Signed?} &
6492 \multicolumn{1}{c}{Description and restrictions} \\\midrule\endhead
6493 \bitvar{RES} & \multicolumn{1}{p{50pt}}{2D Integer Array} &
6494 16 & Yes & An $8\times 8$ array containing the
6495 decoded residual for the current block. \\
6496 \bottomrule\end{tabularx}
6498 \paragraph{Variables used:}\hfill\\*
6499 \begin{tabularx}{\textwidth}{@{}llrcX@{}}\toprule
6500 \multicolumn{1}{c}{Name} &
6501 \multicolumn{1}{c}{Type} &
6502 \multicolumn{1}{p{30pt}}{\centering Size (bits)} &
6503 \multicolumn{1}{c}{Signed?} &
6504 \multicolumn{1}{c}{Description and restrictions} \\\midrule\endhead
6505 \locvar{\ci} & Integer & 3 & No & The column index. \\
6506 \locvar{\ri} & Integer & 3 & No & The row index. \\
6507 \locvar{Y} & \multicolumn{1}{p{40pt}}{Integer Array} &
6508 16 & Yes & An 8-element array of 1D iDCT input
6509 values. \\
6510 \locvar{X} & \multicolumn{1}{p{40pt}}{Integer Array} &
6511 16 & Yes & An 8-element array of 1D iDCT output
6512 values. \\
6513 \bottomrule\end{tabularx}
6514 \medskip
6516 This procedure applies the 1D inverse DCT transform 16 times to a block of
6517 dequantized coefficients: once for each of the 8 rows, and once for each of
6518 the 8 columns of the result.
6519 Note that the coordinate system used for the columns is the same right-handed
6520 coordinate system used by the rest of Theora.
6521 Thus, the column is indexed from bottom to top, not top to bottom.
6522 The final values are divided by sixteen, rounding with ties rounded towards
6523 postive infinity.
6525 \begin{enumerate}
6526 \item
6527 For each value of \locvar{\ri} from 0 to 7:
6528 \begin{enumerate}
6529 \item
6530 For each value of \locvar{\ci} from 0 to 7:
6531 \begin{enumerate}
6532 \item
6533 Assign $\locvar{Y}[\locvar{\ci}]$ the value
6534 $\bitvar{DQC}[\locvar{\ri}*8+\locvar{\ci}]$.
6535 \end{enumerate}
6536 \item
6537 Compute \locvar{X}, the 1D inverse DCT of \locvar{Y} using the procedure
6538 described in Section~\ref{sub:1d-idct}.
6539 \item
6540 For each value of $\locvar{\ci}$ from 0 to 7:
6541 \begin{enumerate}
6542 \item
6543 Assign $\bitvar{RES}[\locvar{\ri}][\locvar{\ci}]$ the value
6544 $\locvar{X}[\locvar{\ci}]$.
6545 \end{enumerate}
6546 \end{enumerate}
6547 \item
6548 For each value of \locvar{\ci} from 0 to 7:
6549 \begin{enumerate}
6550 \item
6551 For each value of \locvar{\ri} from 0 to 7:
6552 \begin{enumerate}
6553 \item
6554 Assign $\locvar{Y}[\locvar{\ri}]$ the value
6555 $\bitvar{RES}[\locvar{\ri}][\locvar{\ci}]$.
6556 \end{enumerate}
6557 \item
6558 Compute \locvar{X}, the 1D inverse DCT of \locvar{Y} using the procedure
6559 described in Section~\ref{sub:1d-idct}.
6560 \item
6561 For each value of \locvar{\ri} from 0 to 7:
6562 \begin{enumerate}
6563 \item
6564 Assign $\bitvar{RES}[\locvar{\ri}][\locvar{\ci}]$ the value
6565 $(\locvar{X}[\locvar{\ri}]+8)>>4$.
6566 \end{enumerate}
6567 \end{enumerate}
6568 \end{enumerate}
6570 \subsubsection{The 1D Forward DCT (Non-Normative)}
6572 \paragraph{Input parameters:}\hfill\\*
6573 \begin{tabularx}{\textwidth}{@{}llrcX@{}}\toprule
6574 \multicolumn{1}{c}{Name} &
6575 \multicolumn{1}{c}{Type} &
6576 \multicolumn{1}{p{30pt}}{\centering Size (bits)} &
6577 \multicolumn{1}{c}{Signed?} &
6578 \multicolumn{1}{c}{Description and restrictions} \\\midrule\endhead
6579 \bitvar{X} & \multicolumn{1}{p{40pt}}{Integer Array} &
6580 14 & Yes & An 8-element array of input values. \\
6581 \bottomrule\end{tabularx}
6583 \paragraph{Output parameters:}\hfill\\*
6584 \begin{tabularx}{\textwidth}{@{}llrcX@{}}\toprule
6585 \multicolumn{1}{c}{Name} &
6586 \multicolumn{1}{c}{Type} &
6587 \multicolumn{1}{p{30pt}}{\centering Size (bits)} &
6588 \multicolumn{1}{c}{Signed?} &
6589 \multicolumn{1}{c}{Description and restrictions} \\\midrule\endhead
6590 \bitvar{Y} & \multicolumn{1}{p{40pt}}{Integer Array} &
6591 16 & Yes & An 8-element array of DCT
6592 coefficients. \\
6593 \bottomrule\end{tabularx}
6595 \paragraph{Variables used:}\hfill\\*
6596 \begin{tabularx}{\textwidth}{@{}llrcX@{}}\toprule
6597 \multicolumn{1}{c}{Name} &
6598 \multicolumn{1}{c}{Type} &
6599 \multicolumn{1}{p{30pt}}{\centering Size (bits)} &
6600 \multicolumn{1}{c}{Signed?} &
6601 \multicolumn{1}{c}{Description and restrictions} \\\midrule\endhead
6602 \locvar{T} & \multicolumn{1}{p{40pt}}{Integer Array} &
6603 16 & Yes & An 8-element array containing the
6604 current value of each signal line. \\
6605 \locvar{R} & Integer & 16 & Yes & A temporary value. \\
6606 \bottomrule\end{tabularx}
6607 \medskip
6609 The forward transform used in the encoder is not mandated by this standard as
6610 the inverse one is.
6611 Precise equivalence in the inverse transform alone is all that is required to
6612 guarantee that there is no mismatch in the prediction loop between encoder and
6613 any compliant decoder implementation.
6614 However, a forward transform is provided here as a convenience for implementing
6615 an encoder.
6616 This is the version of the transform used by Xiph.org's Theora encoder, which
6617 is the same as that used by VP3.
6618 Like the inverse DCT, it is first applied to each row, and then applied to each
6619 column of the result.
6621 \begin{figure}[htbp]
6622 \begin{center}
6623 \includegraphics[width=\textwidth]{fdct}
6624 \end{center}
6625 \caption{Signal Flow Graph for the 1D Forward DCT}
6626 \label{fig:fdct}
6627 \end{figure}
6629 The signal flow graph for the forward transform is given in
6630 Figure~\ref{fig:fdct}.
6631 It is largely the reverse of the flow graph given for the inverse DCT.
6632 It is important to note that the signs on the constants in the rotations have
6633 changed, and the \locvar{C4} scale factors on one of the lower butterflies now
6634 appear on the opposite side.
6635 The column of numbers on the left represents the unpermuted input, and the
6636 column on the right the permuted output DCT coefficients.
6638 A proper division by $2^{16}$ is done after the multiplications instead of a
6639 shift in the forward transform.
6640 This can be implemented quickly by adding an offset of $\hex{FFFF}$ if the
6641 number is negative, and then shifting as before.
6642 This slightly increases the computational complexity of the transform.
6643 Unlike the inverse DCT, 16-bit registers and a $16\times16\rightarrow32$ bit
6644 multiply are sufficient to avoid any overflow, so long as the input is in the
6645 range $-6270\ldots 6270$, which is larger than required.
6647 \begin{enumerate}
6648 \item
6649 Assign $\locvar{T}[0]$ the value $\bitvar{X}[0]+\bitvar{X}[7]$.
6650 \item
6651 Assign $\locvar{T}[1]$ the value $\bitvar{X}[1]+\bitvar{X}[6]$.
6652 \item
6653 Assign $\locvar{T}[2]$ the value $\bitvar{X}[2]+\bitvar{X}[5]$.
6654 \item
6655 Assign $\locvar{T}[3]$ the value $\bitvar{X}[3]+\bitvar{X}[4]$.
6656 \item
6657 Assign $\locvar{T}[4]$ the value $\bitvar{X}[3]-\bitvar{X}[4]$.
6658 \item
6659 Assign $\locvar{T}[5]$ the value $\bitvar{X}[2]-\bitvar{X}[5]$.
6660 \item
6661 Assign $\locvar{T}[6]$ the value $\bitvar{X}[1]-\bitvar{X}[6]$.
6662 \item
6663 Assign $\locvar{T}[7]$ the value $\bitvar{X}[0]-\bitvar{X}[7]$.
6664 \item
6665 Assign \locvar{R} the value $\locvar{T}[0]+\locvar{T}[3]$.
6666 \item
6667 Assign $\locvar{T}[3]$ the value $\locvar{T}[0]-\locvar{T}[3]$.
6668 \item
6669 Assign $\locvar{T}[0]$ the value \locvar{R}.
6670 \item
6671 Assign \locvar{R} the value $\locvar{T}[1]+\locvar{T}[2]$.
6672 \item
6673 Assign $\locvar{T}[2]$ the value $\locvar{T}[1]-\locvar{T}[2]$.
6674 \item
6675 Assign $\locvar{T}[1]$ the value \locvar{R}.
6676 \item
6677 Assign \locvar{R} the value $\locvar{T}[6]-\locvar{T}[5]$.
6678 \item
6679 Assign $\locvar{T}[6]$ the value
6680 $(\locvar{C4}*(\locvar{T}[6]+\locvar{T}[5]))//16$.
6681 \item
6682 Assign $\locvar{T}[5]$ the value $(\locvar{C4}*\locvar{R})//16$.
6683 \item
6684 Assign \locvar{R} the value $\locvar{T}[4]+\locvar{T}[5]$.
6685 \item
6686 Assign $\locvar{T}[5]$ the value $\locvar{T}[4]-\locvar{T}[5]$.
6687 \item
6688 Assign $\locvar{T}[4]$ the value \locvar{R}.
6689 \item
6690 Assign \locvar{R} the value $\locvar{T}[7]+\locvar{T}[6]$.
6691 \item
6692 Assign $\locvar{T}[6]$ the value $\locvar{T}[7]-\locvar{T}[6]$.
6693 \item
6694 Assign $\locvar{T}[7]$ the value \locvar{R}.
6695 \item
6696 Assign $\bitvar{Y}[0]$ the value
6697 $(\locvar{C4}*(\locvar{T}[0]+\locvar{T}[1]))//16$.
6698 \item
6699 Assign $\bitvar{Y}[4]$ the value
6700 $(\locvar{C4}*(\locvar{T}[0]-\locvar{T}[1]))//16$.
6701 \item
6702 Assign $\bitvar{Y}[2]$ the value
6703 $((\locvar{S6}*\locvar{T}[3])//16)+
6704 ((\locvar{C6}*\locvar{T}[2])//16)$.
6705 \item
6706 Assign $\bitvar{Y}[6]$ the value
6707 $((\locvar{C6}*\locvar{T}[3])//16)-
6708 ((\locvar{S6}*\locvar{T}[2])//16)$.
6709 \item
6710 Assign $\bitvar{Y}[1]$ the value
6711 $((\locvar{S7}*\locvar{T}[7])//16)+
6712 ((\locvar{C7}*\locvar{T}[4])//16)$.
6713 \item
6714 Assign $\bitvar{Y}[5]$ the value
6715 $((\locvar{S3}*\locvar{T}[6])//16)+
6716 ((\locvar{C3}*\locvar{T}[5])//16)$.
6717 \item
6718 Assign $\bitvar{Y}[3]$ the value
6719 $((\locvar{C3}*\locvar{T}[6])//16)-
6720 ((\locvar{S3}*\locvar{T}[5])//16)$.
6721 \item
6722 Assign $\bitvar{Y}[7]$ the value
6723 $((\locvar{C7}*\locvar{T}[7])//16)-
6724 ((\locvar{S7}*\locvar{T}[4])//16)$.
6725 \end{enumerate}
6727 \subsection{The Complete Reconstruction Algorithm}
6728 \label{sub:recon}
6730 \paragraph{Input parameters:}\hfill\\*
6731 \begin{tabularx}{\textwidth}{@{}llrcX@{}}\toprule
6732 \multicolumn{1}{c}{Name} &
6733 \multicolumn{1}{c}{Type} &
6734 \multicolumn{1}{p{30pt}}{\centering Size (bits)} &
6735 \multicolumn{1}{c}{Signed?} &
6736 \multicolumn{1}{c}{Description and restrictions} \\\midrule\endhead
6737 \bitvar{ACSCALE} & \multicolumn{1}{p{40pt}}{Integer array} &
6738 16 & No & A 64-element array of scale values
6739 for AC coefficients for each \qi\ value. \\
6740 \bitvar{DCSCALE} & \multicolumn{1}{p{40pt}}{Integer array} &
6741 16 & No & A 64-element array of scale values
6742 for the DC coefficient for each \qi\ value. \\
6743 \bitvar{BMS} & \multicolumn{1}{p{50pt}}{2D Integer array} &
6744 8 & No & A $\bitvar{NBMS}\times 64$ array
6745 containing the base matrices. \\
6746 \bitvar{NQRS} & \multicolumn{1}{p{50pt}}{2D Integer array} &
6747 6 & No & A $2\times 3$ array containing the
6748 number of quant ranges for a given \qti\ and \pli, respectively.
6749 This is at most $63$. \\
6750 \bitvar{QRSIZES} & \multicolumn{1}{p{50pt}}{3D Integer array} &
6751 6 & No & A $2\times 3\times 63$ array of the
6752 sizes of each quant range for a given \qti\ and \pli, respectively.
6753 Only the first $\bitvar{NQRS}[\qti][\pli]$ values are used. \\
6754 \bitvar{QRBMIS} & \multicolumn{1}{p{50pt}}{3D Integer array} &
6755 9 & No & A $2\times 3\times 64$ array of the
6756 \bmi's used for each quant range for a given \qti\ and \pli, respectively.
6757 Only the first $(\bitvar{NQRS}[\qti][\pli]+1)$ values are used. \\
6758 \bitvar{RPYW} & Integer & 20 & No & The width of the $Y'$ plane of the
6759 reference frames in pixels. \\
6760 \bitvar{RPYH} & Integer & 20 & No & The height of the $Y'$ plane of the
6761 reference frames in pixels. \\
6762 \bitvar{RPCW} & Integer & 20 & No & The width of the $C_b$ and $C_r$
6763 planes of the reference frames in pixels. \\
6764 \bitvar{RPCH} & Integer & 20 & No & The height of the $C_b$ and $C_r$
6765 planes of the reference frames in pixels. \\
6766 \bitvar{GOLDREFY} & \multicolumn{1}{p{50pt}}{2D Integer Array} &
6767 8 & No & A $\bitvar{RPYH}\times\bitvar{RPYW}$
6768 array containing the contents of the $Y'$ plane of the golden reference
6769 frame. \\
6770 \bitvar{GOLDREFCB} & \multicolumn{1}{p{50pt}}{2D Integer Array} &
6771 8 & No & A $\bitvar{RPCH}\times\bitvar{RPCW}$
6772 array containing the contents of the $C_b$ plane of the golden reference
6773 frame. \\
6774 \bitvar{GOLDREFCR} & \multicolumn{1}{p{50pt}}{2D Integer Array} &
6775 8 & No & A $\bitvar{RPCH}\times\bitvar{RPCW}$
6776 array containing the contents of the $C_r$ plane of the golden reference
6777 frame. \\
6778 \bitvar{PREVREFY} & \multicolumn{1}{p{50pt}}{2D Integer Array} &
6779 8 & No & A $\bitvar{RPYH}\times\bitvar{RPYW}$
6780 array containing the contents of the $Y'$ plane of the previous reference
6781 frame. \\
6782 \bitvar{PREVREFCB} & \multicolumn{1}{p{50pt}}{2D Integer Array} &
6783 8 & No & A $\bitvar{RPCH}\times\bitvar{RPCW}$
6784 array containing the contents of the $C_b$ plane of the previous reference
6785 frame. \\
6786 \bitvar{PREVREFCR} & \multicolumn{1}{p{50pt}}{2D Integer Array} &
6787 8 & No & A $\bitvar{RPCH}\times\bitvar{RPCW}$
6788 array containing the contents of the $C_r$ plane of the previous reference
6789 frame. \\
6790 \bitvar{NBS} & Integer & 36 & No & The total number of blocks in a
6791 frame. \\
6792 \bitvar{BCODED} & \multicolumn{1}{p{40pt}}{Integer Array} &
6793 1 & No & An \bitvar{NBS}-element array of
6794 flags indicating which blocks are coded. \\
6795 \bitvar{MBMODES} & \multicolumn{1}{p{40pt}}{Integer Array} &
6796 3 & No & An \bitvar{NMBS}-element array of
6797 coding modes for each macro block. \\
6798 \bitvar{MVECTS} & \multicolumn{1}{p{50pt}}{Array of 2D Integer Vectors} &
6799 6 & Yes & An \bitvar{NBS}-element array of
6800 motion vectors for each block. \\
6801 \bitvar{COEFFS} & \multicolumn{1}{p{50pt}}{2D Integer Array} &
6802 16 & Yes & An $\bitvar{NBS}\times 64$ array of
6803 quantized DCT coefficient values for each block in zig-zag order. \\
6804 \bitvar{NCOEFFS} & \multicolumn{1}{p{40pt}}{Integer Array} &
6805 7 & No & An \bitvar{NBS}-element array of the
6806 coefficient count for each block. \\
6807 \bitvar{QIS} & \multicolumn{1}{p{40pt}}{Integer array} &
6808 6 & No & An \bitvar{NQIS}-element array of
6809 \qi\ values. \\
6810 \bitvar{QIIS} & \multicolumn{1}{p{40pt}}{Integer Array} &
6811 2 & No & An \bitvar{NBS}-element array of
6812 \locvar{\qii} values for each block. \\
6813 \bottomrule\end{tabularx}
6815 \paragraph{Output parameters:}\hfill\\*
6816 \begin{tabularx}{\textwidth}{@{}llrcX@{}}\toprule
6817 \multicolumn{1}{c}{Name} &
6818 \multicolumn{1}{c}{Type} &
6819 \multicolumn{1}{p{30pt}}{\centering Size (bits)} &
6820 \multicolumn{1}{c}{Signed?} &
6821 \multicolumn{1}{c}{Description and restrictions} \\\midrule\endhead
6822 \bitvar{RECY} & \multicolumn{1}{p{50pt}}{2D Integer Array} &
6823 8 & No & A $\bitvar{RPYH}\times\bitvar{RPYW}$
6824 array containing the contents of the $Y'$ plane of the reconstructed frame. \\
6825 \bitvar{RECCB} & \multicolumn{1}{p{50pt}}{2D Integer Array} &
6826 8 & No & A $\bitvar{RPCH}\times\bitvar{RPCW}$
6827 array containing the contents of the $C_b$ plane of the reconstructed frame. \\
6828 \bitvar{RECCR} & \multicolumn{1}{p{50pt}}{2D Integer Array} &
6829 8 & No & A $\bitvar{RPCH}\times\bitvar{RPCW}$
6830 array containing the contents of the $C_r$ plane of the reconstructed frame. \\
6831 \bottomrule\end{tabularx}
6833 \paragraph{Variables used:}\hfill\\*
6834 \begin{tabularx}{\textwidth}{@{}llrcX@{}}\toprule
6835 \multicolumn{1}{c}{Name} &
6836 \multicolumn{1}{c}{Type} &
6837 \multicolumn{1}{p{30pt}}{\centering Size (bits)} &
6838 \multicolumn{1}{c}{Signed?} &
6839 \multicolumn{1}{c}{Description and restrictions} \\\midrule\endhead
6840 \locvar{RPW} & Integer & 20 & No & The width of the current plane of the
6841 current reference frame in pixels. \\
6842 \locvar{RPH} & Integer & 20 & No & The height of the current plane of
6843 the current reference frame in pixels. \\
6844 \locvar{REFP} & \multicolumn{1}{p{50pt}}{2D Integer Array} &
6845 8 & No & A $\bitvar{RPH}\times\bitvar{RPW}$
6846 array containing the contents of the current plane of the current reference
6847 frame. \\
6848 \locvar{BX} & Integer & 20 & No & The horizontal pixel index of the
6849 lower-left corner of the current block. \\
6850 \locvar{BY} & Integer & 20 & No & The vertical pixel index of the
6851 lower-left corner of the current block. \\
6852 \locvar{MVX} & Integer & 5 & No & The horizontal component of the first
6853 whole-pixel motion vector. \\
6854 \locvar{MVY} & Integer & 5 & No & The vertical component of the first
6855 whole-pixel motion vector. \\
6856 \locvar{MVX2} & Integer & 5 & No & The horizontal component of the second
6857 whole-pixel motion vector. \\
6858 \locvar{MVY2} & Integer & 5 & No & The vertical component of the second
6859 whole-pixel motion vector. \\
6860 \locvar{PRED} & \multicolumn{1}{p{50pt}}{2D Integer Array} &
6861 8 & No & An $8\times 8$ array of predictor
6862 values to use for the current block. \\
6863 \locvar{RES} & \multicolumn{1}{p{50pt}}{2D Integer Array} &
6864 16 & Yes & An $8\times 8$ array containing the
6865 decoded residual for the current block. \\
6866 \locvar{QMAT} & \multicolumn{1}{p{40pt}}{Integer array} &
6867 16 & No & A 64-element array of quantization
6868 values for each DCT coefficient in natural order. \\
6869 \locvar{DC} & Integer & 29 & Yes & The dequantized DC coefficient of a
6870 block. \\
6871 \locvar{P} & Integer & 17 & Yes & A reconstructed pixel value. \\
6872 \locvar{\bi} & Integer & 36 & No & The index of the current block in
6873 coded order. \\
6874 \locvar{\mbi} & Integer & 32 & No & The index of the macro block
6875 containing block \locvar{\bi}. \\
6876 \locvar{\pli} & Integer & 2 & No & The color plane index of the current
6877 block. \\
6878 \locvar{\rfi} & Integer & 2 & No & The index of the reference frame
6879 indicated by the coding mode for macro block \locvar{\mbi}. \\
6880 \locvar{\idx{bx}} & Integer & 3 & No & The horizontal pixel index in the
6881 block. \\
6882 \locvar{\idx{by}} & Integer & 3 & No & The vertical pixel index in the
6883 block. \\
6884 \locvar{\qti} & Integer & 1 & No & A quantization type index.
6885 See Table~\ref{tab:quant-types}.\\
6886 \locvar{\idx{qi0}} & Integer & 6 & No & The quantization index of the DC
6887 coefficient. \\
6888 \locvar{\qi} & Integer & 6 & No & The quantization index of the AC
6889 coefficients. \\
6890 \bottomrule\end{tabularx}
6891 \medskip
6893 This section takes the decoded packet data and uses the previously defined
6894 procedures to reconstruct each block of the current frame.
6895 For coded blocks, a predictor is formed using the coding mode and, if
6896 applicable, the motion vector, and then the residual is computed from the
6897 quantized DCT coefficients.
6898 For uncoded blocks, the contents of the co-located block are copied from the
6899 previous frame and the residual is cleared to zero.
6900 Then the predictor and residual are added, and the result clamped to the range
6901 $0\ldots 255$ and stored in the current frame.
6903 In the special case that a block contains only a DC coefficient, the
6904 dequantization and inverse DCT transform is skipped.
6905 Instead the constant pixel value for the entire block is computed in one step.
6906 Note that the truncation of intermediate operations is omitted and the final
6907 rounding is slightly different in this case.
6908 The check for whether or not the block contains only a DC coefficient is based
6909 on the coefficient count returned from the token decode procedure of
6910 Section~\ref{sec:dct-decode}, and not by checking to see if the remaining
6911 coefficient values are zero.
6912 Also note that even when the coefficient count indicates the block contains
6913 zero coefficients, the DC coefficient is still processed, as undoing DC
6914 prediction might have made it non-zero.
6916 After this procedure, the frame is completely reconstructed, but before it can
6917 be used as a reference frame, a loop filter must be run over it to help reduce
6918 blocking artifacts.
6919 This is detailed in Section~\ref{sec:loopfilter}.
6921 \begin{enumerate}
6922 \item
6923 Assign \locvar{\idx{qi0}} the value $\bitvar{QIS}[0]$.
6924 \item
6925 For each value of \locvar{\bi} from 0 to $(\bitvar{NBS}-1)$:
6926 \begin{enumerate}
6927 \item
6928 Assign \locvar{\pli} the index of the color plane block \locvar{\bi} belongs
6930 \item
6931 Assign \locvar{BX} the horizontal pixel index of the lower-left corner of block
6932 \locvar{\bi}.
6933 \item
6934 Assign \locvar{BY} the vertical pixel index of the lower-left corner of block
6935 \locvar{\bi}.
6936 \item
6937 If $\bitvar{BCODED}[\locvar{\bi}]$ is non-zero:
6938 \begin{enumerate}
6939 \item
6940 Assign \locvar{\mbi} the index of the macro block containing block
6941 \locvar{\bi}.
6942 \item
6943 If $\bitvar{MBMODES}[\locvar{\mbi}]$ is 1 (INTRA), assign \locvar{\qti} the
6944 value $0$.
6945 \item
6946 Otherwise, assign \locvar{\qti} the value $1$.
6947 \item
6948 Assign \locvar{\rfi} the value of the Reference Frame Index column of
6949 Table~\ref{tab:cm-refs} corresponding to $\bitvar{MBMODES}[\locvar{\mbi}]$.
6950 \item
6951 If \locvar{\rfi} is zero, compute \locvar{PRED} using the procedure given in
6952 Section~\ref{sub:predintra}.
6953 \item
6954 Otherwise:
6955 \begin{enumerate}
6956 \item
6957 Assign \locvar{REFP}, \locvar{RPW}, and \locvar{RPH} the values given in
6958 Table~\ref{tab:refp} corresponding to current value of \locvar{\rfi} and
6959 \locvar{\pli}.
6961 \begin{table}[htbp]
6962 \begin{center}
6963 \begin{tabular}{cclll}\toprule
6964 \locvar{\rfi} & \locvar{\pli} &
6965 \locvar{REFP} & \locvar{RPW} & \locvar{RPH} \\\midrule
6966 $1$ & $0$ & \bitvar{PREVREFY} & \bitvar{RPYW} & \bitvar{RPYH} \\
6967 $1$ & $1$ & \bitvar{PREVREFCB} & \bitvar{RPCW} & \bitvar{RPCH} \\
6968 $1$ & $2$ & \bitvar{PREVREFCR} & \bitvar{RPCW} & \bitvar{RPCH} \\
6969 $2$ & $0$ & \bitvar{GOLDREFY} & \bitvar{RPYW} & \bitvar{RPYH} \\
6970 $2$ & $1$ & \bitvar{GOLDREFCB} & \bitvar{RPCW} & \bitvar{RPCH} \\
6971 $2$ & $2$ & \bitvar{GOLDREFCR} & \bitvar{RPCW} & \bitvar{RPCH} \\
6972 \bottomrule\end{tabular}
6973 \end{center}
6974 \caption{Reference Planes and Sizes for Each \locvar{\rfi} and \locvar{\pli}}
6975 \label{tab:refp}
6976 \end{table}
6978 \item
6979 Assign \locvar{MVX} the value
6980 \begin{equation*}
6981 \left\lfloor\lvert\bitvar{MVECTS}[\locvar{\bi}]_x\rvert\right\rfloor*
6982 \sign(\bitvar{MVECTS}[\locvar{\bi}]_x).
6983 \end{equation*}
6984 \item
6985 Assign \locvar{MVY} the value
6986 \begin{equation*}
6987 \left\lfloor\lvert\bitvar{MVECTS}[\locvar{\bi}]_y\rvert\right\rfloor*
6988 \sign(\bitvar{MVECTS}[\locvar{\bi}]_y).
6989 \end{equation*}
6990 \item
6991 Assign \locvar{MVX2} the value
6992 \begin{equation*}
6993 \left\lceil\lvert\bitvar{MVECTS}[\locvar{\bi}]_x\rvert\right\rceil*
6994 \sign(\bitvar{MVECTS}[\locvar{\bi}]_x).
6995 \end{equation*}
6996 \item
6997 Assign \locvar{MVY2} the value
6998 \begin{equation*}
6999 \left\lceil\lvert\bitvar{MVECTS}[\locvar{\bi}]_y\rvert\right\rceil*
7000 \sign(\bitvar{MVECTS}[\locvar{\bi}]_y).
7001 \end{equation*}
7002 \item
7003 If \locvar{MVX} equals \locvar{MVX2} and \locvar{MVY} equals \locvar{MVY2},
7004 use the values \locvar{REFP}, \locvar{RPW}, \locvar{RPH}, \locvar{BX},
7005 \locvar{BY}, \locvar{MVX}, and \locvar{MVY}, compute \locvar{PRED} using the
7006 procedure given in Section~\ref{sub:predfullpel}.
7007 \item
7008 Otherwise, use the values \locvar{REFP}, \locvar{RPW}, \locvar{RPH},
7009 \locvar{BX}, \locvar{BY}, \locvar{MVX}, \locvar{MVY}, \locvar{MVX2}, and
7010 \locvar{MVY2} to compute \locvar{PRED} using the procedure given in
7011 Section~\ref{sub:predhalfpel}.
7012 \end{enumerate}
7013 \item
7014 If $\bitvar{NCOEFFS}[\locvar{\bi}]$ is less than 2:
7015 \begin{enumerate}
7016 \item
7017 Using \bitvar{ACSCALE}, \bitvar{DCSCALE}, \bitvar{BMS}, \bitvar{NQRS}, \\
7018 \bitvar{QRSIZES}, \bitvar{QRBMIS}, \locvar{\qti}, \locvar{\pli}, and
7019 \locvar{\idx{qi0}}, use the procedure given in Section~\ref{sub:quant-mat} to
7020 compute the DC quantization matrix \locvar{QMAT}.
7021 \item
7022 Assign \locvar{DC} the value
7023 \begin{equation*}
7024 (\bitvar{COEFFS}[\bitvar{\bi}][0]*\locvar{QMAT}[0]+15)>>5.
7025 \end{equation*}
7026 \item
7027 Truncate \locvar{DC} to a 16-bit signed representation by dropping any
7028 higher-order bits.
7029 \item
7030 For each value of \locvar{\idx{by}} from 0 to 7, and each value of
7031 \locvar{\idx{bx}} from 0 to 7, assign
7032 $\locvar{RES}[\locvar{\idx{by}}][\locvar{\idx{bx}}]$ the value \locvar{DC}.
7033 \end{enumerate}
7034 \item
7035 Otherwise:
7036 \begin{enumerate}
7037 \item
7038 Assign \locvar{\qi} the value $\bitvar{QIS}[\bitvar{QIIS}[\locvar{\bi}]]$.
7039 \item
7040 Using \bitvar{ACSCALE}, \bitvar{DCSCALE}, \bitvar{BMS}, \bitvar{NQRS}, \\
7041 \bitvar{QRSIZES}, \bitvar{QRBMIS}, \locvar{\qti}, \locvar{\pli},
7042 \locvar{\idx{qi0}}, and \locvar{\qi}, compute \locvar{DQC} using the procedure
7043 given in Section~\ref{sub:dequant}.
7044 \item
7045 Using \locvar{DQC}, compute \locvar{RES} using the procedure given in
7046 Section~\ref{sub:2d-idct}.
7047 \end{enumerate}
7048 \end{enumerate}
7049 \item
7050 Otherwise:
7051 \begin{enumerate}
7052 \item
7053 Assign \locvar{\rfi} the value 1.
7054 \item
7055 Assign \locvar{REFP}, \locvar{RPW}, and \locvar{RPH} the values given in
7056 Table~\ref{tab:refp} corresponding to current value of \locvar{\rfi} and
7057 \locvar{\pli}.
7058 \item
7059 Assign \locvar{MVX} the value 0.
7060 \item
7061 Assign \locvar{MVY} the value 0.
7062 \item
7063 Using the values \locvar{REFP}, \locvar{RPW}, \locvar{RPH}, \locvar{BX},
7064 \locvar{BY}, \locvar{MVX}, and \locvar{MVY}, compute \locvar{PRED} using the
7065 procedure given in Section~\ref{sub:predfullpel}.
7066 This is simply a copy of the co-located block in the previous reference frame.
7067 \item
7068 For each value of \locvar{\idx{by}} from 0 to 7, and each value of
7069 \locvar{\idx{bx}} from 0 to 7, assign
7070 $\locvar{RES}[\locvar{\idx{by}}][\locvar{\idx{bx}}]$ the value 0.
7071 \end{enumerate}
7072 \item
7073 For each value of \locvar{\idx{by}} from 0 to 7, and each value of
7074 \locvar{\idx{bx}} from 0 to 7:
7075 \begin{enumerate}
7076 \item
7077 Assign \locvar{P} the value
7078 $(\locvar{PRED}[\locvar{\idx{by}}][\locvar{\idx{bx}}]+
7079 \locvar{RES}[\locvar{\idx{by}}][\locvar{\idx{bx}}])$.
7080 \item
7081 If \locvar{P} is greater than $255$, assign \locvar{P} the value $255$.
7082 \item
7083 If \locvar{P} is less than $0$, assign \locvar{P} the value $0$.
7084 \item
7085 If \locvar{\pli} equals 0, assign
7086 $\bitvar{RECY}[\locvar{BY}+\locvar{\idx{by}}][\locvar{BX}+\locvar{\idx{bx}}]$
7087 the value \locvar{P}.
7088 \item
7089 Otherwise, if \locvar{\pli} equals 1, assign
7090 $\bitvar{RECB}[\locvar{BY}+\locvar{\idx{by}}][\locvar{BX}+\locvar{\idx{bx}}]$
7091 the value \locvar{P}.
7092 \item
7093 Otherwise, \locvar{\pli} equals 2, so assign
7094 $\bitvar{RECR}[\locvar{BY}+\locvar{\idx{by}}][\locvar{BX}+\locvar{\idx{bx}}]$
7095 the value \locvar{P}.
7096 \end{enumerate}
7097 \end{enumerate}
7098 \end{enumerate}
7100 \section{Loop Filtering}
7101 \label{sec:loopfilter}
7103 The loop filter is a simple deblocking filter that is based on running a small
7104 edge detecting filter over the coded block edges and adjusting the pixel
7105 values by a tapered response.
7106 The filter response is modulated by the following non-linear function:
7107 \begin{align*}
7108 \lflim(\locvar{R},\bitvar{L})&=\left\{\begin{array}{ll}
7109 0, & \locvar{R}\le-2*\bitvar{L} \\
7110 -\locvar{R}-2*\bitvar{L}, & -2*\bitvar{L}<\locvar{R}\le-\bitvar{L} \\
7111 \locvar{R}, & -\bitvar{L}<\locvar{R}<\bitvar{L} \\
7112 -\locvar{R}+2*\bitvar{L}, & \bitvar{L}\le\locvar{R}<2*\bitvar{L} \\
7113 0, & 2*\bitvar{L}\le\locvar{R}
7114 \end{array}\right.
7115 \end{align*}
7116 Here \bitvar{L} is a limiting value equal to $\bitvar{LFLIMS}[\idx{qi0}]$.
7117 It defines the peaks of the function.
7118 \bitvar{LFLIMS} is an array of values specified in the setup header and is
7119 indexed by \idx{qi0}, the first quantization index for the frame, the one used
7120 for all the DC coefficients.
7121 Larger values of \bitvar{L} indicate a stronger filter.
7123 \subsection{Horizontal Filter}
7124 \label{sub:filth}
7126 \paragraph{Input parameters:}\hfill\\*
7127 \begin{tabularx}{\textwidth}{@{}llrcX@{}}\toprule
7128 \multicolumn{1}{c}{Name} &
7129 \multicolumn{1}{c}{Type} &
7130 \multicolumn{1}{p{30pt}}{\centering Size (bits)} &
7131 \multicolumn{1}{c}{Signed?} &
7132 \multicolumn{1}{c}{Description and restrictions} \\\midrule\endhead
7133 \bitvar{RECP} & \multicolumn{1}{p{50pt}}{2D Integer Array} &
7134 8 & No & A $\bitvar{RPH}\times\bitvar{RPW}$
7135 array containing the contents of a plane of the reconstructed frame. \\
7136 \bitvar{FX} & Integer & 20 & No & The horizontal pixel index of the
7137 lower-left corner of the area to be filtered. \\
7138 \bitvar{FY} & Integer & 20 & No & The vertical pixel index of the
7139 lower-left corner of the area to be filtered. \\
7140 \bitvar{L} & Integer & 7 & No & The loop filter limit value. \\
7141 \bottomrule\end{tabularx}
7143 \paragraph{Output parameters:}\hfill\\*
7144 \begin{tabularx}{\textwidth}{@{}llrcX@{}}\toprule
7145 \multicolumn{1}{c}{Name} &
7146 \multicolumn{1}{c}{Type} &
7147 \multicolumn{1}{p{30pt}}{\centering Size (bits)} &
7148 \multicolumn{1}{c}{Signed?} &
7149 \multicolumn{1}{c}{Description and restrictions} \\\midrule\endhead
7150 \bitvar{RECP} & \multicolumn{1}{p{50pt}}{2D Integer Array} &
7151 8 & No & A $\bitvar{RPH}\times\bitvar{RPW}$
7152 array containing the contents of a plane of the reconstructed frame. \\
7153 \bottomrule\end{tabularx}
7155 \paragraph{Variables used:}\hfill\\*
7156 \begin{tabularx}{\textwidth}{@{}llrcX@{}}\toprule
7157 \multicolumn{1}{c}{Name} &
7158 \multicolumn{1}{c}{Type} &
7159 \multicolumn{1}{p{30pt}}{\centering Size (bits)} &
7160 \multicolumn{1}{c}{Signed?} &
7161 \multicolumn{1}{c}{Description and restrictions} \\\midrule\endhead
7162 \locvar{R} & Integer & 9 & Yes & The edge detector response. \\
7163 \locvar{P} & Integer & 9 & Yes & A filtered pixel value. \\
7164 \locvar{\idx{by}} & Integer & 20 & No & The vertical pixel index in the
7165 block. \\
7166 \bottomrule\end{tabularx}
7167 \medskip
7169 This procedure applies a $4$-tap horizontal filter to each row of a vertical
7170 block edge.
7172 \begin{enumerate}
7173 \item
7174 For each value of \locvar{\idx{by}} from $0$ to $7$:
7175 \begin{enumerate}
7176 \item
7177 Assign \locvar{R} the value
7178 \begin{multline*}
7179 (\bitvar{RECP}[\bitvar{FY}+\locvar{\idx{by}}][\bitvar{FX}]-
7180 3*\bitvar{RECP}[\bitvar{FY}+\locvar{\idx{by}}][\bitvar{FX}+1]+\\
7181 3*\bitvar{RECP}[\bitvar{FY}+\locvar{\idx{by}}][\bitvar{FX}+2]-
7182 \bitvar{RECP}[\bitvar{FY}+\locvar{\idx{by}}][\bitvar{FX}+3]+4)>>3
7183 \end{multline*}
7184 \item
7185 Assign \locvar{P} the value
7186 $(\bitvar{RECP}[\bitvar{FY}+\locvar{\idx{by}}][\bitvar{FX}+1]+
7187 \lflim(\locvar{R},\bitvar{L}))$.
7188 \item
7189 If \locvar{P} is less than zero, assign
7190 $\bitvar{RECP}[\bitvar{FY}+\locvar{\idx{by}}][\bitvar{FX}+1]$ the value zero.
7191 \item
7192 Otherwise, if \locvar{P} is greater than $255$, assign
7193 $\bitvar{RECP}[\bitvar{FY}+\locvar{\idx{by}}][\bitvar{FX}+1]$ the value $255$.
7194 \item
7195 Otherwise, assign
7196 $\bitvar{RECP}[\bitvar{FY}+\locvar{\idx{by}}][\bitvar{FX}+1]$ the value
7197 \locvar{P}.
7198 \item
7199 Assign \locvar{P} the value
7200 $(\bitvar{RECP}[\bitvar{FY}+\locvar{\idx{by}}][\bitvar{FX}+2]-
7201 \lflim(\locvar{R},\bitvar{L}))$.
7202 \item
7203 If \locvar{P} is less than zero, assign
7204 $\bitvar{RECP}[\bitvar{FY}+\locvar{\idx{by}}][\bitvar{FX}+2]$ the value zero.
7205 \item
7206 Otherwise, if \locvar{P} is greater than $255$, assign
7207 $\bitvar{RECP}[\bitvar{FY}+\locvar{\idx{by}}][\bitvar{FX}+2]$ the value $255$.
7208 \item
7209 Otherwise, assign
7210 $\bitvar{RECP}[\bitvar{FY}+\locvar{\idx{by}}][\bitvar{FX}+2]$ the value
7211 \locvar{P}.
7212 \end{enumerate}
7213 \end{enumerate}
7215 \subsection{Vertical Filter}
7216 \label{sub:filtv}
7218 \paragraph{Input parameters:}\hfill\\*
7219 \begin{tabularx}{\textwidth}{@{}llrcX@{}}\toprule
7220 \multicolumn{1}{c}{Name} &
7221 \multicolumn{1}{c}{Type} &
7222 \multicolumn{1}{p{30pt}}{\centering Size (bits)} &
7223 \multicolumn{1}{c}{Signed?} &
7224 \multicolumn{1}{c}{Description and restrictions} \\\midrule\endhead
7225 \bitvar{RECP} & \multicolumn{1}{p{50pt}}{2D Integer Array} &
7226 8 & No & A $\bitvar{RPH}\times\bitvar{RPW}$
7227 array containing the contents of a plane of the reconstructed frame. \\
7228 \bitvar{FX} & Integer & 20 & No & The horizontal pixel index of the
7229 lower-left corner of the area to be filtered. \\
7230 \bitvar{FY} & Integer & 20 & No & The vertical pixel index of the
7231 lower-left corner of the area to be filtered. \\
7232 \bitvar{L} & Integer & 7 & No & The loop filter limit value. \\
7233 \bottomrule\end{tabularx}
7235 \paragraph{Output parameters:}\hfill\\*
7236 \begin{tabularx}{\textwidth}{@{}llrcX@{}}\toprule
7237 \multicolumn{1}{c}{Name} &
7238 \multicolumn{1}{c}{Type} &
7239 \multicolumn{1}{p{30pt}}{\centering Size (bits)} &
7240 \multicolumn{1}{c}{Signed?} &
7241 \multicolumn{1}{c}{Description and restrictions} \\\midrule\endhead
7242 \bitvar{RECP} & \multicolumn{1}{p{50pt}}{2D Integer Array} &
7243 8 & No & A $\bitvar{RPH}\times\bitvar{RPW}$
7244 array containing the contents of a plane of the reconstructed frame. \\
7245 \bottomrule\end{tabularx}
7247 \paragraph{Variables used:}\hfill\\*
7248 \begin{tabularx}{\textwidth}{@{}llrcX@{}}\toprule
7249 \multicolumn{1}{c}{Name} &
7250 \multicolumn{1}{c}{Type} &
7251 \multicolumn{1}{p{30pt}}{\centering Size (bits)} &
7252 \multicolumn{1}{c}{Signed?} &
7253 \multicolumn{1}{c}{Description and restrictions} \\\midrule\endhead
7254 \locvar{R} & Integer & 9 & Yes & The edge detector response. \\
7255 \locvar{P} & Integer & 9 & Yes & A filtered pixel value. \\
7256 \locvar{\idx{bx}} & Integer & 20 & No & The horizontal pixel index in the
7257 block. \\
7258 \bottomrule\end{tabularx}
7259 \medskip
7261 This procedure applies a $4$-tap vertical filter to each column of a horizontal
7262 block edge.
7264 \begin{enumerate}
7265 \item
7266 For each value of \locvar{\idx{bx}} from $0$ to $7$:
7267 \begin{enumerate}
7268 \item
7269 Assign \locvar{R} the value
7270 \begin{multline*}
7271 (\bitvar{RECP}[\bitvar{FY}][\bitvar{FX}+\locvar{\idx{bx}}]-
7272 3*\bitvar{RECP}[\bitvar{FY}+1][\bitvar{FX}+\locvar{\idx{bx}}]+\\
7273 3*\bitvar{RECP}[\bitvar{FY}+2][\bitvar{FX}+\locvar{\idx{bx}}]-
7274 \bitvar{RECP}[\bitvar{FY}+3][\bitvar{FX}+\locvar{\idx{bx}}]+4)>>3
7275 \end{multline*}
7276 \item
7277 Assign \locvar{P} the value
7278 $(\bitvar{RECP}[\bitvar{FY}+1][\bitvar{FX}+\locvar{\idx{bx}}]+
7279 \lflim(\locvar{R},\bitvar{L}))$.
7280 \item
7281 If \locvar{P} is less than zero, assign
7282 $\bitvar{RECP}[\bitvar{FY}+1][\bitvar{FX}+\locvar{\idx{bx}}]$ the value zero.
7283 \item
7284 Otherwise, if \locvar{P} is greater than $255$, assign
7285 $\bitvar{RECP}[\bitvar{FY}+1][\bitvar{FX}+\locvar{\idx{bx}}]$ the value $255$.
7286 \item
7287 Otherwise, assign
7288 $\bitvar{RECP}[\bitvar{FY}+1][\bitvar{FX}+\locvar{\idx{bx}}]$ the value
7289 \locvar{P}.
7290 \item
7291 Assign \locvar{P} the value
7292 $(\bitvar{RECP}[\bitvar{FY}+2][\bitvar{FX}+\locvar{\idx{bx}}]-
7293 \lflim(\locvar{R},\bitvar{L}))$.
7294 \item
7295 If \locvar{P} is less than zero, assign
7296 $\bitvar{RECP}[\bitvar{FY}+2][\bitvar{FX}+\locvar{\idx{bx}}]$ the value zero.
7297 \item
7298 Otherwise, if \locvar{P} is greater than $255$, assign
7299 $\bitvar{RECP}[\bitvar{FY}+2][\bitvar{FX}+\locvar{\idx{bx}}]$ the value $255$.
7300 \item
7301 Otherwise, assign
7302 $\bitvar{RECP}[\bitvar{FY}+2][\bitvar{FX}+\locvar{\idx{bx}}]$ the value
7303 \locvar{P}.
7304 \end{enumerate}
7305 \end{enumerate}
7307 \subsection{Complete Loop Filter}
7308 \label{sub:loop-filt}
7310 \paragraph{Input parameters:}\hfill\\*
7311 \begin{tabularx}{\textwidth}{@{}llrcX@{}}\toprule
7312 \multicolumn{1}{c}{Name} &
7313 \multicolumn{1}{c}{Type} &
7314 \multicolumn{1}{p{30pt}}{\centering Size (bits)} &
7315 \multicolumn{1}{c}{Signed?} &
7316 \multicolumn{1}{c}{Description and restrictions} \\\midrule\endhead
7317 \bitvar{LFLIMS} & \multicolumn{1}{p{40pt}}{Integer array} &
7318 7 & No & A 64-element array of loop filter limit
7319 values. \\
7320 \bitvar{RPYW} & Integer & 20 & No & The width of the $Y'$ plane of the
7321 reconstruced frame in pixels. \\
7322 \bitvar{RPYH} & Integer & 20 & No & The height of the $Y'$ plane of the
7323 reconstruced frame in pixels. \\
7324 \bitvar{RPCW} & Integer & 20 & No & The width of the $C_b$ and $C_r$
7325 planes of the reconstruced frame in pixels. \\
7326 \bitvar{RPCH} & Integer & 20 & No & The height of the $C_b$ and $C_r$
7327 planes of the reconstruced frame in pixels. \\
7328 \bitvar{NBS} & Integer & 36 & No & The total number of blocks in a
7329 frame. \\
7330 \bitvar{BCODED} & \multicolumn{1}{p{40pt}}{Integer Array} &
7331 1 & No & An \bitvar{NBS}-element array of
7332 flags indicating which blocks are coded. \\
7333 \bitvar{QIS} & \multicolumn{1}{p{40pt}}{Integer array} &
7334 6 & No & An \bitvar{NQIS}-element array of
7335 \qi\ values. \\
7336 \bitvar{RECY} & \multicolumn{1}{p{50pt}}{2D Integer Array} &
7337 8 & No & A $\bitvar{RPYH}\times\bitvar{RPYW}$
7338 array containing the contents of the $Y'$ plane of the reconstructed frame. \\
7339 \bitvar{RECCB} & \multicolumn{1}{p{50pt}}{2D Integer Array} &
7340 8 & No & A $\bitvar{RPCH}\times\bitvar{RPCW}$
7341 array containing the contents of the $C_b$ plane of the reconstructed frame. \\
7342 \bitvar{RECCR} & \multicolumn{1}{p{50pt}}{2D Integer Array} &
7343 8 & No & A $\bitvar{RPCH}\times\bitvar{RPCW}$
7344 array containing the contents of the $C_r$ plane of the reconstructed frame. \\
7345 \bottomrule\end{tabularx}
7347 \paragraph{Output parameters:}\hfill\\*
7348 \begin{tabularx}{\textwidth}{@{}llrcX@{}}\toprule
7349 \multicolumn{1}{c}{Name} &
7350 \multicolumn{1}{c}{Type} &
7351 \multicolumn{1}{p{30pt}}{\centering Size (bits)} &
7352 \multicolumn{1}{c}{Signed?} &
7353 \multicolumn{1}{c}{Description and restrictions} \\\midrule\endhead
7354 \bitvar{RECY} & \multicolumn{1}{p{50pt}}{2D Integer Array} &
7355 8 & No & A $\bitvar{RPYH}\times\bitvar{RPYW}$
7356 array containing the contents of the $Y'$ plane of the reconstructed frame. \\
7357 \bitvar{RECCB} & \multicolumn{1}{p{50pt}}{2D Integer Array} &
7358 8 & No & A $\bitvar{RPCH}\times\bitvar{RPCW}$
7359 array containing the contents of the $C_b$ plane of the reconstructed frame. \\
7360 \bitvar{RECCR} & \multicolumn{1}{p{50pt}}{2D Integer Array} &
7361 8 & No & A $\bitvar{RPCH}\times\bitvar{RPCW}$
7362 array containing the contents of the $C_r$ plane of the reconstructed frame. \\
7363 \bottomrule\end{tabularx}
7365 \paragraph{Variables used:}\hfill\\*
7366 \begin{tabularx}{\textwidth}{@{}llrcX@{}}\toprule
7367 \multicolumn{1}{c}{Name} &
7368 \multicolumn{1}{c}{Type} &
7369 \multicolumn{1}{p{30pt}}{\centering Size (bits)} &
7370 \multicolumn{1}{c}{Signed?} &
7371 \multicolumn{1}{c}{Description and restrictions} \\\midrule\endhead
7372 \locvar{RPW} & Integer & 20 & No & The width of the current plane of the
7373 reconstructed frame in pixels. \\
7374 \locvar{RPH} & Integer & 20 & No & The height of the current plane of
7375 the reconstructed frame in pixels. \\
7376 \locvar{RECP} & \multicolumn{1}{p{50pt}}{2D Integer Array} &
7377 8 & No & A $\bitvar{RPH}\times\bitvar{RPW}$
7378 array containing the contents of the current plane of the reconstruced
7379 frame. \\
7380 \locvar{BX} & Integer & 20 & No & The horizontal pixel index of the
7381 lower-left corner of the current block. \\
7382 \locvar{BY} & Integer & 20 & No & The vertical pixel index of the
7383 lower-left corner of the current block. \\
7384 \locvar{FX} & Integer & 20 & No & The horizontal pixel index of the
7385 lower-left corner of the area to be filtered. \\
7386 \locvar{FY} & Integer & 20 & No & The vertical pixel index of the
7387 lower-left corner of the area to be filtered. \\
7388 \locvar{L} & Integer & 7 & No & The loop filter limit value. \\
7389 \locvar{\bi} & Integer & 36 & No & The index of the current block in
7390 coded order. \\
7391 \locvar{\bj} & Integer & 36 & No & The index of a neighboring block in
7392 coded order. \\
7393 \locvar{\pli} & Integer & 2 & No & The color plane index of the current
7394 block. \\
7395 \bottomrule\end{tabularx}
7396 \medskip
7398 This procedure defines the order that the various block edges are filtered.
7399 Because each application of one of the two filters above destructively modifies
7400 the contents of the reconstructed image, the precise output obtained differs
7401 depending on the order that horizontal and vertical filters are applied to the
7402 edges of a single block.
7403 The order defined here conforms to that used by VP3.
7405 \begin{enumerate}
7406 \item
7407 Assign \locvar{L} the value $\bitvar{LFLIMS}[\bitvar{QIS}[0]]$.
7408 \item
7409 For each block in {\em raster} order, with coded-order index \locvar{\bi}:
7410 \begin{enumerate}
7411 \item
7412 If $\bitvar{BCODED}[\locvar{\bi}]$ is non-zero:
7413 \begin{enumerate}
7414 \item
7415 Assign \locvar{\pli} the index of the color plane block \locvar{\bi} belongs
7417 \item
7418 Assign \locvar{RECP}, \locvar{RPW}, and \locvar{RPH} the values given in
7419 Table~\ref{tab:recp} corresponding to the value of \locvar{\pli}.
7421 \begin{table}[htbp]
7422 \begin{center}
7423 \begin{tabular}{clll}\toprule
7424 \locvar{\pli} & \locvar{RECP} & \locvar{RPW} & \locvar{RPH} \\\midrule
7425 $0$ & \bitvar{RECY} & \bitvar{RPYW} & \bitvar{RPYH} \\
7426 $1$ & \bitvar{RECCB} & \bitvar{RPCW} & \bitvar{RPCH} \\
7427 $2$ & \bitvar{RECCR} & \bitvar{RPCW} & \bitvar{RPCH} \\
7428 \bottomrule\end{tabular}
7429 \end{center}
7430 \caption{Reconstructed Planes and Sizes for Each \locvar{\pli}}
7431 \label{tab:recp}
7432 \end{table}
7434 \item
7435 Assign \locvar{BX} the horizontal pixel index of the lower-left corner of the
7436 block \locvar{\bi}.
7437 \item
7438 Assign \locvar{BY} the vertical pixel index of the lower-left corner of the
7439 block \locvar{\bi}.
7440 \item
7441 If \locvar{BX} is greater than zero:
7442 \begin{enumerate}
7443 \item
7444 Assign \locvar{FX} the value $(\locvar{BX}-2)$.
7445 \item
7446 Assign \locvar{FY} the value \locvar{BY}.
7447 \item
7448 Using \locvar{RECP}, \locvar{FX}, \locvar{FY}, and \locvar{L}, apply the
7449 horizontal block filter to the left edge of block \locvar{\bi} with the
7450 procedure described in Section~\ref{sub:filth}.
7451 \end{enumerate}
7452 \item
7453 If \locvar{BY} is greater than zero:
7454 \begin{enumerate}
7455 \item
7456 Assign \locvar{FX} the value \locvar{BX}.
7457 \item
7458 Assign \locvar{FY} the value $(\locvar{BY}-2)$
7459 \item
7460 Using \locvar{RECP}, \locvar{FX}, \locvar{FY}, and \locvar{L}, apply the
7461 vertical block filter to the bottom edge of block \locvar{\bi} with the
7462 procedure described in Section~\ref{sub:filtv}.
7463 \end{enumerate}
7464 \item
7465 If $(\locvar{BX}+8)$ is less than \locvar{RPW} and
7466 $\bitvar{BCODED}[\locvar{\bj}]$ is zero, where \locvar{\bj} is the coded-order
7467 index of the block adjacent to \locvar{\bi} on the right:
7468 \begin{enumerate}
7469 \item
7470 Assign \locvar{FX} the value $(\locvar{BX}+6)$.
7471 \item
7472 Assign \locvar{FY} the value \locvar{BY}.
7473 \item
7474 Using \locvar{RECP}, \locvar{FX}, \locvar{FY}, and \locvar{L}, apply the
7475 horizontal block filter to the right edge of block \locvar{\bi} with the
7476 procedure described in Section~\ref{sub:filth}.
7477 \end{enumerate}
7478 \item
7479 If $(\locvar{BY}+8)$ is less than \locvar{RPH} and
7480 $\bitvar{BCODED}[\locvar{\bj}]$ is zero, where \locvar{\bj} is the coded-order
7481 index of the block adjacent to \locvar{\bi} above:
7482 \begin{enumerate}
7483 \item
7484 Assign \locvar{FX} the value \locvar{BX}.
7485 \item
7486 Assign \locvar{FY} the value $(\locvar{BY}+6)$
7487 \item
7488 Using \locvar{RECP}, \locvar{FX}, \locvar{FY}, and \locvar{L}, apply the
7489 vertical block filter to the top edge of block \locvar{\bi} with the
7490 procedure described in Section~\ref{sub:filtv}.
7491 \end{enumerate}
7492 \end{enumerate}
7493 \end{enumerate}
7494 \end{enumerate}
7496 \paragraph{VP3 Compatibility}
7498 The original VP3 decoder implemented unrestricted motion vectors by enlarging
7499 the reconstructed frame buffers and repeating the pixels on its edges into the
7500 padding region.
7501 However, for the previous reference frame this padding ocurred before the loop
7502 filter was applied, but for the golden reference frame it occurred afterwards.
7504 This means that for the previous reference frame, the padding values were
7505 required to be stored separately from the main image values.
7506 Furthermore, even if the previous and golden reference frames were in fact the
7507 same frame, they could have different padding values.
7508 Finally, the encoder did not apply the loop filter at all, which resulted in
7509 artifacts, particularly in near-static scenes, due to prediction-loop
7510 mismatch.
7511 This last can only be considered a bug in the VP3 encoder.
7513 Given all these things, Theora now uniformly applies the loop filter before
7514 the reference frames are padded.
7515 This means it is possible to use the same buffer for the previous and golden
7516 reference frames when they do indeed refer to the same frame.
7517 It also means that on architectures where memory bandwidth is limited, it is
7518 possible to avoid storing padding values, and simply clamp the motion vectors
7519 applied to each pixel as described in Sections~\ref{sub:predfullpel}
7520 and~\ref{sub:predhalfpel}.
7521 This means that the predicted pixel values along the edges of the frame might
7522 differ slightly between VP3 and Theora, but since the VP3 encoder did not
7523 apply the loop filter in the first place, this is not likely to impose any
7524 serious compatibility issues.
7526 \section{Complete Frame Decode}
7528 \paragraph{Input parameters:}\hfill\\*
7529 \begin{tabularx}{\textwidth}{@{}llrcX@{}}\toprule
7530 \multicolumn{1}{c}{Name} &
7531 \multicolumn{1}{c}{Type} &
7532 \multicolumn{1}{p{30pt}}{\centering Size (bits)} &
7533 \multicolumn{1}{c}{Signed?} &
7534 \multicolumn{1}{c}{Description and restrictions} \\\midrule\endhead
7535 \bitvar{FMBW} & Integer & 16 & No & The width of the frame in macro
7536 blocks. \\
7537 \bitvar{FMBH} & Integer & 16 & No & The height of the frame in macro
7538 blocks. \\
7539 \bitvar{NSBS} & Integer & 32 & No & The total number of super blocks in a
7540 frame. \\
7541 \bitvar{NBS} & Integer & 36 & No & The total number of blocks in a
7542 frame. \\
7543 \bitvar{NMBS} & Integer & 32 & No & The total number of macro blocks in a
7544 frame. \\
7545 \bitvar{FRN} & Integer & 32 & No & The frame-rate numerator. \\
7546 \bitvar{FRD} & Integer & 32 & No & The frame-rate denominator. \\
7547 \bitvar{PARN} & Integer & 24 & No & The pixel aspect-ratio numerator. \\
7548 \bitvar{PARD} & Integer & 24 & No & The pixel aspect-ratio
7549 denominator. \\
7550 \bitvar{CS} & Integer & 8 & No & The color space. \\
7551 \bitvar{PF} & Integer & 2 & No & The pixel format. \\
7552 \bitvar{NOMBR} & Integer & 24 & No & The nominal bitrate of the stream, in
7553 bits per second. \\
7554 \bitvar{QUAL} & Integer & 6 & No & The quality hint. \\
7555 \bitvar{KFGSHIFT} & Integer & 5 & No & The amount to shift the key frame
7556 number by in the granule position. \\
7557 \bitvar{LFLIMS} & \multicolumn{1}{p{40pt}}{Integer array} &
7558 7 & No & A 64-element array of loop filter
7559 limit values. \\
7560 \bitvar{ACSCALE} & \multicolumn{1}{p{40pt}}{Integer array} &
7561 16 & No & A 64-element array of scale values
7562 for AC coefficients for each \qi\ value. \\
7563 \bitvar{DCSCALE} & \multicolumn{1}{p{40pt}}{Integer array} &
7564 16 & No & A 64-element array of scale values
7565 for the DC coefficient for each \qi\ value. \\
7566 \bitvar{NBMS} & Integer & 10 & No & The number of base matrices. \\
7567 \bitvar{BMS} & \multicolumn{1}{p{50pt}}{2D Integer array} &
7568 8 & No & A $\bitvar{NBMS}\times 64$ array
7569 containing the base matrices. \\
7570 \bitvar{NQRS} & \multicolumn{1}{p{50pt}}{2D Integer array} &
7571 6 & No & A $2\times 3$ array containing the
7572 number of quant ranges for a given \qti\ and \pli, respectively.
7573 This is at most $63$. \\
7574 \bitvar{QRSIZES} & \multicolumn{1}{p{50pt}}{3D Integer array} &
7575 6 & No & A $2\times 3\times 63$ array of the
7576 sizes of each quant range for a given \qti\ and \pli, respectively.
7577 Only the first $\bitvar{NQRS}[\qti][\pli]$ values will be used. \\
7578 \bitvar{QRBMIS} & \multicolumn{1}{p{50pt}}{3D Integer array} &
7579 9 & No & A $2\times 3\times 64$ array of the
7580 \bmi's used for each quant range for a given \qti\ and \pli, respectively.
7581 Only the first $(\bitvar{NQRS}[\qti][\pli]+1)$ values will be used. \\
7582 \bitvar{HTS} & \multicolumn{3}{l}{Huffman table array}
7583 & An 80-element array of Huffman tables
7584 with up to 32 entries each. \\
7585 \bitvar{GOLDREFY} & \multicolumn{1}{p{50pt}}{2D Integer Array} &
7586 8 & No & A $\bitvar{RPYH}\times\bitvar{RPYW}$
7587 array containing the contents of the $Y'$ plane of the golden reference
7588 frame. \\
7589 \bitvar{GOLDREFCB} & \multicolumn{1}{p{50pt}}{2D Integer Array} &
7590 8 & No & A $\bitvar{RPCH}\times\bitvar{RPCW}$
7591 array containing the contents of the $C_b$ plane of the golden reference
7592 frame. \\
7593 \bitvar{GOLDREFCR} & \multicolumn{1}{p{50pt}}{2D Integer Array} &
7594 8 & No & A $\bitvar{RPCH}\times\bitvar{RPCW}$
7595 array containing the contents of the $C_r$ plane of the golden reference
7596 frame. \\
7597 \bitvar{PREVREFY} & \multicolumn{1}{p{50pt}}{2D Integer Array} &
7598 8 & No & A $\bitvar{RPYH}\times\bitvar{RPYW}$
7599 array containing the contents of the $Y'$ plane of the previous reference
7600 frame. \\
7601 \bitvar{PREVREFCB} & \multicolumn{1}{p{50pt}}{2D Integer Array} &
7602 8 & No & A $\bitvar{RPCH}\times\bitvar{RPCW}$
7603 array containing the contents of the $C_b$ plane of the previous reference
7604 frame. \\
7605 \bitvar{PREVREFCR} & \multicolumn{1}{p{50pt}}{2D Integer Array} &
7606 8 & No & A $\bitvar{RPCH}\times\bitvar{RPCW}$
7607 array containing the contents of the $C_r$ plane of the previous reference
7608 frame. \\
7609 \bottomrule\end{tabularx}
7611 \paragraph{Output parameters:}\hfill\\*
7612 \begin{tabularx}{\textwidth}{@{}llrcX@{}}\toprule
7613 \multicolumn{1}{c}{Name} &
7614 \multicolumn{1}{c}{Type} &
7615 \multicolumn{1}{p{30pt}}{\centering Size (bits)} &
7616 \multicolumn{1}{c}{Signed?} &
7617 \multicolumn{1}{c}{Description and restrictions} \\\midrule\endhead
7618 \bitvar{RECY} & \multicolumn{1}{p{50pt}}{2D Integer Array} &
7619 8 & No & A $\bitvar{RPYH}\times\bitvar{RPYW}$
7620 array containing the contents of the $Y'$ plane of the reconstructed frame. \\
7621 \bitvar{RECCB} & \multicolumn{1}{p{50pt}}{2D Integer Array} &
7622 8 & No & A $\bitvar{RPCH}\times\bitvar{RPCW}$
7623 array containing the contents of the $C_b$ plane of the reconstructed
7624 frame. \\
7625 \bitvar{RECCR} & \multicolumn{1}{p{50pt}}{2D Integer Array} &
7626 8 & No & A $\bitvar{RPCH}\times\bitvar{RPCW}$
7627 array containing the contents of the $C_r$ plane of the reconstructed
7628 frame. \\
7629 \bitvar{GOLDREFY} & \multicolumn{1}{p{50pt}}{2D Integer Array} &
7630 8 & No & A $\bitvar{RPYH}\times\bitvar{RPYW}$
7631 array containing the contents of the $Y'$ plane of the golden reference
7632 frame. \\
7633 \bitvar{GOLDREFCB} & \multicolumn{1}{p{50pt}}{2D Integer Array} &
7634 8 & No & A $\bitvar{RPCH}\times\bitvar{RPCW}$
7635 array containing the contents of the $C_b$ plane of the golden reference
7636 frame. \\
7637 \bitvar{GOLDREFCR} & \multicolumn{1}{p{50pt}}{2D Integer Array} &
7638 8 & No & A $\bitvar{RPCH}\times\bitvar{RPCW}$
7639 array containing the contents of the $C_r$ plane of the golden reference
7640 frame. \\
7641 \bitvar{PREVREFY} & \multicolumn{1}{p{50pt}}{2D Integer Array} &
7642 8 & No & A $\bitvar{RPYH}\times\bitvar{RPYW}$
7643 array containing the contents of the $Y'$ plane of the previous reference
7644 frame. \\
7645 \bitvar{PREVREFCB} & \multicolumn{1}{p{50pt}}{2D Integer Array} &
7646 8 & No & A $\bitvar{RPCH}\times\bitvar{RPCW}$
7647 array containing the contents of the $C_b$ plane of the previous reference
7648 frame. \\
7649 \bitvar{PREVREFCR} & \multicolumn{1}{p{50pt}}{2D Integer Array} &
7650 8 & No & A $\bitvar{RPCH}\times\bitvar{RPCW}$
7651 array containing the contents of the $C_r$ plane of the previous reference
7652 frame. \\
7653 \bottomrule\end{tabularx}
7655 \paragraph{Variables used:}\hfill\\*
7656 \begin{tabularx}{\textwidth}{@{}llrcX@{}}\toprule
7657 \multicolumn{1}{c}{Name} &
7658 \multicolumn{1}{c}{Type} &
7659 \multicolumn{1}{p{30pt}}{\centering Size (bits)} &
7660 \multicolumn{1}{c}{Signed?} &
7661 \multicolumn{1}{c}{Description and restrictions} \\\midrule\endhead
7662 \locvar{FTYPE} & Integer & 1 & No & The frame type. \\
7663 \locvar{NQIS} & Integer & 2 & No & The number of \qi\ values. \\
7664 \locvar{QIS} & \multicolumn{1}{p{40pt}}{Integer array} &
7665 6 & No & An \locvar{NQIS}-element array of
7666 \qi\ values. \\
7667 \locvar{BCODED} & \multicolumn{1}{p{40pt}}{Integer Array} &
7668 1 & No & An \bitvar{NBS}-element array of flags
7669 indicating which blocks are coded. \\
7670 \locvar{MBMODES} & \multicolumn{1}{p{40pt}}{Integer Array} &
7671 3 & No & An \bitvar{NMBS}-element array of
7672 coding modes for each macro block. \\
7673 \locvar{MVECTS} & \multicolumn{1}{p{50pt}}{Array of 2D Integer Vectors} &
7674 6 & Yes & An \bitvar{NBS}-element array of motion
7675 vectors for each block. \\
7676 \locvar{QIIS} & \multicolumn{1}{p{40pt}}{Integer Array} &
7677 2 & No & An \bitvar{NBS}-element array of
7678 \locvar{\qii} values for each block. \\
7679 \locvar{COEFFS} & \multicolumn{1}{p{50pt}}{2D Integer Array} &
7680 16 & Yes & An $\bitvar{NBS}\times 64$ array of
7681 quantized DCT coefficient values for each block in zig-zag order. \\
7682 \locvar{NCOEFFS} & \multicolumn{1}{p{40pt}}{Integer Array} &
7683 7 & No & An \bitvar{NBS}-element array of the
7684 coefficient count for each block. \\
7685 \bitvar{RPYW} & Integer & 20 & No & The width of the $Y'$ plane of the
7686 reference frames in pixels. \\
7687 \bitvar{RPYH} & Integer & 20 & No & The height of the $Y'$ plane of the
7688 reference frames in pixels. \\
7689 \bitvar{RPCW} & Integer & 20 & No & The width of the $C_b$ and $C_r$
7690 planes of the reference frames in pixels. \\
7691 \bitvar{RPCH} & Integer & 20 & No & The height of the $C_b$ and $C_r$
7692 planes of the reference frames in pixels. \\
7693 \locvar{\bi} & Integer & 36 & No & The index of the current block in coded
7694 order. \\
7695 \bottomrule\end{tabularx}
7696 \medskip
7698 This procedure uses all the procedures defined in the previous section of this
7699 chapter to decode and reconstruct a complete frame.
7700 It takes as input values decoded from the headers, as well as the current
7701 reference frames.
7702 As output, it gives the uncropped, reconstructed frame.
7703 This should be cropped to picture region before display.
7704 As a special case, a 0-byte packet is treated exactly like an inter frame with
7705 no coded blocks.
7707 \begin{enumerate}
7708 \item
7709 If the size of the data packet is non-zero:
7710 \begin{enumerate}
7711 \item
7712 Decode the frame header values \locvar{FTYPE}, \locvar{NQIS}, and \locvar{QIS}
7713 using the procedure given in Section~\ref{sub:frame-header}.
7714 \item
7715 Using \locvar{FTYPE}, \bitvar{NSBS}, and \bitvar{NBS}, decode the list of coded
7716 block flags into \locvar{BCODED} using the procedure given in
7717 Section~\ref{sub:coded-blocks}.
7718 \item
7719 Using \locvar{FTYPE}, \bitvar{NMBS}, \bitvar{NBS}, and \bitvar{BCODED}, decode
7720 the macro block coding modes into \locvar{MBMODES} using the procedure given
7721 in Section~\ref{sub:mb-modes}.
7722 \item
7723 If \locvar{FTYPE} is non-zero (inter frame), using \bitvar{PF}, \bitvar{NMBS},
7724 \locvar{MBMODES}, \bitvar{NBS}, and \locvar{BCODED}, decode the motion vectors
7725 into \locvar{MVECTS} using the procedure given in Section~\ref{sub:mv-decode}.
7726 \item
7727 Using \bitvar{NBS}, \locvar{BCODED}, and \locvar{NQIS}, decode the block-level
7728 \qi\ values into \locvar{QIIS} using the procedure given in
7729 Section~\ref{sub:block-qis}.
7730 \item
7731 Using \bitvar{NBS}, \bitvar{NMBS}, \locvar{BCODED}, and \bitvar{HTS}, decode
7732 the DCT coefficients into \locvar{NCOEFFS} and \locvar{NCOEFFS} using the
7733 procedure given in Section~\ref{sub:dct-coeffs}.
7734 \item
7735 Using \locvar{BCODED} and \locvar{MBMODES}, undo the DC prediction on the DC
7736 coefficients stored in \locvar{COEFFS} using the procedure given in
7737 Section~\ref{sub:dc-pred-undo}.
7738 \end{enumerate}
7739 \item
7740 Otherwise:
7741 \begin{enumerate}
7742 \item
7743 Assign \locvar{FTYPE} the value 1 (inter frame).
7744 \item
7745 Assign \locvar{NQIS} the value 1.
7746 \item
7747 Assign $\locvar{QIS}[0]$ the value 63.
7748 \item
7749 For each value of \locvar{\bi} from 0 to $(\bitvar{NBS}-1)$, assign
7750 $\locvar{BCODED}[\locvar{\bi}]$ the value zero.
7751 \end{enumerate}
7752 \item
7753 Assign \locvar{RPYW} and \locvar{RPYH} the values $(16*\bitvar{FMBW})$ and
7754 $(16*\bitvar{FMBH})$, respectively.
7755 \item
7756 Assign \locvar{RPCW} and \locvar{RPCH} the values from the row of
7757 Table~\ref{tab:rpcwh-for-pf} corresponding to \bitvar{PF}.
7759 \begin{table}[tb]
7760 \begin{center}
7761 \begin{tabular}{crr}\toprule
7762 \bitvar{PF} & \multicolumn{1}{c}{\locvar{RPCW}}
7763 & \multicolumn{1}{c}{\locvar{RPCH}} \\\midrule
7764 $0$ & $8*\bitvar{FMBW}$ & $8*\bitvar{FMBH}$ \\
7765 $2$ & $8*\bitvar{FMBW}$ & $16*\bitvar{FMBH}$ \\
7766 $3$ & $16*\bitvar{FMBW}$ & $16*\bitvar{FMBH}$ \\
7767 \bottomrule\end{tabular}
7768 \end{center}
7769 \caption{Width and Height of Chroma Planes for each Pixel Format}
7770 \label{tab:rpcwh-for-pf}
7771 \end{table}
7773 \item
7774 Using \bitvar{ACSCALE}, \bitvar{DCSCALE}, \bitvar{BMS}, \bitvar{NQRS},
7775 \bitvar{QRSIZES}, \bitvar{QRBMIS}, \bitvar{NBS}, \locvar{BCODED},
7776 \locvar{MBMODES}, \locvar{MVECTS}, \locvar{COEFFS}, \locvar{NCOEFFS},
7777 \locvar{QIS}, \locvar{QIIS}, \locvar{RPYW}, \locvar{RPYH}, \locvar{RPCW},
7778 \locvar{RPCH}, \bitvar{GOLDREFY}, \bitvar{GOLDREFCB}, \bitvar{GOLDREFCR},
7779 \bitvar{PREVREFY}, \bitvar{PREVREFCB}, and \bitvar{PREVREFCR}, reconstruct the
7780 complete frame into \bitvar{RECY}, \bitvar{RECCB}, and \bitvar{RECCR} using
7781 the procedure given in Section~\ref{sub:recon}.
7782 \item
7783 Using \bitvar{LFLIMS}, \locvar{RPYW}, \locvar{RPYH}, \locvar{RPCW},
7784 \locvar{RPCH}, \bitvar{NBS}, \locvar{BCODED}, and \locvar{QIS}, apply the loop
7785 filter to the reconstructed frame in \bitvar{RECY}, \bitvar{RECCB}, and
7786 \bitvar{RECCR} using the procedure given in Section~\ref{sub:loop-filt}.
7787 \item
7788 If \locvar{FTYPE} is zero (intra frame), assign \bitvar{GOLDREFY},
7789 \bitvar{GOLDREFCB}, and \bitvar{GOLDREFCR} the values \bitvar{RECY},
7790 \bitvar{RECCB}, and \bitvar{RECCR}, respectively.
7791 \item
7792 Assign \bitvar{PREVREFY}, \bitvar{PREVREFCB}, and \bitvar{PREVREFCR} the values
7793 \bitvar{RECY}, \bitvar{RECCB}, and \bitvar{RECCR}, respectively.
7794 \end{enumerate}
7796 %\backmatter
7797 \appendix
7799 \chapter{Ogg Bitstream Encapsulation}
7800 \label{app:oggencapsulation}
7802 \section{Overview}
7804 This document specifies the embedding or encapsulation of Theora packets
7805 in an Ogg transport stream.
7807 Ogg is a stream oriented wrapper for coded, linear time-based data.
7808 It provides syncronization, multiplexing, framing, error detection and
7809 seeking landmarks for the decoder and complements the raw packet format
7810 used by the Theora codec.
7812 This document assumes familiarity with the details of the Ogg standard.
7813 The Xiph.org documentation provides an overview of the Ogg transport stream
7814 format at \url{http://www.xiph.org/ogg/doc/oggstream.html} and a detailed
7815 description at \url{http://www.xiph.org/ogg/doc/framing.html}.
7816 The format is also defined in RFC~3533 \cite{rfc3533}.
7817 While Theora packets can be embedded in a wide variety of media
7818 containers and streaming mechanisms, the Xiph.org Foundation
7819 recommends Ogg as the native format for Theora video in file-oriented
7820 storage and transmission contexts.
7822 \subsection{MIME type}
7824 The generic MIME type of any Ogg file is {\tt application/ogg}.
7825 The specific MIME type for the Ogg Theora profile documented here
7826 is {\tt video/ogg}. This is the MIME type recommended for files
7827 conforming to this appendix. The recommended filename extension
7828 is {\tt .ogv}.
7830 Outside of an encapsulation, the mime type {\tt video/theora} may
7831 be used to refer specifically to the Theora compressed video stream.
7833 \section{Embedding in a logical bitstream}
7835 Ogg separates the concept of a {\em logical bitstream} consisting of the
7836 framing of a particular sequence of packets and complete within itself
7837 from the {\em physical bitstream} which may consist either of a single
7838 logical bitstream or a number of logical bitstreams multiplexed
7839 together.
7840 This section specifies the embedding of Theora packets in a logical Ogg
7841 bitstream.
7842 The mapping of Ogg Theora logical bitstreams into a multiplexed physical Ogg
7843 stream is described in the next section.
7845 \subsection{Headers}
7847 The initial identification header packet appears by itself in a
7848 single Ogg page.
7849 This page defines the start of the logical stream and MUST have
7850 the `beginning of stream' flag set.
7852 The second and third header packets (comment metadata and decoder
7853 setup data) can together span one or more Ogg pages.
7854 If there are additional non-normative header packets, they MUST be
7855 included in this sequence of pages as well.
7856 The comment header packet MUST begin the second Ogg page in the logical
7857 bitstream, and there MUST be a page break between the last header
7858 packet and the first frame data packet.
7860 These two page break requirements facilitate stream identification and
7861 simplify header acquisition for seeking and live streaming applications.
7863 All header pages MUST have their granule position field set to zero.
7865 \subsection{Frame data}
7867 The first frame data packet in a logical bitstream MUST begin a new Ogg
7868 page.
7869 All other data packets are placed one at a time into Ogg pages
7870 until the end of the stream.
7871 Packets can span pages and multiple packets can be placed within any
7872 one page.
7873 The last page in the logical bitstream SHOULD have its
7874 'end of stream' flag set to indicate complete transmission
7875 of the available video.
7877 Frame data pages MUST be marked with a granule position corresponding to
7878 the end of the display interval of the last frame/packet that finishes
7879 in that page. See the next section for details.
7881 \subsection{Granule position}
7883 Data packets are marked by a granulepos derived from the count of decodable
7884 frames after that packet is processed. The field itself is divided into two
7885 sections, the width of the less significant section being given by the KFGSHIFT
7886 parameter decoded from the identification header
7887 (Section~\ref{sec:idheader}).
7888 The more significant portion of the field gives the count of coded
7889 frames after the coding of the last keyframe in stream, and the less
7890 significant portion gives the count of frames since the last keyframe.
7891 Thus a stream would begin with a split granulepos of $1|0$ (a keyframe),
7892 followed by $1|1$, $1|2$, $1|3$, etc. Around a keyframe in the
7893 middle of the stream the granulepos sequence might be $1234|35$,
7894 $1234|36$, $1234|37$, $1271|0$ (for the keyframe), $1271|1$, and so
7895 on. In this way the granulepos field increased monotonically as required
7896 by the Ogg format, but contains information necessary to efficiently
7897 find the previous keyframe to continue decoding after a seek.
7899 Prior to bitstream version 3.2.1, data packets were marked by a
7900 granulepos derived from the index of the frame being decoded,
7901 rather than the count. That is they marked the beginning of the
7902 display interval of a frame rather than the end. Such streams
7903 have the VREV field of the identification header set to `0'
7904 instead of `1'. They can be interpreted according to the description
7905 above by adding 1 to the more signification field of the split
7906 granulepos when VREV is less than 1.
7908 \section{Multiplexed stream mapping}
7910 Applications supporting Ogg Theora must support Theora bitstreams
7911 multiplexed with compressed audio data in the Vorbis I and Speex
7912 formats, and should support Ogg-encapsulated MNG graphics for overlays.
7914 Multiple audio and video bitstreams may be multiplexed together.
7915 How playback of multiple/alternate streams is handled is up to the
7916 application.
7917 Some conventions based on included metadata aide interoperability
7918 in this respect.
7919 %TODO: describe multiple vs. alternate streams, language mapping
7920 % and reference metadata descriptions.
7922 \subsection{Chained streams}
7924 Ogg Theora decoders and playback applications MUST support both grouped
7925 streams (multiplexed concurrent logical streams) and chained streams
7926 (sequential concatenation of independent physical bitstreams).
7928 The number and codec data types of multiplexed streams and the decoder
7929 parameters for those stream types that re-occur can all change at a
7930 chaining boundary.
7931 A playback application MUST be prepared to handle such changes and
7932 SHOULD do so smoothly with the minimum possible visible disruption.
7933 The specification of grouped streams below applies independently to each
7934 segment of a chained bitstream.
7936 \subsection{Grouped streams}
7938 At the beginning of a multiplexed stream, the `beginning of stream'
7939 pages for each logical bitstream will be grouped together.
7940 Within these, the first page to occur MUST be the Theora page.
7941 This facilitates identification of Ogg Theora files among other
7942 Ogg-encapsulated content.
7943 A playback application must nevertheless handle streams where this
7944 arrangement is not correct.
7945 %TBT: Then what's the point of requiring it in the spec?
7947 If there is more than one Theora logical stream, the first page should
7948 be from the primary stream.
7949 That is, the best choice for the stream a generic player should begin
7950 displaying without special user direction.
7951 If there is more than one audio stream, or of any other stream
7952 type, the identification page of the primary stream of that type
7953 should be placed before the others.
7954 %TBT: That's all pretty vague.
7956 After the `beginning of stream' pages, the header pages of each of
7957 the logical streams MUST be grouped together before any data pages
7958 occur.
7960 After all the header pages have been placed,
7961 the data pages are multiplexed together.
7962 They should be placed in the stream in increasing order by the
7963 time equivalents of their granule position fields.
7964 This facilitates seeking while limiting the buffering requirements of the
7965 playback demultiplexer.
7966 %TODO: A lot of this language is encoder-oriented.
7967 %TODO: We define a decoder-oriented specification.
7968 %TODO: The language should be changed to match.
7970 \cleardoublepage
7971 \chapter{VP3}
7973 \section{VP3 Compatibility}
7974 \label{app:vp3-compat}
7975 This section lists all of the encoder and decoder issues that may affect VP3
7976 compatibly.
7977 Each is described in more detail in the text itself.
7978 This list is provided merely for reference.
7980 \begin{itemize}
7981 \item
7982 Bitstream headers (Section~\ref{sec:headers}).
7983 \begin{itemize}
7984 \item
7985 Identification header (Section~\ref{sec:idheader}).
7986 \begin{itemize}
7987 \item
7988 Non-multiple of 16 picture sizes.
7989 \item
7990 Standardized color spaces.
7991 \item
7992 Support for $4:4:4$ and $4:2:2$ pixel formats.
7993 \end{itemize}
7994 \item
7995 Setup header
7996 \begin{itemize}
7997 \item
7998 Loop filter limit values (Section~\ref{sub:loop-filter-limits}).
7999 \item
8000 Quantization parameters (Section~\ref{sub:quant-params}).
8001 \item
8002 Huffman tables (Section~\ref{sub:huffman-tables}).
8003 \end{itemize}
8004 \end{itemize}
8005 \item
8006 Frame header format (Section~\ref{sub:frame-header}).
8007 \item
8008 Extended long-run bit strings (Section~\ref{sub:long-run}).
8009 \item
8010 INTER\_MV\_FOUR handling of uncoded blocks (Section~\ref{sub:mb-mv-decode}).
8011 \item
8012 Block-level \qi\ values (Section~\ref{sub:block-qis}).
8013 \item
8014 Zero-length EOB runs (Section~\ref{sub:eob-token}).
8015 \item
8016 Unrestricted motion vector padding and the loop filter
8017 (Section~\ref{sub:loop-filt}).
8018 \end{itemize}
8020 \section{Loop Filter Limit Values}
8021 \label{app:vp3-loop-filter-limits}
8023 The hard-coded loop filter limit values used in VP3 are defined as follows:
8024 \begin{align*}
8025 \bitvar{LFLIMS} = & \begin{array}[t]{r@{}rrrrrrrr@{}l}
8026 \{ & 30, & 25, & 20, & 20, & 15, & 15, & 14, & 14, & \\
8027 & 13, & 13, & 12, & 12, & 11, & 11, & 10, & 10, & \\
8028 & 9, & 9, & 8, & 8, & 7, & 7, & 7, & 7, & \\
8029 & 6, & 6, & 6, & 6, & 5, & 5, & 5, & 5, & \\
8030 & 4, & 4, & 4, & 4, & 3, & 3, & 3, & 3, & \\
8031 & 2, & 2, & 2, & 2, & 2, & 2, & 2, & 2, & \\
8032 & 0, & 0, & 0, & 0, & 0, & 0, & 0, & 0, & \\
8033 & 0, & 0, & 0, & 0, & 0, & 0, & 0, & 0\;\ & \!\} \\
8034 \end{array}
8035 \end{align*}
8037 \section{Quantization Parameters}
8038 \label{app:vp3-quant-params}
8040 The hard-coded quantization parameters used by VP3 are defined as follows:
8042 \begin{align*}
8043 \bitvar{ACSCALE} = & \begin{array}[t]{r@{}rrrrrrrr@{}l}
8044 \{ & 500, & 450, & 400, & 370, & 340, & 310, & 285, & 265, & \\
8045 & 245, & 225, & 210, & 195, & 185, & 180, & 170, & 160, & \\
8046 & 150, & 145, & 135, & 130, & 125, & 115, & 110, & 107, & \\
8047 & 100, & 96, & 93, & 89, & 85, & 82, & 75, & 74, & \\
8048 & 70, & 68, & 64, & 60, & 57, & 56, & 52, & 50, & \\
8049 & 49, & 45, & 44, & 43, & 40, & 38, & 37, & 35, & \\
8050 & 33, & 32, & 30, & 29, & 28, & 25, & 24, & 22, & \\
8051 & 21, & 19, & 18, & 17, & 15, & 13, & 12, & 10\;\ & \!\} \\
8052 \end{array} \\
8053 \bitvar{DCSCALE} = & \begin{array}[t]{r@{}rrrrrrrr@{}l}
8054 \{ & 220, & 200, & 190, & 180, & 170, & 170, & 160, & 160, & \\
8055 & 150, & 150, & 140, & 140, & 130, & 130, & 120, & 120, & \\
8056 & 110, & 110, & 100, & 100, & 90, & 90, & 90, & 80, & \\
8057 & 80, & 80, & 70, & 70, & 70, & 60, & 60, & 60, & \\
8058 & 60, & 50, & 50, & 50, & 50, & 40, & 40, & 40, & \\
8059 & 40, & 40, & 30, & 30, & 30, & 30, & 30, & 30, & \\
8060 & 30, & 20, & 20, & 20, & 20, & 20, & 20, & 20, & \\
8061 & 20, & 10, & 10, & 10, & 10, & 10, & 10, & 10\;\ & \!\} \\
8062 \end{array}
8063 \end{align*}
8065 VP3 defines only a single quantization range for each quantization type and
8066 color plane, and the base matrix used is constant throughout the range.
8067 There are three base matrices defined.
8068 The first is used for the $Y'$ channel of INTRA mode blocks, and the second for
8069 both the $C_b$ and $C_r$ channels of INTRA mode blocks.
8070 The last is used for INTER mode blocks of all channels.
8072 \begin{align*}
8073 \bitvar{BMS} = \{ & \begin{array}[t]{r@{}rrrrrrrr@{}l}
8074 \{ & 16, & 11, & 10, & 16, & 24, & 40, & 51, & 61, & \\
8075 & 12, & 12, & 14, & 19, & 26, & 58, & 60, & 55, & \\
8076 & 14, & 13, & 16, & 24, & 40, & 57, & 69, & 56, & \\
8077 & 14, & 17, & 22, & 29, & 51, & 87, & 80, & 62, & \\
8078 & 18, & 22, & 37, & 58, & 68, & 109, & 103, & 77, & \\
8079 & 24, & 35, & 55, & 64, & 81, & 104, & 113, & 92, & \\
8080 & 49, & 64, & 78, & 87, & 103, & 121, & 120, & 101, & \\
8081 & 72, & 92, & 95, & 98, & 112, & 100, & 103, & 99\;\ & \!\}, \\
8082 %\end{array} \\
8083 %& \begin{array}[t]{r@{}rrrrrrrr@{}l}
8084 \{ & 17, & 18, & 24, & 47, & 99, & 99, & 99, & 99, & \\
8085 & 18, & 21, & 26, & 66, & 99, & 99, & 99, & 99, & \\
8086 & 24, & 26, & 56, & 99, & 99, & 99, & 99, & 99, & \\
8087 & 47, & 66, & 99, & 99, & 99, & 99, & 99, & 99, & \\
8088 & 99, & 99, & 99, & 99, & 99, & 99, & 99, & 99, & \\
8089 & 99, & 99, & 99, & 99, & 99, & 99, & 99, & 99, & \\
8090 & 99, & 99, & 99, & 99, & 99, & 99, & 99, & 99, & \\
8091 & 99, & 99, & 99, & 99, & 99, & 99, & 99, & 99\;\ & \!\}, \\
8092 %\end{array} \\
8093 %& \begin{array}[t]{r@{}rrrrrrrr@{}l}
8094 \{ & 16, & 16, & 16, & 20, & 24, & 28, & 32, & 40, & \\
8095 & 16, & 16, & 20, & 24, & 28, & 32, & 40, & 48, & \\
8096 & 16, & 20, & 24, & 28, & 32, & 40, & 48, & 64, & \\
8097 & 20, & 24, & 28, & 32, & 40, & 48, & 64, & 64, & \\
8098 & 24, & 28, & 32, & 40, & 48, & 64, & 64, & 64, & \\
8099 & 28, & 32, & 40, & 48, & 64, & 64, & 64, & 96, & \\
8100 & 32, & 40, & 48, & 64, & 64, & 64, & 96, & 128, & \\
8101 & 40, & 48, & 64, & 64, & 64, & 96, & 128, & 128\;\ & \!\}\;\;\} \\
8102 \end{array}
8103 \end{align*}
8105 The remaining parameters simply assign these matrices to the proper quant
8106 ranges.
8108 \begin{align*}
8109 \bitvar{NQRS} = & \{ \{1, 1, 1\}, \{1, 1, 1\} \} \\
8110 \bitvar{QRSIZES} = &
8111 \{ \{ \{1\}, \{1\}, \{1\} \}, \{ \{1\}, \{1\}, \{1\} \} \} \\
8112 \bitvar{QRBMIS} = &
8113 \{ \{ \{0, 0\}, \{1, 1\}, \{1, 1\} \}, \{ \{2, 2\}, \{2, 2\}, \{2, 2\} \} \} \\
8114 \end{align*}
8116 \section{Huffman Tables}
8117 \label{app:vp3-huffman-tables}
8119 The following tables contain the hard-coded Huffman codes used by VP3.
8120 There are 80 tables in all, each with a Huffman code for all 32 token values.
8121 The tokens are sorted by the most significant bits of their Huffman code.
8122 This is the same order in which they will be decoded from the setup header.
8124 \include{vp3huff}
8126 \cleardoublepage
8127 \chapter{Colophon}
8129 Ogg is a \href{http://www.xiph.org}{Xiph.org Foundation} effort to protect
8130 essential tenets of Internet multimedia from corporate hostage-taking; Open
8131 Source is the net's greatest tool to keep everyone honest.
8132 See \href{http://www.xiph.org/about.html}{About the Xiph.org Foundation} for
8133 details.
8135 Ogg Theora is the first Ogg video codec.
8136 Anyone may freely use and distribute the Ogg and Theora specifications, whether
8137 in private, public, or corporate capacity.
8138 However, the Xiph.org Foundation and the Ogg project reserve the right to set
8139 the Ogg Theora specification and certify specification compliance.
8141 Xiph.org's Theora software codec implementation is distributed under a BSD-like
8142 license.
8143 This does not restrict third parties from distributing independent
8144 implementations of Theora software under other licenses.
8146 \begin{wrapfigure}{l}{0pt}
8147 \includegraphics[width=2.5cm]{xifish}
8148 \end{wrapfigure}
8150 These pages are Copyright \textcopyright{} 2004-2007 Xiph.org Foundation.
8151 All rights reserved.
8152 Ogg, Theora, Vorbis, Xiph.org Foundation and their logos are trademarks
8153 (\texttrademark) of the \href{http://www.xiph.org}{Xiph.org Foundation}.
8155 This document is set in \LaTeX.
8159 \cleardoublepage
8160 \bibliography{spec}
8162 \end{document}