aboutsummaryrefslogtreecommitdiffstats
path: root/library/cpp/codecs/huffman_codec.cpp
blob: 650fe7cdfdd925902116344360642e80d0f88884 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
#include "huffman_codec.h"
#include <library/cpp/bit_io/bitinput.h>
#include <library/cpp/bit_io/bitoutput.h>

#include <util/generic/algorithm.h>
#include <util/generic/bitops.h>
#include <util/stream/buffer.h>
#include <util/stream/length.h>
#include <util/string/printf.h>

namespace NCodecs {
    template <typename T>
    struct TCanonicalCmp {
        bool operator()(const T& a, const T& b) const {
            if (a.CodeLength == b.CodeLength) {
                return a.Char < b.Char;
            } else {
                return a.CodeLength < b.CodeLength;
            }
        }
    };

    template <typename T>
    struct TByCharCmp {
        bool operator()(const T& a, const T& b) const {
            return a.Char < b.Char;
        }
    };

    struct TTreeEntry {
        static const ui32 InvalidBranch = (ui32)-1;

        ui64 Freq = 0;
        ui32 Branches[2]{InvalidBranch, InvalidBranch};

        ui32 CodeLength = 0;
        ui8 Char = 0;
        bool Invalid = false;

        TTreeEntry() = default;

        static bool ByFreq(const TTreeEntry& a, const TTreeEntry& b) {
            return a.Freq < b.Freq;
        }

        static bool ByFreqRev(const TTreeEntry& a, const TTreeEntry& b) {
            return a.Freq > b.Freq;
        }
    };

    using TCodeTree = TVector<TTreeEntry>;

    void InitTreeByFreqs(TCodeTree& tree, const ui64 freqs[256]) {
        tree.reserve(255 * 256 / 2); // worst case - balanced tree

        for (ui32 i = 0; i < 256; ++i) {
            tree.emplace_back();
            tree.back().Char = i;
            tree.back().Freq = freqs[i];
        }

        StableSort(tree.begin(), tree.end(), TTreeEntry::ByFreq);
    }

    void InitTree(TCodeTree& tree, ISequenceReader* in) {
        using namespace NPrivate;
        ui64 freqs[256];
        Zero(freqs);

        TStringBuf r;
        while (in->NextRegion(r)) {
            for (ui64 i = 0; i < r.size(); ++i)
                ++freqs[(ui8)r[i]];
        }

        InitTreeByFreqs(tree, freqs);
    }

    void CalculateCodeLengths(TCodeTree& tree) {
        Y_ENSURE(tree.size() == 256, " ");
        const ui32 firstbranch = tree.size();

        ui32 curleaf = 0;
        ui32 curbranch = firstbranch;

        // building code tree. two priority queues are combined in one.
        while (firstbranch - curleaf + tree.size() - curbranch >= 2) {
            TTreeEntry e;

            for (auto& branche : e.Branches) {
                ui32 br;

                if (curleaf >= firstbranch)
                    br = curbranch++;
                else if (curbranch >= tree.size())
                    br = curleaf++;
                else if (tree[curleaf].Freq < tree[curbranch].Freq)
                    br = curleaf++;
                else
                    br = curbranch++;

                Y_ENSURE(br < tree.size(), " ");
                branche = br;
                e.Freq += tree[br].Freq;
            }

            tree.push_back(e);
            PushHeap(tree.begin() + curbranch, tree.end(), TTreeEntry::ByFreqRev);
        }

        // computing code lengths
        for (ui64 i = tree.size() - 1; i >= firstbranch; --i) {
            TTreeEntry e = tree[i];

            for (auto branche : e.Branches)
                tree[branche].CodeLength = e.CodeLength + 1;
        }

        // chopping off the branches
        tree.resize(firstbranch);

        Sort(tree.begin(), tree.end(), TCanonicalCmp<TTreeEntry>());

        // simplification: we are stripping codes longer than 64 bits
        while (!tree.empty() && tree.back().CodeLength > 64)
            tree.pop_back();

        // will not compress
        if (tree.empty())
            return;

        // special invalid code word
        tree.back().Invalid = true;
    }

    struct TEncoderEntry {
        ui64 Code = 0;

        ui8 CodeLength = 0;
        ui8 Char = 0;
        ui8 Invalid = true;

        explicit TEncoderEntry(TTreeEntry e)
            : CodeLength(e.CodeLength)
            , Char(e.Char)
            , Invalid(e.Invalid)
        {
        }

