Coverage Report

Created: 2026-07-16 06:32

next uncovered line (L), next uncovered region (R), next uncovered branch (B)
/work/svt-av1/Source/Lib/Codec/full_loop.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 3-Clause Clear License and
6
* the Alliance for Open Media Patent License 1.0. If the BSD 3-Clause Clear License
7
* was not distributed with this source code in the LICENSE file, you can
8
* obtain it at https://www.aomedia.org/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
#include "definitions.h"
14
#include "full_loop.h"
15
#include "pcs.h"
16
#include "rd_cost.h"
17
#include "aom_dsp_rtcd.h"
18
#include "sequence_control_set.h"
19
#include "utility.h"
20
#include "ac_bias.h"
21
22
const int av1_get_tx_scale_tab[TX_SIZES_ALL] = {0, 0, 0, 1, 2, 0, 0, 0, 0, 1, 1, 2, 2, 0, 0, 0, 0, 1, 1};
23
24
void     svt_aom_residual_kernel(uint8_t* input, uint32_t input_offset, uint32_t input_stride, uint8_t* pred,
25
                                 uint32_t pred_offset, uint32_t pred_stride, int16_t* residual, uint32_t residual_offset,
26
                                 uint32_t residual_stride, bool hbd, uint32_t area_width, uint32_t area_height);
27
uint64_t svt_spatial_full_distortion_ssim_kernel(uint8_t* input, uint32_t input_offset, uint32_t input_stride,
28
                                                 uint8_t* recon, int32_t recon_offset, uint32_t recon_stride,
29
                                                 uint32_t area_width, uint32_t area_height, bool hbd, double ac_bias);
