Coverage Report

Created: 2026-01-16 07:48

next uncovered line (L), next uncovered region (R), next uncovered branch (B)
/src/ffmpeg/libavcodec/vvc/cabac.c
Line
Count
Source
1
/*
2
 * VVC CABAC decoder
3
 *
4
 * Copyright (C) 2021 Nuo Mi
5
 *
6
 * This file is part of FFmpeg.
7
 *
8
 * FFmpeg is free software; you can redistribute it and/or
9
 * modify it under the terms of the GNU Lesser General Public
10
 * License as published by the Free Software Foundation; either
11
 * version 2.1 of the License, or (at your option) any later version.
12
 *
13
 * FFmpeg is distributed in the hope that it will be useful,
14
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
15
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
16
 * Lesser General Public License for more details.
17
 *
18
 * You should have received a copy of the GNU Lesser General Public
19
 * License along with FFmpeg; if not, write to the Free Software
20
 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
21
 */
22
#include "libavcodec/cabac_functions.h"
23
24
#include "cabac.h"
25
#include "ctu.h"
26
#include "data.h"
27
28
#define CABAC_MAX_BIN 31
29
30
#define CNU 35
31
32
enum SyntaxElement {
33
    ALF_CTB_FLAG                    =                                 0,
34
    ALF_USE_APS_FLAG                = ALF_CTB_FLAG                  + 9,
35
    ALF_CTB_CC_CB_IDC,
36
    ALF_CTB_CC_CR_IDC               = ALF_CTB_CC_CB_IDC             + 3,
37
    ALF_CTB_FILTER_ALT_IDX          = ALF_CTB_CC_CR_IDC             + 3,
38
    SAO_MERGE_FLAG                  = ALF_CTB_FILTER_ALT_IDX        + 2,
39
    SAO_TYPE_IDX,
40
    SPLIT_CU_FLAG,
41
    SPLIT_QT_FLAG                   = SPLIT_CU_FLAG                 + 9,
42
    MTT_SPLIT_CU_VERTICAL_FLAG      = SPLIT_QT_FLAG                 + 6,
43
    MTT_SPLIT_CU_BINARY_FLAG        = MTT_SPLIT_CU_VERTICAL_FLAG    + 5,
44
    NON_INTER_FLAG                  = MTT_SPLIT_CU_BINARY_FLAG      + 4,
45
    CU_SKIP_FLAG                    = NON_INTER_FLAG                + 2,
46
    PRED_MODE_IBC_FLAG              = CU_SKIP_FLAG                  + 3,
47
    PRED_MODE_FLAG                  = PRED_MODE_IBC_FLAG            + 3,
48
    PRED_MODE_PLT_FLAG              = PRED_MODE_FLAG                + 2,
49
    CU_ACT_ENABLED_FLAG,
50
    INTRA_BDPCM_LUMA_FLAG,
51
    INTRA_BDPCM_LUMA_DIR_FLAG,
52
    INTRA_MIP_FLAG,
53
    INTRA_LUMA_REF_IDX              = INTRA_MIP_FLAG                + 4,
54
    INTRA_SUBPARTITIONS_MODE_FLAG   = INTRA_LUMA_REF_IDX            + 2,
55
    INTRA_SUBPARTITIONS_SPLIT_FLAG,
56
    INTRA_LUMA_MPM_FLAG,
57
    INTRA_LUMA_NOT_PLANAR_FLAG,
58
    INTRA_BDPCM_CHROMA_FLAG         = INTRA_LUMA_NOT_PLANAR_FLAG    + 2,
59
    INTRA_BDPCM_CHROMA_DIR_FLAG,
60
    CCLM_MODE_FLAG,
61
    CCLM_MODE_IDX,
62
    INTRA_CHROMA_PRED_MODE,
63
    GENERAL_MERGE_FLAG,
64
    INTER_PRED_IDC,
65
    INTER_AFFINE_FLAG               = INTER_PRED_IDC                + 6,
66
    CU_AFFINE_TYPE_FLAG             = INTER_AFFINE_FLAG             + 3,
67
    SYM_MVD_FLAG,
68
    REF_IDX_LX,
69
    MVP_LX_FLAG                     = REF_IDX_LX                    + 2,
70
    AMVR_FLAG,
71
    AMVR_PRECISION_IDX              = AMVR_FLAG                     + 2,
72
    BCW_IDX                         = AMVR_PRECISION_IDX            + 3,
73
    CU_CODED_FLAG,
74
    CU_SBT_FLAG,
75
    CU_SBT_QUAD_FLAG                = CU_SBT_FLAG                   + 2,
76
    CU_SBT_HORIZONTAL_FLAG,
77
    CU_SBT_POS_FLAG                 = CU_SBT_HORIZONTAL_FLAG        + 3,
78
    LFNST_IDX,
79
    MTS_IDX                         = LFNST_IDX                     + 3,
80
    COPY_ABOVE_PALETTE_INDICES_FLAG = MTS_IDX                       + 4,
81
    PALETTE_TRANSPOSE_FLAG,
82
    RUN_COPY_FLAG,
83
    REGULAR_MERGE_FLAG              = RUN_COPY_FLAG                 + 8,
84
    MMVD_MERGE_FLAG                 = REGULAR_MERGE_FLAG            + 2,
85
    MMVD_CAND_FLAG,
86
    MMVD_DISTANCE_IDX,
87
    CIIP_FLAG,
88
    MERGE_SUBBLOCK_FLAG,
89
    MERGE_SUBBLOCK_IDX              = MERGE_SUBBLOCK_FLAG           + 3,
90
    MERGE_IDX,
91
    ABS_MVD_GREATER0_FLAG,
92
    ABS_MVD_GREATER1_FLAG,
93
    TU_Y_CODED_FLAG,
94
    TU_CB_CODED_FLAG                = TU_Y_CODED_FLAG               + 4,
95
    TU_CR_CODED_FLAG                = TU_CB_CODED_FLAG              + 2,
96
    CU_QP_DELTA_ABS                 = TU_CR_CODED_FLAG              + 3,
97
    CU_CHROMA_QP_OFFSET_FLAG        = CU_QP_DELTA_ABS               + 2,
98
    CU_CHROMA_QP_OFFSET_IDX,
99
    TRANSFORM_SKIP_FLAG,
100
    TU_JOINT_CBCR_RESIDUAL_FLAG     = TRANSFORM_SKIP_FLAG           + 2,
101
    LAST_SIG_COEFF_X_PREFIX         = TU_JOINT_CBCR_RESIDUAL_FLAG   + 3,
102
    LAST_SIG_COEFF_Y_PREFIX         = LAST_SIG_COEFF_X_PREFIX       +23,
103
    SB_CODED_FLAG                   = LAST_SIG_COEFF_Y_PREFIX       +23,
104
    SIG_COEFF_FLAG                  = SB_CODED_FLAG                 + 7,
105
    PAR_LEVEL_FLAG                  = SIG_COEFF_FLAG                +63,
106
    ABS_LEVEL_GTX_FLAG              = PAR_LEVEL_FLAG                +33,
107
    COEFF_SIGN_FLAG                 = ABS_LEVEL_GTX_FLAG            +72,
108
    SYNTAX_ELEMENT_LAST             = COEFF_SIGN_FLAG               + 6,
109
};
110
111
static const uint8_t init_values[4][SYNTAX_ELEMENT_LAST] = {
112
    {
113
        //alf_ctb_flag
114
        62,  39,  39,  54,  39,  39,  31,  39,  39,
115
        //alf_use_aps_flag
116
        46,
117
        //alf_ctb_cc_cb_idc
118
        18,  30,  31,
119
        //alf_ctb_cc_cr_idc
120
        18,  30,  31,
121
        //alf_ctb_filter_alt_idx
122
        11,  11,
123
        //sao_merge_left_flag and sao_merge_up_flag
124
        60,
125
        //sao_type_idx_luma and sao_type_idx_chroma
126
        13,
127
        //split_cu_flag
128
        19,  28,  38,  27,  29,  38,  20,  30,  31,
129
        //split_qt_flag
130
        27,   6,  15,  25,  19,  37,
131
        //mtt_split_cu_vertical_flag
132
        43,  42,  29,  27,  44,
133
        //mtt_split_cu_binary_flag
134
        36,  45,  36,  45,
135
        //non_inter_flag
136
        CNU, CNU,
137
        //cu_skip_flag
138
        0,  26,  28,
139
        //pred_mode_ibc_flag
140
        17,  42,  36,
141
        //pred_mode_flag
142
        CNU, CNU,
143
        //pred_mode_plt_flag
144
        25,
145
        //cu_act_enabled_flag
146
        52,
147
        //intra_bdpcm_luma_flag
148
        19,
149
        //intra_bdpcm_luma_dir_flag
150
        35,
151
        //intra_mip_flag
152
        33,  49,  50,  25,
153
        //intra_luma_ref_idx
154
        25,  60,
155
        //intra_subpartitions_mode_flag
156
        33,
157
        //intra_subpartitions_split_flag
158
        43,
159
        //intra_luma_mpm_flag
160
        45,
161
        //intra_luma_not_planar_flag
162
        13,  28,
163
        //intra_bdpcm_chroma_flag
164
         1,
165
        //intra_bdpcm_chroma_dir_flag
166
        27,
167
        //cclm_mode_flag
168
        59,
169
        //cclm_mode_idx
170
        27,
171
        //intra_chroma_pred_mode
172
        34,
173
        //general_merge_flag
174
        26,
175
        //inter_pred_idc
176
        CNU, CNU, CNU, CNU, CNU, CNU,
177
        //inter_affine_flag
178
        CNU, CNU, CNU,
179
        //cu_affine_type_flag
180
        CNU,
181
        //sym_mvd_flag
182
        CNU,
183
        //ref_idx_l0 and ref_idx_l1
184
        CNU, CNU,
185
        //mvp_l0_flag and mvp_l1_flag
186
        42,
187
        //amvr_flag
188
        CNU, CNU,
189
        //amvr_precision_idx
190
        35,  34,  35,
191
        //bcw_idx
192
        CNU,
193
        //cu_coded_flag
194
         6,
195
        //cu_sbt_flag
196
        CNU, CNU,
197
        //cu_sbt_quad_flag
198
        CNU,
199
        //cu_sbt_horizontal_flag
200
        CNU, CNU, CNU,
201
        //cu_sbt_pos_flag
202
        CNU,
203
        //lfnst_idx
204
        28,  52,  42,
205
        //mts_idx
206
        29,   0,  28,   0,
207
        //copy_above_palette_indices_flag
208
        42,
209
        //palette_transpose_flag
210
        42,
211
        //run_copy_flag
212
        50,  37,  45,  30,  46,  45,  38,  46,
213
        //regular_merge_flag
214
        CNU, CNU,
215
        //mmvd_merge_flag
216
        CNU,
217
        //mmvd_cand_flag
218
        CNU,
219
        //mmvd_distance_idx
220
        CNU,
221
        //ciip_flag
222
        CNU,
223
        //merge_subblock_flag
224
        CNU, CNU, CNU,
225
        //merge_subblock_idx
226
        CNU,
227
        //merge_idx, merge_gpm_idx0, and merge_gpm_idx1
228
        34,
229
        //abs_mvd_greater0_flag
230
        14,
231
        //abs_mvd_greater1_flag
232
        45,
233
        //tu_y_coded_flag
234
        15,  12,   5,   7,
235
        //tu_cb_coded_flag
236
        12,  21,
237
        //tu_cr_coded_flag
238
        33,  28,  36,
239
        //cu_qp_delta_abs
240
        CNU, CNU,
241
        //cu_chroma_qp_offset_flag
242
        CNU,
243
        //cu_chroma_qp_offset_idx
244
        CNU,
245
        //transform_skip_flag
246
        25,   9,
247
        //tu_joint_cbcr_residual_flag
248
        12,  21,  35,
249
        //last_sig_coeff_x_prefix
250
        13,   5,   4,  21,  14,   4,   6,  14,  21,  11,  14,   7,  14,   5,  11,  21,
251
        30,  22,  13,  42,  12,   4,   3,
252
        //last_sig_coeff_y_prefix
253
        13,   5,   4,   6,  13,  11,  14,   6,   5,   3,  14,  22,   6,   4,   3,   6,
254
        22,  29,  20,  34,  12,   4,   3,
255
        //sb_coded_flag
256
        18,  31,  25,  15,  18,  20,  38,
257
        //sig_coeff_flag
258
        25,  19,  28,  14,  25,  20,  29,  30,  19,  37,  30,  38,  11,  38,  46,  54,
259
        27,  39,  39,  39,  44,  39,  39,  39,  18,  39,  39,  39,  27,  39,  39,  39,
260
         0,  39,  39,  39,  25,  27,  28,  37,  34,  53,  53,  46,  19,  46,  38,  39,
261
        52,  39,  39,  39,  11,  39,  39,  39,  19,  39,  39,  39,  25,  28,  38,
262
        //par_level_flag
263
        33,  25,  18,  26,  34,  27,  25,  26,  19,  42,  35,  33,  19,  27,  35,  35,
264
        34,  42,  20,  43,  20,  33,  25,  26,  42,  19,  27,  26,  50,  35,  20,  43,
265
        11,
266
        //abs_level_gtx_flag
267
        25,  25,  11,  27,  20,  21,  33,  12,  28,  21,  22,  34,  28,  29,  29,  30,
268
        36,  29,  45,  30,  23,  40,  33,  27,  28,  21,  37,  36,  37,  45,  38,  46,
269
        25,   1,  40,  25,  33,  11,  17,  25,  25,  18,   4,  17,  33,  26,  19,  13,
270
        33,  19,  20,  28,  22,  40,   9,  25,  18,  26,  35,  25,  26,  35,  28,  37,
271
        11,   5,   5,  14,  10,   3,   3,   3,
272
        //coeff_sign_flag
273
        12,  17,  46,  28,  25,  46,
274
    },
275
    {
276
        //alf_ctb_flag
277
        13,  23,  46,   4,  61,  54,  19,  46,  54,
278
        //alf_use_aps_flag
279
        46,
280
        //alf_ctb_cc_cb_idc
281
        18,  21,  38,
282
        //alf_ctb_cc_cr_idc
283
        18,  21,  38,
284
        //alf_ctb_filter_alt_idx
285
        20,  12,
286
        //sao_merge_left_flag and sao_merge_up_flag
287
        60,
288
        //sao_type_idx_luma and sao_type_idx_chroma
289
        5,
290
        //split_cu_flag
291
        11,  35,  53,  12,   6,  30,  13,  15,  31,
292
        //split_qt_flag
293
        20,  14,  23,  18,  19,   6,
294
        //mtt_split_cu_vertical_flag
295
        43,  35,  37,  34,  52,
296
        //mtt_split_cu_binary_flag
297
        43,  37,  21,  22,
298
        //non_inter_flag
299
        25,  12,
300
        //cu_skip_flag
301
        57,  59,  45,
302
        //pred_mode_ibc_flag
303
         0,  57,  44,
304
        //pred_mode_flag
305
        40,  35,
306
        //pred_mode_plt_flag
307
        0,
308
        //cu_act_enabled_flag
309
        46,
310
        //intra_bdpcm_luma_flag
311
        40,
312
        //intra_bdpcm_luma_dir_flag
313
        36,
314
        //intra_mip_flag
315
        41,  57,  58,  26,
316
        //intra_luma_ref_idx
317
        25,  58,
318
        //intra_subpartitions_mode_flag
319
        33,
320
        //intra_subpartitions_split_flag
321
        36,
322
        //intra_luma_mpm_flag
323
        36,
324
        //intra_luma_not_planar_flag
325
        12,  20,
326
        //intra_bdpcm_chroma_flag
327
         0,
328
        //intra_bdpcm_chroma_dir_flag
329
        13,
330
        //cclm_mode_flag
331
        34,
332
        //cclm_mode_idx
333
        27,
334
        //intra_chroma_pred_mode
335
        25,
336
        //general_merge_flag
337
        21,
338
        //inter_pred_idc
339
         7,   6,   5,  12,   4,  40,
340
        //inter_affine_flag
341
        12,  13,  14,
342
        //cu_affine_type_flag
343
        35,
344
        //sym_mvd_flag
345
        28,
346
        //ref_idx_l0 and ref_idx_l1
347
        20,  35,
348
        //mvp_l0_flag and mvp_l1_flag
349
        34,
350
        //amvr_flag
351
        59,  58,
352
        //amvr_precision_idx
353
        60,  48,  60,
354
        //bcw_idx
355
         4,
356
        //cu_coded_flag
357
         5,
358
        //cu_sbt_flag
359
        56,  57,
360
        //cu_sbt_quad_flag
361
        42,
362
        //cu_sbt_horizontal_flag
363
        20,  43,  12,
364
        //cu_sbt_pos_flag
365
        28,
366
        //lfnst_idx
367
        37,  45,  27,
368
        //mts_idx
369
        45,  40,  27,   0,
370
        //copy_above_palette_indices_flag
371
        59,
372
        //palette_transpose_flag
373
        42,
374
        //run_copy_flag
375
        51,  30,  30,  38,  23,  38,  53,  46,
376
        //regular_merge_flag
377
        38,   7,
378
        //mmvd_merge_flag
379
        26,
380
        //mmvd_cand_flag
381
        43,
382
        //mmvd_distance_idx
383
        60,
384
        //ciip_flag
385
        57,
386
        //merge_subblock_flag
387
        48,  57,  44,
388
        //merge_subblock_idx
389
         5,
390
        //merge_idx, merge_gpm_idx0, and merge_gpm_idx1
391
        20,
392
        //abs_mvd_greater0_flag
393
        44,
394
        //abs_mvd_greater1_flag
395
        43,
396
        //tu_y_coded_flag
397
        23,   5,  20,   7,
398
        //tu_cb_coded_flag
399
        25,  28,
400
        //tu_cr_coded_flag
401
        25,  29,  45,
402
        //cu_qp_delta_abs
403
        CNU, CNU,
404
        //cu_chroma_qp_offset_flag
405
        CNU,
406
        //cu_chroma_qp_offset_idx
407
        CNU,
408
        //transform_skip_flag
409
        25,   9,
410
        //tu_joint_cbcr_residual_flag
411
        27,  36,  45,
412
        //last_sig_coeff_x_prefix
413
         6,  13,  12,   6,   6,  12,  14,  14,  13,  12,  29,   7,   6,  13,  36,  28,
414
        14,  13,   5,  26,  12,   4,  18,
415
        //last_sig_coeff_y_prefix
416
         5,   5,  12,   6,   6,   4,   6,  14,   5,  12,  14,   7,  13,   5,  13,  21,
417
        14,  20,  12,  34,  11,   4,  18,
418
        //sb_coded_flag
419
        25,  30,  25,  45,  18,  12,  29,
420
        //sig_coeff_flag
421
        17,  41,  42,  29,  25,  49,  43,  37,  33,  58,  51,  30,  19,  38,  38,  46,
422
        34,  54,  54,  39,   6,  39,  39,  39,  19,  39,  54,  39,  19,  39,  39,  39,
423
        56,  39,  39,  39,  17,  34,  35,  21,  41,  59,  60,  38,  35,  45,  53,  54,
424
        44,  39,  39,  39,  34,  38,  62,  39,  26,  39,  39,  39,  40,  35,  44,
425
        //par_level_flag
426
        18,  17,  33,  18,  26,  42,  25,  33,  26,  42,  27,  25,  34,  42,  42,  35,
427
        26,  27,  42,  20,  20,  25,  25,  26,  11,  19,  27,  33,  42,  35,  35,  43,
428
         3,
429
        //abs_level_gtx_flag
430
         0,  17,  26,  19,  35,  21,  25,  34,  20,  28,  29,  33,  27,  28,  29,  22,
431
        34,  28,  44,  37,  38,   0,  25,  19,  20,  13,  14,  57,  44,  30,  30,  23,
432
        17,   0,   1,  17,  25,  18,   0,   9,  25,  33,  34,   9,  25,  18,  26,  20,
433
        25,  18,  19,  27,  29,  17,   9,  25,  10,  18,   4,  17,  33,  19,  20,  29,
434
        18,  11,   4,  28,   2,  10,   3,   3,
435
        //coeff_sign_flag
436
         5,  10,  53,  43,  25,  46,
437
    },
438
    {
439
        //alf_ctb_flag
440
        33,  52,  46,  25,  61,  54,  25,  61,  54,
441
        //alf_use_aps_flag
442
        46,
443
        //alf_ctb_cc_cb_idc
444
        25,  35,  38,
445
        //alf_ctb_cc_cr_idc
446
        25,  28,  38,
447
        //alf_ctb_filter_alt_idx
448
        11,  26,
449
        //sao_merge_left_flag and sao_merge_up_flag
450
        2,
451
        //sao_type_idx_luma and sao_type_idx_chroma
452
        2,
453
        //split_cu_flag
454
        18,  27,  15,  18,  28,  45,  26,   7,  23,
455
        //split_qt_flag
456
        26,  36,  38,  18,  34,  21,
457
        //mtt_split_cu_vertical_flag
458
        43,  42,  37,  42,  44,
459
        //mtt_split_cu_binary_flag
460
        28,  29,  28,  29,
461
        //non_inter_flag
462
        25,  20,
463
        //cu_skip_flag
464
        57,  60,  46,
465
        //pred_mode_ibc_flag
466
         0,  43,  45,
467
        //pred_mode_flag
468
        40,  35,
469
        //pred_mode_plt_flag
470
        17,
471
        //cu_act_enabled_flag
472
        46,
473
        //intra_bdpcm_luma_flag
474
        19,
475
        //intra_bdpcm_luma_dir_flag
476
        21,
477
        //intra_mip_flag
478
        56,  57,  50,  26,
479
        //intra_luma_ref_idx
480
        25,  59,
481
        //intra_subpartitions_mode_flag
482
        33,
483
        //intra_subpartitions_split_flag
484
        43,
485
        //intra_luma_mpm_flag
486
        44,
487
        //intra_luma_not_planar_flag
488
        13,   6,
489
        //intra_bdpcm_chroma_flag
490
         0,
491
        //intra_bdpcm_chroma_dir_flag
492
        28,
493
        //cclm_mode_flag
494
        26,
495
        //cclm_mode_idx
496
        27,
497
        //intra_chroma_pred_mode
498
        25,
499
        //general_merge_flag
500
         6,
501
        //inter_pred_idc
502
        14,  13,   5,   4,   3,  40,
503
        //inter_affine_flag
504
        19,  13,   6,
505
        //cu_affine_type_flag
506
        35,
507
        //sym_mvd_flag
508
        28,
509
        //ref_idx_l0 and ref_idx_l1
510
         5,  35,
511
        //mvp_l0_flag and mvp_l1_flag
512
        34,
513
        //amvr_flag
514
        59,  50,
515
        //amvr_precision_idx
516
        38,  26,  60,
517
        //bcw_idx
518
         5,
519
        //cu_coded_flag
520
        12,
521
        //cu_sbt_flag
522
        41,  57,
523
        //cu_sbt_quad_flag
524
        42,
525
        //cu_sbt_horizontal_flag
526
        35,  51,  27,
527
        //cu_sbt_pos_flag
528
        28,
529
        //lfnst_idx
530
        52,  37,  27,
531
        //mts_idx
532
        45,  25,  27,   0,
533
        //copy_above_palette_indices_flag
534
        50,
535
        //palette_transpose_flag
536
        35,
537
        //run_copy_flag
538
        58,  45,  45,  30,  38,  45,  38,  46,
539
        //regular_merge_flag
540
        46,  15,
541
        //mmvd_merge_flag
542
        25,
543
        //mmvd_cand_flag
544
        43,
545
        //mmvd_distance_idx
546
        59,
547
        //ciip_flag
548
        57,
549
        //merge_subblock_flag
550
        25,  58,  45,
551
        //merge_subblock_idx
552
         4,
553
        //merge_idx, merge_gpm_idx0, and merge_gpm_idx1
554
        18,
555
        //abs_mvd_greater0_flag
556
        51,
557
        //abs_mvd_greater1_flag
558
        36,
559
        //tu_y_coded_flag
560
        15,   6,   5,  14,
561
        //tu_cb_coded_flag
562
        25,  37,
563
        //tu_cr_coded_flag
564
         9,  36,  45,
565
        //cu_qp_delta_abs
566
        CNU, CNU,
567
        //cu_chroma_qp_offset_flag
568
        CNU,
569
        //cu_chroma_qp_offset_idx
570
        CNU,
571
        //transform_skip_flag
572
        25,  17,
573
        //tu_joint_cbcr_residual_flag
574
        42,  43,  52,
575
        //last_sig_coeff_x_prefix
576
         6,   6,  12,  14,   6,   4,  14,   7,   6,   4,  29,   7,   6,   6,  12,  28,
577
         7,  13,  13,  35,  19,   5,   4,
578
        //last_sig_coeff_y_prefix
579
         5,   5,  20,  13,  13,  19,  21,   6,  12,  12,  14,  14,   5,   4,  12,  13,
580
         7,  13,  12,  41,  11,   5,  27,
581
        //sb_coded_flag
582
        25,  45,  25,  14,  18,  35,  45,
583
        //sig_coeff_flag
584
        17,  41,  49,  36,   1,  49,  50,  37,  48,  51,  58,  45,  26,  45,  53,  46,
585
        49,  54,  61,  39,  35,  39,  39,  39,  19,  54,  39,  39,  50,  39,  39,  39,
586
         0,  39,  39,  39,   9,  49,  50,  36,  48,  59,  59,  38,  34,  45,  38,  31,
587
        58,  39,  39,  39,  34,  38,  54,  39,  41,  39,  39,  39,  25,  50,  37,
588
        //par_level_flag
589
        33,  40,  25,  41,  26,  42,  25,  33,  26,  34,  27,  25,  41,  42,  42,  35,
590
        33,  27,  35,  42,  43,  33,  25,  26,  34,  19,  27,  33,  42,  43,  35,  43,
591
        11,
592
        //abs_level_gtx_flag
593
         0,   0,  33,  34,  35,  21,  25,  34,  35,  28,  29,  40,  42,  43,  29,  30,
594
        49,  36,  37,  45,  38,   0,  40,  34,  43,  36,  37,  57,  52,  45,  38,  46,
595
        25,   0,   0,  17,  25,  26,   0,   9,  25,  33,  19,   0,  25,  33,  26,  20,
596
        25,  33,  27,  35,  22,  25,   1,  25,  33,  26,  12,  25,  33,  27,  28,  37,
597
        19,  11,   4,   6,   3,   4,   4,   5,
598
        //coeff_sign_flag
599
        35,  25,  46,  28,  33,  38,
600
    },
601
    //shiftIdx
602
    {
603
        //alf_ctb_flag
604
         0,   0,   0,   4,   0,   0,   1,   0,   0,
605
        //alf_use_aps_flag
606
         0,
607
        //alf_ctb_cc_cb_idc
608
         4,   1,   4,
609
        //alf_ctb_cc_cr_idc
610
         4,   1,   4,
611
        //alf_ctb_filter_alt_idx
612
         0,   0,
613
        //sao_merge_left_flag and sao_merge_up_flag
614
         0,
615
        //sao_type_idx_luma and sao_type_idx_chroma
616
         4,
617
        //split_cu_flag
618
        12,  13,   8,   8,  13,  12,   5,   9,   9,
619
        //split_qt_flag
620
         0,   8,   8,  12,  12,   8,
621
        //mtt_split_cu_vertical_flag
622
         9,   8,   9,   8,   5,
623
        //mtt_split_cu_binary_flag
624
        12,  13,  12,  13,
625
        //non_inter_flag
626
         1,   0,
627
        //cu_skip_flag
628
         5,   4,   8,
629
        //pred_mode_ibc_flag
630
         1,   5,   8,
631
        //pred_mode_flag
632
         5,   1,
633
        //pred_mode_plt_flag
634
         1,
635
        //cu_act_enabled_flag
636
         1,
637
        //intra_bdpcm_luma_flag
638
         1,
639
        //intra_bdpcm_luma_dir_flag
640
         4,
641
        //intra_mip_flag
642
         9,  10,   9,   6,
643
        //intra_luma_ref_idx
644
         5,   8,
645
        //intra_subpartitions_mode_flag
646
         9,
647
        //intra_subpartitions_split_flag
648
         2,
649
        //intra_luma_mpm_flag
650
         6,
651
        //intra_luma_not_planar_flag
652
         1,   5,
653
        //intra_bdpcm_chroma_flag
654
         1,
655
        //intra_bdpcm_chroma_dir_flag
656
         0,
657
        //cclm_mode_flag
658
         4,
659
        //cclm_mode_idx
660
         9,
661
        //intra_chroma_pred_mode
662
         5,
663
        //general_merge_flag
664
         4,
665
        //inter_pred_idc
666
         0,   0,   1,   4,   4,   0,
667
        //inter_affine_flag
668
         4,   0,   0,
669
        //cu_affine_type_flag
670
         4,
671
        //sym_mvd_flag
672
         5,
673
        //ref_idx_l0 and ref_idx_l1
674
         0,   4,
675
        //mvp_l0_flag and mvp_l1_flag
676
        12,
677
        //amvr_flag
678
         0,   0,
679
        //amvr_precision_idx
680
         4,   5,  0,
681
        //bcw_idx
682
         1,
683
        //cu_coded_flag
684
         4,
685
        //cu_sbt_flag
686
         1,   5,
687
        //cu_sbt_quad_flag
688
        10,
689
        //cu_sbt_horizontal_flag
690
         8,   4,   1,
691
        //cu_sbt_pos_flag
692
        13,
693
        //lfnst_idx
694
         9,   9,  10,
695
        //mts_idx
696
         8,   0,   9,   0,
697
        //copy_above_palette_indices_flag
698
         9,
699
        //palette_transpose_flag
700
         5,
701
        //run_copy_flag
702
         9,   6,   9,  10,   5,   0,   9,   5,
703
        //regular_merge_flag
704
         5,   5,
705
        //mmvd_merge_flag
706
         4,
707
        //mmvd_cand_flag
708
        10,
709
        //mmvd_distance_idx
710
         0,
711
        //ciip_flag
712
         1,
713
        //merge_subblock_flag
714
         4,   4,   4,
715
        //merge_subblock_idx
716
         0,
717
        //merge_idx, merge_gpm_idx0, and merge_gpm_idx1
718
         4,
719
        //abs_mvd_greater0_flag
720
         9,
721
        //abs_mvd_greater1_flag
722
         5,
723
        //tu_y_coded_flag
724
         5,   1,   8,   9,
725
        //tu_cb_coded_flag
726
         5,   0,
727
        //tu_cr_coded_flag
728
         2,   1,   0,
729
        //cu_qp_delta_abs
730
         8,   8,
731
        //cu_chroma_qp_offset_flag
732
         8,
733
        //cu_chroma_qp_offset_idx
734
         8,
735
        //transform_skip_flag
736
         1,   1,
737
        //tu_joint_cbcr_residual_flag
738
         1,   1,   0,
739
        //last_sig_coeff_x_prefix
740
         8,   5,   4,   5,   4,   4,   5,   4,   1,   0,   4,   1,   0,   0,   0,   0,
741
         1,   0,   0,   0,   5,   4,   4,
742
        //last_sig_coeff_y_prefix
743
         8,   5,   8,   5,   5,   4,   5,   5,   4,   0,   5,   4,   1,   0,   0,   1,
744
         4,   0,   0,   0,   6,   5,   5,
745
        //sb_coded_flag
746
         8,   5,   5,   8,   5,   8,   8,
747
        //sig_coeff_flag
748
        12,   9,   9,  10,   9,   9,   9,  10,   8,   8,   8,  10,   9,  13,   8,   8,
749
         8,   8,   8,   5,   8,   0,   0,   0,   8,   8,   8,   8,   8,   0,   4,   4,
750
         0,   0,   0,   0,  12,  12,   9,  13,   4,   5,   8,   9,   8,  12,  12,   8,
751
         4,   0,   0,   0,   8,   8,   8,   8,   4,   0,   0,   0,  13,  13,   8,
752
        //par_level_flag
753
         8,   9,  12,  13,  13,  13,  10,  13,  13,  13,  13,  13,  13,  13,  13,  13,
754
        10,  13,  13,  13,  13,   8,  12,  12,  12,  13,  13,  13,  13,  13,  13,  13,
755
         6,
756
        //abs_level_gtx_flag
757
         9,   5,  10,  13,  13,  10,   9,  10,  13,  13,  13,   9,  10,  10,  10,  13,
758
         8,   9,  10,  10,  13,   8,   8,   9,  12,  12,  10,   5,   9,   9,   9,  13,
759
         1,   5,   9,   9,   9,   6,   5,   9,  10,  10,   9,   9,   9,   9,   9,   9,
760
         6,   8,   9,   9,  10,   1,   5,   8,   8,   9,   6,   6,   9,   8,   8,   9,
761
         4,   2,   1,   6,   1,   1,   1,   1,
762
        //coeff_sign_flag
763
         1,   4,   4,   5,   8,   8,
764
    }
765
};
766
767
#define MAX_SUB_BLOCKS 16
768
#define MAX_SUB_BLOCK_SIZE 4
769
#define MAX_TB_SIZE 64
770
771
typedef struct ResidualCoding {
772
    //common for ts and non ts
773
    TransformBlock *tb;
774
775
    int log2_sb_w;
776
    int log2_sb_h;
777
    int last_sub_block;
778
    int hist_value;
779
    int update_hist;
780
    int num_sb_coeff;
781
    int rem_bins_pass1;
782
783
    int width_in_sbs;
784
    int height_in_sbs;
785
    int nb_sbs;
786
787
    const uint8_t *sb_scan_x_off;
788
    const uint8_t *sb_scan_y_off;
789
    const uint8_t *scan_x_off;
790
    const uint8_t *scan_y_off;
791
792
    uint8_t sb_coded_flag[MAX_SUB_BLOCKS * MAX_SUB_BLOCKS];
793
    int sig_coeff_flag[MAX_TB_SIZE * MAX_TB_SIZE];
794
    int abs_level_pass1[MAX_TB_SIZE * MAX_TB_SIZE];              ///< AbsLevelPass1[][]
795
    int abs_level[MAX_TB_SIZE * MAX_TB_SIZE];
796
797
    //for ts only
798
    uint8_t infer_sb_cbf;
799
    int coeff_sign_level[MAX_TB_SIZE * MAX_TB_SIZE];             ///< CoeffSignLevel[][]
800
801
    //for non ts only
802
    int qstate;
803
    int last_scan_pos;
804
    int last_significant_coeff_x;
805
    int last_significant_coeff_y;
806
} ResidualCoding;
807
808
static int cabac_reinit(VVCLocalContext *lc)
809
28.6k
{
810
28.6k
    return skip_bytes(&lc->ep->cc, 0) == NULL ? AVERROR_INVALIDDATA : 0;
811
28.6k
}
812
813
static void cabac_init_state(VVCLocalContext *lc)
814
1.83M
{
815
1.83M
    const VVCSPS *sps             = lc->fc->ps.sps;
816
1.83M
    const H266RawSliceHeader *rsh = lc->sc->sh.r;
817
1.83M
    const int qp                  = av_clip_uintp2(lc->sc->sh.slice_qp_y, 6);
818
1.83M
    int init_type                 = 2 - rsh->sh_slice_type;
819
820
1.83M
    av_assert0(VVC_CONTEXTS == SYNTAX_ELEMENT_LAST);
821
822
1.83M
    ff_vvc_ep_init_stat_coeff(lc->ep, sps->bit_depth, sps->r->sps_persistent_rice_adaptation_enabled_flag);
823
824
1.83M
    if (rsh->sh_cabac_init_flag && !IS_I(rsh))
825
91.0k
        init_type ^= 3;
826
827
693M
    for (int i = 0; i < VVC_CONTEXTS; i++) {
828
691M
        VVCCabacState *state = &lc->ep->cabac_state[i];
829
691M
        const int init_value = init_values[init_type][i];
830
691M
        const int shift_idx  = init_values[3][i];
831
691M
        const int m = (init_value >> 3) - 4;
832
691M
        const int n = ((init_value & 7) * 18) + 1;
833
691M
        const int pre = av_clip(((m * (qp - 16)) >> 1) + n, 1, 127);
834
835
691M
        state->state[0] = pre << 3;
836
691M
        state->state[1] = pre << 7;
837
691M
        state->shift[0] = (shift_idx >> 2 ) + 2;
838
691M
        state->shift[1] = (shift_idx & 3 ) + 3 + state->shift[0];
839
691M
    }
840
1.83M
}
841
842
int ff_vvc_cabac_init(VVCLocalContext *lc,
843
    const int ctu_idx, const int rx, const int ry)
