Include a pre-rendered pdf version
[dirac-spec-errata.git] / dataenc.tex
blob42067e880cbfee3abd4e8df0e9cd5dc0fd17da39
1 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
2 % - This chapter defines how raw and VLC data - %
3 % - is extracted - %
4 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
6 \label{dataenc}
8 Data shall be encoded in the Dirac bitstream using four basic methods:
9 \begin{itemize}
10 \item fixed-length bit-wise codings,
11 \item fixed-length byte-wise codings,
12 \item variable-length codes,
13 \item arithmetic encoding.
14 \end{itemize}
16 This annex defines how data shall be encoded in the Dirac stream and how
17 sequences of bits shall be extracted as values of various types using the
18 aforementioned fundamental data coding types. The extraction of arithmetically
19 encoded data shall require the use of the arithmetic decoding engine defined in
20 Annex~\ref{arithengine}.
22 \subsection{Bit-packing and data input}
23 \label{bitpacking}
25 This section defines the operation of the $read\_bit()$, $read\_byte()$
26 and $byte\_align()$ functions used for direct access to the Dirac stream.
28 The stream data shall be accessed byte by byte, and a decoder is deemed to
29 maintain a copy of the current byte, $\CurrentByte$, and an index to the next
30 bit (in the byte) to be read, $\NextBit$. $\NextBit$ shall be an integer from 0
31 (least-significant bit) to 7 (most-significant bit). Bits within bytes shall be
32 accessed from the msb first to the lsb.
34 \subsubsection{Reading a byte}
36 The $read\_byte()$ function shall perform the following steps:
37 \begin{enumerate}
38 \item Set $\NextBit=7$
39 \item Set $\CurrentByte$ to the next unread byte in the stream
40 \end{enumerate}
42 \subsubsection{Reading a bit}\label{readbit}
44 The $read\_bit()$ function shall be defined as follows:
46 \begin{pseudo}{read\_bit}{}
47 \bsCODE{bit = ( \CurrentByte \gg \NextBit ) \& 1 }
48 \bsCODE{\NextBit -= 1}
49 \bsIF{\NextBit<0}
50 \bsCODE{\NextBit = 7}
51 \bsCODE{read\_byte()}
52 \bsEND
53 \bsRET{bit}
54 \end{pseudo}
56 \subsubsection{Byte alignment}\label{bytealign}
58 The $byte\_align()$ function shall be used to discard data in the current byte
59 and begin data access at the next byte, unless input is already at the beginning
60 of a byte. It shall be defined as follows:
62 \begin{pseudo}{byte\_align}{}
63 \bsIF{\NextBit != 7}
64 \bsCODE{read\_byte()}
65 \bsEND
66 \end{pseudo}
68 \subsection{Parsing of fixed-length data}
70 Dirac defines three fixed length data encodings as follows:
72 \subsubsection{Boolean}
74 The $read\_bool()$ function shall return $\true$ if 1 is read from the stream and
75 $\false$ otherwise. The $read\_bool()$ function shall be defined as follows:
77 \begin{pseudo}{read\_bool}{}
78 \bsIF{read\_bit()==1}
79 \bsRET{\true}
80 \bsELSE
81 \bsRET{\false}
82 \bsEND
83 \end{pseudo}
85 \subsubsection{n-bit literal}\label{readnbits}
87 An $n$-bit number in literal format shall be decoded by extracting $n$ bits
88 in order, using the $read\_bit()$ function (Section \ref{readbit})
89 and placing the first bit in the leftmost position, the second
90 bit in the next position and so on. The resulting value shall be
91 interpreted as an unsigned integer.
93 The $read\_nbits()$ function shall be defined as follows:
95 \begin{pseudo}{read\_nbits}{n}
96 \bsCODE{val=0}
97 \bsFOR{i=0}{n-1}
98 \bsCODE{val \ll = 1}
99 \bsCODE{val += read\_bit()}{\ref{readbit}}
100 \bsEND
101 \bsRET{val}
102 \end{pseudo}
104 \subsubsection{$n$-byte unsigned integer literal}
106 The $read\_uint\_lit()$ function shall be defined as follows:
108 \begin{pseudo}{read\_uint\_lit}{n}
109 \bsCODE{byte\_align()}{\ref{bytealign}}
110 \bsRET{read\_nbits(8*n)}{\ref{readnbits}}
111 \end{pseudo}
113 \subsection{Variable-length codes}
114 \label{vlc}
115 Variable-length codes shall be used in three ways in the Dirac stream:
116 \begin{enumerate}
117 \item The first use shall be for direct encoding of header values into the stream.
118 \item The second use shall be for entropy coding of motion data and coefficients, where arithmetic coding
119 is used.
120 \item The third use shall be for binarisation in the arithmetic encoding/decoding process so that integer
121 values may be coded and decoded using a binary arithmetic coding engine. This is
122 defined in Annex~\ref{arithdecoding}.
123 \end{enumerate}
125 When used for coding motion data and coefficients, VLCs shall be employed within
126 a data block of known length. It is possible to gain additional compression by early termination:
127 maintaining a count of remaining bits, and returning default values when this length
128 is exceeded. This shall be achieved by use of the $read\_bitb()$, $read\_boolb()$,
129 $read\_uintb()$ and $read\_sintb()$ for reading values from data blocks.
131 \begin{informative}
132 A similar early termination facility is a used for arithmetic decoding.
133 \end{informative}
135 \subsubsection{Data input for bounded block operation}
136 \label{blockreadbit}
138 This section specifies the operation of the $read\_bitb()$ process for reading bits from
139 a block of known size, and the $flush\_inputb()$ process for discarding the remainder of a block of
140 data.
142 These processes shall use $\ABitsLeft$ to determine the number of bits left to the
143 end of the block.
145 The $read\_bitb()$ function shall be defined as follows:
147 \begin{pseudo}{read\_bitb}{}
148 \bsIF{\ABitsLeft==0}
149 \bsRET{1}
150 \bsELSE
151 \bsCODE{\ABitsLeft -= 1}
152 \bsRET{read\_bit()}
153 \bsEND
154 \end{pseudo}
156 When all bits in the block have been read, then $read\_bitb()$ shall return 1 by default.
158 It is possible that not all data in a block is exhausted after a sequence of read operations.
159 At the end of a sequence of read operations, the decoder shall flush the block.
161 The $flush\_inputb()$ process shall be defined as follows:
163 \begin{pseudo}{flush\_inputb}{}
164 \bsWHILE{\ABitsLeft>0}
165 \bsCODE{read\_bit()}
166 \bsCODE{\ABitsLeft-=1}
167 \bsEND
168 \end{pseudo}
170 \subsubsection{Unsigned interleaved exp-Golomb codes}
171 This section defines the unsigned interleaved exp-Golomb data format and the operation
172 of the $read\_uint()$ and the $read\_uintb()$ functions.
174 Unsigned interleaved exp-Golomb data shall be decoded to produce unsigned
175 integer values.The format shall consist of two interleaved parts,
176 and each code shall be an odd number, $2K+1$ bits in length.
178 The $K+1$ bits in the even positions (counting from zero) shall be the ``follow" bits, and
179 the $K$ bits in the odd positions shall be the ``data" bits $b_i$ that are used to construct
180 the decoded value itself. A follow bit value of $0$ shall indicate a subsequent data bit,
181 whereas a follow bit value of $1$ shall terminate the code, a typical sequence being
182 \begin{equation*}
183 0\quad x_{K-1}\quad 0\quad x_{K-2}\hdots 0\quad x_{0}\quad 1
184 \end{equation*}
186 The data bits $x_{K-1}, x_{K-2}, \hdots, x_0$ shall form the binary representation
187 of the first $K$ bits of the $(K+1)$-bit number
188 $N+1$, where $N$ is the number to be decoded, i.e.:
189 \begin{equation*}
190 N+1=1 x_{K-1} x_{K-2}\hdots x_0 \text{ (base $2$) }=2^K+\sum_{i=0}^{K-1} 2^i * x_i
191 \end{equation*}
193 Table \ref{table:uegolcodings} shows encodings of the values 0--9.
195 \begin{table}[!ht]
196 \centering
197 \begin{tabular}{|l|c|}
198 \hline
199 \rowcolor[gray]{0.75}Bit sequence & Decoded value \\
200 \hline
201 1 & 0\\
202 0 0 1 & 1\\
203 0 1 1 & 2\\
204 0 0 0 0 1 & 3\\
205 0 0 0 1 1 & 4\\
206 0 1 0 0 1 & 5\\
207 0 1 0 1 1 & 6\\
208 0 0 0 0 0 0 1 & 7\\
209 0 0 0 0 0 1 1 & 8\\
210 0 0 0 1 0 0 1 & 9\\
211 \hline
212 \end{tabular}
214 \caption{Example conversions from unsigned interleaved exp-Golomb-coded
215 values to unsigned integers \label{table:uegolcodings}}
216 \end{table}
218 Although apparently complex, the interleaving ensures that the code has a very simple decoding loop.
220 The $read\_uint()$ function shall return an unsigned integer value and shall be defined
221 as follows:
223 \begin{pseudo}{read\_uint}{}
224 \bsCODE{value = 1}
225 \bsWHILE{read\_bit() == 0}
226 \bsCODE{value \ll = 1}
227 \bsIF{read\_bit()==1}
228 \bsCODE{value += 1}
229 \bsEND
230 \bsEND
231 \bsCODE{value -= 1}
232 \bsRET{value}
233 \end{pseudo}
235 \begin{informative}
236 Conventional exp-Golomb coding places all follow bits at the beginning as a prefix. This is
237 easier to read, but requires that a count of the prefix length be maintained. Values can only
238 be decoded in two loops -- the prefix followed by the data bits. Interleaved exp-Golomb
239 coding allows values to be decoded in a single loop, without the need for a length count.
240 \end{informative}
242 The $read\_uintb()$ function is identical to $read\_uint()$ except that the block-bounded read
243 operation is employed, and shall be defined as follows:
245 \begin{pseudo}{read\_uintb}{}
246 \bsCODE{value = 1}
247 \bsWHILE{read\_bitb() == 0}
248 \bsCODE{value \ll = 1}
249 \bsIF{read\_bitb()==1}
250 \bsCODE{value += 1}
251 \bsEND
252 \bsEND
253 \bsCODE{value -= 1}
254 \bsRET{value}
255 \end{pseudo}
257 \begin{informative}
258 When $\ABitsLeft==0$, all subsequent values read by $read\_uintb()$ will be 0.
259 \end{informative}
261 \subsubsection{Signed interleaved exp-Golomb}
262 \label{segol}
264 This section defines the signed interleaved exp-Golomb data format and the operation
265 of the $read\_sint()$ and $read\_sintb()$ functions.
267 The code for the signed interleaved exp-Golomb data format consists of the
268 unsigned interleaved exp-Golomb code for the magnitude, followed by a sign bit
269 for non-zero values, as shown in the table below:
271 %\begin{table}[!ht]
273 \centering
274 \begin{tabular}{|l|c|}
275 \hline
276 \rowcolor[gray]{0.75}Bit sequence & Decoded value \\
277 \hline
278 0 0 0 1 1 1 & -4\\
279 0 0 0 0 1 1 & -3\\
280 0 1 1 1 & -2\\
281 0 0 1 1 & -1\\
282 1 & 0\\
283 0 0 1 0 & 1\\
284 0 1 1 0 & 2\\
285 0 0 0 0 1 0 & 3\\
286 0 0 0 1 1 0 & 4\\
287 \hline
288 \end{tabular}
290 %\caption{Example conversions from signed interleaved exp-Golomb-coded values
291 %to signed integers \label{table:segolcodings}}
292 %\end{table}
295 The $read\_sint()$ function shall be defined as follows.
297 \begin{pseudo}{read\_sint}{}
298 \bsCODE{value = read\_uint()}
299 \bsIF{ value!= 0}
300 \bsIF{read\_bit()==1}
301 \bsCODE{value = -value}
302 \bsEND
303 \bsEND
304 \bsRET{value}
305 \end{pseudo}
307 The $read\_sintb()$ function is identical to $read\_sint()$ except that the block-bounded read
308 operation is employed, and shall be defined as follows:
310 \begin{pseudo}{read\_sintb}{}
311 \bsCODE{value = read\_uintb()}
312 \bsIF{value != 0}
313 \bsIF{read\_bitb()==1}
314 \bsCODE{value = -value}
315 \bsEND
316 \bsEND
317 \bsRET{value}
318 \end{pseudo}
320 \begin{informative} When $\ABitsLeft==0$, all subsequent values read by $read\_sintb()$ will be 0.\end{informative}
322 \subsection{Parsing of arithmetic-coded data}
324 \label{arithdecoding}
326 This section defines the operations for reading arithmetic-coded
327 data. These operations shall make use of the elementary arithmetic coding functions
328 defined by the arithmetic decoding engine defined in Annex~\ref{arithengine}.
330 Arithmetically-coded data is present in the Dirac stream in data blocks which shall consist of
331 a whole number of bytes and which shall be byte aligned. Where arithmetic coding is used, each such
332 block shall be preceded by data which includes a length code $length$, which shall be equal to the length in
333 bytes of the data block.
335 The function $initialise\_arithmetic\_decoding(length)$
336 (Section \ref{initarith}) shall then initialises the arithmetic decoder. Once the arithmetic
337 decoder is initialised, boolean and integer values may be extracted.
339 After all values in a particular arithmetic coded block have been parsed, any remaining data
340 shall be flushed using the $flush\_inputb()$ process (Section \ref{blockreadbit}).
342 \subsubsection{Context probabilities}
343 \label{contextprobs}
345 Values shall be extracted by using binary context probabilities. A context is a
346 decoding state, representing the set of all data decoded so far.
348 A context probability shall be a 16 bit unsigned integer value representing the
349 probability of a bit being 0 in a given context, where zero probability is
350 represented by 0x0, and equal likelihood by 0x8000. The process for initializing
351 and updating context probabilities shall be as defined in annex
352 \ref{initarith} and \ref{contextupdate}.
354 \begin{informative}
355 Probability 1, or certainty, would be represented by the 17-bit number 0x10000.
356 This value, and probability 0 (0x0), can never be attained due to the operation
357 of the probability update process (Annex~\ref{contextupdate}).
358 \end{informative}
360 Different context probabilities shall be employed for extracting binary values,
361 based on the values of previously decoded data. Each context probability shall
362 be updated by the arithmetic decoding engine to track statistics after it
363 has been used to extract a value.
365 The set of contexts probabilities shall be defined by $\AContexts$, and an
366 individual context shall be accessed via a keyword label i.e.
367 $\AContexts$ is a map and the context value shall be $\AContexts[l]$ for a
368 label $l$.
370 The array of context probability labels to be used in arithmetic decoding shall
371 be passed to the arithmetic decoding engine at initialization (annex
372 \ref{initarith}).
374 \subsubsection{Arithmetic decoding of boolean values}
376 Given a context probability lable $l$, the arithmetic decoding engine shall support a function
377 $read\_boola(l)$, specified in Section \ref{arithreadbool}, which shall returna boolean value.
379 \subsubsection{Arithmetic decoding of integer values}
381 \label{arithreadint}
383 This section defines the operation of the $read\_sinta(context\_prob\_set)$ and
384 $read\_uinta(context\_prob\_set)$ functions
385 for extracting integer values from a block of arithmetically coded data.
387 \paragraph{Binarisation and contexts}\label{binandcontext}
388 $\ $\newline
389 Signed and unsigned integer values shall be coded by first converting to binary form by
390 using interleaved exp-Golomb binarisation as per Section \ref{vlc}. The
391 $read\_sinta()$ and $read\_uinta()$ processes shall be identical to the
392 $read\_sint()$ and $read\_uint()$ processes, except that instances of $read\_bit()$ shall be replaced
393 by instances of $read\_boola()$ (Section \ref{arithreadbool}) using a suitable context
394 for each bit.
396 $read\_sinta()$ and $read\_uinta()$ shall be provided with a map $context\_prob\_set$,
397 which shall consist of three parts:
398 \begin{enumerate}
399 \item an array of follow context indices, $context\_prob\_set[FOLLOW]$
400 \item a single data context index, $context\_prob\_set[DATA]$
401 \item a sign context index, $context\_prob\_set[SIGN]$ (ignored for unsigned integer decoding)
402 \end{enumerate}
404 Each follow context shall be used for decoding the corresponding follow bit, with the
405 last follow context being used for all subsequent follow bits also (if any).
407 The follow context selection function $follow\_context()$ shall be defined as follows:
409 \begin{pseudo}{follow\_context}{index, context\_prob\_set}
410 \bsCODE{pos= \min(index, length(context\_prob\_set[FOLLOW])-1) }
411 \bsCODE{ctx\_label = context\_prob\_set[FOLLOW][pos]}
412 \bsRET{ctx\_label}
413 \end{pseudo}
415 \paragraph{Unsigned integer decoding}
416 $\ $\newline
418 The $read\_uinta()$ function shall be defined as follows:
420 \begin{pseudo}{read\_uinta}{context\_prob\_set}
421 \bsCODE{value = 1}
422 \bsCODE{index = 0}
423 \bsWHILE{read\_boola(follow\_context(index,context\_prob\_set) )== \false}{\ref{binandcontext}}
424 \bsCODE{value \ll = 1}
425 \bsIF{read\_boola(context\_prob\_set[DATA]) == \true}
426 \bsCODE{value += 1}
427 \bsEND
428 \bsCODE{index += 1}
429 \bsEND
430 \bsCODE{value -= 1}
431 \bsRET{value}
432 \end{pseudo}
434 \paragraph{Signed integer decoding}
435 $\ $\newline
436 $read\_sinta()$ decodes first the magnitude then the sign, as necessary:
438 \begin{pseudo}{read\_sinta}{context\_prob\_set}
439 \bsCODE{value=read\_uinta(context\_prob\_set)}
440 \bsIF{value != 0}
441 \bsIF{read\_boola(context\_prob\_set[SIGN]) == \true}
442 \bsCODE{value=-value}
443 \bsEND
444 \bsEND
445 \bsRET{value}
446 \end{pseudo}