Coverage Report

Created: 2026-12-01 06:52

next uncovered line (L), next uncovered region (R), next uncovered branch (B)
/work/svt-av1/Source/Lib/Codec/rd_cost.c
Line
Count
Source
1
/*
2
* Copyright(c) 2019 Intel Corporation
3
* Copyright (c) 2016, Alliance for Open Media. All rights reserved
4
*
5
* This source code is subject to the terms of the BSD 2 Clause License and
6
* the Alliance for Open Media Patent License 1.0. If the BSD 2 Clause License
7
* was not distributed with this source code in the LICENSE file, you can
8
* obtain it at https://www.aomedia.org/license/software-license. If the Alliance for Open
9
* Media Patent License 1.0 was not distributed with this source code in the
10
* PATENTS file, you can obtain it at https://www.aomedia.org/license/patent-license.
11
*/
12
13
/***************************************
14
* Includes
15
***************************************/
16
#include "rd_cost.h"
17
#include "mcomp.h" // svt_mv_cost (shared MV-rate helper; replaces local duplicate)
18
#include "common_utils.h"
19
#include "aom_dsp_rtcd.h"
20
#include "svt_log.h"
21
#include "enc_inter_prediction.h"
22
#include "full_loop.h"
23
#include "entropy_coding.h"
24
25
#include <assert.h>
26
27
0
#define MV_COST_WEIGHT 108
28
int svt_aom_get_reference_mode_context_new(const MacroBlockD* xd);
29
int svt_av1_get_pred_context_uni_comp_ref_p(const MacroBlockD* xd);
30
int svt_av1_get_pred_context_uni_comp_ref_p1(const MacroBlockD* xd);
31
int svt_av1_get_pred_context_uni_comp_ref_p2(const MacroBlockD* xd);
32
int svt_aom_get_comp_reference_type_context_new(const MacroBlockD* xd);
33
34
int  svt_aom_get_palette_bsize_ctx(BlockSize bsize);
35
int  svt_aom_get_palette_mode_ctx(const MacroBlockD* xd);
36
int  svt_aom_write_uniform_cost(int n, int v);
37
int  svt_get_palette_cache_y(const MacroBlockD* const xd, uint16_t* cache);
38
int  svt_av1_palette_color_cost_y(const PaletteModeInfo* const pmi, uint16_t* color_cache, const int palette_size,
39
                                  int n_cache, int bit_depth);
40
int  svt_av1_cost_color_map(ModeDecisionCandidate* cand, MdRateEstimationContext* rate_table,
41
42
                            BlkStruct* blk_ptr, int plane, BlockSize bsize, COLOR_MAP_TYPE type);
43
void svt_aom_get_block_dimensions(BlockSize bsize, int plane, const MacroBlockD* xd, int* width, int* height,
44
                                  int* rows_within_bounds, int* cols_within_bounds);
45
int  svt_aom_allow_palette(int allow_screen_content_tools, BlockSize bsize);
46
int  svt_aom_allow_intrabc(const FrameHeader* frm_hdr, SliceType slice_type);
47
48
0
int32_t svt_av1_mv_bit_cost_light(const Mv mv, const Mv ref) {
49
0
    const uint32_t factor     = 50;
50
0
    const uint32_t absmvdiffx = ABS(mv.x - ref.x);
51
0
    const uint32_t absmvdiffy = ABS(mv.y - ref.y);
52
0
    const uint32_t mv_rate    = 1296 + (factor * (absmvdiffx + absmvdiffy));
53
0
    return mv_rate;
54
0
}
55
56
int32_t svt_av1_mv_bit_cost(const Mv mv, const Mv ref, const int32_t* mvjcost, const int32_t* const mvcost[2],
57
0
                            int32_t weight) {
58
    // Restrict the size of the MV diff to be within the max AV1 range.  If the MV diff
59
    // is outside this range, the diff will index beyond the cost array, causing a seg fault.
60
    // Both the MVs and the MV diffs should be within the allowable range for accessing the MV cost
61
    // infrastructure.
62
0
    const int16_t x         = MIN(MAX(mv.x - ref.x, MV_LOW), MV_UPP);
63
0
    const int16_t y         = MIN(MAX(mv.y - ref.y, MV_LOW), MV_UPP);
64
0
    Mv            temp_diff = {{x, y}};
65
66
0
    return ROUND_POWER_OF_TWO(svt_mv_cost(temp_diff, mvjcost, mvcost) * weight, 7);
67
0
}
68
69
/////////////////////////////COEFFICIENT CALCULATION //////////////////////////////////////////////
70
0
static INLINE int32_t get_golomb_cost(int32_t abs_qc) {
71
0
    if (abs_qc >= 1 + NUM_BASE_LEVELS + COEFF_BASE_RANGE) {
72
0
        const int32_t r      = abs_qc - COEFF_BASE_RANGE - NUM_BASE_LEVELS;
73
0
        const int32_t length = get_msb(r) + 1;
74
0
        return av1_cost_literal(2 * length - 1);
75
0
    }
76
0
    return 0;
77
0
}
78
79
void svt_av1_txb_init_levels_c(const TranLow* const coeff, const int32_t width, const int32_t height,
80
0
                               uint8_t* const levels) {
81
0
    uint8_t* ls = levels;
82
83
0
    for (int32_t i = 0; i < height; i++) {
84
0
        for (int32_t j = 0; j < width; j++) {
85
0
            *ls++ = (uint8_t)clamp(abs(coeff[i * width + j]), 0, INT8_MAX);
86
0
        }
87
0
        for (int32_t j = 0; j < TX_PAD_HOR; j++) {
88
0
            *ls++ = 0;
89
0
        }
90
0
    }
91
0
}
92
93
static int32_t av1_transform_type_rate_estimation(ModeDecisionContext* ctx, uint8_t allow_update_cdf, FRAME_CONTEXT* fc,
94
                                                  ModeDecisionCandidateBuffer* cand_bf, bool is_inter,
95
                                                  TxSize transform_size, TxType transform_type,
96
0
                                                  bool reduced_tx_set_used) {
97
    // const MbModeInfo *mbmi = &xd->mi[0]->mbmi;
98
    // const int32_t is_inter = is_inter_block(mbmi);
99
100
    // get_ext_tx_types() and get_ext_tx_set() each re-derive the set type from
101
    // the same inputs; derive it once and index the tables directly.
102
0
    const TxSetType tx_set_type = get_ext_tx_set_type(transform_size, is_inter, reduced_tx_set_used);
103
0
    if (av1_num_ext_tx_set[tx_set_type] > 1 /*&&    !xd->lossless[xd->mi[0]->mbmi.segment_id]  WE ARE NOT LOSSLESS*/) {
104
0
        const TxSize square_tx_size = txsize_sqr_map[transform_size];
105
0
        assert(square_tx_size < EXT_TX_SIZES);
106
107
0
        const int32_t ext_tx_set = ext_tx_set_index[is_inter][tx_set_type];
108
0
        if (is_inter) {
109
0
            if (ext_tx_set > 0) {
110
0
                if (allow_update_cdf) {
111
0
                    update_cdf(fc->inter_ext_tx_cdf[ext_tx_set][square_tx_size],
112
0
                               av1_ext_tx_ind[tx_set_type][transform_type],
113
0
                               av1_num_ext_tx_set[tx_set_type]);
114
0
                }
115
0
                return ctx->md_rate_est_ctx->inter_tx_type_fac_bits[ext_tx_set][square_tx_size][transform_type];
116
0
            }
117
0
        } else {
118
0
            if (ext_tx_set > 0) {
119
0
                PredictionMode intra_dir;
120
0
                if (cand_bf->cand->block_mi.filter_intra_mode != FILTER_INTRA_MODES) {
121
0
                    intra_dir = fimode_to_intradir[cand_bf->cand->block_mi.filter_intra_mode];
122
0
                } else {
123
0
                    intra_dir = cand_bf->cand->block_mi.mode;
124
0
                }
125
0
                assert(intra_dir < INTRA_MODES);
126
0
                if (allow_update_cdf) {
127
0
                    update_cdf(fc->intra_ext_tx_cdf[ext_tx_set][square_tx_size][intra_dir],
128
0
                               av1_ext_tx_ind[tx_set_type][transform_type],
129
0
                               av1_num_ext_tx_set[tx_set_type]);
130
0
                }
131
0
                return ctx->md_rate_est_ctx
132
0
                    ->intra_tx_type_fac_bits[ext_tx_set][square_tx_size][intra_dir][transform_type];
133
0
            }
134
0
        }
135
0
    }
136
0
    return 0;
137
0
}
138
139
// Update the eob-related CDFs. Function assumes allow_update_cdf is true
140
// as the only action of the function is to update the CDFs.
141
0
static void update_eob_context(int eob, TxSize tx_size, TxClass tx_class, PlaneType plane, FRAME_CONTEXT* ec_ctx) {
142
0
    int          eob_extra;
143
0
    const int    eob_pt  = get_eob_pos_token(eob, &eob_extra);
144
0
    const TxSize txs_ctx = (TxSize)((txsize_sqr_map[tx_size] + txsize_sqr_up_map[tx_size] + 1) >> 1);
145
0
    assert(txs_ctx < TX_SIZES);
146
0
    const int eob_multi_size = txsize_log2_minus4[tx_size];
147
0
    const int eob_multi_ctx  = (tx_class == TX_CLASS_2D) ? 0 : 1;
148
149
0
    switch (eob_multi_size) {
150
0
    case 0:
151
0
        update_cdf(ec_ctx->eob_flag_cdf16[plane][eob_multi_ctx], eob_pt - 1, 5);
152
0
        break;
153
0
    case 1:
154
0
        update_cdf(ec_ctx->eob_flag_cdf32[plane][eob_multi_ctx], eob_pt - 1, 6);
155
0
        break;
156
0
    case 2:
157
0
        update_cdf(ec_ctx->eob_flag_cdf64[plane][eob_multi_ctx], eob_pt - 1, 7);
158
0
        break;
159
0
    case 3:
160
0
        update_cdf(ec_ctx->eob_flag_cdf128[plane][eob_multi_ctx], eob_pt - 1, 8);
161
0
        break;
162
0
    case 4:
163
0
        update_cdf(ec_ctx->eob_flag_cdf256[plane][eob_multi_ctx], eob_pt - 1, 9);
164
0
        break;
165
0
    case 5:
166
0
        update_cdf(ec_ctx->eob_flag_cdf512[plane][eob_multi_ctx], eob_pt - 1, 10);
167
0
        break;
168
0
    case 6:
169
0
    default:
170
0
        update_cdf(ec_ctx->eob_flag_cdf1024[plane][eob_multi_ctx], eob_pt - 1, 11);
171
0
        break;
172
0
    }
173
174
0
    if (eob_pt > 2) {
175
0
        const int cnt = eob_pt - 3;
176
0
        const int bit = (eob_extra >> cnt) & 1;
177
0
        update_cdf(ec_ctx->eob_extra_cdf[txs_ctx][plane][cnt], bit, 2);
178
0
    }
179
0
}
180
181
// Transform end of block bit estimation
182
11.0k
int get_eob_cost(int eob, const LvMapEobCost* txb_eob_costs, const LvMapCoeffCost* txb_costs, TxClass tx_class) {
183
11.0k
    int       eob_extra;
184
11.0k
    const int eob_pt        = get_eob_pos_token(eob, &eob_extra);
185
18.4E
    const int eob_multi_ctx = (tx_class == TX_CLASS_2D) ? 0 : 1;
186
11.0k
    int       eob_cost      = txb_eob_costs->eob_cost[eob_multi_ctx][eob_pt - 1];
187
188
11.0k
    if (eob_pt > 2) {
189
0
        const int cnt = eob_pt - 3;
190
0
        const int bit = (eob_extra >> cnt) & 1;
191
0
        eob_cost += txb_costs->eob_extra_cost[cnt][bit];
192
0
        eob_cost += av1_cost_literal(cnt);
193
0
    }
194
11.0k
    return eob_cost;
195
11.0k
}
196
197
static INLINE int32_t av1_cost_skip_txb(ModeDecisionContext* ctx, uint8_t allow_update_cdf, FRAME_CONTEXT* ec_ctx,
198
0
                                        TxSize transform_size, PlaneType plane_type, int16_t txb_skip_ctx) {
199
0
    const TxSize txs_ctx = (TxSize)((txsize_sqr_map[transform_size] + txsize_sqr_up_map[transform_size] + 1) >> 1);
200
0
    assert(txs_ctx < TX_SIZES);
201
0
    const LvMapCoeffCost* const coeff_costs = &ctx->md_rate_est_ctx->coeff_fac_bits[txs_ctx][plane_type];
202
0
    if (allow_update_cdf) {
203
0
        update_cdf(ec_ctx->txb_skip_cdf[txs_ctx][txb_skip_ctx], 1, 2);
204
0
    }
205
0
    return coeff_costs->txb_skip_cost[txb_skip_ctx][1];
206
0
}
207
208
static INLINE int32_t av1_cost_coeffs_txb_loop_cost_one_eob(const TranLow* const qcoeff, int8_t* const coeff_contexts,
209
0
                                                            const LvMapCoeffCost* coeff_costs, int16_t dc_sign_ctx) {
210
0
    const TranLow v         = qcoeff[0];
211
0
    const int32_t level     = abs(v);
212
0
    const int32_t coeff_ctx = coeff_contexts[0];
213
214
0
    assert((AOMMIN(level, 3) - 1) >= 0);
215
0
    int32_t cost = coeff_costs->base_eob_cost[coeff_ctx][AOMMIN(level, 3) - 1];
216
217
0
    if (v != 0) {
218
0
        const int32_t sign = (v < 0) ? 1 : 0;
219
        // sign bit cost
220
0
        cost += coeff_costs->dc_sign_cost[dc_sign_ctx][sign];
221
222
0
        if (level > NUM_BASE_LEVELS) {
223
0
            const int32_t base_range = level - 1 - NUM_BASE_LEVELS;
224
225
0
            if (base_range < COEFF_BASE_RANGE) {
226
0
                cost += coeff_costs->lps_cost[0][base_range];
227
0
            } else {
228
0
                cost += coeff_costs->lps_cost[0][COEFF_BASE_RANGE];
229
0
            }
230
231
0
            if (level >= 1 + NUM_BASE_LEVELS + COEFF_BASE_RANGE) {
232
0
                cost += get_golomb_cost(level);
233
0
            }
234
0
        }
235
0
    }
236
0
    return cost;
237
0
}
238
239
// Takes TxClass rather than TxType and is force-inlined, so the switch at the
240
// call site instantiates one copy per class with tx_class a compile-time
241
// constant, letting get_br_ctx() fold its class dispatch away in the loop.
242
static AOM_FORCE_INLINE int32_t av1_cost_coeffs_txb_loop_cost_eob(
243
    ModeDecisionContext* md_ctx, uint16_t eob, const int16_t* const scan, const TranLow* const qcoeff,
244
    int8_t* const coeff_contexts, const LvMapCoeffCost* coeff_costs, int16_t dc_sign_ctx, uint8_t* const levels,
245
0
    const int32_t bwl, TxClass tx_class) {
246
0
    const uint32_t cost_literal = av1_cost_literal(1);
247
0
    int32_t        cost         = 0;
248
249
    //Optimized/simplified function when eob is 1
250
0
    if (eob == 1) {
251
0
        return av1_cost_coeffs_txb_loop_cost_one_eob(qcoeff, coeff_contexts, coeff_costs, dc_sign_ctx);
252
0
    }
253
254
    //  first (eob - 1) index
255
0
    {
256
0
        const int32_t pos       = scan[eob - 1];
257
0
        const TranLow v         = qcoeff[pos];
258
0
        const int32_t level     = abs(v);
259
0
        const int32_t coeff_ctx = coeff_contexts[pos];
260
261
0
        assert((AOMMIN(level, 3) - 1) >= 0);
262
0
        cost += coeff_costs->base_eob_cost[coeff_ctx][AOMMIN(level, 3) - 1];
263
264
0
        if (v != 0) {
265
0
            cost += cost_literal;
266
0
            if (level > NUM_BASE_LEVELS) {
267
0
                int32_t       ctx        = get_br_ctx(levels, pos, bwl, tx_class);
268
0
                const int32_t base_range = level - 1 - NUM_BASE_LEVELS;
269
270
0
                if (base_range < COEFF_BASE_RANGE) {
271
0
                    cost += coeff_costs->lps_cost[ctx][base_range];
272
0
                } else {
273
0
                    cost += coeff_costs->lps_cost[ctx][COEFF_BASE_RANGE];
274
0
                }
275
276
0
                if (level >= 1 + NUM_BASE_LEVELS + COEFF_BASE_RANGE) {
277
0
                    cost += get_golomb_cost(level);
278
0
                }
279
0
            }
280
0
        }
281
0
    }
282
    // last (0) index
283
0
    {
284
0
        const TranLow v         = qcoeff[0];
285
0
        const int32_t level     = abs(v);
286
0
        const int32_t coeff_ctx = coeff_contexts[0];
287
288
0
        cost += coeff_costs->base_cost[coeff_ctx][AOMMIN(level, 3)];
289
290
0
        if (v != 0) {
291
0
            const int32_t sign = (v < 0) ? 1 : 0;
292
            // sign bit cost
293
294
0
            cost += coeff_costs->dc_sign_cost[dc_sign_ctx][sign];
295
296
0
            if (level > NUM_BASE_LEVELS) {
297
0
                int32_t       ctx        = get_br_ctx(levels, 0, bwl, tx_class);
298
0
                const int32_t base_range = level - 1 - NUM_BASE_LEVELS;
299
300
0
                if (base_range < COEFF_BASE_RANGE) {
301
0
                    cost += coeff_costs->lps_cost[ctx][base_range];
302
0
                } else {
303
0
                    cost += coeff_costs->lps_cost[ctx][COEFF_BASE_RANGE];
304
0
                }
305
306
0
                if (level >= 1 + NUM_BASE_LEVELS + COEFF_BASE_RANGE) {
307
0
                    cost += get_golomb_cost(level);
308
0
                }
309
0
            }
310
0
        }
311
0
    }
312
0
    int32_t c;
313
    /* Optimized Loop, omitted first (eob - 1) and last (0) index */
314
    // Estimate the rate of the first(eob / fast_coeff_est_level) coeff(s), DC and last coeff only
315
0
    int32_t  c_start = MIN(eob - 2, eob / MAX(1, (int)(md_ctx->mds_fast_coeff_est_level - md_ctx->mds_subres_step)));
316
0
    uint32_t cost_literal_cnt = 0;
317
#if CONFIG_ENABLE_FAST_DECODE_3
318
    // Decoder-complexity rate of the positions this estimate skips (c_start + 1 .. eob - 2)
319
    if (md_ctx->dec_cost_ctrls.enabled && c_start < eob - 2) {
320
        cost += md_ctx->dec_cost_ctrls.pos_rate * (eob - 2 - c_start);
321
    }
322
#endif
323
0
    for (c = c_start; c >= 1; --c) {
324
0
        const int32_t pos = scan[c];
325
0
        cost_literal_cnt += !!(qcoeff[pos]);
326
0
        const int32_t level = abs(qcoeff[pos]);
327
0
        if (level > NUM_BASE_LEVELS) {
328
0
            int32_t       ctx        = get_br_ctx(levels, pos, bwl, tx_class);
329
0
            const int32_t base_range = level - 1 - NUM_BASE_LEVELS;
330
331
0
            cost += coeff_costs->base_cost[coeff_contexts[pos]][3];
332
0
            if (base_range < COEFF_BASE_RANGE) {
333
0
                cost += coeff_costs->lps_cost[ctx][base_range];
334
0
            } else {
335
0
                cost += get_golomb_cost(level) + coeff_costs->lps_cost[ctx][COEFF_BASE_RANGE];
336
0
            }
337
0
        } else {
338
0
            cost += coeff_costs->base_cost[coeff_contexts[pos]][level];
339
0
        }
340
0
    }
341
0
    cost += cost_literal_cnt * cost_literal;
342
343
0
    return cost;
344
0
}
345
346
// Note: don't call this function when eob is 0.
347
uint64_t svt_av1_cost_coeffs_txb(ModeDecisionContext* ctx, uint8_t allow_update_cdf, FRAME_CONTEXT* ec_ctx,
348
                                 ModeDecisionCandidateBuffer* cand_bf, const TranLow* const qcoeff, uint16_t eob,
349
                                 PlaneType plane_type, TxSize transform_size, TxType transform_type,
350
                                 int16_t txb_skip_ctx, int16_t dc_sign_ctx, bool reduced_transform_set_flag)
