Libav
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 Libav.
6  *
7  * Libav 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  * Libav 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 Libav; if not, write to the Free Software
19  * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
20  */
21 
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 (c->bytestream < c->bytestream_end)
58  c->bytestream += CABAC_BITS / 8;
59 }
60 
61 static inline void renorm_cabac_decoder_once(CABACContext *c){
62  int shift= (uint32_t)(c->range - 0x100)>>31;
63  c->range<<= shift;
64  c->low <<= shift;
65  if(!(c->low & CABAC_MASK))
66  refill(c);
67 }
68 
69 #ifndef get_cabac_inline
70 static void refill2(CABACContext *c){
71  int i, x;
72 
73  x= c->low ^ (c->low-1);
74  i= 7 - ff_h264_norm_shift[x>>(CABAC_BITS-1)];
75 
76  x= -CABAC_MASK;
77 
78 #if CABAC_BITS == 16
79  x+= (c->bytestream[0]<<9) + (c->bytestream[1]<<1);
80 #else
81  x+= c->bytestream[0]<<1;
82 #endif
83 
84  c->low += x<<i;
85  if (c->bytestream < c->bytestream_end)
86  c->bytestream += CABAC_BITS/8;
87 }
88 
90  int s = *state;
91  int RangeLPS= ff_h264_lps_range[2*(c->range&0xC0) + s];
92  int bit, lps_mask;
93 
94  c->range -= RangeLPS;
95  lps_mask= ((c->range<<(CABAC_BITS+1)) - c->low)>>31;
96 
97  c->low -= (c->range<<(CABAC_BITS+1)) & lps_mask;
98  c->range += (RangeLPS - c->range) & lps_mask;
99 
100  s^=lps_mask;
101  *state= (ff_h264_mlps_state+128)[s];
102  bit= s&1;
103 
104  lps_mask= ff_h264_norm_shift[c->range];
105  c->range<<= lps_mask;
106  c->low <<= lps_mask;
107  if(!(c->low & CABAC_MASK))
108  refill2(c);
109  return bit;
110 }
111 #endif
112 
114  return get_cabac_inline(c,state);
115 }
116 
117 static int av_unused get_cabac(CABACContext *c, uint8_t * const state){
118  return get_cabac_inline(c,state);
119 }
120 
121 #ifndef get_cabac_bypass
123  int range;
124  c->low += c->low;
125 
126  if(!(c->low & CABAC_MASK))
127  refill(c);
128 
129  range= c->range<<(CABAC_BITS+1);
130  if(c->low < range){
131  return 0;
132  }else{
133  c->low -= range;
134  return 1;
135  }
136 }
137 #endif
138 
139 #ifndef get_cabac_bypass_sign
141  int range, mask;
142  c->low += c->low;
143 
144  if(!(c->low & CABAC_MASK))
145  refill(c);
146 
147  range= c->range<<(CABAC_BITS+1);
148  c->low -= range;
149  mask= c->low >> 31;
150  range &= mask;
151  c->low += range;
152  return (val^mask)-mask;
153 }
154 #endif
155 
161  c->range -= 2;
162  if(c->low < c->range<<(CABAC_BITS+1)){
164  return 0;
165  }else{
166  return c->bytestream - c->bytestream_start;
167  }
168 }
169 
174 static av_unused const uint8_t* skip_bytes(CABACContext *c, int n) {
175  const uint8_t *ptr = c->bytestream;
176 
177  if (c->low & 0x1)
178  ptr--;
179 #if CABAC_BITS == 16
180  if (c->low & 0x1FF)
181  ptr--;
182 #endif
183  if ((int) (c->bytestream_end - ptr) < n)
184  return NULL;
185  ff_init_cabac_decoder(c, ptr + n, c->bytestream_end - ptr - n);
186 
187  return ptr;
188 }
189 
190 #endif /* AVCODEC_CABAC_FUNCTIONS_H */
const uint8_t * bytestream_end
Definition: cabac.h:48
#define CABAC_BITS
Definition: cabac.h:40
static uint8_t *const ff_h264_lps_range
static void renorm_cabac_decoder_once(CABACContext *c)
static uint8_t *const ff_h264_mlps_state
uint8_t ff_h264_cabac_tables[512+4 *2 *64+4 *64+63]
Definition: cabac.c:34
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:47
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:46
#define H264_LAST_COEFF_FLAG_OFFSET_8x8_OFFSET
Definition: cabac.h:38
static int av_unused get_cabac_terminate(CABACContext *c)
static void refill(CABACContext *c)
NULL
Definition: eval.c:55
static uint8_t *const ff_h264_norm_shift
int range
Definition: cabac.h:45
#define H264_MLPS_STATE_OFFSET
Definition: cabac.h:37
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:35
int low
Definition: cabac.h:44
static void refill2(CABACContext *c)
void ff_init_cabac_decoder(CABACContext *c, const uint8_t *buf, int buf_size)
Definition: cabac.c:109
#define av_noinline
Definition: attributes.h:48
static uint8_t *const ff_h264_last_coeff_flag_offset_8x8
#define av_always_inline
Definition: attributes.h:40
#define CABAC_MASK
Definition: cabac.h:41
#define av_unused
Definition: attributes.h:86
Context Adaptive Binary Arithmetic Coder.
#define H264_LPS_RANGE_OFFSET
Definition: cabac.h:36