xref: /aosp_15_r20/external/libavc/common/ih264_cavlc_tables.c (revision 495ae853bb871d1e5a258cb02c2cc13cde8ddb9a)
1 /******************************************************************************
2  *
3  * Copyright (C) 2015 The Android Open Source Project
4  *
5  * Licensed under the Apache License, Version 2.0 (the "License");
6  * you may not use this file except in compliance with the License.
7  * You may obtain a copy of the License at:
8  *
9  * http://www.apache.org/licenses/LICENSE-2.0
10  *
11  * Unless required by applicable law or agreed to in writing, software
12  * distributed under the License is distributed on an "AS IS" BASIS,
13  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14  * See the License for the specific language governing permissions and
15  * limitations under the License.
16  *
17  *****************************************************************************
18  * Originally developed and contributed by Ittiam Systems Pvt. Ltd, Bangalore
19 */
20 
21 /**
22 ******************************************************************************
23 * @file
24 *  ih264_cavlc_tables.c
25 *
26 * @brief
27 *  This file contains H264 cavlc tables for encoding coeff_tokens, levels, total
28 *  zeros and runs before zeros
29 *
30 * @author
31 *  ittiam
32 *
33 * @remarks
34 *  none
35 *
36 ******************************************************************************
37 */
38 
39 /*****************************************************************************/
40 /* File Includes                                                             */
41 /*****************************************************************************/
42 
43 /* User include files */
44 #include "ih264_typedefs.h"
45 #include "ih264_cavlc_tables.h"
46 
47 
48 /*****************************************************************************/
49 /* Extern global definitions                                                 */
50 /*****************************************************************************/
51 
52 /**
53  ******************************************************************************
54  * @brief  Assignment of cbp to a codenum for intra and inter prediction modes
55  * chroma format idc != 0
56  * input  : cbp, intra - 0/inter - 1
57  * output : codenum
58  * @remarks Table 9-4 - Assignment of codeNum to values of coded_block_pattern
59  * for macroblock prediction modes in H264 spec
60  ******************************************************************************
61  */
62 const UWORD8 gu1_cbp_map_tables[48][2]=
63 {
64     { 3,  0},   {29,  2},   {30,  3},   {17,  7},   {31,  4},   {18,  8},   {37, 17},   { 8, 13},
65     {32,  5},   {38, 18},   {19,  9},   { 9, 14},   {20, 10},   {10, 15},   {11, 16},   { 2, 11},
66     {16,  1},   {33, 32},   {34, 33},   {21, 36},   {35, 34},   {22, 37},   {39, 44},   { 4, 40},
67     {36, 35},   {40, 45},   {23, 38},   { 5, 41},   {24, 39},   { 6, 42},   { 7, 43},   { 1, 19},
68     {41,  6},   {42, 24},   {43, 25},   {25, 20},   {44, 26},   {26, 21},   {46, 46},   {12, 28},
69     {45, 27},   {47, 47},   {27, 22},   {13, 29},   {28, 23},   {14, 30},   {15, 31},   { 0, 12},
70 };
71 
72 
73 /**
74  ******************************************************************************
75  * @brief  total non-zero coefficients and numbers of trailing ones of a residual
76  * block are mapped to coeff_token using the tables given below.
77  * input  : VLC-Num  | Trailing ones | Total coeffs
78  * output : coeff_token (code word, size of the code word)
79  * @remarks Table-9-5 coeff_token mapping to TotalCoeff( coeff_token )
80  * and TrailingOnes( coeff_token ) in H264 spec
81  ******************************************************************************
82  */
83 const UWORD8 gu1_code_coeff_token_table[3][4][16] =
84 {
85     {
86         { 5,  7,  7,  7,  7, 15, 11,  8, 15, 11, 15, 11, 15, 11,  7,  4, },
87         { 1,  4,  6,  6,  6,  6, 14, 10, 14, 10, 14, 10,  1, 14, 10,  6, },
88         { 0,  1,  5,  5,  5,  5,  5, 13,  9, 13,  9, 13,  9, 13,  9,  5, },
89         { 0,  0,  3,  3,  4,  4,  4,  4,  4, 12, 12,  8, 12,  8, 12,  8, },
90     },
91     {
92         {11,  7,  7,  7,  4,  7, 15, 11, 15, 11,  8, 15, 11,  7,  9,  7, },
93         { 2,  7, 10,  6,  6,  6,  6, 14, 10, 14, 10, 14, 10, 11,  8,  6, },
94         { 0,  3,  9,  5,  5,  5,  5, 13,  9, 13,  9, 13,  9,  6, 10,  5, },
95         { 0,  0,  5,  4,  6,  8,  4,  4,  4, 12,  8, 12, 12,  8,  1,  4, },
96     },
97     {
98         {15, 11,  8, 15, 11,  9,  8, 15, 11, 15, 11,  8, 13,  9,  5,  1, },
99         {14, 15, 12, 10,  8, 14, 10, 14, 14, 10, 14, 10,  7, 12,  8,  4, },
100         { 0, 13, 14, 11,  9, 13,  9, 13, 10, 13,  9, 13,  9, 11,  7,  3, },
101         { 0,  0, 12, 11, 10,  9,  8, 13, 12, 12, 12,  8, 12, 10,  6,  2, },
102     },
103 };
104 
105 const UWORD8 gu1_size_coeff_token_table[3][4][16] =
106 {
107     {
108         { 6,  8,  9, 10, 11, 13, 13, 13, 14, 14, 15, 15, 16, 16, 16, 16, },
109         { 2,  6,  8,  9, 10, 11, 13, 13, 14, 14, 15, 15, 15, 16, 16, 16, },
110         { 0,  3,  7,  8,  9, 10, 11, 13, 13, 14, 14, 15, 15, 16, 16, 16, },
111         { 0,  0,  5,  6,  7,  8,  9, 10, 11, 13, 14, 14, 15, 15, 16, 16, },
112     },
113     {
114         { 6,  6,  7,  8,  8,  9, 11, 11, 12, 12, 12, 13, 13, 13, 14, 14, },
115         { 2,  5,  6,  6,  7,  8,  9, 11, 11, 12, 12, 13, 13, 14, 14, 14, },
116         { 0,  3,  6,  6,  7,  8,  9, 11, 11, 12, 12, 13, 13, 13, 14, 14, },
117         { 0,  0,  4,  4,  5,  6,  6,  7,  9, 11, 11, 12, 13, 13, 13, 14, },
118     },
119     {
120         { 6,  6,  6,  7,  7,  7,  7,  8,  8,  9,  9,  9, 10, 10, 10, 10, },
121         { 4,  5,  5,  5,  5,  6,  6,  7,  8,  8,  9,  9,  9, 10, 10, 10, },
122         { 0,  4,  5,  5,  5,  6,  6,  7,  7,  8,  8,  9,  9, 10, 10, 10, },
123         { 0,  0,  4,  4,  4,  4,  4,  5,  6,  7,  8,  8,  9, 10, 10, 10, },
124     },
125 };
126 const UWORD8 gu1_code_coeff_token_table_chroma[4][4] =
127 {
128     { 7,  4,  3,  2, },
129     { 1,  6,  3,  3, },
130     { 0,  1,  2,  2, },
131     { 0,  0,  5,  0, },
132 };
133 
134 const UWORD8 gu1_size_coeff_token_table_chroma[4][4] =
135 {
136     { 6,  6,  6,  6, },
137     { 1,  6,  7,  8, },
138     { 0,  3,  7,  8, },
139     { 0,  0,  6,  7, },
140 };
141 
142 /**
143  ******************************************************************************
144  * @brief  After encoding the current Level, to encode the next level, the choice
145  * of VLC table needs to be updated. The update is carried basing on a set of thresholds.
146  * These thresholds are listed in the table below for lookup.
147  * input  : suffix_length
148  * output : threshold
149  ******************************************************************************
150  */
151 const UWORD8 gu1_threshold_vlc_level[6] =
152 {
153     0, 3, 6, 12, 24, 48
154 };
155 
156 
157 /**
158  ******************************************************************************
159  * @brief  table for encoding total number of zeros
160  * input  : coeff_token, total zeros
161  * output : code word, size of the code word
162  * @remarks Table-9-7, 9-8  total_zeros tables for 4x4 blocks with
163  * TotalCoeff( coeff_token ) in H264 spec
164  ******************************************************************************
165  */
166 const UWORD8 gu1_size_zero_table[135] =
167 {
168      1, 3, 3, 4, 4, 5, 5, 6, 6, 7, 7, 8, 8, 9, 9, 9,
169      3, 3, 3, 3, 3, 4, 4, 4, 4, 5, 5, 6, 6, 6, 6,
170      4, 3, 3, 3, 4, 4, 3, 3, 4, 5, 5, 6, 5, 6,
171      5, 3, 4, 4, 3, 3, 3, 4, 3, 4, 5, 5, 5,
172      4, 4, 4, 3, 3, 3, 3, 3, 4, 5, 4, 5,
173      6, 5, 3, 3, 3, 3, 3, 3, 4, 3, 6,
174      6, 5, 3, 3, 3, 2, 3, 4, 3, 6,
175      6, 4, 5, 3, 2, 2, 3, 3, 6,
176      6, 6, 4, 2, 2, 3, 2, 5,
177      5, 5, 3, 2, 2, 2, 4,
178      4, 4, 3, 3, 1, 3,
179      4, 4, 2, 1, 3,
180      3, 3, 1, 2,
181      2, 2, 1,
182      1, 1,
183 };
184 const UWORD8 gu1_code_zero_table[135] =
185 {
186      1, 3, 2, 3, 2, 3, 2, 3, 2, 3, 2, 3, 2, 3, 2, 1,
187      7, 6, 5, 4, 3, 5, 4, 3, 2, 3, 2, 3, 2, 1, 0,
188      5, 7, 6, 5, 4, 3, 4, 3, 2, 3, 2, 1, 1, 0,
189      3, 7, 5, 4, 6, 5, 4, 3, 3, 2, 2, 1, 0,
190      5, 4, 3, 7, 6, 5, 4, 3, 2, 1, 1, 0,
191      1, 1, 7, 6, 5, 4, 3, 2, 1, 1, 0,
192      1, 1, 5, 4, 3, 3, 2, 1, 1, 0,
193      1, 1, 1, 3, 3, 2, 2, 1, 0,
194      1, 0, 1, 3, 2, 1, 1, 1,
195      1, 0, 1, 3, 2, 1, 1,
196      0, 1, 1, 2, 1, 3,
197      0, 1, 1, 1, 1,
198      0, 1, 1, 1,
199      0, 1, 1,
200      0, 1,
201 };
202 const UWORD8 gu1_size_zero_table_chroma[9] =
203 {
204      1, 2, 3, 3,
205      1, 2, 2,
206      1, 1,
207 };
208 const UWORD8 gu1_code_zero_table_chroma[9] =
209 {
210      1, 1, 1, 0,
211      1, 1, 0,
212      1, 0,
213 };
214 
215 /**
216  ******************************************************************************
217  * @brief  index to access zero table (look up)
218  * input  : TotalCoeff( coeff_token )
219  * output : index to access zero table
220  ******************************************************************************
221  */
222 const UWORD8 gu1_index_zero_table[15] =
223 {
224     0,  16,  31,  45,  58,  70,  81,  91, 100, 108, 115, 121, 126, 130, 133,
225 };
226 
227 /**
228  ******************************************************************************
229  * @brief  table for encoding runs of zeros before
230  * input  : zeros left, runs of zeros before
231  * output : code word, size of the code word
232  * @remarks Table-9-10  table for run_before in H264 spec
233  ******************************************************************************
234  */
235 const UWORD8 gu1_size_run_table[42] =
236 {
237       1,  1,
238       1,  2,  2,
239       2,  2,  2,  2,
240       2,  2,  2,  3,  3,
241       2,  2,  3,  3,  3,  3,
242       2,  3,  3,  3,  3,  3,  3,
243       3,  3,  3,  3,  3,  3,  3,  4,  5,  6,  7,  8,  9, 10, 11,
244 };
245 const UWORD8 gu1_code_run_table[42] =
246 {
247       1,  0,
248       1,  1,  0,
249       3,  2,  1,  0,
250       3,  2,  1,  1,  0,
251       3,  2,  3,  2,  1,  0,
252       3,  0,  1,  3,  2,  5,  4,
253       7,  6,  5,  4,  3,  2,  1,  1,  1,  1,  1,  1,  1,  1,  1,
254 };
255 
256 /**
257  ******************************************************************************
258  * @brief  index to access zero table (look up)
259  * input  : TotalCoeff( coeff_token )
260  * output : index to access zero table
261  ******************************************************************************
262  */
263 const UWORD8 gu1_index_run_table[7] =
264 {
265     0,  2,  5,  9,  14,  20,  27,
266 };
267