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/Ppmd7Enc.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/Ppmd7Enc.c')
-rw-r--r-- | C/Ppmd7Enc.c | 87 |
1 files changed, 51 insertions, 36 deletions
diff --git a/C/Ppmd7Enc.c b/C/Ppmd7Enc.c index 62139c5..41106ba 100644 --- a/C/Ppmd7Enc.c +++ b/C/Ppmd7Enc.c | |||
@@ -1,5 +1,5 @@ | |||
1 | /* Ppmd7Enc.c -- Ppmd7z (PPMdH with 7z Range Coder) Encoder | 1 | /* Ppmd7Enc.c -- Ppmd7z (PPMdH with 7z Range Coder) 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.H (2001): Dmitry Shkarin : Public domain */ | 4 | PPMd var.H (2001): Dmitry Shkarin : Public domain */ |
5 | 5 | ||
@@ -8,7 +8,7 @@ This code is based on: | |||
8 | 8 | ||
9 | #include "Ppmd7.h" | 9 | #include "Ppmd7.h" |
10 | 10 | ||
11 | #define kTopValue (1 << 24) | 11 | #define kTopValue ((UInt32)1 << 24) |
12 | 12 | ||
13 | #define R (&p->rc.enc) | 13 | #define R (&p->rc.enc) |
14 | 14 | ||
@@ -20,8 +20,8 @@ void Ppmd7z_Init_RangeEnc(CPpmd7 *p) | |||
20 | R->CacheSize = 1; | 20 | R->CacheSize = 1; |
21 | } | 21 | } |
22 | 22 | ||
23 | MY_NO_INLINE | 23 | Z7_NO_INLINE |
24 | static void RangeEnc_ShiftLow(CPpmd7 *p) | 24 | static void Ppmd7z_RangeEnc_ShiftLow(CPpmd7 *p) |
25 | { | 25 | { |
26 | if ((UInt32)R->Low < (UInt32)0xFF000000 || (unsigned)(R->Low >> 32) != 0) | 26 | if ((UInt32)R->Low < (UInt32)0xFF000000 || (unsigned)(R->Low >> 32) != 0) |
27 | { | 27 | { |
@@ -38,53 +38,53 @@ static void RangeEnc_ShiftLow(CPpmd7 *p) | |||
38 | R->Low = (UInt32)((UInt32)R->Low << 8); | 38 | R->Low = (UInt32)((UInt32)R->Low << 8); |
39 | } | 39 | } |
40 | 40 | ||
41 | #define RC_NORM_BASE(p) if (R->Range < kTopValue) { R->Range <<= 8; RangeEnc_ShiftLow(p); | 41 | #define RC_NORM_BASE(p) if (R->Range < kTopValue) { R->Range <<= 8; Ppmd7z_RangeEnc_ShiftLow(p); |
42 | #define RC_NORM_1(p) RC_NORM_BASE(p) } | 42 | #define RC_NORM_1(p) RC_NORM_BASE(p) } |
43 | #define RC_NORM(p) RC_NORM_BASE(p) RC_NORM_BASE(p) }} | 43 | #define RC_NORM(p) RC_NORM_BASE(p) RC_NORM_BASE(p) }} |
44 | 44 | ||
45 | // we must use only one type of Normalization from two: LOCAL or REMOTE | 45 | // we must use only one type of Normalization from two: LOCAL or REMOTE |
46 | #define RC_NORM_LOCAL(p) // RC_NORM(p) | 46 | #define RC_NORM_LOCAL(p) // RC_NORM(p) |
47 | #define RC_NORM_REMOTE(p) RC_NORM(p) | 47 | #define RC_NORM_REMOTE(p) RC_NORM(p) |
48 | 48 | ||
49 | /* | 49 | /* |
50 | #define RangeEnc_Encode(p, start, _size_) \ | 50 | #define Ppmd7z_RangeEnc_Encode(p, start, _size_) \ |
51 | { UInt32 size = _size_; \ | 51 | { UInt32 size = _size_; \ |
52 | R->Low += start * R->Range; \ | 52 | R->Low += start * R->Range; \ |
53 | R->Range *= size; \ | 53 | R->Range *= size; \ |
54 | RC_NORM_LOCAL(p); } | 54 | RC_NORM_LOCAL(p); } |
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(CPpmd7 *p, UInt32 start, UInt32 size) | 59 | static void Ppmd7z_RangeEnc_Encode(CPpmd7 *p, UInt32 start, UInt32 size) |
60 | { | 60 | { |
61 | R->Low += start * R->Range; | 61 | R->Low += start * R->Range; |
62 | R->Range *= size; | 62 | R->Range *= size; |
63 | RC_NORM_LOCAL(p); | 63 | RC_NORM_LOCAL(p) |
64 | } | 64 | } |
65 | 65 | ||
66 | void Ppmd7z_Flush_RangeEnc(CPpmd7 *p) | 66 | void Ppmd7z_Flush_RangeEnc(CPpmd7 *p) |
67 | { | 67 | { |
68 | unsigned i; | 68 | unsigned i; |
69 | for (i = 0; i < 5; i++) | 69 | for (i = 0; i < 5; i++) |
70 | RangeEnc_ShiftLow(p); | 70 | Ppmd7z_RangeEnc_ShiftLow(p); |
71 | } | 71 | } |
72 | 72 | ||
73 | 73 | ||
74 | 74 | ||
75 | #define RC_Encode(start, size) RangeEnc_Encode(p, start, size); | 75 | #define RC_Encode(start, size) Ppmd7z_RangeEnc_Encode(p, start, size); |
76 | #define RC_EncodeFinal(start, size) RC_Encode(start, size); RC_NORM_REMOTE(p); | 76 | #define RC_EncodeFinal(start, size) RC_Encode(start, size) RC_NORM_REMOTE(p) |
77 | 77 | ||
78 | #define CTX(ref) ((CPpmd7_Context *)Ppmd7_GetContext(p, ref)) | 78 | #define CTX(ref) ((CPpmd7_Context *)Ppmd7_GetContext(p, ref)) |
79 | #define SUFFIX(ctx) CTX((ctx)->Suffix) | 79 | #define SUFFIX(ctx) CTX((ctx)->Suffix) |
80 | typedef CPpmd7_Context * CTX_PTR; | 80 | // typedef CPpmd7_Context * CTX_PTR; |
81 | #define SUCCESSOR(p) Ppmd_GET_SUCCESSOR(p) | 81 | #define SUCCESSOR(p) Ppmd_GET_SUCCESSOR(p) |
82 | 82 | ||
83 | void Ppmd7_UpdateModel(CPpmd7 *p); | 83 | void Ppmd7_UpdateModel(CPpmd7 *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 Ppmd7z_EncodeSymbol(CPpmd7 *p, int symbol) | 89 | void Ppmd7z_EncodeSymbol(CPpmd7 *p, int symbol) |
90 | { | 90 | { |
@@ -104,7 +104,7 @@ void Ppmd7z_EncodeSymbol(CPpmd7 *p, int symbol) | |||
104 | if (s->Symbol == symbol) | 104 | if (s->Symbol == symbol) |
105 | { | 105 | { |
106 | // R->Range /= p->MinContext->Union2.SummFreq; | 106 | // R->Range /= p->MinContext->Union2.SummFreq; |
107 | RC_EncodeFinal(0, s->Freq); | 107 | RC_EncodeFinal(0, s->Freq) |
108 | p->FoundState = s; | 108 | p->FoundState = s; |
109 | Ppmd7_Update1_0(p); | 109 | Ppmd7_Update1_0(p); |
110 | return; | 110 | return; |
@@ -117,7 +117,7 @@ void Ppmd7z_EncodeSymbol(CPpmd7 *p, int symbol) | |||
117 | if ((++s)->Symbol == symbol) | 117 | if ((++s)->Symbol == symbol) |
118 | { | 118 | { |
119 | // R->Range /= p->MinContext->Union2.SummFreq; | 119 | // R->Range /= p->MinContext->Union2.SummFreq; |
120 | RC_EncodeFinal(sum, s->Freq); | 120 | RC_EncodeFinal(sum, s->Freq) |
121 | p->FoundState = s; | 121 | p->FoundState = s; |
122 | Ppmd7_Update1(p); | 122 | Ppmd7_Update1(p); |
123 | return; | 123 | return; |
@@ -127,10 +127,10 @@ void Ppmd7z_EncodeSymbol(CPpmd7 *p, int symbol) | |||
127 | while (--i); | 127 | while (--i); |
128 | 128 | ||
129 | // R->Range /= p->MinContext->Union2.SummFreq; | 129 | // R->Range /= p->MinContext->Union2.SummFreq; |
130 | RC_Encode(sum, p->MinContext->Union2.SummFreq - sum); | 130 | RC_Encode(sum, p->MinContext->Union2.SummFreq - sum) |
131 | 131 | ||
132 | p->HiBitsFlag = PPMD7_HiBitsFlag_3(p->FoundState->Symbol); | 132 | p->HiBitsFlag = PPMD7_HiBitsFlag_3(p->FoundState->Symbol); |
133 | PPMD_SetAllBitsIn256Bytes(charMask); | 133 | PPMD_SetAllBitsIn256Bytes(charMask) |
134 | // MASK(s->Symbol) = 0; | 134 | // MASK(s->Symbol) = 0; |
135 | // i = p->MinContext->NumStats - 1; | 135 | // i = p->MinContext->NumStats - 1; |
136 | // do { MASK((--s)->Symbol) = 0; } while (--i); | 136 | // do { MASK((--s)->Symbol) = 0; } while (--i); |
@@ -153,20 +153,20 @@ void Ppmd7z_EncodeSymbol(CPpmd7 *p, int symbol) | |||
153 | UInt16 *prob = Ppmd7_GetBinSumm(p); | 153 | UInt16 *prob = Ppmd7_GetBinSumm(p); |
154 | CPpmd_State *s = Ppmd7Context_OneState(p->MinContext); | 154 | CPpmd_State *s = Ppmd7Context_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_1(p); | 163 | RC_NORM_1(p) |
164 | 164 | ||
165 | // p->FoundState = s; | 165 | // p->FoundState = s; |
166 | // Ppmd7_UpdateBin(p); | 166 | // Ppmd7_UpdateBin(p); |
167 | { | 167 | { |
168 | unsigned freq = s->Freq; | 168 | const unsigned freq = s->Freq; |
169 | CTX_PTR c = CTX(SUCCESSOR(s)); | 169 | CPpmd7_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 Ppmd7z_EncodeSymbol(CPpmd7 *p, int symbol) | |||
187 | R->Range -= bound; | 187 | R->Range -= bound; |
188 | RC_NORM_LOCAL(p) | 188 | RC_NORM_LOCAL(p) |
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 Ppmd7z_EncodeSymbol(CPpmd7 *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 | ||
@@ -279,7 +279,7 @@ void Ppmd7z_EncodeSymbol(CPpmd7 *p, int symbol) | |||
279 | 279 | ||
280 | 280 | ||
281 | R->Range /= sum; | 281 | R->Range /= sum; |
282 | RC_EncodeFinal(low, freq); | 282 | RC_EncodeFinal(low, freq) |
283 | Ppmd7_Update2(p); | 283 | Ppmd7_Update2(p); |
284 | return; | 284 | return; |
285 | } | 285 | } |
@@ -289,21 +289,21 @@ void Ppmd7z_EncodeSymbol(CPpmd7 *p, int symbol) | |||
289 | while (--i); | 289 | while (--i); |
290 | 290 | ||
291 | { | 291 | { |
292 | UInt32 total = sum + escFreq; | 292 | const UInt32 total = sum + escFreq; |
293 | see->Summ = (UInt16)(see->Summ + total); | 293 | see->Summ = (UInt16)(see->Summ + total); |
294 | 294 | ||
295 | R->Range /= total; | 295 | R->Range /= total; |
296 | RC_Encode(sum, escFreq); | 296 | RC_Encode(sum, escFreq) |
297 | } | 297 | } |
298 | 298 | ||
299 | { | 299 | { |
300 | CPpmd_State *s2 = Ppmd7_GetStats(p, p->MinContext); | 300 | const CPpmd_State *s2 = Ppmd7_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; |
@@ -321,3 +321,18 @@ void Ppmd7z_EncodeSymbols(CPpmd7 *p, const Byte *buf, const Byte *lim) | |||
321 | Ppmd7z_EncodeSymbol(p, *buf); | 321 | Ppmd7z_EncodeSymbol(p, *buf); |
322 | } | 322 | } |
323 | } | 323 | } |
324 | |||
325 | #undef kTopValue | ||
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 | #undef SUFFIX | ||
336 | #undef CTX | ||
337 | #undef SUCCESSOR | ||
338 | #undef MASK | ||