FFmpeg
 All Data Structures Namespaces Files Functions Variables Typedefs Enumerations Enumerator Macros Groups Pages
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 <stdint.h>
31 
32 #include "cabac.h"
33 #include "config.h"
34 
35 #ifndef UNCHECKED_BITSTREAM_READER
36 #define UNCHECKED_BITSTREAM_READER !CONFIG_SAFE_BITSTREAM_READER
37 #endif
38 
39 #if ARCH_AARCH64
40 # include "aarch64/cabac.h"
41 #endif
42 #if ARCH_ARM
43 # include "arm/cabac.h"
44 #endif
45 #if ARCH_X86
46 # include "x86/cabac.h"
47 #endif
48 
53 
54 #if !defined(get_cabac_bypass) || !defined(get_cabac_terminate)
55 static void refill(CABACContext *c){
56 #if CABAC_BITS == 16
57  c->low+= (c->bytestream[0]<<9) + (c->bytestream[1]<<1);
58 #else
59  c->low+= c->bytestream[0]<<1;
60 #endif
61  c->low -= CABAC_MASK;
62 #if !UNCHECKED_BITSTREAM_READER
63  if (c->bytestream < c->bytestream_end)
64 #endif
65  c->bytestream += CABAC_BITS / 8;
66 }
67 #endif
68 
69 #ifndef get_cabac_terminate
71  int shift= (uint32_t)(c->range - 0x100)>>31;
72  c->range<<= shift;
73  c->low <<= shift;
74  if(!(c->low & CABAC_MASK))
75  refill(c);
76 }
77 #endif
78 
79 #ifndef get_cabac_inline
80 static void refill2(CABACContext *c){
81  int i;
82  unsigned x;
83 #if !HAVE_FAST_CLZ
84  x= c->low ^ (c->low-1);
85  i= 7 - ff_h264_norm_shift[x>>(CABAC_BITS-1)];
86 #else
87  i = ff_ctz(c->low) - CABAC_BITS;
88 #endif
89 
90  x= -CABAC_MASK;
91 
92 #if CABAC_BITS == 16
93  x+= (c->bytestream[0]<<9) + (c->bytestream[1]<<1);
94 #else
95  x+= c->bytestream[0]<<1;
96 #endif
97 
98  c->low += x<<i;
99 #if !UNCHECKED_BITSTREAM_READER
100  if (c->bytestream < c->bytestream_end)
101 #endif
102  c->bytestream += CABAC_BITS/8;
103 }
104 #endif
105 
106 #ifndef get_cabac_inline
108  int s = *state;
109  int RangeLPS= ff_h264_lps_range[2*(c->range&0xC0) + s];
110  int bit, lps_mask;
111 
112  c->range -= RangeLPS;
113  lps_mask= ((c->range<<(CABAC_BITS+1)) - c->low)>>31;
114 
115  c->low -= (c->range<<(CABAC_BITS+1)) & lps_mask;
116  c->range += (RangeLPS - c->range) & lps_mask;
117 
118  s^=lps_mask;
119  *state= (ff_h264_mlps_state+128)[s];
120  bit= s&1;
121 
122  lps_mask= ff_h264_norm_shift[c->range];
123  c->range<<= lps_mask;
124  c->low <<= lps_mask;
125  if(!(c->low & CABAC_MASK))
126  refill2(c);
127  return bit;
128 }
129 #endif
130 
132  return get_cabac_inline(c,state);
133 }
134 
136  return get_cabac_inline(c,state);
137 }
138 
139 #ifndef get_cabac_bypass
141  int range;
142  c->low += c->low;
143 
144  if(!(c->low & CABAC_MASK))
145  refill(c);
146 
147  range= c->range<<(CABAC_BITS+1);
148  if(c->low < range){
149  return 0;
150  }else{
151  c->low -= range;
152  return 1;
153  }
154 }
155 #endif
156 
157 #ifndef get_cabac_bypass_sign
159  int range, mask;
160  c->low += c->low;
161 
162  if(!(c->low & CABAC_MASK))
163  refill(c);
164 
165  range= c->range<<(CABAC_BITS+1);
166  c->low -= range;
167  mask= c->low >> 31;
168  range &= mask;
169  c->low += range;
170  return (val^mask)-mask;
171 }
172 #endif
173 
174 /**
175  *
176  * @return the number of bytes read or 0 if no end
177  */
178 #ifndef get_cabac_terminate
180  c->range -= 2;
181  if(c->low < c->range<<(CABAC_BITS+1)){
183  return 0;
184  }else{
185  return c->bytestream - c->bytestream_start;
186  }
187 }
188 #endif
189 
190 /**
191  * Skip @p n bytes and reset the decoder.
192  * @return the address of the first skipped byte or NULL if there's less than @p n bytes left
193  */
194 #ifndef skip_bytes
195 static av_unused const uint8_t* skip_bytes(CABACContext *c, int n) {
196  const uint8_t *ptr = c->bytestream;
197 
198  if (c->low & 0x1)
199  ptr--;
200 #if CABAC_BITS == 16
201  if (c->low & 0x1FF)
202  ptr--;
203 #endif
204  if ((int) (c->bytestream_end - ptr) < n)
205  return NULL;
206  if (ff_init_cabac_decoder(c, ptr + n, c->bytestream_end - ptr - n) < 0)
207  return NULL;
208 
209  return ptr;
210 }
211 #endif
212 
213 #endif /* AVCODEC_CABAC_FUNCTIONS_H */
#define NULL
Definition: coverity.c:32
const char const char void * val
Definition: avisynth_c.h:634
#define ff_ctz
Definition: intmath.h:106
const char * s
Definition: avisynth_c.h:631
static int shift(int a, int b)
Definition: sonic.c:82
const uint8_t * bytestream_end
Definition: cabac.h:49
#define CABAC_BITS
Definition: cabac.h:40
static void renorm_cabac_decoder_once(CABACContext *c)
static int av_unused get_cabac(CABACContext *c, uint8_t *const state)
static const uint8_t *const ff_h264_last_coeff_flag_offset_8x8
static int av_noinline av_unused get_cabac_noinline(CABACContext *c, uint8_t *const state)
const uint8_t * bytestream
Definition: cabac.h:48
uint8_t
static av_unused const uint8_t * skip_bytes(CABACContext *c, int n)
Skip n bytes and reset the decoder.
static av_always_inline int get_cabac_inline(CABACContext *c, uint8_t *const state)
static const uint16_t mask[17]
Definition: lzw.c:38
const uint8_t * bytestream_start
Definition: cabac.h:47
#define H264_LAST_COEFF_FLAG_OFFSET_8x8_OFFSET
Definition: cabac.h:38
int ff_init_cabac_decoder(CABACContext *c, const uint8_t *buf, int buf_size)
Definition: cabac.c:178
int n
Definition: avisynth_c.h:547
static int av_unused get_cabac_terminate(CABACContext *c)
static void refill(CABACContext *c)
int range
Definition: cabac.h:45
#define H264_MLPS_STATE_OFFSET
Definition: cabac.h:37
const uint8_t ff_h264_cabac_tables[512+4 *2 *64+4 *64+63]
Definition: cabac.c:35
static av_always_inline int get_cabac_bypass_sign(CABACContext *c, int val)
static int av_unused get_cabac_bypass(CABACContext *c)
#define H264_NORM_SHIFT_OFFSET
Definition: cabac.h:35
int low
Definition: cabac.h:44
static double c[64]
static void refill2(CABACContext *c)
static const uint8_t *const ff_h264_lps_range
#define av_noinline
Definition: attributes.h:62
static struct @205 state
#define av_always_inline
Definition: attributes.h:39
static const uint8_t *const ff_h264_mlps_state
#define CABAC_MASK
Definition: cabac.h:41
#define av_unused
Definition: attributes.h:126
Context Adaptive Binary Arithmetic Coder.
static const uint8_t *const ff_h264_norm_shift
#define H264_LPS_RANGE_OFFSET
Definition: cabac.h:36