diff libavcodec/golomb.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/golomb.h	Tue Sep 25 15:55:33 2012 +0200
     1.3 @@ -0,0 +1,410 @@
     1.4 +/*
     1.5 + * exp golomb vlc stuff
     1.6 + * Copyright (c) 2003 Michael Niedermayer <michaelni@gmx.at>
     1.7 + * Copyright (c) 2004 Alex Beregszaszi
     1.8 + *
     1.9 + * This file is part of FFmpeg.
    1.10 + *
    1.11 + * FFmpeg is free software; you can redistribute it and/or
    1.12 + * modify it under the terms of the GNU Lesser General Public
    1.13 + * License as published by the Free Software Foundation; either
    1.14 + * version 2.1 of the License, or (at your option) any later version.
    1.15 + *
    1.16 + * FFmpeg is distributed in the hope that it will be useful,
    1.17 + * but WITHOUT ANY WARRANTY; without even the implied warranty of
    1.18 + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
    1.19 + * Lesser General Public License for more details.
    1.20 + *
    1.21 + * You should have received a copy of the GNU Lesser General Public
    1.22 + * License along with FFmpeg; if not, write to the Free Software
    1.23 + * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
    1.24 + */
    1.25 +
    1.26 +/**
    1.27 + * @file
    1.28 + * @brief
    1.29 + *     exp golomb vlc stuff
    1.30 + * @author Michael Niedermayer <michaelni@gmx.at> and Alex Beregszaszi
    1.31 + */
    1.32 +
    1.33 +#ifndef AVCODEC_GOLOMB_H
    1.34 +#define AVCODEC_GOLOMB_H
    1.35 +
    1.36 +#include <stdint.h>
    1.37 +#include "get_bits.h"
    1.38 +
    1.39 +#define INVALID_VLC           0x80000000
    1.40 +
    1.41 +extern const uint8_t ff_golomb_vlc_len[512];
    1.42 +extern const uint8_t ff_ue_golomb_vlc_code[512];
    1.43 +extern const  int8_t ff_se_golomb_vlc_code[512];
    1.44 +extern const uint8_t ff_ue_golomb_len[256];
    1.45 +
    1.46 +extern const uint8_t ff_interleaved_golomb_vlc_len[256];
    1.47 +extern const uint8_t ff_interleaved_ue_golomb_vlc_code[256];
    1.48 +extern const  int8_t ff_interleaved_se_golomb_vlc_code[256];
    1.49 +extern const uint8_t ff_interleaved_dirac_golomb_vlc_code[256];
    1.50 +
    1.51 +
    1.52 + /**
    1.53 + * read unsigned exp golomb code.
    1.54 + */
    1.55 +static inline int get_ue_golomb(GetBitContext *gb){
    1.56 +    unsigned int buf;
    1.57 +    int log;
    1.58 +
    1.59 +    OPEN_READER(re, gb);
    1.60 +    UPDATE_CACHE(re, gb);
    1.61 +    buf=GET_CACHE(re, gb);
    1.62 +
    1.63 +    if(buf >= (1<<27)){
    1.64 +        buf >>= 32 - 9;
    1.65 +        LAST_SKIP_BITS(re, gb, ff_golomb_vlc_len[buf]);
    1.66 +        CLOSE_READER(re, gb);
    1.67 +
    1.68 +        return ff_ue_golomb_vlc_code[buf];
    1.69 +    }else{
    1.70 +        log= 2*av_log2_c(buf) - 31;
    1.71 +        buf>>= log;
    1.72 +        buf--;
    1.73 +        LAST_SKIP_BITS(re, gb, 32 - log);
    1.74 +        CLOSE_READER(re, gb);
    1.75 +
    1.76 +        return buf;
    1.77 +    }
    1.78 +}
    1.79 +
    1.80 + /**
    1.81 + * read unsigned exp golomb code, constraint to a max of 31.
    1.82 + * the return value is undefined if the stored value exceeds 31.
    1.83 + */
    1.84 +static inline int get_ue_golomb_31(GetBitContext *gb){
    1.85 +    unsigned int buf;
    1.86 +
    1.87 +    OPEN_READER(re, gb);
    1.88 +    UPDATE_CACHE(re, gb);
    1.89 +    buf=GET_CACHE(re, gb);
    1.90 +
    1.91 +    buf >>= 32 - 9;
    1.92 +    LAST_SKIP_BITS(re, gb, ff_golomb_vlc_len[buf]);
    1.93 +    CLOSE_READER(re, gb);
    1.94 +
    1.95 +    return ff_ue_golomb_vlc_code[buf];
    1.96 +}
    1.97 +
    1.98 +static inline int svq3_get_ue_golomb(GetBitContext *gb){
    1.99 +    uint32_t buf;
   1.100 +
   1.101 +    OPEN_READER(re, gb);
   1.102 +    UPDATE_CACHE(re, gb);
   1.103 +    buf=GET_CACHE(re, gb);
   1.104 +
   1.105 +    if(buf&0xAA800000){
   1.106 +        buf >>= 32 - 8;
   1.107 +        LAST_SKIP_BITS(re, gb, ff_interleaved_golomb_vlc_len[buf]);
   1.108 +        CLOSE_READER(re, gb);
   1.109 +
   1.110 +        return ff_interleaved_ue_golomb_vlc_code[buf];
   1.111 +    }else{
   1.112 +        int ret = 1;
   1.113 +
   1.114 +        while (1) {
   1.115 +            buf >>= 32 - 8;
   1.116 +            LAST_SKIP_BITS(re, gb, FFMIN(ff_interleaved_golomb_vlc_len[buf], 8));
   1.117 +
   1.118 +            if (ff_interleaved_golomb_vlc_len[buf] != 9){
   1.119 +                ret <<= (ff_interleaved_golomb_vlc_len[buf] - 1) >> 1;
   1.120 +                ret |= ff_interleaved_dirac_golomb_vlc_code[buf];
   1.121 +                break;
   1.122 +            }
   1.123 +            ret = (ret << 4) | ff_interleaved_dirac_golomb_vlc_code[buf];
   1.124 +            UPDATE_CACHE(re, gb);
   1.125 +            buf = GET_CACHE(re, gb);
   1.126 +        }
   1.127 +
   1.128 +        CLOSE_READER(re, gb);
   1.129 +        return ret - 1;
   1.130 +    }
   1.131 +}
   1.132 +
   1.133 +/**
   1.134 + * read unsigned truncated exp golomb code.
   1.135 + */
   1.136 +static inline int get_te0_golomb(GetBitContext *gb, int range){
   1.137 +    assert(range >= 1);
   1.138 +
   1.139 +    if(range==1)      return 0;
   1.140 +    else if(range==2) return get_bits1(gb)^1;
   1.141 +    else              return get_ue_golomb(gb);
   1.142 +}
   1.143 +
   1.144 +/**
   1.145 + * read unsigned truncated exp golomb code.
   1.146 + */
   1.147 +static inline int get_te_golomb(GetBitContext *gb, int range){
   1.148 +    assert(range >= 1);
   1.149 +
   1.150 +    if(range==2) return get_bits1(gb)^1;
   1.151 +    else         return get_ue_golomb(gb);
   1.152 +}
   1.153 +
   1.154 +
   1.155 +/**
   1.156 + * read signed exp golomb code.
   1.157 + */
   1.158 +static inline int get_se_golomb(GetBitContext *gb){
   1.159 +    unsigned int buf;
   1.160 +    int log;
   1.161 +
   1.162 +    OPEN_READER(re, gb);
   1.163 +    UPDATE_CACHE(re, gb);
   1.164 +    buf=GET_CACHE(re, gb);
   1.165 +
   1.166 +    if(buf >= (1<<27)){
   1.167 +        buf >>= 32 - 9;
   1.168 +        LAST_SKIP_BITS(re, gb, ff_golomb_vlc_len[buf]);
   1.169 +        CLOSE_READER(re, gb);
   1.170 +
   1.171 +        return ff_se_golomb_vlc_code[buf];
   1.172 +    }else{
   1.173 +        log= 2*av_log2_c(buf) - 31;
   1.174 +        buf>>= log;
   1.175 +
   1.176 +        LAST_SKIP_BITS(re, gb, 32 - log);
   1.177 +        CLOSE_READER(re, gb);
   1.178 +
   1.179 +        if(buf&1) buf= -(buf>>1);
   1.180 +        else      buf=  (buf>>1);
   1.181 +
   1.182 +        return buf;
   1.183 +    }
   1.184 +}
   1.185 +
   1.186 +static inline int svq3_get_se_golomb(GetBitContext *gb){
   1.187 +    unsigned int buf;
   1.188 +    int log;
   1.189 +
   1.190 +    OPEN_READER(re, gb);
   1.191 +    UPDATE_CACHE(re, gb);
   1.192 +    buf=GET_CACHE(re, gb);
   1.193 +
   1.194 +    if(buf&0xAA800000){
   1.195 +        buf >>= 32 - 8;
   1.196 +        LAST_SKIP_BITS(re, gb, ff_interleaved_golomb_vlc_len[buf]);
   1.197 +        CLOSE_READER(re, gb);
   1.198 +
   1.199 +        return ff_interleaved_se_golomb_vlc_code[buf];
   1.200 +    }else{
   1.201 +        LAST_SKIP_BITS(re, gb, 8);
   1.202 +        UPDATE_CACHE(re, gb);
   1.203 +        buf |= 1 | (GET_CACHE(re, gb) >> 8);
   1.204 +
   1.205 +        if((buf & 0xAAAAAAAA) == 0)
   1.206 +            return INVALID_VLC;
   1.207 +
   1.208 +        for(log=31; (buf & 0x80000000) == 0; log--){
   1.209 +            buf = (buf << 2) - ((buf << log) >> (log - 1)) + (buf >> 30);
   1.210 +        }
   1.211 +
   1.212 +        LAST_SKIP_BITS(re, gb, 63 - 2*log - 8);
   1.213 +        CLOSE_READER(re, gb);
   1.214 +
   1.215 +        return (signed) (((((buf << log) >> log) - 1) ^ -(buf & 0x1)) + 1) >> 1;
   1.216 +    }
   1.217 +}
   1.218 +
   1.219 +static inline int dirac_get_se_golomb(GetBitContext *gb){
   1.220 +    uint32_t buf;
   1.221 +    uint32_t ret;
   1.222 +
   1.223 +    ret = svq3_get_ue_golomb(gb);
   1.224 +
   1.225 +    if (ret) {
   1.226 +        OPEN_READER(re, gb);
   1.227 +        UPDATE_CACHE(re, gb);
   1.228 +        buf = SHOW_SBITS(re, gb, 1);
   1.229 +        LAST_SKIP_BITS(re, gb, 1);
   1.230 +        ret = (ret ^ buf) - buf;
   1.231 +        CLOSE_READER(re, gb);
   1.232 +    }
   1.233 +
   1.234 +    return ret;
   1.235 +}
   1.236 +
   1.237 +/**
   1.238 + * read unsigned golomb rice code (ffv1).
   1.239 + */
   1.240 +static inline int get_ur_golomb(GetBitContext *gb, int k, int limit, int esc_len){
   1.241 +    unsigned int buf;
   1.242 +    int log;
   1.243 +
   1.244 +    OPEN_READER(re, gb);
   1.245 +    UPDATE_CACHE(re, gb);
   1.246 +    buf=GET_CACHE(re, gb);
   1.247 +
   1.248 +    log= av_log2_c(buf);
   1.249 +
   1.250 +    if(log > 31-limit){
   1.251 +        buf >>= log - k;
   1.252 +        buf += (30-log)<<k;
   1.253 +        LAST_SKIP_BITS(re, gb, 32 + k - log);
   1.254 +        CLOSE_READER(re, gb);
   1.255 +
   1.256 +        return buf;
   1.257 +    }else{
   1.258 +        LAST_SKIP_BITS(re, gb, limit);
   1.259 +        UPDATE_CACHE(re, gb);
   1.260 +
   1.261 +        buf = SHOW_UBITS(re, gb, esc_len);
   1.262 +
   1.263 +        LAST_SKIP_BITS(re, gb, esc_len);
   1.264 +        CLOSE_READER(re, gb);
   1.265 +
   1.266 +        return buf + limit - 1;
   1.267 +    }
   1.268 +}
   1.269 +
   1.270 +/**
   1.271 + * read unsigned golomb rice code (jpegls).
   1.272 + */
   1.273 +static inline int get_ur_golomb_jpegls(GetBitContext *gb, int k, int limit, int esc_len){
   1.274 +    unsigned int buf;
   1.275 +    int log;
   1.276 +
   1.277 +    OPEN_READER(re, gb);
   1.278 +    UPDATE_CACHE(re, gb);
   1.279 +    buf=GET_CACHE(re, gb);
   1.280 +
   1.281 +    log= av_log2_c(buf);
   1.282 +
   1.283 +    if(log - k >= 32-MIN_CACHE_BITS+(MIN_CACHE_BITS==32) && 32-log < limit){
   1.284 +        buf >>= log - k;
   1.285 +        buf += (30-log)<<k;
   1.286 +        LAST_SKIP_BITS(re, gb, 32 + k - log);
   1.287 +        CLOSE_READER(re, gb);
   1.288 +
   1.289 +        return buf;
   1.290 +    }else{
   1.291 +        int i;
   1.292 +        for(i=0; SHOW_UBITS(re, gb, 1) == 0; i++){
   1.293 +            LAST_SKIP_BITS(re, gb, 1);
   1.294 +            UPDATE_CACHE(re, gb);
   1.295 +        }
   1.296 +        SKIP_BITS(re, gb, 1);
   1.297 +
   1.298 +        if(i < limit - 1){
   1.299 +            if(k){
   1.300 +                buf = SHOW_UBITS(re, gb, k);
   1.301 +                LAST_SKIP_BITS(re, gb, k);
   1.302 +            }else{
   1.303 +                buf=0;
   1.304 +            }
   1.305 +
   1.306 +            CLOSE_READER(re, gb);
   1.307 +            return buf + (i<<k);
   1.308 +        }else if(i == limit - 1){
   1.309 +            buf = SHOW_UBITS(re, gb, esc_len);
   1.310 +            LAST_SKIP_BITS(re, gb, esc_len);
   1.311 +            CLOSE_READER(re, gb);
   1.312 +
   1.313 +            return buf + 1;
   1.314 +        }else
   1.315 +            return -1;
   1.316 +    }
   1.317 +}
   1.318 +
   1.319 +/**
   1.320 + * read signed golomb rice code (ffv1).
   1.321 + */
   1.322 +static inline int get_sr_golomb(GetBitContext *gb, int k, int limit, int esc_len){
   1.323 +    int v= get_ur_golomb(gb, k, limit, esc_len);
   1.324 +
   1.325 +    v++;
   1.326 +    if (v&1) return v>>1;
   1.327 +    else return -(v>>1);
   1.328 +
   1.329 +//    return (v>>1) ^ -(v&1);
   1.330 +}
   1.331 +
   1.332 +/**
   1.333 + * read signed golomb rice code (flac).
   1.334 + */
   1.335 +static inline int get_sr_golomb_flac(GetBitContext *gb, int k, int limit, int esc_len){
   1.336 +    int v= get_ur_golomb_jpegls(gb, k, limit, esc_len);
   1.337 +    return (v>>1) ^ -(v&1);
   1.338 +}
   1.339 +
   1.340 +/**
   1.341 + * read unsigned golomb rice code (shorten).
   1.342 + */
   1.343 +static inline unsigned int get_ur_golomb_shorten(GetBitContext *gb, int k){
   1.344 +        return get_ur_golomb_jpegls(gb, k, INT_MAX, 0);
   1.345 +}
   1.346 +
   1.347 +/**
   1.348 + * read signed golomb rice code (shorten).
   1.349 + */
   1.350 +static inline int get_sr_golomb_shorten(GetBitContext* gb, int k)
   1.351 +{
   1.352 +    int uvar = get_ur_golomb_jpegls(gb, k + 1, INT_MAX, 0);
   1.353 +    if (uvar & 1)
   1.354 +        return ~(uvar >> 1);
   1.355 +    else
   1.356 +        return uvar >> 1;
   1.357 +}
   1.358 +
   1.359 +
   1.360 +
   1.361 +#ifdef TRACE
   1.362 +
   1.363 +static inline int get_ue(GetBitContext *s, char *file, const char *func, int line){
   1.364 +    int show= show_bits(s, 24);
   1.365 +    int pos= get_bits_count(s);
   1.366 +    int i= get_ue_golomb(s);
   1.367 +    int len= get_bits_count(s) - pos;
   1.368 +    int bits= show>>(24-len);
   1.369 +
   1.370 +    print_bin(bits, len);
   1.371 +
   1.372 +    av_log(NULL, AV_LOG_DEBUG, "%5d %2d %3d ue  @%5d in %s %s:%d\n", bits, len, i, pos, file, func, line);
   1.373 +
   1.374 +    return i;
   1.375 +}
   1.376 +
   1.377 +static inline int get_se(GetBitContext *s, char *file, const char *func, int line){
   1.378 +    int show= show_bits(s, 24);
   1.379 +    int pos= get_bits_count(s);
   1.380 +    int i= get_se_golomb(s);
   1.381 +    int len= get_bits_count(s) - pos;
   1.382 +    int bits= show>>(24-len);
   1.383 +
   1.384 +    print_bin(bits, len);
   1.385 +
   1.386 +    av_log(NULL, AV_LOG_DEBUG, "%5d %2d %3d se  @%5d in %s %s:%d\n", bits, len, i, pos, file, func, line);
   1.387 +
   1.388 +    return i;
   1.389 +}
   1.390 +
   1.391 +static inline int get_te(GetBitContext *s, int r, char *file, const char *func, int line){
   1.392 +    int show= show_bits(s, 24);
   1.393 +    int pos= get_bits_count(s);
   1.394 +    int i= get_te0_golomb(s, r);
   1.395 +    int len= get_bits_count(s) - pos;
   1.396 +    int bits= show>>(24-len);
   1.397 +
   1.398 +    print_bin(bits, len);
   1.399 +
   1.400 +    av_log(NULL, AV_LOG_DEBUG, "%5d %2d %3d te  @%5d in %s %s:%d\n", bits, len, i, pos, file, func, line);
   1.401 +
   1.402 +    return i;
   1.403 +}
   1.404 +
   1.405 +#define get_ue_golomb(a) get_ue(a, __FILE__, __PRETTY_FUNCTION__, __LINE__)
   1.406 +#define get_se_golomb(a) get_se(a, __FILE__, __PRETTY_FUNCTION__, __LINE__)
   1.407 +#define get_te_golomb(a, r) get_te(a, r, __FILE__, __PRETTY_FUNCTION__, __LINE__)
   1.408 +#define get_te0_golomb(a, r) get_te(a, r, __FILE__, __PRETTY_FUNCTION__, __LINE__)
   1.409 +
   1.410 +#endif
   1.411 +
   1.412 +
   1.413 +#endif /* AVCODEC_GOLOMB_H */