lagarith: Convert to the new bitstream reader
[libav.git] / libavcodec / lagarithrac.h
CommitLineData
d267b339
CEH
1/*
2 * Lagarith range decoder
3 * Copyright (c) 2009 Nathan Caldwell <saintdev (at) gmail.com>
4 * Copyright (c) 2009 David Conrad
5 *
2912e87a 6 * This file is part of Libav.
d267b339 7 *
2912e87a 8 * Libav is free software; you can redistribute it and/or
d267b339
CEH
9 * modify it under the terms of the GNU Lesser General Public
10 * License as published by the Free Software Foundation; either
11 * version 2.1 of the License, or (at your option) any later version.
12 *
2912e87a 13 * Libav is distributed in the hope that it will be useful,
d267b339
CEH
14 * but WITHOUT ANY WARRANTY; without even the implied warranty of
15 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
16 * Lesser General Public License for more details.
17 *
18 * You should have received a copy of the GNU Lesser General Public
2912e87a 19 * License along with Libav; if not, write to the Free Software
d267b339
CEH
20 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
21 */
22
23/**
f25a2ece 24 * @file
d267b339
CEH
25 * Lagarith range decoder
26 * @author Nathan Caldwell
27 * @author David Conrad
28 */
29
30#ifndef AVCODEC_LAGARITHRAC_H
31#define AVCODEC_LAGARITHRAC_H
32
33#include <stdint.h>
6fad5abc 34
d267b339
CEH
35#include "libavutil/common.h"
36#include "libavutil/intreadwrite.h"
6fad5abc 37
d267b339 38#include "avcodec.h"
6fad5abc 39#include "bitstream.h"
d267b339
CEH
40
41typedef struct lag_rac {
42 AVCodecContext *avctx;
43 unsigned low;
44 unsigned range;
adbfc605
DB
45 unsigned scale; /**< Number of bits of precision in range. */
46 unsigned hash_shift; /**< Number of bits to shift to calculate hash for radix search. */
d267b339 47
adbfc605
DB
48 const uint8_t *bytestream_start; /**< Start of input bytestream. */
49 const uint8_t *bytestream; /**< Current position in input bytestream. */
50 const uint8_t *bytestream_end; /**< End position of input bytestream. */
d267b339 51
adbfc605
DB
52 uint32_t prob[258]; /**< Table of cumulative probability for each symbol. */
53 uint8_t range_hash[256]; /**< Hash table mapping upper byte to approximate symbol. */
d267b339
CEH
54} lag_rac;
55
6fad5abc 56void ff_lag_rac_init(lag_rac *l, BitstreamContext *bc, int length);
d267b339
CEH
57
58/* TODO: Optimize */
59static inline void lag_rac_refill(lag_rac *l)
60{
61 while (l->range <= 0x800000) {
62 l->low <<= 8;
63 l->range <<= 8;
64 l->low |= 0xff & (AV_RB16(l->bytestream) >> 1);
65 if (l->bytestream < l->bytestream_end)
66 l->bytestream++;
67 }
68}
69
70/**
71 * Decode a single byte from the compressed plane described by *l.
72 * @param l pointer to lag_rac for the current plane
73 * @return next byte of decoded data
74 */
75static inline uint8_t lag_get_rac(lag_rac *l)
76{
77 unsigned range_scaled, low_scaled, div;
78 int val;
79 uint8_t shift;
80
81 lag_rac_refill(l);
82
83 range_scaled = l->range >> l->scale;
84
85 if (l->low < range_scaled * l->prob[255]) {
86 /* val = 0 is frequent enough to deserve a shortcut */
87 if (l->low < range_scaled * l->prob[1]) {
88 val = 0;
89 } else {
90 /* FIXME __builtin_clz is ~20% faster here, but not allowed in generic code. */
91 shift = 30 - av_log2(range_scaled);
92 div = ((range_scaled << shift) + (1 << 23) - 1) >> 23;
93 /* low>>24 ensures that any cases too big for exact FASTDIV are
94 * under- rather than over-estimated
95 */
96 low_scaled = FASTDIV(l->low - (l->low >> 24), div);
97 shift -= l->hash_shift;
98 shift &= 31;
99 low_scaled = (low_scaled << shift) | (low_scaled >> (32 - shift));
100 /* low_scaled is now a lower bound of low/range_scaled */
101 val = l->range_hash[(uint8_t) low_scaled];
102 while (l->low >= range_scaled * l->prob[val + 1])
103 val++;
104 }
105
106 l->range = range_scaled * (l->prob[val + 1] - l->prob[val]);
107 } else {
108 val = 255;
109 l->range -= range_scaled * l->prob[255];
110 }
111
de6dfa2b
AK
112 if (!l->range)
113 l->range = 0x80;
114
d267b339
CEH
115 l->low -= range_scaled * l->prob[val];
116
117 return val;
118}
119
120
121#endif /* AVCODEC_LAGARITHRAC_H */