Coverage Report

Created: 2026-04-01 07:49

next uncovered line (L), next uncovered region (R), next uncovered branch (B)
/src/x264/common/cabac.c
Line
Count
Source
1
/*****************************************************************************
2
 * cabac.c: arithmetic coder
3
 *****************************************************************************
4
 * Copyright (C) 2003-2025 x264 project
5
 *
6
 * Authors: Laurent Aimar <fenrir@via.ecp.fr>
7
 *          Loren Merritt <lorenm@u.washington.edu>
8
 *          Fiona Glaser <fiona@x264.com>
9
 *
10
 * This program is free software; you can redistribute it and/or modify
11
 * it under the terms of the GNU General Public License as published by
12
 * the Free Software Foundation; either version 2 of the License, or
13
 * (at your option) any later version.
14
 *
15
 * This program is distributed in the hope that it will be useful,
16
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
17
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
18
 * GNU General Public License for more details.
19
 *
20
 * You should have received a copy of the GNU General Public License
21
 * along with this program; if not, write to the Free Software
22
 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02111, USA.
23
 *
24
 * This program is also available under a commercial proprietary license.
25
 * For more information, contact us at licensing@x264.com.
26
 *****************************************************************************/
27
28
#include "common.h"
29
30
static uint8_t cabac_contexts[4][QP_MAX_SPEC+1][1024];
31
32
void x264_cabac_init( x264_t *h )
33
0
{
34
0
    int ctx_count = CHROMA444 ? 1024 : 460;
35
0
    for( int i = 0; i < 4; i++ )
36
0
    {
37
0
        const int8_t (*cabac_context_init)[1024][2] = i == 0 ? &x264_cabac_context_init_I
38
0
                                                             : &x264_cabac_context_init_PB[i-1];
39
0
        for( int qp = 0; qp <= QP_MAX_SPEC; qp++ )
40
0
            for( int j = 0; j < ctx_count; j++ )
41
0
            {
42
0
                int state = x264_clip3( (((*cabac_context_init)[j][0] * qp) >> 4) + (*cabac_context_init)[j][1], 1, 126 );
43
0
                cabac_contexts[i][qp][j] = (X264_MIN( state, 127-state ) << 1) | (state >> 6);
44
0
            }
45
0
    }
46
0
}
Unexecuted instantiation: x264_8_cabac_init
Unexecuted instantiation: x264_10_cabac_init
47
48
void x264_cabac_context_init( x264_t *h, x264_cabac_t *cb, int i_slice_type, int i_qp, int i_model )
49
0
{
50
0
    memcpy( cb->state, cabac_contexts[i_slice_type == SLICE_TYPE_I ? 0 : i_model + 1][i_qp], CHROMA444 ? 1024 : 460 );
51
0
}
Unexecuted instantiation: x264_8_cabac_context_init
Unexecuted instantiation: x264_10_cabac_context_init
52
53
void x264_cabac_encode_init_core( x264_cabac_t *cb )
54
0
{
55
0
    cb->i_low   = 0;
56
0
    cb->i_range = 0x01FE;
57
0
    cb->i_queue = -9; // the first bit will be shifted away and not written
58
0
    cb->i_bytes_outstanding = 0;
59
0
}
Unexecuted instantiation: x264_8_cabac_encode_init_core
Unexecuted instantiation: x264_10_cabac_encode_init_core
60
61
void x264_cabac_encode_init( x264_cabac_t *cb, uint8_t *p_data, uint8_t *p_end )
62
0
{
63
0
    x264_cabac_encode_init_core( cb );
64
0
    cb->p_start = p_data;
65
0
    cb->p       = p_data;
66
0
    cb->p_end   = p_end;
67
0
}
Unexecuted instantiation: x264_8_cabac_encode_init
Unexecuted instantiation: x264_10_cabac_encode_init
68
69
static inline void cabac_putbyte( x264_cabac_t *cb )
70
0
{
71
0
    if( cb->i_queue >= 0 )
72
0
    {
73
0
        int out = cb->i_low >> (cb->i_queue+10);
74
0
        cb->i_low &= (0x400<<cb->i_queue)-1;
75
0
        cb->i_queue -= 8;
76
77
0
        if( (out & 0xff) == 0xff )
78
0
            cb->i_bytes_outstanding++;
79
0
        else
80
0
        {
81
0
            int carry = out >> 8;
82
0
            int bytes_outstanding = cb->i_bytes_outstanding;
83
            // this can't modify before the beginning of the stream because
84
            // that would correspond to a probability > 1.
85
            // it will write before the beginning of the stream, which is ok
86
            // because a slice header always comes before cabac data.
87
            // this can't carry beyond the one byte, because any 0xff bytes
88
            // are in bytes_outstanding and thus not written yet.
89
0
            cb->p[-1] += carry;
90
0
            while( bytes_outstanding > 0 )
91
0
            {
92
0
                *(cb->p++) = (uint8_t)(carry-1);
93
0
                bytes_outstanding--;
94
0
            }
95
0
            *(cb->p++) = (uint8_t)out;
96
0
            cb->i_bytes_outstanding = 0;
97
0
        }
98
0
    }
99
0
}
100
101
static inline void cabac_encode_renorm( x264_cabac_t *cb )
102
0
{
103
0
    int shift = x264_cabac_renorm_shift[cb->i_range>>3];
104
0
    cb->i_range <<= shift;
105
0
    cb->i_low   <<= shift;
106
0
    cb->i_queue  += shift;
107
0
    cabac_putbyte( cb );
108
0
}
109
110
/* Making custom versions of this function, even in asm, for the cases where
111
 * b is known to be 0 or 1, proved to be somewhat useful on x86_32 with GCC 3.4
112
 * but nearly useless with GCC 4.3 and worse than useless on x86_64. */
