Home | History | Annotate | Download | only in foundation
      1 /*
      2  * Copyright (C) 2010 The Android Open Source Project
      3  *
      4  * Licensed under the Apache License, Version 2.0 (the "License");
      5  * you may not use this file except in compliance with the License.
      6  * You may obtain a copy of the License at
      7  *
      8  *      http://www.apache.org/licenses/LICENSE-2.0
      9  *
     10  * Unless required by applicable law or agreed to in writing, software
     11  * distributed under the License is distributed on an "AS IS" BASIS,
     12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
     13  * See the License for the specific language governing permissions and
     14  * limitations under the License.
     15  */
     16 
     17 #include "ABitReader.h"
     18 
     19 #include <media/stagefright/foundation/ADebug.h>
     20 
     21 namespace android {
     22 
     23 ABitReader::ABitReader(const uint8_t *data, size_t size)
     24     : mData(data),
     25       mSize(size),
     26       mReservoir(0),
     27       mNumBitsLeft(0) {
     28 }
     29 
     30 void ABitReader::fillReservoir() {
     31     CHECK_GT(mSize, 0u);
     32 
     33     mReservoir = 0;
     34     size_t i;
     35     for (i = 0; mSize > 0 && i < 4; ++i) {
     36         mReservoir = (mReservoir << 8) | *mData;
     37 
     38         ++mData;
     39         --mSize;
     40     }
     41 
     42     mNumBitsLeft = 8 * i;
     43     mReservoir <<= 32 - mNumBitsLeft;
     44 }
     45 
     46 uint32_t ABitReader::getBits(size_t n) {
     47     CHECK_LE(n, 32u);
     48 
     49     uint32_t result = 0;
     50     while (n > 0) {
     51         if (mNumBitsLeft == 0) {
     52             fillReservoir();
     53         }
     54 
     55         size_t m = n;
     56         if (m > mNumBitsLeft) {
     57             m = mNumBitsLeft;
     58         }
     59 
     60         result = (result << m) | (mReservoir >> (32 - m));
     61         mReservoir <<= m;
     62         mNumBitsLeft -= m;
     63 
     64         n -= m;
     65     }
     66 
     67     return result;
     68 }
     69 
     70 void ABitReader::skipBits(size_t n) {
     71     while (n > 32) {
     72         getBits(32);
     73         n -= 32;
     74     }
     75 
     76     if (n > 0) {
     77         getBits(n);
     78     }
     79 }
     80 
     81 void ABitReader::putBits(uint32_t x, size_t n) {
     82     CHECK_LE(mNumBitsLeft + n, 32u);
     83 
     84     mReservoir = (mReservoir >> n) | (x << (32 - n));
     85     mNumBitsLeft += n;
     86 }
     87 
     88 size_t ABitReader::numBitsLeft() const {
     89     return mSize * 8 + mNumBitsLeft;
     90 }
     91 
     92 const uint8_t *ABitReader::data() const {
     93     CHECK_EQ(mNumBitsLeft % 8, 0u);
     94 
     95     return mData - mNumBitsLeft / 8;
     96 }
     97 
     98 }  // namespace android
     99