351
352
0
{
353
    //Note: there is a different version of this function in AOM that seems to be efficient as its name is:
354
    //warehouse_efficients_txb
355
356
0
    const TxSize  txs_ctx  = get_txsize_entropy_ctx(transform_size);
357
0
    const TxClass tx_class = tx_type_to_class[transform_type];
358
0
    int32_t       cost;
359
0
    const int32_t bwl    = get_txb_bwl(transform_size);
360
0
    const int32_t width  = get_txb_wide(transform_size);
361
0
    const int32_t height = get_txb_high(transform_size);
362
363
0
    const ScanOrder* const scan_order     = get_scan_order(transform_size, transform_type);
364
0
    const int16_t* const   scan           = scan_order->scan;
365
0
    uint8_t* const         levels         = set_levels(ctx->md_levels_buf, width, height);
366
0
    int8_t* const          coeff_contexts = ctx->md_coeff_contexts;
367
0
    assert(txs_ctx < TX_SIZES);
368
0
    const LvMapCoeffCost* const coeff_costs = &ctx->md_rate_est_ctx->coeff_fac_bits[txs_ctx][plane_type];
369
370
0
    const int32_t             eob_multi_size = txsize_log2_minus4[transform_size];
371
0
    const LvMapEobCost* const eob_bits       = &ctx->md_rate_est_ctx->eob_frac_bits[eob_multi_size][plane_type];
372
    // eob must be greater than 0 here.
373
0
    assert(eob > 0);
374
0
    cost = coeff_costs->txb_skip_cost[txb_skip_ctx][0];
375
376
0
    if (allow_update_cdf) {
377
0
        update_cdf(ec_ctx->txb_skip_cdf[txs_ctx][txb_skip_ctx], eob == 0, 2);
378
0
    }
379
380
0
    if (eob > 1) {
381
0
        svt_av1_txb_init_levels(qcoeff,
382
0
                                width,
383
0
                                height,
384
0
                                levels); // NM - Needs to be optimized - to be combined with the quantisation.
385
0
    }
386
0
    const bool is_inter = is_inter_mode(cand_bf->cand->block_mi.mode);
387
    // Transform type bit estimation
388
0
    cost += plane_type > PLANE_TYPE_Y ? 0
389
0
                                      : av1_transform_type_rate_estimation(ctx,
390
0
                                                                           allow_update_cdf,
391
0
                                                                           ec_ctx,
392
0
                                                                           cand_bf,
393
0
                                                                           is_inter,
394
0
                                                                           transform_size,
395
0
                                                                           transform_type,
396
0
                                                                           reduced_transform_set_flag);
397
398
    // Transform eob bit estimation
399
0
    cost += get_eob_cost(eob, eob_bits, coeff_costs, tx_class);
400
0
    if (allow_update_cdf) {
401
0
        update_eob_context(eob, transform_size, tx_class, plane_type, ec_ctx);
402
0
    }
403
    // Transform non-zero coeff bit estimation
404
0
    svt_av1_get_nz_map_contexts(levels,
405
0
                                scan,
406
0
                                eob,
407
0
                                transform_size,
408
0
                                tx_class,
409
0
                                coeff_contexts); // NM - Assembly version is available in AOM
410
0
    assert(eob <= width * height);
411
0
    if (allow_update_cdf) {
412
0
        for (int c = eob - 1; c >= 0; --c) {
413
0
            const int     pos       = scan[c];
414
0
            const int     coeff_ctx = coeff_contexts[pos];
415
0
            const TranLow v         = qcoeff[pos];
416
0
            const TranLow level     = abs(v);
417
0
            if (c == eob - 1) {
418
0
                assert(coeff_ctx < 4);
419
0
                update_cdf(ec_ctx->coeff_base_eob_cdf[txs_ctx][plane_type][coeff_ctx], AOMMIN(level, 3) - 1, 3);
420
0
            } else {
421
0
                update_cdf(ec_ctx->coeff_base_cdf[txs_ctx][plane_type][coeff_ctx], AOMMIN(level, 3), 4);
422
0
            }
423
424
0
            {
425
0
                if (c == eob - 1) {
426
0
                    assert(coeff_ctx < 4);
427
0
                }
428
0
            }
429
430
0
            if (level > NUM_BASE_LEVELS) {
431
0
                const int base_range = level - 1 - NUM_BASE_LEVELS;
432
0
                int       br_ctx;
433
0
                if (eob == 1) {
434
0
                    br_ctx = 0;
435
0
                } else {
436
0
                    br_ctx = get_br_ctx(levels, pos, bwl, tx_class);
437
0
                }
438
439
0
                for (int idx = 0; idx < COEFF_BASE_RANGE; idx += BR_CDF_SIZE - 1) {
440
0
                    const int k = AOMMIN(base_range - idx, BR_CDF_SIZE - 1);
441
0
                    update_cdf(ec_ctx->coeff_br_cdf[AOMMIN(txs_ctx, TX_32X32)][plane_type][br_ctx], k, BR_CDF_SIZE);
442
0
                    for (int lps = 0; lps < BR_CDF_SIZE - 1; lps++) {
443
0
                        if (lps == k) {
444
0
                            break;
445
0
                        }
446
0
                    }
447
0
                    if (k < BR_CDF_SIZE - 1) {
448
0
                        break;
449
0
                    }
450
0
                }
451
0
            }
452
0
        }
453
454
0
        if (qcoeff[0] != 0) {
455
0
            update_cdf(ec_ctx->dc_sign_cdf[plane_type][dc_sign_ctx], qcoeff[0] < 0, 2);
456
0
        }
457
458
        //TODO: CHKN  for 128x128 where we need more than one TXb, we need to update the txb_context(dc_sign+skip_ctx) in a Txb basis.
459
460
0
        return 0;
461
0
    }
462
463
0
#define COST_TXB_EOB_CASE(tx_class_literal)                                                                   \
464
0
    case tx_class_literal:                                                                                    \
465
0
        cost += av1_cost_coeffs_txb_loop_cost_eob(                                                            \
466
0
            ctx, eob, scan, qcoeff, coeff_contexts, coeff_costs, dc_sign_ctx, levels, bwl, tx_class_literal); \
467
0
        break;
468
0
    switch (tx_class) {
469
0
        COST_TXB_EOB_CASE(TX_CLASS_2D);
470
0
        COST_TXB_EOB_CASE(TX_CLASS_HORIZ);
471
0
        COST_TXB_EOB_CASE(TX_CLASS_VERT);
472
0
#undef COST_TXB_EOB_CASE
473
0
    default:
474
0
        assert(false);
475
0
        break;
476
0
    }
477
0
    return cost;
478
0
}
479
480
uint64_t svt_aom_get_intra_uv_fast_rate(PictureControlSet* pcs, ModeDecisionContext* ctx,
481
210k
                                        ModeDecisionCandidateBuffer* cand_bf, bool use_accurate_cfl) {
482
210k
    const BlockGeom* const blk_geom = ctx->blk_geom;
483
210k
    ModeDecisionCandidate* cand     = cand_bf->cand;
484
210k
    assert(ctx->has_uv);
485
210k
    assert(!(svt_aom_allow_intrabc(&pcs->ppcs->frm_hdr, pcs->ppcs->slice_type) && cand->block_mi.use_intrabc));
486
210k
    MdRateEstimationContext* md_rate_est_ctx = ctx->md_rate_est_ctx;
487
210k
    const uint8_t            is_cfl_allowed  = (blk_geom->bwidth <= 32 && blk_geom->bheight <= 32) ? 1 : 0;
488
210k
    PredictionMode           intra_mode      = (PredictionMode)cand->block_mi.mode;
489
    // If CFL alphas are not known yet, calculate the chroma mode bits based on DC Mode. If CFL is selected the chroma mode bits must be updated later
490
210k
    const UvPredictionMode chroma_mode = cand->block_mi.uv_mode == UV_CFL_PRED && !use_accurate_cfl
491
210k
        ? UV_DC_PRED
492
210k
        : cand->block_mi.uv_mode;
493
210k
    const uint32_t         mi_row      = ctx->blk_org_y >> MI_SIZE_LOG2;
494
210k
    const uint32_t         mi_col      = ctx->blk_org_x >> MI_SIZE_LOG2;
495
    // Subsampling assumes YUV 420 content
496
210k
    const uint8_t ss_x = 1;
497
210k
    const uint8_t ss_y = 1;
498
499
210k
    uint64_t chroma_rate = 0;
500
    // Estimate chroma nominal intra mode bits
501
210k
    chroma_rate += (uint64_t)md_rate_est_ctx->intra_uv_mode_fac_bits[is_cfl_allowed][intra_mode][chroma_mode];
502
503
    // Estimate chroma angular mode bits; angular offset only allow for bsize >= 8x8
504
210k
    if (blk_geom->bsize >= BLOCK_8X8 && av1_is_directional_mode(get_uv_mode(chroma_mode))) {
505
0
        chroma_rate +=
506
0
            md_rate_est_ctx->angle_delta_fac_bits[chroma_mode - V_PRED]
507
0
                                                 [MAX_ANGLE_DELTA + cand->block_mi.angle_delta[PLANE_TYPE_UV]];
508
0
    }
509
510
    // Estimate CFL factor bits when CFL is used
511
210k
    if (chroma_mode == UV_CFL_PRED) {
512
0
        chroma_rate += (uint64_t)md_rate_est_ctx->cfl_alpha_fac_bits[cand->block_mi.cfl_alpha_signs][CFL_PRED_U]
513
0
                                                                    [CFL_IDX_U(cand->block_mi.cfl_alpha_idx)] +
514
0
            (uint64_t)md_rate_est_ctx->cfl_alpha_fac_bits[cand->block_mi.cfl_alpha_signs][CFL_PRED_V]
515
0
                                                         [CFL_IDX_V(cand->block_mi.cfl_alpha_idx)];
516
0
    }
517
518
    // Estimate chroma palette mode bits (currently not supported, so just cost of signalling off)
519
210k
    if (chroma_mode == UV_DC_PRED &&
520
210k
        svt_aom_allow_palette(pcs->ppcs->frm_hdr.allow_screen_content_tools, blk_geom->bsize) &&
521
0
        is_chroma_reference(mi_row, mi_col, blk_geom->bsize, ss_x, ss_y)) {
522
0
        const int use_palette_y  = cand->palette_info && (cand->palette_size[0] > 0);
523
0
        const int use_palette_uv = cand->palette_info && (cand->palette_size[1] > 0);
524
0
        chroma_rate += ctx->md_rate_est_ctx->palette_uv_mode_fac_bits[use_palette_y][use_palette_uv];
525
0
    }
526
527
210k
    return chroma_rate;
528
210k
}
529
530
uint64_t svt_aom_intra_fast_cost(PictureControlSet* pcs, ModeDecisionContext* ctx, ModeDecisionCandidateBuffer* cand_bf,
531
210k
                                 uint64_t lambda, uint64_t luma_distortion) {
532
210k
    const BlockGeom*       blk_geom = ctx->blk_geom;
533
210k
    BlkStruct*             blk_ptr  = ctx->blk_ptr;
534
210k
    ModeDecisionCandidate* cand     = cand_bf->cand;
535
210k
    if (svt_aom_allow_intrabc(&pcs->ppcs->frm_hdr, pcs->ppcs->slice_type) && cand->block_mi.use_intrabc) {
536
0
        uint64_t rate = 0;
537
538
0
        Mv         mv        = {.as_int = cand->block_mi.mv[0].as_int};
539
0
        Mv         ref_mv    = {.as_int = cand->pred_mv[0].as_int};
540
0
        const int* dvcost[2] = {(int*)&ctx->md_rate_est_ctx->dv_cost[0][MV_MAX],
541
0
                                (int*)&ctx->md_rate_est_ctx->dv_cost[1][MV_MAX]};
542
0
        int32_t    mv_rate   = svt_av1_mv_bit_cost(
543
0
            mv, ref_mv, ctx->md_rate_est_ctx->dv_joint_cost, dvcost, MV_COST_WEIGHT_SUB);
544
545
0
        rate                      = mv_rate + ctx->md_rate_est_ctx->intrabc_fac_bits[cand->block_mi.use_intrabc];
546
0
        cand_bf->fast_luma_rate   = rate;
547
0
        cand_bf->fast_chroma_rate = 0;
548
0
        return (RDCOST(lambda, rate, luma_distortion));
549
210k
    } else {
550
        // Number of bits for each synatax element
551
210k
        uint64_t       intra_mode_bits_num          = 0;
552
210k
        uint64_t       intra_luma_mode_bits_num     = 0;
553
210k
        uint64_t       intra_luma_ang_mode_bits_num = 0;
554
210k
        uint64_t       intra_filter_mode_bits_num   = 0;
555
210k
        uint64_t       skip_mode_rate               = 0;
556
210k
        const uint8_t  skip_mode_ctx                = ctx->skip_mode_ctx;
557
210k
        PredictionMode intra_mode                   = (PredictionMode)cand->block_mi.mode;
558
        // Luma and chroma rate
559
210k
        uint32_t rate;
560
210k
        uint32_t luma_rate   = 0;
561
210k
        uint32_t chroma_rate = 0;
562
210k
        intra_mode_bits_num  = pcs->slice_type != I_SLICE
563
210k
            ? (uint64_t)ctx->md_rate_est_ctx->mb_mode_fac_bits[eb_size_group_lookup[blk_geom->bsize]][intra_mode]
564
210k
            : ZERO_COST;
565
566
210k
        skip_mode_rate = pcs->slice_type != I_SLICE && pcs->ppcs->frm_hdr.skip_mode_params.skip_mode_flag &&
567
0
                is_comp_ref_allowed(blk_geom->bsize)
568
210k
            ? (uint64_t)ctx->md_rate_est_ctx->skip_mode_fac_bits[skip_mode_ctx][0]
569
210k
            : ZERO_COST;
570
        // Estimate luma nominal intra mode bits for key frame
571
210k
        intra_luma_mode_bits_num = pcs->slice_type == I_SLICE
572
210k
            ? (uint64_t)
573
210k
                  ctx->md_rate_est_ctx->y_mode_fac_bits[ctx->intra_luma_top_ctx][ctx->intra_luma_left_ctx][intra_mode]
574
210k
            : ZERO_COST;
575
        // Estimate luma angular mode bits
576
210k
        if (blk_geom->bsize >= BLOCK_8X8 && av1_is_directional_mode(cand->block_mi.mode)) {
577
0
            assert((intra_mode - V_PRED) < 8);
578
0
            assert((intra_mode - V_PRED) >= 0);
579
0
            intra_luma_ang_mode_bits_num =
580
0
                ctx->md_rate_est_ctx->angle_delta_fac_bits[intra_mode - V_PRED]
581
0
                                                          [MAX_ANGLE_DELTA + cand->block_mi.angle_delta[PLANE_TYPE_Y]];
582
0
        }
583
210k
        if (svt_aom_allow_palette(pcs->ppcs->frm_hdr.allow_screen_content_tools, blk_geom->bsize) &&
584
0
            intra_mode == DC_PRED) {
585
0
            const int use_palette = cand->palette_info ? (cand->palette_size[0] > 0) : 0;
586
0
            const int bsize_ctx   = svt_aom_get_palette_bsize_ctx(blk_geom->bsize);
587
0
            const int mode_ctx    = svt_aom_get_palette_mode_ctx(blk_ptr->av1xd);
588
0
            intra_luma_mode_bits_num += ctx->md_rate_est_ctx->palette_ymode_fac_bits[bsize_ctx][mode_ctx][use_palette];
589
0
            if (use_palette) {
590
0
                const uint8_t* const color_map = cand->palette_info->color_idx_map;
591
0
                int                  block_width, block_height, rows, cols;
592
0
                svt_aom_get_block_dimensions(
593
0
                    blk_geom->bsize, 0, blk_ptr->av1xd, &block_width, &block_height, &rows, &cols);
594
0
                const int plt_size = cand->palette_size[0];
595
0
                int       palette_mode_cost =
596
0
                    ctx->md_rate_est_ctx->palette_ysize_fac_bits[bsize_ctx][plt_size - PALETTE_MIN_SIZE] +
597
0
                    svt_aom_write_uniform_cost(plt_size, color_map[0]);
598
0
                uint16_t  color_cache[2 * PALETTE_MAX_SIZE];
599
0
                const int n_cache = svt_get_palette_cache_y(blk_ptr->av1xd, color_cache);
600
0
                palette_mode_cost += svt_av1_palette_color_cost_y(&cand->palette_info->pmi,
601
0
                                                                  color_cache,
602
0
                                                                  cand->palette_size[0],
603
0
                                                                  n_cache,
604
0
                                                                  pcs->ppcs->scs->encoder_bit_depth);
605
0
                palette_mode_cost += svt_av1_cost_color_map(
606
0
                    cand, ctx->md_rate_est_ctx, blk_ptr, 0, blk_geom->bsize, PALETTE_MAP);
607
0
                intra_luma_mode_bits_num += palette_mode_cost;
608
0
            }
609
0
        }
610
611
210k
        if (svt_aom_filter_intra_allowed(pcs->ppcs->scs->seq_header.filter_intra_level,
612
210k
                                         blk_geom->bsize,
613
210k
                                         cand->palette_info ? cand->palette_size[0] : 0,
614
210k
                                         intra_mode)) {
615
0
            intra_filter_mode_bits_num =
616
0
                ctx->md_rate_est_ctx
617
0
                    ->filter_intra_fac_bits[blk_geom->bsize][cand->block_mi.filter_intra_mode != FILTER_INTRA_MODES];
618
0
            if (cand->block_mi.filter_intra_mode != FILTER_INTRA_MODES) {
619
0
                intra_filter_mode_bits_num +=
620
0
                    ctx->md_rate_est_ctx->filter_intra_mode_fac_bits[cand->block_mi.filter_intra_mode];
621
0
            }
622
0
        }
623
210k
        if (ctx->has_uv) {
624
            // CFL info not known in fasta loop, so assume DC mode when CFL is allowed
625
210k
            chroma_rate = (uint32_t)svt_aom_get_intra_uv_fast_rate(pcs, ctx, cand_bf, 0);
626
210k
        }
627
628
210k
        uint32_t is_inter_rate = pcs->slice_type != I_SLICE
629
210k
            ? ctx->md_rate_est_ctx->intra_inter_fac_bits[ctx->is_inter_ctx][0]
630
210k
            : 0;
631
210k
        luma_rate              = (uint32_t)(intra_mode_bits_num + skip_mode_rate + intra_luma_mode_bits_num +
632
210k
                                            intra_luma_ang_mode_bits_num + is_inter_rate + intra_filter_mode_bits_num);
633
210k
        if (svt_aom_allow_intrabc(&pcs->ppcs->frm_hdr, pcs->ppcs->slice_type)) {
634
0
            svt_aom_assert_err(cand->block_mi.use_intrabc == 0, "this block ibc should be off\n");
635
0
            luma_rate += ctx->md_rate_est_ctx->intrabc_fac_bits[cand->block_mi.use_intrabc];
636
0
        }
637
        // Keep the Fast Luma and Chroma rate for future use
638
210k
        cand_bf->fast_luma_rate   = luma_rate;
639
210k
        cand_bf->fast_chroma_rate = chroma_rate;
640
210k
        rate                      = luma_rate + chroma_rate;
641
        // Assign fast cost
642
210k
        return (RDCOST(lambda, rate, luma_distortion));
643
210k
    }
644
210k
}
645
646
// This function encodes the reference frame
647
uint64_t estimate_ref_frame_type_bits(ModeDecisionContext* ctx, BlkStruct* blk_ptr, uint8_t ref_frame_type,
648
0
                                      bool is_compound) {
649
0
    uint64_t ref_rate_bits = 0;
650
651
0
    MbModeInfo* const mbmi = blk_ptr->av1xd->mi[0];
652
0
    MvReferenceFrame  ref_type[2];
653
0
    av1_set_ref_frame(ref_type, ref_frame_type);
654
0
    mbmi->block_mi.ref_frame[0] = ref_type[0];
655
0
    mbmi->block_mi.ref_frame[1] = ref_type[1];
656
    //const int is_compound = svt_aom_has_second_ref(mbmi);
657
0
    {
658
0
        if (is_compound) {
659
0
            const CompReferenceType comp_ref_type = has_uni_comp_refs(&mbmi->block_mi) ? UNIDIR_COMP_REFERENCE
660
0
                                                                                       : BIDIR_COMP_REFERENCE;
661
662
0
            ref_rate_bits += ctx->md_rate_est_ctx->comp_ref_type_fac_bits[svt_aom_get_comp_reference_type_context_new(
663
0
                blk_ptr->av1xd)][comp_ref_type];
664
            /*aom_write_symbol(w, comp_ref_type,
665
               svt_aom_get_comp_reference_type_cdf(blk_ptr->av1xd), 2);*/
666
667
0
            if (comp_ref_type == UNIDIR_COMP_REFERENCE) {
668
                // SVT_LOG("ERROR[AN]: UNIDIR_COMP_REFERENCE not supported\n");
669
0
                const int bit = mbmi->block_mi.ref_frame[0] == BWDREF_FRAME;
670
671
0
                ref_rate_bits += ctx->md_rate_est_ctx->uni_comp_ref_fac_bits[svt_av1_get_pred_context_uni_comp_ref_p(
672
0
                    blk_ptr->av1xd)][0][bit];
673
                // blk_ptr->av1xd->tile_ctx->uni_comp_ref_cdf[pred_context][0];
674
                // WRITE_REF_BIT(bit, uni_comp_ref_p);
675
676
0
                if (!bit) {
677
0
                    assert(mbmi->block_mi.ref_frame[0] == LAST_FRAME);
678
0
                    const int bit1 = mbmi->block_mi.ref_frame[1] == LAST3_FRAME ||
679
0
                        mbmi->block_mi.ref_frame[1] == GOLDEN_FRAME;
680
0
                    ref_rate_bits +=
681
0
                        ctx->md_rate_est_ctx
682
0
                            ->uni_comp_ref_fac_bits[svt_av1_get_pred_context_uni_comp_ref_p1(blk_ptr->av1xd)][1][bit1];
683
                    // ref_rate_d = blk_ptr->av1xd->tile_ctx->uni_comp_ref_cdf[pred_context][1];
684
                    // WRITE_REF_BIT(bit1, uni_comp_ref_p1);
685
0
                    if (bit1) {
686
0
                        const int bit2 = mbmi->block_mi.ref_frame[1] == GOLDEN_FRAME;
687
0
                        ref_rate_bits +=
688
0
                            ctx->md_rate_est_ctx->uni_comp_ref_fac_bits[svt_av1_get_pred_context_uni_comp_ref_p2(
689
0
                                blk_ptr->av1xd)][2][bit2];
690
691
                        // ref_rate_e = blk_ptr->av1xd->tile_ctx->uni_comp_ref_cdf[pred_context][2];
692
                        //WRITE_REF_BIT(bit2, uni_comp_ref_p2);
693
0
                    }
694
0
                }
695
0
                return ref_rate_bits;
696
0
            }
697
698
0
            assert(comp_ref_type == BIDIR_COMP_REFERENCE);
699
700
0
            const int bit = (mbmi->block_mi.ref_frame[0] == GOLDEN_FRAME || mbmi->block_mi.ref_frame[0] == LAST3_FRAME);
701
0
            const int pred_ctx = svt_av1_get_pred_context_comp_ref_p(blk_ptr->av1xd);
702
0
            ref_rate_bits += ctx->md_rate_est_ctx->comp_ref_fac_bits[pred_ctx][0][bit];
703
            // ref_rate_f = blk_ptr->av1xd->tile_ctx->comp_ref_cdf[pred_ctx][0];
704
            // WRITE_REF_BIT(bit, comp_ref_p);
705
706
0
            if (!bit) {
707
0
                const int bit1 = mbmi->block_mi.ref_frame[0] == LAST2_FRAME;
708
0
                ref_rate_bits += ctx->md_rate_est_ctx
709
0
                                     ->comp_ref_fac_bits[svt_av1_get_pred_context_comp_ref_p1(blk_ptr->av1xd)][1][bit1];
710
                // ref_rate_g = blk_ptr->av1xd->tile_ctx->comp_ref_cdf[pred_context][1];
711
                // WRITE_REF_BIT(bit1, comp_ref_p1);
712
0
            } else {
713
0
                const int bit2 = mbmi->block_mi.ref_frame[0] == GOLDEN_FRAME;
714
0
                ref_rate_bits += ctx->md_rate_est_ctx
715
0
                                     ->comp_ref_fac_bits[svt_av1_get_pred_context_comp_ref_p2(blk_ptr->av1xd)][2][bit2];
716
                // ref_rate_h = blk_ptr->av1xd->tile_ctx->comp_ref_cdf[pred_context][2];
717
                // WRITE_REF_BIT(bit2, comp_ref_p2);
718
0
            }
719
720
0
            const int bit_bwd    = mbmi->block_mi.ref_frame[1] == ALTREF_FRAME;
721
0
            const int pred_ctx_2 = svt_av1_get_pred_context_comp_bwdref_p(blk_ptr->av1xd);
722
0
            ref_rate_bits += ctx->md_rate_est_ctx->comp_bwd_ref_fac_bits[pred_ctx_2][0][bit_bwd];
723
            // ref_rate_i = blk_ptr->av1xd->tile_ctx->comp_bwdref_cdf[pred_ctx_2][0];
724
            // WRITE_REF_BIT(bit_bwd, comp_bwdref_p);
725
726
0
            if (!bit_bwd) {
727
0
                ref_rate_bits += ctx->md_rate_est_ctx->comp_bwd_ref_fac_bits[svt_av1_get_pred_context_comp_bwdref_p1(
728
0
                    blk_ptr->av1xd)][1][ref_type[1] == ALTREF2_FRAME];
729
                // ref_rate_j = blk_ptr->av1xd->tile_ctx->comp_bwdref_cdf[pred_context][1];
730
                // WRITE_REF_BIT(mbmi->block_mi.ref_frame[1] == ALTREF2_FRAME, comp_bwdref_p1);
731
0
            }
732
0
        } else {
733
0
            const int bit0 = (mbmi->block_mi.ref_frame[0] <= ALTREF_FRAME &&
734
0
                              mbmi->block_mi.ref_frame[0] >= BWDREF_FRAME);
735
0
            ref_rate_bits += ctx->md_rate_est_ctx
736
0
                                 ->single_ref_fac_bits[svt_av1_get_pred_context_single_ref_p1(blk_ptr->av1xd)][0][bit0];
737
            // ref_rate_k =
738
            // blk_ptr->av1xd->tile_ctx->single_ref_cdf[svt_av1_get_pred_context_single_ref_p1(blk_ptr->av1xd)][0];
739
            // WRITE_REF_BIT(bit0, single_ref_p1);
740
741
0
            if (bit0) {
742
0
                const int bit1 = mbmi->block_mi.ref_frame[0] == ALTREF_FRAME;
743
0
                ref_rate_bits += ctx->md_rate_est_ctx->single_ref_fac_bits[svt_av1_get_pred_context_single_ref_p2(
744
0
                    blk_ptr->av1xd)][1][bit1];
745
                // ref_rate_l =
746
                // blk_ptr->av1xd->tile_ctx->single_ref_cdf[svt_av1_get_pred_context_single_ref_p2(blk_ptr->av1xd)][1];
747
                // WRITE_REF_BIT(bit1, single_ref_p2);
748
0
                if (!bit1) {
749
0
                    ref_rate_bits += ctx->md_rate_est_ctx->single_ref_fac_bits[svt_av1_get_pred_context_single_ref_p6(
750
0
                        blk_ptr->av1xd)][5][ref_frame_type == ALTREF2_FRAME];
751
                    // ref_rate_m =
752
                    // blk_ptr->av1xd->tile_ctx->single_ref_cdf[svt_av1_get_pred_context_single_ref_p6(blk_ptr->av1xd)][5];
753
                    // WRITE_REF_BIT(mbmi->block_mi.ref_frame[0] == ALTREF2_FRAME, single_ref_p6);
754
0
                }
755
0
            } else {
756
0
                const int bit2 = (mbmi->block_mi.ref_frame[0] == LAST3_FRAME ||
757
0
                                  mbmi->block_mi.ref_frame[0] == GOLDEN_FRAME);
758
0
                ref_rate_bits += ctx->md_rate_est_ctx->single_ref_fac_bits[svt_av1_get_pred_context_single_ref_p3(
759
0
                    blk_ptr->av1xd)][2][bit2];
760
                // ref_rate_n =
761
                // blk_ptr->av1xd->tile_ctx->single_ref_cdf[svt_av1_get_pred_context_single_ref_p3(blk_ptr->av1xd)][2];
762
                // WRITE_REF_BIT(bit2, single_ref_p3);
763
0
                if (!bit2) {
764
0
                    const int bit3 = mbmi->block_mi.ref_frame[0] != LAST_FRAME;
765
0
                    ref_rate_bits += ctx->md_rate_est_ctx->single_ref_fac_bits[svt_av1_get_pred_context_single_ref_p4(
766
0
                        blk_ptr->av1xd)][3][bit3];
767
                    // ref_rate_o =
768
                    // blk_ptr->av1xd->tile_ctx->single_ref_cdf[svt_av1_get_pred_context_single_ref_p4(blk_ptr->av1xd)][3];
769
                    // WRITE_REF_BIT(bit3, single_ref_p4);
770
0
                } else {
771
0
                    const int bit4 = mbmi->block_mi.ref_frame[0] != LAST3_FRAME;
772
0
                    ref_rate_bits += ctx->md_rate_est_ctx->single_ref_fac_bits[svt_av1_get_pred_context_single_ref_p5(
773
0
                        blk_ptr->av1xd)][4][bit4];
774
                    // ref_rate_p =
775
                    // blk_ptr->av1xd->tile_ctx->single_ref_cdf[svt_av1_get_pred_context_single_ref_p5(blk_ptr->av1xd)][4];
776
                    // WRITE_REF_BIT(bit4, single_ref_p5);
777
0
                }
778
0
            }
779
0
        }
780
0
    }
781
0
    return ref_rate_bits;
782
0
}
783
784
int svt_aom_get_comp_group_idx_context_enc(const MacroBlockD* xd);
785
int is_any_masked_compound_used(BlockSize bsize);
786
787
static INLINE uint32_t get_compound_mode_rate(PictureControlSet* pcs, ModeDecisionContext* ctx,
788
0
                                              ModeDecisionCandidate* cand, BlockSize bsize) {
789
0
    BlkStruct*          blk_ptr   = ctx->blk_ptr;
790
0
    SequenceControlSet* scs       = pcs->ppcs->scs;
791
0
    uint32_t            comp_rate = 0;
792
0
    MbModeInfo* const   mbmi      = blk_ptr->av1xd->mi[0];
793
0
    MvReferenceFrame    rf[2]     = {cand->block_mi.ref_frame[0], cand->block_mi.ref_frame[1]};
794
0
    mbmi->block_mi.ref_frame[0]   = rf[0];
795
0
    mbmi->block_mi.ref_frame[1]   = rf[1];
796
797
    //NOTE  :  Make sure, any cuPtr data is already set before   usage
798
799
0
    if (has_second_ref(&mbmi->block_mi)) {
800
0
        const int masked_compound_used = is_any_masked_compound_used(bsize) && scs->seq_header.enable_masked_compound;
801
802
0
        if (masked_compound_used) {
803
0
            const int ctx_comp_group_idx = svt_aom_get_comp_group_idx_context_enc(blk_ptr->av1xd);
804
0
            comp_rate =
805
0
                ctx->md_rate_est_ctx->comp_group_idx_fac_bits[ctx_comp_group_idx][cand->block_mi.comp_group_idx];
806
0
        } else {
807
0
            assert(cand->block_mi.comp_group_idx == 0);
808
0
        }
809
810
0
        if (cand->block_mi.comp_group_idx == 0) {
811
0
            if (cand->block_mi.compound_idx) {
812
0
                assert(cand->block_mi.interinter_comp.type == COMPOUND_AVERAGE);
813
0
            }
814
815
0
            if (scs->seq_header.order_hint_info.enable_jnt_comp) {
816
0
                const int comp_index_ctx = svt_aom_get_comp_index_context_enc(pcs->ppcs,
817
0
                                                                              pcs->ppcs->cur_order_hint,
818
0
                                                                              pcs->ppcs->ref_order_hint[rf[0] - 1],
819
0
                                                                              pcs->ppcs->ref_order_hint[rf[1] - 1],
820
0
                                                                              blk_ptr->av1xd);
821
0
                comp_rate += ctx->md_rate_est_ctx->comp_idx_fac_bits[comp_index_ctx][cand->block_mi.compound_idx];
822
0
            } else {
823
0
                assert(cand->block_mi.compound_idx == 1);
824
0
            }
825
0
        } else {
826
0
            assert(pcs->ppcs->frm_hdr.reference_mode != SINGLE_REFERENCE &&
827
0
                   is_inter_compound_mode(cand->block_mi.mode));
828
0
            assert(masked_compound_used);
829
            // compound_diffwtd, wedge
830
0
            assert(cand->block_mi.interinter_comp.type == COMPOUND_WEDGE ||
831
0
                   cand->block_mi.interinter_comp.type == COMPOUND_DIFFWTD);
832
833
0
            if (is_interinter_compound_used(COMPOUND_WEDGE, bsize)) {
834
0
                comp_rate += ctx->md_rate_est_ctx
835
0
                                 ->compound_type_fac_bits[bsize][cand->block_mi.interinter_comp.type - COMPOUND_WEDGE];
836
0
            }
837
838
0
            if (cand->block_mi.interinter_comp.type == COMPOUND_WEDGE) {
839
0
                assert(is_interinter_compound_used(COMPOUND_WEDGE, bsize));
840
0
                comp_rate +=
841
0
                    ctx->md_rate_est_ctx->wedge_idx_fac_bits[bsize][cand->block_mi.interinter_comp.wedge_index];
842
0
                comp_rate += av1_cost_literal(1);
843
0
            } else {
844
0
                assert(cand->block_mi.interinter_comp.type == COMPOUND_DIFFWTD);
845
0
                comp_rate += av1_cost_literal(1);
846
0
            }
847
0
        }
848
0
    }
849
850
0
    return comp_rate;
851
0
}
852
853
int32_t svt_aom_get_switchable_rate(BlockModeInfo* block_mi, const FrameHeader* const frm_hdr, ModeDecisionContext* ctx,
854
0
                                    const bool enable_dual_filter) {
855
0
    if (frm_hdr->interpolation_filter != SWITCHABLE) {
856
0
        return 0;
857
0
    }
858
859
0
    int32_t   inter_filter_cost = 0;
860
0
    const int max_dir           = enable_dual_filter ? 2 : 1;
861
0
    for (int dir = 0; dir < max_dir; ++dir) {
862
0
        const int32_t pred_ctx = svt_aom_get_pred_context_switchable_interp(
863
0
            block_mi->ref_frame[0], block_mi->ref_frame[1], ctx->blk_ptr->av1xd, dir);
864
0
        const InterpFilter filter = av1_extract_interp_filter(block_mi->interp_filters, dir);
865
0
        assert(pred_ctx < SWITCHABLE_FILTER_CONTEXTS);
866
0
        assert(filter < SWITCHABLE_FILTERS);
867
0
        inter_filter_cost += ctx->md_rate_est_ctx->switchable_interp_fac_bitss[pred_ctx][filter];
868
0
    }
869
0
    return inter_filter_cost;
870
0
}
871
872
int svt_aom_is_interintra_wedge_used(BlockSize bsize);
873
874
static uint64_t av1_inter_fast_cost_light(ModeDecisionContext* ctx, BlkStruct* blk_ptr,
875
                                          ModeDecisionCandidateBuffer* cand_bf, uint64_t luma_distortion,
876
0
                                          uint64_t lambda, PictureControlSet* pcs, CandidateMv* ref_mv_stack) {
877
0
    ModeDecisionCandidate* cand = cand_bf->cand;
878
    // NM - fast inter cost estimation
879
0
    MdRateEstimationContext* r = ctx->md_rate_est_ctx;
880
    //_mm_prefetch(p, _MM_HINT_T2);
881
    // Luma rate
882
0
    uint32_t             luma_rate           = 0;
883
0
    uint64_t             mv_rate             = 0;
884
0
    const PredictionMode inter_mode          = (PredictionMode)cand->block_mi.mode;
885
0
    const uint8_t        have_nearmv         = have_nearmv_in_inter_mode(inter_mode);
886
0
    uint64_t             inter_mode_bits_num = 0;
887
0
    const uint8_t        skip_mode_ctx       = ctx->skip_mode_ctx;
888
0
    MvReferenceFrame     rf[2]               = {cand->block_mi.ref_frame[0], cand->block_mi.ref_frame[1]};
889
0
    const int8_t         ref_frame_type      = av1_ref_frame_type(rf);
890
0
    const uint8_t        is_compound         = is_inter_compound_mode(cand->block_mi.mode);
891
0
    const uint32_t       mode_context        = svt_aom_mode_context_analyzer(ctx->inter_mode_ctx[ref_frame_type], rf);
892
0
    uint64_t             reference_picture_bits_num = 0;
893
0
    if (ctx->approx_inter_rate < 2) {
894
0
        reference_picture_bits_num = ctx->estimate_ref_frames_num_bits[ref_frame_type];
895
0
    }
896
0
    if (is_compound) {
897
0
        assert(INTER_COMPOUND_OFFSET(inter_mode) < INTER_COMPOUND_MODES);
898
0
        inter_mode_bits_num += r->inter_compound_mode_fac_bits[mode_context][INTER_COMPOUND_OFFSET(inter_mode)];
899
0
    } else {
900
0
        int16_t newmv_ctx = mode_context & NEWMV_CTX_MASK;
901
        //aom_write_symbol(ec_writer, mode != NEWMV, frame_context->newmv_cdf[newmv_ctx], 2);
902
0
        inter_mode_bits_num += r->new_mv_mode_fac_bits[newmv_ctx][inter_mode != NEWMV];
903
0
        if (inter_mode != NEWMV) {
904
0
            const int16_t zero_mv_ctx = (mode_context >> GLOBALMV_OFFSET) & GLOBALMV_CTX_MASK;
905
            //aom_write_symbol(ec_writer, mode != GLOBALMV, frame_context->zeromv_cdf[zero_mv_ctx], 2);
906
0
            inter_mode_bits_num += r->zero_mv_mode_fac_bits[zero_mv_ctx][inter_mode != GLOBALMV];
907
0
            if (inter_mode != GLOBALMV) {
908
0
                int16_t ref_mv_ctx = (mode_context >> REFMV_OFFSET) & REFMV_CTX_MASK;
909
                /*aom_write_symbol(ec_writer, mode != NEARESTMV, frame_context->refmv_cdf[refmv_ctx], 2);*/
910
0
                inter_mode_bits_num += r->ref_mv_mode_fac_bits[ref_mv_ctx][inter_mode != NEARESTMV];
911
0
            }
912
0
        }
913
0
    }
914
0
    if (inter_mode == NEWMV || inter_mode == NEW_NEWMV || have_nearmv) {
915
        //drLIdex cost estimation
916
0
        const int32_t new_mv = inter_mode == NEWMV || inter_mode == NEW_NEWMV;
917
0
        if (new_mv) {
918
0
            int32_t idx;
919
0
            for (idx = 0; idx < 2; ++idx) {
920
0
                if (blk_ptr->av1xd->ref_mv_count[ref_frame_type] > idx + 1) {
921
0
                    uint8_t drl_1_ctx = av1_drl_ctx(ref_mv_stack, idx);
922
0
                    inter_mode_bits_num += r->drl_mode_fac_bits[drl_1_ctx][cand->drl_index != idx];
923
0
                    if (cand->drl_index == idx) {
924
0
                        break;
925
0
                    }
926
0
                }
927
0
            }
928
0
        }
929
0
        if (have_nearmv) {
930
0
            int32_t idx;
931
0
            for (idx = 1; idx < 3; ++idx) {
932
0
                if (blk_ptr->av1xd->ref_mv_count[ref_frame_type] > idx + 1) {
933
0
                    uint8_t drl_ctx = av1_drl_ctx(ref_mv_stack, idx);
934
0
                    inter_mode_bits_num += r->drl_mode_fac_bits[drl_ctx][cand->drl_index != (idx - 1)];
935
0
                    if (cand->drl_index == (idx - 1)) {
936
0
                        break;
937
0
                    }
938
0
                }
939
0
            }
940
0
        }
941
0
    }
942
0
    if (svt_aom_have_newmv_in_inter_mode(inter_mode)) {
943
0
        const uint16_t factor = pcs->ppcs->frm_hdr.allow_screen_content_tools ? 20 : 50;
944
0
        if (is_compound) {
945
0
            mv_rate = 0;
946
0
            if (inter_mode == NEW_NEWMV) {
947
0
                for (RefList ref_list_idx = 0; ref_list_idx < 2; ++ref_list_idx) {
948
0
                    Mv             mv         = cand->block_mi.mv[ref_list_idx];
949
0
                    Mv             ref_mv     = cand->pred_mv[ref_list_idx];
950
0
                    const uint16_t absmvdiffx = ABS(mv.x - ref_mv.x);
951
0
                    const uint16_t absmvdiffy = ABS(mv.y - ref_mv.y);
952
0
                    mv_rate += 1296 + (factor * (absmvdiffx + absmvdiffy));
953
0
                }
954
0
            } else if (inter_mode == NEAREST_NEWMV || inter_mode == NEAR_NEWMV) {
955
                // New MV is second ref
956
0
                Mv             mv         = cand->block_mi.mv[1];
957
0
                Mv             ref_mv     = cand->pred_mv[1];
958
0
                const uint16_t absmvdiffx = ABS(mv.x - ref_mv.x);
959
0
                const uint16_t absmvdiffy = ABS(mv.y - ref_mv.y);
960
0
                mv_rate += 1296 + (factor * (absmvdiffx + absmvdiffy));
961
0
            } else {
962
0
                assert(inter_mode == NEW_NEARESTMV || inter_mode == NEW_NEARMV);
963
                // New MV is first ref
964
0
                Mv             mv         = cand->block_mi.mv[0];
965
0
                Mv             ref_mv     = cand->pred_mv[0];
966
0
                const uint16_t absmvdiffx = ABS(mv.x - ref_mv.x);
967
0
                const uint16_t absmvdiffy = ABS(mv.y - ref_mv.y);
968
0
                mv_rate += 1296 + (factor * (absmvdiffx + absmvdiffy));
969
0
            }
970
0
        } else {
971
0
            assert(!is_compound); // single ref inter prediction
972
            // unipred MV stored in idx0
973
0
            Mv             mv         = cand->block_mi.mv[0];
974
0
            Mv             ref_mv     = cand->pred_mv[0];
975
0
            const uint16_t absmvdiffx = ABS(mv.x - ref_mv.x);
976
0
            const uint16_t absmvdiffy = ABS(mv.y - ref_mv.y);
977
0
            mv_rate += 1296 + (factor * (absmvdiffx + absmvdiffy));
978
0
        }
979
0
    }
980
    // Get the interpolation filter rate if IFS is performed at MDS0.  Otherwise, the filter is unknown, so the rate will be updated after IFS is performed.
981
0
    uint32_t ifs_rate = 0;
982
0
    if (ctx->ifs_ctrls.level == IFS_MDS0 &&
983
0
        av1_is_interp_needed_md(&cand_bf->cand->block_mi, pcs, ctx->blk_geom->bsize) &&
984
0
        pcs->ppcs->frm_hdr.interpolation_filter == SWITCHABLE) {
985
0
        ifs_rate = svt_aom_get_switchable_rate(
986
0
            &cand_bf->cand->block_mi, &pcs->ppcs->frm_hdr, ctx, pcs->scs->seq_header.enable_dual_filter);
987
0
    }
988
0
    uint32_t is_inter_rate = r->intra_inter_fac_bits[ctx->is_inter_ctx][1];
989
990
0
    uint32_t skip_mode_rate = pcs->ppcs->frm_hdr.skip_mode_params.skip_mode_flag &&
991
0
            is_comp_ref_allowed(ctx->blk_geom->bsize)
992
0
        ? r->skip_mode_fac_bits[skip_mode_ctx][0]
993
0
        : 0;
994
0
    luma_rate = (uint32_t)(reference_picture_bits_num + skip_mode_rate + inter_mode_bits_num + mv_rate + is_inter_rate +
995
0
                           ifs_rate);
996
    // Keep the Fast Luma and Chroma rate for future use
997
0
    cand_bf->fast_luma_rate   = luma_rate;
998
0
    cand_bf->fast_chroma_rate = 0;
999
    // Assign fast cost
1000
0
    if (cand->skip_mode_allowed) {
1001
0
        skip_mode_rate = r->skip_mode_fac_bits[skip_mode_ctx][1];
1002
0
        if (skip_mode_rate < luma_rate) {
1003
0
            return (RDCOST(lambda, skip_mode_rate, luma_distortion));
1004
0
        }
1005
0
    }
1006
0
    return (RDCOST(lambda, luma_rate, luma_distortion));
1007
0
}
1008
1009
uint64_t svt_aom_inter_fast_cost(PictureControlSet* pcs, ModeDecisionContext* ctx, ModeDecisionCandidateBuffer* cand_bf,
1010
0
                                 uint64_t lambda, uint64_t luma_distortion) {
1011
0
    const BlockGeom*       blk_geom       = ctx->blk_geom;
1012
0
    BlkStruct*             blk_ptr        = ctx->blk_ptr;
1013
0
    ModeDecisionCandidate* cand           = cand_bf->cand;
1014
0
    MvReferenceFrame       rf[2]          = {cand->block_mi.ref_frame[0], cand->block_mi.ref_frame[1]};
1015
0
    const int8_t           ref_frame_type = av1_ref_frame_type(cand->block_mi.ref_frame);
1016
0
    CandidateMv*           ref_mv_stack   = &(ctx->ref_mv_stack[ref_frame_type][0]);
1017
1018
0
    if (ctx->approx_inter_rate) {
1019
0
        return av1_inter_fast_cost_light(ctx, blk_ptr, cand_bf, luma_distortion, lambda, pcs, ref_mv_stack);
1020
0
    }
1021
0
    FrameHeader* frm_hdr = &pcs->ppcs->frm_hdr;
1022
1023
    // Luma rate
1024
0
    uint32_t       luma_rate  = 0;
1025
0
    uint64_t       mv_rate    = 0;
1026
0
    PredictionMode inter_mode = (PredictionMode)cand->block_mi.mode;
1027
1028
0
    uint64_t inter_mode_bits_num = 0;
1029
1030
0
    const uint8_t skip_mode_ctx              = ctx->skip_mode_ctx;
1031
0
    const uint8_t is_compound                = is_inter_compound_mode(cand->block_mi.mode);
1032
0
    uint32_t      mode_context               = svt_aom_mode_context_analyzer(ctx->inter_mode_ctx[ref_frame_type], rf);
1033
0
    uint64_t      reference_picture_bits_num = 0;
1034
1035
    //Reference Type and Mode Bit estimation
1036
0
    reference_picture_bits_num = ctx->estimate_ref_frames_num_bits[ref_frame_type];
1037
0
    if (is_compound) {
1038
0
        assert(INTER_COMPOUND_OFFSET(inter_mode) < INTER_COMPOUND_MODES);
1039
0
        inter_mode_bits_num +=
1040
0
            ctx->md_rate_est_ctx->inter_compound_mode_fac_bits[mode_context][INTER_COMPOUND_OFFSET(inter_mode)];
1041
0
    } else {
1042
        // uint32_t newmv_ctx = mode_context & NEWMV_CTX_MASK;
1043
        // inter_mode_bits_num = cand_bf->cand->md_rate_est_ctx->new_mv_mode_fac_bits[mode_ctx][0];
1044
1045
0
        int16_t newmv_ctx = mode_context & NEWMV_CTX_MASK;
1046
        // aom_write_symbol(ec_writer, mode != NEWMV, frame_context->newmv_cdf[newmv_ctx], 2);
1047
0
        inter_mode_bits_num += ctx->md_rate_est_ctx->new_mv_mode_fac_bits[newmv_ctx][inter_mode != NEWMV];
1048
0
        if (inter_mode != NEWMV) {
1049
0
            const int16_t zero_mv_ctx = (mode_context >> GLOBALMV_OFFSET) & GLOBALMV_CTX_MASK;
1050
            // aom_write_symbol(ec_writer, mode != GLOBALMV, frame_context->zeromv_cdf[zero_mv_ctx],
1051
            // 2);
1052
0
            inter_mode_bits_num += ctx->md_rate_est_ctx->zero_mv_mode_fac_bits[zero_mv_ctx][inter_mode != GLOBALMV];
1053
0
            if (inter_mode != GLOBALMV) {
1054
0
                int16_t ref_mv_ctx = (mode_context >> REFMV_OFFSET) & REFMV_CTX_MASK;
1055
                /*aom_write_symbol(ec_writer, mode != NEARESTMV,
1056
                 * frame_context->refmv_cdf[refmv_ctx], 2);*/
1057
0
                inter_mode_bits_num += ctx->md_rate_est_ctx->ref_mv_mode_fac_bits[ref_mv_ctx][inter_mode != NEARESTMV];
1058
0
            }
1059
0
        }
1060
0
    }
1061
0
    if (inter_mode == NEWMV || inter_mode == NEW_NEWMV || have_nearmv_in_inter_mode(inter_mode)) {
1062
        //drLIdex cost estimation
1063
0
        const int32_t new_mv = inter_mode == NEWMV || inter_mode == NEW_NEWMV;
1064
0
        if (new_mv) {
1065
0
            int32_t idx;
1066
0
            for (idx = 0; idx < 2; ++idx) {
1067
0
                if (blk_ptr->av1xd->ref_mv_count[ref_frame_type] > idx + 1) {
1068
0
                    uint8_t drl_1_ctx = av1_drl_ctx(ref_mv_stack, idx);
1069
0
                    inter_mode_bits_num += ctx->md_rate_est_ctx->drl_mode_fac_bits[drl_1_ctx][cand->drl_index != idx];
1070
0
                    if (cand->drl_index == idx) {
1071
0
                        break;
1072
0
                    }
1073
0
                }
1074
0
            }
1075
0
        }
1076
1077
0
        if (have_nearmv_in_inter_mode(inter_mode)) {
1078
0
            int32_t idx;
1079
0
            for (idx = 1; idx < 3; ++idx) {
1080
0
                if (blk_ptr->av1xd->ref_mv_count[ref_frame_type] > idx + 1) {
1081
0
                    uint8_t drl_ctx = av1_drl_ctx(ref_mv_stack, idx);
1082
0
                    inter_mode_bits_num +=
1083
0
                        ctx->md_rate_est_ctx->drl_mode_fac_bits[drl_ctx][cand->drl_index != (idx - 1)];
1084
1085
0
                    if (cand->drl_index == (idx - 1)) {
1086
0
                        break;
1087
0
                    }
1088
0
                }
1089
0
            }
1090
0
        }
1091
0
    }
1092
1093
0
    if (svt_aom_have_newmv_in_inter_mode(inter_mode)) {
1094
0
        if (is_compound) {
1095
0
            mv_rate = 0;
1096
1097
0
            if (inter_mode == NEW_NEWMV) {
1098
0
                for (RefList ref_list_idx = 0; ref_list_idx < 2; ++ref_list_idx) {
1099
0
                    Mv mv     = cand->block_mi.mv[ref_list_idx];
1100
0
                    Mv ref_mv = cand->pred_mv[ref_list_idx];
1101
0
                    mv_rate += svt_av1_mv_bit_cost(mv,
1102
0
                                                   ref_mv,
1103
0
                                                   ctx->md_rate_est_ctx->nmv_vec_cost,
1104
0
                                                   ctx->md_rate_est_ctx->nmvcoststack,
1105
0
                                                   MV_COST_WEIGHT);
1106
0
                }
1107
0
            } else if (inter_mode == NEAREST_NEWMV || inter_mode == NEAR_NEWMV) {
1108
0
                Mv mv     = cand->block_mi.mv[1];
1109
0
                Mv ref_mv = cand->pred_mv[1];
1110
0
                mv_rate += svt_av1_mv_bit_cost(
1111
0
                    mv, ref_mv, ctx->md_rate_est_ctx->nmv_vec_cost, ctx->md_rate_est_ctx->nmvcoststack, MV_COST_WEIGHT);
1112
0
            } else {
1113
0
                assert(inter_mode == NEW_NEARESTMV || inter_mode == NEW_NEARMV);
1114
0
                Mv mv     = cand->block_mi.mv[0];
1115
0
                Mv ref_mv = cand->pred_mv[0];
1116
0
                mv_rate += svt_av1_mv_bit_cost(
1117
0
                    mv, ref_mv, ctx->md_rate_est_ctx->nmv_vec_cost, ctx->md_rate_est_ctx->nmvcoststack, MV_COST_WEIGHT);
1118
0
            }
1119
0
        } else {
1120
0
            assert(!is_compound); // single ref inter prediction
1121
            // unipred MVs stored in idx0
1122
0
            Mv mv     = cand->block_mi.mv[0];
1123
0
            Mv ref_mv = cand->pred_mv[0];
1124
0
            mv_rate   = svt_av1_mv_bit_cost(
1125
0
                mv, ref_mv, ctx->md_rate_est_ctx->nmv_vec_cost, ctx->md_rate_est_ctx->nmvcoststack, MV_COST_WEIGHT);
1126
0
        }
1127
0
    }
1128
    // inter intra mode rate
1129
0
    if (pcs->ppcs->scs->seq_header.enable_interintra_compound &&
1130
        /* Check if inter-intra is allowed for current block size / mode (even if the feature is off
1131
        * for the current block, we still need to signal inter-intra off.
1132
        */
1133
0
        svt_is_interintra_allowed(true, blk_geom->bsize, cand->block_mi.mode, rf)) {
1134
0
        const int interintra  = cand->block_mi.is_interintra_used;
1135
0
        const int bsize_group = eb_size_group_lookup[blk_geom->bsize];
1136
1137
0
        inter_mode_bits_num +=
1138
0
            ctx->md_rate_est_ctx->inter_intra_fac_bits[bsize_group][cand->block_mi.is_interintra_used];
1139
1140
0
        if (interintra) {
1141
0
            inter_mode_bits_num +=
1142
0
                ctx->md_rate_est_ctx->inter_intra_mode_fac_bits[bsize_group][cand->block_mi.interintra_mode];
1143
1144
0
            if (svt_aom_is_interintra_wedge_used(blk_geom->bsize)) {
1145
0
                inter_mode_bits_num +=
1146
0
                    ctx->md_rate_est_ctx
1147
0
                        ->wedge_inter_intra_fac_bits[blk_geom->bsize][cand->block_mi.use_wedge_interintra];
1148
1149
0
                if (cand->block_mi.use_wedge_interintra) {
1150
0
                    inter_mode_bits_num +=
1151
0
                        ctx->md_rate_est_ctx
1152
0
                            ->wedge_idx_fac_bits[blk_geom->bsize][cand->block_mi.interintra_wedge_index];
1153
0
                }
1154
0
            }
1155
0
        }
1156
0
    }
1157
0
    if (is_inter_singleref_mode(inter_mode) && frm_hdr->is_motion_mode_switchable && rf[1] != INTRA_FRAME) {
1158
0
        assert(!cand->block_mi.is_interintra_used);
1159
0
        const MotionMode motion_mode_rd           = cand->block_mi.motion_mode;
1160
0
        const BlockSize  bsize                    = blk_geom->bsize;
1161
0
        const MotionMode last_motion_mode_allowed = svt_aom_motion_mode_allowed(
1162
0
            pcs, cand->block_mi.num_proj_ref, blk_ptr->overlappable_neighbors, bsize, rf[0], rf[1], inter_mode);
1163
0
        switch (last_motion_mode_allowed) {
1164
0
        case SIMPLE_TRANSLATION:
1165
0
            break;
1166
0
        case OBMC_CAUSAL:
1167
0
            inter_mode_bits_num += ctx->md_rate_est_ctx->motion_mode_fac_bits1[bsize][motion_mode_rd == OBMC_CAUSAL];
1168
0
            break;
1169
0
        default:
1170
0
            inter_mode_bits_num += ctx->md_rate_est_ctx->motion_mode_fac_bits[bsize][motion_mode_rd];
1171
0
        }
1172
0
    }
1173
    // this func return 0 if masked=0 and distance=0
1174
0
    inter_mode_bits_num += get_compound_mode_rate(pcs, ctx, cand, blk_geom->bsize);
1175
    // Get the interpolation filter rate if IFS is performed at MDS0.  Otherwise, the filter is unknown, so the rate will be updated after IFS is performed.
1176
0
    uint32_t ifs_rate = 0;
1177
0
    if (ctx->ifs_ctrls.level == IFS_MDS0 &&
1178
0
        av1_is_interp_needed_md(&cand_bf->cand->block_mi, pcs, ctx->blk_geom->bsize) &&
1179
0
        frm_hdr->interpolation_filter == SWITCHABLE) {
1180
0
        ifs_rate = svt_aom_get_switchable_rate(
1181
0
            &cand_bf->cand->block_mi, frm_hdr, ctx, pcs->scs->seq_header.enable_dual_filter);
1182
0
    }
1183
0
    uint32_t is_inter_rate  = ctx->md_rate_est_ctx->intra_inter_fac_bits[ctx->is_inter_ctx][1];
1184
0
    uint32_t skip_mode_rate = pcs->ppcs->frm_hdr.skip_mode_params.skip_mode_flag && is_comp_ref_allowed(blk_geom->bsize)
1185
0
        ? ctx->md_rate_est_ctx->skip_mode_fac_bits[skip_mode_ctx][0]
1186
0
        : 0;
1187
0
    luma_rate = (uint32_t)(reference_picture_bits_num + skip_mode_rate + inter_mode_bits_num + mv_rate + is_inter_rate +
1188
0
                           ifs_rate);
1189
    // Keep the Fast Luma and Chroma rate for future use
1190
0
    cand_bf->fast_luma_rate   = luma_rate;
1191
0
    cand_bf->fast_chroma_rate = 0;
1192
    // Assign fast cost
1193
0
    if (cand->skip_mode_allowed) {
1194
0
        skip_mode_rate = ctx->md_rate_est_ctx->skip_mode_fac_bits[skip_mode_ctx][1];
1195
0
        if (skip_mode_rate < luma_rate) {
1196
0
            return (RDCOST(lambda, skip_mode_rate, luma_distortion));
1197
0
        }
1198
0
    }
1199
0
    return (RDCOST(lambda, luma_rate, luma_distortion));
1200
0
}
1201
1202
/*
1203
 */
