vp8_bool_decoder.h 5.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134
  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. //
  5. /*
  6. * Copyright (c) 2010, The WebM Project authors. All rights reserved.
  7. *
  8. * Redistribution and use in source and binary forms, with or without
  9. * modification, are permitted provided that the following conditions are
  10. * met:
  11. *
  12. * * Redistributions of source code must retain the above copyright
  13. * notice, this list of conditions and the following disclaimer.
  14. *
  15. * * Redistributions in binary form must reproduce the above copyright
  16. * notice, this list of conditions and the following disclaimer in
  17. * the documentation and/or other materials provided with the
  18. * distribution.
  19. *
  20. * * Neither the name of Google, nor the WebM Project, nor the names
  21. * of its contributors may be used to endorse or promote products
  22. * derived from this software without specific prior written
  23. * permission.
  24. *
  25. * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
  26. * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
  27. * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
  28. * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
  29. * HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
  30. * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
  31. * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
  32. * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
  33. * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
  34. * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
  35. * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  36. */
  37. // This file is modified from the dboolhuff.{c,h} from the WebM's libvpx
  38. // project. (http://www.webmproject.org/code)
  39. // It is used to decode bits from a vp8 stream.
  40. #ifndef MEDIA_PARSERS_VP8_BOOL_DECODER_H_
  41. #define MEDIA_PARSERS_VP8_BOOL_DECODER_H_
  42. #include <stddef.h>
  43. #include <stdint.h>
  44. #include <sys/types.h>
  45. #include "media/parsers/media_parsers_export.h"
  46. namespace media {
  47. // A class to decode the VP8's boolean entropy coded stream. It's a variant of
  48. // arithmetic coding. See RFC 6386 - Chapter 7. Boolean Entropy Decoder.
  49. class MEDIA_PARSERS_EXPORT Vp8BoolDecoder {
  50. public:
  51. Vp8BoolDecoder();
  52. Vp8BoolDecoder(const Vp8BoolDecoder&) = delete;
  53. Vp8BoolDecoder& operator=(const Vp8BoolDecoder&) = delete;
  54. // Initializes the decoder to start decoding |data|, |size| being size
  55. // of |data| in bytes. Returns false if |data| is NULL or empty.
  56. bool Initialize(const uint8_t* data, size_t size);
  57. // Reads a boolean from the coded stream. Returns false if it has reached the
  58. // end of |data| and failed to read the boolean. The probability of |out| to
  59. // be true is |probability| / 256, e.g., when |probability| is 0x80, the
  60. // chance is 1/2 (i.e., 0x80 / 256).
  61. bool ReadBool(bool* out, uint8_t probability);
  62. // Reads a boolean from the coded stream with the default probability 1/2.
  63. // Returns false if it has reached the end of |data| and failed to read the
  64. // boolean.
  65. bool ReadBool(bool* out);
  66. // Reads a "literal", that is, a "num_bits"-wide unsigned value whose bits
  67. // come high- to low-order, with each bit encoded at probability 1/2.
  68. // Returns false if it has reached the end of |data| and failed to read the
  69. // literal.
  70. bool ReadLiteral(size_t num_bits, int* out);
  71. // Reads a literal with sign from the coded stream. This is similar to
  72. // the ReadListeral(), it first read a "num_bits"-wide unsigned value, and
  73. // then read an extra bit as the sign of the literal. Returns false if it has
  74. // reached the end of |data| and failed to read the literal or the sign.
  75. // This is different from the "read_signed_literal(d, n)" defined in RFC 6386.
  76. bool ReadLiteralWithSign(size_t num_bits, int* out);
  77. // The following methods are used to get the internal states of the decoder.
  78. // Returns the bit offset to the current top bit of the coded stream. It is
  79. // also the number of bits that have been written in the corresponding
  80. // encoding state. More specifically, we have the following constraint:
  81. // w + (bottom * S) <= v < w + (bottom + range) * S,
  82. // where "w" is for the bits already written,
  83. // "v" is for the possible values of the coded number.
  84. // "S" is the scale for the current bit position,
  85. // i.e., S = pow(2, -(n + 8)), where "n" is the bit number of "w".
  86. // BitOffset() returns the bit count of "w", i.e., "n".
  87. size_t BitOffset();
  88. // Gets the "bottom" of the current coded value. See BitOffset() for
  89. // more details.
  90. uint8_t GetBottom();
  91. // Gets the "range" of the current coded value. See BitOffset() for
  92. // more details.
  93. uint8_t GetRange();
  94. private:
  95. // Reads the next bit from the coded stream. The probability of the bit to
  96. // be one is |probability| / 256.
  97. int ReadBit(int probability);
  98. // Fills more bits from |user_buffer_| to |value_|. We shall keep at least 8
  99. // bits of the current |user_buffer_| in |value_|.
  100. void FillDecoder();
  101. // Returns true iff we have ran out of bits.
  102. bool OutOfBuffer();
  103. const uint8_t* user_buffer_;
  104. const uint8_t* user_buffer_start_;
  105. const uint8_t* user_buffer_end_;
  106. size_t value_;
  107. int count_;
  108. size_t range_;
  109. };
  110. } // namespace media
  111. #endif // MEDIA_PARSERS_VP8_BOOL_DECODER_H_