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 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
/* Copyright 2020 InterDigital Communications, Inc.
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *    http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

/* Rans64 extensions from:
 * https://fgiesen.wordpress.com/2015/12/21/rans-in-practice/
 * Unbounded range coding from:
 * https://github.com/tensorflow/compression/blob/master/tensorflow_compression/cc/kernels/unbounded_index_range_coding_kernels.cc
 **/

#include "rans.h"

#include <algorithm>
#include <cassert>
#include <cstring>

constexpr uint16_t bypass_precision = 2; /* number of bits in bypass mode */
constexpr uint16_t max_bypass_val = (1 << bypass_precision) - 1;

inline void RansEncPutBits(RansState& r, uint8_t*& ptr, uint32_t val)
{
    RansAssert(bypass_precision <= 8);
    RansAssert(val < (1u << bypass_precision));

    constexpr uint32_t freq = 1 << (SCALE_BITS - bypass_precision);
    constexpr uint32_t x_max = freq << ENC_RENORM_SHIFT_BITS;
    while (r >= x_max) {
        *(--ptr) = static_cast<uint8_t>(r & 0xff);
        r >>= 8;
    }

    r = (r << bypass_precision) | val;
}

inline uint32_t RansDecGetBits(RansState& r, uint8_t*& ptr)
{
    uint32_t val = r & ((1u << bypass_precision) - 1);

    /* Re-normalize */
    r = r >> bypass_precision;
    if (r < RANS_BYTE_L) {
        r = (r << 8) | *ptr++;
        RansAssert(r >= RANS_BYTE_L);
    }

    return val;
}

RansEncoderLib::RansEncoderLib()
{
    _stream = std::make_shared<std::vector<uint8_t>>();
}

int RansEncoderLib::add_cdf(const std::shared_ptr<std::vector<std::vector<int32_t>>> cdfs,
                            const std::shared_ptr<std::vector<int32_t>> cdfs_sizes,
                            const std::shared_ptr<std::vector<int32_t>> offsets)
{

    auto ransSymbols = std::make_shared<std::vector<std::vector<RansSymbol>>>(cdfs->size());
    for (int i = 0; i < static_cast<int>(cdfs->size()); i++) {
        const int32_t* cdf = cdfs->at(i).data();
        std::vector<RansSymbol> ransSym(cdfs->at(i).size());
        const int ransSize = static_cast<int>(ransSym.size() - 1);
        for (int j = 0; j < ransSize; j++) {
            ransSym[j] = RansSymbol(
                { static_cast<uint16_t>(cdf[j]), static_cast<uint16_t>(cdf[j + 1] - cdf[j]) });
        }
        ransSymbols->at(i) = ransSym;
    }

    _ransSymbols.push_back(ransSymbols);
    _cdfs_sizes.push_back(cdfs_sizes);
    _offsets.push_back(offsets);
    return static_cast<int>(_ransSymbols.size()) - 1;
}

void RansEncoderLib::empty_cdf_buffer()
{
    _ransSymbols.clear();
    _cdfs_sizes.clear();
    _offsets.clear();
}

FORCE_INLINE void RansEncoderLib::encode_one_symbol(uint8_t*& ptr, RansState& rans, const int32_t symbol,
                                                    const int32_t cdf_size, const int32_t offset,
                                                    const std::vector<RansSymbol>& ransSymbols)
{
    const int32_t max_value = cdf_size - 2;
    int32_t value = symbol - offset;

    uint32_t raw_val = 0;
    if (value < 0) {
        raw_val = -2 * value - 1;
        value = max_value;
    } else if (value >= max_value) {
        raw_val = 2 * (value - max_value);
        value = max_value;
    }

    if (value == max_value) {
        std::vector<uint16_t> bypassBins;
        bypassBins.reserve(20);
        /* Determine the number of bypasses (in bypass_precision size) needed to
         * encode the raw value. */
        int32_t n_bypass = 0;
        while ((raw_val >> (n_bypass * bypass_precision)) != 0) {
            ++n_bypass;
        }

        /* Encode number of bypasses */
        int32_t val = n_bypass;
        while (val >= max_bypass_val) {
            bypassBins.push_back(max_bypass_val);
            val -= max_bypass_val;
        }
        bypassBins.push_back(static_cast<uint16_t>(val));

        /* Encode raw value */
        for (int32_t j = 0; j < n_bypass; ++j) {
            const int32_t val1 = (raw_val >> (j * bypass_precision)) & max_bypass_val;
            bypassBins.push_back(static_cast<uint16_t>(val1));
        }

        for (auto it = bypassBins.rbegin(); it < bypassBins.rend(); it++) {
            RansEncPutBits(rans, ptr, *it);
        }
    }
    RansEncPut(rans, ptr, ransSymbols[value].start, ransSymbols[value].range);
}

