1.什么是CAVLC?
Context Adaptive Variable Length Coding (CAVLC) is a method used to encode residual, scan ordered blocks of transform coefficients.
2.CAVLC是怎么工作?
- 對於量化后的4 * 4的block, CAVLC使用run-level來represent strings of zeros.
- Context Adaptive 的意思就是通過上下文自動調節的一種編碼,所以這邊CAVLC是通過附近4 * 4的block的情況來選擇自己的"VLC table", 用這個VLC talbe 來編碼 coeff_token.
- 對於高頻的值,如果取值為1,-1之類的。使用"Trailing 1s" or "T1s" 來表示. 0代表1, 1代表-1. 最多只能表示3個trailing ones.
- CAVLC編碼包括一下幾個步驟
| 1.編碼 coeff_token, coeff_token代表了有多少個trailing zeros, 以及有多少個no-zero coefficients |
|---|
| 2.編碼trailing ones, 代表了是+1 還是 -1 |
| 3.編碼non-zero coefficients 的值,從高頻到低頻 |
| 4.編碼最后一個none-zero coefficients 之前的所有的0的個數 |
| 5. 編碼每個none-zero coefficients 之前有多少個0 |
3.CAVLC工作流程圖

4. 編碼coeff_token 以及 VLC Table 的選擇
coeff_token必須使用VLC Table來查找對應的二進制.
coeff_token 編碼了 Toal-Coeffs 代表非 0 coefficient的個數,取值從0到16.同時也編碼了 Trailiing 1s(T1s). 從 0 到 3. 代表有多少個coefficient數據是1 或者 -1,從高頻到低頻排序.
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.
如果nCb不可以用,nCC = nCb.
如果nCa不可以用,nCC = nCa.
如果兩者都不可以用,nCC = 0;
根據nC,T1 ,numCoeff 編碼 coeff_token,如下圖



For ChromaDC.
| If chroma_format_idc is equal to 1, nC is set equal to -1 |
|---|
| Otherwise,if chroma_format_idc is equal to 2, nC is set equal to -2 |
| Otherwise (chroma_format_idc is equal to 3), nC is set equal to 0 |
![]() |
5. 編碼 trailing 1s 的符號位
順序為從高頻到低頻, 0 = +1, 1 = -1.
6. 編碼 level of remaing non-zero coefficients.
level 由兩個部分組成 prefix 和 suffix. 通俗的來說就是,prefix代表了高位的數據,suffix代表了低位的數據.
prefix總是以1結尾.

官方decoding代碼

Encoding level算法


encoding level算法的例子.
假設如下4*4block

Reordered block:−2, 4, 3, −3, 0, 0, −1, ...
TotalCoeffs = 5
TotalZeros = 2
T1s = 1
- encoding a = -3的時候.
step2: |a| = |a| - 1. a = -2;
|a| < 8, number of zeros = 2 * (|-2| - 1) + (1) = 3;
所以值就為 <0001><>
更新suffxLength = 1; - encoding a = 3的時候.
step3: |a| = |a| - 1, a = 2;
step5: 符號為正數, 所以suffix的最后一位為0.
step7: number of remaing zeros = 2;
所以值為<001><0> - encoding a = 4的時候.
step3: |a| = |a| - 1, a = 3;
step5:符號為正數, 所以suffix的最后一位為0.
step7: number of remaing zeros = 3;
值為<0001><0>
更新suffxLength = 2;
4.encoding a = -2
step5: suffix 最后一位,符號位為1.
step6: |a| - 1 = 1B(二進制的1). suffix就為<11>
step7: number of zeros = 0;
值為<1><11>
7.編碼 total number of zeros before the last coefficient
For Luma, Chroma DC (4:4:4) and Chroma AC, the (total_zeros, numCoeff) table

For Chroma DC (4:2:0), the code for total_zeros

for Chroma DC (4:2:2)

8.編碼 run before

需要注意的點
- CAVLC uses run-level coding to to compactly represent strings of zeros.
- if CAVLC is used with 8 * 8 interger transform, each 8 * 8 block of quantized transform coeffients is processed as 4 * 4 blocks for the purpose of CAVLC encoding and decoding.
資料來源
- The H.264 Advanced Video Compression Standards
- T-REC-H.264-200503-S!!PDF-E
- CAVLC (Context based Adaptive Variable Length Coding)

