Coverage Report

Created: 2026-08-31 06:22

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