OLD | NEW |
(Empty) | |
| 1 /* |
| 2 * Copyright (c) 2016 The WebRTC project authors. All Rights Reserved. |
| 3 * |
| 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 |
| 6 * tree. An additional intellectual property rights grant can be found |
| 7 * in the file PATENTS. All contributing project authors may |
| 8 * be found in the AUTHORS file in the root of the source tree. |
| 9 */ |
| 10 |
| 11 #include "webrtc/common_video/h264/h264_common.h" |
| 12 |
| 13 namespace webrtc { |
| 14 namespace H264 { |
| 15 |
| 16 const uint8_t kNaluTypeMask = 0x1F; |
| 17 |
| 18 std::vector<NaluIndex> FindNaluIndices(const uint8_t* buffer, |
| 19 size_t buffer_size) { |
| 20 // This is sorta like Boyer-Moore, but with only the first optimization step: |
| 21 // given a 3-byte sequence we're looking at, if the 3rd byte isn't 1 or 0, |
| 22 // skip ahead to the next 3-byte sequence. 0s and 1s are relatively rare, so |
| 23 // this will skip the majority of reads/checks. |
| 24 RTC_CHECK_GE(buffer_size, kNaluShortStartSequenceSize); |
| 25 std::vector<NaluIndex> sequences; |
| 26 const size_t end = buffer_size - kNaluShortStartSequenceSize; |
| 27 for (size_t i = 0; i < end;) { |
| 28 if (buffer[i + 2] > 1) { |
| 29 i += 3; |
| 30 } else if (buffer[i + 2] == 1 && buffer[i + 1] == 0 && buffer[i] == 0) { |
| 31 // We found a start sequence, now check if it was a 3 of 4 byte one. |
| 32 NaluIndex index = {i, i + 3, 0}; |
| 33 if (index.start_offset > 0 && buffer[index.start_offset - 1] == 0) |
| 34 --index.start_offset; |
| 35 |
| 36 // Update length of previous entry. |
| 37 auto it = sequences.rbegin(); |
| 38 if (it != sequences.rend()) |
| 39 it->payload_size = index.start_offset - it->payload_start_offset; |
| 40 |
| 41 sequences.push_back(index); |
| 42 |
| 43 i += 3; |
| 44 } else { |
| 45 ++i; |
| 46 } |
| 47 } |
| 48 |
| 49 // Update length of last entry, if any. |
| 50 auto it = sequences.rbegin(); |
| 51 if (it != sequences.rend()) |
| 52 it->payload_size = buffer_size - it->payload_start_offset; |
| 53 |
| 54 return sequences; |
| 55 } |
| 56 |
| 57 NaluType ParseNaluType(uint8_t data) { |
| 58 return static_cast<NaluType>(data & kNaluTypeMask); |
| 59 } |
| 60 |
| 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()); |
| 63 const char* sps_bytes = reinterpret_cast<const char*>(data); |
| 64 for (size_t i = 0; i < length;) { |
| 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_ |
| 67 // above, and that expression will produce the number of bytes left in |
| 68 // the stream including the byte at i. |
| 69 if (length - i >= 3 && data[i] == 0 && data[i + 1] == 0 && |
| 70 data[i + 2] == 3) { |
| 71 // Two rbsp bytes + the emulation byte. |
| 72 rbsp_buffer->AppendData(sps_bytes + i, 2); |
| 73 i += 3; |
| 74 } else { |
| 75 // Single rbsp byte. |
| 76 rbsp_buffer->AppendData(sps_bytes[i]); |
| 77 ++i; |
| 78 } |
| 79 } |
| 80 return rbsp_buffer; |
| 81 } |
| 82 |
| 83 void WriteRbsp(const uint8_t* bytes, size_t length, rtc::Buffer* destination) { |
| 84 static const uint8_t kZerosInStartSequence = 2; |
| 85 static const uint8_t kEmulationByte = 0x03u; |
| 86 size_t num_consecutive_zeros = 0; |
| 87 |
| 88 for (size_t i = 0; i < length; ++i) { |
| 89 uint8_t byte = bytes[i]; |
| 90 if (byte <= kEmulationByte && |
| 91 num_consecutive_zeros >= kZerosInStartSequence) { |
| 92 // Need to escape. |
| 93 destination->AppendData(kEmulationByte); |
| 94 num_consecutive_zeros = 0; |
| 95 } |
| 96 destination->AppendData(byte); |
| 97 if (byte == 0) { |
| 98 ++num_consecutive_zeros; |
| 99 } else { |
| 100 num_consecutive_zeros = 0; |
| 101 } |
| 102 } |
| 103 } |
| 104 |
| 105 } // namespace H264 |
| 106 } // namespace webrtc |
OLD | NEW |