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

Side by Side Diff: webrtc/modules/rtp_rtcp/source/rtp_format_h264.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: Addressed 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
1 /* 1 /*
2 * Copyright (c) 2014 The WebRTC project authors. All Rights Reserved. 2 * Copyright (c) 2014 The WebRTC project authors. All Rights Reserved.
3 * 3 *
4 * Use of this source code is governed by a BSD-style license 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 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 6 * tree. An additional intellectual property rights grant can be found
7 * in the file PATENTS. All contributing project authors may 7 * in the file PATENTS. All contributing project authors may
8 * be found in the AUTHORS file in the root of the source tree. 8 * be found in the AUTHORS file in the root of the source tree.
9 */ 9 */
10 10
11 #include "webrtc/modules/rtp_rtcp/source/rtp_format_h264.h"
12
11 #include <string.h> 13 #include <string.h>
12 14 #include <vector>
15
16 #include "webrtc/base/checks.h"
13 #include "webrtc/base/logging.h" 17 #include "webrtc/base/logging.h"
14 #include "webrtc/modules/include/module_common_types.h" 18 #include "webrtc/modules/include/module_common_types.h"
15 #include "webrtc/modules/rtp_rtcp/source/byte_io.h" 19 #include "webrtc/modules/rtp_rtcp/source/byte_io.h"
16 #include "webrtc/modules/rtp_rtcp/source/h264_sps_parser.h" 20 #include "webrtc/common_video/h264/sps_vui_rewriter.h"
17 #include "webrtc/modules/rtp_rtcp/source/rtp_format_h264.h" 21 #include "webrtc/common_video/h264/h264_common.h"
22 #include "webrtc/common_video/h264/sps_parser.h"
23 #include "webrtc/system_wrappers/include/metrics.h"
18 24
19 namespace webrtc { 25 namespace webrtc {
20 namespace { 26 namespace {
21 27
22 enum Nalu {
23 kSlice = 1,
24 kIdr = 5,
25 kSei = 6,
26 kSps = 7,
27 kPps = 8,
28 kStapA = 24,
29 kFuA = 28
30 };
31
32 static const size_t kNalHeaderSize = 1; 28 static const size_t kNalHeaderSize = 1;
33 static const size_t kFuAHeaderSize = 2; 29 static const size_t kFuAHeaderSize = 2;
34 static const size_t kLengthFieldSize = 2; 30 static const size_t kLengthFieldSize = 2;
35 static const size_t kStapAHeaderSize = kNalHeaderSize + kLengthFieldSize; 31 static const size_t kStapAHeaderSize = kNalHeaderSize + kLengthFieldSize;
36 32
33 static const char* kSpsValidHistogramName = "WebRTC.Video.H264.SpsValid";
34 enum SpsValidEvent {
35 kReceivedSpsPocOk = 0,
36 kReceivedSpsVuiOk = 1,
37 kReceivedSpsRewritten = 2,
38 kReceivedSpsParseFailure = 3,
39 kSentSpsPocOk = 4,
40 kSentSpsVuiOk = 5,
41 kSentSpsRewritten = 6,
42 kSentSpsParseFailure = 7,
43 kSpsRewrittenMax = 8
44 };
45
37 // Bit masks for FU (A and B) indicators. 46 // Bit masks for FU (A and B) indicators.
38 enum NalDefs { kFBit = 0x80, kNriMask = 0x60, kTypeMask = 0x1F }; 47 enum NalDefs : uint8_t { kFBit = 0x80, kNriMask = 0x60, kTypeMask = 0x1F };
39 48
40 // Bit masks for FU (A and B) headers. 49 // Bit masks for FU (A and B) headers.
41 enum FuDefs { kSBit = 0x80, kEBit = 0x40, kRBit = 0x20 }; 50 enum FuDefs : uint8_t { kSBit = 0x80, kEBit = 0x40, kRBit = 0x20 };
42 51
43 // TODO(pbos): Avoid parsing this here as well as inside the jitter buffer. 52 // TODO(pbos): Avoid parsing this here as well as inside the jitter buffer.
44 bool VerifyStapANaluLengths(const uint8_t* nalu_ptr, size_t length_remaining) { 53 bool ParseStapAStartOffsets(const uint8_t* nalu_ptr,
54 size_t length_remaining,
55 std::vector<size_t>* offsets) {
56 size_t offset = 0;
45 while (length_remaining > 0) { 57 while (length_remaining > 0) {
46 // Buffer doesn't contain room for additional nalu length. 58 // Buffer doesn't contain room for additional nalu length.
47 if (length_remaining < sizeof(uint16_t)) 59 if (length_remaining < sizeof(uint16_t))
48 return false; 60 return false;
49 uint16_t nalu_size = nalu_ptr[0] << 8 | nalu_ptr[1]; 61 uint16_t nalu_size = ByteReader<uint16_t>::ReadBigEndian(nalu_ptr);
50 nalu_ptr += sizeof(uint16_t); 62 nalu_ptr += sizeof(uint16_t);
51 length_remaining -= sizeof(uint16_t); 63 length_remaining -= sizeof(uint16_t);
52 if (nalu_size > length_remaining) 64 if (nalu_size > length_remaining)
53 return false; 65 return false;
54 nalu_ptr += nalu_size; 66 nalu_ptr += nalu_size;
55 length_remaining -= nalu_size; 67 length_remaining -= nalu_size;
68
69 offsets->push_back(offset + kStapAHeaderSize);
70 offset += kLengthFieldSize + nalu_size;
56 } 71 }
57 return true; 72 return true;
58 } 73 }
59 74
60 bool ParseSingleNalu(RtpDepacketizer::ParsedPayload* parsed_payload,
61 const uint8_t* payload_data,
62 size_t payload_data_length) {
63 parsed_payload->type.Video.width = 0;
64 parsed_payload->type.Video.height = 0;
65 parsed_payload->type.Video.codec = kRtpVideoH264;
66 parsed_payload->type.Video.isFirstPacket = true;
67 RTPVideoHeaderH264* h264_header =
68 &parsed_payload->type.Video.codecHeader.H264;
69
70 const uint8_t* nalu_start = payload_data + kNalHeaderSize;
71 size_t nalu_length = payload_data_length - kNalHeaderSize;
72 uint8_t nal_type = payload_data[0] & kTypeMask;
73 if (nal_type == kStapA) {
74 // Skip the StapA header (StapA nal type + length).
75 if (payload_data_length <= kStapAHeaderSize) {
76 LOG(LS_ERROR) << "StapA header truncated.";
77 return false;
78 }
79 if (!VerifyStapANaluLengths(nalu_start, nalu_length)) {
80 LOG(LS_ERROR) << "StapA packet with incorrect NALU packet lengths.";
81 return false;
82 }
83
84 nal_type = payload_data[kStapAHeaderSize] & kTypeMask;
85 nalu_start += kStapAHeaderSize;
86 nalu_length -= kStapAHeaderSize;
87 h264_header->packetization_type = kH264StapA;
88 } else {
89 h264_header->packetization_type = kH264SingleNalu;
90 }
91 h264_header->nalu_type = nal_type;
92
93 // We can read resolution out of sps packets.
94 if (nal_type == kSps) {
95 H264SpsParser parser(nalu_start, nalu_length);
96 if (parser.Parse()) {
97 parsed_payload->type.Video.width = parser.width();
98 parsed_payload->type.Video.height = parser.height();
99 }
100 }
101 switch (nal_type) {
102 case kSps:
103 case kPps:
104 case kSei:
105 case kIdr:
106 parsed_payload->frame_type = kVideoFrameKey;
107 break;
108 default:
109 parsed_payload->frame_type = kVideoFrameDelta;
110 break;
111 }
112 return true;
113 }
114
115 bool ParseFuaNalu(RtpDepacketizer::ParsedPayload* parsed_payload,
116 const uint8_t* payload_data,
117 size_t payload_data_length,
118 size_t* offset) {
119 if (payload_data_length < kFuAHeaderSize) {
120 LOG(LS_ERROR) << "FU-A NAL units truncated.";
121 return false;
122 }
123 uint8_t fnri = payload_data[0] & (kFBit | kNriMask);
124 uint8_t original_nal_type = payload_data[1] & kTypeMask;
125 bool first_fragment = (payload_data[1] & kSBit) > 0;
126
127 uint8_t original_nal_header = fnri | original_nal_type;
128 if (first_fragment) {
129 *offset = kNalHeaderSize;
130 uint8_t* payload = const_cast<uint8_t*>(payload_data + *offset);
131 payload[0] = original_nal_header;
132 } else {
133 *offset = kFuAHeaderSize;
134 }
135
136 if (original_nal_type == kIdr) {
137 parsed_payload->frame_type = kVideoFrameKey;
138 } else {
139 parsed_payload->frame_type = kVideoFrameDelta;
140 }
141 parsed_payload->type.Video.width = 0;
142 parsed_payload->type.Video.height = 0;
143 parsed_payload->type.Video.codec = kRtpVideoH264;
144 parsed_payload->type.Video.isFirstPacket = first_fragment;
145 RTPVideoHeaderH264* h264_header =
146 &parsed_payload->type.Video.codecHeader.H264;
147 h264_header->packetization_type = kH264FuA;
148 h264_header->nalu_type = original_nal_type;
149 return true;
150 }
151 } // namespace 75 } // namespace
152 76
153 RtpPacketizerH264::RtpPacketizerH264(FrameType frame_type, 77 RtpPacketizerH264::RtpPacketizerH264(FrameType frame_type,
154 size_t max_payload_len) 78 size_t max_payload_len)
155 : payload_data_(NULL), 79 : max_payload_len_(max_payload_len) {}
156 payload_size_(0),
157 max_payload_len_(max_payload_len) {
158 }
159 80
160 RtpPacketizerH264::~RtpPacketizerH264() { 81 RtpPacketizerH264::~RtpPacketizerH264() {
161 } 82 }
162 83
84 RtpPacketizerH264::Fragment::Fragment(const uint8_t* buffer, size_t length)
85 : buffer(buffer), length(length) {}
86 RtpPacketizerH264::Fragment::Fragment(const Fragment& fragment)
87 : buffer(fragment.buffer), length(fragment.length) {}
88
163 void RtpPacketizerH264::SetPayloadData( 89 void RtpPacketizerH264::SetPayloadData(
164 const uint8_t* payload_data, 90 const uint8_t* payload_data,
165 size_t payload_size, 91 size_t payload_size,
166 const RTPFragmentationHeader* fragmentation) { 92 const RTPFragmentationHeader* fragmentation) {
167 assert(packets_.empty()); 93 RTC_DCHECK(packets_.empty());
168 assert(fragmentation); 94 RTC_DCHECK(input_fragments_.empty());
169 payload_data_ = payload_data; 95 RTC_DCHECK(fragmentation);
170 payload_size_ = payload_size; 96 for (int i = 0; i < fragmentation->fragmentationVectorSize; ++i) {
171 fragmentation_.CopyFrom(*fragmentation); 97 const uint8_t* buffer =
98 &payload_data[fragmentation->fragmentationOffset[i]];
99 size_t length = fragmentation->fragmentationLength[i];
100
101 bool updated_sps = false;
102 H264::NaluType nalu_type = H264::ParseNaluType(buffer[0]);
103 if (nalu_type == H264::NaluType::kSps) {
104 // Check if stream uses picture order count type 0, and if so rewrite it
105 // to enable faster decoding. Streams in that format incur additional
106 // delay because it allows decode order to differ from render order.
107 // The mechanism used is to rewrite (edit or add) the SPS's VUI to contain
108 // restrictions on the maximum number of reordered pictures. This reduces
109 // latency significantly, though it still adds about a frame of latency to
110 // decoding.
111 // Note that we do this rewriting both here (send side, in order to
112 // protect legacy receive clients) and below in
113 // RtpDepacketizerH264::ParseSingleNalu (receive side, in orderer to
114 // protect us from unknown or legacy send clients).
115
116 // Create temporary RBSP decoded buffer of the payload (exlcuding the
117 // leading nalu type header byte (the SpsParser uses only the payload).
118 std::unique_ptr<rtc::Buffer> rbsp_buffer = H264::ParseRbsp(
119 buffer + H264::kNaluTypeSize, length - H264::kNaluTypeSize);
120 rtc::Optional<SpsParser::SpsState> sps;
121
122 std::unique_ptr<rtc::Buffer> output_buffer(new rtc::Buffer());
123 // Add the type header to the output buffer first, so that the rewriter
124 // can append modified payload on top of that.
125 output_buffer->AppendData(buffer[0]);
126 SpsVuiRewriter::ParseResult result = SpsVuiRewriter::ParseAndRewriteSps(
127 rbsp_buffer->data(), rbsp_buffer->size(), &sps, output_buffer.get());
128
129 switch (result) {
130 case SpsVuiRewriter::ParseResult::kVuiRewritten:
131 input_fragments_.push_back(
132 Fragment(output_buffer->data(), output_buffer->size()));
133 input_fragments_.rbegin()->tmp_buffer = std::move(output_buffer);
134 updated_sps = true;
135 RTC_HISTOGRAM_ENUMERATION(kSpsValidHistogramName,
136 SpsValidEvent::kSentSpsRewritten,
137 SpsValidEvent::kSpsRewrittenMax);
138 break;
139 case SpsVuiRewriter::ParseResult::kPocOk:
140 RTC_HISTOGRAM_ENUMERATION(kSpsValidHistogramName,
141 SpsValidEvent::kSentSpsPocOk,
142 SpsValidEvent::kSpsRewrittenMax);
143 break;
144 case SpsVuiRewriter::ParseResult::kVuiOk:
145 RTC_HISTOGRAM_ENUMERATION(kSpsValidHistogramName,
146 SpsValidEvent::kSentSpsVuiOk,
147 SpsValidEvent::kSpsRewrittenMax);
148 break;
149 case SpsVuiRewriter::ParseResult::kFailure:
150 RTC_HISTOGRAM_ENUMERATION(kSpsValidHistogramName,
151 SpsValidEvent::kSentSpsParseFailure,
152 SpsValidEvent::kSpsRewrittenMax);
153 break;
154 }
155 }
156
157 if (!updated_sps)
158 input_fragments_.push_back(Fragment(buffer, length));
159 }
172 GeneratePackets(); 160 GeneratePackets();
173 } 161 }
174 162
175 void RtpPacketizerH264::GeneratePackets() { 163 void RtpPacketizerH264::GeneratePackets() {
176 for (size_t i = 0; i < fragmentation_.fragmentationVectorSize;) { 164 for (size_t i = 0; i < input_fragments_.size();) {
177 size_t fragment_offset = fragmentation_.fragmentationOffset[i]; 165 if (input_fragments_[i].length > max_payload_len_) {
178 size_t fragment_length = fragmentation_.fragmentationLength[i]; 166 PacketizeFuA(i);
179 if (fragment_length > max_payload_len_) {
180 PacketizeFuA(fragment_offset, fragment_length);
181 ++i; 167 ++i;
182 } else { 168 } else {
183 i = PacketizeStapA(i, fragment_offset, fragment_length); 169 i = PacketizeStapA(i);
184 } 170 }
185 } 171 }
186 } 172 }
187 173
188 void RtpPacketizerH264::PacketizeFuA(size_t fragment_offset, 174 void RtpPacketizerH264::PacketizeFuA(size_t fragment_index) {
189 size_t fragment_length) {
190 // Fragment payload into packets (FU-A). 175 // Fragment payload into packets (FU-A).
191 // Strip out the original header and leave room for the FU-A header. 176 // Strip out the original header and leave room for the FU-A header.
192 fragment_length -= kNalHeaderSize; 177 const Fragment& fragment = input_fragments_[fragment_index];
193 size_t offset = fragment_offset + kNalHeaderSize; 178
179 size_t fragment_length = fragment.length - kNalHeaderSize;
180 size_t offset = kNalHeaderSize;
194 size_t bytes_available = max_payload_len_ - kFuAHeaderSize; 181 size_t bytes_available = max_payload_len_ - kFuAHeaderSize;
195 size_t fragments = 182 const size_t num_fragments =
196 (fragment_length + (bytes_available - 1)) / bytes_available; 183 (fragment_length + (bytes_available - 1)) / bytes_available;
197 size_t avg_size = (fragment_length + fragments - 1) / fragments; 184
185 const size_t avg_size = (fragment_length + num_fragments - 1) / num_fragments;
198 while (fragment_length > 0) { 186 while (fragment_length > 0) {
199 size_t packet_length = avg_size; 187 size_t packet_length = avg_size;
200 if (fragment_length < avg_size) 188 if (fragment_length < avg_size)
201 packet_length = fragment_length; 189 packet_length = fragment_length;
202 uint8_t header = payload_data_[fragment_offset]; 190 packets_.push(PacketUnit(Fragment(fragment.buffer + offset, packet_length),
203 packets_.push(Packet(offset, 191 offset - kNalHeaderSize == 0,
204 packet_length, 192 fragment_length == packet_length, false,
205 offset - kNalHeaderSize == fragment_offset, 193 fragment.buffer[0]));
206 fragment_length == packet_length,
207 false,
208 header));
209 offset += packet_length; 194 offset += packet_length;
210 fragment_length -= packet_length; 195 fragment_length -= packet_length;
211 } 196 }
212 } 197 RTC_CHECK_EQ(0u, fragment_length);
213 198 }
214 int RtpPacketizerH264::PacketizeStapA(size_t fragment_index, 199
215 size_t fragment_offset, 200 size_t RtpPacketizerH264::PacketizeStapA(size_t fragment_index) {
216 size_t fragment_length) {
217 // Aggregate fragments into one packet (STAP-A). 201 // Aggregate fragments into one packet (STAP-A).
218 size_t payload_size_left = max_payload_len_; 202 size_t payload_size_left = max_payload_len_;
219 int aggregated_fragments = 0; 203 int aggregated_fragments = 0;
220 size_t fragment_headers_length = 0; 204 size_t fragment_headers_length = 0;
221 assert(payload_size_left >= fragment_length); 205 const Fragment* fragment = &input_fragments_[fragment_index];
222 while (payload_size_left >= fragment_length + fragment_headers_length) { 206 RTC_CHECK_GE(payload_size_left, fragment->length);
223 assert(fragment_length > 0); 207 while (payload_size_left >= fragment->length + fragment_headers_length) {
224 uint8_t header = payload_data_[fragment_offset]; 208 RTC_CHECK_GT(fragment->length, 0u);
225 packets_.push(Packet(fragment_offset, 209 packets_.push(PacketUnit(*fragment, aggregated_fragments == 0, false, true,
226 fragment_length, 210 fragment->buffer[0]));
227 aggregated_fragments == 0, 211 payload_size_left -= fragment->length;
228 false,
229 true,
230 header));
231 payload_size_left -= fragment_length;
232 payload_size_left -= fragment_headers_length; 212 payload_size_left -= fragment_headers_length;
233 213
234 // Next fragment. 214 // Next fragment.
235 ++fragment_index; 215 ++fragment_index;
236 if (fragment_index == fragmentation_.fragmentationVectorSize) 216 if (fragment_index == input_fragments_.size())
237 break; 217 break;
238 fragment_offset = fragmentation_.fragmentationOffset[fragment_index]; 218 fragment = &input_fragments_[fragment_index];
239 fragment_length = fragmentation_.fragmentationLength[fragment_index];
240 219
241 fragment_headers_length = kLengthFieldSize; 220 fragment_headers_length = kLengthFieldSize;
242 // If we are going to try to aggregate more fragments into this packet 221 // If we are going to try to aggregate more fragments into this packet
243 // we need to add the STAP-A NALU header and a length field for the first 222 // we need to add the STAP-A NALU header and a length field for the first
244 // NALU of this packet. 223 // NALU of this packet.
245 if (aggregated_fragments == 0) 224 if (aggregated_fragments == 0)
246 fragment_headers_length += kNalHeaderSize + kLengthFieldSize; 225 fragment_headers_length += kNalHeaderSize + kLengthFieldSize;
247 ++aggregated_fragments; 226 ++aggregated_fragments;
248 } 227 }
249 packets_.back().last_fragment = true; 228 packets_.back().last_fragment = true;
250 return fragment_index; 229 return fragment_index;
251 } 230 }
252 231
253 bool RtpPacketizerH264::NextPacket(uint8_t* buffer, 232 bool RtpPacketizerH264::NextPacket(uint8_t* buffer,
254 size_t* bytes_to_send, 233 size_t* bytes_to_send,
255 bool* last_packet) { 234 bool* last_packet) {
256 *bytes_to_send = 0; 235 *bytes_to_send = 0;
257 if (packets_.empty()) { 236 if (packets_.empty()) {
258 *bytes_to_send = 0; 237 *bytes_to_send = 0;
259 *last_packet = true; 238 *last_packet = true;
260 return false; 239 return false;
261 } 240 }
262 241
263 Packet packet = packets_.front(); 242 PacketUnit packet = packets_.front();
264 243
265 if (packet.first_fragment && packet.last_fragment) { 244 if (packet.first_fragment && packet.last_fragment) {
266 // Single NAL unit packet. 245 // Single NAL unit packet.
267 *bytes_to_send = packet.size; 246 *bytes_to_send = packet.source_fragment.length;
268 memcpy(buffer, &payload_data_[packet.offset], packet.size); 247 memcpy(buffer, packet.source_fragment.buffer, *bytes_to_send);
269 packets_.pop(); 248 packets_.pop();
270 assert(*bytes_to_send <= max_payload_len_); 249 input_fragments_.pop_front();
250 RTC_CHECK_LE(*bytes_to_send, max_payload_len_);
271 } else if (packet.aggregated) { 251 } else if (packet.aggregated) {
272 NextAggregatePacket(buffer, bytes_to_send); 252 NextAggregatePacket(buffer, bytes_to_send);
273 assert(*bytes_to_send <= max_payload_len_); 253 RTC_CHECK_LE(*bytes_to_send, max_payload_len_);
274 } else { 254 } else {
275 NextFragmentPacket(buffer, bytes_to_send); 255 NextFragmentPacket(buffer, bytes_to_send);
276 assert(*bytes_to_send <= max_payload_len_); 256 RTC_CHECK_LE(*bytes_to_send, max_payload_len_);
277 } 257 }
278 *last_packet = packets_.empty(); 258 *last_packet = packets_.empty();
279 return true; 259 return true;
280 } 260 }
281 261
282 void RtpPacketizerH264::NextAggregatePacket(uint8_t* buffer, 262 void RtpPacketizerH264::NextAggregatePacket(uint8_t* buffer,
283 size_t* bytes_to_send) { 263 size_t* bytes_to_send) {
284 Packet packet = packets_.front(); 264 PacketUnit* packet = &packets_.front();
285 assert(packet.first_fragment); 265 RTC_CHECK(packet->first_fragment);
286 // STAP-A NALU header. 266 // STAP-A NALU header.
287 buffer[0] = (packet.header & (kFBit | kNriMask)) | kStapA; 267 buffer[0] = (packet->header & (kFBit | kNriMask)) | H264::NaluType::kStapA;
288 int index = kNalHeaderSize; 268 int index = kNalHeaderSize;
289 *bytes_to_send += kNalHeaderSize; 269 *bytes_to_send += kNalHeaderSize;
290 while (packet.aggregated) { 270 while (packet->aggregated) {
271 const Fragment& fragment = packet->source_fragment;
291 // Add NAL unit length field. 272 // Add NAL unit length field.
292 ByteWriter<uint16_t>::WriteBigEndian(&buffer[index], packet.size); 273 ByteWriter<uint16_t>::WriteBigEndian(&buffer[index], fragment.length);
293 index += kLengthFieldSize; 274 index += kLengthFieldSize;
294 *bytes_to_send += kLengthFieldSize; 275 *bytes_to_send += kLengthFieldSize;
295 // Add NAL unit. 276 // Add NAL unit.
296 memcpy(&buffer[index], &payload_data_[packet.offset], packet.size); 277 memcpy(&buffer[index], fragment.buffer, fragment.length);
297 index += packet.size; 278 index += fragment.length;
298 *bytes_to_send += packet.size; 279 *bytes_to_send += fragment.length;
299 packets_.pop(); 280 packets_.pop();
300 if (packet.last_fragment) 281 input_fragments_.pop_front();
282 if (packet->last_fragment)
301 break; 283 break;
302 packet = packets_.front(); 284 packet = &packets_.front();
303 } 285 }
304 assert(packet.last_fragment); 286 RTC_CHECK(packet->last_fragment);
305 } 287 }
306 288
307 void RtpPacketizerH264::NextFragmentPacket(uint8_t* buffer, 289 void RtpPacketizerH264::NextFragmentPacket(uint8_t* buffer,
308 size_t* bytes_to_send) { 290 size_t* bytes_to_send) {
309 Packet packet = packets_.front(); 291 PacketUnit* packet = &packets_.front();
310 // NAL unit fragmented over multiple packets (FU-A). 292 // NAL unit fragmented over multiple packets (FU-A).
311 // We do not send original NALU header, so it will be replaced by the 293 // We do not send original NALU header, so it will be replaced by the
312 // FU indicator header of the first packet. 294 // FU indicator header of the first packet.
313 uint8_t fu_indicator = (packet.header & (kFBit | kNriMask)) | kFuA; 295 uint8_t fu_indicator =
296 (packet->header & (kFBit | kNriMask)) | H264::NaluType::kFuA;
314 uint8_t fu_header = 0; 297 uint8_t fu_header = 0;
315 298
316 // S | E | R | 5 bit type. 299 // S | E | R | 5 bit type.
317 fu_header |= (packet.first_fragment ? kSBit : 0); 300 fu_header |= (packet->first_fragment ? kSBit : 0);
318 fu_header |= (packet.last_fragment ? kEBit : 0); 301 fu_header |= (packet->last_fragment ? kEBit : 0);
319 uint8_t type = packet.header & kTypeMask; 302 uint8_t type = packet->header & kTypeMask;
320 fu_header |= type; 303 fu_header |= type;
321 buffer[0] = fu_indicator; 304 buffer[0] = fu_indicator;
322 buffer[1] = fu_header; 305 buffer[1] = fu_header;
323 306
324 if (packet.last_fragment) { 307 const Fragment& fragment = packet->source_fragment;
325 *bytes_to_send = packet.size + kFuAHeaderSize; 308 *bytes_to_send = fragment.length + kFuAHeaderSize;
326 memcpy(buffer + kFuAHeaderSize, &payload_data_[packet.offset], packet.size); 309 memcpy(buffer + kFuAHeaderSize, fragment.buffer, fragment.length);
327 } else { 310 if (packet->last_fragment)
328 *bytes_to_send = packet.size + kFuAHeaderSize; 311 input_fragments_.pop_front();
329 memcpy(buffer + kFuAHeaderSize, &payload_data_[packet.offset], packet.size);
330 }
331 packets_.pop(); 312 packets_.pop();
332 } 313 }
333 314
334 ProtectionType RtpPacketizerH264::GetProtectionType() { 315 ProtectionType RtpPacketizerH264::GetProtectionType() {
335 return kProtectedPacket; 316 return kProtectedPacket;
336 } 317 }
337 318
338 StorageType RtpPacketizerH264::GetStorageType( 319 StorageType RtpPacketizerH264::GetStorageType(
339 uint32_t retransmission_settings) { 320 uint32_t retransmission_settings) {
340 return kAllowRetransmission; 321 return kAllowRetransmission;
341 } 322 }
342 323
343 std::string RtpPacketizerH264::ToString() { 324 std::string RtpPacketizerH264::ToString() {
344 return "RtpPacketizerH264"; 325 return "RtpPacketizerH264";
345 } 326 }
346 327
328 RtpDepacketizerH264::RtpDepacketizerH264() : offset_(0), length_(0) {}
329 RtpDepacketizerH264::~RtpDepacketizerH264() {}
330
347 bool RtpDepacketizerH264::Parse(ParsedPayload* parsed_payload, 331 bool RtpDepacketizerH264::Parse(ParsedPayload* parsed_payload,
348 const uint8_t* payload_data, 332 const uint8_t* payload_data,
349 size_t payload_data_length) { 333 size_t payload_data_length) {
350 assert(parsed_payload != NULL); 334 RTC_CHECK(parsed_payload != nullptr);
351 if (payload_data_length == 0) { 335 if (payload_data_length == 0) {
352 LOG(LS_ERROR) << "Empty payload."; 336 LOG(LS_ERROR) << "Empty payload.";
353 return false; 337 return false;
354 } 338 }
355 339
340 offset_ = 0;
341 length_ = payload_data_length;
342 modified_buffer_.reset();
343
356 uint8_t nal_type = payload_data[0] & kTypeMask; 344 uint8_t nal_type = payload_data[0] & kTypeMask;
357 size_t offset = 0; 345 if (nal_type == H264::NaluType::kFuA) {
358 if (nal_type == kFuA) {
359 // Fragmented NAL units (FU-A). 346 // Fragmented NAL units (FU-A).
360 if (!ParseFuaNalu( 347 if (!ParseFuaNalu(parsed_payload, payload_data))
361 parsed_payload, payload_data, payload_data_length, &offset)) { 348 return false;
362 return false;
363 }
364 } else { 349 } else {
365 // We handle STAP-A and single NALU's the same way here. The jitter buffer 350 // We handle STAP-A and single NALU's the same way here. The jitter buffer
366 // will depacketize the STAP-A into NAL units later. 351 // will depacketize the STAP-A into NAL units later.
367 if (!ParseSingleNalu(parsed_payload, payload_data, payload_data_length)) 352 // TODO(sprang): Parse STAP-A offsets here and store in fragmentation vec.
368 return false; 353 if (!ProcessStapAOrSingleNalu(parsed_payload, payload_data))
369 } 354 return false;
370 355 }
371 parsed_payload->payload = payload_data + offset; 356
372 parsed_payload->payload_length = payload_data_length - offset; 357 const uint8_t* payload =
358 modified_buffer_ ? modified_buffer_->data() : payload_data;
359
360 parsed_payload->payload = payload + offset_;
361 parsed_payload->payload_length = length_;
373 return true; 362 return true;
374 } 363 }
364
365 bool RtpDepacketizerH264::ProcessStapAOrSingleNalu(
366 ParsedPayload* parsed_payload,
367 const uint8_t* payload_data) {
368 parsed_payload->type.Video.width = 0;
369 parsed_payload->type.Video.height = 0;
370 parsed_payload->type.Video.codec = kRtpVideoH264;
371 parsed_payload->type.Video.isFirstPacket = true;
372 RTPVideoHeaderH264* h264_header =
373 &parsed_payload->type.Video.codecHeader.H264;
374
375 const uint8_t* nalu_start = payload_data + kNalHeaderSize;
376 const size_t nalu_length = length_ - kNalHeaderSize;
377 uint8_t nal_type = payload_data[0] & kTypeMask;
378 std::vector<size_t> nalu_start_offsets;
379 if (nal_type == H264::NaluType::kStapA) {
380 // Skip the StapA header (StapA NAL type + length).
381 if (length_ <= kStapAHeaderSize) {
382 LOG(LS_ERROR) << "StapA header truncated.";
383 return false;
384 }
385
386 if (!ParseStapAStartOffsets(nalu_start, nalu_length, &nalu_start_offsets)) {
387 LOG(LS_ERROR) << "StapA packet with incorrect NALU packet lengths.";
388 return false;
389 }
390
391 h264_header->packetization_type = kH264StapA;
392 nal_type = payload_data[kStapAHeaderSize] & kTypeMask;
393 } else {
394 h264_header->packetization_type = kH264SingleNalu;
395 nalu_start_offsets.push_back(0);
396 }
397 h264_header->nalu_type = nal_type;
398 parsed_payload->frame_type = kVideoFrameDelta;
399
400 nalu_start_offsets.push_back(length_ + kLengthFieldSize); // End offset.
401 for (size_t i = 0; i < nalu_start_offsets.size() - 1; ++i) {
402 size_t start_offset = nalu_start_offsets[i];
403 // End offset is actually start offset for next unit, excluding length field
404 // so remove that from this units length.
405 size_t end_offset = nalu_start_offsets[i + 1] - kLengthFieldSize;
406 nal_type = payload_data[start_offset] & kTypeMask;
407 start_offset += H264::kNaluTypeSize;
408
409 if (nal_type == H264::NaluType::kSps) {
410 // Check if VUI is present in SPS and if it needs to be modified to avoid
411 // excessive decoder latency.
412
413 // Copy any previous data first (likely just the first header).
414 std::unique_ptr<rtc::Buffer> output_buffer(new rtc::Buffer());
415 if (start_offset)
416 output_buffer->AppendData(payload_data, start_offset);
417
418 // RBSP decode of payload data.
419 std::unique_ptr<rtc::Buffer> rbsp_buffer = H264::ParseRbsp(
420 &payload_data[start_offset], end_offset - start_offset);
421 rtc::Optional<SpsParser::SpsState> sps;
422
423 SpsVuiRewriter::ParseResult result = SpsVuiRewriter::ParseAndRewriteSps(
424 rbsp_buffer->data(), rbsp_buffer->size(), &sps, output_buffer.get());
425 switch (result) {
426 case SpsVuiRewriter::ParseResult::kVuiRewritten:
427 if (modified_buffer_) {
428 LOG(LS_WARNING) << "More than one H264 SPS NAL units needing "
429 "rewriting found within a single STAP-A packet. "
430 "Keeping the first and rewriting the last.";
431 }
432
433 // Rewrite length field to new SPS size.
434 if (h264_header->packetization_type == kH264StapA) {
435 size_t length_field_offset =
436 start_offset - (H264::kNaluTypeSize + kLengthFieldSize);
437 // Stap-A Length includes payload data and type header.
438 size_t rewritten_size =
439 output_buffer->size() - start_offset + H264::kNaluTypeSize;
440 ByteWriter<uint16_t>::WriteBigEndian(
441 &(*output_buffer)[length_field_offset], rewritten_size);
442 }
443
444 // Append rest of packet.
445 output_buffer->AppendData(&payload_data[end_offset],
446 nalu_length + kNalHeaderSize - end_offset);
447
448 modified_buffer_ = std::move(output_buffer);
449 length_ = modified_buffer_->size();
450
451 RTC_HISTOGRAM_ENUMERATION(kSpsValidHistogramName,
452 SpsValidEvent::kReceivedSpsRewritten,
453 SpsValidEvent::kSpsRewrittenMax);
454 break;
455 case SpsVuiRewriter::ParseResult::kPocOk:
456 RTC_HISTOGRAM_ENUMERATION(kSpsValidHistogramName,
457 SpsValidEvent::kReceivedSpsPocOk,
458 SpsValidEvent::kSpsRewrittenMax);
459 break;
460 case SpsVuiRewriter::ParseResult::kVuiOk:
461 RTC_HISTOGRAM_ENUMERATION(kSpsValidHistogramName,
462 SpsValidEvent::kReceivedSpsVuiOk,
463 SpsValidEvent::kSpsRewrittenMax);
464 break;
465 case SpsVuiRewriter::ParseResult::kFailure:
466 RTC_HISTOGRAM_ENUMERATION(kSpsValidHistogramName,
467 SpsValidEvent::kReceivedSpsParseFailure,
468 SpsValidEvent::kSpsRewrittenMax);
469 break;
470 }
471
472 if (sps) {
473 parsed_payload->type.Video.width = sps->width;
474 parsed_payload->type.Video.height = sps->height;
475 }
476 parsed_payload->frame_type = kVideoFrameKey;
477 } else if (nal_type == H264::NaluType::kPps ||
478 nal_type == H264::NaluType::kSei ||
479 nal_type == H264::NaluType::kIdr) {
480 parsed_payload->frame_type = kVideoFrameKey;
481 }
482 }
483
484 return true;
485 }
486
487 bool RtpDepacketizerH264::ParseFuaNalu(
488 RtpDepacketizer::ParsedPayload* parsed_payload,
489 const uint8_t* payload_data) {
490 if (length_ < kFuAHeaderSize) {
491 LOG(LS_ERROR) << "FU-A NAL units truncated.";
492 return false;
493 }
494 uint8_t fnri = payload_data[0] & (kFBit | kNriMask);
495 uint8_t original_nal_type = payload_data[1] & kTypeMask;
496 bool first_fragment = (payload_data[1] & kSBit) > 0;
497
498 if (first_fragment) {
499 offset_ = 0;
500 length_ -= kNalHeaderSize;
501 uint8_t original_nal_header = fnri | original_nal_type;
502 modified_buffer_.reset(new rtc::Buffer());
503 modified_buffer_->AppendData(payload_data + kNalHeaderSize, length_);
504 (*modified_buffer_)[0] = original_nal_header;
505 } else {
506 offset_ = kFuAHeaderSize;
507 length_ -= kFuAHeaderSize;
508 }
509
510 if (original_nal_type == H264::NaluType::kIdr) {
511 parsed_payload->frame_type = kVideoFrameKey;
512 } else {
513 parsed_payload->frame_type = kVideoFrameDelta;
514 }
515 parsed_payload->type.Video.width = 0;
516 parsed_payload->type.Video.height = 0;
517 parsed_payload->type.Video.codec = kRtpVideoH264;
518 parsed_payload->type.Video.isFirstPacket = first_fragment;
519 RTPVideoHeaderH264* h264_header =
520 &parsed_payload->type.Video.codecHeader.H264;
521 h264_header->packetization_type = kH264FuA;
522 h264_header->nalu_type = original_nal_type;
523 return true;
524 }
525
375 } // namespace webrtc 526 } // namespace webrtc
OLDNEW
« no previous file with comments | « webrtc/modules/rtp_rtcp/source/rtp_format_h264.h ('k') | webrtc/modules/rtp_rtcp/source/rtp_format_h264_unittest.cc » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698