diff options
author | Igor Pavlov <87184205+ip7z@users.noreply.github.com> | 2023-06-21 00:00:00 +0000 |
---|---|---|
committer | Igor Pavlov <87184205+ip7z@users.noreply.github.com> | 2023-12-17 14:59:19 +0500 |
commit | 5b39dc76f1bc82f941d5c800ab9f34407a06b53a (patch) | |
tree | fe5e17420300b715021a76328444088d32047963 /C/Ppmd8Enc.c | |
parent | 93be7d4abfd4233228f58ee1fbbcd76d91be66a4 (diff) | |
download | 7zip-23.01.tar.gz 7zip-23.01.tar.bz2 7zip-23.01.zip |
23.0123.01
Diffstat (limited to 'C/Ppmd8Enc.c')
-rw-r--r-- | C/Ppmd8Enc.c | 86 |
1 files changed, 55 insertions, 31 deletions
diff --git a/C/Ppmd8Enc.c b/C/Ppmd8Enc.c index 32ff805..9e29ef7 100644 --- a/C/Ppmd8Enc.c +++ b/C/Ppmd8Enc.c | |||
@@ -1,5 +1,5 @@ | |||
1 | /* Ppmd8Enc.c -- Ppmd8 (PPMdI) Encoder | 1 | /* Ppmd8Enc.c -- Ppmd8 (PPMdI) Encoder |
2 | 2021-04-13 : Igor Pavlov : Public domain | 2 | 2023-04-02 : Igor Pavlov : Public domain |
3 | This code is based on: | 3 | This code is based on: |
4 | PPMd var.I (2002): Dmitry Shkarin : Public domain | 4 | PPMd var.I (2002): Dmitry Shkarin : Public domain |
5 | Carryless rangecoder (1999): Dmitry Subbotin : Public domain */ | 5 | Carryless rangecoder (1999): Dmitry Subbotin : Public domain */ |
@@ -8,8 +8,8 @@ This code is based on: | |||
8 | 8 | ||
9 | #include "Ppmd8.h" | 9 | #include "Ppmd8.h" |
10 | 10 | ||
11 | #define kTop (1 << 24) | 11 | #define kTop ((UInt32)1 << 24) |
12 | #define kBot (1 << 15) | 12 | #define kBot ((UInt32)1 << 15) |
13 | 13 | ||
14 | #define WRITE_BYTE(p) IByteOut_Write(p->Stream.Out, (Byte)(p->Low >> 24)) | 14 | #define WRITE_BYTE(p) IByteOut_Write(p->Stream.Out, (Byte)(p->Low >> 24)) |
15 | 15 | ||
@@ -54,13 +54,13 @@ void Ppmd8_Flush_RangeEnc(CPpmd8 *p) | |||
54 | 54 | ||
55 | 55 | ||
56 | 56 | ||
57 | MY_FORCE_INLINE | 57 | Z7_FORCE_INLINE |
58 | // MY_NO_INLINE | 58 | // Z7_NO_INLINE |
59 | static void RangeEnc_Encode(CPpmd8 *p, UInt32 start, UInt32 size, UInt32 total) | 59 | static void Ppmd8_RangeEnc_Encode(CPpmd8 *p, UInt32 start, UInt32 size, UInt32 total) |
60 | { | 60 | { |
61 | R->Low += start * (R->Range /= total); | 61 | R->Low += start * (R->Range /= total); |
62 | R->Range *= size; | 62 | R->Range *= size; |
63 | RC_NORM_LOCAL(R); | 63 | RC_NORM_LOCAL(R) |
64 | } | 64 | } |
65 | 65 | ||
66 | 66 | ||
@@ -72,19 +72,19 @@ static void RangeEnc_Encode(CPpmd8 *p, UInt32 start, UInt32 size, UInt32 total) | |||
72 | 72 | ||
73 | 73 | ||
74 | 74 | ||
75 | #define RC_Encode(start, size, total) RangeEnc_Encode(p, start, size, total); | 75 | #define RC_Encode(start, size, total) Ppmd8_RangeEnc_Encode(p, start, size, total); |
76 | #define RC_EncodeFinal(start, size, total) RC_Encode(start, size, total); RC_NORM_REMOTE(p); | 76 | #define RC_EncodeFinal(start, size, total) RC_Encode(start, size, total) RC_NORM_REMOTE(p) |
77 | 77 | ||
78 | #define CTX(ref) ((CPpmd8_Context *)Ppmd8_GetContext(p, ref)) | 78 | #define CTX(ref) ((CPpmd8_Context *)Ppmd8_GetContext(p, ref)) |
79 | 79 | ||
80 | typedef CPpmd8_Context * CTX_PTR; | 80 | // typedef CPpmd8_Context * CTX_PTR; |
81 | #define SUCCESSOR(p) Ppmd_GET_SUCCESSOR(p) | 81 | #define SUCCESSOR(p) Ppmd_GET_SUCCESSOR(p) |
82 | 82 | ||
83 | void Ppmd8_UpdateModel(CPpmd8 *p); | 83 | void Ppmd8_UpdateModel(CPpmd8 *p); |
84 | 84 | ||
85 | #define MASK(sym) ((unsigned char *)charMask)[sym] | 85 | #define MASK(sym) ((unsigned char *)charMask)[sym] |
86 | 86 | ||
87 | // MY_FORCE_INLINE | 87 | // Z7_FORCE_INLINE |
88 | // static | 88 | // static |
89 | void Ppmd8_EncodeSymbol(CPpmd8 *p, int symbol) | 89 | void Ppmd8_EncodeSymbol(CPpmd8 *p, int symbol) |
90 | { | 90 | { |
@@ -104,7 +104,7 @@ void Ppmd8_EncodeSymbol(CPpmd8 *p, int symbol) | |||
104 | if (s->Symbol == symbol) | 104 | if (s->Symbol == symbol) |
105 | { | 105 | { |
106 | 106 | ||
107 | RC_EncodeFinal(0, s->Freq, summFreq); | 107 | RC_EncodeFinal(0, s->Freq, summFreq) |
108 | p->FoundState = s; | 108 | p->FoundState = s; |
109 | Ppmd8_Update1_0(p); | 109 | Ppmd8_Update1_0(p); |
110 | return; | 110 | return; |
@@ -117,7 +117,7 @@ void Ppmd8_EncodeSymbol(CPpmd8 *p, int symbol) | |||
117 | if ((++s)->Symbol == symbol) | 117 | if ((++s)->Symbol == symbol) |
118 | { | 118 | { |
119 | 119 | ||
120 | RC_EncodeFinal(sum, s->Freq, summFreq); | 120 | RC_EncodeFinal(sum, s->Freq, summFreq) |
121 | p->FoundState = s; | 121 | p->FoundState = s; |
122 | Ppmd8_Update1(p); | 122 | Ppmd8_Update1(p); |
123 | return; | 123 | return; |
@@ -127,10 +127,10 @@ void Ppmd8_EncodeSymbol(CPpmd8 *p, int symbol) | |||
127 | while (--i); | 127 | while (--i); |
128 | 128 | ||
129 | 129 | ||
130 | RC_Encode(sum, summFreq - sum, summFreq); | 130 | RC_Encode(sum, summFreq - sum, summFreq) |
131 | 131 | ||
132 | 132 | ||
133 | PPMD_SetAllBitsIn256Bytes(charMask); | 133 | PPMD_SetAllBitsIn256Bytes(charMask) |
134 | // MASK(s->Symbol) = 0; | 134 | // MASK(s->Symbol) = 0; |
135 | // i = p->MinContext->NumStats; | 135 | // i = p->MinContext->NumStats; |
136 | // do { MASK((--s)->Symbol) = 0; } while (--i); | 136 | // do { MASK((--s)->Symbol) = 0; } while (--i); |
@@ -153,20 +153,20 @@ void Ppmd8_EncodeSymbol(CPpmd8 *p, int symbol) | |||
153 | UInt16 *prob = Ppmd8_GetBinSumm(p); | 153 | UInt16 *prob = Ppmd8_GetBinSumm(p); |
154 | CPpmd_State *s = Ppmd8Context_OneState(p->MinContext); | 154 | CPpmd_State *s = Ppmd8Context_OneState(p->MinContext); |
155 | UInt32 pr = *prob; | 155 | UInt32 pr = *prob; |
156 | UInt32 bound = (R->Range >> 14) * pr; | 156 | const UInt32 bound = (R->Range >> 14) * pr; |
157 | pr = PPMD_UPDATE_PROB_1(pr); | 157 | pr = PPMD_UPDATE_PROB_1(pr); |
158 | if (s->Symbol == symbol) | 158 | if (s->Symbol == symbol) |
159 | { | 159 | { |
160 | *prob = (UInt16)(pr + (1 << PPMD_INT_BITS)); | 160 | *prob = (UInt16)(pr + (1 << PPMD_INT_BITS)); |
161 | // RangeEnc_EncodeBit_0(p, bound); | 161 | // RangeEnc_EncodeBit_0(p, bound); |
162 | R->Range = bound; | 162 | R->Range = bound; |
163 | RC_NORM(R); | 163 | RC_NORM(R) |
164 | 164 | ||
165 | // p->FoundState = s; | 165 | // p->FoundState = s; |
166 | // Ppmd8_UpdateBin(p); | 166 | // Ppmd8_UpdateBin(p); |
167 | { | 167 | { |
168 | unsigned freq = s->Freq; | 168 | const unsigned freq = s->Freq; |
169 | CTX_PTR c = CTX(SUCCESSOR(s)); | 169 | CPpmd8_Context *c = CTX(SUCCESSOR(s)); |
170 | p->FoundState = s; | 170 | p->FoundState = s; |
171 | p->PrevSuccess = 1; | 171 | p->PrevSuccess = 1; |
172 | p->RunLength++; | 172 | p->RunLength++; |
@@ -187,7 +187,7 @@ void Ppmd8_EncodeSymbol(CPpmd8 *p, int symbol) | |||
187 | R->Range = (R->Range & ~((UInt32)PPMD_BIN_SCALE - 1)) - bound; | 187 | R->Range = (R->Range & ~((UInt32)PPMD_BIN_SCALE - 1)) - bound; |
188 | RC_NORM_LOCAL(R) | 188 | RC_NORM_LOCAL(R) |
189 | 189 | ||
190 | PPMD_SetAllBitsIn256Bytes(charMask); | 190 | PPMD_SetAllBitsIn256Bytes(charMask) |
191 | MASK(s->Symbol) = 0; | 191 | MASK(s->Symbol) = 0; |
192 | p->PrevSuccess = 0; | 192 | p->PrevSuccess = 0; |
193 | } | 193 | } |
@@ -248,14 +248,14 @@ void Ppmd8_EncodeSymbol(CPpmd8 *p, int symbol) | |||
248 | 248 | ||
249 | do | 249 | do |
250 | { | 250 | { |
251 | unsigned cur = s->Symbol; | 251 | const unsigned cur = s->Symbol; |
252 | if ((int)cur == symbol) | 252 | if ((int)cur == symbol) |
253 | { | 253 | { |
254 | UInt32 low = sum; | 254 | const UInt32 low = sum; |
255 | UInt32 freq = s->Freq; | 255 | const UInt32 freq = s->Freq; |
256 | unsigned num2; | 256 | unsigned num2; |
257 | 257 | ||
258 | Ppmd_See_Update(see); | 258 | Ppmd_See_UPDATE(see) |
259 | p->FoundState = s; | 259 | p->FoundState = s; |
260 | sum += escFreq; | 260 | sum += escFreq; |
261 | 261 | ||
@@ -277,9 +277,9 @@ void Ppmd8_EncodeSymbol(CPpmd8 *p, int symbol) | |||
277 | } | 277 | } |
278 | } | 278 | } |
279 | 279 | ||
280 | PPMD8_CORRECT_SUM_RANGE(p, sum); | 280 | PPMD8_CORRECT_SUM_RANGE(p, sum) |
281 | 281 | ||
282 | RC_EncodeFinal(low, freq, sum); | 282 | RC_EncodeFinal(low, freq, sum) |
283 | Ppmd8_Update2(p); | 283 | Ppmd8_Update2(p); |
284 | return; | 284 | return; |
285 | } | 285 | } |
@@ -291,19 +291,19 @@ void Ppmd8_EncodeSymbol(CPpmd8 *p, int symbol) | |||
291 | { | 291 | { |
292 | UInt32 total = sum + escFreq; | 292 | UInt32 total = sum + escFreq; |
293 | see->Summ = (UInt16)(see->Summ + total); | 293 | see->Summ = (UInt16)(see->Summ + total); |
294 | PPMD8_CORRECT_SUM_RANGE(p, total); | 294 | PPMD8_CORRECT_SUM_RANGE(p, total) |
295 | 295 | ||
296 | RC_Encode(sum, total - sum, total); | 296 | RC_Encode(sum, total - sum, total) |
297 | } | 297 | } |
298 | 298 | ||
299 | { | 299 | { |
300 | CPpmd_State *s2 = Ppmd8_GetStats(p, p->MinContext); | 300 | const CPpmd_State *s2 = Ppmd8_GetStats(p, p->MinContext); |
301 | s--; | 301 | s--; |
302 | MASK(s->Symbol) = 0; | 302 | MASK(s->Symbol) = 0; |
303 | do | 303 | do |
304 | { | 304 | { |
305 | unsigned sym0 = s2[0].Symbol; | 305 | const unsigned sym0 = s2[0].Symbol; |
306 | unsigned sym1 = s2[1].Symbol; | 306 | const unsigned sym1 = s2[1].Symbol; |
307 | s2 += 2; | 307 | s2 += 2; |
308 | MASK(sym0) = 0; | 308 | MASK(sym0) = 0; |
309 | MASK(sym1) = 0; | 309 | MASK(sym1) = 0; |
@@ -312,3 +312,27 @@ void Ppmd8_EncodeSymbol(CPpmd8 *p, int symbol) | |||
312 | } | 312 | } |
313 | } | 313 | } |
314 | } | 314 | } |
315 | |||
316 | |||
317 | |||
318 | |||
319 | |||
320 | |||
321 | |||
322 | |||
323 | |||
324 | #undef kTop | ||
325 | #undef kBot | ||
326 | #undef WRITE_BYTE | ||
327 | #undef RC_NORM_BASE | ||
328 | #undef RC_NORM_1 | ||
329 | #undef RC_NORM | ||
330 | #undef RC_NORM_LOCAL | ||
331 | #undef RC_NORM_REMOTE | ||
332 | #undef R | ||
333 | #undef RC_Encode | ||
334 | #undef RC_EncodeFinal | ||
335 | |||
336 | #undef CTX | ||
337 | #undef SUCCESSOR | ||
338 | #undef MASK | ||