OLD | NEW |
---|---|
(Empty) | |
1 // Copyright (c) 2013 The Chromium Authors. All rights reserved. | |
acolwell GONE FROM CHROMIUM
2014/01/11 00:24:37
s/2013/2014
damienv1
2014/01/13 22:41:06
Done.
| |
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 bool BitReaderH264::ReadUE(uint32* out, int* code_size) { | |
24 int ue_code_size = ReadUEInternal(out); | |
25 | |
26 if (code_size) | |
27 *code_size = ue_code_size; | |
28 | |
29 return (ue_code_size >= 0 && ue_code_size <= kMaxExpGolombCodeSize); | |
30 } | |
31 | |
32 bool BitReaderH264::HasMoreRBSPData() { | |
33 uint64 bit_reg; | |
34 int nbits = bit_reader_core_.PeekBitsMsbAligned(9, &bit_reg); | |
35 | |
36 // Not on last byte. | |
37 if (nbits > 8) | |
38 return true; | |
39 | |
40 // Last byte, look for stop bit; | |
41 // We have more RBSP data if the last non-zero bit we find is not the | |
42 // first available bit. | |
43 return (bit_reg << 1) != 0; | |
44 } | |
45 | |
46 int BitReaderH264::GetBytes(int max_nbytes, const uint8** out) { | |
47 DCHECK_LE(max_nbytes, static_cast<int>(sizeof(data_window_))); | |
48 DCHECK_GE(max_nbytes, 0); | |
49 DCHECK(out); | |
50 | |
51 const uint8* start = data_; | |
52 bool emulation_prevention_byte_detected = false; | |
53 | |
54 int nbytes = 0; | |
55 *out = data_; | |
56 | |
57 while (nbytes < max_nbytes && bytes_left_ > 0) { | |
58 // Emulation prevention three-byte detection. | |
59 // If a sequence of 0x000003 is found, skip (ignore) the last byte (0x03) | |
60 // and starts copying data to |data_window_| which is used as the output. | |
61 if (*data_ == 0x3 && (prev_two_bytes_ & 0xffff) == 0) { | |
62 if (!emulation_prevention_byte_detected && nbytes > 0) | |
63 memcpy(data_window_, start, nbytes); | |
64 emulation_prevention_byte_detected = true; | |
65 *out = data_window_; | |
66 // Need another full three bytes before we can detect the sequence again. | |
67 prev_two_bytes_ = 0xffff; | |
68 } else { | |
69 if (emulation_prevention_byte_detected) | |
70 data_window_[nbytes] = *data_; | |
71 prev_two_bytes_ = (prev_two_bytes_ << 8) | *data_; | |
72 ++nbytes; | |
73 } | |
74 | |
75 ++data_; | |
76 --bytes_left_; | |
77 } | |
78 | |
79 return nbytes; | |
80 } | |
81 | |
82 int BitReaderH264::ReadUEInternal(uint32* out) { | |
83 // Get the number of leading zeros. | |
84 int zero_count = -1; | |
85 bool is_one; | |
86 do { | |
87 if (!bit_reader_core_.ReadFlag(&is_one)) | |
88 return -1; | |
89 zero_count++; | |
90 } while(!is_one); | |
91 | |
92 int code_size = 2 * zero_count + 1; | |
93 | |
94 // If zero_count is greater than 31, the calculated value will overflow, | |
acolwell GONE FROM CHROMIUM
2014/01/11 00:24:37
nit: This comment appears to be out of sync w/ the
damienv1
2014/01/13 22:41:06
Updated the comment based on the exp-golomb code s
| |
95 // just skip the bits corresponding to the exp-golomb value. | |
96 if (code_size > kMaxExpGolombCodeSize) { | |
97 if (!bit_reader_core_.SkipBits(zero_count)) | |
98 return -1; | |
99 return code_size; | |
100 } | |
101 | |
102 // Read the actual value. | |
103 uint32 base = (1 << zero_count) - 1; | |
acolwell GONE FROM CHROMIUM
2014/01/11 00:24:37
nit: Doesn't this take us into undefined territory
damienv1
2014/01/13 22:41:06
The code size is basically twice longer that the s
| |
104 uint32 offset; | |
105 if (!ReadBits(zero_count, &offset)) | |
106 return -1; | |
107 *out = base + offset; | |
108 | |
109 return code_size; | |
110 } | |
111 | |
112 } // namespace media | |
OLD | NEW |