1 /*
2  * Copyright (c) 2017, Alliance for Open Media. All rights reserved
3  *
4  * This source code is subject to the terms of the BSD 2 Clause License and
5  * the Alliance for Open Media Patent License 1.0. If the BSD 2 Clause License
6  * was not distributed with this source code in the LICENSE file, you can
7  * obtain it at www.aomedia.org/license/software. If the Alliance for Open
8  * Media Patent License 1.0 was not distributed with this source code in the
9  * PATENTS file, you can obtain it at www.aomedia.org/license/patent.
10  */
11 #include "aom/aom_integer.h"
12 #include "av1/common/onyxc_int.h"
13 #include "av1/common/txb_common.h"
14 
15 const int16_t av1_coeff_band_4x4[16] = { 0, 1, 2,  3,  4,  5,  6,  7,
16                                          8, 9, 10, 11, 12, 13, 14, 15 };
17 
18 const int16_t av1_coeff_band_8x8[64] = {
19   0,  1,  2,  2,  3,  3,  4,  4,  5,  6,  2,  2,  3,  3,  4,  4,
20   7,  7,  8,  8,  9,  9,  10, 10, 7,  7,  8,  8,  9,  9,  10, 10,
21   11, 11, 12, 12, 13, 13, 14, 14, 11, 11, 12, 12, 13, 13, 14, 14,
22   15, 15, 16, 16, 17, 17, 18, 18, 15, 15, 16, 16, 17, 17, 18, 18,
23 };
24 
25 const int16_t av1_coeff_band_16x16[256] = {
26   0,  1,  4,  4,  7,  7,  7,  7,  8,  8,  8,  8,  9,  9,  9,  9,  2,  3,  4,
27   4,  7,  7,  7,  7,  8,  8,  8,  8,  9,  9,  9,  9,  5,  5,  6,  6,  7,  7,
28   7,  7,  8,  8,  8,  8,  9,  9,  9,  9,  5,  5,  6,  6,  7,  7,  7,  7,  8,
29   8,  8,  8,  9,  9,  9,  9,  10, 10, 10, 10, 11, 11, 11, 11, 12, 12, 12, 12,
30   13, 13, 13, 13, 10, 10, 10, 10, 11, 11, 11, 11, 12, 12, 12, 12, 13, 13, 13,
31   13, 10, 10, 10, 10, 11, 11, 11, 11, 12, 12, 12, 12, 13, 13, 13, 13, 10, 10,
32   10, 10, 11, 11, 11, 11, 12, 12, 12, 12, 13, 13, 13, 13, 14, 14, 14, 14, 15,
33   15, 15, 15, 16, 16, 16, 16, 17, 17, 17, 17, 14, 14, 14, 14, 15, 15, 15, 15,
34   16, 16, 16, 16, 17, 17, 17, 17, 14, 14, 14, 14, 15, 15, 15, 15, 16, 16, 16,
35   16, 17, 17, 17, 17, 14, 14, 14, 14, 15, 15, 15, 15, 16, 16, 16, 16, 17, 17,
36   17, 17, 18, 18, 18, 18, 19, 19, 19, 19, 20, 20, 20, 20, 21, 21, 21, 21, 18,
37   18, 18, 18, 19, 19, 19, 19, 20, 20, 20, 20, 21, 21, 21, 21, 18, 18, 18, 18,
38   19, 19, 19, 19, 20, 20, 20, 20, 21, 21, 21, 21, 18, 18, 18, 18, 19, 19, 19,
39   19, 20, 20, 20, 20, 21, 21, 21, 21,
40 };
41 
42 const int16_t av1_coeff_band_32x32[1024] = {
43   0,  1,  4,  4,  7,  7,  7,  7,  10, 10, 10, 10, 10, 10, 10, 10, 11, 11, 11,
44   11, 11, 11, 11, 11, 12, 12, 12, 12, 12, 12, 12, 12, 2,  3,  4,  4,  7,  7,
45   7,  7,  10, 10, 10, 10, 10, 10, 10, 10, 11, 11, 11, 11, 11, 11, 11, 11, 12,
46   12, 12, 12, 12, 12, 12, 12, 5,  5,  6,  6,  7,  7,  7,  7,  10, 10, 10, 10,
47   10, 10, 10, 10, 11, 11, 11, 11, 11, 11, 11, 11, 12, 12, 12, 12, 12, 12, 12,
48   12, 5,  5,  6,  6,  7,  7,  7,  7,  10, 10, 10, 10, 10, 10, 10, 10, 11, 11,
49   11, 11, 11, 11, 11, 11, 12, 12, 12, 12, 12, 12, 12, 12, 8,  8,  8,  8,  9,
50   9,  9,  9,  10, 10, 10, 10, 10, 10, 10, 10, 11, 11, 11, 11, 11, 11, 11, 11,
51   12, 12, 12, 12, 12, 12, 12, 12, 8,  8,  8,  8,  9,  9,  9,  9,  10, 10, 10,
52   10, 10, 10, 10, 10, 11, 11, 11, 11, 11, 11, 11, 11, 12, 12, 12, 12, 12, 12,
53   12, 12, 8,  8,  8,  8,  9,  9,  9,  9,  10, 10, 10, 10, 10, 10, 10, 10, 11,
54   11, 11, 11, 11, 11, 11, 11, 12, 12, 12, 12, 12, 12, 12, 12, 8,  8,  8,  8,
55   9,  9,  9,  9,  10, 10, 10, 10, 10, 10, 10, 10, 11, 11, 11, 11, 11, 11, 11,
56   11, 12, 12, 12, 12, 12, 12, 12, 12, 13, 13, 13, 13, 13, 13, 13, 13, 14, 14,
57   14, 14, 14, 14, 14, 14, 15, 15, 15, 15, 15, 15, 15, 15, 16, 16, 16, 16, 16,
58   16, 16, 16, 13, 13, 13, 13, 13, 13, 13, 13, 14, 14, 14, 14, 14, 14, 14, 14,
59   15, 15, 15, 15, 15, 15, 15, 15, 16, 16, 16, 16, 16, 16, 16, 16, 13, 13, 13,
60   13, 13, 13, 13, 13, 14, 14, 14, 14, 14, 14, 14, 14, 15, 15, 15, 15, 15, 15,
61   15, 15, 16, 16, 16, 16, 16, 16, 16, 16, 13, 13, 13, 13, 13, 13, 13, 13, 14,
62   14, 14, 14, 14, 14, 14, 14, 15, 15, 15, 15, 15, 15, 15, 15, 16, 16, 16, 16,
63   16, 16, 16, 16, 13, 13, 13, 13, 13, 13, 13, 13, 14, 14, 14, 14, 14, 14, 14,
64   14, 15, 15, 15, 15, 15, 15, 15, 15, 16, 16, 16, 16, 16, 16, 16, 16, 13, 13,
65   13, 13, 13, 13, 13, 13, 14, 14, 14, 14, 14, 14, 14, 14, 15, 15, 15, 15, 15,
66   15, 15, 15, 16, 16, 16, 16, 16, 16, 16, 16, 13, 13, 13, 13, 13, 13, 13, 13,
67   14, 14, 14, 14, 14, 14, 14, 14, 15, 15, 15, 15, 15, 15, 15, 15, 16, 16, 16,
68   16, 16, 16, 16, 16, 13, 13, 13, 13, 13, 13, 13, 13, 14, 14, 14, 14, 14, 14,
69   14, 14, 15, 15, 15, 15, 15, 15, 15, 15, 16, 16, 16, 16, 16, 16, 16, 16, 17,
70   17, 17, 17, 17, 17, 17, 17, 18, 18, 18, 18, 18, 18, 18, 18, 19, 19, 19, 19,
71   19, 19, 19, 19, 20, 20, 20, 20, 20, 20, 20, 20, 17, 17, 17, 17, 17, 17, 17,
72   17, 18, 18, 18, 18, 18, 18, 18, 18, 19, 19, 19, 19, 19, 19, 19, 19, 20, 20,
73   20, 20, 20, 20, 20, 20, 17, 17, 17, 17, 17, 17, 17, 17, 18, 18, 18, 18, 18,
74   18, 18, 18, 19, 19, 19, 19, 19, 19, 19, 19, 20, 20, 20, 20, 20, 20, 20, 20,
75   17, 17, 17, 17, 17, 17, 17, 17, 18, 18, 18, 18, 18, 18, 18, 18, 19, 19, 19,
76   19, 19, 19, 19, 19, 20, 20, 20, 20, 20, 20, 20, 20, 17, 17, 17, 17, 17, 17,
77   17, 17, 18, 18, 18, 18, 18, 18, 18, 18, 19, 19, 19, 19, 19, 19, 19, 19, 20,
78   20, 20, 20, 20, 20, 20, 20, 17, 17, 17, 17, 17, 17, 17, 17, 18, 18, 18, 18,
79   18, 18, 18, 18, 19, 19, 19, 19, 19, 19, 19, 19, 20, 20, 20, 20, 20, 20, 20,
80   20, 17, 17, 17, 17, 17, 17, 17, 17, 18, 18, 18, 18, 18, 18, 18, 18, 19, 19,
81   19, 19, 19, 19, 19, 19, 20, 20, 20, 20, 20, 20, 20, 20, 17, 17, 17, 17, 17,
82   17, 17, 17, 18, 18, 18, 18, 18, 18, 18, 18, 19, 19, 19, 19, 19, 19, 19, 19,
83   20, 20, 20, 20, 20, 20, 20, 20, 21, 21, 21, 21, 21, 21, 21, 21, 22, 22, 22,
84   22, 22, 22, 22, 22, 23, 23, 23, 23, 23, 23, 23, 23, 24, 24, 24, 24, 24, 24,
85   24, 24, 21, 21, 21, 21, 21, 21, 21, 21, 22, 22, 22, 22, 22, 22, 22, 22, 23,
86   23, 23, 23, 23, 23, 23, 23, 24, 24, 24, 24, 24, 24, 24, 24, 21, 21, 21, 21,
87   21, 21, 21, 21, 22, 22, 22, 22, 22, 22, 22, 22, 23, 23, 23, 23, 23, 23, 23,
88   23, 24, 24, 24, 24, 24, 24, 24, 24, 21, 21, 21, 21, 21, 21, 21, 21, 22, 22,
89   22, 22, 22, 22, 22, 22, 23, 23, 23, 23, 23, 23, 23, 23, 24, 24, 24, 24, 24,
90   24, 24, 24, 21, 21, 21, 21, 21, 21, 21, 21, 22, 22, 22, 22, 22, 22, 22, 22,
91   23, 23, 23, 23, 23, 23, 23, 23, 24, 24, 24, 24, 24, 24, 24, 24, 21, 21, 21,
92   21, 21, 21, 21, 21, 22, 22, 22, 22, 22, 22, 22, 22, 23, 23, 23, 23, 23, 23,
93   23, 23, 24, 24, 24, 24, 24, 24, 24, 24, 21, 21, 21, 21, 21, 21, 21, 21, 22,
94   22, 22, 22, 22, 22, 22, 22, 23, 23, 23, 23, 23, 23, 23, 23, 24, 24, 24, 24,
95   24, 24, 24, 24, 21, 21, 21, 21, 21, 21, 21, 21, 22, 22, 22, 22, 22, 22, 22,
96   22, 23, 23, 23, 23, 23, 23, 23, 23, 24, 24, 24, 24, 24, 24, 24, 24,
97 };
98 
99 #if LV_MAP_PROB
av1_init_txb_probs(FRAME_CONTEXT * fc)100 void av1_init_txb_probs(FRAME_CONTEXT *fc) {
101   TX_SIZE tx_size;
102   int plane, ctx, level;
103 
104   // Update probability models for transform block skip flag
105   for (tx_size = 0; tx_size < TX_SIZES; ++tx_size) {
106     for (ctx = 0; ctx < TXB_SKIP_CONTEXTS; ++ctx) {
107       fc->txb_skip_cdf[tx_size][ctx][0] =
108           AOM_ICDF(128 * (aom_cdf_prob)fc->txb_skip[tx_size][ctx]);
109       fc->txb_skip_cdf[tx_size][ctx][1] = AOM_ICDF(32768);
110       fc->txb_skip_cdf[tx_size][ctx][2] = 0;
111     }
112   }
113 
114   for (plane = 0; plane < PLANE_TYPES; ++plane) {
115     for (ctx = 0; ctx < DC_SIGN_CONTEXTS; ++ctx) {
116       fc->dc_sign_cdf[plane][ctx][0] =
117           AOM_ICDF(128 * (aom_cdf_prob)fc->dc_sign[plane][ctx]);
118       fc->dc_sign_cdf[plane][ctx][1] = AOM_ICDF(32768);
119       fc->dc_sign_cdf[plane][ctx][2] = 0;
120     }
121   }
122 
123   // Update probability models for non-zero coefficient map and eob flag.
124   for (tx_size = 0; tx_size < TX_SIZES; ++tx_size) {
125     for (plane = 0; plane < PLANE_TYPES; ++plane) {
126       for (level = 0; level < NUM_BASE_LEVELS; ++level) {
127         for (ctx = 0; ctx < COEFF_BASE_CONTEXTS; ++ctx) {
128           fc->coeff_base_cdf[tx_size][plane][level][ctx][0] = AOM_ICDF(
129               128 * (aom_cdf_prob)fc->coeff_base[tx_size][plane][level][ctx]);
130           fc->coeff_base_cdf[tx_size][plane][level][ctx][1] = AOM_ICDF(32768);
131           fc->coeff_base_cdf[tx_size][plane][level][ctx][2] = 0;
132         }
133       }
134     }
135   }
136 
137   for (tx_size = 0; tx_size < TX_SIZES; ++tx_size) {
138     for (plane = 0; plane < PLANE_TYPES; ++plane) {
139       for (ctx = 0; ctx < SIG_COEF_CONTEXTS; ++ctx) {
140         fc->nz_map_cdf[tx_size][plane][ctx][0] =
141             AOM_ICDF(128 * (aom_cdf_prob)fc->nz_map[tx_size][plane][ctx]);
142         fc->nz_map_cdf[tx_size][plane][ctx][1] = AOM_ICDF(32768);
143         fc->nz_map_cdf[tx_size][plane][ctx][2] = 0;
144       }
145 
146       for (ctx = 0; ctx < EOB_COEF_CONTEXTS; ++ctx) {
147         fc->eob_flag_cdf[tx_size][plane][ctx][0] =
148             AOM_ICDF(128 * (aom_cdf_prob)fc->eob_flag[tx_size][plane][ctx]);
149         fc->eob_flag_cdf[tx_size][plane][ctx][1] = AOM_ICDF(32768);
150         fc->eob_flag_cdf[tx_size][plane][ctx][2] = 0;
151       }
152     }
153   }
154 
155   for (tx_size = 0; tx_size < TX_SIZES; ++tx_size) {
156     for (plane = 0; plane < PLANE_TYPES; ++plane) {
157       for (ctx = 0; ctx < LEVEL_CONTEXTS; ++ctx) {
158         fc->coeff_lps_cdf[tx_size][plane][ctx][0] =
159             AOM_ICDF(128 * (aom_cdf_prob)fc->coeff_lps[tx_size][plane][ctx]);
160         fc->coeff_lps_cdf[tx_size][plane][ctx][1] = AOM_ICDF(32768);
161         fc->coeff_lps_cdf[tx_size][plane][ctx][2] = 0;
162       }
163 #if BR_NODE
164       for (int br = 0; br < BASE_RANGE_SETS; ++br) {
165         for (ctx = 0; ctx < LEVEL_CONTEXTS; ++ctx) {
166           fc->coeff_br_cdf[tx_size][plane][br][ctx][0] = AOM_ICDF(
167               128 * (aom_cdf_prob)fc->coeff_br[tx_size][plane][br][ctx]);
168           fc->coeff_br_cdf[tx_size][plane][br][ctx][1] = AOM_ICDF(32768);
169           fc->coeff_br_cdf[tx_size][plane][br][ctx][2] = 0;
170         }
171       }
172 #endif  // BR_NODE
173     }
174   }
175 #if CONFIG_CTX1D
176   for (tx_size = 0; tx_size < TX_SIZES; ++tx_size) {
177     for (plane = 0; plane < PLANE_TYPES; ++plane) {
178       for (int tx_class = 0; tx_class < TX_CLASSES; ++tx_class) {
179         fc->eob_mode_cdf[tx_size][plane][tx_class][0] = AOM_ICDF(
180             128 * (aom_cdf_prob)fc->eob_mode[tx_size][plane][tx_class]);
181         fc->eob_mode_cdf[tx_size][plane][tx_class][1] = AOM_ICDF(32768);
182         fc->eob_mode_cdf[tx_size][plane][tx_class][2] = 0;
183       }
184     }
185   }
186   for (tx_size = 0; tx_size < TX_SIZES; ++tx_size) {
187     for (plane = 0; plane < PLANE_TYPES; ++plane) {
188       for (int tx_class = 0; tx_class < TX_CLASSES; ++tx_class) {
189         for (ctx = 0; ctx < EMPTY_LINE_CONTEXTS; ++ctx) {
190           fc->empty_line_cdf[tx_size][plane][tx_class][ctx][0] = AOM_ICDF(
191               128 *
192               (aom_cdf_prob)fc->empty_line[tx_size][plane][tx_class][ctx]);
193           fc->empty_line_cdf[tx_size][plane][tx_class][ctx][1] =
194               AOM_ICDF(32768);
195           fc->empty_line_cdf[tx_size][plane][tx_class][ctx][2] = 0;
196         }
197       }
198     }
199   }
200   for (tx_size = 0; tx_size < TX_SIZES; ++tx_size) {
201     for (plane = 0; plane < PLANE_TYPES; ++plane) {
202       for (int tx_class = 0; tx_class < TX_CLASSES; ++tx_class) {
203         for (ctx = 0; ctx < HV_EOB_CONTEXTS; ++ctx) {
204           fc->hv_eob_cdf[tx_size][plane][tx_class][ctx][0] = AOM_ICDF(
205               128 * (aom_cdf_prob)fc->hv_eob[tx_size][plane][tx_class][ctx]);
206           fc->hv_eob_cdf[tx_size][plane][tx_class][ctx][1] = AOM_ICDF(32768);
207           fc->hv_eob_cdf[tx_size][plane][tx_class][ctx][2] = 0;
208         }
209       }
210     }
211   }
212 #endif  // CONFIG_CTX1D
213 }
214 #endif  // LV_MAP_PROB
215 
av1_adapt_txb_probs(AV1_COMMON * cm,unsigned int count_sat,unsigned int update_factor)216 void av1_adapt_txb_probs(AV1_COMMON *cm, unsigned int count_sat,
217                          unsigned int update_factor) {
218   FRAME_CONTEXT *fc = cm->fc;
219   const FRAME_CONTEXT *pre_fc = cm->pre_fc;
220   const FRAME_COUNTS *counts = &cm->counts;
221   TX_SIZE tx_size;
222   int plane, ctx, level;
223 
224   // Update probability models for transform block skip flag
225   for (tx_size = 0; tx_size < TX_SIZES; ++tx_size)
226     for (ctx = 0; ctx < TXB_SKIP_CONTEXTS; ++ctx)
227       fc->txb_skip[tx_size][ctx] = mode_mv_merge_probs(
228           pre_fc->txb_skip[tx_size][ctx], counts->txb_skip[tx_size][ctx]);
229 
230   for (plane = 0; plane < PLANE_TYPES; ++plane)
231     for (ctx = 0; ctx < DC_SIGN_CONTEXTS; ++ctx)
232       fc->dc_sign[plane][ctx] = mode_mv_merge_probs(
233           pre_fc->dc_sign[plane][ctx], counts->dc_sign[plane][ctx]);
234 
235   // Update probability models for non-zero coefficient map and eob flag.
236   for (tx_size = 0; tx_size < TX_SIZES; ++tx_size)
237     for (plane = 0; plane < PLANE_TYPES; ++plane)
238       for (level = 0; level < NUM_BASE_LEVELS; ++level)
239         for (ctx = 0; ctx < COEFF_BASE_CONTEXTS; ++ctx)
240           fc->coeff_base[tx_size][plane][level][ctx] =
241               merge_probs(pre_fc->coeff_base[tx_size][plane][level][ctx],
242                           counts->coeff_base[tx_size][plane][level][ctx],
243                           count_sat, update_factor);
244 
245   for (tx_size = 0; tx_size < TX_SIZES; ++tx_size) {
246     for (plane = 0; plane < PLANE_TYPES; ++plane) {
247       for (ctx = 0; ctx < SIG_COEF_CONTEXTS; ++ctx) {
248         fc->nz_map[tx_size][plane][ctx] = merge_probs(
249             pre_fc->nz_map[tx_size][plane][ctx],
250             counts->nz_map[tx_size][plane][ctx], count_sat, update_factor);
251       }
252 
253       for (ctx = 0; ctx < EOB_COEF_CONTEXTS; ++ctx) {
254         fc->eob_flag[tx_size][plane][ctx] = merge_probs(
255             pre_fc->eob_flag[tx_size][plane][ctx],
256             counts->eob_flag[tx_size][plane][ctx], count_sat, update_factor);
257       }
258     }
259   }
260 
261   for (tx_size = 0; tx_size < TX_SIZES; ++tx_size) {
262     for (plane = 0; plane < PLANE_TYPES; ++plane) {
263       for (ctx = 0; ctx < LEVEL_CONTEXTS; ++ctx) {
264         fc->coeff_lps[tx_size][plane][ctx] = merge_probs(
265             pre_fc->coeff_lps[tx_size][plane][ctx],
266             counts->coeff_lps[tx_size][plane][ctx], count_sat, update_factor);
267       }
268 #if BR_NODE
269       for (int br = 0; br < BASE_RANGE_SETS; ++br) {
270         for (ctx = 0; ctx < LEVEL_CONTEXTS; ++ctx) {
271           fc->coeff_br[tx_size][plane][br][ctx] =
272               merge_probs(pre_fc->coeff_br[tx_size][plane][br][ctx],
273                           counts->coeff_br[tx_size][plane][br][ctx], count_sat,
274                           update_factor);
275         }
276       }
277 #endif  // BR_NODE
278     }
279   }
280 #if CONFIG_CTX1D
281   for (tx_size = 0; tx_size < TX_SIZES; ++tx_size) {
282     for (plane = 0; plane < PLANE_TYPES; ++plane)
283       for (int tx_class = 0; tx_class < TX_CLASSES; ++tx_class)
284         fc->eob_mode[tx_size][plane][tx_class] =
285             merge_probs(pre_fc->eob_mode[tx_size][plane][tx_class],
286                         counts->eob_mode[tx_size][plane][tx_class], count_sat,
287                         update_factor);
288   }
289   for (tx_size = 0; tx_size < TX_SIZES; ++tx_size) {
290     for (plane = 0; plane < PLANE_TYPES; ++plane)
291       for (int tx_class = 0; tx_class < TX_CLASSES; ++tx_class)
292         for (ctx = 0; ctx < EMPTY_LINE_CONTEXTS; ++ctx)
293           fc->empty_line[tx_size][plane][tx_class][ctx] =
294               merge_probs(pre_fc->empty_line[tx_size][plane][tx_class][ctx],
295                           counts->empty_line[tx_size][plane][tx_class][ctx],
296                           count_sat, update_factor);
297   }
298   for (tx_size = 0; tx_size < TX_SIZES; ++tx_size) {
299     for (plane = 0; plane < PLANE_TYPES; ++plane)
300       for (int tx_class = 0; tx_class < TX_CLASSES; ++tx_class)
301         for (ctx = 0; ctx < HV_EOB_CONTEXTS; ++ctx)
302           fc->hv_eob[tx_size][plane][tx_class][ctx] =
303               merge_probs(pre_fc->hv_eob[tx_size][plane][tx_class][ctx],
304                           counts->hv_eob[tx_size][plane][tx_class][ctx],
305                           count_sat, update_factor);
306   }
307 #endif
308 }
309 
av1_init_lv_map(AV1_COMMON * cm)310 void av1_init_lv_map(AV1_COMMON *cm) {
311   LV_MAP_CTX_TABLE *coeff_ctx_table = &cm->coeff_ctx_table;
312   for (int row = 0; row < 2; ++row) {
313     for (int col = 0; col < 2; ++col) {
314       for (int sig_mag = 0; sig_mag < 2; ++sig_mag) {
315         for (int count = 0; count < BASE_CONTEXT_POSITION_NUM + 1; ++count) {
316           coeff_ctx_table->base_ctx_table[row][col][sig_mag][count] =
317               get_base_ctx_from_count_mag(row, col, count, sig_mag);
318         }
319       }
320     }
321   }
322 }
323