Include a pre-rendered pdf version
[dirac-spec-errata.git] / spec-conventions.tex
blob520826c823dce1a4aa40ac1d2336f0abb79f7888
1 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
2 % - This chapter defines specification - %
3 % - conventions - %
4 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
5 \label{spec-conventions}
6 \subsection{State representation}
8 This standard uses a state model to express parsing and decoding operations.
9 The state of the decoder/parser shall be stored in the variable state. Individual elements of the decoder $\StateName$ (state variables) may be accessed
10 by means of
11 named labels, e.g. $\StateName[\text{VAR\_NAME}]$ (i.e. state is a map, as defined in Section \ref{datatypes}).
13 The decoder state shall be globally accessible within the decoder. Other
14 variables, not declared as inputs to a process, shall be local to that process.
15 The parsing and decoding operations are defined in terms of modifying the
16 decoder state. The state variables need not directly correspond to elements
17 of the stream, but may be calculated from them taking into account the decoder
18 state as a whole. For example, a state variable value may be differentially
19 encoded with respect to another value, with the difference, not the variable
20 itself, encoded in the stream.
22 \begin{comment}
23 The Dirac stream syntax structure is illustrated with informative parse diagrams,
24 contained in Annex \ref{parsediagrams},
25 that complement the normative stream syntax definitions.
26 \end{comment}
27 The parsing process
28 is defined by means of pseudocode and/or mathematical formulae. The
29 conventions for these elements are described in the following sections.
31 \subsection{Number formats}
32 \label{mathnotation}
34 Numbers without a prefix shall be interpreted as decimal numbers.
36 The prefix b indicates that the following value shall be interpreted as a binary
37 natural number (non-negative integer).
39 {\bf Example} The value b1110100 is equal to the decimal value 116.
41 The prefix 0x shall indicate that the following value is to be interpreted as a hexadecimal (base 16) natural number.
43 {\bf Example} The value 0x7A is equal to the decimal value 122.
45 \subsection{Data types}
46 \label{datatypes}
48 \subsubsection{Elementary data types}
50 Three basic types are used in the pseudo code:
51 \begin{description}
52 \item[Boolean] - A Boolean variable that has only two possible values: $\true$ and $\false$.
53 \item[Integer] - A positive or negative whole number or zero.
54 \item[Label] - a unique immutable value used in control structures and to
55 access maps (see below).
56 \end{description}
58 \subsubsection{Compound data types}
60 Elementary and compound data types may be aggregated into a single
61 compound data type.
62 There are three compound data type:
64 \begin{description}
65 \item[Set]
66 A collection of data types. A set is indicated by enclosing the elements within
67 curly braces, for example $\{a,b,c\}$ represents a set containing the values $a,b$
68 and $c$. An empty set may be indicated by $\{\}$. The usual set-theoretic
69 operations such as: $\cup$ (union), $\cap$ (intersection), $\in$ (membership)
70 apply to sets and the other compound data types.
72 \item[Map] A set of data types whose elements are accessed by their
73 corresponding label. For example, $p[Y] ,p[C1] ,p[C2] $ might be the values
74 of the different video components of a pixel. The set of labels corresponding to
75 the elements of a map $m$ can be accessed by $\args(m)$, so that, for example, $args(p)$ returns$\{Y,C1,C2\}$.
76 \item[Array] A collection of data types accessed by a non-negative integer index.
77 This compound data type is typically used to represent an array of variables. Elements of a 1-dimensional array $a$ are accessed by $a[n]$ for $n$
78 in the range 0 to $\length(a) - 1$.
79 \end{description}
81 A compound data type may contain other compound data types. For example,
82 a two dimensional array is an array of one dimensional arrays. Elements of a 2-dimensional array are accessed by $a[n][m]$ for $0\leq m\leq(\width(a)-1)$,
83 and $0\leq n\leq (\height(a)-1)$. Compound data types may be more complex.
84 For example, picture data, pic, may be considered to be a map of arrays, where $pic[Y]$ is a 2-dimensional array storing luma data, and $pic[C1]$ and $pic[C2]$
85 are two-dimensional arrays storing chroma data.
87 Elements may be added to a map or array by assignment using the appropriate
88 index (label or integer). For example, $a[7]=2$, adds element 7 to the array $a$,
89 if a does not already contain element 7, then this element is assigned the value 2.
91 \subsection{Functions and operators}
92 \label{functionoperators}
94 This section defines the functions and operators used
95 in the pseudocode in this specification. Functions and operators
96 are similar but functions use the syntax, $(arg1, arg2,\ldots)$
97 whereas operators are simply placed before or between operands,
98 e.g. $a+b$. The difference is purely syntactic and is to
99 correspond with conventional mathematical notation.
101 \subsubsection{Assignment}
103 The assignment operation = applies to all variable types. After performing
104 \[a=b\]
105 the value of $a$ shall become equal to that of $b$, and the value of $b$ shall remain unchanged. For a set (or map or array) this constitutes an element-wise copy
106 i.e.
107 \[a[x]=b[x]\]
108 for all valid values of $x$.
110 \subsubsection{Boolean functions and operators}
111 \label{booleanops}
112 The following functions and operators are defined for one or more Boolean arguments:
113 \begin{description}
114 \item[not] (not a) or returns $\true$ for a boolean value $a$ if and only if
115 $a$ is $\false$
116 \item[and] (a and b) returns $\true$ if and only if a and b are both $\true$. Operator "and" may be used in pseudo-code conditions to denote the logical AND between Boolean values, for example: if (condition1 and condition2): …etc.
117 \item[or] (a or b) returns True if either a or b are True, else it returns False. Operator "or" may be used in pseudo-code conditions to denote the logical OR between Boolean values, for example: if (condition1 or condition2): … etc.
118 \item[majority] Given a set, $S =\{s_0,…, s_{n-1}\}$ of Boolean values, $\majority(S)$ returns the majority condition. That is, if the number of $\true$
119 values is greater than or equal to the number of $\false$ values, $majority(S)$
120 returns $\true$, otherwise it returns $\false$.
121 \end{description}
123 Boolean operations are to be distinguished from bitwise operations which operate on non-negative
124 integer values, and are defined in Section \ref{integerops}.
126 \subsubsection{Integer functions and operators}
127 \label{integerops}
128 The following functions and operators are defined on integer values:
130 \begin{description}
131 \item[Absolute value] $|a|=\left\lbrace\begin{array}{l} a \text{ if $a\geq 0$}\\
132 -a \text{ otherwise} \end{array}\right.$.
134 \item[Sign] $\sign(a)$ is defined by
135 \[\sign(a)=
136 \left\{\begin{array}{l}
137 1 \text{ if $a>0$} \\
138 0 \text{ if $a==0$}\\
139 -1 \text{ if $a<0$}
140 \end{array}\right.\]
142 \item[Addition] The sum of $a$ and $b$ is represented by $a+b$.
144 \item[Subtraction] $a$ minus $b$ is represented by $a-b$.
146 \item[Multiplication] $a$ times $b$ is represented, for clarity, by $a*b$.
148 \item[Integer division] Integer division is defined for integer values $a,b$ with
149 $b>0$ where: $n=a//b$ is defined to be the largest integer $n$ such that
150 \[n*b\leq a\]
152 i.e. numbers are rounded towards -infinity. N.B. this differs from C/C++ conventions
153 of round towards 0.
155 \item[Remainder] For integers $a,b$, with $b>0$, the remainder $a\%b$ is
156 equal to
158 \[a\%b = a-(a//b)*b \]
160 $a\%b$ always lies between 0 and $b-1$.
162 \item[Exponentiation] For integers $a, b$, $b>0$ $a^b$ is defined as $a*a*\hdots *a$ ($b$ times). $a^0$ is 1.
164 \item[Maximum] $\max(a,b)$ returns the largest of $a$ and $b$.
166 \item[Minimum] $\min(a,b)$ returns the smallest of values $a$ and $b$.
168 \item[Clip] $\clip(a,b,t)$ clips the value $a$ to the range defined by $b$ (bottom)
169 and $t$ (top):
170 \[\clip(a,b,t)=\min(\max(a,b),t)\]
172 \item[Shift down] For integers $a,b$, with $b\geq 0$, $a\gg b$ is defined as
173 $a//2^b$.
175 \item[Shift up] For integers $a,b$, with $b\geq 0$, $a\ll b$ is defined as $a*2^b$.
177 \item[Integer logarithm] $m=\intlog2(n)$, for $n>0$, $m$ is the integer such that
178 $2^{m-1}<n\leq 2^m$.
181 \item[Mean] Given a set $S=\{s_0, s_1, \hdots, s_{n-1}\}$ of integer values, the integer unbiased mean, $\mean(S)$, is defined
182 to be
184 \[(s_0+s_1+\hdots +s_{n-1}+(n//2))//n\]
186 \item[Median] Given a set $S=\{s_0, s_1, \hdots, s_{n-1} \}$ of integer values the median, $\median(S)$,
187 returns the middle value. If $t_0\leq t_1\leq \hdots \leq t_{n-1}$ are the values $s_i$ placed in ascending order, this
190 $t_{(n-1)/2}$
192 if $n$ is odd and
194 $\mean(\{ t_{(n-2)/2},t_{n/2}\})$ if $n$ is even. If $S=\emptyset$, $\median(S)$ returns 0.
195 \end{description}
196 The following bitwise operations are defined on non-negative integer values:
197 \begin{description}
198 \item[\&] Logical AND is applied between the corresponding bits in the binary representation of two numbers, e.g.
199 $13\&6$ is $\text{b1101}\&\text{b110}$, which equals $\text{b100}$, or 4.
201 \item[${\mathbf |}$] Logical OR is applied between the corresponding bits in the binary representation of two numbers, e.g.
202 $13|6$ is $\text{b1101}\text{|}\text{b110}$, which equals $\text{b1111}$, or 15.
204 \item[${\mathbf \wedge}$] Logical XOR is applied between the corresponding bits in the binary representation of two numbers, e.g.
205 $13\wedge 6$ is $\text{b1101}\wedge\text{b110}$, which equals $\text{b1011}$, or 11.
207 \item[$\mathbf{\&=}$] $a \&= b$ is equivalent to $a = (a \& b)$.
208 \item[$\mathbf{|=}$] $a |= b$ is equivalent to $a = (a | b)$.
209 \item[$\mathbf{\wedge=}$] $a\wedge= b$ is equivalent to $a = (a \wedge b)$.
210 \end{description}
212 Bitwise-not is not defined for integers to avoid ambiguity concerning leading
213 zeroes
215 The following logical operators are defined for integer and boolean arguments:
216 \begin{description}
217 \item[==] Test of equality of two variables. $a == b$ is $\true$ if and only if the
218 value of a equals the value of b.
219 \item[!=] Not equal to. $a != b$ is equivalent to not $(a == b)$
220 \end{description}
222 The following logical operators are defined for integer arguments only:
223 \begin{description}
224 \item[$\mathbf{<}$] Less than
225 \item[$\mathbf{<=}$] Less than or equal to
226 \item[$\mathbf{>}$] Greater than
227 \item[$\mathbf{>=}$] Greater than or equal to
229 \end{description}
231 The following combined assignment operators are defined for integer
232 arguments:
233 Operators $+, -, *, //, \%, \gg, \ll, \&, |, \wedge$, may be combined with the assignment operator (as for the Boolean operators $\&$, $|$, and $\wedge$
234 above). For example $a += b$ is equivalent to $a = (a + b)$.
236 \subsubsection{Array and map functions and operators}
238 The following functions and operators are defined for sets, maps and arrays.
239 \begin{description}
240 \item[Indexing] For an array $a$, $a[index]$ returns an element of $a$. If $a$ is a map the index shall be a label, else if $a$ is an array the index shall be an integer.
241 \item[Scalar Assignment] Where the notation $a = 0$ is used for an array of integer values, it means "set all elements of the array to zero".
242 \item[Insertion] $a[index] = b$ inserts a copy of $b$ into set $a$
243 if the element does not already exist.
244 \item[Tokens] for a map $a$, $\args(a)$ returns the set of the indexing tokens.
245 \item[Length] for a one dimensional array $a$, $\length(a)$
246 returns the number of elements in the array.
247 \item[Width] for a two dimensional array $a$, $\width(a)$ returns the
248 width the array. The width is the number of scalar elements corresponding to the right most array index.
249 \item[Height] for a two dimensional array $a$, $\height(a)$ returns
250 the height the array. The height is the number of one dimensional arrays in the two dimensional array and the "height" dimension corresponds to the left most array index.
251 \end{description}
252 \subsubsection{Precedence and associativity of operators}
253 \label{operatorprecedence}
254 To avoid any confusion over the order of operator precedence, every equation makes extensive use of the expression operators "(" and ")". All operations recursively execute the innermost expression(s) first until the calculation has been completed. In cases where the expression operators do not make clear the order of precedence, the following table defines the descending order of operator precedence and the associativity of each operator.
255 [Table tbc]
256 \begin{comment}
257 Operator Precedence Associativity
258 ( ) [ ] left to right
259 * // % left to right
260 + - left to right
261 << >> left to right
262 < <= > >= left to right
263 == != left to right
264 ! (not) right to left
265 & (and) left to right
266 ^ (xor) left to right
267 | left to right
268 = += -= *= //= %= &= ^= |= <<= >>= right to left
269 \end{comment}
271 \subsection{Pseudocode}
272 \label{pseudocode}
274 Most of the normative specification is defined by means of pseudocode.
275 The syntax is intended to be both precise and descriptive; the pseudocode is
276 not intended to form the basis for the implementation of a Dirac decoder.
278 All processing defined by this standard is precise and the entire specification
279 can be implemented using only the data types, functions and operators
280 defined herein. That is, no operations on "real" or "floating point" numbers
281 are required. All operations shall be implemented with sufficiently large
282 integers so that overflow cannot occur.
284 The type of variables in the pseudocode is not explicitly declared.
285 A variable assumes a type when it is assigned a value, which shall always
286 have a defined type.
288 \subsubsection{Processes and functions}
289 \label{functionsprocesses}
291 Decoding and parsing operations are specified by means of processes
292 -- a series of operations acting on input data and global variable data.
293 A process can also be a function, which means it returns a value, but
294 it need not do so. So a process
295 taking in variables $in1$ and $in2$ looks like:
297 \begin{pseudo}{foo}{in1, in2}
298 \bsCODE{op1(in1)}
299 \bsCODE{op2(in2)}
300 \bsCODE{\hdots}
301 \end{pseudo}
303 Whilst a function process looks like:
305 \begin{pseudo}{bar}{in1, in2}
306 \bsCODE{op1(in1)}
307 \bsCODE{foo(in1,in2)}{\ref{functionsprocesses}}
308 \bsCODE{\hdots}
309 \bsRET{out1}
310 \end{pseudo}
312 The right-hand column in the pseudocode representation contains a cross-reference to the
313 section in the specification containing the definition of other processes used at that line.
315 \subsubsection{Variables}
317 All input variables are deemed to be passed {\em by reference} in this
318 specification. This means that any modification to a variable value that
319 occurs within a process also applies to that variable within the calling process
320 {\em even if it has a different name} in the calling process. One way to understand
321 this is to envisage variable names as pointers to workspace memory.
323 For example, if we define $foo$ and $bar$ by
325 \begin{pseudo}{foo}{}
326 \bsCODE{num=0}
327 \bsCODE{bar(num)}
328 \bsCODE{\StateName[var\_name]=num}
329 \end{pseudo}
331 and
333 \begin{pseudo}{bar}{val}
334 \bsCODE{val=val+1}
335 \end{pseudo}
337 then at the end of $foo$, $\StateName[var\_name]$ has been set to 1.
339 The only global variables are the state variables encapsulated in $\StateName$.
340 If a variable is not declared as an input to
341 the process and is not a state variable, then it is local to the function.
343 If a process is particularly complex, it may be broken into a number of steps with
344 intermediate discussion. This is signalled by appending and prepending ``$\hdots$" to
345 the parts of the pseudocode specification:
347 \begin{pseudo}{foo}{}
348 \bsCODE{code}
349 \bsCODE{\hdots}
350 \end{pseudo}
352 [text]
354 \begin{pseudo*}
355 \bsCODE{more code}
356 \bsCODE{\hdots}
357 \end{pseudo*}
359 [text]
361 \begin{pseudo*}
362 \bsCODE{even more code}
363 \end{pseudo*}
365 The intervening text may define or modify variables used in the succeeding
366 pseudocode, and must be considered as a normative part of the specification of the process.
367 This is done as it is sometimes much more clear to split up a long and complicated process
368 into a number of steps.
370 \subsubsection{Control flow}
371 \label{controlflow}
373 The pseudocode comprises a series of statements, linked by functions and
374 flow control statements such as {\bf if}, {\bf while}, and {\bf for}.
376 The statements do not have a termination character, unlike the ; in C
377 for example. Blocks of statements are indicated by indentation:
378 indenting in begins a block, indenting out ends one.
380 Statements that expect a block (and hence a following indentation) end
381 in a colon.
383 \paragraph*{if}
385 The if control evaluates a boolean or boolean function, and if true, passes the
386 flow to the block of following statement or block of statements. If the control
387 evaluates as false, then there is an option to include one or more else if
388 controls which offer alternative responses if some other condition is
389 true. If none of the preceding controls evaluate to true, then there is
390 the option to include an else control which catches remaining cases.
392 \begin{pseudo*}
393 \bsIF{control1}
394 \bsCODE{block1}
395 \bsELSEIF{control2}
396 \bsCODE{block2}
397 \bsELSEIF{control3}
398 \bsCODE{block3}
399 \bsELSE
400 \bsCODE{block4}
401 \bsEND
402 \end{pseudo*}
404 The if and else if conditions are evaluated in the order in which they
405 are presented. In particular, if $control1$ or $control2$ is true in
406 the preceding example, $block3$ will not be executed
407 even if $control3$ is true; neither will $block4$.
409 \paragraph*{for}
411 The for control repeats a loop over an integer range of values. For example,
413 \begin{pseudo*}
414 \bsFOR{i=0}{n-1}
415 \bsCODE{foo(i)}
416 \bsEND
417 \end{pseudo*}
419 calls $foo()$ with value $i$, as $i$ steps through from 0 to $n-1$ inclusive.
422 \paragraph*{for each} The for each control loops over the elements in
423 a list:
425 \begin{pseudo*}
426 \bsFOREACH{c}{Y,C1,C2}
427 \bsCODE{block}
428 \bsEND
429 \end{pseudo*}
431 \paragraph*{for such that} The for such that control loops over elements in
432 a set which satisfy some condition:
434 \begin{pseudo*}
435 \bsFORSUCH{a\in A}{control}
436 \bsCODE{block}
437 \bsEND
438 \end{pseudo*}
440 This may only be used when the order in which elements are processed is
441 immaterial.
443 \paragraph*{while}
445 The while control repeats a loop so long as a switch variable is true.
446 When it is false, the loop breaks to the next statement(s) outside the block.
448 \begin{pseudo*}
449 \bsWHILE{condition}
450 \bsCODE{block1}
451 \bsEND
452 \bsCODE{block2}
453 \end{pseudo*}