844
1.86M
{
845
1.86M
    int ret = 0;
846
1.86M
    const VVCPPS *pps            = lc->fc->ps.pps;
847
1.86M
    const int first_ctb_in_slice = !ctu_idx;
848
1.86M
    const int first_ctb_in_tile  = rx == pps->ctb_to_col_bd[rx] && ry == pps->ctb_to_row_bd[ry];
849
850
1.86M
    if (first_ctb_in_slice|| first_ctb_in_tile) {
851
1.83M
        if (lc->sc->nb_eps == 1 && !first_ctb_in_slice)
852
0
            ret = cabac_reinit(lc);
853
1.83M
        if (!ret)
854
1.83M
            cabac_init_state(lc);
855
1.83M
    }
856
1.86M
    return ret;
857
1.86M
}
858
859
//fixme
860
7.51M
static void vvc_refill2(CABACContext* c) {
861
7.51M
    int i;
862
7.51M
    unsigned x;
863
7.51M
#if !HAVE_FAST_CLZ
864
7.51M
    x = c->low ^ (c->low - 1);
865
7.51M
    i = 7 - ff_h264_norm_shift[x >> (CABAC_BITS - 1)];
866
#else
867
    i = ff_ctz(c->low) - CABAC_BITS;
868
#endif
869
870
7.51M
    x = -CABAC_MASK;
871
872
7.51M
#if CABAC_BITS == 16
873
7.51M
    x += (c->bytestream[0] << 9) + (c->bytestream[1] << 1);
874
#else
875
    x += c->bytestream[0] << 1;
876
#endif
877
878
7.51M
    c->low += x << i;
879
7.51M
#if !UNCHECKED_BITSTREAM_READER
880
7.51M
    if (c->bytestream < c->bytestream_end)
881
6.27M
#endif
882
6.27M
        c->bytestream += CABAC_BITS / 8;
883
7.51M
}
884
885
static int inline vvc_get_cabac(CABACContext *c, VVCCabacState* base, const int ctx)
886
236M
{
887
236M
    VVCCabacState *s = base + ctx;
888
236M
    const int qRangeIdx = c->range >> 5;
889
236M
    const int pState = s->state[1] + (s->state[0] << 4);
890
236M
    const int valMps = pState >> 14;
891
236M
    const int RangeLPS = (qRangeIdx * ((valMps ? 32767 - pState : pState) >> 9 ) >> 1) + 4;
892
236M
    int bit, lps_mask;
893
894
236M
    c->range -= RangeLPS;
895
236M
    lps_mask = ((c->range<<(CABAC_BITS+1)) - c->low)>>31;
896
897
236M
    c->low -= (c->range<<(CABAC_BITS+1)) & lps_mask;
898
236M
    c->range += (RangeLPS - c->range) & lps_mask;
899
900
236M
    bit = valMps ^ (lps_mask & 1);
901
902
236M
    lps_mask = ff_h264_norm_shift[c->range];
903
236M
    c->range <<= lps_mask;
904
236M
    c->low  <<= lps_mask;
905
906
236M
    if (!(c->low & CABAC_MASK))
907
7.51M
        vvc_refill2(c);
908
236M
    s->state[0] = s->state[0] - (s->state[0] >> s->shift[0]) + (1023 * bit >> s->shift[0]);
909
236M
    s->state[1] = s->state[1] - (s->state[1] >> s->shift[1]) + (16383 * bit >> s->shift[1]);
910
236M
    return bit;
911
236M
}
912
913
236M
#define GET_CABAC(ctx) vvc_get_cabac(&lc->ep->cc, lc->ep->cabac_state, ctx)
914
915
//9.3.3.4 Truncated binary (TB) binarization process
916
static int truncated_binary_decode(VVCLocalContext *lc, const int c_max)
917
2.47M
{
918
2.47M
    const int n = c_max + 1;
919
2.47M
    const int k = av_log2(n);
920
2.47M
    const int u = (1 << (k+1)) - n;
921
2.47M
    int v = 0;
922
11.9M
    for (int i = 0; i < k; i++)
923
9.46M
        v = (v << 1) | get_cabac_bypass(&lc->ep->cc);
924
2.47M
    if (v >= u) {
925
1.31M
        v = (v << 1) | get_cabac_bypass(&lc->ep->cc);
926
1.31M
        v -= u;
927
1.31M
    }
928
2.47M
    return v;
929
2.47M
}
930
931
// 9.3.3.5 k-th order Exp - Golomb binarization process
932
static int kth_order_egk_decode(CABACContext *c, int k, const int max)
933
1.27M
{
934
1.27M
    int bit    = 1;
935
1.27M
    int value  = 0;
936
1.27M
    int symbol = 0;
937
938
3.40M
    while (bit) {
939
2.13M
        bit = get_cabac_bypass(c);
940
2.13M
        if (max - value < (bit << k))
941
6.00k
            return AVERROR_INVALIDDATA;
942
2.13M
        value += bit << k++;
943
2.13M
    }
944
945
1.26M
    if (--k) {
946
7.93M
        for (int i = 0; i < k; i++)
947
6.67M
            symbol = (symbol << 1) | get_cabac_bypass(c);
948
1.25M
        value += symbol;
949
1.25M
    }
950
951
1.26M
    if (value > max)
952
4.72k
        return AVERROR_INVALIDDATA;
953
954
1.26M
    return value;
955
1.26M
}
956
957
// 9.3.3.6 Limited k-th order Exp-Golomb binarization process
958
static int limited_kth_order_egk_decode(CABACContext *c, const int k, const int max_pre_ext_len, const int trunc_suffix_len)
959
811k
{
960
811k
    int pre_ext_len = 0;
961
811k
    int escape_length;
962
811k
    int val = 0;
963
2.37M
    while ((pre_ext_len < max_pre_ext_len) && get_cabac_bypass(c))
964
1.56M
        pre_ext_len++;
965
811k
    if (pre_ext_len == max_pre_ext_len)
966
101k
        escape_length = trunc_suffix_len;
967
710k
    else
968
710k
        escape_length = pre_ext_len + k;
969
3.87M
    while (escape_length-- > 0) {
970
3.06M
        val = (val << 1) + get_cabac_bypass(c);
971
3.06M
    }
972
811k
    val += ((1 << pre_ext_len) - 1) << k;
973
811k
    return val;
974
811k
}
975
976
// 9.3.3.7 Fixed-length binarization process
977
static int fixed_length_decode(CABACContext* c, const int len)
978
896k
{
979
896k
    int value = 0;
980
981
10.5M
    for (int i = 0; i < len; i++)
982
9.64M
        value = (value << 1) | get_cabac_bypass(c);
983
984
896k
    return value;
985
896k
}
986
987
static av_always_inline
988
void get_left_top(const VVCLocalContext *lc, uint8_t *left, uint8_t *top,
989
    const int x0, const int y0, const uint8_t *left_ctx, const uint8_t *top_ctx)