30
31
void svt_aom_quantize_b_c(const TranLow* coeff_ptr, intptr_t n_coeffs, const int16_t* zbin_ptr,
32
                          const int16_t* round_ptr, const int16_t* quant_ptr, const int16_t* quant_shift_ptr,
33
                          TranLow* qcoeff_ptr, TranLow* dqcoeff_ptr, const int16_t* dequant_ptr, uint16_t* eob_ptr,
34
                          const int16_t* scan, const int16_t* iscan, const QmVal* qm_ptr, const QmVal* iqm_ptr,
35
942k
                          const int32_t log_scale) {
36
942k
    const int32_t zbins[2]  = {ROUND_POWER_OF_TWO(zbin_ptr[0], log_scale), ROUND_POWER_OF_TWO(zbin_ptr[1], log_scale)};
37
942k
    const int32_t nzbins[2] = {zbins[0] * -1, zbins[1] * -1};
38
942k
    intptr_t      non_zero_count = n_coeffs, eob = -1;
39
942k
    (void)iscan;
40
41
942k
    memset(qcoeff_ptr, 0, n_coeffs * sizeof(*qcoeff_ptr));
42
942k
    memset(dqcoeff_ptr, 0, n_coeffs * sizeof(*dqcoeff_ptr));
43
44
    // Pre-scan pass
45
25.3M
    for (intptr_t i = n_coeffs - 1; i >= 0; i--) {
46
24.5M
        const int32_t rc    = scan[i];
47
24.5M
        const QmVal   wt    = qm_ptr != NULL ? qm_ptr[rc] : (1 << AOM_QM_BITS);
48
24.5M
        const int32_t coeff = coeff_ptr[rc] * wt;
49
50
24.5M
        if (coeff < (zbins[rc != 0] * (1 << AOM_QM_BITS)) && coeff > (nzbins[rc != 0] * (1 << AOM_QM_BITS))) {
51
24.4M
            non_zero_count--;
52
24.4M
        } else {
53
124k
            break;
54
124k
        }
55
24.5M
    }
56
57
    // Quantization pass: All coefficients with index >= zero_flag are
58
    // skippable. Note: zero_flag can be zero.
59
953k
    for (intptr_t i = 0; i < non_zero_count; i++) {
60
11.3k
        const int32_t rc         = scan[i];
61
11.3k
        const int32_t coeff      = coeff_ptr[rc];
62
11.3k
        const int     coeff_sign = coeff < 0 ? -1 : 0;
63
11.3k
        const int32_t abs_coeff  = (coeff ^ coeff_sign) - coeff_sign;
64
65
11.3k
        const QmVal wt = qm_ptr != NULL ? qm_ptr[rc] : (1 << AOM_QM_BITS);
66
11.3k
        if (abs_coeff * wt >= (zbins[rc != 0] << AOM_QM_BITS)) {
67
11.3k
            int64_t tmp = clamp(abs_coeff + ROUND_POWER_OF_TWO(round_ptr[rc != 0], log_scale), INT16_MIN, INT16_MAX);
68
11.3k
            tmp *= wt;
69
11.3k
            int32_t tmp32         = (int32_t)(((((tmp * quant_ptr[rc != 0]) >> 16) + tmp) * quant_shift_ptr[rc != 0]) >>
70
11.3k
                                      (16 - log_scale + AOM_QM_BITS)); // quantization
71
11.3k
            qcoeff_ptr[rc]        = (tmp32 ^ coeff_sign) - coeff_sign;
72
11.3k
            const int32_t iwt     = iqm_ptr != NULL ? iqm_ptr[rc] : (1 << AOM_QM_BITS);
73
11.3k
            const int32_t dequant = (dequant_ptr[rc != 0] * iwt + (1 << (AOM_QM_BITS - 1))) >> AOM_QM_BITS;
74
11.3k
            const TranLow abs_dqcoeff = (tmp32 * dequant) >> log_scale;
75
11.3k
            dqcoeff_ptr[rc]           = (TranLow)((abs_dqcoeff ^ coeff_sign) - coeff_sign);
76
77
11.4k
            if (tmp32) {
78
11.4k
                eob = i;
79
11.4k
            }
80
11.3k
        }
81
11.3k
    }
82
942k
    *eob_ptr = (uint16_t)(eob + 1);
83
942k
}
84
85
void svt_aom_highbd_quantize_b_c(const TranLow* coeff_ptr, intptr_t n_coeffs, const int16_t* zbin_ptr,
86
                                 const int16_t* round_ptr, const int16_t* quant_ptr, const int16_t* quant_shift_ptr,
87
                                 TranLow* qcoeff_ptr, TranLow* dqcoeff_ptr, const int16_t* dequant_ptr,
88
                                 uint16_t* eob_ptr, const int16_t* scan, const int16_t* iscan, const QmVal* qm_ptr,
89
0
                                 const QmVal* iqm_ptr, const int32_t log_scale) {
90
0
    intptr_t eob = -1;
91
0
    (void)iscan;
92
93
0
    memset(qcoeff_ptr, 0, n_coeffs * sizeof(*qcoeff_ptr));
94
0
    memset(dqcoeff_ptr, 0, n_coeffs * sizeof(*dqcoeff_ptr));
95
96
0
    const int32_t zbins[2]  = {ROUND_POWER_OF_TWO(zbin_ptr[0], log_scale), ROUND_POWER_OF_TWO(zbin_ptr[1], log_scale)};
97
0
    const int32_t nzbins[2] = {zbins[0] * -1, zbins[1] * -1};
98
0
    intptr_t      idx_arr[4096];
99
0
    int           idx = 0;
100
    // Pre-scan pass
101
0
    for (intptr_t i = 0; i < n_coeffs; i++) {
102
0
        const int32_t rc    = scan[i];
103
0
        const QmVal   wt    = qm_ptr != NULL ? qm_ptr[rc] : (1 << AOM_QM_BITS);
104
0
        const int32_t coeff = coeff_ptr[rc] * wt;
105
106
        // If the coefficient is out of the base ZBIN range, keep it for
107
        // quantization.
108
0
        if (coeff >= (zbins[rc != 0] * (1 << AOM_QM_BITS)) || coeff <= (nzbins[rc != 0] * (1 << AOM_QM_BITS))) {
109
0
            idx_arr[idx++] = i;
110
0
        }
111
0
    }
112
113
    // Quantization pass: only process the coefficients selected in
114
    // pre-scan pass. Note: idx can be zero.
115
0
    for (int i = 0; i < idx; i++) {
116
0
        const int32_t rc          = scan[idx_arr[i]];
117
0
        const int32_t coeff       = coeff_ptr[rc];
118
0
        const int     coeff_sign  = coeff < 0 ? -1 : 0;
119
0
        const QmVal   wt          = qm_ptr != NULL ? qm_ptr[rc] : (1 << AOM_QM_BITS);
120
0
        const QmVal   iwt         = iqm_ptr != NULL ? iqm_ptr[rc] : (1 << AOM_QM_BITS);
121
0
        const int32_t abs_coeff   = (coeff ^ coeff_sign) - coeff_sign;
122
0
        const int64_t tmp1        = abs_coeff + ROUND_POWER_OF_TWO(round_ptr[rc != 0], log_scale);
123
0
        const int64_t tmpw        = tmp1 * wt;
124
0
        const int64_t tmp2        = ((tmpw * quant_ptr[rc != 0]) >> 16) + tmpw;
125
0
        const int32_t abs_qcoeff  = (int32_t)((tmp2 * quant_shift_ptr[rc != 0]) >> (16 - log_scale + AOM_QM_BITS));
126
0
        qcoeff_ptr[rc]            = (TranLow)((abs_qcoeff ^ coeff_sign) - coeff_sign);
127
0
        int32_t       dequant     = (dequant_ptr[rc != 0] * iwt + (1 << (AOM_QM_BITS - 1))) >> AOM_QM_BITS;
128
0
        const TranLow abs_dqcoeff = (abs_qcoeff * dequant) >> log_scale;
129
0
        dqcoeff_ptr[rc]           = (TranLow)((abs_dqcoeff ^ coeff_sign) - coeff_sign);
130
0
        if (abs_qcoeff) {
131
0
            eob = idx_arr[i];
132
0
        }
133
0
    }
134
135
0
    *eob_ptr = (uint16_t)(eob + 1);
136
0
}
137
138
#if CONFIG_ENABLE_HIGH_BIT_DEPTH
139
void svt_av1_highbd_quantize_b_facade(const TranLow* coeff_ptr, intptr_t n_coeffs, const MacroblockPlane* p,
140
                                      TranLow* qcoeff_ptr, TranLow* dqcoeff_ptr, uint16_t* eob_ptr, const ScanOrder* sc,
141
0
                                      const QuantParam* qparam) {
142
0
    const QmVal* qm_ptr  = qparam->qmatrix;
143
0
    const QmVal* iqm_ptr = qparam->iqmatrix;
144
0
    if (qm_ptr || iqm_ptr) {
145
0
        svt_av1_highbd_quantize_b_qm(coeff_ptr,
146
0
                                     n_coeffs,
147
0
                                     p->zbin_qtx,
148
0
                                     p->round_qtx,
149
0
                                     p->quant_qtx,
150
0
                                     p->quant_shift_qtx,
151
0
                                     qcoeff_ptr,
152
0
                                     dqcoeff_ptr,
153
0
                                     p->dequant_qtx,
154
0
                                     eob_ptr,
155
0
                                     sc->scan,
156
0
                                     sc->iscan,
157
0
                                     qm_ptr,
158
0
                                     iqm_ptr,
159
0
                                     qparam->log_scale);
160
0
    } else {
161
0
        svt_aom_highbd_quantize_b(coeff_ptr,
162
0
                                  n_coeffs,
163
0
                                  p->zbin_qtx,
164
0
                                  p->round_qtx,
165
0
                                  p->quant_qtx,
166
0
                                  p->quant_shift_qtx,
167
0
                                  qcoeff_ptr,
168
0
                                  dqcoeff_ptr,
169
0
                                  p->dequant_qtx,
170
0
                                  eob_ptr,
171
0
                                  sc->scan,
172
0
                                  sc->iscan,
173
0
                                  NULL,
174
0
                                  NULL,
175
0
                                  qparam->log_scale);
176
0
    }
177
0
    assert(qparam->log_scale <= 2);
178
0
}
179
#endif
180
181
static void av1_quantize_b_facade_ii(const TranLow* coeff_ptr, intptr_t n_coeffs, const MacroblockPlane* p,
182
                                     TranLow* qcoeff_ptr, TranLow* dqcoeff_ptr, uint16_t* eob_ptr, const ScanOrder* sc,
183
802k
                                     const QuantParam* qparam) {
184
802k
    const QmVal* qm_ptr  = qparam->qmatrix;
185
802k
    const QmVal* iqm_ptr = qparam->iqmatrix;
186
802k
    if (qm_ptr || iqm_ptr) {
187
0
        svt_av1_quantize_b_qm(coeff_ptr,
188
0
                              n_coeffs,
189
0
                              p->zbin_qtx,
190
0
                              p->round_qtx,
191
0
                              p->quant_qtx,
192
0
                              p->quant_shift_qtx,
193
0
                              qcoeff_ptr,
194
0
                              dqcoeff_ptr,
195
0
                              p->dequant_qtx,
196
0
                              eob_ptr,
197
0
                              sc->scan,
198
0
                              sc->iscan,
199
0
                              qm_ptr,
200
0
                              iqm_ptr,
201
0
                              qparam->log_scale);
202
802k
    } else {
203
802k
        svt_aom_quantize_b(coeff_ptr,
204
802k
                           n_coeffs,
205
802k
                           p->zbin_qtx,
206
802k
                           p->round_qtx,
207
802k
                           p->quant_qtx,
208
802k
                           p->quant_shift_qtx,
209
802k
                           qcoeff_ptr,
210
802k
                           dqcoeff_ptr,
211
802k
                           p->dequant_qtx,
212
802k
                           eob_ptr,
213
802k
                           sc->scan,
214
802k
                           sc->iscan,
215
802k
                           NULL,
216
802k
                           NULL,
217
802k
                           qparam->log_scale);
218
802k
    }
219
802k
    assert(qparam->log_scale <= 2);
220
802k
}
221
222
static void quantize_fp_helper_c(const TranLow* coeff_ptr, intptr_t n_coeffs, const int16_t* zbin_ptr,
223
                                 const int16_t* round_ptr, const int16_t* quant_ptr, const int16_t* quant_shift_ptr,
224
                                 TranLow* qcoeff_ptr, TranLow* dqcoeff_ptr, const int16_t* dequant_ptr,
225
                                 uint16_t* eob_ptr, const int16_t* scan, const int16_t* iscan, const QmVal* qm_ptr,
226
19.8k
                                 const QmVal* iqm_ptr, int log_scale) {
227
19.8k
    int       i, eob = -1;
228
19.8k
    const int rounding[2] = {ROUND_POWER_OF_TWO(round_ptr[0], log_scale), ROUND_POWER_OF_TWO(round_ptr[1], log_scale)};
229
19.8k
    (void)zbin_ptr;
230
19.8k
    (void)quant_shift_ptr;
231
19.8k
    (void)iscan;
232
233
19.8k
    memset(qcoeff_ptr, 0, n_coeffs * sizeof(*qcoeff_ptr));
234
19.8k
    memset(dqcoeff_ptr, 0, n_coeffs * sizeof(*dqcoeff_ptr));
235
236
19.8k
    if (qm_ptr == NULL && iqm_ptr == NULL) {
237
8.78M
        for (i = 0; i < n_coeffs; i++) {
238
8.76M
            const int     rc         = scan[i];
239
8.76M
            const int32_t thresh     = (int32_t)(dequant_ptr[rc != 0]);
240
8.76M
            const int     coeff      = coeff_ptr[rc];
241
8.76M
            const int     coeff_sign = coeff < 0 ? -1 : 0;
242
8.76M
            int64_t       abs_coeff  = (coeff ^ coeff_sign) - coeff_sign;
243
8.76M
            int           tmp32      = 0;
244
8.76M
            if ((abs_coeff << (1 + log_scale)) >= thresh) {
245
9.34k
                abs_coeff = clamp64(abs_coeff + rounding[rc != 0], INT16_MIN, INT16_MAX);
246
9.34k
                tmp32     = (int)((abs_coeff * quant_ptr[rc != 0]) >> (16 - log_scale));
247
9.34k
                if (tmp32) {
248
9.21k
                    qcoeff_ptr[rc]            = (tmp32 ^ coeff_sign) - coeff_sign;
249
9.21k
                    const TranLow abs_dqcoeff = (tmp32 * dequant_ptr[rc != 0]) >> log_scale;
250
9.21k
                    dqcoeff_ptr[rc]           = (abs_dqcoeff ^ coeff_sign) - coeff_sign;
251
9.21k
                }
252
9.34k
            }
253
8.76M
            if (tmp32) {
254
9.21k
                eob = i;
255
9.21k
            }
256
8.76M
        }
257
19.8k
    } else {
258
        // Quantization pass: All coefficients with index >= zero_flag are
259
        // skippable. Note: zero_flag can be zero.
260
5
        for (i = 0; i < n_coeffs; i++) {
261
0
            const int   rc         = scan[i];
262
0
            const int   coeff      = coeff_ptr[rc];
263
0
            const QmVal wt         = qm_ptr ? qm_ptr[rc] : (1 << AOM_QM_BITS);
264
0
            const QmVal iwt        = iqm_ptr ? iqm_ptr[rc] : (1 << AOM_QM_BITS);
265
0
            const int   dequant    = (dequant_ptr[rc != 0] * iwt + (1 << (AOM_QM_BITS - 1))) >> AOM_QM_BITS;
266
0
            const int   coeff_sign = coeff < 0 ? -1 : 0;
267
0
            int64_t     abs_coeff  = (coeff ^ coeff_sign) - coeff_sign;
268
0
            int         tmp32      = 0;
269
0
            if (abs_coeff * wt >= (dequant_ptr[rc != 0] << (AOM_QM_BITS - (1 + log_scale)))) {
270
0
                abs_coeff += rounding[rc != 0];
271
0
                abs_coeff      = clamp64(abs_coeff, INT16_MIN, INT16_MAX);
272
0
                tmp32          = (int)((abs_coeff * wt * quant_ptr[rc != 0]) >> (16 - log_scale + AOM_QM_BITS));
273
0
                qcoeff_ptr[rc] = (tmp32 ^ coeff_sign) - coeff_sign;
274
0
                const TranLow abs_dqcoeff = (tmp32 * dequant) >> log_scale;
275
0
                dqcoeff_ptr[rc]           = (abs_dqcoeff ^ coeff_sign) - coeff_sign;
276
0
            }
277
278
0
            if (tmp32) {
279
0
                eob = i;
280
0
            }
281
0
        }
282
5
    }
283
19.8k
    *eob_ptr = eob + 1;
284
19.8k
}
285
286
void svt_av1_quantize_fp_c(const TranLow* coeff_ptr, intptr_t n_coeffs, const int16_t* zbin_ptr,
287
                           const int16_t* round_ptr, const int16_t* quant_ptr, const int16_t* quant_shift_ptr,
288
                           TranLow* qcoeff_ptr, TranLow* dqcoeff_ptr, const int16_t* dequant_ptr, uint16_t* eob_ptr,
289
12.1k
                           const int16_t* scan, const int16_t* iscan) {
290
12.1k
    quantize_fp_helper_c(coeff_ptr,
291
12.1k
                         n_coeffs,
292
12.1k
                         zbin_ptr,
293
12.1k
                         round_ptr,
294
12.1k
                         quant_ptr,
295
12.1k
                         quant_shift_ptr,
296
12.1k
                         qcoeff_ptr,
297
12.1k
                         dqcoeff_ptr,
298
12.1k
                         dequant_ptr,
299
12.1k
                         eob_ptr,
300
12.1k
                         scan,
301
12.1k
                         iscan,
302
12.1k
                         NULL,
303
12.1k
                         NULL,
304
12.1k
                         0);
305
12.1k
}
306
307
void svt_av1_quantize_fp_qm_c(const TranLow* coeff_ptr, intptr_t n_coeffs, const int16_t* zbin_ptr,
308
                              const int16_t* round_ptr, const int16_t* quant_ptr, const int16_t* quant_shift_ptr,
309
                              TranLow* qcoeff_ptr, TranLow* dqcoeff_ptr, const int16_t* dequant_ptr, uint16_t* eob_ptr,
310
                              const int16_t* scan, const int16_t* iscan, const QmVal* qm_ptr, const QmVal* iqm_ptr,
311
0
                              int16_t log_scale) {
312
0
    quantize_fp_helper_c(coeff_ptr,
313
0
                         n_coeffs,
314
0
                         zbin_ptr,
315
0
                         round_ptr,
316
0
                         quant_ptr,
317
0
                         quant_shift_ptr,
318
0
                         qcoeff_ptr,
319
0
                         dqcoeff_ptr,
320
0
                         dequant_ptr,
321
0
                         eob_ptr,
322
0
                         scan,
323
0
                         iscan,
324
0
                         qm_ptr,
325
0
                         iqm_ptr,
326
0
                         log_scale);
327
0
}
328
329
static void highbd_quantize_fp_helper_c(const TranLow* coeff_ptr, intptr_t count, const int16_t* zbin_ptr,
330
                                        const int16_t* round_ptr, const int16_t* quant_ptr,
331
                                        const int16_t* quant_shift_ptr, TranLow* qcoeff_ptr, TranLow* dqcoeff_ptr,
332
                                        const int16_t* dequant_ptr, uint16_t* eob_ptr, const int16_t* scan,
333
                                        const int16_t* iscan, const QmVal* qm_ptr, const QmVal* iqm_ptr,
334
0
                                        int16_t log_scale) {
335
0
    int       i;
336
0
    int       eob   = -1;
337
0
    const int shift = 16 - log_scale;
338
0
    (void)zbin_ptr;
339
0
    (void)quant_shift_ptr;
340
0
    (void)iscan;
341
342
0
    if (qm_ptr || iqm_ptr) {
343
        // Quantization pass: All coefficients with index >= zero_flag are
344
        // skippable. Note: zero_flag can be zero.
345
0
        for (i = 0; i < count; i++) {
346
0
            const int     rc         = scan[i];
347
0
            const int     coeff      = coeff_ptr[rc];
348
0
            const QmVal   wt         = qm_ptr != NULL ? qm_ptr[rc] : (1 << AOM_QM_BITS);
349
0
            const QmVal   iwt        = iqm_ptr != NULL ? iqm_ptr[rc] : (1 << AOM_QM_BITS);
350
0
            const int     dequant    = (dequant_ptr[rc != 0] * iwt + (1 << (AOM_QM_BITS - 1))) >> AOM_QM_BITS;
351
0
            const int     coeff_sign = coeff < 0 ? -1 : 0;
352
0
            const int64_t abs_coeff  = (coeff ^ coeff_sign) - coeff_sign;
353
0
            if (abs_coeff * wt >= (dequant_ptr[rc != 0] << (AOM_QM_BITS - (1 + log_scale)))) {
354
0
                const int64_t tmp         = abs_coeff + ROUND_POWER_OF_TWO(round_ptr[rc != 0], log_scale);
355
0
                const int     abs_qcoeff  = (int)((tmp * quant_ptr[rc != 0] * wt) >> (shift + AOM_QM_BITS));
356
0
                qcoeff_ptr[rc]            = (TranLow)((abs_qcoeff ^ coeff_sign) - coeff_sign);
357
0
                const TranLow abs_dqcoeff = (abs_qcoeff * dequant) >> log_scale;
358
0
                dqcoeff_ptr[rc]           = (TranLow)((abs_dqcoeff ^ coeff_sign) - coeff_sign);
359
0
                if (abs_qcoeff) {
360
0
                    eob = i;
361
0
                }
362
0
            } else {
363
0
                qcoeff_ptr[rc]  = 0;
364
0
                dqcoeff_ptr[rc] = 0;
365
0
            }
366
0
        }
367
0
    } else {
368
0
        const int log_scaled_round_arr[2] = {
369
0
            ROUND_POWER_OF_TWO(round_ptr[0], log_scale),
370
0
            ROUND_POWER_OF_TWO(round_ptr[1], log_scale),
371
0
        };
372
0
        for (i = 0; i < count; i++) {
373
0
            const int rc               = scan[i];
374
0
            const int coeff            = coeff_ptr[rc];
375
0
            const int rc01             = (rc != 0);
376
0
            const int coeff_sign       = coeff < 0 ? -1 : 0;
377
0
            const int abs_coeff        = (coeff ^ coeff_sign) - coeff_sign;
378
0
            const int log_scaled_round = log_scaled_round_arr[rc01];
379
0
            if ((abs_coeff << (1 + log_scale)) >= dequant_ptr[rc01]) {
380
0
                const int     quant       = quant_ptr[rc01];
381
0
                const int     dequant     = dequant_ptr[rc01];
382
0
                const int64_t tmp         = (int64_t)abs_coeff + log_scaled_round;
383
0
                const int     abs_qcoeff  = (int)((tmp * quant) >> shift);
384
0
                qcoeff_ptr[rc]            = (TranLow)((abs_qcoeff ^ coeff_sign) - coeff_sign);
385
0
                const TranLow abs_dqcoeff = (abs_qcoeff * dequant) >> log_scale;
386
0
                if (abs_qcoeff) {
387
0
                    eob = i;
388
0
                }
389
0
                dqcoeff_ptr[rc] = (TranLow)((abs_dqcoeff ^ coeff_sign) - coeff_sign);
390
0
            } else {
391
0
                qcoeff_ptr[rc]  = 0;
392
0
                dqcoeff_ptr[rc] = 0;
393
0
            }
394
0
        }
395
0
    }
396
0
    *eob_ptr = eob + 1;
397
0
}
398
399
void svt_av1_highbd_quantize_fp_c(const TranLow* coeff_ptr, intptr_t count, const int16_t* zbin_ptr,
400
                                  const int16_t* round_ptr, const int16_t* quant_ptr, const int16_t* quant_shift_ptr,
401
                                  TranLow* qcoeff_ptr, TranLow* dqcoeff_ptr, const int16_t* dequant_ptr,
402
0
                                  uint16_t* eob_ptr, const int16_t* scan, const int16_t* iscan, int16_t log_scale) {
403
0
    highbd_quantize_fp_helper_c(coeff_ptr,
404
0
                                count,
405
0
                                zbin_ptr,
406
0
                                round_ptr,
407
0
                                quant_ptr,
408
0
                                quant_shift_ptr,
409
0
                                qcoeff_ptr,
410
0
                                dqcoeff_ptr,
411
0
                                dequant_ptr,
412
0
                                eob_ptr,
413
0
                                scan,
414
0
                                iscan,
415
0
                                NULL,
416
0
                                NULL,
417
0
                                log_scale);
418
0
}
419
420
void svt_av1_quantize_fp_32x32_c(const TranLow* coeff_ptr, intptr_t n_coeffs, const int16_t* zbin_ptr,
421
                                 const int16_t* round_ptr, const int16_t* quant_ptr, const int16_t* quant_shift_ptr,
422
                                 TranLow* qcoeff_ptr, TranLow* dqcoeff_ptr, const int16_t* dequant_ptr,
423
5.32k
                                 uint16_t* eob_ptr, const int16_t* scan, const int16_t* iscan) {
424
5.32k
    quantize_fp_helper_c(coeff_ptr,
425
5.32k
                         n_coeffs,
426
5.32k
                         zbin_ptr,
427
5.32k
                         round_ptr,
428
5.32k
                         quant_ptr,
429
5.32k
                         quant_shift_ptr,
430
5.32k
                         qcoeff_ptr,
431
5.32k
                         dqcoeff_ptr,
432
5.32k
                         dequant_ptr,
433
5.32k
                         eob_ptr,
434
5.32k
                         scan,
435
5.32k
                         iscan,
436
5.32k
                         NULL,
437
5.32k
                         NULL,
438
5.32k
                         1);
439
5.32k
}
440
441
void svt_av1_quantize_fp_64x64_c(const TranLow* coeff_ptr, intptr_t n_coeffs, const int16_t* zbin_ptr,
442
                                 const int16_t* round_ptr, const int16_t* quant_ptr, const int16_t* quant_shift_ptr,
443
                                 TranLow* qcoeff_ptr, TranLow* dqcoeff_ptr, const int16_t* dequant_ptr,
444
2.33k
                                 uint16_t* eob_ptr, const int16_t* scan, const int16_t* iscan) {
445
2.33k
    quantize_fp_helper_c(coeff_ptr,
446
2.33k
                         n_coeffs,
447
2.33k
                         zbin_ptr,
448
2.33k
                         round_ptr,
449
2.33k
                         quant_ptr,
450
2.33k
                         quant_shift_ptr,
451
2.33k
                         qcoeff_ptr,
452
2.33k
                         dqcoeff_ptr,
453
2.33k
                         dequant_ptr,
454
2.33k
                         eob_ptr,
455
2.33k
                         scan,
456
2.33k
                         iscan,
457
2.33k
                         NULL,
458
2.33k
                         NULL,
459
2.33k
                         2);
460
2.33k
}
461
462
void svt_av1_quantize_fp_facade(const TranLow* coeff_ptr, intptr_t n_coeffs, const MacroblockPlane* p,
463
                                TranLow* qcoeff_ptr, TranLow* dqcoeff_ptr, uint16_t* eob_ptr, const ScanOrder* sc,
464
19.8k
                                const QuantParam* qparam) {
465
19.8k
    const QmVal* qm_ptr  = qparam->qmatrix;
466
19.8k
    const QmVal* iqm_ptr = qparam->iqmatrix;
467
468
19.8k
    if (qm_ptr || iqm_ptr) {
469
0
        svt_av1_quantize_fp_qm(coeff_ptr,
470
0
                               n_coeffs,
471
0
                               p->zbin_qtx,
472
0
                               p->round_fp_qtx,
473
0
                               p->quant_fp_qtx,
474
0
                               p->quant_shift_qtx,
475
0
                               qcoeff_ptr,
476
0
                               dqcoeff_ptr,
477
0
                               p->dequant_qtx,
478
0
                               eob_ptr,
479
0
                               sc->scan,
480
0
                               sc->iscan,
481
0
                               qm_ptr,
482
0
                               iqm_ptr,
483
0
                               qparam->log_scale);
484
19.8k
    } else {
485
19.8k
        switch (qparam->log_scale) {
486
12.1k
        case 0:
487
12.1k
            svt_av1_quantize_fp(coeff_ptr,
488
12.1k
                                n_coeffs,
489
12.1k
                                p->zbin_qtx,
490
12.1k
                                p->round_fp_qtx,
491
12.1k
                                p->quant_fp_qtx,
492
12.1k
                                p->quant_shift_qtx,
493
12.1k
                                qcoeff_ptr,
494
12.1k
                                dqcoeff_ptr,
495
12.1k
                                p->dequant_qtx,
496
12.1k
                                eob_ptr,
497
12.1k
                                sc->scan,
498
12.1k
                                sc->iscan);
499
12.1k
            break;
500
5.32k
        case 1:
501
5.32k
            svt_av1_quantize_fp_32x32(coeff_ptr,
502
5.32k
                                      n_coeffs,
503
5.32k
                                      p->zbin_qtx,
504
5.32k
                                      p->round_fp_qtx,
505
5.32k
                                      p->quant_fp_qtx,
506
5.32k
                                      p->quant_shift_qtx,
507
5.32k
                                      qcoeff_ptr,
508
5.32k
                                      dqcoeff_ptr,
509
5.32k
                                      p->dequant_qtx,
510
5.32k
                                      eob_ptr,
511
5.32k
                                      sc->scan,
512
5.32k
                                      sc->iscan);
513
5.32k
            break;
514
2.33k
        case 2:
515
2.33k
            svt_av1_quantize_fp_64x64(coeff_ptr,
516
2.33k
                                      n_coeffs,
517
2.33k
                                      p->zbin_qtx,
518
2.33k
                                      p->round_fp_qtx,
519
2.33k
                                      p->quant_fp_qtx,
520
2.33k
                                      p->quant_shift_qtx,
521
2.33k
                                      qcoeff_ptr,
522
2.33k
                                      dqcoeff_ptr,
523
2.33k
                                      p->dequant_qtx,
524
2.33k
                                      eob_ptr,
525
2.33k
                                      sc->scan,
526
2.33k
                                      sc->iscan);
527
2.33k
            break;
528
0
        default:
529
0
            assert(0);
530
19.8k
        }
531
19.8k
    }
532
19.8k
}
533
534
#if CONFIG_ENABLE_HIGH_BIT_DEPTH
535
void svt_av1_highbd_quantize_fp_facade(const TranLow* coeff_ptr, intptr_t n_coeffs, const MacroblockPlane* p,
536
                                       TranLow* qcoeff_ptr, TranLow* dqcoeff_ptr, uint16_t* eob_ptr,
537
0
                                       const ScanOrder* sc, const QuantParam* qparam) {
538
0
    const QmVal* qm_ptr  = qparam->qmatrix;
539
0
    const QmVal* iqm_ptr = qparam->iqmatrix;
540
0
    if (qm_ptr != NULL && iqm_ptr != NULL) {
541
0
        svt_av1_highbd_quantize_fp_qm(coeff_ptr,
542
0
                                      n_coeffs,
543
0
                                      p->zbin_qtx,
544
0
                                      p->round_fp_qtx,
545
0
                                      p->quant_fp_qtx,
546
0
                                      p->quant_shift_qtx,
547
0
                                      qcoeff_ptr,
548
0
                                      dqcoeff_ptr,
549
0
                                      p->dequant_qtx,
550
0
                                      eob_ptr,
551
0
                                      sc->scan,
552
0
                                      sc->iscan,
553
0
                                      qm_ptr,
554
0
                                      iqm_ptr,
555
0
                                      qparam->log_scale);
556
0
    } else {
557
0
        svt_av1_highbd_quantize_fp(coeff_ptr,
558
0
                                   n_coeffs,
559
0
                                   p->zbin_qtx,
560
0
                                   p->round_fp_qtx,
561
0
                                   p->quant_fp_qtx,
562
0
                                   p->quant_shift_qtx,
563
0
                                   qcoeff_ptr,
564
0
                                   dqcoeff_ptr,
565
0
                                   p->dequant_qtx,
566
0
                                   eob_ptr,
567
0
                                   sc->scan,
568
0
                                   sc->iscan,
569
0
                                   qparam->log_scale);
570
0
    }
571
0
}
572
#endif
573
574
void svt_av1_highbd_quantize_fp_qm_c(const TranLow* coeff_ptr, intptr_t count, const int16_t* zbin_ptr,
575
                                     const int16_t* round_ptr, const int16_t* quant_ptr, const int16_t* quant_shift_ptr,
576
                                     TranLow* qcoeff_ptr, TranLow* dqcoeff_ptr, const int16_t* dequant_ptr,
577
                                     uint16_t* eob_ptr, const int16_t* scan, const int16_t* iscan, const QmVal* qm_ptr,
578
0
                                     const QmVal* iqm_ptr, int16_t log_scale) {
579
0
    highbd_quantize_fp_helper_c(coeff_ptr,
580
0
                                count,
581
0
                                zbin_ptr,
582
0
                                round_ptr,
583
0
                                quant_ptr,
584
0
                                quant_shift_ptr,
585
0
                                qcoeff_ptr,
586
0
                                dqcoeff_ptr,
587
0
                                dequant_ptr,
588
0
                                eob_ptr,
589
0
                                scan,
590
0
                                iscan,
591
0
                                qm_ptr,
592
0
                                iqm_ptr,
593
0
                                log_scale);
594
0
}
595
596
static INLINE int get_lower_levels_ctx_general(int is_last, int scan_idx, int bwl, int height, const uint8_t* levels,
597
7.66k
                                               int coeff_idx, TxSize tx_size, TxClass tx_class) {
598
7.66k
    if (is_last) {
599
7.66k
        if (scan_idx == 0) {
600
7.66k
            return 0;
601
7.66k
        }
602
0
        if (scan_idx <= (height << bwl) >> 3) {
603
0
            return 1;
604
0
        }
605
0
        if (scan_idx <= (height << bwl) >> 2) {
606
0
            return 2;
607
0
        }
608
0
        return 3;
609
0
    }
610
0
    return get_lower_levels_ctx(levels, coeff_idx, bwl, tx_size, tx_class);
611
7.66k
}
612
613
15.2k
static INLINE int32_t get_golomb_cost(int32_t abs_qc) {
614
15.2k
    if (abs_qc >= 1 + NUM_BASE_LEVELS + COEFF_BASE_RANGE) {
615
13.6k
        const int32_t r      = abs_qc - COEFF_BASE_RANGE - NUM_BASE_LEVELS;
616
13.6k
        const int32_t length = get_msb(r) + 1;
617
13.6k
        return av1_cost_literal(2 * length - 1);
618
13.6k
    }
619
1.59k
    return 0;
620
15.2k
}
621
622
15.2k
static INLINE int get_br_cost(TranLow level, const int* coeff_lps) {
623
15.2k
    const int base_range = AOMMIN(level - 1 - NUM_BASE_LEVELS, COEFF_BASE_RANGE);
624
15.2k
    return coeff_lps[base_range] + get_golomb_cost(level);
625
15.2k
}
626
627
static INLINE int get_coeff_cost_general(int is_last, int ci, TranLow abs_qc, int sign, int coeff_ctx, int dc_sign_ctx,
628
                                         const LvMapCoeffCost* txb_costs, int bwl, TxClass tx_class,
629
15.3k
                                         const uint8_t* levels) {
630
15.3k
    int cost = 0;
631
15.3k
    if (is_last) {
632
15.3k
        cost += txb_costs->base_eob_cost[coeff_ctx][AOMMIN(abs_qc, 3) - 1];
633
18.4E
    } else {
634
18.4E
        cost += txb_costs->base_cost[coeff_ctx][AOMMIN(abs_qc, 3)];
635
18.4E
    }
636
15.3k
    if (abs_qc != 0) {
637
15.3k
        if (ci == 0) {
638
15.3k
            cost += txb_costs->dc_sign_cost[dc_sign_ctx][sign];
639
15.3k
        } else {
640
0
            cost += av1_cost_literal(1);
641
0
        }
642
15.3k
        if (abs_qc > NUM_BASE_LEVELS) {
643
15.2k
            int br_ctx;
644
15.2k
            if (is_last) {
645
15.2k
                br_ctx = get_br_ctx_eob(ci, bwl, tx_class);
646
15.2k
            } else {
647
0
                br_ctx = get_br_ctx(levels, ci, bwl, tx_class);
648
0
            }
649
15.2k
            cost += get_br_cost(abs_qc, txb_costs->lps_cost[br_ctx]);
650
15.2k
        }
651
15.3k
    }
652
15.3k
    return cost;
653
15.3k
}
654
655
26.0k
static INLINE int64_t get_coeff_dist(TranLow tcoeff, TranLow dqcoeff, int shift) {
656
26.0k
    return SQR(((int64_t)tcoeff - dqcoeff) * (int64_t)(1lu << shift));
657
26.0k
}
658
659
7.66k
static INLINE void get_qc_dqc_low(TranLow abs_qc, int sign, int dqv, int shift, TranLow* qc_low, TranLow* dqc_low) {
660
7.66k
    TranLow abs_qc_low = abs_qc - 1;
661
7.66k
    *qc_low            = (-sign ^ abs_qc_low) + sign;
662
7.66k
    assert((sign ? -abs_qc_low : abs_qc_low) == *qc_low);
663
7.66k
    TranLow abs_dqc_low = (abs_qc_low * dqv) >> shift;
664
7.66k
    *dqc_low            = (-sign ^ abs_dqc_low) + sign;
665
7.66k
    assert((sign ? -abs_dqc_low : abs_dqc_low) == *dqc_low);
666
7.66k
}
667
668
static const int golomb_bits_cost[32] = {0,       512,     512 * 3, 512 * 3, 512 * 5, 512 * 5, 512 * 5, 512 * 5,
669
                                         512 * 7, 512 * 7, 512 * 7, 512 * 7, 512 * 7, 512 * 7, 512 * 7, 512 * 7,
670
                                         512 * 9, 512 * 9, 512 * 9, 512 * 9, 512 * 9, 512 * 9, 512 * 9, 512 * 9,
671
                                         512 * 9, 512 * 9, 512 * 9, 512 * 9, 512 * 9, 512 * 9, 512 * 9, 512 * 9};
