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

Side by Side Diff: webrtc/modules/rtp_rtcp/source/rtp_format_h264.cc

Issue 2871173008: Fix packetization logic to leave space for extensions in the last packet (Closed)
Patch Set: Implement generic video packetizer unittests Created 3 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
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
(...skipping 61 matching lines...) Expand 10 before | Expand all | Expand 10 after
72 72
73 offsets->push_back(offset + kStapAHeaderSize); 73 offsets->push_back(offset + kStapAHeaderSize);
74 offset += kLengthFieldSize + nalu_size; 74 offset += kLengthFieldSize + nalu_size;
75 } 75 }
76 return true; 76 return true;
77 } 77 }
78 78
79 } // namespace 79 } // namespace
80 80
81 RtpPacketizerH264::RtpPacketizerH264(size_t max_payload_len, 81 RtpPacketizerH264::RtpPacketizerH264(size_t max_payload_len,
82 size_t last_packet_extensions_len,
82 H264PacketizationMode packetization_mode) 83 H264PacketizationMode packetization_mode)
83 : max_payload_len_(max_payload_len), 84 : max_payload_len_(max_payload_len),
85 last_packet_extensions_len_(last_packet_extensions_len),
86 total_packets_(0),
84 packetization_mode_(packetization_mode) { 87 packetization_mode_(packetization_mode) {
85 // Guard against uninitialized memory in packetization_mode. 88 // Guard against uninitialized memory in packetization_mode.
86 RTC_CHECK(packetization_mode == H264PacketizationMode::NonInterleaved || 89 RTC_CHECK(packetization_mode == H264PacketizationMode::NonInterleaved ||
87 packetization_mode == H264PacketizationMode::SingleNalUnit); 90 packetization_mode == H264PacketizationMode::SingleNalUnit);
88 } 91 }
89 92
90 RtpPacketizerH264::~RtpPacketizerH264() { 93 RtpPacketizerH264::~RtpPacketizerH264() {
91 } 94 }
92 95
93 RtpPacketizerH264::Fragment::Fragment(const uint8_t* buffer, size_t length) 96 RtpPacketizerH264::Fragment::Fragment(const uint8_t* buffer, size_t length)
94 : buffer(buffer), length(length) {} 97 : buffer(buffer), length(length) {}
95 RtpPacketizerH264::Fragment::Fragment(const Fragment& fragment) 98 RtpPacketizerH264::Fragment::Fragment(const Fragment& fragment)
96 : buffer(fragment.buffer), length(fragment.length) {} 99 : buffer(fragment.buffer), length(fragment.length) {}
97 100
98 void RtpPacketizerH264::SetPayloadData( 101 size_t RtpPacketizerH264::SetPayloadData(
99 const uint8_t* payload_data, 102 const uint8_t* payload_data,
100 size_t payload_size, 103 size_t payload_size,
101 const RTPFragmentationHeader* fragmentation) { 104 const RTPFragmentationHeader* fragmentation) {
102 RTC_DCHECK(packets_.empty()); 105 RTC_DCHECK(packets_.empty());
103 RTC_DCHECK(input_fragments_.empty()); 106 RTC_DCHECK(input_fragments_.empty());
104 RTC_DCHECK(fragmentation); 107 RTC_DCHECK(fragmentation);
105 for (int i = 0; i < fragmentation->fragmentationVectorSize; ++i) { 108 for (int i = 0; i < fragmentation->fragmentationVectorSize; ++i) {
106 const uint8_t* buffer = 109 const uint8_t* buffer =
107 &payload_data[fragmentation->fragmentationOffset[i]]; 110 &payload_data[fragmentation->fragmentationOffset[i]];
108 size_t length = fragmentation->fragmentationLength[i]; 111 size_t length = fragmentation->fragmentationLength[i];
(...skipping 48 matching lines...) Expand 10 before | Expand all | Expand 10 after
157 SpsValidEvent::kSentSpsParseFailure, 160 SpsValidEvent::kSentSpsParseFailure,
158 SpsValidEvent::kSpsRewrittenMax); 161 SpsValidEvent::kSpsRewrittenMax);
159 break; 162 break;
160 } 163 }
161 } 164 }
162 165
163 if (!updated_sps) 166 if (!updated_sps)
164 input_fragments_.push_back(Fragment(buffer, length)); 167 input_fragments_.push_back(Fragment(buffer, length));
165 } 168 }
166 GeneratePackets(); 169 GeneratePackets();
170 return total_packets_;
167 } 171 }
168 172
169 void RtpPacketizerH264::GeneratePackets() { 173 void RtpPacketizerH264::GeneratePackets() {
170 for (size_t i = 0; i < input_fragments_.size();) { 174 for (size_t i = 0; i < input_fragments_.size();) {
171 switch (packetization_mode_) { 175 switch (packetization_mode_) {
172 case H264PacketizationMode::SingleNalUnit: 176 case H264PacketizationMode::SingleNalUnit:
173 PacketizeSingleNalu(i); 177 PacketizeSingleNalu(i);
174 ++i; 178 ++i;
175 break; 179 break;
176 case H264PacketizationMode::NonInterleaved: 180 case H264PacketizationMode::NonInterleaved:
177 if (input_fragments_[i].length > max_payload_len_) { 181 if (input_fragments_[i].length > max_payload_len_ ||
182 (i + 1 == input_fragments_.size() &&
183 (input_fragments_[i].length + last_packet_extensions_len_ >
184 max_payload_len_))) {
178 PacketizeFuA(i); 185 PacketizeFuA(i);
179 ++i; 186 ++i;
180 } else { 187 } else {
181 i = PacketizeStapA(i); 188 i = PacketizeStapA(i);
182 } 189 }
183 break; 190 break;
184 } 191 }
185 } 192 }
186 } 193 }
187 194
188 void RtpPacketizerH264::PacketizeFuA(size_t fragment_index) { 195 void RtpPacketizerH264::PacketizeFuA(size_t fragment_index) {
189 // Fragment payload into packets (FU-A). 196 // Fragment payload into packets (FU-A).
190 // Strip out the original header and leave room for the FU-A header. 197 // Strip out the original header and leave room for the FU-A header.
191 const Fragment& fragment = input_fragments_[fragment_index]; 198 const Fragment& fragment = input_fragments_[fragment_index];
192 199 bool is_last_fragment = fragment_index + 1 == input_fragments_.size();
193 size_t fragment_length = fragment.length - kNalHeaderSize; 200 size_t fragment_length = fragment.length - kNalHeaderSize;
194 size_t offset = kNalHeaderSize; 201 size_t offset = kNalHeaderSize;
195 size_t bytes_available = max_payload_len_ - kFuAHeaderSize; 202 size_t bytes_available = max_payload_len_ - kFuAHeaderSize;
196 const size_t num_fragments = 203 size_t extra_len = is_last_fragment ? last_packet_extensions_len_ : 0;
197 (fragment_length + (bytes_available - 1)) / bytes_available;
198 204
199 const size_t avg_size = (fragment_length + num_fragments - 1) / num_fragments; 205 size_t num_packets =
206 (fragment_length + extra_len + (bytes_available - 1)) / bytes_available;
207
208 const size_t avg_size =
209 (fragment_length + extra_len + num_packets - 1) / num_packets;
210
200 while (fragment_length > 0) { 211 while (fragment_length > 0) {
201 size_t packet_length = avg_size; 212 size_t packet_length = avg_size;
202 if (fragment_length < avg_size) 213 if (fragment_length <= packet_length) { // Last portion of the payload
203 packet_length = fragment_length; 214 packet_length = fragment_length;
215 // One additional packet may be used for extensions in the last packet.
216 // Together with last payload packet there may be at most 2 of them.
217 RTC_CHECK_LE(num_packets, 2);
218 // Whole payload fits in the first num_packets-1 packets but extra packet
219 // is used for extensions.
220 if (num_packets == 2) {
221 // Leave at least one byte of data for the last packet.
222 packet_length = packet_length - 1;
223 }
224 }
225 RTC_CHECK_GT(packet_length, 0);
204 packets_.push(PacketUnit(Fragment(fragment.buffer + offset, packet_length), 226 packets_.push(PacketUnit(Fragment(fragment.buffer + offset, packet_length),
205 offset - kNalHeaderSize == 0, 227 offset - kNalHeaderSize == 0,
206 fragment_length == packet_length, false, 228 fragment_length == packet_length, false,
207 fragment.buffer[0])); 229 fragment.buffer[0]));
208 offset += packet_length; 230 offset += packet_length;
209 fragment_length -= packet_length; 231 fragment_length -= packet_length;
232 total_packets_++;
233 num_packets--;
210 } 234 }
211 RTC_CHECK_EQ(0, fragment_length); 235 RTC_CHECK_EQ(0, fragment_length);
212 } 236 }
213 237
214 size_t RtpPacketizerH264::PacketizeStapA(size_t fragment_index) { 238 size_t RtpPacketizerH264::PacketizeStapA(size_t fragment_index) {
215 // Aggregate fragments into one packet (STAP-A). 239 // Aggregate fragments into one packet (STAP-A).
216 size_t payload_size_left = max_payload_len_; 240 size_t payload_size_left = max_payload_len_;
217 int aggregated_fragments = 0; 241 int aggregated_fragments = 0;
218 size_t fragment_headers_length = 0; 242 size_t fragment_headers_length = 0;
219 const Fragment* fragment = &input_fragments_[fragment_index]; 243 const Fragment* fragment = &input_fragments_[fragment_index];
220 RTC_CHECK_GE(payload_size_left, fragment->length); 244 RTC_CHECK_GE(payload_size_left, fragment->length);
221 while (payload_size_left >= fragment->length + fragment_headers_length) { 245 total_packets_++;
246 while (payload_size_left >= fragment->length + fragment_headers_length &&
247 (fragment_index + 1 < input_fragments_.size() ||
248 payload_size_left >= fragment->length + fragment_headers_length +
249 last_packet_extensions_len_)) {
222 RTC_CHECK_GT(fragment->length, 0); 250 RTC_CHECK_GT(fragment->length, 0);
223 packets_.push(PacketUnit(*fragment, aggregated_fragments == 0, false, true, 251 packets_.push(PacketUnit(*fragment, aggregated_fragments == 0, false, true,
224 fragment->buffer[0])); 252 fragment->buffer[0]));
225 payload_size_left -= fragment->length; 253 payload_size_left -= fragment->length;
226 payload_size_left -= fragment_headers_length; 254 payload_size_left -= fragment_headers_length;
227 255
228 // Next fragment.
229 ++fragment_index;
230 if (fragment_index == input_fragments_.size())
231 break;
232 fragment = &input_fragments_[fragment_index];
233
234 fragment_headers_length = kLengthFieldSize; 256 fragment_headers_length = kLengthFieldSize;
235 // If we are going to try to aggregate more fragments into this packet 257 // If we are going to try to aggregate more fragments into this packet
236 // we need to add the STAP-A NALU header and a length field for the first 258 // we need to add the STAP-A NALU header and a length field for the first
237 // NALU of this packet. 259 // NALU of this packet.
238 if (aggregated_fragments == 0) 260 if (aggregated_fragments == 0)
239 fragment_headers_length += kNalHeaderSize + kLengthFieldSize; 261 fragment_headers_length += kNalHeaderSize + kLengthFieldSize;
240 ++aggregated_fragments; 262 ++aggregated_fragments;
263
264 // Next fragment.
265 ++fragment_index;
266 if (fragment_index == input_fragments_.size())
267 break;
268 fragment = &input_fragments_[fragment_index];
241 } 269 }
270 RTC_CHECK_GT(aggregated_fragments, 0);
242 packets_.back().last_fragment = true; 271 packets_.back().last_fragment = true;
243 return fragment_index; 272 return fragment_index;
244 } 273 }
245 274
246 void RtpPacketizerH264::PacketizeSingleNalu(size_t fragment_index) { 275 void RtpPacketizerH264::PacketizeSingleNalu(size_t fragment_index) {
247 // Add a single NALU to the queue, no aggregation. 276 // Add a single NALU to the queue, no aggregation.
248 size_t payload_size_left = max_payload_len_; 277 size_t payload_size_left = max_payload_len_;
278 if (fragment_index + 1 == input_fragments_.size())
279 payload_size_left -= last_packet_extensions_len_;
249 const Fragment* fragment = &input_fragments_[fragment_index]; 280 const Fragment* fragment = &input_fragments_[fragment_index];
250 RTC_CHECK_GE(payload_size_left, fragment->length) 281 RTC_CHECK_GE(payload_size_left, fragment->length)
251 << "Payload size left " << payload_size_left << ", fragment length " 282 << "Payload size left " << payload_size_left << ", fragment length "
252 << fragment->length << ", packetization mode " << packetization_mode_; 283 << fragment->length << ", packetization mode " << packetization_mode_;
253 RTC_CHECK_GT(fragment->length, 0u); 284 RTC_CHECK_GT(fragment->length, 0u);
254 packets_.push(PacketUnit(*fragment, true /* first */, true /* last */, 285 packets_.push(PacketUnit(*fragment, true /* first */, true /* last */,
255 false /* aggregated */, fragment->buffer[0])); 286 false /* aggregated */, fragment->buffer[0]));
287 total_packets_++;
256 } 288 }
257 289
258 bool RtpPacketizerH264::NextPacket(RtpPacketToSend* rtp_packet, 290 bool RtpPacketizerH264::NextPacket(RtpPacketToSend* rtp_packet) {
259 bool* last_packet) {
260 RTC_DCHECK(rtp_packet); 291 RTC_DCHECK(rtp_packet);
261 RTC_DCHECK(last_packet);
262 if (packets_.empty()) { 292 if (packets_.empty()) {
263 *last_packet = true;
264 return false; 293 return false;
265 } 294 }
266 295
267 PacketUnit packet = packets_.front(); 296 PacketUnit packet = packets_.front();
268 if (packet.first_fragment && packet.last_fragment) { 297 if (packet.first_fragment && packet.last_fragment) {
269 // Single NAL unit packet. 298 // Single NAL unit packet.
270 size_t bytes_to_send = packet.source_fragment.length; 299 size_t bytes_to_send = packet.source_fragment.length;
271 uint8_t* buffer = rtp_packet->AllocatePayload(bytes_to_send); 300 uint8_t* buffer = rtp_packet->AllocatePayload(bytes_to_send);
272 memcpy(buffer, packet.source_fragment.buffer, bytes_to_send); 301 memcpy(buffer, packet.source_fragment.buffer, bytes_to_send);
273 packets_.pop(); 302 packets_.pop();
274 input_fragments_.pop_front(); 303 input_fragments_.pop_front();
275 } else if (packet.aggregated) { 304 } else if (packet.aggregated) {
276 RTC_CHECK_EQ(H264PacketizationMode::NonInterleaved, packetization_mode_); 305 RTC_CHECK_EQ(H264PacketizationMode::NonInterleaved, packetization_mode_);
277 NextAggregatePacket(rtp_packet); 306 NextAggregatePacket(rtp_packet, total_packets_ == 1);
278 } else { 307 } else {
279 RTC_CHECK_EQ(H264PacketizationMode::NonInterleaved, packetization_mode_); 308 RTC_CHECK_EQ(H264PacketizationMode::NonInterleaved, packetization_mode_);
280 NextFragmentPacket(rtp_packet); 309 NextFragmentPacket(rtp_packet);
281 } 310 }
282 RTC_DCHECK_LE(rtp_packet->payload_size(), max_payload_len_); 311 RTC_DCHECK_LE(rtp_packet->payload_size(), max_payload_len_);
283 *last_packet = packets_.empty(); 312 if (packets_.empty()) {
284 rtp_packet->SetMarker(*last_packet); 313 RTC_DCHECK_LE(rtp_packet->payload_size(),
314 max_payload_len_ - last_packet_extensions_len_);
315 }
316 rtp_packet->SetMarker(packets_.empty());
317 total_packets_--;
285 return true; 318 return true;
286 } 319 }
287 320
288 void RtpPacketizerH264::NextAggregatePacket(RtpPacketToSend* rtp_packet) { 321 void RtpPacketizerH264::NextAggregatePacket(RtpPacketToSend* rtp_packet,
289 uint8_t* buffer = rtp_packet->AllocatePayload(max_payload_len_); 322 bool last) {
323 uint8_t* buffer = rtp_packet->AllocatePayload(
324 last ? max_payload_len_ - last_packet_extensions_len_ : max_payload_len_);
290 RTC_DCHECK(buffer); 325 RTC_DCHECK(buffer);
291 PacketUnit* packet = &packets_.front(); 326 PacketUnit* packet = &packets_.front();
292 RTC_CHECK(packet->first_fragment); 327 RTC_CHECK(packet->first_fragment);
293 // STAP-A NALU header. 328 // STAP-A NALU header.
294 buffer[0] = (packet->header & (kFBit | kNriMask)) | H264::NaluType::kStapA; 329 buffer[0] = (packet->header & (kFBit | kNriMask)) | H264::NaluType::kStapA;
295 size_t index = kNalHeaderSize; 330 size_t index = kNalHeaderSize;
296 bool is_last_fragment = packet->last_fragment; 331 bool is_last_fragment = packet->last_fragment;
297 while (packet->aggregated) { 332 while (packet->aggregated) {
298 const Fragment& fragment = packet->source_fragment; 333 const Fragment& fragment = packet->source_fragment;
299 // Add NAL unit length field. 334 // Add NAL unit length field.
(...skipping 319 matching lines...) Expand 10 before | Expand all | Expand 10 after
619 h264->packetization_type = kH264FuA; 654 h264->packetization_type = kH264FuA;
620 h264->nalu_type = original_nal_type; 655 h264->nalu_type = original_nal_type;
621 if (first_fragment) { 656 if (first_fragment) {
622 h264->nalus[h264->nalus_length] = nalu; 657 h264->nalus[h264->nalus_length] = nalu;
623 h264->nalus_length = 1; 658 h264->nalus_length = 1;
624 } 659 }
625 return true; 660 return true;
626 } 661 }
627 662
628 } // namespace webrtc 663 } // namespace webrtc
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698