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 #if ARCH_X86
36 # include "x86/cabac.h"
37 #endif
38 
39 extern uint8_t ff_h264_cabac_tables[512 + 4*2*64 + 4*64 + 63];
44 
45 static void refill(CABACContext *c){
46 #if CABAC_BITS == 16
47  c->low+= (c->bytestream[0]<<9) + (c->bytestream[1]<<1);
48 #else
49  c->low+= c->bytestream[0]<<1;
50 #endif
51  c->low -= CABAC_MASK;
52 #if !UNCHECKED_BITSTREAM_READER
53  if (c->bytestream < c->bytestream_end)
54 #endif
55  c->bytestream += CABAC_BITS / 8;
56 }
57 
59  int shift= (uint32_t)(c->range - 0x100)>>31;
60  c->range<<= shift;
61  c->low <<= shift;
62  if(!(c->low & CABAC_MASK))
63  refill(c);
64 }
65 
66 #ifndef get_cabac_inline
67 static void refill2(CABACContext *c){
68  int i, x;
69 
70  x= c->low ^ (c->low-1);
71  i= 7 - ff_h264_norm_shift[x>>(CABAC_BITS-1)];
72 
73  x= -CABAC_MASK;
74 
75 #if CABAC_BITS == 16
76  x+= (c->bytestream[0]<<9) + (c->bytestream[1]<<1);
77 #else
78  x+= c->bytestream[0]<<1;
79 #endif
80 
81  c->low += x<<i;
82 #if !UNCHECKED_BITSTREAM_READER
83  if (c->bytestream < c->bytestream_end)
84 #endif
85  c->bytestream += CABAC_BITS/8;
86 }
87 
89  int s = *state;
90  int RangeLPS= ff_h264_lps_range[2*(c->range&0xC0) + s];
91  int bit, lps_mask;
92 
93  c->range -= RangeLPS;
94  lps_mask= ((c->range<<(CABAC_BITS+1)) - c->low)>>31;
95 
96  c->low -= (c->range<<(CABAC_BITS+1)) & lps_mask;
97  c->range += (RangeLPS - c->range) & lps_mask;
98 
99  s^=lps_mask;
100  *state= (ff_h264_mlps_state+128)[s];
101  bit= s&1;
102 
103  lps_mask= ff_h264_norm_shift[c->range];
104  c->range<<= lps_mask;
105  c->low <<= lps_mask;
106  if(!(c->low & CABAC_MASK))
107  refill2(c);
108  return bit;
109 }
110 #endif
111 
113  return get_cabac_inline(c,state);
114 }
115 
117  return get_cabac_inline(c,state);
118 }
119 
120 #ifndef get_cabac_bypass
122  int range;
123  c->low += c->low;
124 
125  if(!(c->low & CABAC_MASK))
126  refill(c);
127 
128  range= c->range<<(CABAC_BITS+1);
129  if(c->low < range){
130  return 0;
131  }else{
132  c->low -= range;
133  return 1;
134  }
135 }
136 #endif
137 
138 #ifndef get_cabac_bypass_sign
140  int range, mask;
141  c->low += c->low;
142 
143  if(!(c->low & CABAC_MASK))
144  refill(c);
145 
146  range= c->range<<(CABAC_BITS+1);
147  c->low -= range;
148  mask= c->low >> 31;
149  range &= mask;
150  c->low += range;
151  return (val^mask)-mask;
152 }
153 #endif
154 
155 /**
156  *
157  * @return the number of bytes read or 0 if no end
158  */
160  c->range -= 2;
161  if(c->low < c->range<<(CABAC_BITS+1)){
163  return 0;
164  }else{
165  return c->bytestream - c->bytestream_start;
166  }
167 }
168 
169 /**
170  * Skip @p n bytes and reset the decoder.
171  * @return the address of the first skipped byte or NULL if there's less than @p n bytes left
172  */
173 static av_unused const uint8_t* skip_bytes(CABACContext *c, int n) {
174  const uint8_t *ptr = c->bytestream;
175 
176  if (c->low & 0x1)
177  ptr--;
178 #if CABAC_BITS == 16
179  if (c->low & 0x1FF)
180  ptr--;
181 #endif
182  if ((int) (c->bytestream_end - ptr) < n)
183  return NULL;
184  ff_init_cabac_decoder(c, ptr + n, c->bytestream_end - ptr - n);
185 
186  return ptr;
187 }
188 
189 #endif /* AVCODEC_CABAC_FUNCTIONS_H */