Line data Source code
1 : /*
2 : * Copyright(c) 2019 Intel Corporation
3 : * SPDX - License - Identifier: BSD - 2 - Clause - Patent
4 : */
5 :
6 : /*
7 : * Copyright (c) 2016, Alliance for Open Media. All rights reserved
8 : *
9 : * This source code is subject to the terms of the BSD 2 Clause License and
10 : * the Alliance for Open Media Patent License 1.0. If the BSD 2 Clause License
11 : * was not distributed with this source code in the LICENSE file, you can
12 : * obtain it at www.aomedia.org/license/software. If the Alliance for Open
13 : * Media Patent License 1.0 was not distributed with this source code in the
14 : * PATENTS file, you can obtain it at www.aomedia.org/license/patent.
15 : */
16 :
17 : #include <stdlib.h>
18 :
19 : #include "EbDefinitions.h"
20 : #include "EbMdRateEstimation.h"
21 :
22 : #include "EbBitstreamUnit.h"
23 :
24 2552 : static INLINE int32_t get_interinter_wedge_bits(BlockSize sb_type) {
25 2552 : const int32_t wbits = wedge_params_lookup[sb_type].bits;
26 2552 : return (wbits > 0) ? wbits + 1 : 0;
27 : }
28 :
29 : /**************************************************************
30 : * AV1GetCostSymbold
31 : * Calculate the cost of a symbol with
32 : * probability p15 / 2^15
33 : ***************************************************************/
34 13645600 : static INLINE int32_t av1_cost_symbol(AomCdfProb p15) {
35 : assert(0 < p15 && p15 < CDF_PROB_TOP);
36 13645600 : const int32_t shift = CDF_PROB_BITS - 1 - get_msb(p15);
37 13644200 : const int32_t prob = get_prob(p15 << shift, CDF_PROB_TOP);
38 : assert(prob >= 128);
39 13662800 : return av1_prob_cost[prob - 128] + av1_cost_literal(shift);
40 : }
41 :
42 : /*************************************************************
43 : * av1_get_syntax_rate_from_cdf
44 : **************************************************************/
45 4058590 : void av1_get_syntax_rate_from_cdf(
46 : int32_t *costs,
47 : const AomCdfProb *cdf,
48 : const int32_t *inv_map)
49 : {
50 : int32_t i;
51 4058590 : AomCdfProb prev_cdf = 0;
52 13695500 : for (i = 0;; ++i) {
53 13695500 : AomCdfProb p15 = AOM_ICDF(cdf[i]) - prev_cdf;
54 13695500 : p15 = (p15 < EC_MIN_PROB) ? EC_MIN_PROB : p15;
55 13695500 : prev_cdf = AOM_ICDF(cdf[i]);
56 :
57 13695500 : if (inv_map)
58 991165 : costs[inv_map[i]] = av1_cost_symbol(p15);
59 : else
60 12704400 : costs[i] = av1_cost_symbol(p15);
61 :
62 : // Stop once we reach the end of the CDF
63 13688800 : if (cdf[i] == AOM_ICDF(CDF_PROB_TOP)) break;
64 : }
65 4051850 : }
66 :
67 : ///tmp function to be removed once we have updated all syntax CDFs
68 3577 : void av1_estimate_syntax_rate___partial(
69 : MdRateEstimationContext *md_rate_estimation_array,
70 : FRAME_CONTEXT *fc)
71 : {
72 : int32_t i, j;
73 :
74 3577 : md_rate_estimation_array->initialized = 1;
75 : #if CABAC_UP1
76 : for (i = 0; i < PARTITION_CONTEXTS; ++i)
77 : av1_get_syntax_rate_from_cdf(md_rate_estimation_array->partitionFacBits[i], fc->partition_cdf[i], NULL);
78 : #endif
79 :
80 : #if CABAC_UP2
81 : //if (cm->skip_mode_flag) { // NM - Hardcoded to true
82 : for (i = 0; i < SKIP_CONTEXTS; ++i)
83 : av1_get_syntax_rate_from_cdf(md_rate_estimation_array->skipModeFacBits[i], fc->skip_mode_cdfs[i], NULL);
84 : //}
85 : #endif
86 :
87 17733 : for (i = TX_4X4; i < EXT_TX_SIZES; ++i) {
88 : int32_t s;
89 56940 : for (s = 1; s < EXT_TX_SETS_INTER; ++s) {
90 42711 : if (use_inter_ext_tx_for_txsize[s][i])
91 14262 : av1_get_syntax_rate_from_cdf(md_rate_estimation_array->inter_tx_type_fac_bits[s][i], fc->inter_ext_tx_cdf[s][i], av1_ext_tx_inv[av1_ext_tx_set_idx_to_type[1][s]]);
92 : }
93 42802 : for (s = 1; s < EXT_TX_SETS_INTRA; ++s) {
94 28646 : if (use_intra_ext_tx_for_txsize[s][i]) {
95 146927 : for (j = 0; j < INTRA_MODES; ++j)
96 136330 : av1_get_syntax_rate_from_cdf(md_rate_estimation_array->intra_tx_type_fac_bits[s][i][j], fc->intra_ext_tx_cdf[s][i][j], av1_ext_tx_inv[av1_ext_tx_set_idx_to_type[0][s]]);
97 : }
98 : }
99 : }
100 3436 : }
101 : #if FILTER_INTRA_FLAG
102 : int av1_filter_intra_allowed_bsize( uint8_t enable_filter_intra, BlockSize bs);
103 : #if !PAL_SUP
104 : int av1_filter_intra_allowed(uint8_t enable_filter_intra, BlockSize bsize, uint32_t mode);
105 : #endif
106 : #endif
107 : /*************************************************************
108 : * av1_estimate_syntax_rate()
109 : * Estimate the rate for each syntax elements and for
110 : * all scenarios based on the frame CDF
111 : **************************************************************/
112 120 : void av1_estimate_syntax_rate(
113 : MdRateEstimationContext *md_rate_estimation_array,
114 : EbBool is_i_slice,
115 : FRAME_CONTEXT *fc)
116 : {
117 : int32_t i, j;
118 :
119 120 : md_rate_estimation_array->initialized = 1;
120 :
121 2519 : for (i = 0; i < PARTITION_CONTEXTS; ++i)
122 2399 : av1_get_syntax_rate_from_cdf(md_rate_estimation_array->partition_fac_bits[i], fc->partition_cdf[i], NULL);
123 :
124 : //if (cm->skip_mode_flag) { // NM - Hardcoded to true
125 480 : for (i = 0; i < SKIP_CONTEXTS; ++i)
126 360 : av1_get_syntax_rate_from_cdf(md_rate_estimation_array->skip_mode_fac_bits[i], fc->skip_mode_cdfs[i], NULL);
127 : //}
128 :
129 480 : for (i = 0; i < SKIP_CONTEXTS; ++i)
130 360 : av1_get_syntax_rate_from_cdf(md_rate_estimation_array->skip_fac_bits[i], fc->skip_cdfs[i], NULL);
131 721 : for (i = 0; i < KF_MODE_CONTEXTS; ++i)
132 3596 : for (j = 0; j < KF_MODE_CONTEXTS; ++j)
133 2995 : av1_get_syntax_rate_from_cdf(md_rate_estimation_array->y_mode_fac_bits[i][j], fc->kf_y_cdf[i][j], NULL);
134 :
135 601 : for (i = 0; i < BlockSize_GROUPS; ++i)
136 480 : av1_get_syntax_rate_from_cdf(md_rate_estimation_array->mb_mode_fac_bits[i], fc->y_mode_cdf[i], NULL);
137 :
138 360 : for (i = 0; i < CFL_ALLOWED_TYPES; ++i) {
139 3356 : for (j = 0; j < INTRA_MODES; ++j)
140 3117 : av1_get_syntax_rate_from_cdf(md_rate_estimation_array->intra_uv_mode_fac_bits[i][j], fc->uv_mode_cdf[i][j], NULL);
141 : }
142 :
143 120 : av1_get_syntax_rate_from_cdf(md_rate_estimation_array->filter_intra_mode_fac_bits, fc->filter_intra_mode_cdf, NULL);
144 : #if FILTER_INTRA_FLAG
145 2760 : for (i = 0; i < BlockSizeS_ALL; ++i) {
146 2640 : if (av1_filter_intra_allowed_bsize(1,i))
147 1680 : av1_get_syntax_rate_from_cdf(md_rate_estimation_array->filter_intra_fac_bits[i], fc->filter_intra_cdfs[i], NULL);
148 : }
149 : #else
150 : // NM - To be added when intra filtering is adopted
151 : /*for (i = 0; i < BlockSizeS_ALL; ++i) {
152 : if (av1_filter_intra_allowed_bsize(cm, i))
153 : av1_FacBits_tokens_from_cdf(md_rate_estimation_array->filter_intra_fac_bits[i],
154 : fc->filter_intra_cdfs[i], NULL);
155 : }*/
156 :
157 : // NM - To be added when inter filtering is adopted
158 : #endif
159 2040 : for (i = 0; i < SWITCHABLE_FILTER_CONTEXTS; ++i)
160 1920 : av1_get_syntax_rate_from_cdf(md_rate_estimation_array->switchable_interp_fac_bitss[i], fc->switchable_interp_cdf[i], NULL);
161 :
162 959 : for (i = 0; i < PALATTE_BSIZE_CTXS; ++i) {
163 840 : av1_get_syntax_rate_from_cdf(md_rate_estimation_array->palette_ysize_fac_bits[i], fc->palette_y_size_cdf[i], NULL);
164 840 : av1_get_syntax_rate_from_cdf(md_rate_estimation_array->palette_uv_size_fac_bits[i], fc->palette_uv_size_cdf[i], NULL);
165 3358 : for (j = 0; j < PALETTE_Y_MODE_CONTEXTS; ++j)
166 2519 : av1_get_syntax_rate_from_cdf(md_rate_estimation_array->palette_ymode_fac_bits[i][j], fc->palette_y_mode_cdf[i][j], NULL);
167 : }
168 :
169 359 : for (i = 0; i < PALETTE_UV_MODE_CONTEXTS; ++i)
170 240 : av1_get_syntax_rate_from_cdf(md_rate_estimation_array->palette_uv_mode_fac_bits[i], fc->palette_uv_mode_cdf[i], NULL);
171 958 : for (i = 0; i < PALETTE_SIZES; ++i) {
172 5030 : for (j = 0; j < PALETTE_COLOR_INDEX_CONTEXTS; ++j) {
173 4191 : av1_get_syntax_rate_from_cdf(md_rate_estimation_array->palette_ycolor_fac_bitss[i][j], fc->palette_y_color_index_cdf[i][j], NULL);
174 4192 : av1_get_syntax_rate_from_cdf(md_rate_estimation_array->palette_uv_color_fac_bits[i][j], fc->palette_uv_color_index_cdf[i][j], NULL);
175 : }
176 : }
177 :
178 : int32_t sign_FacBits[CFL_JOINT_SIGNS];
179 119 : av1_get_syntax_rate_from_cdf(sign_FacBits, fc->cfl_sign_cdf, NULL);
180 1080 : for (int32_t joint_sign = 0; joint_sign < CFL_JOINT_SIGNS; joint_sign++) {
181 960 : int32_t *FacBits_u = md_rate_estimation_array->cfl_alpha_fac_bits[joint_sign][CFL_PRED_U];
182 960 : int32_t *FacBits_v = md_rate_estimation_array->cfl_alpha_fac_bits[joint_sign][CFL_PRED_V];
183 960 : if (CFL_SIGN_U(joint_sign) == CFL_SIGN_ZERO)
184 240 : memset(FacBits_u, 0, CFL_ALPHABET_SIZE * sizeof(*FacBits_u));
185 : else {
186 720 : const AomCdfProb *cdf_u = fc->cfl_alpha_cdf[CFL_CONTEXT_U(joint_sign)];
187 720 : av1_get_syntax_rate_from_cdf(FacBits_u, cdf_u, NULL);
188 : }
189 960 : if (CFL_SIGN_V(joint_sign) == CFL_SIGN_ZERO)
190 240 : memset(FacBits_v, 0, CFL_ALPHABET_SIZE * sizeof(*FacBits_v));
191 : else {
192 720 : int32_t cdf_index = CFL_CONTEXT_V(joint_sign);
193 720 : if ((cdf_index < CFL_ALPHA_CONTEXTS) && (cdf_index >= 0)) {
194 720 : const AomCdfProb *cdf_v = fc->cfl_alpha_cdf[cdf_index];
195 720 : av1_get_syntax_rate_from_cdf(FacBits_v, cdf_v, NULL);
196 : }
197 : }
198 16320 : for (int32_t u = 0; u < CFL_ALPHABET_SIZE; u++)
199 15360 : FacBits_u[u] += sign_FacBits[joint_sign];
200 : }
201 :
202 600 : for (i = 0; i < MAX_TX_CATS; ++i)
203 1920 : for (j = 0; j < TX_SIZE_CONTEXTS; ++j)
204 1440 : av1_get_syntax_rate_from_cdf(md_rate_estimation_array->tx_size_fac_bits[i][j], fc->tx_size_cdf[i][j],
205 : NULL);
206 :
207 2640 : for (i = 0; i < TXFM_PARTITION_CONTEXTS; ++i) {
208 2520 : av1_get_syntax_rate_from_cdf(md_rate_estimation_array->txfm_partition_fac_bits[i],
209 2520 : fc->txfm_partition_cdf[i], NULL);
210 : }
211 :
212 601 : for (i = TX_4X4; i < EXT_TX_SIZES; ++i) {
213 : int32_t s;
214 1920 : for (s = 1; s < EXT_TX_SETS_INTER; ++s) {
215 1440 : if (use_inter_ext_tx_for_txsize[s][i])
216 480 : av1_get_syntax_rate_from_cdf(md_rate_estimation_array->inter_tx_type_fac_bits[s][i], fc->inter_ext_tx_cdf[s][i], av1_ext_tx_inv[av1_ext_tx_set_idx_to_type[1][s]]);
217 : }
218 1440 : for (s = 1; s < EXT_TX_SETS_INTRA; ++s) {
219 959 : if (use_intra_ext_tx_for_txsize[s][i]) {
220 5031 : for (j = 0; j < INTRA_MODES; ++j)
221 4670 : av1_get_syntax_rate_from_cdf(md_rate_estimation_array->intra_tx_type_fac_bits[s][i][j], fc->intra_ext_tx_cdf[s][i][j], av1_ext_tx_inv[av1_ext_tx_set_idx_to_type[0][s]]);
222 : }
223 : }
224 : }
225 1081 : for (i = 0; i < DIRECTIONAL_MODES; ++i)
226 960 : av1_get_syntax_rate_from_cdf(md_rate_estimation_array->angle_delta_fac_bits[i], fc->angle_delta_cdf[i], NULL);
227 121 : av1_get_syntax_rate_from_cdf(md_rate_estimation_array->switchable_restore_fac_bits, fc->switchable_restore_cdf, NULL);
228 120 : av1_get_syntax_rate_from_cdf(md_rate_estimation_array->wiener_restore_fac_bits, fc->wiener_restore_cdf, NULL);
229 120 : av1_get_syntax_rate_from_cdf(md_rate_estimation_array->sgrproj_restore_fac_bits, fc->sgrproj_restore_cdf, NULL);
230 120 : av1_get_syntax_rate_from_cdf(md_rate_estimation_array->intrabc_fac_bits, fc->intrabc_cdf, NULL);
231 :
232 120 : if (!is_i_slice) { // NM - Hardcoded to true
233 : //if (1){
234 696 : for (i = 0; i < COMP_INTER_CONTEXTS; ++i)
235 580 : av1_get_syntax_rate_from_cdf(md_rate_estimation_array->comp_inter_fac_bits[i], fc->comp_inter_cdf[i], NULL);
236 464 : for (i = 0; i < REF_CONTEXTS; ++i) {
237 2436 : for (j = 0; j < SINGLE_REFS - 1; ++j)
238 2088 : av1_get_syntax_rate_from_cdf(md_rate_estimation_array->single_ref_fac_bits[i][j], fc->single_ref_cdf[i][j], NULL);
239 : }
240 :
241 696 : for (i = 0; i < COMP_REF_TYPE_CONTEXTS; ++i)
242 580 : av1_get_syntax_rate_from_cdf(md_rate_estimation_array->comp_ref_type_fac_bits[i], fc->comp_ref_type_cdf[i], NULL);
243 464 : for (i = 0; i < UNI_COMP_REF_CONTEXTS; ++i) {
244 1392 : for (j = 0; j < UNIDIR_COMP_REFS - 1; ++j)
245 1044 : av1_get_syntax_rate_from_cdf(md_rate_estimation_array->uni_comp_ref_fac_bits[i][j], fc->uni_comp_ref_cdf[i][j], NULL);
246 : }
247 :
248 464 : for (i = 0; i < REF_CONTEXTS; ++i) {
249 1392 : for (j = 0; j < FWD_REFS - 1; ++j)
250 1044 : av1_get_syntax_rate_from_cdf(md_rate_estimation_array->comp_ref_fac_bits[i][j], fc->comp_ref_cdf[i][j], NULL);
251 : }
252 :
253 464 : for (i = 0; i < REF_CONTEXTS; ++i) {
254 1044 : for (j = 0; j < BWD_REFS - 1; ++j)
255 696 : av1_get_syntax_rate_from_cdf(md_rate_estimation_array->comp_bwd_ref_fac_bits[i][j], fc->comp_bwdref_cdf[i][j], NULL);
256 : }
257 :
258 580 : for (i = 0; i < INTRA_INTER_CONTEXTS; ++i)
259 464 : av1_get_syntax_rate_from_cdf(md_rate_estimation_array->intra_inter_fac_bits[i], fc->intra_inter_cdf[i], NULL);
260 812 : for (i = 0; i < NEWMV_MODE_CONTEXTS; ++i)
261 696 : av1_get_syntax_rate_from_cdf(md_rate_estimation_array->new_mv_mode_fac_bits[i], fc->newmv_cdf[i], NULL);
262 348 : for (i = 0; i < GLOBALMV_MODE_CONTEXTS; ++i)
263 232 : av1_get_syntax_rate_from_cdf(md_rate_estimation_array->zero_mv_mode_fac_bits[i], fc->zeromv_cdf[i], NULL);
264 812 : for (i = 0; i < REFMV_MODE_CONTEXTS; ++i)
265 696 : av1_get_syntax_rate_from_cdf(md_rate_estimation_array->ref_mv_mode_fac_bits[i], fc->refmv_cdf[i], NULL);
266 464 : for (i = 0; i < DRL_MODE_CONTEXTS; ++i)
267 348 : av1_get_syntax_rate_from_cdf(md_rate_estimation_array->drl_mode_fac_bits[i], fc->drl_cdf[i], NULL);
268 1044 : for (i = 0; i < INTER_MODE_CONTEXTS; ++i)
269 928 : av1_get_syntax_rate_from_cdf(md_rate_estimation_array->inter_compound_mode_fac_bits[i], fc->inter_compound_mode_cdf[i], NULL);
270 2663 : for (i = 0; i < BlockSizeS_ALL; ++i)
271 2547 : av1_get_syntax_rate_from_cdf(md_rate_estimation_array->compound_type_fac_bits[i], fc->compound_type_cdf[i], NULL);
272 2668 : for (i = 0; i < BlockSizeS_ALL; ++i) {
273 2552 : if (get_interinter_wedge_bits((BlockSize)i))
274 1044 : av1_get_syntax_rate_from_cdf(md_rate_estimation_array->wedge_idx_fac_bits[i], fc->wedge_idx_cdf[i], NULL);
275 : }
276 580 : for (i = 0; i < BlockSize_GROUPS; ++i) {
277 464 : av1_get_syntax_rate_from_cdf(md_rate_estimation_array->inter_intra_fac_bits[i], fc->interintra_cdf[i], NULL);
278 464 : av1_get_syntax_rate_from_cdf(md_rate_estimation_array->inter_intra_mode_fac_bits[i], fc->interintra_mode_cdf[i], NULL);
279 : }
280 2665 : for (i = 0; i < BlockSizeS_ALL; ++i)
281 2549 : av1_get_syntax_rate_from_cdf(md_rate_estimation_array->wedge_inter_intra_fac_bits[i], fc->wedge_interintra_cdf[i], NULL);
282 2315 : for (i = BLOCK_8X8; i < BlockSizeS_ALL; i++)
283 2198 : av1_get_syntax_rate_from_cdf(md_rate_estimation_array->motion_mode_fac_bits[i], fc->motion_mode_cdf[i], NULL);
284 2313 : for (i = BLOCK_8X8; i < BlockSizeS_ALL; i++)
285 2196 : av1_get_syntax_rate_from_cdf(md_rate_estimation_array->motion_mode_fac_bits1[i], fc->obmc_cdf[i], NULL);
286 813 : for (i = 0; i < COMP_INDEX_CONTEXTS; ++i)
287 696 : av1_get_syntax_rate_from_cdf(md_rate_estimation_array->comp_idx_fac_bits[i], fc->compound_index_cdf[i], NULL);
288 813 : for (i = 0; i < COMP_GROUP_IDX_CONTEXTS; ++i)
289 696 : av1_get_syntax_rate_from_cdf(md_rate_estimation_array->comp_group_idx_fac_bits[i], fc->comp_group_idx_cdf[i], NULL);
290 : }
291 121 : }
292 :
293 : static const uint8_t log_in_base_2[] = {
294 : 0, 0, 1, 1, 2, 2, 2, 2, 3, 3, 3, 3, 3, 3, 3, 3, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4,
295 : 4, 4, 4, 4, 4, 4, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5,
296 : 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6,
297 : 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,
298 : 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 7, 7,
299 : 7, 7, 7, 7, 7, 7, 7, 7,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9, 9, 9, 9, 9, 9, 9, 9, 9, 9, 9, 9, 9, 9, 9, 9, 9, 9, 9,
325 : 9, 9, 9, 9, 9, 9, 9, 9, 9, 9, 9, 9, 9, 9, 9, 9, 9, 9, 9, 9, 9, 9, 9, 9, 9, 9,
326 : 9, 9, 9, 9, 9, 9, 9, 9, 9, 9, 9, 9, 9, 9, 9, 9, 9, 9, 9, 9, 9, 9, 9, 9, 9, 9,
327 : 9, 9, 9, 9, 9, 9, 9, 9, 9, 9, 9, 9, 9, 9, 9, 9, 9, 9, 9, 9, 9, 9, 9, 9, 9, 9,
328 : 9, 9, 9, 9, 9, 9, 9, 9, 9, 9, 9, 9, 9, 9, 9, 9, 9, 9, 9, 9, 9, 9, 9, 9, 9, 9,
329 : 9, 9, 9, 9, 9, 9, 9, 9, 9, 9, 9, 9, 9, 9, 9, 9, 9, 9, 9, 9, 9, 9, 9, 9, 9, 9,
330 : 9, 9, 9, 9, 9, 9, 9, 9, 9, 9, 9, 9, 9, 9, 9, 9, 9, 9, 9, 9, 9, 9, 9, 9, 9, 9,
331 : 9, 9, 9, 9, 9, 9, 9, 9, 9, 9, 9, 9, 9, 9, 9, 9, 9, 9, 9, 9, 9, 9, 9, 9, 9, 9,
332 : 9, 9, 9, 9, 9, 9, 9, 9, 9, 9, 9, 9, 9, 9, 9, 9, 9, 9, 9, 9, 9, 9, 9, 9, 9, 9,
333 : 9, 9, 9, 9, 9, 9, 9, 9, 9, 9, 10
334 : };
335 :
336 3848530 : static INLINE int32_t mv_class_base(MvClassType c) {
337 3848530 : return c ? CLASS0_SIZE << (c + 2) : 0;
338 : }
339 :
340 3845780 : MvClassType av1_get_mv_class(int32_t z, int32_t *offset) {
341 3845780 : const MvClassType c = (z >= CLASS0_SIZE * 4096)
342 : ? MV_CLASS_10
343 3845780 : : (MvClassType)log_in_base_2[z >> 3];
344 3845780 : if (offset) *offset = z - mv_class_base(c);
345 3854840 : return c;
346 : }
347 :
348 : //void eb_av1_build_nmv_cost_table(int32_t *mvjoint, int32_t *mvcost[2],
349 : // const NmvContext *ctx,
350 : // MvSubpelPrecision precision)
351 :
352 : void eb_av1_build_nmv_cost_table(int32_t *mvjoint, int32_t *mvcost[2],
353 : const NmvContext *ctx,
354 : MvSubpelPrecision precision);
355 :
356 : /**************************************************************************
357 : * av1_estimate_mv_rate()
358 : * Estimate the rate of motion vectors
359 : * based on the frame CDF
360 : ***************************************************************************/
361 120 : void av1_estimate_mv_rate(
362 : PictureControlSet *picture_control_set_ptr,
363 : MdRateEstimationContext *md_rate_estimation_array,
364 : NmvContext *nmv_ctx)
365 : {
366 : int32_t *nmvcost[2];
367 : int32_t *nmvcost_hp[2];
368 120 : FrameHeader *frm_hdr = &picture_control_set_ptr->parent_pcs_ptr->frm_hdr;
369 :
370 120 : nmvcost[0] = &md_rate_estimation_array->nmv_costs[0][MV_MAX];
371 120 : nmvcost[1] = &md_rate_estimation_array->nmv_costs[1][MV_MAX];
372 120 : nmvcost_hp[0] = &md_rate_estimation_array->nmv_costs_hp[0][MV_MAX];
373 120 : nmvcost_hp[1] = &md_rate_estimation_array->nmv_costs_hp[1][MV_MAX];
374 :
375 120 : eb_av1_build_nmv_cost_table(
376 120 : md_rate_estimation_array->nmv_vec_cost,//out
377 120 : frm_hdr->allow_high_precision_mv ? nmvcost_hp : nmvcost, //out
378 : nmv_ctx,
379 120 : frm_hdr->allow_high_precision_mv);
380 :
381 120 : md_rate_estimation_array->nmvcoststack[0] = &md_rate_estimation_array->nmv_costs[0][MV_MAX];
382 120 : md_rate_estimation_array->nmvcoststack[1] = &md_rate_estimation_array->nmv_costs[1][MV_MAX];
383 :
384 120 : if (frm_hdr->allow_intrabc) {
385 0 : int32_t *dvcost[2] = { &md_rate_estimation_array->dv_cost[0][MV_MAX], &md_rate_estimation_array->dv_cost[1][MV_MAX] };
386 0 : eb_av1_build_nmv_cost_table(md_rate_estimation_array->dv_joint_cost, dvcost, &picture_control_set_ptr->coeff_est_entropy_coder_ptr->fc->ndvc,
387 : MV_SUBPEL_NONE);
388 : }
389 120 : }
390 : /**************************************************************************
391 : * av1_estimate_coefficients_rate()
392 : * Estimate the rate of the quantised coefficient
393 : * based on the frame CDF
394 : ***************************************************************************/
395 3703 : void av1_estimate_coefficients_rate(
396 : MdRateEstimationContext *md_rate_estimation_array,
397 : FRAME_CONTEXT *fc)
398 : {
399 3703 : int32_t num_planes = 3; // NM - Hardcoded to 3
400 3703 : const int32_t nplanes = AOMMIN(num_planes, PLANE_TYPES);
401 3703 : int32_t eob_multi_size = 0;
402 3703 : int32_t plane = 0;
403 3703 : int32_t ctx = 0;
404 3703 : int32_t tx_size = 0;
405 :
406 28238 : for (eob_multi_size = 0; eob_multi_size < 7; ++eob_multi_size) {
407 76011 : for (plane = 0; plane < nplanes; ++plane) {
408 51476 : LvMapEobCost *pcost = &md_rate_estimation_array->eob_frac_bits[eob_multi_size][plane];
409 153083 : for (ctx = 0; ctx < 2; ++ctx) {
410 : AomCdfProb *pcdf;
411 102913 : switch (eob_multi_size) {
412 14601 : case 0: pcdf = fc->eob_flag_cdf16[plane][ctx]; break;
413 14664 : case 1: pcdf = fc->eob_flag_cdf32[plane][ctx]; break;
414 14707 : case 2: pcdf = fc->eob_flag_cdf64[plane][ctx]; break;
415 14707 : case 3: pcdf = fc->eob_flag_cdf128[plane][ctx]; break;
416 14745 : case 4: pcdf = fc->eob_flag_cdf256[plane][ctx]; break;
417 14743 : case 5: pcdf = fc->eob_flag_cdf512[plane][ctx]; break;
418 14746 : case 6:
419 14746 : default: pcdf = fc->eob_flag_cdf1024[plane][ctx]; break;
420 : }
421 102913 : av1_get_syntax_rate_from_cdf(pcost->eob_cost[ctx], pcdf, NULL);
422 : }
423 : }
424 : }
425 16825 : for (tx_size = 0; tx_size < TX_SIZES; ++tx_size) {
426 51599 : for (plane = 0; plane < nplanes; ++plane) {
427 37171 : LvMapCoeffCost *pcost = &md_rate_estimation_array->coeff_fac_bits[tx_size][plane];
428 :
429 518081 : for (ctx = 0; ctx < TXB_SKIP_CONTEXTS; ++ctx)
430 481097 : av1_get_syntax_rate_from_cdf(pcost->txb_skip_cost[ctx],
431 481097 : fc->txb_skip_cdf[tx_size][ctx], NULL);
432 :
433 185463 : for (ctx = 0; ctx < SIG_COEF_CONTEXTS_EOB; ++ctx)
434 148430 : av1_get_syntax_rate_from_cdf(pcost->base_eob_cost[ctx],
435 148430 : fc->coeff_base_eob_cdf[tx_size][plane][ctx],
436 : NULL);
437 1567200 : for (ctx = 0; ctx < SIG_COEF_CONTEXTS; ++ctx)
438 1531180 : av1_get_syntax_rate_from_cdf(pcost->base_cost[ctx],
439 1531180 : fc->coeff_base_cdf[tx_size][plane][ctx], NULL);
440 1586980 : for (int ctx = 0; ctx < SIG_COEF_CONTEXTS; ++ctx) {
441 1550960 : pcost->base_cost[ctx][4] = 0;
442 1550960 : pcost->base_cost[ctx][5] = pcost->base_cost[ctx][1] +
443 1550960 : av1_cost_literal(1) -
444 1550960 : pcost->base_cost[ctx][0];
445 1550960 : pcost->base_cost[ctx][6] =
446 1550960 : pcost->base_cost[ctx][2] - pcost->base_cost[ctx][1];
447 1550960 : pcost->base_cost[ctx][7] =
448 1550960 : pcost->base_cost[ctx][3] - pcost->base_cost[ctx][2];
449 : }
450 846788 : for (ctx = 0; ctx < EOB_COEF_CONTEXTS; ++ctx)
451 811146 : av1_get_syntax_rate_from_cdf(pcost->eob_extra_cost[ctx],
452 811146 : fc->eob_extra_cdf[tx_size][plane][ctx], NULL);
453 :
454 147043 : for (ctx = 0; ctx < DC_SIGN_CONTEXTS; ++ctx)
455 111390 : av1_get_syntax_rate_from_cdf(pcost->dc_sign_cost[ctx],
456 111390 : fc->dc_sign_cdf[plane][ctx], NULL);
457 :
458 809419 : for (ctx = 0; ctx < LEVEL_CONTEXTS; ++ctx) {
459 : int32_t br_rate[BR_CDF_SIZE];
460 776402 : int32_t prev_cost = 0;
461 : int32_t i, j;
462 776402 : av1_get_syntax_rate_from_cdf(br_rate, fc->coeff_br_cdf[tx_size][plane][ctx], NULL);
463 : // printf("br_rate: ");
464 : // for(j = 0; j < BR_CDF_SIZE; j++)
465 : // printf("%4d ", br_rate[j]);
466 : // printf("\n");
467 3865270 : for (i = 0; i < COEFF_BASE_RANGE; i += BR_CDF_SIZE - 1) {
468 12360400 : for (j = 0; j < BR_CDF_SIZE - 1; j++)
469 9268920 : pcost->lps_cost[ctx][i + j] = prev_cost + br_rate[j];
470 3091500 : prev_cost += br_rate[j];
471 : }
472 773766 : pcost->lps_cost[ctx][i] = prev_cost;
473 : // printf("lps_cost: %d %d %2d : ", tx_size, plane, ctx);
474 : // for (i = 0; i <= COEFF_BASE_RANGE; i++)
475 : // printf("%5d ", pcost->lps_cost[ctx][i]);
476 : // printf("\n");
477 : }
478 808135 : for (int ctx = 0; ctx < LEVEL_CONTEXTS; ++ctx) {
479 775118 : pcost->lps_cost[ctx][0 + COEFF_BASE_RANGE + 1] =
480 775118 : pcost->lps_cost[ctx][0];
481 10004600 : for (int i = 1; i <= COEFF_BASE_RANGE; ++i) {
482 9229520 : pcost->lps_cost[ctx][i + COEFF_BASE_RANGE + 1] =
483 9229520 : pcost->lps_cost[ctx][i] - pcost->lps_cost[ctx][i - 1];
484 : }
485 : }
486 : }
487 : }
488 0 : }
|