00001 /* 00002 * H.26L/H.264/AVC/JVT/14496-10/... encoder/decoder 00003 * Copyright (c) 2003 Michael Niedermayer <michaelni@gmx.at> 00004 * 00005 * This file is part of FFmpeg. 00006 * 00007 * FFmpeg is free software; you can redistribute it and/or 00008 * modify it under the terms of the GNU Lesser General Public 00009 * License as published by the Free Software Foundation; either 00010 * version 2.1 of the License, or (at your option) any later version. 00011 * 00012 * FFmpeg is distributed in the hope that it will be useful, 00013 * but WITHOUT ANY WARRANTY; without even the implied warranty of 00014 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU 00015 * Lesser General Public License for more details. 00016 * 00017 * You should have received a copy of the GNU Lesser General Public 00018 * License along with FFmpeg; if not, write to the Free Software 00019 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA 00020 */ 00021 00027 #ifndef AVCODEC_CABAC_FUNCTIONS_H 00028 #define AVCODEC_CABAC_FUNCTIONS_H 00029 00030 #include <stdint.h> 00031 00032 #include "cabac.h" 00033 #include "config.h" 00034 00035 #if ARCH_X86 00036 # include "x86/cabac.h" 00037 #endif 00038 00039 extern uint8_t ff_h264_cabac_tables[512 + 4*2*64 + 4*64 + 63]; 00040 static uint8_t * const ff_h264_norm_shift = ff_h264_cabac_tables + H264_NORM_SHIFT_OFFSET; 00041 static uint8_t * const ff_h264_lps_range = ff_h264_cabac_tables + H264_LPS_RANGE_OFFSET; 00042 static uint8_t * const ff_h264_mlps_state = ff_h264_cabac_tables + H264_MLPS_STATE_OFFSET; 00043 static uint8_t * const ff_h264_last_coeff_flag_offset_8x8 = ff_h264_cabac_tables + H264_LAST_COEFF_FLAG_OFFSET_8x8_OFFSET; 00044 00045 static void refill(CABACContext *c){ 00046 #if CABAC_BITS == 16 00047 c->low+= (c->bytestream[0]<<9) + (c->bytestream[1]<<1); 00048 #else 00049 c->low+= c->bytestream[0]<<1; 00050 #endif 00051 c->low -= CABAC_MASK; 00052 c->bytestream += CABAC_BITS / 8; 00053 } 00054 00055 static inline void renorm_cabac_decoder_once(CABACContext *c){ 00056 int shift= (uint32_t)(c->range - 0x100)>>31; 00057 c->range<<= shift; 00058 c->low <<= shift; 00059 if(!(c->low & CABAC_MASK)) 00060 refill(c); 00061 } 00062 00063 #ifndef get_cabac_inline 00064 static void refill2(CABACContext *c){ 00065 int i, x; 00066 00067 x= c->low ^ (c->low-1); 00068 i= 7 - ff_h264_norm_shift[x>>(CABAC_BITS-1)]; 00069 00070 x= -CABAC_MASK; 00071 00072 #if CABAC_BITS == 16 00073 x+= (c->bytestream[0]<<9) + (c->bytestream[1]<<1); 00074 #else 00075 x+= c->bytestream[0]<<1; 00076 #endif 00077 00078 c->low += x<<i; 00079 c->bytestream += CABAC_BITS/8; 00080 } 00081 00082 static av_always_inline int get_cabac_inline(CABACContext *c, uint8_t * const state){ 00083 int s = *state; 00084 int RangeLPS= ff_h264_lps_range[2*(c->range&0xC0) + s]; 00085 int bit, lps_mask; 00086 00087 c->range -= RangeLPS; 00088 lps_mask= ((c->range<<(CABAC_BITS+1)) - c->low)>>31; 00089 00090 c->low -= (c->range<<(CABAC_BITS+1)) & lps_mask; 00091 c->range += (RangeLPS - c->range) & lps_mask; 00092 00093 s^=lps_mask; 00094 *state= (ff_h264_mlps_state+128)[s]; 00095 bit= s&1; 00096 00097 lps_mask= ff_h264_norm_shift[c->range]; 00098 c->range<<= lps_mask; 00099 c->low <<= lps_mask; 00100 if(!(c->low & CABAC_MASK)) 00101 refill2(c); 00102 return bit; 00103 } 00104 #endif 00105 00106 static int av_noinline av_unused get_cabac_noinline(CABACContext *c, uint8_t * const state){ 00107 return get_cabac_inline(c,state); 00108 } 00109 00110 static int av_unused get_cabac(CABACContext *c, uint8_t * const state){ 00111 return get_cabac_inline(c,state); 00112 } 00113 00114 static int av_unused get_cabac_bypass(CABACContext *c){ 00115 int range; 00116 c->low += c->low; 00117 00118 if(!(c->low & CABAC_MASK)) 00119 refill(c); 00120 00121 range= c->range<<(CABAC_BITS+1); 00122 if(c->low < range){ 00123 return 0; 00124 }else{ 00125 c->low -= range; 00126 return 1; 00127 } 00128 } 00129 00130 00131 #ifndef get_cabac_bypass_sign 00132 static av_always_inline int get_cabac_bypass_sign(CABACContext *c, int val){ 00133 int range, mask; 00134 c->low += c->low; 00135 00136 if(!(c->low & CABAC_MASK)) 00137 refill(c); 00138 00139 range= c->range<<(CABAC_BITS+1); 00140 c->low -= range; 00141 mask= c->low >> 31; 00142 range &= mask; 00143 c->low += range; 00144 return (val^mask)-mask; 00145 } 00146 #endif 00147 00152 static int av_unused get_cabac_terminate(CABACContext *c){ 00153 c->range -= 2; 00154 if(c->low < c->range<<(CABAC_BITS+1)){ 00155 renorm_cabac_decoder_once(c); 00156 return 0; 00157 }else{ 00158 return c->bytestream - c->bytestream_start; 00159 } 00160 } 00161 00162 #endif /* AVCODEC_CABAC_FUNCTIONS_H */