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 * @file
23 *  ih264e_globals.c
24 *
25 * @brief
26 *  Contains definitions of global variables used across the encoder
27 *
28 * @author
29 *  ittiam
30 *
31 * @par List of functions
32 *
33 *
34 * @remarks
35 *
36 *******************************************************************************
37 */
38 
39 /*****************************************************************************/
40 /* File Includes                                                             */
41 /*****************************************************************************/
42 
43 /* User include files */
44 #include "ih264_typedefs.h"
45 #include "ih264_defs.h"
46 #include "ih264e_defs.h"
47 #include "ih264e_globals.h"
48 
49 /*****************************************************************************/
50 /* Extern global definitions                                                 */
51 /*****************************************************************************/
52 
53 /**
54 ******************************************************************************
55 * @brief  lamda for varying quantizer scales that would be used to
56 * compute the RD cost while deciding on the MB modes.
57 * input  : qp
58 * output : lambda
59 * @remarks lambda = 0.85 * pow(2, (qp - 12)/3), when SSD is used as metric
60 * for computing distortion (Bit rate estimation for cost function of H.264/
61 * AVC by Mohd Golam Sarwer et. al.)  If the use of distortion metric is SAD
62 * rather than SSD in the stage of encoding, consider sqrt(lambda) simply to
63 * adjust lambda for the lack of squaring operation in the error computation
64 * (from rate distortion optimization for video compression by sullivan).
65 ******************************************************************************
66 */
67 const UWORD16 gu2_qp_lambda[52]=
68 {
69        0,      0,      0,      0,      0,      0,      0,      1,
70        1,      1,      1,      1,      1,      1,      1,      1,
71        1,      2,      2,      2,      2,      3,      3,      3,
72        4,      4,      5,      5,      6,      7,      7,      8,
73        9,     10,     12,     13,     15,     17,     19,     21,
74       23,     26,     30,     33,     37,     42,     47,     53,
75       59,     66,     74,     83,
76 };
77 
78 /**
79 ******************************************************************************
80 * @brief  Lamda for varying quantizer scales that would be used to
81 * compute the RD cost while deciding on the MB modes.
82 * input  : qp
83 * output : lambda
84 * @remarks lambda = pow(2, (qp - 12)/6)
85 ******************************************************************************
86 */
87 const UWORD8 gu1_qp0[52]=
88 {
89        0,      0,      0,      0,      0,      0,      0,      0,
90        0,      0,      0,      0,      1,      1,      1,      1,
91        2,      2,      2,      2,      3,      3,      3,      4,
92        4,      4,      5,      6,      6,      7,      8,      9,
93       10,     11,     13,     14,     16,     18,     20,     23,
94       25,     29,     32,     36,     40,     45,     51,     57,
95       64,     72,     81,     91,
96 };
97 
98 /**
99 ******************************************************************************
100 * @brief  unsigned exp. goulumb codelengths to assign cost to a coefficient of
101 * mb types.
102 * input  : Integer
103 * output : codelength
104 * @remarks Refer sec. 9-1 in h264 specification
105 ******************************************************************************
106 */
107 const UWORD8 u1_uev_codelength[32] =
108 {
109      1,      3,      3,      5,      5,      5,      5,      7,
110      7,      7,      7,      7,      7,      7,      7,      9,
111      9,      9,      9,      9,      9,      9,      9,      9,
112      9,      9,      9,      9,      9,      9,      9,      11,
113 };
114 
115 
116 /**
117 ******************************************************************************
118 * @brief  Look up table to assign cost to a coefficient of a residual block
119 * basing on its surrounding coefficients
120 * input  : Numbers of T1's
121 * output : coeff_cost
122 * @remarks Refer Section 2.3 Elimination of single coefficients in inter
123 * macroblocks in document JVT-O079
124 ******************************************************************************
125 */
126 const UWORD8 gu1_coeff_cost[6] =
127 {
128      3, 2, 2, 1, 1, 1
129 };
130 
131 /**
132 ******************************************************************************
133 * @brief  Indices map to raster scan for luma 4x4 block
134 * input  : scan index
135 * output : scan location
136 * @remarks None
137 ******************************************************************************
138 */
139 const UWORD8 gu1_luma_scan_order[16] =
140 {
141      0,  1,  4,  8,  5,  2,  3,  6,  9,  12, 13, 10, 7,  11, 14, 15
142 };
143 
144 /**
145 ******************************************************************************
146 * @brief  Indices map to raster scan for chroma AC block
147 * input  : scan index
148 * output : scan location
149 * @remarks None
150 ******************************************************************************
151 */
152 const UWORD8 gu1_chroma_scan_order[15] =
153 {
154      1,  4,  8,  5,  2,  3,  6,  9,  12, 13, 10, 7,  11, 14, 15
155 };
156 
157 /**
158 ******************************************************************************
159 * @brief  Indices map to raster scan for luma 4x4 dc block
160 * input  : scan index
161 * output : scan location
162 * @remarks : None
163 ******************************************************************************
164 */
165 const UWORD8 gu1_luma_scan_order_dc[16] =
166 {
167      0, 1,  4,  8,  5,  2,  3,  6,  9,  12, 13, 10, 7,  11, 14, 15
168 };
169 
170 /**
171 ******************************************************************************
172 * @brief  Indices map to raster scan for chroma 2x2 dc block
173 * input  : scan index
174 * output : scan location
175 * @remarks None
176 ******************************************************************************
177 */
178 const UWORD8 gu1_chroma_scan_order_dc[4] =
179 {
180      0, 1,  2,  3
181 };
182 
183 /**
184 ******************************************************************************
185 * @brief  choice of motion vectors to be used during mv prediction
186 * input  : formatted reference idx comparison metric
187 * output : mv prediction has to be median or a simple straight forward selec
188 * tion from neighbors.
189 * @remarks If only one of the candidate blocks has a reference frame equal to
190     the current block then use the same block as the final predictor. A simple
191     look up table to assist this mv prediction condition
192 ******************************************************************************
193 */
194 const WORD8 gi1_mv_pred_condition[8] =
195 {
196      -1,    0,    1,    -1,    2,    -1,    -1,    -1
197 };
198 
199 
200 /*******************************************************************************
201  * Translation of MPEG QP to H264 QP
202  ******************************************************************************/
203 /*
204  * Note : RC library models QP and bits assuming the QP to be MPEG2.
205  *        Since MPEG qp varies linearly, when the relationship is computed,
206  *        it learns that delta(qp) => delta(bits). Now what we are doing by the
207  *        transation of qp is that
208  *              QPrc = a + b*2^(QPen)
209  *        By not considering the weight matrix in both MPEG and H264 we in effect
210  *        only changing the relation to
211  *              QPrc = c + d*2^(QPen)
212  *        This will only entatil changin the RC model parameters, and this will
213  *        not affect rc relation at all
214  *
215  *
216  * We have MPEG qp which varies from 0-228. The quantization factor has a linear
217  * relation ship with the size of quantized values
218  *
219  * We also have H264 Qp, which varies such that for a change in QP of 6 , we
220  * double the corresponding scaling factor. Hence the scaling is linear in terms
221  * of 2^(QPh/6)
222  *
223  * Now we want to have translation between QPm and QPh. Hence we can write
224  *
225  * QPm = a + b*2^(QPh/6)
226  *
227  * Appling boundary condition that
228  *      1) QPm = 0.625 if QPh = 0
229  *      2) QPm =   224 if QPh = 51,
230  *
231  * we will have
232  *  a = 0.0063, b = 0.6187
233  *
234  * Hence the relatiohship is
235  *  QPm = a + b*2^(Qph/6)
236  *  QPh = 6*log((Qpm - a)/b)
237  *
238  *
239  * Unrounded values for gau1_h264_to_mpeg2_qmap[H264_QP_ELEM] =
240  *
241  *   0.625       0.70077     0.78581     0.88127     0.98843     1.10870
242  *   1.24370     1.39523     1.56533     1.75625     1.97055     2.21110
243  *   2.48110     2.78417     3.12435     3.50620     3.93480     4.41589
244  *   4.95590     5.56204     6.24241     7.00609     7.86330     8.82548
245  *   9.90550     11.11778    12.47851    14.00588    15.72030    17.64467
246  *   19.80470    22.22925    24.95072    28.00547    31.43430    35.28304
247  *   39.60310    44.45221    49.89514    56.00463    62.86230    70.55978
248  *   79.19990    88.89811    99.78398    112.00296   125.71830   141.11325
249  *   158.39350   177.78992   199.56167   223.99963
250  *
251  *
252  *
253  * Unrounded values for gau1_mpeg2_to_h264_qmap[MPEG2_QP_ELEM]
254  *
255  *   0         4.1014    10.1288   13.6477   16.1425   18.0768   19.6568
256  *   20.9925   22.1493   23.1696   24.0822   24.9078   25.6614   26.3546
257  *   26.9964   27.5938   28.1527   28.6777   29.1726   29.6408   30.0850
258  *   30.5074   30.9102   31.2951   31.6636   32.0171   32.3567   32.6834
259  *   32.9983   33.3021   33.5957   33.8795   34.1544   34.4208   34.6793
260  *   34.9303   35.1742   35.4114   35.6423   35.8671   36.0863   36.3001
261  *   36.5087   36.7124   36.9115   37.1060   37.2963   37.4825   37.6648
262  *   37.8433   38.0182   38.1896   38.3577   38.5226   38.6844   38.8433
263  *   38.9993   39.1525   39.3031   39.4511   39.5966   39.7397   39.8804
264  *   40.0189   40.1553   40.2895   40.4217   40.5518   40.6801   40.8065
265  *   40.9310   41.0538   41.1749   41.2943   41.4121   41.5283   41.6430
266  *   41.7561   41.8678   41.9781   42.0870   42.1946   42.3008   42.4057
267  *   42.5094   42.6118   42.7131   42.8132   42.9121   43.0099   43.1066
268  *   43.2023   43.2969   43.3905   43.4831   43.5747   43.6653   43.7550
269  *   43.8438   43.9317   44.0187   44.1049   44.1901   44.2746   44.3582
270  *   44.4411   44.5231   44.6044   44.6849   44.7647   44.8438   44.9221
271  *   44.9998   45.0767   45.1530   45.2286   45.3035   45.3779   45.4515
272  *   45.5246   45.5970   45.6689   45.7401   45.8108   45.8809   45.9504
273  *   46.0194   46.0878   46.1557   46.2231   46.2899   46.3563   46.4221
274  *   46.4874   46.5523   46.6166   46.6805   46.7439   46.8069   46.8694
275  *   46.9314   46.9930   47.0542   47.1150   47.1753   47.2352   47.2947
276  *   47.3538   47.4125   47.4708   47.5287   47.5862   47.6433   47.7001
277  *   47.7565   47.8125   47.8682   47.9235   47.9785   48.0331   48.0874
278  *   48.1413   48.1949   48.2482   48.3011   48.3537   48.4060   48.4580
279  *   48.5097   48.5611   48.6122   48.6629   48.7134   48.7636   48.8135
280  *   48.8631   48.9124   48.9615   49.0102   49.0587   49.1069   49.1549
281  *   49.2026   49.2500   49.2972   49.3441   49.3908   49.4372   49.4834
282  *   49.5293   49.5750   49.6204   49.6656   49.7106   49.7553   49.7998
283  *   49.8441   49.8882   49.9320   49.9756   50.0190   50.0622   50.1051
284  *   50.1479   50.1904   50.2327   50.2749   50.3168   50.3585   50.4000
285  *   50.4413   50.4825   50.5234   50.5641   50.6047   50.6450   50.6852
286  *   50.7252   50.7650   50.8046   50.8440   50.8833   50.9224   50.9613
287  *   51.0000
288  */
289 
290 const UWORD8 gau1_h264_to_mpeg2_qmap[H264_QP_ELEM] =
291 {
292      1,    1,    1,    1,   1,    1,    1,   1,
293      2,    2,    2,    2,   2,    3,    3,   4,
294      4,    4,    5,    6,   6,    7,    8,   9,
295      10,   11,   12,   14,  16,   18,   20,  22,
296      25,   28,   31,   35,  40,   44,   50,  56,
297      63,   71,   79,   89,  100,  112,  126, 141,
298      158,  178,  200,  224
299 };
300 
301 const UWORD8 gau1_mpeg2_to_h264_qmap[MPEG2_QP_ELEM] =
302 {
303      0,    4,    10,  14,   16,   18,  20,  21,
304      22,   23,   24,  25,   26,   26,  27,  28,
305      28,   29,   29,  30,   30,   31,  31,  31,
306      32,   32,   32,  33,   33,   33,  34,  34,
307      34,   34,   35,  35,   35,   35,  36,  36,
308      36,   36,   37,  37,   37,   37,  37,  37,
309      38,   38,   38,  38,   38,   39,  39,  39,
310      39,   39,   39,  39,   40,   40,  40,  40,
311      40,   40,   40,  41,   41,   41,  41,  41,
312      41,   41,   41,  42,   42,   42,  42,  42,
313      42,   42,   42,  42,   43,   43,  43,  43,
314      43,   43,   43,  43,   43,   43,  43,  44,
315      44,   44,   44,  44,   44,   44,  44,  44,
316      44,   44,   45,  45,   45,   45,  45,  45,
317      45,   45,   45,  45,   45,   45,  45,  46,
318      46,   46,   46,  46,   46,   46,  46,  46,
319      46,   46,   46,  46,   46,   46,  47,  47,
320      47,   47,   47,  47,   47,   47,  47,  47,
321      47,   47,   47,  47,   47,   47,  48,  48,
322      48,   48,   48,  48,   48,   48,  48,  48,
323      48,   48,   48,  48,   48,   48,  48,  48,
324      49,   49,   49,  49,   49,   49,  49,  49,
325      49,   49,   49,  49,   49,   49,  49,  49,
326      49,   49,   49,  49,   49,   50,  50,  50,
327      50,   50,   50,  50,   50,   50,  50,  50,
328      50,   50,   50,  50,   50,   50,  50,  50,
329      50,   50,   50,  50,   51,   51,  51,  51,
330      51,   51,   51,  51,   51,   51,  51,  51,
331      51
332 };
333 
334