OLD | NEW |
1 // Copyright 2014 The Chromium Authors. All rights reserved. | 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 | 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 "media/filters/h264_bitstream_buffer.h" | 5 #include "media/filters/h264_bitstream_buffer.h" |
6 | 6 |
7 #include "base/sys_byteorder.h" | 7 #include "base/sys_byteorder.h" |
8 | 8 |
9 namespace media { | 9 namespace media { |
10 | 10 |
(...skipping 13 matching lines...) Expand all Loading... |
24 capacity_ = 0; | 24 capacity_ = 0; |
25 pos_ = 0; | 25 pos_ = 0; |
26 reg_ = 0; | 26 reg_ = 0; |
27 | 27 |
28 Grow(); | 28 Grow(); |
29 | 29 |
30 bits_left_in_reg_ = kRegBitSize; | 30 bits_left_in_reg_ = kRegBitSize; |
31 } | 31 } |
32 | 32 |
33 void H264BitstreamBuffer::Grow() { | 33 void H264BitstreamBuffer::Grow() { |
34 data_ = static_cast<uint8*>(realloc(data_, capacity_ + kGrowBytes)); | 34 data_ = static_cast<uint8_t*>(realloc(data_, capacity_ + kGrowBytes)); |
35 CHECK(data_) << "Failed growing the buffer"; | 35 CHECK(data_) << "Failed growing the buffer"; |
36 capacity_ += kGrowBytes; | 36 capacity_ += kGrowBytes; |
37 } | 37 } |
38 | 38 |
39 void H264BitstreamBuffer::FlushReg() { | 39 void H264BitstreamBuffer::FlushReg() { |
40 // Flush all bytes that have at least one bit cached, but not more | 40 // Flush all bytes that have at least one bit cached, but not more |
41 // (on Flush(), reg_ may not be full). | 41 // (on Flush(), reg_ may not be full). |
42 size_t bits_in_reg = kRegBitSize - bits_left_in_reg_; | 42 size_t bits_in_reg = kRegBitSize - bits_left_in_reg_; |
43 if (bits_in_reg == 0) | 43 if (bits_in_reg == 0) |
44 return; | 44 return; |
45 | 45 |
46 size_t bytes_in_reg = (bits_in_reg + 7) / 8; | 46 size_t bytes_in_reg = (bits_in_reg + 7) / 8; |
47 reg_ <<= (kRegBitSize - bits_in_reg); | 47 reg_ <<= (kRegBitSize - bits_in_reg); |
48 | 48 |
49 // Convert to MSB and append as such to the stream. | 49 // Convert to MSB and append as such to the stream. |
50 reg_ = base::HostToNet64(reg_); | 50 reg_ = base::HostToNet64(reg_); |
51 | 51 |
52 // Make sure we have enough space. Grow() will CHECK() on allocation failure. | 52 // Make sure we have enough space. Grow() will CHECK() on allocation failure. |
53 if (pos_ + bytes_in_reg < capacity_) | 53 if (pos_ + bytes_in_reg < capacity_) |
54 Grow(); | 54 Grow(); |
55 | 55 |
56 memcpy(data_ + pos_, ®_, bytes_in_reg); | 56 memcpy(data_ + pos_, ®_, bytes_in_reg); |
57 pos_ += bytes_in_reg; | 57 pos_ += bytes_in_reg; |
58 | 58 |
59 reg_ = 0; | 59 reg_ = 0; |
60 bits_left_in_reg_ = kRegBitSize; | 60 bits_left_in_reg_ = kRegBitSize; |
61 } | 61 } |
62 | 62 |
63 void H264BitstreamBuffer::AppendU64(size_t num_bits, uint64 val) { | 63 void H264BitstreamBuffer::AppendU64(size_t num_bits, uint64_t val) { |
64 CHECK_LE(num_bits, kRegBitSize); | 64 CHECK_LE(num_bits, kRegBitSize); |
65 | 65 |
66 while (num_bits > 0) { | 66 while (num_bits > 0) { |
67 if (bits_left_in_reg_ == 0) | 67 if (bits_left_in_reg_ == 0) |
68 FlushReg(); | 68 FlushReg(); |
69 | 69 |
70 uint64 bits_to_write = | 70 uint64_t bits_to_write = |
71 num_bits > bits_left_in_reg_ ? bits_left_in_reg_ : num_bits; | 71 num_bits > bits_left_in_reg_ ? bits_left_in_reg_ : num_bits; |
72 uint64 val_to_write = (val >> (num_bits - bits_to_write)); | 72 uint64_t val_to_write = (val >> (num_bits - bits_to_write)); |
73 if (bits_to_write < 64) | 73 if (bits_to_write < 64) |
74 val_to_write &= ((1ull << bits_to_write) - 1); | 74 val_to_write &= ((1ull << bits_to_write) - 1); |
75 reg_ <<= bits_to_write; | 75 reg_ <<= bits_to_write; |
76 reg_ |= val_to_write; | 76 reg_ |= val_to_write; |
77 num_bits -= bits_to_write; | 77 num_bits -= bits_to_write; |
78 bits_left_in_reg_ -= bits_to_write; | 78 bits_left_in_reg_ -= bits_to_write; |
79 } | 79 } |
80 } | 80 } |
81 | 81 |
82 void H264BitstreamBuffer::AppendBool(bool val) { | 82 void H264BitstreamBuffer::AppendBool(bool val) { |
83 if (bits_left_in_reg_ == 0) | 83 if (bits_left_in_reg_ == 0) |
84 FlushReg(); | 84 FlushReg(); |
85 | 85 |
86 reg_ <<= 1; | 86 reg_ <<= 1; |
87 reg_ |= (static_cast<uint64>(val) & 1); | 87 reg_ |= (static_cast<uint64_t>(val) & 1); |
88 --bits_left_in_reg_; | 88 --bits_left_in_reg_; |
89 } | 89 } |
90 | 90 |
91 void H264BitstreamBuffer::AppendSE(int val) { | 91 void H264BitstreamBuffer::AppendSE(int val) { |
92 if (val > 0) | 92 if (val > 0) |
93 AppendUE(val * 2 - 1); | 93 AppendUE(val * 2 - 1); |
94 else | 94 else |
95 AppendUE(-val * 2); | 95 AppendUE(-val * 2); |
96 } | 96 } |
97 | 97 |
(...skipping 37 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
135 | 135 |
136 if (bits_left_in_reg_ != kRegBitSize) | 136 if (bits_left_in_reg_ != kRegBitSize) |
137 FlushReg(); | 137 FlushReg(); |
138 } | 138 } |
139 | 139 |
140 size_t H264BitstreamBuffer::BytesInBuffer() { | 140 size_t H264BitstreamBuffer::BytesInBuffer() { |
141 DCHECK_FINISHED(); | 141 DCHECK_FINISHED(); |
142 return pos_; | 142 return pos_; |
143 } | 143 } |
144 | 144 |
145 uint8* H264BitstreamBuffer::data() { | 145 uint8_t* H264BitstreamBuffer::data() { |
146 DCHECK(data_); | 146 DCHECK(data_); |
147 DCHECK_FINISHED(); | 147 DCHECK_FINISHED(); |
148 | 148 |
149 return data_; | 149 return data_; |
150 } | 150 } |
151 | 151 |
152 } // namespace media | 152 } // namespace media |
OLD | NEW |