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

Unified 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, 10 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 side-by-side diff with in-line comments
Download patch
« no previous file with comments | « no previous file | no next file » | no next file with comments »
Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
Index: webrtc/common_video/h264/h264_common.cc
diff --git a/webrtc/common_video/h264/h264_common.cc b/webrtc/common_video/h264/h264_common.cc
index a9cc6a25e62882e8619ed627aeb9cefee67e248f..4af434988dd784c54d982685942c37c306bb0a49 100644
--- a/webrtc/common_video/h264/h264_common.cc
+++ b/webrtc/common_video/h264/h264_common.cc
@@ -61,25 +61,27 @@ NaluType ParseNaluType(uint8_t data) {
}
std::unique_ptr<rtc::Buffer> ParseRbsp(const uint8_t* data, size_t length) {
- std::unique_ptr<rtc::Buffer> rbsp_buffer(new rtc::Buffer(0, length));
- const char* sps_bytes = reinterpret_cast<const char*>(data);
- for (size_t i = 0; i < length;) {
+ 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
+
+ size_t j, i;
+ for (i = j = 0; i < length;) {
+ uint8_t* dst = out.get();
// Be careful about over/underflow here. byte_length_ - 3 can underflow, and
// i + 3 can overflow, but byte_length_ - i can't, because i < byte_length_
// above, and that expression will produce the number of bytes left in
// the stream including the byte at i.
- if (length - i >= 3 && data[i] == 0 && data[i + 1] == 0 &&
- data[i + 2] == 3) {
- // Two rbsp bytes + the emulation byte.
- rbsp_buffer->AppendData(sps_bytes + i, 2);
- i += 3;
+ if (length - i >= 3 && !data[i] && !data[i + 1] && data[i + 2] == 3) {
+ // Two rbsp bytes.
+ dst[j++] = data[i++];
+ dst[j++] = data[i++];
+ // Skip the emulation byte.
+ i++;
} else {
// Single rbsp byte.
- rbsp_buffer->AppendData(sps_bytes[i]);
- ++i;
+ dst[j++] = data[i++];
}
}
- return rbsp_buffer;
+ return std::unique_ptr<rtc::Buffer>(new rtc::Buffer(out.get(), j));
}
void WriteRbsp(const uint8_t* bytes, size_t length, rtc::Buffer* destination) {
« 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