1 // Copyright 2015 The Chromium Authors. All rights reserved.
2 // Use of this source code is governed by a BSD-style license that can be
3 // found in the LICENSE file.
4 // Note: ported from Chromium commit head: e5a9a62
5 
6 #ifndef VP9_RAW_BITS_READER_H_
7 #define VP9_RAW_BITS_READER_H_
8 
9 #include <stddef.h>
10 #include <stdint.h>
11 
12 #include <memory>
13 
14 #include "base/macros.h"
15 
16 namespace media {
17 
18 class BitReader;
19 
20 // A class to read raw bits stream. See VP9 spec, "RAW-BITS DECODING" section
21 // for detail.
22 class Vp9RawBitsReader {
23  public:
24   Vp9RawBitsReader();
25   ~Vp9RawBitsReader();
26 
27   // |data| is the input buffer with |size| bytes.
28   void Initialize(const uint8_t* data, size_t size);
29 
30   // Returns true if none of the reads since the last Initialize() call has
31   // gone beyond the end of available data.
IsValid()32   bool IsValid() const { return valid_; }
33 
34   // Returns how many bytes were read since the last Initialize() call.
35   // Partial bytes will be counted as one byte. For example, it will return 1
36   // if 3 bits were read.
37   size_t GetBytesRead() const;
38 
39   // Reads one bit.
40   // If the read goes beyond the end of buffer, the return value is undefined.
41   bool ReadBool();
42 
43   // Reads a literal with |bits| bits.
44   // If the read goes beyond the end of buffer, the return value is undefined.
45   int ReadLiteral(int bits);
46 
47   // Reads a signed literal with |bits| bits (not including the sign bit).
48   // If the read goes beyond the end of buffer, the return value is undefined.
49   int ReadSignedLiteral(int bits);
50 
51   // Consumes trailing bits up to next byte boundary. Returns true if no
52   // trailing bits or they are all zero.
53   bool ConsumeTrailingBits();
54 
55  private:
56   std::unique_ptr<BitReader> reader_;
57 
58   // Indicates if none of the reads since the last Initialize() call has gone
59   // beyond the end of available data.
60   bool valid_;
61 
62   DISALLOW_COPY_AND_ASSIGN(Vp9RawBitsReader);
63 };
64 
65 }  // namespace media
66 
67 #endif  // VP9_RAW_BITS_READER_H_
68