package h264 // The CAVLC code tables of ITU-T H.264 §9.2 (Tables 9-5, 9-7, 9-8, 9-9 and // 9-10), as code lengths and code values. // coeffTokenLen and coeffTokenBits are coeff_token for 0≤nC<2, 2≤nC<4, // 4≤nC<8 and 8≤nC, at [TotalCoeff*4+TrailingOnes]. var coeffTokenLen = [4][4 * 17]uint8{ { 1, 0, 0, 0, 6, 2, 0, 0, 8, 6, 3, 0, 9, 8, 7, 5, 10, 9, 8, 6, 11, 10, 9, 7, 13, 11, 10, 8, 13, 13, 11, 9, 13, 13, 13, 10, 14, 14, 13, 11, 14, 14, 14, 13, 15, 15, 14, 14, 15, 15, 15, 14, 16, 15, 15, 15, 16, 16, 16, 15, 16, 16, 16, 16, 16, 16, 16, 16, }, { 2, 0, 0, 0, 6, 2, 0, 0, 6, 5, 3, 0, 7, 6, 6, 4, 8, 6, 6, 4, 8, 7, 7, 5, 9, 8, 8, 6, 11, 9, 9, 6, 11, 11, 11, 7, 12, 11, 11, 9, 12, 12, 12, 11, 12, 12, 12, 11, 13, 13, 13, 12, 13, 13, 13, 13, 13, 14, 13, 13, 14, 14, 14, 13, 14, 14, 14, 14, }, { 4, 0, 0, 0, 6, 4, 0, 0, 6, 5, 4, 0, 6, 5, 5, 4, 7, 5, 5, 4, 7, 5, 5, 4, 7, 6, 6, 4, 7, 6, 6, 4, 8, 7, 7, 5, 8, 8, 7, 6, 9, 8, 8, 7, 9, 9, 8, 8, 9, 9, 9, 8, 10, 9, 9, 9, 10, 10, 10, 10, 10, 10, 10, 10, 10, 10, 10, 10, }, { 6, 0, 0, 0, 6, 6, 0, 0, 6, 6, 6, 0, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, }, } var coeffTokenBits = [4][4 * 17]uint8{ { 1, 0, 0, 0, 5, 1, 0, 0, 7, 4, 1, 0, 7, 6, 5, 3, 7, 6, 5, 3, 7, 6, 5, 4, 15, 6, 5, 4, 11, 14, 5, 4, 8, 10, 13, 4, 15, 14, 9, 4, 11, 10, 13, 12, 15, 14, 9, 12, 11, 10, 13, 8, 15, 1, 9, 12, 11, 14, 13, 8, 7, 10, 9, 12, 4, 6, 5, 8, }, { 3, 0, 0, 0, 11, 2, 0, 0, 7, 7, 3, 0, 7, 10, 9, 5, 7, 6, 5, 4, 4, 6, 5, 6, 7, 6, 5, 8, 15, 6, 5, 4, 11, 14, 13, 4, 15, 10, 9, 4, 11, 14, 13, 12, 8, 10, 9, 8, 15, 14, 13, 12, 11, 10, 9, 12, 7, 11, 6, 8, 9, 8, 10, 1, 7, 6, 5, 4, }, { 15, 0, 0, 0, 15, 14, 0, 0, 11, 15, 13, 0, 8, 12, 14, 12, 15, 10, 11, 11, 11, 8, 9, 10, 9, 14, 13, 9, 8, 10, 9, 8, 15, 14, 13, 13, 11, 14, 10, 12, 15, 10, 13, 12, 11, 14, 9, 12, 8, 10, 13, 8, 13, 7, 9, 12, 9, 12, 11, 10, 5, 8, 7, 6, 1, 4, 3, 2, }, { 3, 0, 0, 0, 0, 1, 0, 0, 4, 5, 6, 0, 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, }, } // chromaDCTokenLen and chromaDCTokenBits are coeff_token for nC = -1. var chromaDCTokenLen = [4 * 5]uint8{ 2, 0, 0, 0, 6, 1, 0, 0, 6, 6, 3, 0, 6, 7, 7, 6, 6, 8, 8, 7, } var chromaDCTokenBits = [4 * 5]uint8{ 1, 0, 0, 0, 7, 1, 0, 0, 4, 6, 1, 0, 3, 3, 2, 5, 2, 3, 2, 0, } // totalZerosLen and totalZerosBits are total_zeros of a 4x4 block, at // [TotalCoeff-1][total_zeros]. var totalZerosLen = [15][16]uint8{ {1, 3, 3, 4, 4, 5, 5, 6, 6, 7, 7, 8, 8, 9, 9, 9}, {3, 3, 3, 3, 3, 4, 4, 4, 4, 5, 5, 6, 6, 6, 6}, {4, 3, 3, 3, 4, 4, 3, 3, 4, 5, 5, 6, 5, 6}, {5, 3, 4, 4, 3, 3, 3, 4, 3, 4, 5, 5, 5}, {4, 4, 4, 3, 3, 3, 3, 3, 4, 5, 4, 5}, {6, 5, 3, 3, 3, 3, 3, 3, 4, 3, 6}, {6, 5, 3, 3, 3, 2, 3, 4, 3, 6}, {6, 4, 5, 3, 2, 2, 3, 3, 6}, {6, 6, 4, 2, 2, 3, 2, 5}, {5, 5, 3, 2, 2, 2, 4}, {4, 4, 3, 3, 1, 3}, {4, 4, 2, 1, 3}, {3, 3, 1, 2}, {2, 2, 1}, {1, 1}, } var totalZerosBits = [15][16]uint8{ {1, 3, 2, 3, 2, 3, 2, 3, 2, 3, 2, 3, 2, 3, 2, 1}, {7, 6, 5, 4, 3, 5, 4, 3, 2, 3, 2, 3, 2, 1, 0}, {5, 7, 6, 5, 4, 3, 4, 3, 2, 3, 2, 1, 1, 0}, {3, 7, 5, 4, 6, 5, 4, 3, 3, 2, 2, 1, 0}, {5, 4, 3, 7, 6, 5, 4, 3, 2, 1, 1, 0}, {1, 1, 7, 6, 5, 4, 3, 2, 1, 1, 0}, {1, 1, 5, 4, 3, 3, 2, 1, 1, 0}, {1, 1, 1, 3, 3, 2, 2, 1, 0}, {1, 0, 1, 3, 2, 1, 1, 1}, {1, 0, 1, 3, 2, 1, 1}, {0, 1, 1, 2, 1, 3}, {0, 1, 1, 1, 1}, {0, 1, 1, 1}, {0, 1, 1}, {0, 1}, } // chromaDCZerosLen and chromaDCZerosBits are total_zeros of a chroma DC // block. var chromaDCZerosLen = [3][4]uint8{ {1, 2, 3, 3}, {1, 2, 2, 0}, {1, 1, 0, 0}, } var chromaDCZerosBits = [3][4]uint8{ {1, 1, 1, 0}, {1, 1, 0, 0}, {1, 0, 0, 0}, } // runLen and runBits are run_before, at [min(zerosLeft,7)-1][run_before]. var runLen = [7][15]uint8{ {1, 1}, {1, 2, 2}, {2, 2, 2, 2}, {2, 2, 2, 3, 3}, {2, 2, 3, 3, 3, 3}, {2, 3, 3, 3, 3, 3, 3}, {3, 3, 3, 3, 3, 3, 3, 4, 5, 6, 7, 8, 9, 10, 11}, } var runBits = [7][15]uint8{ {1, 0}, {1, 1, 0}, {3, 2, 1, 0}, {3, 2, 1, 1, 0}, {3, 2, 3, 2, 1, 0}, {3, 0, 1, 3, 2, 5, 4}, {7, 6, 5, 4, 3, 2, 1, 1, 1, 1, 1, 1, 1, 1, 1}, } // zigzag is the 4x4 frame scan: scan position → raster position // (row*4+column). var zigzag = [16]int{0, 1, 4, 8, 5, 2, 3, 6, 9, 12, 13, 10, 7, 11, 14, 15} // normAdjust is v of §8.5.9: for qP%6, a position with both coordinates // even, both odd, or one of each. var normAdjust = [6][3]int32{ {10, 16, 13}, {11, 18, 14}, {13, 20, 16}, {14, 23, 18}, {16, 25, 20}, {18, 29, 23}, } // quantMF is the encoder's multiplier, matched to normAdjust. var quantMF = [6][3]int32{ {13107, 5243, 8066}, {11916, 4660, 7490}, {10082, 4194, 6554}, {9362, 3647, 5825}, {8192, 3355, 5243}, {7282, 2893, 4559}, } // posClass is a raster position's column in normAdjust and quantMF. func posClass(pos int) int { r, c := pos>>2, pos&3 switch { case r&1 == 0 && c&1 == 0: return 0 case r&1 == 1 && c&1 == 1: return 1 } return 2 } // chromaQP is QPc for qPI ≥ 30 (Table 8-15). var chromaQP = [22]int{29, 30, 31, 32, 32, 33, 34, 34, 35, 35, 36, 36, 37, 37, 37, 38, 38, 38, 39, 39, 39, 39} func qpChroma(qp int) int { if qp < 30 { return qp } return chromaQP[qp-30] }