/src/ffmpeg/libavcodec/lagarithrac.h
Line | Count | Source |
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> |
34 | | #include "libavutil/intreadwrite.h" |
35 | | #include "get_bits.h" |
36 | | |
37 | | typedef struct lag_rac { |
38 | | void *logctx; |
39 | | unsigned low; |
40 | | unsigned range; |
41 | | unsigned scale; /**< Number of bits of precision in range. */ |
42 | | unsigned hash_shift; /**< Number of bits to shift to calculate hash for radix search. */ |
43 | | |
44 | | const uint8_t *bytestream_start; /**< Start of input bytestream. */ |
45 | | const uint8_t *bytestream; /**< Current position in input bytestream. */ |
46 | | const uint8_t *bytestream_end; /**< End position of input bytestream. */ |
47 | | |
48 | | int overread; |
49 | 12.8M | #define MAX_OVERREAD 4 |
50 | | |
51 | | uint32_t prob[258]; /**< Table of cumulative probability for each symbol. */ |
52 | | uint8_t range_hash[1024]; /**< Hash table mapping upper byte to approximate symbol. */ |
53 | | } lag_rac; |
54 | | |
55 | | void ff_lag_rac_init(lag_rac *l, GetBitContext *gb, int length); |
56 | | |
57 | | /* TODO: Optimize */ |
58 | | static inline void lag_rac_refill(lag_rac *l) |
59 | 759M | { |
60 | 766M | while (l->range <= 0x800000) { |
61 | 6.10M | l->low <<= 8; |
62 | 6.10M | l->range <<= 8; |
63 | 6.10M | l->low |= 0xff & (AV_RB16(l->bytestream) >> 1); |
64 | 6.10M | if (l->bytestream < l->bytestream_end) |
65 | 614k | l->bytestream++; |
66 | 5.48M | else |
67 | 5.48M | l->overread++; |
68 | 6.10M | } |
69 | 759M | } lagarith.c:lag_rac_refill Line | Count | Source | 59 | 759M | { | 60 | 766M | while (l->range <= 0x800000) { | 61 | 6.10M | l->low <<= 8; | 62 | 6.10M | l->range <<= 8; | 63 | 6.10M | l->low |= 0xff & (AV_RB16(l->bytestream) >> 1); | 64 | 6.10M | if (l->bytestream < l->bytestream_end) | 65 | 614k | l->bytestream++; | 66 | 5.48M | else | 67 | 5.48M | l->overread++; | 68 | 6.10M | } | 69 | 759M | } |
Unexecuted instantiation: lagarithrac.c:lag_rac_refill |
70 | | |
71 | | /** |
72 | | * Decode a single byte from the compressed plane described by *l. |
73 | | * @param l pointer to lag_rac for the current plane |
74 | | * @return next byte of decoded data |
75 | | */ |
76 | | static inline uint8_t lag_get_rac(lag_rac *l) |
77 | 759M | { |
78 | 759M | unsigned range_scaled, low_scaled; |
79 | 759M | int val; |
80 | | |
81 | 759M | lag_rac_refill(l); |
82 | | |
83 | 759M | range_scaled = l->range >> l->scale; |
84 | | |
85 | 759M | if (l->low < range_scaled * l->prob[255]) { |
86 | | /* val = 0 is frequent enough to deserve a shortcut */ |
87 | 759M | if (l->low < range_scaled * l->prob[1]) { |
88 | 468M | val = 0; |
89 | 468M | } else { |
90 | 291M | low_scaled = l->low / (range_scaled<<(l->hash_shift)); |
91 | | |
92 | 291M | val = l->range_hash[low_scaled]; |
93 | 291M | while (l->low >= range_scaled * l->prob[val + 1]) |
94 | 205k | val++; |
95 | 291M | } |
96 | | |
97 | 759M | l->range = range_scaled * (l->prob[val + 1] - l->prob[val]); |
98 | 759M | } else { |
99 | 167k | val = 255; |
100 | 167k | l->range -= range_scaled * l->prob[255]; |
101 | 167k | } |
102 | | |
103 | 759M | if (!l->range) |
104 | 0 | l->range = 0x80; |
105 | | |
106 | 759M | l->low -= range_scaled * l->prob[val]; |
107 | | |
108 | 759M | return val; |
109 | 759M | } Line | Count | Source | 77 | 759M | { | 78 | 759M | unsigned range_scaled, low_scaled; | 79 | 759M | int val; | 80 | | | 81 | 759M | lag_rac_refill(l); | 82 | | | 83 | 759M | range_scaled = l->range >> l->scale; | 84 | | | 85 | 759M | if (l->low < range_scaled * l->prob[255]) { | 86 | | /* val = 0 is frequent enough to deserve a shortcut */ | 87 | 759M | if (l->low < range_scaled * l->prob[1]) { | 88 | 468M | val = 0; | 89 | 468M | } else { | 90 | 291M | low_scaled = l->low / (range_scaled<<(l->hash_shift)); | 91 | | | 92 | 291M | val = l->range_hash[low_scaled]; | 93 | 291M | while (l->low >= range_scaled * l->prob[val + 1]) | 94 | 205k | val++; | 95 | 291M | } | 96 | | | 97 | 759M | l->range = range_scaled * (l->prob[val + 1] - l->prob[val]); | 98 | 759M | } else { | 99 | 167k | val = 255; | 100 | 167k | l->range -= range_scaled * l->prob[255]; | 101 | 167k | } | 102 | | | 103 | 759M | if (!l->range) | 104 | 0 | l->range = 0x80; | 105 | | | 106 | 759M | l->low -= range_scaled * l->prob[val]; | 107 | | | 108 | 759M | return val; | 109 | 759M | } |
Unexecuted instantiation: lagarithrac.c:lag_get_rac |
110 | | |
111 | | |
112 | | #endif /* AVCODEC_LAGARITHRAC_H */ |