void RansEncoderLib::encode_y(const std::shared_ptr<std::vector<int16_t>> symbols,
                              const int cdf_group_index)
{
    PendingTask p;
    p.workType = WorkType::EncodeDecodeY;
    p.symbols_y = symbols;
    p.cdf_group_index = cdf_group_index;
    m_pendingEncodingList.push_back(p);
}

void RansEncoderLib::encode_z(const std::shared_ptr<std::vector<int8_t>> symbols,
                              const int cdf_group_index, const int start_offset,
                              const int per_channel_size)
{
    PendingTask p;
    p.workType = WorkType::EncodeDecodeZ;
    p.symbols_z = symbols;
    p.cdf_group_index = cdf_group_index;
    p.start_offset = start_offset;
    p.per_channel_size = per_channel_size;
    m_pendingEncodingList.push_back(p);
}
#include <iostream>
FORCE_INLINE void RansEncoderLib::encode_y_internal(uint8_t*& ptr, RansState& rans,
                                                    const std::shared_ptr<std::vector<int16_t>> symbols,
                                                    const int cdf_group_index)
{
    // backward loop on symbols from the end;
    const int16_t* symbols_ptr = symbols->data();
    const int32_t* cdfs_sizes_ptr = _cdfs_sizes[cdf_group_index]->data();
    const int32_t* offsets_ptr = _offsets[cdf_group_index]->data();
    const int symbol_size = static_cast<int>(symbols->size());

    for (int i = symbol_size - 1; i >= 0; i--) {
        const int32_t combined_symbol = symbols_ptr[i];
        const int32_t cdf_idx = combined_symbol & 0xff;
        const int32_t s = combined_symbol >> 8;
        encode_one_symbol(ptr, rans, s, cdfs_sizes_ptr[cdf_idx], offsets_ptr[cdf_idx],
                          _ransSymbols[cdf_group_index]->at(cdf_idx));
    }
}

FORCE_INLINE void RansEncoderLib::encode_z_internal(uint8_t*& ptr, RansState& rans,
                                                    const std::shared_ptr<std::vector<int8_t>> symbols,
                                                    const int cdf_group_index, const int start_offset,
                                                    const int per_channel_size)
{
    // backward loop on symbols from the end;
    const int8_t* symbols_ptr = symbols->data();
    const int32_t* cdfs_sizes_ptr = _cdfs_sizes[cdf_group_index]->data();
    const int32_t* offsets_ptr = _offsets[cdf_group_index]->data();
    const int symbol_size = static_cast<int>(symbols->size());

    for (int i = symbol_size - 1; i >= 0; i--) {
        const int32_t cdf_idx = i / per_channel_size + start_offset;
        encode_one_symbol(ptr, rans, symbols_ptr[i], cdfs_sizes_ptr[cdf_idx], offsets_ptr[cdf_idx],
                          _ransSymbols[cdf_group_index]->at(cdf_idx));
    }
}

void RansEncoderLib::flush()
{
    RansState rans;
    RansEncInit(rans);

    int32_t total_symbol_size = 0;
    for (auto it = m_pendingEncodingList.begin(); it != m_pendingEncodingList.end(); it++) {
        if (it->workType == WorkType::EncodeDecodeY) {
            total_symbol_size += static_cast<int32_t>(it->symbols_y->size());
        } else if (it->workType == WorkType::EncodeDecodeZ) {
            total_symbol_size += static_cast<int32_t>(it->symbols_z->size());
        }
    }

    if (total_symbol_size == 0) {
        _stream->resize(0);
        return;
    }

    uint8_t* output = new uint8_t[total_symbol_size];  // too much space ?
    uint8_t* ptrEnd = output + total_symbol_size;
    uint8_t* ptr = ptrEnd;
    assert(ptr != nullptr);

    for (auto it = m_pendingEncodingList.rbegin(); it != m_pendingEncodingList.rend(); it++) {
        PendingTask p = *it;
        if (p.workType == WorkType::EncodeDecodeY) {
            encode_y_internal(ptr, rans, p.symbols_y, p.cdf_group_index);
        } else if (p.workType == WorkType::EncodeDecodeZ) {
            encode_z_internal(ptr, rans, p.symbols_z, p.cdf_group_index, p.start_offset,
                              p.per_channel_size);
        }
    }

    RansEncFlush(rans, ptr);

    const int nbytes = static_cast<int>(std::distance(ptr, ptrEnd));

    _stream->resize(nbytes);
    memcpy(_stream->data(), ptr, nbytes);
    delete[] output;
}

std::shared_ptr<std::vector<uint8_t>> RansEncoderLib::get_encoded_stream()
{
    return _stream;
}

void RansEncoderLib::reset()
{
    m_pendingEncodingList.clear();
    _stream->clear();
}

