Professional Documents
Culture Documents
CAVLC Example
CAVLC Example
Introduction:
H.264, the latest video compression standard, uses CAVLC for encoding the coefficients
after quantization. CAVLC encodes the coefficients (the coefficients may be positive or
negative) into binary bit stream.
Original
Residue
-
DCT
Quantization
CAVLC
Predicted
NALU
In H.264 Encoder, the predicted submacroblock (of size 4x4) is subtracted from the
original submacroblock (of size 4x4), resulting the residue submacroblock (of size 4x4).
This residue is transformed by modified Discrete Cosine Transform. The transformed
residue is also of size 4x4. Then this transformed residue is quantized (dividing by some
defined number, QStep). This quantized-transformed residue is encoded as follows.
Encoding process:
There will be 16 coefficients in a 4x4 submacroblock. The total number of coefficients is
tot_coef = 16.
(0,0)
(1,0)
(2,0)
(3,0)
(0,1)
(1,1)
(2,1)
(3,1)
(0,2)
(1,2)
(2,2)
(3,2)
(0,3)
(1,3)
(2,3)
(3,3)
The 16 coefficients are taken in a zig-zag manner as shown in the above figure, and
written in an array.
(0,0) (0,1) (1,0) (2,0) (1,1) (0,2) (0,3) (1,2) (2,1) (3,0) (3,1) (2,2)
(1,3) (2,3) (3,2) (3,3)
Note: For Chroma coding, two different blocks will come for coding.
One is Chroma DC of 2x2 size (in case of 4:2:0), or 4x2 size (in case 4:2:2) or 4x4 size (in
case of 4:4:4). Here, tot_coef = 4.
Another one is Chroma AC of 4x4 size. But in this Chroma AC, the DC Coefficient is 0 at
(0,0). Here, tot_coef = 15.
The important parameters to be encoded.
1. Number of nonzero coefficients (numCoef) and Trailing Ones (T1)
2. The pattern of Trailing Ones (T1)
3. The nonzero coefficients (Levels)
4. Number of zeros embedded in the nonzero coefficients (Total_zeros)
5. The location of those embedded zeros (run_before)
1. Number of nonzero coefficients (numCoef) and Trailing Ones (T1)
CAVLC Encoder
Calculate number of nonzero coefficients (numCoeff). For this array (or sub
macroblock), allot nCPresent = numCoeff. [0 numCoeff tot_coef]
Remove the zero coefficients from the last, one by one, till the nonzero coefficient is
reached. This reduced array may have zeros in between the first and last nonzero
coefficients.
If the last nonzero coefficient is not equal to 1, then Trailing ones (T1) = 0. If the last
nonzero coefficients is equal to 1, then calculate the number of continuous ones (+1
or -1) from last nonzero coefficient 1 towards first nonzero coefficient. T1 includes
the last nonzero coefficient also, if the last nonzero coefficient is 1.
Restrict the value of T1 to 3. Maximum value of T1 = 3 i.e., consider only three 1s only
from the last nonzero coefficient inclusive. If there is any other 1 still, consider those
1 as normal numbers, not as T1s. So, now T1 is ready. i.e., [0 T1 3].
For Luma, Chroma DC (4:4:4) and Chroma AC
(T1, numCoeff) will be coded based on nCContext. nCContext = (nCA + nCB + 1) >> 1. nCA is the
number of nonzero coefficients available in the left 4x4 submacroblock and nCB is the
number of nonzero coefficients available in the up 4x4 submacroblock. If any of nCA or
nCB is not available, then nCContext = (nCA + nCB). Note that nCPresent has no role here.
Based on nCContext, the corresponding column will be selected in the Table 1. For (T1,
numCoeff), the code will be selected. If nCContext < 8, the codes will be variable length
codes. If nCContext > 7, the codes will be fixed length codes (6-bits). The first four bits of
fixed length code is (numCoeff 1) and the last two bits of fixed length code is T1. But
for T1 = 0 and numCoeff = 0, the code is different.
CAVLC Encoder
T1 numCoeff
0 nC < 2
0
0
1
0
1
000101
0
2
00000111
0
3
000000111
0
4
0000000111
0
5
00000000111
0
6
0000000001111
0
7
0000000001011
0
8
0000000001000
0
9
00000000001111
0
10
00000000001011
0
11
000000000001111
0
12
000000000001011
0
13
0000000000001111
0
14
0000000000001011
0
15
0000000000000111
0
16
0000000000000100
1
1
01
1
2
000100
1
3
00000110
1
4
000000110
1
5
0000000110
1
6
00000000110
1
7
0000000001110
1
8
0000000001010
1
9
00000000001110
1
10
00000000001010
1
11
000000000001110
1
12
000000000001010
1
13
000000000000001
1
14
0000000000001110
1
15
0000000000001010
1
16
0000000000000110
2
2
001
2
3
0000101
2
4
00000101
2
5
000000101
2
6
0000000101
2
7
00000000101
2
8
0000000001101
2
9
0000000001001
2
10
00000000001101
2
11
00000000001001
2 nC < 4
11
001011
000111
0000111
00000111
00000100
000000111
00000001111
00000001011
000000001111
000000001011
000000001000
0000000001111
0000000001011
0000000000111
00000000001001
00000000000111
10
00111
001010
000110
0000110
00000110
000000110
00000001110
00000001010
000000001110
000000001010
0000000001110
0000000001010
00000000001011
00000000001000
00000000000110
011
001001
000101
0000101
00000101
000000101
00000001101
00000001001
000000001101
000000001001
4 nC < 8
1111
001111
001011
001000
0001111
0001011
0001001
0001000
00001111
00001011
000001111
000001011
000001000
0000001101
0000001001
0000000101
0000000001
1110
01111
01100
01010
01000
001110
001010
0001110
00001110
00001010
000001110
000001010
000000111
0000001100
0000001000
0000000100
1101
01110
01011
01001
001101
001001
0001101
0001010
00001101
00001001
8 nC
000011
000000
000100
001000
001100
010000
010100
011000
011100
100000
100100
101000
101100
110000
110100
111000
111100
000001
000101
001001
001101
010001
010101
011001
011101
100001
100101
101001
101101
110001
110101
111001
111101
000110
001010
001110
010010
010110
011010
011110
100010
100110
101010
CAVLC Encoder
T1 numCoeff
0 nC < 2
2 nC < 4
4 nC < 8
8 nC
2
12
000000000001101 0000000001101 000001101 101110
2
13
000000000001001 0000000001001 000001001 110010
2
14
0000000000001101 0000000000110 0000001011 110110
2
15
0000000000001001 00000000001010 0000000111 111010
2
16
0000000000000101 00000000000101 0000000011 111110
3
3
00011
0101
1100
001011
3
4
000011
0100
1011
001111
3
5
0000100
00110
1010
010011
3
6
00000100
001000
1001
010111
3
7
000000100
000100
1000
011011
3
8
0000000100
0000100
01101
011111
3
9
00000000100
000000100
001100
100011
3
10
0000000001100
00000001100
0001100
100111
3
11
00000000001100
00000001000
00001100
101011
3
12
00000000001000
000000001100
00001000
101111
3
13
000000000001100 0000000001100 000001100 110011
3
14
000000000001000 0000000001000 0000001010 110111
3
15
0000000000001100 0000000000001 0000000110 111011
3
16
0000000000001000 00000000000100 0000000010 111111
Table 1 Code for (T1, numCoeff) for Luma coefficients and Chroma AC Coefficients
For Chroma DC
The Table 2 is assigned for Chroma DC Levels only. If Chroma Array Type is 1 (4:2:0),
then nC = -1 (3rd Column of Table 2). If Chroma Array Type is 2 (4:2:2), then nC = -2 (4th
Column of Table 2). If Chroma Array Type is 3 (4:4:4), then 3rd Column of Table 2 is
referred.
T1
0
0
0
0
0
0
0
0
0
1
1
1
1
1
1
num
Coeff
0
1
2
3
4
5
6
7
8
1
2
3
4
5
6
num
nC = -1
Coeff
1
7
01
1
1
8
000111
0001111
2
2
000100
0001110
001
2
3
000011
000000111
0000010
2
4
000010
000000110
00000010
2
5
0000000111
2
6
00000000111
2
7
000000000111
8
0000000000111 2
3
3
1
01
000101
3
4
000110
0001101
0000000
3
5
0000011 0001100
3
6
00000011 000000101
3
7
0000000110
3
8
00000000110
Table 2 Code for Chroma DC Levels
nC = -1
nC = -2
T1
nC = -2
000000000110
000000000101
001
0001011
0001010
000000100
0000000101
00000000101
000000000100
00001
000001
0001001
0001000
0000000100
00000000100
CAVLC Encoder
If (numCoeff > 10) and (T1 < 3), Suffixlength = 1 or else Suffixlength = 0
If (Suffixlength = 0) and (numCoeff 3 or T1 < 3), change |a| |a| 1 and
sign is same. Or else keep a same.
(i) If |a| < 8, there is no suffix. Prefix will be found for a. Prefix is <zeros 1>.
No. of zeros before 1 in prefix = 2 x (|a| 1) + sign. (If a < 0, sign = 1, else
sign = 0). Go to Step 13.
(ii) If |a| < 16, there is suffix of length, suffixlength = 4. The LSB of suffix =
sign bit. The remaining bits (3 bits) is (|a| 8). The prefix is <14 zeros
1>. Go to step 13.
(iii) If |a| > 15, there is <Prefix><Suffix>. Diff = |a| 16. Go to Step 9.
Step 3: Else (Suffixlength = 1), change |a| |a| 1 and sign is same. There will be
<prefix><suffix> = <zeros 1><suffix>.
Step 4: If (|a| 1) > 15 x 2suffixlength-1, Diff = (|a| 1) (15 x 2suffixlength-1), Then go to Step
9.
Step 5: If a is positive, the LSB of suffix = 0, or else LSB of suffix = 1
Step 6: If suffixlength > 1, the remaining bits of suffix = the (Suffixlength 1) LSBs of
(|a| 1)
Step 7: No. of zeros in prefix = value of remaining MSBs of (|a| 1)
Step 8: The code for present nonzero coefficient a is <prefix><suffix> ready. Go to
Step 13.
Step 9: Suffix length = 12 + (Diff >> 11) bits
Step 10: Prefix = < (15 + 2*(Diff >> 11)) zeros 1 >
Step 11: LSB of Suffix = sign bit of a
Step 12: Remaining bits of Suffix = Binary form of Diff (Right Aligned).
CAVLC Encoder
Step 13: Based on present nonzero coefficient a, set the next Suffixlength (Ref: Table
3)
Note 1: If the nonzero coefficient is first nonzero coefficient (other than
trailing ones), and if the present |a| > 3, then new suffixlength = 2.
Note 2: Else if the new Suffixlength = 2 and previous Suffixlength = 0, then
Suffixlength = new Suffixlength (i.e., 2)
Note 3: Else if the new Suffixlength > previous Suffixlength, Suffixlength will
be incremented (it will not be changed drastically).
Note 4: Else keep the same previous Suffixlength as new Suffixlength.
(Note that the previous Suffixlength means the Suffixlength calculated
previously in this step only, not in any other steps.)
Step 14: If any nonzero coefficient is available next (reverse reading!), read a and then
go to Step 4.
Step 15: Stop
Suffix length
to be set
0
0
1, 2, 3
1
4, 5, 6
2
7, 8, 9, 10, 11, 12
3
13 24
4
25 48
5
> 48
6
Table 3 Suffix length
Table 4 Total zeros table for 4x4 blocks for Chroma DC 2x2 block (4:2:0 subsampling)
CAVLC Encoder
1
0
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
1
011
010
0011
0010
00011
00010
000011
000010
0000011
0000010
00000011
00000010
000000011
000000010
000000001
10
00001
00000
001
11
10
01
0001
11
0000
0001
001
010
1
011
Table 6 Total zeros table for 4x4 blocks for Chroma DC 2x4 block (4:2:2 subsampling)
12
0000
0001
01
1
001
13
000
001
1
01
14 15
00 0
01 1
1
CAVLC Encoder
1
1
0
-
zerosLeft
2
3
4
5
1
11
11
11
01
10
10
10
00
01
01
011
00
001
010
000
001
000
Table 7 Tables for run_before
6
11
000
001
011
010
101
100
-
>6
111
110
101
100
011
010
001
0001
00001
000001
0000001
00000001
000000001
0000000001
00000000001
CAVLC Encoder
Example 1
1
0
1
0
0
1
1
0
0
0
0
0
Suffix
No
<1>
a = -4
Because a is negative, LSB of suffix = 1
Take 0 LSBs from |4| 1 = 3 = 112 and substitute it in Suffix before LSB.
After taking 0 LSBs, the remaining MSBs = 3
There will 3 zeros in prefix
Prefix
Suffix
<000><1> <1>
Coeff Level (5 bits) 3
Next Suffixlength = 2 (Ref. Table 3)
<00011>
CAVLC Encoder
10
a = -2
Because a is negative, LSB of suffix = 1
Take 1 LSBs from |2| 1 = 1 = 12 and substitute it in Suffix before LSB.
After taking 1 LSBs, the remaining MSBs = 0
There will 0 zeros in prefix
Prefix
<><1>
Suffix
<1><1>
<111>
a = 1
Because a is positive, LSB of suffix = 0
Take 1 LSBs from |1| 1 = 0 = 02 and substitute it in Suffix before LSB.
After taking 1 LSBs, the remaining MSBs = 0
There will 0 zeros in prefix
Prefix
<><1>
Suffix
<0><0>
<100>
a = 1
Because a is positive, LSB of suffix = 0
Take 1 LSBs from |1| 1 = 0 = 02 and substitute it in Suffix before LSB.
After taking 1 LSBs, the remaining MSBs = 0
There will 0 zeros in prefix
Prefix
<><1>
Suffix
<0><0>
<100>
<11>
<10>
<11>
<11>
<00>
CAVLC Encoder
11
Example 1
Decoding
<01101><000><1><00011><111><100><100><11><10><11><11><00>
nC(Left)=2 nC(Up)=9
selected in Table 9-5.
Matching the bit stream till longest prefix match with each entry in vlc2 column of Table
9-5, the unique code is obtained from bit stream which gives (T1 3, numcoeff 8)
<000><1><00011><111><100><100><11><10><11><11><00>
There are three trailing ones (T1 3). Decoding the next three bits, the last three
coefficients are obtained. Substitute +1 for 0 and -1 for 1.
1
<1><00011><111><100><100><11><10><11><11><00>
Default Suffixlength = 0, prefix = 1, so a = 1
1
<00011><111><100><100><11><10><11><11><00>
Suffixlength = 1
There are 3 zeros before 1. So |a| = 3 (No. of zeros) + (suffix>>1) + 1 = 4
From suffix, a is negative. So a = -4
-4
<111><100><100><11><10><11><11><00>
Suffixlength = 2
There is no zeros before 1. So |a| = 0 (No. of zeros) + (suffix>>1) + 1 = 0 + 1 + 1 = 2
From suffix LSB, a is negative. So a = 2
-2
-4
<100><100><11><10><11><11><00>
Suffixlength = 2
There is no zeros before 1. So |a| = 0 (No. of zeros) + (suffix>>1) + 1 = 0 + 0 + 1 = 1
From suffix LSB, a is positive. So a = 1
1
-2
-4
CAVLC Encoder
12
<100><11><10><11><11><00>
Suffixlength = 2
There is no zeros before 1. So |a| = 0 (No. of zeros) + (suffix>>1) + 1 = 0 + 0 + 1 = 1
From suffix LSB, a is positive. So a = 1
1
-2
-4
<11><10><11><11><00>
In column no. 8 (no. of coefficients) of Table 9-8, 11 is matching. So, total no. of zeros =
4
<10><11><11><00>
In column 4 (no. of zeros left), of Table 9-10, 10 is matching. So, one zero is present
1
-2
-4
<11><11><00>
In column 3 (no. of zeros left), of Table 9-10, 11 is matching. So, no zero is present
1
-2
-4
<11><00>
In column 3 (no. of zeros left), of Table 9-10, 11 is matching. So, no zero is present
1
-2
-4
<00>
In column 3 (no. of zeros left), of Table 9-10, 00 is matching. So, three zero is present
1
-2
-4