Coverage Report

Created: 2026-01-16 07:48

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
142M
static void refill(CABACContext *c){
65
142M
#if CABAC_BITS == 16
66
142M
        c->low+= (c->bytestream[0]<<9) + (c->bytestream[1]<<1);
67
#else
68
        c->low+= c->bytestream[0]<<1;
69
#endif
70
142M
    c->low -= CABAC_MASK;
71
#if !UNCHECKED_BITSTREAM_READER
72
129M
    if (c->bytestream < c->bytestream_end)
73
6.42M
#endif
74
18.7M
        c->bytestream += CABAC_BITS / 8;
75
142M
}
h264_slice.c:refill
Line
Count
Source
64
34.5k
static void refill(CABACContext *c){
65
34.5k
#if CABAC_BITS == 16
66
34.5k
        c->low+= (c->bytestream[0]<<9) + (c->bytestream[1]<<1);
67
#else
68
        c->low+= c->bytestream[0]<<1;
69
#endif
70
34.5k
    c->low -= CABAC_MASK;
71
34.5k
#if !UNCHECKED_BITSTREAM_READER
72
34.5k
    if (c->bytestream < c->bytestream_end)
73
31.5k
#endif
74
31.5k
        c->bytestream += CABAC_BITS / 8;
75
34.5k
}
h264_cabac.c:refill
Line
Count
Source
64
12.3M
static void refill(CABACContext *c){
65
12.3M
#if CABAC_BITS == 16
66
12.3M
        c->low+= (c->bytestream[0]<<9) + (c->bytestream[1]<<1);
67
#else
68
        c->low+= c->bytestream[0]<<1;
69
#endif
70
12.3M
    c->low -= CABAC_MASK;
71
#if !UNCHECKED_BITSTREAM_READER
72
    if (c->bytestream < c->bytestream_end)
73
#endif
74
12.3M
        c->bytestream += CABAC_BITS / 8;
75
12.3M
}
Unexecuted instantiation: hevcdec.c:refill
cabac.c:refill
Line
Count
Source
64
129M
static void refill(CABACContext *c){
65
129M
#if CABAC_BITS == 16
66
129M
        c->low+= (c->bytestream[0]<<9) + (c->bytestream[1]<<1);
67
#else
68
        c->low+= c->bytestream[0]<<1;
69
#endif
70
129M
    c->low -= CABAC_MASK;
71
129M
#if !UNCHECKED_BITSTREAM_READER
72
129M
    if (c->bytestream < c->bytestream_end)
73
6.39M
#endif
74
6.39M
        c->bytestream += CABAC_BITS / 8;
75
129M
}
76
#endif
77
78
#ifndef get_cabac_terminate
79
32.0M
static inline void renorm_cabac_decoder_once(CABACContext *c){
80
32.0M
    int shift= (uint32_t)(c->range - 0x100)>>31;
81
32.0M
    c->range<<= shift;
82
32.0M
    c->low  <<= shift;
83
32.0M
    if(!(c->low & CABAC_MASK))
84
49.7k
        refill(c);
85
32.0M
}
h264_slice.c:renorm_cabac_decoder_once
Line
Count
Source
79
21.7M
static inline void renorm_cabac_decoder_once(CABACContext *c){
80
21.7M
    int shift= (uint32_t)(c->range - 0x100)>>31;
81
21.7M
    c->range<<= shift;
82
21.7M
    c->low  <<= shift;
83
21.7M
    if(!(c->low & CABAC_MASK))
84
34.5k
        refill(c);
85
21.7M
}
h264_cabac.c:renorm_cabac_decoder_once
Line
Count
Source
79
1.44M
static inline void renorm_cabac_decoder_once(CABACContext *c){
80
1.44M
    int shift= (uint32_t)(c->range - 0x100)>>31;
81
1.44M
    c->range<<= shift;
82
1.44M
    c->low  <<= shift;
83
1.44M
    if(!(c->low & CABAC_MASK))
84
1.87k
        refill(c);
85
1.44M
}
Unexecuted instantiation: hevcdec.c:renorm_cabac_decoder_once
cabac.c:renorm_cabac_decoder_once
Line
Count
Source
79
8.77M
static inline void renorm_cabac_decoder_once(CABACContext *c){
80
8.77M
    int shift= (uint32_t)(c->range - 0x100)>>31;
81
8.77M
    c->range<<= shift;
82
8.77M
    c->low  <<= shift;
83
8.77M
    if(!(c->low & CABAC_MASK))
84
13.3k
        refill(c);
85
8.77M
}
86
#endif
87
88
#ifndef get_cabac_inline
89
70.4M
static void refill2(CABACContext *c){
90
70.4M
    int i;
91
70.4M
    unsigned x;
92
70.4M
#if !HAVE_FAST_CLZ
93
70.4M
    x= c->low ^ (c->low-1);
94
70.4M
    i= 7 - ff_h264_norm_shift[x>>(CABAC_BITS-1)];
95
#else
96
    i = ff_ctz(c->low) - CABAC_BITS;
97
#endif
98
99
70.4M
    x= -CABAC_MASK;
100
101
70.4M
#if CABAC_BITS == 16
102
70.4M
        x+= (c->bytestream[0]<<9) + (c->bytestream[1]<<1);
103
#else
104
        x+= c->bytestream[0]<<1;
105
#endif
106
107
70.4M
    c->low += x<<i;
108
#if !UNCHECKED_BITSTREAM_READER
109
33.1M
    if (c->bytestream < c->bytestream_end)
110
3.41M
#endif
111
40.6M
        c->bytestream += CABAC_BITS/8;
112
70.4M
}
Unexecuted instantiation: h264_slice.c:refill2
h264_cabac.c:refill2
Line
Count
Source
89
37.2M
static void refill2(CABACContext *c){
90
37.2M
    int i;
91
37.2M
    unsigned x;
92
37.2M
#if !HAVE_FAST_CLZ
93
37.2M
    x= c->low ^ (c->low-1);
94
37.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
37.2M
    x= -CABAC_MASK;
100
101
37.2M
#if CABAC_BITS == 16
102
37.2M
        x+= (c->bytestream[0]<<9) + (c->bytestream[1]<<1);
103
#else
104
        x+= c->bytestream[0]<<1;
105
#endif
106
107
37.2M
    c->low += x<<i;
108
#if !UNCHECKED_BITSTREAM_READER
109
    if (c->bytestream < c->bytestream_end)
110
#endif
111
37.2M
        c->bytestream += CABAC_BITS/8;
112
37.2M
}
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
3.41M
#endif
111
3.41M
        c->bytestream += CABAC_BITS/8;
