Coverage Report

Created: 2026-05-16 06:41

next uncovered line (L), next uncovered region (R), next uncovered branch (B)
/work/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
169
{
34
169
    int ctx_count = CHROMA444 ? 1024 : 460;
35
845
    for( int i = 0; i < 4; i++ )
36
676
    {
37
676
        const int8_t (*cabac_context_init)[1024][2] = i == 0 ? &x264_cabac_context_init_I
38
676
                                                             : &x264_cabac_context_init_PB[i-1];
39
35.8k
        for( int qp = 0; qp <= QP_MAX_SPEC; qp++ )
40
16.2M
            for( int j = 0; j < ctx_count; j++ )
41
16.1M
            {
42
16.1M
                int state = x264_clip3( (((*cabac_context_init)[j][0] * qp) >> 4) + (*cabac_context_init)[j][1], 1, 126 );
43
16.1M
                cabac_contexts[i][qp][j] = (X264_MIN( state, 127-state ) << 1) | (state >> 6);
44
16.1M
            }
45
676
    }
46
169
}
x264_8_cabac_init
Line
Count
Source
33
169
{
34
169
    int ctx_count = CHROMA444 ? 1024 : 460;
35
845
    for( int i = 0; i < 4; i++ )
36
676
    {
37
676
        const int8_t (*cabac_context_init)[1024][2] = i == 0 ? &x264_cabac_context_init_I
38
676
                                                             : &x264_cabac_context_init_PB[i-1];
39
35.8k
        for( int qp = 0; qp <= QP_MAX_SPEC; qp++ )
40
16.2M
            for( int j = 0; j < ctx_count; j++ )
41
16.1M
            {
42
16.1M
                int state = x264_clip3( (((*cabac_context_init)[j][0] * qp) >> 4) + (*cabac_context_init)[j][1], 1, 126 );
43
16.1M
                cabac_contexts[i][qp][j] = (X264_MIN( state, 127-state ) << 1) | (state >> 6);
44
16.1M
            }
45
676
    }
46
169
}
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
169
{
50
169
    memcpy( cb->state, cabac_contexts[i_slice_type == SLICE_TYPE_I ? 0 : i_model + 1][i_qp], CHROMA444 ? 1024 : 460 );
51
169
}
x264_8_cabac_context_init
Line
Count
Source
49
169
{
50
169
    memcpy( cb->state, cabac_contexts[i_slice_type == SLICE_TYPE_I ? 0 : i_model + 1][i_qp], CHROMA444 ? 1024 : 460 );
51
169
}
Unexecuted instantiation: x264_10_cabac_context_init
52
53
void x264_cabac_encode_init_core( x264_cabac_t *cb )
54
169
{
55
169
    cb->i_low   = 0;
56
169
    cb->i_range = 0x01FE;
57
169
    cb->i_queue = -9; // the first bit will be shifted away and not written
58
169
    cb->i_bytes_outstanding = 0;
59
169
}
x264_8_cabac_encode_init_core
Line
Count
Source
54
169
{
55
169
    cb->i_low   = 0;
56
169
    cb->i_range = 0x01FE;
57
169
    cb->i_queue = -9; // the first bit will be shifted away and not written
58
169
    cb->i_bytes_outstanding = 0;
59
169
}
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
169
{
63
169
    x264_cabac_encode_init_core( cb );
64
169
    cb->p_start = p_data;
65
169
    cb->p       = p_data;
66
169
    cb->p_end   = p_end;
67
169
}
x264_8_cabac_encode_init
Line
Count
Source
62
169
{
63
169
    x264_cabac_encode_init_core( cb );
64
169
    cb->p_start = p_data;
65
169
    cb->p       = p_data;
66
169
    cb->p_end   = p_end;
67
169
}
Unexecuted instantiation: x264_10_cabac_encode_init
68
69
static inline void cabac_putbyte( x264_cabac_t *cb )
70
367k
{
71
367k
    if( cb->i_queue >= 0 )
72
22.9k
    {
73
22.9k
        int out = cb->i_low >> (cb->i_queue+10);
74
22.9k
        cb->i_low &= (0x400<<cb->i_queue)-1;
75
22.9k
        cb->i_queue -= 8;
76
77
22.9k
        if( (out & 0xff) == 0xff )
78
82
            cb->i_bytes_outstanding++;
79
22.8k
        else
80
22.8k
        {
81
22.8k
            int carry = out >> 8;
82
22.8k
            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
22.8k
            cb->p[-1] += carry;
90
22.9k
            while( bytes_outstanding > 0 )
91
81
            {
92
81
                *(cb->p++) = (uint8_t)(carry-1);
93
81
                bytes_outstanding--;
94
81
            }
95
22.8k
            *(cb->p++) = (uint8_t)out;
96
22.8k
            cb->i_bytes_outstanding = 0;
97
22.8k
        }
98
22.9k
    }
99
367k
}
100
101
static inline void cabac_encode_renorm( x264_cabac_t *cb )
102
342k
{
103
342k
    int shift = x264_cabac_renorm_shift[cb->i_range>>3];
104
342k
    cb->i_range <<= shift;
105
342k
    cb->i_low   <<= shift;
106
342k
    cb->i_queue  += shift;
107
342k
    cabac_putbyte( cb );
108
342k
}
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
299k
{
115
299k
    int i_state = cb->state[i_ctx];
116
299k
    int i_range_lps = x264_cabac_range_lps[i_state>>1][(cb->i_range>>6)-4];
117
299k
    cb->i_range -= i_range_lps;
118
299k
    if( b != (i_state & 1) )
119
18.6k
    {
120
18.6k
        cb->i_low += cb->i_range;
121
18.6k
        cb->i_range = i_range_lps;
122
18.6k
    }
123
299k
    cb->state[i_ctx] = x264_cabac_transition[i_state][b];
124
299k
    cabac_encode_renorm( cb );
125
299k
}
x264_8_cabac_encode_decision_c
Line
Count
Source
114
299k
{
115
299k
    int i_state = cb->state[i_ctx];
116
299k
    int i_range_lps = x264_cabac_range_lps[i_state>>1][(cb->i_range>>6)-4];
117
299k
    cb->i_range -= i_range_lps;
118
299k
    if( b != (i_state & 1) )
119
18.6k
    {
120
18.6k
        cb->i_low += cb->i_range;
121
18.6k
        cb->i_range = i_range_lps;
122
18.6k
    }
123
299k
    cb->state[i_ctx] = x264_cabac_transition[i_state][b];
124
299k
    cabac_encode_renorm( cb );
125
299k
}
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
8.08k
{
130
8.08k
    cb->i_low <<= 1;
131
8.08k
    cb->i_low += b & cb->i_range;
132
8.08k
    cb->i_queue += 1;
133
8.08k
    cabac_putbyte( cb );
134
8.08k
}
x264_8_cabac_encode_bypass_c
Line
Count
Source
129
8.08k
{
130
8.08k
    cb->i_low <<= 1;
131
8.08k
    cb->i_low += b & cb->i_range;
132
8.08k
    cb->i_queue += 1;
133
8.08k
    cabac_putbyte( cb );
134
8.08k
}
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
7.98k
{
144
7.98k
    uint32_t v = val + (1<<exp_bits);
145
7.98k
    int k = 31 - x264_clz( v );
146
7.98k
    uint32_t x = ((uint32_t)bypass_lut[k-exp_bits]<<exp_bits) + v;
147
7.98k
    k = 2*k+1-exp_bits;
148
7.98k
    int i = ((k-1)&7)+1;
149
16.1k
    do {
150
16.1k
        k -= i;
151
16.1k
        cb->i_low <<= i;
152
16.1k
        cb->i_low += ((x>>k)&0xff) * cb->i_range;
153
16.1k
        cb->i_queue += i;
154
16.1k
        cabac_putbyte( cb );
155
16.1k
        i = 8;
156
16.1k
    } while( k > 0 );
157
7.98k
}
x264_8_cabac_encode_ue_bypass
Line
Count
Source
143
7.98k
{
144
7.98k
    uint32_t v = val + (1<<exp_bits);
145
7.98k
    int k = 31 - x264_clz( v );
146
7.98k
    uint32_t x = ((uint32_t)bypass_lut[k-exp_bits]<<exp_bits) + v;
147
7.98k
    k = 2*k+1-exp_bits;
148
7.98k
    int i = ((k-1)&7)+1;
149
16.1k
    do {
150
16.1k
        k -= i;
151
16.1k
        cb->i_low <<= i;
152
16.1k
        cb->i_low += ((x>>k)&0xff) * cb->i_range;
153
16.1k
        cb->i_queue += i;
154
16.1k
        cabac_putbyte( cb );
155
16.1k
        i = 8;
156
16.1k
    } while( k > 0 );
157
7.98k
}
Unexecuted instantiation: x264_10_cabac_encode_ue_bypass
158
159
void x264_cabac_encode_terminal_c( x264_cabac_t *cb )
160
42.5k
{
161
42.5k
    cb->i_range -= 2;
162
42.5k
    cabac_encode_renorm( cb );
163
42.5k
}
x264_8_cabac_encode_terminal_c
Line
Count
Source
160
42.5k
{
161
42.5k
    cb->i_range -= 2;
162
42.5k
    cabac_encode_renorm( cb );
163
42.5k
}
Unexecuted instantiation: x264_10_cabac_encode_terminal_c
164
165
void x264_cabac_encode_flush( x264_t *h, x264_cabac_t *cb )
166
169
{
167
169
    cb->i_low += cb->i_range - 2;
168
169
    cb->i_low |= 1;
169
169
    cb->i_low <<= 9;
170
169
    cb->i_queue += 9;
171
169
    cabac_putbyte( cb );
172
169
    cabac_putbyte( cb );
173
169
    cb->i_low <<= -cb->i_queue;
174
169
    cb->i_low |= (0x35a4e4f5 >> (h->i_frame & 31) & 1) << 10;
175
169
    cb->i_queue = 0;
176
169
    cabac_putbyte( cb );
177
178
170
    while( cb->i_bytes_outstanding > 0 )
179
1
    {
180
1
        *(cb->p++) = 0xff;
181
1
        cb->i_bytes_outstanding--;
182
1
    }
183
169
}
x264_8_cabac_encode_flush
Line
Count
Source
166
169
{
167
169
    cb->i_low += cb->i_range - 2;
168
169
    cb->i_low |= 1;
169
169
    cb->i_low <<= 9;
170
169
    cb->i_queue += 9;
171
169
    cabac_putbyte( cb );
172
169
    cabac_putbyte( cb );
173
169
    cb->i_low <<= -cb->i_queue;
174
169
    cb->i_low |= (0x35a4e4f5 >> (h->i_frame & 31) & 1) << 10;
175
169
    cb->i_queue = 0;
176
169
    cabac_putbyte( cb );
177
178
170
    while( cb->i_bytes_outstanding > 0 )
179
1
    {
180
1
        *(cb->p++) = 0xff;
181
1
        cb->i_bytes_outstanding--;
182
1
    }
183
169
}
Unexecuted instantiation: x264_10_cabac_encode_flush
184