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"
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
) {
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;
32 bool H264BitReader::UpdateCurrByte() {
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.
42 // Need another full three bytes before we can detect the sequence again.
43 prev_two_bytes_
= 0xffff;
49 // Load a new byte and advance pointers.
50 curr_byte_
= *data_
++ & 0xff;
52 num_remaining_bits_in_curr_byte_
= 8;
54 prev_two_bytes_
= (prev_two_bytes_
<< 8) | curr_byte_
;
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
62 bool H264BitReader::ReadBits(int num_bits
, int *out
) {
63 int bits_left
= num_bits
;
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())
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
;
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())
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.
101 ((1 << (num_remaining_bits_in_curr_byte_
- 1)) - 1)) != 0;
104 } // namespace content