RansEncoderLibMultiThread::RansEncoderLibMultiThread()
    : RansEncoderLib()
    , m_finish(false)
    , m_result_ready(false)
{
    m_thread = std::thread(&RansEncoderLibMultiThread::worker, this);
}
RansEncoderLibMultiThread::~RansEncoderLibMultiThread()
{
    {
        std::lock_guard<std::mutex> lk(m_mutex_pending);
        std::lock_guard<std::mutex> lk1(m_mutex_result);
        m_finish = true;
    }
    m_cv_pending.notify_one();
    m_cv_result.notify_one();
    m_thread.join();
}

void RansEncoderLibMultiThread::flush()
{
    PendingTask p;
    p.workType = WorkType::Flush;
    {
        std::unique_lock<std::mutex> lk(m_mutex_pending);
        m_pending.push_back(p);
    }
    m_cv_pending.notify_one();
}

std::shared_ptr<std::vector<uint8_t>> RansEncoderLibMultiThread::get_encoded_stream()
{
    std::unique_lock<std::mutex> lk(m_mutex_result);
    m_cv_result.wait(lk, [this] { return m_result_ready || m_finish; });
    return RansEncoderLib::get_encoded_stream();
}

void RansEncoderLibMultiThread::reset()
{
    RansEncoderLib::reset();
    std::lock_guard<std::mutex> lk(m_mutex_result);
    m_result_ready = false;
}

void RansEncoderLibMultiThread::worker()
{
    while (!m_finish) {
        std::unique_lock<std::mutex> lk(m_mutex_pending);
        m_cv_pending.wait(lk, [this] { return m_pending.size() > 0 || m_finish; });
        if (m_finish) {
            lk.unlock();
            break;
        }
        if (m_pending.size() == 0) {
            lk.unlock();
            // std::cout << "contine in worker" << std::endl;
            continue;
        }
        while (m_pending.size() > 0) {
            auto p = m_pending.front();
            m_pending.pop_front();
            lk.unlock();
            if (p.workType == WorkType::Flush) {
                RansEncoderLib::flush();
                {
                    std::lock_guard<std::mutex> lk_result(m_mutex_result);
                    m_result_ready = true;
                }
                m_cv_result.notify_one();
            }
            lk.lock();
        }
        lk.unlock();
    }
}

void RansDecoderLib::set_stream(const std::shared_ptr<std::vector<uint8_t>> encoded)
{
    _stream = encoded;
    _ptr8 = (uint8_t*)(_stream->data());
    RansDecInit(_rans, _ptr8);
}

int RansDecoderLib::add_cdf(const std::shared_ptr<std::vector<std::vector<int32_t>>> cdfs,
                            const std::shared_ptr<std::vector<int32_t>> cdfs_sizes,
                            const std::shared_ptr<std::vector<int32_t>> offsets)
{
    _cdfs.push_back(cdfs);
    _cdfs_sizes.push_back(cdfs_sizes);
    _offsets.push_back(offsets);
    return static_cast<int>(_cdfs.size()) - 1;
}

void RansDecoderLib::empty_cdf_buffer()
{
    _cdfs.clear();
    _cdfs_sizes.clear();
    _offsets.clear();
}

FORCE_INLINE int8_t RansDecoderLib::decode_one_symbol(const int32_t* cdf, const int32_t cdf_size,
                                                      const int32_t offset)
{
    const int32_t max_value = cdf_size - 2;
    const int32_t cum_freq = static_cast<int32_t>(RansDecGet(_rans));

    int s = 1;
    while (cdf[s++] <= cum_freq) {
    }
    s -= 2;

    RansDecAdvance(_rans, _ptr8, cdf[s], cdf[s + 1] - cdf[s]);

    int32_t value = static_cast<int32_t>(s);

    if (value == max_value) {
        /* Bypass decoding mode */
        int32_t val = RansDecGetBits(_rans, _ptr8);
        int32_t n_bypass = val;

        while (val == max_bypass_val) {
            val = RansDecGetBits(_rans, _ptr8);
            n_bypass += val;
        }

        int32_t raw_val = 0;
        for (int j = 0; j < n_bypass; ++j) {
            val = RansDecGetBits(_rans, _ptr8);
            raw_val |= val << (j * bypass_precision);
        }
        value = raw_val >> 1;
        if (raw_val & 1) {
            value = -value - 1;
        } else {
            value += max_value;
        }
    }

    return static_cast<int8_t>(value + offset);
}

