/src/ffmpeg/libavcodec/rangecoder.h
Line | Count | Source |
1 | | /* |
2 | | * Range coder |
3 | | * Copyright (c) 2004 Michael Niedermayer <michaelni@gmx.at> |
4 | | * |
5 | | * This file is part of FFmpeg. |
6 | | * |
7 | | * FFmpeg is free software; you can redistribute it and/or |
8 | | * modify it under the terms of the GNU Lesser General Public |
9 | | * License as published by the Free Software Foundation; either |
10 | | * version 2.1 of the License, or (at your option) any later version. |
11 | | * |
12 | | * FFmpeg is distributed in the hope that it will be useful, |
13 | | * but WITHOUT ANY WARRANTY; without even the implied warranty of |
14 | | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU |
15 | | * Lesser General Public License for more details. |
16 | | * |
17 | | * You should have received a copy of the GNU Lesser General Public |
18 | | * License along with FFmpeg; if not, write to the Free Software |
19 | | * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA |
20 | | */ |
21 | | |
22 | | /** |
23 | | * @file |
24 | | * Range coder. |
25 | | */ |
26 | | |
27 | | #ifndef AVCODEC_RANGECODER_H |
28 | | #define AVCODEC_RANGECODER_H |
29 | | |
30 | | #include <stdint.h> |
31 | | |
32 | | #include "libavutil/avassert.h" |
33 | | #include "libavutil/intmath.h" |
34 | | |
35 | | typedef struct RangeCoder { |
36 | | int low; |
37 | | int range; |
38 | | int outstanding_count; |
39 | | int outstanding_byte; |
40 | | uint8_t zero_state[256]; |
41 | | uint8_t one_state[256]; |
42 | | uint8_t *bytestream_start; |
43 | | uint8_t *bytestream; |
44 | | uint8_t *bytestream_end; |
45 | | int overread; |
46 | 1.95M | #define MAX_OVERREAD 2 |
47 | | } RangeCoder; |
48 | | |
49 | | void ff_init_range_encoder(RangeCoder *c, uint8_t *buf, int buf_size); |
50 | | void ff_init_range_decoder(RangeCoder *c, const uint8_t *buf, int buf_size); |
51 | | |
52 | | /** |
53 | | * Terminates the range coder |
54 | | * @param version version 0 requires the decoder to know the data size in bytes |
55 | | * version 1 needs about 1 bit more space but does not need to |
56 | | * carry the size from encoder to decoder |
57 | | */ |
58 | | int ff_rac_terminate(RangeCoder *c, int version); |
59 | | |
60 | | void ff_build_rac_states(RangeCoder *c, int factor, int max_p); |
61 | | |
62 | | static inline void renorm_encoder(RangeCoder *c) |
63 | 227M | { |
64 | 227M | if (c->low - 0xFF01 >= 0x10000 - 0xFF01U) { |
65 | 222M | int mask = c->low - 0xFF01 >> 31; |
66 | 222M | *c->bytestream = c->outstanding_byte + 1 + mask; |
67 | 222M | c->bytestream += c->outstanding_byte >= 0; |
68 | 227M | for (; c->outstanding_count; c->outstanding_count--) |
69 | 5.44M | *c->bytestream++ = mask; |
70 | 222M | c->outstanding_byte = c->low >> 8; |
71 | 222M | } else { |
72 | 5.19M | c->outstanding_count++; |
73 | 5.19M | } |
74 | | |
75 | 227M | c->low = (c->low & 0xFF) << 8; |
76 | 227M | c->range <<= 8; |
77 | 227M | } Unexecuted instantiation: snow.c:renorm_encoder Unexecuted instantiation: ffv1_parser.c:renorm_encoder rangecoder.c:renorm_encoder Line | Count | Source | 63 | 312k | { | 64 | 312k | if (c->low - 0xFF01 >= 0x10000 - 0xFF01U) { | 65 | 311k | int mask = c->low - 0xFF01 >> 31; | 66 | 311k | *c->bytestream = c->outstanding_byte + 1 + mask; | 67 | 311k | c->bytestream += c->outstanding_byte >= 0; | 68 | 3.72M | for (; c->outstanding_count; c->outstanding_count--) | 69 | 3.41M | *c->bytestream++ = mask; | 70 | 311k | c->outstanding_byte = c->low >> 8; | 71 | 311k | } else { | 72 | 1.00k | c->outstanding_count++; | 73 | 1.00k | } | 74 | | | 75 | 312k | c->low = (c->low & 0xFF) << 8; | 76 | 312k | c->range <<= 8; | 77 | 312k | } |
Unexecuted instantiation: ffv1.c:renorm_encoder Unexecuted instantiation: ffv1_parse.c:renorm_encoder Unexecuted instantiation: snowdec.c:renorm_encoder Unexecuted instantiation: sonic.c:renorm_encoder Unexecuted instantiation: ffv1dec.c:renorm_encoder Line | Count | Source | 63 | 189M | { | 64 | 189M | if (c->low - 0xFF01 >= 0x10000 - 0xFF01U) { | 65 | 184M | int mask = c->low - 0xFF01 >> 31; | 66 | 184M | *c->bytestream = c->outstanding_byte + 1 + mask; | 67 | 184M | c->bytestream += c->outstanding_byte >= 0; | 68 | 185M | for (; c->outstanding_count; c->outstanding_count--) | 69 | 1.62M | *c->bytestream++ = mask; | 70 | 184M | c->outstanding_byte = c->low >> 8; | 71 | 184M | } else { | 72 | 5.04M | c->outstanding_count++; | 73 | 5.04M | } | 74 | | | 75 | 189M | c->low = (c->low & 0xFF) << 8; | 76 | 189M | c->range <<= 8; | 77 | 189M | } |
Line | Count | Source | 63 | 38.0M | { | 64 | 38.0M | if (c->low - 0xFF01 >= 0x10000 - 0xFF01U) { | 65 | 37.9M | int mask = c->low - 0xFF01 >> 31; | 66 | 37.9M | *c->bytestream = c->outstanding_byte + 1 + mask; | 67 | 37.9M | c->bytestream += c->outstanding_byte >= 0; | 68 | 38.3M | for (; c->outstanding_count; c->outstanding_count--) | 69 | 400k | *c->bytestream++ = mask; | 70 | 37.9M | c->outstanding_byte = c->low >> 8; | 71 | 37.9M | } else { | 72 | 154k | c->outstanding_count++; | 73 | 154k | } | 74 | | | 75 | 38.0M | c->low = (c->low & 0xFF) << 8; | 76 | 38.0M | c->range <<= 8; | 77 | 38.0M | } |
|
78 | | |
79 | | static inline int get_rac_count(RangeCoder *c) |
80 | 1.60M | { |
81 | 1.60M | int x = c->bytestream - c->bytestream_start + c->outstanding_count; |
82 | 1.60M | if (c->outstanding_byte >= 0) |
83 | 1.57M | x++; |
84 | 1.60M | return 8 * x - av_log2(c->range); |
85 | 1.60M | } Unexecuted instantiation: snow.c:get_rac_count Unexecuted instantiation: ffv1_parser.c:get_rac_count Unexecuted instantiation: rangecoder.c:get_rac_count Unexecuted instantiation: ffv1.c:get_rac_count Unexecuted instantiation: ffv1_parse.c:get_rac_count Unexecuted instantiation: snowdec.c:get_rac_count Unexecuted instantiation: sonic.c:get_rac_count Unexecuted instantiation: ffv1dec.c:get_rac_count Line | Count | Source | 80 | 299k | { | 81 | 299k | int x = c->bytestream - c->bytestream_start + c->outstanding_count; | 82 | 299k | if (c->outstanding_byte >= 0) | 83 | 299k | x++; | 84 | 299k | return 8 * x - av_log2(c->range); | 85 | 299k | } |
Line | Count | Source | 80 | 1.30M | { | 81 | 1.30M | int x = c->bytestream - c->bytestream_start + c->outstanding_count; | 82 | 1.30M | if (c->outstanding_byte >= 0) | 83 | 1.27M | x++; | 84 | 1.30M | return 8 * x - av_log2(c->range); | 85 | 1.30M | } |
|
86 | | |
87 | | static inline void put_rac(RangeCoder *c, uint8_t *const state, int bit) |
88 | 2.73G | { |
89 | 2.73G | int range1 = (c->range * (*state)) >> 8; |
90 | | |
91 | 2.73G | av_assert2(*state); |
92 | 2.73G | av_assert2(range1 < c->range); |
93 | 2.73G | av_assert2(range1 > 0); |
94 | 2.73G | if (!bit) { |
95 | 988M | c->range -= range1; |
96 | 988M | *state = c->zero_state[*state]; |
97 | 1.74G | } else { |
98 | 1.74G | c->low += c->range - range1; |
99 | 1.74G | c->range = range1; |
100 | 1.74G | *state = c->one_state[*state]; |
101 | 1.74G | } |
102 | | |
103 | 2.73G | if (c->range < 0x100) |
104 | 227M | renorm_encoder(c); |
105 | 2.73G | } Unexecuted instantiation: snow.c:put_rac Unexecuted instantiation: ffv1_parser.c:put_rac Line | Count | Source | 88 | 106k | { | 89 | 106k | int range1 = (c->range * (*state)) >> 8; | 90 | | | 91 | 106k | av_assert2(*state); | 92 | 106k | av_assert2(range1 < c->range); | 93 | 106k | av_assert2(range1 > 0); | 94 | 106k | if (!bit) { | 95 | 106k | c->range -= range1; | 96 | 106k | *state = c->zero_state[*state]; | 97 | 106k | } else { | 98 | 0 | c->low += c->range - range1; | 99 | 0 | c->range = range1; | 100 | 0 | *state = c->one_state[*state]; | 101 | 0 | } | 102 | | | 103 | 106k | if (c->range < 0x100) | 104 | 15.6k | renorm_encoder(c); | 105 | 106k | } |
Unexecuted instantiation: ffv1.c:put_rac Unexecuted instantiation: ffv1_parse.c:put_rac Unexecuted instantiation: snowdec.c:put_rac Unexecuted instantiation: sonic.c:put_rac Unexecuted instantiation: ffv1dec.c:put_rac Line | Count | Source | 88 | 2.34G | { | 89 | 2.34G | int range1 = (c->range * (*state)) >> 8; | 90 | | | 91 | 2.34G | av_assert2(*state); | 92 | 2.34G | av_assert2(range1 < c->range); | 93 | 2.34G | av_assert2(range1 > 0); | 94 | 2.34G | if (!bit) { | 95 | 806M | c->range -= range1; | 96 | 806M | *state = c->zero_state[*state]; | 97 | 1.54G | } else { | 98 | 1.54G | c->low += c->range - range1; | 99 | 1.54G | c->range = range1; | 100 | 1.54G | *state = c->one_state[*state]; | 101 | 1.54G | } | 102 | | | 103 | 2.34G | if (c->range < 0x100) | 104 | 189M | renorm_encoder(c); | 105 | 2.34G | } |
Line | Count | Source | 88 | 388M | { | 89 | 388M | int range1 = (c->range * (*state)) >> 8; | 90 | | | 91 | 388M | av_assert2(*state); | 92 | 388M | av_assert2(range1 < c->range); | 93 | 388M | av_assert2(range1 > 0); | 94 | 388M | if (!bit) { | 95 | 182M | c->range -= range1; | 96 | 182M | *state = c->zero_state[*state]; | 97 | 205M | } else { | 98 | 205M | c->low += c->range - range1; | 99 | 205M | c->range = range1; | 100 | 205M | *state = c->one_state[*state]; | 101 | 205M | } | 102 | | | 103 | 388M | if (c->range < 0x100) | 104 | 38.0M | renorm_encoder(c); | 105 | 388M | } |
|
106 | | |
107 | | static inline void refill(RangeCoder *c) |
108 | 12.5M | { |
109 | 12.5M | c->range <<= 8; |
110 | 12.5M | c->low <<= 8; |
111 | 12.5M | if (c->bytestream < c->bytestream_end) { |
112 | 6.70M | c->low += c->bytestream[0]; |
113 | 6.70M | c->bytestream++; |
114 | 6.70M | } else |
115 | 5.81M | c->overread ++; |
116 | 12.5M | } Unexecuted instantiation: snow.c:refill Unexecuted instantiation: ffv1_parser.c:refill Unexecuted instantiation: rangecoder.c:refill Line | Count | Source | 108 | 506k | { | 109 | 506k | c->range <<= 8; | 110 | 506k | c->low <<= 8; | 111 | 506k | if (c->bytestream < c->bytestream_end) { | 112 | 285k | c->low += c->bytestream[0]; | 113 | 285k | c->bytestream++; | 114 | 285k | } else | 115 | 220k | c->overread ++; | 116 | 506k | } |
Line | Count | Source | 108 | 18.4k | { | 109 | 18.4k | c->range <<= 8; | 110 | 18.4k | c->low <<= 8; | 111 | 18.4k | if (c->bytestream < c->bytestream_end) { | 112 | 16.0k | c->low += c->bytestream[0]; | 113 | 16.0k | c->bytestream++; | 114 | 16.0k | } else | 115 | 2.44k | c->overread ++; | 116 | 18.4k | } |
Line | Count | Source | 108 | 7.94M | { | 109 | 7.94M | c->range <<= 8; | 110 | 7.94M | c->low <<= 8; | 111 | 7.94M | if (c->bytestream < c->bytestream_end) { | 112 | 4.88M | c->low += c->bytestream[0]; | 113 | 4.88M | c->bytestream++; | 114 | 4.88M | } else | 115 | 3.06M | c->overread ++; | 116 | 7.94M | } |
Line | Count | Source | 108 | 3.64M | { | 109 | 3.64M | c->range <<= 8; | 110 | 3.64M | c->low <<= 8; | 111 | 3.64M | if (c->bytestream < c->bytestream_end) { | 112 | 1.27M | c->low += c->bytestream[0]; | 113 | 1.27M | c->bytestream++; | 114 | 1.27M | } else | 115 | 2.37M | c->overread ++; | 116 | 3.64M | } |
Line | Count | Source | 108 | 397k | { | 109 | 397k | c->range <<= 8; | 110 | 397k | c->low <<= 8; | 111 | 397k | if (c->bytestream < c->bytestream_end) { | 112 | 241k | c->low += c->bytestream[0]; | 113 | 241k | c->bytestream++; | 114 | 241k | } else | 115 | 156k | c->overread ++; | 116 | 397k | } |
Unexecuted instantiation: ffv1enc.c:refill Unexecuted instantiation: snowenc.c:refill |
117 | | |
118 | | static inline int get_rac(RangeCoder *c, uint8_t *const state) |
119 | 204M | { |
120 | 204M | int range1 = (c->range * (*state)) >> 8; |
121 | | |
122 | 204M | c->range -= range1; |
123 | 204M | if (c->low < c->range) { |
124 | 128M | *state = c->zero_state[*state]; |
125 | 128M | if (c->range < 0x100) |
126 | 6.28M | refill(c); |
127 | 128M | return 0; |
128 | 128M | } else { |
129 | 75.7M | c->low -= c->range; |
130 | 75.7M | *state = c->one_state[*state]; |
131 | 75.7M | c->range = range1; |
132 | 75.7M | if (c->range < 0x100) |
133 | 6.23M | refill(c); |
134 | 75.7M | return 1; |
135 | 75.7M | } |
136 | 204M | } Unexecuted instantiation: snow.c:get_rac Line | Count | Source | 119 | 66.2k | { | 120 | 66.2k | int range1 = (c->range * (*state)) >> 8; | 121 | | | 122 | 66.2k | c->range -= range1; | 123 | 66.2k | if (c->low < c->range) { | 124 | 29.9k | *state = c->zero_state[*state]; | 125 | 29.9k | if (c->range < 0x100) | 126 | 0 | refill(c); | 127 | 29.9k | return 0; | 128 | 36.3k | } else { | 129 | 36.3k | c->low -= c->range; | 130 | 36.3k | *state = c->one_state[*state]; | 131 | 36.3k | c->range = range1; | 132 | 36.3k | if (c->range < 0x100) | 133 | 0 | refill(c); | 134 | 36.3k | return 1; | 135 | 36.3k | } | 136 | 66.2k | } |
Unexecuted instantiation: rangecoder.c:get_rac Line | Count | Source | 119 | 6.92M | { | 120 | 6.92M | int range1 = (c->range * (*state)) >> 8; | 121 | | | 122 | 6.92M | c->range -= range1; | 123 | 6.92M | if (c->low < c->range) { | 124 | 4.24M | *state = c->zero_state[*state]; | 125 | 4.24M | if (c->range < 0x100) | 126 | 228k | refill(c); | 127 | 4.24M | return 0; | 128 | 4.24M | } else { | 129 | 2.67M | c->low -= c->range; | 130 | 2.67M | *state = c->one_state[*state]; | 131 | 2.67M | c->range = range1; | 132 | 2.67M | if (c->range < 0x100) | 133 | 277k | refill(c); | 134 | 2.67M | return 1; | 135 | 2.67M | } | 136 | 6.92M | } |
Line | Count | Source | 119 | 179k | { | 120 | 179k | int range1 = (c->range * (*state)) >> 8; | 121 | | | 122 | 179k | c->range -= range1; | 123 | 179k | if (c->low < c->range) { | 124 | 64.3k | *state = c->zero_state[*state]; | 125 | 64.3k | if (c->range < 0x100) | 126 | 15.0k | refill(c); | 127 | 64.3k | return 0; | 128 | 115k | } else { | 129 | 115k | c->low -= c->range; | 130 | 115k | *state = c->one_state[*state]; | 131 | 115k | c->range = range1; | 132 | 115k | if (c->range < 0x100) | 133 | 3.36k | refill(c); | 134 | 115k | return 1; | 135 | 115k | } | 136 | 179k | } |
Line | Count | Source | 119 | 111M | { | 120 | 111M | int range1 = (c->range * (*state)) >> 8; | 121 | | | 122 | 111M | c->range -= range1; | 123 | 111M | if (c->low < c->range) { | 124 | 79.4M | *state = c->zero_state[*state]; | 125 | 79.4M | if (c->range < 0x100) | 126 | 4.30M | refill(c); | 127 | 79.4M | return 0; | 128 | 79.4M | } else { | 129 | 32.5M | c->low -= c->range; | 130 | 32.5M | *state = c->one_state[*state]; | 131 | 32.5M | c->range = range1; | 132 | 32.5M | if (c->range < 0x100) | 133 | 3.64M | refill(c); | 134 | 32.5M | return 1; | 135 | 32.5M | } | 136 | 111M | } |
Line | Count | Source | 119 | 61.5M | { | 120 | 61.5M | int range1 = (c->range * (*state)) >> 8; | 121 | | | 122 | 61.5M | c->range -= range1; | 123 | 61.5M | if (c->low < c->range) { | 124 | 22.9M | *state = c->zero_state[*state]; | 125 | 22.9M | if (c->range < 0x100) | 126 | 1.53M | refill(c); | 127 | 22.9M | return 0; | 128 | 38.6M | } else { | 129 | 38.6M | c->low -= c->range; | 130 | 38.6M | *state = c->one_state[*state]; | 131 | 38.6M | c->range = range1; | 132 | 38.6M | if (c->range < 0x100) | 133 | 2.11M | refill(c); | 134 | 38.6M | return 1; | 135 | 38.6M | } | 136 | 61.5M | } |
Line | Count | Source | 119 | 23.9M | { | 120 | 23.9M | int range1 = (c->range * (*state)) >> 8; | 121 | | | 122 | 23.9M | c->range -= range1; | 123 | 23.9M | if (c->low < c->range) { | 124 | 22.2M | *state = c->zero_state[*state]; | 125 | 22.2M | if (c->range < 0x100) | 126 | 203k | refill(c); | 127 | 22.2M | return 0; | 128 | 22.2M | } else { | 129 | 1.70M | c->low -= c->range; | 130 | 1.70M | *state = c->one_state[*state]; | 131 | 1.70M | c->range = range1; | 132 | 1.70M | if (c->range < 0x100) | 133 | 194k | refill(c); | 134 | 1.70M | return 1; | 135 | 1.70M | } | 136 | 23.9M | } |
Unexecuted instantiation: ffv1enc.c:get_rac Unexecuted instantiation: snowenc.c:get_rac |
137 | | |
138 | | #endif /* AVCODEC_RANGECODER_H */ |