Coverage Report

Created: 2026-04-01 07:42

next uncovered line (L), next uncovered region (R), next uncovered branch (B)
/src/ffmpeg/libavcodec/cabac_functions.h
Line
Count
Source
1
/*
2
 * H.26L/H.264/AVC/JVT/14496-10/... encoder/decoder
3
 * Copyright (c) 2003 Michael Niedermayer <michaelni@gmx.at>
4
 *
5
 * This file is part of FFmpeg.
6
 *
7
 * FFmpeg is free software; you can redistribute it and/or
8
 * modify it under the terms of the GNU Lesser General Public
9
 * License as published by the Free Software Foundation; either
10
 * version 2.1 of the License, or (at your option) any later version.
11
 *
12
 * FFmpeg is distributed in the hope that it will be useful,
13
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
14
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
15
 * Lesser General Public License for more details.
16
 *
17
 * You should have received a copy of the GNU Lesser General Public
18
 * License along with FFmpeg; if not, write to the Free Software
19
 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
20
 */
21
22
/**
23
 * @file
24
 * Context Adaptive Binary Arithmetic Coder inline functions
25
 */
26
27
#ifndef AVCODEC_CABAC_FUNCTIONS_H
28
#define AVCODEC_CABAC_FUNCTIONS_H
29
30
#include <stddef.h>
31
#include <stdint.h>
32
33
#include "libavutil/attributes.h"
34
#include "libavutil/intmath.h"
35
#include "cabac.h"
36
#include "config.h"
37
38
#ifndef UNCHECKED_BITSTREAM_READER
39
#define UNCHECKED_BITSTREAM_READER !CONFIG_SAFE_BITSTREAM_READER
40
#endif
41
42
#if ARCH_AARCH64
43
#   include "aarch64/cabac.h"
44
#endif
45
#if ARCH_ARM
46
#   include "arm/cabac.h"
47
#endif
48
#if ARCH_X86
49
#   include "x86/cabac.h"
50
#endif
51
#if ARCH_MIPS
52
#   include "mips/cabac.h"
53
#endif
54
#if ARCH_LOONGARCH64
55
#   include "loongarch/cabac.h"
56
#endif
57
58
static const uint8_t * const ff_h264_norm_shift = ff_h264_cabac_tables + H264_NORM_SHIFT_OFFSET;
59
static const uint8_t * const ff_h264_lps_range = ff_h264_cabac_tables + H264_LPS_RANGE_OFFSET;
60
static const uint8_t * const ff_h264_mlps_state = ff_h264_cabac_tables + H264_MLPS_STATE_OFFSET;
61
static const uint8_t * const ff_h264_last_coeff_flag_offset_8x8 = ff_h264_cabac_tables + H264_LAST_COEFF_FLAG_OFFSET_8x8_OFFSET;
62
63
#if !defined(get_cabac_bypass) || !defined(get_cabac_terminate)
64
151M
static void refill(CABACContext *c){
65
151M
#if CABAC_BITS == 16
66
151M
        c->low+= (c->bytestream[0]<<9) + (c->bytestream[1]<<1);
67
#else
68
        c->low+= c->bytestream[0]<<1;
69
#endif
70
151M
    c->low -= CABAC_MASK;
71
#if !UNCHECKED_BITSTREAM_READER
72
138M
    if (c->bytestream < c->bytestream_end)
73
5.45M
#endif
74
19.1M
        c->bytestream += CABAC_BITS / 8;
75
151M
}
h264_slice.c:refill
Line
Count
Source
64
48.3k
static void refill(CABACContext *c){
65
48.3k
#if CABAC_BITS == 16
66
48.3k
        c->low+= (c->bytestream[0]<<9) + (c->bytestream[1]<<1);
67
#else
68
        c->low+= c->bytestream[0]<<1;
69
#endif
70
48.3k
    c->low -= CABAC_MASK;
71
48.3k
#if !UNCHECKED_BITSTREAM_READER
72
48.3k
    if (c->bytestream < c->bytestream_end)
73
44.1k
#endif
74
44.1k
        c->bytestream += CABAC_BITS / 8;
75
48.3k
}
h264_cabac.c:refill
Line
Count
Source
64
13.7M
static void refill(CABACContext *c){
65
13.7M
#if CABAC_BITS == 16
66
13.7M
        c->low+= (c->bytestream[0]<<9) + (c->bytestream[1]<<1);
67
#else
68
        c->low+= c->bytestream[0]<<1;
69
#endif
70
13.7M
    c->low -= CABAC_MASK;
71
#if !UNCHECKED_BITSTREAM_READER
72
    if (c->bytestream < c->bytestream_end)
73
#endif
74
13.7M
        c->bytestream += CABAC_BITS / 8;
75
13.7M
}
Unexecuted instantiation: hevcdec.c:refill
cabac.c:refill
Line
Count
Source
64
138M
static void refill(CABACContext *c){
65
138M
#if CABAC_BITS == 16
66
138M
        c->low+= (c->bytestream[0]<<9) + (c->bytestream[1]<<1);
67
#else
68
        c->low+= c->bytestream[0]<<1;
69
#endif
70
138M
    c->low -= CABAC_MASK;
71
138M
#if !UNCHECKED_BITSTREAM_READER
72
138M
    if (c->bytestream < c->bytestream_end)
73
5.40M
#endif
74
5.40M
        c->bytestream += CABAC_BITS / 8;
75
138M
}
76
#endif
77
78
#ifndef get_cabac_terminate
79
34.3M
static inline void renorm_cabac_decoder_once(CABACContext *c){
80
34.3M
    int shift= (uint32_t)(c->range - 0x100)>>31;
81
34.3M
    c->range<<= shift;
82
34.3M
    c->low  <<= shift;
83
34.3M
    if(!(c->low & CABAC_MASK))
84
63.1k
        refill(c);
85
34.3M
}
h264_slice.c:renorm_cabac_decoder_once
Line
Count
Source
79
24.2M
static inline void renorm_cabac_decoder_once(CABACContext *c){
80
24.2M
    int shift= (uint32_t)(c->range - 0x100)>>31;
81
24.2M
    c->range<<= shift;
82
24.2M
    c->low  <<= shift;
83
24.2M
    if(!(c->low & CABAC_MASK))
84
48.3k
        refill(c);
85
24.2M
}
h264_cabac.c:renorm_cabac_decoder_once
Line
Count
Source
79
1.65M
static inline void renorm_cabac_decoder_once(CABACContext *c){
80
1.65M
    int shift= (uint32_t)(c->range - 0x100)>>31;
81
1.65M
    c->range<<= shift;
82
1.65M
    c->low  <<= shift;
83
1.65M
    if(!(c->low & CABAC_MASK))
84
1.87k
        refill(c);
85
1.65M
}
Unexecuted instantiation: hevcdec.c:renorm_cabac_decoder_once
cabac.c:renorm_cabac_decoder_once
Line
Count
Source
79
8.49M
static inline void renorm_cabac_decoder_once(CABACContext *c){
80
8.49M
    int shift= (uint32_t)(c->range - 0x100)>>31;
81
8.49M
    c->range<<= shift;
82
8.49M
    c->low  <<= shift;
83
8.49M
    if(!(c->low & CABAC_MASK))
84
12.9k
        refill(c);
85
8.49M
}
86
#endif
87
88
#ifndef get_cabac_inline
89
75.2M
static void refill2(CABACContext *c){
90
75.2M
    int i;
91
75.2M
    unsigned x;
92
75.2M
#if !HAVE_FAST_CLZ
93
75.2M
    x= c->low ^ (c->low-1);
94
75.2M
    i= 7 - ff_h264_norm_shift[x>>(CABAC_BITS-1)];
95
#else
96
    i = ff_ctz(c->low) - CABAC_BITS;
97
#endif
98
99
75.2M
    x= -CABAC_MASK;
100
101
75.2M
#if CABAC_BITS == 16
102
75.2M
        x+= (c->bytestream[0]<<9) + (c->bytestream[1]<<1);
103
#else
104
        x+= c->bytestream[0]<<1;
105
#endif
106
107
75.2M
    c->low += x<<i;
108
#if !UNCHECKED_BITSTREAM_READER
109
33.1M
    if (c->bytestream < c->bytestream_end)
110
2.99M
#endif
111
45.0M
        c->bytestream += CABAC_BITS/8;
112
75.2M
}
Unexecuted instantiation: h264_slice.c:refill2
h264_cabac.c:refill2
Line
Count
Source
89
42.0M
static void refill2(CABACContext *c){
90
42.0M
    int i;
91
42.0M
    unsigned x;
92
42.0M
#if !HAVE_FAST_CLZ
93
42.0M
    x= c->low ^ (c->low-1);
94
42.0M
    i= 7 - ff_h264_norm_shift[x>>(CABAC_BITS-1)];
95
#else
96
    i = ff_ctz(c->low) - CABAC_BITS;
97
#endif
98
99
42.0M
    x= -CABAC_MASK;
100
101
42.0M
#if CABAC_BITS == 16
102
42.0M
        x+= (c->bytestream[0]<<9) + (c->bytestream[1]<<1);
103
#else
104
        x+= c->bytestream[0]<<1;
105
#endif
106
107
42.0M
    c->low += x<<i;
108
#if !UNCHECKED_BITSTREAM_READER
109
    if (c->bytestream < c->bytestream_end)
110
#endif
111
42.0M
        c->bytestream += CABAC_BITS/8;
112
42.0M
}
Unexecuted instantiation: hevcdec.c:refill2
cabac.c:refill2
Line
Count
Source
89
33.1M
static void refill2(CABACContext *c){
90
33.1M
    int i;
91
33.1M
    unsigned x;
92
33.1M
#if !HAVE_FAST_CLZ
93
33.1M
    x= c->low ^ (c->low-1);
94
33.1M
    i= 7 - ff_h264_norm_shift[x>>(CABAC_BITS-1)];
95
#else
96
    i = ff_ctz(c->low) - CABAC_BITS;
97
#endif
98
99
33.1M
    x= -CABAC_MASK;
100
101
33.1M
#if CABAC_BITS == 16
102
33.1M
        x+= (c->bytestream[0]<<9) + (c->bytestream[1]<<1);
103
#else
104
        x+= c->bytestream[0]<<1;
105
#endif
106
107
33.1M
    c->low += x<<i;
108
33.1M
#if !UNCHECKED_BITSTREAM_READER
109
33.1M
    if (c->bytestream < c->bytestream_end)
110
2.99M
#endif
111
2.99M
        c->bytestream += CABAC_BITS/8;
112
33.1M
}
113
#endif
114
115
#ifndef get_cabac_inline
116
5.56G
static av_always_inline int get_cabac_inline(CABACContext *c, uint8_t * const state){
117
5.56G
    int s = *state;
118
5.56G
    int RangeLPS= ff_h264_lps_range[2*(c->range&0xC0) + s];
119
5.56G
    int bit, lps_mask;
120
121
5.56G
    c->range -= RangeLPS;
122
5.56G
    lps_mask= ((c->range<<(CABAC_BITS+1)) - c->low)>>31;
123
124
5.56G
    c->low -= (c->range<<(CABAC_BITS+1)) & lps_mask;
125
5.56G
    c->range += (RangeLPS - c->range) & lps_mask;
126
127
5.56G
    s^=lps_mask;
128
5.56G
    *state= (ff_h264_mlps_state+128)[s];
129
5.56G
    bit= s&1;
130
131
5.56G
    lps_mask= ff_h264_norm_shift[c->range];
132
5.56G
    c->range<<= lps_mask;
133
5.56G
    c->low  <<= lps_mask;
134
5.56G
    if(!(c->low & CABAC_MASK))
135
75.2M
        refill2(c);
136
5.56G
    return bit;
137
5.56G
}
Unexecuted instantiation: h264_slice.c:get_cabac_inline
h264_cabac.c:get_cabac_inline
Line
Count
Source
116
1.29G
static av_always_inline int get_cabac_inline(CABACContext *c, uint8_t * const state){
117
1.29G
    int s = *state;
118
1.29G
    int RangeLPS= ff_h264_lps_range[2*(c->range&0xC0) + s];
119
1.29G
    int bit, lps_mask;
120
121
1.29G
    c->range -= RangeLPS;
122
1.29G
    lps_mask= ((c->range<<(CABAC_BITS+1)) - c->low)>>31;
123
124
1.29G
    c->low -= (c->range<<(CABAC_BITS+1)) & lps_mask;
125
1.29G
    c->range += (RangeLPS - c->range) & lps_mask;
126
127
1.29G
    s^=lps_mask;
128
1.29G
    *state= (ff_h264_mlps_state+128)[s];
129
1.29G
    bit= s&1;
130
131
1.29G
    lps_mask= ff_h264_norm_shift[c->range];
132
1.29G
    c->range<<= lps_mask;
133
1.29G
    c->low  <<= lps_mask;
134
1.29G
    if(!(c->low & CABAC_MASK))
135
42.0M
        refill2(c);
136
1.29G
    return bit;
137
1.29G
}
Unexecuted instantiation: hevcdec.c:get_cabac_inline
cabac.c:get_cabac_inline
Line
Count
Source
116
4.26G
static av_always_inline int get_cabac_inline(CABACContext *c, uint8_t * const state){
117
4.26G
    int s = *state;
118
4.26G
    int RangeLPS= ff_h264_lps_range[2*(c->range&0xC0) + s];
119
4.26G
    int bit, lps_mask;
120
121
4.26G
    c->range -= RangeLPS;
122
4.26G
    lps_mask= ((c->range<<(CABAC_BITS+1)) - c->low)>>31;
123
124
4.26G
    c->low -= (c->range<<(CABAC_BITS+1)) & lps_mask;
125
4.26G
    c->range += (RangeLPS - c->range) & lps_mask;
126
127
4.26G
    s^=lps_mask;
128
4.26G
    *state= (ff_h264_mlps_state+128)[s];
129
4.26G
    bit= s&1;
130
131
4.26G
    lps_mask= ff_h264_norm_shift[c->range];
132
4.26G
    c->range<<= lps_mask;
133
4.26G
    c->low  <<= lps_mask;
134
4.26G
    if(!(c->low & CABAC_MASK))
135
33.1M
        refill2(c);
136
4.26G
    return bit;
137
4.26G
}
138
#endif
139
140
157M
av_unused av_noinline static int get_cabac_noinline(CABACContext *c, uint8_t * const state){
141
157M
    return get_cabac_inline(c,state);
142
157M
}
Unexecuted instantiation: h264_slice.c:get_cabac_noinline
h264_cabac.c:get_cabac_noinline
Line
Count
Source
140
157M
av_unused av_noinline static int get_cabac_noinline(CABACContext *c, uint8_t * const state){
141
157M
    return get_cabac_inline(c,state);
142
157M
}
Unexecuted instantiation: hevcdec.c:get_cabac_noinline
Unexecuted instantiation: cabac.c:get_cabac_noinline
143
144
5.41G
av_unused static int get_cabac(CABACContext *c, uint8_t * const state){
145
5.41G
    return get_cabac_inline(c,state);
146
5.41G
}
Unexecuted instantiation: h264_slice.c:get_cabac
h264_cabac.c:get_cabac
Line
Count
Source
144
1.14G
av_unused static int get_cabac(CABACContext *c, uint8_t * const state){
145
1.14G
    return get_cabac_inline(c,state);
146
1.14G
}
Unexecuted instantiation: hevcdec.c:get_cabac
cabac.c:get_cabac
Line
Count
Source
144
4.26G
av_unused static int get_cabac(CABACContext *c, uint8_t * const state){
145
4.26G
    return get_cabac_inline(c,state);
146
4.26G
}
147
148
#ifndef get_cabac_bypass
149
2.24G
av_unused static int get_cabac_bypass(CABACContext *c){
150
2.24G
    int range;
151
2.24G
    c->low += c->low;
152
153
2.24G
    if(!(c->low & CABAC_MASK))
154
140M
        refill(c);
155
156
2.24G
    range= c->range<<(CABAC_BITS+1);
157
2.24G
    if(c->low < range){
158
1.93G
        return 0;
159
1.93G
    }else{
160
311M
        c->low -= range;
161
311M
        return 1;
162
311M
    }
163
2.24G
}
Unexecuted instantiation: h264_slice.c:get_cabac_bypass
h264_cabac.c:get_cabac_bypass
Line
Count
Source
149
42.1M
av_unused static int get_cabac_bypass(CABACContext *c){
150
42.1M
    int range;
151
42.1M
    c->low += c->low;
152
153
42.1M
    if(!(c->low & CABAC_MASK))
154
2.60M
        refill(c);
155
156
42.1M
    range= c->range<<(CABAC_BITS+1);
157
42.1M
    if(c->low < range){
158
25.3M
        return 0;
159
25.3M
    }else{
160
16.7M
        c->low -= range;
161
16.7M
        return 1;
162
16.7M
    }
163
42.1M
}
Unexecuted instantiation: hevcdec.c:get_cabac_bypass
cabac.c:get_cabac_bypass
Line
Count
Source
149
2.20G
av_unused static int get_cabac_bypass(CABACContext *c){
150
2.20G
    int range;
151
2.20G
    c->low += c->low;
152
153
2.20G
    if(!(c->low & CABAC_MASK))
154
137M
        refill(c);
155
156
2.20G
    range= c->range<<(CABAC_BITS+1);
157
2.20G
    if(c->low < range){
158
1.91G
        return 0;
159
1.91G
    }else{
160
294M
        c->low -= range;
161
294M
        return 1;
162
294M
    }
163
2.20G
}
164
#endif
165
166
#ifndef get_cabac_bypass_sign
167
178M
static av_always_inline int get_cabac_bypass_sign(CABACContext *c, int val){
168
178M
    int range, mask;
169
178M
    c->low += c->low;
170
171
178M
    if(!(c->low & CABAC_MASK))
172
11.1M
        refill(c);
173
174
178M
    range= c->range<<(CABAC_BITS+1);
175
178M
    c->low -= range;
176
178M
    mask= c->low >> 31;
177
178M
    range &= mask;
178
178M
    c->low += range;
179
178M
    return (val^mask)-mask;
180
178M
}
Unexecuted instantiation: h264_slice.c:get_cabac_bypass_sign
h264_cabac.c:get_cabac_bypass_sign
Line
Count
Source
167
177M
static av_always_inline int get_cabac_bypass_sign(CABACContext *c, int val){
168
177M
    int range, mask;
169
177M
    c->low += c->low;
170
171
177M
    if(!(c->low & CABAC_MASK))
172
11.1M
        refill(c);
173
174
177M
    range= c->range<<(CABAC_BITS+1);
175
177M
    c->low -= range;
176
177M
    mask= c->low >> 31;
177
177M
    range &= mask;
178
177M
    c->low += range;
179
177M
    return (val^mask)-mask;
180
177M
}
Unexecuted instantiation: hevcdec.c:get_cabac_bypass_sign
cabac.c:get_cabac_bypass_sign
Line
Count
Source
167
1.28M
static av_always_inline int get_cabac_bypass_sign(CABACContext *c, int val){
168
1.28M
    int range, mask;
169
1.28M
    c->low += c->low;
170
171
1.28M
    if(!(c->low & CABAC_MASK))
172
82.2k
        refill(c);
173
174
1.28M
    range= c->range<<(CABAC_BITS+1);
175
1.28M
    c->low -= range;
176
1.28M
    mask= c->low >> 31;
177
1.28M
    range &= mask;
178
1.28M
    c->low += range;
179
1.28M
    return (val^mask)-mask;
180
1.28M
}
181
#endif
182
183
/**
184
 * @return the number of bytes read or 0 if no end
185
 */
