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