        TEncoderEntry() = default;
    };

    struct TEncoderTable {
        TEncoderEntry Entries[256];

        void Save(IOutputStream* out) const {
            ui16 nval = 0;

            for (auto entrie : Entries)
                nval += !entrie.Invalid;

            ::Save(out, nval);

            for (auto entrie : Entries) {
                if (!entrie.Invalid) {
                    ::Save(out, entrie.Char);
                    ::Save(out, entrie.CodeLength);
                }
            }
        }

        void Load(IInputStream* in) {
            ui16 nval = 0;
            ::Load(in, nval);

            for (ui32 i = 0; i < 256; ++i)
                Entries[i].Char = i;

            for (ui32 i = 0; i < nval; ++i) {
                ui8 ch = 0;
                ui8 len = 0;
                ::Load(in, ch);
                ::Load(in, len);
                Entries[ch].CodeLength = len;
                Entries[ch].Invalid = false;
            }
        }
    };

    struct TDecoderEntry {
        ui32 NextTable : 10;
        ui32 Char : 8;
        ui32 Invalid : 1;
        ui32 Bad : 1;

        TDecoderEntry()
            : NextTable()
            , Char()
            , Invalid()
            , Bad()
        {
        }
    };

    struct TDecoderTable: public TIntrusiveListItem<TDecoderTable> {
        ui64 Length = 0;
        ui64 BaseCode = 0;

        TDecoderEntry Entries[256];

        TDecoderTable() {
            Zero(Entries);
        }
    };

    const int CACHE_BITS_COUNT = 16;
    class THuffmanCodec::TImpl: public TAtomicRefCount<TImpl> {
        TEncoderTable Encoder;
        TDecoderTable Decoder[256];

        TEncoderEntry Invalid;

        ui32 SubTablesNum;

        class THuffmanCache {
            struct TCacheEntry {
                int EndOffset : 24;
                int BitsLeft : 8;
            };
            TVector<char> DecodeCache;
            TVector<TCacheEntry> CacheEntries;
            const TImpl& Original;

        public:
            THuffmanCache(const THuffmanCodec::TImpl& encoder);

            void Decode(NBitIO::TBitInput& in, TBuffer& out) const;
        };

        THolder<THuffmanCache> Cache;

    public:
        TImpl()
            : SubTablesNum(1)
        {
            Invalid.CodeLength = 255;
        }

        ui8 Encode(TStringBuf in, TBuffer& out) const {
            out.Clear();

            if (in.empty()) {
                return 0;
            }

            out.Reserve(in.size() * 2);

            {
                NBitIO::TBitOutputVector<TBuffer> bout(&out);
                TStringBuf tin = in;

                // data is under compression
                bout.Write(1, 1);

                for (auto t : tin) {
                    const TEncoderEntry& ce = Encoder.Entries[(ui8)t];

                    bout.Write(ce.Code, ce.CodeLength);

                    if (ce.Invalid) {
                        bout.Write(t, 8);
                    }
                }

                // in canonical huffman coding there cannot be a code having no 0 in the suffix
                // and shorter than 8 bits.
                bout.Write((ui64)-1, bout.GetByteReminder());
                return bout.GetByteReminder();
            }
        }

        void Decode(TStringBuf in, TBuffer& out) const {
            out.Clear();

            if (in.empty()) {
                return;
            }

            NBitIO::TBitInput bin(in);
            ui64 f = 0;
            bin.ReadK<1>(f);

            // if data is uncompressed
            if (!f) {
                in.Skip(1);
                out.Append(in.data(), in.size());
            } else {
                out.Reserve(in.size() * 8);

                if (Cache.Get()) {
                    Cache->Decode(bin, out);
                } else {
                    while (ReadNextChar(bin, out)) {
                    }
                }
            }
        }

