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 
15 #include <cstddef>
16 #include <cstdint>
17 #include <iterator>
18 #include <memory>
19 #include <utility>
20 #include <vector>
21 
22 #include "absl/types/optional.h"
23 #include "absl/types/variant.h"
24 #include "common_video/h264/h264_common.h"
25 #include "common_video/h264/pps_parser.h"
26 #include "common_video/h264/sps_parser.h"
27 #include "common_video/h264/sps_vui_rewriter.h"
28 #include "modules/rtp_rtcp/source/byte_io.h"
29 #include "modules/rtp_rtcp/source/rtp_packet_to_send.h"
30 #include "rtc_base/checks.h"
31 #include "rtc_base/logging.h"
32 
33 namespace webrtc {
34 namespace {
35 
36 static const size_t kNalHeaderSize = 1;
37 static const size_t kFuAHeaderSize = 2;
38 static const size_t kLengthFieldSize = 2;
39 
40 // Bit masks for FU (A and B) indicators.
41 enum NalDefs : uint8_t { kFBit = 0x80, kNriMask = 0x60, kTypeMask = 0x1F };
42 
43 // Bit masks for FU (A and B) headers.
44 enum FuDefs : uint8_t { kSBit = 0x80, kEBit = 0x40, kRBit = 0x20 };
45 
46 }  // namespace
47 
RtpPacketizerH264(rtc::ArrayView<const uint8_t> payload,PayloadSizeLimits limits,H264PacketizationMode packetization_mode)48 RtpPacketizerH264::RtpPacketizerH264(rtc::ArrayView<const uint8_t> payload,
49                                      PayloadSizeLimits limits,
50                                      H264PacketizationMode packetization_mode)
51     : limits_(limits), num_packets_left_(0) {
52   // Guard against uninitialized memory in packetization_mode.
53   RTC_CHECK(packetization_mode == H264PacketizationMode::NonInterleaved ||
54             packetization_mode == H264PacketizationMode::SingleNalUnit);
55 
56   for (const auto& nalu :
57        H264::FindNaluIndices(payload.data(), payload.size())) {
58     input_fragments_.push_back(
59         payload.subview(nalu.payload_start_offset, nalu.payload_size));
60   }
61 
62   if (!GeneratePackets(packetization_mode)) {
63     // If failed to generate all the packets, discard already generated
64     // packets in case the caller would ignore return value and still try to
65     // call NextPacket().
66     num_packets_left_ = 0;
67     while (!packets_.empty()) {
68       packets_.pop();
69     }
70   }
71 }
72 
73 RtpPacketizerH264::~RtpPacketizerH264() = default;
74 
NumPackets() const75 size_t RtpPacketizerH264::NumPackets() const {
76   return num_packets_left_;
77 }
78 
GeneratePackets(H264PacketizationMode packetization_mode)79 bool RtpPacketizerH264::GeneratePackets(
80     H264PacketizationMode packetization_mode) {
81   for (size_t i = 0; i < input_fragments_.size();) {
82     switch (packetization_mode) {
83       case H264PacketizationMode::SingleNalUnit:
84         if (!PacketizeSingleNalu(i))
85           return false;
86         ++i;
87         break;
88       case H264PacketizationMode::NonInterleaved:
89         int fragment_len = input_fragments_[i].size();
90         int single_packet_capacity = limits_.max_payload_len;
91         if (input_fragments_.size() == 1)
92           single_packet_capacity -= limits_.single_packet_reduction_len;
93         else if (i == 0)
94           single_packet_capacity -= limits_.first_packet_reduction_len;
95         else if (i + 1 == input_fragments_.size())
96           single_packet_capacity -= limits_.last_packet_reduction_len;
97 
98         if (fragment_len > single_packet_capacity) {
99           if (!PacketizeFuA(i))
100             return false;
101           ++i;
102         } else {
103           i = PacketizeStapA(i);
104         }
105         break;
106     }
107   }
108   return true;
109 }
110 
PacketizeFuA(size_t fragment_index)111 bool RtpPacketizerH264::PacketizeFuA(size_t fragment_index) {
112   // Fragment payload into packets (FU-A).
113   rtc::ArrayView<const uint8_t> fragment = input_fragments_[fragment_index];
114 
115   PayloadSizeLimits limits = limits_;
116   // Leave room for the FU-A header.
117   limits.max_payload_len -= kFuAHeaderSize;
118   // Update single/first/last packet reductions unless it is single/first/last
119   // fragment.
120   if (input_fragments_.size() != 1) {
121     // if this fragment is put into a single packet, it might still be the
122     // first or the last packet in the whole sequence of packets.
123     if (fragment_index == input_fragments_.size() - 1) {
124       limits.single_packet_reduction_len = limits_.last_packet_reduction_len;
125     } else if (fragment_index == 0) {
126       limits.single_packet_reduction_len = limits_.first_packet_reduction_len;
127     } else {
128       limits.single_packet_reduction_len = 0;
129     }
130   }
131   if (fragment_index != 0)
132     limits.first_packet_reduction_len = 0;
133   if (fragment_index != input_fragments_.size() - 1)
134     limits.last_packet_reduction_len = 0;
135 
136   // Strip out the original header.
137   size_t payload_left = fragment.size() - kNalHeaderSize;
138   int offset = kNalHeaderSize;
139 
140   std::vector<int> payload_sizes = SplitAboutEqually(payload_left, limits);
141   if (payload_sizes.empty())
142     return false;
143 
144   for (size_t i = 0; i < payload_sizes.size(); ++i) {
145     int packet_length = payload_sizes[i];
146     RTC_CHECK_GT(packet_length, 0);
147     packets_.push(PacketUnit(fragment.subview(offset, packet_length),
148                              /*first_fragment=*/i == 0,
149                              /*last_fragment=*/i == payload_sizes.size() - 1,
150                              false, fragment[0]));
151     offset += packet_length;
152     payload_left -= packet_length;
153   }
154   num_packets_left_ += payload_sizes.size();
155   RTC_CHECK_EQ(0, payload_left);
156   return true;
157 }
158 
PacketizeStapA(size_t fragment_index)159 size_t RtpPacketizerH264::PacketizeStapA(size_t fragment_index) {
160   // Aggregate fragments into one packet (STAP-A).
161   size_t payload_size_left = limits_.max_payload_len;
162   if (input_fragments_.size() == 1)
163     payload_size_left -= limits_.single_packet_reduction_len;
164   else if (fragment_index == 0)
165     payload_size_left -= limits_.first_packet_reduction_len;
166   int aggregated_fragments = 0;
167   size_t fragment_headers_length = 0;
168   rtc::ArrayView<const uint8_t> fragment = input_fragments_[fragment_index];
169   RTC_CHECK_GE(payload_size_left, fragment.size());
170   ++num_packets_left_;
171 
172   auto payload_size_needed = [&] {
173     size_t fragment_size = fragment.size() + fragment_headers_length;
174     if (input_fragments_.size() == 1) {
175       // Single fragment, single packet, payload_size_left already adjusted
176       // with limits_.single_packet_reduction_len.
177       return fragment_size;
178     }
179     if (fragment_index == input_fragments_.size() - 1) {
180       // Last fragment, so StrapA might be the last packet.
181       return fragment_size + limits_.last_packet_reduction_len;
182     }
183     return fragment_size;
184   };
185 
186   while (payload_size_left >= payload_size_needed()) {
187     RTC_CHECK_GT(fragment.size(), 0);
188     packets_.push(PacketUnit(fragment, aggregated_fragments == 0, false, true,
189                              fragment[0]));
190     payload_size_left -= fragment.size();
191     payload_size_left -= fragment_headers_length;
192 
193     fragment_headers_length = kLengthFieldSize;
194     // If we are going to try to aggregate more fragments into this packet
195     // we need to add the STAP-A NALU header and a length field for the first
196     // NALU of this packet.
197     if (aggregated_fragments == 0)
198       fragment_headers_length += kNalHeaderSize + kLengthFieldSize;
199     ++aggregated_fragments;
200 
201     // Next fragment.
202     ++fragment_index;
203     if (fragment_index == input_fragments_.size())
204       break;
205     fragment = input_fragments_[fragment_index];
206   }
207   RTC_CHECK_GT(aggregated_fragments, 0);
208   packets_.back().last_fragment = true;
209   return fragment_index;
210 }
211 
PacketizeSingleNalu(size_t fragment_index)212 bool RtpPacketizerH264::PacketizeSingleNalu(size_t fragment_index) {
213   // Add a single NALU to the queue, no aggregation.
214   size_t payload_size_left = limits_.max_payload_len;
215   if (input_fragments_.size() == 1)
216     payload_size_left -= limits_.single_packet_reduction_len;
217   else if (fragment_index == 0)
218     payload_size_left -= limits_.first_packet_reduction_len;
219   else if (fragment_index + 1 == input_fragments_.size())
220     payload_size_left -= limits_.last_packet_reduction_len;
221   rtc::ArrayView<const uint8_t> fragment = input_fragments_[fragment_index];
222   if (payload_size_left < fragment.size()) {
223     RTC_LOG(LS_ERROR) << "Failed to fit a fragment to packet in SingleNalu "
224                          "packetization mode. Payload size left "
225                       << payload_size_left << ", fragment length "
226                       << fragment.size() << ", packet capacity "
227                       << limits_.max_payload_len;
228     return false;
229   }
230   RTC_CHECK_GT(fragment.size(), 0u);
231   packets_.push(PacketUnit(fragment, true /* first */, true /* last */,
232                            false /* aggregated */, fragment[0]));
233   ++num_packets_left_;
234   return true;
235 }
236 
NextPacket(RtpPacketToSend * rtp_packet)237 bool RtpPacketizerH264::NextPacket(RtpPacketToSend* rtp_packet) {
238   RTC_DCHECK(rtp_packet);
239   if (packets_.empty()) {
240     return false;
241   }
242 
243   PacketUnit packet = packets_.front();
244   if (packet.first_fragment && packet.last_fragment) {
245     // Single NAL unit packet.
246     size_t bytes_to_send = packet.source_fragment.size();
247     uint8_t* buffer = rtp_packet->AllocatePayload(bytes_to_send);
248     memcpy(buffer, packet.source_fragment.data(), bytes_to_send);
249     packets_.pop();
250     input_fragments_.pop_front();
251   } else if (packet.aggregated) {
252     NextAggregatePacket(rtp_packet);
253   } else {
254     NextFragmentPacket(rtp_packet);
255   }
256   rtp_packet->SetMarker(packets_.empty());
257   --num_packets_left_;
258   return true;
259 }
260 
NextAggregatePacket(RtpPacketToSend * rtp_packet)261 void RtpPacketizerH264::NextAggregatePacket(RtpPacketToSend* rtp_packet) {
262   // Reserve maximum available payload, set actual payload size later.
263   size_t payload_capacity = rtp_packet->FreeCapacity();
264   RTC_CHECK_GE(payload_capacity, kNalHeaderSize);
265   uint8_t* buffer = rtp_packet->AllocatePayload(payload_capacity);
266   RTC_DCHECK(buffer);
267   PacketUnit* packet = &packets_.front();
268   RTC_CHECK(packet->first_fragment);
269   // STAP-A NALU header.
270   buffer[0] = (packet->header & (kFBit | kNriMask)) | H264::NaluType::kStapA;
271   size_t index = kNalHeaderSize;
272   bool is_last_fragment = packet->last_fragment;
273   while (packet->aggregated) {
274     rtc::ArrayView<const uint8_t> fragment = packet->source_fragment;
275     RTC_CHECK_LE(index + kLengthFieldSize + fragment.size(), payload_capacity);
276     // Add NAL unit length field.
277     ByteWriter<uint16_t>::WriteBigEndian(&buffer[index], fragment.size());
278     index += kLengthFieldSize;
279     // Add NAL unit.
280     memcpy(&buffer[index], fragment.data(), fragment.size());
281     index += fragment.size();
282     packets_.pop();
283     input_fragments_.pop_front();
284     if (is_last_fragment)
285       break;
286     packet = &packets_.front();
287     is_last_fragment = packet->last_fragment;
288   }
289   RTC_CHECK(is_last_fragment);
290   rtp_packet->SetPayloadSize(index);
291 }
292 
NextFragmentPacket(RtpPacketToSend * rtp_packet)293 void RtpPacketizerH264::NextFragmentPacket(RtpPacketToSend* rtp_packet) {
294   PacketUnit* packet = &packets_.front();
295   // NAL unit fragmented over multiple packets (FU-A).
296   // We do not send original NALU header, so it will be replaced by the
297   // FU indicator header of the first packet.
298   uint8_t fu_indicator =
299       (packet->header & (kFBit | kNriMask)) | H264::NaluType::kFuA;
300   uint8_t fu_header = 0;
301 
302   // S | E | R | 5 bit type.
303   fu_header |= (packet->first_fragment ? kSBit : 0);
304   fu_header |= (packet->last_fragment ? kEBit : 0);
305   uint8_t type = packet->header & kTypeMask;
306   fu_header |= type;
307   rtc::ArrayView<const uint8_t> fragment = packet->source_fragment;
308   uint8_t* buffer =
309       rtp_packet->AllocatePayload(kFuAHeaderSize + fragment.size());
310   buffer[0] = fu_indicator;
311   buffer[1] = fu_header;
312   memcpy(buffer + kFuAHeaderSize, fragment.data(), fragment.size());
313   if (packet->last_fragment)
314     input_fragments_.pop_front();
315   packets_.pop();
316 }
317 
318 }  // namespace webrtc
319