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