1204
EbErrorType svt_aom_txb_estimate_coeff_bits_pd0(ModeDecisionContext* ctx, ModeDecisionCandidateBuffer* cand_bf,
1205
                                                uint32_t txb_origin_index, EbPictureBufferDesc* coeff_buffer_sb,
1206
0
                                                uint32_t y_eob, uint64_t* y_txb_coeff_bits, TxSize txsize) {
1207
0
    if (y_eob) {
1208
0
        *y_txb_coeff_bits = svt_av1_cost_coeffs_txb(
1209
0
            ctx,
1210
0
            0,
1211
0
            0,
1212
0
            cand_bf,
1213
0
            (int32_t*)&coeff_buffer_sb->y_buffer[txb_origin_index * sizeof(int32_t)],
1214
0
            (uint16_t)y_eob,
1215
0
            PLANE_TYPE_Y,
1216
0
            txsize,
1217
0
            DCT_DCT,
1218
0
            0,
1219
0
            0,
1220
0
            0);
1221
1222
0
        *y_txb_coeff_bits = (*y_txb_coeff_bits) << ctx->mds_subres_step;
1223
1224
0
    } else {
1225
0
        *y_txb_coeff_bits = av1_cost_skip_txb(ctx, 0, 0, txsize, PLANE_TYPE_Y, 0);
1226
0
    }
1227
1228
0
    return EB_ErrorNone;
1229
0
}
1230
1231
EbErrorType svt_aom_txb_estimate_coeff_bits(ModeDecisionContext* ctx, uint8_t allow_update_cdf, FRAME_CONTEXT* ec_ctx,
1232
                                            PictureControlSet* pcs, ModeDecisionCandidateBuffer* cand_bf,
1233
                                            uint32_t txb_origin_index, uint32_t txb_chroma_origin_index,
1234
                                            EbPictureBufferDesc* coeff_buffer_sb, uint32_t y_eob, uint32_t cb_eob,
1235
                                            uint32_t cr_eob, uint64_t* y_txb_coeff_bits, uint64_t* cb_txb_coeff_bits,
1236
                                            uint64_t* cr_txb_coeff_bits, TxSize txsize, TxSize txsize_uv,
1237
0
                                            TxType tx_type, TxType tx_type_uv, COMPONENT_TYPE component_type) {
1238
0
    EbErrorType return_error = EB_ErrorNone;
1239
1240
0
    FrameHeader* frm_hdr = &pcs->ppcs->frm_hdr;
1241
1242
0
    int32_t* coeff_buffer;
1243
0
    int16_t  luma_txb_skip_context = ctx->luma_txb_skip_context;
1244
0
    int16_t  luma_dc_sign_context  = ctx->luma_dc_sign_context;
1245
0
    int16_t  cb_txb_skip_context   = ctx->cb_txb_skip_context;
1246
0
    int16_t  cb_dc_sign_context    = ctx->cb_dc_sign_context;
1247
0
    int16_t  cr_txb_skip_context   = ctx->cr_txb_skip_context;
1248
0
    int16_t  cr_dc_sign_context    = ctx->cr_dc_sign_context;
1249
1250
0
    bool reduced_transform_set_flag = frm_hdr->reduced_tx_set ? true : false;
1251
1252
    //Estimate the rate of the transform type and coefficient for Luma
1253
1254
0
    if (component_type == COMPONENT_LUMA || component_type == COMPONENT_ALL) {
1255
0
        if (y_eob) {
1256
0
            coeff_buffer = (int32_t*)&coeff_buffer_sb->y_buffer[txb_origin_index * sizeof(int32_t)];
1257
1258
0
            *y_txb_coeff_bits = svt_av1_cost_coeffs_txb(ctx,
1259
0
                                                        allow_update_cdf,
1260
0
                                                        ec_ctx,
1261
0
                                                        cand_bf,
1262
0
                                                        coeff_buffer,
1263
0
                                                        (uint16_t)y_eob,
1264
0
                                                        PLANE_TYPE_Y,
1265
0
                                                        txsize,
1266
0
                                                        tx_type,
1267
0
                                                        luma_txb_skip_context,
1268
0
                                                        luma_dc_sign_context,
1269
0
                                                        reduced_transform_set_flag);
1270
0
            *y_txb_coeff_bits = (*y_txb_coeff_bits) << ctx->mds_subres_step;
1271
0
        } else {
1272
0
            *y_txb_coeff_bits = av1_cost_skip_txb(
1273
0
                ctx, allow_update_cdf, ec_ctx, txsize, PLANE_TYPE_Y, luma_txb_skip_context);
1274
0
        }
1275
0
    }
1276
    // Estimate the rate of the transform type and coefficient for chroma Cb
1277
1278
0
    if (component_type == COMPONENT_CHROMA_CB || component_type == COMPONENT_CHROMA ||
1279
0
        component_type == COMPONENT_ALL) {
1280
0
        if (cb_eob) {
1281
0
            coeff_buffer = (int32_t*)&coeff_buffer_sb->u_buffer[txb_chroma_origin_index * sizeof(int32_t)];
1282
1283
0
            *cb_txb_coeff_bits = svt_av1_cost_coeffs_txb(ctx,
1284
0
                                                         allow_update_cdf,
1285
0
                                                         ec_ctx,
1286
0
                                                         cand_bf,
1287
0
                                                         coeff_buffer,
1288
0
                                                         (uint16_t)cb_eob,
1289
0
                                                         PLANE_TYPE_UV,
1290
0
                                                         txsize_uv,
1291
0
                                                         tx_type_uv,
1292
0
                                                         cb_txb_skip_context,
1293
0
                                                         cb_dc_sign_context,
1294
0
                                                         reduced_transform_set_flag);
1295
0
        } else {
1296
0
            *cb_txb_coeff_bits = av1_cost_skip_txb(
1297
0
                ctx, allow_update_cdf, ec_ctx, txsize_uv, PLANE_TYPE_UV, cb_txb_skip_context);
1298
0
        }
1299
0
    }
1300
1301
0
    if (component_type == COMPONENT_CHROMA_CR || component_type == COMPONENT_CHROMA ||
1302
0
        component_type == COMPONENT_ALL) {
1303
        //Estimate the rate of the transform type and coefficient for chroma Cr
1304
0
        if (cr_eob) {
1305
0
            coeff_buffer = (int32_t*)&coeff_buffer_sb->v_buffer[txb_chroma_origin_index * sizeof(int32_t)];
1306
1307
0
            *cr_txb_coeff_bits = svt_av1_cost_coeffs_txb(ctx,
1308
0
                                                         allow_update_cdf,
1309
0
                                                         ec_ctx,
1310
0
                                                         cand_bf,
1311
0
                                                         coeff_buffer,
1312
0
                                                         (uint16_t)cr_eob,
1313
0
                                                         PLANE_TYPE_UV,
1314
0
                                                         txsize_uv,
1315
0
                                                         tx_type_uv,
1316
0
                                                         cr_txb_skip_context,
1317
0
                                                         cr_dc_sign_context,
1318
0
                                                         reduced_transform_set_flag);
1319
0
        } else {
1320
0
            *cr_txb_coeff_bits = av1_cost_skip_txb(
1321
0
                ctx, allow_update_cdf, ec_ctx, txsize_uv, PLANE_TYPE_UV, cr_txb_skip_context);
1322
0
        }
1323
0
    }
1324
1325
0
    return return_error;
1326
0
}
1327
1328
EbErrorType svt_aom_full_cost_pd0(ModeDecisionContext* ctx, ModeDecisionCandidateBuffer* cand_bf,
1329
211k
                                  uint64_t* y_distortion, uint64_t lambda, uint64_t* y_coeff_bits) {
1330
211k
    EbErrorType return_error = EB_ErrorNone;
1331
1332
211k
    uint64_t coeff_rate = (*y_coeff_bits + (uint64_t)ctx->md_rate_est_ctx->skip_fac_bits[0][0]);
1333
1334
    // Assign full cost
1335
    // Use context index 0 for the partition rate as an approximation to skip call to
1336
    // av1_partition_rate_cost Partition cost is only needed for > 4x4 blocks, but light-PD0 assumes
1337
    // 4x4 blocks are disallowed
1338
211k
    *(cand_bf->full_cost) = RDCOST(
1339
211k
        lambda, coeff_rate + ctx->md_rate_est_ctx->partition_fac_bits[0][PARTITION_NONE], y_distortion[0]);
1340
211k
    return return_error;
1341
211k
}
1342
1343
/*********************************************************************************
1344
 * svt_aom_av1_full_cost function is used to estimate the cost of a candidate mode
1345
 * for full mode decision module.
1346
 **********************************************************************************/
