2 * H.26L/H.264/AVC/JVT/14496-10/... encoder/decoder
3 * Copyright (c) 2003 Michael Niedermayer <michaelni@gmx.at>
5 * This file is part of FFmpeg.
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.
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.
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
24 * Context Adaptive Binary Arithmetic Coder inline functions
27 #ifndef AVCODEC_CABAC_FUNCTIONS_H
28 #define AVCODEC_CABAC_FUNCTIONS_H
36 #ifndef UNCHECKED_BITSTREAM_READER
37 #define UNCHECKED_BITSTREAM_READER !CONFIG_SAFE_BITSTREAM_READER
41 # include "aarch64/cabac.h"
44 # include "arm/cabac.h"
47 # include "x86/cabac.h"
50 # include "mips/cabac.h"
53 static const uint8_t * const ff_h264_norm_shift = ff_h264_cabac_tables + H264_NORM_SHIFT_OFFSET;
54 static const uint8_t * const ff_h264_lps_range = ff_h264_cabac_tables + H264_LPS_RANGE_OFFSET;
55 static const uint8_t * const ff_h264_mlps_state = ff_h264_cabac_tables + H264_MLPS_STATE_OFFSET;
56 static const uint8_t * const ff_h264_last_coeff_flag_offset_8x8 = ff_h264_cabac_tables + H264_LAST_COEFF_FLAG_OFFSET_8x8_OFFSET;
58 #if !defined(get_cabac_bypass) || !defined(get_cabac_terminate)
59 static void refill(CABACContext *c){
61 c->low+= (c->bytestream[0]<<9) + (c->bytestream[1]<<1);
63 c->low+= c->bytestream[0]<<1;
66 #if !UNCHECKED_BITSTREAM_READER
67 if (c->bytestream < c->bytestream_end)
69 c->bytestream += CABAC_BITS / 8;
73 #ifndef get_cabac_terminate
74 static inline void renorm_cabac_decoder_once(CABACContext *c){
75 int shift= (uint32_t)(c->range - 0x100)>>31;
78 if(!(c->low & CABAC_MASK))
83 #ifndef get_cabac_inline
84 static void refill2(CABACContext *c){
88 x= c->low ^ (c->low-1);
89 i= 7 - ff_h264_norm_shift[x>>(CABAC_BITS-1)];
91 i = ff_ctz(c->low) - CABAC_BITS;
97 x+= (c->bytestream[0]<<9) + (c->bytestream[1]<<1);
99 x+= c->bytestream[0]<<1;
103 #if !UNCHECKED_BITSTREAM_READER
104 if (c->bytestream < c->bytestream_end)
106 c->bytestream += CABAC_BITS/8;
110 #ifndef get_cabac_inline
111 static av_always_inline int get_cabac_inline(CABACContext *c, uint8_t * const state){
113 int RangeLPS= ff_h264_lps_range[2*(c->range&0xC0) + s];
116 c->range -= RangeLPS;
117 lps_mask= ((c->range<<(CABAC_BITS+1)) - c->low)>>31;
119 c->low -= (c->range<<(CABAC_BITS+1)) & lps_mask;
120 c->range += (RangeLPS - c->range) & lps_mask;
123 *state= (ff_h264_mlps_state+128)[s];
126 lps_mask= ff_h264_norm_shift[c->range];
127 c->range<<= lps_mask;
129 if(!(c->low & CABAC_MASK))
135 static int av_noinline av_unused get_cabac_noinline(CABACContext *c, uint8_t * const state){
136 return get_cabac_inline(c,state);
139 static int av_unused get_cabac(CABACContext *c, uint8_t * const state){
140 return get_cabac_inline(c,state);
143 #ifndef get_cabac_bypass
144 static int av_unused get_cabac_bypass(CABACContext *c){
148 if(!(c->low & CABAC_MASK))
151 range= c->range<<(CABAC_BITS+1);
161 #ifndef get_cabac_bypass_sign
162 static av_always_inline int get_cabac_bypass_sign(CABACContext *c, int val){
166 if(!(c->low & CABAC_MASK))
169 range= c->range<<(CABAC_BITS+1);
174 return (val^mask)-mask;
179 * @return the number of bytes read or 0 if no end
181 #ifndef get_cabac_terminate
182 static int av_unused get_cabac_terminate(CABACContext *c){
184 if(c->low < c->range<<(CABAC_BITS+1)){
185 renorm_cabac_decoder_once(c);
188 return c->bytestream - c->bytestream_start;
194 * Skip @p n bytes and reset the decoder.
195 * @return the address of the first skipped byte or NULL if there's less than @p n bytes left
198 static av_unused const uint8_t* skip_bytes(CABACContext *c, int n) {
199 const uint8_t *ptr = c->bytestream;
207 if ((int) (c->bytestream_end - ptr) < n)
209 if (ff_init_cabac_decoder(c, ptr + n, c->bytestream_end - ptr - n) < 0)
216 #endif /* AVCODEC_CABAC_FUNCTIONS_H */