186
#ifndef get_cabac_terminate
187
34.5M
av_unused static int get_cabac_terminate(CABACContext *c){
188
34.5M
    c->range -= 2;
189
34.5M
    if(c->low < c->range<<(CABAC_BITS+1)){
190
34.3M
        renorm_cabac_decoder_once(c);
191
34.3M
        return 0;
192
34.3M
    }else{
193
209k
        return c->bytestream - c->bytestream_start;
194
209k
    }
195
34.5M
}
h264_slice.c:get_cabac_terminate
Line
Count
Source
187
24.3M
av_unused static int get_cabac_terminate(CABACContext *c){
188
24.3M
    c->range -= 2;
189
24.3M
    if(c->low < c->range<<(CABAC_BITS+1)){
190
24.2M
        renorm_cabac_decoder_once(c);
191
24.2M
        return 0;
192
24.2M
    }else{
193
99.9k
        return c->bytestream - c->bytestream_start;
194
99.9k
    }
195
24.3M
}
h264_cabac.c:get_cabac_terminate
Line
Count
Source
187
1.65M
av_unused static int get_cabac_terminate(CABACContext *c){
188
1.65M
    c->range -= 2;
189
1.65M
    if(c->low < c->range<<(CABAC_BITS+1)){
190
1.65M
        renorm_cabac_decoder_once(c);
191
1.65M
        return 0;
192
1.65M
    }else{
193
3.59k
        return c->bytestream - c->bytestream_start;
194
3.59k
    }
195
1.65M
}
Unexecuted instantiation: hevcdec.c:get_cabac_terminate
cabac.c:get_cabac_terminate
Line
Count
Source
187
8.60M
av_unused static int get_cabac_terminate(CABACContext *c){
188
8.60M
    c->range -= 2;
189
8.60M
    if(c->low < c->range<<(CABAC_BITS+1)){
190
8.49M
        renorm_cabac_decoder_once(c);
191
8.49M
        return 0;
192
8.49M
    }else{
193
105k
        return c->bytestream - c->bytestream_start;
194
105k
    }
195
8.60M
}
196
#endif
197
198
/**
199
 * Skip @p n bytes and reset the decoder.
200
 * @return the address of the first skipped byte or NULL if there's less than @p n bytes left
201
 */
