diff libavcodec/cabac.h @ 2:897f711a7157

rearrange to work with autoconf
author Nina Engelhardt <nengel@mailbox.tu-berlin.de>
date Tue, 25 Sep 2012 15:55:33 +0200
parents
children
line diff
     1.1 --- /dev/null	Thu Jan 01 00:00:00 1970 +0000
     1.2 +++ b/libavcodec/cabac.h	Tue Sep 25 15:55:33 2012 +0200
     1.3 @@ -0,0 +1,206 @@
     1.4 +/*
     1.5 + * H.26L/H.264/AVC/JVT/14496-10/... encoder/decoder
     1.6 + * Copyright (c) 2003 Michael Niedermayer <michaelni@gmx.at>
     1.7 + *
     1.8 + * This file is part of FFmpeg.
     1.9 + *
    1.10 + * FFmpeg is free software; you can redistribute it and/or
    1.11 + * modify it under the terms of the GNU Lesser General Public
    1.12 + * License as published by the Free Software Foundation; either
    1.13 + * version 2.1 of the License, or (at your option) any later version.
    1.14 + *
    1.15 + * FFmpeg is distributed in the hope that it will be useful,
    1.16 + * but WITHOUT ANY WARRANTY; without even the implied warranty of
    1.17 + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
    1.18 + * Lesser General Public License for more details.
    1.19 + *
    1.20 + * You should have received a copy of the GNU Lesser General Public
    1.21 + * License along with FFmpeg; if not, write to the Free Software
    1.22 + * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
    1.23 + */
    1.24 +
    1.25 +/**
    1.26 + * @file
    1.27 + * Context Adaptive Binary Arithmetic Coder.
    1.28 + */
    1.29 +
    1.30 +#ifndef AVCODEC_CABAC_H
    1.31 +#define AVCODEC_CABAC_H
    1.32 +
    1.33 +//#undef NDEBUG
    1.34 +#include <assert.h>
    1.35 +#include "libavutil/x86_cpu.h"
    1.36 +#include "libavutil/attributes.h"
    1.37 +
    1.38 +#define CABAC_BITS 16
    1.39 +#define CABAC_MASK ((1<<CABAC_BITS)-1)
    1.40 +#define BRANCHLESS_CABAC_DECODER 1
    1.41 +
    1.42 +typedef struct CABACContext{
    1.43 +    int low;
    1.44 +    int range;
    1.45 +    int outstanding_count;
    1.46 +#ifdef STRICT_LIMITS
    1.47 +    int symCount;
    1.48 +#endif
    1.49 +    const uint8_t *bytestream_start;
    1.50 +    const uint8_t *bytestream;
    1.51 +    const uint8_t *bytestream_end;
    1.52 +    uint8_t  cabac_state[460];
    1.53 +}CABACContext;
    1.54 +
    1.55 +extern uint8_t ff_h264_mlps_state[4*64];
    1.56 +extern uint8_t ff_h264_lps_range[4*2*64];  ///< rangeTabLPS
    1.57 +extern uint8_t ff_h264_mps_state[2*64];     ///< transIdxMPS
    1.58 +extern uint8_t ff_h264_lps_state[2*64];     ///< transIdxLPS
    1.59 +extern const uint8_t ff_h264_norm_shift[512];
    1.60 +
    1.61 +void ff_init_cabac_decoder(CABACContext *c, const uint8_t *buf, int buf_size);
    1.62 +void ff_init_cabac_states(void);
    1.63 +
    1.64 +static void refill(CABACContext *c){
    1.65 +#if CABAC_BITS == 16
    1.66 +        c->low+= (c->bytestream[0]<<9) + (c->bytestream[1]<<1);
    1.67 +#else
    1.68 +        c->low+= c->bytestream[0]<<1;
    1.69 +#endif
    1.70 +    c->low -= CABAC_MASK;
    1.71 +    c->bytestream+= CABAC_BITS/8;
    1.72 +}
    1.73 +
    1.74 +static void refill2(CABACContext *c){
    1.75 +    int i, x;
    1.76 +
    1.77 +    x= c->low ^ (c->low-1);
    1.78 +    i= 7 - ff_h264_norm_shift[x>>(CABAC_BITS-1)];
    1.79 +
    1.80 +    x= -CABAC_MASK;
    1.81 +
    1.82 +#if CABAC_BITS == 16
    1.83 +        x+= (c->bytestream[0]<<9) + (c->bytestream[1]<<1);
    1.84 +#else
    1.85 +        x+= c->bytestream[0]<<1;
    1.86 +#endif
    1.87 +
    1.88 +    c->low += x<<i;
    1.89 +    c->bytestream+= CABAC_BITS/8;
    1.90 +}
    1.91 +
    1.92 +static inline void renorm_cabac_decoder(CABACContext *c){
    1.93 +    while(c->range < 0x100){
    1.94 +        c->range+= c->range;
    1.95 +        c->low+= c->low;
    1.96 +        if(!(c->low & CABAC_MASK))
    1.97 +            refill(c);
    1.98 +    }
    1.99 +}
   1.100 +
   1.101 +static inline void renorm_cabac_decoder_once(CABACContext *c){
   1.102 +
   1.103 +    int shift= (uint32_t)(c->range - 0x100)>>31;
   1.104 +    c->range<<= shift;
   1.105 +    c->low  <<= shift;
   1.106 +
   1.107 +    if(!(c->low & CABAC_MASK))
   1.108 +        refill(c);
   1.109 +}
   1.110 +
   1.111 +static av_always_inline int get_cabac_inline(CABACContext *c, uint8_t * const state){
   1.112 +
   1.113 +    int s = *state;
   1.114 +    int RangeLPS= ff_h264_lps_range[2*(c->range&0xC0) + s];
   1.115 +    int bit, lps_mask av_unused;
   1.116 +
   1.117 +    c->range -= RangeLPS;
   1.118 +#ifndef BRANCHLESS_CABAC_DECODER
   1.119 +    if(c->low < (c->range<<(CABAC_BITS+1))){
   1.120 +        bit= s&1;
   1.121 +        *state= ff_h264_mps_state[s];
   1.122 +        renorm_cabac_decoder_once(c);
   1.123 +    }else{
   1.124 +        bit= ff_h264_norm_shift[RangeLPS];
   1.125 +        c->low -= (c->range<<(CABAC_BITS+1));
   1.126 +        *state= ff_h264_lps_state[s];
   1.127 +        c->range = RangeLPS<<bit;
   1.128 +        c->low <<= bit;
   1.129 +        bit= (s&1)^1;
   1.130 +
   1.131 +        if(!(c->low & CABAC_MASK)){
   1.132 +            refill2(c);
   1.133 +        }
   1.134 +    }
   1.135 +#else /* BRANCHLESS_CABAC_DECODER */
   1.136 +    lps_mask= ((c->range<<(CABAC_BITS+1)) - c->low)>>31;
   1.137 +
   1.138 +    c->low -= (c->range<<(CABAC_BITS+1)) & lps_mask;
   1.139 +    c->range += (RangeLPS - c->range) & lps_mask;
   1.140 +
   1.141 +    s^=lps_mask;
   1.142 +    *state= (ff_h264_mlps_state+128)[s];
   1.143 +    bit= s&1;
   1.144 +
   1.145 +    lps_mask= ff_h264_norm_shift[c->range];
   1.146 +    c->range<<= lps_mask;
   1.147 +    c->low  <<= lps_mask;
   1.148 +    if(!(c->low & CABAC_MASK))
   1.149 +        refill2(c);
   1.150 +#endif /* BRANCHLESS_CABAC_DECODER */
   1.151 +
   1.152 +    return bit;
   1.153 +}
   1.154 +
   1.155 +static int av_noinline av_unused get_cabac_noinline(CABACContext *c, uint8_t * const state){
   1.156 +    return get_cabac_inline(c, state);
   1.157 +}
   1.158 +
   1.159 +static int av_unused get_cabac(CABACContext *c, uint8_t * const state){
   1.160 +    return get_cabac_inline(c, state);
   1.161 +}
   1.162 +
   1.163 +static int av_unused get_cabac_bypass(CABACContext *c){
   1.164 +
   1.165 +    int range;
   1.166 +    c->low += c->low;
   1.167 +
   1.168 +    if(!(c->low & CABAC_MASK))
   1.169 +        refill(c);
   1.170 +
   1.171 +    range= c->range<<(CABAC_BITS+1);
   1.172 +    if(c->low < range){
   1.173 +        return 0;
   1.174 +    }else{
   1.175 +        c->low -= range;
   1.176 +        return 1;
   1.177 +    }
   1.178 +}
   1.179 +
   1.180 +static av_always_inline int get_cabac_bypass_sign(CABACContext *c, int val){
   1.181 +    int range, mask;
   1.182 +    c->low += c->low;
   1.183 +
   1.184 +    if(!(c->low & CABAC_MASK))
   1.185 +        refill(c);
   1.186 +
   1.187 +    range= c->range<<(CABAC_BITS+1);
   1.188 +    c->low -= range;
   1.189 +    mask= c->low >> 31;
   1.190 +    range &= mask;
   1.191 +    c->low += range;
   1.192 +    return (val^mask)-mask;
   1.193 +}
   1.194 +
   1.195 +/**
   1.196 + *
   1.197 + * @return the number of bytes read or 0 if no end
   1.198 + */
   1.199 +static int av_unused get_cabac_terminate(CABACContext *c){
   1.200 +    c->range -= 2;
   1.201 +    if(c->low < c->range<<(CABAC_BITS+1)){
   1.202 +        renorm_cabac_decoder_once(c);
   1.203 +        return 0;
   1.204 +    }else{
   1.205 +        return c->bytestream - c->bytestream_start;
   1.206 +    }
   1.207 +}
   1.208 +
   1.209 +#endif /* AVCODEC_CABAC_H */