OLD | NEW |
---|---|
(Empty) | |
1 // Copyright (c) 2013 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. | |
4 | |
5 #include "media/base/bit_reader_h264.h" | |
6 | |
7 namespace media { | |
8 | |
9 // static | |
10 const int BitReaderH264::kMaxExpGolombCodeSize = 63; | |
11 | |
12 BitReaderH264::BitReaderH264(const uint8* data, int size) | |
13 : data_(data), | |
14 bytes_left_(size), | |
15 prev_two_bytes_(0xffff), | |
16 bit_reader_core_(this) { | |
17 DCHECK(data_ != NULL); | |
18 DCHECK_GE(bytes_left_, 0); | |
19 } | |
20 | |
21 BitReaderH264::~BitReaderH264() {} | |
22 | |
23 int BitReaderH264::ReadUE(uint32* out) { | |
24 // Get the number of leading zeros. | |
25 int zero_count = -1; | |
26 bool is_one; | |
27 do { | |
28 if (!bit_reader_core_.ReadFlag(&is_one)) | |
29 return -1; | |
30 zero_count++; | |
31 } while(!is_one); | |
32 | |
33 int code_size = 2 * zero_count + 1; | |
34 | |
35 // If zero_count is greater than 31, the calculated value will overflow, | |
36 // just skip the bits corresponding to the exp-golomb value. | |
37 if (zero_count > 31) { | |
damienv1
2014/01/02 22:56:23
Equivalent but better would be:
if (code_size > k
damienv1
2014/01/07 00:19:40
Done.
| |
38 if (!bit_reader_core_.SkipBits(zero_count)) | |
39 return -1; | |
40 return code_size; | |
acolwell GONE FROM CHROMIUM
2014/01/06 22:44:06
Why don't we just return -1 here? It seems like th
damienv1
2014/01/07 00:19:40
My assumption here is that the bit reader should n
| |
41 } | |
42 | |
43 // Read the actual value. | |
44 uint32 base = (1 << zero_count) - 1; | |
45 uint32 offset; | |
46 if (!ReadBits(zero_count, &offset)) | |
47 return -1; | |
48 *out = base + offset; | |
49 | |
50 return code_size; | |
51 } | |
52 | |
53 bool BitReaderH264::HasMoreRBSPData() { | |
54 uint64 bit_reg; | |
55 int nbits = bit_reader_core_.PeekBitsMsbAligned(9, &bit_reg); | |
56 | |
57 // Not on last byte. | |
58 if (nbits > 8) | |
59 return true; | |
60 | |
61 // Last byte, look for stop bit; | |
62 // We have more RBSP data if the last non-zero bit we find is not the | |
63 // first available bit. | |
64 return (bit_reg << 1) != 0; | |
acolwell GONE FROM CHROMIUM
2014/01/06 22:44:06
Is this correct? This doesn't check that the MSb i
damienv1
2014/01/07 00:19:40
* Case 1, "(bit_reg << 1) != 0": in this case, thi
| |
65 } | |
66 | |
67 int BitReaderH264::GetBytes(int max_nbytes, const uint8** out) { | |
68 DCHECK_LE(max_nbytes, static_cast<int>(sizeof(data_window_))); | |
69 DCHECK_GE(max_nbytes, 0); | |
70 DCHECK(out); | |
71 | |
72 const uint8* start = data_; | |
73 bool emulation_prevention_byte_detected = false; | |
74 | |
75 int nbytes = 0; | |
76 *out = data_; | |
77 | |
78 while (nbytes < max_nbytes && bytes_left_ > 0) { | |
79 // Emulation prevention three-byte detection. | |
80 // If a sequence of 0x000003 is found, skip (ignore) the last byte (0x03) | |
81 // and starts copying data to |data_window_| which is used as the output. | |
82 if (*data_ == 0x3 && (prev_two_bytes_ & 0xffff) == 0) { | |
83 if (!emulation_prevention_byte_detected && nbytes > 0) | |
84 memcpy(data_window_, start, nbytes); | |
85 emulation_prevention_byte_detected = true; | |
86 *out = data_window_; | |
87 // Need another full three bytes before we can detect the sequence again. | |
88 prev_two_bytes_ = 0xffff; | |
89 } else { | |
90 if (emulation_prevention_byte_detected) | |
91 data_window_[nbytes] = *data_; | |
92 prev_two_bytes_ = (prev_two_bytes_ << 8) | *data_; | |
93 ++nbytes; | |
94 } | |
95 | |
96 ++data_; | |
97 --bytes_left_; | |
98 } | |
99 | |
100 return nbytes; | |
101 } | |
102 | |
103 int BitReaderH264::GetBytesLeft() const { | |
104 // Getting the actual number of bytes left would be really inefficient | |
105 // as this would mean parsing the rest of data | |
106 // for possible start code emulation prevention bytes. | |
107 // So this function should rather not be used in the framework of H264. | |
108 NOTREACHED(); | |
acolwell GONE FROM CHROMIUM
2014/01/06 22:44:06
This is a code smell. If both providers can't prop
damienv1
2014/01/07 00:19:40
Yes, I can do that. This is now possible since the
| |
109 return 0; | |
110 } | |
111 | |
112 } // namespace media | |
OLD | NEW |