672
static const int golomb_cost_diff[32] = {0,       512, 512 * 2, 0, 512 * 2, 0, 0, 0, 512 * 2, 0, 0, 0, 0, 0, 0, 0,
673
                                         512 * 2, 0,   0,       0, 0,       0, 0, 0, 0,       0, 0, 0, 0, 0, 0, 0};
674
675
0
static INLINE int get_br_cost_with_diff(TranLow level, const int* coeff_lps, int* diff) {
676
0
    const int base_range  = AOMMIN(level - 1 - NUM_BASE_LEVELS, COEFF_BASE_RANGE);
677
0
    int       golomb_bits = 0;
678
0
    if (level <= COEFF_BASE_RANGE + 1 + NUM_BASE_LEVELS) {
679
0
        *diff += coeff_lps[base_range + COEFF_BASE_RANGE + 1];
680
0
    }
681
682
0
    if (level >= COEFF_BASE_RANGE + 1 + NUM_BASE_LEVELS) {
683
0
        int r = level - COEFF_BASE_RANGE - NUM_BASE_LEVELS;
684
0
        if (r < 32) {
685
0
            golomb_bits = golomb_bits_cost[r];
686
0
            *diff += golomb_cost_diff[r];
687
0
        } else {
688
0
            golomb_bits = get_golomb_cost(level);
689
0
            *diff += (r & (r - 1)) == 0 ? 1024 : 0;
690
0
        }
691
0
    }
692
693
0
    return coeff_lps[base_range] + golomb_bits;
694
0
}
695
696
static AOM_FORCE_INLINE int get_two_coeff_cost_simple(int ci, TranLow abs_qc, int coeff_ctx,
697
                                                      const LvMapCoeffCost* txb_costs, int bwl, TxClass tx_class,
698
0
                                                      const uint8_t* levels, int* cost_low) {
699
    // this simple version assumes the coeff's scan_idx is not DC (scan_idx != 0)
700
    // and not the last (scan_idx != eob - 1)
701
0
    assert(ci > 0);
702
    //assert(abs_qc + 4 < 4);
703
0
    int cost = txb_costs->base_cost[coeff_ctx][AOMMIN(abs_qc, 3)];
704
0
    int diff = 0;
705
0
    if (abs_qc <= 3) {
706
0
        diff = txb_costs->base_cost[coeff_ctx][abs_qc + 4];
707
0
    }
708
0
    if (abs_qc) {
709
0
        cost += av1_cost_literal(1);
710
0
        if (abs_qc > NUM_BASE_LEVELS) {
711
0
            const int br_ctx      = get_br_ctx(levels, ci, bwl, tx_class);
712
0
            int       brcost_diff = 0;
713
0
            cost += get_br_cost_with_diff(abs_qc, txb_costs->lps_cost[br_ctx], &brcost_diff);
714
0
            diff += brcost_diff;
715
0
        }
716
0
    }
717
0
    *cost_low = cost - diff;
718
719
0
    return cost;
720
0
}
721
722
static INLINE int get_coeff_cost_eob(int ci, TranLow abs_qc, int sign, int coeff_ctx, int dc_sign_ctx,
723
1.54k
                                     const LvMapCoeffCost* txb_costs, int bwl, TxClass tx_class) {
724
1.54k
    int cost = 0;
725
1.54k
    cost += txb_costs->base_eob_cost[coeff_ctx][AOMMIN(abs_qc, 3) - 1];
726
1.54k
    if (abs_qc != 0) {
727
1.54k
        if (ci == 0) {
728
1.54k
            cost += txb_costs->dc_sign_cost[dc_sign_ctx][sign];
729
1.54k
        } else {
730
0
            cost += av1_cost_literal(1);
731
0
        }
732
1.54k
        if (abs_qc > NUM_BASE_LEVELS) {
733
0
            int br_ctx;
734
0
            br_ctx = get_br_ctx_eob(ci, bwl, tx_class);
735
0
            cost += get_br_cost(abs_qc, txb_costs->lps_cost[br_ctx]);
736
0
        }
737
1.54k
    }
738
1.54k
    return cost;
739
1.54k
}
740
741
7.66k
static INLINE int get_dqv(const int16_t* dequant, int coeff_idx, const QmVal* iqm_ptr) {
742
7.66k
    int dqv = dequant[!!coeff_idx];
743
7.66k
    if (iqm_ptr != NULL) {
744
0
        dqv = ((iqm_ptr[coeff_idx] * dqv) + (1 << (AOM_QM_BITS - 1))) >> AOM_QM_BITS;
745
0
    }
746
7.66k
    return dqv;
747
7.66k
}
748
749
static AOM_FORCE_INLINE void update_coeff_eob(int* accu_rate, int64_t* accu_dist, uint16_t* eob, int* nz_num,
750
                                              int* nz_ci, int si, TxSize tx_size, TxClass tx_class, int bwl, int height,
751
                                              int dc_sign_ctx, int64_t rdmult, int shift, const int16_t* dequant,
752
                                              const int16_t* scan, const LvMapEobCost* txb_eob_costs,
753
                                              const LvMapCoeffCost* txb_costs, const TranLow* tcoeff, TranLow* qcoeff,
754
0
                                              TranLow* dqcoeff, uint8_t* levels, int sharpness, const QmVal* iqm_ptr) {
755
0
    assert(si != *eob - 1);
756
0
    const int     ci        = scan[si];
757
0
    const int     dqv       = get_dqv(dequant, ci, iqm_ptr);
758
0
    const TranLow qc        = qcoeff[ci];
759
0
    const int     coeff_ctx = get_lower_levels_ctx(levels, ci, bwl, tx_size, tx_class);
760
0
    if (qc == 0) {
761
0
        *accu_rate += txb_costs->base_cost[coeff_ctx][0];
762
0
    } else {
763
0
        int           lower_level = 0;
764
0
        const TranLow abs_qc      = abs(qc);
765
0
        const TranLow tqc         = tcoeff[ci];
766
0
        const TranLow dqc         = dqcoeff[ci];
767
0
        const int     sign        = (qc < 0) ? 1 : 0;
768
0
        const int64_t dist0       = get_coeff_dist(tqc, 0, shift);
769
0
        int64_t       dist        = get_coeff_dist(tqc, dqc, shift) - dist0;
770
0
        int           rate        = get_coeff_cost_general(
771
0
            0, ci, abs_qc, sign, coeff_ctx, dc_sign_ctx, txb_costs, bwl, tx_class, levels);
772
0
        int64_t rd = RDCOST(rdmult, *accu_rate + rate, *accu_dist + dist);
773
774
0
        TranLow qc_low, dqc_low;
775
0
        TranLow abs_qc_low;
776
0
        int64_t dist_low, rd_low;
777
0
        int     rate_low;
778
0
        if (abs_qc == 1) {
779
0
            abs_qc_low = 0;
780
0
            dqc_low = qc_low = 0;
781
0
            dist_low         = 0;
782
0
            rate_low         = txb_costs->base_cost[coeff_ctx][0];
783
0
            rd_low           = RDCOST(rdmult, *accu_rate + rate_low, *accu_dist);
784
0
        } else {
785
0
            get_qc_dqc_low(abs_qc, sign, dqv, shift, &qc_low, &dqc_low);
786
0
            abs_qc_low = abs_qc - 1;
787
0
            dist_low   = get_coeff_dist(tqc, dqc_low, shift) - dist0;
788
0
            rate_low   = get_coeff_cost_general(
789
0
                0, ci, abs_qc_low, sign, coeff_ctx, dc_sign_ctx, txb_costs, bwl, tx_class, levels);
790
0
            rd_low = RDCOST(rdmult, *accu_rate + rate_low, *accu_dist + dist_low);
791
0
        }
792
793
0
        int       lower_level_new_eob = 0;
794
0
        const int new_eob             = si + 1;
795
0
        const int coeff_ctx_new_eob   = get_lower_levels_ctx_eob(bwl, height, si);
796
0
        const int new_eob_cost        = get_eob_cost(new_eob, txb_eob_costs, txb_costs, tx_class);
797
0
        int       rate_coeff_eob      = new_eob_cost +
798
0
            get_coeff_cost_eob(ci, abs_qc, sign, coeff_ctx_new_eob, dc_sign_ctx, txb_costs, bwl, tx_class);
799
0
        int64_t dist_new_eob = dist;
800
0
        int64_t rd_new_eob   = RDCOST(rdmult, rate_coeff_eob, dist_new_eob);
801
802
0
        if (abs_qc_low > 0) {
803
0
            const int rate_coeff_eob_low = new_eob_cost +
804
0
                get_coeff_cost_eob(ci, abs_qc_low, sign, coeff_ctx_new_eob, dc_sign_ctx, txb_costs, bwl, tx_class);
805
0
            const int64_t dist_new_eob_low = dist_low;
806
0
            const int64_t rd_new_eob_low   = RDCOST(rdmult, rate_coeff_eob_low, dist_new_eob_low);
807
0
            if (rd_new_eob_low < rd_new_eob) {
808
0
                lower_level_new_eob = 1;
809
0
                rd_new_eob          = rd_new_eob_low;
810
0
                rate_coeff_eob      = rate_coeff_eob_low;
811
0
                dist_new_eob        = dist_new_eob_low;
812
0
            }
813
0
        }
814
815
0
        if (rd_low < rd) {
816
0
            lower_level = 1;
817
0
            rd          = rd_low;
818
0
            rate        = rate_low;
819
0
            dist        = dist_low;
820
0
        }
821
822
0
        if (sharpness == 0 && rd_new_eob < rd) {
823
0
            for (int ni = 0; ni < *nz_num; ++ni) {
824
0
                int last_ci                          = nz_ci[ni];
825
0
                levels[get_padded_idx(last_ci, bwl)] = 0;
826
0
                qcoeff[last_ci]                      = 0;
827
0
                dqcoeff[last_ci]                     = 0;
828
0
            }
829
0
            *eob        = new_eob;
830
0
            *nz_num     = 0;
831
0
            *accu_rate  = rate_coeff_eob;
832
0
            *accu_dist  = dist_new_eob;
833
0
            lower_level = lower_level_new_eob;
834
0
        } else {
835
0
            *accu_rate += rate;
836
0
            *accu_dist += dist;
837
0
        }
838
839
0
        if (lower_level) {
840
0
            qcoeff[ci]                      = qc_low;
841
0
            dqcoeff[ci]                     = dqc_low;
842
0
            levels[get_padded_idx(ci, bwl)] = AOMMIN(abs_qc_low, INT8_MAX);
843
0
        }
844
0
        if (qcoeff[ci]) {
845
0
            nz_ci[*nz_num] = ci;
846
0
            ++*nz_num;
847
0
        }
848
0
    }
849
0
}
850
851
static INLINE void update_coeff_general(int* accu_rate, int64_t* accu_dist, int si, int eob, TxSize tx_size,
852
                                        TxClass tx_class, int bwl, int height, int64_t rdmult, int shift,
853
                                        int dc_sign_ctx, const int16_t* dequant, const int16_t* scan,
854
                                        const LvMapCoeffCost* txb_costs, const TranLow* tcoeff, TranLow* qcoeff,
855
7.66k
                                        TranLow* dqcoeff, uint8_t* levels, const QmVal* iqm_ptr) {
856
7.66k
    const int     ci        = scan[si];
857
7.66k
    const int     dqv       = get_dqv(dequant, ci, iqm_ptr);
858
7.66k
    const TranLow qc        = qcoeff[ci];
859
7.66k
    const int     is_last   = si == (eob - 1);
860
7.66k
    const int     coeff_ctx = get_lower_levels_ctx_general(is_last, si, bwl, height, levels, ci, tx_size, tx_class);
861
7.66k
    if (qc == 0) {
862
0
        *accu_rate += txb_costs->base_cost[coeff_ctx][0];
863
7.66k
    } else {
864
18.4E
        const int     sign   = (qc < 0) ? 1 : 0;
865
7.66k
        const TranLow abs_qc = abs(qc);
866
7.66k
        const TranLow tqc    = tcoeff[ci];
867
7.66k
        const TranLow dqc    = dqcoeff[ci];
868
7.66k
        const int64_t dist   = get_coeff_dist(tqc, dqc, shift);
869
7.66k
        const int64_t dist0  = get_coeff_dist(tqc, 0, shift);
870
7.66k
        const int     rate   = get_coeff_cost_general(
871
7.66k
            is_last, ci, abs_qc, sign, coeff_ctx, dc_sign_ctx, txb_costs, bwl, tx_class, levels);
872
7.66k
        const int64_t rd = RDCOST(rdmult, rate, dist);
873
874
7.66k
        TranLow qc_low, dqc_low;
875
7.66k
        TranLow abs_qc_low;
876
7.66k
        int64_t dist_low, rd_low;
877
7.66k
        int     rate_low;
878
7.66k
        if (abs_qc == 1) {
879
0
            abs_qc_low = qc_low = dqc_low = 0;
880
0
            dist_low                      = dist0;
881
0
            rate_low                      = txb_costs->base_cost[coeff_ctx][0];
882
7.66k
        } else {
883
7.66k
            get_qc_dqc_low(abs_qc, sign, dqv, shift, &qc_low, &dqc_low);
884
7.66k
            abs_qc_low = abs_qc - 1;
885
7.66k
            dist_low   = get_coeff_dist(tqc, dqc_low, shift);
886
7.66k
            rate_low   = get_coeff_cost_general(
887
7.66k
                is_last, ci, abs_qc_low, sign, coeff_ctx, dc_sign_ctx, txb_costs, bwl, tx_class, levels);
888
7.66k
        }
889
890
7.66k
        rd_low = RDCOST(rdmult, rate_low, dist_low);
891
7.66k
        if (rd_low < rd) {
892
54
            qcoeff[ci]                      = qc_low;
893
54
            dqcoeff[ci]                     = dqc_low;
894
54
            levels[get_padded_idx(ci, bwl)] = AOMMIN(abs_qc_low, INT8_MAX);
895
54
            *accu_rate += rate_low;
896
54
            *accu_dist += dist_low - dist0;
897
7.61k
        } else {
898
7.61k
            *accu_rate += rate;
899
7.61k
            *accu_dist += dist - dist0;
900
7.61k
        }
901
7.66k
    }
902
7.66k
}
903
904
static AOM_FORCE_INLINE void update_coeff_simple(int* accu_rate, int si, int eob, TxSize tx_size, TxClass tx_class,
905
                                                 int bwl, int64_t rdmult, int shift, const int16_t* dequant,
906
                                                 const int16_t* scan, const LvMapCoeffCost* txb_costs,
907
                                                 const TranLow* tcoeff, TranLow* qcoeff, TranLow* dqcoeff,
908
0
                                                 uint8_t* levels, const QmVal* iqm_ptr) {
909
0
    const int dqv = get_dqv(dequant, scan[si], iqm_ptr);
910
0
    (void)eob;
911
    // this simple version assumes the coeff's scan_idx is not DC (scan_idx != 0)
912
    // and not the last (scan_idx != eob - 1)
913
0
    assert(si != eob - 1);
914
0
    assert(si > 0);
915
0
    const int     ci        = scan[si];
916
0
    const TranLow qc        = qcoeff[ci];
917
0
    const int     coeff_ctx = get_lower_levels_ctx(levels, ci, bwl, tx_size, tx_class);
918
0
    if (qc == 0) {
919
0
        *accu_rate += txb_costs->base_cost[coeff_ctx][0];
920
0
    } else {
921
0
        const TranLow abs_qc   = abs(qc);
922
0
        const TranLow abs_tqc  = abs(tcoeff[ci]);
923
0
        const TranLow abs_dqc  = abs(dqcoeff[ci]);
924
0
        int           rate_low = 0;
925
0
        const int rate = get_two_coeff_cost_simple(ci, abs_qc, coeff_ctx, txb_costs, bwl, tx_class, levels, &rate_low);
926
0
        if (abs_dqc < abs_tqc) {
927
0
            *accu_rate += rate;
928
0
            return;
929
0
        }
930
931
0
        const int64_t dist = get_coeff_dist(abs_tqc, abs_dqc, shift);
932
0
        const int64_t rd   = RDCOST(rdmult, rate, dist);
933
934
0
        const TranLow abs_qc_low  = abs_qc - 1;
935
0
        const TranLow abs_dqc_low = (abs_qc_low * dqv) >> shift;
936
0
        const int64_t dist_low    = get_coeff_dist(abs_tqc, abs_dqc_low, shift);
937
0
        const int64_t rd_low      = RDCOST(rdmult, rate_low, dist_low);
938
939
0
        if (rd_low < rd) {
940
0
            const int sign                  = (qc < 0) ? 1 : 0;
941
0
            qcoeff[ci]                      = (-sign ^ abs_qc_low) + sign;
942
0
            dqcoeff[ci]                     = (-sign ^ abs_dqc_low) + sign;
943
0
            levels[get_padded_idx(ci, bwl)] = AOMMIN(abs_qc_low, INT8_MAX);
944
0
            *accu_rate += rate_low;
945
0
        } else {
946
0
            *accu_rate += rate;
947
0
        }
948
0
    }
949
0
}
950
951
static INLINE void update_skip(int* accu_rate, int64_t accu_dist, uint16_t* eob, int nz_num, int* nz_ci, int64_t rdmult,
952
9.21k
                               int skip_cost, int non_skip_cost, TranLow* qcoeff, TranLow* dqcoeff, int sharpness) {
953
9.21k
    const int64_t rd         = RDCOST(rdmult, *accu_rate + non_skip_cost, accu_dist);
954
9.21k
    const int64_t rd_new_eob = RDCOST(rdmult, skip_cost, 0);
955
9.21k
    if (sharpness == 0 && rd_new_eob < rd) {
956
0
        for (int i = 0; i < nz_num; ++i) {
957
0
            const int ci = nz_ci[i];
958
0
            qcoeff[ci]   = 0;
959
0
            dqcoeff[ci]  = 0;
960
            // no need to set up levels because this is the last step
961
            // levels[get_padded_idx(ci, bwl)] = 0;
962
0
        }
963
0
        *accu_rate = 0;
964
0
        *eob       = 0;
965
0
    }
966
9.21k
}
967
968
enum {
969
    NO_AQ             = 0,
970
    VARIANCE_AQ       = 1,
971
    COMPLEXITY_AQ     = 2,
972
    CYCLIC_REFRESH_AQ = 3,
973
    AQ_MODE_COUNT // This should always be the last member of the enum
974
} UENUM1BYTE(AQ_MODE);
975
976
enum {
977
    NO_DELTA_Q   = 0,
978
    DELTA_Q_ONLY = 1,
979
    DELTA_Q_LF   = 2,
980
    DELTAQ_MODE_COUNT // This should always be the last member of the enum
981
} UENUM1BYTE(DELTAQ_MODE);
982
983
// These numbers are empirically obtained.
984
#if TUNE_CHROMA_SSIM
985
static const int plane_rd_mult[2][REF_TYPES][PLANE_TYPES] = {{
986
                                                                 {17, 13},
987
                                                                 {16, 10},
988
                                                             },
989
                                                             {
990
                                                                 {17, 13},
991
                                                                 {16, 10},
992
                                                             }};