113
void x264_cabac_encode_decision_c( x264_cabac_t *cb, int i_ctx, int b )
114
0
{
115
0
    int i_state = cb->state[i_ctx];
116
0
    int i_range_lps = x264_cabac_range_lps[i_state>>1][(cb->i_range>>6)-4];
117
0
    cb->i_range -= i_range_lps;
118
0
    if( b != (i_state & 1) )
119
0
    {
120
0
        cb->i_low += cb->i_range;
121
0
        cb->i_range = i_range_lps;
122
0
    }
123
0
    cb->state[i_ctx] = x264_cabac_transition[i_state][b];
124
0
    cabac_encode_renorm( cb );
125
0
}
Unexecuted instantiation: x264_8_cabac_encode_decision_c
Unexecuted instantiation: x264_10_cabac_encode_decision_c
126
127
/* Note: b is negated for this function */
128
void x264_cabac_encode_bypass_c( x264_cabac_t *cb, int b )
129
0
{
130
0
    cb->i_low <<= 1;
131
0
    cb->i_low += b & cb->i_range;
132
0
    cb->i_queue += 1;
133
0
    cabac_putbyte( cb );
134
0
}
Unexecuted instantiation: x264_8_cabac_encode_bypass_c
Unexecuted instantiation: x264_10_cabac_encode_bypass_c
135
136
static const int bypass_lut[16] =
137
{
138
    -1,      0x2,     0x14,     0x68,     0x1d0,     0x7a0,     0x1f40,     0x7e80,
139
    0x1fd00, 0x7fa00, 0x1ff400, 0x7fe800, 0x1ffd000, 0x7ffa000, 0x1fff4000, 0x7ffe8000
140
};
141
142
void x264_cabac_encode_ue_bypass( x264_cabac_t *cb, int exp_bits, int val )
143
0
{
144
0
    uint32_t v = val + (1<<exp_bits);
145
0
    int k = 31 - x264_clz( v );
146
0
    uint32_t x = ((uint32_t)bypass_lut[k-exp_bits]<<exp_bits) + v;
147
0
    k = 2*k+1-exp_bits;
148
0
    int i = ((k-1)&7)+1;
149
0
    do {
150
0
        k -= i;
151
0
        cb->i_low <<= i;
152
0
        cb->i_low += ((x>>k)&0xff) * cb->i_range;
153
0
        cb->i_queue += i;
154
0
        cabac_putbyte( cb );
155
0
        i = 8;
156
0
    } while( k > 0 );
157
0
}
Unexecuted instantiation: x264_8_cabac_encode_ue_bypass
Unexecuted instantiation: x264_10_cabac_encode_ue_bypass
158
159
void x264_cabac_encode_terminal_c( x264_cabac_t *cb )
160
0
{
161
0
    cb->i_range -= 2;
162
0
    cabac_encode_renorm( cb );
163
0
}
Unexecuted instantiation: x264_8_cabac_encode_terminal_c
Unexecuted instantiation: x264_10_cabac_encode_terminal_c
164
165
void x264_cabac_encode_flush( x264_t *h, x264_cabac_t *cb )
166
0
{
167
0
    cb->i_low += cb->i_range - 2;
168
0
    cb->i_low |= 1;
169
0
    cb->i_low <<= 9;
170
0
    cb->i_queue += 9;
171
0
    cabac_putbyte( cb );
172
0
    cabac_putbyte( cb );
173
0
    cb->i_low <<= -cb->i_queue;
174
0
    cb->i_low |= (0x35a4e4f5 >> (h->i_frame & 31) & 1) << 10;
175
0
    cb->i_queue = 0;
176
0
    cabac_putbyte( cb );
177
178
0
    while( cb->i_bytes_outstanding > 0 )
179
0
    {
180
0
        *(cb->p++) = 0xff;
181
0
        cb->i_bytes_outstanding--;
182
0
    }
183
0
}
Unexecuted instantiation: x264_8_cabac_encode_flush
Unexecuted instantiation: x264_10_cabac_encode_flush
184