From 75bc75ccef59caeeeecdeac83426565efddb6780 Mon Sep 17 00:00:00 2001 From: Piasy Date: Fri, 29 Nov 2019 21:35:19 +0800 Subject: [PATCH] Optimize FindNaluIndices MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit We can add i by 3 even if it's not 001, because in any xx1 combination, 1 could not be part of start code. Bug: None Change-Id: Ica653d9e49b9f7c665027b9cf4453753c5fdd3ed Reviewed-on: https://webrtc-review.googlesource.com/c/src/+/159721 Commit-Queue: Kári Helgason Reviewed-by: Erik Språng Reviewed-by: Kári Helgason Reviewed-by: Tommi Cr-Commit-Position: refs/heads/master@{#30064} --- common_video/h264/h264_common.cc | 24 ++++++++++++++---------- 1 file changed, 14 insertions(+), 10 deletions(-) diff --git a/common_video/h264/h264_common.cc b/common_video/h264/h264_common.cc index 5e58ba62e9..06d94e0305 100644 --- a/common_video/h264/h264_common.cc +++ b/common_video/h264/h264_common.cc @@ -27,22 +27,26 @@ std::vector FindNaluIndices(const uint8_t* buffer, if (buffer_size < kNaluShortStartSequenceSize) return sequences; + static_assert(kNaluShortStartSequenceSize >= 2, + "kNaluShortStartSequenceSize must be larger or equals to 2"); const size_t end = buffer_size - kNaluShortStartSequenceSize; for (size_t i = 0; i < end;) { if (buffer[i + 2] > 1) { i += 3; - } else if (buffer[i + 2] == 1 && buffer[i + 1] == 0 && buffer[i] == 0) { - // We found a start sequence, now check if it was a 3 of 4 byte one. - NaluIndex index = {i, i + 3, 0}; - if (index.start_offset > 0 && buffer[index.start_offset - 1] == 0) - --index.start_offset; + } else if (buffer[i + 2] == 1) { + if (buffer[i + 1] == 0 && buffer[i] == 0) { + // We found a start sequence, now check if it was a 3 of 4 byte one. + NaluIndex index = {i, i + 3, 0}; + if (index.start_offset > 0 && buffer[index.start_offset - 1] == 0) + --index.start_offset; - // Update length of previous entry. - auto it = sequences.rbegin(); - if (it != sequences.rend()) - it->payload_size = index.start_offset - it->payload_start_offset; + // Update length of previous entry. + auto it = sequences.rbegin(); + if (it != sequences.rend()) + it->payload_size = index.start_offset - it->payload_start_offset; - sequences.push_back(index); + sequences.push_back(index); + } i += 3; } else {