1347
void svt_aom_full_cost(PictureControlSet* pcs, ModeDecisionContext* ctx, ModeDecisionCandidateBuffer* cand_bf,
1348
                       uint64_t lambda, uint64_t y_distortion[DIST_TOTAL][DIST_CALC_TOTAL],
1349
                       uint64_t cb_distortion[DIST_TOTAL][DIST_CALC_TOTAL],
1350
                       uint64_t cr_distortion[DIST_TOTAL][DIST_CALC_TOTAL], uint64_t* y_coeff_bits,
1351
211k
                       uint64_t* cb_coeff_bits, uint64_t* cr_coeff_bits) {
1352
211k
    const uint8_t skip_coeff_ctx        = ctx->skip_coeff_ctx;
1353
211k
    const bool    update_full_cost_ssim = ctx->tune_ssim_level > SSIM_LVL_0 ? true : false;
1354
1355
    // Get the TX size rate for skip and non-skip block. Need both to make non-skip decision
1356
211k
    uint64_t non_skip_tx_size_bits = 0, skip_tx_size_bits = 0;
1357
211k
    if (!ctx->shut_fast_rate && pcs->ppcs->frm_hdr.tx_mode == TX_MODE_SELECT) {
1358
211k
        if (cand_bf->block_has_coeff) {
1359
7.30k
            non_skip_tx_size_bits = svt_aom_get_tx_size_bits(
1360
7.30k
                cand_bf, ctx, pcs, cand_bf->cand->block_mi.tx_depth, /*cand_bf->block_has_coeff*/ 1);
1361
7.30k
        }
1362
1363
211k
        skip_tx_size_bits = svt_aom_get_tx_size_bits(
1364
211k
            cand_bf, ctx, pcs, cand_bf->cand->block_mi.tx_depth, /*cand_bf->block_has_coeff*/ 0);
1365
211k
    }
1366
1367
211k
    assert(IMPLIES(is_inter_mode(cand_bf->cand->block_mi.mode), skip_tx_size_bits == 0));
1368
1369
    // Decide if block should be signalled as skip (send no coeffs)
1370
211k
    if (!svt_av1_is_lossless_segment(pcs, ctx->blk_ptr->segment_id) && ctx->blk_skip_decision &&
1371
7.99k
        cand_bf->block_has_coeff && is_inter_mode(cand_bf->cand->block_mi.mode)) {
1372
0
        const uint64_t non_skip_cost = RDCOST(
1373
0
            lambda,
1374
0
            (*y_coeff_bits + *cb_coeff_bits + *cr_coeff_bits + non_skip_tx_size_bits +
1375
0
             (uint64_t)ctx->md_rate_est_ctx->skip_fac_bits[skip_coeff_ctx][0]),
1376
0
            (y_distortion[DIST_SSD][0] + cb_distortion[DIST_SSD][0] + cr_distortion[DIST_SSD][0]));
1377
1378
0
        const uint64_t skip_cost = RDCOST(
1379
0
            lambda,
1380
0
            ((uint64_t)ctx->md_rate_est_ctx->skip_fac_bits[skip_coeff_ctx][1]) + skip_tx_size_bits,
1381
0
            (y_distortion[DIST_SSD][1] + cb_distortion[DIST_SSD][1] + cr_distortion[DIST_SSD][1]));
1382
1383
        // Update signals to correspond to skip_mode values (no coeffs, etc.)
1384
0
        if (skip_cost < non_skip_cost) {
1385
0
            y_distortion[DIST_SSD][0]  = y_distortion[DIST_SSD][1];
1386
0
            cb_distortion[DIST_SSD][0] = cb_distortion[DIST_SSD][1];
1387
0
            cr_distortion[DIST_SSD][0] = cr_distortion[DIST_SSD][1];
1388
1389
0
            y_distortion[DIST_SSIM][0]  = y_distortion[DIST_SSIM][1];
1390
0
            cb_distortion[DIST_SSIM][0] = cb_distortion[DIST_SSIM][1];
1391
0
            cr_distortion[DIST_SSIM][0] = cr_distortion[DIST_SSIM][1];
1392
0
            cand_bf->block_has_coeff    = 0;
1393
0
            cand_bf->y_has_coeff        = 0;
1394
0
            cand_bf->u_has_coeff        = 0;
1395
0
            cand_bf->v_has_coeff        = 0;
1396
0
            cand_bf->cnt_nz_coeff       = 0;
1397
1398
            // For inter modes, signalling skip means no TX depth is used and the TX type will be DCT_DCT
1399
0
            cand_bf->cand->block_mi.tx_depth = 0;
1400
0
            cand_bf->cand->transform_type_uv = DCT_DCT;
1401
0
            memset(cand_bf->cand->transform_type, DCT_DCT, 16 * sizeof(cand_bf->cand->transform_type[0]));
1402
0
            memset(&cand_bf->quant_dc, 0, sizeof(QuantDcData));
1403
0
            memset(&cand_bf->eob, 0, sizeof(EobData));
1404
0
        }
1405
0
    }
1406
1407
211k
    uint64_t coeff_rate = 0;
1408
211k
    if (cand_bf->block_has_coeff) {
1409
7.30k
        coeff_rate = (*y_coeff_bits + *cb_coeff_bits + *cr_coeff_bits + non_skip_tx_size_bits +
1410
7.30k
                      (uint64_t)ctx->md_rate_est_ctx->skip_fac_bits[skip_coeff_ctx][0]);
1411
204k
    } else {
1412
204k
        coeff_rate = ctx->md_rate_est_ctx->skip_fac_bits[skip_coeff_ctx][1] + skip_tx_size_bits;
1413
204k
    }
1414
1415
211k
    uint64_t mode_rate            = cand_bf->fast_luma_rate + cand_bf->fast_chroma_rate + coeff_rate;
1416
211k
    uint64_t mode_distortion      = y_distortion[DIST_SSD][0] + cb_distortion[DIST_SSD][0] + cr_distortion[DIST_SSD][0];
1417
211k
    uint64_t mode_ssim_distortion = update_full_cost_ssim
1418
211k
        ? y_distortion[DIST_SSIM][0] + cb_distortion[DIST_SSIM][0] + cr_distortion[DIST_SSIM][0]
1419
211k
        : 0;
1420
211k
    uint64_t mode_cost            = RDCOST(lambda, mode_rate, mode_distortion);
1421
1422
    // If skip_mode is allowed for this candidate, check cost of skip mode compared to regular cost
1423
211k
    if (cand_bf->cand->skip_mode_allowed == true) {
1424
0
        const uint8_t skip_mode_ctx = ctx->skip_mode_ctx;
1425
1426
        // Skip mode cost
1427
0
        const uint64_t skip_mode_rate       = ctx->md_rate_est_ctx->skip_mode_fac_bits[skip_mode_ctx][1];
1428
0
        const uint64_t skip_mode_distortion = y_distortion[DIST_SSD][1] + cb_distortion[DIST_SSD][1] +
1429
0
            cr_distortion[DIST_SSD][1];
1430
0
        const uint64_t skip_mode_ssim_distortion = update_full_cost_ssim
1431
0
            ? y_distortion[DIST_SSIM][1] + cb_distortion[DIST_SSIM][1] + cr_distortion[DIST_SSIM][1]
1432
0
            : 0;
1433
0
        const uint64_t skip_mode_cost            = RDCOST(lambda, skip_mode_rate, skip_mode_distortion);
1434
1435
0
        cand_bf->cand->block_mi.skip_mode = false;
1436
0
        if (skip_mode_cost <= mode_cost) {
1437
            // Update candidate cost
1438
0
            mode_cost                         = skip_mode_cost;
1439
0
            mode_rate                         = skip_mode_rate;
1440
0
            mode_distortion                   = skip_mode_distortion;
1441
0
            mode_ssim_distortion              = skip_mode_ssim_distortion;
1442
0
            cand_bf->cand->block_mi.skip_mode = true;
1443
1444
            // Update signals to correspond to skip_mode values (no coeffs, etc.)
1445
0
            cand_bf->block_has_coeff         = 0;
1446
0
            cand_bf->y_has_coeff             = 0;
1447
0
            cand_bf->u_has_coeff             = 0;
1448
0
            cand_bf->v_has_coeff             = 0;
1449
0
            cand_bf->cnt_nz_coeff            = 0;
1450
0
            cand_bf->cand->block_mi.tx_depth = 0;
1451
0
            memset(cand_bf->cand->transform_type, DCT_DCT, 16 * sizeof(cand_bf->cand->transform_type[0]));
1452
0
            cand_bf->cand->transform_type_uv = DCT_DCT;
1453
0
            memset(&cand_bf->quant_dc, 0, sizeof(QuantDcData));
1454
0
            memset(&cand_bf->eob, 0, sizeof(EobData));
1455
0
        }
1456
0
    }
1457
1458
    // Assign full cost
1459
211k
    *(cand_bf->full_cost) = mode_cost;
1460
211k
    cand_bf->total_rate   = mode_rate;
1461
211k
    cand_bf->full_dist    = (uint32_t)mode_distortion;
1462
211k
    if (update_full_cost_ssim) {
1463
0
        assert(ctx->pd_pass == PD_PASS_1);
1464
0
        assert(ctx->md_stage == MD_STAGE_3);
1465
0
        *(cand_bf->full_cost_ssim) = RDCOST(lambda, mode_rate, mode_ssim_distortion);
1466
0
    }
1467
#if CONFIG_ENABLE_FAST_DECODE_3
1468
    // Decoder-complexity cost of the additional luma transform blocks; affects decisions only, not total_rate
1469
    if (ctx->dec_cost_ctrls.enabled) {
1470
        const uint32_t txb_count = tx_blocks_per_depth[ctx->blk_geom->bsize][cand_bf->cand->block_mi.tx_depth];
1471
        if (txb_count > 1) {
1472
            const uint64_t dec_cost = RDCOST(lambda, ctx->dec_cost_ctrls.txsplit_rate * (txb_count - 1), 0);
1473
            *(cand_bf->full_cost) += dec_cost;
1474
            if (update_full_cost_ssim) {
1475
                *(cand_bf->full_cost_ssim) += dec_cost;
1476
            }
1477
        }
1478
    }
1479
#endif
1480
211k
    return;
1481
211k
}
1482
1483
/************************************************************
1484
 * Coding Loop Context Generation
1485
 ************************************************************/
