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

Side by Side Diff: webrtc/modules/rtp_rtcp/source/h264/h264_common.cc

Issue 1979443004: Add H264 bitstream rewriting to limit frame reordering marker in header (Closed) Base URL: https://chromium.googlesource.com/external/webrtc.git@master
Patch Set: Fixed compiler warning on win Created 4 years, 7 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
OLDNEW
(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/modules/rtp_rtcp/source/h264/h264_common.h"
12
13 namespace webrtc {
14
15 const size_t H264Common::kNaluLongStartSequenceSize = 4;
16 const size_t H264Common::kNaluShortStartSequenceSize = 3;
17 const size_t H264Common::kNaluTypeSize = 1;
18 const uint8_t kNaluTypeMask = 0x1F;
19
20 std::vector<H264Common::NaluIndex> H264Common::FindNaluIndices(
21 const uint8_t* buffer,
22 size_t buffer_size) {
23 // This is sorta like Boyer-Moore, but with only the first optimization step:
24 // given a 3-byte sequence we're looking at, if the 3rd byte isn't 1 or 0,
25 // skip ahead to the next 3-byte sequence. 0s and 1s are relatively rare, so
26 // this will skip the majority of reads/checks.
27 RTC_CHECK_GE(buffer_size, kNaluShortStartSequenceSize);
28 std::vector<NaluIndex> sequences;
29 const size_t end = buffer_size - kNaluShortStartSequenceSize;
30 for (size_t i = 0; i < end;) {
31 if (buffer[i + 2] > 1) {
32 i += 3;
33 } else if (buffer[i + 2] == 1 && buffer[i + 1] == 0 && buffer[i] == 0) {
34 // We found a start sequence, now check if it was a 3 of 4 byte one.
35 NaluIndex index = {i, i + 3, 0};
36 if (index.start_offset > 0 && buffer[index.start_offset - 1] == 0)
37 --index.start_offset;
38
39 // Update length of previous entry.
40 auto it = sequences.rbegin();
41 if (it != sequences.rend())
42 it->payload_size = index.start_offset - it->payload_start_offset;
43
44 sequences.push_back(index);
45
46 i += 3;
47 } else {
48 ++i;
49 }
50 }
51
52 // Update length of last entry, if any.
53 auto it = sequences.rbegin();
54 if (it != sequences.rend())
55 it->payload_size = buffer_size - it->payload_start_offset;
56
57 return sequences;
58 }
59
60 H264Common::NaluType webrtc::H264Common::ParseNaluType(uint8_t data) {
61 return static_cast<NaluType>(data & kNaluTypeMask);
62 }
63
64 std::unique_ptr<rtc::Buffer> webrtc::H264Common::ParseRbsp(const uint8_t* data,
65 size_t length) {
66 // Parse out RBSP, which is basically the source buffer minus emulation
67 // bytes (the last byte of a 0x00 0x00 0x03 sequence). RBSP is defined in
68 // section 7.3.1 of the H.264 standard.
69 std::unique_ptr<rtc::Buffer> rbsp_buffer(new rtc::Buffer());
70 const char* sps_bytes = reinterpret_cast<const char*>(data);
71 for (size_t i = 0; i < length;) {
72 // Be careful about over/underflow here. byte_length_ - 3 can underflow, and
73 // i + 3 can overflow, but byte_length_ - i can't, because i < byte_length_
74 // above, and that expression will produce the number of bytes left in
75 // the stream including the byte at i.
76 if (length - i >= 3 && data[i] == 0 && data[i + 1] == 0 &&
77 data[i + 2] == 3) {
78 // Two rbsp bytes + the emulation byte.
79 rbsp_buffer->AppendData(sps_bytes + i, 2);
80 i += 3;
81 } else {
82 // Single rbsp byte.
83 rbsp_buffer->AppendData(sps_bytes[i]);
84 ++i;
85 }
86 }
87 return rbsp_buffer;
88 }
89
90 // Writes bytes into RBSP, adding emulation (0x03) bytes where necessary.
91 void webrtc::H264Common::WriteRbsp(const uint8_t* bytes,
92 size_t length,
93 rtc::Buffer* destination) {
94 static const uint8_t kZerosInStartSequence = 2;
95 static const uint8_t kEmulationByte = 0x03u;
96 size_t num_consecutive_zeros = 0;
97
98 for (size_t i = 0; i < length; ++i) {
99 uint8_t byte = bytes[i];
100 if (byte <= kEmulationByte &&
101 num_consecutive_zeros >= kZerosInStartSequence) {
102 // Need to escape.
103 destination->AppendData(kEmulationByte);
104 num_consecutive_zeros = 0;
105 }
106 destination->AppendData(byte);
107 if (byte == 0) {
108 ++num_consecutive_zeros;
109 } else {
110 num_consecutive_zeros = 0;
111 }
112 }
113 }
114
115 } // namespace webrtc
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698