FFmpeg  4.4.6
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 
57 
58 #if !defined(get_cabac_bypass) || !defined(get_cabac_terminate)
59 static void refill(CABACContext *c){
60 #if CABAC_BITS == 16
61  c->low+= (c->bytestream[0]<<9) + (c->bytestream[1]<<1);
62 #else
63  c->low+= c->bytestream[0]<<1;
64 #endif
65  c->low -= CABAC_MASK;
66 #if !UNCHECKED_BITSTREAM_READER
67  if (c->bytestream < c->bytestream_end)
68 #endif
69  c->bytestream += CABAC_BITS / 8;
70 }
71 #endif
72 
73 #ifndef get_cabac_terminate
75  int shift= (uint32_t)(c->range - 0x100)>>31;
76  c->range<<= shift;
77  c->low <<= shift;
78  if(!(c->low & CABAC_MASK))
79  refill(c);
80 }
81 #endif
82 
83 #ifndef get_cabac_inline
84 static void refill2(CABACContext *c){
85  int i;
86  unsigned x;
87 #if !HAVE_FAST_CLZ
88  x= c->low ^ (c->low-1);
89  i= 7 - ff_h264_norm_shift[x>>(CABAC_BITS-1)];
90 #else
91  i = ff_ctz(c->low) - CABAC_BITS;
92 #endif
93 
94  x= -CABAC_MASK;
95 
96 #if CABAC_BITS == 16
97  x+= (c->bytestream[0]<<9) + (c->bytestream[1]<<1);
98 #else
99  x+= c->bytestream[0]<<1;
100 #endif
101 
102  c->low += x<<i;
103 #if !UNCHECKED_BITSTREAM_READER
104  if (c->bytestream < c->bytestream_end)
105 #endif
106  c->bytestream += CABAC_BITS/8;
107 }
108 #endif
109 
110 #ifndef get_cabac_inline
112  int s = *state;
113  int RangeLPS= ff_h264_lps_range[2*(c->range&0xC0) + s];
114  int bit, lps_mask;
115 
116  c->range -= RangeLPS;
117  lps_mask= ((c->range<<(CABAC_BITS+1)) - c->low)>>31;
118 
119  c->low -= (c->range<<(CABAC_BITS+1)) & lps_mask;
120  c->range += (RangeLPS - c->range) & lps_mask;
121 
122  s^=lps_mask;
123  *state= (ff_h264_mlps_state+128)[s];
124  bit= s&1;
125 
126  lps_mask= ff_h264_norm_shift[c->range];
127  c->range<<= lps_mask;
128  c->low <<= lps_mask;
129  if(!(c->low & CABAC_MASK))
130  refill2(c);
131  return bit;
132 }
133 #endif
134 
136  return get_cabac_inline(c,state);
137 }
138 
140  return get_cabac_inline(c,state);
141 }
142 
143 #ifndef get_cabac_bypass
145  int range;
146  c->low += c->low;
147 
148  if(!(c->low & CABAC_MASK))
149  refill(c);
150 
151  range= c->range<<(CABAC_BITS+1);
152  if(c->low < range){
153  return 0;
154  }else{
155  c->low -= range;
156  return 1;
157  }
158 }
159 #endif
160 
161 #ifndef get_cabac_bypass_sign
163  int range, mask;
164  c->low += c->low;
165 
166  if(!(c->low & CABAC_MASK))
167  refill(c);
168 
169  range= c->range<<(CABAC_BITS+1);
170  c->low -= range;
171  mask= c->low >> 31;
172  range &= mask;
173  c->low += range;
174  return (val^mask)-mask;
175 }
176 #endif
177 
178 /**
179  * @return the number of bytes read or 0 if no end
180  */
181 #ifndef get_cabac_terminate
183  c->range -= 2;
184  if(c->low < c->range<<(CABAC_BITS+1)){
186  return 0;
187  }else{
188  return c->bytestream - c->bytestream_start;
189  }
190 }
191 #endif
192 
193 /**
194  * Skip @p n bytes and reset the decoder.
195  * @return the address of the first skipped byte or NULL if there's less than @p n bytes left
196  */
197 #ifndef skip_bytes
198 static av_unused const uint8_t* skip_bytes(CABACContext *c, int n) {
199  const uint8_t *ptr = c->bytestream;
200 
201  if (c->low & 0x1)
202  ptr--;
203 #if CABAC_BITS == 16
204  if (c->low & 0x1FF)
205  ptr--;
206 #endif
207  if ((int) (c->bytestream_end - ptr) < n)
208  return NULL;
209  if (ff_init_cabac_decoder(c, ptr + n, c->bytestream_end - ptr - n) < 0)
210  return NULL;
211 
212  return ptr;
213 }
214 #endif
215 
216 #endif /* AVCODEC_CABAC_FUNCTIONS_H */
static double val(void *priv, double ch)
Definition: aeval.c:76
#define av_always_inline
Definition: attributes.h:45
#define av_noinline
Definition: attributes.h:68
#define av_unused
Definition: attributes.h:131
uint8_t
int ff_init_cabac_decoder(CABACContext *c, const uint8_t *buf, int buf_size)
Definition: cabac.c:165
#define CABAC_BITS
Definition: cabac.h:38
#define H264_LAST_COEFF_FLAG_OFFSET_8x8_OFFSET
Definition: cabac.h:36
#define CABAC_MASK
Definition: cabac.h:39
#define H264_LPS_RANGE_OFFSET
Definition: cabac.h:34
#define H264_MLPS_STATE_OFFSET
Definition: cabac.h:35
#define H264_NORM_SHIFT_OFFSET
Definition: cabac.h:33
const uint8_t ff_h264_cabac_tables[512+4 *2 *64+4 *64+63]
static int av_unused get_cabac(CABACContext *c, uint8_t *const state)
static void refill(CABACContext *c)
static void refill2(CABACContext *c)
static void renorm_cabac_decoder_once(CABACContext *c)
static const uint8_t *const ff_h264_last_coeff_flag_offset_8x8
static const uint8_t *const ff_h264_norm_shift
static int av_noinline av_unused get_cabac_noinline(CABACContext *c, uint8_t *const state)
static av_always_inline int get_cabac_bypass_sign(CABACContext *c, int val)
static const uint8_t *const ff_h264_lps_range
static av_always_inline int get_cabac_inline(CABACContext *c, uint8_t *const state)
static int av_unused get_cabac_bypass(CABACContext *c)
static av_unused const uint8_t * skip_bytes(CABACContext *c, int n)
Skip n bytes and reset the decoder.
static const uint8_t *const ff_h264_mlps_state
static int av_unused get_cabac_terminate(CABACContext *c)
#define bit(string, value)
Definition: cbs_mpeg2.c:58
#define s(width, name)
Definition: cbs_vp9.c:257
static struct @321 state
#define NULL
Definition: coverity.c:32
#define ff_ctz
Definition: intmath.h:106
int i
Definition: input.c:407
static const uint16_t mask[17]
Definition: lzw.c:38
static int shift(int a, int b)
Definition: sonic.c:82
static double c[64]