        Y_FORCE_INLINE int ReadNextChar(NBitIO::TBitInput& bin, TBuffer& out) const {
            const TDecoderTable* table = Decoder;
            TDecoderEntry e;

            int bitsRead = 0;
            while (true) {
                ui64 code = 0;

                if (Y_UNLIKELY(!bin.Read(code, table->Length)))
                    return 0;
                bitsRead += table->Length;

                if (Y_UNLIKELY(code < table->BaseCode))
                    return 0;

                code -= table->BaseCode;

                if (Y_UNLIKELY(code > 255))
                    return 0;

                e = table->Entries[code];

                if (Y_UNLIKELY(e.Bad))
                    return 0;

                if (e.NextTable) {
                    table = Decoder + e.NextTable;
                } else {
                    if (e.Invalid) {
                        code = 0;
                        bin.ReadK<8>(code);
                        bitsRead += 8;
                        out.Append((ui8)code);
                    } else {
                        out.Append((ui8)e.Char);
                    }

                    return bitsRead;
                }
            }

            Y_ENSURE(false, " could not decode input");
            return 0;
        }

        void GenerateEncoder(TCodeTree& tree) {
            const ui64 sz = tree.size();

            TEncoderEntry lastcode = Encoder.Entries[tree[0].Char] = TEncoderEntry(tree[0]);

            for (ui32 i = 1; i < sz; ++i) {
                const TTreeEntry& te = tree[i];
                TEncoderEntry& e = Encoder.Entries[te.Char];
                e = TEncoderEntry(te);

                e.Code = (lastcode.Code + 1) << (e.CodeLength - lastcode.CodeLength);
                lastcode = e;

                e.Code = ReverseBits(e.Code, e.CodeLength);

                if (e.Invalid)
                    Invalid = e;
            }

            for (auto& e : Encoder.Entries) {
                if (e.Invalid)
                    e = Invalid;

                Y_ENSURE(e.CodeLength, " ");
            }
        }

        void RegenerateEncoder() {
            for (auto& entrie : Encoder.Entries) {
                if (entrie.Invalid)
                    entrie.CodeLength = Invalid.CodeLength;
            }

            Sort(Encoder.Entries, Encoder.Entries + 256, TCanonicalCmp<TEncoderEntry>());

            TEncoderEntry lastcode = Encoder.Entries[0];

            for (ui32 i = 1; i < 256; ++i) {
                TEncoderEntry& e = Encoder.Entries[i];
                e.Code = (lastcode.Code + 1) << (e.CodeLength - lastcode.CodeLength);
                lastcode = e;

                e.Code = ReverseBits(e.Code, e.CodeLength);
            }

            for (auto& entrie : Encoder.Entries) {
                if (entrie.Invalid) {
                    Invalid = entrie;
                    break;
                }
            }

            Sort(Encoder.Entries, Encoder.Entries + 256, TByCharCmp<TEncoderEntry>());

            for (auto& entrie : Encoder.Entries) {
                if (entrie.Invalid)
                    entrie = Invalid;
            }
        }

        void BuildDecoder() {
            TEncoderTable enc = Encoder;
            Sort(enc.Entries, enc.Entries + 256, TCanonicalCmp<TEncoderEntry>());

            TEncoderEntry& e1 = enc.Entries[0];
            Decoder[0].BaseCode = e1.Code;
            Decoder[0].Length = e1.CodeLength;

            for (auto e2 : enc.Entries) {
                SetEntry(Decoder, e2.Code, e2.CodeLength, e2);
            }
            Cache.Reset(new THuffmanCache(*this));
        }

        void SetEntry(TDecoderTable* t, ui64 code, ui64 len, TEncoderEntry e) {
            Y_ENSURE(len >= t->Length, len << " < " << t->Length);

            ui64 idx = (code & MaskLowerBits(t->Length)) - t->BaseCode;
            TDecoderEntry& d = t->Entries[idx];

            if (len == t->Length) {
                Y_ENSURE(!d.NextTable, " ");

                d.Char = e.Char;
                d.Invalid = e.Invalid;
                return;
            }

            if (!d.NextTable) {
                Y_ENSURE(SubTablesNum < Y_ARRAY_SIZE(Decoder), " ");
                d.NextTable = SubTablesNum++;
                TDecoderTable* nt = Decoder + d.NextTable;
                nt->Length = Min<ui64>(8, len - t->Length);
                nt->BaseCode = (code >> t->Length) & MaskLowerBits(nt->Length);
            }

            SetEntry(Decoder + d.NextTable, code >> t->Length, len - t->Length, e);
        }

        void Learn(ISequenceReader* in) {
            {
                TCodeTree tree;
                InitTree(tree, in);
                CalculateCodeLengths(tree);
                Y_ENSURE(!tree.empty(), " ");
                GenerateEncoder(tree);
            }
            BuildDecoder();
        }

