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