vp9_bool_decoder.h 2.0 KB

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