993
#else
994
static const int plane_rd_mult[2][REF_TYPES][PLANE_TYPES] = {{{17, 20}, {16, 20}},
995
                                                             {
996
                                                                 {17, 13},
997
                                                                 {16, 10},
998
                                                             }};
999
#endif
1000
1001
/*
1002
 * Reduce the number of non-zero quantized coefficients before getting to the main/complex RDOQ stage
1003
 * (it performs an early check of whether to zero out each of the non-zero quantized coefficients,
1004
 * and updates the quantized coeffs if it is determined it can be zeroed out).
1005
 */
1006
static INLINE void update_coeff_eob_fast(uint16_t* eob, int shift, const int16_t* dequant_ptr, const int16_t* scan,
1007
0
                                         const TranLow* coeff_ptr, TranLow* qcoeff_ptr, TranLow* dqcoeff_ptr) {
1008
0
    int       eob_out = *eob;
1009
0
    const int zbin[2] = {dequant_ptr[0] + ROUND_POWER_OF_TWO(dequant_ptr[0] * 70, 7),
1010
0
                         dequant_ptr[1] + ROUND_POWER_OF_TWO(dequant_ptr[1] * 70, 7)};
1011
0
    for (int i = *eob - 1; i >= 0; i--) {
1012
0
        const int rc         = scan[i];
1013
0
        const int qcoeff     = qcoeff_ptr[rc];
1014
0
        const int coeff      = coeff_ptr[rc];
1015
0
        const int coeff_sign = -(coeff < 0);
1016
0
        int64_t   abs_coeff  = (coeff ^ coeff_sign) - coeff_sign;
1017
0
        if (((abs_coeff << (1 + shift)) < zbin[rc != 0]) || (qcoeff == 0)) {
1018
0
            eob_out--;
1019
0
            qcoeff_ptr[rc]  = 0;
1020
0
            dqcoeff_ptr[rc] = 0;
1021
0
        } else {
1022
0
            break;
1023
0
        }
1024
0
    }
1025
0
    *eob = eob_out;
1026
0
}
1027
1028
static void svt_fast_optimize_b(const TranLow* coeff_ptr, const MacroblockPlane* p, TranLow* qcoeff_ptr,
1029
                                TranLow* dqcoeff_ptr, uint16_t* eob, TxSize tx_size, TxType tx_type)
