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/. */
4 
5 #ifndef BIT_WRITER_H_
6 #define BIT_WRITER_H_
7 
8 #include "mozilla/RefPtr.h"
9 
10 namespace mozilla {
11 
12 class MediaByteBuffer;
13 
14 class BitWriter {
15  public:
16   explicit BitWriter(MediaByteBuffer* aBuffer);
17   virtual ~BitWriter();
18   void WriteBits(uint64_t aValue, size_t aBits);
WriteBit(bool aValue)19   void WriteBit(bool aValue) { WriteBits(aValue, 1); }
WriteU8(uint8_t aValue)20   void WriteU8(uint8_t aValue) { WriteBits(aValue, 8); }
WriteU32(uint32_t aValue)21   void WriteU32(uint32_t aValue) { WriteBits(aValue, 32); }
WriteU64(uint64_t aValue)22   void WriteU64(uint64_t aValue) { WriteBits(aValue, 64); }
23 
24   // Write unsigned integer into Exp-Golomb-coded. 2^32-2 at most
25   void WriteUE(uint32_t aValue);
26 
27   // Write RBSP trailing bits.
28   void CloseWithRbspTrailing();
29 
30   // Return the number of bits written so far;
BitCount()31   size_t BitCount() const { return mPosition * 8 + mBitIndex; }
32 
33  private:
34   RefPtr<MediaByteBuffer> mBuffer;
35   size_t mPosition = 0;
36   uint8_t mBitIndex = 0;
37 };
38 
39 }  // namespace mozilla
40 
41 #endif  // BIT_WRITER_H_
42