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

Side by Side Diff: webrtc/common_video/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: Comments Created 4 years, 6 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/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 // Parse out RBSP, which is basically the source buffer minus emulation
63 // bytes (the last byte of a 0x00 0x00 0x03 sequence). RBSP is defined in
64 // section 7.3.1 of the H.264 standard.
65 std::unique_ptr<rtc::Buffer> rbsp_buffer(new rtc::Buffer());
66 const char* sps_bytes = reinterpret_cast<const char*>(data);
67 for (size_t i = 0; i < length;) {
68 // Be careful about over/underflow here. byte_length_ - 3 can underflow, and
69 // i + 3 can overflow, but byte_length_ - i can't, because i < byte_length_
70 // above, and that expression will produce the number of bytes left in
71 // the stream including the byte at i.
72 if (length - i >= 3 && data[i] == 0 && data[i + 1] == 0 &&
73 data[i + 2] == 3) {
74 // Two rbsp bytes + the emulation byte.
75 rbsp_buffer->AppendData(sps_bytes + i, 2);
76 i += 3;
77 } else {
78 // Single rbsp byte.
79 rbsp_buffer->AppendData(sps_bytes[i]);
80 ++i;
81 }
82 }
83 return rbsp_buffer;
84 }
85
86 // Writes bytes into RBSP, adding emulation (0x03) bytes where necessary.
noahric 2016/06/01 17:56:43 Could you either quote the section of the standard
sprang_webrtc 2016/06/02 08:53:14 Added comments in the header file.
87 void WriteRbsp(const uint8_t* bytes, size_t length, rtc::Buffer* destination) {
88 static const uint8_t kZerosInStartSequence = 2;
89 static const uint8_t kEmulationByte = 0x03u;
90 size_t num_consecutive_zeros = 0;
91
92 for (size_t i = 0; i < length; ++i) {
93 uint8_t byte = bytes[i];
94 if (byte <= kEmulationByte &&
95 num_consecutive_zeros >= kZerosInStartSequence) {
96 // Need to escape.
97 destination->AppendData(kEmulationByte);
98 num_consecutive_zeros = 0;
99 }
100 destination->AppendData(byte);
101 if (byte == 0) {
102 ++num_consecutive_zeros;
103 } else {
104 num_consecutive_zeros = 0;
105 }
106 }
107 }
108
109 } // namespace H264
110 } // namespace webrtc
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698