990
13.7M
{
991
13.7M
    const VVCFrameContext *fc = lc->fc;
992
13.7M
    const VVCSPS *sps         = fc->ps.sps;
993
13.7M
    const int min_cb_width    = fc->ps.pps->min_cb_width;
994
13.7M
    const int x0b = av_zero_extend(x0, sps->ctb_log2_size_y);
995
13.7M
    const int y0b = av_zero_extend(y0, sps->ctb_log2_size_y);
996
13.7M
    const int x_cb = x0 >> sps->min_cb_log2_size_y;
997
13.7M
    const int y_cb = y0 >> sps->min_cb_log2_size_y;
998
999
13.7M
    if (lc->ctb_left_flag || x0b)
1000
5.50M
        *left = SAMPLE_CTB(left_ctx, x_cb - 1, y_cb);
1001
13.7M
    if (lc->ctb_up_flag || y0b)
1002
6.17M
        *top = SAMPLE_CTB(top_ctx, x_cb, y_cb - 1);
1003
13.7M
}
1004
1005
static av_always_inline
1006
uint8_t get_inc(VVCLocalContext *lc, const uint8_t *ctx)
1007
3.83M
{
1008
3.83M
    uint8_t left = 0, top = 0;
1009
3.83M
    get_left_top(lc, &left, &top, lc->cu->x0, lc->cu->y0, ctx, ctx);
1010
3.83M
    return left + top;
1011
3.83M
}
1012
1013
int ff_vvc_sao_merge_flag_decode(VVCLocalContext *lc)
1014
32.4k
{
1015
32.4k
    return GET_CABAC(SAO_MERGE_FLAG);
1016
32.4k
}
1017
1018
int ff_vvc_sao_type_idx_decode(VVCLocalContext *lc)
1019
1.84M
{
1020
1.84M
    if (!GET_CABAC(SAO_TYPE_IDX))
1021
1.55M
        return SAO_NOT_APPLIED;
1022
1023
286k
    if (!get_cabac_bypass(&lc->ep->cc))
1024
93.2k
        return SAO_BAND;
1025
193k
    return SAO_EDGE;
1026
286k
}
1027
1028
int ff_vvc_sao_band_position_decode(VVCLocalContext *lc)
1029
110k
{
1030
110k
    return fixed_length_decode(&lc->ep->cc, 5);
1031
110k
}
1032
1033
int ff_vvc_sao_offset_abs_decode(VVCLocalContext *lc)
1034
1.87M
{
1035
1.87M
    int i = 0;
1036
1.87M
    const int length = (1 << (FFMIN(lc->fc->ps.sps->bit_depth, 10) - 5)) - 1;
1037
1038
3.79M
    while (i < length && get_cabac_bypass(&lc->ep->cc))
1039
1.92M
        i++;
1040
1.87M
    return i;
1041
1.87M
}
1042
1043
int ff_vvc_sao_offset_sign_decode(VVCLocalContext *lc)
1044
223k
{
1045
223k
    return get_cabac_bypass(&lc->ep->cc);
1046
223k
}
1047
1048
int ff_vvc_sao_eo_class_decode(VVCLocalContext *lc)
1049
193k
{
1050
193k
    return (get_cabac_bypass(&lc->ep->cc) << 1) | get_cabac_bypass(&lc->ep->cc);
1051
193k
}
1052
1053
int ff_vvc_alf_ctb_flag(VVCLocalContext *lc, const int rx, const int ry, const int c_idx)
1054
110k
{
1055
110k
    int inc = c_idx * 3;
1056
110k
    const VVCFrameContext *fc = lc->fc;
1057
110k
    if (lc->ctb_left_flag) {
1058
87.6k
        const ALFParams *left = &CTB(fc->tab.alf, rx - 1, ry);
1059
87.6k
        inc += left->ctb_flag[c_idx];
1060
87.6k
    }
1061
110k
    if (lc->ctb_up_flag) {
1062
83.5k
        const ALFParams *above = &CTB(fc->tab.alf, rx, ry - 1);
1063
83.5k
        inc += above->ctb_flag[c_idx];
1064
83.5k
    }
1065
110k
    return GET_CABAC(ALF_CTB_FLAG + inc);
1066
110k
}
1067
1068
int ff_vvc_alf_use_aps_flag(VVCLocalContext *lc)
1069
31.3k
{
1070
31.3k
    return GET_CABAC(ALF_USE_APS_FLAG);
1071
31.3k
}
1072
1073
int ff_vvc_alf_luma_prev_filter_idx(VVCLocalContext *lc)
1074
0
{
1075
0
    return truncated_binary_decode(lc, lc->sc->sh.r->sh_num_alf_aps_ids_luma - 1);
1076
0
}
1077
1078
int ff_vvc_alf_luma_fixed_filter_idx(VVCLocalContext *lc)
1079
16.3k
{
1080
16.3k
    return truncated_binary_decode(lc, 15);
1081
16.3k
}
1082
1083
int ff_vvc_alf_ctb_filter_alt_idx(VVCLocalContext *lc, const int c_idx, const int num_chroma_filters)
1084
0
{
1085
0
    int i = 0;
1086
0
    const int length = num_chroma_filters - 1;
1087
1088
0
    while (i < length && GET_CABAC(ALF_CTB_FILTER_ALT_IDX + c_idx - 1))
1089
0
        i++;
1090
0
    return i;
1091
0
}
1092
1093
int ff_vvc_alf_ctb_cc_idc(VVCLocalContext *lc, const int rx, const int ry, const int idx, const int cc_filters_signalled)
1094
31.3k
{
1095
31.3k
    int inc = !idx ? ALF_CTB_CC_CB_IDC : ALF_CTB_CC_CR_IDC;
1096
31.3k
    int i = 0;
1097
31.3k
    const VVCFrameContext *fc = lc->fc;
1098
31.3k
    if (lc->ctb_left_flag) {
1099
29.2k
        const ALFParams *left = &CTB(fc->tab.alf, rx - 1, ry);
1100
29.2k
        inc += left->ctb_cc_idc[idx] != 0;
1101
29.2k
    }
1102
31.3k
    if (lc->ctb_up_flag) {
1103
27.8k
        const ALFParams *above = &CTB(fc->tab.alf, rx, ry - 1);
1104
27.8k
        inc += above->ctb_cc_idc[idx] != 0;
1105
27.8k
    }
1106
1107
31.3k
    if (!GET_CABAC(inc))
1108
27.6k
        return 0;
1109
3.63k
    i++;
1110
3.81k
    while (i < cc_filters_signalled && get_cabac_bypass(&lc->ep->cc))
1111
179
        i++;
1112
3.63k
    return i;
1113
31.3k
}
1114
1115
int ff_vvc_split_cu_flag(VVCLocalContext *lc, const int x0, const int y0,
1116
    const int cb_width, const int cb_height, const int is_chroma, const VVCAllowedSplit *a)
