Coverage Report

Created: 2026-05-23 07:06

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
168M
static void refill(CABACContext *c){
65
168M
#if CABAC_BITS == 16
66
168M
        c->low+= (c->bytestream[0]<<9) + (c->bytestream[1]<<1);
67
#else
68
        c->low+= c->bytestream[0]<<1;
69
#endif
70
168M
    c->low -= CABAC_MASK;
71
#if !UNCHECKED_BITSTREAM_READER
72
154M
    if (c->bytestream < c->bytestream_end)
73
5.80M
#endif
74
19.6M
        c->bytestream += CABAC_BITS / 8;
75
168M
}
h264_slice.c:refill
Line
Count
Source
64
48.1k
static void refill(CABACContext *c){
65
48.1k
#if CABAC_BITS == 16
66
48.1k
        c->low+= (c->bytestream[0]<<9) + (c->bytestream[1]<<1);
67
#else
68
        c->low+= c->bytestream[0]<<1;
69
#endif
70
48.1k
    c->low -= CABAC_MASK;
71
48.1k
#if !UNCHECKED_BITSTREAM_READER
72
48.1k
    if (c->bytestream < c->bytestream_end)
73
43.7k
#endif
74
43.7k
        c->bytestream += CABAC_BITS / 8;
75
48.1k
}
h264_cabac.c:refill
Line
Count
Source
64
13.8M
static void refill(CABACContext *c){
65
13.8M
#if CABAC_BITS == 16
66
13.8M
        c->low+= (c->bytestream[0]<<9) + (c->bytestream[1]<<1);
67
#else
68
        c->low+= c->bytestream[0]<<1;
69
#endif
70
13.8M
    c->low -= CABAC_MASK;
71
#if !UNCHECKED_BITSTREAM_READER
72
    if (c->bytestream < c->bytestream_end)
73
#endif
74
13.8M
        c->bytestream += CABAC_BITS / 8;
75
13.8M
}
Unexecuted instantiation: hevcdec.c:refill
cabac.c:refill
Line
Count
Source
64
154M
static void refill(CABACContext *c){
65
154M
#if CABAC_BITS == 16
66
154M
        c->low+= (c->bytestream[0]<<9) + (c->bytestream[1]<<1);
67
#else
68
        c->low+= c->bytestream[0]<<1;
69
#endif
70
154M
    c->low -= CABAC_MASK;
71
154M
#if !UNCHECKED_BITSTREAM_READER
72
154M
    if (c->bytestream < c->bytestream_end)
73
5.75M
#endif
74
5.75M
        c->bytestream += CABAC_BITS / 8;
75
154M
}
76
#endif
77
78
#ifndef get_cabac_terminate
79
35.4M
static inline void renorm_cabac_decoder_once(CABACContext *c){
80
35.4M
    int shift= (uint32_t)(c->range - 0x100)>>31;
81
35.4M
    c->range<<= shift;
82
35.4M
    c->low  <<= shift;
83
35.4M
    if(!(c->low & CABAC_MASK))
84
63.9k
        refill(c);
85
35.4M
}
h264_slice.c:renorm_cabac_decoder_once
Line
Count
Source
79
24.7M
static inline void renorm_cabac_decoder_once(CABACContext *c){
80
24.7M
    int shift= (uint32_t)(c->range - 0x100)>>31;
81
24.7M
    c->range<<= shift;
82
24.7M
    c->low  <<= shift;
83
24.7M
    if(!(c->low & CABAC_MASK))
84
48.1k
        refill(c);
85
24.7M
}
h264_cabac.c:renorm_cabac_decoder_once
Line
Count
Source
79
1.45M
static inline void renorm_cabac_decoder_once(CABACContext *c){
80
1.45M
    int shift= (uint32_t)(c->range - 0x100)>>31;
81
1.45M
    c->range<<= shift;
82
1.45M
    c->low  <<= shift;
83
1.45M
    if(!(c->low & CABAC_MASK))
84
2.63k
        refill(c);
85
1.45M
}
Unexecuted instantiation: hevcdec.c:renorm_cabac_decoder_once
cabac.c:renorm_cabac_decoder_once
Line
Count
Source
79
9.19M
static inline void renorm_cabac_decoder_once(CABACContext *c){
80
9.19M
    int shift= (uint32_t)(c->range - 0x100)>>31;
81
9.19M
    c->range<<= shift;
82
9.19M
    c->low  <<= shift;
83
9.19M
    if(!(c->low & CABAC_MASK))
84
13.1k
        refill(c);
85
9.19M
}
86
#endif
87
88
#ifndef get_cabac_inline
89
78.1M
static void refill2(CABACContext *c){
90
78.1M
    int i;
91
78.1M
    unsigned x;
92
78.1M
#if !HAVE_FAST_CLZ
93
78.1M
    x= c->low ^ (c->low-1);
94
78.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
78.1M
    x= -CABAC_MASK;
100
101
78.1M
#if CABAC_BITS == 16
102
78.1M
        x+= (c->bytestream[0]<<9) + (c->bytestream[1]<<1);
103
#else
104
        x+= c->bytestream[0]<<1;
105
#endif
106
107
78.1M
    c->low += x<<i;
108
#if !UNCHECKED_BITSTREAM_READER
109
35.4M
    if (c->bytestream < c->bytestream_end)
110
3.22M
#endif
111
45.9M
        c->bytestream += CABAC_BITS/8;
112
78.1M
}
Unexecuted instantiation: h264_slice.c:refill2
h264_cabac.c:refill2
Line
Count
Source
89
42.7M
static void refill2(CABACContext *c){
90
42.7M
    int i;
91
42.7M
    unsigned x;
92
42.7M
#if !HAVE_FAST_CLZ
93
42.7M
    x= c->low ^ (c->low-1);
94
42.7M
    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.7M
    x= -CABAC_MASK;
100
101
42.7M
#if CABAC_BITS == 16
102
42.7M
        x+= (c->bytestream[0]<<9) + (c->bytestream[1]<<1);
103
#else
104
        x+= c->bytestream[0]<<1;
105
#endif
106
107
42.7M
    c->low += x<<i;
108
#if !UNCHECKED_BITSTREAM_READER
109
    if (c->bytestream < c->bytestream_end)
110
#endif
111
42.7M
        c->bytestream += CABAC_BITS/8;
112
42.7M
}
Unexecuted instantiation: hevcdec.c:refill2
cabac.c:refill2
Line
Count
Source
89
35.4M
static void refill2(CABACContext *c){
90
35.4M
    int i;
91
35.4M
    unsigned x;
92
35.4M
#if !HAVE_FAST_CLZ
93
35.4M
    x= c->low ^ (c->low-1);
94
35.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
35.4M
    x= -CABAC_MASK;
100
101
35.4M
#if CABAC_BITS == 16
102
35.4M
        x+= (c->bytestream[0]<<9) + (c->bytestream[1]<<1);
103
#else
104
        x+= c->bytestream[0]<<1;
105
#endif
106
107
35.4M
    c->low += x<<i;
108
35.4M
#if !UNCHECKED_BITSTREAM_READER
109
35.4M
    if (c->bytestream < c->bytestream_end)
110
3.22M
#endif
111
3.22M
        c->bytestream += CABAC_BITS/8;
112
35.4M
}
113
#endif
114
115
#ifndef get_cabac_inline
116
6.00G
static av_always_inline int get_cabac_inline(CABACContext *c, uint8_t * const state){
117
6.00G
    int s = *state;
118
6.00G
    int RangeLPS= ff_h264_lps_range[2*(c->range&0xC0) + s];
119
6.00G
    int bit, lps_mask;
120
121
6.00G
    c->range -= RangeLPS;
122
6.00G
    lps_mask= ((c->range<<(CABAC_BITS+1)) - c->low)>>31;
123
124
6.00G
    c->low -= (c->range<<(CABAC_BITS+1)) & lps_mask;
125
6.00G
    c->range += (RangeLPS - c->range) & lps_mask;
126
127
6.00G
    s^=lps_mask;
128
6.00G
    *state= (ff_h264_mlps_state+128)[s];
129
6.00G
    bit= s&1;
130
131
6.00G
    lps_mask= ff_h264_norm_shift[c->range];
132
6.00G
    c->range<<= lps_mask;
133
6.00G
    c->low  <<= lps_mask;
134
6.00G
    if(!(c->low & CABAC_MASK))
135
78.1M
        refill2(c);
136
6.00G
    return bit;
137
6.00G
}
Unexecuted instantiation: h264_slice.c:get_cabac_inline
h264_cabac.c:get_cabac_inline
Line
Count
Source
116
1.31G
static av_always_inline int get_cabac_inline(CABACContext *c, uint8_t * const state){
117
1.31G
    int s = *state;
118
1.31G
    int RangeLPS= ff_h264_lps_range[2*(c->range&0xC0) + s];
119
1.31G
    int bit, lps_mask;
120
121
1.31G
    c->range -= RangeLPS;
122
1.31G
    lps_mask= ((c->range<<(CABAC_BITS+1)) - c->low)>>31;
123
124
1.31G
    c->low -= (c->range<<(CABAC_BITS+1)) & lps_mask;
125
1.31G
    c->range += (RangeLPS - c->range) & lps_mask;
126
127
1.31G
    s^=lps_mask;
128
1.31G
    *state= (ff_h264_mlps_state+128)[s];
129
1.31G
    bit= s&1;
130
131
1.31G
    lps_mask= ff_h264_norm_shift[c->range];
132
1.31G
    c->range<<= lps_mask;
133
1.31G
    c->low  <<= lps_mask;
134
1.31G
    if(!(c->low & CABAC_MASK))
135
42.7M
        refill2(c);
136
1.31G
    return bit;
137
1.31G
}
Unexecuted instantiation: hevcdec.c:get_cabac_inline
cabac.c:get_cabac_inline
Line
Count
Source
116
4.69G
static av_always_inline int get_cabac_inline(CABACContext *c, uint8_t * const state){
117
4.69G
    int s = *state;
118
4.69G
    int RangeLPS= ff_h264_lps_range[2*(c->range&0xC0) + s];
119
4.69G
    int bit, lps_mask;
120
121
4.69G
    c->range -= RangeLPS;
122
4.69G
    lps_mask= ((c->range<<(CABAC_BITS+1)) - c->low)>>31;
123
124
4.69G
    c->low -= (c->range<<(CABAC_BITS+1)) & lps_mask;
125
4.69G
    c->range += (RangeLPS - c->range) & lps_mask;
126
127
4.69G
    s^=lps_mask;
128
4.69G
    *state= (ff_h264_mlps_state+128)[s];
129
4.69G
    bit= s&1;
130
131
4.69G
    lps_mask= ff_h264_norm_shift[c->range];
132
4.69G
    c->range<<= lps_mask;
133
4.69G
    c->low  <<= lps_mask;
134
4.69G
    if(!(c->low & CABAC_MASK))
135
35.4M
        refill2(c);
136
4.69G
    return bit;
137
4.69G
}
138
#endif
139
140
163M
av_unused av_noinline static int get_cabac_noinline(CABACContext *c, uint8_t * const state){
141
163M
    return get_cabac_inline(c,state);
142
163M
}
Unexecuted instantiation: h264_slice.c:get_cabac_noinline
h264_cabac.c:get_cabac_noinline
Line
Count
Source
140
163M
av_unused av_noinline static int get_cabac_noinline(CABACContext *c, uint8_t * const state){
141
163M
    return get_cabac_inline(c,state);
142
163M
}
Unexecuted instantiation: hevcdec.c:get_cabac_noinline
Unexecuted instantiation: cabac.c:get_cabac_noinline
143
144
5.84G
av_unused static int get_cabac(CABACContext *c, uint8_t * const state){
145
5.84G
    return get_cabac_inline(c,state);
146
5.84G
}
Unexecuted instantiation: h264_slice.c:get_cabac
h264_cabac.c:get_cabac
Line
Count
Source
144
1.15G
av_unused static int get_cabac(CABACContext *c, uint8_t * const state){
145
1.15G
    return get_cabac_inline(c,state);
146
1.15G
}
Unexecuted instantiation: hevcdec.c:get_cabac
cabac.c:get_cabac
Line
Count
Source
144
4.69G
av_unused static int get_cabac(CABACContext *c, uint8_t * const state){
145
4.69G
    return get_cabac_inline(c,state);
146
4.69G
}
147
148
#ifndef get_cabac_bypass
149
2.52G
av_unused static int get_cabac_bypass(CABACContext *c){
150
2.52G
    int range;
151
2.52G
    c->low += c->low;
152
153
2.52G
    if(!(c->low & CABAC_MASK))
154
157M
        refill(c);
155
156
2.52G
    range= c->range<<(CABAC_BITS+1);
157
2.52G
    if(c->low < range){
158
2.17G
        return 0;
159
2.17G
    }else{
160
343M
        c->low -= range;
161
343M
        return 1;
162
343M
    }
163
2.52G
}
Unexecuted instantiation: h264_slice.c:get_cabac_bypass
h264_cabac.c:get_cabac_bypass
Line
Count
Source
149
43.7M
av_unused static int get_cabac_bypass(CABACContext *c){
150
43.7M
    int range;
151
43.7M
    c->low += c->low;
152
153
43.7M
    if(!(c->low & CABAC_MASK))
154
2.71M
        refill(c);
155
156
43.7M
    range= c->range<<(CABAC_BITS+1);
157
43.7M
    if(c->low < range){
158
26.4M
        return 0;
159
26.4M
    }else{
160
17.3M
        c->low -= range;
161
17.3M
        return 1;
162
17.3M
    }
163
43.7M
}
Unexecuted instantiation: hevcdec.c:get_cabac_bypass
cabac.c:get_cabac_bypass
Line
Count
Source
149
2.47G
av_unused static int get_cabac_bypass(CABACContext *c){
150
2.47G
    int range;
151
2.47G
    c->low += c->low;
152
153
2.47G
    if(!(c->low & CABAC_MASK))
154
154M
        refill(c);
155
156
2.47G
    range= c->range<<(CABAC_BITS+1);
157
2.47G
    if(c->low < range){
158
2.15G
        return 0;
159
2.15G
    }else{
160
325M
        c->low -= range;
161
325M
        return 1;
162
325M
    }
163
2.47G
}
164
#endif
165
166
#ifndef get_cabac_bypass_sign
167
179M
static av_always_inline int get_cabac_bypass_sign(CABACContext *c, int val){
168
179M
    int range, mask;
169
179M
    c->low += c->low;
170
171
179M
    if(!(c->low & CABAC_MASK))
172
11.2M
        refill(c);
173
174
179M
    range= c->range<<(CABAC_BITS+1);
175
179M
    c->low -= range;
176
179M
    mask= c->low >> 31;
177
179M
    range &= mask;
178
179M
    c->low += range;
179
179M
    return (val^mask)-mask;
180
179M
}
Unexecuted instantiation: h264_slice.c:get_cabac_bypass_sign
h264_cabac.c:get_cabac_bypass_sign
Line
Count
Source
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: hevcdec.c:get_cabac_bypass_sign
cabac.c:get_cabac_bypass_sign
Line
Count
Source
167
1.22M
static av_always_inline int get_cabac_bypass_sign(CABACContext *c, int val){
168
1.22M
    int range, mask;
169
1.22M
    c->low += c->low;
170
171
1.22M
    if(!(c->low & CABAC_MASK))
172
78.4k
        refill(c);
173
174
1.22M
    range= c->range<<(CABAC_BITS+1);
175
1.22M
    c->low -= range;
176
1.22M
    mask= c->low >> 31;
177
1.22M
    range &= mask;
178
1.22M
    c->low += range;
179
1.22M
    return (val^mask)-mask;
180
1.22M
}
181
#endif
182
183
/**
184
 * @return the number of bytes read or 0 if no end
185
 */