112
33.1M
}
113
#endif
114
115
#ifndef get_cabac_inline
116
5.31G
static av_always_inline int get_cabac_inline(CABACContext *c, uint8_t * const state){
117
5.31G
    int s = *state;
118
5.31G
    int RangeLPS= ff_h264_lps_range[2*(c->range&0xC0) + s];
119
5.31G
    int bit, lps_mask;
120
121
5.31G
    c->range -= RangeLPS;
122
5.31G
    lps_mask= ((c->range<<(CABAC_BITS+1)) - c->low)>>31;
123
124
5.31G
    c->low -= (c->range<<(CABAC_BITS+1)) & lps_mask;
125
5.31G
    c->range += (RangeLPS - c->range) & lps_mask;
126
127
5.31G
    s^=lps_mask;
128
5.31G
    *state= (ff_h264_mlps_state+128)[s];
129
5.31G
    bit= s&1;
130
131
5.31G
    lps_mask= ff_h264_norm_shift[c->range];
132
5.31G
    c->range<<= lps_mask;
133
5.31G
    c->low  <<= lps_mask;
134
5.31G
    if(!(c->low & CABAC_MASK))
135
70.4M
        refill2(c);
136
5.31G
    return bit;
137
5.31G
}
Unexecuted instantiation: h264_slice.c:get_cabac_inline
h264_cabac.c:get_cabac_inline
Line
Count
Source
116
1.18G
static av_always_inline int get_cabac_inline(CABACContext *c, uint8_t * const state){
117
1.18G
    int s = *state;
118
1.18G
    int RangeLPS= ff_h264_lps_range[2*(c->range&0xC0) + s];
119
1.18G
    int bit, lps_mask;
120
121
1.18G
    c->range -= RangeLPS;
122
1.18G
    lps_mask= ((c->range<<(CABAC_BITS+1)) - c->low)>>31;
123
124
1.18G
    c->low -= (c->range<<(CABAC_BITS+1)) & lps_mask;
125
1.18G
    c->range += (RangeLPS - c->range) & lps_mask;
126
127
1.18G
    s^=lps_mask;
128
1.18G
    *state= (ff_h264_mlps_state+128)[s];
129
1.18G
    bit= s&1;
130
131
1.18G
    lps_mask= ff_h264_norm_shift[c->range];
132
1.18G
    c->range<<= lps_mask;
133
1.18G
    c->low  <<= lps_mask;
134
1.18G
    if(!(c->low & CABAC_MASK))
135
37.2M
        refill2(c);
136
1.18G
    return bit;
137
1.18G
}
Unexecuted instantiation: hevcdec.c:get_cabac_inline
cabac.c:get_cabac_inline
Line
Count
Source
116
4.12G
static av_always_inline int get_cabac_inline(CABACContext *c, uint8_t * const state){
117
4.12G
    int s = *state;
118
4.12G
    int RangeLPS= ff_h264_lps_range[2*(c->range&0xC0) + s];
119
4.12G
    int bit, lps_mask;
120
121
4.12G
    c->range -= RangeLPS;
122
4.12G
    lps_mask= ((c->range<<(CABAC_BITS+1)) - c->low)>>31;
123
124
4.12G
    c->low -= (c->range<<(CABAC_BITS+1)) & lps_mask;
125
4.12G
    c->range += (RangeLPS - c->range) & lps_mask;
126
127
4.12G
    s^=lps_mask;
128
4.12G
    *state= (ff_h264_mlps_state+128)[s];
129
4.12G
    bit= s&1;
130
131
4.12G
    lps_mask= ff_h264_norm_shift[c->range];
132
4.12G
    c->range<<= lps_mask;
133
4.12G
    c->low  <<= lps_mask;
134
4.12G
    if(!(c->low & CABAC_MASK))
135
33.1M
        refill2(c);
136
4.12G
    return bit;
137
4.12G
}
138
#endif
139
140
145M
av_unused av_noinline static int get_cabac_noinline(CABACContext *c, uint8_t * const state){
141
145M
    return get_cabac_inline(c,state);
142
145M
}
Unexecuted instantiation: h264_slice.c:get_cabac_noinline
h264_cabac.c:get_cabac_noinline
Line
Count
Source
140
145M
av_unused av_noinline static int get_cabac_noinline(CABACContext *c, uint8_t * const state){
141
145M
    return get_cabac_inline(c,state);
142
145M
}
Unexecuted instantiation: hevcdec.c:get_cabac_noinline
Unexecuted instantiation: cabac.c:get_cabac_noinline
143
144
5.17G
av_unused static int get_cabac(CABACContext *c, uint8_t * const state){
145
5.17G
    return get_cabac_inline(c,state);
146
5.17G
}
Unexecuted instantiation: h264_slice.c:get_cabac
h264_cabac.c:get_cabac
Line
Count
Source
144
1.04G
av_unused static int get_cabac(CABACContext *c, uint8_t * const state){
145
1.04G
    return get_cabac_inline(c,state);
146
1.04G
}
Unexecuted instantiation: hevcdec.c:get_cabac
cabac.c:get_cabac
Line
Count
Source
144
4.12G
av_unused static int get_cabac(CABACContext *c, uint8_t * const state){
145
4.12G
    return get_cabac_inline(c,state);
146
4.12G
}
147
148
#ifndef get_cabac_bypass
149
2.11G
av_unused static int get_cabac_bypass(CABACContext *c){
150
2.11G
    int range;
151
2.11G
    c->low += c->low;
152
153
2.11G
    if(!(c->low & CABAC_MASK))
154
132M
        refill(c);
155
156
2.11G
    range= c->range<<(CABAC_BITS+1);
157
2.11G
    if(c->low < range){
158
1.80G
        return 0;
159
1.80G
    }else{
160
309M
        c->low -= range;
161
309M
        return 1;
162
309M
    }
163
2.11G
}
Unexecuted instantiation: h264_slice.c:get_cabac_bypass
h264_cabac.c:get_cabac_bypass
Line
Count
Source
149
37.3M
av_unused static int get_cabac_bypass(CABACContext *c){
150
37.3M
    int range;
151
37.3M
    c->low += c->low;
152
153
37.3M
    if(!(c->low & CABAC_MASK))
154
2.32M
        refill(c);
155
156
37.3M
    range= c->range<<(CABAC_BITS+1);
157
37.3M
    if(c->low < range){
158
22.9M
        return 0;
159
22.9M
    }else{
160
14.4M
        c->low -= range;
161
14.4M
        return 1;
162
14.4M
    }
163
37.3M
}
Unexecuted instantiation: hevcdec.c:get_cabac_bypass
cabac.c:get_cabac_bypass
Line
Count
Source
149
2.07G
av_unused static int get_cabac_bypass(CABACContext *c){
150
2.07G
    int range;
151
2.07G
    c->low += c->low;
152
153
2.07G
    if(!(c->low & CABAC_MASK))
154
129M
        refill(c);
155
156
2.07G
    range= c->range<<(CABAC_BITS+1);
157
2.07G
    if(c->low < range){
158
1.78G
        return 0;
159
1.78G
    }else{
160
295M
        c->low -= range;
161
295M
        return 1;
162
295M
    }
163
2.07G
}
164
#endif
165
166
#ifndef get_cabac_bypass_sign
167
161M
static av_always_inline int get_cabac_bypass_sign(CABACContext *c, int val){
168
161M
    int range, mask;
169
161M
    c->low += c->low;
170
171
161M
    if(!(c->low & CABAC_MASK))
172
10.0M
        refill(c);
173
174
161M
    range= c->range<<(CABAC_BITS+1);
175
161M
    c->low -= range;
176
161M
    mask= c->low >> 31;
177
161M
    range &= mask;
178
161M
    c->low += range;
179
161M
    return (val^mask)-mask;
180
161M
}
Unexecuted instantiation: h264_slice.c:get_cabac_bypass_sign
h264_cabac.c:get_cabac_bypass_sign
Line
Count
Source
167
160M
static av_always_inline int get_cabac_bypass_sign(CABACContext *c, int val){
168
160M
    int range, mask;
169
160M
    c->low += c->low;
170
171
160M
    if(!(c->low & CABAC_MASK))
172
10.0M
        refill(c);
173
174
160M
    range= c->range<<(CABAC_BITS+1);
175
160M
    c->low -= range;
176
160M
    mask= c->low >> 31;
177
160M
    range &= mask;
178
160M
    c->low += range;
179
160M
    return (val^mask)-mask;
180
160M
}
Unexecuted instantiation: hevcdec.c:get_cabac_bypass_sign
cabac.c:get_cabac_bypass_sign
Line
Count
Source
167
1.30M
static av_always_inline int get_cabac_bypass_sign(CABACContext *c, int val){
168
1.30M
    int range, mask;
169
1.30M
    c->low += c->low;
170
171
1.30M
    if(!(c->low & CABAC_MASK))
172
83.0k
        refill(c);
173
174
1.30M
    range= c->range<<(CABAC_BITS+1);
175
1.30M
    c->low -= range;
176
1.30M
    mask= c->low >> 31;
177
1.30M
    range &= mask;
178
1.30M
    c->low += range;
179
1.30M
    return (val^mask)-mask;
180
1.30M
}
181
#endif
182
183
/**
184
 * @return the number of bytes read or 0 if no end
185
 */