1117
17.7M
{
1118
17.7M
    const VVCFrameContext *fc = lc->fc;
1119
17.7M
    const VVCPPS *pps         = fc->ps.pps;
1120
17.7M
    const int is_inside       = (x0 + cb_width <= pps->width) && (y0 + cb_height <= pps->height);
1121
1122
17.7M
    if ((a->btv || a->bth || a->ttv || a->tth || a->qt) && is_inside)
1123
3.42M
    {
1124
3.42M
        uint8_t inc = 0, left_height = cb_height, top_width = cb_width;
1125
1126
3.42M
        get_left_top(lc, &left_height, &top_width, x0, y0, fc->tab.cb_height[is_chroma], fc->tab.cb_width[is_chroma]);
1127
3.42M
        inc += left_height < cb_height;
1128
3.42M
        inc += top_width   < cb_width;
1129
3.42M
        inc += (a->btv + a->bth + a->ttv + a->tth + 2 * a->qt - 1) / 2 * 3;
1130
1131
3.42M
        return GET_CABAC(SPLIT_CU_FLAG + inc);
1132
1133
3.42M
    }
1134
14.3M
    return !is_inside;
1135
17.7M
}
1136
1137
static int split_qt_flag_decode(VVCLocalContext *lc, const int x0, const int y0, const int ch_type, const int cqt_depth)
1138
428k
{
1139
428k
    const VVCFrameContext *fc = lc->fc;
1140
428k
    int inc = 0;
1141
428k
    uint8_t depth_left = 0, depth_top = 0;
1142
1143
428k
    get_left_top(lc,  &depth_left, &depth_top, x0, y0, fc->tab.cqt_depth[ch_type], fc->tab.cqt_depth[ch_type]);
1144
428k
    inc += depth_left > cqt_depth;
1145
428k
    inc += depth_top  > cqt_depth;
1146
428k
    inc += (cqt_depth >= 2) * 3;
1147
1148
428k
    return GET_CABAC(SPLIT_QT_FLAG + inc);
1149
428k
}
1150
1151
static int mtt_split_cu_vertical_flag_decode(VVCLocalContext *lc, const int x0, const int y0,
1152
    const int cb_width, const int cb_height, const int ch_type, const VVCAllowedSplit* a)
1153
2.27M
{
1154
2.27M
    if ((a->bth || a->tth) && (a->btv || a->ttv)) {
1155
958k
        int inc;
1156
958k
        const int v = a->btv + a->ttv;
1157
958k
        const int h = a->bth + a->tth;
1158
958k
        if (v > h)
1159
51.4k
            inc = 4;
1160
906k
        else if (v < h)
1161
14.8k
            inc = 3;
1162
892k
        else {
1163
892k
            const VVCFrameContext *fc = lc->fc;
1164
892k
            const VVCSPS *sps         = fc->ps.sps;
1165
892k
            const int min_cb_width    = fc->ps.pps->min_cb_width;
1166
892k
            const int x0b             = av_zero_extend(x0, sps->ctb_log2_size_y);
1167
892k
            const int y0b             = av_zero_extend(y0, sps->ctb_log2_size_y);
1168
892k
            const int x_cb            = x0 >> sps->min_cb_log2_size_y;
1169
892k
            const int y_cb            = y0 >> sps->min_cb_log2_size_y;
1170
892k
            const int available_a     = lc->ctb_up_flag || y0b;
1171
892k
            const int available_l     = lc->ctb_left_flag || x0b;
1172
892k
            const int da              = cb_width  / (available_a ? SAMPLE_CTB(fc->tab.cb_width[ch_type], x_cb, y_cb - 1) : 1);
1173
892k
            const int dl              = cb_height / (available_l ? SAMPLE_CTB(fc->tab.cb_height[ch_type], x_cb - 1, y_cb) : 1);
1174
1175
892k
            if (da == dl || !available_a || !available_l)
1176
642k
                inc = 0;
1177
249k
            else if (da < dl)
1178
117k
                inc = 1;
1179
132k
            else
1180
132k
                inc = 2;
1181
892k
        }
1182
958k
        return GET_CABAC(MTT_SPLIT_CU_VERTICAL_FLAG + inc);
1183
958k
    }
1184
1.31M
    return !(a->bth || a->tth);
1185
2.27M
}
1186
1187
static int mtt_split_cu_binary_flag_decode(VVCLocalContext *lc, const int mtt_split_cu_vertical_flag, const int mtt_depth)
1188
349k
{
1189
349k
    const int inc = (2 * mtt_split_cu_vertical_flag) + ((mtt_depth <= 1) ? 1 : 0);
1190
349k
    return GET_CABAC(MTT_SPLIT_CU_BINARY_FLAG + inc);
1191
349k
}
1192
1193
VVCSplitMode ff_vvc_split_mode(VVCLocalContext *lc, const int x0, const int y0, const int cb_width, const int cb_height,
1194
    const int cqt_depth, const int mtt_depth, const int ch_type, const VVCAllowedSplit *a)
1195
10.4M
{
1196
10.4M
    const int allow_no_qt = a->btv || a->bth || a->ttv || a->tth;
1197
10.4M
    int split_qt_flag;
1198
10.4M
    int mtt_split_cu_vertical_flag;
1199
10.4M
    int mtt_split_cu_binary_flag;
1200
10.4M
    const VVCSplitMode mtt_split_modes[] = {
1201
10.4M
        SPLIT_TT_HOR, SPLIT_BT_HOR, SPLIT_TT_VER, SPLIT_BT_VER,
1202
10.4M
    };
1203
10.4M
    if (allow_no_qt && a->qt) {
1204
428k
        split_qt_flag = split_qt_flag_decode(lc, x0, y0, ch_type, cqt_depth);
1205
10.0M
    } else {
1206
10.0M
        split_qt_flag = !allow_no_qt || a->qt;
1207
10.0M
    }
1208
10.4M
    if (split_qt_flag)
1209
8.17M
        return SPLIT_QT;
1210
2.27M
    mtt_split_cu_vertical_flag = mtt_split_cu_vertical_flag_decode(lc, x0, y0, cb_width, cb_height, ch_type, a);
1211
2.27M
    if ((a->btv && a->ttv && mtt_split_cu_vertical_flag) ||
1212
2.08M
        (a->bth && a->tth && !mtt_split_cu_vertical_flag)) {
1213
349k
        mtt_split_cu_binary_flag = mtt_split_cu_binary_flag_decode(lc, mtt_split_cu_vertical_flag, mtt_depth);
1214
1.92M
    } else {
1215
1.92M
        if (!a->btv && !a->bth)
1216
0
            mtt_split_cu_binary_flag = 0;
1217
1.92M
        else if (!a->ttv && !a->tth)
1218
1.92M
            mtt_split_cu_binary_flag = 1;
1219
2.89k
        else if (a->bth && a->ttv)
1220
1.57k
            mtt_split_cu_binary_flag = 1 - mtt_split_cu_vertical_flag;
1221
1.31k
        else
1222
1.31k
            mtt_split_cu_binary_flag = mtt_split_cu_vertical_flag;
1223
1.92M
    }
1224
2.27M
    return mtt_split_modes[(mtt_split_cu_vertical_flag << 1) + mtt_split_cu_binary_flag];
1225
10.4M
}
1226
1227
int ff_vvc_non_inter_flag(VVCLocalContext *lc, const int x0, const int y0, const int ch_type)
1228
14.7k
{
1229
14.7k
    const VVCFrameContext *fc = lc->fc;
1230
14.7k
    uint8_t inc, left = MODE_INTER, top = MODE_INTER;
1231
1232
14.7k
    get_left_top(lc, &left, &top, x0, y0, fc->tab.cpm[ch_type], fc->tab.cpm[ch_type]);
1233
14.7k
    inc = left == MODE_INTRA || top == MODE_INTRA;
1234
14.7k
    return GET_CABAC(NON_INTER_FLAG + inc);
1235
14.7k
}
1236
1237
int ff_vvc_pred_mode_flag(VVCLocalContext *lc, const int is_chroma)
1238
61.2k
{
1239
61.2k
    const VVCFrameContext *fc = lc->fc;
1240
61.2k
    const CodingUnit *cu      = lc->cu;
1241
61.2k
    uint8_t inc, left = MODE_INTER, top = MODE_INTER;
1242
1243
61.2k
    get_left_top(lc, &left, &top, cu->x0, cu->y0, fc->tab.cpm[is_chroma], fc->tab.cpm[is_chroma]);
1244
61.2k
    inc = left == MODE_INTRA || top == MODE_INTRA;
1245
61.2k
    return GET_CABAC(PRED_MODE_FLAG + inc);
1246
61.2k
}
1247
1248
int ff_vvc_pred_mode_plt_flag(VVCLocalContext *lc)
1249
770k
{
1250
770k
    return GET_CABAC(PRED_MODE_PLT_FLAG);
1251
770k
}
1252
1253
int ff_vvc_intra_bdpcm_luma_flag(VVCLocalContext *lc)
1254
1.68M
{
1255
1.68M
    return GET_CABAC(INTRA_BDPCM_LUMA_FLAG);
1256
1.68M
}
1257
1258
int ff_vvc_intra_bdpcm_luma_dir_flag(VVCLocalContext *lc)
1259
374k
{
1260
374k
    return GET_CABAC(INTRA_BDPCM_LUMA_DIR_FLAG);
1261
374k
}
1262
1263
int ff_vvc_intra_bdpcm_chroma_flag(VVCLocalContext *lc)
1264
529k
{
1265
529k
    return GET_CABAC(INTRA_BDPCM_CHROMA_FLAG);
1266
529k
}
1267
1268
int ff_vvc_intra_bdpcm_chroma_dir_flag(VVCLocalContext *lc)
1269
32.1k
{
1270
32.1k
    return GET_CABAC(INTRA_BDPCM_CHROMA_DIR_FLAG);
1271
32.1k
}
1272
1273
int ff_vvc_cu_skip_flag(VVCLocalContext *lc, const uint8_t *cu_skip_flag)
1274
3.83M
{
1275
3.83M
    const int inc = get_inc(lc, cu_skip_flag);
1276
3.83M
    return GET_CABAC(CU_SKIP_FLAG + inc);
1277
3.83M
}
1278
1279
int ff_vvc_pred_mode_ibc_flag(VVCLocalContext *lc, const int is_chroma)
1280
3.74M
{
1281
3.74M
    const VVCFrameContext *fc = lc->fc;
1282
3.74M
    const CodingUnit *cu      = lc->cu;
1283
3.74M
    uint8_t left_mode = MODE_INTER, top_mode = MODE_INTER;
1284
3.74M
    int inc;
1285
1286
3.74M
    get_left_top(lc, &left_mode, &top_mode, cu->x0, cu->y0, fc->tab.cpm[is_chroma], fc->tab.cpm[is_chroma]);
1287
3.74M
    inc = (left_mode == MODE_IBC) + (top_mode == MODE_IBC);
1288
3.74M
    return GET_CABAC(PRED_MODE_IBC_FLAG + inc);
1289
3.74M
}
1290
1291
static av_always_inline
1292
uint8_t get_mip_inc(VVCLocalContext *lc, const uint8_t *ctx)
1293
2.24M
{
1294
2.24M
    uint8_t left = 0, top = 0;
1295
2.24M
    get_left_top(lc, &left, &top, lc->cu->x0, lc->cu->y0, ctx, ctx);
1296
2.24M
    return (left & 1) + (top & 1);
1297
2.24M
}
1298
1299
int ff_vvc_intra_mip_flag(VVCLocalContext *lc, const uint8_t *intra_mip_flag)
1300
2.32M
{
1301
2.32M
    const int w   = lc->cu->cb_width;
1302
2.32M
    const int h   = lc->cu->cb_height;
1303
2.32M
    const int inc =  (w > h * 2 || h > w * 2) ? 3 : get_mip_inc(lc, intra_mip_flag);
1304
2.32M
    return GET_CABAC(INTRA_MIP_FLAG + inc);
1305
2.32M
}
1306
1307
int ff_vvc_intra_mip_transposed_flag(VVCLocalContext *lc)
1308
405k
{
1309
405k
    return get_cabac_bypass(&lc->ep->cc);
1310
405k
}
1311
1312
int ff_vvc_intra_mip_mode(VVCLocalContext *lc)
1313
405k
{
1314
405k
    const int w     = lc->cu->cb_width;
1315
405k
    const int h     = lc->cu->cb_height;
1316
405k
    const int c_max = (w == 4 && h == 4) ? 15 :
1317
405k
        ((w == 4 || h == 4) || (w == 8 && h == 8)) ? 7: 5;
1318
405k
    return truncated_binary_decode(lc, c_max);
1319
405k
}
1320
1321
int ff_vvc_intra_luma_ref_idx(VVCLocalContext *lc)
1322
783k
{
1323
783k
    int i;
1324
829k
    for (i = 0; i < 2; i++) {
1325
810k
        if (!GET_CABAC(INTRA_LUMA_REF_IDX + i))
1326
764k
            return i;
1327
810k
    }
1328
18.9k
    return i;
1329
783k
}
1330
1331
int ff_vvc_intra_subpartitions_mode_flag(VVCLocalContext *lc)
1332
781k
{
1333
781k
    return GET_CABAC(INTRA_SUBPARTITIONS_MODE_FLAG);
1334
781k
}
1335
1336
enum IspType ff_vvc_isp_split_type(VVCLocalContext *lc, const int intra_subpartitions_mode_flag)
1337
4.18M
{
1338
4.18M
    if (!intra_subpartitions_mode_flag)
1339
4.12M
        return ISP_NO_SPLIT;
1340
62.6k
    return 1 + GET_CABAC(INTRA_SUBPARTITIONS_SPLIT_FLAG);
1341
4.18M
}
1342
1343
int ff_vvc_intra_luma_mpm_flag(VVCLocalContext *lc)
1344
4.15M
{
1345
4.15M
    return GET_CABAC(INTRA_LUMA_MPM_FLAG);
1346
4.15M
}
1347
1348
int ff_vvc_intra_luma_not_planar_flag(VVCLocalContext *lc, const int intra_subpartitions_mode_flag)
1349
2.86M
{
1350
2.86M
    return GET_CABAC(INTRA_LUMA_NOT_PLANAR_FLAG + !intra_subpartitions_mode_flag);
1351
2.86M
}
1352
1353
int ff_vvc_intra_luma_mpm_idx(VVCLocalContext *lc)
1354
1.85M
{
1355
1.85M
    int i;
1356
2.70M
    for (i = 0; i < 4 && get_cabac_bypass(&lc->ep->cc); i++)
1357
852k
        /* nothing */;
1358
1.85M
    return i;
1359
1.85M
}
1360
1361
int ff_vvc_intra_luma_mpm_remainder(VVCLocalContext *lc)
1362
1.29M
{
1363
1.29M
    return truncated_binary_decode(lc, 60);
1364
1.29M
}
1365
1366
int ff_vvc_cclm_mode_flag(VVCLocalContext *lc)
1367
2.12M
{
1368
2.12M
    return GET_CABAC(CCLM_MODE_FLAG);
1369
2.12M
}
1370
1371
int ff_vvc_cclm_mode_idx(VVCLocalContext *lc)
1372
1.33M
{
1373
1.33M
    if (!GET_CABAC(CCLM_MODE_IDX))
1374
873k
        return 0;
1375
466k
    return get_cabac_bypass(&lc->ep->cc) + 1;
1376
1.33M
}
1377
1378
int ff_vvc_intra_chroma_pred_mode(VVCLocalContext *lc)
1379
1.08M
{
1380
1.08M
    if (!GET_CABAC(INTRA_CHROMA_PRED_MODE))
1381
814k
        return 4;
1382
266k
    return (get_cabac_bypass(&lc->ep->cc) << 1) | get_cabac_bypass(&lc->ep->cc);
1383
1.08M
}
1384
1385
int ff_vvc_palette_predictor_run(VVCLocalContext *lc, const int max)
1386
1.58k
{
1387
1.58k
    return kth_order_egk_decode(&lc->ep->cc, 0, max);
1388
1.58k
}
1389
1390
int ff_vvc_num_signalled_palette_entries(VVCLocalContext *lc, const int max)
1391
97.8k
{
1392
97.8k
    return kth_order_egk_decode(&lc->ep->cc, 0, max);
1393
97.8k
}
1394
1395
int ff_vvc_new_palette_entries(VVCLocalContext *lc, const int bit_depth)
1396
775k
{
1397
775k
    return fixed_length_decode(&lc->ep->cc, bit_depth);
1398
775k
}
1399
1400
bool ff_vvc_palette_escape_val_present_flag(VVCLocalContext *lc)
1401
84.5k
{
1402
84.5k
    return get_cabac_bypass(&lc->ep->cc);
1403
84.5k
}
1404
1405
bool ff_vvc_palette_transpose_flag(VVCLocalContext *lc)
1406
84.1k
{
1407
84.1k
    return GET_CABAC(PALETTE_TRANSPOSE_FLAG);
1408
84.1k
}
1409
1410
bool ff_vvc_run_copy_flag(VVCLocalContext *lc, const int prev_run_type, const int prev_run_position, const int cur_pos)
1411
4.93M
{
1412
4.93M
    uint8_t run_left_lut[] = { 0, 1, 2, 3, 4 };
1413
4.93M
    uint8_t run_top_lut[] = { 5, 6, 6, 7, 7 };
1414
1415
4.93M
    int bin_dist = cur_pos - prev_run_position - 1;
1416
4.93M
    uint8_t *run_lut = prev_run_type == 1 ? run_top_lut : run_left_lut;
1417
4.93M
    uint8_t ctx_inc = bin_dist <= 4 ? run_lut[bin_dist] : run_lut[4];
1418
1419
4.93M
    return GET_CABAC(RUN_COPY_FLAG + ctx_inc);
1420
4.93M
}
1421
1422
bool ff_vvc_copy_above_palette_indices_flag(VVCLocalContext *lc)
1423
426k
{
1424
426k
    return GET_CABAC(COPY_ABOVE_PALETTE_INDICES_FLAG);
1425
426k
}
1426
1427
int ff_vvc_palette_idx_idc(VVCLocalContext *lc, const int max_palette_index, const bool adjust)
1428
754k
{
1429
754k
    return truncated_binary_decode(lc, max_palette_index - adjust);
1430
754k
}
1431
1432
int ff_vvc_palette_escape_val(VVCLocalContext *lc, const int max)
1433
1.17M
{
1434
1.17M
    return kth_order_egk_decode(&lc->ep->cc, 5, max);
1435
1.17M
}
1436
1437
int ff_vvc_general_merge_flag(VVCLocalContext *lc)
1438
449k
{
1439
449k
    return GET_CABAC(GENERAL_MERGE_FLAG);
1440
449k
}
1441
1442
static int get_inter_flag_inc(VVCLocalContext *lc, const int x0, const int y0)
1443
23.7k
{
1444
23.7k
    uint8_t left_merge = 0,  top_merge = 0;
1445
23.7k
    uint8_t left_affine = 0, top_affine = 0;
1446
23.7k
    const VVCFrameContext *fc = lc->fc;
1447
1448
23.7k
    get_left_top(lc, &left_merge, &top_merge, x0, y0, fc->tab.msf, fc->tab.msf);
1449
23.7k
    get_left_top(lc, &left_affine, &top_affine, x0, y0, fc->tab.iaf, fc->tab.iaf);
1450
23.7k
    return (left_merge || left_affine) + (top_merge + top_affine);
1451
23.7k
}
1452
1453
int ff_vvc_merge_subblock_flag(VVCLocalContext *lc)
1454
23.7k
{
1455
23.7k
    const int inc = get_inter_flag_inc(lc, lc->cu->x0, lc->cu->y0);
1456
23.7k
    return GET_CABAC(MERGE_SUBBLOCK_FLAG + inc);
1457
23.7k
}
1458
1459
int ff_vvc_merge_subblock_idx(VVCLocalContext *lc, const int max_num_subblock_merge_cand)
1460
3.82k
{
1461
3.82k
    int i;
1462
3.82k
    if (!GET_CABAC(MERGE_SUBBLOCK_IDX))
1463
1.39k
        return 0;
1464
6.51k
    for (i = 1; i < max_num_subblock_merge_cand - 1 && get_cabac_bypass(&lc->ep->cc); i++)
1465
4.07k
        /* nothing */;
1466
2.43k
    return i;
1467
3.82k
}
1468
1469
int ff_vvc_regular_merge_flag(VVCLocalContext *lc, const int cu_skip_flag)
1470
39.5k
{
1471
39.5k
    int inc = !cu_skip_flag;
1472
39.5k
    return GET_CABAC(REGULAR_MERGE_FLAG + inc);
1473
39.5k
}
1474
1475
int ff_vvc_mmvd_merge_flag(VVCLocalContext *lc)
1476
45.1k
{
1477
45.1k
    return GET_CABAC(MMVD_MERGE_FLAG);
1478
45.1k
}
1479
1480
int ff_vvc_mmvd_cand_flag(VVCLocalContext *lc)
1481
22.0k
{
1482
22.0k
    return GET_CABAC(MMVD_CAND_FLAG);
1483
22.0k
}
1484
1485
static int mmvd_distance_idx_decode(VVCLocalContext *lc)
1486
22.5k
{
1487
22.5k
    int i;
1488
22.5k
    if (!GET_CABAC(MMVD_DISTANCE_IDX))
1489
6.46k
        return 0;
1490
26.9k
    for (i = 1; i < 7 && get_cabac_bypass(&lc->ep->cc); i++)
1491
10.9k
        /* nothing */;
1492
16.0k
    return i;
1493
22.5k
}
1494
1495
static int mmvd_direction_idx_decode(VVCLocalContext *lc)
1496
22.5k
{
1497
22.5k
    return (get_cabac_bypass(&lc->ep->cc) << 1) | get_cabac_bypass(&lc->ep->cc);
1498
22.5k
}
1499
1500
void ff_vvc_mmvd_offset_coding(VVCLocalContext *lc, Mv *mmvd_offset, const int ph_mmvd_fullpel_only_flag)
1501
22.5k
{
1502
22.5k
    const int shift = ph_mmvd_fullpel_only_flag ? 4 : 2;
1503
22.5k
    const int mmvd_distance = 1 << (mmvd_distance_idx_decode(lc) + shift);
1504
22.5k
    const int mmvd_direction_idx = mmvd_direction_idx_decode(lc);
1505
22.5k
    const int mmvd_signs[][2] = { {1, 0}, {-1, 0}, {0, 1}, {0, -1} };
1506
22.5k
    mmvd_offset->x = mmvd_distance * mmvd_signs[mmvd_direction_idx][0];
1507
22.5k
    mmvd_offset->y = mmvd_distance * mmvd_signs[mmvd_direction_idx][1];
1508
22.5k
}
1509
1510
static PredMode get_luma_pred_mode(VVCLocalContext *lc)
1511
140k
{
1512
140k
    const VVCFrameContext *fc = lc->fc;
1513
140k
    const CodingUnit *cu      = lc->cu;
1514
140k
    PredMode pred_mode;
1515
1516
140k
    if (cu->tree_type != DUAL_TREE_CHROMA) {
1517
140k
        pred_mode = cu->pred_mode;
1518
140k
    } else {
1519
0
        const int x_cb         = cu->x0 >> fc->ps.sps->min_cb_log2_size_y;
1520
0
        const int y_cb         = cu->y0 >> fc->ps.sps->min_cb_log2_size_y;
1521
0
        const int min_cb_width = fc->ps.pps->min_cb_width;
1522
0
        pred_mode = SAMPLE_CTB(fc->tab.cpm[0], x_cb, y_cb);
1523
0
    }
1524
140k
    return pred_mode;
1525
140k
}
1526
1527
int ff_vvc_merge_idx(VVCLocalContext *lc)
1528
140k
{
1529
140k
    const VVCSPS *sps = lc->fc->ps.sps;
1530
140k
    const int is_ibc = get_luma_pred_mode(lc) == MODE_IBC;
1531
140k
    const int c_max = (is_ibc ? sps->max_num_ibc_merge_cand : sps->max_num_merge_cand) - 1;
1532
140k
    int i;
1533
1534
140k
    if (!GET_CABAC(MERGE_IDX))
1535
104k
        return 0;
1536
1537
77.4k
    for (i = 1; i < c_max && get_cabac_bypass(&lc->ep->cc); i++)
1538
42.0k
        /* nothing */;
1539
35.4k
    return i;
1540
140k
}
1541
1542
int ff_vvc_merge_gpm_partition_idx(VVCLocalContext *lc)
1543
10.1k
{
1544
10.1k
    return fixed_length_decode(&lc->ep->cc, 6);
1545
10.1k
}
1546
1547
int ff_vvc_merge_gpm_idx(VVCLocalContext *lc, const int idx)
1548
16.7k
{
1549
16.7k
    const int c_max = lc->fc->ps.sps->max_num_gpm_merge_cand - idx - 1;
1550
16.7k
    int i;
1551
1552
16.7k
    if (!GET_CABAC(MERGE_IDX))
1553
8.27k
        return 0;
1554
1555
10.8k
    for (i = 1; i < c_max && get_cabac_bypass(&lc->ep->cc); i++)
1556
2.35k
        /* nothing */;
1557
1558
8.44k
    return i;
1559
16.7k
}
1560
1561
int ff_vvc_ciip_flag(VVCLocalContext *lc)
1562
4.88k
{
1563
4.88k
    return GET_CABAC(CIIP_FLAG);
1564
4.88k
}
1565
1566
PredFlag ff_vvc_pred_flag(VVCLocalContext *lc, const int is_b)
1567
33.8k
{
1568
33.8k
    const int w = lc->cu->cb_width;
1569
33.8k
    const int h = lc->cu->cb_height;
1570
33.8k
    if (!is_b)
1571
7.03k
        return  PF_L0;
1572
26.8k
    if (w + h > 12) {
1573
14.4k
        const int log2 = av_log2(w) + av_log2(h);
1574
14.4k
        const int inc = 7 - ((1 + log2)>>1);
1575
14.4k
        if (GET_CABAC(INTER_PRED_IDC + inc))
1576
1.93k
            return PF_BI;
1577
14.4k
    }
1578
24.8k
    return PF_L0 + GET_CABAC(INTER_PRED_IDC + 5);
1579
26.8k
}
1580
1581
int ff_vvc_inter_affine_flag(VVCLocalContext *lc)
1582
0
{
1583
0
    const int inc = get_inter_flag_inc(lc, lc->cu->x0, lc->cu->y0);
1584
0
    return GET_CABAC(INTER_AFFINE_FLAG + inc);
1585
0
}
1586
1587
int ff_vvc_cu_affine_type_flag(VVCLocalContext *lc)
1588
0
{
1589
0
    return GET_CABAC(CU_AFFINE_TYPE_FLAG);
1590
0
}
1591
1592
int ff_vvc_sym_mvd_flag(VVCLocalContext *lc)
1593
173
{
1594
173
    return GET_CABAC(SYM_MVD_FLAG);
1595
173
}
1596
1597
int ff_vvc_ref_idx_lx(VVCLocalContext *lc, const uint8_t nb_refs)
1598
832
{
1599
832
    const int c_max = nb_refs - 1;
1600
832
    const int max_ctx = FFMIN(c_max, 2);
1601
832
    int i = 0;
1602
1603
1.44k
    while (i < max_ctx && GET_CABAC(REF_IDX_LX + i))
1604
610
        i++;
1605
832
    if (i == 2) {
1606
300
        while (i < c_max && get_cabac_bypass(&lc->ep->cc))
1607
132
            i++;
1608
168
    }
1609
832
    return i;
1610
832
}
1611
1612
int ff_vvc_abs_mvd_greater0_flag(VVCLocalContext *lc)
1613
642k
{
1614
642k
    return GET_CABAC(ABS_MVD_GREATER0_FLAG);
1615
642k
}
1616
1617
int ff_vvc_abs_mvd_greater1_flag(VVCLocalContext *lc)
1618
449k
{
1619
449k
    return GET_CABAC(ABS_MVD_GREATER1_FLAG);
1620
449k
}
1621
1622
int ff_vvc_abs_mvd_minus2(VVCLocalContext *lc)
1623
346k
{
1624
346k
    return limited_kth_order_egk_decode(&lc->ep->cc, 1, 15, 17);
1625
346k
}
1626
1627
int ff_vvc_mvd_sign_flag(VVCLocalContext *lc)
1628
449k
{
1629
449k
    return get_cabac_bypass(&lc->ep->cc);
1630
449k
}
1631
1632
int ff_vvc_mvp_lx_flag(VVCLocalContext *lc)
1633
318k
{
1634
318k
    return GET_CABAC(MVP_LX_FLAG);
1635
318k
}
1636
1637
static int amvr_flag(VVCLocalContext *lc, const int inter_affine_flag)
1638
5.47k
{
1639
5.47k
    return GET_CABAC(AMVR_FLAG + inter_affine_flag);
1640
5.47k
}
1641
1642
static int amvr_precision_idx(VVCLocalContext *lc, const int inc, const int c_max)
1643
196k
{
1644
196k
    int i = 0;
1645
196k
    if (!GET_CABAC(AMVR_PRECISION_IDX + inc))
1646
141k
        return 0;
1647
55.7k
    i++;
1648
55.7k
    if (i < c_max && GET_CABAC(AMVR_PRECISION_IDX + 1))
1649
395
        i++;
1650
55.7k
    return i;
1651
196k
}
1652
1653
int ff_vvc_amvr_shift(VVCLocalContext *lc, const int inter_affine_flag,
1654
    const PredMode pred_mode, const int has_amvr_flag)
