1 // Copyright 2016 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_BOOL_DECODER_H_ 7 #define VP9_BOOL_DECODER_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 class Vp9BoolDecoder { 21 public: 22 Vp9BoolDecoder(); 23 ~Vp9BoolDecoder(); 24 25 // |data| is the input buffer with |size| bytes. 26 // Returns true if read first marker bit successfully. 27 bool Initialize(const uint8_t* data, size_t size); 28 29 // Returns true if none of the reads since the last Initialize() call has 30 // gone beyond the end of available data. IsValid()31 bool IsValid() const { return valid_; } 32 33 // Reads one bit. B(p). 34 // If the read goes beyond the end of buffer, the return value is undefined. 35 bool ReadBool(int prob); 36 37 // Reads a literal. L(n). 38 // If the read goes beyond the end of buffer, the return value is undefined. 39 uint8_t ReadLiteral(int bits); 40 41 // Consumes padding bits up to end of data. Returns true if no 42 // padding bits or they are all zero. 43 bool ConsumePaddingBits(); 44 45 private: 46 // The highest 8 bits of BigBool is actual "bool value". The remain bits 47 // are optimization of prefill buffer. 48 using BigBool = size_t; 49 // The size of "bool value" used for boolean decoding defined in spec. 50 const int kBoolSize = 8; 51 const int kBigBoolBitSize = sizeof(BigBool) * 8; 52 53 bool Fill(); 54 55 std::unique_ptr<BitReader> reader_; 56 57 // Indicates if none of the reads since the last Initialize() call has gone 58 // beyond the end of available data. 59 bool valid_ = true; 60 61 BigBool bool_value_ = 0; 62 63 // Need to fill at least |count_to_fill_| bits. Negative value means extra 64 // bits pre-filled. 65 int count_to_fill_ = 0; 66 unsigned int bool_range_ = 0; 67 68 DISALLOW_COPY_AND_ASSIGN(Vp9BoolDecoder); 69 }; 70 71 } // namespace media 72 73 #endif // VP9_BOOL_DECODER_H_ 74