1 /*
2 * Lagarith range decoder
3 * Copyright (c) 2009 Nathan Caldwell <saintdev (at) gmail.com>
4 * Copyright (c) 2009 David Conrad
5 *
6 * This file is part of FFmpeg.
7 *
8 * FFmpeg is free software; you can redistribute it and/or
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 *
13 * FFmpeg is distributed in the hope that it will be useful,
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
19 * License along with FFmpeg; if not, write to the Free Software
20 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
21 */
22
23 /**
24 * @file
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>
38
43 unsigned scale;
/**< Number of bits of precision in range. */
44 unsigned hash_shift;
/**< Number of bits to shift to calculate hash for radix search. */
45
49
50 uint32_t
prob[258];
/**< Table of cumulative probability for each symbol. */
53
55
56 /* TODO: Optimize */
58 {
59 while (l->
range <= 0x800000) {
65 }
66 }
67
68 /**
69 * Decode a single byte from the compressed plane described by *l.
70 * @param l pointer to lag_rac for the current plane
71 * @return next byte of decoded data
72 */
74 {
75 unsigned range_scaled, low_scaled, div;
78
80
82
83 if (l->
low < range_scaled * l->
prob[255]) {
84 /* val = 0 is frequent enough to deserve a shortcut */
85 if (l->
low < range_scaled * l->
prob[1]) {
86 val = 0;
87 } else {
88 /* FIXME __builtin_clz is ~20% faster here, but not allowed in generic code. */
89 shift = 30 -
av_log2(range_scaled);
90 div = ((range_scaled <<
shift) + (1 << 23) - 1) >> 23;
91 /* low>>24 ensures that any cases too big for exact FASTDIV are
92 * under- rather than over-estimated
93 */
96 shift &= 31;
97 low_scaled = (low_scaled << shift) | (low_scaled >> (32 -
shift));
98 /* low_scaled is now a lower bound of low/range_scaled */
100 while (l->
low >= range_scaled * l->
prob[val + 1])
101 val++;
102 }
103
105 } else {
106 val = 255;
108 }
109
112
114
116 }
117
118
119 #endif /* AVCODEC_LAGARITHRAC_H */