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

Side by Side Diff: webrtc/modules/video_coding/codecs/h264/h264_video_toolbox_nalu.cc

Issue 2356793002: Add support for 3-byte headers in VideoToolbox NALU parser. (Closed)
Patch Set: rebase Created 4 years, 2 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) 2015 The WebRTC project authors. All Rights Reserved. 2 * Copyright (c) 2015 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 11
12 #include "webrtc/modules/video_coding/codecs/h264/h264_video_toolbox_nalu.h" 12 #include "webrtc/modules/video_coding/codecs/h264/h264_video_toolbox_nalu.h"
13 13
14 #if defined(WEBRTC_VIDEO_TOOLBOX_SUPPORTED) 14 #if defined(WEBRTC_VIDEO_TOOLBOX_SUPPORTED)
15 15
16 #include <CoreFoundation/CoreFoundation.h> 16 #include <CoreFoundation/CoreFoundation.h>
17 #include <memory> 17 #include <memory>
18 #include <vector> 18 #include <vector>
19 19
20 #include "webrtc/base/checks.h" 20 #include "webrtc/base/checks.h"
21 #include "webrtc/base/logging.h" 21 #include "webrtc/base/logging.h"
22 #include "webrtc/common_video/h264/h264_common.h"
23 22
24 namespace webrtc { 23 namespace webrtc {
25 24
26 using H264::NaluType;
27 using H264::kAud; 25 using H264::kAud;
28 using H264::kSps; 26 using H264::kSps;
27 using H264::NaluIndex;
28 using H264::NaluType;
29 using H264::ParseNaluType; 29 using H264::ParseNaluType;
30 30
31 const char kAnnexBHeaderBytes[4] = {0, 0, 0, 1}; 31 const char kAnnexBHeaderBytes[4] = {0, 0, 0, 1};
32 const size_t kAvccHeaderByteSize = sizeof(uint32_t); 32 const size_t kAvccHeaderByteSize = sizeof(uint32_t);
33 33
34 bool H264CMSampleBufferToAnnexBBuffer( 34 bool H264CMSampleBufferToAnnexBBuffer(
35 CMSampleBufferRef avcc_sample_buffer, 35 CMSampleBufferRef avcc_sample_buffer,
36 bool is_keyframe, 36 bool is_keyframe,
37 rtc::Buffer* annexb_buffer, 37 rtc::Buffer* annexb_buffer,
38 webrtc::RTPFragmentationHeader** out_header) { 38 webrtc::RTPFragmentationHeader** out_header) {
(...skipping 11 matching lines...) Expand all
50 50
51 // Get parameter set information. 51 // Get parameter set information.
52 int nalu_header_size = 0; 52 int nalu_header_size = 0;
53 size_t param_set_count = 0; 53 size_t param_set_count = 0;
54 OSStatus status = CMVideoFormatDescriptionGetH264ParameterSetAtIndex( 54 OSStatus status = CMVideoFormatDescriptionGetH264ParameterSetAtIndex(
55 description, 0, nullptr, nullptr, &param_set_count, &nalu_header_size); 55 description, 0, nullptr, nullptr, &param_set_count, &nalu_header_size);
56 if (status != noErr) { 56 if (status != noErr) {
57 LOG(LS_ERROR) << "Failed to get parameter set."; 57 LOG(LS_ERROR) << "Failed to get parameter set.";
58 return false; 58 return false;
59 } 59 }
60 // TODO(tkchin): handle other potential sizes. 60 RTC_CHECK_EQ(nalu_header_size, kAvccHeaderByteSize);
61 RTC_DCHECK_EQ(nalu_header_size, 4);
62 RTC_DCHECK_EQ(param_set_count, 2u); 61 RTC_DCHECK_EQ(param_set_count, 2u);
63 62
64 // Truncate any previous data in the buffer without changing its capacity. 63 // Truncate any previous data in the buffer without changing its capacity.
65 annexb_buffer->SetSize(0); 64 annexb_buffer->SetSize(0);
66 65
67 size_t nalu_offset = 0; 66 size_t nalu_offset = 0;
68 std::vector<size_t> frag_offsets; 67 std::vector<size_t> frag_offsets;
69 std::vector<size_t> frag_lengths; 68 std::vector<size_t> frag_lengths;
70 69
71 // Place all parameter sets at the front of buffer. 70 // Place all parameter sets at the front of buffer.
(...skipping 61 matching lines...) Expand 10 before | Expand all | Expand 10 after
133 uint32_t* uint32_data_ptr = reinterpret_cast<uint32_t*>(data_ptr); 132 uint32_t* uint32_data_ptr = reinterpret_cast<uint32_t*>(data_ptr);
134 uint32_t packet_size = CFSwapInt32BigToHost(*uint32_data_ptr); 133 uint32_t packet_size = CFSwapInt32BigToHost(*uint32_data_ptr);
135 // Update buffer. 134 // Update buffer.
136 annexb_buffer->AppendData(kAnnexBHeaderBytes, sizeof(kAnnexBHeaderBytes)); 135 annexb_buffer->AppendData(kAnnexBHeaderBytes, sizeof(kAnnexBHeaderBytes));
137 annexb_buffer->AppendData(data_ptr + nalu_header_size, packet_size); 136 annexb_buffer->AppendData(data_ptr + nalu_header_size, packet_size);
138 // Update fragmentation. 137 // Update fragmentation.
139 frag_offsets.push_back(nalu_offset + sizeof(kAnnexBHeaderBytes)); 138 frag_offsets.push_back(nalu_offset + sizeof(kAnnexBHeaderBytes));
140 frag_lengths.push_back(packet_size); 139 frag_lengths.push_back(packet_size);
141 nalu_offset += sizeof(kAnnexBHeaderBytes) + packet_size; 140 nalu_offset += sizeof(kAnnexBHeaderBytes) + packet_size;
142 141
143 size_t bytes_written = packet_size + nalu_header_size; 142 size_t bytes_written = packet_size + sizeof(kAnnexBHeaderBytes);
144 bytes_remaining -= bytes_written; 143 bytes_remaining -= bytes_written;
145 data_ptr += bytes_written; 144 data_ptr += bytes_written;
146 } 145 }
147 RTC_DCHECK_EQ(bytes_remaining, (size_t)0); 146 RTC_DCHECK_EQ(bytes_remaining, (size_t)0);
148 147
149 std::unique_ptr<webrtc::RTPFragmentationHeader> header; 148 std::unique_ptr<webrtc::RTPFragmentationHeader> header;
150 header.reset(new webrtc::RTPFragmentationHeader()); 149 header.reset(new webrtc::RTPFragmentationHeader());
151 header->VerifyAndAllocateFragmentationHeader(frag_offsets.size()); 150 header->VerifyAndAllocateFragmentationHeader(frag_offsets.size());
152 RTC_DCHECK_EQ(frag_lengths.size(), frag_offsets.size()); 151 RTC_DCHECK_EQ(frag_lengths.size(), frag_offsets.size());
153 for (size_t i = 0; i < frag_offsets.size(); ++i) { 152 for (size_t i = 0; i < frag_offsets.size(); ++i) {
(...skipping 151 matching lines...) Expand 10 before | Expand all | Expand 10 after
305 &description); 304 &description);
306 if (status != noErr) { 305 if (status != noErr) {
307 LOG(LS_ERROR) << "Failed to create video format description."; 306 LOG(LS_ERROR) << "Failed to create video format description.";
308 return nullptr; 307 return nullptr;
309 } 308 }
310 return description; 309 return description;
311 } 310 }
312 311
313 AnnexBBufferReader::AnnexBBufferReader(const uint8_t* annexb_buffer, 312 AnnexBBufferReader::AnnexBBufferReader(const uint8_t* annexb_buffer,
314 size_t length) 313 size_t length)
315 : start_(annexb_buffer), offset_(0), next_offset_(0), length_(length) { 314 : start_(annexb_buffer), length_(length) {
316 RTC_DCHECK(annexb_buffer); 315 RTC_DCHECK(annexb_buffer);
317 offset_ = FindNextNaluHeader(start_, length_, 0); 316 offsets_ = H264::FindNaluIndices(annexb_buffer, length);
318 next_offset_ = 317 offset_ = offsets_.begin();
319 FindNextNaluHeader(start_, length_, offset_ + sizeof(kAnnexBHeaderBytes));
320 } 318 }
321 319
322 bool AnnexBBufferReader::ReadNalu(const uint8_t** out_nalu, 320 bool AnnexBBufferReader::ReadNalu(const uint8_t** out_nalu,
323 size_t* out_length) { 321 size_t* out_length) {
324 RTC_DCHECK(out_nalu); 322 RTC_DCHECK(out_nalu);
325 RTC_DCHECK(out_length); 323 RTC_DCHECK(out_length);
326 *out_nalu = nullptr; 324 *out_nalu = nullptr;
327 *out_length = 0; 325 *out_length = 0;
328 326
329 size_t data_offset = offset_ + sizeof(kAnnexBHeaderBytes); 327 if (offset_ == offsets_.end()) {
330 if (data_offset > length_) {
331 return false; 328 return false;
332 } 329 }
333 *out_nalu = start_ + data_offset; 330 *out_nalu = start_ + offset_->payload_start_offset;
334 *out_length = next_offset_ - data_offset; 331 *out_length = offset_->payload_size;
335 offset_ = next_offset_; 332 ++offset_;
336 next_offset_ =
337 FindNextNaluHeader(start_, length_, offset_ + sizeof(kAnnexBHeaderBytes));
338 return true; 333 return true;
339 } 334 }
340 335
341 size_t AnnexBBufferReader::BytesRemaining() const { 336 size_t AnnexBBufferReader::BytesRemaining() const {
342 return length_ - offset_; 337 if (offset_ == offsets_.end()) {
343 } 338 return 0;
344
345 size_t AnnexBBufferReader::FindNextNaluHeader(const uint8_t* start,
346 size_t length,
347 size_t offset) const {
348 RTC_DCHECK(start);
349 if (offset + sizeof(kAnnexBHeaderBytes) > length) {
350 return length;
351 } 339 }
352 // NALUs are separated by an 00 00 00 01 header. Scan the byte stream 340 return length_ - offset_->start_offset;
353 // starting from the offset for the next such sequence.
354 const uint8_t* current = start + offset;
355 // The loop reads sizeof(kAnnexBHeaderBytes) at a time, so stop when there
356 // aren't enough bytes remaining.
357 const uint8_t* const end = start + length - sizeof(kAnnexBHeaderBytes);
358 while (current < end) {
359 if (current[3] > 1) {
360 current += 4;
361 } else if (current[3] == 1 && current[2] == 0 && current[1] == 0 &&
362 current[0] == 0) {
363 return current - start;
364 } else {
365 ++current;
366 }
367 }
368 return length;
369 } 341 }
370 342
371 AvccBufferWriter::AvccBufferWriter(uint8_t* const avcc_buffer, size_t length) 343 AvccBufferWriter::AvccBufferWriter(uint8_t* const avcc_buffer, size_t length)
372 : start_(avcc_buffer), offset_(0), length_(length) { 344 : start_(avcc_buffer), offset_(0), length_(length) {
373 RTC_DCHECK(avcc_buffer); 345 RTC_DCHECK(avcc_buffer);
374 } 346 }
375 347
376 bool AvccBufferWriter::WriteNalu(const uint8_t* data, size_t data_size) { 348 bool AvccBufferWriter::WriteNalu(const uint8_t* data, size_t data_size) {
377 // Check if we can write this length of data. 349 // Check if we can write this length of data.
378 if (data_size + kAvccHeaderByteSize > BytesRemaining()) { 350 if (data_size + kAvccHeaderByteSize > BytesRemaining()) {
379 return false; 351 return false;
380 } 352 }
381 // Write length header, which needs to be big endian. 353 // Write length header, which needs to be big endian.
382 uint32_t big_endian_length = CFSwapInt32HostToBig(data_size); 354 uint32_t big_endian_length = CFSwapInt32HostToBig(data_size);
383 memcpy(start_ + offset_, &big_endian_length, sizeof(big_endian_length)); 355 memcpy(start_ + offset_, &big_endian_length, sizeof(big_endian_length));
384 offset_ += sizeof(big_endian_length); 356 offset_ += sizeof(big_endian_length);
385 // Write data. 357 // Write data.
386 memcpy(start_ + offset_, data, data_size); 358 memcpy(start_ + offset_, data, data_size);
387 offset_ += data_size; 359 offset_ += data_size;
388 return true; 360 return true;
389 } 361 }
390 362
391 size_t AvccBufferWriter::BytesRemaining() const { 363 size_t AvccBufferWriter::BytesRemaining() const {
392 return length_ - offset_; 364 return length_ - offset_;
393 } 365 }
394 366
395 } // namespace webrtc 367 } // namespace webrtc
396 368
397 #endif // defined(WEBRTC_VIDEO_TOOLBOX_SUPPORTED) 369 #endif // defined(WEBRTC_VIDEO_TOOLBOX_SUPPORTED)
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698