Source code

Revision control

Other Tools

1
/*
2
* Copyright (c) 2014 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 "modules/rtp_rtcp/source/rtp_format_h264.h"
12
13
#include <string.h>
14
#include <memory>
15
#include <utility>
16
#include <vector>
17
18
#include "common_video/h264/h264_common.h"
19
#include "common_video/h264/pps_parser.h"
20
#include "common_video/h264/sps_parser.h"
21
#include "common_video/h264/sps_vui_rewriter.h"
22
#include "modules/include/module_common_types.h"
23
#include "modules/rtp_rtcp/source/byte_io.h"
24
#include "modules/rtp_rtcp/source/rtp_packet_to_send.h"
25
#include "rtc_base/checks.h"
26
#include "rtc_base/logging.h"
27
#include "system_wrappers/include/metrics.h"
28
29
namespace webrtc {
30
namespace {
31
32
static const size_t kNalHeaderSize = 1;
33
static const size_t kFuAHeaderSize = 2;
34
static const size_t kLengthFieldSize = 2;
35
static const size_t kStapAHeaderSize = kNalHeaderSize + kLengthFieldSize;
36
37
static const char* kSpsValidHistogramName = "WebRTC.Video.H264.SpsValid";
38
enum SpsValidEvent {
39
kReceivedSpsPocOk = 0,
40
kReceivedSpsVuiOk = 1,
41
kReceivedSpsRewritten = 2,
42
kReceivedSpsParseFailure = 3,
43
kSentSpsPocOk = 4,
44
kSentSpsVuiOk = 5,
45
kSentSpsRewritten = 6,
46
kSentSpsParseFailure = 7,
47
kSpsRewrittenMax = 8
48
};
49
50
// Bit masks for FU (A and B) indicators.
51
enum NalDefs : uint8_t { kFBit = 0x80, kNriMask = 0x60, kTypeMask = 0x1F };
52
53
// Bit masks for FU (A and B) headers.
54
enum FuDefs : uint8_t { kSBit = 0x80, kEBit = 0x40, kRBit = 0x20 };
55
56
// TODO(pbos): Avoid parsing this here as well as inside the jitter buffer.
57
bool ParseStapAStartOffsets(const uint8_t* nalu_ptr,
58
size_t length_remaining,
59
std::vector<size_t>* offsets) {
60
size_t offset = 0;
61
while (length_remaining > 0) {
62
// Buffer doesn't contain room for additional nalu length.
63
if (length_remaining < sizeof(uint16_t))
64
return false;
65
uint16_t nalu_size = ByteReader<uint16_t>::ReadBigEndian(nalu_ptr);
66
nalu_ptr += sizeof(uint16_t);
67
length_remaining -= sizeof(uint16_t);
68
if (nalu_size > length_remaining)
69
return false;
70
nalu_ptr += nalu_size;
71
length_remaining -= nalu_size;
72
73
offsets->push_back(offset + kStapAHeaderSize);
74
offset += kLengthFieldSize + nalu_size;
75
}
76
return true;
77
}
78
79
} // namespace
80
81
RtpPacketizerH264::RtpPacketizerH264(size_t max_payload_len,
82
size_t last_packet_reduction_len,
83
H264PacketizationMode packetization_mode)
84
: max_payload_len_(max_payload_len),
85
last_packet_reduction_len_(last_packet_reduction_len),
86
num_packets_left_(0),
87
packetization_mode_(packetization_mode) {
88
// Guard against uninitialized memory in packetization_mode.
89
RTC_CHECK(packetization_mode == H264PacketizationMode::NonInterleaved ||
90
packetization_mode == H264PacketizationMode::SingleNalUnit);
91
RTC_CHECK_GT(max_payload_len, last_packet_reduction_len);
92
}
93
94
RtpPacketizerH264::~RtpPacketizerH264() {
95
}
96
97
RtpPacketizerH264::Fragment::Fragment(const uint8_t* buffer, size_t length)
98
: buffer(buffer), length(length) {}
99
RtpPacketizerH264::Fragment::Fragment(const Fragment& fragment)
100
: buffer(fragment.buffer), length(fragment.length) {}
101
102
size_t RtpPacketizerH264::SetPayloadData(
103
const uint8_t* payload_data,
104
size_t payload_size,
105
const RTPFragmentationHeader* fragmentation) {
106
RTC_DCHECK(packets_.empty());
107
RTC_DCHECK(input_fragments_.empty());
108
RTC_DCHECK(fragmentation);
109
for (int i = 0; i < fragmentation->fragmentationVectorSize; ++i) {
110
const uint8_t* buffer =
111
&payload_data[fragmentation->fragmentationOffset[i]];
112
size_t length = fragmentation->fragmentationLength[i];
113
114
bool updated_sps = false;
115
H264::NaluType nalu_type = H264::ParseNaluType(buffer[0]);
116
if (nalu_type == H264::NaluType::kSps) {
117
// Check if stream uses picture order count type 0, and if so rewrite it
118
// to enable faster decoding. Streams in that format incur additional
119
// delay because it allows decode order to differ from render order.
120
// The mechanism used is to rewrite (edit or add) the SPS's VUI to contain
121
// restrictions on the maximum number of reordered pictures. This reduces
122
// latency significantly, though it still adds about a frame of latency to
123
// decoding.
124
// Note that we do this rewriting both here (send side, in order to
125
// protect legacy receive clients) and below in
126
// RtpDepacketizerH264::ParseSingleNalu (receive side, in orderer to
127
// protect us from unknown or legacy send clients).
128
129
rtc::Optional<SpsParser::SpsState> sps;
130
131
std::unique_ptr<rtc::Buffer> output_buffer(new rtc::Buffer());
132
// Add the type header to the output buffer first, so that the rewriter
133
// can append modified payload on top of that.
134
output_buffer->AppendData(buffer[0]);
135
SpsVuiRewriter::ParseResult result = SpsVuiRewriter::ParseAndRewriteSps(
136
buffer + H264::kNaluTypeSize, length - H264::kNaluTypeSize, &sps,
137
output_buffer.get());
138
139
switch (result) {
140
case SpsVuiRewriter::ParseResult::kVuiRewritten:
141
input_fragments_.push_back(
142
Fragment(output_buffer->data(), output_buffer->size()));
143
input_fragments_.rbegin()->tmp_buffer = std::move(output_buffer);
144
updated_sps = true;
145
RTC_HISTOGRAM_ENUMERATION(kSpsValidHistogramName,
146
SpsValidEvent::kSentSpsRewritten,
147
SpsValidEvent::kSpsRewrittenMax);
148
break;
149
case SpsVuiRewriter::ParseResult::kPocOk:
150
RTC_HISTOGRAM_ENUMERATION(kSpsValidHistogramName,
151
SpsValidEvent::kSentSpsPocOk,
152
SpsValidEvent::kSpsRewrittenMax);
153
break;
154
case SpsVuiRewriter::ParseResult::kVuiOk:
155
RTC_HISTOGRAM_ENUMERATION(kSpsValidHistogramName,
156
SpsValidEvent::kSentSpsVuiOk,
157
SpsValidEvent::kSpsRewrittenMax);
158
break;
159
case SpsVuiRewriter::ParseResult::kFailure:
160
RTC_HISTOGRAM_ENUMERATION(kSpsValidHistogramName,
161
SpsValidEvent::kSentSpsParseFailure,
162
SpsValidEvent::kSpsRewrittenMax);
163
break;
164
}
165
}
166
167
if (!updated_sps)
168
input_fragments_.push_back(Fragment(buffer, length));
169
}
170
GeneratePackets();
171
return num_packets_left_;
172
}
173
174
void RtpPacketizerH264::GeneratePackets() {
175
for (size_t i = 0; i < input_fragments_.size();) {
176
switch (packetization_mode_) {
177
case H264PacketizationMode::SingleNalUnit:
178
PacketizeSingleNalu(i);
179
++i;
180
break;
181
case H264PacketizationMode::NonInterleaved:
182
size_t fragment_len = input_fragments_[i].length;
183
if (i + 1 == input_fragments_.size()) {
184
// Pretend that last fragment is larger instead of making last packet
185
// smaller.
186
fragment_len += last_packet_reduction_len_;
187
}
188
if (fragment_len > max_payload_len_) {
189
PacketizeFuA(i);
190
++i;
191
} else {
192
i = PacketizeStapA(i);
193
}
194
break;
195
}
196
}
197
}
198
199
void RtpPacketizerH264::PacketizeFuA(size_t fragment_index) {
200
// Fragment payload into packets (FU-A).
201
// Strip out the original header and leave room for the FU-A header.
202
const Fragment& fragment = input_fragments_[fragment_index];
203
bool is_last_fragment = fragment_index + 1 == input_fragments_.size();
204
size_t payload_left = fragment.length - kNalHeaderSize;
205
size_t offset = kNalHeaderSize;
206
size_t per_packet_capacity = max_payload_len_ - kFuAHeaderSize;
207
208
// Instead of making the last packet smaller we pretend that all packets are
209
// of the same size but we write additional virtual payload to the last
210
// packet.
211
size_t extra_len = is_last_fragment ? last_packet_reduction_len_ : 0;
212
213
// Integer divisions with rounding up. Minimal number of packets to fit all
214
// payload and virtual payload.
215
size_t num_packets = (payload_left + extra_len + (per_packet_capacity - 1)) /
216
per_packet_capacity;
217
// Bytes per packet. Average rounded down.
218
size_t payload_per_packet = (payload_left + extra_len) / num_packets;
219
// We make several first packets to be 1 bytes smaller than the rest.
220
// i.e 14 bytes splitted in 4 packets would be 3+3+4+4.
221
size_t num_larger_packets = (payload_left + extra_len) % num_packets;
222
223
num_packets_left_ += num_packets;
224
while (payload_left > 0) {
225
// Increase payload per packet at the right time.
226
if (num_packets == num_larger_packets)
227
++payload_per_packet;
228
size_t packet_length = payload_per_packet;
229
if (payload_left <= packet_length) { // Last portion of the payload
230
packet_length = payload_left;
231
// One additional packet may be used for extensions in the last packet.
232
// Together with last payload packet there may be at most 2 of them.
233
RTC_DCHECK_LE(num_packets, 2);
234
if (num_packets == 2) {
235
// Whole payload fits in the first num_packets-1 packets but extra
236
// packet is used for virtual payload. Leave at least one byte of data
237
// for the last packet.
238
--packet_length;
239
}
240
}
241
RTC_CHECK_GT(packet_length, 0);
242
packets_.push(PacketUnit(Fragment(fragment.buffer + offset, packet_length),
243
offset - kNalHeaderSize == 0,
244
payload_left == packet_length, false,
245
fragment.buffer[0]));
246
offset += packet_length;
247
payload_left -= packet_length;
248
--num_packets;
249
}
250
RTC_CHECK_EQ(0, payload_left);
251
}
252
253
size_t RtpPacketizerH264::PacketizeStapA(size_t fragment_index) {
254
// Aggregate fragments into one packet (STAP-A).
255
size_t payload_size_left = max_payload_len_;
256
int aggregated_fragments = 0;
257
size_t fragment_headers_length = 0;
258
const Fragment* fragment = &input_fragments_[fragment_index];
259
RTC_CHECK_GE(payload_size_left, fragment->length);
260
++num_packets_left_;
261
while (payload_size_left >= fragment->length + fragment_headers_length &&
262
(fragment_index + 1 < input_fragments_.size() ||
263
payload_size_left >= fragment->length + fragment_headers_length +
264
last_packet_reduction_len_)) {
265
RTC_CHECK_GT(fragment->length, 0);
266
packets_.push(PacketUnit(*fragment, aggregated_fragments == 0, false, true,
267
fragment->buffer[0]));
268
payload_size_left -= fragment->length;
269
payload_size_left -= fragment_headers_length;
270
271
fragment_headers_length = kLengthFieldSize;
272
// If we are going to try to aggregate more fragments into this packet
273
// we need to add the STAP-A NALU header and a length field for the first
274
// NALU of this packet.
275
if (aggregated_fragments == 0)
276
fragment_headers_length += kNalHeaderSize + kLengthFieldSize;
277
++aggregated_fragments;
278
279
// Next fragment.
280
++fragment_index;
281
if (fragment_index == input_fragments_.size())
282
break;
283
fragment = &input_fragments_[fragment_index];
284
}
285
RTC_CHECK_GT(aggregated_fragments, 0);
286
packets_.back().last_fragment = true;
287
return fragment_index;
288
}
289
290
void RtpPacketizerH264::PacketizeSingleNalu(size_t fragment_index) {
291
// Add a single NALU to the queue, no aggregation.
292
size_t payload_size_left = max_payload_len_;
293
if (fragment_index + 1 == input_fragments_.size())
294
payload_size_left -= last_packet_reduction_len_;
295
const Fragment* fragment = &input_fragments_[fragment_index];
296
RTC_CHECK_GE(payload_size_left, fragment->length)
297
<< "Payload size left " << payload_size_left << ", fragment length "
298
<< fragment->length << ", packetization mode " << packetization_mode_;
299
RTC_CHECK_GT(fragment->length, 0u);
300
packets_.push(PacketUnit(*fragment, true /* first */, true /* last */,
301
false /* aggregated */, fragment->buffer[0]));
302
++num_packets_left_;
303
}
304
305
bool RtpPacketizerH264::NextPacket(RtpPacketToSend* rtp_packet) {
306
RTC_DCHECK(rtp_packet);
307
if (packets_.empty()) {
308
return false;
309
}
310
311
PacketUnit packet = packets_.front();
312
if (packet.first_fragment && packet.last_fragment) {
313
// Single NAL unit packet.
314
size_t bytes_to_send = packet.source_fragment.length;
315
uint8_t* buffer = rtp_packet->AllocatePayload(bytes_to_send);
316
memcpy(buffer, packet.source_fragment.buffer, bytes_to_send);
317
packets_.pop();
318
input_fragments_.pop_front();
319
} else if (packet.aggregated) {
320
RTC_CHECK_EQ(H264PacketizationMode::NonInterleaved, packetization_mode_);
321
bool is_last_packet = num_packets_left_ == 1;
322
NextAggregatePacket(rtp_packet, is_last_packet);
323
} else {
324
RTC_CHECK_EQ(H264PacketizationMode::NonInterleaved, packetization_mode_);
325
NextFragmentPacket(rtp_packet);
326
}
327
RTC_DCHECK_LE(rtp_packet->payload_size(), max_payload_len_);
328
if (packets_.empty()) {
329
RTC_DCHECK_LE(rtp_packet->payload_size(),
330
max_payload_len_ - last_packet_reduction_len_);
331
}
332
rtp_packet->SetMarker(packets_.empty());
333
--num_packets_left_;
334
return true;
335
}
336
337
void RtpPacketizerH264::NextAggregatePacket(RtpPacketToSend* rtp_packet,
338
bool last) {
339
uint8_t* buffer = rtp_packet->AllocatePayload(
340
last ? max_payload_len_ - last_packet_reduction_len_ : max_payload_len_);
341
RTC_DCHECK(buffer);
342
PacketUnit* packet = &packets_.front();
343
RTC_CHECK(packet->first_fragment);
344
// STAP-A NALU header.
345
buffer[0] = (packet->header & (kFBit | kNriMask)) | H264::NaluType::kStapA;
346
size_t index = kNalHeaderSize;
347
bool is_last_fragment = packet->last_fragment;
348
while (packet->aggregated) {
349
const Fragment& fragment = packet->source_fragment;
350
// Add NAL unit length field.
351
ByteWriter<uint16_t>::WriteBigEndian(&buffer[index], fragment.length);
352
index += kLengthFieldSize;
353
// Add NAL unit.
354
memcpy(&buffer[index], fragment.buffer, fragment.length);
355
index += fragment.length;
356
packets_.pop();
357
input_fragments_.pop_front();
358
if (is_last_fragment)
359
break;
360
packet = &packets_.front();
361
is_last_fragment = packet->last_fragment;
362
}
363
RTC_CHECK(is_last_fragment);
364
rtp_packet->SetPayloadSize(index);
365
}
366
367
void RtpPacketizerH264::NextFragmentPacket(RtpPacketToSend* rtp_packet) {
368
PacketUnit* packet = &packets_.front();
369
// NAL unit fragmented over multiple packets (FU-A).
370
// We do not send original NALU header, so it will be replaced by the
371
// FU indicator header of the first packet.
372
uint8_t fu_indicator =
373
(packet->header & (kFBit | kNriMask)) | H264::NaluType::kFuA;
374
uint8_t fu_header = 0;
375
376
// S | E | R | 5 bit type.
377
fu_header |= (packet->first_fragment ? kSBit : 0);
378
fu_header |= (packet->last_fragment ? kEBit : 0);
379
uint8_t type = packet->header & kTypeMask;
380
fu_header |= type;
381
const Fragment& fragment = packet->source_fragment;
382
uint8_t* buffer =
383
rtp_packet->AllocatePayload(kFuAHeaderSize + fragment.length);
384
buffer[0] = fu_indicator;
385
buffer[1] = fu_header;
386
memcpy(buffer + kFuAHeaderSize, fragment.buffer, fragment.length);
387
if (packet->last_fragment)
388
input_fragments_.pop_front();
389
packets_.pop();
390
}
391
392
std::string RtpPacketizerH264::ToString() {
393
return "RtpPacketizerH264";
394
}
395
396
RtpDepacketizerH264::RtpDepacketizerH264() : offset_(0), length_(0) {}
397
RtpDepacketizerH264::~RtpDepacketizerH264() {}
398
399
bool RtpDepacketizerH264::Parse(ParsedPayload* parsed_payload,
400
const uint8_t* payload_data,
401
size_t payload_data_length) {
402
RTC_CHECK(parsed_payload != nullptr);
403
if (payload_data_length == 0) {
404
RTC_LOG(LS_ERROR) << "Empty payload.";
405
return false;
406
}
407
408
offset_ = 0;
409
length_ = payload_data_length;
410
modified_buffer_.reset();
411
412
uint8_t nal_type = payload_data[0] & kTypeMask;
413
parsed_payload->type.Video.codecHeader.H264.nalus_length = 0;
414
if (nal_type == H264::NaluType::kFuA) {
415
// Fragmented NAL units (FU-A).
416
if (!ParseFuaNalu(parsed_payload, payload_data))
417
return false;
418
} else {
419
// We handle STAP-A and single NALU's the same way here. The jitter buffer
420
// will depacketize the STAP-A into NAL units later.
421
// TODO(sprang): Parse STAP-A offsets here and store in fragmentation vec.
422
if (!ProcessStapAOrSingleNalu(parsed_payload, payload_data))
423
return false;
424
}
425
426
const uint8_t* payload =
427
modified_buffer_ ? modified_buffer_->data() : payload_data;
428
429
parsed_payload->payload = payload + offset_;
430
parsed_payload->payload_length = length_;
431
return true;
432
}
433
434
bool RtpDepacketizerH264::ProcessStapAOrSingleNalu(
435
ParsedPayload* parsed_payload,
436
const uint8_t* payload_data) {
437
parsed_payload->type.Video.width = 0;
438
parsed_payload->type.Video.height = 0;
439
parsed_payload->type.Video.codec = kRtpVideoH264;
440
parsed_payload->type.Video.is_first_packet_in_frame = true;
441
RTPVideoHeaderH264* h264_header =
442
&parsed_payload->type.Video.codecHeader.H264;
443
444
const uint8_t* nalu_start = payload_data + kNalHeaderSize;
445
const size_t nalu_length = length_ - kNalHeaderSize;
446
uint8_t nal_type = payload_data[0] & kTypeMask;
447
std::vector<size_t> nalu_start_offsets;
448
if (nal_type == H264::NaluType::kStapA) {
449
// Skip the StapA header (StapA NAL type + length).
450
if (length_ <= kStapAHeaderSize) {
451
RTC_LOG(LS_ERROR) << "StapA header truncated.";
452
return false;
453
}
454
455
if (!ParseStapAStartOffsets(nalu_start, nalu_length, &nalu_start_offsets)) {
456
RTC_LOG(LS_ERROR) << "StapA packet with incorrect NALU packet lengths.";
457
return false;
458
}
459
460
h264_header->packetization_type = kH264StapA;
461
nal_type = payload_data[kStapAHeaderSize] & kTypeMask;
462
} else {
463
h264_header->packetization_type = kH264SingleNalu;
464
nalu_start_offsets.push_back(0);
465
}
466
h264_header->nalu_type = nal_type;
467
parsed_payload->frame_type = kVideoFrameDelta;
468
469
nalu_start_offsets.push_back(length_ + kLengthFieldSize); // End offset.
470
for (size_t i = 0; i < nalu_start_offsets.size() - 1; ++i) {
471
size_t start_offset = nalu_start_offsets[i];
472
// End offset is actually start offset for next unit, excluding length field
473
// so remove that from this units length.
474
size_t end_offset = nalu_start_offsets[i + 1] - kLengthFieldSize;
475
if (end_offset - start_offset < H264::kNaluTypeSize) {
476
RTC_LOG(LS_ERROR) << "STAP-A packet too short";
477
return false;
478
}
479
480
NaluInfo nalu;
481
nalu.type = payload_data[start_offset] & kTypeMask;
482
nalu.sps_id = -1;
483
nalu.pps_id = -1;
484
start_offset += H264::kNaluTypeSize;
485
486
switch (nalu.type) {
487
case H264::NaluType::kSps: {
488
// Check if VUI is present in SPS and if it needs to be modified to
489
// avoid
490
// excessive decoder latency.
491
492
// Copy any previous data first (likely just the first header).
493
std::unique_ptr<rtc::Buffer> output_buffer(new rtc::Buffer());
494
if (start_offset)
495
output_buffer->AppendData(payload_data, start_offset);
496
497
rtc::Optional<SpsParser::SpsState> sps;
498
499
SpsVuiRewriter::ParseResult result = SpsVuiRewriter::ParseAndRewriteSps(
500
&payload_data[start_offset], end_offset - start_offset, &sps,
501
output_buffer.get());
502
switch (result) {
503
case SpsVuiRewriter::ParseResult::kVuiRewritten:
504
if (modified_buffer_) {
505
RTC_LOG(LS_WARNING)
506
<< "More than one H264 SPS NAL units needing "
507
"rewriting found within a single STAP-A packet. "
508
"Keeping the first and rewriting the last.";
509
}
510
511
// Rewrite length field to new SPS size.
512
if (h264_header->packetization_type == kH264StapA) {
513
size_t length_field_offset =
514
start_offset - (H264::kNaluTypeSize + kLengthFieldSize);
515
// Stap-A Length includes payload data and type header.
516
size_t rewritten_size =
517
output_buffer->size() - start_offset + H264::kNaluTypeSize;
518
ByteWriter<uint16_t>::WriteBigEndian(
519
&(*output_buffer)[length_field_offset], rewritten_size);
520
}
521
522
// Append rest of packet.
523
output_buffer->AppendData(
524
&payload_data[end_offset],
525
nalu_length + kNalHeaderSize - end_offset);
526
527
modified_buffer_ = std::move(output_buffer);
528
length_ = modified_buffer_->size();
529
530
RTC_HISTOGRAM_ENUMERATION(kSpsValidHistogramName,
531
SpsValidEvent::kReceivedSpsRewritten,
532
SpsValidEvent::kSpsRewrittenMax);
533
break;
534
case SpsVuiRewriter::ParseResult::kPocOk:
535
RTC_HISTOGRAM_ENUMERATION(kSpsValidHistogramName,
536
SpsValidEvent::kReceivedSpsPocOk,
537
SpsValidEvent::kSpsRewrittenMax);
538
break;
539
case SpsVuiRewriter::ParseResult::kVuiOk:
540
RTC_HISTOGRAM_ENUMERATION(kSpsValidHistogramName,
541
SpsValidEvent::kReceivedSpsVuiOk,
542
SpsValidEvent::kSpsRewrittenMax);
543
break;
544
case SpsVuiRewriter::ParseResult::kFailure:
545
RTC_HISTOGRAM_ENUMERATION(kSpsValidHistogramName,
546
SpsValidEvent::kReceivedSpsParseFailure,
547
SpsValidEvent::kSpsRewrittenMax);
548
break;
549
}
550
551
if (sps) {
552
parsed_payload->type.Video.width = sps->width;
553
parsed_payload->type.Video.height = sps->height;
554
nalu.sps_id = sps->id;
555
} else {
556
RTC_LOG(LS_WARNING) << "Failed to parse SPS id from SPS slice.";
557
}
558
parsed_payload->frame_type = kVideoFrameKey;
559
break;
560
}
561
case H264::NaluType::kPps: {
562
uint32_t pps_id;
563
uint32_t sps_id;
564
if (PpsParser::ParsePpsIds(&payload_data[start_offset],
565
end_offset - start_offset, &pps_id,
566
&sps_id)) {
567
nalu.pps_id = pps_id;
568
nalu.sps_id = sps_id;
569
} else {
570
RTC_LOG(LS_WARNING)
571
<< "Failed to parse PPS id and SPS id from PPS slice.";
572
}
573
break;
574
}
575
case H264::NaluType::kIdr:
576
parsed_payload->frame_type = kVideoFrameKey;
577
FALLTHROUGH();
578
case H264::NaluType::kSlice: {
579
rtc::Optional<uint32_t> pps_id = PpsParser::ParsePpsIdFromSlice(
580
&payload_data[start_offset], end_offset - start_offset);
581
if (pps_id) {
582
nalu.pps_id = *pps_id;
583
} else {
584
RTC_LOG(LS_WARNING) << "Failed to parse PPS id from slice of type: "
585
<< static_cast<int>(nalu.type);
586
}
587
break;
588
}
589
// Slices below don't contain SPS or PPS ids.
590
case H264::NaluType::kAud:
591
case H264::NaluType::kEndOfSequence:
592
case H264::NaluType::kEndOfStream:
593
case H264::NaluType::kFiller:
594
case H264::NaluType::kSei:
595
break;
596
case H264::NaluType::kStapA:
597
case H264::NaluType::kFuA:
598
RTC_LOG(LS_WARNING) << "Unexpected STAP-A or FU-A received.";
599
return false;
600
}
601
RTPVideoHeaderH264* h264 = &parsed_payload->type.Video.codecHeader.H264;
602
if (h264->nalus_length == kMaxNalusPerPacket) {
603
RTC_LOG(LS_WARNING)
604
<< "Received packet containing more than " << kMaxNalusPerPacket
605
<< " NAL units. Will not keep track sps and pps ids for all of them.";
606
} else {
607
h264->nalus[h264->nalus_length++] = nalu;
608
}
609
}
610
611
return true;
612
}
613
614
bool RtpDepacketizerH264::ParseFuaNalu(
615
RtpDepacketizer::ParsedPayload* parsed_payload,
616
const uint8_t* payload_data) {
617
if (length_ < kFuAHeaderSize) {
618
RTC_LOG(LS_ERROR) << "FU-A NAL units truncated.";
619
return false;
620
}
621
uint8_t fnri = payload_data[0] & (kFBit | kNriMask);
622
uint8_t original_nal_type = payload_data[1] & kTypeMask;
623
bool first_fragment = (payload_data[1] & kSBit) > 0;
624
NaluInfo nalu;
625
nalu.type = original_nal_type;
626
nalu.sps_id = -1;
627
nalu.pps_id = -1;
628
if (first_fragment) {
629
offset_ = 0;
630
length_ -= kNalHeaderSize;
631
rtc::Optional<uint32_t> pps_id = PpsParser::ParsePpsIdFromSlice(
632
payload_data + 2 * kNalHeaderSize, length_ - kNalHeaderSize);
633
if (pps_id) {
634
nalu.pps_id = *pps_id;
635
} else {
636
RTC_LOG(LS_WARNING)
637
<< "Failed to parse PPS from first fragment of FU-A NAL "
638
"unit with original type: "
639
<< static_cast<int>(nalu.type);
640
}
641
uint8_t original_nal_header = fnri | original_nal_type;
642
modified_buffer_.reset(new rtc::Buffer());
643
modified_buffer_->AppendData(payload_data + kNalHeaderSize, length_);
644
(*modified_buffer_)[0] = original_nal_header;
645
} else {
646
offset_ = kFuAHeaderSize;
647
length_ -= kFuAHeaderSize;
648
}
649
650
if (original_nal_type == H264::NaluType::kIdr) {
651
parsed_payload->frame_type = kVideoFrameKey;
652
} else {
653
parsed_payload->frame_type = kVideoFrameDelta;
654
}
655
parsed_payload->type.Video.width = 0;
656
parsed_payload->type.Video.height = 0;
657
parsed_payload->type.Video.codec = kRtpVideoH264;
658
parsed_payload->type.Video.is_first_packet_in_frame = first_fragment;
659
RTPVideoHeaderH264* h264 = &parsed_payload->type.Video.codecHeader.H264;
660
h264->packetization_type = kH264FuA;
661
h264->nalu_type = original_nal_type;
662
if (first_fragment) {
663
h264->nalus[h264->nalus_length] = nalu;
664
h264->nalus_length = 1;
665
}
666
return true;
667
}
668
669
} // namespace webrtc