| 44 | : byteVector(inArr), posInBufferBytes(inArrOffset), initialPosInBuffer(inArrOffset){}; |
| 45 | |
| 46 | std::tuple<uint64_t, std::string> SubByteReader::readBits(size_t nrBits) |
| 47 | { |
| 48 | uint64_t out = 0; |
| 49 | auto nrBitsRead = nrBits; |
| 50 | |
| 51 | // The return unsigned int is of depth 64 bits |
| 52 | if (nrBits > 64) |
| 53 | throw std::logic_error("Trying to read more than 64 bits at once from the bitstream."); |
| 54 | if (nrBits == 0) |
| 55 | return {0, ""}; |
| 56 | |
| 57 | while (nrBits > 0) |
| 58 | { |
| 59 | if (this->posInBufferBits == 8 && nrBits != 0) |
| 60 | { |
| 61 | // We read all bits we could from the current byte but we need more. Go to |
| 62 | // the next byte. |
| 63 | if (!this->gotoNextByte()) |
| 64 | // We are at the end of the buffer but we need to read more. Error. |
| 65 | throw std::logic_error("Error while reading annexB file. Trying to " |
| 66 | "read over buffer boundary."); |
| 67 | } |
| 68 | |
| 69 | // How many bits can be gotton from the current byte? |
| 70 | auto curBitsLeft = 8 - this->posInBufferBits; |
| 71 | |
| 72 | size_t readBits; // Nr of bits to read |
| 73 | size_t offset; // Offset for reading from the right |
| 74 | if (nrBits >= curBitsLeft) |
| 75 | { |
| 76 | // Read "curBitsLeft" bits |
| 77 | readBits = curBitsLeft; |
| 78 | offset = 0; |
| 79 | } |
| 80 | else |
| 81 | { |
| 82 | // Read "nrBits" bits |
| 83 | assert(nrBits < 8 && nrBits < curBitsLeft); |
| 84 | readBits = nrBits; |
| 85 | offset = curBitsLeft - nrBits; |
| 86 | } |
| 87 | |
| 88 | // Shift output value so that the new bits fit |
| 89 | out = out << readBits; |
| 90 | |
| 91 | char c = this->byteVector[this->posInBufferBytes]; |
| 92 | c = c >> offset; |
| 93 | int mask = ((1 << readBits) - 1); |
| 94 | |
| 95 | // Write bits to output |
| 96 | out += (c & mask); |
| 97 | |
| 98 | // Update counters |
| 99 | nrBits -= readBits; |
| 100 | this->posInBufferBits += readBits; |
| 101 | } |
| 102 | |
| 103 | std::string bitsRead; |
no test coverage detected