        void LearnByFreqs(const TArrayRef<std::pair<char, ui64>>& freqs) {
            TCodeTree tree;

            ui64 freqsArray[256];
            Zero(freqsArray);

            for (const auto& freq : freqs)
                freqsArray[static_cast<ui8>(freq.first)] += freq.second;

            InitTreeByFreqs(tree, freqsArray);
            CalculateCodeLengths(tree);

            Y_ENSURE(!tree.empty(), " ");

            GenerateEncoder(tree);
            BuildDecoder();
        }

        void Save(IOutputStream* out) {
            ::Save(out, Invalid.CodeLength);
            Encoder.Save(out);
        }

        void Load(IInputStream* in) {
            ::Load(in, Invalid.CodeLength);
            Encoder.Load(in);
            RegenerateEncoder();
            BuildDecoder();
        }
    };

    THuffmanCodec::TImpl::THuffmanCache::THuffmanCache(const THuffmanCodec::TImpl& codec)
        : Original(codec)
    {
        CacheEntries.resize(1 << CACHE_BITS_COUNT);
        DecodeCache.reserve(CacheEntries.size() * 2);
        char buffer[2];
        TBuffer decoded;
        for (size_t i = 0; i < CacheEntries.size(); i++) {
            buffer[1] = i >> 8;
            buffer[0] = i;
            NBitIO::TBitInput bin(buffer, buffer + sizeof(buffer));
            int totalBits = 0;
            while (true) {
                decoded.Resize(0);
                int bits = codec.ReadNextChar(bin, decoded);
                if (totalBits + bits > 16 || !bits) {
                    TCacheEntry e = {static_cast<int>(DecodeCache.size()), 16 - totalBits};
                    CacheEntries[i] = e;
                    break;
                }

                for (TBuffer::TConstIterator it = decoded.Begin(); it != decoded.End(); ++it) {
                    DecodeCache.push_back(*it);
                }
                totalBits += bits;
            }
        }
        DecodeCache.push_back(0);
        CacheEntries.shrink_to_fit();
        DecodeCache.shrink_to_fit();
    }

    void THuffmanCodec::TImpl::THuffmanCache::Decode(NBitIO::TBitInput& bin, TBuffer& out) const {
        int bits = 0;
        ui64 code = 0;
        while (!bin.Eof()) {
            ui64 f = 0;
            const int toRead = 16 - bits;
            if (toRead > 0 && bin.Read(f, toRead)) {
                code = (code >> (16 - bits)) | (f << bits);
                code &= 0xFFFF;
                TCacheEntry entry = CacheEntries[code];
                int start = code > 0 ? CacheEntries[code - 1].EndOffset : 0;
                out.Append((const char*)&DecodeCache[start], (const char*)&DecodeCache[entry.EndOffset]);
                bits = entry.BitsLeft;
            } else { // should never happen until there are exceptions or unaligned input
                bin.Back(bits);
                if (!Original.ReadNextChar(bin, out))
                    break;

                code = 0;
                bits = 0;
            }
        }
    }

    THuffmanCodec::THuffmanCodec()
        : Impl(new TImpl)
    {
        MyTraits.NeedsTraining = true;
        MyTraits.PreservesPrefixGrouping = true;
        MyTraits.PaddingBit = 1;
        MyTraits.SizeOnEncodeMultiplier = 2;
        MyTraits.SizeOnDecodeMultiplier = 8;
        MyTraits.RecommendedSampleSize = 1 << 21;
    }

    THuffmanCodec::~THuffmanCodec() = default;

    ui8 THuffmanCodec::Encode(TStringBuf in, TBuffer& bbb) const {
        if (Y_UNLIKELY(!Trained))
            ythrow TCodecException() << " not trained";

        return Impl->Encode(in, bbb);
    }

    void THuffmanCodec::Decode(TStringBuf in, TBuffer& bbb) const {
        Impl->Decode(in, bbb);
    }

    void THuffmanCodec::Save(IOutputStream* out) const {
        Impl->Save(out);
    }

    void THuffmanCodec::Load(IInputStream* in) {
        Impl->Load(in);
    }

    void THuffmanCodec::DoLearn(ISequenceReader& in) {
        Impl->Learn(&in);
    }

    void THuffmanCodec::LearnByFreqs(const TArrayRef<std::pair<char, ui64>>& freqs) {
        Impl->LearnByFreqs(freqs);
        Trained = true;
    }

}