Respond with QuotaExceededError when IndexedDB has no disk space on open.
[chromium-blink-merge.git] / content / common / gpu / media / h264_bit_reader.cc
blob64e12bee203ecfdcd34322faabbde9362d31c5f2
1 // Copyright (c) 2012 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.
5 #include "content/common/gpu/media/h264_bit_reader.h"
6 #include "base/logging.h"
8 namespace content {
10 H264BitReader::H264BitReader()
11 : data_(NULL), bytes_left_(0), curr_byte_(0),
12 num_remaining_bits_in_curr_byte_(0), prev_two_bytes_(0) {
15 H264BitReader::~H264BitReader() {}
17 bool H264BitReader::Initialize(const uint8* data, off_t size) {
18 DCHECK(data);
20 if (size < 1)
21 return false;
23 data_ = data;
24 bytes_left_ = size;
25 num_remaining_bits_in_curr_byte_ = 0;
26 // Initially set to 0xffff to accept all initial two-byte sequences.
27 prev_two_bytes_ = 0xffff;
29 return true;
32 bool H264BitReader::UpdateCurrByte() {
33 if (bytes_left_ < 1)
34 return false;
36 // Emulation prevention three-byte detection.
37 // If a sequence of 0x000003 is found, skip (ignore) the last byte (0x03).
38 if (*data_ == 0x03 && (prev_two_bytes_ & 0xffff) == 0) {
39 // Detected 0x000003, skip last byte.
40 ++data_;
41 --bytes_left_;
42 // Need another full three bytes before we can detect the sequence again.
43 prev_two_bytes_ = 0xffff;
45 if (bytes_left_ < 1)
46 return false;
49 // Load a new byte and advance pointers.
50 curr_byte_ = *data_++ & 0xff;
51 --bytes_left_;
52 num_remaining_bits_in_curr_byte_ = 8;
54 prev_two_bytes_ = (prev_two_bytes_ << 8) | curr_byte_;
56 return true;
59 // Read |num_bits| (1 to 31 inclusive) from the stream and return them
60 // in |out|, with first bit in the stream as MSB in |out| at position
61 // (|num_bits| - 1).
62 bool H264BitReader::ReadBits(int num_bits, int *out) {
63 int bits_left = num_bits;
64 *out = 0;
65 DCHECK(num_bits <= 31);
67 while (num_remaining_bits_in_curr_byte_ < bits_left) {
68 // Take all that's left in current byte, shift to make space for the rest.
69 *out |= (curr_byte_ << (bits_left - num_remaining_bits_in_curr_byte_));
70 bits_left -= num_remaining_bits_in_curr_byte_;
72 if (!UpdateCurrByte())
73 return false;
76 *out |= (curr_byte_ >> (num_remaining_bits_in_curr_byte_ - bits_left));
77 *out &= ((1 << num_bits) - 1);
78 num_remaining_bits_in_curr_byte_ -= bits_left;
80 return true;
83 off_t H264BitReader::NumBitsLeft() {
84 return (num_remaining_bits_in_curr_byte_ + bytes_left_ * 8);
87 bool H264BitReader::HasMoreRBSPData() {
88 // Make sure we have more bits, if we are at 0 bits in current byte
89 // and updating current byte fails, we don't have more data anyway.
90 if (num_remaining_bits_in_curr_byte_ == 0 && !UpdateCurrByte())
91 return false;
93 // On last byte?
94 if (bytes_left_)
95 return true;
97 // Last byte, look for stop bit;
98 // We have more RBSP data if the last non-zero bit we find is not the
99 // first available bit.
100 return (curr_byte_ &
101 ((1 << (num_remaining_bits_in_curr_byte_ - 1)) - 1)) != 0;
104 } // namespace content