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

Side by Side Diff: webrtc/common_video/h264/h264_common.cc

Issue 2728093002: Optimize ParseRbsp method in H264 bitstream parser. (Closed)
Patch Set: return vector to avoid copy Created 3 years, 9 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 /* 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 42 matching lines...) Expand 10 before | Expand all | Expand 10 after
53 if (it != sequences.rend()) 53 if (it != sequences.rend())
54 it->payload_size = buffer_size - it->payload_start_offset; 54 it->payload_size = buffer_size - it->payload_start_offset;
55 55
56 return sequences; 56 return sequences;
57 } 57 }
58 58
59 NaluType ParseNaluType(uint8_t data) { 59 NaluType ParseNaluType(uint8_t data) {
60 return static_cast<NaluType>(data & kNaluTypeMask); 60 return static_cast<NaluType>(data & kNaluTypeMask);
61 } 61 }
62 62
63 std::unique_ptr<rtc::Buffer> ParseRbsp(const uint8_t* data, size_t length) { 63 std::vector<uint8_t> ParseRbsp(const uint8_t* data, size_t length) {
64 std::unique_ptr<rtc::Buffer> rbsp_buffer(new rtc::Buffer(0, length)); 64 std::vector<uint8_t> out;
65 const char* sps_bytes = reinterpret_cast<const char*>(data); 65 out.reserve(length);
66
66 for (size_t i = 0; i < length;) { 67 for (size_t i = 0; i < length;) {
67 // Be careful about over/underflow here. byte_length_ - 3 can underflow, and 68 // Be careful about over/underflow here. byte_length_ - 3 can underflow, and
68 // i + 3 can overflow, but byte_length_ - i can't, because i < byte_length_ 69 // i + 3 can overflow, but byte_length_ - i can't, because i < byte_length_
69 // above, and that expression will produce the number of bytes left in 70 // above, and that expression will produce the number of bytes left in
70 // the stream including the byte at i. 71 // the stream including the byte at i.
71 if (length - i >= 3 && data[i] == 0 && data[i + 1] == 0 && 72 if (length - i >= 3 && !data[i] && !data[i + 1] && data[i + 2] == 3) {
72 data[i + 2] == 3) { 73 // Two rbsp bytes.
73 // Two rbsp bytes + the emulation byte. 74 out.push_back(data[i++]);
74 rbsp_buffer->AppendData(sps_bytes + i, 2); 75 out.push_back(data[i++]);
75 i += 3; 76 // Skip the emulation byte.
77 i++;
76 } else { 78 } else {
77 // Single rbsp byte. 79 // Single rbsp byte.
78 rbsp_buffer->AppendData(sps_bytes[i]); 80 out.push_back(data[i++]);
79 ++i;
80 } 81 }
81 } 82 }
82 return rbsp_buffer; 83 return out;
83 } 84 }
84 85
85 void WriteRbsp(const uint8_t* bytes, size_t length, rtc::Buffer* destination) { 86 void WriteRbsp(const uint8_t* bytes, size_t length, rtc::Buffer* destination) {
86 static const uint8_t kZerosInStartSequence = 2; 87 static const uint8_t kZerosInStartSequence = 2;
87 static const uint8_t kEmulationByte = 0x03u; 88 static const uint8_t kEmulationByte = 0x03u;
88 size_t num_consecutive_zeros = 0; 89 size_t num_consecutive_zeros = 0;
89 destination->EnsureCapacity(destination->size() + length); 90 destination->EnsureCapacity(destination->size() + length);
90 91
91 for (size_t i = 0; i < length; ++i) { 92 for (size_t i = 0; i < length; ++i) {
92 uint8_t byte = bytes[i]; 93 uint8_t byte = bytes[i];
93 if (byte <= kEmulationByte && 94 if (byte <= kEmulationByte &&
94 num_consecutive_zeros >= kZerosInStartSequence) { 95 num_consecutive_zeros >= kZerosInStartSequence) {
95 // Need to escape. 96 // Need to escape.
96 destination->AppendData(kEmulationByte); 97 destination->AppendData(kEmulationByte);
97 num_consecutive_zeros = 0; 98 num_consecutive_zeros = 0;
98 } 99 }
99 destination->AppendData(byte); 100 destination->AppendData(byte);
100 if (byte == 0) { 101 if (byte == 0) {
101 ++num_consecutive_zeros; 102 ++num_consecutive_zeros;
102 } else { 103 } else {
103 num_consecutive_zeros = 0; 104 num_consecutive_zeros = 0;
104 } 105 }
105 } 106 }
106 } 107 }
107 108
108 } // namespace H264 109 } // namespace H264
109 } // namespace webrtc 110 } // namespace webrtc
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698