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"
10 H264BitReader::H264BitReader()
14 num_remaining_bits_in_curr_byte_(0),
16 emulation_prevention_bytes_(0) {}
18 H264BitReader::~H264BitReader() {}
20 bool H264BitReader::Initialize(const uint8
* data
, off_t 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;
36 bool H264BitReader::UpdateCurrByte() {
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.
46 ++emulation_prevention_bytes_
;
47 // Need another full three bytes before we can detect the sequence again.
48 prev_two_bytes_
= 0xffff;
54 // Load a new byte and advance pointers.
55 curr_byte_
= *data_
++ & 0xff;
57 num_remaining_bits_in_curr_byte_
= 8;
59 prev_two_bytes_
= (prev_two_bytes_
<< 8) | curr_byte_
;
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
67 bool H264BitReader::ReadBits(int num_bits
, int* out
) {
68 int bits_left
= num_bits
;
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())
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
;
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())
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.
106 ((1 << (num_remaining_bits_in_curr_byte_
- 1)) - 1)) != 0;
109 size_t H264BitReader::NumEmulationPreventionBytesRead() {
110 return emulation_prevention_bytes_
;