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