1030
1031
0
{
1032
0
    const ScanOrder* const scan_order = get_scan_order(tx_size, tx_type);
1033
0
    const int16_t*         scan       = scan_order->scan;
1034
0
    const int              shift      = av1_get_tx_scale_tab[tx_size];
1035
0
    update_coeff_eob_fast(eob, shift, p->dequant_qtx, scan, coeff_ptr, qcoeff_ptr, dqcoeff_ptr);
1036
0
}
1037
1038
static void svt_av1_optimize_b(PictureControlSet* pcs, ModeDecisionContext* ctx, int16_t txb_skip_context,
1039
                               int16_t dc_sign_context, const TranLow* coeff_ptr, const MacroblockPlane* p,
1040
                               TranLow* qcoeff_ptr, TranLow* dqcoeff_ptr, uint16_t* eob, const QuantParam* qparam,
1041
                               TxSize tx_size, TxType tx_type, bool is_inter, uint8_t use_sharpness,
1042
9.21k
                               uint8_t delta_q_present, uint8_t picture_qp, uint32_t lambda, int plane) {
1043
9.21k
    SequenceControlSet*    scs        = pcs->scs;
1044
9.21k
    bool                   allintra   = scs->allintra;
1045
9.21k
    bool                   rtc        = scs->static_config.rtc;
1046
9.21k
    int                    sharpness  = 0; // No Sharpness
1047
9.21k
    const ScanOrder* const scan_order = get_scan_order(tx_size, tx_type);
1048
9.21k
    const int16_t*         scan       = scan_order->scan;
1049
9.21k
    const int              shift      = av1_get_tx_scale_tab[tx_size];
1050
9.21k
    const PlaneType        plane_type = plane;
1051
9.21k
    const TxSize           txs_ctx    = get_txsize_entropy_ctx(tx_size);
1052
9.21k
    const TxClass          tx_class   = tx_type_to_class[tx_type];
1053
9.21k
    const int              bwl        = get_txb_bwl(tx_size);
1054
9.21k
    const int              width      = get_txb_wide(tx_size);
1055
9.21k
    const int              height     = get_txb_high(tx_size);
1056
9.21k
    assert(width == (1 << bwl));
1057
9.21k
    assert(txs_ctx < TX_SIZES);
1058
9.21k
    const LvMapCoeffCost* txb_costs      = &ctx->md_rate_est_ctx->coeff_fac_bits[txs_ctx][plane_type];
1059
9.21k
    const int             eob_multi_size = txsize_log2_minus4[tx_size];
1060
9.21k
    const LvMapEobCost*   txb_eob_costs  = &ctx->md_rate_est_ctx->eob_frac_bits[eob_multi_size][plane_type];
1061
9.21k
    const int             non_skip_cost  = txb_costs->txb_skip_cost[txb_skip_context][0];
1062
9.21k
    const int             skip_cost      = txb_costs->txb_skip_cost[txb_skip_context][1];
1063
9.21k
    const int             eob_cost       = get_eob_cost(*eob, txb_eob_costs, txb_costs, tx_class);
1064
9.21k
    int                   rweight        = 100;
1065
9.21k
    const int32_t         sharpness_val  = CLIP3(0, 7, pcs->scs->static_config.sharpness);
1066
9.21k
    const int             rshift         = MAX(2, (int)sharpness_val);
1067
9.21k
    if (use_sharpness && delta_q_present && plane == 0) {
1068
0
        int diff = ctx->sb_ptr->qindex - quantizer_to_qindex[picture_qp];
1069
0
        if (diff < 0) {
1070
0
            sharpness = 1;
1071
0
            rweight   = 0;
1072
0
        }
1073
0
    }
1074
9.21k
    const int64_t rdmult =
1075
9.21k
        (((((int64_t)lambda * plane_rd_mult[allintra || rtc][is_inter][plane_type]) * rweight) / 100) + 2) >> rshift;
1076
9.21k
    uint8_t* const levels = set_levels(ctx->md_levels_buf, width, height);
1077
1078
9.21k
    if (*eob > 1) {
1079
0
        svt_av1_txb_init_levels(qcoeff_ptr, width, height, levels);
1080
0
    }
1081
9.21k
    int accu_rate = eob_cost;
1082
1083
9.21k
    int64_t       accu_dist  = 0;
1084
9.21k
    int           si         = *eob - 1;
1085
9.21k
    const int     ci         = scan[si];
1086
9.21k
    const TranLow qc         = qcoeff_ptr[ci];
1087
9.21k
    const TranLow abs_qc     = abs(qc);
1088
9.21k
    const int     sign       = qc < 0;
1089
9.21k
    const int     max_nz_num = 4;
1090
9.21k
    int           nz_num     = 1;
1091
9.21k
    int           nz_ci[5]   = {ci, 0, 0, 0, 0};
1092
9.21k
    if (abs_qc >= 2) {
1093
7.66k
        update_coeff_general(&accu_rate,
1094
7.66k
                             &accu_dist,
1095
7.66k
                             si,
1096
7.66k
                             *eob,
1097
7.66k
                             tx_size,
1098
7.66k
                             tx_class,
1099
7.66k
                             bwl,
1100
7.66k
                             height,
1101
7.66k
                             rdmult,
1102
7.66k
                             shift,
1103
7.66k
                             dc_sign_context,
1104
7.66k
                             p->dequant_qtx,
1105
7.66k
                             scan,
1106
7.66k
                             txb_costs,
1107
7.66k
                             coeff_ptr,
1108
7.66k
                             qcoeff_ptr,
1109
7.66k
                             dqcoeff_ptr,
1110
7.66k
                             levels,
1111
7.66k
                             qparam->iqmatrix);
1112
7.66k
        --si;
1113
7.66k
    } else {
1114
1.55k
        assert(abs_qc == 1);
1115
1.55k
        const int coeff_ctx = get_lower_levels_ctx_eob(bwl, height, si);
1116
1.55k
        accu_rate += get_coeff_cost_eob(ci, abs_qc, sign, coeff_ctx, dc_sign_context, txb_costs, bwl, tx_class);
1117
1118
1.55k
        const TranLow tqc   = coeff_ptr[ci];
1119
1.55k
        const TranLow dqc   = dqcoeff_ptr[ci];
1120
1.55k
        const int64_t dist  = get_coeff_dist(tqc, dqc, shift);
1121
1.55k
        const int64_t dist0 = get_coeff_dist(tqc, 0, shift);
1122
1.55k
        accu_dist += dist - dist0;
1123
1.55k
        --si;
1124
1.55k
    }
1125
9.21k
#define UPDATE_COEFF_EOB_CASE(tx_class_literal)         \
1126
9.21k
    case tx_class_literal:                              \
1127
9.21k
        for (; si >= 0 && nz_num <= max_nz_num; --si) { \
1128
0
            update_coeff_eob(&accu_rate,                \
1129
0
                             &accu_dist,                \
1130
0
                             eob,                       \
1131
0
                             &nz_num,                   \
1132
0
                             nz_ci,                     \
1133
0
                             si,                        \
1134
0
                             tx_size,                   \
1135
0
                             tx_class_literal,          \
1136
0
                             bwl,                       \
1137
0
                             height,                    \
1138
0
                             dc_sign_context,           \
1139
0
                             rdmult,                    \
1140
0
                             shift,                     \
1141
0
                             p->dequant_qtx,            \
1142
0
                             scan,                      \
1143
0
                             txb_eob_costs,             \
1144
0
                             txb_costs,                 \
1145
0
                             coeff_ptr,                 \
1146
0
                             qcoeff_ptr,                \
1147
0
                             dqcoeff_ptr,               \
1148
0
                             levels,                    \
1149
0
                             sharpness,                 \
1150
0
                             qparam->iqmatrix);         \
1151
0
        }                                               \
1152
9.21k
        break;
1153
9.21k
    switch (tx_class) {
1154
9.21k
        UPDATE_COEFF_EOB_CASE(TX_CLASS_2D);
1155
0
        UPDATE_COEFF_EOB_CASE(TX_CLASS_HORIZ);
1156
0
        UPDATE_COEFF_EOB_CASE(TX_CLASS_VERT);
1157
0
#undef UPDATE_COEFF_EOB_CASE
1158
0
    default:
1159
0
        assert(false);
1160
9.21k
    }
1161
1162
9.21k
    if (si == -1 && nz_num <= max_nz_num) {
1163
9.21k
        update_skip(&accu_rate,
1164
9.21k
                    accu_dist,
1165
9.21k
                    eob,
1166
9.21k
                    nz_num,
1167
9.21k
                    nz_ci,
1168
9.21k
                    rdmult,
1169
9.21k
                    skip_cost,
1170
9.21k
                    non_skip_cost,
1171
9.21k
                    qcoeff_ptr,
1172
9.21k
                    dqcoeff_ptr,
1173
9.21k
                    sharpness);
1174
9.21k
    }
1175
1176
9.21k
    int si_end = 1; // default: full RDOQ
1177
9.21k
    if (ctx->rdoq_ctrls.cut_off_num) {
1178
9.21k
        const int cut_off_coeff = AOMMAX((width * height) >> 7,
1179
9.21k
                                         (*eob * ctx->rdoq_ctrls.cut_off_num) / ctx->rdoq_ctrls.cut_off_denum);
1180
9.21k
        si_end                  = AOMMAX(1, *eob - cut_off_coeff);
1181
9.21k
    }
1182
9.21k
#define UPDATE_COEFF_SIMPLE_CASE(tx_class_literal) \
1183
9.21k
    case tx_class_literal:                         \
1184
9.21k
        for (; si >= si_end; --si) {               \
1185
0
            update_coeff_simple(&accu_rate,        \
1186
0
                                si,                \
1187
0
                                *eob,              \
1188
0
                                tx_size,           \
1189
0
                                tx_class_literal,  \
1190
0
                                bwl,               \
1191
0
                                rdmult,            \
1192
0
                                shift,             \
1193
0
                                p->dequant_qtx,    \
1194
0
                                scan,              \
1195
0
                                txb_costs,         \
1196
0
                                coeff_ptr,         \
1197
0
                                qcoeff_ptr,        \
1198
0
                                dqcoeff_ptr,       \
1199
0
                                levels,            \
1200
0
                                qparam->iqmatrix); \
1201
0
        }                                          \
1202
9.21k
        break;
1203
9.21k
    switch (tx_class) {
1204
9.21k
        UPDATE_COEFF_SIMPLE_CASE(TX_CLASS_2D);
1205
0
        UPDATE_COEFF_SIMPLE_CASE(TX_CLASS_HORIZ);
1206
0
        UPDATE_COEFF_SIMPLE_CASE(TX_CLASS_VERT);
1207
0
#undef UPDATE_COEFF_SIMPLE_CASE
1208
0
    default:
1209
0
        assert(false);
1210
9.21k
    }
1211
1212
    // DC position
1213
9.21k
    if (si == 0) {
1214
        // no need to update accu_dist because it's not used after this point
1215
0
        int64_t dummy_dist = 0;
1216
0
        update_coeff_general(&accu_rate,
1217
0
                             &dummy_dist,
1218
0
                             si,
1219
0
                             *eob,
1220
0
                             tx_size,
1221
0
                             tx_class,
1222
0
                             bwl,
1223
0
                             height,
1224
0
                             rdmult,
1225
0
                             shift,
1226
0
                             dc_sign_context,
1227
0
                             p->dequant_qtx,
1228
0
                             scan,
1229
0
                             txb_costs,
1230
0
                             coeff_ptr,
1231
0
                             qcoeff_ptr,
1232
0
                             dqcoeff_ptr,
1233
0
                             levels,
1234
0
                             qparam->iqmatrix);
1235
0
    }
1236
9.21k
}
1237
1238
963k
static INLINE TxSize aom_av1_get_adjusted_tx_size(TxSize tx_size) {
1239
963k
    switch (tx_size) {
1240
2.70k
    case TX_64X64:
1241
4.67k
    case TX_64X32:
1242
4.67k
    case TX_32X64:
1243
4.67k
        return TX_32X32;
1244
0
    case TX_64X16:
1245
0
        return TX_32X16;
1246
0
    case TX_16X64:
1247
0
        return TX_16X32;
1248
958k
    default:
1249
958k
        return tx_size;
1250
963k
    }
1251
963k
}
1252
1253
void svt_aom_quantize_inv_quantize_light(PictureControlSet* pcs, int32_t* coeff, int32_t* quant_coeff,
1254
                                         int32_t* recon_coeff, uint32_t qindex, TxSize txsize, uint16_t* eob,
1255
141k
                                         uint32_t bit_depth, TxType tx_type) {
1256
141k
    EncodeContext* enc_ctx = pcs->scs->enc_ctx;
1257
1258
141k
    uint32_t q_index = qindex;
1259
1260
141k
    const ScanOrder* const scan_order = get_scan_order(txsize, tx_type);
1261
1262
141k
    const int32_t n_coeffs = av1_get_max_eob(txsize);
1263
1264
141k
    int32_t qmatrix_level = (IS_2D_TRANSFORM(tx_type) && pcs->ppcs->frm_hdr.quantization_params.using_qmatrix)
1265
1266
141k
        ? pcs->ppcs->frm_hdr.quantization_params.qm[PLANE_Y]
1267
1268
141k
        : NUM_QM_LEVELS - 1;
1269
1270
141k
    TxSize adjusted_tx_size = aom_av1_get_adjusted_tx_size(txsize);
1271
1272
141k
    const QmVal* q_matrix = pcs->ppcs->gqmatrix[qmatrix_level][PLANE_Y][adjusted_tx_size];
1273
1274
141k
    const QmVal* iq_matrix = pcs->ppcs->giqmatrix[qmatrix_level][PLANE_Y][adjusted_tx_size];
1275
1276
141k
    if (q_matrix == NULL && iq_matrix == NULL) {
1277
141k
#if CONFIG_ENABLE_HIGH_BIT_DEPTH
1278
141k
        if (bit_depth > EB_EIGHT_BIT) {
1279
0
            svt_aom_highbd_quantize_b((TranLow*)coeff,
1280
0
                                      n_coeffs,
1281
0
                                      enc_ctx->quants_bd.y_zbin[q_index],
1282
0
                                      enc_ctx->quants_bd.y_round[q_index],
1283
0
                                      enc_ctx->quants_bd.y_quant[q_index],
1284
0
                                      enc_ctx->quants_bd.y_quant_shift[q_index],
1285
0
                                      quant_coeff,
1286
0
                                      (TranLow*)recon_coeff,
1287
0
                                      enc_ctx->deq_bd.v_dequant_qtx[q_index],
1288
0
                                      eob,
1289
0
                                      scan_order->scan,
1290
0
                                      scan_order->iscan,
1291
0
                                      q_matrix,
1292
0
                                      iq_matrix,
1293
0
                                      av1_get_tx_scale_tab[txsize]);
1294
0
        } else
1295
#else
1296
        UNUSED(bit_depth);
1297
#endif
1298
141k
        {
1299
141k
            svt_aom_quantize_b((TranLow*)coeff,
1300
141k
                               n_coeffs,
1301
141k
                               enc_ctx->quants_8bit.v_zbin[q_index],
1302
141k
                               enc_ctx->quants_8bit.v_round[q_index],
1303
141k
                               enc_ctx->quants_8bit.v_quant[q_index],
1304
141k
                               enc_ctx->quants_8bit.v_quant_shift[q_index],
1305
141k
                               quant_coeff,
1306
141k
                               (TranLow*)recon_coeff,
1307
141k
                               enc_ctx->deq_8bit.y_dequant_qtx[q_index],
1308
141k
                               eob,
1309
141k
                               scan_order->scan,
1310
141k
                               scan_order->iscan,
1311
141k
                               q_matrix,
1312
141k
                               iq_matrix,
1313
141k
                               av1_get_tx_scale_tab[txsize]);
1314
141k
        }
1315
18.4E
    } else {
1316
18.4E
#if CONFIG_ENABLE_HIGH_BIT_DEPTH
1317
18.4E
        if (bit_depth > EB_EIGHT_BIT) {
1318
0
            svt_av1_highbd_quantize_b_qm((TranLow*)coeff,
1319
0
                                         n_coeffs,
1320
0
                                         enc_ctx->quants_bd.y_zbin[q_index],
1321
0
                                         enc_ctx->quants_bd.y_round[q_index],
1322
0
                                         enc_ctx->quants_bd.y_quant[q_index],
1323
0
                                         enc_ctx->quants_bd.y_quant_shift[q_index],
1324
0
                                         quant_coeff,
1325
0
                                         (TranLow*)recon_coeff,
1326
0
                                         enc_ctx->deq_bd.v_dequant_qtx[q_index],
1327
0
                                         eob,
1328
0
                                         scan_order->scan,
1329
0
                                         scan_order->iscan,
1330
0
                                         q_matrix,
1331
0
                                         iq_matrix,
1332
0
                                         av1_get_tx_scale_tab[txsize]);
1333
0
        } else
1334
18.4E
#endif
1335
18.4E
        {
1336
18.4E
            svt_av1_quantize_b_qm((TranLow*)coeff,
1337
18.4E
                                  n_coeffs,
1338
18.4E
                                  enc_ctx->quants_8bit.v_zbin[q_index],
1339
18.4E
                                  enc_ctx->quants_8bit.v_round[q_index],
1340
18.4E
                                  enc_ctx->quants_8bit.v_quant[q_index],
1341
18.4E
                                  enc_ctx->quants_8bit.v_quant_shift[q_index],
1342
18.4E
                                  quant_coeff,
1343
18.4E
                                  (TranLow*)recon_coeff,
1344
18.4E
                                  enc_ctx->deq_8bit.y_dequant_qtx[q_index],
1345
18.4E
                                  eob,
1346
18.4E
                                  scan_order->scan,
1347
18.4E
                                  scan_order->iscan,
1348
18.4E
                                  q_matrix,
1349
18.4E
                                  iq_matrix,
1350
18.4E
                                  av1_get_tx_scale_tab[txsize]);
1351
18.4E
        }
1352
18.4E
    }
1353
141k
}
1354
1355
// See av1_get_txb_entropy_context in libaom
1356
0
uint8_t svt_av1_compute_cul_level_c(const int16_t* const scan, const int32_t* const quant_coeff, uint16_t* eob) {
1357
0
    int32_t cul_level = 0;
1358
0
    for (int32_t c = 0; c < *eob; ++c) {
1359
0
        const int16_t pos   = scan[c];
1360
0
        const int32_t v     = quant_coeff[pos];
1361
0
        int32_t       level = ABS(v);
1362
0
        cul_level += level;
1363
        // Early exit the loop if cul_level reaches COEFF_CONTEXT_MASK
1364
0
        if (cul_level >= COEFF_CONTEXT_MASK) {
1365
0
            break;
1366
0
        }
1367
0
    }
1368
1369
0
    cul_level = AOMMIN(COEFF_CONTEXT_MASK, cul_level);
1370
    // DC value
1371
0
    set_dc_sign(&cul_level, quant_coeff[0]);
1372
0
    return (uint8_t)cul_level;
1373
0
}
1374
1375
// Retract EOB by removing trailing low-magnitude coefficients separated by zero gaps
1376
// Tracks symbol-count knees at levels 3/6/9/12 and golomb tail at 15+.
1377
0
static INLINE int32_t ec_shave_est_zero_rate_save(int32_t ref_level, int32_t bit_cost) {
1378
0
    int32_t save = ((ref_level > 3) + (ref_level > 6) + (ref_level > 9) + (ref_level > 12)) * bit_cost;
1379
0
    if (ref_level > 14) {
1380
0
        save += get_golomb_cost(ref_level);
1381
0
    }
1382
0
    return save;
1383
0
}
1384
1385
static INLINE uint16_t shave_coeff(int32_t* quant_buf, int32_t* recon_buf, const int32_t* tcoeff, uint16_t eob,
1386
0
                                   TxSize tx_size, TxType tx_type, uint32_t lambda, const CoeffShavingCtrls* ctrls) {
1387
0
    const int16_t* const scan             = get_scan_order(tx_size, tx_type)->scan;
1388
0
    const int            level_th         = ctrls->level_threshold;
1389
0
    const int            gap_th           = ctrls->zero_gap_threshold;
1390
0
    int                  updated_eob      = (int)eob;
1391
0
    int                  prev_nz_scan_idx = updated_eob - 2;
1392
1393
    // Two-phase design rationale:
1394
    // 1) Run a cheap structural pass first (gap/level only, no RD math) to retract EOB quickly.
1395
    // 2) Then run the expensive RD-gated pass only on the shortened tail.
1396
1397
    // Phase 1: trailing coeff zeroing by zero-gap criterion.
1398
0
    while (updated_eob > 1) {
1399
0
        const int     last_scan_idx = updated_eob - 1;
1400
0
        const int     last_pos      = scan[last_scan_idx];
1401
0
        const int32_t val           = quant_buf[last_pos];
1402
0
        const int32_t abs_val       = (val < 0) ? -val : val;
1403
1404
        // Current trailing coeff is not eligible for shaving.
1405
        // Since phase 2 obeys the same level-threshold rule, we are done.
1406
0
        if (abs_val > level_th) {
1407
0
            return (uint16_t)updated_eob;
1408
0
        }
1409
1410
0
        while (prev_nz_scan_idx >= 0) {
1411
0
            const int pos = scan[prev_nz_scan_idx];
1412
0
            if (quant_buf[pos] != 0) {
1413
0
                break;
1414
0
            }
1415
0
            --prev_nz_scan_idx;
1416
0
        }
1417
1418
0
        if (prev_nz_scan_idx < 0) {
1419
0
            break;
1420
0
        }
1421
1422
0
        const int gap = last_scan_idx - prev_nz_scan_idx - 1;
1423
0
        if (gap < gap_th) {
1424
0
            break;
1425
0
        }
1426
1427
0
        quant_buf[last_pos] = 0;
1428
0
        recon_buf[last_pos] = 0;
1429
1430
0
        updated_eob = prev_nz_scan_idx + 1;
1431
0
        --prev_nz_scan_idx;
1432
0
    }
1433
1434
    // Nothing more to do if RD shaving is disabled or no trailing coeff remains.
1435
0
    if (ctrls->rd_zero_strength <= 0 || updated_eob <= 1) {
1436
0
        return (uint16_t)updated_eob;
1437
0
    }
1438
1439
0
    const int     shift         = av1_get_tx_scale_tab[tx_size];
1440
0
    const int32_t bit_cost      = av1_cost_literal(1);
1441
0
    const int64_t rd_rate_scale = (int64_t)ctrls->rd_zero_strength;
1442
1443
    // Fast path: only |level| == 1 is eligible.
1444
0
    if (level_th == 1) {
1445
0
        while (updated_eob > 1) {
1446
0
            const int     last_scan_idx = updated_eob - 1;
1447
0
            const int     last_pos      = scan[last_scan_idx];
1448
0
            const int32_t val           = quant_buf[last_pos];
1449
0
            const int32_t abs_val       = (val >= 0) ? val : -val;
1450
1451
0
            if (abs_val > 1) {
1452
0
                break;
1453
0
            }
1454
1455
0
            const TranLow tqc      = (TranLow)tcoeff[last_pos];
1456
0
            const TranLow dqc_cur  = (TranLow)recon_buf[last_pos];
1457
0
            const int64_t dist_cur = get_coeff_dist(tqc, dqc_cur, shift);
1458
0
            const int64_t dist_new = get_coeff_dist(tqc, 0, shift);
1459
1460
            // For |level| == 1, ec_shave_est_zero_rate_save() contributes 0.
1461
0
            const int64_t rate_save = (int64_t)bit_cost * rd_rate_scale;
1462
1463
0
            const int64_t dist_term = (dist_new - dist_cur) * ((int64_t)1 << RDDIV_BITS);
1464
0
            const int64_t rate_term = ROUND_POWER_OF_TWO(rate_save * lambda, AV1_PROB_COST_SHIFT);
1465
0
            if (dist_term >= rate_term) {
1466
0
                break;
1467
0
            }
1468
1469
0
            quant_buf[last_pos] = 0;
1470
0
            recon_buf[last_pos] = 0;
1471
1472
0
            int next_eob = last_scan_idx;
1473
0
            while (next_eob > 0 && quant_buf[scan[next_eob - 1]] == 0) {
1474
0
                --next_eob;
1475
0
            }
1476
0
            updated_eob = next_eob;
1477
0
        }
1478
1479
0
        return (uint16_t)updated_eob;
1480
0
    }
1481
1482
    // Generic phase 2 for level_threshold > 1.
1483
0
    while (updated_eob > 1) {
1484
0
        const int     last_scan_idx = updated_eob - 1;
1485
0
        const int     last_pos      = scan[last_scan_idx];
1486
0
        const int32_t val           = quant_buf[last_pos];
1487
0
        const int32_t abs_val       = (val >= 0) ? val : -val;
1488
1489
0
        if (abs_val > level_th) {
1490
0
            break;
1491
0
        }
1492
1493
0
        const int64_t rate_save = (int64_t)(ec_shave_est_zero_rate_save(abs_val, bit_cost) + bit_cost) * rd_rate_scale;
1494
1495
0
        const TranLow tqc      = (TranLow)tcoeff[last_pos];
1496
0
        const TranLow dqc_cur  = (TranLow)recon_buf[last_pos];
1497
0
        const int64_t dist_cur = get_coeff_dist(tqc, dqc_cur, shift);
1498
0
        const int64_t dist_new = get_coeff_dist(tqc, 0, shift);
1499
1500
0
        const int64_t dist_term = (dist_new - dist_cur) * ((int64_t)1 << RDDIV_BITS);
1501
0
        const int64_t rate_term = ROUND_POWER_OF_TWO(rate_save * lambda, AV1_PROB_COST_SHIFT);
1502
0
        if (dist_term >= rate_term) {
1503
0
            break;
1504
0
        }
1505
1506
0
        quant_buf[last_pos] = 0;
1507
0
        recon_buf[last_pos] = 0;
1508
1509
0
        int next_eob = last_scan_idx;
1510
0
        while (next_eob > 0 && quant_buf[scan[next_eob - 1]] == 0) {
1511
0
            --next_eob;
1512
0
        }
1513
0
        updated_eob = next_eob;
1514
0
    }
1515
1516
0
    return (uint16_t)updated_eob;
1517
0
}
1518
1519
uint8_t svt_aom_quantize_inv_quantize(PictureControlSet* pcs, ModeDecisionContext* ctx, int32_t* coeff,
1520
                                      int32_t* quant_coeff, int32_t* recon_coeff, uint32_t qindex,
1521
                                      int32_t segmentation_qp_offset, TxSize txsize, uint16_t* eob,
1522
                                      uint32_t component_type, uint32_t bit_depth, TxType tx_type,
1523
                                      int16_t txb_skip_context, int16_t dc_sign_context, PredictionMode pred_mode,
1524
820k
                                      uint32_t lambda, bool is_encode_pass) {
1525
820k
    SequenceControlSet* scs     = pcs->scs;
1526
820k
    EncodeContext*      enc_ctx = scs->enc_ctx;
1527
820k
    int32_t             plane   = component_type == COMPONENT_LUMA ? PLANE_Y
1528
820k
                                                                   : (component_type == COMPONENT_CHROMA_CB ? PLANE_U : PLANE_V);
1529
1530
822k
    int32_t qmatrix_level = (IS_2D_TRANSFORM(tx_type) && pcs->ppcs->frm_hdr.quantization_params.using_qmatrix)
1531
820k
        ? pcs->ppcs->frm_hdr.quantization_params.qm[plane]
1532
820k
        : NUM_QM_LEVELS - 1;
1533
1534
820k
    TxSize          adjusted_tx_size = aom_av1_get_adjusted_tx_size(txsize);
1535
820k
    MacroblockPlane candidate_plane;
1536
820k
    const QmVal*    q_matrix  = pcs->ppcs->gqmatrix[qmatrix_level][plane][adjusted_tx_size];
1537
820k
    const QmVal*    iq_matrix = pcs->ppcs->giqmatrix[qmatrix_level][plane][adjusted_tx_size];
1538
820k
    int32_t         q_index   = pcs->ppcs->frm_hdr.delta_q_params.delta_q_present
1539
820k
                  ? qindex
1540
820k
                  : pcs->ppcs->frm_hdr.quantization_params.base_q_idx;
1541
820k
    if (segmentation_qp_offset != 0) {
1542
0
        q_index = CLIP3(0, 255, q_index + segmentation_qp_offset);
1543
0
    }
1544
820k
    if (component_type != COMPONENT_LUMA) {
1545
281k
        const int8_t offset = (component_type == COMPONENT_CHROMA_CB)
1546
281k
            ? pcs->ppcs->frm_hdr.quantization_params.delta_q_dc[1] // we are assuming delta_q_ac == delta_q_dc
1547
281k
            : pcs->ppcs->frm_hdr.quantization_params.delta_q_dc[2];
1548
281k
        q_index += offset;
1549
281k
        q_index = (uint32_t)CLIP3(0, 255, (int32_t)q_index);
1550
281k
    }
1551
822k
    if (bit_depth == EB_EIGHT_BIT) {
1552
822k
        if (component_type == COMPONENT_LUMA) {
1553
541k
            candidate_plane.quant_qtx       = enc_ctx->quants_8bit.y_quant[q_index];
1554
541k
            candidate_plane.quant_fp_qtx    = enc_ctx->quants_8bit.y_quant_fp[q_index];
1555
541k
            candidate_plane.round_fp_qtx    = enc_ctx->quants_8bit.y_round_fp[q_index];
1556
541k
            candidate_plane.quant_shift_qtx = enc_ctx->quants_8bit.y_quant_shift[q_index];
1557
541k
            candidate_plane.zbin_qtx        = enc_ctx->quants_8bit.y_zbin[q_index];
1558
541k
            candidate_plane.round_qtx       = enc_ctx->quants_8bit.y_round[q_index];
1559
541k
            candidate_plane.dequant_qtx     = enc_ctx->deq_8bit.y_dequant_qtx[q_index];
1560
541k
        } else if (component_type == COMPONENT_CHROMA_CB) {
1561
140k
            candidate_plane.quant_qtx       = enc_ctx->quants_8bit.u_quant[q_index];
1562
140k
            candidate_plane.quant_fp_qtx    = enc_ctx->quants_8bit.u_quant_fp[q_index];
1563
140k
            candidate_plane.round_fp_qtx    = enc_ctx->quants_8bit.u_round_fp[q_index];
1564
140k
            candidate_plane.quant_shift_qtx = enc_ctx->quants_8bit.u_quant_shift[q_index];
1565
140k
            candidate_plane.zbin_qtx        = enc_ctx->quants_8bit.u_zbin[q_index];
1566
140k
            candidate_plane.round_qtx       = enc_ctx->quants_8bit.u_round[q_index];
1567
140k
            candidate_plane.dequant_qtx     = enc_ctx->deq_8bit.u_dequant_qtx[q_index];
1568
140k
        }
1569
1570
140k
        else {
1571
140k
            candidate_plane.quant_qtx       = enc_ctx->quants_8bit.v_quant[q_index];
1572
140k
            candidate_plane.quant_fp_qtx    = enc_ctx->quants_8bit.v_quant_fp[q_index];
1573
140k
            candidate_plane.round_fp_qtx    = enc_ctx->quants_8bit.v_round_fp[q_index];
1574
140k
            candidate_plane.quant_shift_qtx = enc_ctx->quants_8bit.v_quant_shift[q_index];
1575
140k
            candidate_plane.zbin_qtx        = enc_ctx->quants_8bit.v_zbin[q_index];
1576
140k
            candidate_plane.round_qtx       = enc_ctx->quants_8bit.v_round[q_index];
1577
140k
            candidate_plane.dequant_qtx     = enc_ctx->deq_8bit.v_dequant_qtx[q_index];
1578
140k
        }
1579
18.4E
    } else {
1580
18.4E
        if (component_type == COMPONENT_LUMA) {
1581
0
            candidate_plane.quant_qtx       = enc_ctx->quants_bd.y_quant[q_index];
1582
0
            candidate_plane.quant_fp_qtx    = enc_ctx->quants_bd.y_quant_fp[q_index];
1583
0
            candidate_plane.round_fp_qtx    = enc_ctx->quants_bd.y_round_fp[q_index];
1584
0
            candidate_plane.quant_shift_qtx = enc_ctx->quants_bd.y_quant_shift[q_index];
1585
0
            candidate_plane.zbin_qtx        = enc_ctx->quants_bd.y_zbin[q_index];
1586
0
            candidate_plane.round_qtx       = enc_ctx->quants_bd.y_round[q_index];
1587
0
            candidate_plane.dequant_qtx     = enc_ctx->deq_bd.y_dequant_qtx[q_index];
1588
0
        }
1589
1590
18.4E
        else if (component_type == COMPONENT_CHROMA_CB) {
1591
0
            candidate_plane.quant_qtx       = enc_ctx->quants_bd.u_quant[q_index];
1592
0
            candidate_plane.quant_fp_qtx    = enc_ctx->quants_bd.u_quant_fp[q_index];
1593
0
            candidate_plane.round_fp_qtx    = enc_ctx->quants_bd.u_round_fp[q_index];
1594
0
            candidate_plane.quant_shift_qtx = enc_ctx->quants_bd.u_quant_shift[q_index];
1595
0
            candidate_plane.zbin_qtx        = enc_ctx->quants_bd.u_zbin[q_index];
1596
0
            candidate_plane.round_qtx       = enc_ctx->quants_bd.u_round[q_index];
1597
0
            candidate_plane.dequant_qtx     = enc_ctx->deq_bd.u_dequant_qtx[q_index];
1598
0
        }
1599
1600
18.4E
        else {
1601
18.4E
            candidate_plane.quant_qtx       = enc_ctx->quants_bd.v_quant[q_index];
1602
18.4E
            candidate_plane.quant_fp_qtx    = enc_ctx->quants_bd.v_quant_fp[q_index];
1603
18.4E
            candidate_plane.round_fp_qtx    = enc_ctx->quants_bd.v_round_fp[q_index];
1604
18.4E
            candidate_plane.quant_shift_qtx = enc_ctx->quants_bd.v_quant_shift[q_index];
1605
18.4E
            candidate_plane.zbin_qtx        = enc_ctx->quants_bd.v_zbin[q_index];
1606
18.4E
            candidate_plane.round_qtx       = enc_ctx->quants_bd.v_round[q_index];
1607
18.4E
            candidate_plane.dequant_qtx     = enc_ctx->deq_bd.v_dequant_qtx[q_index];
1608
18.4E
        }
1609
18.4E
    }
1610
1611
820k
    const ScanOrder* const scan_order = get_scan_order(txsize, tx_type);
1612
1613
820k
    const int32_t n_coeffs = av1_get_max_eob(txsize);
1614
1615
820k
    QuantParam qparam;
1616
1617
820k
    qparam.log_scale = av1_get_tx_scale_tab[txsize];
1618
820k
    qparam.tx_size   = txsize;
1619
820k
    qparam.qmatrix   = q_matrix;
1620
820k
    qparam.iqmatrix  = iq_matrix;
1621
1622
820k
    bool is_inter = (pred_mode >= NEARESTMV);
1623
820k
    bool perform_rdoq;
1624
1625
    // If rdoq_level is specified in the command line instruction, set perform_rdoq accordingly.
1626
820k
    perform_rdoq = !svt_av1_is_lossless_segment(pcs, ctx->blk_ptr->segment_id) &&
1627
19.8k
        ((ctx->mds_do_rdoq || is_encode_pass) && ctx->rdoq_ctrls.enabled);
1628
823k
    if (!is_encode_pass) {
1629
823k
        if ((ctx->rdoq_ctrls.dct_dct_only && tx_type != DCT_DCT) ||
1630
823k
            (ctx->rdoq_ctrls.skip_uv && component_type != COMPONENT_LUMA)) {
1631
0
            perform_rdoq = 0;
1632
0
        }
1633
823k
    }
1634
820k
    if (perform_rdoq) {
1635
19.8k
#if CONFIG_ENABLE_HIGH_BIT_DEPTH
1636
19.8k
        if ((bit_depth > EB_EIGHT_BIT) || (is_encode_pass && scs->is_16bit_pipeline)) {
1637
0
            svt_av1_highbd_quantize_fp_facade((TranLow*)coeff,
1638
0
                                              n_coeffs,
1639
0
                                              &candidate_plane,
1640
0
                                              quant_coeff,
1641
0
                                              (TranLow*)recon_coeff,
1642
0
                                              eob,
1643
0
                                              scan_order,
1644
0
                                              &qparam);
1645
0
        } else
1646
19.8k
#endif
1647
19.8k
        {
1648
19.8k
            svt_av1_quantize_fp_facade((TranLow*)coeff,
1649
19.8k
                                       n_coeffs,
1650
19.8k
                                       &candidate_plane,
1651
19.8k
                                       quant_coeff,
1652
19.8k
                                       (TranLow*)recon_coeff,
1653
19.8k
                                       eob,
1654
19.8k
                                       scan_order,
1655
19.8k
                                       &qparam);
1656
19.8k
        }
1657
800k
    } else {
1658
800k
#if CONFIG_ENABLE_HIGH_BIT_DEPTH
1659
803k
        if ((bit_depth > EB_EIGHT_BIT) || (is_encode_pass && scs->is_16bit_pipeline)) {
1660
0
            svt_av1_highbd_quantize_b_facade((TranLow*)coeff,
1661
0
                                             n_coeffs,
1662
0
                                             &candidate_plane,
1663
0
                                             quant_coeff,
1664
0
                                             (TranLow*)recon_coeff,
1665
0
                                             eob,
1666
0
                                             scan_order,
1667
0
                                             &qparam);
1668
0
        } else
1669
800k
#endif
1670
800k
        {
1671
800k
            av1_quantize_b_facade_ii((TranLow*)coeff,
1672
800k
                                     n_coeffs,
1673
800k
                                     &candidate_plane,
1674
800k
                                     quant_coeff,
1675
800k
                                     (TranLow*)recon_coeff,
1676
800k
                                     eob,
1677
800k
                                     scan_order,
1678
800k
                                     &qparam);
1679
800k
        }
1680
800k
    }
1681
820k
    if (perform_rdoq && *eob != 0) {
1682
9.21k
        int width    = tx_size_wide[txsize];
1683
9.21k
        int height   = tx_size_high[txsize];
1684
9.21k
        int eob_perc = (*eob) * 100 / (width * height);
1685
9.21k
        if (eob_perc >= ctx->rdoq_ctrls.eob_th) {
1686
0
            perform_rdoq = 0;
1687
0
        }
1688
9.21k
        if (perform_rdoq && (eob_perc >= ctx->rdoq_ctrls.eob_fast_th)) {
1689
0
            svt_fast_optimize_b(
1690
0
                (TranLow*)coeff, &candidate_plane, quant_coeff, (TranLow*)recon_coeff, eob, txsize, tx_type);
1691
0
        }
1692
9.21k
        if (perform_rdoq == 0) {
1693
0
#if CONFIG_ENABLE_HIGH_BIT_DEPTH
1694
0
            if ((bit_depth > EB_EIGHT_BIT) || (is_encode_pass && scs->is_16bit_pipeline)) {
1695
0
                svt_av1_highbd_quantize_b_facade((TranLow*)coeff,
1696
0
                                                 n_coeffs,
1697
0
                                                 &candidate_plane,
1698
0
                                                 quant_coeff,
1699
0
                                                 (TranLow*)recon_coeff,
1700
0
                                                 eob,
1701
0
                                                 scan_order,
1702
0
                                                 &qparam);
1703
0
            } else
1704
0
#endif
1705
0
            {
1706
0
                av1_quantize_b_facade_ii((TranLow*)coeff,
1707
0
                                         n_coeffs,
1708
0
                                         &candidate_plane,
1709
0
                                         quant_coeff,
1710
0
                                         (TranLow*)recon_coeff,
1711
0
                                         eob,
1712
0
                                         scan_order,
1713
0
                                         &qparam);
1714
0
            }
1715
0
        }
1716
9.21k
    }
1717
820k
    if (perform_rdoq && *eob != 0) {
1718
        // Perform rdoq
1719
9.21k
        svt_av1_optimize_b(pcs,
1720
9.21k
                           ctx,
1721
9.21k
                           txb_skip_context,
1722
9.21k
                           dc_sign_context,
1723
9.21k
                           (TranLow*)coeff,
1724
9.21k
                           &candidate_plane,
1725
9.21k
                           quant_coeff,
1726
9.21k
                           (TranLow*)recon_coeff,
1727
9.21k
                           eob,
1728
9.21k
                           &qparam,
1729
9.21k
                           txsize,
1730
9.21k
                           tx_type,
1731
9.21k
                           is_inter,
1732
9.21k
                           scs->vq_ctrls.sharpness_ctrls.rdoq,
1733
9.21k
                           pcs->ppcs->frm_hdr.delta_q_params.delta_q_present,
1734
9.21k
                           pcs->ppcs->picture_qp,
1735
9.21k
                           lambda,
1736
9.21k
                           (component_type == COMPONENT_LUMA) ? 0 : 1);
1737
9.21k
    }
1738
1739
    // Apply coefficient shaving for luma after all quantization/RDOQ is complete.
1740
    // This catches all luma quantize paths (light PD1, regular TX, encode pass)
1741
    // in a single place.
1742
820k
    if (component_type == COMPONENT_LUMA && ctx->coeff_shaving_ctrls.enabled && *eob > 1) {
1743
0
        *eob = shave_coeff(quant_coeff, recon_coeff, coeff, *eob, txsize, tx_type, lambda, &ctx->coeff_shaving_ctrls);
1744
0
    }
1745
1746
820k
    if (!ctx->rate_est_ctrls.update_skip_ctx_dc_sign_ctx) {
1747
810k
        return 0;
1748
810k
    }
1749
1750
    // Derive cul_level
1751
9.13k
    return svt_av1_compute_cul_level(scan_order->scan, quant_coeff, eob);
1752
820k
}
1753
1754
void svt_aom_inv_transform_recon_wrapper(PictureControlSet* pcs, ModeDecisionContext* ctx, uint8_t* pred_buffer,
1755
                                         uint32_t pred_offset, uint32_t pred_stride, uint8_t* rec_buffer,
1756
                                         uint32_t rec_offset, uint32_t rec_stride, int32_t* rec_coeff_buffer,
1757
                                         uint32_t coeff_offset, bool hbd, TxSize txsize, TxType transform_type,
1758
31.6k
                                         PlaneType component_type, uint32_t eob) {
1759
31.6k
    if (hbd) {
1760
0
        svt_aom_inv_transform_recon(rec_coeff_buffer + coeff_offset,
1761
0
                                    CONVERT_TO_BYTEPTR(((uint16_t*)pred_buffer) + pred_offset),
1762
0
                                    pred_stride,
1763
0
                                    CONVERT_TO_BYTEPTR(((uint16_t*)rec_buffer) + rec_offset),
1764
0
                                    rec_stride,
1765
0
                                    txsize,
1766
0
                                    EB_TEN_BIT,
1767
0
                                    transform_type,
1768
0
                                    component_type,
1769
0
                                    eob,
1770
0
                                    svt_av1_is_lossless_segment(pcs, ctx->blk_ptr->segment_id));
1771
31.6k
    } else {
1772
31.6k
        svt_aom_inv_transform_recon8bit(rec_coeff_buffer + coeff_offset,
1773
31.6k
                                        pred_buffer + pred_offset,
1774
31.6k
                                        pred_stride,
1775
31.6k
                                        rec_buffer + rec_offset,
1776
31.6k
                                        rec_stride,
1777
31.6k
                                        txsize,
1778
31.6k
                                        transform_type,
1779
31.6k
                                        component_type,
1780
31.6k
                                        eob,
1781
31.6k
                                        svt_av1_is_lossless_segment(pcs, ctx->blk_ptr->segment_id));
1782
31.6k
    }
1783
31.6k
}
1784
1785
// Computes an EOB-based approximation of chroma coefficient rate.
1786
// Returns true if the approximation was applied; false if full estimation is required.
1787
static bool skip_chroma_rate_est(const ModeDecisionContext* ctx, const ModeDecisionCandidateBuffer* cand_bf,
1788
                                 COMPONENT_TYPE component_type, uint32_t tx_width_uv, uint32_t tx_height_uv,
1789
140k
                                 uint64_t* cb_coeff_bits, uint64_t* cr_coeff_bits) {
1790
    // lvl=1 always uses full estimation; lvl=0 and lvl>=2 use approximation
1791
140k
    if (!(ctx->rate_est_ctrls.coeff_rate_est_lvl >= 2 || ctx->rate_est_ctrls.coeff_rate_est_lvl == 0)) {
1792
0
        return false;
1793
0
    }
1794
140k
    const uint64_t th = ((uint64_t)tx_width_uv * tx_height_uv) >> 6;
1795
140k
    if (component_type == COMPONENT_CHROMA || component_type == COMPONENT_CHROMA_CB) {
1796
140k
        if (cand_bf->eob.u[0] < th) {
1797
3.79k
            *cb_coeff_bits = cand_bf->eob.u[0] ? (3000 + (uint64_t)cand_bf->eob.u[0] * 500) : 0;
1798
136k
        } else if (ctx->rate_est_ctrls.coeff_rate_est_lvl == 0) {
1799
136k
            *cb_coeff_bits = cand_bf->eob.u[0] ? (1500 + (uint64_t)cand_bf->eob.u[0] * 50) : 0;
1800
18.4E
        } else {
1801
18.4E
            return false;
1802
18.4E
        }
1803
140k
    }
1804
140k
    if (component_type == COMPONENT_CHROMA || component_type == COMPONENT_CHROMA_CR) {
1805
140k
        if (cand_bf->eob.v[0] < th) {
1806
3.79k
            *cr_coeff_bits = cand_bf->eob.v[0] ? (3000 + (uint64_t)cand_bf->eob.v[0] * 500) : 0;
1807
136k
        } else if (ctx->rate_est_ctrls.coeff_rate_est_lvl == 0) {
1808
136k
            *cr_coeff_bits = cand_bf->eob.v[0] ? (1500 + (uint64_t)cand_bf->eob.v[0] * 50) : 0;
1809
136k
        } else {
1810
184
            return false;
1811
184
        }
1812
140k
    }
1813
140k
    return true;
1814
140k
}
1815
1816
/*
1817
  tx path for light PD1 chroma
1818
*/
1819
void svt_aom_full_loop_chroma_light_pd1(PictureControlSet* pcs, ModeDecisionContext* ctx,
1820
                                        ModeDecisionCandidateBuffer* cand_bf, EbPictureBufferDesc* input_pic,
1821
                                        uint32_t input_cb_origin_in_index, uint32_t blk_chroma_origin_index,
1822
                                        COMPONENT_TYPE component_type, uint32_t chroma_qindex,
1823
                                        uint64_t cb_full_distortion[DIST_CALC_TOTAL],
1824
                                        uint64_t cr_full_distortion[DIST_CALC_TOTAL], uint64_t* cb_coeff_bits,
1825
0
                                        uint64_t* cr_coeff_bits) {
1826
0
    uint32_t     full_lambda  = ctx->hbd_md ? ctx->full_lambda_md[EB_10_BIT_MD] : ctx->full_lambda_md[EB_8_BIT_MD];
1827
0
    const TxSize tx_size_uv   = av1_get_max_uv_txsize(ctx->blk_geom->bsize, 1, 1);
1828
0
    const int    tx_width_uv  = tx_size_wide[tx_size_uv];
1829
0
    const int    tx_height_uv = tx_size_high[tx_size_uv];
1830
1831
0
    TxCoeffShape pf_shape = ctx->pf_ctrls.pf_shape;
1832
    // If Cb component not detected as complex, can use TX shortcuts
1833
0
    if (ctx->use_tx_shortcuts_mds3 &&
1834
0
        (ctx->chroma_complexity == COMPONENT_LUMA || ctx->chroma_complexity == COMPONENT_CHROMA_CR)) {
1835
0
        pf_shape = N4_SHAPE;
1836
0
    } else {
1837
0
        uint8_t use_pfn4_cond = 0;
1838
0
        if (ctx->lpd1_tx_ctrls.use_uv_shortcuts_on_y_coeffs &&
1839
0
            (ctx->chroma_complexity == COMPONENT_LUMA || ctx->chroma_complexity == COMPONENT_CHROMA_CR)) {
1840
0
            const uint16_t th = ((tx_width_uv >> 4) * (tx_height_uv >> 4));
1841
0
            use_pfn4_cond     = (cand_bf->cnt_nz_coeff < th) || !cand_bf->block_has_coeff ? 1 : 0;
1842
0
        }
1843
0
        if (use_pfn4_cond) {
1844
0
            pf_shape = N4_SHAPE;
1845
0
        }
1846
0
    }
1847
0
    assert(tx_size_uv < TX_SIZES_ALL);
1848
0
    const int32_t chroma_shift = (MAX_TX_SCALE - av1_get_tx_scale_tab[tx_size_uv]) * 2;
1849
0
    uint32_t      bwidth       = tx_width_uv;
1850
0
    uint32_t      bheight      = tx_height_uv;
1851
0
    if (pf_shape) {
1852
0
        bwidth  = MAX((bwidth >> pf_shape), 4);
1853
0
        bheight = (bheight >> pf_shape);
1854
0
    }
1855
0
    if (component_type == COMPONENT_CHROMA || component_type == COMPONENT_CHROMA_CB) {
1856
0
        svt_aom_residual_kernel(input_pic->u_buffer,
1857
0
                                input_cb_origin_in_index,
1858
0
                                input_pic->u_stride,
1859
0
                                cand_bf->pred->u_buffer,
1860
0
                                blk_chroma_origin_index,
1861
0
                                cand_bf->pred->u_stride,
1862
0
                                (int16_t*)cand_bf->residual->u_buffer,
1863
0
                                blk_chroma_origin_index,
1864
0
                                cand_bf->residual->u_stride,
1865
0
                                ctx->hbd_md,
1866
0
                                ctx->blk_geom->bwidth_uv,
1867
0
                                ctx->blk_geom->bheight_uv);
1868
1869
        // Cb Transform
1870
0
        svt_aom_estimate_transform(pcs,
1871
0
                                   ctx,
1872
0
                                   &(((int16_t*)cand_bf->residual->u_buffer)[blk_chroma_origin_index]),
1873
0
                                   cand_bf->residual->u_stride,
1874
0
                                   &(((int32_t*)ctx->tx_coeffs->u_buffer)[0]),
1875
0
                                   NOT_USED_VALUE,
1876
0
                                   tx_size_uv,
1877
0
                                   &ctx->three_quad_energy,
1878
0
                                   ctx->hbd_md ? EB_TEN_BIT : EB_EIGHT_BIT,
1879
0
                                   cand_bf->cand->transform_type_uv,
1880
0
                                   PLANE_TYPE_UV,
1881
0
                                   pf_shape);
1882
0
        cand_bf->quant_dc.u[0] = svt_aom_quantize_inv_quantize(pcs,
1883
0
                                                               ctx,
1884
0
                                                               &(((int32_t*)ctx->tx_coeffs->u_buffer)[0]),
1885
0
                                                               &(((int32_t*)cand_bf->quant->u_buffer)[0]),
1886
0
                                                               &(((int32_t*)cand_bf->rec_coeff->u_buffer)[0]),
1887
0
                                                               chroma_qindex,
1888
0
                                                               0,
1889
0
                                                               tx_size_uv,
1890
0
                                                               &cand_bf->eob.u[0],
1891
0
                                                               COMPONENT_CHROMA_CB,
1892
0
                                                               ctx->hbd_md ? EB_TEN_BIT : EB_EIGHT_BIT,
1893
0
                                                               cand_bf->cand->transform_type_uv,
1894
0
                                                               0,
1895
0
                                                               0,
1896
0
                                                               cand_bf->cand->block_mi.mode,
1897
0
                                                               full_lambda,
1898
0
                                                               false);
1899
1900
0
        svt_aom_picture_full_distortion32_bits_single(&(((int32_t*)ctx->tx_coeffs->u_buffer)[0]),
1901
0
                                                      &(((int32_t*)cand_bf->rec_coeff->u_buffer)[0]),
1902
0
                                                      tx_width_uv,
1903
0
                                                      bwidth,
1904
0
                                                      bheight,
1905
0
                                                      cb_full_distortion,
1906
0
                                                      cand_bf->eob.u[0]);
1907
0
        cb_full_distortion[DIST_CALC_RESIDUAL]   = RIGHT_SIGNED_SHIFT(cb_full_distortion[DIST_CALC_RESIDUAL],
1908
0
                                                                    chroma_shift);
1909
0
        cb_full_distortion[DIST_CALC_PREDICTION] = RIGHT_SIGNED_SHIFT(cb_full_distortion[DIST_CALC_PREDICTION],
1910
0
                                                                      chroma_shift);
1911
0
        cand_bf->u_has_coeff                     = (cand_bf->eob.u[0] > 0);
1912
0
    }
1913
1914
0
    pf_shape = ctx->pf_ctrls.pf_shape;
1915
    // If Cr component not detected as complex, can use TX shortcuts
1916
0
    if (ctx->use_tx_shortcuts_mds3 &&
1917
0
        (ctx->chroma_complexity == COMPONENT_LUMA || ctx->chroma_complexity == COMPONENT_CHROMA_CB)) {
1918
0
        pf_shape = N4_SHAPE;
1919
0
    } else {
1920
0
        uint8_t use_pfn4_cond = 0;
1921
0
        if (ctx->lpd1_tx_ctrls.use_uv_shortcuts_on_y_coeffs &&
1922
0
            (ctx->chroma_complexity == COMPONENT_LUMA || ctx->chroma_complexity == COMPONENT_CHROMA_CB)) {
1923
0
            const uint16_t th = ((tx_width_uv >> 4) * (tx_height_uv >> 4));
1924
0
            use_pfn4_cond     = (cand_bf->cnt_nz_coeff < th) || !cand_bf->block_has_coeff ? 1 : 0;
1925
0
        }
1926
0
        if (use_pfn4_cond) {
1927
0
            pf_shape = N4_SHAPE;
1928
0
        }
1929
0
    }
1930
0
    bwidth  = tx_width_uv;
1931
0
    bheight = tx_height_uv;
1932
0
    if (pf_shape) {
1933
0
        bwidth  = MAX((bwidth >> pf_shape), 4);
1934
0
        bheight = (bheight >> pf_shape);
1935
0
    }
1936
1937
0
    if (component_type == COMPONENT_CHROMA || component_type == COMPONENT_CHROMA_CR) {
1938
        //Cr Residual
1939
0
        svt_aom_residual_kernel(input_pic->v_buffer,
1940
0
                                input_cb_origin_in_index,
1941
0
                                input_pic->v_stride,
1942
0
                                cand_bf->pred->v_buffer,
1943
0
                                blk_chroma_origin_index,
1944
0
                                cand_bf->pred->v_stride,
1945
0
                                (int16_t*)cand_bf->residual->v_buffer,
1946
0
                                blk_chroma_origin_index,
1947
0
                                cand_bf->residual->v_stride,
1948
0
                                ctx->hbd_md,
1949
0
                                ctx->blk_geom->bwidth_uv,
1950
0
                                ctx->blk_geom->bheight_uv);
1951
        // Cr Transform
1952
0
        svt_aom_estimate_transform(pcs,
1953
0
                                   ctx,
1954
0
                                   &(((int16_t*)cand_bf->residual->v_buffer)[blk_chroma_origin_index]),
1955
0
                                   cand_bf->residual->v_stride,
1956
0
                                   &(((int32_t*)ctx->tx_coeffs->v_buffer)[0]),
1957
0
                                   NOT_USED_VALUE,
1958
0
                                   tx_size_uv,
1959
0
                                   &ctx->three_quad_energy,
1960
0
                                   ctx->hbd_md ? EB_TEN_BIT : EB_EIGHT_BIT,
1961
0
                                   cand_bf->cand->transform_type_uv,
1962
0
                                   PLANE_TYPE_UV,
1963
0
                                   pf_shape);
1964
0
        cand_bf->quant_dc.v[0] = svt_aom_quantize_inv_quantize(pcs,
1965
0
                                                               ctx,
1966
0
                                                               &(((int32_t*)ctx->tx_coeffs->v_buffer)[0]),
1967
0
                                                               &(((int32_t*)cand_bf->quant->v_buffer)[0]),
1968
0
                                                               &(((int32_t*)cand_bf->rec_coeff->v_buffer)[0]),
1969
0
                                                               chroma_qindex,
1970
0
                                                               0,
1971
0
                                                               tx_size_uv,
1972
0
                                                               &cand_bf->eob.v[0],
1973
0
                                                               COMPONENT_CHROMA_CR,
1974
0
                                                               ctx->hbd_md ? EB_TEN_BIT : EB_EIGHT_BIT,
1975
0
                                                               cand_bf->cand->transform_type_uv,
1976
0
                                                               0,
1977
0
                                                               0,
1978
0
                                                               cand_bf->cand->block_mi.mode,
1979
0
                                                               full_lambda,
1980
0
                                                               false);
1981
1982
0
        svt_aom_picture_full_distortion32_bits_single(&(((int32_t*)ctx->tx_coeffs->v_buffer)[0]),
1983
0
                                                      &(((int32_t*)cand_bf->rec_coeff->v_buffer)[0]),
1984
0
                                                      tx_width_uv,
1985
0
                                                      bwidth,
1986
0
                                                      bheight,
1987
0
                                                      cr_full_distortion,
1988
0
                                                      cand_bf->eob.v[0]);
1989
1990
0
        cr_full_distortion[DIST_CALC_RESIDUAL]   = RIGHT_SIGNED_SHIFT(cr_full_distortion[DIST_CALC_RESIDUAL],
1991
0
                                                                    chroma_shift);
1992
0
        cr_full_distortion[DIST_CALC_PREDICTION] = RIGHT_SIGNED_SHIFT(cr_full_distortion[DIST_CALC_PREDICTION],
1993
0
                                                                      chroma_shift);
1994
0
        cand_bf->v_has_coeff                     = (cand_bf->eob.v[0] > 0);
1995
0
    }
1996
1997
    //CHROMA-ONLY
1998
0
    if (!skip_chroma_rate_est(ctx, cand_bf, component_type, tx_width_uv, tx_height_uv, cb_coeff_bits, cr_coeff_bits)) {
1999
0
        svt_aom_txb_estimate_coeff_bits(ctx,
2000
0
                                        0,
2001
0
                                        NULL,
2002
0
                                        pcs,
2003
0
                                        cand_bf,
2004
0
                                        NOT_USED_VALUE,
2005
0
                                        0,
2006
0
                                        cand_bf->quant,
2007
0
                                        NOT_USED_VALUE,
2008
0
                                        cand_bf->eob.u[0],
2009
0
                                        cand_bf->eob.v[0],
2010
0
                                        NOT_USED_VALUE,
2011
0
                                        cb_coeff_bits,
2012
0
                                        cr_coeff_bits,
2013
0
                                        NOT_USED_VALUE,
2014
0
                                        tx_size_uv,
2015
0
                                        NOT_USED_VALUE,
2016
0
                                        cand_bf->cand->transform_type_uv,
2017
0
                                        component_type);
2018
0
    }
2019
0
}
2020
2021
/****************************************
2022
 ************  Full loop ****************
2023
****************************************/
2024
void svt_aom_full_loop_uv(PictureControlSet* pcs, ModeDecisionContext* ctx, ModeDecisionCandidateBuffer* cand_bf,
2025
                          EbPictureBufferDesc* input_pic, COMPONENT_TYPE component_type, uint32_t chroma_qindex,
2026
                          uint64_t cb_full_distortion[DIST_TOTAL][DIST_CALC_TOTAL],
2027
                          uint64_t cr_full_distortion[DIST_TOTAL][DIST_CALC_TOTAL], uint64_t* cb_coeff_bits,
2028
139k
                          uint64_t* cr_coeff_bits, bool is_full_loop) {
2029
139k
    EbSpatialFullDistType spatial_full_dist_type_fun = ctx->hbd_md ? svt_full_distortion_kernel16_bits
2030
139k
                                                                   : svt_spatial_full_distortion_kernel;
2031
139k
    EB_ALIGN(16) uint64_t txb_full_distortion[DIST_TOTAL][3][DIST_CALC_TOTAL];
2032
139k
    const SsimLevel       ssim_level = ctx->tune_ssim_level;
2033
139k
    if (ssim_level > SSIM_LVL_0) {
2034
0
        assert(ctx->pd_pass == PD_PASS_1);
2035
0
        assert(ctx->md_stage == MD_STAGE_3);
2036
0
    }
2037
139k
    cand_bf->u_has_coeff = 0;
2038
139k
    cand_bf->v_has_coeff = 0;
2039
139k
    int16_t* chroma_residual_ptr;
2040
139k
    uint32_t full_lambda = ctx->hbd_md ? ctx->full_lambda_md[EB_10_BIT_MD] : ctx->full_lambda_md[EB_8_BIT_MD];
2041
2042
139k
    ctx->three_quad_energy = 0;
2043
2044
139k
    const double effective_ac_bias = get_effective_ac_bias(
2045
139k
        pcs->scs->static_config.ac_bias, pcs->slice_type == I_SLICE, pcs->temporal_layer_index);
2046
139k
    const uint8_t tx_depth     = cand_bf->cand->block_mi.tx_depth;
2047
139k
    const TxSize  tx_size      = av1_get_tx_size(ctx->blk_geom->bsize, tx_depth, PLANE_TYPE_Y);
2048
139k
    const TxSize  tx_size_uv   = av1_get_max_uv_txsize(ctx->blk_geom->bsize, 1, 1);
2049
139k
    const int     tx_width_uv  = tx_size_wide[tx_size_uv];
2050
139k
    const int     tx_height_uv = tx_size_high[tx_size_uv];
2051
140k
    const bool    is_inter = (is_inter_mode(cand_bf->cand->block_mi.mode) || cand_bf->cand->block_mi.use_intrabc) ? true
2052
139k
                                                                                                                  : false;
2053
139k
    const int     tu_count = tx_depth ? 1 : tx_blocks_per_depth[ctx->blk_geom->bsize][tx_depth]; //NM: 128x128 exeption
2054
139k
    uint32_t      txb_1d_offset = 0;
2055
2056
139k
    int txb_itr = 0;
2057
139k
    do {
2058
139k
        const uint32_t txb_origin_x        = tx_org[ctx->blk_geom->bsize][is_inter][tx_depth][txb_itr].x;
2059
139k
        const uint32_t txb_origin_y        = tx_org[ctx->blk_geom->bsize][is_inter][tx_depth][txb_itr].y;
2060
139k
        int32_t        cropped_tx_width_uv = MIN(
2061
139k
            (uint32_t)tx_width_uv, (pcs->ppcs->aligned_width >> 1) - ((ROUND_UV(ctx->blk_org_x + txb_origin_x)) >> 1));
2062
139k
        int32_t cropped_tx_height_uv = MIN(
2063
139k
            (uint32_t)tx_height_uv,
2064
139k
            (pcs->ppcs->aligned_height >> 1) - ((ROUND_UV(ctx->blk_org_y + txb_origin_y)) >> 1));
2065
139k
        uint32_t tu_cb_origin_index = (ROUND_UV(txb_origin_x) +
2066
139k
                                       (ROUND_UV(txb_origin_y) * cand_bf->residual->u_stride)) >>
2067
139k
            1;
2068
139k
        uint32_t tu_cr_origin_index = (ROUND_UV(txb_origin_x) +
2069
139k
                                       (ROUND_UV(txb_origin_y) * cand_bf->residual->v_stride)) >>
2070
139k
            1;
2071
139k
        TxCoeffShape pf_shape = ctx->pf_ctrls.pf_shape;
2072
140k
        if (ctx->md_stage == MD_STAGE_3 && ctx->use_tx_shortcuts_mds3 && ctx->chroma_complexity == COMPONENT_LUMA) {
2073
0
            pf_shape = N4_SHAPE;
2074
0
        }
2075
        // for chroma path, use luma coeff info to make shortcut decisions (available even if MDS1 is skipped)
2076
139k
        else if (ctx->tx_shortcut_ctrls.apply_pf_on_coeffs && ctx->md_stage == MD_STAGE_3 &&
2077
0
                 ctx->chroma_complexity == COMPONENT_LUMA) {
2078
0
            uint8_t use_pfn4_cond = 0;
2079
2080
0
            const uint16_t th = (tx_width_uv >> 4) * (tx_height_uv >> 4);
2081
0
            use_pfn4_cond     = (cand_bf->cnt_nz_coeff < th) || !cand_bf->block_has_coeff ? 1 : 0;
2082
2083
0
            if (use_pfn4_cond) {
2084
0
                pf_shape = N4_SHAPE;
2085
0
            }
2086
0
        }
2087
        //    This function replaces the previous Intra Chroma mode if the LM fast
2088
        //    cost is better.
2089
        //    *Note - this might require that we have inv transform in the loop
2090
139k
        if (component_type == COMPONENT_CHROMA_CB || component_type == COMPONENT_CHROMA ||
2091
139k
            component_type == COMPONENT_ALL) {
2092
139k
            ctx->cb_txb_skip_context = 0;
2093
139k
            ctx->cb_dc_sign_context  = 0;
2094
139k
            if (ctx->rate_est_ctrls.update_skip_ctx_dc_sign_ctx) {
2095
0
                svt_aom_get_txb_ctx(pcs,
2096
0
                                    COMPONENT_CHROMA,
2097
0
                                    ctx->cb_dc_sign_level_coeff_na,
2098
0
                                    ROUND_UV(ctx->blk_org_x + txb_origin_x) >> 1,
2099
0
                                    ROUND_UV(ctx->blk_org_y + txb_origin_y) >> 1,
2100
0
                                    ctx->blk_geom->bsize_uv,
2101
0
                                    tx_size_uv,
2102
0
                                    &ctx->cb_txb_skip_context,
2103
0
                                    &ctx->cb_dc_sign_context);
2104
0
            }
2105
            // Configure the Chroma Residual Ptr
2106
2107
139k
            chroma_residual_ptr = &(((int16_t*)cand_bf->residual->u_buffer)[tu_cb_origin_index]);
2108
2109
            // Cb Transform
2110
139k
            svt_aom_estimate_transform(pcs,
2111
139k
                                       ctx,
2112
139k
                                       chroma_residual_ptr,
2113
139k
                                       cand_bf->residual->u_stride,
2114
139k
                                       &(((int32_t*)ctx->tx_coeffs->u_buffer)[txb_1d_offset]),
2115
139k
                                       NOT_USED_VALUE,
2116
139k
                                       tx_size_uv,
2117
139k
                                       &ctx->three_quad_energy,
2118
139k
                                       ctx->hbd_md ? EB_TEN_BIT : EB_EIGHT_BIT,
2119
139k
                                       cand_bf->cand->transform_type_uv,
2120
139k
                                       PLANE_TYPE_UV,
2121
139k
                                       pf_shape);
2122
2123
139k
            int32_t seg_qp               = pcs->ppcs->frm_hdr.segmentation_params.segmentation_enabled
2124
139k
                              ? pcs->ppcs->frm_hdr.segmentation_params.feature_data[ctx->blk_ptr->segment_id][SEG_LVL_ALT_Q]
2125
139k
                              : 0;
2126
139k
            cand_bf->quant_dc.u[txb_itr] = svt_aom_quantize_inv_quantize(
2127
139k
                pcs,
2128
139k
                ctx,
2129
139k
                &(((int32_t*)ctx->tx_coeffs->u_buffer)[txb_1d_offset]),
2130
139k
                &(((int32_t*)cand_bf->quant->u_buffer)[txb_1d_offset]),
2131
139k
                &(((int32_t*)cand_bf->rec_coeff->u_buffer)[txb_1d_offset]),
2132
139k
                chroma_qindex,
2133
139k
                seg_qp,
2134
139k
                tx_size_uv,
2135
139k
                &cand_bf->eob.u[txb_itr],
2136
139k
                COMPONENT_CHROMA_CB,
2137
139k
                ctx->hbd_md ? EB_TEN_BIT : EB_EIGHT_BIT,
2138
139k
                cand_bf->cand->transform_type_uv,
2139
139k
                ctx->cb_txb_skip_context,
2140
139k
                ctx->cb_dc_sign_context,
2141
139k
                cand_bf->cand->block_mi.mode,
2142
139k
                full_lambda,
2143
139k
                false);
2144
2145
140k
            if (is_full_loop && ctx->mds_do_spatial_sse) {
2146
140k
                uint32_t cb_has_coeff = cand_bf->eob.u[txb_itr] > 0;
2147
2148
140k
                if (cb_has_coeff) {
2149
5.22k
                    svt_aom_inv_transform_recon_wrapper(pcs,
2150
5.22k
                                                        ctx,
2151
5.22k
                                                        cand_bf->pred->u_buffer,
2152
5.22k
                                                        tu_cb_origin_index,
2153
5.22k
                                                        cand_bf->pred->u_stride,
2154
5.22k
                                                        cand_bf->recon->u_buffer,
2155
5.22k
                                                        tu_cb_origin_index,
2156
5.22k
                                                        cand_bf->recon->u_stride,
2157
5.22k
                                                        (int32_t*)cand_bf->rec_coeff->u_buffer,
2158
5.22k
                                                        txb_1d_offset,
2159
5.22k
                                                        ctx->hbd_md,
2160
5.22k
                                                        tx_size_uv,
2161
5.22k
                                                        cand_bf->cand->transform_type_uv,
2162
5.22k
                                                        PLANE_TYPE_UV,
2163
5.22k
                                                        (uint32_t)cand_bf->eob.u[txb_itr]);
2164
135k
                } else {
2165
135k
                    svt_av1_picture_copy_cb(cand_bf->pred,
2166
135k
                                            tu_cb_origin_index,
2167
135k
                                            cand_bf->recon,
2168
135k
                                            tu_cb_origin_index,
2169
135k
                                            tx_width_uv,
2170
135k
                                            tx_height_uv,
2171
135k
                                            ctx->hbd_md);
2172
135k
                }
2173
2174
140k
                const uint32_t input_chroma_txb_origin_index = ((ROUND_UV(ctx->blk_org_x + txb_origin_x)) >> 1) +
2175
140k
                    ((ROUND_UV(ctx->blk_org_y + txb_origin_y)) >> 1) * input_pic->u_stride;
2176
140k
                const int32_t txb_uv_origin_index = (ROUND_UV(txb_origin_x) +
2177
140k
                                                     (ROUND_UV(txb_origin_y) * cand_bf->quant->u_stride)) >>
2178
140k
                    1;
2179
2180
140k
                if (ssim_level == SSIM_LVL_1 || ssim_level == SSIM_LVL_3) {
2181
0
                    txb_full_distortion[DIST_SSIM][1][DIST_CALC_PREDICTION] = svt_spatial_full_distortion_ssim_kernel(
2182
0
                        input_pic->u_buffer,
2183
0
                        input_chroma_txb_origin_index,
2184
0
                        input_pic->u_stride,
2185
0
                        cand_bf->pred->u_buffer,
2186
0
                        txb_uv_origin_index,
2187
0
                        cand_bf->pred->u_stride,
2188
0
                        cropped_tx_width_uv,
2189
0
                        cropped_tx_height_uv,
2190
0
                        ctx->hbd_md,
2191
0
                        effective_ac_bias);
2192
2193
0
                    txb_full_distortion[DIST_SSIM][1][DIST_CALC_RESIDUAL] = svt_spatial_full_distortion_ssim_kernel(
2194
0
                        input_pic->u_buffer,
2195
0
                        input_chroma_txb_origin_index,
2196
0
                        input_pic->u_stride,
2197
0
                        cand_bf->recon->u_buffer,
2198
0
                        txb_uv_origin_index,
2199
0
                        cand_bf->recon->u_stride,
2200
0
                        cropped_tx_width_uv,
2201
0
                        cropped_tx_height_uv,
2202
0
                        ctx->hbd_md,
2203
0
                        effective_ac_bias);
2204
2205
0
                    txb_full_distortion[DIST_SSIM][1][DIST_CALC_PREDICTION] <<= 4;
2206
0
                    txb_full_distortion[DIST_SSIM][1][DIST_CALC_RESIDUAL] <<= 4;
2207
0
                }
2208
140k
                txb_full_distortion[DIST_SSD][1][DIST_CALC_PREDICTION] = spatial_full_dist_type_fun(
2209
140k
                    input_pic->u_buffer,
2210
140k
                    input_chroma_txb_origin_index,
2211
140k
                    input_pic->u_stride,
2212
140k
                    cand_bf->pred->u_buffer,
2213
140k
                    txb_uv_origin_index,
2214
140k
                    cand_bf->pred->u_stride,
2215
140k
                    cropped_tx_width_uv,
2216
140k
                    cropped_tx_height_uv);
2217
140k
                if (effective_ac_bias) {
2218
0
                    txb_full_distortion[DIST_SSD][1][DIST_CALC_PREDICTION] += get_svt_psy_full_dist(
2219
0
                        input_pic->u_buffer,
2220
0
                        input_chroma_txb_origin_index,
2221
0
                        input_pic->u_stride,
2222
0
                        cand_bf->pred->u_buffer,
2223
0
                        txb_uv_origin_index,
2224
0
                        cand_bf->pred->u_stride,
2225
0
                        cropped_tx_width_uv,
2226
0
                        cropped_tx_height_uv,
2227
0
                        ctx->hbd_md,
2228
0
                        effective_ac_bias);
2229
0
                }
2230
2231
140k
                txb_full_distortion[DIST_SSD][1][DIST_CALC_RESIDUAL] = spatial_full_dist_type_fun(
2232
140k
                    input_pic->u_buffer,
2233
140k
                    input_chroma_txb_origin_index,
2234
140k
                    input_pic->u_stride,
2235
140k
                    cand_bf->recon->u_buffer,
2236
140k
                    txb_uv_origin_index,
2237
140k
                    cand_bf->recon->u_stride,
2238
140k
                    cropped_tx_width_uv,
2239
140k
                    cropped_tx_height_uv);
2240
140k
                if (effective_ac_bias) {
2241
0
                    txb_full_distortion[DIST_SSD][1][DIST_CALC_RESIDUAL] += get_svt_psy_full_dist(
2242
0
                        input_pic->u_buffer,
2243
0
                        input_chroma_txb_origin_index,
2244
0
                        input_pic->u_stride,
2245
0
                        cand_bf->recon->u_buffer,
2246
0
                        txb_uv_origin_index,
2247
0
                        cand_bf->recon->u_stride,
2248
0
                        cropped_tx_width_uv,
2249
0
                        cropped_tx_height_uv,
2250
0
                        ctx->hbd_md,
2251
0
                        effective_ac_bias);
2252
0
                }
2253
2254
140k
                txb_full_distortion[DIST_SSD][1][DIST_CALC_PREDICTION] <<= 4;
2255
140k
                txb_full_distortion[DIST_SSD][1][DIST_CALC_RESIDUAL] <<= 4;
2256
18.4E
            } else {
2257
                // *Full Distortion (SSE)
2258
                // *Note - there are known issues with how this distortion metric is currently
2259
                //    calculated.  The amount of scaling between the two arrays is not
2260
                //    equivalent.
2261
18.4E
                uint32_t bwidth  = tx_width_uv;
2262
18.4E
                uint32_t bheight = tx_height_uv;
2263
18.4E
                if (pf_shape) {
2264
0
                    bwidth  = MAX((bwidth >> pf_shape), 4);
2265
0
                    bheight = (bheight >> pf_shape);
2266
0
                }
2267
18.4E
                svt_aom_picture_full_distortion32_bits_single(
2268
18.4E
                    &(((int32_t*)ctx->tx_coeffs->u_buffer)[txb_1d_offset]),
2269
18.4E
                    &(((int32_t*)cand_bf->rec_coeff->u_buffer)[txb_1d_offset]),
2270
18.4E
                    tx_width_uv,
2271
18.4E
                    bwidth,
2272
18.4E
                    bheight,
2273
18.4E
                    txb_full_distortion[DIST_SSD][1],
2274
18.4E
                    cand_bf->eob.u[txb_itr]);
2275
2276
18.4E
                const int32_t chroma_shift = (MAX_TX_SCALE - av1_get_tx_scale_tab[tx_size_uv]) * 2;
2277
18.4E
                txb_full_distortion[DIST_SSD][1][DIST_CALC_RESIDUAL] = RIGHT_SIGNED_SHIFT(
2278
18.4E
                    txb_full_distortion[DIST_SSD][1][DIST_CALC_RESIDUAL], chroma_shift);
2279
18.4E
                txb_full_distortion[DIST_SSD][1][DIST_CALC_PREDICTION] = RIGHT_SIGNED_SHIFT(
2280
18.4E
                    txb_full_distortion[DIST_SSD][1][DIST_CALC_PREDICTION], chroma_shift);
2281
18.4E
            }
2282
139k
            cand_bf->u_has_coeff |= ((cand_bf->eob.u[txb_itr] != 0) << txb_itr);
2283
139k
            cb_full_distortion[DIST_SSIM][DIST_CALC_RESIDUAL] += txb_full_distortion[DIST_SSIM][1][DIST_CALC_RESIDUAL];
2284
139k
            cb_full_distortion[DIST_SSIM][DIST_CALC_PREDICTION] +=
2285
139k
                txb_full_distortion[DIST_SSIM][1][DIST_CALC_PREDICTION];
2286
2287
139k
            cb_full_distortion[DIST_SSD][DIST_CALC_RESIDUAL] += txb_full_distortion[DIST_SSD][1][DIST_CALC_RESIDUAL];
2288
139k
            cb_full_distortion[DIST_SSD][DIST_CALC_PREDICTION] +=
2289
139k
                txb_full_distortion[DIST_SSD][1][DIST_CALC_PREDICTION];
2290
139k
        }
2291
2292
140k
        if (component_type == COMPONENT_CHROMA_CR || component_type == COMPONENT_CHROMA ||
2293
140k
            component_type == COMPONENT_ALL) {
2294
140k
            ctx->cr_txb_skip_context = 0;
2295
140k
            ctx->cr_dc_sign_context  = 0;
2296
140k
            if (ctx->rate_est_ctrls.update_skip_ctx_dc_sign_ctx) {
2297
0
                svt_aom_get_txb_ctx(pcs,
2298
0
                                    COMPONENT_CHROMA,
2299
0
                                    ctx->cr_dc_sign_level_coeff_na,
2300
0
                                    ROUND_UV(ctx->blk_org_x + txb_origin_x) >> 1,
2301
0
                                    ROUND_UV(ctx->blk_org_y + txb_origin_y) >> 1,
2302
0
                                    ctx->blk_geom->bsize_uv,
2303
0
                                    tx_size_uv,
2304
0
                                    &ctx->cr_txb_skip_context,
2305
0
                                    &ctx->cr_dc_sign_context);
2306
0
            }
2307
            // Configure the Chroma Residual Ptr
2308
2309
140k
            chroma_residual_ptr = &(((int16_t*)cand_bf->residual->v_buffer)[tu_cr_origin_index]);
2310
2311
            // Cr Transform
2312
140k
            svt_aom_estimate_transform(pcs,
2313
140k
                                       ctx,
2314
140k
                                       chroma_residual_ptr,
2315
140k
                                       cand_bf->residual->v_stride,
2316
140k
                                       &(((int32_t*)ctx->tx_coeffs->v_buffer)[txb_1d_offset]),
2317
140k
                                       NOT_USED_VALUE,
2318
140k
                                       tx_size_uv,
2319
140k
                                       &ctx->three_quad_energy,
2320
140k
                                       ctx->hbd_md ? EB_TEN_BIT : EB_EIGHT_BIT,
2321
140k
                                       cand_bf->cand->transform_type_uv,
2322
140k
                                       PLANE_TYPE_UV,
2323
140k
                                       pf_shape);
2324
140k
            int32_t seg_qp               = pcs->ppcs->frm_hdr.segmentation_params.segmentation_enabled
2325
140k
                              ? pcs->ppcs->frm_hdr.segmentation_params.feature_data[ctx->blk_ptr->segment_id][SEG_LVL_ALT_Q]
2326
140k
                              : 0;
2327
140k
            cand_bf->quant_dc.v[txb_itr] = svt_aom_quantize_inv_quantize(
2328
140k
                pcs,
2329
140k
                ctx,
2330
140k
                &(((int32_t*)ctx->tx_coeffs->v_buffer)[txb_1d_offset]),
2331
140k
                &(((int32_t*)cand_bf->quant->v_buffer)[txb_1d_offset]),
2332
140k
                &(((int32_t*)cand_bf->rec_coeff->v_buffer)[txb_1d_offset]),
2333
140k
                chroma_qindex,
2334
140k
                seg_qp,
2335
140k
                tx_size_uv,
2336
140k
                &cand_bf->eob.v[txb_itr],
2337
140k
                COMPONENT_CHROMA_CR,
2338
140k
                ctx->hbd_md ? EB_TEN_BIT : EB_EIGHT_BIT,
2339
140k
                cand_bf->cand->transform_type_uv,
2340
140k
                ctx->cr_txb_skip_context,
2341
140k
                ctx->cr_dc_sign_context,
2342
140k
                cand_bf->cand->block_mi.mode,
2343
140k
                full_lambda,
2344
140k
                false);
2345
140k
            if (is_full_loop && ctx->mds_do_spatial_sse) {
2346
140k
                uint32_t cr_has_coeff = cand_bf->eob.v[txb_itr] > 0;
2347
2348
140k
                if (cr_has_coeff) {
2349
5.22k
                    svt_aom_inv_transform_recon_wrapper(pcs,
2350
5.22k
                                                        ctx,
2351
5.22k
                                                        cand_bf->pred->v_buffer,
2352
5.22k
                                                        tu_cr_origin_index,
2353
5.22k
                                                        cand_bf->pred->v_stride,
2354
5.22k
                                                        cand_bf->recon->v_buffer,
2355
5.22k
                                                        tu_cr_origin_index,
2356
5.22k
                                                        cand_bf->recon->v_stride,
2357
5.22k
                                                        (int32_t*)cand_bf->rec_coeff->v_buffer,
2358
5.22k
                                                        txb_1d_offset,
2359
5.22k
                                                        ctx->hbd_md,
2360
5.22k
                                                        tx_size_uv,
2361
5.22k
                                                        cand_bf->cand->transform_type_uv,
2362
5.22k
                                                        PLANE_TYPE_UV,
2363
5.22k
                                                        (uint32_t)cand_bf->eob.v[txb_itr]);
2364
135k
                } else {
2365
135k
                    svt_av1_picture_copy_cr(cand_bf->pred,
2366
135k
                                            tu_cb_origin_index,
2367
135k
                                            cand_bf->recon,
2368
135k
                                            tu_cb_origin_index,
2369
135k
                                            tx_width_uv,
2370
135k
                                            tx_height_uv,
2371
135k
                                            ctx->hbd_md);
2372
135k
                }
2373
140k
                const uint32_t input_chroma_txb_origin_index = ((ROUND_UV(ctx->blk_org_x + txb_origin_x)) >> 1) +
2374
140k
                    ((ROUND_UV(ctx->blk_org_y + txb_origin_y)) >> 1) * input_pic->v_stride;
2375
140k
                const int32_t txb_uv_origin_index = (ROUND_UV(txb_origin_x) +
2376
140k
                                                     (ROUND_UV(txb_origin_y) * cand_bf->quant->v_stride)) >>
2377
140k
                    1;
2378
2379
140k
                if (ssim_level == SSIM_LVL_1 || ssim_level == SSIM_LVL_3) {
2380
0
                    txb_full_distortion[DIST_SSIM][2][DIST_CALC_PREDICTION] = svt_spatial_full_distortion_ssim_kernel(
2381
0
                        input_pic->v_buffer,
2382
0
                        input_chroma_txb_origin_index,
2383
0
                        input_pic->v_stride,
2384
0
                        cand_bf->pred->v_buffer,
2385
0
                        txb_uv_origin_index,
2386
0
                        cand_bf->pred->v_stride,
2387
0
                        cropped_tx_width_uv,
2388
0
                        cropped_tx_height_uv,
2389
0
                        ctx->hbd_md,
2390
0
                        effective_ac_bias);
2391
2392
0
                    txb_full_distortion[DIST_SSIM][2][DIST_CALC_RESIDUAL] = svt_spatial_full_distortion_ssim_kernel(
2393
0
                        input_pic->v_buffer,
2394
0
                        input_chroma_txb_origin_index,
2395
0
                        input_pic->v_stride,
2396
0
                        cand_bf->recon->v_buffer,
2397
0
                        txb_uv_origin_index,
2398
0
                        cand_bf->recon->v_stride,
2399
0
                        cropped_tx_width_uv,
2400
0
                        cropped_tx_height_uv,
2401
0
                        ctx->hbd_md,
2402
0
                        effective_ac_bias);
2403
2404
0
                    txb_full_distortion[DIST_SSIM][2][DIST_CALC_PREDICTION] <<= 4;
2405
0
                    txb_full_distortion[DIST_SSIM][2][DIST_CALC_RESIDUAL] <<= 4;
2406
0
                }
2407
140k
                txb_full_distortion[DIST_SSD][2][DIST_CALC_PREDICTION] = spatial_full_dist_type_fun(
2408
140k
                    input_pic->v_buffer,
2409
140k
                    input_chroma_txb_origin_index,
2410
140k
                    input_pic->v_stride,
2411
140k
                    cand_bf->pred->v_buffer,
2412
140k
                    txb_uv_origin_index,
2413
140k
                    cand_bf->pred->v_stride,
2414
140k
                    cropped_tx_width_uv,
2415
140k
                    cropped_tx_height_uv);
2416
140k
                if (effective_ac_bias) {
2417
0
                    txb_full_distortion[DIST_SSD][2][DIST_CALC_PREDICTION] += get_svt_psy_full_dist(
2418
0
                        input_pic->v_buffer,
2419
0
                        input_chroma_txb_origin_index,
2420
0
                        input_pic->v_stride,
2421
0
                        cand_bf->pred->v_buffer,
2422
0
                        txb_uv_origin_index,
2423
0
                        cand_bf->pred->v_stride,
2424
0
                        cropped_tx_width_uv,
2425
0
                        cropped_tx_height_uv,
2426
0
                        ctx->hbd_md,
2427
0
                        effective_ac_bias);
2428
0
                }
2429
2430
140k
                txb_full_distortion[DIST_SSD][2][DIST_CALC_RESIDUAL] = spatial_full_dist_type_fun(
2431
140k
                    input_pic->v_buffer,
2432
140k
                    input_chroma_txb_origin_index,
2433
140k
                    input_pic->v_stride,
2434
140k
                    cand_bf->recon->v_buffer,
2435
140k
                    txb_uv_origin_index,
2436
140k
                    cand_bf->recon->v_stride,
2437
140k
                    cropped_tx_width_uv,
2438
140k
                    cropped_tx_height_uv);
2439
140k
                if (effective_ac_bias) {
2440
0
                    txb_full_distortion[DIST_SSD][2][DIST_CALC_RESIDUAL] += get_svt_psy_full_dist(
2441
0
                        input_pic->v_buffer,
2442
0
                        input_chroma_txb_origin_index,
2443
0
                        input_pic->v_stride,
2444
0
                        cand_bf->recon->v_buffer,
2445
0
                        txb_uv_origin_index,
2446
0
                        cand_bf->recon->v_stride,
2447
0
                        cropped_tx_width_uv,
2448
0
                        cropped_tx_height_uv,
2449
0
                        ctx->hbd_md,
2450
0
                        effective_ac_bias);
2451
0
                }
2452
2453
140k
                txb_full_distortion[DIST_SSD][2][DIST_CALC_PREDICTION] <<= 4;
2454
140k
                txb_full_distortion[DIST_SSD][2][DIST_CALC_RESIDUAL] <<= 4;
2455
140k
            } else {
2456
                // *Full Distortion (SSE)
2457
                // *Note - there are known issues with how this distortion metric is currently
2458
                //    calculated.  The amount of scaling between the two arrays is not
2459
                //    equivalent.
2460
243
                uint32_t bwidth  = tx_width_uv;
2461
243
                uint32_t bheight = tx_height_uv;
2462
243
                if (pf_shape) {
2463
0
                    bwidth  = MAX((bwidth >> pf_shape), 4);
2464
0
                    bheight = (bheight >> pf_shape);
2465
0
                }
2466
243
                svt_aom_picture_full_distortion32_bits_single(
2467
243
                    &(((int32_t*)ctx->tx_coeffs->v_buffer)[txb_1d_offset]),
2468
243
                    &(((int32_t*)cand_bf->rec_coeff->v_buffer)[txb_1d_offset]),
2469
243
                    tx_width_uv,
2470
243
                    bwidth,
2471
243
                    bheight,
2472
243
                    txb_full_distortion[DIST_SSD][2],
2473
243
                    cand_bf->eob.v[txb_itr]);
2474
2475
243
                const int32_t chroma_shift = (MAX_TX_SCALE - av1_get_tx_scale_tab[tx_size_uv]) * 2;
2476
243
                txb_full_distortion[DIST_SSD][2][DIST_CALC_RESIDUAL] = RIGHT_SIGNED_SHIFT(
2477
243
                    txb_full_distortion[DIST_SSD][2][DIST_CALC_RESIDUAL], chroma_shift);
2478
243
                txb_full_distortion[DIST_SSD][2][DIST_CALC_PREDICTION] = RIGHT_SIGNED_SHIFT(
2479
243
                    txb_full_distortion[DIST_SSD][2][DIST_CALC_PREDICTION], chroma_shift);
2480
243
            }
2481
140k
            cand_bf->v_has_coeff |= ((cand_bf->eob.v[txb_itr] != 0) << txb_itr);
2482
140k
            cr_full_distortion[DIST_SSIM][DIST_CALC_RESIDUAL] += txb_full_distortion[DIST_SSIM][2][DIST_CALC_RESIDUAL];
2483
140k
            cr_full_distortion[DIST_SSIM][DIST_CALC_PREDICTION] +=
2484
140k
                txb_full_distortion[DIST_SSIM][2][DIST_CALC_PREDICTION];
2485
2486
140k
            cr_full_distortion[DIST_SSD][DIST_CALC_RESIDUAL] += txb_full_distortion[DIST_SSD][2][DIST_CALC_RESIDUAL];
2487
140k
            cr_full_distortion[DIST_SSD][DIST_CALC_PREDICTION] +=
2488
140k
                txb_full_distortion[DIST_SSD][2][DIST_CALC_PREDICTION];
2489
140k
        }
2490
2491
139k
        const uint32_t txb_origin_index = txb_origin_x + txb_origin_y * cand_bf->quant->y_stride;
2492
2493
        // Reset the Bit Costs
2494
139k
        uint64_t y_txb_coeff_bits  = 0;
2495
139k
        uint64_t cb_txb_coeff_bits = 0;
2496
139k
        uint64_t cr_txb_coeff_bits = 0;
2497
2498
139k
        if (!skip_chroma_rate_est(
2499
139k
                ctx, cand_bf, component_type, tx_width_uv, tx_height_uv, cb_coeff_bits, cr_coeff_bits)) {
2500
            //CHROMA-ONLY
2501
0
            svt_aom_txb_estimate_coeff_bits(ctx,
2502
0
                                            0,
2503
0
                                            NULL,
2504
0
                                            pcs,
2505
0
                                            cand_bf,
2506
0
                                            txb_origin_index,
2507
0
                                            txb_1d_offset,
2508
0
                                            cand_bf->quant,
2509
0
                                            cand_bf->eob.y[txb_itr],
2510
0
                                            cand_bf->eob.u[txb_itr],
2511
0
                                            cand_bf->eob.v[txb_itr],
2512
0
                                            &y_txb_coeff_bits,
2513
0
                                            &cb_txb_coeff_bits,
2514
0
                                            &cr_txb_coeff_bits,
2515
0
                                            tx_size,
2516
0
                                            tx_size_uv,
2517
0
                                            cand_bf->cand->transform_type[txb_itr],
2518
0
                                            cand_bf->cand->transform_type_uv,
2519
0
                                            component_type);
2520
2521
0
            *cb_coeff_bits += cb_txb_coeff_bits;
2522
0
            *cr_coeff_bits += cr_txb_coeff_bits;
2523
0
        }
2524
139k
        txb_1d_offset += tx_width_uv * tx_height_uv;
2525
2526
139k
        ++txb_itr;
2527
139k
    } while (txb_itr < tu_count);
2528
139k
}
2529
2530
/*
2531
  check if we need to do inverse transform and recon
2532
*/
2533
140k
uint8_t svt_aom_do_md_recon(PictureParentControlSet* pcs, ModeDecisionContext* ctx) {
2534
140k
    const uint8_t encdec_bypass = ctx->bypass_encdec &&
2535
140k
        (ctx->pd_pass == PD_PASS_1); // if enc dec is bypassed MD has to produce the final recon
2536
140k
    const uint8_t need_md_rec_for_intra_pred = !ctx->skip_intra ||
2537
0
        ctx->inter_intra_comp_ctrls.enabled; // for intra prediction of current frame
2538
140k
    const uint8_t need_md_rec_for_ref = (pcs->is_ref || pcs->scs->static_config.recon_enabled) &&
2539
0
        encdec_bypass; // for inter prediction of future frame or if recon is being output
2540
140k
    const uint8_t need_md_rec_for_dlf_search  = pcs->dlf_ctrls.enabled; // for DLF levels
2541
140k
    const uint8_t need_md_rec_for_cdef_search = pcs->cdef_search_ctrls.enabled &&
2542
140k
        !pcs->cdef_search_ctrls.use_qp_strength &&
2543
0
        !pcs->cdef_search_ctrls.use_reference_cdef_fs; // CDEF search levels needing the recon samples
2544
140k
    const uint8_t need_md_rec_for_restoration_search = pcs->enable_restoration; // any resoration search level
2545
140k
    const uint8_t need_md_rec_for_quality            = (pcs->compute_psnr || pcs->compute_ssim) &&
2546
0
        (ctx->pd_pass == PD_PASS_1); // stat report needs recon samples for metrics
2547
140k
    uint8_t do_recon;
2548
140k
    if (need_md_rec_for_intra_pred || need_md_rec_for_ref || need_md_rec_for_dlf_search ||
2549
140k
        need_md_rec_for_cdef_search || need_md_rec_for_restoration_search || need_md_rec_for_quality) {
2550
140k
        do_recon = 1;
2551
140k
    } else {
2552
111
        do_recon = 0;
2553
111
    }
2554
2555
140k
    return do_recon;
2556
140k
}