Line data Source code
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 0 : 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 0 : std::vector<NaluIndex> sequences;
25 0 : if (buffer_size < kNaluShortStartSequenceSize)
26 0 : return sequences;
27 :
28 0 : const size_t end = buffer_size - kNaluShortStartSequenceSize;
29 0 : for (size_t i = 0; i < end;) {
30 0 : if (buffer[i + 2] > 1) {
31 0 : i += 3;
32 0 : } else if (buffer[i + 2] == 1 && buffer[i + 1] == 0 && buffer[i] == 0) {
33 : // We found a start sequence, now check if it was a 3 of 4 byte one.
34 0 : NaluIndex index = {i, i + 3, 0};
35 0 : if (index.start_offset > 0 && buffer[index.start_offset - 1] == 0)
36 0 : --index.start_offset;
37 :
38 : // Update length of previous entry.
39 0 : auto it = sequences.rbegin();
40 0 : if (it != sequences.rend())
41 0 : it->payload_size = index.start_offset - it->payload_start_offset;
42 :
43 0 : sequences.push_back(index);
44 :
45 0 : i += 3;
46 : } else {
47 0 : ++i;
48 : }
49 : }
50 :
51 : // Update length of last entry, if any.
52 0 : auto it = sequences.rbegin();
53 0 : if (it != sequences.rend())
54 0 : it->payload_size = buffer_size - it->payload_start_offset;
55 :
56 0 : return sequences;
57 : }
58 :
59 0 : NaluType ParseNaluType(uint8_t data) {
60 0 : return static_cast<NaluType>(data & kNaluTypeMask);
61 : }
62 :
63 0 : std::unique_ptr<rtc::Buffer> ParseRbsp(const uint8_t* data, size_t length) {
64 0 : std::unique_ptr<rtc::Buffer> rbsp_buffer(new rtc::Buffer(0, length));
65 0 : const char* sps_bytes = reinterpret_cast<const char*>(data);
66 0 : for (size_t i = 0; i < length;) {
67 : // 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 : // above, and that expression will produce the number of bytes left in
70 : // the stream including the byte at i.
71 0 : if (length - i >= 3 && data[i] == 0 && data[i + 1] == 0 &&
72 0 : data[i + 2] == 3) {
73 : // Two rbsp bytes + the emulation byte.
74 0 : rbsp_buffer->AppendData(sps_bytes + i, 2);
75 0 : i += 3;
76 : } else {
77 : // Single rbsp byte.
78 0 : rbsp_buffer->AppendData(sps_bytes[i]);
79 0 : ++i;
80 : }
81 : }
82 0 : return rbsp_buffer;
83 : }
84 :
85 0 : void WriteRbsp(const uint8_t* bytes, size_t length, rtc::Buffer* destination) {
86 : static const uint8_t kZerosInStartSequence = 2;
87 : static const uint8_t kEmulationByte = 0x03u;
88 0 : size_t num_consecutive_zeros = 0;
89 0 : destination->EnsureCapacity(destination->size() + length);
90 :
91 0 : for (size_t i = 0; i < length; ++i) {
92 0 : uint8_t byte = bytes[i];
93 0 : if (byte <= kEmulationByte &&
94 : num_consecutive_zeros >= kZerosInStartSequence) {
95 : // Need to escape.
96 0 : destination->AppendData(kEmulationByte);
97 0 : num_consecutive_zeros = 0;
98 : }
99 0 : destination->AppendData(byte);
100 0 : if (byte == 0) {
101 0 : ++num_consecutive_zeros;
102 : } else {
103 0 : num_consecutive_zeros = 0;
104 : }
105 : }
106 0 : }
107 :
108 : } // namespace H264
109 : } // namespace webrtc
|