186
#ifndef get_cabac_terminate
187
32.2M
av_unused static int get_cabac_terminate(CABACContext *c){
188
32.2M
    c->range -= 2;
189
32.2M
    if(c->low < c->range<<(CABAC_BITS+1)){
190
32.0M
        renorm_cabac_decoder_once(c);
191
32.0M
        return 0;
192
32.0M
    }else{
193
219k
        return c->bytestream - c->bytestream_start;
194
219k
    }
195
32.2M
}
h264_slice.c:get_cabac_terminate
Line
Count
Source
187
21.8M
av_unused static int get_cabac_terminate(CABACContext *c){
188
21.8M
    c->range -= 2;
189
21.8M
    if(c->low < c->range<<(CABAC_BITS+1)){
190
21.7M
        renorm_cabac_decoder_once(c);
191
21.7M
        return 0;
192
21.7M
    }else{
193
95.4k
        return c->bytestream - c->bytestream_start;
194
95.4k
    }
195
21.8M
}
h264_cabac.c:get_cabac_terminate
Line
Count
Source
187
1.44M
av_unused static int get_cabac_terminate(CABACContext *c){
188
1.44M
    c->range -= 2;
189
1.44M
    if(c->low < c->range<<(CABAC_BITS+1)){
190
1.44M
        renorm_cabac_decoder_once(c);
191
1.44M
        return 0;
192
1.44M
    }else{
193
3.37k
        return c->bytestream - c->bytestream_start;
194
3.37k
    }
195
1.44M
}
Unexecuted instantiation: hevcdec.c:get_cabac_terminate
cabac.c:get_cabac_terminate
Line
Count
Source
187
8.89M
av_unused static int get_cabac_terminate(CABACContext *c){
188
8.89M
    c->range -= 2;
189
8.89M
    if(c->low < c->range<<(CABAC_BITS+1)){
190
8.77M
        renorm_cabac_decoder_once(c);
191
8.77M
        return 0;
192
8.77M
    }else{
193
121k
        return c->bytestream - c->bytestream_start;
194
121k
    }
195
8.89M
}
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
32.5k
av_unused static const uint8_t* skip_bytes(CABACContext *c, int n) {
204
32.5k
    const uint8_t *ptr = c->bytestream;
205
206
32.5k
    if (c->low & 0x1)
207
3.30k
        ptr--;
208
32.5k
#if CABAC_BITS == 16
209
32.5k
    if (c->low & 0x1FF)
210
19.6k
        ptr--;
211
32.5k
#endif
212
32.5k
    if ((int) (c->bytestream_end - ptr) < n)
213
11.3k
        return NULL;
214
21.2k
    if (ff_init_cabac_decoder(c, ptr + n, c->bytestream_end - ptr - n) < 0)
215
163
        return NULL;
216
217
21.0k
    return ptr;
218
21.2k
}
Unexecuted instantiation: h264_slice.c:skip_bytes
Unexecuted instantiation: h264_cabac.c:skip_bytes
hevcdec.c:skip_bytes
Line
Count
Source
203
3.93k
av_unused static const uint8_t* skip_bytes(CABACContext *c, int n) {
204
3.93k
    const uint8_t *ptr = c->bytestream;
205
206
3.93k
    if (c->low & 0x1)
207
268
        ptr--;
208
3.93k
#if CABAC_BITS == 16
209
3.93k
    if (c->low & 0x1FF)
210
2.57k
        ptr--;
211
3.93k
#endif
212
3.93k
    if ((int) (c->bytestream_end - ptr) < n)
213
2.26k
        return NULL;
214
1.67k
    if (ff_init_cabac_decoder(c, ptr + n, c->bytestream_end - ptr - n) < 0)
215
48
        return NULL;
216
217
1.62k
    return ptr;
218
1.67k
}
cabac.c:skip_bytes
Line
Count
Source
203
28.6k
av_unused static const uint8_t* skip_bytes(CABACContext *c, int n) {
204
28.6k
    const uint8_t *ptr = c->bytestream;
205
206
28.6k
    if (c->low & 0x1)
207
3.03k
        ptr--;
208
28.6k
#if CABAC_BITS == 16
209
28.6k
    if (c->low & 0x1FF)
210
17.0k
        ptr--;
211
28.6k
#endif
212
28.6k
    if ((int) (c->bytestream_end - ptr) < n)
213
9.10k
        return NULL;
214
19.5k
    if (ff_init_cabac_decoder(c, ptr + n, c->bytestream_end - ptr - n) < 0)
215
115
        return NULL;
216
217
19.4k
    return ptr;
218
19.5k
}
219
#endif
220
221
#endif /* AVCODEC_CABAC_FUNCTIONS_H */