aboutsummaryrefslogtreecommitdiffstats
path: root/src/bitstream/bitstream.cpp
diff options
context:
space:
mode:
authorDaniil Cherednik <dan.cherednik@gmail.com>2015-10-31 02:14:04 +0300
committerDaniil Cherednik <dan.cherednik@gmail.com>2015-10-31 02:14:04 +0300
commit592b4bd68f3eb9d1cdbcda74feaffc7b1d5f0485 (patch)
treea7e1b93335631a8ff969f6d395e08ec24281fe62 /src/bitstream/bitstream.cpp
parent4bfe046e9620644dda130aaf3f9d9078251ffd59 (diff)
downloadatracdenc-592b4bd68f3eb9d1cdbcda74feaffc7b1d5f0485.tar.gz
experimental first implementation of ATRAC encoder
current limitations: - only long window - naive MDCT (O(n^2)) - bad table of fixed bit allocation - bad usage of CBR - time accuracy is not guaranteed - dirty, not optimized code
Diffstat (limited to 'src/bitstream/bitstream.cpp')
-rw-r--r--src/bitstream/bitstream.cpp55
1 files changed, 55 insertions, 0 deletions
diff --git a/src/bitstream/bitstream.cpp b/src/bitstream/bitstream.cpp
new file mode 100644
index 0000000..7de4873
--- /dev/null
+++ b/src/bitstream/bitstream.cpp
@@ -0,0 +1,55 @@
+#include "bitstream.h"
+namespace NBitStream {
+
+union TMix {
+ unsigned long long ull = 0;
+ uint8_t bytes[8];
+};
+
+TBitStream::TBitStream(const char* buf, int size)
+ : Buf(buf, buf+size)
+{}
+TBitStream::TBitStream()
+{}
+
+void TBitStream::Write(unsigned long long 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 (bitsReq > 0) {
+ Buf.resize(Buf.size() + (bitsReq / 8 + (overlap ? 2 : 1 )), 0);
+ }
+ TMix t;
+ t.ull = val;
+ t.ull = (t.ull << (64 - n) >> overlap);
+
+ for (int i = 0; i < n/8 + (overlap ? 2 : 1); ++i) {
+ Buf[bytesPos+i] |= t.bytes[7-i];
+ }
+
+ BitsUsed += n;
+}
+unsigned long long TBitStream::Read(int n) {
+ if (n >23 || n < 0)
+ abort();
+ const int bytesPos = ReadPos / 8;
+ const int overlap = ReadPos % 8;
+ TMix t;
+ for (int i = 0; i < n/8 + (overlap ? 2 : 1); ++i) {
+ t.bytes[7-i] = (uint8_t)Buf[bytesPos+i];
+ }
+
+ t.ull = (t.ull << overlap >> (64 - n));
+ ReadPos += n;
+ return t.ull;
+}
+
+unsigned long long TBitStream::GetSizeInBits() const {
+ return BitsUsed;
+}
+
+}