00001 /* 00002 * Lagarith range decoder 00003 * Copyright (c) 2009 Nathan Caldwell <saintdev (at) gmail.com> 00004 * Copyright (c) 2009 David Conrad 00005 * 00006 * This file is part of FFmpeg. 00007 * 00008 * FFmpeg is free software; you can redistribute it and/or 00009 * modify it under the terms of the GNU Lesser General Public 00010 * License as published by the Free Software Foundation; either 00011 * version 2.1 of the License, or (at your option) any later version. 00012 * 00013 * FFmpeg is distributed in the hope that it will be useful, 00014 * but WITHOUT ANY WARRANTY; without even the implied warranty of 00015 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU 00016 * Lesser General Public License for more details. 00017 * 00018 * You should have received a copy of the GNU Lesser General Public 00019 * License along with FFmpeg; if not, write to the Free Software 00020 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA 00021 */ 00022 00030 #ifndef AVCODEC_LAGARITHRAC_H 00031 #define AVCODEC_LAGARITHRAC_H 00032 00033 #include <stdint.h> 00034 #include "libavutil/common.h" 00035 #include "libavutil/intreadwrite.h" 00036 #include "avcodec.h" 00037 #include "get_bits.h" 00038 00039 typedef struct lag_rac { 00040 AVCodecContext *avctx; 00041 unsigned low; 00042 unsigned range; 00043 unsigned scale; 00044 unsigned hash_shift; 00046 const uint8_t *bytestream_start; 00047 const uint8_t *bytestream; 00048 const uint8_t *bytestream_end; 00050 uint32_t prob[258]; 00051 uint8_t range_hash[256]; 00052 } lag_rac; 00053 00054 void lag_rac_init(lag_rac *l, GetBitContext *gb, int length); 00055 00056 /* TODO: Optimize */ 00057 static inline void lag_rac_refill(lag_rac *l) 00058 { 00059 while (l->range <= 0x800000) { 00060 l->low <<= 8; 00061 l->range <<= 8; 00062 l->low |= 0xff & (AV_RB16(l->bytestream) >> 1); 00063 if (l->bytestream < l->bytestream_end) 00064 l->bytestream++; 00065 } 00066 } 00067 00073 static inline uint8_t lag_get_rac(lag_rac *l) 00074 { 00075 unsigned range_scaled, low_scaled, div; 00076 int val; 00077 uint8_t shift; 00078 00079 lag_rac_refill(l); 00080 00081 range_scaled = l->range >> l->scale; 00082 00083 if (l->low < range_scaled * l->prob[255]) { 00084 /* val = 0 is frequent enough to deserve a shortcut */ 00085 if (l->low < range_scaled * l->prob[1]) { 00086 val = 0; 00087 } else { 00088 /* FIXME __builtin_clz is ~20% faster here, but not allowed in generic code. */ 00089 shift = 30 - av_log2(range_scaled); 00090 div = ((range_scaled << shift) + (1 << 23) - 1) >> 23; 00091 /* low>>24 ensures that any cases too big for exact FASTDIV are 00092 * under- rather than over-estimated 00093 */ 00094 low_scaled = FASTDIV(l->low - (l->low >> 24), div); 00095 shift -= l->hash_shift; 00096 shift &= 31; 00097 low_scaled = (low_scaled << shift) | (low_scaled >> (32 - shift)); 00098 /* low_scaled is now a lower bound of low/range_scaled */ 00099 val = l->range_hash[(uint8_t) low_scaled]; 00100 while (l->low >= range_scaled * l->prob[val + 1]) 00101 val++; 00102 } 00103 00104 l->range = range_scaled * (l->prob[val + 1] - l->prob[val]); 00105 } else { 00106 val = 255; 00107 l->range -= range_scaled * l->prob[255]; 00108 } 00109 00110 l->low -= range_scaled * l->prob[val]; 00111 00112 return val; 00113 } 00114 00115 00116 #endif /* AVCODEC_LAGARITHRAC_H */