Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(316)

Side by Side Diff: content/common/gpu/media/h264_bit_reader.cc

Issue 10837058: Make H264BitReader ignore trailing zero bytes and stop bit. (Closed) Base URL: http://git.chromium.org/chromium/src.git@master
Patch Set: Address CR comments. Created 8 years, 4 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch
OLDNEW
1 // Copyright (c) 2012 The Chromium Authors. All rights reserved. 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 2 // Use of this source code is governed by a BSD-style license that can be
3 // found in the LICENSE file. 3 // found in the LICENSE file.
4 4
5 #include "content/common/gpu/media/h264_bit_reader.h" 5 #include "content/common/gpu/media/h264_bit_reader.h"
6 #include "base/logging.h" 6 #include "base/logging.h"
7 7
8 namespace content { 8 namespace content {
9 9
10 H264BitReader::H264BitReader() 10 H264BitReader::H264BitReader() {}
11 : data_(NULL), bytes_left_(0), curr_byte_(0),
12 num_remaining_bits_in_curr_byte_(0), prev_two_bytes_(0) {
13 }
14 11
15 H264BitReader::~H264BitReader() {} 12 H264BitReader::~H264BitReader() {}
16 13
17 bool H264BitReader::Initialize(const uint8* data, off_t size) { 14 bool H264BitReader::Initialize(const uint8* data, off_t size) {
18 DCHECK(data); 15 DCHECK(data);
19 16 DCHECK_GE(size, 0);
20 if (size < 1)
21 return false;
22 17
23 data_ = data; 18 data_ = data;
24 bytes_left_ = size; 19 bytes_left_ = size;
20
21 if (!RemoveTrailingZeroAndStopBit())
22 return false;
23
25 num_remaining_bits_in_curr_byte_ = 0; 24 num_remaining_bits_in_curr_byte_ = 0;
26 // Initially set to 0xffff to accept all initial two-byte sequences. 25 // Initially set to 0xffff to accept all initial two-byte sequences.
27 prev_two_bytes_ = 0xffff; 26 prev_two_bytes_ = 0xffff;
28 27
29 return true; 28 return true;
30 } 29 }
31 30
32 bool H264BitReader::UpdateCurrByte() { 31 bool H264BitReader::UpdateCurrByte() {
33 if (bytes_left_ < 1) 32 if (bytes_left_ < 1)
34 return false; 33 return false;
35 34
36 // Emulation prevention three-byte detection. 35 // Emulation prevention three-byte detection.
37 // If a sequence of 0x000003 is found, skip (ignore) the last byte (0x03). 36 // If a sequence of 0x000003 is found, skip (ignore) the last byte (0x03).
38 if (*data_ == 0x03 && (prev_two_bytes_ & 0xffff) == 0) { 37 if (*data_ == 0x03 && (prev_two_bytes_ & 0xffff) == 0) {
39 // Detected 0x000003, skip last byte. 38 // Detected 0x000003, skip last byte.
40 ++data_; 39 ++data_;
41 --bytes_left_; 40 --bytes_left_;
42 // Need another full three bytes before we can detect the sequence again. 41 // Need another full three bytes before we can detect the sequence again.
43 prev_two_bytes_ = 0xffff; 42 prev_two_bytes_ = 0xffff;
44 43
45 if (bytes_left_ < 1) 44 if (bytes_left_ < 1)
46 return false; 45 return false;
47 } 46 }
48 47
49 // Load a new byte and advance pointers. 48 // Load a new byte and advance pointers.
50 curr_byte_ = *data_++ & 0xff; 49 curr_byte_ = *data_++ & 0xff;
51 --bytes_left_; 50 --bytes_left_;
52 num_remaining_bits_in_curr_byte_ = 8; 51
52 if (bytes_left_ == 0) {
53 curr_byte_ >>= 8 - data_bits_in_last_byte_;
54 num_remaining_bits_in_curr_byte_ = data_bits_in_last_byte_;
55 } else {
56 num_remaining_bits_in_curr_byte_ = 8;
57 }
53 58
54 prev_two_bytes_ = (prev_two_bytes_ << 8) | curr_byte_; 59 prev_two_bytes_ = (prev_two_bytes_ << 8) | curr_byte_;
55 60
56 return true; 61 return true;
57 } 62 }
58 63
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) { 64 bool H264BitReader::ReadBits(int num_bits, int *out) {
63 int bits_left = num_bits; 65 int bits_left = num_bits;
64 *out = 0; 66 *out = 0;
65 DCHECK(num_bits <= 31); 67 DCHECK_LE(num_bits, 31);
66 68
67 while (num_remaining_bits_in_curr_byte_ < bits_left) { 69 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. 70 // 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_)); 71 *out |= (curr_byte_ << (bits_left - num_remaining_bits_in_curr_byte_));
Ami GONE FROM CHROMIUM 2012/08/02 19:49:48 I think this bugfix belongs in its own CL. How did
xiaomings 2012/08/03 00:37:58 Done.
70 bits_left -= num_remaining_bits_in_curr_byte_; 72 bits_left -= num_remaining_bits_in_curr_byte_;
71 73
72 if (!UpdateCurrByte()) 74 if (!UpdateCurrByte())
73 return false; 75 return false;
74 } 76 }
75 77
76 *out |= (curr_byte_ >> (num_remaining_bits_in_curr_byte_ - bits_left)); 78 *out |= (curr_byte_ >> (num_remaining_bits_in_curr_byte_ - bits_left));
77 *out &= ((1 << num_bits) - 1); 79 *out &= ((1 << num_bits) - 1);
78 num_remaining_bits_in_curr_byte_ -= bits_left; 80 num_remaining_bits_in_curr_byte_ -= bits_left;
79 81
80 return true; 82 return true;
81 } 83 }
82 84
83 off_t H264BitReader::NumBitsLeft() { 85 off_t H264BitReader::NumBitsLeft() const {
84 return (num_remaining_bits_in_curr_byte_ + bytes_left_ * 8); 86 off_t bits_left = num_remaining_bits_in_curr_byte_ +
87 (bytes_left_ + trailing_zero_bytes_) * 8;
88 if (bytes_left_ == 0)
89 bits_left += 8 - data_bits_in_last_byte_;
90 return bits_left;
85 } 91 }
86 92
87 bool H264BitReader::HasMoreRBSPData() { 93 bool H264BitReader::RemoveTrailingZeroAndStopBit() {
88 // Make sure we have more bits, if we are at 0 bits in current byte 94 // First remove all trailing zero bytes.
89 // and updating current byte fails, we don't have more data anyway. 95 trailing_zero_bytes_ = 0;
90 if (num_remaining_bits_in_curr_byte_ == 0 && !UpdateCurrByte())
91 return false;
92 96
93 // On last byte? 97 while (bytes_left_ != 0) {
94 if (bytes_left_) 98 if (data_[bytes_left_ - 1] == 0) {
95 return true; 99 --bytes_left_;
100 ++trailing_zero_bytes_;
101 continue;
102 }
103 if (bytes_left_ >= 3 && data_[bytes_left_ - 1] == 0x03 &&
104 data_[bytes_left_ - 2] == 0x00 && data_[bytes_left_ - 3] == 0x00) {
105 bytes_left_ -= 3;
106 trailing_zero_bytes_ += 3;
107 continue;
108 }
96 109
97 // Last byte, look for stop bit; 110 break;
98 // We have more RBSP data if the last non-zero bit we find is not the 111 }
99 // first available bit. 112
100 return (curr_byte_ & 113 if (bytes_left_ == 0)
101 ((1 << (num_remaining_bits_in_curr_byte_ - 1)) - 1)) != 0; 114 return false;
115
116 // Then remove the trailing zero bits of the last byte.
117 uint8 last_byte = data_[bytes_left_ - 1];
118
119 data_bits_in_last_byte_ = 8;
Ami GONE FROM CHROMIUM 2012/08/02 19:49:48 l.118-125 can be replaced with: data_bits_in_last_
xiaomings 2012/08/03 00:37:58 Done.
120
121 while ((last_byte & (1 << (8 - data_bits_in_last_byte_))) == 0)
122 --data_bits_in_last_byte_;
123
124 --data_bits_in_last_byte_; // Remove the stop bit
125
126 if (data_bits_in_last_byte_ == 0) {
Ami GONE FROM CHROMIUM 2012/08/02 19:49:48 I see now what you were doing before, and I think
xiaomings 2012/08/03 00:37:58 Done.
127 --bytes_left_;
128 data_bits_in_last_byte_ = 8;
129 ++trailing_zero_bytes_;
130 }
131
132 return bytes_left_ != 0;
102 } 133 }
103 134
104 } // namespace content 135 } // namespace content
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698