void RansDecoderLib::decode_y(const std::shared_ptr<std::vector<uint8_t>> indexes,
                              const int cdf_group_index)
{
    int index_size = static_cast<int>(indexes->size());
    m_decoded = std::make_shared<std::vector<int8_t>>(index_size);

    int8_t* outout_ptr = m_decoded->data();
    const uint8_t* indexes_ptr = indexes->data();
    const int32_t* cdfs_sizes_ptr = _cdfs_sizes[cdf_group_index]->data();
    const int32_t* offsets_ptr = _offsets[cdf_group_index]->data();
    const auto& cdfs = _cdfs[cdf_group_index];
    for (int i = 0; i < index_size; ++i) {
        const int32_t cdf_idx = indexes_ptr[i];
        outout_ptr[i] = decode_one_symbol(cdfs->at(cdf_idx).data(), cdfs_sizes_ptr[cdf_idx],
                                          offsets_ptr[cdf_idx]);
    }
}

void RansDecoderLib::decode_z(const int total_size, const int cdf_group_index,
                              const int start_offset, const int per_channel_size)
{
    m_decoded = std::make_shared<std::vector<int8_t>>(total_size);

    int8_t* outout_ptr = m_decoded->data();
    const int32_t* cdfs_sizes_ptr = _cdfs_sizes[cdf_group_index]->data();
    const int32_t* offsets_ptr = _offsets[cdf_group_index]->data();
    const auto& cdfs = _cdfs[cdf_group_index];
    for (int i = 0; i < total_size; ++i) {
        const int32_t cdf_idx = i / per_channel_size + start_offset;
        outout_ptr[i] = decode_one_symbol(cdfs->at(cdf_idx).data(), cdfs_sizes_ptr[cdf_idx],
                                          offsets_ptr[cdf_idx]);
    }
}

std::shared_ptr<std::vector<int8_t>> RansDecoderLib::get_decoded_tensor()
{
    return m_decoded;
}

RansDecoderLibMultiThread::RansDecoderLibMultiThread()
    : RansDecoderLib()
    , m_finish(false)
    , m_result_ready(false)
{
    m_thread = std::thread(&RansDecoderLibMultiThread::worker, this);
}

RansDecoderLibMultiThread::~RansDecoderLibMultiThread()
{
    {
        std::lock_guard<std::mutex> lk(m_mutex_pending);
        std::lock_guard<std::mutex> lk1(m_mutex_result);
        m_finish = true;
    }
    m_cv_pending.notify_one();
    m_cv_result.notify_one();
    m_thread.join();
}

void RansDecoderLibMultiThread::decode_y(const std::shared_ptr<std::vector<uint8_t>> indexes,
                                         const int cdf_group_index)
{
    {
        std::lock_guard<std::mutex> lk(m_mutex_result);
        m_result_ready = false;
    }
    PendingTask p;
    p.workType = WorkType::EncodeDecodeY;
    p.indexes = indexes;
    p.cdf_group_index = cdf_group_index;
    {
        std::unique_lock<std::mutex> lk(m_mutex_pending);
        m_pending.push_back(p);
    }
    m_cv_pending.notify_one();
}

void RansDecoderLibMultiThread::decode_z(const int total_size, const int cdf_group_index,
                                         const int start_offset, const int per_channel_size)
{
    {
        std::lock_guard<std::mutex> lk(m_mutex_result);
        m_result_ready = false;
    }
    PendingTask p;
    p.workType = WorkType::EncodeDecodeZ;
    p.total_size = total_size;
    p.cdf_group_index = cdf_group_index;
    p.start_offset = start_offset;
    p.per_channel_size = per_channel_size;
    {
        std::unique_lock<std::mutex> lk(m_mutex_pending);
        m_pending.push_back(p);
    }
    m_cv_pending.notify_one();
}

std::shared_ptr<std::vector<int8_t>> RansDecoderLibMultiThread::get_decoded_tensor()
{
    std::unique_lock<std::mutex> lk(m_mutex_result);
    m_cv_result.wait(lk, [this] { return m_result_ready || m_finish; });
    return RansDecoderLib::get_decoded_tensor();
}

void RansDecoderLibMultiThread::worker()
{
    while (!m_finish) {
        std::unique_lock<std::mutex> lk(m_mutex_pending);
        m_cv_pending.wait(lk, [this] { return m_pending.size() > 0 || m_finish; });
        if (m_finish) {
            lk.unlock();
            break;
        }
        if (m_pending.size() == 0) {
            lk.unlock();
            // std::cout << "contine in worker" << std::endl;
            continue;
        }
        while (m_pending.size() > 0) {
            auto p = m_pending.front();
            m_pending.pop_front();
            lk.unlock();
            if (p.workType == WorkType::EncodeDecodeY) {
                RansDecoderLib::decode_y(p.indexes, p.cdf_group_index);
            } else if (p.workType == WorkType::EncodeDecodeZ) {
                RansDecoderLib::decode_z(p.total_size, p.cdf_group_index, p.start_offset,
                                         p.per_channel_size);
            }
            {
                std::lock_guard<std::mutex> lk_result(m_mutex_result);
                m_result_ready = true;
            }
            m_cv_result.notify_one();
            lk.lock();
        }
        lk.unlock();
    }
}