Bug 1852754: part 9) Add tests for dynamically loading <link rel="prefetch"> elements...
[gecko.git] / dom / media / BitWriter.cpp
bloba52e49c106a3f157fbb13bd01b6051aaa838e871
1 /* This Source Code Form is subject to the terms of the Mozilla Public
2 * License, v. 2.0. If a copy of the MPL was not distributed with this
3 * file, You can obtain one at http://mozilla.org/MPL/2.0/. */
5 #include "BitWriter.h"
6 #include "MediaData.h"
7 #include "mozilla/MathAlgorithms.h"
9 namespace mozilla {
11 constexpr uint8_t golombLen[256] = {
12 1, 3, 3, 5, 5, 5, 5, 7, 7, 7, 7, 7, 7, 7, 7, 9, 9, 9, 9,
13 9, 9, 9, 9, 9, 9, 9, 9, 9, 9, 9, 9, 11, 11, 11, 11, 11, 11, 11,
14 11, 11, 11, 11, 11, 11, 11, 11, 11, 11, 11, 11, 11, 11, 11, 11, 11, 11, 11,
15 11, 11, 11, 11, 11, 11, 13, 13, 13, 13, 13, 13, 13, 13, 13, 13, 13, 13, 13,
16 13, 13, 13, 13, 13, 13, 13, 13, 13, 13, 13, 13, 13, 13, 13, 13, 13, 13, 13,
17 13, 13, 13, 13, 13, 13, 13, 13, 13, 13, 13, 13, 13, 13, 13, 13, 13, 13, 13,
18 13, 13, 13, 13, 13, 13, 13, 13, 13, 13, 13, 13, 13, 15, 15, 15, 15, 15, 15,
19 15, 15, 15, 15, 15, 15, 15, 15, 15, 15, 15, 15, 15, 15, 15, 15, 15, 15, 15,
20 15, 15, 15, 15, 15, 15, 15, 15, 15, 15, 15, 15, 15, 15, 15, 15, 15, 15, 15,
21 15, 15, 15, 15, 15, 15, 15, 15, 15, 15, 15, 15, 15, 15, 15, 15, 15, 15, 15,
22 15, 15, 15, 15, 15, 15, 15, 15, 15, 15, 15, 15, 15, 15, 15, 15, 15, 15, 15,
23 15, 15, 15, 15, 15, 15, 15, 15, 15, 15, 15, 15, 15, 15, 15, 15, 15, 15, 15,
24 15, 15, 15, 15, 15, 15, 15, 15, 15, 15, 15, 15, 15, 15, 15, 15, 15, 15, 15,
25 15, 15, 15, 15, 15, 15, 15, 15, 17,
28 BitWriter::BitWriter(MediaByteBuffer* aBuffer) : mBuffer(aBuffer) {}
30 BitWriter::~BitWriter() = default;
32 void BitWriter::WriteBits(uint64_t aValue, size_t aBits) {
33 MOZ_ASSERT(aBits <= sizeof(uint64_t) * 8);
35 while (aBits) {
36 if (mBitIndex == 0) {
37 mBuffer->AppendElement(0);
40 const uint8_t clearMask = ~(~0u << (8 - mBitIndex));
41 uint8_t mask = 0;
43 if (mBitIndex + aBits > 8) {
44 // Not enough bits in the current byte to write all the bits
45 // required, we'll process what we can and continue with the left over.
46 const uint8_t leftOverBits = mBitIndex + aBits - 8;
47 const uint64_t leftOver = aValue & (~uint64_t(0) >> (8 - mBitIndex));
48 mask = aValue >> leftOverBits;
50 mBitIndex = 8;
51 aValue = leftOver;
52 aBits = leftOverBits;
53 } else {
54 const uint8_t offset = 8 - mBitIndex - aBits;
55 mask = aValue << offset;
57 mBitIndex += aBits;
58 aBits = 0;
61 mBuffer->ElementAt(mPosition) |= mask & clearMask;
63 if (mBitIndex == 8) {
64 mPosition++;
65 mBitIndex = 0;
70 void BitWriter::WriteUE(uint32_t aValue) {
71 MOZ_ASSERT(aValue <= (UINT32_MAX - 1));
73 if (aValue < 256) {
74 WriteBits(aValue + 1, golombLen[aValue]);
75 } else {
76 const uint32_t e = FloorLog2(aValue + 1);
77 WriteBits(aValue + 1, e * 2 + 1);
81 void BitWriter::WriteULEB128(uint64_t aValue) {
82 // See https://en.wikipedia.org/wiki/LEB128#Encode_unsigned_integer
83 do {
84 uint8_t byte = aValue & 0x7F;
85 aValue >>= 7;
86 WriteBit(aValue != 0);
87 WriteBits(byte, 7);
88 } while (aValue != 0);
91 void BitWriter::CloseWithRbspTrailing() {
92 WriteBit(true);
93 WriteBits(0, (8 - mBitIndex) & 7);
96 } // namespace mozilla