186
#ifndef get_cabac_terminate
187
35.6M
av_unused static int get_cabac_terminate(CABACContext *c){
188
35.6M
    c->range -= 2;
189
35.6M
    if(c->low < c->range<<(CABAC_BITS+1)){
190
35.4M
        renorm_cabac_decoder_once(c);
191
35.4M
        return 0;
192
35.4M
    }else{
193
212k
        return c->bytestream - c->bytestream_start;
194
212k
    }
195
35.6M
}
h264_slice.c:get_cabac_terminate
Line
Count
Source
187
24.8M
av_unused static int get_cabac_terminate(CABACContext *c){
188
24.8M
    c->range -= 2;
189
24.8M
    if(c->low < c->range<<(CABAC_BITS+1)){
190
24.7M
        renorm_cabac_decoder_once(c);
191
24.7M
        return 0;
192
24.7M
    }else{
193
102k
        return c->bytestream - c->bytestream_start;
194
102k
    }
195
24.8M
}
h264_cabac.c:get_cabac_terminate
Line
Count
Source
187
1.45M
av_unused static int get_cabac_terminate(CABACContext *c){
188
1.45M
    c->range -= 2;
189
1.45M
    if(c->low < c->range<<(CABAC_BITS+1)){
190
1.45M
        renorm_cabac_decoder_once(c);
191
1.45M
        return 0;
192
1.45M
    }else{
193
4.04k
        return c->bytestream - c->bytestream_start;
194
4.04k
    }
195
1.45M
}
Unexecuted instantiation: hevcdec.c:get_cabac_terminate
cabac.c:get_cabac_terminate
Line
Count
Source
187
9.30M
av_unused static int get_cabac_terminate(CABACContext *c){
188
9.30M
    c->range -= 2;
189
9.30M
    if(c->low < c->range<<(CABAC_BITS+1)){
190
9.19M
        renorm_cabac_decoder_once(c);
191
9.19M
        return 0;
192
9.19M
    }else{
193
105k
        return c->bytestream - c->bytestream_start;
194
105k
    }
195
9.30M
}
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
34.6k
av_unused static const uint8_t* skip_bytes(CABACContext *c, int n) {
204
34.6k
    const uint8_t *ptr = c->bytestream;
205
206
34.6k
    if (c->low & 0x1)
207
3.54k
        ptr--;
208
34.6k
#if CABAC_BITS == 16
209
34.6k
    if (c->low & 0x1FF)
210
20.8k
        ptr--;
211
34.6k
#endif
212
34.6k
    if ((int) (c->bytestream_end - ptr) < n)
213
12.2k
        return NULL;
214
22.3k
    if (ff_init_cabac_decoder(c, ptr + n, c->bytestream_end - ptr - n) < 0)
215
158
        return NULL;
216
217
22.1k
    return ptr;
218
22.3k
}
Unexecuted instantiation: h264_slice.c:skip_bytes
Unexecuted instantiation: h264_cabac.c:skip_bytes
hevcdec.c:skip_bytes
Line
Count
Source
203
4.15k
av_unused static const uint8_t* skip_bytes(CABACContext *c, int n) {
204
4.15k
    const uint8_t *ptr = c->bytestream;
205
206
4.15k
    if (c->low & 0x1)
207
332
        ptr--;
208
4.15k
#if CABAC_BITS == 16
209
4.15k
    if (c->low & 0x1FF)
210
2.74k
        ptr--;
211
4.15k
#endif
212
4.15k
    if ((int) (c->bytestream_end - ptr) < n)
213
2.64k
        return NULL;
214
1.51k
    if (ff_init_cabac_decoder(c, ptr + n, c->bytestream_end - ptr - n) < 0)
215
5
        return NULL;
216
217
1.50k
    return ptr;
218
1.51k
}
cabac.c:skip_bytes
Line
Count
Source
203
30.4k
av_unused static const uint8_t* skip_bytes(CABACContext *c, int n) {
204
30.4k
    const uint8_t *ptr = c->bytestream;
205
206
30.4k
    if (c->low & 0x1)
207
3.20k
        ptr--;
208
30.4k
#if CABAC_BITS == 16
209
30.4k
    if (c->low & 0x1FF)
210
18.1k
        ptr--;
211
30.4k
#endif
212
30.4k
    if ((int) (c->bytestream_end - ptr) < n)
213
9.64k
        return NULL;
214
20.8k
    if (ff_init_cabac_decoder(c, ptr + n, c->bytestream_end - ptr - n) < 0)
215
153
        return NULL;
216
217
20.6k
    return ptr;
218
20.8k
}
219
#endif
220
221
#endif /* AVCODEC_CABAC_FUNCTIONS_H */