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 static void refill(CABACContext *c){
55 #if CABAC_BITS == 16
56  c->low+= (c->bytestream[0]<<9) + (c->bytestream[1]<<1);
57 #else
58  c->low+= c->bytestream[0]<<1;
59 #endif
60  c->low -= CABAC_MASK;
61 #if !UNCHECKED_BITSTREAM_READER
62  if (c->bytestream < c->bytestream_end)
63 #endif
64  c->bytestream += CABAC_BITS / 8;
65 }
66 
68  int shift= (uint32_t)(c->range - 0x100)>>31;
69  c->range<<= shift;
70  c->low <<= shift;
71  if(!(c->low & CABAC_MASK))
72  refill(c);
73 }
74 
75 #ifndef get_cabac_inline
76 static void refill2(CABACContext *c){
77  int i, x;
78 
79  x= c->low ^ (c->low-1);
80  i= 7 - ff_h264_norm_shift[x>>(CABAC_BITS-1)];
81 
82  x= -CABAC_MASK;
83 
84 #if CABAC_BITS == 16
85  x+= (c->bytestream[0]<<9) + (c->bytestream[1]<<1);
86 #else
87  x+= c->bytestream[0]<<1;
88 #endif
89 
90  c->low += x<<i;
91 #if !UNCHECKED_BITSTREAM_READER
92  if (c->bytestream < c->bytestream_end)
93 #endif
94  c->bytestream += CABAC_BITS/8;
95 }
96 
98  int s = *state;
99  int RangeLPS= ff_h264_lps_range[2*(c->range&0xC0) + s];
100  int bit, lps_mask;
101 
102  c->range -= RangeLPS;
103  lps_mask= ((c->range<<(CABAC_BITS+1)) - c->low)>>31;
104 
105  c->low -= (c->range<<(CABAC_BITS+1)) & lps_mask;
106  c->range += (RangeLPS - c->range) & lps_mask;
107 
108  s^=lps_mask;
109  *state= (ff_h264_mlps_state+128)[s];
110  bit= s&1;
111 
112  lps_mask= ff_h264_norm_shift[c->range];
113  c->range<<= lps_mask;
114  c->low <<= lps_mask;
115  if(!(c->low & CABAC_MASK))
116  refill2(c);
117  return bit;
118 }
119 #endif
120 
122  return get_cabac_inline(c,state);
123 }
124 
126  return get_cabac_inline(c,state);
127 }
128 
129 #ifndef get_cabac_bypass
131  int range;
132  c->low += c->low;
133 
134  if(!(c->low & CABAC_MASK))
135  refill(c);
136 
137  range= c->range<<(CABAC_BITS+1);
138  if(c->low < range){
139  return 0;
140  }else{
141  c->low -= range;
142  return 1;
143  }
144 }
145 #endif
146 
147 #ifndef get_cabac_bypass_sign
149  int range, mask;
150  c->low += c->low;
151 
152  if(!(c->low & CABAC_MASK))
153  refill(c);
154 
155  range= c->range<<(CABAC_BITS+1);
156  c->low -= range;
157  mask= c->low >> 31;
158  range &= mask;
159  c->low += range;
160  return (val^mask)-mask;
161 }
162 #endif
163 
164 /**
165  *
166  * @return the number of bytes read or 0 if no end
167  */
169  c->range -= 2;
170  if(c->low < c->range<<(CABAC_BITS+1)){
172  return 0;
173  }else{
174  return c->bytestream - c->bytestream_start;
175  }
176 }
177 
178 /**
179  * Skip @p n bytes and reset the decoder.
180  * @return the address of the first skipped byte or NULL if there's less than @p n bytes left
181  */
182 static av_unused const uint8_t* skip_bytes(CABACContext *c, int n) {
183  const uint8_t *ptr = c->bytestream;
184 
185  if (c->low & 0x1)
186  ptr--;
187 #if CABAC_BITS == 16
188  if (c->low & 0x1FF)
189  ptr--;
190 #endif
191  if ((int) (c->bytestream_end - ptr) < n)
192  return NULL;
193  ff_init_cabac_decoder(c, ptr + n, c->bytestream_end - ptr - n);
194 
195  return ptr;
196 }
197 
198 #endif /* AVCODEC_CABAC_FUNCTIONS_H */
#define NULL
Definition: coverity.c:32
const char const char void * val
Definition: avisynth_c.h:634
const char * s
Definition: avisynth_c.h:631
static int shift(int a, int b)
Definition: sonic.c:82
CABAC_TABLE_CONST uint8_t ff_h264_cabac_tables[512+4 *2 *64+4 *64+63]
const uint8_t * bytestream_end
Definition: cabac.h:54
#define CABAC_BITS
Definition: cabac.h:45
static CABAC_TABLE_CONST uint8_t *const ff_h264_mlps_state
static void renorm_cabac_decoder_once(CABACContext *c)
static int av_unused get_cabac(CABACContext *c, uint8_t *const state)
static int av_noinline av_unused get_cabac_noinline(CABACContext *c, uint8_t *const state)
const uint8_t * bytestream
Definition: cabac.h:53
uint8_t
static av_unused const uint8_t * skip_bytes(CABACContext *c, int n)
Skip n bytes and reset the decoder.
static CABAC_TABLE_CONST uint8_t *const ff_h264_lps_range
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:52
#define H264_LAST_COEFF_FLAG_OFFSET_8x8_OFFSET
Definition: cabac.h:43
int n
Definition: avisynth_c.h:547
static int av_unused get_cabac_terminate(CABACContext *c)
static void refill(CABACContext *c)
static CABAC_TABLE_CONST uint8_t *const ff_h264_last_coeff_flag_offset_8x8
int range
Definition: cabac.h:50
#define H264_MLPS_STATE_OFFSET
Definition: cabac.h:42
static uint32_t state
Definition: trasher.c:27
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:40
int low
Definition: cabac.h:49
static CABAC_TABLE_CONST uint8_t *const ff_h264_norm_shift
static double c[64]
#define CABAC_TABLE_CONST
Definition: cabac.h:37
static void refill2(CABACContext *c)
void ff_init_cabac_decoder(CABACContext *c, const uint8_t *buf, int buf_size)
Definition: cabac.c:54
#define av_noinline
Definition: attributes.h:54
#define av_always_inline
Definition: attributes.h:37
#define CABAC_MASK
Definition: cabac.h:46
#define av_unused
Definition: attributes.h:118
Context Adaptive Binary Arithmetic Coder.
#define H264_LPS_RANGE_OFFSET
Definition: cabac.h:41