FFmpeg coverage


Directory: ../../../ffmpeg/
File: src/libavcodec/cabac_functions.h
Date: 2022-01-18 10:05:52
Exec Total Coverage
Lines: 32 34 94.1%
Branches: 12 14 85.7%

Line Branch Exec 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 "cabac.h"
34 #include "config.h"
35
36 #ifndef UNCHECKED_BITSTREAM_READER
37 #define UNCHECKED_BITSTREAM_READER !CONFIG_SAFE_BITSTREAM_READER
38 #endif
39
40 #if ARCH_AARCH64
41 # include "aarch64/cabac.h"
42 #endif
43 #if ARCH_ARM
44 # include "arm/cabac.h"
45 #endif
46 #if ARCH_X86
47 # include "x86/cabac.h"
48 #endif
49 #if ARCH_MIPS
50 # include "mips/cabac.h"
51 #endif
52 #if ARCH_LOONGARCH64
53 # include "loongarch/cabac.h"
54 #endif
55
56 static const uint8_t * const ff_h264_norm_shift = ff_h264_cabac_tables + H264_NORM_SHIFT_OFFSET;
57 static const uint8_t * const ff_h264_lps_range = ff_h264_cabac_tables + H264_LPS_RANGE_OFFSET;
58 static const uint8_t * const ff_h264_mlps_state = ff_h264_cabac_tables + H264_MLPS_STATE_OFFSET;
59 static const uint8_t * const ff_h264_last_coeff_flag_offset_8x8 = ff_h264_cabac_tables + H264_LAST_COEFF_FLAG_OFFSET_8x8_OFFSET;
60
61 #if !defined(get_cabac_bypass) || !defined(get_cabac_terminate)
62 23174 static void refill(CABACContext *c){
63 #if CABAC_BITS == 16
64 23174 c->low+= (c->bytestream[0]<<9) + (c->bytestream[1]<<1);
65 #else
66 c->low+= c->bytestream[0]<<1;
67 #endif
68 23174 c->low -= CABAC_MASK;
69 #if !UNCHECKED_BITSTREAM_READER
70
2/2
✓ Branch 0 taken 21058 times.
✓ Branch 1 taken 46 times.
21104 if (c->bytestream < c->bytestream_end)
71 #endif
72 23128 c->bytestream += CABAC_BITS / 8;
73 23174 }
74 #endif
75
76 #ifndef get_cabac_terminate
77 10707993 static inline void renorm_cabac_decoder_once(CABACContext *c){
78 10707993 int shift= (uint32_t)(c->range - 0x100)>>31;
79 10707993 c->range<<= shift;
80 10707993 c->low <<= shift;
81
2/2
✓ Branch 0 taken 23174 times.
✓ Branch 1 taken 10684819 times.
10707993 if(!(c->low & CABAC_MASK))
82 23174 refill(c);
83 10707993 }
84 #endif
85
86 #ifndef get_cabac_inline
87 static void refill2(CABACContext *c){
88 int i;
89 unsigned x;
90 #if !HAVE_FAST_CLZ
91 x= c->low ^ (c->low-1);
92 i= 7 - ff_h264_norm_shift[x>>(CABAC_BITS-1)];
93 #else
94 i = ff_ctz(c->low) - CABAC_BITS;
95 #endif
96
97 x= -CABAC_MASK;
98
99 #if CABAC_BITS == 16
100 x+= (c->bytestream[0]<<9) + (c->bytestream[1]<<1);
101 #else
102 x+= c->bytestream[0]<<1;
103 #endif
104
105 c->low += x<<i;
106 #if !UNCHECKED_BITSTREAM_READER
107 if (c->bytestream < c->bytestream_end)
108 #endif
109 c->bytestream += CABAC_BITS/8;
110 }
111 #endif
112
113 #ifndef get_cabac_inline
114 static av_always_inline int get_cabac_inline(CABACContext *c, uint8_t * const state){
115 int s = *state;
116 int RangeLPS= ff_h264_lps_range[2*(c->range&0xC0) + s];
117 int bit, lps_mask;
118
119 c->range -= RangeLPS;
120 lps_mask= ((c->range<<(CABAC_BITS+1)) - c->low)>>31;
121
122 c->low -= (c->range<<(CABAC_BITS+1)) & lps_mask;
123 c->range += (RangeLPS - c->range) & lps_mask;
124
125 s^=lps_mask;
126 *state= (ff_h264_mlps_state+128)[s];
127 bit= s&1;
128
129 lps_mask= ff_h264_norm_shift[c->range];
130 c->range<<= lps_mask;
131 c->low <<= lps_mask;
132 if(!(c->low & CABAC_MASK))
133 refill2(c);
134 return bit;
135 }
136 #endif
137
138 83483484 static int av_noinline av_unused get_cabac_noinline(CABACContext *c, uint8_t * const state){
139 83483484 return get_cabac_inline(c,state);
140 }
141
142 999160656 static int av_unused get_cabac(CABACContext *c, uint8_t * const state){
143 999160656 return get_cabac_inline(c,state);
144 }
145
146 #ifndef get_cabac_bypass
147 static int av_unused get_cabac_bypass(CABACContext *c){
148 int range;
149 c->low += c->low;
150
151 if(!(c->low & CABAC_MASK))
152 refill(c);
153
154 range= c->range<<(CABAC_BITS+1);
155 if(c->low < range){
156 return 0;
157 }else{
158 c->low -= range;
159 return 1;
160 }
161 }
162 #endif
163
164 #ifndef get_cabac_bypass_sign
165 static av_always_inline int get_cabac_bypass_sign(CABACContext *c, int val){
166 int range, mask;
167 c->low += c->low;
168
169 if(!(c->low & CABAC_MASK))
170 refill(c);
171
172 range= c->range<<(CABAC_BITS+1);
173 c->low -= range;
174 mask= c->low >> 31;
175 range &= mask;
176 c->low += range;
177 return (val^mask)-mask;
178 }
179 #endif
180
181 /**
182 * @return the number of bytes read or 0 if no end
183 */
184 #ifndef get_cabac_terminate
185 10774974 static int av_unused get_cabac_terminate(CABACContext *c){
186 10774974 c->range -= 2;
187
2/2
✓ Branch 0 taken 10707993 times.
✓ Branch 1 taken 66981 times.
10774974 if(c->low < c->range<<(CABAC_BITS+1)){
188 10707993 renorm_cabac_decoder_once(c);
189 10707993 return 0;
190 }else{
191 66981 return c->bytestream - c->bytestream_start;
192 }
193 }
194 #endif
195
196 /**
197 * Skip @p n bytes and reset the decoder.
198 * @return the address of the first skipped byte or NULL if there's less than @p n bytes left
199 */
200 #ifndef skip_bytes
201 20103 static av_unused const uint8_t* skip_bytes(CABACContext *c, int n) {
202 20103 const uint8_t *ptr = c->bytestream;
203
204
2/2
✓ Branch 0 taken 1149 times.
✓ Branch 1 taken 18954 times.
20103 if (c->low & 0x1)
205 1149 ptr--;
206 #if CABAC_BITS == 16
207
2/2
✓ Branch 0 taken 10939 times.
✓ Branch 1 taken 9164 times.
20103 if (c->low & 0x1FF)
208 10939 ptr--;
209 #endif
210
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 20103 times.
20103 if ((int) (c->bytestream_end - ptr) < n)
211 return NULL;
212
1/2
✗ Branch 1 not taken.
✓ Branch 2 taken 20103 times.
20103 if (ff_init_cabac_decoder(c, ptr + n, c->bytestream_end - ptr - n) < 0)
213 return NULL;
214
215 20103 return ptr;
216 }
217 #endif
218
219 #endif /* AVCODEC_CABAC_FUNCTIONS_H */
220