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