FFmpeg
 All Data Structures 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  c->bytestream += CABAC_BITS / 8;
53 }
54 
56  int shift= (uint32_t)(c->range - 0x100)>>31;
57  c->range<<= shift;
58  c->low <<= shift;
59  if(!(c->low & CABAC_MASK))
60  refill(c);
61 }
62 
63 #ifndef get_cabac_inline
64 static void refill2(CABACContext *c){
65  int i, x;
66 
67  x= c->low ^ (c->low-1);
68  i= 7 - ff_h264_norm_shift[x>>(CABAC_BITS-1)];
69 
70  x= -CABAC_MASK;
71 
72 #if CABAC_BITS == 16
73  x+= (c->bytestream[0]<<9) + (c->bytestream[1]<<1);
74 #else
75  x+= c->bytestream[0]<<1;
76 #endif
77 
78  c->low += x<<i;
79  c->bytestream += CABAC_BITS/8;
80 }
81 
83  int s = *state;
84  int RangeLPS= ff_h264_lps_range[2*(c->range&0xC0) + s];
85  int bit, lps_mask;
86 
87  c->range -= RangeLPS;
88  lps_mask= ((c->range<<(CABAC_BITS+1)) - c->low)>>31;
89 
90  c->low -= (c->range<<(CABAC_BITS+1)) & lps_mask;
91  c->range += (RangeLPS - c->range) & lps_mask;
92 
93  s^=lps_mask;
94  *state= (ff_h264_mlps_state+128)[s];
95  bit= s&1;
96 
97  lps_mask= ff_h264_norm_shift[c->range];
98  c->range<<= lps_mask;
99  c->low <<= lps_mask;
100  if(!(c->low & CABAC_MASK))
101  refill2(c);
102  return bit;
103 }
104 #endif
105 
107  return get_cabac_inline(c,state);
108 }
109 
111  return get_cabac_inline(c,state);
112 }
113 
115  int range;
116  c->low += c->low;
117 
118  if(!(c->low & CABAC_MASK))
119  refill(c);
120 
121  range= c->range<<(CABAC_BITS+1);
122  if(c->low < range){
123  return 0;
124  }else{
125  c->low -= range;
126  return 1;
127  }
128 }
129 
130 
131 #ifndef get_cabac_bypass_sign
133  int range, mask;
134  c->low += c->low;
135 
136  if(!(c->low & CABAC_MASK))
137  refill(c);
138 
139  range= c->range<<(CABAC_BITS+1);
140  c->low -= range;
141  mask= c->low >> 31;
142  range &= mask;
143  c->low += range;
144  return (val^mask)-mask;
145 }
146 #endif
147 
148 /**
149  *
150  * @return the number of bytes read or 0 if no end
151  */
153  c->range -= 2;
154  if(c->low < c->range<<(CABAC_BITS+1)){
156  return 0;
157  }else{
158  return c->bytestream - c->bytestream_start;
159  }
160 }
161 
162 #endif /* AVCODEC_CABAC_FUNCTIONS_H */