aboutsummaryrefslogtreecommitdiffstats
path: root/src/lib/bitstream/bitstream.cpp
diff options
context:
space:
mode:
authorDaniil Cherednik <dan.cherednik@gmail.com>2024-06-17 20:12:45 +0000
committerDaniil Cherednik <dan.cherednik@gmail.com>2024-06-17 22:21:52 +0200
commit23a4e5f1dd7ce24f65a2af0598d1f92af4b5c424 (patch)
tree8a259ca8363c5b15fd3605b760518cb37e6ac63c /src/lib/bitstream/bitstream.cpp
parent73dbd1609445a0142e1e138b6b44ec6d1925cbb8 (diff)
downloadatracdenc-23a4e5f1dd7ce24f65a2af0598d1f92af4b5c424.tar.gz
[refactoring] move some libraries in to library directory
Diffstat (limited to 'src/lib/bitstream/bitstream.cpp')
-rw-r--r--src/lib/bitstream/bitstream.cpp88
1 files changed, 88 insertions, 0 deletions
diff --git a/src/lib/bitstream/bitstream.cpp b/src/lib/bitstream/bitstream.cpp
new file mode 100644
index 0000000..ddff907
--- /dev/null
+++ b/src/lib/bitstream/bitstream.cpp
@@ -0,0 +1,88 @@
+/*
+ * This file is part of AtracDEnc.
+ *
+ * AtracDEnc is free software; you can redistribute it and/or
+ * modify it under the terms of the GNU Lesser General Public
+ * License as published by the Free Software Foundation; either
+ * version 2.1 of the License, or (at your option) any later version.
+ *
+ * AtracDEnc is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
+ * Lesser General Public License for more details.
+ *
+ * You should have received a copy of the GNU Lesser General Public
+ * License along with AtracDEnc; if not, write to the Free Software
+ * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
+ */
+#include <cstdint>
+#include "bitstream.h"
+
+#ifndef BIGENDIAN_ORDER
+#define NBITSTREAM__LITTLE_ENDIAN_CPU
+#endif
+
+namespace NBitStream {
+
+union UBytes {
+ uint32_t ui = 0;
+ uint8_t bytes[4];
+};
+
+TBitStream::TBitStream(const char* buf, int size)
+ : Buf(buf, buf+size)
+{}
+
+TBitStream::TBitStream()
+{}
+
+void TBitStream::Write(uint32_t val, int n) {
+ if (n > 23 || n < 0)
+ abort();
+ const int bitsLeft = Buf.size() * 8 - BitsUsed;
+ const int bitsReq = n - bitsLeft;
+ const int bytesPos = BitsUsed / 8;
+ const int overlap = BitsUsed % 8;
+
+ if (overlap || bitsReq >= 0) {
+ Buf.resize(Buf.size() + (bitsReq / 8 + (overlap ? 2 : 1 )), 0);
+ }
+ UBytes t;
+ t.ui = (val << (32 - n) >> overlap);
+
+ for (int i = 0; i < n/8 + (overlap ? 2 : 1); ++i) {
+#ifdef NBITSTREAM__LITTLE_ENDIAN_CPU
+ Buf[bytesPos+i] |= t.bytes[3-i];
+#else
+ Buf[bytesPos + i] |= t.bytes[i];
+#endif
+ }
+
+ BitsUsed += n;
+}
+
+uint32_t TBitStream::Read(int n) {
+ if (n >23 || n < 0)
+ abort();
+ const int bytesPos = ReadPos / 8;
+ const int overlap = ReadPos % 8;
+
+ UBytes t;
+ for (int i = 0; i < n/8 + (overlap ? 2 : 1); ++i) {
+#ifdef NBITSTREAM__LITTLE_ENDIAN_CPU
+ t.bytes[3-i] = (uint8_t)Buf[bytesPos+i];
+#else
+ t.bytes[i] = (uint8_t)Buf[bytesPos+i];
+#endif
+ }
+
+ t.ui = (t.ui << overlap >> (32 - n));
+ ReadPos += n;
+ return t.ui;
+}
+
+unsigned long long TBitStream::GetSizeInBits() const {
+ return BitsUsed;
+}
+
+}