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

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

Issue 2728093002: Optimize ParseRbsp method in H264 bitstream parser. (Closed)
Patch Set: Fix memory leak 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
« no previous file with comments | « no previous file | no next file » | no next file with comments »
Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
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 43 matching lines...) Expand 10 before | Expand all | Expand 10 after
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::unique_ptr<rtc::Buffer> ParseRbsp(const uint8_t* data, size_t length) {
64 std::unique_ptr<rtc::Buffer> rbsp_buffer(new rtc::Buffer(0, length)); 64 std::unique_ptr<uint8_t[]> out(new uint8_t[length]);
magjed_webrtc 2017/03/03 13:14:14 I think it would be better with an std::vector<uin
65 const char* sps_bytes = reinterpret_cast<const char*>(data); 65
66 for (size_t i = 0; i < length;) { 66 size_t j, i;
67 for (i = j = 0; i < length;) {
68 uint8_t* dst = out.get();
67 // Be careful about over/underflow here. byte_length_ - 3 can underflow, and 69 // 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_ 70 // 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 71 // above, and that expression will produce the number of bytes left in
70 // the stream including the byte at i. 72 // the stream including the byte at i.
71 if (length - i >= 3 && data[i] == 0 && data[i + 1] == 0 && 73 if (length - i >= 3 && !data[i] && !data[i + 1] && data[i + 2] == 3) {
72 data[i + 2] == 3) { 74 // Two rbsp bytes.
73 // Two rbsp bytes + the emulation byte. 75 dst[j++] = data[i++];
74 rbsp_buffer->AppendData(sps_bytes + i, 2); 76 dst[j++] = data[i++];
75 i += 3; 77 // Skip the emulation byte.
78 i++;
76 } else { 79 } else {
77 // Single rbsp byte. 80 // Single rbsp byte.
78 rbsp_buffer->AppendData(sps_bytes[i]); 81 dst[j++] = data[i++];
79 ++i;
80 } 82 }
81 } 83 }
82 return rbsp_buffer; 84 return std::unique_ptr<rtc::Buffer>(new rtc::Buffer(out.get(), j));
83 } 85 }
84 86
85 void WriteRbsp(const uint8_t* bytes, size_t length, rtc::Buffer* destination) { 87 void WriteRbsp(const uint8_t* bytes, size_t length, rtc::Buffer* destination) {
86 static const uint8_t kZerosInStartSequence = 2; 88 static const uint8_t kZerosInStartSequence = 2;
87 static const uint8_t kEmulationByte = 0x03u; 89 static const uint8_t kEmulationByte = 0x03u;
88 size_t num_consecutive_zeros = 0; 90 size_t num_consecutive_zeros = 0;
89 destination->EnsureCapacity(destination->size() + length); 91 destination->EnsureCapacity(destination->size() + length);
90 92
91 for (size_t i = 0; i < length; ++i) { 93 for (size_t i = 0; i < length; ++i) {
92 uint8_t byte = bytes[i]; 94 uint8_t byte = bytes[i];
93 if (byte <= kEmulationByte && 95 if (byte <= kEmulationByte &&
94 num_consecutive_zeros >= kZerosInStartSequence) { 96 num_consecutive_zeros >= kZerosInStartSequence) {
95 // Need to escape. 97 // Need to escape.
96 destination->AppendData(kEmulationByte); 98 destination->AppendData(kEmulationByte);
97 num_consecutive_zeros = 0; 99 num_consecutive_zeros = 0;
98 } 100 }
99 destination->AppendData(byte); 101 destination->AppendData(byte);
100 if (byte == 0) { 102 if (byte == 0) {
101 ++num_consecutive_zeros; 103 ++num_consecutive_zeros;
102 } else { 104 } else {
103 num_consecutive_zeros = 0; 105 num_consecutive_zeros = 0;
104 } 106 }
105 } 107 }
106 } 108 }
107 109
108 } // namespace H264 110 } // namespace H264
109 } // namespace webrtc 111 } // namespace webrtc
OLDNEW
« no previous file with comments | « no previous file | no next file » | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698