FFmpeg
cabac_functions.h
Go to the documentation of this file.
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 
60 
61 #if !defined(get_cabac_bypass) || !defined(get_cabac_terminate)
62 static void refill(CABACContext *c){
63 #if CABAC_BITS == 16
64  c->low+= (c->bytestream[0]<<9) + (c->bytestream[1]<<1);
65 #else
66  c->low+= c->bytestream[0]<<1;
67 #endif
68  c->low -= CABAC_MASK;
69 #if !UNCHECKED_BITSTREAM_READER
70  if (c->bytestream < c->bytestream_end)
71 #endif
72  c->bytestream += CABAC_BITS / 8;
73 }
74 #endif
75 
76 #ifndef get_cabac_terminate
78  int shift= (uint32_t)(c->range - 0x100)>>31;
79  c->range<<= shift;
80  c->low <<= shift;
81  if(!(c->low & CABAC_MASK))
82  refill(c);
83 }
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 
139  return get_cabac_inline(c,state);
140 }
141 
142 static int av_unused get_cabac(CABACContext *c, uint8_t * const state){
143  return get_cabac_inline(c,state);
144 }
145 
146 #ifndef get_cabac_bypass
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
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
186  c->range -= 2;
187  if(c->low < c->range<<(CABAC_BITS+1)){
189  return 0;
190  }else{
191  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 static av_unused const uint8_t* skip_bytes(CABACContext *c, int n) {
202  const uint8_t *ptr = c->bytestream;
203 
204  if (c->low & 0x1)
205  ptr--;
206 #if CABAC_BITS == 16
207  if (c->low & 0x1FF)
208  ptr--;
209 #endif
210  if ((int) (c->bytestream_end - ptr) < n)
211  return NULL;
212  if (ff_init_cabac_decoder(c, ptr + n, c->bytestream_end - ptr - n) < 0)
213  return NULL;
214 
215  return ptr;
216 }
217 #endif
218 
219 #endif /* AVCODEC_CABAC_FUNCTIONS_H */
cabac.h
ff_ctz
#define ff_ctz
Definition: intmath.h:106
state
static struct @322 state
renorm_cabac_decoder_once
static void renorm_cabac_decoder_once(CABACContext *c)
Definition: cabac_functions.h:77
av_unused
#define av_unused
Definition: attributes.h:131
cabac.h
get_cabac_inline
static av_always_inline int get_cabac_inline(CABACContext *c, uint8_t *const state)
Definition: cabac_functions.h:114
bit
#define bit(string, value)
Definition: cbs_mpeg2.c:58
get_cabac
static int av_unused get_cabac(CABACContext *c, uint8_t *const state)
Definition: cabac_functions.h:142
ff_h264_last_coeff_flag_offset_8x8
static const uint8_t *const ff_h264_last_coeff_flag_offset_8x8
Definition: cabac_functions.h:59
val
static double val(void *priv, double ch)
Definition: aeval.c:76
ff_h264_cabac_tables
const uint8_t ff_h264_cabac_tables[512+4 *2 *64+4 *64+63]
av_noinline
#define av_noinline
Definition: attributes.h:72
CABAC_MASK
#define CABAC_MASK
Definition: cabac.h:39
cabac.h
ff_h264_norm_shift
static const uint8_t *const ff_h264_norm_shift
Definition: cabac_functions.h:56
mask
static const uint16_t mask[17]
Definition: lzw.c:38
s
#define s(width, name)
Definition: cbs_vp9.c:257
cabac.h
refill2
static void refill2(CABACContext *c)
Definition: cabac_functions.h:87
NULL
#define NULL
Definition: coverity.c:32
ff_h264_mlps_state
static const uint8_t *const ff_h264_mlps_state
Definition: cabac_functions.h:58
c
Undefined Behavior In the C some operations are like signed integer dereferencing freed accessing outside allocated Undefined Behavior must not occur in a C it is not safe even if the output of undefined operations is unused The unsafety may seem nit picking but Optimizing compilers have in fact optimized code on the assumption that no undefined Behavior occurs Optimizing code based on wrong assumptions can and has in some cases lead to effects beyond the output of computations The signed integer overflow problem in speed critical code Code which is highly optimized and works with signed integers sometimes has the problem that often the output of the computation does not c
Definition: undefined.txt:32
get_cabac_noinline
static int av_noinline av_unused get_cabac_noinline(CABACContext *c, uint8_t *const state)
Definition: cabac_functions.h:138
H264_LPS_RANGE_OFFSET
#define H264_LPS_RANGE_OFFSET
Definition: cabac.h:34
get_cabac_bypass_sign
static av_always_inline int get_cabac_bypass_sign(CABACContext *c, int val)
Definition: cabac_functions.h:165
get_cabac_terminate
static int av_unused get_cabac_terminate(CABACContext *c)
Definition: cabac_functions.h:185
ff_h264_lps_range
static const uint8_t *const ff_h264_lps_range
Definition: cabac_functions.h:57
ff_init_cabac_decoder
int ff_init_cabac_decoder(CABACContext *c, const uint8_t *buf, int buf_size)
Definition: cabac.c:165
i
#define i(width, name, range_min, range_max)
Definition: cbs_h2645.c:271
get_cabac_bypass
static int av_unused get_cabac_bypass(CABACContext *c)
Definition: cabac_functions.h:147
av_always_inline
#define av_always_inline
Definition: attributes.h:49
cabac.h
H264_NORM_SHIFT_OFFSET
#define H264_NORM_SHIFT_OFFSET
Definition: cabac.h:33
cabac.h
H264_LAST_COEFF_FLAG_OFFSET_8x8_OFFSET
#define H264_LAST_COEFF_FLAG_OFFSET_8x8_OFFSET
Definition: cabac.h:36
refill
static void refill(CABACContext *c)
Definition: cabac_functions.h:62
shift
static int shift(int a, int b)
Definition: sonic.c:84
skip_bytes
static const av_unused uint8_t * skip_bytes(CABACContext *c, int n)
Skip n bytes and reset the decoder.
Definition: cabac_functions.h:201
CABAC_BITS
#define CABAC_BITS
Definition: cabac.h:38
H264_MLPS_STATE_OFFSET
#define H264_MLPS_STATE_OFFSET
Definition: cabac.h:35
CABACContext
Definition: cabac.h:41