1655
226k
{
1656
226k
    int amvr_shift = 2;
1657
226k
    if (has_amvr_flag) {
1658
198k
        if (pred_mode == MODE_IBC || amvr_flag(lc, inter_affine_flag)) {
1659
196k
            int idx;
1660
196k
            if (inter_affine_flag) {
1661
0
                idx = amvr_precision_idx(lc, 2, 1);
1662
0
                amvr_shift = idx * 4;
1663
196k
            } else if (pred_mode == MODE_IBC) {
1664
192k
                idx = amvr_precision_idx(lc, 1, 1);
1665
192k
                amvr_shift = 4 + idx * 2;
1666
192k
            } else {
1667
4.00k
                static const int shifts[] = {3, 4, 6};
1668
4.00k
                idx = amvr_precision_idx(lc, 0, 2);
1669
4.00k
                amvr_shift = shifts[idx];
1670
4.00k
            }
1671
196k
        }
1672
198k
    }
1673
226k
    return amvr_shift;
1674
226k
}
1675
1676
int ff_vvc_bcw_idx(VVCLocalContext *lc, const int no_backward_pred_flag)
1677
0
{
1678
0
    const int c_max = no_backward_pred_flag ? 4 : 2;
1679
0
    int i = 1;
1680
0
    if (!GET_CABAC(BCW_IDX))
1681
0
        return 0;
1682
0
    while (i < c_max && get_cabac_bypass(&lc->ep->cc))
1683
0
        i++;
1684
0
    return i;
1685
0
}
1686
1687
int ff_vvc_tu_cb_coded_flag(VVCLocalContext *lc)
1688
2.76M
{
1689
2.76M
    return GET_CABAC(TU_CB_CODED_FLAG + lc->cu->bdpcm_flag[1]);
1690
2.76M
}
1691
1692
int ff_vvc_tu_cr_coded_flag(VVCLocalContext *lc, int tu_cb_coded_flag)
1693
2.76M
{
1694
2.76M
    return GET_CABAC(TU_CR_CODED_FLAG + (lc->cu->bdpcm_flag[1] ? 2 : tu_cb_coded_flag));
1695
2.76M
}
1696
1697
int ff_vvc_tu_y_coded_flag(VVCLocalContext *lc)
1698
4.94M
{
1699
4.94M
    const CodingUnit *cu = lc->cu;
1700
4.94M
    int inc;
1701
4.94M
    if (cu->bdpcm_flag[0])
1702
374k
        inc = 1;
1703
4.57M
    else if (cu->isp_split_type == ISP_NO_SPLIT)
1704
4.39M
        inc = 0;
1705
179k
    else
1706
179k
        inc = 2 + lc->parse.prev_tu_cbf_y;
1707
4.94M
    lc->parse.prev_tu_cbf_y = GET_CABAC(TU_Y_CODED_FLAG + inc);
1708
4.94M
    return lc->parse.prev_tu_cbf_y;
1709
4.94M
}
1710
1711
int ff_vvc_cu_act_enabled_flag(VVCLocalContext *lc)
1712
353k
{
1713
353k
    return GET_CABAC(CU_ACT_ENABLED_FLAG);
1714
353k
}
1715
1716
int ff_vvc_cu_qp_delta_abs(VVCLocalContext *lc)
1717
885k
{
1718
885k
    int v, i, k;
1719
885k
    if (!GET_CABAC(CU_QP_DELTA_ABS))
1720
201k
        return 0;
1721
1722
    // prefixVal
1723
914k
    for (v = 1; v < 5 && GET_CABAC(CU_QP_DELTA_ABS + 1); v++)
1724
229k
        /* nothing */;
1725
684k
    if (v < 5)
1726
677k
        return v;
1727
1728
    // 9.3.3.5 k-th order Exp-Golomb binarization process
1729
    // suffixVal
1730
1731
    // CuQpDeltaVal shall in the range of −( 32 + QpBdOffset / 2 ) to +( 31 + QpBdOffset / 2 )
1732
    // so k = 6 should enough
1733
21.0k
    for (k = 0; k < 6 && get_cabac_bypass(&lc->ep->cc); k++)
1734
13.9k
        /* nothing */;
1735
7.07k
    i = (1 << k) - 1;
1736
7.07k
    v = 0;
1737
21.0k
    while (k--)
1738
13.9k
        v = (v << 1) + get_cabac_bypass(&lc->ep->cc);
1739
7.07k
    v += i;
1740
1741
7.07k
    return v + 5;
1742
684k
}
1743
1744
int ff_vvc_cu_qp_delta_sign_flag(VVCLocalContext *lc)
1745
684k
{
1746
684k
    return get_cabac_bypass(&lc->ep->cc);
1747
684k
}
1748
1749
int ff_vvc_cu_chroma_qp_offset_flag(VVCLocalContext *lc)
1750
27.3k
{
1751
27.3k
    return GET_CABAC(CU_CHROMA_QP_OFFSET_FLAG);
1752
27.3k
}
1753
1754
int ff_vvc_cu_chroma_qp_offset_idx(VVCLocalContext *lc)
1755
4.25k
{
1756
4.25k
    const int c_max = lc->fc->ps.pps->r->pps_chroma_qp_offset_list_len_minus1;
1757
4.25k
    int i;
1758
6.53k
    for (i = 0; i < c_max && GET_CABAC(CU_CHROMA_QP_OFFSET_IDX); i++)
1759
2.28k
        /* nothing */;
1760
4.25k
    return i;
1761
4.25k
}
1762
1763
static av_always_inline int last_significant_coeff_xy_prefix(VVCLocalContext *lc,
1764
    const int log2_tb_size, const int log2_zo_tb_size, const int c_idx, const int ctx)