1486
209k
void svt_aom_coding_loop_context_generation(PictureControlSet* pcs, ModeDecisionContext* ctx) {
1487
209k
    BlkStruct*   blk_ptr = ctx->blk_ptr;
1488
209k
    MacroBlockD* xd      = blk_ptr->av1xd;
1489
209k
    if (!ctx->shut_fast_rate) {
1490
209k
        if (pcs->slice_type == I_SLICE) {
1491
209k
            svt_aom_get_kf_y_mode_ctx(xd, &ctx->intra_luma_top_ctx, &ctx->intra_luma_left_ctx);
1492
209k
        }
1493
209k
        ctx->is_inter_ctx  = svt_av1_get_intra_inter_context(xd);
1494
209k
        ctx->skip_mode_ctx = av1_get_skip_mode_context(xd);
1495
209k
    }
1496
    // Collect Neighbor ref cout
1497
    // At approx_inter_rate>=2, estimate_ref_frames_num_bits is skipped so ref counts
1498
    // are not consumed in MD. EC has its own call to collect_neighbors_ref_counts_new.
1499
210k
    if ((pcs->slice_type != I_SLICE || pcs->ppcs->frm_hdr.allow_intrabc) && ctx->approx_inter_rate < 2) {
1500
0
        svt_aom_collect_neighbors_ref_counts_new(blk_ptr->av1xd);
1501
0
    }
1502
1503
    // Skip Coeff Context
1504
209k
    ctx->skip_coeff_ctx = ctx->rate_est_ctrls.update_skip_coeff_ctx ? av1_get_skip_context(xd) : 0;
1505
209k
}
1506
1507
433k
static INLINE int block_signals_txsize(BlockSize bsize) {
1508
433k
    return bsize > BLOCK_4X4;
1509
433k
}
1510
1511
0
static INLINE int get_vartx_max_txsize(/*const MbModeInfo *xd,*/ BlockSize bsize, int plane) {
1512
    /* if (xd->lossless[xd->mi[0]->segment_id]) return TX_4X4;*/
1513
0
    const TxSize max_txsize = blocksize_to_txsize[bsize];
1514
0
    if (plane == 0) {
1515
0
        return max_txsize; // luma
1516
0
    }
1517
0
    return av1_get_adjusted_tx_size(max_txsize); // chroma
1518
0
}
1519
1520
0
static INLINE int max_block_wide(const MacroBlockD* xd, BlockSize bsize, int plane) {
1521
0
    int max_blocks_wide = block_size_wide[bsize];
1522
1523
0
    if (xd->mb_to_right_edge < 0) {
1524
0
        max_blocks_wide += xd->mb_to_right_edge >> (3 + !!plane);
1525
0
    }
1526
1527
    // Scale the width in the transform block unit.
1528
0
    return max_blocks_wide >> tx_size_wide_log2[0];
1529
0
}
1530
1531
0
static INLINE int max_block_high(const MacroBlockD* xd, BlockSize bsize, int plane) {
1532
0
    int max_blocks_high = block_size_high[bsize];
1533
1534
0
    if (xd->mb_to_bottom_edge < 0) {
1535
0
        max_blocks_high += xd->mb_to_bottom_edge >> (3 + !!plane);
1536
0
    }
1537
1538
    // Scale the height in the transform block unit.
1539
0
    return max_blocks_high >> tx_size_high_log2[0];
1540
0
}
1541
1542
static INLINE void txfm_partition_update(TXFM_CONTEXT* above_ctx, TXFM_CONTEXT* left_ctx, TxSize tx_size,
1543
0
                                         TxSize txb_size) {
1544
0
    BlockSize bsize = txsize_to_bsize[txb_size];
1545
0
    assert(bsize < BLOCK_SIZES_ALL);
1546
0
    int     bh  = mi_size_high[bsize];
1547
0
    int     bw  = mi_size_wide[bsize];
1548
0
    uint8_t txw = tx_size_wide[tx_size];
1549
0
    uint8_t txh = tx_size_high[tx_size];
1550
0
    int     i;
1551
0
    for (i = 0; i < bh; ++i) {
1552
0
        left_ctx[i] = txh;
1553
0
    }
1554
0
    for (i = 0; i < bw; ++i) {
1555
0
        above_ctx[i] = txw;
1556
0
    }
1557
0
}
1558
1559
0
static INLINE TxSize get_sqr_tx_size(int tx_dim) {
1560
0
    switch (tx_dim) {
1561
0
    case 128:
1562
0
    case 64:
1563
0
        return TX_64X64;
1564
0
        break;
1565
0
    case 32:
1566
0
        return TX_32X32;
1567
0
        break;
1568
0
    case 16:
1569
0
        return TX_16X16;
1570
0
        break;
1571
0
    case 8:
1572
0
        return TX_8X8;
1573
0
        break;
1574
0
    default:
1575
0
        return TX_4X4;
1576
0
    }
1577
0
}
1578
1579
static INLINE int txfm_partition_context(TXFM_CONTEXT* above_ctx, TXFM_CONTEXT* left_ctx, BlockSize bsize,
1580
0
                                         TxSize tx_size) {
1581
0
    const uint8_t txw      = tx_size_wide[tx_size];
1582
0
    const uint8_t txh      = tx_size_high[tx_size];
1583
0
    const int     above    = *above_ctx < txw;
1584
0
    const int     left     = *left_ctx < txh;
1585
0
    int           category = TXFM_PARTITION_CONTEXTS;
1586
1587
    // dummy return, not used by others.
1588
0
    if (tx_size == TX_4X4) {
1589
0
        return 0;
1590
0
    }
1591
1592
0
    TxSize max_tx_size = get_sqr_tx_size(AOMMAX(block_size_wide[bsize], block_size_high[bsize]));
1593
1594
0
    if (max_tx_size >= TX_8X8) {
1595
0
        category = (txsize_sqr_up_map[tx_size] != max_tx_size && max_tx_size > TX_8X8) +
1596
0
            (TX_SIZES - 1 - max_tx_size) * 2;
1597
0
    }
1598
0
    assert(category != TXFM_PARTITION_CONTEXTS);
1599
0
    return category * 3 + above + left;
1600
0
}
1601
1602
static uint64_t cost_tx_size_vartx(MacroBlockD* xd, const MbModeInfo* mbmi, TxSize tx_size, int depth, int blk_row,
1603
                                   int blk_col, MdRateEstimationContext* md_rate_est_ctx, FRAME_CONTEXT* ec_ctx,
1604
0
                                   uint8_t allow_update_cdf) {
1605
0
    uint64_t  bits            = 0;
1606
0
    const int max_blocks_high = max_block_high(xd, mbmi->bsize, 0);
1607
0
    const int max_blocks_wide = max_block_wide(xd, mbmi->bsize, 0);
1608
1609
0
    if (blk_row >= max_blocks_high || blk_col >= max_blocks_wide) {
1610
0
        return bits;
1611
0
    }
1612
1613
0
    if (depth == MAX_VARTX_DEPTH) {
1614
0
        txfm_partition_update(xd->above_txfm_context + blk_col, xd->left_txfm_context + blk_row, tx_size, tx_size);
1615
1616
0
        return bits;
1617
0
    }
1618
1619
0
    const int ctx = txfm_partition_context(
1620
0
        xd->above_txfm_context + blk_col, xd->left_txfm_context + blk_row, mbmi->bsize, tx_size);
1621
0
    const int write_txfm_partition = (tx_size == tx_depth_to_tx_size[mbmi->block_mi.tx_depth][mbmi->bsize]);
1622
0
    if (write_txfm_partition) {
1623
0
        bits += md_rate_est_ctx->txfm_partition_fac_bits[ctx][0];
1624
1625
0
        if (allow_update_cdf) {
1626
0
            update_cdf(ec_ctx->txfm_partition_cdf[ctx], 0, 2);
1627
0
        }
1628
1629
0
        txfm_partition_update(xd->above_txfm_context + blk_col, xd->left_txfm_context + blk_row, tx_size, tx_size);
1630
1631
0
    } else {
1632
0
        assert(tx_size < TX_SIZES_ALL);
1633
0
        const TxSize sub_txs = eb_sub_tx_size_map[tx_size];
1634
0
        const int    bsw     = eb_tx_size_wide_unit[sub_txs];
1635
0
        const int    bsh     = eb_tx_size_high_unit[sub_txs];
1636
1637
0
        bits += md_rate_est_ctx->txfm_partition_fac_bits[ctx][1];
1638
1639
0
        if (allow_update_cdf) {
1640
0
            update_cdf(ec_ctx->txfm_partition_cdf[ctx], 1, 2);
1641
0
        }
1642
1643
0
        if (sub_txs == TX_4X4) {
1644
0
            txfm_partition_update(xd->above_txfm_context + blk_col, xd->left_txfm_context + blk_row, sub_txs, tx_size);
1645
1646
0
            return bits;
1647
0
        }
1648
1649
0
        assert(bsw > 0 && bsh > 0);
1650
0
        for (int row = 0; row < eb_tx_size_high_unit[tx_size]; row += bsh) {
1651
0
            for (int col = 0; col < eb_tx_size_wide_unit[tx_size]; col += bsw) {
1652
0
                int offsetr = blk_row + row;
1653
0
                int offsetc = blk_col + col;
1654
0
                bits += cost_tx_size_vartx(
1655
0
                    xd, mbmi, sub_txs, depth + 1, offsetr, offsetc, md_rate_est_ctx, ec_ctx, allow_update_cdf);
1656
0
            }
1657
0
        }
1658
0
    }
1659
0
    return bits;
1660
0
}
1661
1662
838k
static INLINE void set_txfm_ctx(TXFM_CONTEXT* txfm_ctx, uint8_t txs, int len) {
1663
838k
    int i;
1664
2.68M
    for (i = 0; i < len; ++i) {
1665
1.84M
        txfm_ctx[i] = txs;
1666
1.84M
    }
1667
838k
}
1668
1669
420k
static INLINE void set_txfm_ctxs(TxSize tx_size, int n8_w, int n8_h, int skip, const MacroBlockD* xd) {
1670
420k
    uint8_t bw = tx_size_wide[tx_size];
1671
420k
    uint8_t bh = tx_size_high[tx_size];
1672
1673
420k
    if (skip) {
1674
0
        bw = n8_w * MI_SIZE;
1675
0
        bh = n8_h * MI_SIZE;
1676
0
    }
1677
1678
420k
    set_txfm_ctx(xd->above_txfm_context, bw, n8_w);
1679
420k
    set_txfm_ctx(xd->left_txfm_context, bh, n8_h);
1680
420k
}
1681
1682
11.9k
static INLINE int tx_size_to_depth(TxSize tx_size, BlockSize bsize) {
1683
11.9k
    TxSize ctx_size = blocksize_to_txsize[bsize];
1684
11.9k
    int    depth    = 0;
1685
11.9k
    while (tx_size != ctx_size) {
1686
0
        depth++;
1687
0
        ctx_size = eb_sub_tx_size_map[ctx_size];
1688
0
        assert(depth <= MAX_TX_DEPTH);
1689
0
    }
1690
11.9k
    return depth;
1691
11.9k
}
1692
1693
// Returns a context number for the given MB prediction signal
1694
// The mode info data structure has a one element border above and to the
1695
// left of the entries corresponding to real blocks.
1696
// The prediction flags in these dummy entries are initialized to 0.
1697
11.9k
static INLINE int get_tx_size_context(const MacroBlockD* xd) {
1698
11.9k
    const MbModeInfo*       mbmi        = xd->mi[0];
1699
11.9k
    const MbModeInfo* const above_mbmi  = xd->above_mbmi;
1700
11.9k
    const MbModeInfo* const left_mbmi   = xd->left_mbmi;
1701
11.9k
    const TxSize            max_tx_size = blocksize_to_txsize[mbmi->bsize];
1702
11.9k
    const int               max_tx_wide = tx_size_wide[max_tx_size];
1703
11.9k
    const int               max_tx_high = tx_size_high[max_tx_size];
1704
11.9k
    const int               has_above   = xd->up_available;
1705
11.9k
    const int               has_left    = xd->left_available;
1706
1707
11.9k
    int above = xd->above_txfm_context[0] >= max_tx_wide;
1708
11.9k
    int left  = xd->left_txfm_context[0] >= max_tx_high;
1709
1710
11.9k
    if (has_above) {
1711
3.13k
        if (is_inter_block(&above_mbmi->block_mi)) {
1712
0
            above = block_size_wide[above_mbmi->bsize] >= max_tx_wide;
1713
0
        }
1714
3.13k
    }
1715
1716
11.9k
    if (has_left) {
1717
3.18k
        if (is_inter_block(&left_mbmi->block_mi)) {
1718
0
            left = block_size_high[left_mbmi->bsize] >= max_tx_high;
1719
0
        }
1720
3.18k
    }
1721
1722
11.9k
    if (has_above && has_left) {
1723
167
        return (above + left);
1724
11.7k
    } else if (has_above) {
1725
2.96k
        return above;
1726
8.80k
    } else if (has_left) {
1727
3.01k
        return left;
1728
5.78k
    } else {
1729
5.78k
        return 0;
1730
5.78k
    }
1731
11.9k
}
1732
1733
static uint64_t cost_selected_tx_size(const MacroBlockD* xd, MdRateEstimationContext* md_rate_est_ctx, TxSize tx_size,
1734
11.9k
                                      FRAME_CONTEXT* ec_ctx, uint8_t allow_update_cdf) {
1735
11.9k
    const MbModeInfo* const mbmi  = xd->mi[0];
1736
11.9k
    const BlockSize         bsize = mbmi->bsize;
1737
11.9k
    uint64_t                bits  = 0;
1738
1739
11.9k
    if (block_signals_txsize(bsize)) {
1740
11.9k
        const int tx_size_ctx = get_tx_size_context(xd);
1741
11.9k
        assert(bsize < BLOCK_SIZES_ALL);
1742
11.9k
        const int     depth       = tx_size_to_depth(tx_size, bsize);
1743
11.9k
        const int32_t tx_size_cat = bsize_to_tx_size_cat(bsize);
1744
11.9k
        bits += md_rate_est_ctx->tx_size_fac_bits[tx_size_cat][tx_size_ctx][depth];
1745
1746
11.9k
        if (allow_update_cdf) {
1747
0
            const int max_depths = bsize_to_max_depth(bsize);
1748
0
            assert(depth >= 0 && depth <= max_depths);
1749
0
            assert(!is_inter_block(&mbmi->block_mi));
1750
0
            assert(IMPLIES(is_rect_tx(tx_size), is_rect_tx_allowed(/*xd,*/ mbmi)));
1751
0
            update_cdf(ec_ctx->tx_size_cdf[tx_size_cat][tx_size_ctx], depth, max_depths + 1);
1752
0
        }
1753
11.9k
    }
1754
1755
11.9k
    return bits;
1756
11.9k
}
1757
1758
/* Get the TXS rate and update the txfm context.  If allow_update_cdf is true, the TX size CDFs will
1759
be updated. */
1760
uint64_t svt_aom_tx_size_bits(PictureControlSet* pcs, uint8_t segment_id, MdRateEstimationContext* md_rate_est_ctx,
1761
                              MacroBlockD* xd, const MbModeInfo* mbmi, TxSize tx_size, TxMode tx_mode, BlockSize bsize,
1762
421k
                              uint8_t skip, FRAME_CONTEXT* ec_ctx, uint8_t allow_update_cdf) {
1763
421k
    uint64_t bits        = 0;
1764
421k
    int      is_inter_tx = is_inter_block(&mbmi->block_mi);
1765
421k
    if (tx_mode == TX_MODE_SELECT && block_signals_txsize(bsize) && !(is_inter_tx && skip) &&
1766
421k
        !svt_av1_is_lossless_segment(pcs, segment_id)) {
1767
11.9k
        if (is_inter_tx) { // This implies skip flag is 0.
1768
0
            const TxSize max_tx_size = get_vartx_max_txsize(/*xd,*/ bsize, 0);
1769
0
            const int    txbh        = eb_tx_size_high_unit[max_tx_size];
1770
0
            const int    txbw        = eb_tx_size_wide_unit[max_tx_size];
1771
0
            const int    width       = block_size_wide[bsize] >> tx_size_wide_log2[0];
1772
0
            const int    height      = block_size_high[bsize] >> tx_size_high_log2[0];
1773
0
            int          idx, idy;
1774
0
            for (idy = 0; idy < height; idy += txbh) {
1775
0
                for (idx = 0; idx < width; idx += txbw) {
1776
0
                    bits += cost_tx_size_vartx(
1777
0
                        xd, mbmi, max_tx_size, 0, idy, idx, md_rate_est_ctx, ec_ctx, allow_update_cdf);
1778
0
                }
1779
0
            }
1780
11.9k
        } else {
1781
11.9k
            bits += cost_selected_tx_size(xd, md_rate_est_ctx, tx_size, ec_ctx, allow_update_cdf);
1782
11.9k
            set_txfm_ctxs(tx_size, xd->n8_w, xd->n8_h, 0, xd);
1783
11.9k
        }
1784
409k
    } else {
1785
409k
        set_txfm_ctxs(tx_size, xd->n8_w, xd->n8_h, skip && is_inter_block(&mbmi->block_mi), xd);
1786
409k
    }
1787
1788
421k
    return bits;
1789
421k
}
1790
1791
/* Get the TXS rate.  A dummy txfm context array will be used, so context updates will not be saved for
1792
future blocks. */
1793
uint64_t svt_aom_get_tx_size_bits(ModeDecisionCandidateBuffer* candidateBuffer, ModeDecisionContext* ctx,
1794
421k
                                  PictureControlSet* pcs, uint8_t tx_depth, bool block_has_coeff) {
1795
421k
    NeighborArrayUnit* txfm_context_array = ctx->txfm_context_array;
1796
421k
    const uint8_t*     txfm_above_ptr     = svt_aom_na_top_ptr_pu(txfm_context_array, ctx->blk_org_x);
1797
421k
    const uint8_t*     txfm_left_ptr      = svt_aom_na_left_ptr_pu(txfm_context_array, ctx->blk_org_y);
1798
1799
421k
    TxMode       tx_mode = pcs->ppcs->frm_hdr.tx_mode;
1800
421k
    MacroBlockD* xd      = ctx->blk_ptr->av1xd;
1801
421k
    BlockSize    bsize   = ctx->blk_geom->bsize;
1802
421k
    const TxSize tx_size = tx_depth_to_tx_size[tx_depth][bsize];
1803
421k
    MbModeInfo*  mbmi    = xd->mi[0];
1804
1805
421k
    svt_memcpy(ctx->above_txfm_context, txfm_above_ptr, (ctx->blk_geom->bwidth >> MI_SIZE_LOG2) * sizeof(TXFM_CONTEXT));
1806
421k
    svt_memcpy(ctx->left_txfm_context, txfm_left_ptr, (ctx->blk_geom->bheight >> MI_SIZE_LOG2) * sizeof(TXFM_CONTEXT));
1807
1808
421k
    xd->above_txfm_context      = ctx->above_txfm_context;
1809
421k
    xd->left_txfm_context       = ctx->left_txfm_context;
1810
421k
    mbmi->bsize                 = ctx->blk_geom->bsize;
1811
421k
    mbmi->block_mi.use_intrabc  = candidateBuffer->cand->block_mi.use_intrabc;
1812
421k
    mbmi->block_mi.ref_frame[0] = candidateBuffer->cand->block_mi.ref_frame[0];
1813
421k
    mbmi->block_mi.tx_depth     = tx_depth;
1814
1815
421k
    const uint64_t bits = svt_aom_tx_size_bits(pcs,
1816
421k
                                               ctx->blk_ptr->segment_id,
1817
421k
                                               ctx->md_rate_est_ctx,
1818
421k
                                               xd,
1819
421k
                                               mbmi,
1820
421k
                                               tx_size,
1821
421k
                                               tx_mode,
1822
421k
                                               bsize,
1823
421k
                                               !block_has_coeff,
1824
421k
                                               NULL,
1825
421k
                                               0);
1826
421k
    return bits;
1827
421k
}
1828
1829
/*
1830
 * av1_partition_rate_cost function is used to generate the rate of signaling the
1831
 * partition type for a given block.
1832
 */
