Coverage Report

Created: 2026-07-30 06:27

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