1765
11.1M
{
1766
11.1M
    int i = 0;
1767
11.1M
    int max = (log2_zo_tb_size << 1) - 1;
1768
11.1M
    int ctx_offset, ctx_shift;
1769
11.1M
    if (!log2_tb_size)
1770
353
        return 0;
1771
11.1M
    if (!c_idx) {
1772
8.05M
        const int offset_y[] = {0, 0, 3, 6, 10, 15};
1773
8.05M
        ctx_offset = offset_y[log2_tb_size - 1];
1774
8.05M
        ctx_shift  = (log2_tb_size + 1) >> 2;
1775
8.05M
    } else {
1776
3.13M
        const int shifts[] = {0, 0, 0, 1, 2, 2, 2};
1777
3.13M
        ctx_offset = 20;
1778
3.13M
        ctx_shift  = shifts[log2_tb_size];
1779
3.13M
    }
1780
27.6M
    while (i < max && GET_CABAC(ctx + (i >> ctx_shift) + ctx_offset))
1781
16.4M
        i++;
1782
11.1M
    return i;
1783
11.1M
}
1784
1785
static av_always_inline int last_significant_coeff_x_prefix_decode(VVCLocalContext *lc,
1786
    const int log2_tb_width, const int log2_zo_tb_width, const int c_idx)
1787
5.59M
{
1788
5.59M
    return last_significant_coeff_xy_prefix(lc, log2_tb_width, log2_zo_tb_width, c_idx, LAST_SIG_COEFF_X_PREFIX);
1789
5.59M
}
1790
1791
static av_always_inline int last_significant_coeff_y_prefix_decode(VVCLocalContext *lc,
1792
    const int log2_tb_height, const int log2_zo_tb_height, const int c_idx)
1793
5.59M
{
1794
5.59M
    return last_significant_coeff_xy_prefix(lc, log2_tb_height, log2_zo_tb_height, c_idx, LAST_SIG_COEFF_Y_PREFIX);
1795
5.59M
}
1796
1797
static av_always_inline int last_sig_coeff_suffix_decode(VVCLocalContext *lc,
1798
    const int last_significant_coeff_y_prefix)
1799
1.24M
{
1800
1.24M
    const int length = (last_significant_coeff_y_prefix >> 1) - 1;
1801
1.24M
    int value = get_cabac_bypass(&lc->ep->cc);
1802
1803
1.73M
    for (int i = 1; i < length; i++)
1804
496k
        value = (value << 1) | get_cabac_bypass(&lc->ep->cc);
1805
1.24M
    return value;
1806
1.24M
}
1807
1808
int ff_vvc_tu_joint_cbcr_residual_flag(VVCLocalContext *lc, const int tu_cb_coded_flag, const int tu_cr_coded_flag)
1809
1.19M
{
1810
1.19M
    return GET_CABAC(TU_JOINT_CBCR_RESIDUAL_FLAG + 2 * tu_cb_coded_flag + tu_cr_coded_flag - 1);
1811
1.19M
}
1812
1813
int ff_vvc_transform_skip_flag(VVCLocalContext *lc, const int inc)
1814
3.89M
{
1815
3.89M
    return GET_CABAC(TRANSFORM_SKIP_FLAG + inc);
1816
3.89M
}
1817
1818
//9.3.4.2.7 Derivation process for the variables locNumSig, locSumAbsPass1
1819
static int get_local_sum(const int *level, const int w, const int h,
1820
    const int xc, const int yc, const int hist_value)
1821
145M
{
1822
145M
    int loc_sum = 3 * hist_value;
1823
145M
    level += w * yc + xc;
1824
145M
    if (xc < w - 1) {
1825
141M
        loc_sum += level[1];
1826
141M
        if (xc < w - 2)
1827
131M
            loc_sum += level[2] - hist_value;
1828
141M
        if (yc < h - 1)
1829
133M
            loc_sum += level[w + 1] - hist_value;
1830
141M
    }
1831
145M
    if (yc < h - 1) {
1832
137M
        loc_sum += level[w];
1833
137M
        if (yc < h - 2)
1834
122M
            loc_sum += level[w << 1] - hist_value;
1835
137M
    }
1836
145M
    return loc_sum;
1837
145M
}
1838
1839
//9.3.4.2.7 Derivation process for the variables locNumSig, locSumAbsPass1
1840
static int get_local_sum_ts(const int *level, const int w, const int h, const int xc, const int yc)
1841
9.50M
{
1842
9.50M
    int loc_sum = 0;
1843
9.50M
    level += w * yc + xc;
1844
9.50M
    if (xc > 0)
1845
6.90M
        loc_sum += level[-1];
1846
9.50M
    if (yc > 0)
1847
6.87M
        loc_sum += level[-w];
1848
9.50M
    return loc_sum;
1849
9.50M
}
1850
1851
static int get_gtx_flag_inc(const ResidualCoding* rc, const int xc, const int yc, const int last)
1852
39.1M
{
1853
39.1M
    const TransformBlock *tb = rc->tb;
1854
39.1M
    int inc;
1855
39.1M
    if (last) {
1856
5.59M
        const int incs[] = {0, 21, 21};
1857
5.59M
        inc =  incs[tb->c_idx];
1858
33.5M
    } else {
1859
33.5M
        const int d = xc + yc;
1860
33.5M
        const int local_sum_sig = get_local_sum(rc->sig_coeff_flag,
1861
33.5M
                tb->tb_width,tb->tb_height, xc, yc, rc->hist_value);
1862
33.5M
        const int loc_sum_abs_pass1 = get_local_sum(rc->abs_level_pass1,
1863
33.5M
                tb->tb_width, tb->tb_height, xc, yc, rc->hist_value);
1864
33.5M
        const int offset = FFMIN(loc_sum_abs_pass1 - local_sum_sig, 4);
1865
1866
33.5M
        if (!tb->c_idx)
1867
29.3M
            inc =  1 + offset + (!d ? 15 : (d < 3 ? 10 : (d < 10 ? 5 : 0)));
1868
4.24M
        else
1869
4.24M
            inc = 22 + offset + (!d ? 5 : 0);
1870
33.5M
    }
1871
39.1M
    return inc;
1872
39.1M
}
1873
1874
static int abs_level_gtx_flag_decode(VVCLocalContext *lc, const int inc)
1875
47.1M
{
1876
47.1M
    return GET_CABAC(ABS_LEVEL_GTX_FLAG + inc);
1877
47.1M
}
1878
1879
static int par_level_flag_decode(VVCLocalContext *lc, const int inc)
1880
7.96M
{
1881
7.96M
    return GET_CABAC(PAR_LEVEL_FLAG + inc);
1882
7.96M
}
1883
1884
static int par_level_flag_ts_decode(VVCLocalContext *lc)
1885
1.63M
{
1886
1.63M
    const int inc = 32;
1887
1.63M
    return GET_CABAC(PAR_LEVEL_FLAG + inc);
1888
1.63M
}
1889
1890
static int sb_coded_flag_decode(VVCLocalContext *lc, const uint8_t *sb_coded_flag,
1891
    const ResidualCoding *rc, const int xs, const int ys)
1892
3.45M
{
1893
3.45M
    const H266RawSliceHeader *rsh = lc->sc->sh.r;
1894
3.45M
    const TransformBlock *tb      = rc->tb;
1895
3.45M
    const int w                   = rc->width_in_sbs;
1896
3.45M
    const int h                   = rc->height_in_sbs;
1897
3.45M
    int inc;
1898
1899
3.45M
    if (tb->ts && !rsh->sh_ts_residual_coding_disabled_flag) {
1900
61.9k
        const int left  = xs > 0 ? sb_coded_flag[-1] : 0;
1901
61.9k
        const int above = ys > 0 ? sb_coded_flag[-w] : 0;
1902
61.9k
        inc = left + above + 4;
1903
3.39M
    } else {
1904
3.39M
        const int right  = (xs < w - 1) ? sb_coded_flag[1] : 0;
1905
3.39M
        const int bottom = (ys < h - 1) ? sb_coded_flag[w] : 0;
1906
3.39M
        inc = (right | bottom) + (tb->c_idx ? 2 : 0);
1907
3.39M
    }
1908
3.45M
    return GET_CABAC(SB_CODED_FLAG + inc);
1909
3.45M
}
1910
1911
static int sig_coeff_flag_decode(VVCLocalContext *lc, const ResidualCoding* rc, const int xc, const int yc)
1912
84.0M
{
1913
84.0M
    const H266RawSliceHeader *rsh = lc->sc->sh.r;
1914
84.0M
    const TransformBlock *tb      = rc->tb;
1915
84.0M
    int inc;
1916
1917
84.0M
    if (tb->ts && !rsh->sh_ts_residual_coding_disabled_flag) {
1918
9.50M
        const int local_num_sig = get_local_sum_ts(rc->sig_coeff_flag, tb->tb_width, tb->tb_height, xc, yc);
1919
9.50M
        inc = 60 + local_num_sig;
1920
74.5M
    } else {
1921
74.5M
        const int d = xc + yc;
1922
74.5M
        const int loc_sum_abs_pass1 = get_local_sum(rc->abs_level_pass1,
1923
74.5M
                tb->tb_width, tb->tb_height, xc, yc, 0);
1924
1925
74.5M
        if (!tb->c_idx) {
1926
66.7M
            inc = 12 * FFMAX(0, rc->qstate - 1) + FFMIN((loc_sum_abs_pass1 + 1) >> 1, 3) + ((d < 2) ? 8 : (d < 5 ? 4 : 0));
1927
66.7M
        } else {
1928
7.83M
            inc = 36 + 8 * FFMAX(0, rc->qstate - 1) + FFMIN((loc_sum_abs_pass1 + 1) >> 1, 3) + (d < 2 ? 4 : 0);
1929
7.83M
        }
1930
74.5M
    }
1931
84.0M
    return GET_CABAC(SIG_COEFF_FLAG + inc);
1932
84.0M
}
1933
1934
static int abs_get_rice_param(VVCLocalContext *lc, const ResidualCoding* rc,
1935
                              const int xc, const int yc, const int base_level)
1936
3.58M
{
1937
3.58M
    const VVCSPS *sps = lc->fc->ps.sps;
1938
3.58M
    const TransformBlock* tb = rc->tb;
1939
3.58M
    const int rice_params[] = {
1940
3.58M
        0, 0, 0, 0, 0, 0, 0, 1, 1, 1, 1, 1, 1, 1, 2, 2,
1941
3.58M
        2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 3, 3, 3, 3,
1942
3.58M
    };
1943
3.58M
    int loc_sum_abs;
1944
3.58M
    int shift_val;
1945
1946
3.58M
    loc_sum_abs = get_local_sum(rc->abs_level, tb->tb_width, tb->tb_height, xc,
1947
3.58M
            yc, rc->hist_value);
1948
1949
3.58M
    if (!sps->r->sps_rrc_rice_extension_flag) {
1950
3.37M
        shift_val = 0;
1951
3.37M
    } else {
1952
210k
        shift_val = (av_log2(FFMAX(FFMIN(loc_sum_abs, 2048), 8)) - 3) & ~1;
1953
210k
    }
1954
1955
3.58M
    loc_sum_abs = av_clip_uintp2((loc_sum_abs >> shift_val) - base_level * 5, 5);
1956
1957
3.58M
    return rice_params[loc_sum_abs] + shift_val;
1958
3.58M
}
1959
1960
static int abs_decode(VVCLocalContext *lc, const int c_rice_param)
1961
6.19M
{
1962
6.19M
    const VVCSPS *sps = lc->fc->ps.sps;
1963
6.19M
    const int MAX_BIN = 6;
1964
6.19M
    int prefix = 0;
1965
6.19M
    int suffix = 0;
1966
1967
13.8M
    while (prefix < MAX_BIN && get_cabac_bypass(&lc->ep->cc))
1968
7.61M
        prefix++;
1969
6.19M
    if (prefix < MAX_BIN) {
1970
9.83M
        for (int i = 0; i < c_rice_param; i++) {
1971
4.10M
            suffix = (suffix << 1) | get_cabac_bypass(&lc->ep->cc);
1972
4.10M
        }
1973
5.73M
    } else {
1974
465k
        suffix = limited_kth_order_egk_decode(&lc->ep->cc,
1975
465k
                                              c_rice_param + 1,
1976
465k
                                              26 - sps->log2_transform_range,
1977
465k
                                              sps->log2_transform_range);
1978
465k
    }
1979
6.19M
    return suffix + (prefix << c_rice_param);
1980
6.19M
}
1981
1982
static int abs_remainder_decode(VVCLocalContext *lc, const ResidualCoding* rc, const int xc, const int yc)
1983
1.88M
{
1984
1.88M
    const VVCSPS *sps             = lc->fc->ps.sps;
1985
1.88M
    const H266RawSliceHeader *rsh = lc->sc->sh.r;
1986
1.88M
    const int base_level[][2][2]  = {
1987
1.88M
        { {4, 4}, {4, 4} },
1988
1.88M
        { {3, 2}, {2, 1} }
1989
1.88M
    };
1990
1.88M
    const int c_rice_param = abs_get_rice_param(lc, rc, xc, yc,
1991
1.88M
        base_level[sps->r->sps_rrc_rice_extension_flag][sps->bit_depth > 12][IS_I(rsh)]);
1992
1.88M
    const int rem = abs_decode(lc, c_rice_param);
1993
1994
1.88M
    return rem;
1995
1.88M
}
1996
1997
static int abs_remainder_ts_decode(VVCLocalContext *lc, const ResidualCoding* rc, const int xc, const int yc)
1998
2.61M
{
1999
2.61M
    const H266RawSliceHeader *rsh = lc->sc->sh.r;
2000
2.61M
    const int c_rice_param = rsh->sh_ts_residual_coding_rice_idx_minus1 + 1;
2001
2.61M
    const int rem = abs_decode(lc, c_rice_param);
2002
2003
2.61M
    return rem;
2004
2.61M
}
2005
2006
static int coeff_sign_flag_decode(VVCLocalContext *lc)
2007
39.5M
{
2008
39.5M
    return get_cabac_bypass(&lc->ep->cc);
2009
39.5M
}
2010
2011
//9.3.4.2.10 Derivation process of ctxInc for the syntax element coeff_sign_flag for transform skip mode
2012
static int coeff_sign_flag_ts_decode(VVCLocalContext *lc, const CodingUnit *cu, const ResidualCoding *rc, const int xc, const int yc)
2013
2.47M
{
2014
2.47M
    const TransformBlock *tb = rc->tb;
2015
2.47M
    const int w              = tb->tb_width;
2016
2.47M
    const int *level         = rc->coeff_sign_level + yc * w + xc;
2017
2.47M
    const int left_sign      = xc ? level[-1] : 0;
2018
2.47M
    const int above_sign     = yc ? level[-w] : 0;
2019
2.47M
    const int bdpcm_flag     = cu->bdpcm_flag[tb->c_idx];
2020
2.47M
    int inc;
2021
2022
2.47M
    if (left_sign == -above_sign)
2023
909k
        inc = bdpcm_flag ? 3 : 0;
2024
1.56M
    else if (left_sign >= 0 && above_sign >= 0)
2025
863k
        inc = bdpcm_flag ? 4 : 1;
2026
697k
    else
2027
697k
        inc = bdpcm_flag ? 5 : 2;
2028
2.47M
    return GET_CABAC(COEFF_SIGN_FLAG + inc);
2029
2.47M
}
2030
2031
static int abs_level_gt1_flag_ts_decode(VVCLocalContext *lc, const CodingUnit *cu, const ResidualCoding *rc, const int xc, const int yc)
2032
2.47M
{
2033
2.47M
    const TransformBlock *tb = rc->tb;
2034
2.47M
    const int *sig_coeff_flag = rc->sig_coeff_flag + yc * tb->tb_width + xc;
2035
2.47M
    int inc;
2036
2037
2.47M
    if (cu->bdpcm_flag[tb->c_idx]) {
2038
1.29M
        inc = 67;
2039
1.29M
    } else {
2040
1.18M
        const int l = xc > 0 ? sig_coeff_flag[-1] : 0;
2041
1.18M
        const int a = yc > 0 ? sig_coeff_flag[-tb->tb_width] : 0;
2042
1.18M
        inc = 64 + a + l;
2043
1.18M
    }
2044
2.47M
    return GET_CABAC(ABS_LEVEL_GTX_FLAG + inc);
2045
2.47M
}
2046
2047
static int abs_level_gtx_flag_ts_decode(VVCLocalContext *lc, const int j)
2048
367k
{
2049
367k
    const int inc = 67 + j;
2050
367k
    return GET_CABAC(ABS_LEVEL_GTX_FLAG + inc);
2051
367k
}
2052
2053
static const uint8_t qstate_translate_table[][2] = {
2054
    { 0, 2 }, { 2, 0 }, { 1, 3 }, { 3, 1 }
2055
};
2056
2057
static int dec_abs_level_decode(VVCLocalContext *lc, const ResidualCoding *rc,
2058
    const int xc, const int yc, int *abs_level)