1833
int64_t svt_aom_partition_rate_cost(PictureParentControlSet* ppcs, const BlockSize bsize, const int mi_row,
1834
                                    const int mi_col, MdRateEstimationContext* md_rate_est_ctx, PartitionType p,
1835
367k
                                    const PartitionContextType left_ctx, const PartitionContextType above_ctx) {
1836
367k
    if (bsize < BLOCK_8X8) {
1837
0
        return 0;
1838
0
    }
1839
367k
    assert(bsize < BLOCK_SIZES_ALL && mi_size_wide_log2[bsize] == mi_size_high_log2[bsize]);
1840
1841
367k
    const int hbs      = mi_size_wide[bsize] >> 1;
1842
367k
    const int has_rows = (mi_row + hbs) < ppcs->av1_cm->mi_rows;
1843
367k
    const int has_cols = (mi_col + hbs) < ppcs->av1_cm->mi_cols;
1844
    // Don't consider invalid partitions or blocks outside the picture
1845
367k
    if (!has_rows && !has_cols) {
1846
1.22k
        return 0;
1847
1.22k
    }
1848
1849
366k
    const int bsl = mi_size_wide_log2[bsize] - mi_size_wide_log2[BLOCK_8X8];
1850
366k
    assert(bsl >= 0);
1851
1852
366k
    const int      above = (above_ctx >> bsl) & 1, left = (left_ctx >> bsl) & 1;
1853
366k
    const uint32_t context_index = (left * 2 + above) + bsl * PARTITION_PLOFFSET;
1854
1855
366k
    uint64_t split_rate = 0;
1856
1857
366k
    if (has_rows && has_cols) {
1858
342k
        split_rate = (uint64_t)md_rate_est_ctx->partition_fac_bits[context_index][p];
1859
342k
    } else if (!has_rows && has_cols) {
1860
        // 8x8 blocks will not use the split_or_horz or the split_or_vert paritition CDFs, per
1861
        // section 8.3.2 of the AV1 spec (Cdf selection process).  Therefore, only update partition ctx 4+,
1862
        // which corresponds to the paritition CDFs for 16x16 and larger blocks
1863
11.8k
        assert(bsize != BLOCK_8X8);
1864
11.8k
        split_rate = bsize == BLOCK_128X128
1865
11.8k
            ? (uint64_t)md_rate_est_ctx->partition_vert_alike_128x128_fac_bits[context_index][p == PARTITION_SPLIT]
1866
11.8k
            : (uint64_t)md_rate_est_ctx->partition_vert_alike_fac_bits[context_index][p == PARTITION_SPLIT];
1867
12.2k
    } else {
1868
        // 8x8 blocks will not use the split_or_horz or the split_or_vert paritition CDFs, per
1869
        // section 8.3.2 of the AV1 spec (Cdf selection process).  Therefore, only update partition ctx 4+,
1870
        // which corresponds to the paritition CDFs for 16x16 and larger blocks
1871
12.2k
        assert(bsize != BLOCK_8X8);
1872
12.2k
        split_rate = bsize == BLOCK_128X128
1873
12.2k
            ? (uint64_t)md_rate_est_ctx->partition_horz_alike_128x128_fac_bits[context_index][p == PARTITION_SPLIT]
1874
12.2k
            : (uint64_t)md_rate_est_ctx->partition_horz_alike_fac_bits[context_index][p == PARTITION_SPLIT];
1875
12.2k
    }
1876
1877
366k
    return split_rate;
1878
367k
}