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