1 /* 2 * Copyright (C) 2011 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 <decode_buffer.h> 18 19 namespace { 20 21 static const size_t kNumberOfBytesPerSample = 2; 22 23 } // namespace 24 25 DecodeBuffer::DecodeBuffer(size_t sizeOfOneBuffer, size_t maxSize) 26 : sizeOfOneBuffer_(sizeOfOneBuffer), maxSize_(maxSize), 27 start_(0), end_(0), advancedCount_(0), data_() { 28 Clear(); 29 } 30 31 DecodeBuffer::~DecodeBuffer() { 32 Clear(); 33 } 34 35 size_t DecodeBuffer::GetSizeInBytes() const { 36 return kNumberOfBytesPerSample * (end_ - start_); 37 } 38 39 bool DecodeBuffer::IsTooLarge() const { 40 return GetSizeInBytes() > maxSize_; 41 } 42 43 void DecodeBuffer::AddData(int8_t* pointer, size_t lengthInBytes) { 44 for (size_t i = 0; i < lengthInBytes / kNumberOfBytesPerSample; ++i) { 45 PushValue(reinterpret_cast<int16*>(pointer)[i]); 46 } 47 } 48 49 void DecodeBuffer::Clear() { 50 while (data_.size() > 0) { 51 delete[] data_.front(); 52 data_.erase(data_.begin()); 53 } 54 start_ = 0; 55 end_ = 0; 56 advancedCount_ = 0; 57 } 58 59 size_t DecodeBuffer::GetTotalAdvancedCount() const { 60 return advancedCount_; 61 } 62 63 void DecodeBuffer::AdvanceHeadPointerShorts(size_t numberOfShorts) { 64 start_ += numberOfShorts; 65 while (start_ > sizeOfOneBuffer_) { 66 data_.push_back(data_.front()); 67 data_.erase(data_.begin()); 68 start_ -= sizeOfOneBuffer_; 69 end_ -= sizeOfOneBuffer_; 70 } 71 advancedCount_ += numberOfShorts; 72 } 73 74 void DecodeBuffer::PushValue(int16 value) { 75 size_t bufferIndex = end_ / sizeOfOneBuffer_; 76 if (bufferIndex >= data_.size()) { 77 data_.push_back(new int16[sizeOfOneBuffer_]); 78 } 79 data_.at(bufferIndex)[end_ % sizeOfOneBuffer_] = value; 80 ++end_; 81 } 82 83 int16 DecodeBuffer::GetAtIndex(size_t index) { 84 return data_.at((start_ + index) / sizeOfOneBuffer_) 85 [(start_ + index) % sizeOfOneBuffer_]; 86 } 87