Add @VisibleForTesting to fix ChromePublic release build.
[chromium-blink-merge.git] / media / filters / h264_bit_reader.cc
blob9894d97897813c389e91d5ddd39024c2857d2719
1 // Copyright 2014 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 "base/logging.h"
6 #include "media/filters/h264_bit_reader.h"
8 namespace media {
10 H264BitReader::H264BitReader()
11 : data_(NULL),
12 bytes_left_(0),
13 curr_byte_(0),
14 num_remaining_bits_in_curr_byte_(0),
15 prev_two_bytes_(0),
16 emulation_prevention_bytes_(0) {}
18 H264BitReader::~H264BitReader() {}
20 bool H264BitReader::Initialize(const uint8* data, off_t size) {
21 DCHECK(data);
23 if (size < 1)
24 return false;
26 data_ = data;
27 bytes_left_ = size;
28 num_remaining_bits_in_curr_byte_ = 0;
29 // Initially set to 0xffff to accept all initial two-byte sequences.
30 prev_two_bytes_ = 0xffff;
31 emulation_prevention_bytes_ = 0;
33 return true;
36 bool H264BitReader::UpdateCurrByte() {
37 if (bytes_left_ < 1)
38 return false;
40 // Emulation prevention three-byte detection.
41 // If a sequence of 0x000003 is found, skip (ignore) the last byte (0x03).
42 if (*data_ == 0x03 && (prev_two_bytes_ & 0xffff) == 0) {
43 // Detected 0x000003, skip last byte.
44 ++data_;
45 --bytes_left_;
46 ++emulation_prevention_bytes_;
47 // Need another full three bytes before we can detect the sequence again.
48 prev_two_bytes_ = 0xffff;
50 if (bytes_left_ < 1)
51 return false;
54 // Load a new byte and advance pointers.
55 curr_byte_ = *data_++ & 0xff;
56 --bytes_left_;
57 num_remaining_bits_in_curr_byte_ = 8;
59 prev_two_bytes_ = (prev_two_bytes_ << 8) | curr_byte_;
61 return true;
64 // Read |num_bits| (1 to 31 inclusive) from the stream and return them
65 // in |out|, with first bit in the stream as MSB in |out| at position
66 // (|num_bits| - 1).
67 bool H264BitReader::ReadBits(int num_bits, int* out) {
68 int bits_left = num_bits;
69 *out = 0;
70 DCHECK(num_bits <= 31);
72 while (num_remaining_bits_in_curr_byte_ < bits_left) {
73 // Take all that's left in current byte, shift to make space for the rest.
74 *out |= (curr_byte_ << (bits_left - num_remaining_bits_in_curr_byte_));
75 bits_left -= num_remaining_bits_in_curr_byte_;
77 if (!UpdateCurrByte())
78 return false;
81 *out |= (curr_byte_ >> (num_remaining_bits_in_curr_byte_ - bits_left));
82 *out &= ((1 << num_bits) - 1);
83 num_remaining_bits_in_curr_byte_ -= bits_left;
85 return true;
88 off_t H264BitReader::NumBitsLeft() {
89 return (num_remaining_bits_in_curr_byte_ + bytes_left_ * 8);
92 bool H264BitReader::HasMoreRBSPData() {
93 // Make sure we have more bits, if we are at 0 bits in current byte
94 // and updating current byte fails, we don't have more data anyway.
95 if (num_remaining_bits_in_curr_byte_ == 0 && !UpdateCurrByte())
96 return false;
98 // On last byte?
99 if (bytes_left_)
100 return true;
102 // Last byte, look for stop bit;
103 // We have more RBSP data if the last non-zero bit we find is not the
104 // first available bit.
105 return (curr_byte_ &
106 ((1 << (num_remaining_bits_in_curr_byte_ - 1)) - 1)) != 0;
109 size_t H264BitReader::NumEmulationPreventionBytesRead() {
110 return emulation_prevention_bytes_;
113 } // namespace media