202
#ifndef skip_bytes
203
33.3k
av_unused static const uint8_t* skip_bytes(CABACContext *c, int n) {
204
33.3k
    const uint8_t *ptr = c->bytestream;
205
206
33.3k
    if (c->low & 0x1)
207
3.38k
        ptr--;
208
33.3k
#if CABAC_BITS == 16
209
33.3k
    if (c->low & 0x1FF)
210
20.0k
        ptr--;
211
33.3k
#endif
212
33.3k
    if ((int) (c->bytestream_end - ptr) < n)
213
11.5k
        return NULL;
214
21.8k
    if (ff_init_cabac_decoder(c, ptr + n, c->bytestream_end - ptr - n) < 0)
215
136
        return NULL;
216
217
21.6k
    return ptr;
218
21.8k
}
Unexecuted instantiation: h264_slice.c:skip_bytes
Unexecuted instantiation: h264_cabac.c:skip_bytes
hevcdec.c:skip_bytes
Line
Count
Source
203
3.67k
av_unused static const uint8_t* skip_bytes(CABACContext *c, int n) {
204
3.67k
    const uint8_t *ptr = c->bytestream;
205
206
3.67k
    if (c->low & 0x1)
207
207
        ptr--;
208
3.67k
#if CABAC_BITS == 16
209
3.67k
    if (c->low & 0x1FF)
210
2.37k
        ptr--;
211
3.67k
#endif
212
3.67k
    if ((int) (c->bytestream_end - ptr) < n)
213
2.13k
        return NULL;
214
1.54k
    if (ff_init_cabac_decoder(c, ptr + n, c->bytestream_end - ptr - n) < 0)
215
5
        return NULL;
216
217
1.53k
    return ptr;
218
1.54k
}
cabac.c:skip_bytes
Line
Count
Source
203
29.6k
av_unused static const uint8_t* skip_bytes(CABACContext *c, int n) {
204
29.6k
    const uint8_t *ptr = c->bytestream;
205
206
29.6k
    if (c->low & 0x1)
207
3.18k
        ptr--;
208
29.6k
#if CABAC_BITS == 16
209
29.6k
    if (c->low & 0x1FF)
210
17.7k
        ptr--;
211
29.6k
#endif
212
29.6k
    if ((int) (c->bytestream_end - ptr) < n)
213
9.39k
        return NULL;
214
20.2k
    if (ff_init_cabac_decoder(c, ptr + n, c->bytestream_end - ptr - n) < 0)
215
131
        return NULL;
216
217
20.1k
    return ptr;
218
20.2k
}
219
#endif
220
221
#endif /* AVCODEC_CABAC_FUNCTIONS_H */