Branch data Line data Source code
1 : : // Copyright (c) 2026 M. A. Chatterjee
2 : : // SPDX-License-Identifier: BSD-2-Clause
3 : :
4 : : #include "triepack/bitstream.hpp"
5 : :
6 : : extern "C" {
7 : : #include "triepack/triepack_bitstream.h"
8 : : }
9 : :
10 : : namespace triepack
11 : : {
12 : :
13 : : // ---------------------------------------------------------------------------
14 : : // BitstreamReader
15 : : // ---------------------------------------------------------------------------
16 : :
17 : 8 : BitstreamReader::BitstreamReader(const uint8_t *data, size_t size) : handle_(nullptr)
18 : : {
19 : 8 : tp_bs_reader_create(&handle_, data, (uint64_t)size * 8);
20 : 8 : }
21 : :
22 : 9 : BitstreamReader::~BitstreamReader()
23 : : {
24 : 9 : tp_bs_reader_destroy(&handle_);
25 : 9 : }
26 : :
27 : 1 : BitstreamReader::BitstreamReader(BitstreamReader &&other) noexcept : handle_(nullptr)
28 : : {
29 : 1 : auto *tmp = other.handle_;
30 : 1 : other.handle_ = nullptr;
31 : 1 : handle_ = tmp;
32 : 1 : }
33 : :
34 : 2 : BitstreamReader &BitstreamReader::operator=(BitstreamReader &&other) noexcept
35 : : {
36 [ + + ]: 2 : if (this != &other) {
37 : 1 : tp_bs_reader_destroy(&handle_);
38 : 1 : auto *tmp = other.handle_;
39 : 1 : other.handle_ = nullptr;
40 : 1 : handle_ = tmp;
41 : : }
42 : 2 : return *this;
43 : : }
44 : :
45 : 10 : uint32_t BitstreamReader::read(unsigned bits)
46 : : {
47 : 10 : uint32_t val = 0;
48 [ + - ]: 10 : tp_bs_read_bits32(handle_, (uint8_t)bits, &val);
49 : 10 : return val;
50 : : }
51 : :
52 : 1 : size_t BitstreamReader::position() const
53 : : {
54 : 1 : return (size_t)tp_bs_reader_position(handle_);
55 : : }
56 : :
57 : 7 : tp_bitstream_reader *BitstreamReader::handle() const
58 : : {
59 : 7 : return handle_;
60 : : }
61 : :
62 : : // ---------------------------------------------------------------------------
63 : : // BitstreamWriter
64 : : // ---------------------------------------------------------------------------
65 : :
66 : 9 : BitstreamWriter::BitstreamWriter(size_t initial_capacity) : handle_(nullptr)
67 : : {
68 : 9 : tp_bs_writer_create(&handle_, initial_capacity, 0);
69 : 9 : }
70 : :
71 : 10 : BitstreamWriter::~BitstreamWriter()
72 : : {
73 : 10 : tp_bs_writer_destroy(&handle_);
74 : 10 : }
75 : :
76 : 1 : BitstreamWriter::BitstreamWriter(BitstreamWriter &&other) noexcept : handle_(nullptr)
77 : : {
78 : 1 : auto *tmp = other.handle_;
79 : 1 : other.handle_ = nullptr;
80 : 1 : handle_ = tmp;
81 : 1 : }
82 : :
83 : 2 : BitstreamWriter &BitstreamWriter::operator=(BitstreamWriter &&other) noexcept
84 : : {
85 [ + + ]: 2 : if (this != &other) {
86 : 1 : tp_bs_writer_destroy(&handle_);
87 : 1 : auto *tmp = other.handle_;
88 : 1 : other.handle_ = nullptr;
89 : 1 : handle_ = tmp;
90 : : }
91 : 2 : return *this;
92 : : }
93 : :
94 : 12 : void BitstreamWriter::write(uint32_t value, unsigned bits)
95 : : {
96 : 12 : tp_bs_write_bits(handle_, value, (uint8_t)bits);
97 : 12 : }
98 : :
99 : 5 : size_t BitstreamWriter::position() const
100 : : {
101 : 5 : return (size_t)tp_bs_writer_position(handle_);
102 : : }
103 : :
104 : 4 : const uint8_t *BitstreamWriter::data() const
105 : : {
106 : 4 : const uint8_t *buf = nullptr;
107 : 4 : uint64_t bit_len = 0;
108 [ + - ]: 4 : tp_bs_writer_get_buffer(handle_, &buf, &bit_len);
109 : 4 : return buf;
110 : : }
111 : :
112 : 4 : size_t BitstreamWriter::size() const
113 : : {
114 : 4 : const uint8_t *buf = nullptr;
115 : 4 : uint64_t bit_len = 0;
116 [ + - ]: 4 : tp_bs_writer_get_buffer(handle_, &buf, &bit_len);
117 : 4 : return (size_t)((bit_len + 7) / 8);
118 : : }
119 : :
120 : 7 : tp_bitstream_writer *BitstreamWriter::handle() const
121 : : {
122 : 7 : return handle_;
123 : : }
124 : :
125 : : } // namespace triepack
|