OLD | NEW |
1 /* | 1 /* |
2 * Copyright (c) 2016 The WebRTC project authors. All Rights Reserved. | 2 * Copyright (c) 2016 The WebRTC project authors. All Rights Reserved. |
3 * | 3 * |
4 * Use of this source code is governed by a BSD-style license | 4 * Use of this source code is governed by a BSD-style license |
5 * that can be found in the LICENSE file in the root of the source | 5 * that can be found in the LICENSE file in the root of the source |
6 * tree. An additional intellectual property rights grant can be found | 6 * tree. An additional intellectual property rights grant can be found |
7 * in the file PATENTS. All contributing project authors may | 7 * in the file PATENTS. All contributing project authors may |
8 * be found in the AUTHORS file in the root of the source tree. | 8 * be found in the AUTHORS file in the root of the source tree. |
9 */ | 9 */ |
10 | 10 |
(...skipping 41 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
52 it->payload_size = buffer_size - it->payload_start_offset; | 52 it->payload_size = buffer_size - it->payload_start_offset; |
53 | 53 |
54 return sequences; | 54 return sequences; |
55 } | 55 } |
56 | 56 |
57 NaluType ParseNaluType(uint8_t data) { | 57 NaluType ParseNaluType(uint8_t data) { |
58 return static_cast<NaluType>(data & kNaluTypeMask); | 58 return static_cast<NaluType>(data & kNaluTypeMask); |
59 } | 59 } |
60 | 60 |
61 std::unique_ptr<rtc::Buffer> ParseRbsp(const uint8_t* data, size_t length) { | 61 std::unique_ptr<rtc::Buffer> ParseRbsp(const uint8_t* data, size_t length) { |
62 std::unique_ptr<rtc::Buffer> rbsp_buffer(new rtc::Buffer()); | 62 std::unique_ptr<rtc::Buffer> rbsp_buffer(new rtc::Buffer(0, length)); |
63 const char* sps_bytes = reinterpret_cast<const char*>(data); | 63 const char* sps_bytes = reinterpret_cast<const char*>(data); |
64 for (size_t i = 0; i < length;) { | 64 for (size_t i = 0; i < length;) { |
65 // Be careful about over/underflow here. byte_length_ - 3 can underflow, and | 65 // Be careful about over/underflow here. byte_length_ - 3 can underflow, and |
66 // i + 3 can overflow, but byte_length_ - i can't, because i < byte_length_ | 66 // i + 3 can overflow, but byte_length_ - i can't, because i < byte_length_ |
67 // above, and that expression will produce the number of bytes left in | 67 // above, and that expression will produce the number of bytes left in |
68 // the stream including the byte at i. | 68 // the stream including the byte at i. |
69 if (length - i >= 3 && data[i] == 0 && data[i + 1] == 0 && | 69 if (length - i >= 3 && data[i] == 0 && data[i + 1] == 0 && |
70 data[i + 2] == 3) { | 70 data[i + 2] == 3) { |
71 // Two rbsp bytes + the emulation byte. | 71 // Two rbsp bytes + the emulation byte. |
72 rbsp_buffer->AppendData(sps_bytes + i, 2); | 72 rbsp_buffer->AppendData(sps_bytes + i, 2); |
73 i += 3; | 73 i += 3; |
74 } else { | 74 } else { |
75 // Single rbsp byte. | 75 // Single rbsp byte. |
76 rbsp_buffer->AppendData(sps_bytes[i]); | 76 rbsp_buffer->AppendData(sps_bytes[i]); |
77 ++i; | 77 ++i; |
78 } | 78 } |
79 } | 79 } |
80 return rbsp_buffer; | 80 return rbsp_buffer; |
81 } | 81 } |
82 | 82 |
83 void WriteRbsp(const uint8_t* bytes, size_t length, rtc::Buffer* destination) { | 83 void WriteRbsp(const uint8_t* bytes, size_t length, rtc::Buffer* destination) { |
84 static const uint8_t kZerosInStartSequence = 2; | 84 static const uint8_t kZerosInStartSequence = 2; |
85 static const uint8_t kEmulationByte = 0x03u; | 85 static const uint8_t kEmulationByte = 0x03u; |
86 size_t num_consecutive_zeros = 0; | 86 size_t num_consecutive_zeros = 0; |
| 87 destination->EnsureCapacity(destination->size() + length); |
87 | 88 |
88 for (size_t i = 0; i < length; ++i) { | 89 for (size_t i = 0; i < length; ++i) { |
89 uint8_t byte = bytes[i]; | 90 uint8_t byte = bytes[i]; |
90 if (byte <= kEmulationByte && | 91 if (byte <= kEmulationByte && |
91 num_consecutive_zeros >= kZerosInStartSequence) { | 92 num_consecutive_zeros >= kZerosInStartSequence) { |
92 // Need to escape. | 93 // Need to escape. |
93 destination->AppendData(kEmulationByte); | 94 destination->AppendData(kEmulationByte); |
94 num_consecutive_zeros = 0; | 95 num_consecutive_zeros = 0; |
95 } | 96 } |
96 destination->AppendData(byte); | 97 destination->AppendData(byte); |
97 if (byte == 0) { | 98 if (byte == 0) { |
98 ++num_consecutive_zeros; | 99 ++num_consecutive_zeros; |
99 } else { | 100 } else { |
100 num_consecutive_zeros = 0; | 101 num_consecutive_zeros = 0; |
101 } | 102 } |
102 } | 103 } |
103 } | 104 } |
104 | 105 |
105 } // namespace H264 | 106 } // namespace H264 |
106 } // namespace webrtc | 107 } // namespace webrtc |
OLD | NEW |