2059
1.69M
{
2060
1.69M
    const int c_rice_param  = abs_get_rice_param(lc, rc, xc, yc, 0);
2061
1.69M
    const int dec_abs_level =  abs_decode(lc, c_rice_param);
2062
1.69M
    const int zero_pos      = (rc->qstate < 2 ? 1 : 2) << c_rice_param;
2063
2064
1.69M
    *abs_level = 0;
2065
1.69M
    if (dec_abs_level != zero_pos) {
2066
1.46M
        *abs_level = dec_abs_level;
2067
1.46M
        if (dec_abs_level < zero_pos)
2068
1.04M
            *abs_level += 1;
2069
1.46M
    }
2070
1.69M
    return dec_abs_level;
2071
1.69M
}
2072
2073
static void ep_update_hist(EntryPoint *ep, ResidualCoding *rc,
2074
    const int remainder, const int addin)
2075
3.58M
{
2076
3.58M
    int *stat = ep->stat_coeff + rc->tb->c_idx;
2077
3.58M
    if (rc->update_hist && remainder > 0) {
2078
97.5k
        *stat = (*stat + av_log2(remainder) + addin) >> 1;
2079
97.5k
        rc->update_hist = 0;
2080
97.5k
    }
2081
3.58M
}
2082
2083
static void init_residual_coding(const VVCLocalContext *lc, ResidualCoding *rc,
2084
    const int log2_zo_tb_width, const int log2_zo_tb_height,
2085
    TransformBlock *tb)
2086
6.25M
{
2087
6.25M
    const VVCSPS *sps = lc->fc->ps.sps;
2088
6.25M
    int log2_sb_w     = (FFMIN(log2_zo_tb_width, log2_zo_tb_height ) < 2 ? 1 : 2 );
2089
6.25M
    int log2_sb_h     = log2_sb_w;
2090
2091
6.25M
    if ( log2_zo_tb_width + log2_zo_tb_height > 3 ) {
2092
6.24M
        if ( log2_zo_tb_width < 2 ) {
2093
68.2k
            log2_sb_w = log2_zo_tb_width;
2094
68.2k
            log2_sb_h = 4 - log2_sb_w;
2095
6.17M
        } else if ( log2_zo_tb_height < 2 ) {
2096
36.6k
            log2_sb_h = log2_zo_tb_height;
2097
36.6k
            log2_sb_w = 4 - log2_sb_h;
2098
36.6k
        }
2099
6.24M
    }
2100
6.25M
    rc->log2_sb_w = log2_sb_w;
2101
6.25M
    rc->log2_sb_h = log2_sb_h;
2102
6.25M
    rc->num_sb_coeff   = 1 << (log2_sb_w + log2_sb_h);
2103
6.25M
    rc->last_sub_block = ( 1 << ( log2_zo_tb_width + log2_zo_tb_height - (log2_sb_w + log2_sb_h))) - 1;
2104
6.25M
    rc->hist_value     = sps->r->sps_persistent_rice_adaptation_enabled_flag ? (1 << lc->ep->stat_coeff[tb->c_idx]) : 0;
2105
6.25M
    rc->update_hist    = sps->r->sps_persistent_rice_adaptation_enabled_flag ? 1 : 0;
2106
6.25M
    rc->rem_bins_pass1 = (( 1 << ( log2_zo_tb_width + log2_zo_tb_height)) * 7 ) >> 2;
2107
2108
2109
6.25M
    rc->sb_scan_x_off = ff_vvc_diag_scan_x[log2_zo_tb_width - log2_sb_w][log2_zo_tb_height - log2_sb_h];
2110
6.25M
    rc->sb_scan_y_off = ff_vvc_diag_scan_y[log2_zo_tb_width - log2_sb_w][log2_zo_tb_height - log2_sb_h];
2111
2112
6.25M
    rc->scan_x_off = ff_vvc_diag_scan_x[log2_sb_w][log2_sb_h];
2113
6.25M
    rc->scan_y_off = ff_vvc_diag_scan_y[log2_sb_w][log2_sb_h];
2114
2115
6.25M
    rc->infer_sb_cbf = 1;
2116
2117
6.25M
    rc->width_in_sbs  = (1 << (log2_zo_tb_width - log2_sb_w));
2118
6.25M
    rc->height_in_sbs = (1 << (log2_zo_tb_height - log2_sb_h));
2119
6.25M
    rc->nb_sbs        = rc->width_in_sbs * rc->height_in_sbs;
2120
2121
6.25M
    rc->last_scan_pos = rc->num_sb_coeff;
2122
6.25M
    rc->qstate        = 0;
2123
2124
6.25M
    rc->tb = tb;
2125
6.25M
}
2126
2127
static int residual_ts_coding_subblock(VVCLocalContext *lc, ResidualCoding* rc, const int i)
2128
709k
{
2129
709k
    const CodingUnit *cu   = lc->cu;
2130
709k
    TransformBlock *tb     = rc->tb;
2131
709k
    const int bdpcm_flag   = cu->bdpcm_flag[tb->c_idx];
2132
709k
    const int xs           = rc->sb_scan_x_off[i];
2133
709k
    const int ys           = rc->sb_scan_y_off[i];
2134
709k
    uint8_t *sb_coded_flag = rc->sb_coded_flag + ys * rc->width_in_sbs + xs;
2135
709k
    int infer_sb_sig_coeff_flag = 1;
2136
709k
    int last_scan_pos_pass1 = -1, last_scan_pos_pass2 = -1, n;
2137
709k
    int abs_level_gtx_flag[MAX_SUB_BLOCK_SIZE * MAX_SUB_BLOCK_SIZE];
2138
709k
    int abs_level_pass2[MAX_SUB_BLOCK_SIZE * MAX_SUB_BLOCK_SIZE];       ///< AbsLevelPass2
2139
2140
709k
    if (i != rc->last_sub_block || !rc->infer_sb_cbf)
2141
61.9k
        *sb_coded_flag = sb_coded_flag_decode(lc, sb_coded_flag, rc, xs, ys);
2142
647k
    else
2143
647k
        *sb_coded_flag = 1;
2144
709k
    if (*sb_coded_flag && i < rc->last_sub_block)
2145
20.8k
        rc->infer_sb_cbf = 0;
2146
2147
    //first scan pass
2148
10.7M
    for (n = 0; n < rc->num_sb_coeff && rc->rem_bins_pass1 >= 4; n++) {
2149
10.0M
        const int xc = (xs << rc->log2_sb_w) + rc->scan_x_off[n];
2150
10.0M
        const int yc = (ys << rc->log2_sb_h) + rc->scan_y_off[n];
2151
10.0M
        const int off = yc * tb->tb_width + xc;
2152
10.0M
        int *sig_coeff_flag   = rc->sig_coeff_flag + off;
2153
10.0M
        int *abs_level_pass1  = rc->abs_level_pass1 + off;
2154
10.0M
        int *coeff_sign_level = rc->coeff_sign_level + off;
2155
10.0M
        int par_level_flag    = 0;
2156
2157
10.0M
        abs_level_gtx_flag[n] = 0;
2158
10.0M
        last_scan_pos_pass1 = n;
2159
10.0M
        if (*sb_coded_flag && (n != rc->num_sb_coeff - 1 || !infer_sb_sig_coeff_flag)) {
2160
9.50M
            *sig_coeff_flag = sig_coeff_flag_decode(lc, rc, xc, yc);
2161
9.50M
            rc->rem_bins_pass1--;
2162
9.50M
            if (*sig_coeff_flag)
2163
2.39M
                infer_sb_sig_coeff_flag = 0;
2164
9.50M
        } else {
2165
558k
            *sig_coeff_flag = (n == rc->num_sb_coeff - 1) && infer_sb_sig_coeff_flag && *sb_coded_flag;
2166
558k
        }
2167
10.0M
        *coeff_sign_level = 0;
2168
10.0M
        if (*sig_coeff_flag) {
2169
2.47M
            *coeff_sign_level = 1 - 2 * coeff_sign_flag_ts_decode(lc, cu, rc, xc, yc);
2170
2.47M
            abs_level_gtx_flag[n] = abs_level_gt1_flag_ts_decode(lc, cu, rc, xc, yc);
2171
2.47M
            rc->rem_bins_pass1 -= 2;
2172
2.47M
            if (abs_level_gtx_flag[n]) {
2173
1.63M
                par_level_flag = par_level_flag_ts_decode(lc);
2174
1.63M
                rc->rem_bins_pass1--;
2175
1.63M
            }
2176
2.47M
        }
2177
10.0M
        *abs_level_pass1 = *sig_coeff_flag + par_level_flag + abs_level_gtx_flag[n];
2178
10.0M
    }
2179
2180
    //greater than x scan pass
2181
6.21M
    for (n = 0; n < rc->num_sb_coeff && rc->rem_bins_pass1 >= 4; n++) {
2182
5.50M
        const int xc  = (xs << rc->log2_sb_w) + rc->scan_x_off[n];
2183
5.50M
        const int yc  = (ys << rc->log2_sb_h) + rc->scan_y_off[n];
2184
5.50M
        const int off = yc * tb->tb_width + xc;
2185
2186
5.50M
        abs_level_pass2[n] = rc->abs_level_pass1[off];
2187
5.87M
        for (int j = 1; j < 5 && abs_level_gtx_flag[n]; j++) {
2188
367k
            abs_level_gtx_flag[n] = abs_level_gtx_flag_ts_decode(lc, j);
2189
367k
            abs_level_pass2[n] += abs_level_gtx_flag[n] << 1;
2190
367k
            rc->rem_bins_pass1--;
2191
367k
        }
2192
5.50M
        last_scan_pos_pass2 = n;
2193
5.50M
    }
2194
2195
    /* remainder scan pass */
2196
12.0M
    for (n = 0; n < rc->num_sb_coeff; n++) {
2197
11.3M
        const int xc  = (xs << rc->log2_sb_w) + rc->scan_x_off[n];
2198
11.3M
        const int yc  = (ys << rc->log2_sb_h) + rc->scan_y_off[n];
2199
11.3M
        const int off = yc * tb->tb_width + xc;
2200
11.3M
        const int *abs_level_pass1 = rc->abs_level_pass1 + off;
2201
11.3M
        int *abs_level             = rc->abs_level + off;
2202
11.3M
        int *coeff_sign_level      = rc->coeff_sign_level + off;
2203
11.3M
        int abs_remainder          = 0;
2204
2205
11.3M
        if ((n <= last_scan_pos_pass2 && abs_level_pass2[n] >= 10) ||
2206
11.3M
            (n > last_scan_pos_pass2 && n <= last_scan_pos_pass1 &&
2207
4.55M
            *abs_level_pass1 >= 2) ||
2208
9.99M
            (n > last_scan_pos_pass1 &&  *sb_coded_flag))
2209
2.61M
            abs_remainder = abs_remainder_ts_decode(lc, rc, xc, yc);
2210
11.3M
        if (n <= last_scan_pos_pass2) {
2211
5.50M
            *abs_level = abs_level_pass2[n] + 2 * abs_remainder;
2212
5.83M
        } else if (n <= last_scan_pos_pass1) {
2213
4.55M
            *abs_level = *abs_level_pass1 + 2 * abs_remainder;
2214
4.55M
        } else {
2215
1.27M
            *abs_level = abs_remainder;
2216
1.27M
            if (abs_remainder) {
2217
                //n > lastScanPosPass1
2218
814k
                *coeff_sign_level = 1 - 2 * coeff_sign_flag_decode(lc);
2219
814k
            }
2220
1.27M
        }
2221
11.3M
        if (!bdpcm_flag && n <= last_scan_pos_pass1) {
2222
6.19M
            const int left  = xc > 0 ? abs_level[-1] : 0;
2223
6.19M
            const int above = yc > 0 ? abs_level[-tb->tb_width] : 0;
2224
6.19M
            const int pred  = FFMAX(left, above);
2225
2226
6.19M
            if (*abs_level == 1 && pred > 0)
2227
214k
                *abs_level = pred;
2228
5.97M
            else if (*abs_level > 0 && *abs_level <= pred)
2229
122k
                (*abs_level)--;
2230
6.19M
        }
2231
11.3M
        if (*abs_level) {
2232
3.28M
            tb->coeffs[off] = *coeff_sign_level * *abs_level;
2233
3.28M
            tb->max_scan_x = FFMAX(xc, tb->max_scan_x);
2234
3.28M
            tb->max_scan_y = FFMAX(yc, tb->max_scan_y);
2235
3.28M
            tb->min_scan_x = FFMIN(xc, tb->min_scan_x);
2236
3.28M
            tb->min_scan_y = FFMIN(yc, tb->min_scan_y);
2237
8.05M
        } else {
2238
8.05M
            tb->coeffs[off] = 0;
2239
8.05M
        }
2240
11.3M
    }
2241
2242
709k
    return 0;
2243
709k
}
2244
2245
static int hls_residual_ts_coding(VVCLocalContext *lc, TransformBlock *tb)
2246
661k
{
2247
661k
    ResidualCoding rc;
2248
661k
    tb->min_scan_x = tb->min_scan_y = INT_MAX;
2249
661k
    init_residual_coding(lc, &rc, tb->log2_tb_width, tb->log2_tb_height, tb);
2250
1.37M
    for (int i = 0; i <= rc.last_sub_block; i++) {
2251
709k
        int ret = residual_ts_coding_subblock(lc, &rc, i);
2252
709k
        if (ret < 0)
2253
0
            return ret;
2254
709k
    }
2255
2256
661k
    return 0;
2257
661k
}
2258
2259
static inline int residual_coding_subblock(VVCLocalContext *lc, ResidualCoding *rc, const int i)
2260
9.94M
{
2261
9.94M
    const H266RawSliceHeader *rsh = lc->sc->sh.r;
2262
9.94M
    TransformBlock *tb            = rc->tb;
2263
9.94M
    int first_sig_scan_pos_sb, last_sig_scan_pos_sb;
2264
9.94M
    int first_pos_mode0, first_pos_mode1;
2265
9.94M
    int infer_sb_dc_sig_coeff_flag = 0;
2266
9.94M
    int n, sig_hidden_flag, sum = 0;
2267
9.94M
    int abs_level_gt2_flag[MAX_SUB_BLOCK_SIZE * MAX_SUB_BLOCK_SIZE];
2268
9.94M
    const int start_qstate_sb = rc->qstate;
2269
9.94M
    const int xs = rc->sb_scan_x_off[i];
2270
9.94M
    const int ys = rc->sb_scan_y_off[i];
2271
9.94M
    uint8_t *sb_coded_flag = rc->sb_coded_flag + ys * rc->width_in_sbs + xs;
2272
2273
2274
9.94M
    av_assert0(rc->num_sb_coeff <= MAX_SUB_BLOCK_SIZE * MAX_SUB_BLOCK_SIZE);
2275
9.94M
    if (i < rc->last_sub_block && i > 0) {
2276
3.39M
        *sb_coded_flag = sb_coded_flag_decode(lc, sb_coded_flag, rc, xs, ys);
2277
3.39M
        infer_sb_dc_sig_coeff_flag = 1;
2278
6.54M
    } else {
2279
6.54M
        *sb_coded_flag = 1;
2280
6.54M
    }
2281
9.94M
    if (*sb_coded_flag && (xs > 3 || ys > 3) && !tb->c_idx)
2282
358k
        lc->parse.mts_zero_out_sig_coeff_flag = 0;
2283
2284
9.94M
    if (!*sb_coded_flag)
2285
1.23M
        return 0;
2286
2287
8.70M
    first_sig_scan_pos_sb = rc->num_sb_coeff;
2288
8.70M
    last_sig_scan_pos_sb = -1;
2289
8.70M
    first_pos_mode0 = (i == rc->last_sub_block ? rc->last_scan_pos : rc->num_sb_coeff -1);
2290
8.70M
    first_pos_mode1 = first_pos_mode0;
2291
89.6M
    for (n = first_pos_mode0; n >= 0 && rc->rem_bins_pass1 >= 4; n--) {
2292
80.9M
        const int xc   = (xs << rc->log2_sb_w) + rc->scan_x_off[n];
2293
80.9M
        const int yc   = (ys << rc->log2_sb_h) + rc->scan_y_off[n];
2294
80.9M
        const int last = (xc == rc->last_significant_coeff_x && yc == rc->last_significant_coeff_y);
2295
80.9M
        int *abs_level_pass1 = rc->abs_level_pass1 + yc * tb->tb_width + xc;
2296
80.9M
        int *sig_coeff_flag  = rc->sig_coeff_flag + yc * tb->tb_width + xc;
2297
2298
80.9M
        if ((n > 0 || !infer_sb_dc_sig_coeff_flag ) && !last) {
2299
74.5M
            *sig_coeff_flag = sig_coeff_flag_decode(lc, rc, xc, yc);
2300
74.5M
            rc->rem_bins_pass1--;
2301
74.5M
            if (*sig_coeff_flag)
2302
32.8M
                infer_sb_dc_sig_coeff_flag = 0;
2303
74.5M
        } else {
2304
6.37M
            *sig_coeff_flag = last || (!rc->scan_x_off[n] && !rc ->scan_y_off[n] &&
2305
779k
                infer_sb_dc_sig_coeff_flag);
2306
6.37M
        }
2307
80.9M
        *abs_level_pass1 = 0;
2308
80.9M
        if (*sig_coeff_flag) {
2309
39.1M
            int abs_level_gt1_flag, par_level_flag = 0;
2310
39.1M
            const int inc = get_gtx_flag_inc(rc, xc, yc, last);
2311
39.1M
            abs_level_gt1_flag = abs_level_gtx_flag_decode(lc, inc);
2312
39.1M
            rc->rem_bins_pass1--;
2313
39.1M
            if (abs_level_gt1_flag) {
2314
7.96M
                par_level_flag = par_level_flag_decode(lc, inc);
2315
7.96M
                abs_level_gt2_flag[n] = abs_level_gtx_flag_decode(lc, inc + 32);
2316
7.96M
                rc->rem_bins_pass1 -= 2;
2317
31.2M
            } else {
2318
31.2M
                abs_level_gt2_flag[n] = 0;
2319
31.2M
            }
2320
39.1M
            if (last_sig_scan_pos_sb == -1)
2321
8.62M
                last_sig_scan_pos_sb = n;
2322
39.1M
            first_sig_scan_pos_sb = n;
2323
2324
39.1M
            *abs_level_pass1 =
2325
39.1M
                1  + par_level_flag + abs_level_gt1_flag + (abs_level_gt2_flag[n] << 1);
2326
41.7M
        } else {
2327
41.7M
            abs_level_gt2_flag[n] = 0;
2328
41.7M
        }
2329
2330
80.9M
        if (rsh->sh_dep_quant_used_flag)
2331
52.4M
            rc->qstate = qstate_translate_table[rc->qstate][*abs_level_pass1 & 1];
2332
2333
80.9M
        first_pos_mode1 = n - 1;
2334
80.9M
    }
2335
89.6M
    for (n = first_pos_mode0; n > first_pos_mode1; n--) {
2336
80.9M
        const int xc = (xs << rc->log2_sb_w) + rc->scan_x_off[n];
2337
80.9M
        const int yc = (ys << rc->log2_sb_h) + rc->scan_y_off[n];
2338
80.9M
        const int *abs_level_pass1 = rc->abs_level_pass1 + yc * tb->tb_width + xc;
2339
80.9M
        int *abs_level             = rc->abs_level + yc * tb->tb_width + xc;
2340
2341
80.9M
        *abs_level = *abs_level_pass1;
2342
80.9M
        if (abs_level_gt2_flag[n]) {
2343
1.88M
            const int abs_remainder = abs_remainder_decode(lc, rc, xc, yc);
2344
1.88M
            ep_update_hist(lc->ep, rc, abs_remainder, 2);
2345
1.88M
            *abs_level += 2 * abs_remainder;
2346
1.88M
        }
2347
80.9M
    }
2348
10.4M
    for (n = first_pos_mode1; n >= 0; n--) {
2349
1.69M
        const int xc   = (xs << rc->log2_sb_w) + rc->scan_x_off[n];
2350
1.69M
        const int yc   = (ys << rc->log2_sb_h) + rc->scan_y_off[n];
2351
1.69M
        int *abs_level = rc->abs_level + yc * tb->tb_width + xc;
2352
2353
1.69M
        if (*sb_coded_flag) {
2354
1.69M
            const int dec_abs_level = dec_abs_level_decode(lc, rc, xc, yc, abs_level);
2355
1.69M
            ep_update_hist(lc->ep, rc, dec_abs_level, 0);
2356
1.69M
        }
2357
1.69M
        if (*abs_level > 0) {
2358
1.46M
            if (last_sig_scan_pos_sb == -1)
2359
450
                last_sig_scan_pos_sb = n;
2360
1.46M
            first_sig_scan_pos_sb = n;
2361
1.46M
        }
2362
1.69M
        if (rsh->sh_dep_quant_used_flag)
2363
841k
            rc->qstate = qstate_translate_table[rc->qstate][*abs_level & 1];
2364
1.69M
    }
2365
8.70M
    sig_hidden_flag = rsh->sh_sign_data_hiding_used_flag &&
2366
3.82M
        (last_sig_scan_pos_sb - first_sig_scan_pos_sb > 3 ? 1 : 0);
2367
2368
8.70M
    if (rsh->sh_dep_quant_used_flag)
2369
4.63M
        rc->qstate = start_qstate_sb;
2370
8.70M
    n = (i == rc->last_sub_block ? rc->last_scan_pos : rc->num_sb_coeff -1);
2371
91.3M
    for (/* nothing */; n >= 0; n--) {
2372
82.6M
        int trans_coeff_level;
2373
82.6M
        const int xc  = (xs << rc->log2_sb_w) + rc->scan_x_off[n];
2374
82.6M
        const int yc  = (ys << rc->log2_sb_h) + rc->scan_y_off[n];
2375
82.6M
        const int off = yc * tb->tb_width + xc;
2376
82.6M
        const int *abs_level = rc->abs_level + off;
2377
2378
82.6M
        if (*abs_level > 0) {
2379
40.6M
            int sign = 1;
2380
40.6M
            if (!sig_hidden_flag || (n != first_sig_scan_pos_sb))
2381
38.7M
                sign = 1 - 2 * coeff_sign_flag_decode(lc);
2382
40.6M
            if (rsh->sh_dep_quant_used_flag) {
2383
23.8M
                trans_coeff_level = (2 * *abs_level - (rc->qstate > 1)) * sign;
2384
23.8M
            } else {
2385
16.7M
                trans_coeff_level = *abs_level * sign;
2386
16.7M
                if (sig_hidden_flag) {
2387
12.8M
                    sum += *abs_level;
2388
12.8M
                    if (n == first_sig_scan_pos_sb && (sum % 2))
2389
1.02M
                        trans_coeff_level = -trans_coeff_level;
2390
12.8M
                }
2391
16.7M
            }
2392
40.6M
            tb->coeffs[off] = trans_coeff_level;
2393
40.6M
            tb->max_scan_x = FFMAX(xc, tb->max_scan_x);
2394
40.6M
            tb->max_scan_y = FFMAX(yc, tb->max_scan_y);
2395
40.6M
        }
2396
82.6M
        if (rsh->sh_dep_quant_used_flag)
2397
53.3M
            rc->qstate = qstate_translate_table[rc->qstate][*abs_level & 1];
2398
82.6M
    }
2399
2400
8.70M
    return 0;
2401
9.94M
}
2402
2403
static void derive_last_scan_pos(ResidualCoding *rc)
2404
5.59M
{
2405
5.59M
    int xc, yc, xs, ys;
2406
190M
    do {
2407
190M
        if (!rc->last_scan_pos) {
2408
8.02M
            rc->last_scan_pos = rc->num_sb_coeff;
2409
8.02M
            rc->last_sub_block--;
2410
8.02M
        }
2411
190M
        rc->last_scan_pos--;
2412
190M
        xs = rc->sb_scan_x_off[rc->last_sub_block];
2413
190M
        ys = rc->sb_scan_y_off[rc->last_sub_block];
2414
190M
        xc = (xs << rc->log2_sb_w) + rc->scan_x_off[rc->last_scan_pos];
2415
190M
        yc = (ys << rc->log2_sb_h) + rc->scan_y_off[rc->last_scan_pos];
2416
190M
    } while ((xc != rc->last_significant_coeff_x) || (yc != rc->last_significant_coeff_y));
2417
5.59M
}
2418
2419
static void last_significant_coeff_x_y_decode(ResidualCoding *rc, VVCLocalContext *lc,
2420
    const int log2_zo_tb_width, const int log2_zo_tb_height)
2421
5.59M
{
2422
5.59M
    const H266RawSliceHeader *rsh = lc->sc->sh.r;
2423
5.59M
    const TransformBlock *tb      = rc->tb;
2424
5.59M
    int last_significant_coeff_x, last_significant_coeff_y;
2425
2426
5.59M
    last_significant_coeff_x = last_significant_coeff_x_prefix_decode(lc,
2427
5.59M
            tb->log2_tb_width, log2_zo_tb_width, tb->c_idx);
2428
2429
5.59M
    last_significant_coeff_y = last_significant_coeff_y_prefix_decode(lc,
2430
5.59M
        tb->log2_tb_height, log2_zo_tb_height, tb->c_idx);
2431
2432
5.59M
    if (last_significant_coeff_x > 3) {
2433
837k
        int suffix = last_sig_coeff_suffix_decode(lc, last_significant_coeff_x);
2434
837k
        last_significant_coeff_x = (1 << ((last_significant_coeff_x >> 1) - 1)) *
2435
837k
            (2 + (last_significant_coeff_x & 1)) + suffix;
2436
837k
    }
2437
5.59M
    if (last_significant_coeff_y > 3) {
2438
404k
        int suffix = last_sig_coeff_suffix_decode(lc, last_significant_coeff_y);
2439
404k
        last_significant_coeff_y = (1 << ((last_significant_coeff_y >> 1) - 1)) *
2440
404k
            (2 + (last_significant_coeff_y & 1)) + suffix;
2441
404k
    }
2442
5.59M
    if (rsh->sh_reverse_last_sig_coeff_flag) {
2443
14.3k
        last_significant_coeff_x = (1 << log2_zo_tb_width) - 1 - last_significant_coeff_x;
2444
14.3k
        last_significant_coeff_y = (1 << log2_zo_tb_height) - 1 - last_significant_coeff_y;
2445
14.3k
    }
2446
5.59M
    rc->last_significant_coeff_x = last_significant_coeff_x;
2447
5.59M
    rc->last_significant_coeff_y = last_significant_coeff_y;
2448
5.59M
}
2449
2450
static int hls_residual_coding(VVCLocalContext *lc, TransformBlock *tb)
2451
5.59M
{
2452
5.59M
    const VVCSPS *sps        = lc->fc->ps.sps;
2453
5.59M
    const CodingUnit *cu     = lc->cu;
2454
5.59M
    const int log2_tb_width  = tb->log2_tb_width;
2455
5.59M
    const int log2_tb_height = tb->log2_tb_height;
2456
5.59M
    const int c_idx          = tb->c_idx;
2457
5.59M
    int log2_zo_tb_width, log2_zo_tb_height;
2458
5.59M
    ResidualCoding rc;
2459
2460
5.59M
    if (sps->r->sps_mts_enabled_flag && cu->sbt_flag && !c_idx && log2_tb_width == 5 && log2_tb_height < 6)
2461
0
        log2_zo_tb_width = 4;
2462
5.59M
    else
2463
5.59M
        log2_zo_tb_width = FFMIN(log2_tb_width, 5 );
2464
2465
5.59M
    if (sps->r->sps_mts_enabled_flag && cu->sbt_flag && !c_idx && log2_tb_width < 6 && log2_tb_height == 5 )
2466
0
        log2_zo_tb_height = 4;
2467
5.59M
    else
2468
5.59M
        log2_zo_tb_height = FFMIN(log2_tb_height, 5);
2469
2470
5.59M
    init_residual_coding(lc, &rc, log2_zo_tb_width, log2_zo_tb_height, tb);
2471
5.59M
    last_significant_coeff_x_y_decode(&rc, lc, log2_zo_tb_width, log2_zo_tb_height);
2472
5.59M
    derive_last_scan_pos(&rc);
2473
2474
5.59M
    if (!rc.last_sub_block && log2_tb_width >= 2 && log2_tb_height >= 2 && !tb->ts && rc.last_scan_pos > 0)
2475
3.70M
        lc->parse.lfnst_dc_only = 0;
2476
5.59M
    if ((rc.last_sub_block > 0 && log2_tb_width >= 2 && log2_tb_height >= 2 ) ||
2477
4.64M
         (rc.last_scan_pos > 7 && (log2_tb_width == 2 || log2_tb_width == 3 ) &&
2478
1.28M
         log2_tb_width == log2_tb_height))
2479
2.08M
        lc->parse.lfnst_zero_out_sig_coeff_flag = 0;
2480
5.59M
    if ((rc.last_sub_block > 0 || rc.last_scan_pos > 0 ) && !c_idx)
2481
3.60M
        lc->parse.mts_dc_only = 0;
2482
2483
5.59M
    memset(tb->coeffs, 0, tb->tb_width * tb->tb_height * sizeof(*tb->coeffs));
2484
5.59M
    memset(rc.abs_level, 0, tb->tb_width * tb->tb_height * sizeof(rc.abs_level[0]));
2485
5.59M
    memset(rc.sb_coded_flag, 0, rc.nb_sbs);
2486
5.59M
    memset(rc.abs_level_pass1, 0, tb->tb_width * tb->tb_height * sizeof(rc.abs_level_pass1[0]));
2487
5.59M
    memset(rc.sig_coeff_flag, 0, tb->tb_width * tb->tb_height * sizeof(rc.sig_coeff_flag[0]));
2488
2489
15.5M
    for (int i = rc.last_sub_block; i >= 0; i--) {
2490
9.94M
        int ret = residual_coding_subblock(lc, &rc, i);
2491
9.94M
        if (ret < 0)
2492
0
            return ret;
2493
9.94M
    }
2494
2495
5.59M
    return 0;
2496
5.59M
}
2497
2498
int ff_vvc_residual_coding(VVCLocalContext *lc, TransformBlock *tb)
2499
6.25M
{
2500
6.25M
    const H266RawSliceHeader *rsh = lc->sc->sh.r;
2501
6.25M
    const int ts                  = !rsh->sh_ts_residual_coding_disabled_flag && tb->ts;
2502
2503
6.25M
    return ts ? hls_residual_ts_coding(lc, tb) : hls_residual_coding(lc, tb);
2504
6.25M
}
2505
2506
int ff_vvc_cu_coded_flag(VVCLocalContext *lc)
2507
294k
{
2508
294k
    return GET_CABAC(CU_CODED_FLAG);
2509
294k
}
2510
2511
int ff_vvc_sbt_flag(VVCLocalContext *lc)
2512
23.2k
{
2513
23.2k
    const int w   = lc->cu->cb_width;
2514
23.2k
    const int h   = lc->cu->cb_height;
2515
23.2k
    const int inc = w * h <= 256;
2516
23.2k
    return GET_CABAC(CU_SBT_FLAG + inc);
2517
23.2k
}
2518
2519
int ff_vvc_sbt_quad_flag(VVCLocalContext *lc)
2520
0
{
2521
0
    return GET_CABAC(CU_SBT_QUAD_FLAG);
2522
0
}
2523
2524
int ff_vvc_sbt_horizontal_flag(VVCLocalContext *lc)
2525
5.67k
{
2526
5.67k
    const int w = lc->cu->cb_width;
2527
5.67k
    const int h = lc->cu->cb_height;
2528
5.67k
    const int inc = (w == h) ? 0 : ((w < h) ? 1 : 2);
2529
5.67k
    return GET_CABAC(CU_SBT_HORIZONTAL_FLAG + inc);
2530
5.67k
}
2531
2532
int ff_vvc_sbt_pos_flag(VVCLocalContext *lc)
2533
6.62k
{
2534
6.62k
    return GET_CABAC(CU_SBT_POS_FLAG);
2535
6.62k
}
2536
2537
int ff_vvc_lfnst_idx(VVCLocalContext *lc, const int inc)
2538
306k
{
2539
306k
    if (!GET_CABAC(LFNST_IDX + inc))
2540
72.2k
        return 0;
2541
233k
    if (!GET_CABAC(LFNST_IDX + 2))
2542
178k
        return 1;
2543
55.7k
    return 2;
2544
233k
}
2545
2546
int ff_vvc_mts_idx(VVCLocalContext *lc)
2547
574k
{
2548
574k
    int i;
2549
1.06M
    for (i = 0; i < 4; i++) {
2550
1.06M
        if (!GET_CABAC(MTS_IDX + i))
2551
574k
            return i;
2552
1.06M
    }
2553
88
    return i;
2554
574k
}
2555
2556
int ff_vvc_end_of_slice_flag_decode(VVCLocalContext *lc)
2557
1.79M
{
2558
1.79M
    return get_cabac_terminate(&lc->ep->cc);
2559
1.79M
}
2560
2561
int ff_vvc_end_of_tile_one_bit(VVCLocalContext *lc)
2562
0
{
2563
0
    return get_cabac_terminate(&lc->ep->cc);
2564
0
}
2565
2566
int ff_vvc_end_of_subset_one_bit(VVCLocalContext *lc)
2567
0
{
2568
0
    return get_cabac_terminate(&lc->ep->cc);
2569
0
}